]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Disable HSTR_EL2 traps if EL2 is not enabled
[mirror_qemu.git] / target / arm / helper.c
CommitLineData
ed3baad1
PMD
1/*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
db725815 8
74c21bd0 9#include "qemu/osdep.h"
cd617484 10#include "qemu/log.h"
194cbc49 11#include "trace.h"
b5ff1b31 12#include "cpu.h"
ccd38087 13#include "internals.h"
2ef6175a 14#include "exec/helper-proto.h"
db725815 15#include "qemu/main-loop.h"
b8012ecf 16#include "qemu/timer.h"
1de7afc9 17#include "qemu/bitops.h"
eb0ecd5a 18#include "qemu/crc32c.h"
0442428a 19#include "qemu/qemu-print.h"
63c91552 20#include "exec/exec-all.h"
eb0ecd5a 21#include <zlib.h> /* For crc32 */
64552b6b 22#include "hw/irq.h"
740b1759 23#include "sysemu/cpu-timers.h"
f3a9b694 24#include "sysemu/kvm.h"
7f7b4e7a 25#include "qapi/qapi-commands-machine-target.h"
de390645
RH
26#include "qapi/error.h"
27#include "qemu/guest-random.h"
91f78c58 28#ifdef CONFIG_TCG
6b5fe137 29#include "semihosting/common-semi.h"
91f78c58 30#endif
cf7c6d10 31#include "cpregs.h"
0b03bdfc 32
352c98e5
LV
33#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
34
affdb64d
PM
35static void switch_mode(CPUARMState *env, int mode);
36
c4241c7d 37static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 38{
375421cc 39 assert(ri->fieldoffset);
67ed771d 40 if (cpreg_field_is_64bit(ri)) {
c4241c7d 41 return CPREG_FIELD64(env, ri);
22d9e1a9 42 } else {
c4241c7d 43 return CPREG_FIELD32(env, ri);
22d9e1a9 44 }
d4e6df63
PM
45}
46
f43ee493 47void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
d4e6df63 48{
375421cc 49 assert(ri->fieldoffset);
67ed771d 50 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
51 CPREG_FIELD64(env, ri) = value;
52 } else {
53 CPREG_FIELD32(env, ri) = value;
54 }
d4e6df63
PM
55}
56
11f136ee
FA
57static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
58{
59 return (char *)env + ri->fieldoffset;
60}
61
49a66191 62uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 63{
59a1c327 64 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 65 if (ri->type & ARM_CP_CONST) {
59a1c327 66 return ri->resetvalue;
721fae12 67 } else if (ri->raw_readfn) {
59a1c327 68 return ri->raw_readfn(env, ri);
721fae12 69 } else if (ri->readfn) {
59a1c327 70 return ri->readfn(env, ri);
721fae12 71 } else {
59a1c327 72 return raw_read(env, ri);
721fae12 73 }
721fae12
PM
74}
75
59a1c327 76static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 77 uint64_t v)
721fae12 78{
9b37a28c
FR
79 /*
80 * Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
81 * Note that constant registers are treated as write-ignored; the
82 * caller should check for success by whether a readback gives the
83 * value written.
84 */
85 if (ri->type & ARM_CP_CONST) {
59a1c327 86 return;
721fae12 87 } else if (ri->raw_writefn) {
c4241c7d 88 ri->raw_writefn(env, ri, v);
721fae12 89 } else if (ri->writefn) {
c4241c7d 90 ri->writefn(env, ri, v);
721fae12 91 } else {
afb2530f 92 raw_write(env, ri, v);
721fae12 93 }
721fae12
PM
94}
95
375421cc
PM
96static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
97{
9b37a28c
FR
98 /*
99 * Return true if the regdef would cause an assertion if you called
375421cc
PM
100 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
101 * program bug for it not to have the NO_RAW flag).
102 * NB that returning false here doesn't necessarily mean that calling
103 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
104 * read/write access functions which are safe for raw use" from "has
105 * read/write access functions which have side effects but has forgotten
106 * to provide raw access functions".
107 * The tests here line up with the conditions in read/write_raw_cp_reg()
108 * and assertions in raw_read()/raw_write().
109 */
110 if ((ri->type & ARM_CP_CONST) ||
111 ri->fieldoffset ||
112 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
113 return false;
114 }
115 return true;
116}
117
b698e4ee 118bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
119{
120 /* Write the coprocessor state from cpu->env to the (index,value) list. */
121 int i;
122 bool ok = true;
123
124 for (i = 0; i < cpu->cpreg_array_len; i++) {
125 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
126 const ARMCPRegInfo *ri;
b698e4ee 127 uint64_t newval;
59a1c327 128
60322b39 129 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
130 if (!ri) {
131 ok = false;
132 continue;
133 }
7a0e58fa 134 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
135 continue;
136 }
b698e4ee
PM
137
138 newval = read_raw_cp_reg(&cpu->env, ri);
139 if (kvm_sync) {
140 /*
141 * Only sync if the previous list->cpustate sync succeeded.
142 * Rather than tracking the success/failure state for every
143 * item in the list, we just recheck "does the raw write we must
144 * have made in write_list_to_cpustate() read back OK" here.
145 */
146 uint64_t oldval = cpu->cpreg_values[i];
147
148 if (oldval == newval) {
149 continue;
150 }
151
152 write_raw_cp_reg(&cpu->env, ri, oldval);
153 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
154 continue;
155 }
156
157 write_raw_cp_reg(&cpu->env, ri, newval);
158 }
159 cpu->cpreg_values[i] = newval;
721fae12
PM
160 }
161 return ok;
162}
163
164bool write_list_to_cpustate(ARMCPU *cpu)
165{
166 int i;
167 bool ok = true;
168
169 for (i = 0; i < cpu->cpreg_array_len; i++) {
170 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
171 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
172 const ARMCPRegInfo *ri;
173
60322b39 174 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
175 if (!ri) {
176 ok = false;
177 continue;
178 }
7a0e58fa 179 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
180 continue;
181 }
9b37a28c
FR
182 /*
183 * Write value and confirm it reads back as written
721fae12
PM
184 * (to catch read-only registers and partially read-only
185 * registers where the incoming migration value doesn't match)
186 */
59a1c327
PM
187 write_raw_cp_reg(&cpu->env, ri, v);
188 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
189 ok = false;
190 }
191 }
192 return ok;
193}
194
195static void add_cpreg_to_list(gpointer key, gpointer opaque)
196{
197 ARMCPU *cpu = opaque;
5860362d
RH
198 uint32_t regidx = (uintptr_t)key;
199 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 200
04215eb1 201 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
202 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
203 /* The value array need not be initialized at this point */
204 cpu->cpreg_array_len++;
205 }
206}
207
208static void count_cpreg(gpointer key, gpointer opaque)
209{
210 ARMCPU *cpu = opaque;
721fae12
PM
211 const ARMCPRegInfo *ri;
212
5860362d 213 ri = g_hash_table_lookup(cpu->cp_regs, key);
721fae12 214
04215eb1 215 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
216 cpu->cpreg_array_len++;
217 }
218}
219
220static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
221{
5860362d
RH
222 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
223 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
721fae12 224
cbf239b7
AR
225 if (aidx > bidx) {
226 return 1;
227 }
228 if (aidx < bidx) {
229 return -1;
230 }
231 return 0;
721fae12
PM
232}
233
234void init_cpreg_list(ARMCPU *cpu)
235{
9b37a28c
FR
236 /*
237 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
721fae12
PM
238 * Note that we require cpreg_tuples[] to be sorted by key ID.
239 */
57b6d95e 240 GList *keys;
721fae12
PM
241 int arraylen;
242
57b6d95e 243 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
244 keys = g_list_sort(keys, cpreg_key_compare);
245
246 cpu->cpreg_array_len = 0;
247
248 g_list_foreach(keys, count_cpreg, cpu);
249
250 arraylen = cpu->cpreg_array_len;
251 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
252 cpu->cpreg_values = g_new(uint64_t, arraylen);
253 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
254 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
255 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
256 cpu->cpreg_array_len = 0;
257
258 g_list_foreach(keys, add_cpreg_to_list, cpu);
259
260 assert(cpu->cpreg_array_len == arraylen);
261
262 g_list_free(keys);
263}
264
68e9c2fe 265/*
93dd1e61 266 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
267 */
268static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
269 const ARMCPRegInfo *ri,
270 bool isread)
68e9c2fe 271{
93dd1e61
EI
272 if (!is_a64(env) && arm_current_el(env) == 3 &&
273 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
274 return CP_ACCESS_TRAP_UNCATEGORIZED;
275 }
276 return CP_ACCESS_OK;
277}
278
9b37a28c
FR
279/*
280 * Some secure-only AArch32 registers trap to EL3 if used from
5513c3ab
PM
281 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
282 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
283 * We assume that the .access field is set to PL1_RW.
284 */
285static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
286 const ARMCPRegInfo *ri,
287 bool isread)
5513c3ab
PM
288{
289 if (arm_current_el(env) == 3) {
290 return CP_ACCESS_OK;
291 }
292 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
293 if (env->cp15.scr_el3 & SCR_EEL2) {
294 return CP_ACCESS_TRAP_EL2;
295 }
5513c3ab
PM
296 return CP_ACCESS_TRAP_EL3;
297 }
298 /* This will be EL1 NS and EL2 NS, which just UNDEF */
299 return CP_ACCESS_TRAP_UNCATEGORIZED;
300}
301
9b37a28c
FR
302/*
303 * Check for traps to performance monitor registers, which are controlled
1fce1ba9
PM
304 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
305 */
306static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
307 bool isread)
308{
309 int el = arm_current_el(env);
59dd089c 310 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 311
59dd089c 312 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
313 return CP_ACCESS_TRAP_EL2;
314 }
315 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
316 return CP_ACCESS_TRAP_EL3;
317 }
318 return CP_ACCESS_OK;
319}
320
84929218
RH
321/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
322static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
323 bool isread)
324{
325 if (arm_current_el(env) == 1) {
326 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
327 if (arm_hcr_el2_eff(env) & trap) {
328 return CP_ACCESS_TRAP_EL2;
329 }
330 }
331 return CP_ACCESS_OK;
332}
333
1803d271
RH
334/* Check for traps from EL1 due to HCR_EL2.TSW. */
335static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
336 bool isread)
337{
338 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
339 return CP_ACCESS_TRAP_EL2;
340 }
341 return CP_ACCESS_OK;
342}
343
99602377
RH
344/* Check for traps from EL1 due to HCR_EL2.TACR. */
345static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
346 bool isread)
347{
348 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
349 return CP_ACCESS_TRAP_EL2;
350 }
351 return CP_ACCESS_OK;
352}
353
30881b73
RH
354/* Check for traps from EL1 due to HCR_EL2.TTLB. */
355static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
356 bool isread)
357{
358 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
359 return CP_ACCESS_TRAP_EL2;
360 }
361 return CP_ACCESS_OK;
362}
363
0f66d223
PM
364/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
365static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
366 bool isread)
367{
368 if (arm_current_el(env) == 1 &&
369 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
370 return CP_ACCESS_TRAP_EL2;
371 }
372 return CP_ACCESS_OK;
373}
374
fe3ca86c
PM
375#ifdef TARGET_AARCH64
376/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
377static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
378 bool isread)
379{
380 if (arm_current_el(env) == 1 &&
381 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
382 return CP_ACCESS_TRAP_EL2;
383 }
384 return CP_ACCESS_OK;
385}
386#endif
387
c4241c7d 388static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 389{
2fc0cc0e 390 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 391
8d5c773e 392 raw_write(env, ri, value);
d10eb08f 393 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
394}
395
c4241c7d 396static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 397{
2fc0cc0e 398 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 399
8d5c773e 400 if (raw_read(env, ri) != value) {
9b37a28c
FR
401 /*
402 * Unlike real hardware the qemu TLB uses virtual addresses,
08de207b
PM
403 * not modified virtual addresses, so this causes a TLB flush.
404 */
d10eb08f 405 tlb_flush(CPU(cpu));
8d5c773e 406 raw_write(env, ri, value);
08de207b 407 }
08de207b 408}
c4241c7d
PM
409
410static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
411 uint64_t value)
08de207b 412{
2fc0cc0e 413 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 414
452a0955 415 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 416 && !extended_addresses_enabled(env)) {
9b37a28c
FR
417 /*
418 * For VMSA (when not using the LPAE long descriptor page table
08de207b
PM
419 * format) this register includes the ASID, so do a TLB flush.
420 * For PMSA it is purely a process ID and no action is needed.
421 */
d10eb08f 422 tlb_flush(CPU(cpu));
08de207b 423 }
8d5c773e 424 raw_write(env, ri, value);
08de207b
PM
425}
426
575a94af
RH
427static int alle1_tlbmask(CPUARMState *env)
428{
429 /*
430 * Note that the 'ALL' scope must invalidate both stage 1 and
431 * stage 2 translations, whereas most other scopes only invalidate
432 * stage 1 translations.
433 */
434 return (ARMMMUIdxBit_E10_1 |
435 ARMMMUIdxBit_E10_1_PAN |
436 ARMMMUIdxBit_E10_0 |
437 ARMMMUIdxBit_Stage2 |
438 ARMMMUIdxBit_Stage2_S);
439}
440
441
b4ab8ce9
PM
442/* IS variants of TLB operations must affect all cores */
443static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
444 uint64_t value)
445{
29a0af61 446 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
447
448 tlb_flush_all_cpus_synced(cs);
449}
450
451static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
452 uint64_t value)
453{
29a0af61 454 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
455
456 tlb_flush_all_cpus_synced(cs);
457}
458
459static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
460 uint64_t value)
461{
29a0af61 462 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
463
464 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
465}
466
467static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
468 uint64_t value)
469{
29a0af61 470 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
471
472 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
473}
474
475/*
476 * Non-IS variants of TLB operations are upgraded to
373e7ffd 477 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
478 * force broadcast of these operations.
479 */
480static bool tlb_force_broadcast(CPUARMState *env)
481{
373e7ffd 482 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
483}
484
c4241c7d
PM
485static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
486 uint64_t value)
d929823f
PM
487{
488 /* Invalidate all (TLBIALL) */
527db2be 489 CPUState *cs = env_cpu(env);
00c8cb0a 490
b4ab8ce9 491 if (tlb_force_broadcast(env)) {
527db2be
RH
492 tlb_flush_all_cpus_synced(cs);
493 } else {
494 tlb_flush(cs);
b4ab8ce9 495 }
d929823f
PM
496}
497
c4241c7d
PM
498static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
499 uint64_t value)
d929823f
PM
500{
501 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 502 CPUState *cs = env_cpu(env);
31b030d4 503
527db2be 504 value &= TARGET_PAGE_MASK;
b4ab8ce9 505 if (tlb_force_broadcast(env)) {
527db2be
RH
506 tlb_flush_page_all_cpus_synced(cs, value);
507 } else {
508 tlb_flush_page(cs, value);
b4ab8ce9 509 }
d929823f
PM
510}
511
c4241c7d
PM
512static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 uint64_t value)
d929823f
PM
514{
515 /* Invalidate by ASID (TLBIASID) */
527db2be 516 CPUState *cs = env_cpu(env);
00c8cb0a 517
b4ab8ce9 518 if (tlb_force_broadcast(env)) {
527db2be
RH
519 tlb_flush_all_cpus_synced(cs);
520 } else {
521 tlb_flush(cs);
b4ab8ce9 522 }
d929823f
PM
523}
524
c4241c7d
PM
525static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
526 uint64_t value)
d929823f
PM
527{
528 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 529 CPUState *cs = env_cpu(env);
31b030d4 530
527db2be 531 value &= TARGET_PAGE_MASK;
b4ab8ce9 532 if (tlb_force_broadcast(env)) {
527db2be
RH
533 tlb_flush_page_all_cpus_synced(cs, value);
534 } else {
535 tlb_flush_page(cs, value);
b4ab8ce9 536 }
fa439fc5
PM
537}
538
541ef8c2
SS
539static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
540 uint64_t value)
541{
29a0af61 542 CPUState *cs = env_cpu(env);
541ef8c2 543
575a94af 544 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
541ef8c2
SS
545}
546
547static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548 uint64_t value)
549{
29a0af61 550 CPUState *cs = env_cpu(env);
541ef8c2 551
575a94af 552 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
541ef8c2
SS
553}
554
541ef8c2
SS
555
556static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
557 uint64_t value)
558{
29a0af61 559 CPUState *cs = env_cpu(env);
541ef8c2 560
e013b741 561 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
562}
563
564static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566{
29a0af61 567 CPUState *cs = env_cpu(env);
541ef8c2 568
e013b741 569 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
570}
571
572static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
573 uint64_t value)
574{
29a0af61 575 CPUState *cs = env_cpu(env);
541ef8c2
SS
576 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
577
e013b741 578 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
579}
580
581static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
582 uint64_t value)
583{
29a0af61 584 CPUState *cs = env_cpu(env);
541ef8c2
SS
585 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
586
a67cf277 587 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 588 ARMMMUIdxBit_E2);
541ef8c2
SS
589}
590
575a94af
RH
591static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
592 uint64_t value)
593{
594 CPUState *cs = env_cpu(env);
595 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
596
597 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
598}
599
600static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
601 uint64_t value)
602{
603 CPUState *cs = env_cpu(env);
604 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
605
606 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
607}
608
e9aa6c21 609static const ARMCPRegInfo cp_reginfo[] = {
9b37a28c
FR
610 /*
611 * Define the secure and non-secure FCSE identifier CP registers
54bf36ed
FA
612 * separately because there is no secure bank in V8 (no _EL3). This allows
613 * the secure register to be properly reset and migrated. There is also no
614 * v8 EL1 version of the register so the non-secure instance stands alone.
615 */
9c513e78 616 { .name = "FCSEIDR",
54bf36ed
FA
617 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
618 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
619 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
620 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 621 { .name = "FCSEIDR_S",
54bf36ed
FA
622 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
623 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
624 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 625 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9b37a28c
FR
626 /*
627 * Define the secure and non-secure context identifier CP registers
54bf36ed
FA
628 * separately because there is no secure bank in V8 (no _EL3). This allows
629 * the secure register to be properly reset and migrated. In the
630 * non-secure case, the 32-bit register will have reset and migration
631 * disabled during registration as it is handled by the 64-bit instance.
632 */
633 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 634 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
635 .access = PL1_RW, .accessfn = access_tvm_trvm,
636 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
637 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
638 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 639 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 640 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
641 .access = PL1_RW, .accessfn = access_tvm_trvm,
642 .secure = ARM_CP_SECSTATE_S,
54bf36ed 643 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 644 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
645};
646
647static const ARMCPRegInfo not_v8_cp_reginfo[] = {
9b37a28c
FR
648 /*
649 * NB: Some of these registers exist in v8 but with more precise
9449fdf6
PM
650 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
651 */
652 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
653 { .name = "DACR",
654 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 655 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
656 .writefn = dacr_write, .raw_writefn = raw_write,
657 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
658 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
9b37a28c
FR
659 /*
660 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
a903c449 661 * For v6 and v5, these mappings are overly broad.
4fdd17dd 662 */
a903c449
EI
663 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
664 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
665 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
666 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
667 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
668 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
669 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 670 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
671 /* Cache maintenance ops; some of this space may be overridden later. */
672 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
673 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
674 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
675};
676
7d57f408 677static const ARMCPRegInfo not_v6_cp_reginfo[] = {
9b37a28c
FR
678 /*
679 * Not all pre-v6 cores implemented this WFI, so this is slightly
7d57f408
PM
680 * over-broad.
681 */
682 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
683 .access = PL1_W, .type = ARM_CP_WFI },
7d57f408
PM
684};
685
686static const ARMCPRegInfo not_v7_cp_reginfo[] = {
9b37a28c
FR
687 /*
688 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
7d57f408
PM
689 * is UNPREDICTABLE; we choose to NOP as most implementations do).
690 */
691 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
692 .access = PL1_W, .type = ARM_CP_WFI },
9b37a28c
FR
693 /*
694 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
34f90529
PM
695 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
696 * OMAPCP will override this space.
697 */
698 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
699 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
700 .resetvalue = 0 },
701 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
702 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
703 .resetvalue = 0 },
776d4e5c
PM
704 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
705 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 706 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 707 .resetvalue = 0 },
9b37a28c
FR
708 /*
709 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
50300698
PM
710 * implementing it as RAZ means the "debug architecture version" bits
711 * will read as a reserved value, which should cause Linux to not try
712 * to use the debug hardware.
713 */
714 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
715 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
716 /*
717 * MMU TLB control. Note that the wildcarding means we cover not just
995939a6
PM
718 * the unified TLB ops but also the dside/iside/inner-shareable variants.
719 */
720 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
721 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 722 .type = ARM_CP_NO_RAW },
995939a6
PM
723 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
724 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 725 .type = ARM_CP_NO_RAW },
995939a6
PM
726 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
727 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 728 .type = ARM_CP_NO_RAW },
995939a6
PM
729 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
730 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 731 .type = ARM_CP_NO_RAW },
a903c449
EI
732 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
733 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
734 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
735 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
736};
737
c4241c7d
PM
738static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
739 uint64_t value)
2771db27 740{
f0aff255
FA
741 uint32_t mask = 0;
742
743 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
744 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
745 /*
746 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
f0aff255
FA
747 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
748 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
749 */
7fbc6a40 750 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255 751 /* VFP coprocessor: cp10 & cp11 [23:20] */
fab8ad39
RH
752 mask |= R_CPACR_ASEDIS_MASK |
753 R_CPACR_D32DIS_MASK |
754 R_CPACR_CP11_MASK |
755 R_CPACR_CP10_MASK;
f0aff255
FA
756
757 if (!arm_feature(env, ARM_FEATURE_NEON)) {
758 /* ASEDIS [31] bit is RAO/WI */
fab8ad39 759 value |= R_CPACR_ASEDIS_MASK;
f0aff255
FA
760 }
761
9b37a28c
FR
762 /*
763 * VFPv3 and upwards with NEON implement 32 double precision
f0aff255
FA
764 * registers (D0-D31).
765 */
a6627f5f 766 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255 767 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
fab8ad39 768 value |= R_CPACR_D32DIS_MASK;
f0aff255
FA
769 }
770 }
771 value &= mask;
2771db27 772 }
fc1120a7
PM
773
774 /*
775 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
776 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
777 */
778 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
779 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
780 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
781 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
fc1120a7
PM
782 }
783
7ebd5f2e 784 env->cp15.cpacr_el1 = value;
2771db27
PM
785}
786
fc1120a7
PM
787static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
788{
789 /*
790 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
791 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
792 */
793 uint64_t value = env->cp15.cpacr_el1;
794
795 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
796 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 797 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
fc1120a7
PM
798 }
799 return value;
800}
801
802
5deac39c
PM
803static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
804{
9b37a28c
FR
805 /*
806 * Call cpacr_write() so that we reset with the correct RAO bits set
5deac39c
PM
807 * for our CPU features.
808 */
809 cpacr_write(env, ri, 0);
810}
811
3f208fd7
PM
812static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
813 bool isread)
c6f19164
GB
814{
815 if (arm_feature(env, ARM_FEATURE_V8)) {
816 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169 817 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
fab8ad39 818 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
c6f19164
GB
819 return CP_ACCESS_TRAP_EL2;
820 /* Check if CPACR accesses are to be trapped to EL3 */
821 } else if (arm_current_el(env) < 3 &&
fab8ad39 822 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
823 return CP_ACCESS_TRAP_EL3;
824 }
825 }
826
827 return CP_ACCESS_OK;
828}
829
3f208fd7
PM
830static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
831 bool isread)
c6f19164
GB
832{
833 /* Check if CPTR accesses are set to trap to EL3 */
fab8ad39
RH
834 if (arm_current_el(env) == 2 &&
835 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
836 return CP_ACCESS_TRAP_EL3;
837 }
838
839 return CP_ACCESS_OK;
840}
841
7d57f408
PM
842static const ARMCPRegInfo v6_cp_reginfo[] = {
843 /* prefetch by MVA in v6, NOP in v7 */
844 { .name = "MVA_prefetch",
845 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
846 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
847 /*
848 * We need to break the TB after ISB to execute self-modifying code
6df99dec
SS
849 * correctly and also to take any pending interrupts immediately.
850 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
851 */
7d57f408 852 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 853 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 854 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 855 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 856 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 857 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 858 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 859 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
860 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
861 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31 862 .resetvalue = 0, },
9b37a28c
FR
863 /*
864 * Watchpoint Fault Address Register : should actually only be present
06d76f31
PM
865 * for 1136, 1176, 11MPCore.
866 */
867 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
868 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 869 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 870 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 871 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 872 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
873};
874
57a4a11b
AL
875typedef struct pm_event {
876 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
877 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
878 bool (*supported)(CPUARMState *);
879 /*
880 * Retrieve the current count of the underlying event. The programmed
881 * counters hold a difference from the return value from this function
882 */
883 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
884 /*
885 * Return how many nanoseconds it will take (at a minimum) for count events
886 * to occur. A negative value indicates the counter will never overflow, or
887 * that the counter has otherwise arranged for the overflow bit to be set
888 * and the PMU interrupt to be raised on overflow.
889 */
890 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
891} pm_event;
892
b2e23725
AL
893static bool event_always_supported(CPUARMState *env)
894{
895 return true;
896}
897
0d4bfd7d
AL
898static uint64_t swinc_get_count(CPUARMState *env)
899{
900 /*
901 * SW_INCR events are written directly to the pmevcntr's by writes to
902 * PMSWINC, so there is no underlying count maintained by the PMU itself
903 */
904 return 0;
905}
906
4e7beb0c
AL
907static int64_t swinc_ns_per(uint64_t ignored)
908{
909 return -1;
910}
911
b2e23725
AL
912/*
913 * Return the underlying cycle count for the PMU cycle counters. If we're in
914 * usermode, simply return 0.
915 */
916static uint64_t cycles_get_count(CPUARMState *env)
917{
918#ifndef CONFIG_USER_ONLY
919 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
920 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
921#else
922 return cpu_get_host_ticks();
923#endif
924}
925
926#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
927static int64_t cycles_ns_per(uint64_t cycles)
928{
929 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
930}
931
b2e23725
AL
932static bool instructions_supported(CPUARMState *env)
933{
740b1759 934 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
935}
936
937static uint64_t instructions_get_count(CPUARMState *env)
938{
8191d368 939 return (uint64_t)icount_get_raw();
b2e23725 940}
4e7beb0c
AL
941
942static int64_t instructions_ns_per(uint64_t icount)
943{
8191d368 944 return icount_to_ns((int64_t)icount);
4e7beb0c 945}
b2e23725
AL
946#endif
947
a793bcd0 948static bool pmuv3p1_events_supported(CPUARMState *env)
0727f63b
PM
949{
950 /* For events which are supported in any v8.1 PMU */
a793bcd0 951 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
0727f63b
PM
952}
953
a793bcd0 954static bool pmuv3p4_events_supported(CPUARMState *env)
15dd1ebd
PM
955{
956 /* For events which are supported in any v8.1 PMU */
a793bcd0 957 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
15dd1ebd
PM
958}
959
0727f63b
PM
960static uint64_t zero_event_get_count(CPUARMState *env)
961{
962 /* For events which on QEMU never fire, so their count is always zero */
963 return 0;
964}
965
966static int64_t zero_event_ns_per(uint64_t cycles)
967{
968 /* An event which never fires can never overflow */
969 return -1;
970}
971
57a4a11b 972static const pm_event pm_events[] = {
0d4bfd7d
AL
973 { .number = 0x000, /* SW_INCR */
974 .supported = event_always_supported,
975 .get_count = swinc_get_count,
4e7beb0c 976 .ns_per_count = swinc_ns_per,
0d4bfd7d 977 },
b2e23725
AL
978#ifndef CONFIG_USER_ONLY
979 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
980 .supported = instructions_supported,
981 .get_count = instructions_get_count,
4e7beb0c 982 .ns_per_count = instructions_ns_per,
b2e23725
AL
983 },
984 { .number = 0x011, /* CPU_CYCLES, Cycle */
985 .supported = event_always_supported,
986 .get_count = cycles_get_count,
4e7beb0c 987 .ns_per_count = cycles_ns_per,
0727f63b 988 },
b2e23725 989#endif
0727f63b 990 { .number = 0x023, /* STALL_FRONTEND */
a793bcd0 991 .supported = pmuv3p1_events_supported,
0727f63b
PM
992 .get_count = zero_event_get_count,
993 .ns_per_count = zero_event_ns_per,
994 },
995 { .number = 0x024, /* STALL_BACKEND */
a793bcd0 996 .supported = pmuv3p1_events_supported,
0727f63b
PM
997 .get_count = zero_event_get_count,
998 .ns_per_count = zero_event_ns_per,
999 },
15dd1ebd 1000 { .number = 0x03c, /* STALL */
a793bcd0 1001 .supported = pmuv3p4_events_supported,
15dd1ebd
PM
1002 .get_count = zero_event_get_count,
1003 .ns_per_count = zero_event_ns_per,
1004 },
57a4a11b
AL
1005};
1006
1007/*
1008 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1009 * events (i.e. the statistical profiling extension), this implementation
1010 * should first be updated to something sparse instead of the current
1011 * supported_event_map[] array.
1012 */
15dd1ebd 1013#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1014#define UNSUPPORTED_EVENT UINT16_MAX
1015static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1016
1017/*
bf8d0969
AL
1018 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1019 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1020 *
1021 * Note: Events in the 0x40XX range are not currently supported.
1022 */
bf8d0969 1023void pmu_init(ARMCPU *cpu)
57a4a11b 1024{
57a4a11b
AL
1025 unsigned int i;
1026
bf8d0969
AL
1027 /*
1028 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1029 * events to them
1030 */
57a4a11b
AL
1031 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1032 supported_event_map[i] = UNSUPPORTED_EVENT;
1033 }
bf8d0969
AL
1034 cpu->pmceid0 = 0;
1035 cpu->pmceid1 = 0;
57a4a11b
AL
1036
1037 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1038 const pm_event *cnt = &pm_events[i];
1039 assert(cnt->number <= MAX_EVENT_ID);
1040 /* We do not currently support events in the 0x40xx range */
1041 assert(cnt->number <= 0x3f);
1042
bf8d0969 1043 if (cnt->supported(&cpu->env)) {
57a4a11b 1044 supported_event_map[cnt->number] = i;
67da43d6 1045 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1046 if (cnt->number & 0x20) {
1047 cpu->pmceid1 |= event_mask;
1048 } else {
1049 cpu->pmceid0 |= event_mask;
1050 }
57a4a11b
AL
1051 }
1052 }
57a4a11b
AL
1053}
1054
5ecdd3e4
AL
1055/*
1056 * Check at runtime whether a PMU event is supported for the current machine
1057 */
1058static bool event_supported(uint16_t number)
1059{
1060 if (number > MAX_EVENT_ID) {
1061 return false;
1062 }
1063 return supported_event_map[number] != UNSUPPORTED_EVENT;
1064}
1065
3f208fd7
PM
1066static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1067 bool isread)
200ac0ef 1068{
9b37a28c
FR
1069 /*
1070 * Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1071 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1072 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1073 */
1fce1ba9 1074 int el = arm_current_el(env);
59dd089c 1075 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1076
6ecd0b6b 1077 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1078 return CP_ACCESS_TRAP;
200ac0ef 1079 }
59dd089c 1080 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1081 return CP_ACCESS_TRAP_EL2;
1082 }
1083 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1084 return CP_ACCESS_TRAP_EL3;
1085 }
1086
fcd25206 1087 return CP_ACCESS_OK;
200ac0ef
PM
1088}
1089
6ecd0b6b
AB
1090static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1091 const ARMCPRegInfo *ri,
1092 bool isread)
1093{
1094 /* ER: event counter read trap control */
1095 if (arm_feature(env, ARM_FEATURE_V8)
1096 && arm_current_el(env) == 0
1097 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1098 && isread) {
1099 return CP_ACCESS_OK;
1100 }
1101
1102 return pmreg_access(env, ri, isread);
1103}
1104
1105static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1106 const ARMCPRegInfo *ri,
1107 bool isread)
1108{
1109 /* SW: software increment write trap control */
1110 if (arm_feature(env, ARM_FEATURE_V8)
1111 && arm_current_el(env) == 0
1112 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1113 && !isread) {
1114 return CP_ACCESS_OK;
1115 }
1116
1117 return pmreg_access(env, ri, isread);
1118}
1119
6ecd0b6b
AB
1120static CPAccessResult pmreg_access_selr(CPUARMState *env,
1121 const ARMCPRegInfo *ri,
1122 bool isread)
1123{
1124 /* ER: event counter read trap control */
1125 if (arm_feature(env, ARM_FEATURE_V8)
1126 && arm_current_el(env) == 0
1127 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1128 return CP_ACCESS_OK;
1129 }
1130
1131 return pmreg_access(env, ri, isread);
1132}
1133
1134static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1135 const ARMCPRegInfo *ri,
1136 bool isread)
1137{
1138 /* CR: cycle counter read trap control */
1139 if (arm_feature(env, ARM_FEATURE_V8)
1140 && arm_current_el(env) == 0
1141 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1142 && isread) {
1143 return CP_ACCESS_OK;
1144 }
1145
1146 return pmreg_access(env, ri, isread);
1147}
1148
01765386
PM
1149/*
1150 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1151 * We use these to decide whether we need to wrap a write to MDCR_EL2
1152 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1153 */
47b385da
PM
1154#define MDCR_EL2_PMU_ENABLE_BITS \
1155 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
0b42f4fa 1156#define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
01765386 1157
9b37a28c
FR
1158/*
1159 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
033614c4
AL
1160 * the current EL, security state, and register configuration.
1161 */
1162static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1163{
033614c4
AL
1164 uint64_t filter;
1165 bool e, p, u, nsk, nsu, nsh, m;
872d2034 1166 bool enabled, prohibited = false, filtered;
033614c4
AL
1167 bool secure = arm_is_secure(env);
1168 int el = arm_current_el(env);
59dd089c
RDC
1169 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1170 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1171
cbbb3041
AJ
1172 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1173 return false;
1174 }
1175
033614c4
AL
1176 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1177 (counter < hpmn || counter == 31)) {
1178 e = env->cp15.c9_pmcr & PMCRE;
1179 } else {
59dd089c 1180 e = mdcr_el2 & MDCR_HPME;
87124fde 1181 }
033614c4 1182 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1183
872d2034
PM
1184 /* Is event counting prohibited? */
1185 if (el == 2 && (counter < hpmn || counter == 31)) {
1186 prohibited = mdcr_el2 & MDCR_HPMD;
1187 }
1188 if (secure) {
1189 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1190 }
1191
0b42f4fa
PM
1192 if (counter == 31) {
1193 /*
1194 * The cycle counter defaults to running. PMCR.DP says "disable
1195 * the cycle counter when event counting is prohibited".
1196 * Some MDCR bits disable the cycle counter specifically.
1197 */
1198 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1199 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1200 if (secure) {
1201 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1202 }
1203 if (el == 2) {
1204 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1205 }
1206 }
033614c4
AL
1207 }
1208
5ecdd3e4
AL
1209 if (counter == 31) {
1210 filter = env->cp15.pmccfiltr_el0;
1211 } else {
1212 filter = env->cp15.c14_pmevtyper[counter];
1213 }
033614c4
AL
1214
1215 p = filter & PMXEVTYPER_P;
1216 u = filter & PMXEVTYPER_U;
1217 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1218 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1219 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1220 m = arm_el_is_aa64(env, 1) &&
1221 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1222
1223 if (el == 0) {
1224 filtered = secure ? u : u != nsu;
1225 } else if (el == 1) {
1226 filtered = secure ? p : p != nsk;
1227 } else if (el == 2) {
1228 filtered = !nsh;
1229 } else { /* EL3 */
1230 filtered = m != p;
1231 }
1232
5ecdd3e4
AL
1233 if (counter != 31) {
1234 /*
1235 * If not checking PMCCNTR, ensure the counter is setup to an event we
1236 * support
1237 */
1238 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1239 if (!event_supported(event)) {
1240 return false;
1241 }
1242 }
1243
033614c4 1244 return enabled && !prohibited && !filtered;
87124fde 1245}
033614c4 1246
f4efb4b2
AL
1247static void pmu_update_irq(CPUARMState *env)
1248{
2fc0cc0e 1249 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1250 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1251 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1252}
1253
b57aa7bd
PM
1254static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1255{
1256 /*
1257 * Return true if the clock divider is enabled and the cycle counter
1258 * is supposed to tick only once every 64 clock cycles. This is
1259 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1260 * (64-bit) cycle counter PMCR.D has no effect.
1261 */
1262 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1263}
1264
47b385da
PM
1265static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1266{
1267 /* Return true if the specified event counter is configured to be 64 bit */
1268
1269 /* This isn't intended to be used with the cycle counter */
1270 assert(counter < 31);
1271
1272 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1273 return false;
1274 }
1275
1276 if (arm_feature(env, ARM_FEATURE_EL2)) {
1277 /*
1278 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1279 * current security state, so we don't use arm_mdcr_el2_eff() here.
1280 */
1281 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1282 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1283
1284 if (hpmn != 0 && counter >= hpmn) {
1285 return hlp;
1286 }
1287 }
1288 return env->cp15.c9_pmcr & PMCRLP;
1289}
1290
5d05b9d4
AL
1291/*
1292 * Ensure c15_ccnt is the guest-visible count so that operations such as
1293 * enabling/disabling the counter or filtering, modifying the count itself,
1294 * etc. can be done logically. This is essentially a no-op if the counter is
1295 * not enabled at the time of the call.
1296 */
f2b2f53f 1297static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1298{
b2e23725 1299 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1300
033614c4 1301 if (pmu_counter_enabled(env, 31)) {
5d05b9d4 1302 uint64_t eff_cycles = cycles;
b57aa7bd 1303 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1304 eff_cycles /= 64;
1305 }
1306
f4efb4b2
AL
1307 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1308
1309 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1310 1ull << 63 : 1ull << 31;
1311 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
76e25d41 1312 env->cp15.c9_pmovsr |= (1ULL << 31);
f4efb4b2
AL
1313 pmu_update_irq(env);
1314 }
1315
1316 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1317 }
5d05b9d4
AL
1318 env->cp15.c15_ccnt_delta = cycles;
1319}
ec7b4ce4 1320
5d05b9d4
AL
1321/*
1322 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1323 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1324 * pmccntr_op_start.
1325 */
f2b2f53f 1326static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1327{
033614c4 1328 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1329#ifndef CONFIG_USER_ONLY
1330 /* Calculate when the counter will next overflow */
1331 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1332 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1333 remaining_cycles = (uint32_t)remaining_cycles;
1334 }
1335 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1336
1337 if (overflow_in > 0) {
f1dd2506
PM
1338 int64_t overflow_at;
1339
1340 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1341 overflow_in, &overflow_at)) {
1342 ARMCPU *cpu = env_archcpu(env);
1343 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1344 }
4e7beb0c
AL
1345 }
1346#endif
5d05b9d4 1347
4e7beb0c 1348 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
b57aa7bd 1349 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1350 prev_cycles /= 64;
1351 }
5d05b9d4 1352 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1353 }
1354}
1355
5ecdd3e4
AL
1356static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1357{
1358
1359 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1360 uint64_t count = 0;
1361 if (event_supported(event)) {
1362 uint16_t event_idx = supported_event_map[event];
1363 count = pm_events[event_idx].get_count(env);
1364 }
1365
1366 if (pmu_counter_enabled(env, counter)) {
47b385da
PM
1367 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1368 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1369 1ULL << 63 : 1ULL << 31;
f4efb4b2 1370
47b385da 1371 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
f4efb4b2
AL
1372 env->cp15.c9_pmovsr |= (1 << counter);
1373 pmu_update_irq(env);
1374 }
1375 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1376 }
1377 env->cp15.c14_pmevcntr_delta[counter] = count;
1378}
1379
1380static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1381{
1382 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1383#ifndef CONFIG_USER_ONLY
1384 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1385 uint16_t event_idx = supported_event_map[event];
47b385da
PM
1386 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1387 int64_t overflow_in;
1388
1389 if (!pmevcntr_is_64_bit(env, counter)) {
1390 delta = (uint32_t)delta;
1391 }
1392 overflow_in = pm_events[event_idx].ns_per_count(delta);
4e7beb0c
AL
1393
1394 if (overflow_in > 0) {
f1dd2506
PM
1395 int64_t overflow_at;
1396
1397 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1398 overflow_in, &overflow_at)) {
1399 ARMCPU *cpu = env_archcpu(env);
1400 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1401 }
4e7beb0c
AL
1402 }
1403#endif
1404
5ecdd3e4
AL
1405 env->cp15.c14_pmevcntr_delta[counter] -=
1406 env->cp15.c14_pmevcntr[counter];
1407 }
1408}
1409
5d05b9d4
AL
1410void pmu_op_start(CPUARMState *env)
1411{
5ecdd3e4 1412 unsigned int i;
5d05b9d4 1413 pmccntr_op_start(env);
5ecdd3e4
AL
1414 for (i = 0; i < pmu_num_counters(env); i++) {
1415 pmevcntr_op_start(env, i);
1416 }
5d05b9d4
AL
1417}
1418
1419void pmu_op_finish(CPUARMState *env)
1420{
5ecdd3e4 1421 unsigned int i;
5d05b9d4 1422 pmccntr_op_finish(env);
5ecdd3e4
AL
1423 for (i = 0; i < pmu_num_counters(env); i++) {
1424 pmevcntr_op_finish(env, i);
1425 }
5d05b9d4
AL
1426}
1427
033614c4
AL
1428void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1429{
1430 pmu_op_start(&cpu->env);
1431}
1432
1433void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1434{
1435 pmu_op_finish(&cpu->env);
1436}
1437
4e7beb0c
AL
1438void arm_pmu_timer_cb(void *opaque)
1439{
1440 ARMCPU *cpu = opaque;
1441
1442 /*
1443 * Update all the counter values based on the current underlying counts,
1444 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1445 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1446 * counter may expire.
1447 */
1448 pmu_op_start(&cpu->env);
1449 pmu_op_finish(&cpu->env);
1450}
1451
c4241c7d
PM
1452static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453 uint64_t value)
200ac0ef 1454{
5d05b9d4 1455 pmu_op_start(env);
7c2cb42b
AF
1456
1457 if (value & PMCRC) {
1458 /* The counter has been reset */
1459 env->cp15.c15_ccnt = 0;
1460 }
1461
5ecdd3e4
AL
1462 if (value & PMCRP) {
1463 unsigned int i;
1464 for (i = 0; i < pmu_num_counters(env); i++) {
1465 env->cp15.c14_pmevcntr[i] = 0;
1466 }
1467 }
1468
9323e79f
PM
1469 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1470 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
7c2cb42b 1471
5d05b9d4 1472 pmu_op_finish(env);
7c2cb42b
AF
1473}
1474
0d4bfd7d
AL
1475static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1476 uint64_t value)
1477{
1478 unsigned int i;
47b385da
PM
1479 uint64_t overflow_mask, new_pmswinc;
1480
0d4bfd7d
AL
1481 for (i = 0; i < pmu_num_counters(env); i++) {
1482 /* Increment a counter's count iff: */
1483 if ((value & (1 << i)) && /* counter's bit is set */
1484 /* counter is enabled and not filtered */
1485 pmu_counter_enabled(env, i) &&
1486 /* counter is SW_INCR */
1487 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1488 pmevcntr_op_start(env, i);
f4efb4b2
AL
1489
1490 /*
1491 * Detect if this write causes an overflow since we can't predict
1492 * PMSWINC overflows like we can for other events
1493 */
47b385da
PM
1494 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1495
1496 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1497 1ULL << 63 : 1ULL << 31;
f4efb4b2 1498
47b385da 1499 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
f4efb4b2
AL
1500 env->cp15.c9_pmovsr |= (1 << i);
1501 pmu_update_irq(env);
1502 }
1503
1504 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1505
0d4bfd7d
AL
1506 pmevcntr_op_finish(env, i);
1507 }
1508 }
1509}
1510
7c2cb42b
AF
1511static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1512{
5d05b9d4
AL
1513 uint64_t ret;
1514 pmccntr_op_start(env);
1515 ret = env->cp15.c15_ccnt;
1516 pmccntr_op_finish(env);
1517 return ret;
7c2cb42b
AF
1518}
1519
6b040780
WH
1520static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521 uint64_t value)
1522{
9b37a28c
FR
1523 /*
1524 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
6b040780
WH
1525 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1526 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1527 * accessed.
1528 */
1529 env->cp15.c9_pmselr = value & 0x1f;
1530}
1531
7c2cb42b
AF
1532static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1533 uint64_t value)
1534{
5d05b9d4
AL
1535 pmccntr_op_start(env);
1536 env->cp15.c15_ccnt = value;
1537 pmccntr_op_finish(env);
200ac0ef 1538}
421c7ebd
PC
1539
1540static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1541 uint64_t value)
1542{
1543 uint64_t cur_val = pmccntr_read(env, NULL);
1544
1545 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1546}
1547
0614601c
AF
1548static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1549 uint64_t value)
1550{
5d05b9d4 1551 pmccntr_op_start(env);
4b8afa1f
AL
1552 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1553 pmccntr_op_finish(env);
1554}
1555
1556static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1557 uint64_t value)
1558{
1559 pmccntr_op_start(env);
1560 /* M is not accessible from AArch32 */
1561 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1562 (value & PMCCFILTR);
5d05b9d4 1563 pmccntr_op_finish(env);
0614601c
AF
1564}
1565
4b8afa1f
AL
1566static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1567{
1568 /* M is not visible in AArch32 */
1569 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1570}
1571
c4241c7d 1572static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1573 uint64_t value)
1574{
01765386 1575 pmu_op_start(env);
7ece99b1 1576 value &= pmu_counter_mask(env);
200ac0ef 1577 env->cp15.c9_pmcnten |= value;
01765386 1578 pmu_op_finish(env);
200ac0ef
PM
1579}
1580
c4241c7d
PM
1581static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1582 uint64_t value)
200ac0ef 1583{
01765386 1584 pmu_op_start(env);
7ece99b1 1585 value &= pmu_counter_mask(env);
200ac0ef 1586 env->cp15.c9_pmcnten &= ~value;
01765386 1587 pmu_op_finish(env);
200ac0ef
PM
1588}
1589
c4241c7d
PM
1590static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1591 uint64_t value)
200ac0ef 1592{
599b71e2 1593 value &= pmu_counter_mask(env);
200ac0ef 1594 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1595 pmu_update_irq(env);
200ac0ef
PM
1596}
1597
327dd510
AL
1598static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1599 uint64_t value)
1600{
1601 value &= pmu_counter_mask(env);
1602 env->cp15.c9_pmovsr |= value;
f4efb4b2 1603 pmu_update_irq(env);
327dd510
AL
1604}
1605
5ecdd3e4
AL
1606static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1607 uint64_t value, const uint8_t counter)
200ac0ef 1608{
5ecdd3e4
AL
1609 if (counter == 31) {
1610 pmccfiltr_write(env, ri, value);
1611 } else if (counter < pmu_num_counters(env)) {
1612 pmevcntr_op_start(env, counter);
1613
1614 /*
1615 * If this counter's event type is changing, store the current
1616 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1617 * pmevcntr_op_finish has the correct baseline when it converts back to
1618 * a delta.
1619 */
1620 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1621 PMXEVTYPER_EVTCOUNT;
1622 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1623 if (old_event != new_event) {
1624 uint64_t count = 0;
1625 if (event_supported(new_event)) {
1626 uint16_t event_idx = supported_event_map[new_event];
1627 count = pm_events[event_idx].get_count(env);
1628 }
1629 env->cp15.c14_pmevcntr_delta[counter] = count;
1630 }
1631
1632 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1633 pmevcntr_op_finish(env, counter);
1634 }
9b37a28c
FR
1635 /*
1636 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
fdb86656
WH
1637 * PMSELR value is equal to or greater than the number of implemented
1638 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1639 */
5ecdd3e4
AL
1640}
1641
1642static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1643 const uint8_t counter)
1644{
1645 if (counter == 31) {
1646 return env->cp15.pmccfiltr_el0;
1647 } else if (counter < pmu_num_counters(env)) {
1648 return env->cp15.c14_pmevtyper[counter];
1649 } else {
1650 /*
1651 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1652 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1653 */
1654 return 0;
1655 }
1656}
1657
1658static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1659 uint64_t value)
1660{
1661 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1662 pmevtyper_write(env, ri, value, counter);
1663}
1664
1665static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1666 uint64_t value)
1667{
1668 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1669 env->cp15.c14_pmevtyper[counter] = value;
1670
1671 /*
1672 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1673 * pmu_op_finish calls when loading saved state for a migration. Because
1674 * we're potentially updating the type of event here, the value written to
1675 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1676 * different counter type. Therefore, we need to set this value to the
1677 * current count for the counter type we're writing so that pmu_op_finish
1678 * has the correct count for its calculation.
1679 */
1680 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1681 if (event_supported(event)) {
1682 uint16_t event_idx = supported_event_map[event];
1683 env->cp15.c14_pmevcntr_delta[counter] =
1684 pm_events[event_idx].get_count(env);
fdb86656
WH
1685 }
1686}
1687
5ecdd3e4
AL
1688static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1689{
1690 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1691 return pmevtyper_read(env, ri, counter);
1692}
1693
1694static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1695 uint64_t value)
1696{
1697 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1698}
1699
fdb86656
WH
1700static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1701{
5ecdd3e4
AL
1702 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1703}
1704
1705static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1706 uint64_t value, uint8_t counter)
1707{
47b385da
PM
1708 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1709 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1710 value &= MAKE_64BIT_MASK(0, 32);
1711 }
5ecdd3e4
AL
1712 if (counter < pmu_num_counters(env)) {
1713 pmevcntr_op_start(env, counter);
1714 env->cp15.c14_pmevcntr[counter] = value;
1715 pmevcntr_op_finish(env, counter);
1716 }
1717 /*
1718 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1719 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1720 */
5ecdd3e4
AL
1721}
1722
1723static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1724 uint8_t counter)
1725{
1726 if (counter < pmu_num_counters(env)) {
1727 uint64_t ret;
1728 pmevcntr_op_start(env, counter);
1729 ret = env->cp15.c14_pmevcntr[counter];
1730 pmevcntr_op_finish(env, counter);
47b385da
PM
1731 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1732 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1733 ret &= MAKE_64BIT_MASK(0, 32);
1734 }
5ecdd3e4 1735 return ret;
fdb86656 1736 } else {
9b37a28c
FR
1737 /*
1738 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1739 * are CONSTRAINED UNPREDICTABLE.
1740 */
fdb86656
WH
1741 return 0;
1742 }
200ac0ef
PM
1743}
1744
5ecdd3e4
AL
1745static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1746 uint64_t value)
1747{
1748 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1749 pmevcntr_write(env, ri, value, counter);
1750}
1751
1752static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1753{
1754 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1755 return pmevcntr_read(env, ri, counter);
1756}
1757
1758static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1759 uint64_t value)
1760{
1761 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1762 assert(counter < pmu_num_counters(env));
1763 env->cp15.c14_pmevcntr[counter] = value;
1764 pmevcntr_write(env, ri, value, counter);
1765}
1766
1767static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1768{
1769 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1770 assert(counter < pmu_num_counters(env));
1771 return env->cp15.c14_pmevcntr[counter];
1772}
1773
1774static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1775 uint64_t value)
1776{
1777 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1778}
1779
1780static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1781{
1782 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1783}
1784
c4241c7d 1785static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1786 uint64_t value)
1787{
6ecd0b6b
AB
1788 if (arm_feature(env, ARM_FEATURE_V8)) {
1789 env->cp15.c9_pmuserenr = value & 0xf;
1790 } else {
1791 env->cp15.c9_pmuserenr = value & 1;
1792 }
200ac0ef
PM
1793}
1794
c4241c7d
PM
1795static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1796 uint64_t value)
200ac0ef
PM
1797{
1798 /* We have no event counters so only the C bit can be changed */
7ece99b1 1799 value &= pmu_counter_mask(env);
200ac0ef 1800 env->cp15.c9_pminten |= value;
f4efb4b2 1801 pmu_update_irq(env);
200ac0ef
PM
1802}
1803
c4241c7d
PM
1804static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1805 uint64_t value)
200ac0ef 1806{
7ece99b1 1807 value &= pmu_counter_mask(env);
200ac0ef 1808 env->cp15.c9_pminten &= ~value;
f4efb4b2 1809 pmu_update_irq(env);
200ac0ef
PM
1810}
1811
c4241c7d
PM
1812static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1813 uint64_t value)
8641136c 1814{
9b37a28c
FR
1815 /*
1816 * Note that even though the AArch64 view of this register has bits
a505d7fe
PM
1817 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1818 * architectural requirements for bits which are RES0 only in some
1819 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1820 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1821 */
855ea66d 1822 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1823}
1824
64e0e2de
EI
1825static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1826{
ea22747c 1827 /* Begin with base v8.0 state. */
06f2adcc 1828 uint64_t valid_mask = 0x3fff;
2fc0cc0e 1829 ARMCPU *cpu = env_archcpu(env);
d902ae75 1830 uint64_t changed;
ea22747c 1831
bfe43e3d
RH
1832 /*
1833 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1834 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1835 * Instead, choose the format based on the mode of EL3.
1836 */
1837 if (arm_el_is_aa64(env, 3)) {
1838 value |= SCR_FW | SCR_AW; /* RES1 */
1839 valid_mask &= ~SCR_NET; /* RES0 */
252e8c69 1840
6bcbb07a
RH
1841 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1842 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1843 value |= SCR_RW; /* RAO/WI */
1844 }
da3d8b13
RH
1845 if (cpu_isar_feature(aa64_ras, cpu)) {
1846 valid_mask |= SCR_TERR;
1847 }
252e8c69
RH
1848 if (cpu_isar_feature(aa64_lor, cpu)) {
1849 valid_mask |= SCR_TLOR;
1850 }
1851 if (cpu_isar_feature(aa64_pauth, cpu)) {
1852 valid_mask |= SCR_API | SCR_APK;
1853 }
926c1b97
RDC
1854 if (cpu_isar_feature(aa64_sel2, cpu)) {
1855 valid_mask |= SCR_EEL2;
1856 }
8ddb300b
RH
1857 if (cpu_isar_feature(aa64_mte, cpu)) {
1858 valid_mask |= SCR_ATA;
1859 }
7cb1e618
RH
1860 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1861 valid_mask |= SCR_ENSCXT;
1862 }
7ac61020
PM
1863 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1864 valid_mask |= SCR_EASE | SCR_NMEA;
1865 }
06f2adcc
JF
1866 if (cpu_isar_feature(aa64_sme, cpu)) {
1867 valid_mask |= SCR_ENTP2;
1868 }
08899b5c
EI
1869 if (cpu_isar_feature(aa64_hcx, cpu)) {
1870 valid_mask |= SCR_HXEN;
1871 }
ea22747c
RH
1872 } else {
1873 valid_mask &= ~(SCR_RW | SCR_ST);
da3d8b13
RH
1874 if (cpu_isar_feature(aa32_ras, cpu)) {
1875 valid_mask |= SCR_TERR;
1876 }
ea22747c 1877 }
64e0e2de
EI
1878
1879 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1880 valid_mask &= ~SCR_HCE;
1881
9b37a28c
FR
1882 /*
1883 * On ARMv7, SMD (or SCD as it is called in v7) is only
64e0e2de
EI
1884 * supported if EL2 exists. The bit is UNK/SBZP when
1885 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1886 * when EL2 is unavailable.
4eb27640 1887 * On ARMv8, this bit is always available.
64e0e2de 1888 */
4eb27640
GB
1889 if (arm_feature(env, ARM_FEATURE_V7) &&
1890 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1891 valid_mask &= ~SCR_SMD;
1892 }
1893 }
1894
1895 /* Clear all-context RES0 bits. */
1896 value &= valid_mask;
d902ae75
RH
1897 changed = env->cp15.scr_el3 ^ value;
1898 env->cp15.scr_el3 = value;
1899
1900 /*
1901 * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1902 * we must invalidate all TLBs below EL3.
1903 */
1904 if (changed & SCR_NS) {
1905 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1906 ARMMMUIdxBit_E20_0 |
1907 ARMMMUIdxBit_E10_1 |
1908 ARMMMUIdxBit_E20_2 |
1909 ARMMMUIdxBit_E10_1_PAN |
1910 ARMMMUIdxBit_E20_2_PAN |
1911 ARMMMUIdxBit_E2));
1912 }
64e0e2de
EI
1913}
1914
10d0ef3e
MN
1915static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1916{
1917 /*
1918 * scr_write will set the RES1 bits on an AArch64-only CPU.
1919 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1920 */
1921 scr_write(env, ri, 0);
1922}
1923
e2ce5fcd
PM
1924static CPAccessResult access_tid4(CPUARMState *env,
1925 const ARMCPRegInfo *ri,
1926 bool isread)
630fcd4d 1927{
e2ce5fcd
PM
1928 if (arm_current_el(env) == 1 &&
1929 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
630fcd4d
MZ
1930 return CP_ACCESS_TRAP_EL2;
1931 }
1932
1933 return CP_ACCESS_OK;
1934}
1935
c4241c7d 1936static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1937{
2fc0cc0e 1938 ARMCPU *cpu = env_archcpu(env);
b85a1fd6 1939
9b37a28c
FR
1940 /*
1941 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
b85a1fd6
FA
1942 * bank
1943 */
1944 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1945 ri->secure & ARM_CP_SECSTATE_S);
1946
1947 return cpu->ccsidr[index];
776d4e5c
PM
1948}
1949
c4241c7d
PM
1950static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1951 uint64_t value)
776d4e5c 1952{
8d5c773e 1953 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1954}
1955
1090b9c6
PM
1956static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1957{
29a0af61 1958 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1959 bool el1 = arm_current_el(env) == 1;
1960 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1961 uint64_t ret = 0;
1962
cc974d5c 1963 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1964 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1965 ret |= CPSR_I;
1966 }
1967 } else {
1968 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1969 ret |= CPSR_I;
1970 }
1090b9c6 1971 }
636540e9 1972
cc974d5c 1973 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1974 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1975 ret |= CPSR_F;
1976 }
1977 } else {
1978 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1979 ret |= CPSR_F;
1980 }
1090b9c6 1981 }
636540e9 1982
3c29632f
RH
1983 if (hcr_el2 & HCR_AMO) {
1984 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1985 ret |= CPSR_A;
1986 }
1987 }
1988
1090b9c6
PM
1989 return ret;
1990}
1991
93fbc983
MZ
1992static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1993 bool isread)
1994{
1995 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1996 return CP_ACCESS_TRAP_EL2;
1997 }
1998
1999 return CP_ACCESS_OK;
2000}
2001
2002static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2003 bool isread)
2004{
2005 if (arm_feature(env, ARM_FEATURE_V8)) {
2006 return access_aa64_tid1(env, ri, isread);
2007 }
2008
2009 return CP_ACCESS_OK;
2010}
2011
e9aa6c21 2012static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2013 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2014 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2015 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
2016 /*
2017 * Performance monitors are implementation defined in v7,
200ac0ef 2018 * but with an ARM recommended set of registers, which we
ac689a2e 2019 * follow.
200ac0ef
PM
2020 *
2021 * Performance registers fall into three categories:
2022 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2023 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2024 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2025 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2026 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2027 */
2028 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7f4fbfb5 2029 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b 2030 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2031 .writefn = pmcntenset_write,
2032 .accessfn = pmreg_access,
2033 .raw_writefn = raw_write },
7f4fbfb5 2034 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
8521466b
AF
2035 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2036 .access = PL0_RW, .accessfn = pmreg_access,
2037 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2038 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2039 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2040 .access = PL0_RW,
2041 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2042 .accessfn = pmreg_access,
2043 .writefn = pmcntenclr_write,
7f4fbfb5 2044 .type = ARM_CP_ALIAS | ARM_CP_IO },
8521466b
AF
2045 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2046 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2047 .access = PL0_RW, .accessfn = pmreg_access,
7f4fbfb5 2048 .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b
AF
2049 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2050 .writefn = pmcntenclr_write },
200ac0ef 2051 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2052 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2053 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2054 .accessfn = pmreg_access,
2055 .writefn = pmovsr_write,
2056 .raw_writefn = raw_write },
978364f1
AF
2057 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2058 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2059 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2060 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2061 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2062 .writefn = pmovsr_write,
2063 .raw_writefn = raw_write },
200ac0ef 2064 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2065 .access = PL0_W, .accessfn = pmreg_access_swinc,
2066 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2067 .writefn = pmswinc_write },
2068 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2069 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2070 .access = PL0_W, .accessfn = pmreg_access_swinc,
2071 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2072 .writefn = pmswinc_write },
6b040780
WH
2073 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2074 .access = PL0_RW, .type = ARM_CP_ALIAS,
2075 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2076 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2077 .raw_writefn = raw_write},
2078 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2079 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2080 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2081 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2082 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2083 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2084 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2085 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2086 .accessfn = pmreg_access_ccntr },
8521466b
AF
2087 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2088 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2089 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2090 .type = ARM_CP_IO,
980ebe87
AL
2091 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2092 .readfn = pmccntr_read, .writefn = pmccntr_write,
2093 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2094 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2095 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2096 .access = PL0_RW, .accessfn = pmreg_access,
2097 .type = ARM_CP_ALIAS | ARM_CP_IO,
2098 .resetvalue = 0, },
8521466b
AF
2099 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2100 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2101 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2102 .access = PL0_RW, .accessfn = pmreg_access,
2103 .type = ARM_CP_IO,
2104 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2105 .resetvalue = 0, },
200ac0ef 2106 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2107 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2108 .accessfn = pmreg_access,
fdb86656
WH
2109 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2110 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2112 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2113 .accessfn = pmreg_access,
fdb86656 2114 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2115 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2116 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2117 .accessfn = pmreg_access_xevcntr,
2118 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2119 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2120 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2121 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2122 .accessfn = pmreg_access_xevcntr,
2123 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2124 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2125 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2126 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2127 .resetvalue = 0,
d4e6df63 2128 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2129 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2130 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2131 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2132 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2133 .resetvalue = 0,
2134 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2135 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2136 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2137 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2138 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2139 .resetvalue = 0,
d4e6df63 2140 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2141 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2142 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2143 .access = PL1_RW, .accessfn = access_tpm,
2144 .type = ARM_CP_IO,
2145 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2146 .writefn = pmintenset_write, .raw_writefn = raw_write,
2147 .resetvalue = 0x0 },
200ac0ef 2148 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2149 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2150 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2151 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2152 .writefn = pmintenclr_write, },
978364f1
AF
2153 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2154 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2155 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2156 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2157 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2158 .writefn = pmintenclr_write },
7da845b0
PM
2159 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2160 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d 2161 .access = PL1_R,
e2ce5fcd 2162 .accessfn = access_tid4,
630fcd4d 2163 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2164 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2165 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d 2166 .access = PL1_RW,
e2ce5fcd 2167 .accessfn = access_tid4,
630fcd4d 2168 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2169 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2170 offsetof(CPUARMState, cp15.csselr_ns) } },
9b37a28c
FR
2171 /*
2172 * Auxiliary ID register: this actually has an IMPDEF value but for now
776d4e5c
PM
2173 * just RAZ for all cores:
2174 */
0ff644a7
PM
2175 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2176 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2177 .access = PL1_R, .type = ARM_CP_CONST,
2178 .accessfn = access_aa64_tid1,
2179 .resetvalue = 0 },
9b37a28c
FR
2180 /*
2181 * Auxiliary fault status registers: these also are IMPDEF, and we
f32cdad5
PM
2182 * choose to RAZ/WI for all cores.
2183 */
2184 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2185 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2186 .access = PL1_RW, .accessfn = access_tvm_trvm,
2187 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2188 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2189 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2190 .access = PL1_RW, .accessfn = access_tvm_trvm,
2191 .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
2192 /*
2193 * MAIR can just read-as-written because we don't implement caches
b0fe2427
PM
2194 * and so don't need to care about memory attributes.
2195 */
2196 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2197 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2198 .access = PL1_RW, .accessfn = access_tvm_trvm,
2199 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2200 .resetvalue = 0 },
4cfb8ad8
PM
2201 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2202 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2203 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2204 .resetvalue = 0 },
9b37a28c
FR
2205 /*
2206 * For non-long-descriptor page tables these are PRRR and NMRR;
b0fe2427 2207 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2208 */
9b37a28c
FR
2209 /*
2210 * MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2211 * allows them to assign the correct fieldoffset based on the endianness
2212 * handled in the field definitions.
2213 */
a903c449 2214 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2215 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2216 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2217 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2218 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2219 .resetfn = arm_cp_reset_ignore },
a903c449 2220 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2221 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2222 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2223 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2224 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2225 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2226 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2227 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2228 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2229 /* 32 bit ITLB invalidates */
2230 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2231 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2232 .writefn = tlbiall_write },
995939a6 2233 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2234 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2235 .writefn = tlbimva_write },
995939a6 2236 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2237 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2238 .writefn = tlbiasid_write },
995939a6
PM
2239 /* 32 bit DTLB invalidates */
2240 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2241 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2242 .writefn = tlbiall_write },
995939a6 2243 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2244 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2245 .writefn = tlbimva_write },
995939a6 2246 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2247 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2248 .writefn = tlbiasid_write },
995939a6
PM
2249 /* 32 bit TLB invalidates */
2250 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2251 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2252 .writefn = tlbiall_write },
995939a6 2253 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2254 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2255 .writefn = tlbimva_write },
995939a6 2256 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2257 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2258 .writefn = tlbiasid_write },
995939a6 2259 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2260 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2261 .writefn = tlbimvaa_write },
995939a6
PM
2262};
2263
2264static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2265 /* 32 bit TLB invalidates, Inner Shareable */
2266 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 2267 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2268 .writefn = tlbiall_is_write },
995939a6 2269 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 2270 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2271 .writefn = tlbimva_is_write },
995939a6 2272 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 2273 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2274 .writefn = tlbiasid_is_write },
995939a6 2275 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 2276 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2277 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2278};
2279
327dd510
AL
2280static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2281 /* PMOVSSET is not implemented in v7 before v7ve */
2282 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2283 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2284 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2285 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2286 .writefn = pmovsset_write,
2287 .raw_writefn = raw_write },
2288 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2289 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2290 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2291 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2292 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2293 .writefn = pmovsset_write,
2294 .raw_writefn = raw_write },
327dd510
AL
2295};
2296
c4241c7d
PM
2297static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2298 uint64_t value)
c326b979
PM
2299{
2300 value &= 1;
2301 env->teecr = value;
c326b979
PM
2302}
2303
cc7613bf
PM
2304static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2305 bool isread)
2306{
2307 /*
2308 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2309 * at all, so we don't need to check whether we're v8A.
2310 */
2311 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2312 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2313 return CP_ACCESS_TRAP_EL2;
2314 }
2315 return CP_ACCESS_OK;
2316}
2317
3f208fd7
PM
2318static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2319 bool isread)
c326b979 2320{
dcbff19b 2321 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2322 return CP_ACCESS_TRAP;
c326b979 2323 }
cc7613bf 2324 return teecr_access(env, ri, isread);
c326b979
PM
2325}
2326
2327static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2328 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2329 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2330 .resetvalue = 0,
cc7613bf 2331 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2332 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2333 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2334 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2335};
2336
4d31c596 2337static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2338 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2339 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2340 .access = PL0_RW,
54bf36ed 2341 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2342 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2343 .access = PL0_RW,
54bf36ed
FA
2344 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2345 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2346 .resetfn = arm_cp_reset_ignore },
2347 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2348 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
04215eb1 2349 .access = PL0_R | PL1_W,
54bf36ed
FA
2350 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2351 .resetvalue = 0},
4d31c596 2352 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
04215eb1 2353 .access = PL0_R | PL1_W,
54bf36ed
FA
2354 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2355 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2356 .resetfn = arm_cp_reset_ignore },
54bf36ed 2357 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2358 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2359 .access = PL1_RW,
54bf36ed
FA
2360 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2361 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2362 .access = PL1_RW,
2363 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2364 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2365 .resetvalue = 0 },
4d31c596
PM
2366};
2367
55d284af
PM
2368#ifndef CONFIG_USER_ONLY
2369
3f208fd7
PM
2370static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2371 bool isread)
00108f2d 2372{
9b37a28c
FR
2373 /*
2374 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
75502672
PM
2375 * Writable only at the highest implemented exception level.
2376 */
2377 int el = arm_current_el(env);
5bc84371
RH
2378 uint64_t hcr;
2379 uint32_t cntkctl;
75502672
PM
2380
2381 switch (el) {
2382 case 0:
5bc84371
RH
2383 hcr = arm_hcr_el2_eff(env);
2384 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2385 cntkctl = env->cp15.cnthctl_el2;
2386 } else {
2387 cntkctl = env->cp15.c14_cntkctl;
2388 }
2389 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2390 return CP_ACCESS_TRAP;
2391 }
2392 break;
2393 case 1:
2394 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2395 arm_is_secure_below_el3(env)) {
2396 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2397 return CP_ACCESS_TRAP_UNCATEGORIZED;
2398 }
2399 break;
2400 case 2:
2401 case 3:
2402 break;
00108f2d 2403 }
75502672
PM
2404
2405 if (!isread && el < arm_highest_el(env)) {
2406 return CP_ACCESS_TRAP_UNCATEGORIZED;
2407 }
2408
00108f2d
PM
2409 return CP_ACCESS_OK;
2410}
2411
3f208fd7
PM
2412static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2413 bool isread)
00108f2d 2414{
0b6440af 2415 unsigned int cur_el = arm_current_el(env);
e6ef0169 2416 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2417 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2418
5bc84371
RH
2419 switch (cur_el) {
2420 case 0:
2421 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2422 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2423 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2424 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2425 }
0b6440af 2426
5bc84371
RH
2427 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2428 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2429 return CP_ACCESS_TRAP;
2430 }
2431
2432 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2433 if (hcr & HCR_E2H) {
2434 if (timeridx == GTIMER_PHYS &&
2435 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2436 return CP_ACCESS_TRAP_EL2;
2437 }
2438 } else {
2439 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2440 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2441 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2442 return CP_ACCESS_TRAP_EL2;
2443 }
2444 }
2445 break;
2446
2447 case 1:
2448 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2449 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2450 (hcr & HCR_E2H
2451 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2452 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2453 return CP_ACCESS_TRAP_EL2;
2454 }
2455 break;
0b6440af 2456 }
00108f2d
PM
2457 return CP_ACCESS_OK;
2458}
2459
3f208fd7
PM
2460static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2461 bool isread)
00108f2d 2462{
0b6440af 2463 unsigned int cur_el = arm_current_el(env);
e6ef0169 2464 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2465 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2466
5bc84371
RH
2467 switch (cur_el) {
2468 case 0:
2469 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2470 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2471 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2472 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2473 }
0b6440af 2474
5bc84371
RH
2475 /*
2476 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2477 * EL0 if EL0[PV]TEN is zero.
2478 */
2479 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2480 return CP_ACCESS_TRAP;
2481 }
2482 /* fall through */
2483
2484 case 1:
e6ef0169 2485 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2486 if (hcr & HCR_E2H) {
2487 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2488 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2489 return CP_ACCESS_TRAP_EL2;
2490 }
2491 } else {
2492 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2493 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2494 return CP_ACCESS_TRAP_EL2;
2495 }
2496 }
2497 }
2498 break;
0b6440af 2499 }
00108f2d
PM
2500 return CP_ACCESS_OK;
2501}
2502
2503static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2504 const ARMCPRegInfo *ri,
2505 bool isread)
00108f2d 2506{
3f208fd7 2507 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2508}
2509
2510static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2511 const ARMCPRegInfo *ri,
2512 bool isread)
00108f2d 2513{
3f208fd7 2514 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2515}
2516
3f208fd7
PM
2517static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2518 bool isread)
00108f2d 2519{
3f208fd7 2520 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2521}
2522
3f208fd7
PM
2523static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2524 bool isread)
00108f2d 2525{
3f208fd7 2526 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2527}
2528
b4d3978c 2529static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2530 const ARMCPRegInfo *ri,
2531 bool isread)
b4d3978c 2532{
9b37a28c
FR
2533 /*
2534 * The AArch64 register view of the secure physical timer is
b4d3978c
PM
2535 * always accessible from EL3, and configurably accessible from
2536 * Secure EL1.
2537 */
2538 switch (arm_current_el(env)) {
2539 case 1:
2540 if (!arm_is_secure(env)) {
2541 return CP_ACCESS_TRAP;
2542 }
2543 if (!(env->cp15.scr_el3 & SCR_ST)) {
2544 return CP_ACCESS_TRAP_EL3;
2545 }
2546 return CP_ACCESS_OK;
2547 case 0:
2548 case 2:
2549 return CP_ACCESS_TRAP;
2550 case 3:
2551 return CP_ACCESS_OK;
2552 default:
2553 g_assert_not_reached();
2554 }
2555}
2556
55d284af
PM
2557static uint64_t gt_get_countervalue(CPUARMState *env)
2558{
7def8754
AJ
2559 ARMCPU *cpu = env_archcpu(env);
2560
2561 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2562}
2563
2564static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2565{
2566 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2567
2568 if (gt->ctl & 1) {
9b37a28c
FR
2569 /*
2570 * Timer enabled: calculate and set current ISTATUS, irq, and
55d284af
PM
2571 * reset timer to when ISTATUS next has to change
2572 */
edac4d8a
EI
2573 uint64_t offset = timeridx == GTIMER_VIRT ?
2574 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2575 uint64_t count = gt_get_countervalue(&cpu->env);
2576 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2577 int istatus = count - offset >= gt->cval;
55d284af 2578 uint64_t nexttick;
194cbc49 2579 int irqstate;
55d284af
PM
2580
2581 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2582
2583 irqstate = (istatus && !(gt->ctl & 2));
2584 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2585
55d284af
PM
2586 if (istatus) {
2587 /* Next transition is when count rolls back over to zero */
2588 nexttick = UINT64_MAX;
2589 } else {
2590 /* Next transition is when we hit cval */
edac4d8a 2591 nexttick = gt->cval + offset;
55d284af 2592 }
9b37a28c
FR
2593 /*
2594 * Note that the desired next expiry time might be beyond the
55d284af
PM
2595 * signed-64-bit range of a QEMUTimer -- in this case we just
2596 * set the timer for as far in the future as possible. When the
2597 * timer expires we will reset the timer for any remaining period.
2598 */
7def8754 2599 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2600 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2601 } else {
2602 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2603 }
194cbc49 2604 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2605 } else {
2606 /* Timer disabled: ISTATUS and timer output always clear */
2607 gt->ctl &= ~4;
2608 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2609 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2610 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2611 }
2612}
2613
0e3eca4c
EI
2614static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2615 int timeridx)
55d284af 2616{
2fc0cc0e 2617 ARMCPU *cpu = env_archcpu(env);
55d284af 2618
bc72ad67 2619 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2620}
2621
c4241c7d 2622static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2623{
c4241c7d 2624 return gt_get_countervalue(env);
55d284af
PM
2625}
2626
53d1f856
RH
2627static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2628{
2629 uint64_t hcr;
2630
2631 switch (arm_current_el(env)) {
2632 case 2:
2633 hcr = arm_hcr_el2_eff(env);
2634 if (hcr & HCR_E2H) {
2635 return 0;
2636 }
2637 break;
2638 case 0:
2639 hcr = arm_hcr_el2_eff(env);
2640 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2641 return 0;
2642 }
2643 break;
2644 }
2645
2646 return env->cp15.cntvoff_el2;
2647}
2648
edac4d8a
EI
2649static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2650{
53d1f856 2651 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2652}
2653
c4241c7d 2654static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2655 int timeridx,
c4241c7d 2656 uint64_t value)
55d284af 2657{
194cbc49 2658 trace_arm_gt_cval_write(timeridx, value);
55d284af 2659 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2660 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2661}
c4241c7d 2662
0e3eca4c
EI
2663static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2664 int timeridx)
55d284af 2665{
53d1f856
RH
2666 uint64_t offset = 0;
2667
2668 switch (timeridx) {
2669 case GTIMER_VIRT:
8c94b071 2670 case GTIMER_HYPVIRT:
53d1f856
RH
2671 offset = gt_virt_cnt_offset(env);
2672 break;
2673 }
55d284af 2674
c4241c7d 2675 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2676 (gt_get_countervalue(env) - offset));
55d284af
PM
2677}
2678
c4241c7d 2679static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2680 int timeridx,
c4241c7d 2681 uint64_t value)
55d284af 2682{
53d1f856
RH
2683 uint64_t offset = 0;
2684
2685 switch (timeridx) {
2686 case GTIMER_VIRT:
8c94b071 2687 case GTIMER_HYPVIRT:
53d1f856
RH
2688 offset = gt_virt_cnt_offset(env);
2689 break;
2690 }
55d284af 2691
194cbc49 2692 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2693 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2694 sextract64(value, 0, 32);
2fc0cc0e 2695 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2696}
2697
c4241c7d 2698static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2699 int timeridx,
c4241c7d 2700 uint64_t value)
55d284af 2701{
2fc0cc0e 2702 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2703 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2704
194cbc49 2705 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2706 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2707 if ((oldval ^ value) & 1) {
2708 /* Enable toggled */
2709 gt_recalc_timer(cpu, timeridx);
d3afacc7 2710 } else if ((oldval ^ value) & 2) {
9b37a28c
FR
2711 /*
2712 * IMASK toggled: don't need to recalculate,
55d284af
PM
2713 * just set the interrupt line based on ISTATUS
2714 */
194cbc49
PM
2715 int irqstate = (oldval & 4) && !(value & 2);
2716
2717 trace_arm_gt_imask_toggle(timeridx, irqstate);
2718 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2719 }
55d284af
PM
2720}
2721
0e3eca4c
EI
2722static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2723{
2724 gt_timer_reset(env, ri, GTIMER_PHYS);
2725}
2726
2727static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2728 uint64_t value)
2729{
2730 gt_cval_write(env, ri, GTIMER_PHYS, value);
2731}
2732
2733static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2734{
2735 return gt_tval_read(env, ri, GTIMER_PHYS);
2736}
2737
2738static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2739 uint64_t value)
2740{
2741 gt_tval_write(env, ri, GTIMER_PHYS, value);
2742}
2743
2744static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2745 uint64_t value)
2746{
2747 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2748}
2749
bb5972e4
RH
2750static int gt_phys_redir_timeridx(CPUARMState *env)
2751{
2752 switch (arm_mmu_idx(env)) {
2753 case ARMMMUIdx_E20_0:
2754 case ARMMMUIdx_E20_2:
452ef8cb 2755 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2756 return GTIMER_HYP;
2757 default:
2758 return GTIMER_PHYS;
2759 }
2760}
2761
2762static int gt_virt_redir_timeridx(CPUARMState *env)
2763{
2764 switch (arm_mmu_idx(env)) {
2765 case ARMMMUIdx_E20_0:
2766 case ARMMMUIdx_E20_2:
452ef8cb 2767 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2768 return GTIMER_HYPVIRT;
2769 default:
2770 return GTIMER_VIRT;
2771 }
2772}
2773
2774static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2775 const ARMCPRegInfo *ri)
2776{
2777 int timeridx = gt_phys_redir_timeridx(env);
2778 return env->cp15.c14_timer[timeridx].cval;
2779}
2780
2781static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782 uint64_t value)
2783{
2784 int timeridx = gt_phys_redir_timeridx(env);
2785 gt_cval_write(env, ri, timeridx, value);
2786}
2787
2788static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2789 const ARMCPRegInfo *ri)
2790{
2791 int timeridx = gt_phys_redir_timeridx(env);
2792 return gt_tval_read(env, ri, timeridx);
2793}
2794
2795static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2796 uint64_t value)
2797{
2798 int timeridx = gt_phys_redir_timeridx(env);
2799 gt_tval_write(env, ri, timeridx, value);
2800}
2801
2802static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2803 const ARMCPRegInfo *ri)
2804{
2805 int timeridx = gt_phys_redir_timeridx(env);
2806 return env->cp15.c14_timer[timeridx].ctl;
2807}
2808
2809static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810 uint64_t value)
2811{
2812 int timeridx = gt_phys_redir_timeridx(env);
2813 gt_ctl_write(env, ri, timeridx, value);
2814}
2815
0e3eca4c
EI
2816static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2817{
2818 gt_timer_reset(env, ri, GTIMER_VIRT);
2819}
2820
2821static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822 uint64_t value)
2823{
2824 gt_cval_write(env, ri, GTIMER_VIRT, value);
2825}
2826
2827static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2828{
2829 return gt_tval_read(env, ri, GTIMER_VIRT);
2830}
2831
2832static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833 uint64_t value)
2834{
2835 gt_tval_write(env, ri, GTIMER_VIRT, value);
2836}
2837
2838static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839 uint64_t value)
2840{
2841 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2842}
2843
edac4d8a
EI
2844static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2845 uint64_t value)
2846{
2fc0cc0e 2847 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2848
194cbc49 2849 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2850 raw_write(env, ri, value);
2851 gt_recalc_timer(cpu, GTIMER_VIRT);
2852}
2853
bb5972e4
RH
2854static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2855 const ARMCPRegInfo *ri)
2856{
2857 int timeridx = gt_virt_redir_timeridx(env);
2858 return env->cp15.c14_timer[timeridx].cval;
2859}
2860
2861static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2862 uint64_t value)
2863{
2864 int timeridx = gt_virt_redir_timeridx(env);
2865 gt_cval_write(env, ri, timeridx, value);
2866}
2867
2868static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2869 const ARMCPRegInfo *ri)
2870{
2871 int timeridx = gt_virt_redir_timeridx(env);
2872 return gt_tval_read(env, ri, timeridx);
2873}
2874
2875static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2876 uint64_t value)
2877{
2878 int timeridx = gt_virt_redir_timeridx(env);
2879 gt_tval_write(env, ri, timeridx, value);
2880}
2881
2882static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2883 const ARMCPRegInfo *ri)
2884{
2885 int timeridx = gt_virt_redir_timeridx(env);
2886 return env->cp15.c14_timer[timeridx].ctl;
2887}
2888
2889static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2890 uint64_t value)
2891{
2892 int timeridx = gt_virt_redir_timeridx(env);
2893 gt_ctl_write(env, ri, timeridx, value);
2894}
2895
b0e66d95
EI
2896static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2897{
2898 gt_timer_reset(env, ri, GTIMER_HYP);
2899}
2900
2901static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2902 uint64_t value)
2903{
2904 gt_cval_write(env, ri, GTIMER_HYP, value);
2905}
2906
2907static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2908{
2909 return gt_tval_read(env, ri, GTIMER_HYP);
2910}
2911
2912static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2913 uint64_t value)
2914{
2915 gt_tval_write(env, ri, GTIMER_HYP, value);
2916}
2917
2918static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2919 uint64_t value)
2920{
2921 gt_ctl_write(env, ri, GTIMER_HYP, value);
2922}
2923
b4d3978c
PM
2924static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2925{
2926 gt_timer_reset(env, ri, GTIMER_SEC);
2927}
2928
2929static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2930 uint64_t value)
2931{
2932 gt_cval_write(env, ri, GTIMER_SEC, value);
2933}
2934
2935static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2936{
2937 return gt_tval_read(env, ri, GTIMER_SEC);
2938}
2939
2940static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2941 uint64_t value)
2942{
2943 gt_tval_write(env, ri, GTIMER_SEC, value);
2944}
2945
2946static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2947 uint64_t value)
2948{
2949 gt_ctl_write(env, ri, GTIMER_SEC, value);
2950}
2951
8c94b071
RH
2952static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2953{
2954 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2955}
2956
2957static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2958 uint64_t value)
2959{
2960 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2961}
2962
2963static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2964{
2965 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2966}
2967
2968static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2969 uint64_t value)
2970{
2971 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2972}
2973
2974static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2975 uint64_t value)
2976{
2977 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2978}
2979
55d284af
PM
2980void arm_gt_ptimer_cb(void *opaque)
2981{
2982 ARMCPU *cpu = opaque;
2983
2984 gt_recalc_timer(cpu, GTIMER_PHYS);
2985}
2986
2987void arm_gt_vtimer_cb(void *opaque)
2988{
2989 ARMCPU *cpu = opaque;
2990
2991 gt_recalc_timer(cpu, GTIMER_VIRT);
2992}
2993
b0e66d95
EI
2994void arm_gt_htimer_cb(void *opaque)
2995{
2996 ARMCPU *cpu = opaque;
2997
2998 gt_recalc_timer(cpu, GTIMER_HYP);
2999}
3000
b4d3978c
PM
3001void arm_gt_stimer_cb(void *opaque)
3002{
3003 ARMCPU *cpu = opaque;
3004
3005 gt_recalc_timer(cpu, GTIMER_SEC);
3006}
3007
8c94b071
RH
3008void arm_gt_hvtimer_cb(void *opaque)
3009{
3010 ARMCPU *cpu = opaque;
3011
3012 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3013}
3014
96eec6b2
AJ
3015static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3016{
3017 ARMCPU *cpu = env_archcpu(env);
3018
3019 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3020}
3021
55d284af 3022static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
9b37a28c
FR
3023 /*
3024 * Note that CNTFRQ is purely reads-as-written for the benefit
55d284af
PM
3025 * of software; writing it doesn't actually change the timer frequency.
3026 * Our reset value matches the fixed frequency we implement the timer at.
3027 */
3028 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3029 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3030 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3031 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3032 },
3033 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3034 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3035 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3036 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3037 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3038 },
3039 /* overall control: mostly access permissions */
a7adc4b7
PM
3040 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3041 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3042 .access = PL1_RW,
3043 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3044 .resetvalue = 0,
3045 },
3046 /* per-timer control */
3047 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3048 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3049 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3050 .accessfn = gt_ptimer_access,
3051 .fieldoffset = offsetoflow32(CPUARMState,
3052 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3053 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3054 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3055 },
9c513e78 3056 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3057 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3058 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3059 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3060 .accessfn = gt_ptimer_access,
3061 .fieldoffset = offsetoflow32(CPUARMState,
3062 cp15.c14_timer[GTIMER_SEC].ctl),
3063 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3064 },
a7adc4b7
PM
3065 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3066 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3067 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3068 .accessfn = gt_ptimer_access,
55d284af
PM
3069 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3070 .resetvalue = 0,
bb5972e4
RH
3071 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3072 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3073 },
3074 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3075 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3076 .accessfn = gt_vtimer_access,
3077 .fieldoffset = offsetoflow32(CPUARMState,
3078 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3079 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3080 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3081 },
3082 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3083 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3084 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3085 .accessfn = gt_vtimer_access,
55d284af
PM
3086 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3087 .resetvalue = 0,
bb5972e4
RH
3088 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3089 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3090 },
3091 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3092 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3093 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3094 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3095 .accessfn = gt_ptimer_access,
bb5972e4 3096 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3097 },
9c513e78 3098 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3099 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3100 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3101 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3102 .accessfn = gt_ptimer_access,
3103 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3104 },
a7adc4b7
PM
3105 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3106 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3107 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3108 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3109 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3110 },
55d284af 3111 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3112 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3113 .accessfn = gt_vtimer_access,
bb5972e4 3114 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3115 },
a7adc4b7
PM
3116 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3117 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3118 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3119 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3120 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3121 },
55d284af
PM
3122 /* The counter itself */
3123 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3124 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3125 .accessfn = gt_pct_access,
a7adc4b7
PM
3126 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3127 },
3128 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3129 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3130 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3131 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3132 },
3133 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3134 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3135 .accessfn = gt_vct_access,
edac4d8a 3136 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3137 },
3138 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3139 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3140 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3141 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3142 },
3143 /* Comparison value, indicating when the timer goes off */
3144 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3145 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3146 .access = PL0_RW,
7a0e58fa 3147 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3148 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3149 .accessfn = gt_ptimer_access,
bb5972e4
RH
3150 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3151 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3152 },
9c513e78 3153 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3154 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3155 .access = PL0_RW,
9ff9dd3c
PM
3156 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3157 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3158 .accessfn = gt_ptimer_access,
3159 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3160 },
a7adc4b7
PM
3161 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3162 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3163 .access = PL0_RW,
a7adc4b7
PM
3164 .type = ARM_CP_IO,
3165 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3166 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3167 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3168 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3169 },
3170 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3171 .access = PL0_RW,
7a0e58fa 3172 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3173 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3174 .accessfn = gt_vtimer_access,
bb5972e4
RH
3175 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3176 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3177 },
3178 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3179 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3180 .access = PL0_RW,
a7adc4b7
PM
3181 .type = ARM_CP_IO,
3182 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3183 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3184 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3185 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3186 },
9b37a28c
FR
3187 /*
3188 * Secure timer -- this is actually restricted to only EL3
b4d3978c
PM
3189 * and configurably Secure-EL1 via the accessfn.
3190 */
3191 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3192 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3193 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3194 .accessfn = gt_stimer_access,
3195 .readfn = gt_sec_tval_read,
3196 .writefn = gt_sec_tval_write,
3197 .resetfn = gt_sec_timer_reset,
3198 },
3199 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3200 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3201 .type = ARM_CP_IO, .access = PL1_RW,
3202 .accessfn = gt_stimer_access,
3203 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3204 .resetvalue = 0,
3205 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3206 },
3207 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3208 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3209 .type = ARM_CP_IO, .access = PL1_RW,
3210 .accessfn = gt_stimer_access,
3211 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3212 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3213 },
55d284af
PM
3214};
3215
bb5972e4
RH
3216static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3217 bool isread)
3218{
3219 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3220 return CP_ACCESS_TRAP;
3221 }
3222 return CP_ACCESS_OK;
3223}
3224
55d284af 3225#else
26c4a83b 3226
9b37a28c
FR
3227/*
3228 * In user-mode most of the generic timer registers are inaccessible
26c4a83b 3229 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3230 */
26c4a83b
AB
3231
3232static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3233{
7def8754
AJ
3234 ARMCPU *cpu = env_archcpu(env);
3235
9b37a28c
FR
3236 /*
3237 * Currently we have no support for QEMUTimer in linux-user so we
26c4a83b
AB
3238 * can't call gt_get_countervalue(env), instead we directly
3239 * call the lower level functions.
3240 */
7def8754 3241 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3242}
3243
6cc7a3ae 3244static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3245 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3246 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3247 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3248 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3249 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3250 },
3251 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3252 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3253 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3254 .readfn = gt_virt_cnt_read,
3255 },
6cc7a3ae
PM
3256};
3257
55d284af
PM
3258#endif
3259
c4241c7d 3260static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3261{
891a2fe7 3262 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3263 raw_write(env, ri, value);
891a2fe7 3264 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3265 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3266 } else {
8d5c773e 3267 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3268 }
4a501606
PM
3269}
3270
3271#ifndef CONFIG_USER_ONLY
3272/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3273
3f208fd7
PM
3274static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3275 bool isread)
92611c00
PM
3276{
3277 if (ri->opc2 & 4) {
9b37a28c
FR
3278 /*
3279 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3280 * Secure EL1 (which can only happen if EL3 is AArch64).
3281 * They are simply UNDEF if executed from NS EL1.
3282 * They function normally from EL2 or EL3.
92611c00 3283 */
87562e4f
PM
3284 if (arm_current_el(env) == 1) {
3285 if (arm_is_secure_below_el3(env)) {
926c1b97 3286 if (env->cp15.scr_el3 & SCR_EEL2) {
ce9a8863 3287 return CP_ACCESS_TRAP_EL2;
926c1b97 3288 }
ce9a8863 3289 return CP_ACCESS_TRAP_EL3;
87562e4f
PM
3290 }
3291 return CP_ACCESS_TRAP_UNCATEGORIZED;
3292 }
92611c00
PM
3293 }
3294 return CP_ACCESS_OK;
3295}
3296
9fb005b0 3297#ifdef CONFIG_TCG
060e8a48 3298static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
7aee3cb9
RH
3299 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3300 bool is_secure)
4a501606 3301{
b7cc4e82 3302 bool ret;
01c097f7 3303 uint64_t par64;
1313e2d7 3304 bool format64 = false;
e14b5a23 3305 ARMMMUFaultInfo fi = {};
de05a709 3306 GetPhysAddrResult res = {};
4a501606 3307
7aee3cb9
RH
3308 ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3309 is_secure, &res, &fi);
1313e2d7 3310
9f225e60
PM
3311 /*
3312 * ATS operations only do S1 or S1+S2 translations, so we never
3313 * have to deal with the ARMCacheAttrs format for S2 only.
3314 */
de05a709 3315 assert(!res.cacheattrs.is_s2_format);
9f225e60 3316
0710b2fa
PM
3317 if (ret) {
3318 /*
3319 * Some kinds of translation fault must cause exceptions rather
3320 * than being reported in the PAR.
3321 */
3322 int current_el = arm_current_el(env);
3323 int target_el;
3324 uint32_t syn, fsr, fsc;
3325 bool take_exc = false;
3326
b1a10c86 3327 if (fi.s1ptw && current_el == 1
fee7aa46 3328 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3329 /*
3330 * Synchronous stage 2 fault on an access made as part of the
3331 * translation table walk for AT S1E0* or AT S1E1* insn
3332 * executed from NS EL1. If this is a synchronous external abort
3333 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3334 * to EL3. Otherwise the fault is taken as an exception to EL2,
3335 * and HPFAR_EL2 holds the faulting IPA.
3336 */
3337 if (fi.type == ARMFault_SyncExternalOnWalk &&
3338 (env->cp15.scr_el3 & SCR_EA)) {
3339 target_el = 3;
3340 } else {
3341 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3342 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3343 env->cp15.hpfar_el2 |= HPFAR_NS;
3344 }
0710b2fa
PM
3345 target_el = 2;
3346 }
3347 take_exc = true;
3348 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3349 /*
3350 * Synchronous external aborts during a translation table walk
3351 * are taken as Data Abort exceptions.
3352 */
3353 if (fi.stage2) {
3354 if (current_el == 3) {
3355 target_el = 3;
3356 } else {
3357 target_el = 2;
3358 }
3359 } else {
3360 target_el = exception_target_el(env);
3361 }
3362 take_exc = true;
3363 }
3364
3365 if (take_exc) {
3366 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3367 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3368 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3369 fsr = arm_fi_to_lfsc(&fi);
3370 fsc = extract32(fsr, 0, 6);
3371 } else {
3372 fsr = arm_fi_to_sfsc(&fi);
3373 fsc = 0x3f;
3374 }
3375 /*
3376 * Report exception with ESR indicating a fault due to a
3377 * translation table walk for a cache maintenance instruction.
3378 */
e24fd076 3379 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3380 fi.ea, 1, fi.s1ptw, 1, fsc);
3381 env->exception.vaddress = value;
3382 env->exception.fsr = fsr;
3383 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3384 }
3385 }
3386
1313e2d7
EI
3387 if (is_a64(env)) {
3388 format64 = true;
3389 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3390 /*
3391 * ATS1Cxx:
3392 * * TTBCR.EAE determines whether the result is returned using the
3393 * 32-bit or the 64-bit PAR format
3394 * * Instructions executed in Hyp mode always use the 64bit format
3395 *
3396 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3397 * * The Non-secure TTBCR.EAE bit is set to 1
3398 * * The implementation includes EL2, and the value of HCR.VM is 1
3399 *
9d1bab33
PM
3400 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3401 *
23463e0e 3402 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3403 */
3404 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3405
3406 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3407 if (mmu_idx == ARMMMUIdx_E10_0 ||
3408 mmu_idx == ARMMMUIdx_E10_1 ||
3409 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3410 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3411 } else {
3412 format64 |= arm_current_el(env) == 2;
3413 }
3414 }
3415 }
3416
3417 if (format64) {
5efe9ed4 3418 /* Create a 64-bit PAR */
01c097f7 3419 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3420 if (!ret) {
7fa7ea8f
RH
3421 par64 |= res.f.phys_addr & ~0xfffULL;
3422 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3423 par64 |= (1 << 9); /* NS */
3424 }
de05a709
RH
3425 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3426 par64 |= res.cacheattrs.shareability << 7; /* SH */
4a501606 3427 } else {
5efe9ed4
PM
3428 uint32_t fsr = arm_fi_to_lfsc(&fi);
3429
702a9357 3430 par64 |= 1; /* F */
b7cc4e82 3431 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3432 if (fi.stage2) {
3433 par64 |= (1 << 9); /* S */
3434 }
3435 if (fi.s1ptw) {
3436 par64 |= (1 << 8); /* PTW */
3437 }
4a501606
PM
3438 }
3439 } else {
9b37a28c
FR
3440 /*
3441 * fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3442 * translation table format (with WnR always clear).
3443 * Convert it to a 32-bit PAR.
3444 */
b7cc4e82 3445 if (!ret) {
702a9357 3446 /* We do not set any attribute bits in the PAR */
7fa7ea8f 3447 if (res.f.lg_page_size == 24
702a9357 3448 && arm_feature(env, ARM_FEATURE_V7)) {
7fa7ea8f 3449 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
702a9357 3450 } else {
7fa7ea8f 3451 par64 = res.f.phys_addr & 0xfffff000;
702a9357 3452 }
7fa7ea8f 3453 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3454 par64 |= (1 << 9); /* NS */
3455 }
702a9357 3456 } else {
5efe9ed4
PM
3457 uint32_t fsr = arm_fi_to_sfsc(&fi);
3458
b7cc4e82
PC
3459 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3460 ((fsr & 0xf) << 1) | 1;
702a9357 3461 }
4a501606 3462 }
060e8a48
PM
3463 return par64;
3464}
9fb005b0 3465#endif /* CONFIG_TCG */
060e8a48
PM
3466
3467static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3468{
9fb005b0 3469#ifdef CONFIG_TCG
03ae85f8 3470 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3471 uint64_t par64;
d3649702
PM
3472 ARMMMUIdx mmu_idx;
3473 int el = arm_current_el(env);
3474 bool secure = arm_is_secure_below_el3(env);
060e8a48 3475
d3649702
PM
3476 switch (ri->opc2 & 6) {
3477 case 0:
04b07d29 3478 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3479 switch (el) {
3480 case 3:
d902ae75 3481 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3482 secure = true;
d3649702
PM
3483 break;
3484 case 2:
b6ad6062 3485 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3486 /* fall through */
d3649702 3487 case 1:
04b07d29 3488 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
d902ae75 3489 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3490 } else {
d902ae75 3491 mmu_idx = ARMMMUIdx_Stage1_E1;
04b07d29 3492 }
d3649702
PM
3493 break;
3494 default:
3495 g_assert_not_reached();
3496 }
3497 break;
3498 case 2:
3499 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3500 switch (el) {
3501 case 3:
d902ae75 3502 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3503 secure = true;
d3649702
PM
3504 break;
3505 case 2:
b1a10c86 3506 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3507 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3508 break;
3509 case 1:
d902ae75 3510 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3511 break;
3512 default:
3513 g_assert_not_reached();
3514 }
3515 break;
3516 case 4:
3517 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3518 mmu_idx = ARMMMUIdx_E10_1;
7aee3cb9 3519 secure = false;
d3649702
PM
3520 break;
3521 case 6:
3522 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3523 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3524 secure = false;
d3649702
PM
3525 break;
3526 default:
3527 g_assert_not_reached();
3528 }
3529
7aee3cb9 3530 par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
01c097f7
FA
3531
3532 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3533#else
3534 /* Handled by hardware accelerator. */
3535 g_assert_not_reached();
3536#endif /* CONFIG_TCG */
4a501606 3537}
060e8a48 3538
14db7fe0
PM
3539static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3540 uint64_t value)
3541{
9fb005b0 3542#ifdef CONFIG_TCG
03ae85f8 3543 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3544 uint64_t par64;
3545
7aee3cb9
RH
3546 /* There is no SecureEL2 for AArch32. */
3547 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
14db7fe0
PM
3548
3549 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3550#else
3551 /* Handled by hardware accelerator. */
3552 g_assert_not_reached();
3553#endif /* CONFIG_TCG */
14db7fe0
PM
3554}
3555
3f208fd7
PM
3556static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3557 bool isread)
2a47df95 3558{
926c1b97
RDC
3559 if (arm_current_el(env) == 3 &&
3560 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3561 return CP_ACCESS_TRAP;
3562 }
3563 return CP_ACCESS_OK;
3564}
3565
060e8a48
PM
3566static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3567 uint64_t value)
3568{
9fb005b0 3569#ifdef CONFIG_TCG
03ae85f8 3570 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3571 ARMMMUIdx mmu_idx;
3572 int secure = arm_is_secure_below_el3(env);
638d5dbd
AK
3573 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3574 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
d3649702
PM
3575
3576 switch (ri->opc2 & 6) {
3577 case 0:
3578 switch (ri->opc1) {
04b07d29
RH
3579 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3580 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
638d5dbd
AK
3581 mmu_idx = regime_e20 ?
3582 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3583 } else {
638d5dbd 3584 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
04b07d29 3585 }
d3649702
PM
3586 break;
3587 case 4: /* AT S1E2R, AT S1E2W */
638d5dbd 3588 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
d3649702
PM
3589 break;
3590 case 6: /* AT S1E3R, AT S1E3W */
d902ae75 3591 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3592 secure = true;
d3649702
PM
3593 break;
3594 default:
3595 g_assert_not_reached();
3596 }
3597 break;
3598 case 2: /* AT S1E0R, AT S1E0W */
638d5dbd 3599 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3600 break;
3601 case 4: /* AT S12E1R, AT S12E1W */
638d5dbd 3602 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
d3649702
PM
3603 break;
3604 case 6: /* AT S12E0R, AT S12E0W */
638d5dbd 3605 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
d3649702
PM
3606 break;
3607 default:
3608 g_assert_not_reached();
3609 }
060e8a48 3610
7aee3cb9
RH
3611 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3612 mmu_idx, secure);
9fb005b0
PMD
3613#else
3614 /* Handled by hardware accelerator. */
3615 g_assert_not_reached();
3616#endif /* CONFIG_TCG */
060e8a48 3617}
4a501606
PM
3618#endif
3619
3620static const ARMCPRegInfo vapa_cp_reginfo[] = {
3621 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3622 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3623 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3624 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3625 .writefn = par_write },
3626#ifndef CONFIG_USER_ONLY
87562e4f 3627 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3628 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3629 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3630 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3631#endif
4a501606
PM
3632};
3633
18032bec
PM
3634/* Return basic MPU access permission bits. */
3635static uint32_t simple_mpu_ap_bits(uint32_t val)
3636{
3637 uint32_t ret;
3638 uint32_t mask;
3639 int i;
3640 ret = 0;
3641 mask = 3;
3642 for (i = 0; i < 16; i += 2) {
3643 ret |= (val >> i) & mask;
3644 mask <<= 2;
3645 }
3646 return ret;
3647}
3648
3649/* Pad basic MPU access permission bits to extended format. */
3650static uint32_t extended_mpu_ap_bits(uint32_t val)
3651{
3652 uint32_t ret;
3653 uint32_t mask;
3654 int i;
3655 ret = 0;
3656 mask = 3;
3657 for (i = 0; i < 16; i += 2) {
3658 ret |= (val & mask) << i;
3659 mask <<= 2;
3660 }
3661 return ret;
3662}
3663
c4241c7d
PM
3664static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3665 uint64_t value)
18032bec 3666{
7e09797c 3667 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3668}
3669
c4241c7d 3670static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3671{
7e09797c 3672 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3673}
3674
c4241c7d
PM
3675static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3676 uint64_t value)
18032bec 3677{
7e09797c 3678 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3679}
3680
c4241c7d 3681static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3682{
7e09797c 3683 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3684}
3685
6cb0b013
PC
3686static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3687{
3688 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3689
3690 if (!u32p) {
3691 return 0;
3692 }
3693
1bc04a88 3694 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3695 return *u32p;
3696}
3697
3698static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3699 uint64_t value)
3700{
2fc0cc0e 3701 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3702 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3703
3704 if (!u32p) {
3705 return;
3706 }
3707
1bc04a88 3708 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3709 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3710 *u32p = value;
3711}
3712
6cb0b013
PC
3713static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3714 uint64_t value)
3715{
2fc0cc0e 3716 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3717 uint32_t nrgs = cpu->pmsav7_dregion;
3718
3719 if (value >= nrgs) {
3720 qemu_log_mask(LOG_GUEST_ERROR,
3721 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3722 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3723 return;
3724 }
3725
3726 raw_write(env, ri, value);
3727}
3728
761c4642
TR
3729static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3730 uint64_t value)
3731{
3732 ARMCPU *cpu = env_archcpu(env);
3733
3734 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3735 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3736}
3737
3738static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3739{
3740 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3741}
3742
3743static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3744 uint64_t value)
3745{
3746 ARMCPU *cpu = env_archcpu(env);
3747
3748 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3749 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3750}
3751
3752static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3753{
3754 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3755}
3756
3757static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3758 uint64_t value)
3759{
3760 ARMCPU *cpu = env_archcpu(env);
3761
3762 /*
3763 * Ignore writes that would select not implemented region.
3764 * This is architecturally UNPREDICTABLE.
3765 */
3766 if (value >= cpu->pmsav7_dregion) {
3767 return;
3768 }
3769
3770 env->pmsav7.rnr[M_REG_NS] = value;
3771}
3772
3773static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3774 uint64_t value)
3775{
3776 ARMCPU *cpu = env_archcpu(env);
3777
3778 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3779 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3780}
3781
3782static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3783{
3784 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3785}
3786
3787static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3788 uint64_t value)
3789{
3790 ARMCPU *cpu = env_archcpu(env);
3791
3792 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3793 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3794}
3795
3796static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3797{
3798 return env->pmsav8.hprlar[env->pmsav8.hprselr];
3799}
3800
3801static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3802 uint64_t value)
3803{
3804 uint32_t n;
3805 uint32_t bit;
3806 ARMCPU *cpu = env_archcpu(env);
3807
3808 /* Ignore writes to unimplemented regions */
3809 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3810 value &= MAKE_64BIT_MASK(0, rmax);
3811
3812 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3813
3814 /* Register alias is only valid for first 32 indexes */
3815 for (n = 0; n < rmax; ++n) {
3816 bit = extract32(value, n, 1);
3817 env->pmsav8.hprlar[n] = deposit32(
3818 env->pmsav8.hprlar[n], 0, 1, bit);
3819 }
3820}
3821
3822static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3823{
3824 uint32_t n;
3825 uint32_t result = 0x0;
3826 ARMCPU *cpu = env_archcpu(env);
3827
3828 /* Register alias is only valid for first 32 indexes */
3829 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3830 if (env->pmsav8.hprlar[n] & 0x1) {
3831 result |= (0x1 << n);
3832 }
3833 }
3834 return result;
3835}
3836
3837static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3838 uint64_t value)
3839{
3840 ARMCPU *cpu = env_archcpu(env);
3841
3842 /*
3843 * Ignore writes that would select not implemented region.
3844 * This is architecturally UNPREDICTABLE.
3845 */
3846 if (value >= cpu->pmsav8r_hdregion) {
3847 return;
3848 }
3849
3850 env->pmsav8.hprselr = value;
3851}
3852
3853static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3854 uint64_t value)
3855{
3856 ARMCPU *cpu = env_archcpu(env);
3857 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3858 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3859
3860 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3861
3862 if (ri->opc1 & 4) {
3863 if (index >= cpu->pmsav8r_hdregion) {
3864 return;
3865 }
3866 if (ri->opc2 & 0x1) {
3867 env->pmsav8.hprlar[index] = value;
3868 } else {
3869 env->pmsav8.hprbar[index] = value;
3870 }
3871 } else {
3872 if (index >= cpu->pmsav7_dregion) {
3873 return;
3874 }
3875 if (ri->opc2 & 0x1) {
3876 env->pmsav8.rlar[M_REG_NS][index] = value;
3877 } else {
3878 env->pmsav8.rbar[M_REG_NS][index] = value;
3879 }
3880 }
3881}
3882
3883static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3884{
3885 ARMCPU *cpu = env_archcpu(env);
3886 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3887 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3888
3889 if (ri->opc1 & 4) {
3890 if (index >= cpu->pmsav8r_hdregion) {
3891 return 0x0;
3892 }
3893 if (ri->opc2 & 0x1) {
3894 return env->pmsav8.hprlar[index];
3895 } else {
3896 return env->pmsav8.hprbar[index];
3897 }
3898 } else {
3899 if (index >= cpu->pmsav7_dregion) {
3900 return 0x0;
3901 }
3902 if (ri->opc2 & 0x1) {
3903 return env->pmsav8.rlar[M_REG_NS][index];
3904 } else {
3905 return env->pmsav8.rbar[M_REG_NS][index];
3906 }
3907 }
3908}
3909
3910static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
3911 { .name = "PRBAR",
3912 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
3913 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3914 .accessfn = access_tvm_trvm,
3915 .readfn = prbar_read, .writefn = prbar_write },
3916 { .name = "PRLAR",
3917 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
3918 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3919 .accessfn = access_tvm_trvm,
3920 .readfn = prlar_read, .writefn = prlar_write },
3921 { .name = "PRSELR", .resetvalue = 0,
3922 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
3923 .access = PL1_RW, .accessfn = access_tvm_trvm,
3924 .writefn = prselr_write,
3925 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
3926 { .name = "HPRBAR", .resetvalue = 0,
3927 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
3928 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3929 .readfn = hprbar_read, .writefn = hprbar_write },
3930 { .name = "HPRLAR",
3931 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
3932 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3933 .readfn = hprlar_read, .writefn = hprlar_write },
3934 { .name = "HPRSELR", .resetvalue = 0,
3935 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
3936 .access = PL2_RW,
3937 .writefn = hprselr_write,
3938 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
3939 { .name = "HPRENR",
3940 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
3941 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3942 .readfn = hprenr_read, .writefn = hprenr_write },
3943};
3944
6cb0b013 3945static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
9b37a28c
FR
3946 /*
3947 * Reset for all these registers is handled in arm_cpu_reset(),
69ceea64
PM
3948 * because the PMSAv7 is also used by M-profile CPUs, which do
3949 * not register cpregs but still need the state to be reset.
3950 */
6cb0b013
PC
3951 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3952 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3953 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3954 .readfn = pmsav7_read, .writefn = pmsav7_write,
3955 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3956 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3957 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3958 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3959 .readfn = pmsav7_read, .writefn = pmsav7_write,
3960 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3961 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3962 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3963 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3964 .readfn = pmsav7_read, .writefn = pmsav7_write,
3965 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3966 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3967 .access = PL1_RW,
1bc04a88 3968 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3969 .writefn = pmsav7_rgnr_write,
3970 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3971};
3972
18032bec
PM
3973static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3974 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3975 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3976 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3977 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3978 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3979 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3980 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3981 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3982 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3983 .access = PL1_RW,
7e09797c
PM
3984 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3985 .resetvalue = 0, },
18032bec
PM
3986 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3987 .access = PL1_RW,
7e09797c
PM
3988 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3989 .resetvalue = 0, },
ecce5c3c
PM
3990 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3991 .access = PL1_RW,
3992 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3993 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3994 .access = PL1_RW,
3995 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3996 /* Protection region base and size registers */
e508a92b
PM
3997 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3998 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3999 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4000 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4001 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4002 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4003 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4004 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4005 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4006 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4007 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4008 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4009 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4010 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4011 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4012 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4013 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4014 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4015 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4016 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4017 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4018 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4019 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4020 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
4021};
4022
cb4a0a34
PM
4023static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4024 uint64_t value)
ecce5c3c 4025{
cb4a0a34 4026 ARMCPU *cpu = env_archcpu(env);
2ebcebe2 4027
e389be16
FA
4028 if (!arm_feature(env, ARM_FEATURE_V8)) {
4029 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
cb4a0a34
PM
4030 /*
4031 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4032 * using Long-descriptor translation table format
4033 */
e389be16
FA
4034 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4035 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
cb4a0a34
PM
4036 /*
4037 * In an implementation that includes the Security Extensions
e389be16
FA
4038 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4039 * Short-descriptor translation table format.
4040 */
4041 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4042 } else {
4043 value &= TTBCR_N;
4044 }
e42c4db3 4045 }
e389be16 4046
d4e6df63 4047 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9b37a28c
FR
4048 /*
4049 * With LPAE the TTBCR could result in a change of ASID
d4e6df63
PM
4050 * via the TTBCR.A1 bit, so do a TLB flush.
4051 */
d10eb08f 4052 tlb_flush(CPU(cpu));
d4e6df63 4053 }
cb4a0a34 4054 raw_write(env, ri, value);
ecce5c3c
PM
4055}
4056
d06dc933 4057static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
4058 uint64_t value)
4059{
2fc0cc0e 4060 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 4061
cb2e37df 4062 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 4063 tlb_flush(CPU(cpu));
cb4a0a34 4064 raw_write(env, ri, value);
cb2e37df
PM
4065}
4066
327ed10f
PM
4067static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4068 uint64_t value)
4069{
93f379b0
RH
4070 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4071 if (cpreg_field_is_64bit(ri) &&
4072 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 4073 ARMCPU *cpu = env_archcpu(env);
d10eb08f 4074 tlb_flush(CPU(cpu));
327ed10f
PM
4075 }
4076 raw_write(env, ri, value);
4077}
4078
ed30da8e
RH
4079static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4080 uint64_t value)
4081{
d06dc933
RH
4082 /*
4083 * If we are running with E2&0 regime, then an ASID is active.
4084 * Flush if that might be changing. Note we're not checking
4085 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4086 * holds the active ASID, only checking the field that might.
4087 */
4088 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4089 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
4090 uint16_t mask = ARMMMUIdxBit_E20_2 |
4091 ARMMMUIdxBit_E20_2_PAN |
4092 ARMMMUIdxBit_E20_0;
b6ad6062 4093 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 4094 }
ed30da8e
RH
4095 raw_write(env, ri, value);
4096}
4097
b698e9cf
EI
4098static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4099 uint64_t value)
4100{
2fc0cc0e 4101 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
4102 CPUState *cs = CPU(cpu);
4103
97fa9350
RH
4104 /*
4105 * A change in VMID to the stage2 page table (Stage2) invalidates
575a94af 4106 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
97fa9350 4107 */
00b20ee4 4108 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
575a94af 4109 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
b698e9cf 4110 }
00b20ee4 4111 raw_write(env, ri, value);
b698e9cf
EI
4112}
4113
8e5d75c9 4114static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4115 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4116 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4117 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4118 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4119 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4120 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4121 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4122 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4123 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4124 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4125 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4126 offsetof(CPUARMState, cp15.dfar_ns) } },
4127 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4128 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
4129 .access = PL1_RW, .accessfn = access_tvm_trvm,
4130 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 4131 .resetvalue = 0, },
8e5d75c9
PC
4132};
4133
4134static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4135 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4136 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4137 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 4138 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4139 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4140 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
4141 .access = PL1_RW, .accessfn = access_tvm_trvm,
4142 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4143 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4144 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4145 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4146 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
4147 .access = PL1_RW, .accessfn = access_tvm_trvm,
4148 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4149 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4150 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4151 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4152 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4153 .access = PL1_RW, .accessfn = access_tvm_trvm,
4154 .writefn = vmsa_tcr_el12_write,
cb4a0a34
PM
4155 .raw_writefn = raw_write,
4156 .resetvalue = 0,
11f136ee 4157 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4158 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4159 .access = PL1_RW, .accessfn = access_tvm_trvm,
4160 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
cb4a0a34
PM
4161 .raw_writefn = raw_write,
4162 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4163 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4164};
4165
9b37a28c
FR
4166/*
4167 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
ab638a32
RH
4168 * qemu tlbs nor adjusting cached masks.
4169 */
4170static const ARMCPRegInfo ttbcr2_reginfo = {
4171 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4172 .access = PL1_RW, .accessfn = access_tvm_trvm,
4173 .type = ARM_CP_ALIAS,
d102058e 4174 .bank_fieldoffsets = {
cb4a0a34
PM
4175 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4176 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
d102058e 4177 },
ab638a32
RH
4178};
4179
c4241c7d
PM
4180static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4181 uint64_t value)
1047b9d7
PM
4182{
4183 env->cp15.c15_ticonfig = value & 0xe7;
4184 /* The OS_TYPE bit in this register changes the reported CPUID! */
4185 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4186 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4187}
4188
c4241c7d
PM
4189static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4190 uint64_t value)
1047b9d7
PM
4191{
4192 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4193}
4194
c4241c7d
PM
4195static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4196 uint64_t value)
1047b9d7
PM
4197{
4198 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4199 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4200}
4201
c4241c7d
PM
4202static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4203 uint64_t value)
c4804214 4204{
9b37a28c
FR
4205 /*
4206 * On OMAP there are registers indicating the max/min index of dcache lines
c4804214
PM
4207 * containing a dirty line; cache flush operations have to reset these.
4208 */
4209 env->cp15.c15_i_max = 0x000;
4210 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4211}
4212
18032bec
PM
4213static const ARMCPRegInfo omap_cp_reginfo[] = {
4214 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4215 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4216 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4217 .resetvalue = 0, },
1047b9d7
PM
4218 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4219 .access = PL1_RW, .type = ARM_CP_NOP },
4220 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4221 .access = PL1_RW,
4222 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4223 .writefn = omap_ticonfig_write },
4224 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4225 .access = PL1_RW,
4226 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4227 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4228 .access = PL1_RW, .resetvalue = 0xff0,
4229 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4230 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4231 .access = PL1_RW,
4232 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4233 .writefn = omap_threadid_write },
4234 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4235 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4236 .type = ARM_CP_NO_RAW,
1047b9d7 4237 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
9b37a28c
FR
4238 /*
4239 * TODO: Peripheral port remap register:
1047b9d7
PM
4240 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4241 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4242 * when MMU is off.
4243 */
c4804214 4244 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4245 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4246 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4247 .writefn = omap_cachemaint_write },
34f90529
PM
4248 { .name = "C9", .cp = 15, .crn = 9,
4249 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4250 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4251};
4252
c4241c7d
PM
4253static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4254 uint64_t value)
1047b9d7 4255{
c0f4af17 4256 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4257}
4258
4259static const ARMCPRegInfo xscale_cp_reginfo[] = {
4260 { .name = "XSCALE_CPAR",
4261 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4262 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4263 .writefn = xscale_cpar_write, },
2771db27
PM
4264 { .name = "XSCALE_AUXCR",
4265 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4266 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4267 .resetvalue = 0, },
9b37a28c
FR
4268 /*
4269 * XScale specific cache-lockdown: since we have no cache we NOP these
3b771579
PM
4270 * and hope the guest does not really rely on cache behaviour.
4271 */
4272 { .name = "XSCALE_LOCK_ICACHE_LINE",
4273 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4274 .access = PL1_W, .type = ARM_CP_NOP },
4275 { .name = "XSCALE_UNLOCK_ICACHE",
4276 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4277 .access = PL1_W, .type = ARM_CP_NOP },
4278 { .name = "XSCALE_DCACHE_LOCK",
4279 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4280 .access = PL1_RW, .type = ARM_CP_NOP },
4281 { .name = "XSCALE_UNLOCK_DCACHE",
4282 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4283 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4284};
4285
4286static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
9b37a28c
FR
4287 /*
4288 * RAZ/WI the whole crn=15 space, when we don't have a more specific
1047b9d7
PM
4289 * implementation of this implementation-defined space.
4290 * Ideally this should eventually disappear in favour of actually
4291 * implementing the correct behaviour for all cores.
4292 */
4293 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4294 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4295 .access = PL1_RW,
7a0e58fa 4296 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4297 .resetvalue = 0 },
18032bec
PM
4298};
4299
c4804214
PM
4300static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4301 /* Cache status: RAZ because we have no cache so it's always clean */
4302 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4303 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4304 .resetvalue = 0 },
c4804214
PM
4305};
4306
4307static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
a07d9df0 4308 /* We never have a block transfer operation in progress */
c4804214 4309 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4310 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4311 .resetvalue = 0 },
30b05bba
PM
4312 /* The cache ops themselves: these all NOP for QEMU */
4313 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
04215eb1 4314 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4315 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
04215eb1 4316 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4317 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
04215eb1 4318 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4319 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
04215eb1 4320 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4321 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
04215eb1 4322 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4323 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
04215eb1 4324 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
c4804214
PM
4325};
4326
4327static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
9b37a28c
FR
4328 /*
4329 * The cache test-and-clean instructions always return (1 << 30)
c4804214
PM
4330 * to indicate that there are no dirty cache lines.
4331 */
4332 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4333 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4334 .resetvalue = (1 << 30) },
c4804214 4335 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4336 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4337 .resetvalue = (1 << 30) },
c4804214
PM
4338};
4339
34f90529
PM
4340static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4341 /* Ignore ReadBuffer accesses */
4342 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4343 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4344 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4345 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4346};
4347
731de9e6
EI
4348static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4349{
731de9e6 4350 unsigned int cur_el = arm_current_el(env);
731de9e6 4351
e6ef0169 4352 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4353 return env->cp15.vpidr_el2;
4354 }
4355 return raw_read(env, ri);
4356}
4357
06a7e647 4358static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4359{
2fc0cc0e 4360 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4361 uint64_t mpidr = cpu->mp_affinity;
4362
81bdde9d 4363 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4364 mpidr |= (1U << 31);
9b37a28c
FR
4365 /*
4366 * Cores which are uniprocessor (non-coherent)
81bdde9d 4367 * but still implement the MP extensions set
a8e81b31 4368 * bit 30. (For instance, Cortex-R5).
81bdde9d 4369 */
a8e81b31
PC
4370 if (cpu->mp_is_up) {
4371 mpidr |= (1u << 30);
4372 }
81bdde9d 4373 }
c4241c7d 4374 return mpidr;
81bdde9d
PM
4375}
4376
06a7e647
EI
4377static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4378{
f0d574d6 4379 unsigned int cur_el = arm_current_el(env);
f0d574d6 4380
e6ef0169 4381 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4382 return env->cp15.vmpidr_el2;
4383 }
06a7e647
EI
4384 return mpidr_read_val(env);
4385}
4386
7ac681cf 4387static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4388 /* NOP AMAIR0/1 */
b0fe2427
PM
4389 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4390 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4391 .access = PL1_RW, .accessfn = access_tvm_trvm,
4392 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4393 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4394 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4395 .access = PL1_RW, .accessfn = access_tvm_trvm,
4396 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4397 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4398 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4399 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4400 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4401 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4402 .access = PL1_RW, .accessfn = access_tvm_trvm,
4403 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4404 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4405 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4406 .writefn = vmsa_ttbr_write, },
891a2fe7 4407 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4408 .access = PL1_RW, .accessfn = access_tvm_trvm,
4409 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4410 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4411 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4412 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4413};
4414
c4241c7d 4415static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4416{
c4241c7d 4417 return vfp_get_fpcr(env);
b0d2b7d0
PM
4418}
4419
c4241c7d
PM
4420static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4421 uint64_t value)
b0d2b7d0
PM
4422{
4423 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4424}
4425
c4241c7d 4426static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4427{
c4241c7d 4428 return vfp_get_fpsr(env);
b0d2b7d0
PM
4429}
4430
c4241c7d
PM
4431static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4432 uint64_t value)
b0d2b7d0
PM
4433{
4434 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4435}
4436
3f208fd7
PM
4437static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4438 bool isread)
c2b820fe 4439{
aaec1432 4440 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4441 return CP_ACCESS_TRAP;
4442 }
4443 return CP_ACCESS_OK;
4444}
4445
4446static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4447 uint64_t value)
4448{
4449 env->daif = value & PSTATE_DAIF;
4450}
4451
220f508f
RH
4452static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4453{
4454 return env->pstate & PSTATE_PAN;
4455}
4456
4457static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4458 uint64_t value)
4459{
4460 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4461}
4462
4463static const ARMCPRegInfo pan_reginfo = {
4464 .name = "PAN", .state = ARM_CP_STATE_AA64,
4465 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4466 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4467 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4468};
4469
9eeb7a1c
RH
4470static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4471{
4472 return env->pstate & PSTATE_UAO;
4473}
4474
4475static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4476 uint64_t value)
4477{
4478 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4479}
4480
4481static const ARMCPRegInfo uao_reginfo = {
4482 .name = "UAO", .state = ARM_CP_STATE_AA64,
4483 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4484 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4485 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4486};
4487
dc8b1853
RC
4488static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4489{
4490 return env->pstate & PSTATE_DIT;
4491}
4492
4493static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4494 uint64_t value)
4495{
4496 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4497}
4498
4499static const ARMCPRegInfo dit_reginfo = {
4500 .name = "DIT", .state = ARM_CP_STATE_AA64,
4501 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4502 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4503 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4504};
4505
f2f68a78
RC
4506static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4507{
4508 return env->pstate & PSTATE_SSBS;
4509}
4510
4511static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4512 uint64_t value)
4513{
4514 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4515}
4516
4517static const ARMCPRegInfo ssbs_reginfo = {
4518 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4519 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4520 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4521 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4522};
4523
38262d8a
RH
4524static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4525 const ARMCPRegInfo *ri,
4526 bool isread)
8af35c37 4527{
38262d8a
RH
4528 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4529 switch (arm_current_el(env)) {
4530 case 0:
4531 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4532 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4533 return CP_ACCESS_TRAP;
4534 }
4535 /* fall through */
4536 case 1:
4537 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4538 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4539 return CP_ACCESS_TRAP_EL2;
4540 }
4541 break;
8af35c37
PM
4542 }
4543 return CP_ACCESS_OK;
4544}
4545
2d3ce4c6 4546static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
1bed4d2e 4547{
38262d8a 4548 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4549 switch (arm_current_el(env)) {
4550 case 0:
4551 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4552 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4553 return CP_ACCESS_TRAP;
4554 }
4555 /* fall through */
4556 case 1:
2d3ce4c6
PM
4557 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4558 if (arm_hcr_el2_eff(env) & hcrflags) {
1bed4d2e
RH
4559 return CP_ACCESS_TRAP_EL2;
4560 }
4561 break;
4562 }
4563 return CP_ACCESS_OK;
4564}
4565
2d3ce4c6
PM
4566static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4567 bool isread)
4568{
4569 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4570}
4571
4572static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4573 bool isread)
4574{
4575 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4576}
4577
9b37a28c
FR
4578/*
4579 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
dbb1fb27
AB
4580 * Page D4-1736 (DDI0487A.b)
4581 */
4582
b7e0730d
RH
4583static int vae1_tlbmask(CPUARMState *env)
4584{
e04a5752 4585 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4586 uint16_t mask;
e04a5752
RDC
4587
4588 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4589 mask = ARMMMUIdxBit_E20_2 |
4590 ARMMMUIdxBit_E20_2_PAN |
4591 ARMMMUIdxBit_E20_0;
b7e0730d 4592 } else {
bc944d3a 4593 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4594 ARMMMUIdxBit_E10_1_PAN |
4595 ARMMMUIdxBit_E10_0;
b7e0730d 4596 }
bc944d3a 4597 return mask;
b7e0730d
RH
4598}
4599
ea04dce7
RH
4600/* Return 56 if TBI is enabled, 64 otherwise. */
4601static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4602 uint64_t addr)
4603{
c1547bba 4604 uint64_t tcr = regime_tcr(env, mmu_idx);
ea04dce7
RH
4605 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4606 int select = extract64(addr, 55, 1);
4607
4608 return (tbi >> select) & 1 ? 56 : 64;
4609}
4610
4611static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4612{
b6ad6062 4613 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4614 ARMMMUIdx mmu_idx;
4615
4616 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4617 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4618 mmu_idx = ARMMMUIdx_E20_0;
4619 } else {
4620 mmu_idx = ARMMMUIdx_E10_0;
4621 }
b6ad6062 4622
ea04dce7
RH
4623 return tlbbits_for_regime(env, mmu_idx, addr);
4624}
4625
fd3ed969
PM
4626static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4627 uint64_t value)
168aa23b 4628{
29a0af61 4629 CPUState *cs = env_cpu(env);
b7e0730d 4630 int mask = vae1_tlbmask(env);
dbb1fb27 4631
b7e0730d 4632 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4633}
4634
b4ab8ce9
PM
4635static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4636 uint64_t value)
4637{
29a0af61 4638 CPUState *cs = env_cpu(env);
b7e0730d 4639 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4640
4641 if (tlb_force_broadcast(env)) {
527db2be
RH
4642 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4643 } else {
4644 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4645 }
b4ab8ce9
PM
4646}
4647
85d0dc9f
RH
4648static int e2_tlbmask(CPUARMState *env)
4649{
d902ae75
RH
4650 return (ARMMMUIdxBit_E20_0 |
4651 ARMMMUIdxBit_E20_2 |
4652 ARMMMUIdxBit_E20_2_PAN |
4653 ARMMMUIdxBit_E2);
85d0dc9f
RH
4654}
4655
90c19cdf
RH
4656static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4657 uint64_t value)
4658{
4659 CPUState *cs = env_cpu(env);
4660 int mask = alle1_tlbmask(env);
4661
4662 tlb_flush_by_mmuidx(cs, mask);
4663}
4664
fd3ed969 4665static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4666 uint64_t value)
4667{
85d0dc9f
RH
4668 CPUState *cs = env_cpu(env);
4669 int mask = e2_tlbmask(env);
fd3ed969 4670
85d0dc9f 4671 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4672}
4673
43efaa33
PM
4674static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4675 uint64_t value)
4676{
2fc0cc0e 4677 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4678 CPUState *cs = CPU(cpu);
4679
d902ae75 4680 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4681}
4682
fd3ed969
PM
4683static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4684 uint64_t value)
4685{
29a0af61 4686 CPUState *cs = env_cpu(env);
90c19cdf
RH
4687 int mask = alle1_tlbmask(env);
4688
4689 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4690}
4691
2bfb9d75
PM
4692static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4693 uint64_t value)
4694{
29a0af61 4695 CPUState *cs = env_cpu(env);
85d0dc9f 4696 int mask = e2_tlbmask(env);
2bfb9d75 4697
85d0dc9f 4698 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4699}
4700
43efaa33
PM
4701static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4702 uint64_t value)
4703{
29a0af61 4704 CPUState *cs = env_cpu(env);
43efaa33 4705
d902ae75 4706 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4707}
4708
fd3ed969
PM
4709static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4710 uint64_t value)
fa439fc5 4711{
9b37a28c
FR
4712 /*
4713 * Invalidate by VA, EL2
fd3ed969
PM
4714 * Currently handles both VAE2 and VALE2, since we don't support
4715 * flush-last-level-only.
4716 */
85d0dc9f
RH
4717 CPUState *cs = env_cpu(env);
4718 int mask = e2_tlbmask(env);
fd3ed969
PM
4719 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4720
85d0dc9f 4721 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4722}
4723
43efaa33
PM
4724static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4725 uint64_t value)
4726{
9b37a28c
FR
4727 /*
4728 * Invalidate by VA, EL3
43efaa33
PM
4729 * Currently handles both VAE3 and VALE3, since we don't support
4730 * flush-last-level-only.
4731 */
2fc0cc0e 4732 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4733 CPUState *cs = CPU(cpu);
4734 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4735
d902ae75 4736 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
43efaa33
PM
4737}
4738
fd3ed969
PM
4739static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4740 uint64_t value)
4741{
90c19cdf
RH
4742 CPUState *cs = env_cpu(env);
4743 int mask = vae1_tlbmask(env);
fa439fc5 4744 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4745 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4746
ea04dce7 4747 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4748}
4749
b4ab8ce9
PM
4750static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4751 uint64_t value)
4752{
9b37a28c
FR
4753 /*
4754 * Invalidate by VA, EL1&0 (AArch64 version).
b4ab8ce9
PM
4755 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4756 * since we don't support flush-for-specific-ASID-only or
4757 * flush-last-level-only.
4758 */
90c19cdf
RH
4759 CPUState *cs = env_cpu(env);
4760 int mask = vae1_tlbmask(env);
b4ab8ce9 4761 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4762 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4763
4764 if (tlb_force_broadcast(env)) {
ea04dce7 4765 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4766 } else {
ea04dce7 4767 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4768 }
b4ab8ce9
PM
4769}
4770
fd3ed969
PM
4771static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4772 uint64_t value)
fa439fc5 4773{
29a0af61 4774 CPUState *cs = env_cpu(env);
fd3ed969 4775 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4776 int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
fa439fc5 4777
d902ae75
RH
4778 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4779 ARMMMUIdxBit_E2, bits);
fa439fc5
PM
4780}
4781
43efaa33
PM
4782static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4783 uint64_t value)
4784{
29a0af61 4785 CPUState *cs = env_cpu(env);
43efaa33 4786 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4787 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
43efaa33 4788
ea04dce7 4789 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
d902ae75 4790 ARMMMUIdxBit_E3, bits);
43efaa33
PM
4791}
4792
575a94af
RH
4793static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4794{
4795 /*
4796 * The MSB of value is the NS field, which only applies if SEL2
4797 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4798 */
4799 return (value >= 0
4800 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4801 && arm_is_secure_below_el3(env)
4802 ? ARMMMUIdxBit_Stage2_S
4803 : ARMMMUIdxBit_Stage2);
4804}
4805
4806static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4807 uint64_t value)
4808{
4809 CPUState *cs = env_cpu(env);
4810 int mask = ipas2e1_tlbmask(env, value);
4811 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4812
4813 if (tlb_force_broadcast(env)) {
4814 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4815 } else {
4816 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4817 }
4818}
4819
4820static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4821 uint64_t value)
4822{
4823 CPUState *cs = env_cpu(env);
4824 int mask = ipas2e1_tlbmask(env, value);
4825 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4826
4827 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4828}
4829
84940ed8 4830#ifdef TARGET_AARCH64
ab1cdb47
RH
4831typedef struct {
4832 uint64_t base;
84940ed8 4833 uint64_t length;
ab1cdb47
RH
4834} TLBIRange;
4835
3c003f70
PM
4836static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4837{
4838 /*
4839 * Note that the TLBI range TG field encoding differs from both
4840 * TG0 and TG1 encodings.
4841 */
4842 switch (tg) {
4843 case 1:
4844 return Gran4K;
4845 case 2:
4846 return Gran16K;
4847 case 3:
4848 return Gran64K;
4849 default:
4850 return GranInvalid;
4851 }
4852}
4853
ab1cdb47
RH
4854static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4855 uint64_t value)
4856{
4857 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4858 /* Extract one bit to represent the va selector in use. */
4859 uint64_t select = sextract64(value, 36, 1);
4860 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4861 TLBIRange ret = { };
3c003f70 4862 ARMGranuleSize gran;
84940ed8 4863
84940ed8 4864 page_size_granule = extract64(value, 46, 2);
3c003f70 4865 gran = tlbi_range_tg_to_gran_size(page_size_granule);
84940ed8 4866
3974ff93 4867 /* The granule encoded in value must match the granule in use. */
3c003f70 4868 if (gran != param.gran) {
3974ff93 4869 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4870 page_size_granule);
ab1cdb47 4871 return ret;
84940ed8
RC
4872 }
4873
3c003f70 4874 page_shift = arm_granule_bits(gran);
ab1cdb47
RH
4875 num = extract64(value, 39, 5);
4876 scale = extract64(value, 44, 2);
84940ed8 4877 exponent = (5 * scale) + 1;
84940ed8 4878
ab1cdb47 4879 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4880
3974ff93 4881 if (param.select) {
d976de21 4882 ret.base = sextract64(value, 0, 37);
84940ed8 4883 } else {
d976de21 4884 ret.base = extract64(value, 0, 37);
84940ed8 4885 }
ef56c242
RH
4886 if (param.ds) {
4887 /*
4888 * With DS=1, BaseADDR is always shifted 16 so that it is able
4889 * to address all 52 va bits. The input address is perforce
4890 * aligned on a 64k boundary regardless of translation granule.
4891 */
4892 page_shift = 16;
4893 }
d976de21 4894 ret.base <<= page_shift;
84940ed8 4895
ab1cdb47 4896 return ret;
84940ed8
RC
4897}
4898
4899static void do_rvae_write(CPUARMState *env, uint64_t value,
4900 int idxmap, bool synced)
4901{
4902 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4903 TLBIRange range;
84940ed8
RC
4904 int bits;
4905
ab1cdb47
RH
4906 range = tlbi_aa64_get_range(env, one_idx, value);
4907 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4908
4909 if (synced) {
4910 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4911 range.base,
4912 range.length,
84940ed8
RC
4913 idxmap,
4914 bits);
4915 } else {
ab1cdb47
RH
4916 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4917 range.length, idxmap, bits);
84940ed8
RC
4918 }
4919}
4920
4921static void tlbi_aa64_rvae1_write(CPUARMState *env,
4922 const ARMCPRegInfo *ri,
4923 uint64_t value)
4924{
4925 /*
4926 * Invalidate by VA range, EL1&0.
4927 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4928 * since we don't support flush-for-specific-ASID-only or
4929 * flush-last-level-only.
4930 */
4931
4932 do_rvae_write(env, value, vae1_tlbmask(env),
4933 tlb_force_broadcast(env));
4934}
4935
4936static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4937 const ARMCPRegInfo *ri,
4938 uint64_t value)
4939{
4940 /*
4941 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4942 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4943 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4944 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4945 * shareable specific flushes.
4946 */
4947
4948 do_rvae_write(env, value, vae1_tlbmask(env), true);
4949}
4950
4951static int vae2_tlbmask(CPUARMState *env)
4952{
d902ae75 4953 return ARMMMUIdxBit_E2;
84940ed8
RC
4954}
4955
4956static void tlbi_aa64_rvae2_write(CPUARMState *env,
4957 const ARMCPRegInfo *ri,
4958 uint64_t value)
4959{
4960 /*
4961 * Invalidate by VA range, EL2.
4962 * Currently handles all of RVAE2 and RVALE2,
4963 * since we don't support flush-for-specific-ASID-only or
4964 * flush-last-level-only.
4965 */
4966
4967 do_rvae_write(env, value, vae2_tlbmask(env),
4968 tlb_force_broadcast(env));
4969
4970
4971}
4972
4973static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4974 const ARMCPRegInfo *ri,
4975 uint64_t value)
4976{
4977 /*
4978 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4979 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4980 * since we don't support flush-for-specific-ASID-only,
4981 * flush-last-level-only or inner/outer shareable specific flushes.
4982 */
4983
4984 do_rvae_write(env, value, vae2_tlbmask(env), true);
4985
4986}
4987
4988static void tlbi_aa64_rvae3_write(CPUARMState *env,
4989 const ARMCPRegInfo *ri,
4990 uint64_t value)
4991{
4992 /*
4993 * Invalidate by VA range, EL3.
4994 * Currently handles all of RVAE3 and RVALE3,
4995 * since we don't support flush-for-specific-ASID-only or
4996 * flush-last-level-only.
4997 */
4998
d902ae75 4999 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
84940ed8
RC
5000}
5001
5002static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5003 const ARMCPRegInfo *ri,
5004 uint64_t value)
5005{
5006 /*
5007 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5008 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5009 * since we don't support flush-for-specific-ASID-only,
5010 * flush-last-level-only or inner/outer specific flushes.
5011 */
5012
d902ae75 5013 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
84940ed8 5014}
575a94af
RH
5015
5016static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5017 uint64_t value)
5018{
5019 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5020 tlb_force_broadcast(env));
5021}
5022
5023static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5024 const ARMCPRegInfo *ri,
5025 uint64_t value)
5026{
5027 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5028}
84940ed8
RC
5029#endif
5030
3f208fd7
PM
5031static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5032 bool isread)
aca3f40b 5033{
4351cb72
RH
5034 int cur_el = arm_current_el(env);
5035
5036 if (cur_el < 2) {
5037 uint64_t hcr = arm_hcr_el2_eff(env);
5038
5039 if (cur_el == 0) {
5040 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5041 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5042 return CP_ACCESS_TRAP_EL2;
5043 }
5044 } else {
5045 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5046 return CP_ACCESS_TRAP;
5047 }
5048 if (hcr & HCR_TDZ) {
5049 return CP_ACCESS_TRAP_EL2;
5050 }
5051 }
5052 } else if (hcr & HCR_TDZ) {
5053 return CP_ACCESS_TRAP_EL2;
5054 }
aca3f40b
PM
5055 }
5056 return CP_ACCESS_OK;
5057}
5058
5059static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5060{
2fc0cc0e 5061 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
5062 int dzp_bit = 1 << 4;
5063
5064 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 5065 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
5066 dzp_bit = 0;
5067 }
5068 return cpu->dcz_blocksize | dzp_bit;
5069}
5070
3f208fd7
PM
5071static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5072 bool isread)
f502cfc2 5073{
cdcf1405 5074 if (!(env->pstate & PSTATE_SP)) {
9b37a28c
FR
5075 /*
5076 * Access to SP_EL0 is undefined if it's being used as
f502cfc2
PM
5077 * the stack pointer.
5078 */
5079 return CP_ACCESS_TRAP_UNCATEGORIZED;
5080 }
5081 return CP_ACCESS_OK;
5082}
5083
5084static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5085{
5086 return env->pstate & PSTATE_SP;
5087}
5088
5089static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5090{
5091 update_spsel(env, val);
5092}
5093
137feaa9
FA
5094static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5095 uint64_t value)
5096{
2fc0cc0e 5097 ARMCPU *cpu = env_archcpu(env);
137feaa9 5098
f00faf13
RH
5099 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5100 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5101 value &= ~SCTLR_M;
5102 }
5103
5104 /* ??? Lots of these bits are not implemented. */
5105
5106 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5107 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5108 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5109 } else {
5110 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5111 SCTLR_ATA0 | SCTLR_ATA);
5112 }
5113 }
5114
137feaa9 5115 if (raw_read(env, ri) == value) {
9b37a28c
FR
5116 /*
5117 * Skip the TLB flush if nothing actually changed; Linux likes
137feaa9
FA
5118 * to do a lot of pointless SCTLR writes.
5119 */
5120 return;
5121 }
5122
5123 raw_write(env, ri, value);
f00faf13 5124
137feaa9 5125 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 5126 tlb_flush(CPU(cpu));
2e5dcf36
RH
5127
5128 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
5129 /*
5130 * Normally we would always end the TB on an SCTLR write; see the
5131 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5132 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5133 * of hflags from the translator, so do it here.
5134 */
5135 arm_rebuild_hflags(env);
5136 }
137feaa9
FA
5137}
5138
80d2b43b
PM
5139static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5140 uint64_t value)
a8d64e73 5141{
01765386
PM
5142 /*
5143 * Some MDCR_EL3 bits affect whether PMU counters are running:
5144 * if we are trying to change any of those then we must
5145 * bracket this update with PMU start/finish calls.
5146 */
5147 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5148
5149 if (pmu_op) {
5150 pmu_op_start(env);
5151 }
80d2b43b 5152 env->cp15.mdcr_el3 = value;
01765386
PM
5153 if (pmu_op) {
5154 pmu_op_finish(env);
5155 }
5156}
5157
80d2b43b
PM
5158static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5159 uint64_t value)
5160{
5161 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5162 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5163}
5164
01765386
PM
5165static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5166 uint64_t value)
5167{
5168 /*
5169 * Some MDCR_EL2 bits affect whether PMU counters are running:
5170 * if we are trying to change any of those then we must
5171 * bracket this update with PMU start/finish calls.
5172 */
5173 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5174
5175 if (pmu_op) {
5176 pmu_op_start(env);
5177 }
5178 env->cp15.mdcr_el2 = value;
5179 if (pmu_op) {
5180 pmu_op_finish(env);
5181 }
a8d64e73
PM
5182}
5183
b0d2b7d0 5184static const ARMCPRegInfo v8_cp_reginfo[] = {
9b37a28c
FR
5185 /*
5186 * Minimal set of EL0-visible registers. This will need to be expanded
b0d2b7d0
PM
5187 * significantly for system emulation of AArch64 CPUs.
5188 */
5189 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5190 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5191 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
5192 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5193 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 5194 .type = ARM_CP_NO_RAW,
c2b820fe
PM
5195 .access = PL0_RW, .accessfn = aa64_daif_access,
5196 .fieldoffset = offsetof(CPUARMState, daif),
5197 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
5198 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5199 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 5200 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5201 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
5202 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5203 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 5204 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5205 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
5206 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5207 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 5208 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
5209 .readfn = aa64_dczid_read },
5210 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5211 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5212 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5213#ifndef CONFIG_USER_ONLY
5214 /* Avoid overhead of an access check that always passes in user-mode */
5215 .accessfn = aa64_zva_access,
5216#endif
5217 },
0eef9d98
PM
5218 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5219 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5220 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
5221 /* Cache ops: all NOPs since we don't emulate caches */
5222 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5223 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5224 .access = PL1_W, .type = ARM_CP_NOP,
2d3ce4c6 5225 .accessfn = access_ticab },
8af35c37
PM
5226 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5227 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5228 .access = PL1_W, .type = ARM_CP_NOP,
2d3ce4c6 5229 .accessfn = access_tocu },
8af35c37
PM
5230 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5231 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5232 .access = PL0_W, .type = ARM_CP_NOP,
2d3ce4c6 5233 .accessfn = access_tocu },
8af35c37
PM
5234 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5235 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
5236 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5237 .type = ARM_CP_NOP },
8af35c37
PM
5238 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5239 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5240 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5241 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5242 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5243 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 5244 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5245 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5246 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5247 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5248 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5249 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5250 .access = PL0_W, .type = ARM_CP_NOP,
2d3ce4c6 5251 .accessfn = access_tocu },
8af35c37
PM
5252 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5253 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5254 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 5255 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5256 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5257 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5258 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
5259 /* TLBI operations */
5260 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5261 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 5262 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5263 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5264 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5265 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 5266 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5267 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5268 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5269 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 5270 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5271 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5272 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5273 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 5274 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5275 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5276 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5277 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5278 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5279 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5280 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5281 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5282 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5283 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5284 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5285 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 5286 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5287 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5288 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5289 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 5290 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5291 .writefn = tlbi_aa64_vae1_write },
168aa23b 5292 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5293 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 5294 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5295 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5296 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5297 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 5298 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5299 .writefn = tlbi_aa64_vae1_write },
168aa23b 5300 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5301 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 5302 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5303 .writefn = tlbi_aa64_vae1_write },
168aa23b 5304 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5305 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 5306 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5307 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
5308 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5309 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5310 .access = PL2_W, .type = ARM_CP_NO_RAW,
5311 .writefn = tlbi_aa64_ipas2e1is_write },
cea66e91
PM
5312 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5313 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5314 .access = PL2_W, .type = ARM_CP_NO_RAW,
5315 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
5316 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5317 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5318 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5319 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
5320 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5321 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5322 .access = PL2_W, .type = ARM_CP_NO_RAW,
5323 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
5324 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5325 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5326 .access = PL2_W, .type = ARM_CP_NO_RAW,
5327 .writefn = tlbi_aa64_ipas2e1_write },
cea66e91
PM
5328 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5329 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5330 .access = PL2_W, .type = ARM_CP_NO_RAW,
5331 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
5332 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5333 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5334 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5335 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
5336 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5337 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5338 .access = PL2_W, .type = ARM_CP_NO_RAW,
5339 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
5340#ifndef CONFIG_USER_ONLY
5341 /* 64 bit address translation operations */
5342 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5343 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5344 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5345 .writefn = ats_write64 },
19525524
PM
5346 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5347 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5348 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5349 .writefn = ats_write64 },
19525524
PM
5350 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5351 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
5352 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5353 .writefn = ats_write64 },
19525524
PM
5354 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5355 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
5356 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5357 .writefn = ats_write64 },
2a47df95 5358 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 5359 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
5360 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5361 .writefn = ats_write64 },
2a47df95 5362 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 5363 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
5364 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5365 .writefn = ats_write64 },
2a47df95 5366 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 5367 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
5368 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5369 .writefn = ats_write64 },
2a47df95 5370 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 5371 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
5372 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5373 .writefn = ats_write64 },
2a47df95
PM
5374 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5375 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5376 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5377 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5378 .writefn = ats_write64 },
2a47df95
PM
5379 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5380 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5381 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5382 .writefn = ats_write64 },
c96fc9b5
EI
5383 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5384 .type = ARM_CP_ALIAS,
5385 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5386 .access = PL1_RW, .resetvalue = 0,
5387 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5388 .writefn = par_write },
19525524 5389#endif
995939a6 5390 /* TLB invalidate last level of translation table walk */
9449fdf6 5391 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5392 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 5393 .writefn = tlbimva_is_write },
9449fdf6 5394 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5395 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 5396 .writefn = tlbimvaa_is_write },
9449fdf6 5397 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
5398 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5399 .writefn = tlbimva_write },
9449fdf6 5400 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5401 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5402 .writefn = tlbimvaa_write },
541ef8c2
SS
5403 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5404 .type = ARM_CP_NO_RAW, .access = PL2_W,
5405 .writefn = tlbimva_hyp_write },
5406 { .name = "TLBIMVALHIS",
5407 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5408 .type = ARM_CP_NO_RAW, .access = PL2_W,
5409 .writefn = tlbimva_hyp_is_write },
5410 { .name = "TLBIIPAS2",
5411 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5412 .type = ARM_CP_NO_RAW, .access = PL2_W,
5413 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5414 { .name = "TLBIIPAS2IS",
5415 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5416 .type = ARM_CP_NO_RAW, .access = PL2_W,
5417 .writefn = tlbiipas2is_hyp_write },
541ef8c2
SS
5418 { .name = "TLBIIPAS2L",
5419 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5420 .type = ARM_CP_NO_RAW, .access = PL2_W,
5421 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5422 { .name = "TLBIIPAS2LIS",
5423 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5424 .type = ARM_CP_NO_RAW, .access = PL2_W,
5425 .writefn = tlbiipas2is_hyp_write },
9449fdf6
PM
5426 /* 32 bit cache operations */
5427 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2d3ce4c6 5428 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
9449fdf6
PM
5429 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5430 .type = ARM_CP_NOP, .access = PL1_W },
5431 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2d3ce4c6 5432 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5433 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2d3ce4c6 5434 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6
PM
5435 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5436 .type = ARM_CP_NOP, .access = PL1_W },
5437 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5438 .type = ARM_CP_NOP, .access = PL1_W },
5439 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5440 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5441 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5442 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5443 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5444 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5445 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5446 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5447 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2d3ce4c6 5448 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5449 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5450 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5451 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5452 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5453 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5454 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5455 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5456 .writefn = dacr_write, .raw_writefn = raw_write,
5457 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5458 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5459 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5460 .type = ARM_CP_ALIAS,
a0618a19 5461 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5462 .access = PL1_RW,
5463 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5464 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5465 .type = ARM_CP_ALIAS,
a65f1de9 5466 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5467 .access = PL1_RW,
5468 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
9b37a28c
FR
5469 /*
5470 * We rely on the access checks not allowing the guest to write to the
f502cfc2
PM
5471 * state field when SPSel indicates that it's being used as the stack
5472 * pointer.
5473 */
5474 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5475 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5476 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5477 .type = ARM_CP_ALIAS,
f502cfc2 5478 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5479 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5480 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
beeec926 5481 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
884b4dee 5482 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5483 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5484 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5485 .type = ARM_CP_NO_RAW,
f502cfc2 5486 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5487 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5488 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5489 .access = PL2_RW,
5490 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5491 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5492 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5493 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5494 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5495 .writefn = dacr_write, .raw_writefn = raw_write,
5496 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5497 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5498 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5499 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5500 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5501 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5502 .type = ARM_CP_ALIAS,
5503 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5504 .access = PL2_RW,
5505 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5506 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5507 .type = ARM_CP_ALIAS,
5508 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5509 .access = PL2_RW,
5510 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5511 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5512 .type = ARM_CP_ALIAS,
5513 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5514 .access = PL2_RW,
5515 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5516 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5517 .type = ARM_CP_ALIAS,
5518 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5519 .access = PL2_RW,
5520 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73 5521 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
80d2b43b 5522 .type = ARM_CP_IO,
a8d64e73
PM
5523 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5524 .resetvalue = 0,
80d2b43b
PM
5525 .access = PL3_RW,
5526 .writefn = mdcr_el3_write,
5527 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
7f4fbfb5 5528 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
a8d64e73
PM
5529 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5530 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5531 .writefn = sdcr_write,
5532 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5533};
5534
d1fb4da2 5535static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5536{
2fc0cc0e 5537 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5538
5539 if (arm_feature(env, ARM_FEATURE_V8)) {
5540 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5541 } else {
5542 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5543 }
f149e3e8
EI
5544
5545 if (arm_feature(env, ARM_FEATURE_EL3)) {
5546 valid_mask &= ~HCR_HCD;
77077a83 5547 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
9b37a28c
FR
5548 /*
5549 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
77077a83
JK
5550 * However, if we're using the SMC PSCI conduit then QEMU is
5551 * effectively acting like EL3 firmware and so the guest at
5552 * EL2 should retain the ability to prevent EL1 from being
5553 * able to make SMC calls into the ersatz firmware, so in
5554 * that case HCR.TSC should be read/write.
5555 */
f149e3e8
EI
5556 valid_mask &= ~HCR_TSC;
5557 }
d1fb4da2
RH
5558
5559 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5560 if (cpu_isar_feature(aa64_vh, cpu)) {
5561 valid_mask |= HCR_E2H;
5562 }
da3d8b13
RH
5563 if (cpu_isar_feature(aa64_ras, cpu)) {
5564 valid_mask |= HCR_TERR | HCR_TEA;
5565 }
d1fb4da2
RH
5566 if (cpu_isar_feature(aa64_lor, cpu)) {
5567 valid_mask |= HCR_TLOR;
5568 }
5569 if (cpu_isar_feature(aa64_pauth, cpu)) {
5570 valid_mask |= HCR_API | HCR_APK;
5571 }
8ddb300b
RH
5572 if (cpu_isar_feature(aa64_mte, cpu)) {
5573 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5574 }
7cb1e618
RH
5575 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5576 valid_mask |= HCR_ENSCXT;
5577 }
8c7e17ef
PM
5578 if (cpu_isar_feature(aa64_fwb, cpu)) {
5579 valid_mask |= HCR_FWB;
5580 }
ef682cdb 5581 }
f149e3e8 5582
d2fd9313
PM
5583 if (cpu_isar_feature(any_evt, cpu)) {
5584 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5585 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5586 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5587 }
5588
f149e3e8
EI
5589 /* Clear RES0 bits. */
5590 value &= valid_mask;
5591
8ddb300b
RH
5592 /*
5593 * These bits change the MMU setup:
f149e3e8
EI
5594 * HCR_VM enables stage 2 translation
5595 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5596 * HCR_DC disables stage1 and enables stage2 translation
5597 * HCR_DCT enables tagging on (disabled) stage1 translation
8c7e17ef 5598 * HCR_FWB changes the interpretation of stage2 descriptor bits
f149e3e8 5599 */
8c7e17ef
PM
5600 if ((env->cp15.hcr_el2 ^ value) &
5601 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
d10eb08f 5602 tlb_flush(CPU(cpu));
f149e3e8 5603 }
ce4afed8 5604 env->cp15.hcr_el2 = value;
89430fc6
PM
5605
5606 /*
5607 * Updates to VI and VF require us to update the status of
5608 * virtual interrupts, which are the logical OR of these bits
5609 * and the state of the input lines from the GIC. (This requires
5610 * that we have the iothread lock, which is done by marking the
5611 * reginfo structs as ARM_CP_IO.)
5612 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5613 * possible for it to be taken immediately, because VIRQ and
5614 * VFIQ are masked unless running at EL0 or EL1, and HCR
5615 * can only be written at EL2.
5616 */
5617 g_assert(qemu_mutex_iothread_locked());
5618 arm_cpu_update_virq(cpu);
5619 arm_cpu_update_vfiq(cpu);
3c29632f 5620 arm_cpu_update_vserr(cpu);
ce4afed8
PM
5621}
5622
d1fb4da2
RH
5623static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5624{
5625 do_hcr_write(env, value, 0);
5626}
5627
ce4afed8
PM
5628static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5629 uint64_t value)
5630{
5631 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5632 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5633 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5634}
5635
5636static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5637 uint64_t value)
5638{
5639 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5640 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5641 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5642}
5643
f7778444 5644/*
b74c0443 5645 * Return the effective value of HCR_EL2, at the given security state.
f7778444
RH
5646 * Bits that are not included here:
5647 * RW (read from SCR_EL3.RW as needed)
5648 */
b74c0443 5649uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
f7778444
RH
5650{
5651 uint64_t ret = env->cp15.hcr_el2;
5652
b74c0443 5653 if (!arm_is_el2_enabled_secstate(env, secure)) {
f7778444
RH
5654 /*
5655 * "This register has no effect if EL2 is not enabled in the
5656 * current Security state". This is ARMv8.4-SecEL2 speak for
5657 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5658 *
5659 * Prior to that, the language was "In an implementation that
5660 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5661 * as if this field is 0 for all purposes other than a direct
5662 * read or write access of HCR_EL2". With lots of enumeration
5663 * on a per-field basis. In current QEMU, this is condition
5664 * is arm_is_secure_below_el3.
5665 *
5666 * Since the v8.4 language applies to the entire register, and
5667 * appears to be backward compatible, use that.
5668 */
4990e1d3
RH
5669 return 0;
5670 }
5671
5672 /*
5673 * For a cpu that supports both aarch64 and aarch32, we can set bits
5674 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5675 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5676 */
5677 if (!arm_el_is_aa64(env, 2)) {
5678 uint64_t aa32_valid;
5679
5680 /*
5681 * These bits are up-to-date as of ARMv8.6.
5682 * For HCR, it's easiest to list just the 2 bits that are invalid.
5683 * For HCR2, list those that are valid.
5684 */
5685 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5686 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5687 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5688 ret &= aa32_valid;
5689 }
5690
5691 if (ret & HCR_TGE) {
5692 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5693 if (ret & HCR_E2H) {
5694 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5695 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5696 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5697 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5698 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5699 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5700 } else {
5701 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5702 }
5703 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5704 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5705 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5706 HCR_TLOR);
5707 }
5708
5709 return ret;
5710}
5711
b74c0443
RH
5712uint64_t arm_hcr_el2_eff(CPUARMState *env)
5713{
5714 return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5715}
5716
19668718
RH
5717/*
5718 * Corresponds to ARM pseudocode function ELIsInHost().
5719 */
5720bool el_is_in_host(CPUARMState *env, int el)
5721{
5722 uint64_t mask;
5723
5724 /*
5725 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5726 * Perform the simplest bit tests first, and validate EL2 afterward.
5727 */
5728 if (el & 1) {
5729 return false; /* EL1 or EL3 */
5730 }
5731
5732 /*
5733 * Note that hcr_write() checks isar_feature_aa64_vh(),
5734 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5735 */
5736 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5737 if ((env->cp15.hcr_el2 & mask) != mask) {
5738 return false;
5739 }
5740
5741 /* TGE and/or E2H set: double check those bits are currently legal. */
5742 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5743}
5744
5814d587
RH
5745static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5746 uint64_t value)
5747{
5748 uint64_t valid_mask = 0;
5749
5750 /* No features adding bits to HCRX are implemented. */
5751
5752 /* Clear RES0 bits. */
5753 env->cp15.hcrx_el2 = value & valid_mask;
5754}
5755
5756static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5757 bool isread)
5758{
5759 if (arm_current_el(env) < 3
5760 && arm_feature(env, ARM_FEATURE_EL3)
5761 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5762 return CP_ACCESS_TRAP_EL3;
5763 }
5764 return CP_ACCESS_OK;
5765}
5766
5767static const ARMCPRegInfo hcrx_el2_reginfo = {
5768 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5769 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5770 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5771 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5772};
5773
5774/* Return the effective value of HCRX_EL2. */
5775uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5776{
5777 /*
5778 * The bits in this register behave as 0 for all purposes other than
5779 * direct reads of the register if:
5780 * - EL2 is not enabled in the current security state,
5781 * - SCR_EL3.HXEn is 0.
5782 */
5783 if (!arm_is_el2_enabled(env)
5784 || (arm_feature(env, ARM_FEATURE_EL3)
5785 && !(env->cp15.scr_el3 & SCR_HXEN))) {
5786 return 0;
5787 }
5788 return env->cp15.hcrx_el2;
5789}
5790
fc1120a7
PM
5791static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5792 uint64_t value)
5793{
5794 /*
5795 * For A-profile AArch32 EL3, if NSACR.CP10
5796 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5797 */
5798 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5799 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
5800 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5801 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
fc1120a7
PM
5802 }
5803 env->cp15.cptr_el[2] = value;
5804}
5805
5806static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5807{
5808 /*
5809 * For A-profile AArch32 EL3, if NSACR.CP10
5810 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5811 */
5812 uint64_t value = env->cp15.cptr_el[2];
5813
5814 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5815 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 5816 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
fc1120a7
PM
5817 }
5818 return value;
5819}
5820
4771cd01 5821static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5822 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5823 .type = ARM_CP_IO,
f149e3e8
EI
5824 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5825 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5826 .writefn = hcr_write },
ce4afed8 5827 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5828 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5829 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5830 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5831 .writefn = hcr_writelow },
831a2fca
PM
5832 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5833 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5834 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5835 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5836 .type = ARM_CP_ALIAS,
3b685ba7
EI
5837 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5838 .access = PL2_RW,
5839 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5840 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5841 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5842 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5843 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5844 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5845 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5846 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5847 .type = ARM_CP_ALIAS,
5848 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5849 .access = PL2_RW,
5850 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5851 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5852 .type = ARM_CP_ALIAS,
3b685ba7 5853 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5854 .access = PL2_RW,
5855 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5856 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5857 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5858 .access = PL2_RW, .writefn = vbar_write,
5859 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5860 .resetvalue = 0 },
884b4dee
GB
5861 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5862 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5863 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5864 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5865 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5866 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5867 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5868 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5869 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5870 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5871 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5872 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5873 .resetvalue = 0 },
5874 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5875 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5876 .access = PL2_RW, .type = ARM_CP_ALIAS,
5877 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5878 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5879 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5880 .access = PL2_RW, .type = ARM_CP_CONST,
5881 .resetvalue = 0 },
5882 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5883 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5884 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5885 .access = PL2_RW, .type = ARM_CP_CONST,
5886 .resetvalue = 0 },
37cd6c24
PM
5887 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5888 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5889 .access = PL2_RW, .type = ARM_CP_CONST,
5890 .resetvalue = 0 },
5891 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5892 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5893 .access = PL2_RW, .type = ARM_CP_CONST,
5894 .resetvalue = 0 },
06ec4c8c
EI
5895 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5896 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933 5897 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
06ec4c8c 5898 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5899 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5900 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5901 .type = ARM_CP_ALIAS,
68e9c2fe 5902 .access = PL2_RW, .accessfn = access_el3_aa32ns,
afbb181c 5903 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
68e9c2fe
EI
5904 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5905 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5906 .access = PL2_RW,
988cc190 5907 /* no .writefn needed as this can't cause an ASID change */
68e9c2fe 5908 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5909 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5910 .cp = 15, .opc1 = 6, .crm = 2,
5911 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5912 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5913 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5914 .writefn = vttbr_write },
5915 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5916 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5917 .access = PL2_RW, .writefn = vttbr_write,
5918 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5919 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5920 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5921 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5922 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5923 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5924 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5925 .access = PL2_RW, .resetvalue = 0,
5926 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5927 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5928 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5929 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5930 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5931 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5932 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5933 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5934 { .name = "TLBIALLNSNH",
5935 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5936 .type = ARM_CP_NO_RAW, .access = PL2_W,
5937 .writefn = tlbiall_nsnh_write },
5938 { .name = "TLBIALLNSNHIS",
5939 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5940 .type = ARM_CP_NO_RAW, .access = PL2_W,
5941 .writefn = tlbiall_nsnh_is_write },
5942 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5943 .type = ARM_CP_NO_RAW, .access = PL2_W,
5944 .writefn = tlbiall_hyp_write },
5945 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5946 .type = ARM_CP_NO_RAW, .access = PL2_W,
5947 .writefn = tlbiall_hyp_is_write },
5948 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5949 .type = ARM_CP_NO_RAW, .access = PL2_W,
5950 .writefn = tlbimva_hyp_write },
5951 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5952 .type = ARM_CP_NO_RAW, .access = PL2_W,
5953 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5954 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5955 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 5956 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5957 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5958 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5959 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 5960 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5961 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5962 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5963 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 5964 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
5965 .writefn = tlbi_aa64_vae2_write },
5966 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5967 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 5968 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5969 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5970 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5971 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 5972 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5973 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5974 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5975 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 5976 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5977 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5978#ifndef CONFIG_USER_ONLY
9b37a28c
FR
5979 /*
5980 * Unlike the other EL2-related AT operations, these must
2a47df95
PM
5981 * UNDEF from EL3 if EL2 is not implemented, which is why we
5982 * define them here rather than with the rest of the AT ops.
5983 */
5984 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5985 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5986 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5987 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5988 .writefn = ats_write64 },
2a47df95
PM
5989 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5990 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5991 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5992 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5993 .writefn = ats_write64 },
9b37a28c
FR
5994 /*
5995 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
14db7fe0
PM
5996 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5997 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5998 * to behave as if SCR.NS was 1.
5999 */
6000 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6001 .access = PL2_W,
0710b2fa 6002 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
6003 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6004 .access = PL2_W,
0710b2fa 6005 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
6006 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6007 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
9b37a28c
FR
6008 /*
6009 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
0b6440af
EI
6010 * reset values as IMPDEF. We choose to reset to 3 to comply with
6011 * both ARMv7 and ARMv8.
6012 */
6013 .access = PL2_RW, .resetvalue = 3,
6014 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
6015 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6016 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6017 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6018 .writefn = gt_cntvoff_write,
6019 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6020 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6021 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6022 .writefn = gt_cntvoff_write,
6023 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
6024 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6025 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6026 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6027 .type = ARM_CP_IO, .access = PL2_RW,
6028 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6029 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6030 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6031 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6032 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6033 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6034 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 6035 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
6036 .resetfn = gt_hyp_timer_reset,
6037 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6038 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6039 .type = ARM_CP_IO,
6040 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6041 .access = PL2_RW,
6042 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6043 .resetvalue = 0,
6044 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 6045#endif
59e05530
EI
6046 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6047 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6048 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6049 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6050 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6051 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6052 .access = PL2_RW,
6053 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
6054 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6055 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6056 .access = PL2_RW,
6057 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
6058};
6059
ce4afed8
PM
6060static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6061 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 6062 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
6063 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6064 .access = PL2_RW,
6065 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6066 .writefn = hcr_writehigh },
ce4afed8
PM
6067};
6068
e9152ee9
RDC
6069static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6070 bool isread)
6071{
6072 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6073 return CP_ACCESS_OK;
6074 }
6075 return CP_ACCESS_TRAP_UNCATEGORIZED;
6076}
6077
6078static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6079 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6080 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6081 .access = PL2_RW, .accessfn = sel2_access,
6082 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6083 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6084 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6085 .access = PL2_RW, .accessfn = sel2_access,
6086 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
6087};
6088
2f027fc5
PM
6089static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6090 bool isread)
6091{
9b37a28c
FR
6092 /*
6093 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 6094 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
6095 */
6096 if (arm_current_el(env) == 3) {
6097 return CP_ACCESS_OK;
6098 }
6099 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
6100 if (env->cp15.scr_el3 & SCR_EEL2) {
6101 return CP_ACCESS_TRAP_EL2;
6102 }
2f027fc5
PM
6103 return CP_ACCESS_TRAP_EL3;
6104 }
6105 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6106 if (isread) {
6107 return CP_ACCESS_OK;
6108 }
6109 return CP_ACCESS_TRAP_UNCATEGORIZED;
6110}
6111
60fb1a87
GB
6112static const ARMCPRegInfo el3_cp_reginfo[] = {
6113 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6114 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6115 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 6116 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 6117 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 6118 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
6119 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6120 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 6121 .writefn = scr_write },
60fb1a87
GB
6122 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6123 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6124 .access = PL3_RW, .resetvalue = 0,
6125 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6126 { .name = "SDER",
6127 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6128 .access = PL3_RW, .resetvalue = 0,
6129 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 6130 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
6131 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6132 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 6133 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
6134 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6135 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 6136 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 6137 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
6138 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6139 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c 6140 .access = PL3_RW,
cb4a0a34
PM
6141 /* no .writefn needed as this can't cause an ASID change */
6142 .resetvalue = 0,
11f136ee 6143 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 6144 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6145 .type = ARM_CP_ALIAS,
81547d66
EI
6146 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6147 .access = PL3_RW,
6148 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 6149 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
6150 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6151 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
6152 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6153 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6154 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 6155 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6156 .type = ARM_CP_ALIAS,
81547d66 6157 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
6158 .access = PL3_RW,
6159 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
6160 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6161 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6162 .access = PL3_RW, .writefn = vbar_write,
6163 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6164 .resetvalue = 0 },
c6f19164
GB
6165 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6166 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6167 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6168 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
6169 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6170 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6171 .access = PL3_RW, .resetvalue = 0,
6172 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
6173 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6174 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6175 .access = PL3_RW, .type = ARM_CP_CONST,
6176 .resetvalue = 0 },
37cd6c24
PM
6177 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6178 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6179 .access = PL3_RW, .type = ARM_CP_CONST,
6180 .resetvalue = 0 },
6181 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6182 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6183 .access = PL3_RW, .type = ARM_CP_CONST,
6184 .resetvalue = 0 },
43efaa33
PM
6185 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6186 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6187 .access = PL3_W, .type = ARM_CP_NO_RAW,
6188 .writefn = tlbi_aa64_alle3is_write },
6189 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6190 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6191 .access = PL3_W, .type = ARM_CP_NO_RAW,
6192 .writefn = tlbi_aa64_vae3is_write },
6193 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6194 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6195 .access = PL3_W, .type = ARM_CP_NO_RAW,
6196 .writefn = tlbi_aa64_vae3is_write },
6197 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6198 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6199 .access = PL3_W, .type = ARM_CP_NO_RAW,
6200 .writefn = tlbi_aa64_alle3_write },
6201 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6202 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6203 .access = PL3_W, .type = ARM_CP_NO_RAW,
6204 .writefn = tlbi_aa64_vae3_write },
6205 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6206 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6207 .access = PL3_W, .type = ARM_CP_NO_RAW,
6208 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
6209};
6210
e2cce18f
RH
6211#ifndef CONFIG_USER_ONLY
6212/* Test if system register redirection is to occur in the current state. */
6213static bool redirect_for_e2h(CPUARMState *env)
6214{
6215 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6216}
6217
6218static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6219{
6220 CPReadFn *readfn;
6221
6222 if (redirect_for_e2h(env)) {
6223 /* Switch to the saved EL2 version of the register. */
6224 ri = ri->opaque;
6225 readfn = ri->readfn;
6226 } else {
6227 readfn = ri->orig_readfn;
6228 }
6229 if (readfn == NULL) {
6230 readfn = raw_read;
6231 }
6232 return readfn(env, ri);
6233}
6234
6235static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6236 uint64_t value)
6237{
6238 CPWriteFn *writefn;
6239
6240 if (redirect_for_e2h(env)) {
6241 /* Switch to the saved EL2 version of the register. */
6242 ri = ri->opaque;
6243 writefn = ri->writefn;
6244 } else {
6245 writefn = ri->orig_writefn;
6246 }
6247 if (writefn == NULL) {
6248 writefn = raw_write;
6249 }
6250 writefn(env, ri, value);
6251}
6252
6253static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6254{
6255 struct E2HAlias {
6256 uint32_t src_key, dst_key, new_key;
6257 const char *src_name, *dst_name, *new_name;
6258 bool (*feature)(const ARMISARegisters *id);
6259 };
6260
6261#define K(op0, op1, crn, crm, op2) \
6262 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6263
6264 static const struct E2HAlias aliases[] = {
6265 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6266 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6267 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6268 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6269 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6270 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6271 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6272 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6273 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6274 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6275 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6276 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6277 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6278 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6279 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6280 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6281 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6282 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6283 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6284 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6285 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6286 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6287 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6288 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6289 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6290 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6291 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6292 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6293 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6294 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6295 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6296 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6297
6298 /*
6299 * Note that redirection of ZCR is mentioned in the description
6300 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6301 * not in the summary table.
6302 */
6303 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6304 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
de561988
RH
6305 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6306 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
e2cce18f 6307
4b779ceb
RH
6308 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6309 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6310
7cb1e618
RH
6311 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6312 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6313 isar_feature_aa64_scxtnum },
6314
e2cce18f
RH
6315 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6316 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6317 };
6318#undef K
6319
6320 size_t i;
6321
6322 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6323 const struct E2HAlias *a = &aliases[i];
9da35a40 6324 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 6325 bool ok;
e2cce18f
RH
6326
6327 if (a->feature && !a->feature(&cpu->isar)) {
6328 continue;
6329 }
6330
5860362d
RH
6331 src_reg = g_hash_table_lookup(cpu->cp_regs,
6332 (gpointer)(uintptr_t)a->src_key);
6333 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6334 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
6335 g_assert(src_reg != NULL);
6336 g_assert(dst_reg != NULL);
6337
6338 /* Cross-compare names to detect typos in the keys. */
6339 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6340 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6341
6342 /* None of the core system registers use opaque; we will. */
6343 g_assert(src_reg->opaque == NULL);
6344
6345 /* Create alias before redirection so we dup the right data. */
9da35a40 6346 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
6347
6348 new_reg->name = a->new_name;
6349 new_reg->type |= ARM_CP_ALIAS;
6350 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6351 new_reg->access &= PL2_RW | PL3_RW;
6352
5860362d
RH
6353 ok = g_hash_table_insert(cpu->cp_regs,
6354 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 6355 g_assert(ok);
e2cce18f
RH
6356
6357 src_reg->opaque = dst_reg;
6358 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6359 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6360 if (!src_reg->raw_readfn) {
6361 src_reg->raw_readfn = raw_read;
6362 }
6363 if (!src_reg->raw_writefn) {
6364 src_reg->raw_writefn = raw_write;
6365 }
6366 src_reg->readfn = el2_e2h_read;
6367 src_reg->writefn = el2_e2h_write;
6368 }
6369}
6370#endif
6371
3f208fd7
PM
6372static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6373 bool isread)
7da845b0 6374{
97475a89
RH
6375 int cur_el = arm_current_el(env);
6376
6377 if (cur_el < 2) {
6378 uint64_t hcr = arm_hcr_el2_eff(env);
6379
6380 if (cur_el == 0) {
6381 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6382 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6383 return CP_ACCESS_TRAP_EL2;
6384 }
6385 } else {
6386 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6387 return CP_ACCESS_TRAP;
6388 }
6389 if (hcr & HCR_TID2) {
6390 return CP_ACCESS_TRAP_EL2;
6391 }
6392 }
6393 } else if (hcr & HCR_TID2) {
6394 return CP_ACCESS_TRAP_EL2;
6395 }
7da845b0 6396 }
630fcd4d
MZ
6397
6398 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6399 return CP_ACCESS_TRAP_EL2;
6400 }
6401
7da845b0
PM
6402 return CP_ACCESS_OK;
6403}
6404
58e93b48
RH
6405/*
6406 * Check for traps to RAS registers, which are controlled
6407 * by HCR_EL2.TERR and SCR_EL3.TERR.
6408 */
6409static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6410 bool isread)
6411{
6412 int el = arm_current_el(env);
6413
6414 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6415 return CP_ACCESS_TRAP_EL2;
6416 }
6417 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6418 return CP_ACCESS_TRAP_EL3;
6419 }
6420 return CP_ACCESS_OK;
6421}
6422
6423static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6424{
6425 int el = arm_current_el(env);
6426
6427 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6428 return env->cp15.vdisr_el2;
6429 }
6430 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6431 return 0; /* RAZ/WI */
6432 }
6433 return env->cp15.disr_el1;
6434}
6435
6436static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6437{
6438 int el = arm_current_el(env);
6439
6440 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6441 env->cp15.vdisr_el2 = val;
6442 return;
6443 }
6444 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6445 return; /* RAZ/WI */
6446 }
6447 env->cp15.disr_el1 = val;
6448}
6449
6450/*
6451 * Minimal RAS implementation with no Error Records.
6452 * Which means that all of the Error Record registers:
6453 * ERXADDR_EL1
6454 * ERXCTLR_EL1
6455 * ERXFR_EL1
6456 * ERXMISC0_EL1
6457 * ERXMISC1_EL1
6458 * ERXMISC2_EL1
6459 * ERXMISC3_EL1
6460 * ERXPFGCDN_EL1 (RASv1p1)
6461 * ERXPFGCTL_EL1 (RASv1p1)
6462 * ERXPFGF_EL1 (RASv1p1)
6463 * ERXSTATUS_EL1
6464 * and
6465 * ERRSELR_EL1
6466 * may generate UNDEFINED, which is the effect we get by not
6467 * listing them at all.
6468 */
6469static const ARMCPRegInfo minimal_ras_reginfo[] = {
6470 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6471 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6472 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6473 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6474 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6475 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6476 .access = PL1_R, .accessfn = access_terr,
6477 .type = ARM_CP_CONST, .resetvalue = 0 },
6478 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6479 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6480 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6481 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6482 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6483 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6484};
6485
397d922c
RH
6486/*
6487 * Return the exception level to which exceptions should be taken
6488 * via SVEAccessTrap. This excludes the check for whether the exception
6489 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6490 * be found by testing 0 < fp_exception_el < sve_exception_el.
6491 *
6492 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6493 * pseudocode does *not* separate out the FP trap checks, but has them
6494 * all in one function.
5be5e8ed 6495 */
ced31551 6496int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6497{
6498#ifndef CONFIG_USER_ONLY
aa4451b6 6499 if (el <= 1 && !el_is_in_host(env, el)) {
fab8ad39 6500 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7701cee5
RH
6501 case 1:
6502 if (el != 0) {
6503 break;
6504 }
6505 /* fall through */
6506 case 0:
6507 case 2:
61a8c23a 6508 return 1;
5be5e8ed 6509 }
5be5e8ed
RH
6510 }
6511
7d38cb92
RH
6512 if (el <= 2 && arm_is_el2_enabled(env)) {
6513 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6514 if (env->cp15.hcr_el2 & HCR_E2H) {
fab8ad39 6515 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
d5a6fa2d 6516 case 1:
7d38cb92 6517 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
d5a6fa2d
RH
6518 break;
6519 }
6520 /* fall through */
6521 case 0:
6522 case 2:
6523 return 2;
6524 }
7d38cb92 6525 } else {
fab8ad39 6526 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
d5a6fa2d
RH
6527 return 2;
6528 }
60eed086 6529 }
5be5e8ed
RH
6530 }
6531
60eed086
RH
6532 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6533 if (arm_feature(env, ARM_FEATURE_EL3)
fab8ad39 6534 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
5be5e8ed
RH
6535 return 3;
6536 }
6537#endif
6538 return 0;
6539}
6540
6b2ca83e
RH
6541/*
6542 * Return the exception level to which exceptions should be taken for SME.
6543 * C.f. the ARM pseudocode function CheckSMEAccess.
6544 */
6545int sme_exception_el(CPUARMState *env, int el)
6546{
6547#ifndef CONFIG_USER_ONLY
6548 if (el <= 1 && !el_is_in_host(env, el)) {
6549 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6550 case 1:
6551 if (el != 0) {
6552 break;
6553 }
6554 /* fall through */
6555 case 0:
6556 case 2:
6557 return 1;
6558 }
6559 }
6560
6561 if (el <= 2 && arm_is_el2_enabled(env)) {
6562 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6563 if (env->cp15.hcr_el2 & HCR_E2H) {
6564 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6565 case 1:
6566 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6567 break;
6568 }
6569 /* fall through */
6570 case 0:
6571 case 2:
6572 return 2;
6573 }
6574 } else {
6575 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6576 return 2;
6577 }
6578 }
6579 }
6580
6581 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6582 if (arm_feature(env, ARM_FEATURE_EL3)
6583 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6584 return 3;
6585 }
6586#endif
6587 return 0;
6588}
6589
75fe8356
RH
6590/* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6591static bool sme_fa64(CPUARMState *env, int el)
6592{
6593 if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6594 return false;
6595 }
6596
6597 if (el <= 1 && !el_is_in_host(env, el)) {
6598 if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6599 return false;
6600 }
6601 }
6602 if (el <= 2 && arm_is_el2_enabled(env)) {
6603 if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6604 return false;
6605 }
6606 }
6607 if (arm_feature(env, ARM_FEATURE_EL3)) {
6608 if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6609 return false;
6610 }
6611 }
6612
6613 return true;
6614}
6615
0ab5953b
RH
6616/*
6617 * Given that SVE is enabled, return the vector length for EL.
6618 */
6ca54aa9 6619uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
0ab5953b 6620{
2fc0cc0e 6621 ARMCPU *cpu = env_archcpu(env);
6ca54aa9
RH
6622 uint64_t *cr = env->vfp.zcr_el;
6623 uint32_t map = cpu->sve_vq.map;
6624 uint32_t len = ARM_MAX_VQ - 1;
6625
6626 if (sm) {
6627 cr = env->vfp.smcr_el;
6628 map = cpu->sme_vq.map;
6629 }
0ab5953b 6630
c6225beb 6631 if (el <= 1 && !el_is_in_host(env, el)) {
6ca54aa9 6632 len = MIN(len, 0xf & (uint32_t)cr[1]);
0ab5953b 6633 }
6a02a732 6634 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6ca54aa9 6635 len = MIN(len, 0xf & (uint32_t)cr[2]);
0ab5953b 6636 }
6a02a732 6637 if (arm_feature(env, ARM_FEATURE_EL3)) {
6ca54aa9
RH
6638 len = MIN(len, 0xf & (uint32_t)cr[3]);
6639 }
6640
6641 map &= MAKE_64BIT_MASK(0, len + 1);
6642 if (map != 0) {
6643 return 31 - clz32(map);
0ab5953b 6644 }
0df9142d 6645
6ca54aa9
RH
6646 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6647 assert(sm);
6648 return ctz32(cpu->sme_vq.map);
6649}
6650
6651uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6652{
6653 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
0ab5953b
RH
6654}
6655
5be5e8ed
RH
6656static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6657 uint64_t value)
6658{
0ab5953b 6659 int cur_el = arm_current_el(env);
5ef3cc56 6660 int old_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6661 int new_len;
6662
5be5e8ed 6663 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6664 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6665 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6666
6667 /*
6668 * Because we arrived here, we know both FP and SVE are enabled;
6669 * otherwise we would have trapped access to the ZCR_ELn register.
6670 */
5ef3cc56 6671 new_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6672 if (new_len < old_len) {
6673 aarch64_sve_narrow_vq(env, new_len + 1);
6674 }
5be5e8ed
RH
6675}
6676
60360d82
RH
6677static const ARMCPRegInfo zcr_reginfo[] = {
6678 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6679 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6680 .access = PL1_RW, .type = ARM_CP_SVE,
6681 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6682 .writefn = zcr_write, .raw_writefn = raw_write },
6683 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6684 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6685 .access = PL2_RW, .type = ARM_CP_SVE,
6686 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6687 .writefn = zcr_write, .raw_writefn = raw_write },
6688 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6689 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6690 .access = PL3_RW, .type = ARM_CP_SVE,
6691 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6692 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6693};
6694
9e5ec745
RH
6695#ifdef TARGET_AARCH64
6696static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6697 bool isread)
6698{
6699 int el = arm_current_el(env);
6700
6701 if (el == 0) {
6702 uint64_t sctlr = arm_sctlr(env, el);
6703 if (!(sctlr & SCTLR_EnTP2)) {
6704 return CP_ACCESS_TRAP;
6705 }
6706 }
6707 /* TODO: FEAT_FGT */
6708 if (el < 3
6709 && arm_feature(env, ARM_FEATURE_EL3)
6710 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6711 return CP_ACCESS_TRAP_EL3;
6712 }
6713 return CP_ACCESS_OK;
6714}
6715
d5b1223a
RH
6716static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6717 bool isread)
6718{
6719 /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6720 if (arm_current_el(env) < 3
6721 && arm_feature(env, ARM_FEATURE_EL3)
6722 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6723 return CP_ACCESS_TRAP_EL3;
6724 }
6725 return CP_ACCESS_OK;
6726}
6727
7f2a01e7
RH
6728/* ResetSVEState */
6729static void arm_reset_sve_state(CPUARMState *env)
6730{
6731 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6732 /* Recall that FFR is stored as pregs[16]. */
6733 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6734 vfp_set_fpcr(env, 0x0800009f);
6735}
6736
2a8af382
RH
6737void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6738{
6739 uint64_t change = (env->svcr ^ new) & mask;
6740
f4318557
RH
6741 if (change == 0) {
6742 return;
6743 }
2a8af382 6744 env->svcr ^= change;
7f2a01e7
RH
6745
6746 if (change & R_SVCR_SM_MASK) {
6747 arm_reset_sve_state(env);
6748 }
fccb4918
RH
6749
6750 /*
6751 * ResetSMEState.
6752 *
6753 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
6754 * on enable: while disabled, the storage is inaccessible and the
6755 * value does not matter. We're not saving the storage in vmstate
6756 * when disabled either.
6757 */
6758 if (change & new & R_SVCR_ZA_MASK) {
6759 memset(env->zarray, 0, sizeof(env->zarray));
6760 }
f4318557
RH
6761
6762 arm_rebuild_hflags(env);
2a8af382
RH
6763}
6764
c37e6ac9
RH
6765static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6766 uint64_t value)
6767{
2a8af382 6768 aarch64_set_svcr(env, value, -1);
c37e6ac9
RH
6769}
6770
de561988
RH
6771static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6772 uint64_t value)
6773{
6774 int cur_el = arm_current_el(env);
6775 int old_len = sve_vqm1_for_el(env, cur_el);
6776 int new_len;
6777
6778 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6779 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6780 raw_write(env, ri, value);
6781
6782 /*
6783 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6784 * when SVL is widened (old values kept, or zeros). Choose to keep the
6785 * current values for simplicity. But for QEMU internals, we must still
6786 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6787 * above aarch64_sve_narrow_vq.
6788 */
6789 new_len = sve_vqm1_for_el(env, cur_el);
6790 if (new_len < old_len) {
6791 aarch64_sve_narrow_vq(env, new_len + 1);
6792 }
6793}
6794
9e5ec745
RH
6795static const ARMCPRegInfo sme_reginfo[] = {
6796 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6797 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6798 .access = PL0_RW, .accessfn = access_tpidr2,
6799 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
c37e6ac9
RH
6800 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6801 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6802 .access = PL0_RW, .type = ARM_CP_SME,
6803 .fieldoffset = offsetof(CPUARMState, svcr),
6804 .writefn = svcr_write, .raw_writefn = raw_write },
de561988
RH
6805 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6806 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6807 .access = PL1_RW, .type = ARM_CP_SME,
6808 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6809 .writefn = smcr_write, .raw_writefn = raw_write },
6810 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6811 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6812 .access = PL2_RW, .type = ARM_CP_SME,
6813 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6814 .writefn = smcr_write, .raw_writefn = raw_write },
6815 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6816 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6817 .access = PL3_RW, .type = ARM_CP_SME,
6818 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6819 .writefn = smcr_write, .raw_writefn = raw_write },
d5b1223a
RH
6820 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6821 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6822 .access = PL1_R, .accessfn = access_aa64_tid1,
6823 /*
6824 * IMPLEMENTOR = 0 (software)
6825 * REVISION = 0 (implementation defined)
6826 * SMPS = 0 (no streaming execution priority in QEMU)
6827 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
6828 */
6829 .type = ARM_CP_CONST, .resetvalue = 0, },
6830 /*
6831 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6832 */
6833 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6834 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6835 .access = PL1_RW, .accessfn = access_esm,
6836 .type = ARM_CP_CONST, .resetvalue = 0 },
6837 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6838 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6839 .access = PL2_RW, .accessfn = access_esm,
6840 .type = ARM_CP_CONST, .resetvalue = 0 },
9e5ec745
RH
6841};
6842#endif /* TARGET_AARCH64 */
6843
24183fb6
PM
6844static void define_pmu_regs(ARMCPU *cpu)
6845{
6846 /*
6847 * v7 performance monitor control register: same implementor
6848 * field as main ID register, and we implement four counters in
6849 * addition to the cycle count register.
6850 */
24526bb9 6851 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
24183fb6
PM
6852 ARMCPRegInfo pmcr = {
6853 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6854 .access = PL0_RW,
6855 .type = ARM_CP_IO | ARM_CP_ALIAS,
6856 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6857 .accessfn = pmreg_access, .writefn = pmcr_write,
6858 .raw_writefn = raw_write,
6859 };
6860 ARMCPRegInfo pmcr64 = {
6861 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6862 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6863 .access = PL0_RW, .accessfn = pmreg_access,
6864 .type = ARM_CP_IO,
6865 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
24526bb9 6866 .resetvalue = cpu->isar.reset_pmcr_el0,
24183fb6
PM
6867 .writefn = pmcr_write, .raw_writefn = raw_write,
6868 };
24526bb9 6869
24183fb6
PM
6870 define_one_arm_cp_reg(cpu, &pmcr);
6871 define_one_arm_cp_reg(cpu, &pmcr64);
6872 for (i = 0; i < pmcrn; i++) {
6873 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6874 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6875 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6876 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6877 ARMCPRegInfo pmev_regs[] = {
6878 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6879 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6880 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6881 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 6882 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
6883 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6884 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 6885 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6
PM
6886 .type = ARM_CP_IO,
6887 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6888 .raw_readfn = pmevcntr_rawread,
6889 .raw_writefn = pmevcntr_rawwrite },
6890 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6891 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6892 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6893 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6894 .accessfn = pmreg_access },
6895 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6896 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6897 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6898 .type = ARM_CP_IO,
6899 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6900 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6901 };
6902 define_arm_cp_regs(cpu, pmev_regs);
6903 g_free(pmevcntr_name);
6904 g_free(pmevcntr_el0_name);
6905 g_free(pmevtyper_name);
6906 g_free(pmevtyper_el0_name);
6907 }
a793bcd0 6908 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
24183fb6
PM
6909 ARMCPRegInfo v81_pmu_regs[] = {
6910 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6911 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6912 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6913 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6914 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6915 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6916 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6917 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6918 };
6919 define_arm_cp_regs(cpu, v81_pmu_regs);
6920 }
a793bcd0 6921 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
15dd1ebd
PM
6922 static const ARMCPRegInfo v84_pmmir = {
6923 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6924 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6925 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6926 .resetvalue = 0
6927 };
6928 define_one_arm_cp_reg(cpu, &v84_pmmir);
6929 }
24183fb6
PM
6930}
6931
9b37a28c
FR
6932/*
6933 * We don't know until after realize whether there's a GICv3
96a8b92e
PM
6934 * attached, and that is what registers the gicv3 sysregs.
6935 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6936 * at runtime.
6937 */
6938static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6939{
2fc0cc0e 6940 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6941 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6942
6943 if (env->gicv3state) {
6944 pfr1 |= 1 << 28;
6945 }
6946 return pfr1;
6947}
6948
976b99b6 6949#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6950static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6951{
2fc0cc0e 6952 ARMCPU *cpu = env_archcpu(env);
47576b94 6953 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6954
6955 if (env->gicv3state) {
6956 pfr0 |= 1 << 24;
6957 }
6958 return pfr0;
6959}
976b99b6 6960#endif
96a8b92e 6961
9b37a28c
FR
6962/*
6963 * Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6964 * Secure state exclusion has already been dealt with.
2d7137c1 6965 */
9bd268ba
RDC
6966static CPAccessResult access_lor_ns(CPUARMState *env,
6967 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6968{
6969 int el = arm_current_el(env);
6970
6971 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6972 return CP_ACCESS_TRAP_EL2;
6973 }
6974 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6975 return CP_ACCESS_TRAP_EL3;
6976 }
6977 return CP_ACCESS_OK;
6978}
6979
2d7137c1
RH
6980static CPAccessResult access_lor_other(CPUARMState *env,
6981 const ARMCPRegInfo *ri, bool isread)
6982{
6983 if (arm_is_secure_below_el3(env)) {
6984 /* Access denied in secure mode. */
6985 return CP_ACCESS_TRAP;
6986 }
9bd268ba 6987 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6988}
6989
d8564ee4
RH
6990/*
6991 * A trivial implementation of ARMv8.1-LOR leaves all of these
6992 * registers fixed at 0, which indicates that there are zero
6993 * supported Limited Ordering regions.
6994 */
6995static const ARMCPRegInfo lor_reginfo[] = {
6996 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6997 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6998 .access = PL1_RW, .accessfn = access_lor_other,
6999 .type = ARM_CP_CONST, .resetvalue = 0 },
7000 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7001 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7002 .access = PL1_RW, .accessfn = access_lor_other,
7003 .type = ARM_CP_CONST, .resetvalue = 0 },
7004 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7005 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7006 .access = PL1_RW, .accessfn = access_lor_other,
7007 .type = ARM_CP_CONST, .resetvalue = 0 },
7008 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7009 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7010 .access = PL1_RW, .accessfn = access_lor_other,
7011 .type = ARM_CP_CONST, .resetvalue = 0 },
7012 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7013 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 7014 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4 7015 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
7016};
7017
967aa94f
RH
7018#ifdef TARGET_AARCH64
7019static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7020 bool isread)
7021{
7022 int el = arm_current_el(env);
7023
7024 if (el < 2 &&
07b034ea 7025 arm_is_el2_enabled(env) &&
967aa94f
RH
7026 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7027 return CP_ACCESS_TRAP_EL2;
7028 }
7029 if (el < 3 &&
7030 arm_feature(env, ARM_FEATURE_EL3) &&
7031 !(env->cp15.scr_el3 & SCR_APK)) {
7032 return CP_ACCESS_TRAP_EL3;
7033 }
7034 return CP_ACCESS_OK;
7035}
7036
7037static const ARMCPRegInfo pauth_reginfo[] = {
7038 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7039 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7040 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7041 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
7042 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7043 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7044 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7045 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
7046 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7047 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7048 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7049 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
7050 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7051 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7052 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7053 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
7054 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7055 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7056 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7057 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
7058 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7059 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7060 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7061 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
7062 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7063 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7064 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7065 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
7066 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7067 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7068 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7069 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
7070 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7071 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7072 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7073 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
7074 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7075 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7076 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7077 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 7078};
de390645 7079
84940ed8
RC
7080static const ARMCPRegInfo tlbirange_reginfo[] = {
7081 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7082 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
0f66d223 7083 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7084 .writefn = tlbi_aa64_rvae1is_write },
7085 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7086 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
0f66d223 7087 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7088 .writefn = tlbi_aa64_rvae1is_write },
7089 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7090 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
0f66d223 7091 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7092 .writefn = tlbi_aa64_rvae1is_write },
7093 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7094 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
0f66d223 7095 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7096 .writefn = tlbi_aa64_rvae1is_write },
7097 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7098 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
fe3ca86c 7099 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7100 .writefn = tlbi_aa64_rvae1is_write },
7101 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7102 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
fe3ca86c 7103 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7104 .writefn = tlbi_aa64_rvae1is_write },
7105 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7106 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
fe3ca86c 7107 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7108 .writefn = tlbi_aa64_rvae1is_write },
7109 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7110 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
fe3ca86c 7111 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7112 .writefn = tlbi_aa64_rvae1is_write },
7113 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7114 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
4870f38b 7115 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7116 .writefn = tlbi_aa64_rvae1_write },
7117 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7118 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
4870f38b 7119 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7120 .writefn = tlbi_aa64_rvae1_write },
7121 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7122 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
4870f38b 7123 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7124 .writefn = tlbi_aa64_rvae1_write },
7125 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7126 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
4870f38b 7127 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7128 .writefn = tlbi_aa64_rvae1_write },
7129 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7130 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
575a94af
RH
7131 .access = PL2_W, .type = ARM_CP_NO_RAW,
7132 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7133 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7134 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
575a94af
RH
7135 .access = PL2_W, .type = ARM_CP_NO_RAW,
7136 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7137 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7138 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 7139 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7140 .writefn = tlbi_aa64_rvae2is_write },
7141 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7142 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 7143 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7144 .writefn = tlbi_aa64_rvae2is_write },
7145 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7146 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
575a94af
RH
7147 .access = PL2_W, .type = ARM_CP_NO_RAW,
7148 .writefn = tlbi_aa64_ripas2e1_write },
7149 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
84940ed8 7150 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
575a94af
RH
7151 .access = PL2_W, .type = ARM_CP_NO_RAW,
7152 .writefn = tlbi_aa64_ripas2e1_write },
84940ed8
RC
7153 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 7155 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7156 .writefn = tlbi_aa64_rvae2is_write },
7157 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7158 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 7159 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7160 .writefn = tlbi_aa64_rvae2is_write },
7161 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7162 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 7163 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7164 .writefn = tlbi_aa64_rvae2_write },
7165 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7166 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 7167 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7168 .writefn = tlbi_aa64_rvae2_write },
7169 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7170 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7171 .access = PL3_W, .type = ARM_CP_NO_RAW,
7172 .writefn = tlbi_aa64_rvae3is_write },
7173 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7174 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7175 .access = PL3_W, .type = ARM_CP_NO_RAW,
7176 .writefn = tlbi_aa64_rvae3is_write },
7177 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7178 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7179 .access = PL3_W, .type = ARM_CP_NO_RAW,
7180 .writefn = tlbi_aa64_rvae3is_write },
7181 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7182 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7183 .access = PL3_W, .type = ARM_CP_NO_RAW,
7184 .writefn = tlbi_aa64_rvae3is_write },
7185 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7186 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7187 .access = PL3_W, .type = ARM_CP_NO_RAW,
7188 .writefn = tlbi_aa64_rvae3_write },
7189 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7190 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7191 .access = PL3_W, .type = ARM_CP_NO_RAW,
7192 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
7193};
7194
7113d618
RC
7195static const ARMCPRegInfo tlbios_reginfo[] = {
7196 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7197 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
fe3ca86c 7198 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7113d618 7199 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7200 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7201 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
fe3ca86c 7202 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7203 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7204 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7205 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
fe3ca86c 7206 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7113d618 7207 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7208 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7209 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
fe3ca86c 7210 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9
IH
7211 .writefn = tlbi_aa64_vae1is_write },
7212 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7213 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
fe3ca86c 7214 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9
IH
7215 .writefn = tlbi_aa64_vae1is_write },
7216 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7217 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
fe3ca86c 7218 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7219 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7220 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7221 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 7222 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 7223 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
7224 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7225 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 7226 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7227 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7228 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7229 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7230 .access = PL2_W, .type = ARM_CP_NO_RAW,
7231 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
7232 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7233 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 7234 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7235 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7236 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7237 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7238 .access = PL2_W, .type = ARM_CP_NO_RAW,
7239 .writefn = tlbi_aa64_alle1is_write },
7240 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7241 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7242 .access = PL2_W, .type = ARM_CP_NOP },
7243 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7244 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7245 .access = PL2_W, .type = ARM_CP_NOP },
7246 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7247 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7248 .access = PL2_W, .type = ARM_CP_NOP },
7249 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7250 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7251 .access = PL2_W, .type = ARM_CP_NOP },
7252 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7253 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7254 .access = PL3_W, .type = ARM_CP_NO_RAW,
7255 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
7256 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7257 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7258 .access = PL3_W, .type = ARM_CP_NO_RAW,
7259 .writefn = tlbi_aa64_vae3is_write },
7260 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7261 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7262 .access = PL3_W, .type = ARM_CP_NO_RAW,
7263 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
7264};
7265
de390645
RH
7266static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7267{
7268 Error *err = NULL;
7269 uint64_t ret;
7270
7271 /* Success sets NZCV = 0000. */
7272 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7273
7274 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7275 /*
7276 * ??? Failed, for unknown reasons in the crypto subsystem.
7277 * The best we can do is log the reason and return the
7278 * timed-out indication to the guest. There is no reason
7279 * we know to expect this failure to be transitory, so the
7280 * guest may well hang retrying the operation.
7281 */
7282 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7283 ri->name, error_get_pretty(err));
7284 error_free(err);
7285
7286 env->ZF = 0; /* NZCF = 0100 */
7287 return 0;
7288 }
7289 return ret;
7290}
7291
7292/* We do not support re-seeding, so the two registers operate the same. */
7293static const ARMCPRegInfo rndr_reginfo[] = {
7294 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7295 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7296 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7297 .access = PL0_R, .readfn = rndr_readfn },
7298 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7299 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7300 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7301 .access = PL0_R, .readfn = rndr_readfn },
de390645 7302};
0d57b499
BM
7303
7304#ifndef CONFIG_USER_ONLY
7305static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7306 uint64_t value)
7307{
7308 ARMCPU *cpu = env_archcpu(env);
7309 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7310 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7311 uint64_t vaddr_in = (uint64_t) value;
7312 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7313 void *haddr;
7314 int mem_idx = cpu_mmu_index(env, false);
7315
7316 /* This won't be crossing page boundaries */
7317 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7318 if (haddr) {
7319
7320 ram_addr_t offset;
7321 MemoryRegion *mr;
7322
7323 /* RCU lock is already being held */
7324 mr = memory_region_from_host(haddr, &offset);
7325
7326 if (mr) {
4dfe59d1 7327 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
7328 }
7329 }
7330}
7331
7332static const ARMCPRegInfo dcpop_reg[] = {
7333 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7334 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7335 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7336 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7337};
7338
7339static const ARMCPRegInfo dcpodp_reg[] = {
7340 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7341 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7342 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7343 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7344};
7345#endif /*CONFIG_USER_ONLY*/
7346
4b779ceb
RH
7347static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7348 bool isread)
7349{
7350 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7351 return CP_ACCESS_TRAP_EL2;
7352 }
7353
7354 return CP_ACCESS_OK;
7355}
7356
7357static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7358 bool isread)
7359{
7360 int el = arm_current_el(env);
7361
0da067f2 7362 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7363 uint64_t hcr = arm_hcr_el2_eff(env);
7364 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7365 return CP_ACCESS_TRAP_EL2;
7366 }
4b779ceb
RH
7367 }
7368 if (el < 3 &&
7369 arm_feature(env, ARM_FEATURE_EL3) &&
7370 !(env->cp15.scr_el3 & SCR_ATA)) {
7371 return CP_ACCESS_TRAP_EL3;
7372 }
7373 return CP_ACCESS_OK;
7374}
7375
7376static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7377{
7378 return env->pstate & PSTATE_TCO;
7379}
7380
7381static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7382{
7383 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7384}
7385
7386static const ARMCPRegInfo mte_reginfo[] = {
7387 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7388 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7389 .access = PL1_RW, .accessfn = access_mte,
7390 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7391 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7392 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7393 .access = PL1_RW, .accessfn = access_mte,
7394 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7395 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7396 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7397 .access = PL2_RW, .accessfn = access_mte,
7398 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7399 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7400 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7401 .access = PL3_RW,
7402 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7403 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7404 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7405 .access = PL1_RW, .accessfn = access_mte,
7406 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7407 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7408 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7409 .access = PL1_RW, .accessfn = access_mte,
7410 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7411 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7412 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7413 .access = PL1_R, .accessfn = access_aa64_tid5,
7414 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7415 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7416 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7417 .type = ARM_CP_NO_RAW,
7418 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7419 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7420 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7421 .type = ARM_CP_NOP, .access = PL1_W,
7422 .accessfn = aa64_cacheop_poc_access },
7423 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7424 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7425 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7426 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7427 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7428 .type = ARM_CP_NOP, .access = PL1_W,
7429 .accessfn = aa64_cacheop_poc_access },
7430 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7431 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7432 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7433 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7434 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7435 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7436 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7437 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7438 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7439 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7440 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7441 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7442 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7443 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7444 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7445};
7446
7447static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7448 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7449 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7450 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7451};
5463df16
RH
7452
7453static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7454 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7455 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7456 .type = ARM_CP_NOP, .access = PL0_W,
7457 .accessfn = aa64_cacheop_poc_access },
7458 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7459 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7460 .type = ARM_CP_NOP, .access = PL0_W,
7461 .accessfn = aa64_cacheop_poc_access },
7462 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7463 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7464 .type = ARM_CP_NOP, .access = PL0_W,
7465 .accessfn = aa64_cacheop_poc_access },
7466 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7467 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7468 .type = ARM_CP_NOP, .access = PL0_W,
7469 .accessfn = aa64_cacheop_poc_access },
7470 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7471 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7472 .type = ARM_CP_NOP, .access = PL0_W,
7473 .accessfn = aa64_cacheop_poc_access },
7474 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7475 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7476 .type = ARM_CP_NOP, .access = PL0_W,
7477 .accessfn = aa64_cacheop_poc_access },
7478 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7479 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7480 .type = ARM_CP_NOP, .access = PL0_W,
7481 .accessfn = aa64_cacheop_poc_access },
7482 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7483 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7484 .type = ARM_CP_NOP, .access = PL0_W,
7485 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7486 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7487 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7488 .access = PL0_W, .type = ARM_CP_DC_GVA,
7489#ifndef CONFIG_USER_ONLY
7490 /* Avoid overhead of an access check that always passes in user-mode */
7491 .accessfn = aa64_zva_access,
7492#endif
7493 },
7494 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7495 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7496 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7497#ifndef CONFIG_USER_ONLY
7498 /* Avoid overhead of an access check that always passes in user-mode */
7499 .accessfn = aa64_zva_access,
7500#endif
7501 },
5463df16
RH
7502};
7503
7cb1e618
RH
7504static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7505 bool isread)
7506{
7507 uint64_t hcr = arm_hcr_el2_eff(env);
7508 int el = arm_current_el(env);
7509
7510 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7511 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7512 if (hcr & HCR_TGE) {
7513 return CP_ACCESS_TRAP_EL2;
7514 }
7515 return CP_ACCESS_TRAP;
7516 }
7517 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7518 return CP_ACCESS_TRAP_EL2;
7519 }
7520 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7521 return CP_ACCESS_TRAP_EL2;
7522 }
7523 if (el < 3
7524 && arm_feature(env, ARM_FEATURE_EL3)
7525 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7526 return CP_ACCESS_TRAP_EL3;
7527 }
7528 return CP_ACCESS_OK;
7529}
7530
7531static const ARMCPRegInfo scxtnum_reginfo[] = {
7532 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7533 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7534 .access = PL0_RW, .accessfn = access_scxtnum,
7535 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7536 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7537 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7538 .access = PL1_RW, .accessfn = access_scxtnum,
7539 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7540 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7541 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7542 .access = PL2_RW, .accessfn = access_scxtnum,
7543 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7544 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7545 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7546 .access = PL3_RW,
7547 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7548};
7549#endif /* TARGET_AARCH64 */
967aa94f 7550
cb570bd3
RH
7551static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7552 bool isread)
7553{
7554 int el = arm_current_el(env);
7555
7556 if (el == 0) {
7557 uint64_t sctlr = arm_sctlr(env, el);
7558 if (!(sctlr & SCTLR_EnRCTX)) {
7559 return CP_ACCESS_TRAP;
7560 }
7561 } else if (el == 1) {
7562 uint64_t hcr = arm_hcr_el2_eff(env);
7563 if (hcr & HCR_NV) {
7564 return CP_ACCESS_TRAP_EL2;
7565 }
7566 }
7567 return CP_ACCESS_OK;
7568}
7569
7570static const ARMCPRegInfo predinv_reginfo[] = {
7571 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7572 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7573 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7574 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7575 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7576 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7577 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7578 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7579 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7580 /*
7581 * Note the AArch32 opcodes have a different OPC1.
7582 */
7583 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7584 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7585 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7586 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7587 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7588 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7589 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7590 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7591 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7592};
7593
957e6155
PM
7594static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7595{
7596 /* Read the high 32 bits of the current CCSIDR */
7597 return extract64(ccsidr_read(env, ri), 32, 32);
7598}
7599
7600static const ARMCPRegInfo ccsidr2_reginfo[] = {
7601 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7602 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7603 .access = PL1_R,
e2ce5fcd 7604 .accessfn = access_tid4,
957e6155 7605 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7606};
7607
6a4ef4e5
MZ
7608static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7609 bool isread)
7610{
7611 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7612 return CP_ACCESS_TRAP_EL2;
7613 }
7614
7615 return CP_ACCESS_OK;
7616}
7617
7618static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7619 bool isread)
7620{
7621 if (arm_feature(env, ARM_FEATURE_V8)) {
7622 return access_aa64_tid3(env, ri, isread);
7623 }
7624
7625 return CP_ACCESS_OK;
7626}
7627
f96f3d5f
MZ
7628static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7629 bool isread)
7630{
7631 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7632 return CP_ACCESS_TRAP_EL2;
7633 }
7634
7635 return CP_ACCESS_OK;
7636}
7637
8e228c9e
PM
7638static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7639 const ARMCPRegInfo *ri, bool isread)
7640{
7641 /*
7642 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7643 * in v7A, not in v8A.
7644 */
7645 if (!arm_feature(env, ARM_FEATURE_V8) &&
7646 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7647 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7648 return CP_ACCESS_TRAP_EL2;
7649 }
7650 return CP_ACCESS_OK;
7651}
7652
f96f3d5f
MZ
7653static const ARMCPRegInfo jazelle_regs[] = {
7654 { .name = "JIDR",
7655 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7656 .access = PL1_R, .accessfn = access_jazelle,
7657 .type = ARM_CP_CONST, .resetvalue = 0 },
7658 { .name = "JOSCR",
7659 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7660 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7661 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7662 { .name = "JMCR",
7663 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7664 .accessfn = access_joscr_jmcr,
f96f3d5f 7665 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7666};
7667
52d18727
RH
7668static const ARMCPRegInfo contextidr_el2 = {
7669 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7670 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7671 .access = PL2_RW,
7672 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7673};
7674
e2a1a461 7675static const ARMCPRegInfo vhe_reginfo[] = {
ed30da8e
RH
7676 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7677 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7678 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7679 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7680#ifndef CONFIG_USER_ONLY
7681 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7682 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7683 .fieldoffset =
7684 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7685 .type = ARM_CP_IO, .access = PL2_RW,
7686 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7687 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7688 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7689 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7690 .resetfn = gt_hv_timer_reset,
7691 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7692 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7693 .type = ARM_CP_IO,
7694 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7695 .access = PL2_RW,
7696 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7697 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7698 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7699 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7700 .type = ARM_CP_IO | ARM_CP_ALIAS,
7701 .access = PL2_RW, .accessfn = e2h_access,
7702 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7703 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7704 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7705 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7706 .type = ARM_CP_IO | ARM_CP_ALIAS,
7707 .access = PL2_RW, .accessfn = e2h_access,
7708 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7709 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7710 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7711 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7712 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7713 .access = PL2_RW, .accessfn = e2h_access,
7714 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7715 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7716 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7717 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7718 .access = PL2_RW, .accessfn = e2h_access,
7719 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7720 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7721 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7722 .type = ARM_CP_IO | ARM_CP_ALIAS,
7723 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7724 .access = PL2_RW, .accessfn = e2h_access,
7725 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7726 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7727 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7728 .type = ARM_CP_IO | ARM_CP_ALIAS,
7729 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7730 .access = PL2_RW, .accessfn = e2h_access,
7731 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7732#endif
e2a1a461
RH
7733};
7734
04b07d29
RH
7735#ifndef CONFIG_USER_ONLY
7736static const ARMCPRegInfo ats1e1_reginfo[] = {
3999d2d2 7737 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
7738 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7739 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7740 .writefn = ats_write64 },
3999d2d2 7741 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
7742 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7743 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7744 .writefn = ats_write64 },
04b07d29
RH
7745};
7746
7747static const ARMCPRegInfo ats1cp_reginfo[] = {
7748 { .name = "ATS1CPRP",
7749 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7750 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7751 .writefn = ats_write },
7752 { .name = "ATS1CPWP",
7753 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7754 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7755 .writefn = ats_write },
04b07d29
RH
7756};
7757#endif
7758
f6287c24
PM
7759/*
7760 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7761 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7762 * is non-zero, which is never for ARMv7, optionally in ARMv8
7763 * and mandatorily for ARMv8.2 and up.
7764 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7765 * implementation is RAZ/WI we can ignore this detail, as we
7766 * do for ACTLR.
7767 */
7768static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7769 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7770 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7771 .access = PL1_RW, .accessfn = access_tacr,
7772 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7773 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7774 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7775 .access = PL2_RW, .type = ARM_CP_CONST,
7776 .resetvalue = 0 },
f6287c24
PM
7777};
7778
2ceb98c0
PM
7779void register_cp_regs_for_features(ARMCPU *cpu)
7780{
7781 /* Register all the coprocessor registers based on feature bits */
7782 CPUARMState *env = &cpu->env;
7783 if (arm_feature(env, ARM_FEATURE_M)) {
7784 /* M profile has no coprocessor registers */
7785 return;
7786 }
7787
e9aa6c21 7788 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6 7789 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
7790 /*
7791 * Must go early as it is full of wildcards that may be
9449fdf6
PM
7792 * overridden by later definitions.
7793 */
7794 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7795 }
7796
7d57f408 7797 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7798 /* The ID registers all have impdef reset values */
7799 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7800 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7801 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7802 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7803 .accessfn = access_aa32_tid3,
8a130a7b 7804 .resetvalue = cpu->isar.id_pfr0 },
9b37a28c
FR
7805 /*
7806 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
96a8b92e
PM
7807 * the value of the GIC field until after we define these regs.
7808 */
0ff644a7
PM
7809 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7810 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7811 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7812 .accessfn = access_aa32_tid3,
96a8b92e
PM
7813 .readfn = id_pfr1_read,
7814 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7815 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7816 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7817 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7818 .accessfn = access_aa32_tid3,
a6179538 7819 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7820 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7821 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7822 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7823 .accessfn = access_aa32_tid3,
8515a092 7824 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7825 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7826 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7827 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7828 .accessfn = access_aa32_tid3,
10054016 7829 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7830 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7831 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7832 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7833 .accessfn = access_aa32_tid3,
10054016 7834 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7835 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7836 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7837 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7838 .accessfn = access_aa32_tid3,
10054016 7839 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7840 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7841 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7842 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7843 .accessfn = access_aa32_tid3,
10054016 7844 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7845 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7846 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7847 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7848 .accessfn = access_aa32_tid3,
47576b94 7849 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7850 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7851 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7852 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7853 .accessfn = access_aa32_tid3,
47576b94 7854 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7855 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7856 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7857 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7858 .accessfn = access_aa32_tid3,
47576b94 7859 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7860 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7861 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7862 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7863 .accessfn = access_aa32_tid3,
47576b94 7864 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7865 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7866 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7867 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7868 .accessfn = access_aa32_tid3,
47576b94 7869 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7870 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7871 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7872 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7873 .accessfn = access_aa32_tid3,
47576b94 7874 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7875 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7876 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7877 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7878 .accessfn = access_aa32_tid3,
10054016 7879 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7880 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7881 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7882 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7883 .accessfn = access_aa32_tid3,
47576b94 7884 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7885 };
7886 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7887 define_arm_cp_regs(cpu, v6_cp_reginfo);
7888 } else {
7889 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7890 }
4d31c596
PM
7891 if (arm_feature(env, ARM_FEATURE_V6K)) {
7892 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7893 }
5e5cf9e3 7894 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7895 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7896 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7897 }
327dd510
AL
7898 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7899 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7900 }
e9aa6c21 7901 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7902 ARMCPRegInfo clidr = {
7da845b0
PM
7903 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7904 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d 7905 .access = PL1_R, .type = ARM_CP_CONST,
e2ce5fcd 7906 .accessfn = access_tid4,
630fcd4d 7907 .resetvalue = cpu->clidr
776d4e5c 7908 };
776d4e5c 7909 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7910 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7911 define_debug_regs(cpu);
24183fb6 7912 define_pmu_regs(cpu);
7d57f408
PM
7913 } else {
7914 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7915 }
b0d2b7d0 7916 if (arm_feature(env, ARM_FEATURE_V8)) {
dde4d028
PM
7917 /*
7918 * v8 ID registers, which all have impdef reset values.
e20d84c1
PM
7919 * Note that within the ID register ranges the unused slots
7920 * must all RAZ, not UNDEF; future architecture versions may
7921 * define new registers here.
dde4d028
PM
7922 * ID registers which are AArch64 views of the AArch32 ID registers
7923 * which already existed in v6 and v7 are handled elsewhere,
7924 * in v6_idregs[].
e20d84c1 7925 */
dde4d028 7926 int i;
e60cef86 7927 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7928 /*
7929 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7930 * emulation because we don't know the right value for the
7931 * GIC field until after we define these regs.
96a8b92e 7932 */
e60cef86
PM
7933 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7935 .access = PL1_R,
7936#ifdef CONFIG_USER_ONLY
7937 .type = ARM_CP_CONST,
7938 .resetvalue = cpu->isar.id_aa64pfr0
7939#else
7940 .type = ARM_CP_NO_RAW,
6a4ef4e5 7941 .accessfn = access_aa64_tid3,
96a8b92e 7942 .readfn = id_aa64pfr0_read,
976b99b6
AB
7943 .writefn = arm_cp_write_ignore
7944#endif
7945 },
e60cef86
PM
7946 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7947 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7948 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7949 .accessfn = access_aa64_tid3,
47576b94 7950 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7951 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7952 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7953 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7954 .accessfn = access_aa64_tid3,
e20d84c1
PM
7955 .resetvalue = 0 },
7956 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7957 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7958 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7959 .accessfn = access_aa64_tid3,
e20d84c1 7960 .resetvalue = 0 },
9516d772 7961 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7962 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7963 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7964 .accessfn = access_aa64_tid3,
2dc10fa2 7965 .resetvalue = cpu->isar.id_aa64zfr0 },
414c54d5 7966 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7967 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7968 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7969 .accessfn = access_aa64_tid3,
414c54d5 7970 .resetvalue = cpu->isar.id_aa64smfr0 },
e20d84c1
PM
7971 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7973 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7974 .accessfn = access_aa64_tid3,
e20d84c1
PM
7975 .resetvalue = 0 },
7976 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7977 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7978 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7979 .accessfn = access_aa64_tid3,
e20d84c1 7980 .resetvalue = 0 },
e60cef86
PM
7981 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7982 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7983 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7984 .accessfn = access_aa64_tid3,
2a609df8 7985 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7986 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7987 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7988 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7989 .accessfn = access_aa64_tid3,
2a609df8 7990 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7991 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7993 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7994 .accessfn = access_aa64_tid3,
e20d84c1
PM
7995 .resetvalue = 0 },
7996 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7997 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7998 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7999 .accessfn = access_aa64_tid3,
e20d84c1 8000 .resetvalue = 0 },
e60cef86
PM
8001 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8002 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8003 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8004 .accessfn = access_aa64_tid3,
e60cef86
PM
8005 .resetvalue = cpu->id_aa64afr0 },
8006 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8007 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8008 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8009 .accessfn = access_aa64_tid3,
e60cef86 8010 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
8011 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8013 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8014 .accessfn = access_aa64_tid3,
e20d84c1
PM
8015 .resetvalue = 0 },
8016 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8017 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8018 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8019 .accessfn = access_aa64_tid3,
e20d84c1 8020 .resetvalue = 0 },
e60cef86
PM
8021 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8022 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8023 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8024 .accessfn = access_aa64_tid3,
47576b94 8025 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
8026 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8027 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8028 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8029 .accessfn = access_aa64_tid3,
47576b94 8030 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
8031 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8033 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8034 .accessfn = access_aa64_tid3,
e20d84c1
PM
8035 .resetvalue = 0 },
8036 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8037 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8038 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8039 .accessfn = access_aa64_tid3,
e20d84c1
PM
8040 .resetvalue = 0 },
8041 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8042 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8043 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8044 .accessfn = access_aa64_tid3,
e20d84c1
PM
8045 .resetvalue = 0 },
8046 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8047 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8048 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8049 .accessfn = access_aa64_tid3,
e20d84c1
PM
8050 .resetvalue = 0 },
8051 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8052 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8053 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8054 .accessfn = access_aa64_tid3,
e20d84c1
PM
8055 .resetvalue = 0 },
8056 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8057 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8058 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8059 .accessfn = access_aa64_tid3,
e20d84c1 8060 .resetvalue = 0 },
e60cef86
PM
8061 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8062 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8063 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8064 .accessfn = access_aa64_tid3,
3dc91ddb 8065 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
8066 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8067 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8068 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8069 .accessfn = access_aa64_tid3,
3dc91ddb 8070 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 8071 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8072 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8073 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8074 .accessfn = access_aa64_tid3,
64761e10 8075 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
8076 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8077 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8078 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8079 .accessfn = access_aa64_tid3,
e20d84c1
PM
8080 .resetvalue = 0 },
8081 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8082 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8083 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8084 .accessfn = access_aa64_tid3,
e20d84c1
PM
8085 .resetvalue = 0 },
8086 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8087 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8088 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8089 .accessfn = access_aa64_tid3,
e20d84c1
PM
8090 .resetvalue = 0 },
8091 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8092 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8093 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8094 .accessfn = access_aa64_tid3,
e20d84c1
PM
8095 .resetvalue = 0 },
8096 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8097 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8098 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8099 .accessfn = access_aa64_tid3,
e20d84c1 8100 .resetvalue = 0 },
a50c0f51
PM
8101 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8102 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8103 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8104 .accessfn = access_aa64_tid3,
47576b94 8105 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
8106 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8107 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8108 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8109 .accessfn = access_aa64_tid3,
47576b94 8110 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
8111 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8112 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8113 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8114 .accessfn = access_aa64_tid3,
47576b94 8115 .resetvalue = cpu->isar.mvfr2 },
dde4d028
PM
8116 /*
8117 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8118 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8119 * as RAZ, since it is in the "reserved for future ID
8120 * registers, RAZ" part of the AArch32 encoding space.
8121 */
8122 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8123 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8124 .access = PL1_R, .type = ARM_CP_CONST,
8125 .accessfn = access_aa64_tid3,
8126 .resetvalue = 0 },
8127 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8128 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8129 .access = PL1_R, .type = ARM_CP_CONST,
8130 .accessfn = access_aa64_tid3,
8131 .resetvalue = 0 },
8132 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8133 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8134 .access = PL1_R, .type = ARM_CP_CONST,
8135 .accessfn = access_aa64_tid3,
8136 .resetvalue = 0 },
8137 /*
8138 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8139 * they're also RAZ for AArch64, and in v8 are gradually
8140 * being filled with AArch64-view-of-AArch32-ID-register
8141 * for new ID registers.
8142 */
8143 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8144 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8145 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8146 .accessfn = access_aa64_tid3,
e20d84c1 8147 .resetvalue = 0 },
1d51bc96 8148 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8149 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8150 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8151 .accessfn = access_aa64_tid3,
1d51bc96 8152 .resetvalue = cpu->isar.id_pfr2 },
d22c5649 8153 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8154 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8155 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8156 .accessfn = access_aa64_tid3,
d22c5649 8157 .resetvalue = cpu->isar.id_dfr1 },
32957aad 8158 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8159 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8160 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8161 .accessfn = access_aa64_tid3,
32957aad 8162 .resetvalue = cpu->isar.id_mmfr5 },
dde4d028 8163 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8164 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8165 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8166 .accessfn = access_aa64_tid3,
e20d84c1 8167 .resetvalue = 0 },
4054bfa9
AF
8168 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8169 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8170 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 8171 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
8172 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8173 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8174 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8175 .resetvalue = cpu->pmceid0 },
8176 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8177 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8178 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 8179 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
8180 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8181 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8182 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8183 .resetvalue = cpu->pmceid1 },
e60cef86 8184 };
6c5c0fec 8185#ifdef CONFIG_USER_ONLY
10b0220e 8186 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec 8187 { .name = "ID_AA64PFR0_EL1",
bc6bd20e
ZS
8188 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8189 R_ID_AA64PFR0_ADVSIMD_MASK |
8190 R_ID_AA64PFR0_SVE_MASK |
8191 R_ID_AA64PFR0_DIT_MASK,
8192 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8193 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
6c5c0fec 8194 { .name = "ID_AA64PFR1_EL1",
bc6bd20e
ZS
8195 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8196 R_ID_AA64PFR1_SSBS_MASK |
8197 R_ID_AA64PFR1_MTE_MASK |
8198 R_ID_AA64PFR1_SME_MASK },
d040242e 8199 { .name = "ID_AA64PFR*_EL1_RESERVED",
bc6bd20e
ZS
8200 .is_glob = true },
8201 { .name = "ID_AA64ZFR0_EL1",
8202 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8203 R_ID_AA64ZFR0_AES_MASK |
8204 R_ID_AA64ZFR0_BITPERM_MASK |
8205 R_ID_AA64ZFR0_BFLOAT16_MASK |
8206 R_ID_AA64ZFR0_SHA3_MASK |
8207 R_ID_AA64ZFR0_SM4_MASK |
8208 R_ID_AA64ZFR0_I8MM_MASK |
8209 R_ID_AA64ZFR0_F32MM_MASK |
8210 R_ID_AA64ZFR0_F64MM_MASK },
8211 { .name = "ID_AA64SMFR0_EL1",
8212 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8213 R_ID_AA64SMFR0_B16F32_MASK |
8214 R_ID_AA64SMFR0_F16F32_MASK |
8215 R_ID_AA64SMFR0_I8I32_MASK |
8216 R_ID_AA64SMFR0_F64F64_MASK |
8217 R_ID_AA64SMFR0_I16I64_MASK |
8218 R_ID_AA64SMFR0_FA64_MASK },
6c5c0fec 8219 { .name = "ID_AA64MMFR0_EL1",
bc6bd20e
ZS
8220 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8221 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8222 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8223 { .name = "ID_AA64MMFR1_EL1",
8224 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8225 { .name = "ID_AA64MMFR2_EL1",
8226 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
d040242e 8227 { .name = "ID_AA64MMFR*_EL1_RESERVED",
bc6bd20e 8228 .is_glob = true },
6c5c0fec 8229 { .name = "ID_AA64DFR0_EL1",
bc6bd20e
ZS
8230 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8231 { .name = "ID_AA64DFR1_EL1" },
d040242e 8232 { .name = "ID_AA64DFR*_EL1_RESERVED",
bc6bd20e 8233 .is_glob = true },
d040242e 8234 { .name = "ID_AA64AFR*",
bc6bd20e 8235 .is_glob = true },
6c5c0fec 8236 { .name = "ID_AA64ISAR0_EL1",
bc6bd20e
ZS
8237 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8238 R_ID_AA64ISAR0_SHA1_MASK |
8239 R_ID_AA64ISAR0_SHA2_MASK |
8240 R_ID_AA64ISAR0_CRC32_MASK |
8241 R_ID_AA64ISAR0_ATOMIC_MASK |
8242 R_ID_AA64ISAR0_RDM_MASK |
8243 R_ID_AA64ISAR0_SHA3_MASK |
8244 R_ID_AA64ISAR0_SM3_MASK |
8245 R_ID_AA64ISAR0_SM4_MASK |
8246 R_ID_AA64ISAR0_DP_MASK |
8247 R_ID_AA64ISAR0_FHM_MASK |
8248 R_ID_AA64ISAR0_TS_MASK |
8249 R_ID_AA64ISAR0_RNDR_MASK },
6c5c0fec 8250 { .name = "ID_AA64ISAR1_EL1",
bc6bd20e
ZS
8251 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8252 R_ID_AA64ISAR1_APA_MASK |
8253 R_ID_AA64ISAR1_API_MASK |
8254 R_ID_AA64ISAR1_JSCVT_MASK |
8255 R_ID_AA64ISAR1_FCMA_MASK |
8256 R_ID_AA64ISAR1_LRCPC_MASK |
8257 R_ID_AA64ISAR1_GPA_MASK |
8258 R_ID_AA64ISAR1_GPI_MASK |
8259 R_ID_AA64ISAR1_FRINTTS_MASK |
8260 R_ID_AA64ISAR1_SB_MASK |
8261 R_ID_AA64ISAR1_BF16_MASK |
8262 R_ID_AA64ISAR1_DGH_MASK |
8263 R_ID_AA64ISAR1_I8MM_MASK },
8264 { .name = "ID_AA64ISAR2_EL1",
8265 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8266 R_ID_AA64ISAR2_RPRES_MASK |
8267 R_ID_AA64ISAR2_GPA3_MASK |
8268 R_ID_AA64ISAR2_APA3_MASK },
d040242e 8269 { .name = "ID_AA64ISAR*_EL1_RESERVED",
bc6bd20e 8270 .is_glob = true },
6c5c0fec
AB
8271 };
8272 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8273#endif
be8e8128
GB
8274 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8275 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8276 !arm_feature(env, ARM_FEATURE_EL2)) {
8277 ARMCPRegInfo rvbar = {
910e4f24 8278 .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
be8e8128 8279 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8280 .access = PL1_R,
8281 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
8282 };
8283 define_one_arm_cp_reg(cpu, &rvbar);
8284 }
e60cef86 8285 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0 8286 define_arm_cp_regs(cpu, v8_cp_reginfo);
dde4d028
PM
8287
8288 for (i = 4; i < 16; i++) {
8289 /*
8290 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8291 * For pre-v8 cores there are RAZ patterns for these in
8292 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8293 * v8 extends the "must RAZ" part of the ID register space
8294 * to also cover c0, 0, c{8-15}, {0-7}.
8295 * These are STATE_AA32 because in the AArch64 sysreg space
8296 * c4-c7 is where the AArch64 ID registers live (and we've
8297 * already defined those in v8_idregs[]), and c8-c15 are not
8298 * "must RAZ" for AArch64.
8299 */
8300 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8301 ARMCPRegInfo v8_aa32_raz_idregs = {
8302 .name = name,
8303 .state = ARM_CP_STATE_AA32,
8304 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8305 .access = PL1_R, .type = ARM_CP_CONST,
8306 .accessfn = access_aa64_tid3,
8307 .resetvalue = 0 };
8308 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8309 }
b0d2b7d0 8310 }
99a90811
RH
8311
8312 /*
8313 * Register the base EL2 cpregs.
8314 * Pre v8, these registers are implemented only as part of the
8315 * Virtualization Extensions (EL2 present). Beginning with v8,
8316 * if EL2 is missing but EL3 is enabled, mostly these become
8317 * RES0 from EL3, with some specific exceptions.
8318 */
8319 if (arm_feature(env, ARM_FEATURE_EL2)
8320 || (arm_feature(env, ARM_FEATURE_EL3)
8321 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 8322 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
8323 ARMCPRegInfo vpidr_regs[] = {
8324 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8325 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8326 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8327 .resetvalue = cpu->midr,
8328 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8329 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
8330 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8331 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8332 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 8333 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 8334 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
8335 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8336 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8337 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8338 .resetvalue = vmpidr_def,
8339 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8340 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
8341 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8342 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
8343 .access = PL2_RW, .resetvalue = vmpidr_def,
8344 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 8345 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6 8346 };
24526bb9
PM
8347 /*
8348 * The only field of MDCR_EL2 that has a defined architectural reset
8349 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8350 */
8351 ARMCPRegInfo mdcr_el2 = {
7f4fbfb5 8352 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
24526bb9 8353 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
01765386 8354 .writefn = mdcr_el2_write,
24526bb9
PM
8355 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8356 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8357 };
8358 define_one_arm_cp_reg(cpu, &mdcr_el2);
731de9e6 8359 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 8360 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
8361 if (arm_feature(env, ARM_FEATURE_V8)) {
8362 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8363 }
e9152ee9
RDC
8364 if (cpu_isar_feature(aa64_sel2, cpu)) {
8365 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8366 }
be8e8128
GB
8367 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8368 if (!arm_feature(env, ARM_FEATURE_EL3)) {
910e4f24
TR
8369 ARMCPRegInfo rvbar[] = {
8370 {
8371 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8372 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8373 .access = PL2_R,
8374 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8375 },
8376 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8377 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8378 .access = PL2_R,
8379 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8380 },
be8e8128 8381 };
910e4f24 8382 define_arm_cp_regs(cpu, rvbar);
be8e8128 8383 }
3b685ba7 8384 }
99a90811
RH
8385
8386 /* Register the base EL3 cpregs. */
81547d66 8387 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 8388 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
8389 ARMCPRegInfo el3_regs[] = {
8390 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8391 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8392 .access = PL3_R,
8393 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8394 },
e24fdd23
PM
8395 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8396 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8397 .access = PL3_RW,
8398 .raw_writefn = raw_write, .writefn = sctlr_write,
8399 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8400 .resetvalue = cpu->reset_sctlr },
be8e8128 8401 };
e24fdd23
PM
8402
8403 define_arm_cp_regs(cpu, el3_regs);
81547d66 8404 }
9b37a28c
FR
8405 /*
8406 * The behaviour of NSACR is sufficiently various that we don't
2f027fc5
PM
8407 * try to describe it in a single reginfo:
8408 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8409 * reads as constant 0xc00 from NS EL1 and NS EL2
8410 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8411 * if v7 without EL3, register doesn't exist
8412 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8413 */
8414 if (arm_feature(env, ARM_FEATURE_EL3)) {
8415 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 8416 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8417 .name = "NSACR", .type = ARM_CP_CONST,
8418 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8419 .access = PL1_RW, .accessfn = nsacr_access,
8420 .resetvalue = 0xc00
8421 };
8422 define_one_arm_cp_reg(cpu, &nsacr);
8423 } else {
10b0220e 8424 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8425 .name = "NSACR",
8426 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8427 .access = PL3_RW | PL1_R,
8428 .resetvalue = 0,
8429 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8430 };
8431 define_one_arm_cp_reg(cpu, &nsacr);
8432 }
8433 } else {
8434 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 8435 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8436 .name = "NSACR", .type = ARM_CP_CONST,
8437 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8438 .access = PL1_R,
8439 .resetvalue = 0xc00
8440 };
8441 define_one_arm_cp_reg(cpu, &nsacr);
8442 }
8443 }
8444
452a0955 8445 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8446 if (arm_feature(env, ARM_FEATURE_V6)) {
8447 /* PMSAv6 not implemented */
8448 assert(arm_feature(env, ARM_FEATURE_V7));
8449 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8450 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8451 } else {
8452 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8453 }
18032bec 8454 } else {
8e5d75c9 8455 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8456 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8457 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8458 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8459 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8460 }
18032bec 8461 }
c326b979
PM
8462 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8463 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8464 }
6cc7a3ae
PM
8465 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8466 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8467 }
4a501606
PM
8468 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8469 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8470 }
c4804214
PM
8471 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8472 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8473 }
8474 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8475 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8476 }
8477 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8478 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8479 }
18032bec
PM
8480 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8481 define_arm_cp_regs(cpu, omap_cp_reginfo);
8482 }
34f90529
PM
8483 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8484 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8485 }
1047b9d7
PM
8486 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8487 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8488 }
8489 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8490 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8491 }
7ac681cf
PM
8492 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8493 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8494 }
873b73c0 8495 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8496 define_arm_cp_regs(cpu, jazelle_regs);
8497 }
9b37a28c
FR
8498 /*
8499 * Slightly awkwardly, the OMAP and StrongARM cores need all of
7884849c
PM
8500 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8501 * be read-only (ie write causes UNDEF exception).
8502 */
8503 {
00a29f3d 8504 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9b37a28c
FR
8505 /*
8506 * Pre-v8 MIDR space.
00a29f3d 8507 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8508 * of the TI925 behaviour where writes to another register can
8509 * cause the MIDR value to change.
97ce8d61
PC
8510 *
8511 * Unimplemented registers in the c15 0 0 0 space default to
8512 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8513 * and friends override accordingly.
7884849c
PM
8514 */
8515 { .name = "MIDR",
97ce8d61 8516 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8517 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8518 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8519 .readfn = midr_read,
97ce8d61
PC
8520 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8521 .type = ARM_CP_OVERRIDE },
7884849c
PM
8522 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8523 { .name = "DUMMY",
8524 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8525 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8526 { .name = "DUMMY",
8527 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8528 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8529 { .name = "DUMMY",
8530 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8531 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8532 { .name = "DUMMY",
8533 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8534 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8535 { .name = "DUMMY",
8536 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8537 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8538 };
00a29f3d 8539 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8540 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8541 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
8542 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8543 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8544 .readfn = midr_read },
c7f786ab 8545 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
ac00c79f
SF
8546 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8547 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8548 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8549 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8550 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8551 .access = PL1_R,
8552 .accessfn = access_aa64_tid1,
8553 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d 8554 };
c7f786ab
TR
8555 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8556 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8557 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8558 .access = PL1_R, .resetvalue = cpu->midr
8559 };
00a29f3d
PM
8560 ARMCPRegInfo id_cp_reginfo[] = {
8561 /* These are common to v8 and pre-v8 */
8562 { .name = "CTR",
8563 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8564 .access = PL1_R, .accessfn = ctr_el0_access,
8565 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8566 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8567 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8568 .access = PL0_R, .accessfn = ctr_el0_access,
8569 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8570 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8571 { .name = "TCMTR",
8572 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8573 .access = PL1_R,
8574 .accessfn = access_aa32_tid1,
8575 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8576 };
8085ce63
PC
8577 /* TLBTR is specific to VMSA */
8578 ARMCPRegInfo id_tlbtr_reginfo = {
8579 .name = "TLBTR",
8580 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8581 .access = PL1_R,
8582 .accessfn = access_aa32_tid1,
8583 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8584 };
3281af81
PC
8585 /* MPUIR is specific to PMSA V6+ */
8586 ARMCPRegInfo id_mpuir_reginfo = {
8587 .name = "MPUIR",
8588 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8589 .access = PL1_R, .type = ARM_CP_CONST,
8590 .resetvalue = cpu->pmsav7_dregion << 8
8591 };
761c4642
TR
8592 /* HMPUIR is specific to PMSA V8 */
8593 ARMCPRegInfo id_hmpuir_reginfo = {
8594 .name = "HMPUIR",
8595 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8596 .access = PL2_R, .type = ARM_CP_CONST,
8597 .resetvalue = cpu->pmsav8r_hdregion
8598 };
10b0220e 8599 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8600 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8601 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8602 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8603 };
6c5c0fec 8604#ifdef CONFIG_USER_ONLY
10b0220e 8605 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec 8606 { .name = "MIDR_EL1",
bc6bd20e
ZS
8607 .exported_bits = R_MIDR_EL1_REVISION_MASK |
8608 R_MIDR_EL1_PARTNUM_MASK |
8609 R_MIDR_EL1_ARCHITECTURE_MASK |
8610 R_MIDR_EL1_VARIANT_MASK |
8611 R_MIDR_EL1_IMPLEMENTER_MASK },
8612 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8613 };
8614 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8615#endif
7884849c
PM
8616 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8617 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8618 size_t i;
9b37a28c
FR
8619 /*
8620 * Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8621 * whole space. Then update the specific ID registers to allow write
8622 * access, so that they ignore writes rather than causing them to
8623 * UNDEF.
7884849c
PM
8624 */
8625 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8626 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8627 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8628 }
5809ac57
RH
8629 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8630 id_cp_reginfo[i].access = PL1_RW;
7884849c 8631 }
10006112 8632 id_mpuir_reginfo.access = PL1_RW;
3281af81 8633 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8634 }
00a29f3d
PM
8635 if (arm_feature(env, ARM_FEATURE_V8)) {
8636 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
c7f786ab
TR
8637 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8638 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8639 }
00a29f3d
PM
8640 } else {
8641 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8642 }
a703eda1 8643 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8644 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8645 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
761c4642
TR
8646 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8647 arm_feature(env, ARM_FEATURE_V8)) {
8648 uint32_t i = 0;
8649 char *tmp_string;
8650
8651 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8652 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8653 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8654
8655 /* Register alias is only valid for first 32 indexes */
8656 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8657 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8658 uint8_t opc1 = extract32(i, 4, 1);
8659 uint8_t opc2 = extract32(i, 0, 1) << 2;
8660
8661 tmp_string = g_strdup_printf("PRBAR%u", i);
8662 ARMCPRegInfo tmp_prbarn_reginfo = {
8663 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8664 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8665 .access = PL1_RW, .resetvalue = 0,
8666 .accessfn = access_tvm_trvm,
8667 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8668 };
8669 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8670 g_free(tmp_string);
8671
8672 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8673 tmp_string = g_strdup_printf("PRLAR%u", i);
8674 ARMCPRegInfo tmp_prlarn_reginfo = {
8675 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8676 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8677 .access = PL1_RW, .resetvalue = 0,
8678 .accessfn = access_tvm_trvm,
8679 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8680 };
8681 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8682 g_free(tmp_string);
8683 }
8684
8685 /* Register alias is only valid for first 32 indexes */
8686 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8687 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8688 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8689 uint8_t opc2 = extract32(i, 0, 1) << 2;
8690
8691 tmp_string = g_strdup_printf("HPRBAR%u", i);
8692 ARMCPRegInfo tmp_hprbarn_reginfo = {
8693 .name = tmp_string,
8694 .type = ARM_CP_NO_RAW,
8695 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8696 .access = PL2_RW, .resetvalue = 0,
8697 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8698 };
8699 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8700 g_free(tmp_string);
8701
8702 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8703 tmp_string = g_strdup_printf("HPRLAR%u", i);
8704 ARMCPRegInfo tmp_hprlarn_reginfo = {
8705 .name = tmp_string,
8706 .type = ARM_CP_NO_RAW,
8707 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8708 .access = PL2_RW, .resetvalue = 0,
8709 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8710 };
8711 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8712 g_free(tmp_string);
8713 }
3281af81
PC
8714 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8715 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8716 }
7884849c
PM
8717 }
8718
97ce8d61 8719 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8720 ARMCPRegInfo mpidr_cp_reginfo[] = {
8721 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8722 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8723 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8724 };
8725#ifdef CONFIG_USER_ONLY
10b0220e 8726 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8727 { .name = "MPIDR_EL1",
8728 .fixed_bits = 0x0000000080000000 },
52264166
AB
8729 };
8730 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8731#endif
97ce8d61
PC
8732 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8733 }
8734
2771db27 8735 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8736 ARMCPRegInfo auxcr_reginfo[] = {
8737 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8738 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8739 .access = PL1_RW, .accessfn = access_tacr,
8740 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8741 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8742 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8743 .access = PL2_RW, .type = ARM_CP_CONST,
8744 .resetvalue = 0 },
8745 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8746 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8747 .access = PL3_RW, .type = ARM_CP_CONST,
8748 .resetvalue = 0 },
2771db27 8749 };
834a6c69 8750 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8751 if (cpu_isar_feature(aa32_ac2, cpu)) {
8752 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8753 }
2771db27
PM
8754 }
8755
d8ba780b 8756 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8757 /*
8758 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8759 * There are two flavours:
8760 * (1) older 32-bit only cores have a simple 32-bit CBAR
8761 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8762 * 32-bit register visible to AArch32 at a different encoding
8763 * to the "flavour 1" register and with the bits rearranged to
8764 * be able to squash a 64-bit address into the 32-bit view.
8765 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8766 * in future if we support AArch32-only configs of some of the
8767 * AArch64 cores we might need to add a specific feature flag
8768 * to indicate cores with "flavour 2" CBAR.
8769 */
f318cec6
PM
8770 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8771 /* 32 bit view is [31:18] 0...0 [43:32]. */
8772 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8773 | extract64(cpu->reset_cbar, 32, 12);
8774 ARMCPRegInfo cbar_reginfo[] = {
8775 { .name = "CBAR",
8776 .type = ARM_CP_CONST,
d56974af
LM
8777 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8778 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8779 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8780 .type = ARM_CP_CONST,
8781 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8782 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8783 };
8784 /* We don't implement a r/w 64 bit CBAR currently */
8785 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8786 define_arm_cp_regs(cpu, cbar_reginfo);
8787 } else {
8788 ARMCPRegInfo cbar = {
8789 .name = "CBAR",
8790 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
04215eb1 8791 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
f318cec6
PM
8792 .fieldoffset = offsetof(CPUARMState,
8793 cp15.c15_config_base_address)
8794 };
8795 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8796 cbar.access = PL1_R;
8797 cbar.fieldoffset = 0;
8798 cbar.type = ARM_CP_CONST;
8799 }
8800 define_one_arm_cp_reg(cpu, &cbar);
8801 }
d8ba780b
PC
8802 }
8803
91db4642 8804 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8805 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8806 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8807 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8808 .access = PL1_RW, .writefn = vbar_write,
8809 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8810 offsetof(CPUARMState, cp15.vbar_ns) },
8811 .resetvalue = 0 },
91db4642
CLG
8812 };
8813 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8814 }
8815
2771db27
PM
8816 /* Generic registers whose values depend on the implementation */
8817 {
8818 ARMCPRegInfo sctlr = {
5ebafdf3 8819 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8820 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8821 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8822 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8823 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8824 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8825 .raw_writefn = raw_write,
2771db27
PM
8826 };
8827 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9b37a28c
FR
8828 /*
8829 * Normally we would always end the TB on an SCTLR write, but Linux
2771db27
PM
8830 * arch/arm/mach-pxa/sleep.S expects two instructions following
8831 * an MMU enable to execute from cache. Imitate this behaviour.
8832 */
8833 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8834 }
8835 define_one_arm_cp_reg(cpu, &sctlr);
761c4642
TR
8836
8837 if (arm_feature(env, ARM_FEATURE_PMSA) &&
8838 arm_feature(env, ARM_FEATURE_V8)) {
8839 ARMCPRegInfo vsctlr = {
8840 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8841 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8842 .access = PL2_RW, .resetvalue = 0x0,
8843 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8844 };
8845 define_one_arm_cp_reg(cpu, &vsctlr);
8846 }
2771db27 8847 }
5be5e8ed 8848
2d7137c1 8849 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8850 define_arm_cp_regs(cpu, lor_reginfo);
8851 }
220f508f
RH
8852 if (cpu_isar_feature(aa64_pan, cpu)) {
8853 define_one_arm_cp_reg(cpu, &pan_reginfo);
8854 }
04b07d29
RH
8855#ifndef CONFIG_USER_ONLY
8856 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8857 define_arm_cp_regs(cpu, ats1e1_reginfo);
8858 }
8859 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8860 define_arm_cp_regs(cpu, ats1cp_reginfo);
8861 }
8862#endif
9eeb7a1c
RH
8863 if (cpu_isar_feature(aa64_uao, cpu)) {
8864 define_one_arm_cp_reg(cpu, &uao_reginfo);
8865 }
2d7137c1 8866
dc8b1853
RC
8867 if (cpu_isar_feature(aa64_dit, cpu)) {
8868 define_one_arm_cp_reg(cpu, &dit_reginfo);
8869 }
f2f68a78
RC
8870 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8871 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8872 }
58e93b48
RH
8873 if (cpu_isar_feature(any_ras, cpu)) {
8874 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8875 }
dc8b1853 8876
52d18727
RH
8877 if (cpu_isar_feature(aa64_vh, cpu) ||
8878 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8879 define_one_arm_cp_reg(cpu, &contextidr_el2);
8880 }
e2a1a461
RH
8881 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8882 define_arm_cp_regs(cpu, vhe_reginfo);
8883 }
8884
cd208a1c 8885 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 8886 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 8887 }
967aa94f 8888
5814d587
RH
8889 if (cpu_isar_feature(aa64_hcx, cpu)) {
8890 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8891 }
8892
967aa94f 8893#ifdef TARGET_AARCH64
9e5ec745
RH
8894 if (cpu_isar_feature(aa64_sme, cpu)) {
8895 define_arm_cp_regs(cpu, sme_reginfo);
8896 }
967aa94f
RH
8897 if (cpu_isar_feature(aa64_pauth, cpu)) {
8898 define_arm_cp_regs(cpu, pauth_reginfo);
8899 }
de390645
RH
8900 if (cpu_isar_feature(aa64_rndr, cpu)) {
8901 define_arm_cp_regs(cpu, rndr_reginfo);
8902 }
84940ed8
RC
8903 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8904 define_arm_cp_regs(cpu, tlbirange_reginfo);
8905 }
7113d618
RC
8906 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8907 define_arm_cp_regs(cpu, tlbios_reginfo);
8908 }
0d57b499
BM
8909#ifndef CONFIG_USER_ONLY
8910 /* Data Cache clean instructions up to PoP */
8911 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8912 define_one_arm_cp_reg(cpu, dcpop_reg);
8913
8914 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8915 define_one_arm_cp_reg(cpu, dcpodp_reg);
8916 }
8917 }
8918#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8919
8920 /*
8921 * If full MTE is enabled, add all of the system registers.
8922 * If only "instructions available at EL0" are enabled,
8923 * then define only a RAZ/WI version of PSTATE.TCO.
8924 */
8925 if (cpu_isar_feature(aa64_mte, cpu)) {
8926 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8927 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8928 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8929 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8930 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8931 }
7cb1e618
RH
8932
8933 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8934 define_arm_cp_regs(cpu, scxtnum_reginfo);
8935 }
967aa94f 8936#endif
cb570bd3 8937
22e57073 8938 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8939 define_arm_cp_regs(cpu, predinv_reginfo);
8940 }
e2cce18f 8941
957e6155
PM
8942 if (cpu_isar_feature(any_ccidx, cpu)) {
8943 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8944 }
8945
e2cce18f
RH
8946#ifndef CONFIG_USER_ONLY
8947 /*
8948 * Register redirections and aliases must be done last,
8949 * after the registers from the other extensions have been defined.
8950 */
8951 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8952 define_arm_vh_e2h_redirects_aliases(cpu);
8953 }
8954#endif
2ceb98c0
PM
8955}
8956
777dc784
PM
8957/* Sort alphabetically by type name, except for "any". */
8958static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8959{
777dc784
PM
8960 ObjectClass *class_a = (ObjectClass *)a;
8961 ObjectClass *class_b = (ObjectClass *)b;
8962 const char *name_a, *name_b;
5adb4839 8963
777dc784
PM
8964 name_a = object_class_get_name(class_a);
8965 name_b = object_class_get_name(class_b);
51492fd1 8966 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8967 return 1;
51492fd1 8968 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8969 return -1;
8970 } else {
8971 return strcmp(name_a, name_b);
5adb4839
PB
8972 }
8973}
8974
777dc784 8975static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8976{
777dc784 8977 ObjectClass *oc = data;
977c33ba 8978 CPUClass *cc = CPU_CLASS(oc);
51492fd1
AF
8979 const char *typename;
8980 char *name;
3371d272 8981
51492fd1
AF
8982 typename = object_class_get_name(oc);
8983 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
977c33ba
DB
8984 if (cc->deprecation_note) {
8985 qemu_printf(" %s (deprecated)\n", name);
8986 } else {
8987 qemu_printf(" %s\n", name);
8988 }
51492fd1 8989 g_free(name);
777dc784
PM
8990}
8991
0442428a 8992void arm_cpu_list(void)
777dc784 8993{
777dc784
PM
8994 GSList *list;
8995
8996 list = object_class_get_list(TYPE_ARM_CPU, false);
8997 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8998 qemu_printf("Available CPUs:\n");
8999 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 9000 g_slist_free(list);
40f137e1
PB
9001}
9002
78027bb6
CR
9003static void arm_cpu_add_definition(gpointer data, gpointer user_data)
9004{
9005 ObjectClass *oc = data;
9006 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
9007 CpuDefinitionInfo *info;
9008 const char *typename;
9009
9010 typename = object_class_get_name(oc);
9011 info = g_malloc0(sizeof(*info));
9012 info->name = g_strndup(typename,
9013 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 9014 info->q_typename = g_strdup(typename);
78027bb6 9015
54aa3de7 9016 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
9017}
9018
25a9d6ca 9019CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
9020{
9021 CpuDefinitionInfoList *cpu_list = NULL;
9022 GSList *list;
9023
9024 list = object_class_get_list(TYPE_ARM_CPU, false);
9025 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
9026 g_slist_free(list);
9027
9028 return cpu_list;
9029}
9030
1859f8c3
RH
9031/*
9032 * Private utility function for define_one_arm_cp_reg_with_opaque():
9033 * add a single reginfo struct to the hash table.
9034 */
6e6efd61 9035static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
9036 void *opaque, CPState state,
9037 CPSecureState secstate,
9c513e78
AB
9038 int crm, int opc1, int opc2,
9039 const char *name)
6e6efd61 9040{
696ba377 9041 CPUARMState *env = &cpu->env;
5860362d 9042 uint32_t key;
c27f5d3a 9043 ARMCPRegInfo *r2;
4c8c4541
RH
9044 bool is64 = r->type & ARM_CP_64BIT;
9045 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 9046 int cp = r->cp;
c27f5d3a 9047 size_t name_len;
696ba377 9048 bool make_const;
c27f5d3a 9049
cac65299
RH
9050 switch (state) {
9051 case ARM_CP_STATE_AA32:
9052 /* We assume it is a cp15 register if the .cp field is left unset. */
9053 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9054 cp = 15;
9055 }
9056 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9057 break;
9058 case ARM_CP_STATE_AA64:
9059 /*
9060 * To allow abbreviation of ARMCPRegInfo definitions, we treat
9061 * cp == 0 as equivalent to the value for "standard guest-visible
9062 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
9063 * in their AArch64 view (the .cp value may be non-zero for the
9064 * benefit of the AArch32 view).
9065 */
9066 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9067 cp = CP_REG_ARM64_SYSREG_CP;
9068 }
9069 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9070 break;
9071 default:
9072 g_assert_not_reached();
9073 }
9074
dc44545b
RH
9075 /* Overriding of an existing definition must be explicitly requested. */
9076 if (!(r->type & ARM_CP_OVERRIDE)) {
9077 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9078 if (oldreg) {
9079 assert(oldreg->type & ARM_CP_OVERRIDE);
9080 }
9081 }
9082
696ba377
RH
9083 /*
9084 * Eliminate registers that are not present because the EL is missing.
9085 * Doing this here makes it easier to put all registers for a given
9086 * feature into the same ARMCPRegInfo array and define them all at once.
9087 */
9088 make_const = false;
9089 if (arm_feature(env, ARM_FEATURE_EL3)) {
9090 /*
9091 * An EL2 register without EL2 but with EL3 is (usually) RES0.
9092 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9093 */
9094 int min_el = ctz32(r->access) / 2;
9095 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9096 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9097 return;
9098 }
9099 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9100 }
9101 } else {
9102 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9103 ? PL2_RW : PL1_RW);
9104 if ((r->access & max_el) == 0) {
9105 return;
9106 }
9107 }
9108
c27f5d3a
RH
9109 /* Combine cpreg and name into one allocation. */
9110 name_len = strlen(name) + 1;
9111 r2 = g_malloc(sizeof(*r2) + name_len);
9112 *r2 = *r;
9113 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 9114
cc946d96
RH
9115 /*
9116 * Update fields to match the instantiation, overwiting wildcards
9117 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 9118 */
cc946d96
RH
9119 r2->cp = cp;
9120 r2->crm = crm;
9121 r2->opc1 = opc1;
9122 r2->opc2 = opc2;
9123 r2->state = state;
3f3c82a5 9124 r2->secure = secstate;
cc946d96
RH
9125 if (opaque) {
9126 r2->opaque = opaque;
9127 }
3f3c82a5 9128
696ba377
RH
9129 if (make_const) {
9130 /* This should not have been a very special register to begin. */
9131 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9132 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 9133 /*
696ba377
RH
9134 * Set the special function to CONST, retaining the other flags.
9135 * This is important for e.g. ARM_CP_SVE so that we still
9136 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 9137 */
696ba377
RH
9138 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9139 /*
9140 * Usually, these registers become RES0, but there are a few
9141 * special cases like VPIDR_EL2 which have a constant non-zero
9142 * value with writes ignored.
9143 */
9144 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9145 r2->resetvalue = 0;
9146 }
9147 /*
9148 * ARM_CP_CONST has precedence, so removing the callbacks and
9149 * offsets are not strictly necessary, but it is potentially
9150 * less confusing to debug later.
9151 */
9152 r2->readfn = NULL;
9153 r2->writefn = NULL;
9154 r2->raw_readfn = NULL;
9155 r2->raw_writefn = NULL;
9156 r2->resetfn = NULL;
9157 r2->fieldoffset = 0;
9158 r2->bank_fieldoffsets[0] = 0;
9159 r2->bank_fieldoffsets[1] = 0;
9160 } else {
9161 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 9162
10748a96 9163 if (isbanked) {
1859f8c3 9164 /*
696ba377
RH
9165 * Register is banked (using both entries in array).
9166 * Overwriting fieldoffset as the array is only used to define
9167 * banked registers but later only fieldoffset is used.
3f3c82a5 9168 */
696ba377
RH
9169 r2->fieldoffset = r->bank_fieldoffsets[ns];
9170 }
9171 if (state == ARM_CP_STATE_AA32) {
9172 if (isbanked) {
9173 /*
9174 * If the register is banked then we don't need to migrate or
9175 * reset the 32-bit instance in certain cases:
9176 *
9177 * 1) If the register has both 32-bit and 64-bit instances
9178 * then we can count on the 64-bit instance taking care
9179 * of the non-secure bank.
9180 * 2) If ARMv8 is enabled then we can count on a 64-bit
9181 * version taking care of the secure bank. This requires
9182 * that separate 32 and 64-bit definitions are provided.
9183 */
9184 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9185 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9186 r2->type |= ARM_CP_ALIAS;
9187 }
9188 } else if ((secstate != r->secure) && !ns) {
9189 /*
9190 * The register is not banked so we only want to allow
9191 * migration of the non-secure instance.
9192 */
7a0e58fa 9193 r2->type |= ARM_CP_ALIAS;
3f3c82a5 9194 }
3f3c82a5 9195
696ba377
RH
9196 if (HOST_BIG_ENDIAN &&
9197 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9198 r2->fieldoffset += sizeof(uint32_t);
9199 }
3f3c82a5 9200 }
f5a0a5a5 9201 }
cc946d96 9202
1859f8c3
RH
9203 /*
9204 * By convention, for wildcarded registers only the first
6e6efd61 9205 * entry is used for migration; the others are marked as
7a0e58fa 9206 * ALIAS so we don't try to transfer the register
6e6efd61 9207 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 9208 * never migratable and not even raw-accessible.
6e6efd61 9209 */
696ba377 9210 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
9211 r2->type |= ARM_CP_NO_RAW;
9212 }
9213 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
9214 ((r->opc1 == CP_ANY) && opc1 != 0) ||
9215 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 9216 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
9217 }
9218
1859f8c3
RH
9219 /*
9220 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
9221 * we can't assert this earlier because the setup of fieldoffset for
9222 * banked registers has to be done first.
9223 */
9224 if (!(r2->type & ARM_CP_NO_RAW)) {
9225 assert(!raw_accessors_invalid(r2));
9226 }
9227
5860362d 9228 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
9229}
9230
9231
4b6a83fb
PM
9232void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9233 const ARMCPRegInfo *r, void *opaque)
9234{
9b37a28c
FR
9235 /*
9236 * Define implementations of coprocessor registers.
4b6a83fb
PM
9237 * We store these in a hashtable because typically
9238 * there are less than 150 registers in a space which
9239 * is 16*16*16*8*8 = 262144 in size.
9240 * Wildcarding is supported for the crm, opc1 and opc2 fields.
9241 * If a register is defined twice then the second definition is
9242 * used, so this can be used to define some generic registers and
9243 * then override them with implementation specific variations.
9244 * At least one of the original and the second definition should
9245 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9246 * against accidental use.
f5a0a5a5
PM
9247 *
9248 * The state field defines whether the register is to be
9249 * visible in the AArch32 or AArch64 execution state. If the
9250 * state is set to ARM_CP_STATE_BOTH then we synthesise a
9251 * reginfo structure for the AArch32 view, which sees the lower
9252 * 32 bits of the 64 bit register.
9253 *
9254 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9255 * be wildcarded. AArch64 registers are always considered to be 64
9256 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9257 * the register, if any.
4b6a83fb 9258 */
d95101d6 9259 int crm, opc1, opc2;
4b6a83fb
PM
9260 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9261 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9262 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9263 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9264 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9265 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
9266 CPState state;
9267
4b6a83fb
PM
9268 /* 64 bit registers have only CRm and Opc1 fields */
9269 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
9270 /* op0 only exists in the AArch64 encodings */
9271 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9272 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9273 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
9274 /*
9275 * This API is only for Arm's system coprocessors (14 and 15) or
9276 * (M-profile or v7A-and-earlier only) for implementation defined
9277 * coprocessors in the range 0..7. Our decode assumes this, since
9278 * 8..13 can be used for other insns including VFP and Neon. See
9279 * valid_cp() in translate.c. Assert here that we haven't tried
9280 * to use an invalid coprocessor number.
9281 */
9282 switch (r->state) {
9283 case ARM_CP_STATE_BOTH:
9284 /* 0 has a special meaning, but otherwise the same rules as AA32. */
9285 if (r->cp == 0) {
9286 break;
9287 }
9288 /* fall through */
9289 case ARM_CP_STATE_AA32:
9290 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9291 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9292 assert(r->cp >= 14 && r->cp <= 15);
9293 } else {
9294 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9295 }
9296 break;
9297 case ARM_CP_STATE_AA64:
9298 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9299 break;
9300 default:
9301 g_assert_not_reached();
9302 }
9b37a28c
FR
9303 /*
9304 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
f5a0a5a5
PM
9305 * encodes a minimum access level for the register. We roll this
9306 * runtime check into our general permission check code, so check
9307 * here that the reginfo's specified permissions are strict enough
9308 * to encompass the generic architectural permission check.
9309 */
9310 if (r->state != ARM_CP_STATE_AA32) {
39107337 9311 CPAccessRights mask;
f5a0a5a5 9312 switch (r->opc1) {
b5bd7440
AB
9313 case 0:
9314 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9315 mask = PL0U_R | PL1_RW;
9316 break;
9317 case 1: case 2:
f5a0a5a5
PM
9318 /* min_EL EL1 */
9319 mask = PL1_RW;
9320 break;
9321 case 3:
9322 /* min_EL EL0 */
9323 mask = PL0_RW;
9324 break;
9325 case 4:
b4ecf60f 9326 case 5:
f5a0a5a5
PM
9327 /* min_EL EL2 */
9328 mask = PL2_RW;
9329 break;
f5a0a5a5
PM
9330 case 6:
9331 /* min_EL EL3 */
9332 mask = PL3_RW;
9333 break;
9334 case 7:
9335 /* min_EL EL1, secure mode only (we don't check the latter) */
9336 mask = PL1_RW;
9337 break;
9338 default:
9339 /* broken reginfo with out-of-range opc1 */
d385a605 9340 g_assert_not_reached();
f5a0a5a5
PM
9341 }
9342 /* assert our permissions are not too lax (stricter is fine) */
9343 assert((r->access & ~mask) == 0);
9344 }
9345
9b37a28c
FR
9346 /*
9347 * Check that the register definition has enough info to handle
4b6a83fb
PM
9348 * reads and writes if they are permitted.
9349 */
87c3f0f2 9350 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 9351 if (r->access & PL3_R) {
3f3c82a5
FA
9352 assert((r->fieldoffset ||
9353 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9354 r->readfn);
4b6a83fb
PM
9355 }
9356 if (r->access & PL3_W) {
3f3c82a5
FA
9357 assert((r->fieldoffset ||
9358 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9359 r->writefn);
4b6a83fb
PM
9360 }
9361 }
5809ac57 9362
4b6a83fb
PM
9363 for (crm = crmmin; crm <= crmmax; crm++) {
9364 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9365 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
9366 for (state = ARM_CP_STATE_AA32;
9367 state <= ARM_CP_STATE_AA64; state++) {
9368 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9369 continue;
9370 }
3f3c82a5 9371 if (state == ARM_CP_STATE_AA32) {
9b37a28c
FR
9372 /*
9373 * Under AArch32 CP registers can be common
3f3c82a5
FA
9374 * (same for secure and non-secure world) or banked.
9375 */
9c513e78
AB
9376 char *name;
9377
3f3c82a5
FA
9378 switch (r->secure) {
9379 case ARM_CP_SECSTATE_S:
9380 case ARM_CP_SECSTATE_NS:
9381 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
9382 r->secure, crm, opc1, opc2,
9383 r->name);
3f3c82a5 9384 break;
cbe64585 9385 case ARM_CP_SECSTATE_BOTH:
9c513e78 9386 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
9387 add_cpreg_to_hashtable(cpu, r, opaque, state,
9388 ARM_CP_SECSTATE_S,
9c513e78
AB
9389 crm, opc1, opc2, name);
9390 g_free(name);
3f3c82a5
FA
9391 add_cpreg_to_hashtable(cpu, r, opaque, state,
9392 ARM_CP_SECSTATE_NS,
9c513e78 9393 crm, opc1, opc2, r->name);
3f3c82a5 9394 break;
cbe64585
RH
9395 default:
9396 g_assert_not_reached();
3f3c82a5
FA
9397 }
9398 } else {
9b37a28c
FR
9399 /*
9400 * AArch64 registers get mapped to non-secure instance
9401 * of AArch32
9402 */
3f3c82a5
FA
9403 add_cpreg_to_hashtable(cpu, r, opaque, state,
9404 ARM_CP_SECSTATE_NS,
9c513e78 9405 crm, opc1, opc2, r->name);
3f3c82a5 9406 }
f5a0a5a5 9407 }
4b6a83fb
PM
9408 }
9409 }
9410 }
9411}
9412
5809ac57
RH
9413/* Define a whole list of registers */
9414void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9415 void *opaque, size_t len)
4b6a83fb 9416{
5809ac57
RH
9417 size_t i;
9418 for (i = 0; i < len; ++i) {
9419 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
9420 }
9421}
9422
6c5c0fec
AB
9423/*
9424 * Modify ARMCPRegInfo for access from userspace.
9425 *
9426 * This is a data driven modification directed by
9427 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9428 * user-space cannot alter any values and dynamic values pertaining to
9429 * execution state are hidden from user space view anyway.
9430 */
5809ac57
RH
9431void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9432 const ARMCPRegUserSpaceInfo *mods,
9433 size_t mods_len)
6c5c0fec 9434{
5809ac57
RH
9435 for (size_t mi = 0; mi < mods_len; ++mi) {
9436 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 9437 GPatternSpec *pat = NULL;
5809ac57 9438
d040242e
AB
9439 if (m->is_glob) {
9440 pat = g_pattern_spec_new(m->name);
9441 }
5809ac57
RH
9442 for (size_t ri = 0; ri < regs_len; ++ri) {
9443 ARMCPRegInfo *r = regs + ri;
9444
d040242e
AB
9445 if (pat && g_pattern_match_string(pat, r->name)) {
9446 r->type = ARM_CP_CONST;
9447 r->access = PL0U_R;
9448 r->resetvalue = 0;
9449 /* continue */
9450 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
9451 r->type = ARM_CP_CONST;
9452 r->access = PL0U_R;
9453 r->resetvalue &= m->exported_bits;
9454 r->resetvalue |= m->fixed_bits;
9455 break;
9456 }
9457 }
d040242e
AB
9458 if (pat) {
9459 g_pattern_spec_free(pat);
9460 }
6c5c0fec
AB
9461 }
9462}
9463
60322b39 9464const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 9465{
5860362d 9466 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
9467}
9468
c4241c7d
PM
9469void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9470 uint64_t value)
4b6a83fb
PM
9471{
9472 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
9473}
9474
c4241c7d 9475uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
9476{
9477 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
9478 return 0;
9479}
9480
f5a0a5a5
PM
9481void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9482{
9483 /* Helper coprocessor reset function for do-nothing-on-reset registers */
9484}
9485
af393ffc 9486static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b 9487{
9b37a28c
FR
9488 /*
9489 * Return true if it is not valid for us to switch to
37064a8b
PM
9490 * this CPU mode (ie all the UNPREDICTABLE cases in
9491 * the ARM ARM CPSRWriteByInstr pseudocode).
9492 */
af393ffc
PM
9493
9494 /* Changes to or from Hyp via MSR and CPS are illegal. */
9495 if (write_type == CPSRWriteByInstr &&
9496 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9497 mode == ARM_CPU_MODE_HYP)) {
9498 return 1;
9499 }
9500
37064a8b
PM
9501 switch (mode) {
9502 case ARM_CPU_MODE_USR:
10eacda7 9503 return 0;
37064a8b
PM
9504 case ARM_CPU_MODE_SYS:
9505 case ARM_CPU_MODE_SVC:
9506 case ARM_CPU_MODE_ABT:
9507 case ARM_CPU_MODE_UND:
9508 case ARM_CPU_MODE_IRQ:
9509 case ARM_CPU_MODE_FIQ:
9b37a28c
FR
9510 /*
9511 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
52ff951b
PM
9512 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9513 */
9b37a28c
FR
9514 /*
9515 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10eacda7
PM
9516 * and CPS are treated as illegal mode changes.
9517 */
9518 if (write_type == CPSRWriteByInstr &&
10eacda7 9519 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 9520 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
9521 return 1;
9522 }
37064a8b 9523 return 0;
e6c8fc07 9524 case ARM_CPU_MODE_HYP:
e6ef0169 9525 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 9526 case ARM_CPU_MODE_MON:
58ae2d1f 9527 return arm_current_el(env) < 3;
37064a8b
PM
9528 default:
9529 return 1;
9530 }
9531}
9532
2f4a40e5
AZ
9533uint32_t cpsr_read(CPUARMState *env)
9534{
9535 int ZF;
6fbe23d5
PB
9536 ZF = (env->ZF == 0);
9537 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
9538 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9539 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9540 | ((env->condexec_bits & 0xfc) << 8)
af519934 9541 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
9542}
9543
50866ba5
PM
9544void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9545 CPSRWriteType write_type)
2f4a40e5 9546{
6e8801f9 9547 uint32_t changed_daif;
e784807c
PM
9548 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9549 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 9550
2f4a40e5 9551 if (mask & CPSR_NZCV) {
6fbe23d5
PB
9552 env->ZF = (~val) & CPSR_Z;
9553 env->NF = val;
2f4a40e5
AZ
9554 env->CF = (val >> 29) & 1;
9555 env->VF = (val << 3) & 0x80000000;
9556 }
f927dbda 9557 if (mask & CPSR_Q) {
2f4a40e5 9558 env->QF = ((val & CPSR_Q) != 0);
f927dbda
FR
9559 }
9560 if (mask & CPSR_T) {
2f4a40e5 9561 env->thumb = ((val & CPSR_T) != 0);
f927dbda 9562 }
2f4a40e5
AZ
9563 if (mask & CPSR_IT_0_1) {
9564 env->condexec_bits &= ~3;
9565 env->condexec_bits |= (val >> 25) & 3;
9566 }
9567 if (mask & CPSR_IT_2_7) {
9568 env->condexec_bits &= 3;
9569 env->condexec_bits |= (val >> 8) & 0xfc;
9570 }
9571 if (mask & CPSR_GE) {
9572 env->GE = (val >> 16) & 0xf;
9573 }
9574
9b37a28c
FR
9575 /*
9576 * In a V7 implementation that includes the security extensions but does
6e8801f9
FA
9577 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9578 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9579 * bits respectively.
9580 *
9581 * In a V8 implementation, it is permitted for privileged software to
9582 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9583 */
f8c88bbc 9584 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
9585 arm_feature(env, ARM_FEATURE_EL3) &&
9586 !arm_feature(env, ARM_FEATURE_EL2) &&
9587 !arm_is_secure(env)) {
9588
9589 changed_daif = (env->daif ^ val) & mask;
9590
9591 if (changed_daif & CPSR_A) {
9b37a28c
FR
9592 /*
9593 * Check to see if we are allowed to change the masking of async
6e8801f9
FA
9594 * abort exceptions from a non-secure state.
9595 */
9596 if (!(env->cp15.scr_el3 & SCR_AW)) {
9597 qemu_log_mask(LOG_GUEST_ERROR,
9598 "Ignoring attempt to switch CPSR_A flag from "
9599 "non-secure world with SCR.AW bit clear\n");
9600 mask &= ~CPSR_A;
9601 }
9602 }
9603
9604 if (changed_daif & CPSR_F) {
9b37a28c
FR
9605 /*
9606 * Check to see if we are allowed to change the masking of FIQ
6e8801f9
FA
9607 * exceptions from a non-secure state.
9608 */
9609 if (!(env->cp15.scr_el3 & SCR_FW)) {
9610 qemu_log_mask(LOG_GUEST_ERROR,
9611 "Ignoring attempt to switch CPSR_F flag from "
9612 "non-secure world with SCR.FW bit clear\n");
9613 mask &= ~CPSR_F;
9614 }
9615
9b37a28c
FR
9616 /*
9617 * Check whether non-maskable FIQ (NMFI) support is enabled.
6e8801f9
FA
9618 * If this bit is set software is not allowed to mask
9619 * FIQs, but is allowed to set CPSR_F to 0.
9620 */
9621 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9622 (val & CPSR_F)) {
9623 qemu_log_mask(LOG_GUEST_ERROR,
9624 "Ignoring attempt to enable CPSR_F flag "
9625 "(non-maskable FIQ [NMFI] support enabled)\n");
9626 mask &= ~CPSR_F;
9627 }
9628 }
9629 }
9630
4cc35614
PM
9631 env->daif &= ~(CPSR_AIF & mask);
9632 env->daif |= val & CPSR_AIF & mask;
9633
f8c88bbc
PM
9634 if (write_type != CPSRWriteRaw &&
9635 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9 9636 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9b37a28c
FR
9637 /*
9638 * Note that we can only get here in USR mode if this is a
8c4f0eb9
PM
9639 * gdb stub write; for this case we follow the architectural
9640 * behaviour for guest writes in USR mode of ignoring an attempt
9641 * to switch mode. (Those are caught by translate.c for writes
9642 * triggered by guest instructions.)
9643 */
9644 mask &= ~CPSR_M;
9645 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9b37a28c
FR
9646 /*
9647 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
81907a58
PM
9648 * v7, and has defined behaviour in v8:
9649 * + leave CPSR.M untouched
9650 * + allow changes to the other CPSR fields
9651 * + set PSTATE.IL
9652 * For user changes via the GDB stub, we don't set PSTATE.IL,
9653 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9654 */
9655 mask &= ~CPSR_M;
81907a58
PM
9656 if (write_type != CPSRWriteByGDBStub &&
9657 arm_feature(env, ARM_FEATURE_V8)) {
9658 mask |= CPSR_IL;
9659 val |= CPSR_IL;
9660 }
81e37284
PM
9661 qemu_log_mask(LOG_GUEST_ERROR,
9662 "Illegal AArch32 mode switch attempt from %s to %s\n",
9663 aarch32_mode_name(env->uncached_cpsr),
9664 aarch32_mode_name(val));
37064a8b 9665 } else {
81e37284
PM
9666 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9667 write_type == CPSRWriteExceptionReturn ?
9668 "Exception return from AArch32" :
9669 "AArch32 mode switch from",
9670 aarch32_mode_name(env->uncached_cpsr),
9671 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9672 switch_mode(env, val & CPSR_M);
9673 }
2f4a40e5
AZ
9674 }
9675 mask &= ~CACHED_CPSR_BITS;
9676 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
9677 if (rebuild_hflags) {
9678 arm_rebuild_hflags(env);
9679 }
2f4a40e5
AZ
9680}
9681
b26eefb6
PB
9682/* Sign/zero extend */
9683uint32_t HELPER(sxtb16)(uint32_t x)
9684{
9685 uint32_t res;
9686 res = (uint16_t)(int8_t)x;
9687 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9688 return res;
9689}
9690
e5346292
PM
9691static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9692{
9693 /*
9694 * Take a division-by-zero exception if necessary; otherwise return
9695 * to get the usual non-trapping division behaviour (result of 0)
9696 */
9697 if (arm_feature(env, ARM_FEATURE_M)
9698 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9699 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9700 }
9701}
9702
b26eefb6
PB
9703uint32_t HELPER(uxtb16)(uint32_t x)
9704{
9705 uint32_t res;
9706 res = (uint16_t)(uint8_t)x;
9707 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9708 return res;
9709}
9710
e5346292 9711int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9712{
fc7a5038 9713 if (den == 0) {
e5346292 9714 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9715 return 0;
9716 }
9717 if (num == INT_MIN && den == -1) {
9718 return INT_MIN;
9719 }
3670669c
PB
9720 return num / den;
9721}
9722
e5346292 9723uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9724{
fc7a5038 9725 if (den == 0) {
e5346292 9726 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9727 return 0;
9728 }
3670669c
PB
9729 return num / den;
9730}
9731
9732uint32_t HELPER(rbit)(uint32_t x)
9733{
42fedbca 9734 return revbit32(x);
3670669c
PB
9735}
9736
c47eaf9f 9737#ifdef CONFIG_USER_ONLY
b5ff1b31 9738
affdb64d 9739static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9740{
2fc0cc0e 9741 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9742
9743 if (mode != ARM_CPU_MODE_USR) {
9744 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9745 }
b5ff1b31
FB
9746}
9747
012a906b
GB
9748uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9749 uint32_t cur_el, bool secure)
9e729b57
EI
9750{
9751 return 1;
9752}
9753
ce02049d
GB
9754void aarch64_sync_64_to_32(CPUARMState *env)
9755{
9756 g_assert_not_reached();
9757}
9758
b5ff1b31
FB
9759#else
9760
affdb64d 9761static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9762{
9763 int old_mode;
9764 int i;
9765
9766 old_mode = env->uncached_cpsr & CPSR_M;
f927dbda 9767 if (mode == old_mode) {
b5ff1b31 9768 return;
f927dbda 9769 }
b5ff1b31
FB
9770
9771 if (old_mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9772 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9773 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31 9774 } else if (mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9775 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9776 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9777 }
9778
f5206413 9779 i = bank_number(old_mode);
b5ff1b31 9780 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9781 env->banked_spsr[i] = env->spsr;
9782
f5206413 9783 i = bank_number(mode);
b5ff1b31 9784 env->regs[13] = env->banked_r13[i];
b5ff1b31 9785 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9786
9787 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9788 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9789}
9790
9b37a28c
FR
9791/*
9792 * Physical Interrupt Target EL Lookup Table
0eeb17d6
GB
9793 *
9794 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9795 *
9796 * The below multi-dimensional table is used for looking up the target
9797 * exception level given numerous condition criteria. Specifically, the
9798 * target EL is based on SCR and HCR routing controls as well as the
9799 * currently executing EL and secure state.
9800 *
9801 * Dimensions:
9802 * target_el_table[2][2][2][2][2][4]
9803 * | | | | | +--- Current EL
9804 * | | | | +------ Non-secure(0)/Secure(1)
9805 * | | | +--------- HCR mask override
9806 * | | +------------ SCR exec state control
9807 * | +--------------- SCR mask override
9808 * +------------------ 32-bit(0)/64-bit(1) EL3
9809 *
9810 * The table values are as such:
9811 * 0-3 = EL0-EL3
9812 * -1 = Cannot occur
9813 *
9814 * The ARM ARM target EL table includes entries indicating that an "exception
9815 * is not taken". The two cases where this is applicable are:
9816 * 1) An exception is taken from EL3 but the SCR does not have the exception
9817 * routed to EL3.
9818 * 2) An exception is taken from EL2 but the HCR does not have the exception
9819 * routed to EL2.
9820 * In these two cases, the below table contain a target of EL1. This value is
9821 * returned as it is expected that the consumer of the table data will check
9822 * for "target EL >= current EL" to ensure the exception is not taken.
9823 *
9824 * SCR HCR
9825 * 64 EA AMO From
9826 * BIT IRQ IMO Non-secure Secure
9827 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9828 */
82c39f6a 9829static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9830 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9831 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9832 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9833 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9834 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9835 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9836 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9837 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9838 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9839 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9840 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9841 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9842 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9843 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9844 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9845 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9846};
9847
9848/*
9849 * Determine the target EL for physical exceptions
9850 */
012a906b
GB
9851uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9852 uint32_t cur_el, bool secure)
0eeb17d6
GB
9853{
9854 CPUARMState *env = cs->env_ptr;
f7778444
RH
9855 bool rw;
9856 bool scr;
9857 bool hcr;
0eeb17d6 9858 int target_el;
2cde031f 9859 /* Is the highest EL AArch64? */
f7778444
RH
9860 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9861 uint64_t hcr_el2;
2cde031f
SS
9862
9863 if (arm_feature(env, ARM_FEATURE_EL3)) {
9864 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9865 } else {
9b37a28c
FR
9866 /*
9867 * Either EL2 is the highest EL (and so the EL2 register width
2cde031f
SS
9868 * is given by is64); or there is no EL2 or EL3, in which case
9869 * the value of 'rw' does not affect the table lookup anyway.
9870 */
9871 rw = is64;
9872 }
0eeb17d6 9873
f7778444 9874 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9875 switch (excp_idx) {
9876 case EXCP_IRQ:
9877 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9878 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9879 break;
9880 case EXCP_FIQ:
9881 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9882 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9883 break;
9884 default:
9885 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9886 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9887 break;
9888 };
9889
d1b31428
RH
9890 /*
9891 * For these purposes, TGE and AMO/IMO/FMO both force the
9892 * interrupt to EL2. Fold TGE into the bit extracted above.
9893 */
9894 hcr |= (hcr_el2 & HCR_TGE) != 0;
9895
0eeb17d6
GB
9896 /* Perform a table-lookup for the target EL given the current state */
9897 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9898
9899 assert(target_el > 0);
9900
9901 return target_el;
9902}
9903
fc6177af 9904void arm_log_exception(CPUState *cs)
b59f479b 9905{
fc6177af
PM
9906 int idx = cs->exception_index;
9907
b59f479b
PMD
9908 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9909 const char *exc = NULL;
9910 static const char * const excnames[] = {
9911 [EXCP_UDEF] = "Undefined Instruction",
9912 [EXCP_SWI] = "SVC",
9913 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9914 [EXCP_DATA_ABORT] = "Data Abort",
9915 [EXCP_IRQ] = "IRQ",
9916 [EXCP_FIQ] = "FIQ",
9917 [EXCP_BKPT] = "Breakpoint",
9918 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9919 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9920 [EXCP_HVC] = "Hypervisor Call",
9921 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9922 [EXCP_SMC] = "Secure Monitor Call",
9923 [EXCP_VIRQ] = "Virtual IRQ",
9924 [EXCP_VFIQ] = "Virtual FIQ",
9925 [EXCP_SEMIHOST] = "Semihosting call",
9926 [EXCP_NOCP] = "v7M NOCP UsageFault",
9927 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9928 [EXCP_STKOF] = "v8M STKOF UsageFault",
9929 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9930 [EXCP_LSERR] = "v8M LSERR UsageFault",
9931 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 9932 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
3c29632f 9933 [EXCP_VSERR] = "Virtual SERR",
b59f479b
PMD
9934 };
9935
9936 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9937 exc = excnames[idx];
9938 }
9939 if (!exc) {
9940 exc = "unknown";
9941 }
fc6177af
PM
9942 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9943 idx, exc, cs->cpu_index);
b59f479b
PMD
9944 }
9945}
9946
a356dacf 9947/*
7aab5a8c
PMD
9948 * Function used to synchronize QEMU's AArch64 register set with AArch32
9949 * register set. This is necessary when switching between AArch32 and AArch64
9950 * execution state.
a356dacf 9951 */
7aab5a8c 9952void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9953{
7aab5a8c
PMD
9954 int i;
9955 uint32_t mode = env->uncached_cpsr & CPSR_M;
9956
9957 /* We can blanket copy R[0:7] to X[0:7] */
9958 for (i = 0; i < 8; i++) {
9959 env->xregs[i] = env->regs[i];
fd592d89 9960 }
70d74660 9961
9a223097 9962 /*
7aab5a8c
PMD
9963 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9964 * Otherwise, they come from the banked user regs.
fd592d89 9965 */
7aab5a8c
PMD
9966 if (mode == ARM_CPU_MODE_FIQ) {
9967 for (i = 8; i < 13; i++) {
9968 env->xregs[i] = env->usr_regs[i - 8];
9969 }
9970 } else {
9971 for (i = 8; i < 13; i++) {
9972 env->xregs[i] = env->regs[i];
9973 }
fd592d89 9974 }
9ee6e8bb 9975
7aab5a8c
PMD
9976 /*
9977 * Registers x13-x23 are the various mode SP and FP registers. Registers
9978 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9979 * from the mode banked register.
9980 */
9981 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9982 env->xregs[13] = env->regs[13];
9983 env->xregs[14] = env->regs[14];
9984 } else {
9985 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9986 /* HYP is an exception in that it is copied from r14 */
9987 if (mode == ARM_CPU_MODE_HYP) {
9988 env->xregs[14] = env->regs[14];
95695eff 9989 } else {
7aab5a8c 9990 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9991 }
95695eff
PM
9992 }
9993
7aab5a8c
PMD
9994 if (mode == ARM_CPU_MODE_HYP) {
9995 env->xregs[15] = env->regs[13];
9996 } else {
9997 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9998 }
9999
7aab5a8c
PMD
10000 if (mode == ARM_CPU_MODE_IRQ) {
10001 env->xregs[16] = env->regs[14];
10002 env->xregs[17] = env->regs[13];
10003 } else {
10004 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10005 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10006 }
95695eff 10007
7aab5a8c
PMD
10008 if (mode == ARM_CPU_MODE_SVC) {
10009 env->xregs[18] = env->regs[14];
10010 env->xregs[19] = env->regs[13];
10011 } else {
10012 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10013 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10014 }
95695eff 10015
7aab5a8c
PMD
10016 if (mode == ARM_CPU_MODE_ABT) {
10017 env->xregs[20] = env->regs[14];
10018 env->xregs[21] = env->regs[13];
10019 } else {
10020 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10021 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10022 }
e33cf0f8 10023
7aab5a8c
PMD
10024 if (mode == ARM_CPU_MODE_UND) {
10025 env->xregs[22] = env->regs[14];
10026 env->xregs[23] = env->regs[13];
10027 } else {
10028 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10029 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
10030 }
10031
10032 /*
7aab5a8c
PMD
10033 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10034 * mode, then we can copy from r8-r14. Otherwise, we copy from the
10035 * FIQ bank for r8-r14.
e33cf0f8 10036 */
7aab5a8c
PMD
10037 if (mode == ARM_CPU_MODE_FIQ) {
10038 for (i = 24; i < 31; i++) {
10039 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10040 }
10041 } else {
10042 for (i = 24; i < 29; i++) {
10043 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 10044 }
7aab5a8c
PMD
10045 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10046 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 10047 }
7aab5a8c
PMD
10048
10049 env->pc = env->regs[15];
e33cf0f8
PM
10050}
10051
9a223097 10052/*
7aab5a8c
PMD
10053 * Function used to synchronize QEMU's AArch32 register set with AArch64
10054 * register set. This is necessary when switching between AArch32 and AArch64
10055 * execution state.
de2db7ec 10056 */
7aab5a8c 10057void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 10058{
7aab5a8c
PMD
10059 int i;
10060 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 10061
7aab5a8c
PMD
10062 /* We can blanket copy X[0:7] to R[0:7] */
10063 for (i = 0; i < 8; i++) {
10064 env->regs[i] = env->xregs[i];
de2db7ec 10065 }
3f0cddee 10066
9a223097 10067 /*
7aab5a8c
PMD
10068 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10069 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 10070 */
7aab5a8c
PMD
10071 if (mode == ARM_CPU_MODE_FIQ) {
10072 for (i = 8; i < 13; i++) {
10073 env->usr_regs[i - 8] = env->xregs[i];
10074 }
10075 } else {
10076 for (i = 8; i < 13; i++) {
10077 env->regs[i] = env->xregs[i];
10078 }
fb602cb7
PM
10079 }
10080
9a223097 10081 /*
7aab5a8c
PMD
10082 * Registers r13 & r14 depend on the current mode.
10083 * If we are in a given mode, we copy the corresponding x registers to r13
10084 * and r14. Otherwise, we copy the x register to the banked r13 and r14
10085 * for the mode.
fb602cb7 10086 */
7aab5a8c
PMD
10087 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10088 env->regs[13] = env->xregs[13];
10089 env->regs[14] = env->xregs[14];
fb602cb7 10090 } else {
7aab5a8c 10091 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 10092
7aab5a8c
PMD
10093 /*
10094 * HYP is an exception in that it does not have its own banked r14 but
10095 * shares the USR r14
10096 */
10097 if (mode == ARM_CPU_MODE_HYP) {
10098 env->regs[14] = env->xregs[14];
10099 } else {
10100 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10101 }
10102 }
fb602cb7 10103
7aab5a8c
PMD
10104 if (mode == ARM_CPU_MODE_HYP) {
10105 env->regs[13] = env->xregs[15];
fb602cb7 10106 } else {
7aab5a8c 10107 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 10108 }
d02a8698 10109
7aab5a8c
PMD
10110 if (mode == ARM_CPU_MODE_IRQ) {
10111 env->regs[14] = env->xregs[16];
10112 env->regs[13] = env->xregs[17];
d02a8698 10113 } else {
7aab5a8c
PMD
10114 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10115 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
10116 }
10117
7aab5a8c
PMD
10118 if (mode == ARM_CPU_MODE_SVC) {
10119 env->regs[14] = env->xregs[18];
10120 env->regs[13] = env->xregs[19];
10121 } else {
10122 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10123 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
10124 }
10125
7aab5a8c
PMD
10126 if (mode == ARM_CPU_MODE_ABT) {
10127 env->regs[14] = env->xregs[20];
10128 env->regs[13] = env->xregs[21];
10129 } else {
10130 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10131 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
10132 }
10133
10134 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
10135 env->regs[14] = env->xregs[22];
10136 env->regs[13] = env->xregs[23];
ce02049d 10137 } else {
593cfa2b 10138 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 10139 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
10140 }
10141
9b37a28c
FR
10142 /*
10143 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
ce02049d
GB
10144 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10145 * FIQ bank for r8-r14.
10146 */
10147 if (mode == ARM_CPU_MODE_FIQ) {
10148 for (i = 24; i < 31; i++) {
10149 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10150 }
10151 } else {
10152 for (i = 24; i < 29; i++) {
10153 env->fiq_regs[i - 24] = env->xregs[i];
10154 }
10155 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 10156 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
10157 }
10158
10159 env->regs[15] = env->pc;
10160}
10161
dea8378b
PM
10162static void take_aarch32_exception(CPUARMState *env, int new_mode,
10163 uint32_t mask, uint32_t offset,
10164 uint32_t newpc)
10165{
4a2696c0
RH
10166 int new_el;
10167
dea8378b
PM
10168 /* Change the CPU state so as to actually take the exception. */
10169 switch_mode(env, new_mode);
4a2696c0 10170
dea8378b
PM
10171 /*
10172 * For exceptions taken to AArch32 we must clear the SS bit in both
10173 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10174 */
f944a854 10175 env->pstate &= ~PSTATE_SS;
dea8378b
PM
10176 env->spsr = cpsr_read(env);
10177 /* Clear IT bits. */
10178 env->condexec_bits = 0;
10179 /* Switch to the new mode, and to the correct instruction set. */
10180 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
10181
10182 /* This must be after mode switching. */
10183 new_el = arm_current_el(env);
10184
dea8378b
PM
10185 /* Set new mode endianness */
10186 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 10187 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
10188 env->uncached_cpsr |= CPSR_E;
10189 }
829f9fd3
PM
10190 /* J and IL must always be cleared for exception entry */
10191 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
10192 env->daif |= mask;
10193
f2f68a78
RC
10194 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10195 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10196 env->uncached_cpsr |= CPSR_SSBS;
10197 } else {
10198 env->uncached_cpsr &= ~CPSR_SSBS;
10199 }
10200 }
10201
dea8378b
PM
10202 if (new_mode == ARM_CPU_MODE_HYP) {
10203 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10204 env->elr_el[2] = env->regs[15];
10205 } else {
4a2696c0 10206 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 10207 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
10208 switch (new_el) {
10209 case 3:
10210 if (!arm_is_secure_below_el3(env)) {
10211 /* ... the target is EL3, from non-secure state. */
10212 env->uncached_cpsr &= ~CPSR_PAN;
10213 break;
10214 }
10215 /* ... the target is EL3, from secure state ... */
10216 /* fall through */
10217 case 1:
10218 /* ... the target is EL1 and SCTLR.SPAN is 0. */
10219 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10220 env->uncached_cpsr |= CPSR_PAN;
10221 }
10222 break;
10223 }
10224 }
dea8378b
PM
10225 /*
10226 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10227 * and we should just guard the thumb mode on V4
10228 */
10229 if (arm_feature(env, ARM_FEATURE_V4T)) {
10230 env->thumb =
10231 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10232 }
10233 env->regs[14] = env->regs[15] + offset;
10234 }
10235 env->regs[15] = newpc;
a8a79c7a 10236 arm_rebuild_hflags(env);
dea8378b
PM
10237}
10238
b9bc21ff
PM
10239static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10240{
10241 /*
10242 * Handle exception entry to Hyp mode; this is sufficiently
10243 * different to entry to other AArch32 modes that we handle it
10244 * separately here.
10245 *
10246 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 10247 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
10248 * The offset applied to the preferred return address is always zero
10249 * (see DDI0487C.a section G1.12.3).
10250 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10251 */
10252 uint32_t addr, mask;
10253 ARMCPU *cpu = ARM_CPU(cs);
10254 CPUARMState *env = &cpu->env;
10255
10256 switch (cs->exception_index) {
10257 case EXCP_UDEF:
10258 addr = 0x04;
10259 break;
10260 case EXCP_SWI:
2c023d36 10261 addr = 0x08;
b9bc21ff
PM
10262 break;
10263 case EXCP_BKPT:
10264 /* Fall through to prefetch abort. */
10265 case EXCP_PREFETCH_ABORT:
10266 env->cp15.ifar_s = env->exception.vaddress;
10267 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10268 (uint32_t)env->exception.vaddress);
10269 addr = 0x0c;
10270 break;
10271 case EXCP_DATA_ABORT:
10272 env->cp15.dfar_s = env->exception.vaddress;
10273 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10274 (uint32_t)env->exception.vaddress);
10275 addr = 0x10;
10276 break;
10277 case EXCP_IRQ:
10278 addr = 0x18;
10279 break;
10280 case EXCP_FIQ:
10281 addr = 0x1c;
10282 break;
10283 case EXCP_HVC:
10284 addr = 0x08;
10285 break;
10286 case EXCP_HYP_TRAP:
10287 addr = 0x14;
9bbb4ef9 10288 break;
b9bc21ff
PM
10289 default:
10290 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10291 }
10292
10293 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
10294 if (!arm_feature(env, ARM_FEATURE_V8)) {
10295 /*
10296 * QEMU syndrome values are v8-style. v7 has the IL bit
10297 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10298 * If this is a v7 CPU, squash the IL bit in those cases.
10299 */
10300 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10301 (cs->exception_index == EXCP_DATA_ABORT &&
10302 !(env->exception.syndrome & ARM_EL_ISV)) ||
10303 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10304 env->exception.syndrome &= ~ARM_EL_IL;
10305 }
10306 }
b9bc21ff
PM
10307 env->cp15.esr_el[2] = env->exception.syndrome;
10308 }
10309
10310 if (arm_current_el(env) != 2 && addr < 0x14) {
10311 addr = 0x14;
10312 }
10313
10314 mask = 0;
10315 if (!(env->cp15.scr_el3 & SCR_EA)) {
10316 mask |= CPSR_A;
10317 }
10318 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10319 mask |= CPSR_I;
10320 }
10321 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10322 mask |= CPSR_F;
10323 }
10324
10325 addr += env->cp15.hvbar;
10326
10327 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10328}
10329
966f758c 10330static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 10331{
97a8ea5a
AF
10332 ARMCPU *cpu = ARM_CPU(cs);
10333 CPUARMState *env = &cpu->env;
b5ff1b31
FB
10334 uint32_t addr;
10335 uint32_t mask;
10336 int new_mode;
10337 uint32_t offset;
16a906fd 10338 uint32_t moe;
b5ff1b31 10339
16a906fd 10340 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 10341 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
10342 case EC_BREAKPOINT:
10343 case EC_BREAKPOINT_SAME_EL:
10344 moe = 1;
10345 break;
10346 case EC_WATCHPOINT:
10347 case EC_WATCHPOINT_SAME_EL:
10348 moe = 10;
10349 break;
10350 case EC_AA32_BKPT:
10351 moe = 3;
10352 break;
10353 case EC_VECTORCATCH:
10354 moe = 5;
10355 break;
10356 default:
10357 moe = 0;
10358 break;
10359 }
10360
10361 if (moe) {
10362 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10363 }
10364
b9bc21ff
PM
10365 if (env->exception.target_el == 2) {
10366 arm_cpu_do_interrupt_aarch32_hyp(cs);
10367 return;
10368 }
10369
27103424 10370 switch (cs->exception_index) {
b5ff1b31
FB
10371 case EXCP_UDEF:
10372 new_mode = ARM_CPU_MODE_UND;
10373 addr = 0x04;
10374 mask = CPSR_I;
f927dbda 10375 if (env->thumb) {
b5ff1b31 10376 offset = 2;
f927dbda 10377 } else {
b5ff1b31 10378 offset = 4;
f927dbda 10379 }
b5ff1b31
FB
10380 break;
10381 case EXCP_SWI:
10382 new_mode = ARM_CPU_MODE_SVC;
10383 addr = 0x08;
10384 mask = CPSR_I;
601d70b9 10385 /* The PC already points to the next instruction. */
b5ff1b31
FB
10386 offset = 0;
10387 break;
06c949e6 10388 case EXCP_BKPT:
9ee6e8bb
PB
10389 /* Fall through to prefetch abort. */
10390 case EXCP_PREFETCH_ABORT:
88ca1c2d 10391 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 10392 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 10393 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 10394 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10395 new_mode = ARM_CPU_MODE_ABT;
10396 addr = 0x0c;
10397 mask = CPSR_A | CPSR_I;
10398 offset = 4;
10399 break;
10400 case EXCP_DATA_ABORT:
4a7e2d73 10401 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 10402 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 10403 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 10404 env->exception.fsr,
6cd8a264 10405 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10406 new_mode = ARM_CPU_MODE_ABT;
10407 addr = 0x10;
10408 mask = CPSR_A | CPSR_I;
10409 offset = 8;
10410 break;
10411 case EXCP_IRQ:
10412 new_mode = ARM_CPU_MODE_IRQ;
10413 addr = 0x18;
10414 /* Disable IRQ and imprecise data aborts. */
10415 mask = CPSR_A | CPSR_I;
10416 offset = 4;
de38d23b
FA
10417 if (env->cp15.scr_el3 & SCR_IRQ) {
10418 /* IRQ routed to monitor mode */
10419 new_mode = ARM_CPU_MODE_MON;
10420 mask |= CPSR_F;
10421 }
b5ff1b31
FB
10422 break;
10423 case EXCP_FIQ:
10424 new_mode = ARM_CPU_MODE_FIQ;
10425 addr = 0x1c;
10426 /* Disable FIQ, IRQ and imprecise data aborts. */
10427 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
10428 if (env->cp15.scr_el3 & SCR_FIQ) {
10429 /* FIQ routed to monitor mode */
10430 new_mode = ARM_CPU_MODE_MON;
10431 }
b5ff1b31
FB
10432 offset = 4;
10433 break;
87a4b270
PM
10434 case EXCP_VIRQ:
10435 new_mode = ARM_CPU_MODE_IRQ;
10436 addr = 0x18;
10437 /* Disable IRQ and imprecise data aborts. */
10438 mask = CPSR_A | CPSR_I;
10439 offset = 4;
10440 break;
10441 case EXCP_VFIQ:
10442 new_mode = ARM_CPU_MODE_FIQ;
10443 addr = 0x1c;
10444 /* Disable FIQ, IRQ and imprecise data aborts. */
10445 mask = CPSR_A | CPSR_I | CPSR_F;
10446 offset = 4;
10447 break;
3c29632f
RH
10448 case EXCP_VSERR:
10449 {
10450 /*
10451 * Note that this is reported as a data abort, but the DFAR
10452 * has an UNKNOWN value. Construct the SError syndrome from
10453 * AET and ExT fields.
10454 */
10455 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10456
10457 if (extended_addresses_enabled(env)) {
10458 env->exception.fsr = arm_fi_to_lfsc(&fi);
10459 } else {
10460 env->exception.fsr = arm_fi_to_sfsc(&fi);
10461 }
10462 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10463 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10464 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10465 env->exception.fsr);
10466
10467 new_mode = ARM_CPU_MODE_ABT;
10468 addr = 0x10;
10469 mask = CPSR_A | CPSR_I;
10470 offset = 8;
10471 }
10472 break;
dbe9d163
FA
10473 case EXCP_SMC:
10474 new_mode = ARM_CPU_MODE_MON;
10475 addr = 0x08;
10476 mask = CPSR_A | CPSR_I | CPSR_F;
10477 offset = 0;
10478 break;
b5ff1b31 10479 default:
a47dddd7 10480 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
10481 return; /* Never happens. Keep compiler happy. */
10482 }
e89e51a1
FA
10483
10484 if (new_mode == ARM_CPU_MODE_MON) {
10485 addr += env->cp15.mvbar;
137feaa9 10486 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 10487 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 10488 addr += 0xffff0000;
8641136c 10489 } else {
9b37a28c
FR
10490 /*
10491 * ARM v7 architectures provide a vector base address register to remap
8641136c 10492 * the interrupt vector table.
e89e51a1 10493 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
10494 * Note: only bits 31:5 are valid.
10495 */
fb6c91ba 10496 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 10497 }
dbe9d163
FA
10498
10499 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10500 env->cp15.scr_el3 &= ~SCR_NS;
10501 }
10502
dea8378b 10503 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
10504}
10505
a65dabf7
PM
10506static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10507{
10508 /*
10509 * Return the register number of the AArch64 view of the AArch32
10510 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10511 * be that of the AArch32 mode the exception came from.
10512 */
10513 int mode = env->uncached_cpsr & CPSR_M;
10514
10515 switch (aarch32_reg) {
10516 case 0 ... 7:
10517 return aarch32_reg;
10518 case 8 ... 12:
10519 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10520 case 13:
10521 switch (mode) {
10522 case ARM_CPU_MODE_USR:
10523 case ARM_CPU_MODE_SYS:
10524 return 13;
10525 case ARM_CPU_MODE_HYP:
10526 return 15;
10527 case ARM_CPU_MODE_IRQ:
10528 return 17;
10529 case ARM_CPU_MODE_SVC:
10530 return 19;
10531 case ARM_CPU_MODE_ABT:
10532 return 21;
10533 case ARM_CPU_MODE_UND:
10534 return 23;
10535 case ARM_CPU_MODE_FIQ:
10536 return 29;
10537 default:
10538 g_assert_not_reached();
10539 }
10540 case 14:
10541 switch (mode) {
10542 case ARM_CPU_MODE_USR:
10543 case ARM_CPU_MODE_SYS:
10544 case ARM_CPU_MODE_HYP:
10545 return 14;
10546 case ARM_CPU_MODE_IRQ:
10547 return 16;
10548 case ARM_CPU_MODE_SVC:
10549 return 18;
10550 case ARM_CPU_MODE_ABT:
10551 return 20;
10552 case ARM_CPU_MODE_UND:
10553 return 22;
10554 case ARM_CPU_MODE_FIQ:
10555 return 30;
10556 default:
10557 g_assert_not_reached();
10558 }
10559 case 15:
10560 return 31;
10561 default:
10562 g_assert_not_reached();
10563 }
10564}
10565
f944a854
RC
10566static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10567{
10568 uint32_t ret = cpsr_read(env);
10569
10570 /* Move DIT to the correct location for SPSR_ELx */
10571 if (ret & CPSR_DIT) {
10572 ret &= ~CPSR_DIT;
10573 ret |= PSTATE_DIT;
10574 }
10575 /* Merge PSTATE.SS into SPSR_ELx */
10576 ret |= env->pstate & PSTATE_SS;
10577
10578 return ret;
10579}
10580
7ac61020
PM
10581static bool syndrome_is_sync_extabt(uint32_t syndrome)
10582{
10583 /* Return true if this syndrome value is a synchronous external abort */
10584 switch (syn_get_ec(syndrome)) {
10585 case EC_INSNABORT:
10586 case EC_INSNABORT_SAME_EL:
10587 case EC_DATAABORT:
10588 case EC_DATAABORT_SAME_EL:
10589 /* Look at fault status code for all the synchronous ext abort cases */
10590 switch (syndrome & 0x3f) {
10591 case 0x10:
10592 case 0x13:
10593 case 0x14:
10594 case 0x15:
10595 case 0x16:
10596 case 0x17:
10597 return true;
10598 default:
10599 return false;
10600 }
10601 default:
10602 return false;
10603 }
10604}
10605
966f758c
PM
10606/* Handle exception entry to a target EL which is using AArch64 */
10607static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
10608{
10609 ARMCPU *cpu = ARM_CPU(cs);
10610 CPUARMState *env = &cpu->env;
10611 unsigned int new_el = env->exception.target_el;
10612 target_ulong addr = env->cp15.vbar_el[new_el];
10613 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 10614 unsigned int old_mode;
0ab5953b 10615 unsigned int cur_el = arm_current_el(env);
a65dabf7 10616 int rt;
0ab5953b 10617
9a05f7b6
RH
10618 /*
10619 * Note that new_el can never be 0. If cur_el is 0, then
10620 * el0_a64 is is_a64(), else el0_a64 is ignored.
10621 */
10622 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 10623
0ab5953b 10624 if (cur_el < new_el) {
9b37a28c
FR
10625 /*
10626 * Entry vector offset depends on whether the implemented EL
3d6f7617
PM
10627 * immediately lower than the target level is using AArch32 or AArch64
10628 */
10629 bool is_aa64;
cb092fbb 10630 uint64_t hcr;
3d6f7617
PM
10631
10632 switch (new_el) {
10633 case 3:
10634 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10635 break;
10636 case 2:
cb092fbb
RH
10637 hcr = arm_hcr_el2_eff(env);
10638 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10639 is_aa64 = (hcr & HCR_RW) != 0;
10640 break;
10641 }
10642 /* fall through */
3d6f7617
PM
10643 case 1:
10644 is_aa64 = is_a64(env);
10645 break;
10646 default:
10647 g_assert_not_reached();
10648 }
10649
10650 if (is_aa64) {
f3a9b694
PM
10651 addr += 0x400;
10652 } else {
10653 addr += 0x600;
10654 }
10655 } else if (pstate_read(env) & PSTATE_SP) {
10656 addr += 0x200;
10657 }
10658
f3a9b694
PM
10659 switch (cs->exception_index) {
10660 case EXCP_PREFETCH_ABORT:
10661 case EXCP_DATA_ABORT:
7ac61020
PM
10662 /*
10663 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10664 * to be taken to the SError vector entrypoint.
10665 */
10666 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10667 syndrome_is_sync_extabt(env->exception.syndrome)) {
10668 addr += 0x180;
10669 }
f3a9b694
PM
10670 env->cp15.far_el[new_el] = env->exception.vaddress;
10671 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10672 env->cp15.far_el[new_el]);
10673 /* fall through */
10674 case EXCP_BKPT:
10675 case EXCP_UDEF:
10676 case EXCP_SWI:
10677 case EXCP_HVC:
10678 case EXCP_HYP_TRAP:
10679 case EXCP_SMC:
a65dabf7
PM
10680 switch (syn_get_ec(env->exception.syndrome)) {
10681 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10682 /*
10683 * QEMU internal FP/SIMD syndromes from AArch32 include the
10684 * TA and coproc fields which are only exposed if the exception
10685 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10686 * AArch64 format syndrome.
10687 */
10688 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10689 break;
10690 case EC_CP14RTTRAP:
10691 case EC_CP15RTTRAP:
10692 case EC_CP14DTTRAP:
10693 /*
10694 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10695 * the raw register field from the insn; when taking this to
10696 * AArch64 we must convert it to the AArch64 view of the register
10697 * number. Notice that we read a 4-bit AArch32 register number and
10698 * write back a 5-bit AArch64 one.
10699 */
10700 rt = extract32(env->exception.syndrome, 5, 4);
10701 rt = aarch64_regnum(env, rt);
10702 env->exception.syndrome = deposit32(env->exception.syndrome,
10703 5, 5, rt);
10704 break;
10705 case EC_CP15RRTTRAP:
10706 case EC_CP14RRTTRAP:
10707 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10708 rt = extract32(env->exception.syndrome, 5, 4);
10709 rt = aarch64_regnum(env, rt);
10710 env->exception.syndrome = deposit32(env->exception.syndrome,
10711 5, 5, rt);
10712 rt = extract32(env->exception.syndrome, 10, 4);
10713 rt = aarch64_regnum(env, rt);
10714 env->exception.syndrome = deposit32(env->exception.syndrome,
10715 10, 5, rt);
10716 break;
4be42f40 10717 }
f3a9b694
PM
10718 env->cp15.esr_el[new_el] = env->exception.syndrome;
10719 break;
10720 case EXCP_IRQ:
10721 case EXCP_VIRQ:
10722 addr += 0x80;
10723 break;
10724 case EXCP_FIQ:
10725 case EXCP_VFIQ:
10726 addr += 0x100;
10727 break;
3c29632f
RH
10728 case EXCP_VSERR:
10729 addr += 0x180;
10730 /* Construct the SError syndrome from IDS and ISS fields. */
10731 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10732 env->cp15.esr_el[new_el] = env->exception.syndrome;
10733 break;
f3a9b694
PM
10734 default:
10735 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10736 }
10737
10738 if (is_a64(env)) {
4a2696c0 10739 old_mode = pstate_read(env);
f3a9b694
PM
10740 aarch64_save_sp(env, arm_current_el(env));
10741 env->elr_el[new_el] = env->pc;
10742 } else {
f944a854 10743 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10744 env->elr_el[new_el] = env->regs[15];
10745
10746 aarch64_sync_32_to_64(env);
10747
10748 env->condexec_bits = 0;
10749 }
4a2696c0
RH
10750 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10751
f3a9b694
PM
10752 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10753 env->elr_el[new_el]);
10754
4a2696c0
RH
10755 if (cpu_isar_feature(aa64_pan, cpu)) {
10756 /* The value of PSTATE.PAN is normally preserved, except when ... */
10757 new_mode |= old_mode & PSTATE_PAN;
10758 switch (new_el) {
10759 case 2:
10760 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10761 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10762 != (HCR_E2H | HCR_TGE)) {
10763 break;
10764 }
10765 /* fall through */
10766 case 1:
10767 /* ... the target is EL1 ... */
10768 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10769 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10770 new_mode |= PSTATE_PAN;
10771 }
10772 break;
10773 }
10774 }
34669338
RH
10775 if (cpu_isar_feature(aa64_mte, cpu)) {
10776 new_mode |= PSTATE_TCO;
10777 }
4a2696c0 10778
f2f68a78
RC
10779 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10780 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10781 new_mode |= PSTATE_SSBS;
10782 } else {
10783 new_mode &= ~PSTATE_SSBS;
10784 }
10785 }
10786
f3a9b694 10787 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10788 env->aarch64 = true;
f3a9b694 10789 aarch64_restore_sp(env, new_el);
a8a79c7a 10790 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10791
10792 env->pc = addr;
10793
10794 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10795 new_el, env->pc, pstate_read(env));
966f758c
PM
10796}
10797
ed6e6ba9
AB
10798/*
10799 * Do semihosting call and set the appropriate return value. All the
10800 * permission and validity checks have been done at translate time.
10801 *
10802 * We only see semihosting exceptions in TCG only as they are not
10803 * trapped to the hypervisor in KVM.
10804 */
91f78c58 10805#ifdef CONFIG_TCG
ed6e6ba9
AB
10806static void handle_semihosting(CPUState *cs)
10807{
904c04de
PM
10808 ARMCPU *cpu = ARM_CPU(cs);
10809 CPUARMState *env = &cpu->env;
10810
10811 if (is_a64(env)) {
ed6e6ba9
AB
10812 qemu_log_mask(CPU_LOG_INT,
10813 "...handling as semihosting call 0x%" PRIx64 "\n",
10814 env->xregs[0]);
ed3a06b1 10815 do_common_semihosting(cs);
4ff5ef9e 10816 env->pc += 4;
904c04de 10817 } else {
904c04de
PM
10818 qemu_log_mask(CPU_LOG_INT,
10819 "...handling as semihosting call 0x%x\n",
10820 env->regs[0]);
ed3a06b1 10821 do_common_semihosting(cs);
4ff5ef9e 10822 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10823 }
10824}
ed6e6ba9 10825#endif
904c04de 10826
9b37a28c
FR
10827/*
10828 * Handle a CPU exception for A and R profile CPUs.
966f758c
PM
10829 * Do any appropriate logging, handle PSCI calls, and then hand off
10830 * to the AArch64-entry or AArch32-entry function depending on the
10831 * target exception level's register width.
853bfef4
CF
10832 *
10833 * Note: this is used for both TCG (as the do_interrupt tcg op),
10834 * and KVM to re-inject guest debug exceptions, and to
10835 * inject a Synchronous-External-Abort.
966f758c
PM
10836 */
10837void arm_cpu_do_interrupt(CPUState *cs)
10838{
10839 ARMCPU *cpu = ARM_CPU(cs);
10840 CPUARMState *env = &cpu->env;
10841 unsigned int new_el = env->exception.target_el;
10842
531c60a9 10843 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 10844
fc6177af 10845 arm_log_exception(cs);
966f758c
PM
10846 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10847 new_el);
10848 if (qemu_loglevel_mask(CPU_LOG_INT)
10849 && !excp_is_internal(cs->exception_index)) {
6568da45 10850 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 10851 syn_get_ec(env->exception.syndrome),
966f758c
PM
10852 env->exception.syndrome);
10853 }
10854
10855 if (arm_is_psci_call(cpu, cs->exception_index)) {
10856 arm_handle_psci_call(cpu);
10857 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10858 return;
10859 }
10860
ed6e6ba9
AB
10861 /*
10862 * Semihosting semantics depend on the register width of the code
10863 * that caused the exception, not the target exception level, so
10864 * must be handled here.
966f758c 10865 */
ed6e6ba9
AB
10866#ifdef CONFIG_TCG
10867 if (cs->exception_index == EXCP_SEMIHOST) {
10868 handle_semihosting(cs);
904c04de
PM
10869 return;
10870 }
ed6e6ba9 10871#endif
904c04de 10872
9b37a28c
FR
10873 /*
10874 * Hooks may change global state so BQL should be held, also the
b5c53d1b
AL
10875 * BQL needs to be held for any modification of
10876 * cs->interrupt_request.
10877 */
10878 g_assert(qemu_mutex_iothread_locked());
10879
10880 arm_call_pre_el_change_hook(cpu);
10881
904c04de
PM
10882 assert(!excp_is_internal(cs->exception_index));
10883 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
10884 arm_cpu_do_interrupt_aarch64(cs);
10885 } else {
10886 arm_cpu_do_interrupt_aarch32(cs);
10887 }
f3a9b694 10888
bd7d00fc
PM
10889 arm_call_el_change_hook(cpu);
10890
f3a9b694
PM
10891 if (!kvm_enabled()) {
10892 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10893 }
10894}
c47eaf9f 10895#endif /* !CONFIG_USER_ONLY */
0480f69a 10896
aaec1432
RH
10897uint64_t arm_sctlr(CPUARMState *env, int el)
10898{
10899 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10900 if (el == 0) {
10901 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
d902ae75 10902 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
aaec1432
RH
10903 }
10904 return env->cp15.sctlr_el[el];
10905}
c47eaf9f 10906
8ae08860 10907int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
10908{
10909 if (regime_has_2_ranges(mmu_idx)) {
10910 return extract64(tcr, 37, 2);
edc05dd4 10911 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
10912 return 0; /* VTCR_EL2 */
10913 } else {
3e270f67
RH
10914 /* Replicate the single TBI bit so we always have 2 bits. */
10915 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
10916 }
10917}
10918
8ae08860 10919int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
10920{
10921 if (regime_has_2_ranges(mmu_idx)) {
10922 return extract64(tcr, 51, 2);
edc05dd4 10923 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
10924 return 0; /* VTCR_EL2 */
10925 } else {
3e270f67
RH
10926 /* Replicate the single TBID bit so we always have 2 bits. */
10927 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
10928 }
10929}
10930
81ae05fa
RH
10931static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10932{
10933 if (regime_has_2_ranges(mmu_idx)) {
10934 return extract64(tcr, 57, 2);
10935 } else {
10936 /* Replicate the single TCMA bit so we always have 2 bits. */
10937 return extract32(tcr, 30, 1) * 3;
10938 }
10939}
10940
104f703d
PM
10941static ARMGranuleSize tg0_to_gran_size(int tg)
10942{
10943 switch (tg) {
10944 case 0:
10945 return Gran4K;
10946 case 1:
10947 return Gran64K;
10948 case 2:
10949 return Gran16K;
10950 default:
10951 return GranInvalid;
10952 }
10953}
10954
10955static ARMGranuleSize tg1_to_gran_size(int tg)
10956{
10957 switch (tg) {
10958 case 1:
10959 return Gran16K;
10960 case 2:
10961 return Gran4K;
10962 case 3:
10963 return Gran64K;
10964 default:
10965 return GranInvalid;
10966 }
10967}
10968
10969static inline bool have4k(ARMCPU *cpu, bool stage2)
10970{
10971 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10972 : cpu_isar_feature(aa64_tgran4, cpu);
10973}
10974
10975static inline bool have16k(ARMCPU *cpu, bool stage2)
10976{
10977 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10978 : cpu_isar_feature(aa64_tgran16, cpu);
10979}
10980
10981static inline bool have64k(ARMCPU *cpu, bool stage2)
10982{
10983 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10984 : cpu_isar_feature(aa64_tgran64, cpu);
10985}
10986
10987static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10988 bool stage2)
10989{
10990 switch (gran) {
10991 case Gran4K:
10992 if (have4k(cpu, stage2)) {
10993 return gran;
10994 }
10995 break;
10996 case Gran16K:
10997 if (have16k(cpu, stage2)) {
10998 return gran;
10999 }
11000 break;
11001 case Gran64K:
11002 if (have64k(cpu, stage2)) {
11003 return gran;
11004 }
11005 break;
11006 case GranInvalid:
11007 break;
11008 }
11009 /*
11010 * If the guest selects a granule size that isn't implemented,
11011 * the architecture requires that we behave as if it selected one
11012 * that is (with an IMPDEF choice of which one to pick). We choose
11013 * to implement the smallest supported granule size.
11014 */
11015 if (have4k(cpu, stage2)) {
11016 return Gran4K;
11017 }
11018 if (have16k(cpu, stage2)) {
11019 return Gran16K;
11020 }
11021 assert(have64k(cpu, stage2));
11022 return Gran64K;
11023}
11024
b830a5ee
RH
11025ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11026 ARMMMUIdx mmu_idx, bool data)
ba97be9f 11027{
c1547bba 11028 uint64_t tcr = regime_tcr(env, mmu_idx);
89739227 11029 bool epd, hpd, tsz_oob, ds, ha, hd;
ef56c242 11030 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
104f703d 11031 ARMGranuleSize gran;
ef56c242 11032 ARMCPU *cpu = env_archcpu(env);
edc05dd4 11033 bool stage2 = regime_is_stage2(mmu_idx);
ba97be9f 11034
339370b9 11035 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11036 select = 0;
ba97be9f 11037 tsz = extract32(tcr, 0, 6);
104f703d
PM
11038 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11039 if (stage2) {
ba97be9f 11040 /* VTCR_EL2 */
b830a5ee 11041 hpd = false;
ba97be9f 11042 } else {
ba97be9f
RH
11043 hpd = extract32(tcr, 24, 1);
11044 }
11045 epd = false;
ef56c242 11046 sh = extract32(tcr, 12, 2);
f4ecc015 11047 ps = extract32(tcr, 16, 3);
89739227
RH
11048 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11049 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11050 ds = extract64(tcr, 32, 1);
ba97be9f 11051 } else {
e4c93e44
PM
11052 bool e0pd;
11053
71d18164
RH
11054 /*
11055 * Bit 55 is always between the two regions, and is canonical for
11056 * determining if address tagging is enabled.
11057 */
11058 select = extract64(va, 55, 1);
11059 if (!select) {
11060 tsz = extract32(tcr, 0, 6);
104f703d 11061 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
71d18164 11062 epd = extract32(tcr, 7, 1);
ef56c242 11063 sh = extract32(tcr, 12, 2);
71d18164 11064 hpd = extract64(tcr, 41, 1);
e4c93e44 11065 e0pd = extract64(tcr, 55, 1);
71d18164 11066 } else {
71d18164 11067 tsz = extract32(tcr, 16, 6);
104f703d 11068 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
71d18164 11069 epd = extract32(tcr, 23, 1);
ef56c242 11070 sh = extract32(tcr, 28, 2);
71d18164 11071 hpd = extract64(tcr, 42, 1);
e4c93e44 11072 e0pd = extract64(tcr, 56, 1);
71d18164 11073 }
f4ecc015 11074 ps = extract64(tcr, 32, 3);
89739227
RH
11075 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11076 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11077 ds = extract64(tcr, 59, 1);
e4c93e44
PM
11078
11079 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11080 regime_is_user(env, mmu_idx)) {
11081 epd = true;
11082 }
ba97be9f 11083 }
c36c65ea 11084
104f703d 11085 gran = sanitize_gran_size(cpu, gran, stage2);
104f703d 11086
ef56c242 11087 if (cpu_isar_feature(aa64_st, cpu)) {
3c003f70 11088 max_tsz = 48 - (gran == Gran64K);
c36c65ea
RDC
11089 } else {
11090 max_tsz = 39;
11091 }
0af312b6 11092
ef56c242
RH
11093 /*
11094 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11095 * adjust the effective value of DS, as documented.
11096 */
0af312b6 11097 min_tsz = 16;
3c003f70 11098 if (gran == Gran64K) {
ef56c242
RH
11099 if (cpu_isar_feature(aa64_lva, cpu)) {
11100 min_tsz = 12;
11101 }
11102 ds = false;
11103 } else if (ds) {
edc05dd4 11104 if (regime_is_stage2(mmu_idx)) {
3c003f70 11105 if (gran == Gran16K) {
ef56c242
RH
11106 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11107 } else {
11108 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11109 }
edc05dd4 11110 } else {
3c003f70 11111 if (gran == Gran16K) {
ef56c242
RH
11112 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11113 } else {
11114 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11115 }
ef56c242
RH
11116 }
11117 if (ds) {
0af312b6
RH
11118 min_tsz = 12;
11119 }
11120 }
c36c65ea 11121
ebf93ce7
RH
11122 if (tsz > max_tsz) {
11123 tsz = max_tsz;
11124 tsz_oob = true;
11125 } else if (tsz < min_tsz) {
11126 tsz = min_tsz;
11127 tsz_oob = true;
11128 } else {
11129 tsz_oob = false;
11130 }
ba97be9f 11131
b830a5ee
RH
11132 /* Present TBI as a composite with TBID. */
11133 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11134 if (!data) {
11135 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11136 }
11137 tbi = (tbi >> select) & 1;
11138
ba97be9f
RH
11139 return (ARMVAParameters) {
11140 .tsz = tsz,
f4ecc015 11141 .ps = ps,
ef56c242 11142 .sh = sh,
ba97be9f
RH
11143 .select = select,
11144 .tbi = tbi,
11145 .epd = epd,
11146 .hpd = hpd,
ebf93ce7 11147 .tsz_oob = tsz_oob,
ef56c242 11148 .ds = ds,
89739227
RH
11149 .ha = ha,
11150 .hd = ha && hd,
3c003f70 11151 .gran = gran,
ba97be9f
RH
11152 };
11153}
11154
9b37a28c
FR
11155/*
11156 * Note that signed overflow is undefined in C. The following routines are
11157 * careful to use unsigned types where modulo arithmetic is required.
11158 * Failure to do so _will_ break on newer gcc.
11159 */
6ddbc6e4
PB
11160
11161/* Signed saturating arithmetic. */
11162
1654b2d6 11163/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11164static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11165{
11166 uint16_t res;
11167
11168 res = a + b;
11169 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
f927dbda 11170 if (a & 0x8000) {
6ddbc6e4 11171 res = 0x8000;
f927dbda 11172 } else {
6ddbc6e4 11173 res = 0x7fff;
f927dbda 11174 }
6ddbc6e4
PB
11175 }
11176 return res;
11177}
11178
1654b2d6 11179/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11180static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11181{
11182 uint8_t res;
11183
11184 res = a + b;
11185 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
f927dbda 11186 if (a & 0x80) {
6ddbc6e4 11187 res = 0x80;
f927dbda 11188 } else {
6ddbc6e4 11189 res = 0x7f;
f927dbda 11190 }
6ddbc6e4
PB
11191 }
11192 return res;
11193}
11194
1654b2d6 11195/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11196static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11197{
11198 uint16_t res;
11199
11200 res = a - b;
11201 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
f927dbda 11202 if (a & 0x8000) {
6ddbc6e4 11203 res = 0x8000;
f927dbda 11204 } else {
6ddbc6e4 11205 res = 0x7fff;
f927dbda 11206 }
6ddbc6e4
PB
11207 }
11208 return res;
11209}
11210
1654b2d6 11211/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11212static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11213{
11214 uint8_t res;
11215
11216 res = a - b;
11217 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
f927dbda 11218 if (a & 0x80) {
6ddbc6e4 11219 res = 0x80;
f927dbda 11220 } else {
6ddbc6e4 11221 res = 0x7f;
f927dbda 11222 }
6ddbc6e4
PB
11223 }
11224 return res;
11225}
11226
11227#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11228#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11229#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11230#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11231#define PFX q
11232
11233#include "op_addsub.h"
11234
11235/* Unsigned saturating arithmetic. */
460a09c1 11236static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11237{
11238 uint16_t res;
11239 res = a + b;
f927dbda 11240 if (res < a) {
6ddbc6e4 11241 res = 0xffff;
f927dbda 11242 }
6ddbc6e4
PB
11243 return res;
11244}
11245
460a09c1 11246static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11247{
f927dbda 11248 if (a > b) {
6ddbc6e4 11249 return a - b;
f927dbda 11250 } else {
6ddbc6e4 11251 return 0;
f927dbda 11252 }
6ddbc6e4
PB
11253}
11254
11255static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11256{
11257 uint8_t res;
11258 res = a + b;
f927dbda 11259 if (res < a) {
6ddbc6e4 11260 res = 0xff;
f927dbda 11261 }
6ddbc6e4
PB
11262 return res;
11263}
11264
11265static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11266{
f927dbda 11267 if (a > b) {
6ddbc6e4 11268 return a - b;
f927dbda 11269 } else {
6ddbc6e4 11270 return 0;
f927dbda 11271 }
6ddbc6e4
PB
11272}
11273
11274#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11275#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11276#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11277#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11278#define PFX uq
11279
11280#include "op_addsub.h"
11281
11282/* Signed modulo arithmetic. */
11283#define SARITH16(a, b, n, op) do { \
11284 int32_t sum; \
db6e2e65 11285 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11286 RESULT(sum, n, 16); \
11287 if (sum >= 0) \
11288 ge |= 3 << (n * 2); \
04215eb1 11289 } while (0)
6ddbc6e4
PB
11290
11291#define SARITH8(a, b, n, op) do { \
11292 int32_t sum; \
db6e2e65 11293 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11294 RESULT(sum, n, 8); \
11295 if (sum >= 0) \
11296 ge |= 1 << n; \
04215eb1 11297 } while (0)
6ddbc6e4
PB
11298
11299
11300#define ADD16(a, b, n) SARITH16(a, b, n, +)
11301#define SUB16(a, b, n) SARITH16(a, b, n, -)
11302#define ADD8(a, b, n) SARITH8(a, b, n, +)
11303#define SUB8(a, b, n) SARITH8(a, b, n, -)
11304#define PFX s
11305#define ARITH_GE
11306
11307#include "op_addsub.h"
11308
11309/* Unsigned modulo arithmetic. */
11310#define ADD16(a, b, n) do { \
11311 uint32_t sum; \
11312 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11313 RESULT(sum, n, 16); \
a87aa10b 11314 if ((sum >> 16) == 1) \
6ddbc6e4 11315 ge |= 3 << (n * 2); \
04215eb1 11316 } while (0)
6ddbc6e4
PB
11317
11318#define ADD8(a, b, n) do { \
11319 uint32_t sum; \
11320 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11321 RESULT(sum, n, 8); \
a87aa10b
AZ
11322 if ((sum >> 8) == 1) \
11323 ge |= 1 << n; \
04215eb1 11324 } while (0)
6ddbc6e4
PB
11325
11326#define SUB16(a, b, n) do { \
11327 uint32_t sum; \
11328 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11329 RESULT(sum, n, 16); \
11330 if ((sum >> 16) == 0) \
11331 ge |= 3 << (n * 2); \
04215eb1 11332 } while (0)
6ddbc6e4
PB
11333
11334#define SUB8(a, b, n) do { \
11335 uint32_t sum; \
11336 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11337 RESULT(sum, n, 8); \
11338 if ((sum >> 8) == 0) \
a87aa10b 11339 ge |= 1 << n; \
04215eb1 11340 } while (0)
6ddbc6e4
PB
11341
11342#define PFX u
11343#define ARITH_GE
11344
11345#include "op_addsub.h"
11346
11347/* Halved signed arithmetic. */
11348#define ADD16(a, b, n) \
11349 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11350#define SUB16(a, b, n) \
11351 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11352#define ADD8(a, b, n) \
11353 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11354#define SUB8(a, b, n) \
11355 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11356#define PFX sh
11357
11358#include "op_addsub.h"
11359
11360/* Halved unsigned arithmetic. */
11361#define ADD16(a, b, n) \
11362 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11363#define SUB16(a, b, n) \
11364 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11365#define ADD8(a, b, n) \
11366 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11367#define SUB8(a, b, n) \
11368 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11369#define PFX uh
11370
11371#include "op_addsub.h"
11372
11373static inline uint8_t do_usad(uint8_t a, uint8_t b)
11374{
f927dbda 11375 if (a > b) {
6ddbc6e4 11376 return a - b;
f927dbda 11377 } else {
6ddbc6e4 11378 return b - a;
f927dbda 11379 }
6ddbc6e4
PB
11380}
11381
11382/* Unsigned sum of absolute byte differences. */
11383uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11384{
11385 uint32_t sum;
11386 sum = do_usad(a, b);
11387 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 11388 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
11389 sum += do_usad(a >> 24, b >> 24);
11390 return sum;
11391}
11392
11393/* For ARMv6 SEL instruction. */
11394uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11395{
11396 uint32_t mask;
11397
11398 mask = 0;
f927dbda 11399 if (flags & 1) {
6ddbc6e4 11400 mask |= 0xff;
f927dbda
FR
11401 }
11402 if (flags & 2) {
6ddbc6e4 11403 mask |= 0xff00;
f927dbda
FR
11404 }
11405 if (flags & 4) {
6ddbc6e4 11406 mask |= 0xff0000;
f927dbda
FR
11407 }
11408 if (flags & 8) {
6ddbc6e4 11409 mask |= 0xff000000;
f927dbda 11410 }
6ddbc6e4
PB
11411 return (a & mask) | (b & ~mask);
11412}
11413
9b37a28c
FR
11414/*
11415 * CRC helpers.
aa633469
PM
11416 * The upper bytes of val (above the number specified by 'bytes') must have
11417 * been zeroed out by the caller.
11418 */
eb0ecd5a
WN
11419uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11420{
11421 uint8_t buf[4];
11422
aa633469 11423 stl_le_p(buf, val);
eb0ecd5a
WN
11424
11425 /* zlib crc32 converts the accumulator and output to one's complement. */
11426 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11427}
11428
11429uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11430{
11431 uint8_t buf[4];
11432
aa633469 11433 stl_le_p(buf, val);
eb0ecd5a
WN
11434
11435 /* Linux crc32c converts the output to one's complement. */
11436 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11437}
a9e01311 11438
9b37a28c
FR
11439/*
11440 * Return the exception level to which FP-disabled exceptions should
a9e01311
RH
11441 * be taken, or 0 if FP is enabled.
11442 */
ced31551 11443int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11444{
55faa212 11445#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
11446 uint64_t hcr_el2;
11447
9b37a28c
FR
11448 /*
11449 * CPACR and the CPTR registers don't exist before v6, so FP is
a9e01311
RH
11450 * always accessible
11451 */
11452 if (!arm_feature(env, ARM_FEATURE_V6)) {
11453 return 0;
11454 }
11455
d87513c0
PM
11456 if (arm_feature(env, ARM_FEATURE_M)) {
11457 /* CPACR can cause a NOCP UsageFault taken to current security state */
11458 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11459 return 1;
11460 }
11461
11462 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11463 if (!extract32(env->v7m.nsacr, 10, 1)) {
11464 /* FP insns cause a NOCP UsageFault taken to Secure */
11465 return 3;
11466 }
11467 }
11468
11469 return 0;
11470 }
11471
d5a6fa2d
RH
11472 hcr_el2 = arm_hcr_el2_eff(env);
11473
9b37a28c
FR
11474 /*
11475 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
a9e01311
RH
11476 * 0, 2 : trap EL0 and EL1/PL1 accesses
11477 * 1 : trap only EL0 accesses
11478 * 3 : trap no accesses
c2ddb7cf 11479 * This register is ignored if E2H+TGE are both set.
a9e01311 11480 */
d5a6fa2d 11481 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
fab8ad39 11482 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
c2ddb7cf
RH
11483
11484 switch (fpen) {
02e1de14
RH
11485 case 1:
11486 if (cur_el != 0) {
11487 break;
11488 }
11489 /* fall through */
c2ddb7cf
RH
11490 case 0:
11491 case 2:
02e1de14
RH
11492 /* Trap from Secure PL0 or PL1 to Secure PL1. */
11493 if (!arm_el_is_aa64(env, 3)
11494 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
a9e01311
RH
11495 return 3;
11496 }
02e1de14 11497 if (cur_el <= 1) {
c2ddb7cf
RH
11498 return 1;
11499 }
11500 break;
a9e01311 11501 }
a9e01311
RH
11502 }
11503
fc1120a7
PM
11504 /*
11505 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11506 * to control non-secure access to the FPU. It doesn't have any
11507 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11508 */
11509 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11510 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11511 if (!extract32(env->cp15.nsacr, 10, 1)) {
11512 /* FP insns act as UNDEF */
11513 return cur_el == 2 ? 2 : 1;
11514 }
11515 }
11516
d5a6fa2d
RH
11517 /*
11518 * CPTR_EL2 is present in v7VE or v8, and changes format
11519 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 11520 */
d5a6fa2d
RH
11521 if (cur_el <= 2) {
11522 if (hcr_el2 & HCR_E2H) {
fab8ad39 11523 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
d5a6fa2d
RH
11524 case 1:
11525 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11526 break;
11527 }
11528 /* fall through */
11529 case 0:
11530 case 2:
11531 return 2;
11532 }
11533 } else if (arm_is_el2_enabled(env)) {
fab8ad39 11534 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
d5a6fa2d
RH
11535 return 2;
11536 }
11537 }
a9e01311
RH
11538 }
11539
11540 /* CPTR_EL3 : present in v8 */
fab8ad39 11541 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
a9e01311
RH
11542 /* Trap all FP ops to EL3 */
11543 return 3;
11544 }
55faa212 11545#endif
a9e01311
RH
11546 return 0;
11547}
11548
b9f6033c
RH
11549/* Return the exception level we're running at if this is our mmu_idx */
11550int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11551{
11552 if (mmu_idx & ARM_MMU_IDX_M) {
11553 return mmu_idx & ARM_MMU_IDX_M_PRIV;
11554 }
11555
11556 switch (mmu_idx) {
11557 case ARMMMUIdx_E10_0:
11558 case ARMMMUIdx_E20_0:
b9f6033c
RH
11559 return 0;
11560 case ARMMMUIdx_E10_1:
452ef8cb 11561 case ARMMMUIdx_E10_1_PAN:
b9f6033c
RH
11562 return 1;
11563 case ARMMMUIdx_E2:
11564 case ARMMMUIdx_E20_2:
452ef8cb 11565 case ARMMMUIdx_E20_2_PAN:
b9f6033c 11566 return 2;
d902ae75 11567 case ARMMMUIdx_E3:
b9f6033c
RH
11568 return 3;
11569 default:
11570 g_assert_not_reached();
11571 }
11572}
11573
7aab5a8c 11574#ifndef CONFIG_TCG
65e4655c
RH
11575ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11576{
7aab5a8c 11577 g_assert_not_reached();
65e4655c 11578}
7aab5a8c 11579#endif
65e4655c 11580
6f2d9d74
TK
11581static bool arm_pan_enabled(CPUARMState *env)
11582{
11583 if (is_a64(env)) {
11584 return env->pstate & PSTATE_PAN;
11585 } else {
11586 return env->uncached_cpsr & CPSR_PAN;
11587 }
11588}
11589
164690b2 11590ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 11591{
b6ad6062
RDC
11592 ARMMMUIdx idx;
11593 uint64_t hcr;
11594
65e4655c 11595 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 11596 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
11597 }
11598
6003d980 11599 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
11600 switch (el) {
11601 case 0:
b6ad6062
RDC
11602 hcr = arm_hcr_el2_eff(env);
11603 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11604 idx = ARMMMUIdx_E20_0;
11605 } else {
11606 idx = ARMMMUIdx_E10_0;
6003d980 11607 }
b6ad6062 11608 break;
b9f6033c 11609 case 1:
6f2d9d74 11610 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11611 idx = ARMMMUIdx_E10_1_PAN;
11612 } else {
11613 idx = ARMMMUIdx_E10_1;
66412260 11614 }
b6ad6062 11615 break;
b9f6033c 11616 case 2:
6003d980 11617 /* Note that TGE does not apply at EL2. */
b6ad6062 11618 if (arm_hcr_el2_eff(env) & HCR_E2H) {
6f2d9d74 11619 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11620 idx = ARMMMUIdx_E20_2_PAN;
11621 } else {
11622 idx = ARMMMUIdx_E20_2;
66412260 11623 }
b6ad6062
RDC
11624 } else {
11625 idx = ARMMMUIdx_E2;
6003d980 11626 }
b6ad6062 11627 break;
b9f6033c 11628 case 3:
d902ae75 11629 return ARMMMUIdx_E3;
b9f6033c
RH
11630 default:
11631 g_assert_not_reached();
65e4655c 11632 }
b6ad6062 11633
b6ad6062 11634 return idx;
50494a27
RH
11635}
11636
164690b2
RH
11637ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11638{
11639 return arm_mmu_idx_el(env, arm_current_el(env));
11640}
11641
3902bfc6
RH
11642static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11643 ARMMMUIdx mmu_idx,
11644 CPUARMTBFlags flags)
fdd1b228 11645{
a729a46b
RH
11646 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11647 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 11648
fdd1b228 11649 if (arm_singlestep_active(env)) {
a729a46b 11650 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228
RH
11651 }
11652 return flags;
11653}
11654
3902bfc6
RH
11655static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11656 ARMMMUIdx mmu_idx,
11657 CPUARMTBFlags flags)
43eccfb6 11658{
8061a649
RH
11659 bool sctlr_b = arm_sctlr_b(env);
11660
11661 if (sctlr_b) {
a729a46b 11662 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
11663 }
11664 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 11665 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 11666 }
a729a46b 11667 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
11668
11669 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11670}
11671
3902bfc6
RH
11672static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11673 ARMMMUIdx mmu_idx)
6e33ced5 11674{
3902bfc6 11675 CPUARMTBFlags flags = {};
4479ec30
RH
11676 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11677
11678 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11679 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11680 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11681 }
6e33ced5
RH
11682
11683 if (arm_v7m_is_handler_mode(env)) {
a729a46b 11684 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
11685 }
11686
11687 /*
11688 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11689 * is suppressing them because the requested execution priority
11690 * is less than 0.
11691 */
11692 if (arm_feature(env, ARM_FEATURE_V8) &&
11693 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 11694 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 11695 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
11696 }
11697
a393dee0
RH
11698 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11699 DP_TBFLAG_M32(flags, SECURE, 1);
11700 }
11701
6e33ced5
RH
11702 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11703}
11704
3902bfc6
RH
11705static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11706 ARMMMUIdx mmu_idx)
c747224c 11707{
8480e933 11708 CPUARMTBFlags flags = {};
4479ec30
RH
11709 int el = arm_current_el(env);
11710
11711 if (arm_sctlr(env, el) & SCTLR_A) {
11712 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11713 }
0a54d68e
RH
11714
11715 if (arm_el_is_aa64(env, 1)) {
a729a46b 11716 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 11717 }
5bb0a20b 11718
034bb45a 11719 if (el < 2 && env->cp15.hstr_el2 && arm_is_el2_enabled(env) &&
5bb0a20b 11720 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 11721 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
11722 }
11723
520d1621
PM
11724 if (env->uncached_cpsr & CPSR_IL) {
11725 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11726 }
11727
75fe8356
RH
11728 /*
11729 * The SME exception we are testing for is raised via
11730 * AArch64.CheckFPAdvSIMDEnabled(), as called from
11731 * AArch32.CheckAdvSIMDOrFPEnabled().
11732 */
11733 if (el == 0
11734 && FIELD_EX64(env->svcr, SVCR, SM)
11735 && (!arm_is_el2_enabled(env)
11736 || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11737 && arm_el_is_aa64(env, 1)
11738 && !sme_fa64(env, el)) {
11739 DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11740 }
11741
83f4baef 11742 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
11743}
11744
3902bfc6
RH
11745static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11746 ARMMMUIdx mmu_idx)
a9e01311 11747{
8480e933 11748 CPUARMTBFlags flags = {};
d4d7503a 11749 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
c1547bba 11750 uint64_t tcr = regime_tcr(env, mmu_idx);
d4d7503a
RH
11751 uint64_t sctlr;
11752 int tbii, tbid;
b9adaa70 11753
a729a46b 11754 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 11755
339370b9 11756 /* Get control bits for tagged addresses. */
b830a5ee
RH
11757 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11758 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 11759
a729a46b
RH
11760 DP_TBFLAG_A64(flags, TBII, tbii);
11761 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
11762
11763 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11764 int sve_el = sve_exception_el(env, el);
5d8634f5 11765
d4d7503a 11766 /*
397d922c
RH
11767 * If either FP or SVE are disabled, translator does not need len.
11768 * If SVE EL > FP EL, FP exception has precedence, and translator
11769 * does not need SVE EL. Save potential re-translations by forcing
11770 * the unneeded data to zero.
d4d7503a 11771 */
397d922c
RH
11772 if (fp_el != 0) {
11773 if (sve_el > fp_el) {
11774 sve_el = 0;
11775 }
11776 } else if (sve_el == 0) {
5ef3cc56 11777 DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
5d8634f5 11778 }
a729a46b 11779 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
d4d7503a 11780 }
6b2ca83e 11781 if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
5d7953ad 11782 int sme_el = sme_exception_el(env, el);
62151133 11783 bool sm = FIELD_EX64(env->svcr, SVCR, SM);
5d7953ad
RH
11784
11785 DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11786 if (sme_el == 0) {
11787 /* Similarly, do not compute SVL if SME is disabled. */
62151133
RH
11788 int svl = sve_vqm1_for_el_sm(env, el, true);
11789 DP_TBFLAG_A64(flags, SVL, svl);
11790 if (sm) {
11791 /* If SVE is disabled, we will not have set VL above. */
11792 DP_TBFLAG_A64(flags, VL, svl);
11793 }
5d7953ad 11794 }
62151133 11795 if (sm) {
a3637e88 11796 DP_TBFLAG_A64(flags, PSTATE_SM, 1);
75fe8356 11797 DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
a3637e88
RH
11798 }
11799 DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
6b2ca83e 11800 }
1db5e96c 11801
aaec1432 11802 sctlr = regime_sctlr(env, stage1);
1db5e96c 11803
4479ec30
RH
11804 if (sctlr & SCTLR_A) {
11805 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11806 }
11807
8061a649 11808 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 11809 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
11810 }
11811
d4d7503a
RH
11812 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11813 /*
11814 * In order to save space in flags, we record only whether
11815 * pauth is "inactive", meaning all insns are implemented as
11816 * a nop, or "active" when some action must be performed.
11817 * The decision of which action to take is left to a helper.
11818 */
11819 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 11820 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 11821 }
d4d7503a 11822 }
0816ef1b 11823
d4d7503a
RH
11824 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11825 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11826 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 11827 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 11828 }
d4d7503a 11829 }
08f1434a 11830
cc28fc30 11831 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
11832 if (!(env->pstate & PSTATE_UAO)) {
11833 switch (mmu_idx) {
11834 case ARMMMUIdx_E10_1:
11835 case ARMMMUIdx_E10_1_PAN:
7a8014ab 11836 /* TODO: ARMv8.3-NV */
a729a46b 11837 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
11838 break;
11839 case ARMMMUIdx_E20_2:
11840 case ARMMMUIdx_E20_2_PAN:
7a8014ab
RH
11841 /*
11842 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11843 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11844 */
11845 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 11846 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
11847 }
11848 break;
11849 default:
11850 break;
cc28fc30 11851 }
cc28fc30
RH
11852 }
11853
520d1621
PM
11854 if (env->pstate & PSTATE_IL) {
11855 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11856 }
11857
81ae05fa
RH
11858 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11859 /*
11860 * Set MTE_ACTIVE if any access may be Checked, and leave clear
11861 * if all accesses must be Unchecked:
11862 * 1) If no TBI, then there are no tags in the address to check,
11863 * 2) If Tag Check Override, then all accesses are Unchecked,
11864 * 3) If Tag Check Fail == 0, then Checked access have no effect,
11865 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11866 */
11867 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 11868 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
11869 if (tbid
11870 && !(env->pstate & PSTATE_TCO)
11871 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 11872 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
11873 }
11874 }
11875 /* And again for unprivileged accesses, if required. */
a729a46b 11876 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
11877 && tbid
11878 && !(env->pstate & PSTATE_TCO)
2d928adf 11879 && (sctlr & SCTLR_TCF0)
81ae05fa 11880 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 11881 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
11882 }
11883 /* Cache TCMA as well as TBI. */
a729a46b 11884 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
11885 }
11886
d4d7503a
RH
11887 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11888}
11889
3902bfc6 11890static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
11891{
11892 int el = arm_current_el(env);
11893 int fp_el = fp_exception_el(env, el);
164690b2 11894 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
11895
11896 if (is_a64(env)) {
11897 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11898 } else if (arm_feature(env, ARM_FEATURE_M)) {
11899 return rebuild_hflags_m32(env, fp_el, mmu_idx);
11900 } else {
11901 return rebuild_hflags_a32(env, fp_el, mmu_idx);
11902 }
11903}
11904
11905void arm_rebuild_hflags(CPUARMState *env)
11906{
11907 env->hflags = rebuild_hflags_internal(env);
11908}
11909
19717e9b
PM
11910/*
11911 * If we have triggered a EL state change we can't rely on the
11912 * translator having passed it to us, we need to recompute.
11913 */
11914void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11915{
11916 int el = arm_current_el(env);
11917 int fp_el = fp_exception_el(env, el);
11918 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 11919
19717e9b
PM
11920 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11921}
11922
14f3c588
RH
11923void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11924{
11925 int fp_el = fp_exception_el(env, el);
11926 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11927
11928 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11929}
11930
f80741d1
AB
11931/*
11932 * If we have triggered a EL state change we can't rely on the
563152e0 11933 * translator having passed it to us, we need to recompute.
f80741d1
AB
11934 */
11935void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11936{
11937 int el = arm_current_el(env);
11938 int fp_el = fp_exception_el(env, el);
11939 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11940 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11941}
11942
14f3c588
RH
11943void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11944{
11945 int fp_el = fp_exception_el(env, el);
11946 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11947
11948 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11949}
11950
11951void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11952{
11953 int fp_el = fp_exception_el(env, el);
11954 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11955
11956 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11957}
11958
0ee8b24a
PMD
11959static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11960{
11961#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
11962 CPUARMTBFlags c = env->hflags;
11963 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 11964
a378206a
RH
11965 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11966 fprintf(stderr, "TCG hflags mismatch "
11967 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11968 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11969 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
11970 abort();
11971 }
11972#endif
11973}
11974
26702213
PM
11975static bool mve_no_pred(CPUARMState *env)
11976{
11977 /*
11978 * Return true if there is definitely no predication of MVE
11979 * instructions by VPR or LTPSIZE. (Returning false even if there
11980 * isn't any predication is OK; generated code will just be
11981 * a little worse.)
11982 * If the CPU does not implement MVE then this TB flag is always 0.
11983 *
11984 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11985 * logic in gen_update_fp_context() needs to be updated to match.
11986 *
11987 * We do not include the effect of the ECI bits here -- they are
11988 * tracked in other TB flags. This simplifies the logic for
11989 * "when did we emit code that changes the MVE_NO_PRED TB flag
11990 * and thus need to end the TB?".
11991 */
11992 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11993 return false;
11994 }
11995 if (env->v7m.vpr) {
11996 return false;
11997 }
11998 if (env->v7m.ltpsize < 4) {
11999 return false;
12000 }
12001 return true;
12002}
12003
d4d7503a
RH
12004void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12005 target_ulong *cs_base, uint32_t *pflags)
12006{
3902bfc6 12007 CPUARMTBFlags flags;
d4d7503a 12008
0ee8b24a 12009 assert_hflags_rebuild_correctly(env);
3902bfc6 12010 flags = env->hflags;
3d74e2e9 12011
a729a46b 12012 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 12013 *pc = env->pc;
d4d7503a 12014 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 12015 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 12016 }
a9e01311
RH
12017 } else {
12018 *pc = env->regs[15];
6e33ced5
RH
12019
12020 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12021 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12022 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12023 != env->v7m.secure) {
a729a46b 12024 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
12025 }
12026
12027 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12028 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12029 (env->v7m.secure &&
12030 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12031 /*
12032 * ASPEN is set, but FPCA/SFPA indicate that there is no
12033 * active FP context; we must create a new FP context before
12034 * executing any FP insn.
12035 */
a729a46b 12036 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12037 }
12038
12039 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12040 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 12041 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 12042 }
26702213
PM
12043
12044 if (mve_no_pred(env)) {
12045 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12046 }
6e33ced5 12047 } else {
bbad7c62
RH
12048 /*
12049 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12050 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12051 */
12052 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 12053 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 12054 } else {
a729a46b
RH
12055 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12056 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 12057 }
0a54d68e 12058 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 12059 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 12060 }
6e33ced5
RH
12061 }
12062
a729a46b
RH
12063 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12064 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 12065 }
a9e01311 12066
60e12c37
RH
12067 /*
12068 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12069 * states defined in the ARM ARM for software singlestep:
12070 * SS_ACTIVE PSTATE.SS State
12071 * 0 x Inactive (the TB flag for SS is always 0)
12072 * 1 0 Active-pending
12073 * 1 1 Active-not-pending
ae6eb1e9 12074 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 12075 */
a729a46b
RH
12076 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12077 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 12078 }
a9e01311 12079
3902bfc6 12080 *pflags = flags.flags;
a378206a 12081 *cs_base = flags.flags2;
a9e01311 12082}
0ab5953b
RH
12083
12084#ifdef TARGET_AARCH64
12085/*
12086 * The manual says that when SVE is enabled and VQ is widened the
12087 * implementation is allowed to zero the previously inaccessible
12088 * portion of the registers. The corollary to that is that when
12089 * SVE is enabled and VQ is narrowed we are also allowed to zero
12090 * the now inaccessible portion of the registers.
12091 *
12092 * The intent of this is that no predicate bit beyond VQ is ever set.
12093 * Which means that some operations on predicate registers themselves
12094 * may operate on full uint64_t or even unrolled across the maximum
12095 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12096 * may well be cheaper than conditionals to restrict the operation
12097 * to the relevant portion of a uint16_t[16].
12098 */
12099void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12100{
12101 int i, j;
12102 uint64_t pmask;
12103
12104 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12105 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12106
12107 /* Zap the high bits of the zregs. */
12108 for (i = 0; i < 32; i++) {
12109 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12110 }
12111
12112 /* Zap the high bits of the pregs and ffr. */
12113 pmask = 0;
12114 if (vq & 3) {
12115 pmask = ~(-1ULL << (16 * (vq & 3)));
12116 }
12117 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12118 for (i = 0; i < 17; ++i) {
12119 env->vfp.pregs[i].p[j] &= pmask;
12120 }
12121 pmask = 0;
12122 }
12123}
12124
6a775fd6
RH
12125static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12126{
12127 int exc_el;
12128
12129 if (sm) {
12130 exc_el = sme_exception_el(env, el);
12131 } else {
12132 exc_el = sve_exception_el(env, el);
12133 }
12134 if (exc_el) {
12135 return 0; /* disabled */
12136 }
12137 return sve_vqm1_for_el_sm(env, el, sm);
12138}
12139
0ab5953b
RH
12140/*
12141 * Notice a change in SVE vector size when changing EL.
12142 */
9a05f7b6
RH
12143void aarch64_sve_change_el(CPUARMState *env, int old_el,
12144 int new_el, bool el0_a64)
0ab5953b 12145{
2fc0cc0e 12146 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12147 int old_len, new_len;
6a775fd6 12148 bool old_a64, new_a64, sm;
0ab5953b
RH
12149
12150 /* Nothing to do if no SVE. */
cd208a1c 12151 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12152 return;
12153 }
12154
12155 /* Nothing to do if FP is disabled in either EL. */
12156 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12157 return;
12158 }
12159
04fbce76
RH
12160 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12161 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12162
12163 /*
12164 * Both AArch64.TakeException and AArch64.ExceptionReturn
12165 * invoke ResetSVEState when taking an exception from, or
12166 * returning to, AArch32 state when PSTATE.SM is enabled.
12167 */
6a775fd6
RH
12168 sm = FIELD_EX64(env->svcr, SVCR, SM);
12169 if (old_a64 != new_a64 && sm) {
04fbce76
RH
12170 arm_reset_sve_state(env);
12171 return;
12172 }
12173
0ab5953b
RH
12174 /*
12175 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12176 * at ELx, or not available because the EL is in AArch32 state, then
12177 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12178 * has an effective value of 0".
12179 *
12180 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12181 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12182 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12183 * we already have the correct register contents when encountering the
12184 * vq0->vq0 transition between EL0->EL1.
12185 */
6a775fd6
RH
12186 old_len = new_len = 0;
12187 if (old_a64) {
12188 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12189 }
12190 if (new_a64) {
12191 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12192 }
0ab5953b
RH
12193
12194 /* When changing vector length, clear inaccessible state. */
12195 if (new_len < old_len) {
12196 aarch64_sve_narrow_vq(env, new_len + 1);
12197 }
12198}
12199#endif