]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
hw/timer/xilinx_timer: Use XpsTimerState instead of 'struct timerblock'
[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 }
ea22747c
RH
1869 } else {
1870 valid_mask &= ~(SCR_RW | SCR_ST);
da3d8b13
RH
1871 if (cpu_isar_feature(aa32_ras, cpu)) {
1872 valid_mask |= SCR_TERR;
1873 }
ea22747c 1874 }
64e0e2de
EI
1875
1876 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1877 valid_mask &= ~SCR_HCE;
1878
9b37a28c
FR
1879 /*
1880 * On ARMv7, SMD (or SCD as it is called in v7) is only
64e0e2de
EI
1881 * supported if EL2 exists. The bit is UNK/SBZP when
1882 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1883 * when EL2 is unavailable.
4eb27640 1884 * On ARMv8, this bit is always available.
64e0e2de 1885 */
4eb27640
GB
1886 if (arm_feature(env, ARM_FEATURE_V7) &&
1887 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1888 valid_mask &= ~SCR_SMD;
1889 }
1890 }
1891
1892 /* Clear all-context RES0 bits. */
1893 value &= valid_mask;
d902ae75
RH
1894 changed = env->cp15.scr_el3 ^ value;
1895 env->cp15.scr_el3 = value;
1896
1897 /*
1898 * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1899 * we must invalidate all TLBs below EL3.
1900 */
1901 if (changed & SCR_NS) {
1902 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1903 ARMMMUIdxBit_E20_0 |
1904 ARMMMUIdxBit_E10_1 |
1905 ARMMMUIdxBit_E20_2 |
1906 ARMMMUIdxBit_E10_1_PAN |
1907 ARMMMUIdxBit_E20_2_PAN |
1908 ARMMMUIdxBit_E2));
1909 }
64e0e2de
EI
1910}
1911
10d0ef3e
MN
1912static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1913{
1914 /*
1915 * scr_write will set the RES1 bits on an AArch64-only CPU.
1916 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1917 */
1918 scr_write(env, ri, 0);
1919}
1920
e2ce5fcd
PM
1921static CPAccessResult access_tid4(CPUARMState *env,
1922 const ARMCPRegInfo *ri,
1923 bool isread)
630fcd4d 1924{
e2ce5fcd
PM
1925 if (arm_current_el(env) == 1 &&
1926 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
630fcd4d
MZ
1927 return CP_ACCESS_TRAP_EL2;
1928 }
1929
1930 return CP_ACCESS_OK;
1931}
1932
c4241c7d 1933static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1934{
2fc0cc0e 1935 ARMCPU *cpu = env_archcpu(env);
b85a1fd6 1936
9b37a28c
FR
1937 /*
1938 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
b85a1fd6
FA
1939 * bank
1940 */
1941 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1942 ri->secure & ARM_CP_SECSTATE_S);
1943
1944 return cpu->ccsidr[index];
776d4e5c
PM
1945}
1946
c4241c7d
PM
1947static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1948 uint64_t value)
776d4e5c 1949{
8d5c773e 1950 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1951}
1952
1090b9c6
PM
1953static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1954{
29a0af61 1955 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1956 bool el1 = arm_current_el(env) == 1;
1957 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1958 uint64_t ret = 0;
1959
cc974d5c 1960 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1961 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1962 ret |= CPSR_I;
1963 }
1964 } else {
1965 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1966 ret |= CPSR_I;
1967 }
1090b9c6 1968 }
636540e9 1969
cc974d5c 1970 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1971 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1972 ret |= CPSR_F;
1973 }
1974 } else {
1975 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1976 ret |= CPSR_F;
1977 }
1090b9c6 1978 }
636540e9 1979
3c29632f
RH
1980 if (hcr_el2 & HCR_AMO) {
1981 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1982 ret |= CPSR_A;
1983 }
1984 }
1985
1090b9c6
PM
1986 return ret;
1987}
1988
93fbc983
MZ
1989static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1990 bool isread)
1991{
1992 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1993 return CP_ACCESS_TRAP_EL2;
1994 }
1995
1996 return CP_ACCESS_OK;
1997}
1998
1999static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2000 bool isread)
2001{
2002 if (arm_feature(env, ARM_FEATURE_V8)) {
2003 return access_aa64_tid1(env, ri, isread);
2004 }
2005
2006 return CP_ACCESS_OK;
2007}
2008
e9aa6c21 2009static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2010 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2011 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2012 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
2013 /*
2014 * Performance monitors are implementation defined in v7,
200ac0ef 2015 * but with an ARM recommended set of registers, which we
ac689a2e 2016 * follow.
200ac0ef
PM
2017 *
2018 * Performance registers fall into three categories:
2019 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2020 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2021 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2022 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2023 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2024 */
2025 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7f4fbfb5 2026 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b 2027 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2028 .writefn = pmcntenset_write,
2029 .accessfn = pmreg_access,
2030 .raw_writefn = raw_write },
7f4fbfb5 2031 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
8521466b
AF
2032 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2033 .access = PL0_RW, .accessfn = pmreg_access,
2034 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2035 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2036 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2037 .access = PL0_RW,
2038 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2039 .accessfn = pmreg_access,
2040 .writefn = pmcntenclr_write,
7f4fbfb5 2041 .type = ARM_CP_ALIAS | ARM_CP_IO },
8521466b
AF
2042 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2043 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2044 .access = PL0_RW, .accessfn = pmreg_access,
7f4fbfb5 2045 .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b
AF
2046 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2047 .writefn = pmcntenclr_write },
200ac0ef 2048 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2049 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2050 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2051 .accessfn = pmreg_access,
2052 .writefn = pmovsr_write,
2053 .raw_writefn = raw_write },
978364f1
AF
2054 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2055 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2056 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2057 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2058 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2059 .writefn = pmovsr_write,
2060 .raw_writefn = raw_write },
200ac0ef 2061 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2062 .access = PL0_W, .accessfn = pmreg_access_swinc,
2063 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2064 .writefn = pmswinc_write },
2065 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2066 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2067 .access = PL0_W, .accessfn = pmreg_access_swinc,
2068 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2069 .writefn = pmswinc_write },
6b040780
WH
2070 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2071 .access = PL0_RW, .type = ARM_CP_ALIAS,
2072 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2073 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2074 .raw_writefn = raw_write},
2075 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2076 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2077 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2078 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2079 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2080 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2081 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2082 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2083 .accessfn = pmreg_access_ccntr },
8521466b
AF
2084 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2085 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2086 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2087 .type = ARM_CP_IO,
980ebe87
AL
2088 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2089 .readfn = pmccntr_read, .writefn = pmccntr_write,
2090 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2091 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2092 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2093 .access = PL0_RW, .accessfn = pmreg_access,
2094 .type = ARM_CP_ALIAS | ARM_CP_IO,
2095 .resetvalue = 0, },
8521466b
AF
2096 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2097 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2098 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2099 .access = PL0_RW, .accessfn = pmreg_access,
2100 .type = ARM_CP_IO,
2101 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2102 .resetvalue = 0, },
200ac0ef 2103 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2104 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2105 .accessfn = pmreg_access,
fdb86656
WH
2106 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2107 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2108 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2109 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2110 .accessfn = pmreg_access,
fdb86656 2111 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2112 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2113 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2114 .accessfn = pmreg_access_xevcntr,
2115 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2116 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2117 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2118 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2119 .accessfn = pmreg_access_xevcntr,
2120 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2121 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2122 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2123 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2124 .resetvalue = 0,
d4e6df63 2125 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2126 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2127 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2128 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2129 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2130 .resetvalue = 0,
2131 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2132 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2133 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2134 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2135 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2136 .resetvalue = 0,
d4e6df63 2137 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2138 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2139 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2140 .access = PL1_RW, .accessfn = access_tpm,
2141 .type = ARM_CP_IO,
2142 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2143 .writefn = pmintenset_write, .raw_writefn = raw_write,
2144 .resetvalue = 0x0 },
200ac0ef 2145 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2146 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2147 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2148 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2149 .writefn = pmintenclr_write, },
978364f1
AF
2150 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2151 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2152 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2153 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2154 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2155 .writefn = pmintenclr_write },
7da845b0
PM
2156 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2157 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d 2158 .access = PL1_R,
e2ce5fcd 2159 .accessfn = access_tid4,
630fcd4d 2160 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2161 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2162 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d 2163 .access = PL1_RW,
e2ce5fcd 2164 .accessfn = access_tid4,
630fcd4d 2165 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2166 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2167 offsetof(CPUARMState, cp15.csselr_ns) } },
9b37a28c
FR
2168 /*
2169 * Auxiliary ID register: this actually has an IMPDEF value but for now
776d4e5c
PM
2170 * just RAZ for all cores:
2171 */
0ff644a7
PM
2172 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2173 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2174 .access = PL1_R, .type = ARM_CP_CONST,
2175 .accessfn = access_aa64_tid1,
2176 .resetvalue = 0 },
9b37a28c
FR
2177 /*
2178 * Auxiliary fault status registers: these also are IMPDEF, and we
f32cdad5
PM
2179 * choose to RAZ/WI for all cores.
2180 */
2181 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2182 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2183 .access = PL1_RW, .accessfn = access_tvm_trvm,
2184 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2185 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2186 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2187 .access = PL1_RW, .accessfn = access_tvm_trvm,
2188 .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
2189 /*
2190 * MAIR can just read-as-written because we don't implement caches
b0fe2427
PM
2191 * and so don't need to care about memory attributes.
2192 */
2193 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2194 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2195 .access = PL1_RW, .accessfn = access_tvm_trvm,
2196 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2197 .resetvalue = 0 },
4cfb8ad8
PM
2198 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2199 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2200 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2201 .resetvalue = 0 },
9b37a28c
FR
2202 /*
2203 * For non-long-descriptor page tables these are PRRR and NMRR;
b0fe2427 2204 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2205 */
9b37a28c
FR
2206 /*
2207 * MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2208 * allows them to assign the correct fieldoffset based on the endianness
2209 * handled in the field definitions.
2210 */
a903c449 2211 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2212 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2213 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2214 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2215 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2216 .resetfn = arm_cp_reset_ignore },
a903c449 2217 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2218 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2219 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2220 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2221 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2222 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2223 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2224 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2225 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2226 /* 32 bit ITLB invalidates */
2227 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2228 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2229 .writefn = tlbiall_write },
995939a6 2230 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2231 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2232 .writefn = tlbimva_write },
995939a6 2233 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2234 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2235 .writefn = tlbiasid_write },
995939a6
PM
2236 /* 32 bit DTLB invalidates */
2237 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2238 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2239 .writefn = tlbiall_write },
995939a6 2240 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2241 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2242 .writefn = tlbimva_write },
995939a6 2243 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2244 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2245 .writefn = tlbiasid_write },
995939a6
PM
2246 /* 32 bit TLB invalidates */
2247 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2248 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2249 .writefn = tlbiall_write },
995939a6 2250 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2251 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2252 .writefn = tlbimva_write },
995939a6 2253 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2254 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2255 .writefn = tlbiasid_write },
995939a6 2256 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2257 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2258 .writefn = tlbimvaa_write },
995939a6
PM
2259};
2260
2261static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2262 /* 32 bit TLB invalidates, Inner Shareable */
2263 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 2264 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2265 .writefn = tlbiall_is_write },
995939a6 2266 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 2267 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2268 .writefn = tlbimva_is_write },
995939a6 2269 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 2270 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2271 .writefn = tlbiasid_is_write },
995939a6 2272 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 2273 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2274 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2275};
2276
327dd510
AL
2277static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2278 /* PMOVSSET is not implemented in v7 before v7ve */
2279 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2280 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2281 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2282 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2283 .writefn = pmovsset_write,
2284 .raw_writefn = raw_write },
2285 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2286 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2287 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2288 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2289 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2290 .writefn = pmovsset_write,
2291 .raw_writefn = raw_write },
327dd510
AL
2292};
2293
c4241c7d
PM
2294static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2295 uint64_t value)
c326b979
PM
2296{
2297 value &= 1;
2298 env->teecr = value;
c326b979
PM
2299}
2300
cc7613bf
PM
2301static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2302 bool isread)
2303{
2304 /*
2305 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2306 * at all, so we don't need to check whether we're v8A.
2307 */
2308 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2309 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2310 return CP_ACCESS_TRAP_EL2;
2311 }
2312 return CP_ACCESS_OK;
2313}
2314
3f208fd7
PM
2315static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2316 bool isread)
c326b979 2317{
dcbff19b 2318 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2319 return CP_ACCESS_TRAP;
c326b979 2320 }
cc7613bf 2321 return teecr_access(env, ri, isread);
c326b979
PM
2322}
2323
2324static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2325 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2326 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2327 .resetvalue = 0,
cc7613bf 2328 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2329 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2330 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2331 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2332};
2333
4d31c596 2334static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2335 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2336 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2337 .access = PL0_RW,
54bf36ed 2338 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2339 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2340 .access = PL0_RW,
54bf36ed
FA
2341 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2342 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2343 .resetfn = arm_cp_reset_ignore },
2344 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2345 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
04215eb1 2346 .access = PL0_R | PL1_W,
54bf36ed
FA
2347 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2348 .resetvalue = 0},
4d31c596 2349 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
04215eb1 2350 .access = PL0_R | PL1_W,
54bf36ed
FA
2351 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2352 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2353 .resetfn = arm_cp_reset_ignore },
54bf36ed 2354 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2355 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2356 .access = PL1_RW,
54bf36ed
FA
2357 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2358 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2359 .access = PL1_RW,
2360 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2361 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2362 .resetvalue = 0 },
4d31c596
PM
2363};
2364
55d284af
PM
2365#ifndef CONFIG_USER_ONLY
2366
3f208fd7
PM
2367static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2368 bool isread)
00108f2d 2369{
9b37a28c
FR
2370 /*
2371 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
75502672
PM
2372 * Writable only at the highest implemented exception level.
2373 */
2374 int el = arm_current_el(env);
5bc84371
RH
2375 uint64_t hcr;
2376 uint32_t cntkctl;
75502672
PM
2377
2378 switch (el) {
2379 case 0:
5bc84371
RH
2380 hcr = arm_hcr_el2_eff(env);
2381 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2382 cntkctl = env->cp15.cnthctl_el2;
2383 } else {
2384 cntkctl = env->cp15.c14_cntkctl;
2385 }
2386 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2387 return CP_ACCESS_TRAP;
2388 }
2389 break;
2390 case 1:
2391 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2392 arm_is_secure_below_el3(env)) {
2393 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2394 return CP_ACCESS_TRAP_UNCATEGORIZED;
2395 }
2396 break;
2397 case 2:
2398 case 3:
2399 break;
00108f2d 2400 }
75502672
PM
2401
2402 if (!isread && el < arm_highest_el(env)) {
2403 return CP_ACCESS_TRAP_UNCATEGORIZED;
2404 }
2405
00108f2d
PM
2406 return CP_ACCESS_OK;
2407}
2408
3f208fd7
PM
2409static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2410 bool isread)
00108f2d 2411{
0b6440af 2412 unsigned int cur_el = arm_current_el(env);
e6ef0169 2413 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2414 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2415
5bc84371
RH
2416 switch (cur_el) {
2417 case 0:
2418 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2419 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2420 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2421 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2422 }
0b6440af 2423
5bc84371
RH
2424 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2425 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2426 return CP_ACCESS_TRAP;
2427 }
2428
2429 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2430 if (hcr & HCR_E2H) {
2431 if (timeridx == GTIMER_PHYS &&
2432 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2433 return CP_ACCESS_TRAP_EL2;
2434 }
2435 } else {
2436 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2437 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2438 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2439 return CP_ACCESS_TRAP_EL2;
2440 }
2441 }
2442 break;
2443
2444 case 1:
2445 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2446 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2447 (hcr & HCR_E2H
2448 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2449 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2450 return CP_ACCESS_TRAP_EL2;
2451 }
2452 break;
0b6440af 2453 }
00108f2d
PM
2454 return CP_ACCESS_OK;
2455}
2456
3f208fd7
PM
2457static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2458 bool isread)
00108f2d 2459{
0b6440af 2460 unsigned int cur_el = arm_current_el(env);
e6ef0169 2461 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2462 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2463
5bc84371
RH
2464 switch (cur_el) {
2465 case 0:
2466 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2467 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2468 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2469 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2470 }
0b6440af 2471
5bc84371
RH
2472 /*
2473 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2474 * EL0 if EL0[PV]TEN is zero.
2475 */
2476 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2477 return CP_ACCESS_TRAP;
2478 }
2479 /* fall through */
2480
2481 case 1:
e6ef0169 2482 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2483 if (hcr & HCR_E2H) {
2484 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2485 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2486 return CP_ACCESS_TRAP_EL2;
2487 }
2488 } else {
2489 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2490 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2491 return CP_ACCESS_TRAP_EL2;
2492 }
2493 }
2494 }
2495 break;
0b6440af 2496 }
00108f2d
PM
2497 return CP_ACCESS_OK;
2498}
2499
2500static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2501 const ARMCPRegInfo *ri,
2502 bool isread)
00108f2d 2503{
3f208fd7 2504 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2505}
2506
2507static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2508 const ARMCPRegInfo *ri,
2509 bool isread)
00108f2d 2510{
3f208fd7 2511 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2512}
2513
3f208fd7
PM
2514static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2515 bool isread)
00108f2d 2516{
3f208fd7 2517 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2518}
2519
3f208fd7
PM
2520static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2521 bool isread)
00108f2d 2522{
3f208fd7 2523 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2524}
2525
b4d3978c 2526static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2527 const ARMCPRegInfo *ri,
2528 bool isread)
b4d3978c 2529{
9b37a28c
FR
2530 /*
2531 * The AArch64 register view of the secure physical timer is
b4d3978c
PM
2532 * always accessible from EL3, and configurably accessible from
2533 * Secure EL1.
2534 */
2535 switch (arm_current_el(env)) {
2536 case 1:
2537 if (!arm_is_secure(env)) {
2538 return CP_ACCESS_TRAP;
2539 }
2540 if (!(env->cp15.scr_el3 & SCR_ST)) {
2541 return CP_ACCESS_TRAP_EL3;
2542 }
2543 return CP_ACCESS_OK;
2544 case 0:
2545 case 2:
2546 return CP_ACCESS_TRAP;
2547 case 3:
2548 return CP_ACCESS_OK;
2549 default:
2550 g_assert_not_reached();
2551 }
2552}
2553
55d284af
PM
2554static uint64_t gt_get_countervalue(CPUARMState *env)
2555{
7def8754
AJ
2556 ARMCPU *cpu = env_archcpu(env);
2557
2558 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2559}
2560
2561static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2562{
2563 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2564
2565 if (gt->ctl & 1) {
9b37a28c
FR
2566 /*
2567 * Timer enabled: calculate and set current ISTATUS, irq, and
55d284af
PM
2568 * reset timer to when ISTATUS next has to change
2569 */
edac4d8a
EI
2570 uint64_t offset = timeridx == GTIMER_VIRT ?
2571 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2572 uint64_t count = gt_get_countervalue(&cpu->env);
2573 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2574 int istatus = count - offset >= gt->cval;
55d284af 2575 uint64_t nexttick;
194cbc49 2576 int irqstate;
55d284af
PM
2577
2578 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2579
2580 irqstate = (istatus && !(gt->ctl & 2));
2581 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2582
55d284af
PM
2583 if (istatus) {
2584 /* Next transition is when count rolls back over to zero */
2585 nexttick = UINT64_MAX;
2586 } else {
2587 /* Next transition is when we hit cval */
edac4d8a 2588 nexttick = gt->cval + offset;
55d284af 2589 }
9b37a28c
FR
2590 /*
2591 * Note that the desired next expiry time might be beyond the
55d284af
PM
2592 * signed-64-bit range of a QEMUTimer -- in this case we just
2593 * set the timer for as far in the future as possible. When the
2594 * timer expires we will reset the timer for any remaining period.
2595 */
7def8754 2596 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2597 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2598 } else {
2599 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2600 }
194cbc49 2601 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2602 } else {
2603 /* Timer disabled: ISTATUS and timer output always clear */
2604 gt->ctl &= ~4;
2605 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2606 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2607 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2608 }
2609}
2610
0e3eca4c
EI
2611static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2612 int timeridx)
55d284af 2613{
2fc0cc0e 2614 ARMCPU *cpu = env_archcpu(env);
55d284af 2615
bc72ad67 2616 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2617}
2618
c4241c7d 2619static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2620{
c4241c7d 2621 return gt_get_countervalue(env);
55d284af
PM
2622}
2623
53d1f856
RH
2624static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2625{
2626 uint64_t hcr;
2627
2628 switch (arm_current_el(env)) {
2629 case 2:
2630 hcr = arm_hcr_el2_eff(env);
2631 if (hcr & HCR_E2H) {
2632 return 0;
2633 }
2634 break;
2635 case 0:
2636 hcr = arm_hcr_el2_eff(env);
2637 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2638 return 0;
2639 }
2640 break;
2641 }
2642
2643 return env->cp15.cntvoff_el2;
2644}
2645
edac4d8a
EI
2646static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2647{
53d1f856 2648 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2649}
2650
c4241c7d 2651static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2652 int timeridx,
c4241c7d 2653 uint64_t value)
55d284af 2654{
194cbc49 2655 trace_arm_gt_cval_write(timeridx, value);
55d284af 2656 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2657 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2658}
c4241c7d 2659
0e3eca4c
EI
2660static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2661 int timeridx)
55d284af 2662{
53d1f856
RH
2663 uint64_t offset = 0;
2664
2665 switch (timeridx) {
2666 case GTIMER_VIRT:
8c94b071 2667 case GTIMER_HYPVIRT:
53d1f856
RH
2668 offset = gt_virt_cnt_offset(env);
2669 break;
2670 }
55d284af 2671
c4241c7d 2672 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2673 (gt_get_countervalue(env) - offset));
55d284af
PM
2674}
2675
c4241c7d 2676static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2677 int timeridx,
c4241c7d 2678 uint64_t value)
55d284af 2679{
53d1f856
RH
2680 uint64_t offset = 0;
2681
2682 switch (timeridx) {
2683 case GTIMER_VIRT:
8c94b071 2684 case GTIMER_HYPVIRT:
53d1f856
RH
2685 offset = gt_virt_cnt_offset(env);
2686 break;
2687 }
55d284af 2688
194cbc49 2689 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2690 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2691 sextract64(value, 0, 32);
2fc0cc0e 2692 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2693}
2694
c4241c7d 2695static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2696 int timeridx,
c4241c7d 2697 uint64_t value)
55d284af 2698{
2fc0cc0e 2699 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2700 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2701
194cbc49 2702 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2703 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2704 if ((oldval ^ value) & 1) {
2705 /* Enable toggled */
2706 gt_recalc_timer(cpu, timeridx);
d3afacc7 2707 } else if ((oldval ^ value) & 2) {
9b37a28c
FR
2708 /*
2709 * IMASK toggled: don't need to recalculate,
55d284af
PM
2710 * just set the interrupt line based on ISTATUS
2711 */
194cbc49
PM
2712 int irqstate = (oldval & 4) && !(value & 2);
2713
2714 trace_arm_gt_imask_toggle(timeridx, irqstate);
2715 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2716 }
55d284af
PM
2717}
2718
0e3eca4c
EI
2719static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2720{
2721 gt_timer_reset(env, ri, GTIMER_PHYS);
2722}
2723
2724static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725 uint64_t value)
2726{
2727 gt_cval_write(env, ri, GTIMER_PHYS, value);
2728}
2729
2730static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2731{
2732 return gt_tval_read(env, ri, GTIMER_PHYS);
2733}
2734
2735static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2736 uint64_t value)
2737{
2738 gt_tval_write(env, ri, GTIMER_PHYS, value);
2739}
2740
2741static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742 uint64_t value)
2743{
2744 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2745}
2746
bb5972e4
RH
2747static int gt_phys_redir_timeridx(CPUARMState *env)
2748{
2749 switch (arm_mmu_idx(env)) {
2750 case ARMMMUIdx_E20_0:
2751 case ARMMMUIdx_E20_2:
452ef8cb 2752 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2753 return GTIMER_HYP;
2754 default:
2755 return GTIMER_PHYS;
2756 }
2757}
2758
2759static int gt_virt_redir_timeridx(CPUARMState *env)
2760{
2761 switch (arm_mmu_idx(env)) {
2762 case ARMMMUIdx_E20_0:
2763 case ARMMMUIdx_E20_2:
452ef8cb 2764 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2765 return GTIMER_HYPVIRT;
2766 default:
2767 return GTIMER_VIRT;
2768 }
2769}
2770
2771static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2772 const ARMCPRegInfo *ri)
2773{
2774 int timeridx = gt_phys_redir_timeridx(env);
2775 return env->cp15.c14_timer[timeridx].cval;
2776}
2777
2778static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2779 uint64_t value)
2780{
2781 int timeridx = gt_phys_redir_timeridx(env);
2782 gt_cval_write(env, ri, timeridx, value);
2783}
2784
2785static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2786 const ARMCPRegInfo *ri)
2787{
2788 int timeridx = gt_phys_redir_timeridx(env);
2789 return gt_tval_read(env, ri, timeridx);
2790}
2791
2792static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2793 uint64_t value)
2794{
2795 int timeridx = gt_phys_redir_timeridx(env);
2796 gt_tval_write(env, ri, timeridx, value);
2797}
2798
2799static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2800 const ARMCPRegInfo *ri)
2801{
2802 int timeridx = gt_phys_redir_timeridx(env);
2803 return env->cp15.c14_timer[timeridx].ctl;
2804}
2805
2806static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2807 uint64_t value)
2808{
2809 int timeridx = gt_phys_redir_timeridx(env);
2810 gt_ctl_write(env, ri, timeridx, value);
2811}
2812
0e3eca4c
EI
2813static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2814{
2815 gt_timer_reset(env, ri, GTIMER_VIRT);
2816}
2817
2818static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2819 uint64_t value)
2820{
2821 gt_cval_write(env, ri, GTIMER_VIRT, value);
2822}
2823
2824static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2825{
2826 return gt_tval_read(env, ri, GTIMER_VIRT);
2827}
2828
2829static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2830 uint64_t value)
2831{
2832 gt_tval_write(env, ri, GTIMER_VIRT, value);
2833}
2834
2835static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2836 uint64_t value)
2837{
2838 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2839}
2840
edac4d8a
EI
2841static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2842 uint64_t value)
2843{
2fc0cc0e 2844 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2845
194cbc49 2846 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2847 raw_write(env, ri, value);
2848 gt_recalc_timer(cpu, GTIMER_VIRT);
2849}
2850
bb5972e4
RH
2851static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2852 const ARMCPRegInfo *ri)
2853{
2854 int timeridx = gt_virt_redir_timeridx(env);
2855 return env->cp15.c14_timer[timeridx].cval;
2856}
2857
2858static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2859 uint64_t value)
2860{
2861 int timeridx = gt_virt_redir_timeridx(env);
2862 gt_cval_write(env, ri, timeridx, value);
2863}
2864
2865static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2866 const ARMCPRegInfo *ri)
2867{
2868 int timeridx = gt_virt_redir_timeridx(env);
2869 return gt_tval_read(env, ri, timeridx);
2870}
2871
2872static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2873 uint64_t value)
2874{
2875 int timeridx = gt_virt_redir_timeridx(env);
2876 gt_tval_write(env, ri, timeridx, value);
2877}
2878
2879static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2880 const ARMCPRegInfo *ri)
2881{
2882 int timeridx = gt_virt_redir_timeridx(env);
2883 return env->cp15.c14_timer[timeridx].ctl;
2884}
2885
2886static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887 uint64_t value)
2888{
2889 int timeridx = gt_virt_redir_timeridx(env);
2890 gt_ctl_write(env, ri, timeridx, value);
2891}
2892
b0e66d95
EI
2893static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2894{
2895 gt_timer_reset(env, ri, GTIMER_HYP);
2896}
2897
2898static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2899 uint64_t value)
2900{
2901 gt_cval_write(env, ri, GTIMER_HYP, value);
2902}
2903
2904static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2905{
2906 return gt_tval_read(env, ri, GTIMER_HYP);
2907}
2908
2909static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2910 uint64_t value)
2911{
2912 gt_tval_write(env, ri, GTIMER_HYP, value);
2913}
2914
2915static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2916 uint64_t value)
2917{
2918 gt_ctl_write(env, ri, GTIMER_HYP, value);
2919}
2920
b4d3978c
PM
2921static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2922{
2923 gt_timer_reset(env, ri, GTIMER_SEC);
2924}
2925
2926static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2927 uint64_t value)
2928{
2929 gt_cval_write(env, ri, GTIMER_SEC, value);
2930}
2931
2932static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2933{
2934 return gt_tval_read(env, ri, GTIMER_SEC);
2935}
2936
2937static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938 uint64_t value)
2939{
2940 gt_tval_write(env, ri, GTIMER_SEC, value);
2941}
2942
2943static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944 uint64_t value)
2945{
2946 gt_ctl_write(env, ri, GTIMER_SEC, value);
2947}
2948
8c94b071
RH
2949static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2950{
2951 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2952}
2953
2954static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955 uint64_t value)
2956{
2957 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2958}
2959
2960static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2961{
2962 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2963}
2964
2965static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2966 uint64_t value)
2967{
2968 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2969}
2970
2971static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972 uint64_t value)
2973{
2974 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2975}
2976
55d284af
PM
2977void arm_gt_ptimer_cb(void *opaque)
2978{
2979 ARMCPU *cpu = opaque;
2980
2981 gt_recalc_timer(cpu, GTIMER_PHYS);
2982}
2983
2984void arm_gt_vtimer_cb(void *opaque)
2985{
2986 ARMCPU *cpu = opaque;
2987
2988 gt_recalc_timer(cpu, GTIMER_VIRT);
2989}
2990
b0e66d95
EI
2991void arm_gt_htimer_cb(void *opaque)
2992{
2993 ARMCPU *cpu = opaque;
2994
2995 gt_recalc_timer(cpu, GTIMER_HYP);
2996}
2997
b4d3978c
PM
2998void arm_gt_stimer_cb(void *opaque)
2999{
3000 ARMCPU *cpu = opaque;
3001
3002 gt_recalc_timer(cpu, GTIMER_SEC);
3003}
3004
8c94b071
RH
3005void arm_gt_hvtimer_cb(void *opaque)
3006{
3007 ARMCPU *cpu = opaque;
3008
3009 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3010}
3011
96eec6b2
AJ
3012static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3013{
3014 ARMCPU *cpu = env_archcpu(env);
3015
3016 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3017}
3018
55d284af 3019static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
9b37a28c
FR
3020 /*
3021 * Note that CNTFRQ is purely reads-as-written for the benefit
55d284af
PM
3022 * of software; writing it doesn't actually change the timer frequency.
3023 * Our reset value matches the fixed frequency we implement the timer at.
3024 */
3025 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3026 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3027 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3028 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3029 },
3030 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3031 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3032 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3033 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3034 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3035 },
3036 /* overall control: mostly access permissions */
a7adc4b7
PM
3037 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3038 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3039 .access = PL1_RW,
3040 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3041 .resetvalue = 0,
3042 },
3043 /* per-timer control */
3044 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3045 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3046 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3047 .accessfn = gt_ptimer_access,
3048 .fieldoffset = offsetoflow32(CPUARMState,
3049 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3050 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3051 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3052 },
9c513e78 3053 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3054 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3055 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3056 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3057 .accessfn = gt_ptimer_access,
3058 .fieldoffset = offsetoflow32(CPUARMState,
3059 cp15.c14_timer[GTIMER_SEC].ctl),
3060 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3061 },
a7adc4b7
PM
3062 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3063 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3064 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3065 .accessfn = gt_ptimer_access,
55d284af
PM
3066 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3067 .resetvalue = 0,
bb5972e4
RH
3068 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3069 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3070 },
3071 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3072 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3073 .accessfn = gt_vtimer_access,
3074 .fieldoffset = offsetoflow32(CPUARMState,
3075 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3076 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3077 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3078 },
3079 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3080 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3081 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3082 .accessfn = gt_vtimer_access,
55d284af
PM
3083 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3084 .resetvalue = 0,
bb5972e4
RH
3085 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3086 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3087 },
3088 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3089 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3090 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3091 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3092 .accessfn = gt_ptimer_access,
bb5972e4 3093 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3094 },
9c513e78 3095 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3096 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3097 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3098 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3099 .accessfn = gt_ptimer_access,
3100 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3101 },
a7adc4b7
PM
3102 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3103 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3104 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3105 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3106 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3107 },
55d284af 3108 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3109 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3110 .accessfn = gt_vtimer_access,
bb5972e4 3111 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3112 },
a7adc4b7
PM
3113 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3114 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3115 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3116 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3117 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3118 },
55d284af
PM
3119 /* The counter itself */
3120 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3121 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3122 .accessfn = gt_pct_access,
a7adc4b7
PM
3123 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3124 },
3125 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3126 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3127 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3128 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3129 },
3130 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3131 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3132 .accessfn = gt_vct_access,
edac4d8a 3133 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3134 },
3135 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3136 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3137 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3138 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3139 },
3140 /* Comparison value, indicating when the timer goes off */
3141 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3142 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3143 .access = PL0_RW,
7a0e58fa 3144 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3145 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3146 .accessfn = gt_ptimer_access,
bb5972e4
RH
3147 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3148 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3149 },
9c513e78 3150 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3151 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3152 .access = PL0_RW,
9ff9dd3c
PM
3153 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3154 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3155 .accessfn = gt_ptimer_access,
3156 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3157 },
a7adc4b7
PM
3158 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3159 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3160 .access = PL0_RW,
a7adc4b7
PM
3161 .type = ARM_CP_IO,
3162 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3163 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3164 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3165 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3166 },
3167 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3168 .access = PL0_RW,
7a0e58fa 3169 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3170 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3171 .accessfn = gt_vtimer_access,
bb5972e4
RH
3172 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3173 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3174 },
3175 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3176 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3177 .access = PL0_RW,
a7adc4b7
PM
3178 .type = ARM_CP_IO,
3179 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3180 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3181 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3182 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3183 },
9b37a28c
FR
3184 /*
3185 * Secure timer -- this is actually restricted to only EL3
b4d3978c
PM
3186 * and configurably Secure-EL1 via the accessfn.
3187 */
3188 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3189 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3190 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3191 .accessfn = gt_stimer_access,
3192 .readfn = gt_sec_tval_read,
3193 .writefn = gt_sec_tval_write,
3194 .resetfn = gt_sec_timer_reset,
3195 },
3196 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3197 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3198 .type = ARM_CP_IO, .access = PL1_RW,
3199 .accessfn = gt_stimer_access,
3200 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3201 .resetvalue = 0,
3202 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3203 },
3204 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3205 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3206 .type = ARM_CP_IO, .access = PL1_RW,
3207 .accessfn = gt_stimer_access,
3208 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3209 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3210 },
55d284af
PM
3211};
3212
bb5972e4
RH
3213static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3214 bool isread)
3215{
3216 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3217 return CP_ACCESS_TRAP;
3218 }
3219 return CP_ACCESS_OK;
3220}
3221
55d284af 3222#else
26c4a83b 3223
9b37a28c
FR
3224/*
3225 * In user-mode most of the generic timer registers are inaccessible
26c4a83b 3226 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3227 */
26c4a83b
AB
3228
3229static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3230{
7def8754
AJ
3231 ARMCPU *cpu = env_archcpu(env);
3232
9b37a28c
FR
3233 /*
3234 * Currently we have no support for QEMUTimer in linux-user so we
26c4a83b
AB
3235 * can't call gt_get_countervalue(env), instead we directly
3236 * call the lower level functions.
3237 */
7def8754 3238 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3239}
3240
6cc7a3ae 3241static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3242 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3243 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3244 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3245 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3246 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3247 },
3248 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3249 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3250 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3251 .readfn = gt_virt_cnt_read,
3252 },
6cc7a3ae
PM
3253};
3254
55d284af
PM
3255#endif
3256
c4241c7d 3257static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3258{
891a2fe7 3259 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3260 raw_write(env, ri, value);
891a2fe7 3261 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3262 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3263 } else {
8d5c773e 3264 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3265 }
4a501606
PM
3266}
3267
3268#ifndef CONFIG_USER_ONLY
3269/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3270
3f208fd7
PM
3271static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3272 bool isread)
92611c00
PM
3273{
3274 if (ri->opc2 & 4) {
9b37a28c
FR
3275 /*
3276 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3277 * Secure EL1 (which can only happen if EL3 is AArch64).
3278 * They are simply UNDEF if executed from NS EL1.
3279 * They function normally from EL2 or EL3.
92611c00 3280 */
87562e4f
PM
3281 if (arm_current_el(env) == 1) {
3282 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
3283 if (env->cp15.scr_el3 & SCR_EEL2) {
3284 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3285 }
87562e4f
PM
3286 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3287 }
3288 return CP_ACCESS_TRAP_UNCATEGORIZED;
3289 }
92611c00
PM
3290 }
3291 return CP_ACCESS_OK;
3292}
3293
9fb005b0 3294#ifdef CONFIG_TCG
060e8a48 3295static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
7aee3cb9
RH
3296 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3297 bool is_secure)
4a501606 3298{
b7cc4e82 3299 bool ret;
01c097f7 3300 uint64_t par64;
1313e2d7 3301 bool format64 = false;
e14b5a23 3302 ARMMMUFaultInfo fi = {};
de05a709 3303 GetPhysAddrResult res = {};
4a501606 3304
7aee3cb9
RH
3305 ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3306 is_secure, &res, &fi);
1313e2d7 3307
9f225e60
PM
3308 /*
3309 * ATS operations only do S1 or S1+S2 translations, so we never
3310 * have to deal with the ARMCacheAttrs format for S2 only.
3311 */
de05a709 3312 assert(!res.cacheattrs.is_s2_format);
9f225e60 3313
0710b2fa
PM
3314 if (ret) {
3315 /*
3316 * Some kinds of translation fault must cause exceptions rather
3317 * than being reported in the PAR.
3318 */
3319 int current_el = arm_current_el(env);
3320 int target_el;
3321 uint32_t syn, fsr, fsc;
3322 bool take_exc = false;
3323
b1a10c86 3324 if (fi.s1ptw && current_el == 1
fee7aa46 3325 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3326 /*
3327 * Synchronous stage 2 fault on an access made as part of the
3328 * translation table walk for AT S1E0* or AT S1E1* insn
3329 * executed from NS EL1. If this is a synchronous external abort
3330 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3331 * to EL3. Otherwise the fault is taken as an exception to EL2,
3332 * and HPFAR_EL2 holds the faulting IPA.
3333 */
3334 if (fi.type == ARMFault_SyncExternalOnWalk &&
3335 (env->cp15.scr_el3 & SCR_EA)) {
3336 target_el = 3;
3337 } else {
3338 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3339 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3340 env->cp15.hpfar_el2 |= HPFAR_NS;
3341 }
0710b2fa
PM
3342 target_el = 2;
3343 }
3344 take_exc = true;
3345 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3346 /*
3347 * Synchronous external aborts during a translation table walk
3348 * are taken as Data Abort exceptions.
3349 */
3350 if (fi.stage2) {
3351 if (current_el == 3) {
3352 target_el = 3;
3353 } else {
3354 target_el = 2;
3355 }
3356 } else {
3357 target_el = exception_target_el(env);
3358 }
3359 take_exc = true;
3360 }
3361
3362 if (take_exc) {
3363 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3364 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3365 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3366 fsr = arm_fi_to_lfsc(&fi);
3367 fsc = extract32(fsr, 0, 6);
3368 } else {
3369 fsr = arm_fi_to_sfsc(&fi);
3370 fsc = 0x3f;
3371 }
3372 /*
3373 * Report exception with ESR indicating a fault due to a
3374 * translation table walk for a cache maintenance instruction.
3375 */
e24fd076 3376 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3377 fi.ea, 1, fi.s1ptw, 1, fsc);
3378 env->exception.vaddress = value;
3379 env->exception.fsr = fsr;
3380 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3381 }
3382 }
3383
1313e2d7
EI
3384 if (is_a64(env)) {
3385 format64 = true;
3386 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3387 /*
3388 * ATS1Cxx:
3389 * * TTBCR.EAE determines whether the result is returned using the
3390 * 32-bit or the 64-bit PAR format
3391 * * Instructions executed in Hyp mode always use the 64bit format
3392 *
3393 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3394 * * The Non-secure TTBCR.EAE bit is set to 1
3395 * * The implementation includes EL2, and the value of HCR.VM is 1
3396 *
9d1bab33
PM
3397 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3398 *
23463e0e 3399 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3400 */
3401 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3402
3403 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3404 if (mmu_idx == ARMMMUIdx_E10_0 ||
3405 mmu_idx == ARMMMUIdx_E10_1 ||
3406 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3407 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3408 } else {
3409 format64 |= arm_current_el(env) == 2;
3410 }
3411 }
3412 }
3413
3414 if (format64) {
5efe9ed4 3415 /* Create a 64-bit PAR */
01c097f7 3416 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3417 if (!ret) {
7fa7ea8f
RH
3418 par64 |= res.f.phys_addr & ~0xfffULL;
3419 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3420 par64 |= (1 << 9); /* NS */
3421 }
de05a709
RH
3422 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3423 par64 |= res.cacheattrs.shareability << 7; /* SH */
4a501606 3424 } else {
5efe9ed4
PM
3425 uint32_t fsr = arm_fi_to_lfsc(&fi);
3426
702a9357 3427 par64 |= 1; /* F */
b7cc4e82 3428 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3429 if (fi.stage2) {
3430 par64 |= (1 << 9); /* S */
3431 }
3432 if (fi.s1ptw) {
3433 par64 |= (1 << 8); /* PTW */
3434 }
4a501606
PM
3435 }
3436 } else {
9b37a28c
FR
3437 /*
3438 * fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3439 * translation table format (with WnR always clear).
3440 * Convert it to a 32-bit PAR.
3441 */
b7cc4e82 3442 if (!ret) {
702a9357 3443 /* We do not set any attribute bits in the PAR */
7fa7ea8f 3444 if (res.f.lg_page_size == 24
702a9357 3445 && arm_feature(env, ARM_FEATURE_V7)) {
7fa7ea8f 3446 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
702a9357 3447 } else {
7fa7ea8f 3448 par64 = res.f.phys_addr & 0xfffff000;
702a9357 3449 }
7fa7ea8f 3450 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3451 par64 |= (1 << 9); /* NS */
3452 }
702a9357 3453 } else {
5efe9ed4
PM
3454 uint32_t fsr = arm_fi_to_sfsc(&fi);
3455
b7cc4e82
PC
3456 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3457 ((fsr & 0xf) << 1) | 1;
702a9357 3458 }
4a501606 3459 }
060e8a48
PM
3460 return par64;
3461}
9fb005b0 3462#endif /* CONFIG_TCG */
060e8a48
PM
3463
3464static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3465{
9fb005b0 3466#ifdef CONFIG_TCG
03ae85f8 3467 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3468 uint64_t par64;
d3649702
PM
3469 ARMMMUIdx mmu_idx;
3470 int el = arm_current_el(env);
3471 bool secure = arm_is_secure_below_el3(env);
060e8a48 3472
d3649702
PM
3473 switch (ri->opc2 & 6) {
3474 case 0:
04b07d29 3475 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3476 switch (el) {
3477 case 3:
d902ae75 3478 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3479 secure = true;
d3649702
PM
3480 break;
3481 case 2:
b6ad6062 3482 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3483 /* fall through */
d3649702 3484 case 1:
04b07d29 3485 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
d902ae75 3486 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3487 } else {
d902ae75 3488 mmu_idx = ARMMMUIdx_Stage1_E1;
04b07d29 3489 }
d3649702
PM
3490 break;
3491 default:
3492 g_assert_not_reached();
3493 }
3494 break;
3495 case 2:
3496 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3497 switch (el) {
3498 case 3:
d902ae75 3499 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3500 secure = true;
d3649702
PM
3501 break;
3502 case 2:
b1a10c86 3503 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3504 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3505 break;
3506 case 1:
d902ae75 3507 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3508 break;
3509 default:
3510 g_assert_not_reached();
3511 }
3512 break;
3513 case 4:
3514 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3515 mmu_idx = ARMMMUIdx_E10_1;
7aee3cb9 3516 secure = false;
d3649702
PM
3517 break;
3518 case 6:
3519 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3520 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3521 secure = false;
d3649702
PM
3522 break;
3523 default:
3524 g_assert_not_reached();
3525 }
3526
7aee3cb9 3527 par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
01c097f7
FA
3528
3529 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3530#else
3531 /* Handled by hardware accelerator. */
3532 g_assert_not_reached();
3533#endif /* CONFIG_TCG */
4a501606 3534}
060e8a48 3535
14db7fe0
PM
3536static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3537 uint64_t value)
3538{
9fb005b0 3539#ifdef CONFIG_TCG
03ae85f8 3540 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3541 uint64_t par64;
3542
7aee3cb9
RH
3543 /* There is no SecureEL2 for AArch32. */
3544 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
14db7fe0
PM
3545
3546 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3547#else
3548 /* Handled by hardware accelerator. */
3549 g_assert_not_reached();
3550#endif /* CONFIG_TCG */
14db7fe0
PM
3551}
3552
3f208fd7
PM
3553static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3554 bool isread)
2a47df95 3555{
926c1b97
RDC
3556 if (arm_current_el(env) == 3 &&
3557 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3558 return CP_ACCESS_TRAP;
3559 }
3560 return CP_ACCESS_OK;
3561}
3562
060e8a48
PM
3563static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3564 uint64_t value)
3565{
9fb005b0 3566#ifdef CONFIG_TCG
03ae85f8 3567 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3568 ARMMMUIdx mmu_idx;
3569 int secure = arm_is_secure_below_el3(env);
638d5dbd
AK
3570 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3571 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
d3649702
PM
3572
3573 switch (ri->opc2 & 6) {
3574 case 0:
3575 switch (ri->opc1) {
04b07d29
RH
3576 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3577 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
638d5dbd
AK
3578 mmu_idx = regime_e20 ?
3579 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3580 } else {
638d5dbd 3581 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
04b07d29 3582 }
d3649702
PM
3583 break;
3584 case 4: /* AT S1E2R, AT S1E2W */
638d5dbd 3585 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
d3649702
PM
3586 break;
3587 case 6: /* AT S1E3R, AT S1E3W */
d902ae75 3588 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3589 secure = true;
d3649702
PM
3590 break;
3591 default:
3592 g_assert_not_reached();
3593 }
3594 break;
3595 case 2: /* AT S1E0R, AT S1E0W */
638d5dbd 3596 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3597 break;
3598 case 4: /* AT S12E1R, AT S12E1W */
638d5dbd 3599 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
d3649702
PM
3600 break;
3601 case 6: /* AT S12E0R, AT S12E0W */
638d5dbd 3602 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
d3649702
PM
3603 break;
3604 default:
3605 g_assert_not_reached();
3606 }
060e8a48 3607
7aee3cb9
RH
3608 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3609 mmu_idx, secure);
9fb005b0
PMD
3610#else
3611 /* Handled by hardware accelerator. */
3612 g_assert_not_reached();
3613#endif /* CONFIG_TCG */
060e8a48 3614}
4a501606
PM
3615#endif
3616
3617static const ARMCPRegInfo vapa_cp_reginfo[] = {
3618 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3619 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3620 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3621 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3622 .writefn = par_write },
3623#ifndef CONFIG_USER_ONLY
87562e4f 3624 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3625 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3626 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3627 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3628#endif
4a501606
PM
3629};
3630
18032bec
PM
3631/* Return basic MPU access permission bits. */
3632static uint32_t simple_mpu_ap_bits(uint32_t val)
3633{
3634 uint32_t ret;
3635 uint32_t mask;
3636 int i;
3637 ret = 0;
3638 mask = 3;
3639 for (i = 0; i < 16; i += 2) {
3640 ret |= (val >> i) & mask;
3641 mask <<= 2;
3642 }
3643 return ret;
3644}
3645
3646/* Pad basic MPU access permission bits to extended format. */
3647static uint32_t extended_mpu_ap_bits(uint32_t val)
3648{
3649 uint32_t ret;
3650 uint32_t mask;
3651 int i;
3652 ret = 0;
3653 mask = 3;
3654 for (i = 0; i < 16; i += 2) {
3655 ret |= (val & mask) << i;
3656 mask <<= 2;
3657 }
3658 return ret;
3659}
3660
c4241c7d
PM
3661static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3662 uint64_t value)
18032bec 3663{
7e09797c 3664 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3665}
3666
c4241c7d 3667static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3668{
7e09797c 3669 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3670}
3671
c4241c7d
PM
3672static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3673 uint64_t value)
18032bec 3674{
7e09797c 3675 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3676}
3677
c4241c7d 3678static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3679{
7e09797c 3680 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3681}
3682
6cb0b013
PC
3683static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3684{
3685 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3686
3687 if (!u32p) {
3688 return 0;
3689 }
3690
1bc04a88 3691 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3692 return *u32p;
3693}
3694
3695static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3696 uint64_t value)
3697{
2fc0cc0e 3698 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3699 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3700
3701 if (!u32p) {
3702 return;
3703 }
3704
1bc04a88 3705 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3706 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3707 *u32p = value;
3708}
3709
6cb0b013
PC
3710static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3711 uint64_t value)
3712{
2fc0cc0e 3713 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3714 uint32_t nrgs = cpu->pmsav7_dregion;
3715
3716 if (value >= nrgs) {
3717 qemu_log_mask(LOG_GUEST_ERROR,
3718 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3719 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3720 return;
3721 }
3722
3723 raw_write(env, ri, value);
3724}
3725
761c4642
TR
3726static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3727 uint64_t value)
3728{
3729 ARMCPU *cpu = env_archcpu(env);
3730
3731 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3732 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3733}
3734
3735static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3736{
3737 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3738}
3739
3740static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3741 uint64_t value)
3742{
3743 ARMCPU *cpu = env_archcpu(env);
3744
3745 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3746 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3747}
3748
3749static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3750{
3751 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3752}
3753
3754static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3755 uint64_t value)
3756{
3757 ARMCPU *cpu = env_archcpu(env);
3758
3759 /*
3760 * Ignore writes that would select not implemented region.
3761 * This is architecturally UNPREDICTABLE.
3762 */
3763 if (value >= cpu->pmsav7_dregion) {
3764 return;
3765 }
3766
3767 env->pmsav7.rnr[M_REG_NS] = value;
3768}
3769
3770static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3771 uint64_t value)
3772{
3773 ARMCPU *cpu = env_archcpu(env);
3774
3775 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3776 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3777}
3778
3779static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3780{
3781 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3782}
3783
3784static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3785 uint64_t value)
3786{
3787 ARMCPU *cpu = env_archcpu(env);
3788
3789 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3790 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3791}
3792
3793static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3794{
3795 return env->pmsav8.hprlar[env->pmsav8.hprselr];
3796}
3797
3798static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3799 uint64_t value)
3800{
3801 uint32_t n;
3802 uint32_t bit;
3803 ARMCPU *cpu = env_archcpu(env);
3804
3805 /* Ignore writes to unimplemented regions */
3806 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3807 value &= MAKE_64BIT_MASK(0, rmax);
3808
3809 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3810
3811 /* Register alias is only valid for first 32 indexes */
3812 for (n = 0; n < rmax; ++n) {
3813 bit = extract32(value, n, 1);
3814 env->pmsav8.hprlar[n] = deposit32(
3815 env->pmsav8.hprlar[n], 0, 1, bit);
3816 }
3817}
3818
3819static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3820{
3821 uint32_t n;
3822 uint32_t result = 0x0;
3823 ARMCPU *cpu = env_archcpu(env);
3824
3825 /* Register alias is only valid for first 32 indexes */
3826 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3827 if (env->pmsav8.hprlar[n] & 0x1) {
3828 result |= (0x1 << n);
3829 }
3830 }
3831 return result;
3832}
3833
3834static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3835 uint64_t value)
3836{
3837 ARMCPU *cpu = env_archcpu(env);
3838
3839 /*
3840 * Ignore writes that would select not implemented region.
3841 * This is architecturally UNPREDICTABLE.
3842 */
3843 if (value >= cpu->pmsav8r_hdregion) {
3844 return;
3845 }
3846
3847 env->pmsav8.hprselr = value;
3848}
3849
3850static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3851 uint64_t value)
3852{
3853 ARMCPU *cpu = env_archcpu(env);
3854 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3855 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3856
3857 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3858
3859 if (ri->opc1 & 4) {
3860 if (index >= cpu->pmsav8r_hdregion) {
3861 return;
3862 }
3863 if (ri->opc2 & 0x1) {
3864 env->pmsav8.hprlar[index] = value;
3865 } else {
3866 env->pmsav8.hprbar[index] = value;
3867 }
3868 } else {
3869 if (index >= cpu->pmsav7_dregion) {
3870 return;
3871 }
3872 if (ri->opc2 & 0x1) {
3873 env->pmsav8.rlar[M_REG_NS][index] = value;
3874 } else {
3875 env->pmsav8.rbar[M_REG_NS][index] = value;
3876 }
3877 }
3878}
3879
3880static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3881{
3882 ARMCPU *cpu = env_archcpu(env);
3883 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3884 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3885
3886 if (ri->opc1 & 4) {
3887 if (index >= cpu->pmsav8r_hdregion) {
3888 return 0x0;
3889 }
3890 if (ri->opc2 & 0x1) {
3891 return env->pmsav8.hprlar[index];
3892 } else {
3893 return env->pmsav8.hprbar[index];
3894 }
3895 } else {
3896 if (index >= cpu->pmsav7_dregion) {
3897 return 0x0;
3898 }
3899 if (ri->opc2 & 0x1) {
3900 return env->pmsav8.rlar[M_REG_NS][index];
3901 } else {
3902 return env->pmsav8.rbar[M_REG_NS][index];
3903 }
3904 }
3905}
3906
3907static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
3908 { .name = "PRBAR",
3909 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
3910 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3911 .accessfn = access_tvm_trvm,
3912 .readfn = prbar_read, .writefn = prbar_write },
3913 { .name = "PRLAR",
3914 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
3915 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3916 .accessfn = access_tvm_trvm,
3917 .readfn = prlar_read, .writefn = prlar_write },
3918 { .name = "PRSELR", .resetvalue = 0,
3919 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
3920 .access = PL1_RW, .accessfn = access_tvm_trvm,
3921 .writefn = prselr_write,
3922 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
3923 { .name = "HPRBAR", .resetvalue = 0,
3924 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
3925 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3926 .readfn = hprbar_read, .writefn = hprbar_write },
3927 { .name = "HPRLAR",
3928 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
3929 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3930 .readfn = hprlar_read, .writefn = hprlar_write },
3931 { .name = "HPRSELR", .resetvalue = 0,
3932 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
3933 .access = PL2_RW,
3934 .writefn = hprselr_write,
3935 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
3936 { .name = "HPRENR",
3937 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
3938 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3939 .readfn = hprenr_read, .writefn = hprenr_write },
3940};
3941
6cb0b013 3942static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
9b37a28c
FR
3943 /*
3944 * Reset for all these registers is handled in arm_cpu_reset(),
69ceea64
PM
3945 * because the PMSAv7 is also used by M-profile CPUs, which do
3946 * not register cpregs but still need the state to be reset.
3947 */
6cb0b013
PC
3948 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3949 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3950 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3951 .readfn = pmsav7_read, .writefn = pmsav7_write,
3952 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3953 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3954 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3955 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3956 .readfn = pmsav7_read, .writefn = pmsav7_write,
3957 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3958 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3959 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3960 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3961 .readfn = pmsav7_read, .writefn = pmsav7_write,
3962 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3963 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3964 .access = PL1_RW,
1bc04a88 3965 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3966 .writefn = pmsav7_rgnr_write,
3967 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3968};
3969
18032bec
PM
3970static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3971 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3972 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3973 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3974 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3975 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3976 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3977 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3978 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3979 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3980 .access = PL1_RW,
7e09797c
PM
3981 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3982 .resetvalue = 0, },
18032bec
PM
3983 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3984 .access = PL1_RW,
7e09797c
PM
3985 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3986 .resetvalue = 0, },
ecce5c3c
PM
3987 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3988 .access = PL1_RW,
3989 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3990 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3991 .access = PL1_RW,
3992 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3993 /* Protection region base and size registers */
e508a92b
PM
3994 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3995 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3996 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3997 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3998 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3999 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4000 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4001 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4002 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4003 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4004 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4005 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4006 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4007 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4008 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4009 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4010 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4011 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4012 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4013 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4014 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4015 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4016 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4017 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
4018};
4019
cb4a0a34
PM
4020static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4021 uint64_t value)
ecce5c3c 4022{
cb4a0a34 4023 ARMCPU *cpu = env_archcpu(env);
2ebcebe2 4024
e389be16
FA
4025 if (!arm_feature(env, ARM_FEATURE_V8)) {
4026 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
cb4a0a34
PM
4027 /*
4028 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4029 * using Long-descriptor translation table format
4030 */
e389be16
FA
4031 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4032 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
cb4a0a34
PM
4033 /*
4034 * In an implementation that includes the Security Extensions
e389be16
FA
4035 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4036 * Short-descriptor translation table format.
4037 */
4038 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4039 } else {
4040 value &= TTBCR_N;
4041 }
e42c4db3 4042 }
e389be16 4043
d4e6df63 4044 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9b37a28c
FR
4045 /*
4046 * With LPAE the TTBCR could result in a change of ASID
d4e6df63
PM
4047 * via the TTBCR.A1 bit, so do a TLB flush.
4048 */
d10eb08f 4049 tlb_flush(CPU(cpu));
d4e6df63 4050 }
cb4a0a34 4051 raw_write(env, ri, value);
ecce5c3c
PM
4052}
4053
d06dc933 4054static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
4055 uint64_t value)
4056{
2fc0cc0e 4057 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 4058
cb2e37df 4059 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 4060 tlb_flush(CPU(cpu));
cb4a0a34 4061 raw_write(env, ri, value);
cb2e37df
PM
4062}
4063
327ed10f
PM
4064static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4065 uint64_t value)
4066{
93f379b0
RH
4067 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4068 if (cpreg_field_is_64bit(ri) &&
4069 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 4070 ARMCPU *cpu = env_archcpu(env);
d10eb08f 4071 tlb_flush(CPU(cpu));
327ed10f
PM
4072 }
4073 raw_write(env, ri, value);
4074}
4075
ed30da8e
RH
4076static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4077 uint64_t value)
4078{
d06dc933
RH
4079 /*
4080 * If we are running with E2&0 regime, then an ASID is active.
4081 * Flush if that might be changing. Note we're not checking
4082 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4083 * holds the active ASID, only checking the field that might.
4084 */
4085 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4086 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
4087 uint16_t mask = ARMMMUIdxBit_E20_2 |
4088 ARMMMUIdxBit_E20_2_PAN |
4089 ARMMMUIdxBit_E20_0;
b6ad6062 4090 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 4091 }
ed30da8e
RH
4092 raw_write(env, ri, value);
4093}
4094
b698e9cf
EI
4095static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4096 uint64_t value)
4097{
2fc0cc0e 4098 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
4099 CPUState *cs = CPU(cpu);
4100
97fa9350
RH
4101 /*
4102 * A change in VMID to the stage2 page table (Stage2) invalidates
575a94af 4103 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
97fa9350 4104 */
00b20ee4 4105 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
575a94af 4106 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
b698e9cf 4107 }
00b20ee4 4108 raw_write(env, ri, value);
b698e9cf
EI
4109}
4110
8e5d75c9 4111static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4112 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4113 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4114 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4115 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4116 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4117 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4118 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4119 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4120 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4121 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4122 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4123 offsetof(CPUARMState, cp15.dfar_ns) } },
4124 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4125 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
4126 .access = PL1_RW, .accessfn = access_tvm_trvm,
4127 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 4128 .resetvalue = 0, },
8e5d75c9
PC
4129};
4130
4131static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4132 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4133 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4134 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 4135 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4136 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4137 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
4138 .access = PL1_RW, .accessfn = access_tvm_trvm,
4139 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4140 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4141 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4142 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4143 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
4144 .access = PL1_RW, .accessfn = access_tvm_trvm,
4145 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4146 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4147 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4148 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4149 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4150 .access = PL1_RW, .accessfn = access_tvm_trvm,
4151 .writefn = vmsa_tcr_el12_write,
cb4a0a34
PM
4152 .raw_writefn = raw_write,
4153 .resetvalue = 0,
11f136ee 4154 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4155 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4156 .access = PL1_RW, .accessfn = access_tvm_trvm,
4157 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
cb4a0a34
PM
4158 .raw_writefn = raw_write,
4159 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4160 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4161};
4162
9b37a28c
FR
4163/*
4164 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
ab638a32
RH
4165 * qemu tlbs nor adjusting cached masks.
4166 */
4167static const ARMCPRegInfo ttbcr2_reginfo = {
4168 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4169 .access = PL1_RW, .accessfn = access_tvm_trvm,
4170 .type = ARM_CP_ALIAS,
d102058e 4171 .bank_fieldoffsets = {
cb4a0a34
PM
4172 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4173 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
d102058e 4174 },
ab638a32
RH
4175};
4176
c4241c7d
PM
4177static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4178 uint64_t value)
1047b9d7
PM
4179{
4180 env->cp15.c15_ticonfig = value & 0xe7;
4181 /* The OS_TYPE bit in this register changes the reported CPUID! */
4182 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4183 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4184}
4185
c4241c7d
PM
4186static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4187 uint64_t value)
1047b9d7
PM
4188{
4189 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4190}
4191
c4241c7d
PM
4192static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4193 uint64_t value)
1047b9d7
PM
4194{
4195 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4196 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4197}
4198
c4241c7d
PM
4199static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4200 uint64_t value)
c4804214 4201{
9b37a28c
FR
4202 /*
4203 * On OMAP there are registers indicating the max/min index of dcache lines
c4804214
PM
4204 * containing a dirty line; cache flush operations have to reset these.
4205 */
4206 env->cp15.c15_i_max = 0x000;
4207 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4208}
4209
18032bec
PM
4210static const ARMCPRegInfo omap_cp_reginfo[] = {
4211 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4212 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4213 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4214 .resetvalue = 0, },
1047b9d7
PM
4215 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4216 .access = PL1_RW, .type = ARM_CP_NOP },
4217 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4218 .access = PL1_RW,
4219 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4220 .writefn = omap_ticonfig_write },
4221 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4222 .access = PL1_RW,
4223 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4224 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4225 .access = PL1_RW, .resetvalue = 0xff0,
4226 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4227 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4228 .access = PL1_RW,
4229 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4230 .writefn = omap_threadid_write },
4231 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4232 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4233 .type = ARM_CP_NO_RAW,
1047b9d7 4234 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
9b37a28c
FR
4235 /*
4236 * TODO: Peripheral port remap register:
1047b9d7
PM
4237 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4238 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4239 * when MMU is off.
4240 */
c4804214 4241 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4242 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4243 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4244 .writefn = omap_cachemaint_write },
34f90529
PM
4245 { .name = "C9", .cp = 15, .crn = 9,
4246 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4247 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4248};
4249
c4241c7d
PM
4250static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4251 uint64_t value)
1047b9d7 4252{
c0f4af17 4253 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4254}
4255
4256static const ARMCPRegInfo xscale_cp_reginfo[] = {
4257 { .name = "XSCALE_CPAR",
4258 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4259 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4260 .writefn = xscale_cpar_write, },
2771db27
PM
4261 { .name = "XSCALE_AUXCR",
4262 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4263 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4264 .resetvalue = 0, },
9b37a28c
FR
4265 /*
4266 * XScale specific cache-lockdown: since we have no cache we NOP these
3b771579
PM
4267 * and hope the guest does not really rely on cache behaviour.
4268 */
4269 { .name = "XSCALE_LOCK_ICACHE_LINE",
4270 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4271 .access = PL1_W, .type = ARM_CP_NOP },
4272 { .name = "XSCALE_UNLOCK_ICACHE",
4273 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4274 .access = PL1_W, .type = ARM_CP_NOP },
4275 { .name = "XSCALE_DCACHE_LOCK",
4276 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4277 .access = PL1_RW, .type = ARM_CP_NOP },
4278 { .name = "XSCALE_UNLOCK_DCACHE",
4279 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4280 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4281};
4282
4283static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
9b37a28c
FR
4284 /*
4285 * RAZ/WI the whole crn=15 space, when we don't have a more specific
1047b9d7
PM
4286 * implementation of this implementation-defined space.
4287 * Ideally this should eventually disappear in favour of actually
4288 * implementing the correct behaviour for all cores.
4289 */
4290 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4291 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4292 .access = PL1_RW,
7a0e58fa 4293 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4294 .resetvalue = 0 },
18032bec
PM
4295};
4296
c4804214
PM
4297static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4298 /* Cache status: RAZ because we have no cache so it's always clean */
4299 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4300 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4301 .resetvalue = 0 },
c4804214
PM
4302};
4303
4304static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
a07d9df0 4305 /* We never have a block transfer operation in progress */
c4804214 4306 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4307 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4308 .resetvalue = 0 },
30b05bba
PM
4309 /* The cache ops themselves: these all NOP for QEMU */
4310 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
04215eb1 4311 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4312 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
04215eb1 4313 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4314 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
04215eb1 4315 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4316 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
04215eb1 4317 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4318 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
04215eb1 4319 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4320 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
04215eb1 4321 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
c4804214
PM
4322};
4323
4324static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
9b37a28c
FR
4325 /*
4326 * The cache test-and-clean instructions always return (1 << 30)
c4804214
PM
4327 * to indicate that there are no dirty cache lines.
4328 */
4329 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4330 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4331 .resetvalue = (1 << 30) },
c4804214 4332 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4333 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4334 .resetvalue = (1 << 30) },
c4804214
PM
4335};
4336
34f90529
PM
4337static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4338 /* Ignore ReadBuffer accesses */
4339 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4340 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4341 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4342 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4343};
4344
731de9e6
EI
4345static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4346{
731de9e6 4347 unsigned int cur_el = arm_current_el(env);
731de9e6 4348
e6ef0169 4349 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4350 return env->cp15.vpidr_el2;
4351 }
4352 return raw_read(env, ri);
4353}
4354
06a7e647 4355static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4356{
2fc0cc0e 4357 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4358 uint64_t mpidr = cpu->mp_affinity;
4359
81bdde9d 4360 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4361 mpidr |= (1U << 31);
9b37a28c
FR
4362 /*
4363 * Cores which are uniprocessor (non-coherent)
81bdde9d 4364 * but still implement the MP extensions set
a8e81b31 4365 * bit 30. (For instance, Cortex-R5).
81bdde9d 4366 */
a8e81b31
PC
4367 if (cpu->mp_is_up) {
4368 mpidr |= (1u << 30);
4369 }
81bdde9d 4370 }
c4241c7d 4371 return mpidr;
81bdde9d
PM
4372}
4373
06a7e647
EI
4374static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4375{
f0d574d6 4376 unsigned int cur_el = arm_current_el(env);
f0d574d6 4377
e6ef0169 4378 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4379 return env->cp15.vmpidr_el2;
4380 }
06a7e647
EI
4381 return mpidr_read_val(env);
4382}
4383
7ac681cf 4384static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4385 /* NOP AMAIR0/1 */
b0fe2427
PM
4386 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4387 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4388 .access = PL1_RW, .accessfn = access_tvm_trvm,
4389 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4390 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4391 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4392 .access = PL1_RW, .accessfn = access_tvm_trvm,
4393 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4394 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4395 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4396 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4397 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4398 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4399 .access = PL1_RW, .accessfn = access_tvm_trvm,
4400 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4401 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4402 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4403 .writefn = vmsa_ttbr_write, },
891a2fe7 4404 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4405 .access = PL1_RW, .accessfn = access_tvm_trvm,
4406 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4407 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4408 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4409 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4410};
4411
c4241c7d 4412static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4413{
c4241c7d 4414 return vfp_get_fpcr(env);
b0d2b7d0
PM
4415}
4416
c4241c7d
PM
4417static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4418 uint64_t value)
b0d2b7d0
PM
4419{
4420 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4421}
4422
c4241c7d 4423static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4424{
c4241c7d 4425 return vfp_get_fpsr(env);
b0d2b7d0
PM
4426}
4427
c4241c7d
PM
4428static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4429 uint64_t value)
b0d2b7d0
PM
4430{
4431 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4432}
4433
3f208fd7
PM
4434static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4435 bool isread)
c2b820fe 4436{
aaec1432 4437 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4438 return CP_ACCESS_TRAP;
4439 }
4440 return CP_ACCESS_OK;
4441}
4442
4443static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4444 uint64_t value)
4445{
4446 env->daif = value & PSTATE_DAIF;
4447}
4448
220f508f
RH
4449static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4450{
4451 return env->pstate & PSTATE_PAN;
4452}
4453
4454static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4455 uint64_t value)
4456{
4457 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4458}
4459
4460static const ARMCPRegInfo pan_reginfo = {
4461 .name = "PAN", .state = ARM_CP_STATE_AA64,
4462 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4463 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4464 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4465};
4466
9eeb7a1c
RH
4467static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4468{
4469 return env->pstate & PSTATE_UAO;
4470}
4471
4472static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4473 uint64_t value)
4474{
4475 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4476}
4477
4478static const ARMCPRegInfo uao_reginfo = {
4479 .name = "UAO", .state = ARM_CP_STATE_AA64,
4480 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4481 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4482 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4483};
4484
dc8b1853
RC
4485static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4486{
4487 return env->pstate & PSTATE_DIT;
4488}
4489
4490static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4491 uint64_t value)
4492{
4493 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4494}
4495
4496static const ARMCPRegInfo dit_reginfo = {
4497 .name = "DIT", .state = ARM_CP_STATE_AA64,
4498 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4499 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4500 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4501};
4502
f2f68a78
RC
4503static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4504{
4505 return env->pstate & PSTATE_SSBS;
4506}
4507
4508static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4509 uint64_t value)
4510{
4511 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4512}
4513
4514static const ARMCPRegInfo ssbs_reginfo = {
4515 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4516 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4517 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4518 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4519};
4520
38262d8a
RH
4521static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4522 const ARMCPRegInfo *ri,
4523 bool isread)
8af35c37 4524{
38262d8a
RH
4525 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4526 switch (arm_current_el(env)) {
4527 case 0:
4528 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4529 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4530 return CP_ACCESS_TRAP;
4531 }
4532 /* fall through */
4533 case 1:
4534 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4535 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4536 return CP_ACCESS_TRAP_EL2;
4537 }
4538 break;
8af35c37
PM
4539 }
4540 return CP_ACCESS_OK;
4541}
4542
2d3ce4c6 4543static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
1bed4d2e 4544{
38262d8a 4545 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4546 switch (arm_current_el(env)) {
4547 case 0:
4548 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4549 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4550 return CP_ACCESS_TRAP;
4551 }
4552 /* fall through */
4553 case 1:
2d3ce4c6
PM
4554 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4555 if (arm_hcr_el2_eff(env) & hcrflags) {
1bed4d2e
RH
4556 return CP_ACCESS_TRAP_EL2;
4557 }
4558 break;
4559 }
4560 return CP_ACCESS_OK;
4561}
4562
2d3ce4c6
PM
4563static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4564 bool isread)
4565{
4566 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4567}
4568
4569static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4570 bool isread)
4571{
4572 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4573}
4574
9b37a28c
FR
4575/*
4576 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
dbb1fb27
AB
4577 * Page D4-1736 (DDI0487A.b)
4578 */
4579
b7e0730d
RH
4580static int vae1_tlbmask(CPUARMState *env)
4581{
e04a5752 4582 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4583 uint16_t mask;
e04a5752
RDC
4584
4585 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4586 mask = ARMMMUIdxBit_E20_2 |
4587 ARMMMUIdxBit_E20_2_PAN |
4588 ARMMMUIdxBit_E20_0;
b7e0730d 4589 } else {
bc944d3a 4590 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4591 ARMMMUIdxBit_E10_1_PAN |
4592 ARMMMUIdxBit_E10_0;
b7e0730d 4593 }
bc944d3a 4594 return mask;
b7e0730d
RH
4595}
4596
ea04dce7
RH
4597/* Return 56 if TBI is enabled, 64 otherwise. */
4598static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4599 uint64_t addr)
4600{
c1547bba 4601 uint64_t tcr = regime_tcr(env, mmu_idx);
ea04dce7
RH
4602 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4603 int select = extract64(addr, 55, 1);
4604
4605 return (tbi >> select) & 1 ? 56 : 64;
4606}
4607
4608static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4609{
b6ad6062 4610 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4611 ARMMMUIdx mmu_idx;
4612
4613 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4614 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4615 mmu_idx = ARMMMUIdx_E20_0;
4616 } else {
4617 mmu_idx = ARMMMUIdx_E10_0;
4618 }
b6ad6062 4619
ea04dce7
RH
4620 return tlbbits_for_regime(env, mmu_idx, addr);
4621}
4622
fd3ed969
PM
4623static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4624 uint64_t value)
168aa23b 4625{
29a0af61 4626 CPUState *cs = env_cpu(env);
b7e0730d 4627 int mask = vae1_tlbmask(env);
dbb1fb27 4628
b7e0730d 4629 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4630}
4631
b4ab8ce9
PM
4632static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4633 uint64_t value)
4634{
29a0af61 4635 CPUState *cs = env_cpu(env);
b7e0730d 4636 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4637
4638 if (tlb_force_broadcast(env)) {
527db2be
RH
4639 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4640 } else {
4641 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4642 }
b4ab8ce9
PM
4643}
4644
85d0dc9f
RH
4645static int e2_tlbmask(CPUARMState *env)
4646{
d902ae75
RH
4647 return (ARMMMUIdxBit_E20_0 |
4648 ARMMMUIdxBit_E20_2 |
4649 ARMMMUIdxBit_E20_2_PAN |
4650 ARMMMUIdxBit_E2);
85d0dc9f
RH
4651}
4652
90c19cdf
RH
4653static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4654 uint64_t value)
4655{
4656 CPUState *cs = env_cpu(env);
4657 int mask = alle1_tlbmask(env);
4658
4659 tlb_flush_by_mmuidx(cs, mask);
4660}
4661
fd3ed969 4662static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4663 uint64_t value)
4664{
85d0dc9f
RH
4665 CPUState *cs = env_cpu(env);
4666 int mask = e2_tlbmask(env);
fd3ed969 4667
85d0dc9f 4668 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4669}
4670
43efaa33
PM
4671static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4672 uint64_t value)
4673{
2fc0cc0e 4674 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4675 CPUState *cs = CPU(cpu);
4676
d902ae75 4677 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4678}
4679
fd3ed969
PM
4680static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4681 uint64_t value)
4682{
29a0af61 4683 CPUState *cs = env_cpu(env);
90c19cdf
RH
4684 int mask = alle1_tlbmask(env);
4685
4686 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4687}
4688
2bfb9d75
PM
4689static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4690 uint64_t value)
4691{
29a0af61 4692 CPUState *cs = env_cpu(env);
85d0dc9f 4693 int mask = e2_tlbmask(env);
2bfb9d75 4694
85d0dc9f 4695 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4696}
4697
43efaa33
PM
4698static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4699 uint64_t value)
4700{
29a0af61 4701 CPUState *cs = env_cpu(env);
43efaa33 4702
d902ae75 4703 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4704}
4705
fd3ed969
PM
4706static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4707 uint64_t value)
fa439fc5 4708{
9b37a28c
FR
4709 /*
4710 * Invalidate by VA, EL2
fd3ed969
PM
4711 * Currently handles both VAE2 and VALE2, since we don't support
4712 * flush-last-level-only.
4713 */
85d0dc9f
RH
4714 CPUState *cs = env_cpu(env);
4715 int mask = e2_tlbmask(env);
fd3ed969
PM
4716 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4717
85d0dc9f 4718 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4719}
4720
43efaa33
PM
4721static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4722 uint64_t value)
4723{
9b37a28c
FR
4724 /*
4725 * Invalidate by VA, EL3
43efaa33
PM
4726 * Currently handles both VAE3 and VALE3, since we don't support
4727 * flush-last-level-only.
4728 */
2fc0cc0e 4729 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4730 CPUState *cs = CPU(cpu);
4731 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4732
d902ae75 4733 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
43efaa33
PM
4734}
4735
fd3ed969
PM
4736static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4737 uint64_t value)
4738{
90c19cdf
RH
4739 CPUState *cs = env_cpu(env);
4740 int mask = vae1_tlbmask(env);
fa439fc5 4741 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4742 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4743
ea04dce7 4744 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4745}
4746
b4ab8ce9
PM
4747static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4748 uint64_t value)
4749{
9b37a28c
FR
4750 /*
4751 * Invalidate by VA, EL1&0 (AArch64 version).
b4ab8ce9
PM
4752 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4753 * since we don't support flush-for-specific-ASID-only or
4754 * flush-last-level-only.
4755 */
90c19cdf
RH
4756 CPUState *cs = env_cpu(env);
4757 int mask = vae1_tlbmask(env);
b4ab8ce9 4758 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4759 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4760
4761 if (tlb_force_broadcast(env)) {
ea04dce7 4762 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4763 } else {
ea04dce7 4764 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4765 }
b4ab8ce9
PM
4766}
4767
fd3ed969
PM
4768static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4769 uint64_t value)
fa439fc5 4770{
29a0af61 4771 CPUState *cs = env_cpu(env);
fd3ed969 4772 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4773 int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
fa439fc5 4774
d902ae75
RH
4775 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4776 ARMMMUIdxBit_E2, bits);
fa439fc5
PM
4777}
4778
43efaa33
PM
4779static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4780 uint64_t value)
4781{
29a0af61 4782 CPUState *cs = env_cpu(env);
43efaa33 4783 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4784 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
43efaa33 4785
ea04dce7 4786 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
d902ae75 4787 ARMMMUIdxBit_E3, bits);
43efaa33
PM
4788}
4789
575a94af
RH
4790static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4791{
4792 /*
4793 * The MSB of value is the NS field, which only applies if SEL2
4794 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4795 */
4796 return (value >= 0
4797 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4798 && arm_is_secure_below_el3(env)
4799 ? ARMMMUIdxBit_Stage2_S
4800 : ARMMMUIdxBit_Stage2);
4801}
4802
4803static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4804 uint64_t value)
4805{
4806 CPUState *cs = env_cpu(env);
4807 int mask = ipas2e1_tlbmask(env, value);
4808 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4809
4810 if (tlb_force_broadcast(env)) {
4811 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4812 } else {
4813 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4814 }
4815}
4816
4817static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4818 uint64_t value)
4819{
4820 CPUState *cs = env_cpu(env);
4821 int mask = ipas2e1_tlbmask(env, value);
4822 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4823
4824 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4825}
4826
84940ed8 4827#ifdef TARGET_AARCH64
ab1cdb47
RH
4828typedef struct {
4829 uint64_t base;
84940ed8 4830 uint64_t length;
ab1cdb47
RH
4831} TLBIRange;
4832
3c003f70
PM
4833static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4834{
4835 /*
4836 * Note that the TLBI range TG field encoding differs from both
4837 * TG0 and TG1 encodings.
4838 */
4839 switch (tg) {
4840 case 1:
4841 return Gran4K;
4842 case 2:
4843 return Gran16K;
4844 case 3:
4845 return Gran64K;
4846 default:
4847 return GranInvalid;
4848 }
4849}
4850
ab1cdb47
RH
4851static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4852 uint64_t value)
4853{
4854 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4855 /* Extract one bit to represent the va selector in use. */
4856 uint64_t select = sextract64(value, 36, 1);
4857 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4858 TLBIRange ret = { };
3c003f70 4859 ARMGranuleSize gran;
84940ed8 4860
84940ed8 4861 page_size_granule = extract64(value, 46, 2);
3c003f70 4862 gran = tlbi_range_tg_to_gran_size(page_size_granule);
84940ed8 4863
3974ff93 4864 /* The granule encoded in value must match the granule in use. */
3c003f70 4865 if (gran != param.gran) {
3974ff93 4866 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4867 page_size_granule);
ab1cdb47 4868 return ret;
84940ed8
RC
4869 }
4870
3c003f70 4871 page_shift = arm_granule_bits(gran);
ab1cdb47
RH
4872 num = extract64(value, 39, 5);
4873 scale = extract64(value, 44, 2);
84940ed8 4874 exponent = (5 * scale) + 1;
84940ed8 4875
ab1cdb47 4876 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4877
3974ff93 4878 if (param.select) {
d976de21 4879 ret.base = sextract64(value, 0, 37);
84940ed8 4880 } else {
d976de21 4881 ret.base = extract64(value, 0, 37);
84940ed8 4882 }
ef56c242
RH
4883 if (param.ds) {
4884 /*
4885 * With DS=1, BaseADDR is always shifted 16 so that it is able
4886 * to address all 52 va bits. The input address is perforce
4887 * aligned on a 64k boundary regardless of translation granule.
4888 */
4889 page_shift = 16;
4890 }
d976de21 4891 ret.base <<= page_shift;
84940ed8 4892
ab1cdb47 4893 return ret;
84940ed8
RC
4894}
4895
4896static void do_rvae_write(CPUARMState *env, uint64_t value,
4897 int idxmap, bool synced)
4898{
4899 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4900 TLBIRange range;
84940ed8
RC
4901 int bits;
4902
ab1cdb47
RH
4903 range = tlbi_aa64_get_range(env, one_idx, value);
4904 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4905
4906 if (synced) {
4907 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4908 range.base,
4909 range.length,
84940ed8
RC
4910 idxmap,
4911 bits);
4912 } else {
ab1cdb47
RH
4913 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4914 range.length, idxmap, bits);
84940ed8
RC
4915 }
4916}
4917
4918static void tlbi_aa64_rvae1_write(CPUARMState *env,
4919 const ARMCPRegInfo *ri,
4920 uint64_t value)
4921{
4922 /*
4923 * Invalidate by VA range, EL1&0.
4924 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4925 * since we don't support flush-for-specific-ASID-only or
4926 * flush-last-level-only.
4927 */
4928
4929 do_rvae_write(env, value, vae1_tlbmask(env),
4930 tlb_force_broadcast(env));
4931}
4932
4933static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4934 const ARMCPRegInfo *ri,
4935 uint64_t value)
4936{
4937 /*
4938 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4939 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4940 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4941 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4942 * shareable specific flushes.
4943 */
4944
4945 do_rvae_write(env, value, vae1_tlbmask(env), true);
4946}
4947
4948static int vae2_tlbmask(CPUARMState *env)
4949{
d902ae75 4950 return ARMMMUIdxBit_E2;
84940ed8
RC
4951}
4952
4953static void tlbi_aa64_rvae2_write(CPUARMState *env,
4954 const ARMCPRegInfo *ri,
4955 uint64_t value)
4956{
4957 /*
4958 * Invalidate by VA range, EL2.
4959 * Currently handles all of RVAE2 and RVALE2,
4960 * since we don't support flush-for-specific-ASID-only or
4961 * flush-last-level-only.
4962 */
4963
4964 do_rvae_write(env, value, vae2_tlbmask(env),
4965 tlb_force_broadcast(env));
4966
4967
4968}
4969
4970static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4971 const ARMCPRegInfo *ri,
4972 uint64_t value)
4973{
4974 /*
4975 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4976 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4977 * since we don't support flush-for-specific-ASID-only,
4978 * flush-last-level-only or inner/outer shareable specific flushes.
4979 */
4980
4981 do_rvae_write(env, value, vae2_tlbmask(env), true);
4982
4983}
4984
4985static void tlbi_aa64_rvae3_write(CPUARMState *env,
4986 const ARMCPRegInfo *ri,
4987 uint64_t value)
4988{
4989 /*
4990 * Invalidate by VA range, EL3.
4991 * Currently handles all of RVAE3 and RVALE3,
4992 * since we don't support flush-for-specific-ASID-only or
4993 * flush-last-level-only.
4994 */
4995
d902ae75 4996 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
84940ed8
RC
4997}
4998
4999static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5000 const ARMCPRegInfo *ri,
5001 uint64_t value)
5002{
5003 /*
5004 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5005 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5006 * since we don't support flush-for-specific-ASID-only,
5007 * flush-last-level-only or inner/outer specific flushes.
5008 */
5009
d902ae75 5010 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
84940ed8 5011}
575a94af
RH
5012
5013static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5014 uint64_t value)
5015{
5016 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5017 tlb_force_broadcast(env));
5018}
5019
5020static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5021 const ARMCPRegInfo *ri,
5022 uint64_t value)
5023{
5024 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5025}
84940ed8
RC
5026#endif
5027
3f208fd7
PM
5028static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5029 bool isread)
aca3f40b 5030{
4351cb72
RH
5031 int cur_el = arm_current_el(env);
5032
5033 if (cur_el < 2) {
5034 uint64_t hcr = arm_hcr_el2_eff(env);
5035
5036 if (cur_el == 0) {
5037 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5038 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5039 return CP_ACCESS_TRAP_EL2;
5040 }
5041 } else {
5042 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5043 return CP_ACCESS_TRAP;
5044 }
5045 if (hcr & HCR_TDZ) {
5046 return CP_ACCESS_TRAP_EL2;
5047 }
5048 }
5049 } else if (hcr & HCR_TDZ) {
5050 return CP_ACCESS_TRAP_EL2;
5051 }
aca3f40b
PM
5052 }
5053 return CP_ACCESS_OK;
5054}
5055
5056static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5057{
2fc0cc0e 5058 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
5059 int dzp_bit = 1 << 4;
5060
5061 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 5062 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
5063 dzp_bit = 0;
5064 }
5065 return cpu->dcz_blocksize | dzp_bit;
5066}
5067
3f208fd7
PM
5068static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5069 bool isread)
f502cfc2 5070{
cdcf1405 5071 if (!(env->pstate & PSTATE_SP)) {
9b37a28c
FR
5072 /*
5073 * Access to SP_EL0 is undefined if it's being used as
f502cfc2
PM
5074 * the stack pointer.
5075 */
5076 return CP_ACCESS_TRAP_UNCATEGORIZED;
5077 }
5078 return CP_ACCESS_OK;
5079}
5080
5081static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5082{
5083 return env->pstate & PSTATE_SP;
5084}
5085
5086static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5087{
5088 update_spsel(env, val);
5089}
5090
137feaa9
FA
5091static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5092 uint64_t value)
5093{
2fc0cc0e 5094 ARMCPU *cpu = env_archcpu(env);
137feaa9 5095
f00faf13
RH
5096 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5097 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5098 value &= ~SCTLR_M;
5099 }
5100
5101 /* ??? Lots of these bits are not implemented. */
5102
5103 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5104 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5105 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5106 } else {
5107 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5108 SCTLR_ATA0 | SCTLR_ATA);
5109 }
5110 }
5111
137feaa9 5112 if (raw_read(env, ri) == value) {
9b37a28c
FR
5113 /*
5114 * Skip the TLB flush if nothing actually changed; Linux likes
137feaa9
FA
5115 * to do a lot of pointless SCTLR writes.
5116 */
5117 return;
5118 }
5119
5120 raw_write(env, ri, value);
f00faf13 5121
137feaa9 5122 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 5123 tlb_flush(CPU(cpu));
2e5dcf36
RH
5124
5125 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
5126 /*
5127 * Normally we would always end the TB on an SCTLR write; see the
5128 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5129 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5130 * of hflags from the translator, so do it here.
5131 */
5132 arm_rebuild_hflags(env);
5133 }
137feaa9
FA
5134}
5135
80d2b43b
PM
5136static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5137 uint64_t value)
a8d64e73 5138{
01765386
PM
5139 /*
5140 * Some MDCR_EL3 bits affect whether PMU counters are running:
5141 * if we are trying to change any of those then we must
5142 * bracket this update with PMU start/finish calls.
5143 */
5144 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5145
5146 if (pmu_op) {
5147 pmu_op_start(env);
5148 }
80d2b43b 5149 env->cp15.mdcr_el3 = value;
01765386
PM
5150 if (pmu_op) {
5151 pmu_op_finish(env);
5152 }
5153}
5154
80d2b43b
PM
5155static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5156 uint64_t value)
5157{
5158 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5159 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5160}
5161
01765386
PM
5162static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5163 uint64_t value)
5164{
5165 /*
5166 * Some MDCR_EL2 bits affect whether PMU counters are running:
5167 * if we are trying to change any of those then we must
5168 * bracket this update with PMU start/finish calls.
5169 */
5170 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5171
5172 if (pmu_op) {
5173 pmu_op_start(env);
5174 }
5175 env->cp15.mdcr_el2 = value;
5176 if (pmu_op) {
5177 pmu_op_finish(env);
5178 }
a8d64e73
PM
5179}
5180
b0d2b7d0 5181static const ARMCPRegInfo v8_cp_reginfo[] = {
9b37a28c
FR
5182 /*
5183 * Minimal set of EL0-visible registers. This will need to be expanded
b0d2b7d0
PM
5184 * significantly for system emulation of AArch64 CPUs.
5185 */
5186 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5187 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5188 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
5189 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5190 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 5191 .type = ARM_CP_NO_RAW,
c2b820fe
PM
5192 .access = PL0_RW, .accessfn = aa64_daif_access,
5193 .fieldoffset = offsetof(CPUARMState, daif),
5194 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
5195 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5196 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 5197 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5198 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
5199 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5200 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 5201 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5202 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
5203 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5204 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 5205 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
5206 .readfn = aa64_dczid_read },
5207 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5208 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5209 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5210#ifndef CONFIG_USER_ONLY
5211 /* Avoid overhead of an access check that always passes in user-mode */
5212 .accessfn = aa64_zva_access,
5213#endif
5214 },
0eef9d98
PM
5215 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5216 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5217 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
5218 /* Cache ops: all NOPs since we don't emulate caches */
5219 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5220 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5221 .access = PL1_W, .type = ARM_CP_NOP,
2d3ce4c6 5222 .accessfn = access_ticab },
8af35c37
PM
5223 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5224 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5225 .access = PL1_W, .type = ARM_CP_NOP,
2d3ce4c6 5226 .accessfn = access_tocu },
8af35c37
PM
5227 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5228 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5229 .access = PL0_W, .type = ARM_CP_NOP,
2d3ce4c6 5230 .accessfn = access_tocu },
8af35c37
PM
5231 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5232 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
5233 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5234 .type = ARM_CP_NOP },
8af35c37
PM
5235 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5236 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5237 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5238 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5239 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5240 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 5241 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5242 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5243 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5244 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5245 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5246 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5247 .access = PL0_W, .type = ARM_CP_NOP,
2d3ce4c6 5248 .accessfn = access_tocu },
8af35c37
PM
5249 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5250 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5251 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 5252 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5253 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5254 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5255 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
5256 /* TLBI operations */
5257 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5258 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 5259 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5260 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5261 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5262 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 5263 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5264 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5265 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5266 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 5267 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5268 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5269 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5270 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 5271 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5272 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5273 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5274 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5275 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5276 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5277 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5278 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5279 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5280 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5281 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5282 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 5283 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5284 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5285 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5286 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 5287 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5288 .writefn = tlbi_aa64_vae1_write },
168aa23b 5289 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5290 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 5291 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5292 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5293 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5294 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 5295 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5296 .writefn = tlbi_aa64_vae1_write },
168aa23b 5297 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5298 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 5299 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5300 .writefn = tlbi_aa64_vae1_write },
168aa23b 5301 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5302 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 5303 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5304 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
5305 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5306 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5307 .access = PL2_W, .type = ARM_CP_NO_RAW,
5308 .writefn = tlbi_aa64_ipas2e1is_write },
cea66e91
PM
5309 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5310 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5311 .access = PL2_W, .type = ARM_CP_NO_RAW,
5312 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
5313 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5314 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5315 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5316 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
5317 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5318 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5319 .access = PL2_W, .type = ARM_CP_NO_RAW,
5320 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
5321 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5322 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5323 .access = PL2_W, .type = ARM_CP_NO_RAW,
5324 .writefn = tlbi_aa64_ipas2e1_write },
cea66e91
PM
5325 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5326 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5327 .access = PL2_W, .type = ARM_CP_NO_RAW,
5328 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
5329 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5330 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5331 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5332 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
5333 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5334 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5335 .access = PL2_W, .type = ARM_CP_NO_RAW,
5336 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
5337#ifndef CONFIG_USER_ONLY
5338 /* 64 bit address translation operations */
5339 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5340 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5341 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5342 .writefn = ats_write64 },
19525524
PM
5343 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5344 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5345 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5346 .writefn = ats_write64 },
19525524
PM
5347 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5348 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
5349 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5350 .writefn = ats_write64 },
19525524
PM
5351 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5352 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
5353 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5354 .writefn = ats_write64 },
2a47df95 5355 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 5356 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
5357 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5358 .writefn = ats_write64 },
2a47df95 5359 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 5360 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
5361 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5362 .writefn = ats_write64 },
2a47df95 5363 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 5364 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
5365 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5366 .writefn = ats_write64 },
2a47df95 5367 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 5368 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
5369 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5370 .writefn = ats_write64 },
2a47df95
PM
5371 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5372 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5373 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5374 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5375 .writefn = ats_write64 },
2a47df95
PM
5376 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5377 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5378 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5379 .writefn = ats_write64 },
c96fc9b5
EI
5380 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5381 .type = ARM_CP_ALIAS,
5382 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5383 .access = PL1_RW, .resetvalue = 0,
5384 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5385 .writefn = par_write },
19525524 5386#endif
995939a6 5387 /* TLB invalidate last level of translation table walk */
9449fdf6 5388 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5389 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 5390 .writefn = tlbimva_is_write },
9449fdf6 5391 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5392 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 5393 .writefn = tlbimvaa_is_write },
9449fdf6 5394 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
5395 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5396 .writefn = tlbimva_write },
9449fdf6 5397 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5398 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5399 .writefn = tlbimvaa_write },
541ef8c2
SS
5400 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5401 .type = ARM_CP_NO_RAW, .access = PL2_W,
5402 .writefn = tlbimva_hyp_write },
5403 { .name = "TLBIMVALHIS",
5404 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5405 .type = ARM_CP_NO_RAW, .access = PL2_W,
5406 .writefn = tlbimva_hyp_is_write },
5407 { .name = "TLBIIPAS2",
5408 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5409 .type = ARM_CP_NO_RAW, .access = PL2_W,
5410 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5411 { .name = "TLBIIPAS2IS",
5412 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5413 .type = ARM_CP_NO_RAW, .access = PL2_W,
5414 .writefn = tlbiipas2is_hyp_write },
541ef8c2
SS
5415 { .name = "TLBIIPAS2L",
5416 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5417 .type = ARM_CP_NO_RAW, .access = PL2_W,
5418 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5419 { .name = "TLBIIPAS2LIS",
5420 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5421 .type = ARM_CP_NO_RAW, .access = PL2_W,
5422 .writefn = tlbiipas2is_hyp_write },
9449fdf6
PM
5423 /* 32 bit cache operations */
5424 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2d3ce4c6 5425 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
9449fdf6
PM
5426 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5427 .type = ARM_CP_NOP, .access = PL1_W },
5428 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2d3ce4c6 5429 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5430 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2d3ce4c6 5431 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6
PM
5432 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5433 .type = ARM_CP_NOP, .access = PL1_W },
5434 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5435 .type = ARM_CP_NOP, .access = PL1_W },
5436 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5437 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5438 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5439 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5440 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5441 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5442 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5443 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5444 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2d3ce4c6 5445 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5446 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5447 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5448 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5449 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5450 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5451 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5452 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5453 .writefn = dacr_write, .raw_writefn = raw_write,
5454 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5455 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5456 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5457 .type = ARM_CP_ALIAS,
a0618a19 5458 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5459 .access = PL1_RW,
5460 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5461 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5462 .type = ARM_CP_ALIAS,
a65f1de9 5463 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5464 .access = PL1_RW,
5465 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
9b37a28c
FR
5466 /*
5467 * We rely on the access checks not allowing the guest to write to the
f502cfc2
PM
5468 * state field when SPSel indicates that it's being used as the stack
5469 * pointer.
5470 */
5471 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5472 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5473 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5474 .type = ARM_CP_ALIAS,
f502cfc2 5475 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5476 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5477 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
beeec926 5478 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
884b4dee 5479 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5480 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5481 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5482 .type = ARM_CP_NO_RAW,
f502cfc2 5483 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5484 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5485 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5486 .access = PL2_RW,
5487 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5488 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5489 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5490 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5491 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5492 .writefn = dacr_write, .raw_writefn = raw_write,
5493 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5494 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5495 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5496 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5497 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5498 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5499 .type = ARM_CP_ALIAS,
5500 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5501 .access = PL2_RW,
5502 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5503 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5504 .type = ARM_CP_ALIAS,
5505 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5506 .access = PL2_RW,
5507 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5508 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5509 .type = ARM_CP_ALIAS,
5510 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5511 .access = PL2_RW,
5512 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5513 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5514 .type = ARM_CP_ALIAS,
5515 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5516 .access = PL2_RW,
5517 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73 5518 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
80d2b43b 5519 .type = ARM_CP_IO,
a8d64e73
PM
5520 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5521 .resetvalue = 0,
80d2b43b
PM
5522 .access = PL3_RW,
5523 .writefn = mdcr_el3_write,
5524 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
7f4fbfb5 5525 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
a8d64e73
PM
5526 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5527 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5528 .writefn = sdcr_write,
5529 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5530};
5531
d1fb4da2 5532static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5533{
2fc0cc0e 5534 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5535
5536 if (arm_feature(env, ARM_FEATURE_V8)) {
5537 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5538 } else {
5539 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5540 }
f149e3e8
EI
5541
5542 if (arm_feature(env, ARM_FEATURE_EL3)) {
5543 valid_mask &= ~HCR_HCD;
77077a83 5544 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
9b37a28c
FR
5545 /*
5546 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
77077a83
JK
5547 * However, if we're using the SMC PSCI conduit then QEMU is
5548 * effectively acting like EL3 firmware and so the guest at
5549 * EL2 should retain the ability to prevent EL1 from being
5550 * able to make SMC calls into the ersatz firmware, so in
5551 * that case HCR.TSC should be read/write.
5552 */
f149e3e8
EI
5553 valid_mask &= ~HCR_TSC;
5554 }
d1fb4da2
RH
5555
5556 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5557 if (cpu_isar_feature(aa64_vh, cpu)) {
5558 valid_mask |= HCR_E2H;
5559 }
da3d8b13
RH
5560 if (cpu_isar_feature(aa64_ras, cpu)) {
5561 valid_mask |= HCR_TERR | HCR_TEA;
5562 }
d1fb4da2
RH
5563 if (cpu_isar_feature(aa64_lor, cpu)) {
5564 valid_mask |= HCR_TLOR;
5565 }
5566 if (cpu_isar_feature(aa64_pauth, cpu)) {
5567 valid_mask |= HCR_API | HCR_APK;
5568 }
8ddb300b
RH
5569 if (cpu_isar_feature(aa64_mte, cpu)) {
5570 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5571 }
7cb1e618
RH
5572 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5573 valid_mask |= HCR_ENSCXT;
5574 }
8c7e17ef
PM
5575 if (cpu_isar_feature(aa64_fwb, cpu)) {
5576 valid_mask |= HCR_FWB;
5577 }
ef682cdb 5578 }
f149e3e8 5579
d2fd9313
PM
5580 if (cpu_isar_feature(any_evt, cpu)) {
5581 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5582 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5583 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5584 }
5585
f149e3e8
EI
5586 /* Clear RES0 bits. */
5587 value &= valid_mask;
5588
8ddb300b
RH
5589 /*
5590 * These bits change the MMU setup:
f149e3e8
EI
5591 * HCR_VM enables stage 2 translation
5592 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5593 * HCR_DC disables stage1 and enables stage2 translation
5594 * HCR_DCT enables tagging on (disabled) stage1 translation
8c7e17ef 5595 * HCR_FWB changes the interpretation of stage2 descriptor bits
f149e3e8 5596 */
8c7e17ef
PM
5597 if ((env->cp15.hcr_el2 ^ value) &
5598 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
d10eb08f 5599 tlb_flush(CPU(cpu));
f149e3e8 5600 }
ce4afed8 5601 env->cp15.hcr_el2 = value;
89430fc6
PM
5602
5603 /*
5604 * Updates to VI and VF require us to update the status of
5605 * virtual interrupts, which are the logical OR of these bits
5606 * and the state of the input lines from the GIC. (This requires
5607 * that we have the iothread lock, which is done by marking the
5608 * reginfo structs as ARM_CP_IO.)
5609 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5610 * possible for it to be taken immediately, because VIRQ and
5611 * VFIQ are masked unless running at EL0 or EL1, and HCR
5612 * can only be written at EL2.
5613 */
5614 g_assert(qemu_mutex_iothread_locked());
5615 arm_cpu_update_virq(cpu);
5616 arm_cpu_update_vfiq(cpu);
3c29632f 5617 arm_cpu_update_vserr(cpu);
ce4afed8
PM
5618}
5619
d1fb4da2
RH
5620static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5621{
5622 do_hcr_write(env, value, 0);
5623}
5624
ce4afed8
PM
5625static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5626 uint64_t value)
5627{
5628 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5629 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5630 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5631}
5632
5633static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5634 uint64_t value)
5635{
5636 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5637 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5638 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5639}
5640
f7778444 5641/*
b74c0443 5642 * Return the effective value of HCR_EL2, at the given security state.
f7778444
RH
5643 * Bits that are not included here:
5644 * RW (read from SCR_EL3.RW as needed)
5645 */
b74c0443 5646uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
f7778444
RH
5647{
5648 uint64_t ret = env->cp15.hcr_el2;
5649
b74c0443 5650 if (!arm_is_el2_enabled_secstate(env, secure)) {
f7778444
RH
5651 /*
5652 * "This register has no effect if EL2 is not enabled in the
5653 * current Security state". This is ARMv8.4-SecEL2 speak for
5654 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5655 *
5656 * Prior to that, the language was "In an implementation that
5657 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5658 * as if this field is 0 for all purposes other than a direct
5659 * read or write access of HCR_EL2". With lots of enumeration
5660 * on a per-field basis. In current QEMU, this is condition
5661 * is arm_is_secure_below_el3.
5662 *
5663 * Since the v8.4 language applies to the entire register, and
5664 * appears to be backward compatible, use that.
5665 */
4990e1d3
RH
5666 return 0;
5667 }
5668
5669 /*
5670 * For a cpu that supports both aarch64 and aarch32, we can set bits
5671 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5672 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5673 */
5674 if (!arm_el_is_aa64(env, 2)) {
5675 uint64_t aa32_valid;
5676
5677 /*
5678 * These bits are up-to-date as of ARMv8.6.
5679 * For HCR, it's easiest to list just the 2 bits that are invalid.
5680 * For HCR2, list those that are valid.
5681 */
5682 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5683 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5684 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5685 ret &= aa32_valid;
5686 }
5687
5688 if (ret & HCR_TGE) {
5689 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5690 if (ret & HCR_E2H) {
5691 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5692 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5693 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5694 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5695 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5696 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5697 } else {
5698 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5699 }
5700 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5701 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5702 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5703 HCR_TLOR);
5704 }
5705
5706 return ret;
5707}
5708
b74c0443
RH
5709uint64_t arm_hcr_el2_eff(CPUARMState *env)
5710{
5711 return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5712}
5713
19668718
RH
5714/*
5715 * Corresponds to ARM pseudocode function ELIsInHost().
5716 */
5717bool el_is_in_host(CPUARMState *env, int el)
5718{
5719 uint64_t mask;
5720
5721 /*
5722 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5723 * Perform the simplest bit tests first, and validate EL2 afterward.
5724 */
5725 if (el & 1) {
5726 return false; /* EL1 or EL3 */
5727 }
5728
5729 /*
5730 * Note that hcr_write() checks isar_feature_aa64_vh(),
5731 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5732 */
5733 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5734 if ((env->cp15.hcr_el2 & mask) != mask) {
5735 return false;
5736 }
5737
5738 /* TGE and/or E2H set: double check those bits are currently legal. */
5739 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5740}
5741
5814d587
RH
5742static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5743 uint64_t value)
5744{
5745 uint64_t valid_mask = 0;
5746
5747 /* No features adding bits to HCRX are implemented. */
5748
5749 /* Clear RES0 bits. */
5750 env->cp15.hcrx_el2 = value & valid_mask;
5751}
5752
5753static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5754 bool isread)
5755{
5756 if (arm_current_el(env) < 3
5757 && arm_feature(env, ARM_FEATURE_EL3)
5758 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5759 return CP_ACCESS_TRAP_EL3;
5760 }
5761 return CP_ACCESS_OK;
5762}
5763
5764static const ARMCPRegInfo hcrx_el2_reginfo = {
5765 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5766 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5767 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5768 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5769};
5770
5771/* Return the effective value of HCRX_EL2. */
5772uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5773{
5774 /*
5775 * The bits in this register behave as 0 for all purposes other than
5776 * direct reads of the register if:
5777 * - EL2 is not enabled in the current security state,
5778 * - SCR_EL3.HXEn is 0.
5779 */
5780 if (!arm_is_el2_enabled(env)
5781 || (arm_feature(env, ARM_FEATURE_EL3)
5782 && !(env->cp15.scr_el3 & SCR_HXEN))) {
5783 return 0;
5784 }
5785 return env->cp15.hcrx_el2;
5786}
5787
fc1120a7
PM
5788static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5789 uint64_t value)
5790{
5791 /*
5792 * For A-profile AArch32 EL3, if NSACR.CP10
5793 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5794 */
5795 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5796 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
5797 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5798 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
fc1120a7
PM
5799 }
5800 env->cp15.cptr_el[2] = value;
5801}
5802
5803static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5804{
5805 /*
5806 * For A-profile AArch32 EL3, if NSACR.CP10
5807 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5808 */
5809 uint64_t value = env->cp15.cptr_el[2];
5810
5811 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5812 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 5813 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
fc1120a7
PM
5814 }
5815 return value;
5816}
5817
4771cd01 5818static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5819 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5820 .type = ARM_CP_IO,
f149e3e8
EI
5821 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5822 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5823 .writefn = hcr_write },
ce4afed8 5824 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5825 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5826 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5827 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5828 .writefn = hcr_writelow },
831a2fca
PM
5829 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5830 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5831 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5832 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5833 .type = ARM_CP_ALIAS,
3b685ba7
EI
5834 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5835 .access = PL2_RW,
5836 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5837 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5838 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5839 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5840 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5841 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5842 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5843 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5844 .type = ARM_CP_ALIAS,
5845 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5846 .access = PL2_RW,
5847 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5848 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5849 .type = ARM_CP_ALIAS,
3b685ba7 5850 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5851 .access = PL2_RW,
5852 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5853 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5854 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5855 .access = PL2_RW, .writefn = vbar_write,
5856 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5857 .resetvalue = 0 },
884b4dee
GB
5858 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5859 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5860 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5861 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5862 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5863 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5864 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5865 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5866 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5867 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5868 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5869 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5870 .resetvalue = 0 },
5871 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5872 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5873 .access = PL2_RW, .type = ARM_CP_ALIAS,
5874 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5875 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5876 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5877 .access = PL2_RW, .type = ARM_CP_CONST,
5878 .resetvalue = 0 },
5879 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5880 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5881 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5882 .access = PL2_RW, .type = ARM_CP_CONST,
5883 .resetvalue = 0 },
37cd6c24
PM
5884 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5885 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5886 .access = PL2_RW, .type = ARM_CP_CONST,
5887 .resetvalue = 0 },
5888 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5889 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5890 .access = PL2_RW, .type = ARM_CP_CONST,
5891 .resetvalue = 0 },
06ec4c8c
EI
5892 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5893 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933 5894 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
06ec4c8c 5895 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5896 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5897 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5898 .type = ARM_CP_ALIAS,
68e9c2fe 5899 .access = PL2_RW, .accessfn = access_el3_aa32ns,
afbb181c 5900 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
68e9c2fe
EI
5901 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5902 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5903 .access = PL2_RW,
988cc190 5904 /* no .writefn needed as this can't cause an ASID change */
68e9c2fe 5905 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5906 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5907 .cp = 15, .opc1 = 6, .crm = 2,
5908 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5909 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5910 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5911 .writefn = vttbr_write },
5912 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5913 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5914 .access = PL2_RW, .writefn = vttbr_write,
5915 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5916 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5917 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5918 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5919 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5920 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5921 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5922 .access = PL2_RW, .resetvalue = 0,
5923 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5924 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5925 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5926 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5927 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5928 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5929 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5930 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5931 { .name = "TLBIALLNSNH",
5932 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5933 .type = ARM_CP_NO_RAW, .access = PL2_W,
5934 .writefn = tlbiall_nsnh_write },
5935 { .name = "TLBIALLNSNHIS",
5936 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5937 .type = ARM_CP_NO_RAW, .access = PL2_W,
5938 .writefn = tlbiall_nsnh_is_write },
5939 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5940 .type = ARM_CP_NO_RAW, .access = PL2_W,
5941 .writefn = tlbiall_hyp_write },
5942 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5943 .type = ARM_CP_NO_RAW, .access = PL2_W,
5944 .writefn = tlbiall_hyp_is_write },
5945 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5946 .type = ARM_CP_NO_RAW, .access = PL2_W,
5947 .writefn = tlbimva_hyp_write },
5948 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5949 .type = ARM_CP_NO_RAW, .access = PL2_W,
5950 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5951 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5952 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 5953 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5954 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5955 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5956 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 5957 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5958 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5959 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5960 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 5961 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
5962 .writefn = tlbi_aa64_vae2_write },
5963 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5964 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 5965 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5966 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5967 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5968 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 5969 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5970 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5971 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5972 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 5973 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5974 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5975#ifndef CONFIG_USER_ONLY
9b37a28c
FR
5976 /*
5977 * Unlike the other EL2-related AT operations, these must
2a47df95
PM
5978 * UNDEF from EL3 if EL2 is not implemented, which is why we
5979 * define them here rather than with the rest of the AT ops.
5980 */
5981 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5982 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5983 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5984 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5985 .writefn = ats_write64 },
2a47df95
PM
5986 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5987 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5988 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5989 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5990 .writefn = ats_write64 },
9b37a28c
FR
5991 /*
5992 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
14db7fe0
PM
5993 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5994 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5995 * to behave as if SCR.NS was 1.
5996 */
5997 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5998 .access = PL2_W,
0710b2fa 5999 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
6000 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6001 .access = PL2_W,
0710b2fa 6002 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
6003 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6004 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
9b37a28c
FR
6005 /*
6006 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
0b6440af
EI
6007 * reset values as IMPDEF. We choose to reset to 3 to comply with
6008 * both ARMv7 and ARMv8.
6009 */
6010 .access = PL2_RW, .resetvalue = 3,
6011 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
6012 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6013 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6014 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6015 .writefn = gt_cntvoff_write,
6016 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6017 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6018 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6019 .writefn = gt_cntvoff_write,
6020 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
6021 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6022 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6023 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6024 .type = ARM_CP_IO, .access = PL2_RW,
6025 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6026 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6027 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6028 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6029 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6030 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6031 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 6032 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
6033 .resetfn = gt_hyp_timer_reset,
6034 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6035 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6036 .type = ARM_CP_IO,
6037 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6038 .access = PL2_RW,
6039 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6040 .resetvalue = 0,
6041 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 6042#endif
59e05530
EI
6043 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6044 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6045 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6046 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6047 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6048 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6049 .access = PL2_RW,
6050 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
6051 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6052 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6053 .access = PL2_RW,
6054 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
6055};
6056
ce4afed8
PM
6057static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6058 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 6059 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
6060 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6061 .access = PL2_RW,
6062 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6063 .writefn = hcr_writehigh },
ce4afed8
PM
6064};
6065
e9152ee9
RDC
6066static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6067 bool isread)
6068{
6069 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6070 return CP_ACCESS_OK;
6071 }
6072 return CP_ACCESS_TRAP_UNCATEGORIZED;
6073}
6074
6075static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6076 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6077 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6078 .access = PL2_RW, .accessfn = sel2_access,
6079 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6080 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6081 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6082 .access = PL2_RW, .accessfn = sel2_access,
6083 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
6084};
6085
2f027fc5
PM
6086static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6087 bool isread)
6088{
9b37a28c
FR
6089 /*
6090 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 6091 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
6092 */
6093 if (arm_current_el(env) == 3) {
6094 return CP_ACCESS_OK;
6095 }
6096 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
6097 if (env->cp15.scr_el3 & SCR_EEL2) {
6098 return CP_ACCESS_TRAP_EL2;
6099 }
2f027fc5
PM
6100 return CP_ACCESS_TRAP_EL3;
6101 }
6102 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6103 if (isread) {
6104 return CP_ACCESS_OK;
6105 }
6106 return CP_ACCESS_TRAP_UNCATEGORIZED;
6107}
6108
60fb1a87
GB
6109static const ARMCPRegInfo el3_cp_reginfo[] = {
6110 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6111 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6112 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 6113 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 6114 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 6115 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
6116 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6117 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 6118 .writefn = scr_write },
60fb1a87
GB
6119 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6120 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6121 .access = PL3_RW, .resetvalue = 0,
6122 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6123 { .name = "SDER",
6124 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6125 .access = PL3_RW, .resetvalue = 0,
6126 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 6127 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
6128 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6129 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 6130 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
6131 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6132 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 6133 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 6134 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
6135 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6136 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c 6137 .access = PL3_RW,
cb4a0a34
PM
6138 /* no .writefn needed as this can't cause an ASID change */
6139 .resetvalue = 0,
11f136ee 6140 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 6141 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6142 .type = ARM_CP_ALIAS,
81547d66
EI
6143 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6144 .access = PL3_RW,
6145 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 6146 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
6147 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6148 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
6149 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6150 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6151 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 6152 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6153 .type = ARM_CP_ALIAS,
81547d66 6154 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
6155 .access = PL3_RW,
6156 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
6157 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6158 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6159 .access = PL3_RW, .writefn = vbar_write,
6160 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6161 .resetvalue = 0 },
c6f19164
GB
6162 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6163 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6164 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6165 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
6166 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6167 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6168 .access = PL3_RW, .resetvalue = 0,
6169 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
6170 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6171 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6172 .access = PL3_RW, .type = ARM_CP_CONST,
6173 .resetvalue = 0 },
37cd6c24
PM
6174 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6175 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6176 .access = PL3_RW, .type = ARM_CP_CONST,
6177 .resetvalue = 0 },
6178 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6179 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6180 .access = PL3_RW, .type = ARM_CP_CONST,
6181 .resetvalue = 0 },
43efaa33
PM
6182 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6183 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6184 .access = PL3_W, .type = ARM_CP_NO_RAW,
6185 .writefn = tlbi_aa64_alle3is_write },
6186 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6187 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6188 .access = PL3_W, .type = ARM_CP_NO_RAW,
6189 .writefn = tlbi_aa64_vae3is_write },
6190 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6191 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6192 .access = PL3_W, .type = ARM_CP_NO_RAW,
6193 .writefn = tlbi_aa64_vae3is_write },
6194 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6195 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6196 .access = PL3_W, .type = ARM_CP_NO_RAW,
6197 .writefn = tlbi_aa64_alle3_write },
6198 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6199 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6200 .access = PL3_W, .type = ARM_CP_NO_RAW,
6201 .writefn = tlbi_aa64_vae3_write },
6202 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6203 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6204 .access = PL3_W, .type = ARM_CP_NO_RAW,
6205 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
6206};
6207
e2cce18f
RH
6208#ifndef CONFIG_USER_ONLY
6209/* Test if system register redirection is to occur in the current state. */
6210static bool redirect_for_e2h(CPUARMState *env)
6211{
6212 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6213}
6214
6215static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6216{
6217 CPReadFn *readfn;
6218
6219 if (redirect_for_e2h(env)) {
6220 /* Switch to the saved EL2 version of the register. */
6221 ri = ri->opaque;
6222 readfn = ri->readfn;
6223 } else {
6224 readfn = ri->orig_readfn;
6225 }
6226 if (readfn == NULL) {
6227 readfn = raw_read;
6228 }
6229 return readfn(env, ri);
6230}
6231
6232static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6233 uint64_t value)
6234{
6235 CPWriteFn *writefn;
6236
6237 if (redirect_for_e2h(env)) {
6238 /* Switch to the saved EL2 version of the register. */
6239 ri = ri->opaque;
6240 writefn = ri->writefn;
6241 } else {
6242 writefn = ri->orig_writefn;
6243 }
6244 if (writefn == NULL) {
6245 writefn = raw_write;
6246 }
6247 writefn(env, ri, value);
6248}
6249
6250static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6251{
6252 struct E2HAlias {
6253 uint32_t src_key, dst_key, new_key;
6254 const char *src_name, *dst_name, *new_name;
6255 bool (*feature)(const ARMISARegisters *id);
6256 };
6257
6258#define K(op0, op1, crn, crm, op2) \
6259 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6260
6261 static const struct E2HAlias aliases[] = {
6262 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6263 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6264 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6265 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6266 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6267 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6268 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6269 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6270 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6271 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6272 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6273 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6274 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6275 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6276 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6277 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6278 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6279 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6280 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6281 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6282 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6283 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6284 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6285 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6286 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6287 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6288 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6289 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6290 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6291 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6292 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6293 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6294
6295 /*
6296 * Note that redirection of ZCR is mentioned in the description
6297 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6298 * not in the summary table.
6299 */
6300 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6301 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
de561988
RH
6302 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6303 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
e2cce18f 6304
4b779ceb
RH
6305 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6306 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6307
7cb1e618
RH
6308 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6309 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6310 isar_feature_aa64_scxtnum },
6311
e2cce18f
RH
6312 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6313 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6314 };
6315#undef K
6316
6317 size_t i;
6318
6319 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6320 const struct E2HAlias *a = &aliases[i];
9da35a40 6321 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 6322 bool ok;
e2cce18f
RH
6323
6324 if (a->feature && !a->feature(&cpu->isar)) {
6325 continue;
6326 }
6327
5860362d
RH
6328 src_reg = g_hash_table_lookup(cpu->cp_regs,
6329 (gpointer)(uintptr_t)a->src_key);
6330 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6331 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
6332 g_assert(src_reg != NULL);
6333 g_assert(dst_reg != NULL);
6334
6335 /* Cross-compare names to detect typos in the keys. */
6336 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6337 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6338
6339 /* None of the core system registers use opaque; we will. */
6340 g_assert(src_reg->opaque == NULL);
6341
6342 /* Create alias before redirection so we dup the right data. */
9da35a40 6343 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
6344
6345 new_reg->name = a->new_name;
6346 new_reg->type |= ARM_CP_ALIAS;
6347 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6348 new_reg->access &= PL2_RW | PL3_RW;
6349
5860362d
RH
6350 ok = g_hash_table_insert(cpu->cp_regs,
6351 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 6352 g_assert(ok);
e2cce18f
RH
6353
6354 src_reg->opaque = dst_reg;
6355 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6356 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6357 if (!src_reg->raw_readfn) {
6358 src_reg->raw_readfn = raw_read;
6359 }
6360 if (!src_reg->raw_writefn) {
6361 src_reg->raw_writefn = raw_write;
6362 }
6363 src_reg->readfn = el2_e2h_read;
6364 src_reg->writefn = el2_e2h_write;
6365 }
6366}
6367#endif
6368
3f208fd7
PM
6369static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6370 bool isread)
7da845b0 6371{
97475a89
RH
6372 int cur_el = arm_current_el(env);
6373
6374 if (cur_el < 2) {
6375 uint64_t hcr = arm_hcr_el2_eff(env);
6376
6377 if (cur_el == 0) {
6378 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6379 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6380 return CP_ACCESS_TRAP_EL2;
6381 }
6382 } else {
6383 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6384 return CP_ACCESS_TRAP;
6385 }
6386 if (hcr & HCR_TID2) {
6387 return CP_ACCESS_TRAP_EL2;
6388 }
6389 }
6390 } else if (hcr & HCR_TID2) {
6391 return CP_ACCESS_TRAP_EL2;
6392 }
7da845b0 6393 }
630fcd4d
MZ
6394
6395 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6396 return CP_ACCESS_TRAP_EL2;
6397 }
6398
7da845b0
PM
6399 return CP_ACCESS_OK;
6400}
6401
58e93b48
RH
6402/*
6403 * Check for traps to RAS registers, which are controlled
6404 * by HCR_EL2.TERR and SCR_EL3.TERR.
6405 */
6406static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6407 bool isread)
6408{
6409 int el = arm_current_el(env);
6410
6411 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6412 return CP_ACCESS_TRAP_EL2;
6413 }
6414 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6415 return CP_ACCESS_TRAP_EL3;
6416 }
6417 return CP_ACCESS_OK;
6418}
6419
6420static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6421{
6422 int el = arm_current_el(env);
6423
6424 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6425 return env->cp15.vdisr_el2;
6426 }
6427 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6428 return 0; /* RAZ/WI */
6429 }
6430 return env->cp15.disr_el1;
6431}
6432
6433static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6434{
6435 int el = arm_current_el(env);
6436
6437 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6438 env->cp15.vdisr_el2 = val;
6439 return;
6440 }
6441 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6442 return; /* RAZ/WI */
6443 }
6444 env->cp15.disr_el1 = val;
6445}
6446
6447/*
6448 * Minimal RAS implementation with no Error Records.
6449 * Which means that all of the Error Record registers:
6450 * ERXADDR_EL1
6451 * ERXCTLR_EL1
6452 * ERXFR_EL1
6453 * ERXMISC0_EL1
6454 * ERXMISC1_EL1
6455 * ERXMISC2_EL1
6456 * ERXMISC3_EL1
6457 * ERXPFGCDN_EL1 (RASv1p1)
6458 * ERXPFGCTL_EL1 (RASv1p1)
6459 * ERXPFGF_EL1 (RASv1p1)
6460 * ERXSTATUS_EL1
6461 * and
6462 * ERRSELR_EL1
6463 * may generate UNDEFINED, which is the effect we get by not
6464 * listing them at all.
6465 */
6466static const ARMCPRegInfo minimal_ras_reginfo[] = {
6467 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6468 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6469 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6470 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6471 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6472 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6473 .access = PL1_R, .accessfn = access_terr,
6474 .type = ARM_CP_CONST, .resetvalue = 0 },
6475 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6476 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6477 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6478 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6479 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6480 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6481};
6482
397d922c
RH
6483/*
6484 * Return the exception level to which exceptions should be taken
6485 * via SVEAccessTrap. This excludes the check for whether the exception
6486 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6487 * be found by testing 0 < fp_exception_el < sve_exception_el.
6488 *
6489 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6490 * pseudocode does *not* separate out the FP trap checks, but has them
6491 * all in one function.
5be5e8ed 6492 */
ced31551 6493int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6494{
6495#ifndef CONFIG_USER_ONLY
aa4451b6 6496 if (el <= 1 && !el_is_in_host(env, el)) {
fab8ad39 6497 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7701cee5
RH
6498 case 1:
6499 if (el != 0) {
6500 break;
6501 }
6502 /* fall through */
6503 case 0:
6504 case 2:
61a8c23a 6505 return 1;
5be5e8ed 6506 }
5be5e8ed
RH
6507 }
6508
7d38cb92
RH
6509 if (el <= 2 && arm_is_el2_enabled(env)) {
6510 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6511 if (env->cp15.hcr_el2 & HCR_E2H) {
fab8ad39 6512 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
d5a6fa2d 6513 case 1:
7d38cb92 6514 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
d5a6fa2d
RH
6515 break;
6516 }
6517 /* fall through */
6518 case 0:
6519 case 2:
6520 return 2;
6521 }
7d38cb92 6522 } else {
fab8ad39 6523 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
d5a6fa2d
RH
6524 return 2;
6525 }
60eed086 6526 }
5be5e8ed
RH
6527 }
6528
60eed086
RH
6529 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6530 if (arm_feature(env, ARM_FEATURE_EL3)
fab8ad39 6531 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
5be5e8ed
RH
6532 return 3;
6533 }
6534#endif
6535 return 0;
6536}
6537
6b2ca83e
RH
6538/*
6539 * Return the exception level to which exceptions should be taken for SME.
6540 * C.f. the ARM pseudocode function CheckSMEAccess.
6541 */
6542int sme_exception_el(CPUARMState *env, int el)
6543{
6544#ifndef CONFIG_USER_ONLY
6545 if (el <= 1 && !el_is_in_host(env, el)) {
6546 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6547 case 1:
6548 if (el != 0) {
6549 break;
6550 }
6551 /* fall through */
6552 case 0:
6553 case 2:
6554 return 1;
6555 }
6556 }
6557
6558 if (el <= 2 && arm_is_el2_enabled(env)) {
6559 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6560 if (env->cp15.hcr_el2 & HCR_E2H) {
6561 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6562 case 1:
6563 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6564 break;
6565 }
6566 /* fall through */
6567 case 0:
6568 case 2:
6569 return 2;
6570 }
6571 } else {
6572 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6573 return 2;
6574 }
6575 }
6576 }
6577
6578 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6579 if (arm_feature(env, ARM_FEATURE_EL3)
6580 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6581 return 3;
6582 }
6583#endif
6584 return 0;
6585}
6586
75fe8356
RH
6587/* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6588static bool sme_fa64(CPUARMState *env, int el)
6589{
6590 if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6591 return false;
6592 }
6593
6594 if (el <= 1 && !el_is_in_host(env, el)) {
6595 if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6596 return false;
6597 }
6598 }
6599 if (el <= 2 && arm_is_el2_enabled(env)) {
6600 if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6601 return false;
6602 }
6603 }
6604 if (arm_feature(env, ARM_FEATURE_EL3)) {
6605 if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6606 return false;
6607 }
6608 }
6609
6610 return true;
6611}
6612
0ab5953b
RH
6613/*
6614 * Given that SVE is enabled, return the vector length for EL.
6615 */
6ca54aa9 6616uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
0ab5953b 6617{
2fc0cc0e 6618 ARMCPU *cpu = env_archcpu(env);
6ca54aa9
RH
6619 uint64_t *cr = env->vfp.zcr_el;
6620 uint32_t map = cpu->sve_vq.map;
6621 uint32_t len = ARM_MAX_VQ - 1;
6622
6623 if (sm) {
6624 cr = env->vfp.smcr_el;
6625 map = cpu->sme_vq.map;
6626 }
0ab5953b 6627
c6225beb 6628 if (el <= 1 && !el_is_in_host(env, el)) {
6ca54aa9 6629 len = MIN(len, 0xf & (uint32_t)cr[1]);
0ab5953b 6630 }
6a02a732 6631 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6ca54aa9 6632 len = MIN(len, 0xf & (uint32_t)cr[2]);
0ab5953b 6633 }
6a02a732 6634 if (arm_feature(env, ARM_FEATURE_EL3)) {
6ca54aa9
RH
6635 len = MIN(len, 0xf & (uint32_t)cr[3]);
6636 }
6637
6638 map &= MAKE_64BIT_MASK(0, len + 1);
6639 if (map != 0) {
6640 return 31 - clz32(map);
0ab5953b 6641 }
0df9142d 6642
6ca54aa9
RH
6643 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6644 assert(sm);
6645 return ctz32(cpu->sme_vq.map);
6646}
6647
6648uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6649{
6650 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
0ab5953b
RH
6651}
6652
5be5e8ed
RH
6653static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6654 uint64_t value)
6655{
0ab5953b 6656 int cur_el = arm_current_el(env);
5ef3cc56 6657 int old_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6658 int new_len;
6659
5be5e8ed 6660 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6661 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6662 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6663
6664 /*
6665 * Because we arrived here, we know both FP and SVE are enabled;
6666 * otherwise we would have trapped access to the ZCR_ELn register.
6667 */
5ef3cc56 6668 new_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6669 if (new_len < old_len) {
6670 aarch64_sve_narrow_vq(env, new_len + 1);
6671 }
5be5e8ed
RH
6672}
6673
60360d82
RH
6674static const ARMCPRegInfo zcr_reginfo[] = {
6675 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6676 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6677 .access = PL1_RW, .type = ARM_CP_SVE,
6678 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6679 .writefn = zcr_write, .raw_writefn = raw_write },
6680 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6681 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6682 .access = PL2_RW, .type = ARM_CP_SVE,
6683 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6684 .writefn = zcr_write, .raw_writefn = raw_write },
6685 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6686 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6687 .access = PL3_RW, .type = ARM_CP_SVE,
6688 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6689 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6690};
6691
9e5ec745
RH
6692#ifdef TARGET_AARCH64
6693static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6694 bool isread)
6695{
6696 int el = arm_current_el(env);
6697
6698 if (el == 0) {
6699 uint64_t sctlr = arm_sctlr(env, el);
6700 if (!(sctlr & SCTLR_EnTP2)) {
6701 return CP_ACCESS_TRAP;
6702 }
6703 }
6704 /* TODO: FEAT_FGT */
6705 if (el < 3
6706 && arm_feature(env, ARM_FEATURE_EL3)
6707 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6708 return CP_ACCESS_TRAP_EL3;
6709 }
6710 return CP_ACCESS_OK;
6711}
6712
d5b1223a
RH
6713static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6714 bool isread)
6715{
6716 /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6717 if (arm_current_el(env) < 3
6718 && arm_feature(env, ARM_FEATURE_EL3)
6719 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6720 return CP_ACCESS_TRAP_EL3;
6721 }
6722 return CP_ACCESS_OK;
6723}
6724
c37e6ac9
RH
6725static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6726 uint64_t value)
6727{
f84734b8
RH
6728 helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6729 helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6730 arm_rebuild_hflags(env);
c37e6ac9
RH
6731}
6732
de561988
RH
6733static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6734 uint64_t value)
6735{
6736 int cur_el = arm_current_el(env);
6737 int old_len = sve_vqm1_for_el(env, cur_el);
6738 int new_len;
6739
6740 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6741 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6742 raw_write(env, ri, value);
6743
6744 /*
6745 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6746 * when SVL is widened (old values kept, or zeros). Choose to keep the
6747 * current values for simplicity. But for QEMU internals, we must still
6748 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6749 * above aarch64_sve_narrow_vq.
6750 */
6751 new_len = sve_vqm1_for_el(env, cur_el);
6752 if (new_len < old_len) {
6753 aarch64_sve_narrow_vq(env, new_len + 1);
6754 }
6755}
6756
9e5ec745
RH
6757static const ARMCPRegInfo sme_reginfo[] = {
6758 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6759 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6760 .access = PL0_RW, .accessfn = access_tpidr2,
6761 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
c37e6ac9
RH
6762 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6763 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6764 .access = PL0_RW, .type = ARM_CP_SME,
6765 .fieldoffset = offsetof(CPUARMState, svcr),
6766 .writefn = svcr_write, .raw_writefn = raw_write },
de561988
RH
6767 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6768 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6769 .access = PL1_RW, .type = ARM_CP_SME,
6770 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6771 .writefn = smcr_write, .raw_writefn = raw_write },
6772 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6773 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6774 .access = PL2_RW, .type = ARM_CP_SME,
6775 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6776 .writefn = smcr_write, .raw_writefn = raw_write },
6777 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6778 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6779 .access = PL3_RW, .type = ARM_CP_SME,
6780 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6781 .writefn = smcr_write, .raw_writefn = raw_write },
d5b1223a
RH
6782 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6783 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6784 .access = PL1_R, .accessfn = access_aa64_tid1,
6785 /*
6786 * IMPLEMENTOR = 0 (software)
6787 * REVISION = 0 (implementation defined)
6788 * SMPS = 0 (no streaming execution priority in QEMU)
6789 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
6790 */
6791 .type = ARM_CP_CONST, .resetvalue = 0, },
6792 /*
6793 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6794 */
6795 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6796 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6797 .access = PL1_RW, .accessfn = access_esm,
6798 .type = ARM_CP_CONST, .resetvalue = 0 },
6799 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6800 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6801 .access = PL2_RW, .accessfn = access_esm,
6802 .type = ARM_CP_CONST, .resetvalue = 0 },
9e5ec745
RH
6803};
6804#endif /* TARGET_AARCH64 */
6805
24183fb6
PM
6806static void define_pmu_regs(ARMCPU *cpu)
6807{
6808 /*
6809 * v7 performance monitor control register: same implementor
6810 * field as main ID register, and we implement four counters in
6811 * addition to the cycle count register.
6812 */
24526bb9 6813 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
24183fb6
PM
6814 ARMCPRegInfo pmcr = {
6815 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6816 .access = PL0_RW,
6817 .type = ARM_CP_IO | ARM_CP_ALIAS,
6818 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6819 .accessfn = pmreg_access, .writefn = pmcr_write,
6820 .raw_writefn = raw_write,
6821 };
6822 ARMCPRegInfo pmcr64 = {
6823 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6824 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6825 .access = PL0_RW, .accessfn = pmreg_access,
6826 .type = ARM_CP_IO,
6827 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
24526bb9 6828 .resetvalue = cpu->isar.reset_pmcr_el0,
24183fb6
PM
6829 .writefn = pmcr_write, .raw_writefn = raw_write,
6830 };
24526bb9 6831
24183fb6
PM
6832 define_one_arm_cp_reg(cpu, &pmcr);
6833 define_one_arm_cp_reg(cpu, &pmcr64);
6834 for (i = 0; i < pmcrn; i++) {
6835 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6836 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6837 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6838 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6839 ARMCPRegInfo pmev_regs[] = {
6840 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6841 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6842 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6843 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 6844 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
6845 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6846 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 6847 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6
PM
6848 .type = ARM_CP_IO,
6849 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6850 .raw_readfn = pmevcntr_rawread,
6851 .raw_writefn = pmevcntr_rawwrite },
6852 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6853 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6854 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6855 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6856 .accessfn = pmreg_access },
6857 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6858 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6859 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6860 .type = ARM_CP_IO,
6861 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6862 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6863 };
6864 define_arm_cp_regs(cpu, pmev_regs);
6865 g_free(pmevcntr_name);
6866 g_free(pmevcntr_el0_name);
6867 g_free(pmevtyper_name);
6868 g_free(pmevtyper_el0_name);
6869 }
a793bcd0 6870 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
24183fb6
PM
6871 ARMCPRegInfo v81_pmu_regs[] = {
6872 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6873 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6874 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6875 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6876 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6877 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6878 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6879 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6880 };
6881 define_arm_cp_regs(cpu, v81_pmu_regs);
6882 }
a793bcd0 6883 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
15dd1ebd
PM
6884 static const ARMCPRegInfo v84_pmmir = {
6885 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6886 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6887 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6888 .resetvalue = 0
6889 };
6890 define_one_arm_cp_reg(cpu, &v84_pmmir);
6891 }
24183fb6
PM
6892}
6893
9b37a28c
FR
6894/*
6895 * We don't know until after realize whether there's a GICv3
96a8b92e
PM
6896 * attached, and that is what registers the gicv3 sysregs.
6897 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6898 * at runtime.
6899 */
6900static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6901{
2fc0cc0e 6902 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6903 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6904
6905 if (env->gicv3state) {
6906 pfr1 |= 1 << 28;
6907 }
6908 return pfr1;
6909}
6910
976b99b6 6911#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6912static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6913{
2fc0cc0e 6914 ARMCPU *cpu = env_archcpu(env);
47576b94 6915 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6916
6917 if (env->gicv3state) {
6918 pfr0 |= 1 << 24;
6919 }
6920 return pfr0;
6921}
976b99b6 6922#endif
96a8b92e 6923
9b37a28c
FR
6924/*
6925 * Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6926 * Secure state exclusion has already been dealt with.
2d7137c1 6927 */
9bd268ba
RDC
6928static CPAccessResult access_lor_ns(CPUARMState *env,
6929 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6930{
6931 int el = arm_current_el(env);
6932
6933 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6934 return CP_ACCESS_TRAP_EL2;
6935 }
6936 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6937 return CP_ACCESS_TRAP_EL3;
6938 }
6939 return CP_ACCESS_OK;
6940}
6941
2d7137c1
RH
6942static CPAccessResult access_lor_other(CPUARMState *env,
6943 const ARMCPRegInfo *ri, bool isread)
6944{
6945 if (arm_is_secure_below_el3(env)) {
6946 /* Access denied in secure mode. */
6947 return CP_ACCESS_TRAP;
6948 }
9bd268ba 6949 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6950}
6951
d8564ee4
RH
6952/*
6953 * A trivial implementation of ARMv8.1-LOR leaves all of these
6954 * registers fixed at 0, which indicates that there are zero
6955 * supported Limited Ordering regions.
6956 */
6957static const ARMCPRegInfo lor_reginfo[] = {
6958 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6959 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6960 .access = PL1_RW, .accessfn = access_lor_other,
6961 .type = ARM_CP_CONST, .resetvalue = 0 },
6962 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6963 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6964 .access = PL1_RW, .accessfn = access_lor_other,
6965 .type = ARM_CP_CONST, .resetvalue = 0 },
6966 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6967 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6968 .access = PL1_RW, .accessfn = access_lor_other,
6969 .type = ARM_CP_CONST, .resetvalue = 0 },
6970 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6971 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6972 .access = PL1_RW, .accessfn = access_lor_other,
6973 .type = ARM_CP_CONST, .resetvalue = 0 },
6974 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6975 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 6976 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4 6977 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
6978};
6979
967aa94f
RH
6980#ifdef TARGET_AARCH64
6981static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6982 bool isread)
6983{
6984 int el = arm_current_el(env);
6985
6986 if (el < 2 &&
07b034ea 6987 arm_is_el2_enabled(env) &&
967aa94f
RH
6988 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6989 return CP_ACCESS_TRAP_EL2;
6990 }
6991 if (el < 3 &&
6992 arm_feature(env, ARM_FEATURE_EL3) &&
6993 !(env->cp15.scr_el3 & SCR_APK)) {
6994 return CP_ACCESS_TRAP_EL3;
6995 }
6996 return CP_ACCESS_OK;
6997}
6998
6999static const ARMCPRegInfo pauth_reginfo[] = {
7000 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7001 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7002 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7003 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
7004 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7005 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7006 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7007 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
7008 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7009 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7010 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7011 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
7012 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7013 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7014 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7015 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
7016 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7017 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7018 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7019 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
7020 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7021 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7022 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7023 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
7024 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7025 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7026 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7027 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
7028 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7029 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7030 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7031 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
7032 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7033 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7034 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7035 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
7036 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7037 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7038 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 7039 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 7040};
de390645 7041
84940ed8
RC
7042static const ARMCPRegInfo tlbirange_reginfo[] = {
7043 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7044 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
0f66d223 7045 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7046 .writefn = tlbi_aa64_rvae1is_write },
7047 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7048 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
0f66d223 7049 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7050 .writefn = tlbi_aa64_rvae1is_write },
7051 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7052 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
0f66d223 7053 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7054 .writefn = tlbi_aa64_rvae1is_write },
7055 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7056 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
0f66d223 7057 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7058 .writefn = tlbi_aa64_rvae1is_write },
7059 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7060 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
fe3ca86c 7061 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7062 .writefn = tlbi_aa64_rvae1is_write },
7063 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7064 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
fe3ca86c 7065 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7066 .writefn = tlbi_aa64_rvae1is_write },
7067 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7068 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
fe3ca86c 7069 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7070 .writefn = tlbi_aa64_rvae1is_write },
7071 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7072 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
fe3ca86c 7073 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7074 .writefn = tlbi_aa64_rvae1is_write },
7075 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7076 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
4870f38b 7077 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7078 .writefn = tlbi_aa64_rvae1_write },
7079 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7080 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
4870f38b 7081 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7082 .writefn = tlbi_aa64_rvae1_write },
7083 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7084 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
4870f38b 7085 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7086 .writefn = tlbi_aa64_rvae1_write },
7087 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7088 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
4870f38b 7089 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7090 .writefn = tlbi_aa64_rvae1_write },
7091 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7092 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
575a94af
RH
7093 .access = PL2_W, .type = ARM_CP_NO_RAW,
7094 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7095 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7096 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
575a94af
RH
7097 .access = PL2_W, .type = ARM_CP_NO_RAW,
7098 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7099 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7100 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 7101 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7102 .writefn = tlbi_aa64_rvae2is_write },
7103 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7104 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 7105 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7106 .writefn = tlbi_aa64_rvae2is_write },
7107 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7108 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
575a94af
RH
7109 .access = PL2_W, .type = ARM_CP_NO_RAW,
7110 .writefn = tlbi_aa64_ripas2e1_write },
7111 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
84940ed8 7112 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
575a94af
RH
7113 .access = PL2_W, .type = ARM_CP_NO_RAW,
7114 .writefn = tlbi_aa64_ripas2e1_write },
84940ed8
RC
7115 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7116 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 7117 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7118 .writefn = tlbi_aa64_rvae2is_write },
7119 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7120 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 7121 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7122 .writefn = tlbi_aa64_rvae2is_write },
7123 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7124 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 7125 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7126 .writefn = tlbi_aa64_rvae2_write },
7127 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7128 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 7129 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7130 .writefn = tlbi_aa64_rvae2_write },
7131 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7132 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7133 .access = PL3_W, .type = ARM_CP_NO_RAW,
7134 .writefn = tlbi_aa64_rvae3is_write },
7135 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7136 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7137 .access = PL3_W, .type = ARM_CP_NO_RAW,
7138 .writefn = tlbi_aa64_rvae3is_write },
7139 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7140 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7141 .access = PL3_W, .type = ARM_CP_NO_RAW,
7142 .writefn = tlbi_aa64_rvae3is_write },
7143 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7144 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7145 .access = PL3_W, .type = ARM_CP_NO_RAW,
7146 .writefn = tlbi_aa64_rvae3is_write },
7147 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7148 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7149 .access = PL3_W, .type = ARM_CP_NO_RAW,
7150 .writefn = tlbi_aa64_rvae3_write },
7151 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7152 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7153 .access = PL3_W, .type = ARM_CP_NO_RAW,
7154 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
7155};
7156
7113d618
RC
7157static const ARMCPRegInfo tlbios_reginfo[] = {
7158 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
fe3ca86c 7160 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7113d618 7161 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7162 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7163 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
fe3ca86c 7164 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7165 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7166 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7167 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
fe3ca86c 7168 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7113d618 7169 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7170 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7171 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
fe3ca86c 7172 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9
IH
7173 .writefn = tlbi_aa64_vae1is_write },
7174 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7175 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
fe3ca86c 7176 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9
IH
7177 .writefn = tlbi_aa64_vae1is_write },
7178 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7179 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
fe3ca86c 7180 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7181 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7182 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7183 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 7184 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 7185 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
7186 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7187 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 7188 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7189 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7190 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7191 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7192 .access = PL2_W, .type = ARM_CP_NO_RAW,
7193 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
7194 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7195 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 7196 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7197 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7198 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7199 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7200 .access = PL2_W, .type = ARM_CP_NO_RAW,
7201 .writefn = tlbi_aa64_alle1is_write },
7202 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7203 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7204 .access = PL2_W, .type = ARM_CP_NOP },
7205 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7206 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7207 .access = PL2_W, .type = ARM_CP_NOP },
7208 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7209 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7210 .access = PL2_W, .type = ARM_CP_NOP },
7211 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7212 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7213 .access = PL2_W, .type = ARM_CP_NOP },
7214 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7215 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7216 .access = PL3_W, .type = ARM_CP_NO_RAW,
7217 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
7218 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7219 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7220 .access = PL3_W, .type = ARM_CP_NO_RAW,
7221 .writefn = tlbi_aa64_vae3is_write },
7222 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7223 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7224 .access = PL3_W, .type = ARM_CP_NO_RAW,
7225 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
7226};
7227
de390645
RH
7228static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7229{
7230 Error *err = NULL;
7231 uint64_t ret;
7232
7233 /* Success sets NZCV = 0000. */
7234 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7235
7236 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7237 /*
7238 * ??? Failed, for unknown reasons in the crypto subsystem.
7239 * The best we can do is log the reason and return the
7240 * timed-out indication to the guest. There is no reason
7241 * we know to expect this failure to be transitory, so the
7242 * guest may well hang retrying the operation.
7243 */
7244 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7245 ri->name, error_get_pretty(err));
7246 error_free(err);
7247
7248 env->ZF = 0; /* NZCF = 0100 */
7249 return 0;
7250 }
7251 return ret;
7252}
7253
7254/* We do not support re-seeding, so the two registers operate the same. */
7255static const ARMCPRegInfo rndr_reginfo[] = {
7256 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7257 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7258 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7259 .access = PL0_R, .readfn = rndr_readfn },
7260 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7261 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7262 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7263 .access = PL0_R, .readfn = rndr_readfn },
de390645 7264};
0d57b499
BM
7265
7266#ifndef CONFIG_USER_ONLY
7267static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7268 uint64_t value)
7269{
7270 ARMCPU *cpu = env_archcpu(env);
7271 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7272 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7273 uint64_t vaddr_in = (uint64_t) value;
7274 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7275 void *haddr;
7276 int mem_idx = cpu_mmu_index(env, false);
7277
7278 /* This won't be crossing page boundaries */
7279 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7280 if (haddr) {
7281
7282 ram_addr_t offset;
7283 MemoryRegion *mr;
7284
7285 /* RCU lock is already being held */
7286 mr = memory_region_from_host(haddr, &offset);
7287
7288 if (mr) {
4dfe59d1 7289 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
7290 }
7291 }
7292}
7293
7294static const ARMCPRegInfo dcpop_reg[] = {
7295 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7296 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7297 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7298 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7299};
7300
7301static const ARMCPRegInfo dcpodp_reg[] = {
7302 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7303 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7304 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7305 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7306};
7307#endif /*CONFIG_USER_ONLY*/
7308
4b779ceb
RH
7309static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7310 bool isread)
7311{
7312 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7313 return CP_ACCESS_TRAP_EL2;
7314 }
7315
7316 return CP_ACCESS_OK;
7317}
7318
7319static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7320 bool isread)
7321{
7322 int el = arm_current_el(env);
7323
0da067f2 7324 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7325 uint64_t hcr = arm_hcr_el2_eff(env);
7326 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7327 return CP_ACCESS_TRAP_EL2;
7328 }
4b779ceb
RH
7329 }
7330 if (el < 3 &&
7331 arm_feature(env, ARM_FEATURE_EL3) &&
7332 !(env->cp15.scr_el3 & SCR_ATA)) {
7333 return CP_ACCESS_TRAP_EL3;
7334 }
7335 return CP_ACCESS_OK;
7336}
7337
7338static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7339{
7340 return env->pstate & PSTATE_TCO;
7341}
7342
7343static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7344{
7345 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7346}
7347
7348static const ARMCPRegInfo mte_reginfo[] = {
7349 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7350 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7351 .access = PL1_RW, .accessfn = access_mte,
7352 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7353 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7354 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7355 .access = PL1_RW, .accessfn = access_mte,
7356 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7357 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7358 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7359 .access = PL2_RW, .accessfn = access_mte,
7360 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7361 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7362 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7363 .access = PL3_RW,
7364 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7365 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7366 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7367 .access = PL1_RW, .accessfn = access_mte,
7368 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7369 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7370 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7371 .access = PL1_RW, .accessfn = access_mte,
7372 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7373 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7374 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7375 .access = PL1_R, .accessfn = access_aa64_tid5,
7376 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7377 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7378 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7379 .type = ARM_CP_NO_RAW,
7380 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7381 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7382 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7383 .type = ARM_CP_NOP, .access = PL1_W,
7384 .accessfn = aa64_cacheop_poc_access },
7385 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7386 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7387 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7388 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7389 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7390 .type = ARM_CP_NOP, .access = PL1_W,
7391 .accessfn = aa64_cacheop_poc_access },
7392 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7393 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7394 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7395 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7396 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7397 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7398 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7399 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7400 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7401 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7402 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7403 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7404 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7405 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7406 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7407};
7408
7409static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7410 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7411 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7412 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7413};
5463df16
RH
7414
7415static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7416 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7417 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7418 .type = ARM_CP_NOP, .access = PL0_W,
7419 .accessfn = aa64_cacheop_poc_access },
7420 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7421 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7422 .type = ARM_CP_NOP, .access = PL0_W,
7423 .accessfn = aa64_cacheop_poc_access },
7424 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7425 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7426 .type = ARM_CP_NOP, .access = PL0_W,
7427 .accessfn = aa64_cacheop_poc_access },
7428 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7429 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7430 .type = ARM_CP_NOP, .access = PL0_W,
7431 .accessfn = aa64_cacheop_poc_access },
7432 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7433 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7434 .type = ARM_CP_NOP, .access = PL0_W,
7435 .accessfn = aa64_cacheop_poc_access },
7436 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7437 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7438 .type = ARM_CP_NOP, .access = PL0_W,
7439 .accessfn = aa64_cacheop_poc_access },
7440 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7441 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7442 .type = ARM_CP_NOP, .access = PL0_W,
7443 .accessfn = aa64_cacheop_poc_access },
7444 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7445 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7446 .type = ARM_CP_NOP, .access = PL0_W,
7447 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7448 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7449 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7450 .access = PL0_W, .type = ARM_CP_DC_GVA,
7451#ifndef CONFIG_USER_ONLY
7452 /* Avoid overhead of an access check that always passes in user-mode */
7453 .accessfn = aa64_zva_access,
7454#endif
7455 },
7456 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7457 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7458 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7459#ifndef CONFIG_USER_ONLY
7460 /* Avoid overhead of an access check that always passes in user-mode */
7461 .accessfn = aa64_zva_access,
7462#endif
7463 },
5463df16
RH
7464};
7465
7cb1e618
RH
7466static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7467 bool isread)
7468{
7469 uint64_t hcr = arm_hcr_el2_eff(env);
7470 int el = arm_current_el(env);
7471
7472 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7473 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7474 if (hcr & HCR_TGE) {
7475 return CP_ACCESS_TRAP_EL2;
7476 }
7477 return CP_ACCESS_TRAP;
7478 }
7479 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7480 return CP_ACCESS_TRAP_EL2;
7481 }
7482 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7483 return CP_ACCESS_TRAP_EL2;
7484 }
7485 if (el < 3
7486 && arm_feature(env, ARM_FEATURE_EL3)
7487 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7488 return CP_ACCESS_TRAP_EL3;
7489 }
7490 return CP_ACCESS_OK;
7491}
7492
7493static const ARMCPRegInfo scxtnum_reginfo[] = {
7494 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7495 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7496 .access = PL0_RW, .accessfn = access_scxtnum,
7497 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7498 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7499 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7500 .access = PL1_RW, .accessfn = access_scxtnum,
7501 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7502 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7503 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7504 .access = PL2_RW, .accessfn = access_scxtnum,
7505 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7506 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7507 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7508 .access = PL3_RW,
7509 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7510};
7511#endif /* TARGET_AARCH64 */
967aa94f 7512
cb570bd3
RH
7513static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7514 bool isread)
7515{
7516 int el = arm_current_el(env);
7517
7518 if (el == 0) {
7519 uint64_t sctlr = arm_sctlr(env, el);
7520 if (!(sctlr & SCTLR_EnRCTX)) {
7521 return CP_ACCESS_TRAP;
7522 }
7523 } else if (el == 1) {
7524 uint64_t hcr = arm_hcr_el2_eff(env);
7525 if (hcr & HCR_NV) {
7526 return CP_ACCESS_TRAP_EL2;
7527 }
7528 }
7529 return CP_ACCESS_OK;
7530}
7531
7532static const ARMCPRegInfo predinv_reginfo[] = {
7533 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7534 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7535 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7536 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7537 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7538 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7539 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7540 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7541 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7542 /*
7543 * Note the AArch32 opcodes have a different OPC1.
7544 */
7545 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7546 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7547 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7548 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7549 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7550 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7551 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7552 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7553 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7554};
7555
957e6155
PM
7556static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7557{
7558 /* Read the high 32 bits of the current CCSIDR */
7559 return extract64(ccsidr_read(env, ri), 32, 32);
7560}
7561
7562static const ARMCPRegInfo ccsidr2_reginfo[] = {
7563 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7564 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7565 .access = PL1_R,
e2ce5fcd 7566 .accessfn = access_tid4,
957e6155 7567 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7568};
7569
6a4ef4e5
MZ
7570static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7571 bool isread)
7572{
7573 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7574 return CP_ACCESS_TRAP_EL2;
7575 }
7576
7577 return CP_ACCESS_OK;
7578}
7579
7580static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7581 bool isread)
7582{
7583 if (arm_feature(env, ARM_FEATURE_V8)) {
7584 return access_aa64_tid3(env, ri, isread);
7585 }
7586
7587 return CP_ACCESS_OK;
7588}
7589
f96f3d5f
MZ
7590static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7591 bool isread)
7592{
7593 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7594 return CP_ACCESS_TRAP_EL2;
7595 }
7596
7597 return CP_ACCESS_OK;
7598}
7599
8e228c9e
PM
7600static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7601 const ARMCPRegInfo *ri, bool isread)
7602{
7603 /*
7604 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7605 * in v7A, not in v8A.
7606 */
7607 if (!arm_feature(env, ARM_FEATURE_V8) &&
7608 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7609 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7610 return CP_ACCESS_TRAP_EL2;
7611 }
7612 return CP_ACCESS_OK;
7613}
7614
f96f3d5f
MZ
7615static const ARMCPRegInfo jazelle_regs[] = {
7616 { .name = "JIDR",
7617 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7618 .access = PL1_R, .accessfn = access_jazelle,
7619 .type = ARM_CP_CONST, .resetvalue = 0 },
7620 { .name = "JOSCR",
7621 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7622 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7623 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7624 { .name = "JMCR",
7625 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7626 .accessfn = access_joscr_jmcr,
f96f3d5f 7627 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7628};
7629
52d18727
RH
7630static const ARMCPRegInfo contextidr_el2 = {
7631 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7632 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7633 .access = PL2_RW,
7634 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7635};
7636
e2a1a461 7637static const ARMCPRegInfo vhe_reginfo[] = {
ed30da8e
RH
7638 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7639 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7640 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7641 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7642#ifndef CONFIG_USER_ONLY
7643 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7644 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7645 .fieldoffset =
7646 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7647 .type = ARM_CP_IO, .access = PL2_RW,
7648 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7649 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7650 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7651 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7652 .resetfn = gt_hv_timer_reset,
7653 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7654 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7655 .type = ARM_CP_IO,
7656 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7657 .access = PL2_RW,
7658 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7659 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7660 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7661 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7662 .type = ARM_CP_IO | ARM_CP_ALIAS,
7663 .access = PL2_RW, .accessfn = e2h_access,
7664 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7665 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7666 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7667 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7668 .type = ARM_CP_IO | ARM_CP_ALIAS,
7669 .access = PL2_RW, .accessfn = e2h_access,
7670 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7671 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7672 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7673 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7674 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7675 .access = PL2_RW, .accessfn = e2h_access,
7676 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7677 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7678 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7679 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7680 .access = PL2_RW, .accessfn = e2h_access,
7681 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7682 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7683 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7684 .type = ARM_CP_IO | ARM_CP_ALIAS,
7685 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7686 .access = PL2_RW, .accessfn = e2h_access,
7687 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7688 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7689 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7690 .type = ARM_CP_IO | ARM_CP_ALIAS,
7691 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7692 .access = PL2_RW, .accessfn = e2h_access,
7693 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7694#endif
e2a1a461
RH
7695};
7696
04b07d29
RH
7697#ifndef CONFIG_USER_ONLY
7698static const ARMCPRegInfo ats1e1_reginfo[] = {
7699 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7700 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7701 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7702 .writefn = ats_write64 },
7703 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7704 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7705 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7706 .writefn = ats_write64 },
04b07d29
RH
7707};
7708
7709static const ARMCPRegInfo ats1cp_reginfo[] = {
7710 { .name = "ATS1CPRP",
7711 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7712 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7713 .writefn = ats_write },
7714 { .name = "ATS1CPWP",
7715 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7716 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7717 .writefn = ats_write },
04b07d29
RH
7718};
7719#endif
7720
f6287c24
PM
7721/*
7722 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7723 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7724 * is non-zero, which is never for ARMv7, optionally in ARMv8
7725 * and mandatorily for ARMv8.2 and up.
7726 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7727 * implementation is RAZ/WI we can ignore this detail, as we
7728 * do for ACTLR.
7729 */
7730static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7731 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7732 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7733 .access = PL1_RW, .accessfn = access_tacr,
7734 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7735 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7736 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7737 .access = PL2_RW, .type = ARM_CP_CONST,
7738 .resetvalue = 0 },
f6287c24
PM
7739};
7740
2ceb98c0
PM
7741void register_cp_regs_for_features(ARMCPU *cpu)
7742{
7743 /* Register all the coprocessor registers based on feature bits */
7744 CPUARMState *env = &cpu->env;
7745 if (arm_feature(env, ARM_FEATURE_M)) {
7746 /* M profile has no coprocessor registers */
7747 return;
7748 }
7749
e9aa6c21 7750 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6 7751 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
7752 /*
7753 * Must go early as it is full of wildcards that may be
9449fdf6
PM
7754 * overridden by later definitions.
7755 */
7756 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7757 }
7758
7d57f408 7759 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7760 /* The ID registers all have impdef reset values */
7761 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7762 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7763 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7764 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7765 .accessfn = access_aa32_tid3,
8a130a7b 7766 .resetvalue = cpu->isar.id_pfr0 },
9b37a28c
FR
7767 /*
7768 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
96a8b92e
PM
7769 * the value of the GIC field until after we define these regs.
7770 */
0ff644a7
PM
7771 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7773 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7774 .accessfn = access_aa32_tid3,
96a8b92e
PM
7775 .readfn = id_pfr1_read,
7776 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7777 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7778 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7779 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7780 .accessfn = access_aa32_tid3,
a6179538 7781 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7782 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7783 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7784 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7785 .accessfn = access_aa32_tid3,
8515a092 7786 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7787 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7789 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7790 .accessfn = access_aa32_tid3,
10054016 7791 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7792 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7793 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7794 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7795 .accessfn = access_aa32_tid3,
10054016 7796 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7797 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7798 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7799 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7800 .accessfn = access_aa32_tid3,
10054016 7801 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7802 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7803 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7804 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7805 .accessfn = access_aa32_tid3,
10054016 7806 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7807 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7809 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7810 .accessfn = access_aa32_tid3,
47576b94 7811 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7812 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7814 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7815 .accessfn = access_aa32_tid3,
47576b94 7816 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7817 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7818 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7819 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7820 .accessfn = access_aa32_tid3,
47576b94 7821 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7822 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7823 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7824 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7825 .accessfn = access_aa32_tid3,
47576b94 7826 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7827 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7829 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7830 .accessfn = access_aa32_tid3,
47576b94 7831 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7832 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7833 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7834 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7835 .accessfn = access_aa32_tid3,
47576b94 7836 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7837 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7838 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7839 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7840 .accessfn = access_aa32_tid3,
10054016 7841 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7842 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7843 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7844 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7845 .accessfn = access_aa32_tid3,
47576b94 7846 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7847 };
7848 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7849 define_arm_cp_regs(cpu, v6_cp_reginfo);
7850 } else {
7851 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7852 }
4d31c596
PM
7853 if (arm_feature(env, ARM_FEATURE_V6K)) {
7854 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7855 }
5e5cf9e3 7856 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7857 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7858 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7859 }
327dd510
AL
7860 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7861 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7862 }
e9aa6c21 7863 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7864 ARMCPRegInfo clidr = {
7da845b0
PM
7865 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7866 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d 7867 .access = PL1_R, .type = ARM_CP_CONST,
e2ce5fcd 7868 .accessfn = access_tid4,
630fcd4d 7869 .resetvalue = cpu->clidr
776d4e5c 7870 };
776d4e5c 7871 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7872 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7873 define_debug_regs(cpu);
24183fb6 7874 define_pmu_regs(cpu);
7d57f408
PM
7875 } else {
7876 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7877 }
b0d2b7d0 7878 if (arm_feature(env, ARM_FEATURE_V8)) {
dde4d028
PM
7879 /*
7880 * v8 ID registers, which all have impdef reset values.
e20d84c1
PM
7881 * Note that within the ID register ranges the unused slots
7882 * must all RAZ, not UNDEF; future architecture versions may
7883 * define new registers here.
dde4d028
PM
7884 * ID registers which are AArch64 views of the AArch32 ID registers
7885 * which already existed in v6 and v7 are handled elsewhere,
7886 * in v6_idregs[].
e20d84c1 7887 */
dde4d028 7888 int i;
e60cef86 7889 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7890 /*
7891 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7892 * emulation because we don't know the right value for the
7893 * GIC field until after we define these regs.
96a8b92e 7894 */
e60cef86
PM
7895 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7896 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7897 .access = PL1_R,
7898#ifdef CONFIG_USER_ONLY
7899 .type = ARM_CP_CONST,
7900 .resetvalue = cpu->isar.id_aa64pfr0
7901#else
7902 .type = ARM_CP_NO_RAW,
6a4ef4e5 7903 .accessfn = access_aa64_tid3,
96a8b92e 7904 .readfn = id_aa64pfr0_read,
976b99b6
AB
7905 .writefn = arm_cp_write_ignore
7906#endif
7907 },
e60cef86
PM
7908 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7909 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7910 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7911 .accessfn = access_aa64_tid3,
47576b94 7912 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7913 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7914 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7915 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7916 .accessfn = access_aa64_tid3,
e20d84c1
PM
7917 .resetvalue = 0 },
7918 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7919 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7920 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7921 .accessfn = access_aa64_tid3,
e20d84c1 7922 .resetvalue = 0 },
9516d772 7923 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7924 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7925 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7926 .accessfn = access_aa64_tid3,
2dc10fa2 7927 .resetvalue = cpu->isar.id_aa64zfr0 },
414c54d5 7928 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7930 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7931 .accessfn = access_aa64_tid3,
414c54d5 7932 .resetvalue = cpu->isar.id_aa64smfr0 },
e20d84c1
PM
7933 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7935 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7936 .accessfn = access_aa64_tid3,
e20d84c1
PM
7937 .resetvalue = 0 },
7938 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7939 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7940 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7941 .accessfn = access_aa64_tid3,
e20d84c1 7942 .resetvalue = 0 },
e60cef86
PM
7943 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7945 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7946 .accessfn = access_aa64_tid3,
2a609df8 7947 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7948 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7950 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7951 .accessfn = access_aa64_tid3,
2a609df8 7952 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7953 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7954 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7955 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7956 .accessfn = access_aa64_tid3,
e20d84c1
PM
7957 .resetvalue = 0 },
7958 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7959 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7960 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7961 .accessfn = access_aa64_tid3,
e20d84c1 7962 .resetvalue = 0 },
e60cef86
PM
7963 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7965 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7966 .accessfn = access_aa64_tid3,
e60cef86
PM
7967 .resetvalue = cpu->id_aa64afr0 },
7968 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7969 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7970 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7971 .accessfn = access_aa64_tid3,
e60cef86 7972 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7973 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7974 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7975 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7976 .accessfn = access_aa64_tid3,
e20d84c1
PM
7977 .resetvalue = 0 },
7978 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7979 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7980 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7981 .accessfn = access_aa64_tid3,
e20d84c1 7982 .resetvalue = 0 },
e60cef86
PM
7983 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7985 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7986 .accessfn = access_aa64_tid3,
47576b94 7987 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7988 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7990 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7991 .accessfn = access_aa64_tid3,
47576b94 7992 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7993 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7995 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7996 .accessfn = access_aa64_tid3,
e20d84c1
PM
7997 .resetvalue = 0 },
7998 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7999 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8000 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8001 .accessfn = access_aa64_tid3,
e20d84c1
PM
8002 .resetvalue = 0 },
8003 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8005 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8006 .accessfn = access_aa64_tid3,
e20d84c1
PM
8007 .resetvalue = 0 },
8008 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8010 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8011 .accessfn = access_aa64_tid3,
e20d84c1
PM
8012 .resetvalue = 0 },
8013 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8014 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8015 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8016 .accessfn = access_aa64_tid3,
e20d84c1
PM
8017 .resetvalue = 0 },
8018 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8019 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8020 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8021 .accessfn = access_aa64_tid3,
e20d84c1 8022 .resetvalue = 0 },
e60cef86
PM
8023 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8025 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8026 .accessfn = access_aa64_tid3,
3dc91ddb 8027 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
8028 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8030 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8031 .accessfn = access_aa64_tid3,
3dc91ddb 8032 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 8033 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8034 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8035 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8036 .accessfn = access_aa64_tid3,
64761e10 8037 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
8038 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8039 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8040 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8041 .accessfn = access_aa64_tid3,
e20d84c1
PM
8042 .resetvalue = 0 },
8043 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8044 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8045 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8046 .accessfn = access_aa64_tid3,
e20d84c1
PM
8047 .resetvalue = 0 },
8048 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8049 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8050 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8051 .accessfn = access_aa64_tid3,
e20d84c1
PM
8052 .resetvalue = 0 },
8053 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8054 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8055 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8056 .accessfn = access_aa64_tid3,
e20d84c1
PM
8057 .resetvalue = 0 },
8058 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8059 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8060 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8061 .accessfn = access_aa64_tid3,
e20d84c1 8062 .resetvalue = 0 },
a50c0f51
PM
8063 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8064 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8065 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8066 .accessfn = access_aa64_tid3,
47576b94 8067 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
8068 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8069 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8070 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8071 .accessfn = access_aa64_tid3,
47576b94 8072 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
8073 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8074 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8075 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8076 .accessfn = access_aa64_tid3,
47576b94 8077 .resetvalue = cpu->isar.mvfr2 },
dde4d028
PM
8078 /*
8079 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8080 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8081 * as RAZ, since it is in the "reserved for future ID
8082 * registers, RAZ" part of the AArch32 encoding space.
8083 */
8084 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8085 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8086 .access = PL1_R, .type = ARM_CP_CONST,
8087 .accessfn = access_aa64_tid3,
8088 .resetvalue = 0 },
8089 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8090 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8091 .access = PL1_R, .type = ARM_CP_CONST,
8092 .accessfn = access_aa64_tid3,
8093 .resetvalue = 0 },
8094 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8095 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8096 .access = PL1_R, .type = ARM_CP_CONST,
8097 .accessfn = access_aa64_tid3,
8098 .resetvalue = 0 },
8099 /*
8100 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8101 * they're also RAZ for AArch64, and in v8 are gradually
8102 * being filled with AArch64-view-of-AArch32-ID-register
8103 * for new ID registers.
8104 */
8105 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8106 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8107 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8108 .accessfn = access_aa64_tid3,
e20d84c1 8109 .resetvalue = 0 },
1d51bc96 8110 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8111 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8112 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8113 .accessfn = access_aa64_tid3,
1d51bc96 8114 .resetvalue = cpu->isar.id_pfr2 },
d22c5649 8115 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8116 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8117 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8118 .accessfn = access_aa64_tid3,
d22c5649 8119 .resetvalue = cpu->isar.id_dfr1 },
32957aad 8120 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8121 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8122 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8123 .accessfn = access_aa64_tid3,
32957aad 8124 .resetvalue = cpu->isar.id_mmfr5 },
dde4d028 8125 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8126 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8127 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8128 .accessfn = access_aa64_tid3,
e20d84c1 8129 .resetvalue = 0 },
4054bfa9
AF
8130 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8131 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8132 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 8133 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
8134 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8135 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8136 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8137 .resetvalue = cpu->pmceid0 },
8138 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8139 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8140 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 8141 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
8142 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8143 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8144 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8145 .resetvalue = cpu->pmceid1 },
e60cef86 8146 };
6c5c0fec 8147#ifdef CONFIG_USER_ONLY
10b0220e 8148 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec 8149 { .name = "ID_AA64PFR0_EL1",
bc6bd20e
ZS
8150 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8151 R_ID_AA64PFR0_ADVSIMD_MASK |
8152 R_ID_AA64PFR0_SVE_MASK |
8153 R_ID_AA64PFR0_DIT_MASK,
8154 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8155 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
6c5c0fec 8156 { .name = "ID_AA64PFR1_EL1",
bc6bd20e
ZS
8157 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8158 R_ID_AA64PFR1_SSBS_MASK |
8159 R_ID_AA64PFR1_MTE_MASK |
8160 R_ID_AA64PFR1_SME_MASK },
d040242e 8161 { .name = "ID_AA64PFR*_EL1_RESERVED",
bc6bd20e
ZS
8162 .is_glob = true },
8163 { .name = "ID_AA64ZFR0_EL1",
8164 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8165 R_ID_AA64ZFR0_AES_MASK |
8166 R_ID_AA64ZFR0_BITPERM_MASK |
8167 R_ID_AA64ZFR0_BFLOAT16_MASK |
8168 R_ID_AA64ZFR0_SHA3_MASK |
8169 R_ID_AA64ZFR0_SM4_MASK |
8170 R_ID_AA64ZFR0_I8MM_MASK |
8171 R_ID_AA64ZFR0_F32MM_MASK |
8172 R_ID_AA64ZFR0_F64MM_MASK },
8173 { .name = "ID_AA64SMFR0_EL1",
8174 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8175 R_ID_AA64SMFR0_B16F32_MASK |
8176 R_ID_AA64SMFR0_F16F32_MASK |
8177 R_ID_AA64SMFR0_I8I32_MASK |
8178 R_ID_AA64SMFR0_F64F64_MASK |
8179 R_ID_AA64SMFR0_I16I64_MASK |
8180 R_ID_AA64SMFR0_FA64_MASK },
6c5c0fec 8181 { .name = "ID_AA64MMFR0_EL1",
bc6bd20e
ZS
8182 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8183 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8184 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8185 { .name = "ID_AA64MMFR1_EL1",
8186 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8187 { .name = "ID_AA64MMFR2_EL1",
8188 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
d040242e 8189 { .name = "ID_AA64MMFR*_EL1_RESERVED",
bc6bd20e 8190 .is_glob = true },
6c5c0fec 8191 { .name = "ID_AA64DFR0_EL1",
bc6bd20e
ZS
8192 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8193 { .name = "ID_AA64DFR1_EL1" },
d040242e 8194 { .name = "ID_AA64DFR*_EL1_RESERVED",
bc6bd20e 8195 .is_glob = true },
d040242e 8196 { .name = "ID_AA64AFR*",
bc6bd20e 8197 .is_glob = true },
6c5c0fec 8198 { .name = "ID_AA64ISAR0_EL1",
bc6bd20e
ZS
8199 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8200 R_ID_AA64ISAR0_SHA1_MASK |
8201 R_ID_AA64ISAR0_SHA2_MASK |
8202 R_ID_AA64ISAR0_CRC32_MASK |
8203 R_ID_AA64ISAR0_ATOMIC_MASK |
8204 R_ID_AA64ISAR0_RDM_MASK |
8205 R_ID_AA64ISAR0_SHA3_MASK |
8206 R_ID_AA64ISAR0_SM3_MASK |
8207 R_ID_AA64ISAR0_SM4_MASK |
8208 R_ID_AA64ISAR0_DP_MASK |
8209 R_ID_AA64ISAR0_FHM_MASK |
8210 R_ID_AA64ISAR0_TS_MASK |
8211 R_ID_AA64ISAR0_RNDR_MASK },
6c5c0fec 8212 { .name = "ID_AA64ISAR1_EL1",
bc6bd20e
ZS
8213 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8214 R_ID_AA64ISAR1_APA_MASK |
8215 R_ID_AA64ISAR1_API_MASK |
8216 R_ID_AA64ISAR1_JSCVT_MASK |
8217 R_ID_AA64ISAR1_FCMA_MASK |
8218 R_ID_AA64ISAR1_LRCPC_MASK |
8219 R_ID_AA64ISAR1_GPA_MASK |
8220 R_ID_AA64ISAR1_GPI_MASK |
8221 R_ID_AA64ISAR1_FRINTTS_MASK |
8222 R_ID_AA64ISAR1_SB_MASK |
8223 R_ID_AA64ISAR1_BF16_MASK |
8224 R_ID_AA64ISAR1_DGH_MASK |
8225 R_ID_AA64ISAR1_I8MM_MASK },
8226 { .name = "ID_AA64ISAR2_EL1",
8227 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8228 R_ID_AA64ISAR2_RPRES_MASK |
8229 R_ID_AA64ISAR2_GPA3_MASK |
8230 R_ID_AA64ISAR2_APA3_MASK },
d040242e 8231 { .name = "ID_AA64ISAR*_EL1_RESERVED",
bc6bd20e 8232 .is_glob = true },
6c5c0fec
AB
8233 };
8234 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8235#endif
be8e8128
GB
8236 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8237 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8238 !arm_feature(env, ARM_FEATURE_EL2)) {
8239 ARMCPRegInfo rvbar = {
910e4f24 8240 .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
be8e8128 8241 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8242 .access = PL1_R,
8243 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
8244 };
8245 define_one_arm_cp_reg(cpu, &rvbar);
8246 }
e60cef86 8247 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0 8248 define_arm_cp_regs(cpu, v8_cp_reginfo);
dde4d028
PM
8249
8250 for (i = 4; i < 16; i++) {
8251 /*
8252 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8253 * For pre-v8 cores there are RAZ patterns for these in
8254 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8255 * v8 extends the "must RAZ" part of the ID register space
8256 * to also cover c0, 0, c{8-15}, {0-7}.
8257 * These are STATE_AA32 because in the AArch64 sysreg space
8258 * c4-c7 is where the AArch64 ID registers live (and we've
8259 * already defined those in v8_idregs[]), and c8-c15 are not
8260 * "must RAZ" for AArch64.
8261 */
8262 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8263 ARMCPRegInfo v8_aa32_raz_idregs = {
8264 .name = name,
8265 .state = ARM_CP_STATE_AA32,
8266 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8267 .access = PL1_R, .type = ARM_CP_CONST,
8268 .accessfn = access_aa64_tid3,
8269 .resetvalue = 0 };
8270 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8271 }
b0d2b7d0 8272 }
99a90811
RH
8273
8274 /*
8275 * Register the base EL2 cpregs.
8276 * Pre v8, these registers are implemented only as part of the
8277 * Virtualization Extensions (EL2 present). Beginning with v8,
8278 * if EL2 is missing but EL3 is enabled, mostly these become
8279 * RES0 from EL3, with some specific exceptions.
8280 */
8281 if (arm_feature(env, ARM_FEATURE_EL2)
8282 || (arm_feature(env, ARM_FEATURE_EL3)
8283 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 8284 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
8285 ARMCPRegInfo vpidr_regs[] = {
8286 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8287 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8288 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8289 .resetvalue = cpu->midr,
8290 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8291 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
8292 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8293 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8294 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 8295 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 8296 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
8297 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8298 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8299 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8300 .resetvalue = vmpidr_def,
8301 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8302 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
8303 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8304 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
8305 .access = PL2_RW, .resetvalue = vmpidr_def,
8306 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 8307 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6 8308 };
24526bb9
PM
8309 /*
8310 * The only field of MDCR_EL2 that has a defined architectural reset
8311 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8312 */
8313 ARMCPRegInfo mdcr_el2 = {
7f4fbfb5 8314 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
24526bb9 8315 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
01765386 8316 .writefn = mdcr_el2_write,
24526bb9
PM
8317 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8318 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8319 };
8320 define_one_arm_cp_reg(cpu, &mdcr_el2);
731de9e6 8321 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 8322 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
8323 if (arm_feature(env, ARM_FEATURE_V8)) {
8324 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8325 }
e9152ee9
RDC
8326 if (cpu_isar_feature(aa64_sel2, cpu)) {
8327 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8328 }
be8e8128
GB
8329 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8330 if (!arm_feature(env, ARM_FEATURE_EL3)) {
910e4f24
TR
8331 ARMCPRegInfo rvbar[] = {
8332 {
8333 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8334 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8335 .access = PL2_R,
8336 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8337 },
8338 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8339 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8340 .access = PL2_R,
8341 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8342 },
be8e8128 8343 };
910e4f24 8344 define_arm_cp_regs(cpu, rvbar);
be8e8128 8345 }
3b685ba7 8346 }
99a90811
RH
8347
8348 /* Register the base EL3 cpregs. */
81547d66 8349 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 8350 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
8351 ARMCPRegInfo el3_regs[] = {
8352 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8353 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8354 .access = PL3_R,
8355 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8356 },
e24fdd23
PM
8357 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8358 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8359 .access = PL3_RW,
8360 .raw_writefn = raw_write, .writefn = sctlr_write,
8361 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8362 .resetvalue = cpu->reset_sctlr },
be8e8128 8363 };
e24fdd23
PM
8364
8365 define_arm_cp_regs(cpu, el3_regs);
81547d66 8366 }
9b37a28c
FR
8367 /*
8368 * The behaviour of NSACR is sufficiently various that we don't
2f027fc5
PM
8369 * try to describe it in a single reginfo:
8370 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8371 * reads as constant 0xc00 from NS EL1 and NS EL2
8372 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8373 * if v7 without EL3, register doesn't exist
8374 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8375 */
8376 if (arm_feature(env, ARM_FEATURE_EL3)) {
8377 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 8378 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8379 .name = "NSACR", .type = ARM_CP_CONST,
8380 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8381 .access = PL1_RW, .accessfn = nsacr_access,
8382 .resetvalue = 0xc00
8383 };
8384 define_one_arm_cp_reg(cpu, &nsacr);
8385 } else {
10b0220e 8386 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8387 .name = "NSACR",
8388 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8389 .access = PL3_RW | PL1_R,
8390 .resetvalue = 0,
8391 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8392 };
8393 define_one_arm_cp_reg(cpu, &nsacr);
8394 }
8395 } else {
8396 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 8397 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8398 .name = "NSACR", .type = ARM_CP_CONST,
8399 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8400 .access = PL1_R,
8401 .resetvalue = 0xc00
8402 };
8403 define_one_arm_cp_reg(cpu, &nsacr);
8404 }
8405 }
8406
452a0955 8407 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8408 if (arm_feature(env, ARM_FEATURE_V6)) {
8409 /* PMSAv6 not implemented */
8410 assert(arm_feature(env, ARM_FEATURE_V7));
8411 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8412 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8413 } else {
8414 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8415 }
18032bec 8416 } else {
8e5d75c9 8417 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8418 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8419 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8420 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8421 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8422 }
18032bec 8423 }
c326b979
PM
8424 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8425 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8426 }
6cc7a3ae
PM
8427 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8428 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8429 }
4a501606
PM
8430 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8431 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8432 }
c4804214
PM
8433 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8434 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8435 }
8436 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8437 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8438 }
8439 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8440 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8441 }
18032bec
PM
8442 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8443 define_arm_cp_regs(cpu, omap_cp_reginfo);
8444 }
34f90529
PM
8445 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8446 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8447 }
1047b9d7
PM
8448 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8449 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8450 }
8451 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8452 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8453 }
7ac681cf
PM
8454 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8455 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8456 }
873b73c0 8457 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8458 define_arm_cp_regs(cpu, jazelle_regs);
8459 }
9b37a28c
FR
8460 /*
8461 * Slightly awkwardly, the OMAP and StrongARM cores need all of
7884849c
PM
8462 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8463 * be read-only (ie write causes UNDEF exception).
8464 */
8465 {
00a29f3d 8466 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9b37a28c
FR
8467 /*
8468 * Pre-v8 MIDR space.
00a29f3d 8469 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8470 * of the TI925 behaviour where writes to another register can
8471 * cause the MIDR value to change.
97ce8d61
PC
8472 *
8473 * Unimplemented registers in the c15 0 0 0 space default to
8474 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8475 * and friends override accordingly.
7884849c
PM
8476 */
8477 { .name = "MIDR",
97ce8d61 8478 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8479 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8480 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8481 .readfn = midr_read,
97ce8d61
PC
8482 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8483 .type = ARM_CP_OVERRIDE },
7884849c
PM
8484 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8485 { .name = "DUMMY",
8486 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8487 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8488 { .name = "DUMMY",
8489 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8490 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8491 { .name = "DUMMY",
8492 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8493 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8494 { .name = "DUMMY",
8495 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8496 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8497 { .name = "DUMMY",
8498 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8499 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8500 };
00a29f3d 8501 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8502 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8503 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
8504 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8505 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8506 .readfn = midr_read },
c7f786ab 8507 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
ac00c79f
SF
8508 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8509 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8510 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8511 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8512 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8513 .access = PL1_R,
8514 .accessfn = access_aa64_tid1,
8515 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d 8516 };
c7f786ab
TR
8517 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8518 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8519 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8520 .access = PL1_R, .resetvalue = cpu->midr
8521 };
00a29f3d
PM
8522 ARMCPRegInfo id_cp_reginfo[] = {
8523 /* These are common to v8 and pre-v8 */
8524 { .name = "CTR",
8525 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8526 .access = PL1_R, .accessfn = ctr_el0_access,
8527 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8528 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8529 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8530 .access = PL0_R, .accessfn = ctr_el0_access,
8531 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8532 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8533 { .name = "TCMTR",
8534 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8535 .access = PL1_R,
8536 .accessfn = access_aa32_tid1,
8537 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8538 };
8085ce63
PC
8539 /* TLBTR is specific to VMSA */
8540 ARMCPRegInfo id_tlbtr_reginfo = {
8541 .name = "TLBTR",
8542 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8543 .access = PL1_R,
8544 .accessfn = access_aa32_tid1,
8545 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8546 };
3281af81
PC
8547 /* MPUIR is specific to PMSA V6+ */
8548 ARMCPRegInfo id_mpuir_reginfo = {
8549 .name = "MPUIR",
8550 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8551 .access = PL1_R, .type = ARM_CP_CONST,
8552 .resetvalue = cpu->pmsav7_dregion << 8
8553 };
761c4642
TR
8554 /* HMPUIR is specific to PMSA V8 */
8555 ARMCPRegInfo id_hmpuir_reginfo = {
8556 .name = "HMPUIR",
8557 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8558 .access = PL2_R, .type = ARM_CP_CONST,
8559 .resetvalue = cpu->pmsav8r_hdregion
8560 };
10b0220e 8561 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8562 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8563 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8564 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8565 };
6c5c0fec 8566#ifdef CONFIG_USER_ONLY
10b0220e 8567 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec 8568 { .name = "MIDR_EL1",
bc6bd20e
ZS
8569 .exported_bits = R_MIDR_EL1_REVISION_MASK |
8570 R_MIDR_EL1_PARTNUM_MASK |
8571 R_MIDR_EL1_ARCHITECTURE_MASK |
8572 R_MIDR_EL1_VARIANT_MASK |
8573 R_MIDR_EL1_IMPLEMENTER_MASK },
8574 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8575 };
8576 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8577#endif
7884849c
PM
8578 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8579 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8580 size_t i;
9b37a28c
FR
8581 /*
8582 * Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8583 * whole space. Then update the specific ID registers to allow write
8584 * access, so that they ignore writes rather than causing them to
8585 * UNDEF.
7884849c
PM
8586 */
8587 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8588 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8589 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8590 }
5809ac57
RH
8591 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8592 id_cp_reginfo[i].access = PL1_RW;
7884849c 8593 }
10006112 8594 id_mpuir_reginfo.access = PL1_RW;
3281af81 8595 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8596 }
00a29f3d
PM
8597 if (arm_feature(env, ARM_FEATURE_V8)) {
8598 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
c7f786ab
TR
8599 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8600 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8601 }
00a29f3d
PM
8602 } else {
8603 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8604 }
a703eda1 8605 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8606 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8607 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
761c4642
TR
8608 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8609 arm_feature(env, ARM_FEATURE_V8)) {
8610 uint32_t i = 0;
8611 char *tmp_string;
8612
8613 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8614 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8615 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8616
8617 /* Register alias is only valid for first 32 indexes */
8618 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8619 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8620 uint8_t opc1 = extract32(i, 4, 1);
8621 uint8_t opc2 = extract32(i, 0, 1) << 2;
8622
8623 tmp_string = g_strdup_printf("PRBAR%u", i);
8624 ARMCPRegInfo tmp_prbarn_reginfo = {
8625 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8626 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8627 .access = PL1_RW, .resetvalue = 0,
8628 .accessfn = access_tvm_trvm,
8629 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8630 };
8631 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8632 g_free(tmp_string);
8633
8634 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8635 tmp_string = g_strdup_printf("PRLAR%u", i);
8636 ARMCPRegInfo tmp_prlarn_reginfo = {
8637 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8638 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8639 .access = PL1_RW, .resetvalue = 0,
8640 .accessfn = access_tvm_trvm,
8641 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8642 };
8643 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8644 g_free(tmp_string);
8645 }
8646
8647 /* Register alias is only valid for first 32 indexes */
8648 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8649 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8650 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8651 uint8_t opc2 = extract32(i, 0, 1) << 2;
8652
8653 tmp_string = g_strdup_printf("HPRBAR%u", i);
8654 ARMCPRegInfo tmp_hprbarn_reginfo = {
8655 .name = tmp_string,
8656 .type = ARM_CP_NO_RAW,
8657 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8658 .access = PL2_RW, .resetvalue = 0,
8659 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8660 };
8661 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8662 g_free(tmp_string);
8663
8664 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8665 tmp_string = g_strdup_printf("HPRLAR%u", i);
8666 ARMCPRegInfo tmp_hprlarn_reginfo = {
8667 .name = tmp_string,
8668 .type = ARM_CP_NO_RAW,
8669 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8670 .access = PL2_RW, .resetvalue = 0,
8671 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8672 };
8673 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8674 g_free(tmp_string);
8675 }
3281af81
PC
8676 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8677 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8678 }
7884849c
PM
8679 }
8680
97ce8d61 8681 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8682 ARMCPRegInfo mpidr_cp_reginfo[] = {
8683 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8684 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8685 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8686 };
8687#ifdef CONFIG_USER_ONLY
10b0220e 8688 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8689 { .name = "MPIDR_EL1",
8690 .fixed_bits = 0x0000000080000000 },
52264166
AB
8691 };
8692 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8693#endif
97ce8d61
PC
8694 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8695 }
8696
2771db27 8697 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8698 ARMCPRegInfo auxcr_reginfo[] = {
8699 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8700 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8701 .access = PL1_RW, .accessfn = access_tacr,
8702 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8703 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8704 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8705 .access = PL2_RW, .type = ARM_CP_CONST,
8706 .resetvalue = 0 },
8707 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8708 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8709 .access = PL3_RW, .type = ARM_CP_CONST,
8710 .resetvalue = 0 },
2771db27 8711 };
834a6c69 8712 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8713 if (cpu_isar_feature(aa32_ac2, cpu)) {
8714 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8715 }
2771db27
PM
8716 }
8717
d8ba780b 8718 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8719 /*
8720 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8721 * There are two flavours:
8722 * (1) older 32-bit only cores have a simple 32-bit CBAR
8723 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8724 * 32-bit register visible to AArch32 at a different encoding
8725 * to the "flavour 1" register and with the bits rearranged to
8726 * be able to squash a 64-bit address into the 32-bit view.
8727 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8728 * in future if we support AArch32-only configs of some of the
8729 * AArch64 cores we might need to add a specific feature flag
8730 * to indicate cores with "flavour 2" CBAR.
8731 */
f318cec6
PM
8732 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8733 /* 32 bit view is [31:18] 0...0 [43:32]. */
8734 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8735 | extract64(cpu->reset_cbar, 32, 12);
8736 ARMCPRegInfo cbar_reginfo[] = {
8737 { .name = "CBAR",
8738 .type = ARM_CP_CONST,
d56974af
LM
8739 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8740 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8741 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8742 .type = ARM_CP_CONST,
8743 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8744 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8745 };
8746 /* We don't implement a r/w 64 bit CBAR currently */
8747 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8748 define_arm_cp_regs(cpu, cbar_reginfo);
8749 } else {
8750 ARMCPRegInfo cbar = {
8751 .name = "CBAR",
8752 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
04215eb1 8753 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
f318cec6
PM
8754 .fieldoffset = offsetof(CPUARMState,
8755 cp15.c15_config_base_address)
8756 };
8757 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8758 cbar.access = PL1_R;
8759 cbar.fieldoffset = 0;
8760 cbar.type = ARM_CP_CONST;
8761 }
8762 define_one_arm_cp_reg(cpu, &cbar);
8763 }
d8ba780b
PC
8764 }
8765
91db4642 8766 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8767 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8768 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8769 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8770 .access = PL1_RW, .writefn = vbar_write,
8771 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8772 offsetof(CPUARMState, cp15.vbar_ns) },
8773 .resetvalue = 0 },
91db4642
CLG
8774 };
8775 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8776 }
8777
2771db27
PM
8778 /* Generic registers whose values depend on the implementation */
8779 {
8780 ARMCPRegInfo sctlr = {
5ebafdf3 8781 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8782 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8783 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8784 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8785 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8786 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8787 .raw_writefn = raw_write,
2771db27
PM
8788 };
8789 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9b37a28c
FR
8790 /*
8791 * Normally we would always end the TB on an SCTLR write, but Linux
2771db27
PM
8792 * arch/arm/mach-pxa/sleep.S expects two instructions following
8793 * an MMU enable to execute from cache. Imitate this behaviour.
8794 */
8795 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8796 }
8797 define_one_arm_cp_reg(cpu, &sctlr);
761c4642
TR
8798
8799 if (arm_feature(env, ARM_FEATURE_PMSA) &&
8800 arm_feature(env, ARM_FEATURE_V8)) {
8801 ARMCPRegInfo vsctlr = {
8802 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8803 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8804 .access = PL2_RW, .resetvalue = 0x0,
8805 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8806 };
8807 define_one_arm_cp_reg(cpu, &vsctlr);
8808 }
2771db27 8809 }
5be5e8ed 8810
2d7137c1 8811 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8812 define_arm_cp_regs(cpu, lor_reginfo);
8813 }
220f508f
RH
8814 if (cpu_isar_feature(aa64_pan, cpu)) {
8815 define_one_arm_cp_reg(cpu, &pan_reginfo);
8816 }
04b07d29
RH
8817#ifndef CONFIG_USER_ONLY
8818 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8819 define_arm_cp_regs(cpu, ats1e1_reginfo);
8820 }
8821 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8822 define_arm_cp_regs(cpu, ats1cp_reginfo);
8823 }
8824#endif
9eeb7a1c
RH
8825 if (cpu_isar_feature(aa64_uao, cpu)) {
8826 define_one_arm_cp_reg(cpu, &uao_reginfo);
8827 }
2d7137c1 8828
dc8b1853
RC
8829 if (cpu_isar_feature(aa64_dit, cpu)) {
8830 define_one_arm_cp_reg(cpu, &dit_reginfo);
8831 }
f2f68a78
RC
8832 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8833 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8834 }
58e93b48
RH
8835 if (cpu_isar_feature(any_ras, cpu)) {
8836 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8837 }
dc8b1853 8838
52d18727
RH
8839 if (cpu_isar_feature(aa64_vh, cpu) ||
8840 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8841 define_one_arm_cp_reg(cpu, &contextidr_el2);
8842 }
e2a1a461
RH
8843 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8844 define_arm_cp_regs(cpu, vhe_reginfo);
8845 }
8846
cd208a1c 8847 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 8848 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 8849 }
967aa94f 8850
5814d587
RH
8851 if (cpu_isar_feature(aa64_hcx, cpu)) {
8852 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8853 }
8854
967aa94f 8855#ifdef TARGET_AARCH64
9e5ec745
RH
8856 if (cpu_isar_feature(aa64_sme, cpu)) {
8857 define_arm_cp_regs(cpu, sme_reginfo);
8858 }
967aa94f
RH
8859 if (cpu_isar_feature(aa64_pauth, cpu)) {
8860 define_arm_cp_regs(cpu, pauth_reginfo);
8861 }
de390645
RH
8862 if (cpu_isar_feature(aa64_rndr, cpu)) {
8863 define_arm_cp_regs(cpu, rndr_reginfo);
8864 }
84940ed8
RC
8865 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8866 define_arm_cp_regs(cpu, tlbirange_reginfo);
8867 }
7113d618
RC
8868 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8869 define_arm_cp_regs(cpu, tlbios_reginfo);
8870 }
0d57b499
BM
8871#ifndef CONFIG_USER_ONLY
8872 /* Data Cache clean instructions up to PoP */
8873 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8874 define_one_arm_cp_reg(cpu, dcpop_reg);
8875
8876 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8877 define_one_arm_cp_reg(cpu, dcpodp_reg);
8878 }
8879 }
8880#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8881
8882 /*
8883 * If full MTE is enabled, add all of the system registers.
8884 * If only "instructions available at EL0" are enabled,
8885 * then define only a RAZ/WI version of PSTATE.TCO.
8886 */
8887 if (cpu_isar_feature(aa64_mte, cpu)) {
8888 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8889 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8890 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8891 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8892 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8893 }
7cb1e618
RH
8894
8895 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8896 define_arm_cp_regs(cpu, scxtnum_reginfo);
8897 }
967aa94f 8898#endif
cb570bd3 8899
22e57073 8900 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8901 define_arm_cp_regs(cpu, predinv_reginfo);
8902 }
e2cce18f 8903
957e6155
PM
8904 if (cpu_isar_feature(any_ccidx, cpu)) {
8905 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8906 }
8907
e2cce18f
RH
8908#ifndef CONFIG_USER_ONLY
8909 /*
8910 * Register redirections and aliases must be done last,
8911 * after the registers from the other extensions have been defined.
8912 */
8913 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8914 define_arm_vh_e2h_redirects_aliases(cpu);
8915 }
8916#endif
2ceb98c0
PM
8917}
8918
777dc784
PM
8919/* Sort alphabetically by type name, except for "any". */
8920static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8921{
777dc784
PM
8922 ObjectClass *class_a = (ObjectClass *)a;
8923 ObjectClass *class_b = (ObjectClass *)b;
8924 const char *name_a, *name_b;
5adb4839 8925
777dc784
PM
8926 name_a = object_class_get_name(class_a);
8927 name_b = object_class_get_name(class_b);
51492fd1 8928 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8929 return 1;
51492fd1 8930 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8931 return -1;
8932 } else {
8933 return strcmp(name_a, name_b);
5adb4839
PB
8934 }
8935}
8936
777dc784 8937static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8938{
777dc784 8939 ObjectClass *oc = data;
977c33ba 8940 CPUClass *cc = CPU_CLASS(oc);
51492fd1
AF
8941 const char *typename;
8942 char *name;
3371d272 8943
51492fd1
AF
8944 typename = object_class_get_name(oc);
8945 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
977c33ba
DB
8946 if (cc->deprecation_note) {
8947 qemu_printf(" %s (deprecated)\n", name);
8948 } else {
8949 qemu_printf(" %s\n", name);
8950 }
51492fd1 8951 g_free(name);
777dc784
PM
8952}
8953
0442428a 8954void arm_cpu_list(void)
777dc784 8955{
777dc784
PM
8956 GSList *list;
8957
8958 list = object_class_get_list(TYPE_ARM_CPU, false);
8959 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8960 qemu_printf("Available CPUs:\n");
8961 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8962 g_slist_free(list);
40f137e1
PB
8963}
8964
78027bb6
CR
8965static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8966{
8967 ObjectClass *oc = data;
8968 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
8969 CpuDefinitionInfo *info;
8970 const char *typename;
8971
8972 typename = object_class_get_name(oc);
8973 info = g_malloc0(sizeof(*info));
8974 info->name = g_strndup(typename,
8975 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8976 info->q_typename = g_strdup(typename);
78027bb6 8977
54aa3de7 8978 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
8979}
8980
25a9d6ca 8981CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8982{
8983 CpuDefinitionInfoList *cpu_list = NULL;
8984 GSList *list;
8985
8986 list = object_class_get_list(TYPE_ARM_CPU, false);
8987 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8988 g_slist_free(list);
8989
8990 return cpu_list;
8991}
8992
1859f8c3
RH
8993/*
8994 * Private utility function for define_one_arm_cp_reg_with_opaque():
8995 * add a single reginfo struct to the hash table.
8996 */
6e6efd61 8997static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
8998 void *opaque, CPState state,
8999 CPSecureState secstate,
9c513e78
AB
9000 int crm, int opc1, int opc2,
9001 const char *name)
6e6efd61 9002{
696ba377 9003 CPUARMState *env = &cpu->env;
5860362d 9004 uint32_t key;
c27f5d3a 9005 ARMCPRegInfo *r2;
4c8c4541
RH
9006 bool is64 = r->type & ARM_CP_64BIT;
9007 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 9008 int cp = r->cp;
c27f5d3a 9009 size_t name_len;
696ba377 9010 bool make_const;
c27f5d3a 9011
cac65299
RH
9012 switch (state) {
9013 case ARM_CP_STATE_AA32:
9014 /* We assume it is a cp15 register if the .cp field is left unset. */
9015 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9016 cp = 15;
9017 }
9018 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9019 break;
9020 case ARM_CP_STATE_AA64:
9021 /*
9022 * To allow abbreviation of ARMCPRegInfo definitions, we treat
9023 * cp == 0 as equivalent to the value for "standard guest-visible
9024 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
9025 * in their AArch64 view (the .cp value may be non-zero for the
9026 * benefit of the AArch32 view).
9027 */
9028 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9029 cp = CP_REG_ARM64_SYSREG_CP;
9030 }
9031 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9032 break;
9033 default:
9034 g_assert_not_reached();
9035 }
9036
dc44545b
RH
9037 /* Overriding of an existing definition must be explicitly requested. */
9038 if (!(r->type & ARM_CP_OVERRIDE)) {
9039 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9040 if (oldreg) {
9041 assert(oldreg->type & ARM_CP_OVERRIDE);
9042 }
9043 }
9044
696ba377
RH
9045 /*
9046 * Eliminate registers that are not present because the EL is missing.
9047 * Doing this here makes it easier to put all registers for a given
9048 * feature into the same ARMCPRegInfo array and define them all at once.
9049 */
9050 make_const = false;
9051 if (arm_feature(env, ARM_FEATURE_EL3)) {
9052 /*
9053 * An EL2 register without EL2 but with EL3 is (usually) RES0.
9054 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9055 */
9056 int min_el = ctz32(r->access) / 2;
9057 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9058 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9059 return;
9060 }
9061 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9062 }
9063 } else {
9064 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9065 ? PL2_RW : PL1_RW);
9066 if ((r->access & max_el) == 0) {
9067 return;
9068 }
9069 }
9070
c27f5d3a
RH
9071 /* Combine cpreg and name into one allocation. */
9072 name_len = strlen(name) + 1;
9073 r2 = g_malloc(sizeof(*r2) + name_len);
9074 *r2 = *r;
9075 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 9076
cc946d96
RH
9077 /*
9078 * Update fields to match the instantiation, overwiting wildcards
9079 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 9080 */
cc946d96
RH
9081 r2->cp = cp;
9082 r2->crm = crm;
9083 r2->opc1 = opc1;
9084 r2->opc2 = opc2;
9085 r2->state = state;
3f3c82a5 9086 r2->secure = secstate;
cc946d96
RH
9087 if (opaque) {
9088 r2->opaque = opaque;
9089 }
3f3c82a5 9090
696ba377
RH
9091 if (make_const) {
9092 /* This should not have been a very special register to begin. */
9093 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9094 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 9095 /*
696ba377
RH
9096 * Set the special function to CONST, retaining the other flags.
9097 * This is important for e.g. ARM_CP_SVE so that we still
9098 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 9099 */
696ba377
RH
9100 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9101 /*
9102 * Usually, these registers become RES0, but there are a few
9103 * special cases like VPIDR_EL2 which have a constant non-zero
9104 * value with writes ignored.
9105 */
9106 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9107 r2->resetvalue = 0;
9108 }
9109 /*
9110 * ARM_CP_CONST has precedence, so removing the callbacks and
9111 * offsets are not strictly necessary, but it is potentially
9112 * less confusing to debug later.
9113 */
9114 r2->readfn = NULL;
9115 r2->writefn = NULL;
9116 r2->raw_readfn = NULL;
9117 r2->raw_writefn = NULL;
9118 r2->resetfn = NULL;
9119 r2->fieldoffset = 0;
9120 r2->bank_fieldoffsets[0] = 0;
9121 r2->bank_fieldoffsets[1] = 0;
9122 } else {
9123 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 9124
10748a96 9125 if (isbanked) {
1859f8c3 9126 /*
696ba377
RH
9127 * Register is banked (using both entries in array).
9128 * Overwriting fieldoffset as the array is only used to define
9129 * banked registers but later only fieldoffset is used.
3f3c82a5 9130 */
696ba377
RH
9131 r2->fieldoffset = r->bank_fieldoffsets[ns];
9132 }
9133 if (state == ARM_CP_STATE_AA32) {
9134 if (isbanked) {
9135 /*
9136 * If the register is banked then we don't need to migrate or
9137 * reset the 32-bit instance in certain cases:
9138 *
9139 * 1) If the register has both 32-bit and 64-bit instances
9140 * then we can count on the 64-bit instance taking care
9141 * of the non-secure bank.
9142 * 2) If ARMv8 is enabled then we can count on a 64-bit
9143 * version taking care of the secure bank. This requires
9144 * that separate 32 and 64-bit definitions are provided.
9145 */
9146 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9147 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9148 r2->type |= ARM_CP_ALIAS;
9149 }
9150 } else if ((secstate != r->secure) && !ns) {
9151 /*
9152 * The register is not banked so we only want to allow
9153 * migration of the non-secure instance.
9154 */
7a0e58fa 9155 r2->type |= ARM_CP_ALIAS;
3f3c82a5 9156 }
3f3c82a5 9157
696ba377
RH
9158 if (HOST_BIG_ENDIAN &&
9159 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9160 r2->fieldoffset += sizeof(uint32_t);
9161 }
3f3c82a5 9162 }
f5a0a5a5 9163 }
cc946d96 9164
1859f8c3
RH
9165 /*
9166 * By convention, for wildcarded registers only the first
6e6efd61 9167 * entry is used for migration; the others are marked as
7a0e58fa 9168 * ALIAS so we don't try to transfer the register
6e6efd61 9169 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 9170 * never migratable and not even raw-accessible.
6e6efd61 9171 */
696ba377 9172 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
9173 r2->type |= ARM_CP_NO_RAW;
9174 }
9175 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
9176 ((r->opc1 == CP_ANY) && opc1 != 0) ||
9177 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 9178 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
9179 }
9180
1859f8c3
RH
9181 /*
9182 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
9183 * we can't assert this earlier because the setup of fieldoffset for
9184 * banked registers has to be done first.
9185 */
9186 if (!(r2->type & ARM_CP_NO_RAW)) {
9187 assert(!raw_accessors_invalid(r2));
9188 }
9189
5860362d 9190 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
9191}
9192
9193
4b6a83fb
PM
9194void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9195 const ARMCPRegInfo *r, void *opaque)
9196{
9b37a28c
FR
9197 /*
9198 * Define implementations of coprocessor registers.
4b6a83fb
PM
9199 * We store these in a hashtable because typically
9200 * there are less than 150 registers in a space which
9201 * is 16*16*16*8*8 = 262144 in size.
9202 * Wildcarding is supported for the crm, opc1 and opc2 fields.
9203 * If a register is defined twice then the second definition is
9204 * used, so this can be used to define some generic registers and
9205 * then override them with implementation specific variations.
9206 * At least one of the original and the second definition should
9207 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9208 * against accidental use.
f5a0a5a5
PM
9209 *
9210 * The state field defines whether the register is to be
9211 * visible in the AArch32 or AArch64 execution state. If the
9212 * state is set to ARM_CP_STATE_BOTH then we synthesise a
9213 * reginfo structure for the AArch32 view, which sees the lower
9214 * 32 bits of the 64 bit register.
9215 *
9216 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9217 * be wildcarded. AArch64 registers are always considered to be 64
9218 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9219 * the register, if any.
4b6a83fb 9220 */
d95101d6 9221 int crm, opc1, opc2;
4b6a83fb
PM
9222 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9223 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9224 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9225 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9226 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9227 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
9228 CPState state;
9229
4b6a83fb
PM
9230 /* 64 bit registers have only CRm and Opc1 fields */
9231 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
9232 /* op0 only exists in the AArch64 encodings */
9233 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9234 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9235 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
9236 /*
9237 * This API is only for Arm's system coprocessors (14 and 15) or
9238 * (M-profile or v7A-and-earlier only) for implementation defined
9239 * coprocessors in the range 0..7. Our decode assumes this, since
9240 * 8..13 can be used for other insns including VFP and Neon. See
9241 * valid_cp() in translate.c. Assert here that we haven't tried
9242 * to use an invalid coprocessor number.
9243 */
9244 switch (r->state) {
9245 case ARM_CP_STATE_BOTH:
9246 /* 0 has a special meaning, but otherwise the same rules as AA32. */
9247 if (r->cp == 0) {
9248 break;
9249 }
9250 /* fall through */
9251 case ARM_CP_STATE_AA32:
9252 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9253 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9254 assert(r->cp >= 14 && r->cp <= 15);
9255 } else {
9256 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9257 }
9258 break;
9259 case ARM_CP_STATE_AA64:
9260 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9261 break;
9262 default:
9263 g_assert_not_reached();
9264 }
9b37a28c
FR
9265 /*
9266 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
f5a0a5a5
PM
9267 * encodes a minimum access level for the register. We roll this
9268 * runtime check into our general permission check code, so check
9269 * here that the reginfo's specified permissions are strict enough
9270 * to encompass the generic architectural permission check.
9271 */
9272 if (r->state != ARM_CP_STATE_AA32) {
39107337 9273 CPAccessRights mask;
f5a0a5a5 9274 switch (r->opc1) {
b5bd7440
AB
9275 case 0:
9276 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9277 mask = PL0U_R | PL1_RW;
9278 break;
9279 case 1: case 2:
f5a0a5a5
PM
9280 /* min_EL EL1 */
9281 mask = PL1_RW;
9282 break;
9283 case 3:
9284 /* min_EL EL0 */
9285 mask = PL0_RW;
9286 break;
9287 case 4:
b4ecf60f 9288 case 5:
f5a0a5a5
PM
9289 /* min_EL EL2 */
9290 mask = PL2_RW;
9291 break;
f5a0a5a5
PM
9292 case 6:
9293 /* min_EL EL3 */
9294 mask = PL3_RW;
9295 break;
9296 case 7:
9297 /* min_EL EL1, secure mode only (we don't check the latter) */
9298 mask = PL1_RW;
9299 break;
9300 default:
9301 /* broken reginfo with out-of-range opc1 */
d385a605 9302 g_assert_not_reached();
f5a0a5a5
PM
9303 }
9304 /* assert our permissions are not too lax (stricter is fine) */
9305 assert((r->access & ~mask) == 0);
9306 }
9307
9b37a28c
FR
9308 /*
9309 * Check that the register definition has enough info to handle
4b6a83fb
PM
9310 * reads and writes if they are permitted.
9311 */
87c3f0f2 9312 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 9313 if (r->access & PL3_R) {
3f3c82a5
FA
9314 assert((r->fieldoffset ||
9315 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9316 r->readfn);
4b6a83fb
PM
9317 }
9318 if (r->access & PL3_W) {
3f3c82a5
FA
9319 assert((r->fieldoffset ||
9320 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9321 r->writefn);
4b6a83fb
PM
9322 }
9323 }
5809ac57 9324
4b6a83fb
PM
9325 for (crm = crmmin; crm <= crmmax; crm++) {
9326 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9327 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
9328 for (state = ARM_CP_STATE_AA32;
9329 state <= ARM_CP_STATE_AA64; state++) {
9330 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9331 continue;
9332 }
3f3c82a5 9333 if (state == ARM_CP_STATE_AA32) {
9b37a28c
FR
9334 /*
9335 * Under AArch32 CP registers can be common
3f3c82a5
FA
9336 * (same for secure and non-secure world) or banked.
9337 */
9c513e78
AB
9338 char *name;
9339
3f3c82a5
FA
9340 switch (r->secure) {
9341 case ARM_CP_SECSTATE_S:
9342 case ARM_CP_SECSTATE_NS:
9343 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
9344 r->secure, crm, opc1, opc2,
9345 r->name);
3f3c82a5 9346 break;
cbe64585 9347 case ARM_CP_SECSTATE_BOTH:
9c513e78 9348 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
9349 add_cpreg_to_hashtable(cpu, r, opaque, state,
9350 ARM_CP_SECSTATE_S,
9c513e78
AB
9351 crm, opc1, opc2, name);
9352 g_free(name);
3f3c82a5
FA
9353 add_cpreg_to_hashtable(cpu, r, opaque, state,
9354 ARM_CP_SECSTATE_NS,
9c513e78 9355 crm, opc1, opc2, r->name);
3f3c82a5 9356 break;
cbe64585
RH
9357 default:
9358 g_assert_not_reached();
3f3c82a5
FA
9359 }
9360 } else {
9b37a28c
FR
9361 /*
9362 * AArch64 registers get mapped to non-secure instance
9363 * of AArch32
9364 */
3f3c82a5
FA
9365 add_cpreg_to_hashtable(cpu, r, opaque, state,
9366 ARM_CP_SECSTATE_NS,
9c513e78 9367 crm, opc1, opc2, r->name);
3f3c82a5 9368 }
f5a0a5a5 9369 }
4b6a83fb
PM
9370 }
9371 }
9372 }
9373}
9374
5809ac57
RH
9375/* Define a whole list of registers */
9376void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9377 void *opaque, size_t len)
4b6a83fb 9378{
5809ac57
RH
9379 size_t i;
9380 for (i = 0; i < len; ++i) {
9381 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
9382 }
9383}
9384
6c5c0fec
AB
9385/*
9386 * Modify ARMCPRegInfo for access from userspace.
9387 *
9388 * This is a data driven modification directed by
9389 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9390 * user-space cannot alter any values and dynamic values pertaining to
9391 * execution state are hidden from user space view anyway.
9392 */
5809ac57
RH
9393void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9394 const ARMCPRegUserSpaceInfo *mods,
9395 size_t mods_len)
6c5c0fec 9396{
5809ac57
RH
9397 for (size_t mi = 0; mi < mods_len; ++mi) {
9398 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 9399 GPatternSpec *pat = NULL;
5809ac57 9400
d040242e
AB
9401 if (m->is_glob) {
9402 pat = g_pattern_spec_new(m->name);
9403 }
5809ac57
RH
9404 for (size_t ri = 0; ri < regs_len; ++ri) {
9405 ARMCPRegInfo *r = regs + ri;
9406
d040242e
AB
9407 if (pat && g_pattern_match_string(pat, r->name)) {
9408 r->type = ARM_CP_CONST;
9409 r->access = PL0U_R;
9410 r->resetvalue = 0;
9411 /* continue */
9412 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
9413 r->type = ARM_CP_CONST;
9414 r->access = PL0U_R;
9415 r->resetvalue &= m->exported_bits;
9416 r->resetvalue |= m->fixed_bits;
9417 break;
9418 }
9419 }
d040242e
AB
9420 if (pat) {
9421 g_pattern_spec_free(pat);
9422 }
6c5c0fec
AB
9423 }
9424}
9425
60322b39 9426const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 9427{
5860362d 9428 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
9429}
9430
c4241c7d
PM
9431void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9432 uint64_t value)
4b6a83fb
PM
9433{
9434 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
9435}
9436
c4241c7d 9437uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
9438{
9439 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
9440 return 0;
9441}
9442
f5a0a5a5
PM
9443void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9444{
9445 /* Helper coprocessor reset function for do-nothing-on-reset registers */
9446}
9447
af393ffc 9448static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b 9449{
9b37a28c
FR
9450 /*
9451 * Return true if it is not valid for us to switch to
37064a8b
PM
9452 * this CPU mode (ie all the UNPREDICTABLE cases in
9453 * the ARM ARM CPSRWriteByInstr pseudocode).
9454 */
af393ffc
PM
9455
9456 /* Changes to or from Hyp via MSR and CPS are illegal. */
9457 if (write_type == CPSRWriteByInstr &&
9458 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9459 mode == ARM_CPU_MODE_HYP)) {
9460 return 1;
9461 }
9462
37064a8b
PM
9463 switch (mode) {
9464 case ARM_CPU_MODE_USR:
10eacda7 9465 return 0;
37064a8b
PM
9466 case ARM_CPU_MODE_SYS:
9467 case ARM_CPU_MODE_SVC:
9468 case ARM_CPU_MODE_ABT:
9469 case ARM_CPU_MODE_UND:
9470 case ARM_CPU_MODE_IRQ:
9471 case ARM_CPU_MODE_FIQ:
9b37a28c
FR
9472 /*
9473 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
52ff951b
PM
9474 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9475 */
9b37a28c
FR
9476 /*
9477 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10eacda7
PM
9478 * and CPS are treated as illegal mode changes.
9479 */
9480 if (write_type == CPSRWriteByInstr &&
10eacda7 9481 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 9482 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
9483 return 1;
9484 }
37064a8b 9485 return 0;
e6c8fc07 9486 case ARM_CPU_MODE_HYP:
e6ef0169 9487 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 9488 case ARM_CPU_MODE_MON:
58ae2d1f 9489 return arm_current_el(env) < 3;
37064a8b
PM
9490 default:
9491 return 1;
9492 }
9493}
9494
2f4a40e5
AZ
9495uint32_t cpsr_read(CPUARMState *env)
9496{
9497 int ZF;
6fbe23d5
PB
9498 ZF = (env->ZF == 0);
9499 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
9500 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9501 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9502 | ((env->condexec_bits & 0xfc) << 8)
af519934 9503 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
9504}
9505
50866ba5
PM
9506void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9507 CPSRWriteType write_type)
2f4a40e5 9508{
6e8801f9 9509 uint32_t changed_daif;
e784807c
PM
9510 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9511 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 9512
2f4a40e5 9513 if (mask & CPSR_NZCV) {
6fbe23d5
PB
9514 env->ZF = (~val) & CPSR_Z;
9515 env->NF = val;
2f4a40e5
AZ
9516 env->CF = (val >> 29) & 1;
9517 env->VF = (val << 3) & 0x80000000;
9518 }
f927dbda 9519 if (mask & CPSR_Q) {
2f4a40e5 9520 env->QF = ((val & CPSR_Q) != 0);
f927dbda
FR
9521 }
9522 if (mask & CPSR_T) {
2f4a40e5 9523 env->thumb = ((val & CPSR_T) != 0);
f927dbda 9524 }
2f4a40e5
AZ
9525 if (mask & CPSR_IT_0_1) {
9526 env->condexec_bits &= ~3;
9527 env->condexec_bits |= (val >> 25) & 3;
9528 }
9529 if (mask & CPSR_IT_2_7) {
9530 env->condexec_bits &= 3;
9531 env->condexec_bits |= (val >> 8) & 0xfc;
9532 }
9533 if (mask & CPSR_GE) {
9534 env->GE = (val >> 16) & 0xf;
9535 }
9536
9b37a28c
FR
9537 /*
9538 * In a V7 implementation that includes the security extensions but does
6e8801f9
FA
9539 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9540 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9541 * bits respectively.
9542 *
9543 * In a V8 implementation, it is permitted for privileged software to
9544 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9545 */
f8c88bbc 9546 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
9547 arm_feature(env, ARM_FEATURE_EL3) &&
9548 !arm_feature(env, ARM_FEATURE_EL2) &&
9549 !arm_is_secure(env)) {
9550
9551 changed_daif = (env->daif ^ val) & mask;
9552
9553 if (changed_daif & CPSR_A) {
9b37a28c
FR
9554 /*
9555 * Check to see if we are allowed to change the masking of async
6e8801f9
FA
9556 * abort exceptions from a non-secure state.
9557 */
9558 if (!(env->cp15.scr_el3 & SCR_AW)) {
9559 qemu_log_mask(LOG_GUEST_ERROR,
9560 "Ignoring attempt to switch CPSR_A flag from "
9561 "non-secure world with SCR.AW bit clear\n");
9562 mask &= ~CPSR_A;
9563 }
9564 }
9565
9566 if (changed_daif & CPSR_F) {
9b37a28c
FR
9567 /*
9568 * Check to see if we are allowed to change the masking of FIQ
6e8801f9
FA
9569 * exceptions from a non-secure state.
9570 */
9571 if (!(env->cp15.scr_el3 & SCR_FW)) {
9572 qemu_log_mask(LOG_GUEST_ERROR,
9573 "Ignoring attempt to switch CPSR_F flag from "
9574 "non-secure world with SCR.FW bit clear\n");
9575 mask &= ~CPSR_F;
9576 }
9577
9b37a28c
FR
9578 /*
9579 * Check whether non-maskable FIQ (NMFI) support is enabled.
6e8801f9
FA
9580 * If this bit is set software is not allowed to mask
9581 * FIQs, but is allowed to set CPSR_F to 0.
9582 */
9583 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9584 (val & CPSR_F)) {
9585 qemu_log_mask(LOG_GUEST_ERROR,
9586 "Ignoring attempt to enable CPSR_F flag "
9587 "(non-maskable FIQ [NMFI] support enabled)\n");
9588 mask &= ~CPSR_F;
9589 }
9590 }
9591 }
9592
4cc35614
PM
9593 env->daif &= ~(CPSR_AIF & mask);
9594 env->daif |= val & CPSR_AIF & mask;
9595
f8c88bbc
PM
9596 if (write_type != CPSRWriteRaw &&
9597 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9 9598 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9b37a28c
FR
9599 /*
9600 * Note that we can only get here in USR mode if this is a
8c4f0eb9
PM
9601 * gdb stub write; for this case we follow the architectural
9602 * behaviour for guest writes in USR mode of ignoring an attempt
9603 * to switch mode. (Those are caught by translate.c for writes
9604 * triggered by guest instructions.)
9605 */
9606 mask &= ~CPSR_M;
9607 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9b37a28c
FR
9608 /*
9609 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
81907a58
PM
9610 * v7, and has defined behaviour in v8:
9611 * + leave CPSR.M untouched
9612 * + allow changes to the other CPSR fields
9613 * + set PSTATE.IL
9614 * For user changes via the GDB stub, we don't set PSTATE.IL,
9615 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9616 */
9617 mask &= ~CPSR_M;
81907a58
PM
9618 if (write_type != CPSRWriteByGDBStub &&
9619 arm_feature(env, ARM_FEATURE_V8)) {
9620 mask |= CPSR_IL;
9621 val |= CPSR_IL;
9622 }
81e37284
PM
9623 qemu_log_mask(LOG_GUEST_ERROR,
9624 "Illegal AArch32 mode switch attempt from %s to %s\n",
9625 aarch32_mode_name(env->uncached_cpsr),
9626 aarch32_mode_name(val));
37064a8b 9627 } else {
81e37284
PM
9628 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9629 write_type == CPSRWriteExceptionReturn ?
9630 "Exception return from AArch32" :
9631 "AArch32 mode switch from",
9632 aarch32_mode_name(env->uncached_cpsr),
9633 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9634 switch_mode(env, val & CPSR_M);
9635 }
2f4a40e5
AZ
9636 }
9637 mask &= ~CACHED_CPSR_BITS;
9638 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
9639 if (rebuild_hflags) {
9640 arm_rebuild_hflags(env);
9641 }
2f4a40e5
AZ
9642}
9643
b26eefb6
PB
9644/* Sign/zero extend */
9645uint32_t HELPER(sxtb16)(uint32_t x)
9646{
9647 uint32_t res;
9648 res = (uint16_t)(int8_t)x;
9649 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9650 return res;
9651}
9652
e5346292
PM
9653static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9654{
9655 /*
9656 * Take a division-by-zero exception if necessary; otherwise return
9657 * to get the usual non-trapping division behaviour (result of 0)
9658 */
9659 if (arm_feature(env, ARM_FEATURE_M)
9660 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9661 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9662 }
9663}
9664
b26eefb6
PB
9665uint32_t HELPER(uxtb16)(uint32_t x)
9666{
9667 uint32_t res;
9668 res = (uint16_t)(uint8_t)x;
9669 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9670 return res;
9671}
9672
e5346292 9673int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9674{
fc7a5038 9675 if (den == 0) {
e5346292 9676 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9677 return 0;
9678 }
9679 if (num == INT_MIN && den == -1) {
9680 return INT_MIN;
9681 }
3670669c
PB
9682 return num / den;
9683}
9684
e5346292 9685uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9686{
fc7a5038 9687 if (den == 0) {
e5346292 9688 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9689 return 0;
9690 }
3670669c
PB
9691 return num / den;
9692}
9693
9694uint32_t HELPER(rbit)(uint32_t x)
9695{
42fedbca 9696 return revbit32(x);
3670669c
PB
9697}
9698
c47eaf9f 9699#ifdef CONFIG_USER_ONLY
b5ff1b31 9700
affdb64d 9701static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9702{
2fc0cc0e 9703 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9704
9705 if (mode != ARM_CPU_MODE_USR) {
9706 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9707 }
b5ff1b31
FB
9708}
9709
012a906b
GB
9710uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9711 uint32_t cur_el, bool secure)
9e729b57
EI
9712{
9713 return 1;
9714}
9715
ce02049d
GB
9716void aarch64_sync_64_to_32(CPUARMState *env)
9717{
9718 g_assert_not_reached();
9719}
9720
b5ff1b31
FB
9721#else
9722
affdb64d 9723static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9724{
9725 int old_mode;
9726 int i;
9727
9728 old_mode = env->uncached_cpsr & CPSR_M;
f927dbda 9729 if (mode == old_mode) {
b5ff1b31 9730 return;
f927dbda 9731 }
b5ff1b31
FB
9732
9733 if (old_mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9734 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9735 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31 9736 } else if (mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9737 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9738 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9739 }
9740
f5206413 9741 i = bank_number(old_mode);
b5ff1b31 9742 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9743 env->banked_spsr[i] = env->spsr;
9744
f5206413 9745 i = bank_number(mode);
b5ff1b31 9746 env->regs[13] = env->banked_r13[i];
b5ff1b31 9747 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9748
9749 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9750 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9751}
9752
9b37a28c
FR
9753/*
9754 * Physical Interrupt Target EL Lookup Table
0eeb17d6
GB
9755 *
9756 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9757 *
9758 * The below multi-dimensional table is used for looking up the target
9759 * exception level given numerous condition criteria. Specifically, the
9760 * target EL is based on SCR and HCR routing controls as well as the
9761 * currently executing EL and secure state.
9762 *
9763 * Dimensions:
9764 * target_el_table[2][2][2][2][2][4]
9765 * | | | | | +--- Current EL
9766 * | | | | +------ Non-secure(0)/Secure(1)
9767 * | | | +--------- HCR mask override
9768 * | | +------------ SCR exec state control
9769 * | +--------------- SCR mask override
9770 * +------------------ 32-bit(0)/64-bit(1) EL3
9771 *
9772 * The table values are as such:
9773 * 0-3 = EL0-EL3
9774 * -1 = Cannot occur
9775 *
9776 * The ARM ARM target EL table includes entries indicating that an "exception
9777 * is not taken". The two cases where this is applicable are:
9778 * 1) An exception is taken from EL3 but the SCR does not have the exception
9779 * routed to EL3.
9780 * 2) An exception is taken from EL2 but the HCR does not have the exception
9781 * routed to EL2.
9782 * In these two cases, the below table contain a target of EL1. This value is
9783 * returned as it is expected that the consumer of the table data will check
9784 * for "target EL >= current EL" to ensure the exception is not taken.
9785 *
9786 * SCR HCR
9787 * 64 EA AMO From
9788 * BIT IRQ IMO Non-secure Secure
9789 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9790 */
82c39f6a 9791static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9792 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9793 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9794 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9795 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9796 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9797 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9798 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9799 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9800 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9801 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9802 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9803 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9804 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9805 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9806 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9807 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9808};
9809
9810/*
9811 * Determine the target EL for physical exceptions
9812 */
012a906b
GB
9813uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9814 uint32_t cur_el, bool secure)
0eeb17d6
GB
9815{
9816 CPUARMState *env = cs->env_ptr;
f7778444
RH
9817 bool rw;
9818 bool scr;
9819 bool hcr;
0eeb17d6 9820 int target_el;
2cde031f 9821 /* Is the highest EL AArch64? */
f7778444
RH
9822 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9823 uint64_t hcr_el2;
2cde031f
SS
9824
9825 if (arm_feature(env, ARM_FEATURE_EL3)) {
9826 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9827 } else {
9b37a28c
FR
9828 /*
9829 * Either EL2 is the highest EL (and so the EL2 register width
2cde031f
SS
9830 * is given by is64); or there is no EL2 or EL3, in which case
9831 * the value of 'rw' does not affect the table lookup anyway.
9832 */
9833 rw = is64;
9834 }
0eeb17d6 9835
f7778444 9836 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9837 switch (excp_idx) {
9838 case EXCP_IRQ:
9839 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9840 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9841 break;
9842 case EXCP_FIQ:
9843 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9844 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9845 break;
9846 default:
9847 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9848 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9849 break;
9850 };
9851
d1b31428
RH
9852 /*
9853 * For these purposes, TGE and AMO/IMO/FMO both force the
9854 * interrupt to EL2. Fold TGE into the bit extracted above.
9855 */
9856 hcr |= (hcr_el2 & HCR_TGE) != 0;
9857
0eeb17d6
GB
9858 /* Perform a table-lookup for the target EL given the current state */
9859 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9860
9861 assert(target_el > 0);
9862
9863 return target_el;
9864}
9865
fc6177af 9866void arm_log_exception(CPUState *cs)
b59f479b 9867{
fc6177af
PM
9868 int idx = cs->exception_index;
9869
b59f479b
PMD
9870 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9871 const char *exc = NULL;
9872 static const char * const excnames[] = {
9873 [EXCP_UDEF] = "Undefined Instruction",
9874 [EXCP_SWI] = "SVC",
9875 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9876 [EXCP_DATA_ABORT] = "Data Abort",
9877 [EXCP_IRQ] = "IRQ",
9878 [EXCP_FIQ] = "FIQ",
9879 [EXCP_BKPT] = "Breakpoint",
9880 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9881 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9882 [EXCP_HVC] = "Hypervisor Call",
9883 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9884 [EXCP_SMC] = "Secure Monitor Call",
9885 [EXCP_VIRQ] = "Virtual IRQ",
9886 [EXCP_VFIQ] = "Virtual FIQ",
9887 [EXCP_SEMIHOST] = "Semihosting call",
9888 [EXCP_NOCP] = "v7M NOCP UsageFault",
9889 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9890 [EXCP_STKOF] = "v8M STKOF UsageFault",
9891 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9892 [EXCP_LSERR] = "v8M LSERR UsageFault",
9893 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 9894 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
3c29632f 9895 [EXCP_VSERR] = "Virtual SERR",
b59f479b
PMD
9896 };
9897
9898 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9899 exc = excnames[idx];
9900 }
9901 if (!exc) {
9902 exc = "unknown";
9903 }
fc6177af
PM
9904 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9905 idx, exc, cs->cpu_index);
b59f479b
PMD
9906 }
9907}
9908
a356dacf 9909/*
7aab5a8c
PMD
9910 * Function used to synchronize QEMU's AArch64 register set with AArch32
9911 * register set. This is necessary when switching between AArch32 and AArch64
9912 * execution state.
a356dacf 9913 */
7aab5a8c 9914void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9915{
7aab5a8c
PMD
9916 int i;
9917 uint32_t mode = env->uncached_cpsr & CPSR_M;
9918
9919 /* We can blanket copy R[0:7] to X[0:7] */
9920 for (i = 0; i < 8; i++) {
9921 env->xregs[i] = env->regs[i];
fd592d89 9922 }
70d74660 9923
9a223097 9924 /*
7aab5a8c
PMD
9925 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9926 * Otherwise, they come from the banked user regs.
fd592d89 9927 */
7aab5a8c
PMD
9928 if (mode == ARM_CPU_MODE_FIQ) {
9929 for (i = 8; i < 13; i++) {
9930 env->xregs[i] = env->usr_regs[i - 8];
9931 }
9932 } else {
9933 for (i = 8; i < 13; i++) {
9934 env->xregs[i] = env->regs[i];
9935 }
fd592d89 9936 }
9ee6e8bb 9937
7aab5a8c
PMD
9938 /*
9939 * Registers x13-x23 are the various mode SP and FP registers. Registers
9940 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9941 * from the mode banked register.
9942 */
9943 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9944 env->xregs[13] = env->regs[13];
9945 env->xregs[14] = env->regs[14];
9946 } else {
9947 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9948 /* HYP is an exception in that it is copied from r14 */
9949 if (mode == ARM_CPU_MODE_HYP) {
9950 env->xregs[14] = env->regs[14];
95695eff 9951 } else {
7aab5a8c 9952 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9953 }
95695eff
PM
9954 }
9955
7aab5a8c
PMD
9956 if (mode == ARM_CPU_MODE_HYP) {
9957 env->xregs[15] = env->regs[13];
9958 } else {
9959 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9960 }
9961
7aab5a8c
PMD
9962 if (mode == ARM_CPU_MODE_IRQ) {
9963 env->xregs[16] = env->regs[14];
9964 env->xregs[17] = env->regs[13];
9965 } else {
9966 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9967 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9968 }
95695eff 9969
7aab5a8c
PMD
9970 if (mode == ARM_CPU_MODE_SVC) {
9971 env->xregs[18] = env->regs[14];
9972 env->xregs[19] = env->regs[13];
9973 } else {
9974 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9975 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9976 }
95695eff 9977
7aab5a8c
PMD
9978 if (mode == ARM_CPU_MODE_ABT) {
9979 env->xregs[20] = env->regs[14];
9980 env->xregs[21] = env->regs[13];
9981 } else {
9982 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9983 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9984 }
e33cf0f8 9985
7aab5a8c
PMD
9986 if (mode == ARM_CPU_MODE_UND) {
9987 env->xregs[22] = env->regs[14];
9988 env->xregs[23] = env->regs[13];
9989 } else {
9990 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9991 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9992 }
9993
9994 /*
7aab5a8c
PMD
9995 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9996 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9997 * FIQ bank for r8-r14.
e33cf0f8 9998 */
7aab5a8c
PMD
9999 if (mode == ARM_CPU_MODE_FIQ) {
10000 for (i = 24; i < 31; i++) {
10001 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10002 }
10003 } else {
10004 for (i = 24; i < 29; i++) {
10005 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 10006 }
7aab5a8c
PMD
10007 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10008 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 10009 }
7aab5a8c
PMD
10010
10011 env->pc = env->regs[15];
e33cf0f8
PM
10012}
10013
9a223097 10014/*
7aab5a8c
PMD
10015 * Function used to synchronize QEMU's AArch32 register set with AArch64
10016 * register set. This is necessary when switching between AArch32 and AArch64
10017 * execution state.
de2db7ec 10018 */
7aab5a8c 10019void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 10020{
7aab5a8c
PMD
10021 int i;
10022 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 10023
7aab5a8c
PMD
10024 /* We can blanket copy X[0:7] to R[0:7] */
10025 for (i = 0; i < 8; i++) {
10026 env->regs[i] = env->xregs[i];
de2db7ec 10027 }
3f0cddee 10028
9a223097 10029 /*
7aab5a8c
PMD
10030 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10031 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 10032 */
7aab5a8c
PMD
10033 if (mode == ARM_CPU_MODE_FIQ) {
10034 for (i = 8; i < 13; i++) {
10035 env->usr_regs[i - 8] = env->xregs[i];
10036 }
10037 } else {
10038 for (i = 8; i < 13; i++) {
10039 env->regs[i] = env->xregs[i];
10040 }
fb602cb7
PM
10041 }
10042
9a223097 10043 /*
7aab5a8c
PMD
10044 * Registers r13 & r14 depend on the current mode.
10045 * If we are in a given mode, we copy the corresponding x registers to r13
10046 * and r14. Otherwise, we copy the x register to the banked r13 and r14
10047 * for the mode.
fb602cb7 10048 */
7aab5a8c
PMD
10049 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10050 env->regs[13] = env->xregs[13];
10051 env->regs[14] = env->xregs[14];
fb602cb7 10052 } else {
7aab5a8c 10053 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 10054
7aab5a8c
PMD
10055 /*
10056 * HYP is an exception in that it does not have its own banked r14 but
10057 * shares the USR r14
10058 */
10059 if (mode == ARM_CPU_MODE_HYP) {
10060 env->regs[14] = env->xregs[14];
10061 } else {
10062 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10063 }
10064 }
fb602cb7 10065
7aab5a8c
PMD
10066 if (mode == ARM_CPU_MODE_HYP) {
10067 env->regs[13] = env->xregs[15];
fb602cb7 10068 } else {
7aab5a8c 10069 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 10070 }
d02a8698 10071
7aab5a8c
PMD
10072 if (mode == ARM_CPU_MODE_IRQ) {
10073 env->regs[14] = env->xregs[16];
10074 env->regs[13] = env->xregs[17];
d02a8698 10075 } else {
7aab5a8c
PMD
10076 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10077 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
10078 }
10079
7aab5a8c
PMD
10080 if (mode == ARM_CPU_MODE_SVC) {
10081 env->regs[14] = env->xregs[18];
10082 env->regs[13] = env->xregs[19];
10083 } else {
10084 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10085 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
10086 }
10087
7aab5a8c
PMD
10088 if (mode == ARM_CPU_MODE_ABT) {
10089 env->regs[14] = env->xregs[20];
10090 env->regs[13] = env->xregs[21];
10091 } else {
10092 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10093 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
10094 }
10095
10096 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
10097 env->regs[14] = env->xregs[22];
10098 env->regs[13] = env->xregs[23];
ce02049d 10099 } else {
593cfa2b 10100 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 10101 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
10102 }
10103
9b37a28c
FR
10104 /*
10105 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
ce02049d
GB
10106 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10107 * FIQ bank for r8-r14.
10108 */
10109 if (mode == ARM_CPU_MODE_FIQ) {
10110 for (i = 24; i < 31; i++) {
10111 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10112 }
10113 } else {
10114 for (i = 24; i < 29; i++) {
10115 env->fiq_regs[i - 24] = env->xregs[i];
10116 }
10117 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 10118 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
10119 }
10120
10121 env->regs[15] = env->pc;
10122}
10123
dea8378b
PM
10124static void take_aarch32_exception(CPUARMState *env, int new_mode,
10125 uint32_t mask, uint32_t offset,
10126 uint32_t newpc)
10127{
4a2696c0
RH
10128 int new_el;
10129
dea8378b
PM
10130 /* Change the CPU state so as to actually take the exception. */
10131 switch_mode(env, new_mode);
4a2696c0 10132
dea8378b
PM
10133 /*
10134 * For exceptions taken to AArch32 we must clear the SS bit in both
10135 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10136 */
f944a854 10137 env->pstate &= ~PSTATE_SS;
dea8378b
PM
10138 env->spsr = cpsr_read(env);
10139 /* Clear IT bits. */
10140 env->condexec_bits = 0;
10141 /* Switch to the new mode, and to the correct instruction set. */
10142 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
10143
10144 /* This must be after mode switching. */
10145 new_el = arm_current_el(env);
10146
dea8378b
PM
10147 /* Set new mode endianness */
10148 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 10149 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
10150 env->uncached_cpsr |= CPSR_E;
10151 }
829f9fd3
PM
10152 /* J and IL must always be cleared for exception entry */
10153 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
10154 env->daif |= mask;
10155
f2f68a78
RC
10156 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10157 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10158 env->uncached_cpsr |= CPSR_SSBS;
10159 } else {
10160 env->uncached_cpsr &= ~CPSR_SSBS;
10161 }
10162 }
10163
dea8378b
PM
10164 if (new_mode == ARM_CPU_MODE_HYP) {
10165 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10166 env->elr_el[2] = env->regs[15];
10167 } else {
4a2696c0 10168 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 10169 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
10170 switch (new_el) {
10171 case 3:
10172 if (!arm_is_secure_below_el3(env)) {
10173 /* ... the target is EL3, from non-secure state. */
10174 env->uncached_cpsr &= ~CPSR_PAN;
10175 break;
10176 }
10177 /* ... the target is EL3, from secure state ... */
10178 /* fall through */
10179 case 1:
10180 /* ... the target is EL1 and SCTLR.SPAN is 0. */
10181 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10182 env->uncached_cpsr |= CPSR_PAN;
10183 }
10184 break;
10185 }
10186 }
dea8378b
PM
10187 /*
10188 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10189 * and we should just guard the thumb mode on V4
10190 */
10191 if (arm_feature(env, ARM_FEATURE_V4T)) {
10192 env->thumb =
10193 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10194 }
10195 env->regs[14] = env->regs[15] + offset;
10196 }
10197 env->regs[15] = newpc;
a8a79c7a 10198 arm_rebuild_hflags(env);
dea8378b
PM
10199}
10200
b9bc21ff
PM
10201static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10202{
10203 /*
10204 * Handle exception entry to Hyp mode; this is sufficiently
10205 * different to entry to other AArch32 modes that we handle it
10206 * separately here.
10207 *
10208 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 10209 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
10210 * The offset applied to the preferred return address is always zero
10211 * (see DDI0487C.a section G1.12.3).
10212 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10213 */
10214 uint32_t addr, mask;
10215 ARMCPU *cpu = ARM_CPU(cs);
10216 CPUARMState *env = &cpu->env;
10217
10218 switch (cs->exception_index) {
10219 case EXCP_UDEF:
10220 addr = 0x04;
10221 break;
10222 case EXCP_SWI:
2c023d36 10223 addr = 0x08;
b9bc21ff
PM
10224 break;
10225 case EXCP_BKPT:
10226 /* Fall through to prefetch abort. */
10227 case EXCP_PREFETCH_ABORT:
10228 env->cp15.ifar_s = env->exception.vaddress;
10229 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10230 (uint32_t)env->exception.vaddress);
10231 addr = 0x0c;
10232 break;
10233 case EXCP_DATA_ABORT:
10234 env->cp15.dfar_s = env->exception.vaddress;
10235 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10236 (uint32_t)env->exception.vaddress);
10237 addr = 0x10;
10238 break;
10239 case EXCP_IRQ:
10240 addr = 0x18;
10241 break;
10242 case EXCP_FIQ:
10243 addr = 0x1c;
10244 break;
10245 case EXCP_HVC:
10246 addr = 0x08;
10247 break;
10248 case EXCP_HYP_TRAP:
10249 addr = 0x14;
9bbb4ef9 10250 break;
b9bc21ff
PM
10251 default:
10252 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10253 }
10254
10255 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
10256 if (!arm_feature(env, ARM_FEATURE_V8)) {
10257 /*
10258 * QEMU syndrome values are v8-style. v7 has the IL bit
10259 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10260 * If this is a v7 CPU, squash the IL bit in those cases.
10261 */
10262 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10263 (cs->exception_index == EXCP_DATA_ABORT &&
10264 !(env->exception.syndrome & ARM_EL_ISV)) ||
10265 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10266 env->exception.syndrome &= ~ARM_EL_IL;
10267 }
10268 }
b9bc21ff
PM
10269 env->cp15.esr_el[2] = env->exception.syndrome;
10270 }
10271
10272 if (arm_current_el(env) != 2 && addr < 0x14) {
10273 addr = 0x14;
10274 }
10275
10276 mask = 0;
10277 if (!(env->cp15.scr_el3 & SCR_EA)) {
10278 mask |= CPSR_A;
10279 }
10280 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10281 mask |= CPSR_I;
10282 }
10283 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10284 mask |= CPSR_F;
10285 }
10286
10287 addr += env->cp15.hvbar;
10288
10289 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10290}
10291
966f758c 10292static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 10293{
97a8ea5a
AF
10294 ARMCPU *cpu = ARM_CPU(cs);
10295 CPUARMState *env = &cpu->env;
b5ff1b31
FB
10296 uint32_t addr;
10297 uint32_t mask;
10298 int new_mode;
10299 uint32_t offset;
16a906fd 10300 uint32_t moe;
b5ff1b31 10301
16a906fd 10302 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 10303 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
10304 case EC_BREAKPOINT:
10305 case EC_BREAKPOINT_SAME_EL:
10306 moe = 1;
10307 break;
10308 case EC_WATCHPOINT:
10309 case EC_WATCHPOINT_SAME_EL:
10310 moe = 10;
10311 break;
10312 case EC_AA32_BKPT:
10313 moe = 3;
10314 break;
10315 case EC_VECTORCATCH:
10316 moe = 5;
10317 break;
10318 default:
10319 moe = 0;
10320 break;
10321 }
10322
10323 if (moe) {
10324 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10325 }
10326
b9bc21ff
PM
10327 if (env->exception.target_el == 2) {
10328 arm_cpu_do_interrupt_aarch32_hyp(cs);
10329 return;
10330 }
10331
27103424 10332 switch (cs->exception_index) {
b5ff1b31
FB
10333 case EXCP_UDEF:
10334 new_mode = ARM_CPU_MODE_UND;
10335 addr = 0x04;
10336 mask = CPSR_I;
f927dbda 10337 if (env->thumb) {
b5ff1b31 10338 offset = 2;
f927dbda 10339 } else {
b5ff1b31 10340 offset = 4;
f927dbda 10341 }
b5ff1b31
FB
10342 break;
10343 case EXCP_SWI:
10344 new_mode = ARM_CPU_MODE_SVC;
10345 addr = 0x08;
10346 mask = CPSR_I;
601d70b9 10347 /* The PC already points to the next instruction. */
b5ff1b31
FB
10348 offset = 0;
10349 break;
06c949e6 10350 case EXCP_BKPT:
9ee6e8bb
PB
10351 /* Fall through to prefetch abort. */
10352 case EXCP_PREFETCH_ABORT:
88ca1c2d 10353 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 10354 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 10355 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 10356 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10357 new_mode = ARM_CPU_MODE_ABT;
10358 addr = 0x0c;
10359 mask = CPSR_A | CPSR_I;
10360 offset = 4;
10361 break;
10362 case EXCP_DATA_ABORT:
4a7e2d73 10363 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 10364 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 10365 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 10366 env->exception.fsr,
6cd8a264 10367 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10368 new_mode = ARM_CPU_MODE_ABT;
10369 addr = 0x10;
10370 mask = CPSR_A | CPSR_I;
10371 offset = 8;
10372 break;
10373 case EXCP_IRQ:
10374 new_mode = ARM_CPU_MODE_IRQ;
10375 addr = 0x18;
10376 /* Disable IRQ and imprecise data aborts. */
10377 mask = CPSR_A | CPSR_I;
10378 offset = 4;
de38d23b
FA
10379 if (env->cp15.scr_el3 & SCR_IRQ) {
10380 /* IRQ routed to monitor mode */
10381 new_mode = ARM_CPU_MODE_MON;
10382 mask |= CPSR_F;
10383 }
b5ff1b31
FB
10384 break;
10385 case EXCP_FIQ:
10386 new_mode = ARM_CPU_MODE_FIQ;
10387 addr = 0x1c;
10388 /* Disable FIQ, IRQ and imprecise data aborts. */
10389 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
10390 if (env->cp15.scr_el3 & SCR_FIQ) {
10391 /* FIQ routed to monitor mode */
10392 new_mode = ARM_CPU_MODE_MON;
10393 }
b5ff1b31
FB
10394 offset = 4;
10395 break;
87a4b270
PM
10396 case EXCP_VIRQ:
10397 new_mode = ARM_CPU_MODE_IRQ;
10398 addr = 0x18;
10399 /* Disable IRQ and imprecise data aborts. */
10400 mask = CPSR_A | CPSR_I;
10401 offset = 4;
10402 break;
10403 case EXCP_VFIQ:
10404 new_mode = ARM_CPU_MODE_FIQ;
10405 addr = 0x1c;
10406 /* Disable FIQ, IRQ and imprecise data aborts. */
10407 mask = CPSR_A | CPSR_I | CPSR_F;
10408 offset = 4;
10409 break;
3c29632f
RH
10410 case EXCP_VSERR:
10411 {
10412 /*
10413 * Note that this is reported as a data abort, but the DFAR
10414 * has an UNKNOWN value. Construct the SError syndrome from
10415 * AET and ExT fields.
10416 */
10417 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10418
10419 if (extended_addresses_enabled(env)) {
10420 env->exception.fsr = arm_fi_to_lfsc(&fi);
10421 } else {
10422 env->exception.fsr = arm_fi_to_sfsc(&fi);
10423 }
10424 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10425 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10426 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10427 env->exception.fsr);
10428
10429 new_mode = ARM_CPU_MODE_ABT;
10430 addr = 0x10;
10431 mask = CPSR_A | CPSR_I;
10432 offset = 8;
10433 }
10434 break;
dbe9d163
FA
10435 case EXCP_SMC:
10436 new_mode = ARM_CPU_MODE_MON;
10437 addr = 0x08;
10438 mask = CPSR_A | CPSR_I | CPSR_F;
10439 offset = 0;
10440 break;
b5ff1b31 10441 default:
a47dddd7 10442 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
10443 return; /* Never happens. Keep compiler happy. */
10444 }
e89e51a1
FA
10445
10446 if (new_mode == ARM_CPU_MODE_MON) {
10447 addr += env->cp15.mvbar;
137feaa9 10448 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 10449 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 10450 addr += 0xffff0000;
8641136c 10451 } else {
9b37a28c
FR
10452 /*
10453 * ARM v7 architectures provide a vector base address register to remap
8641136c 10454 * the interrupt vector table.
e89e51a1 10455 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
10456 * Note: only bits 31:5 are valid.
10457 */
fb6c91ba 10458 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 10459 }
dbe9d163
FA
10460
10461 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10462 env->cp15.scr_el3 &= ~SCR_NS;
10463 }
10464
dea8378b 10465 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
10466}
10467
a65dabf7
PM
10468static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10469{
10470 /*
10471 * Return the register number of the AArch64 view of the AArch32
10472 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10473 * be that of the AArch32 mode the exception came from.
10474 */
10475 int mode = env->uncached_cpsr & CPSR_M;
10476
10477 switch (aarch32_reg) {
10478 case 0 ... 7:
10479 return aarch32_reg;
10480 case 8 ... 12:
10481 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10482 case 13:
10483 switch (mode) {
10484 case ARM_CPU_MODE_USR:
10485 case ARM_CPU_MODE_SYS:
10486 return 13;
10487 case ARM_CPU_MODE_HYP:
10488 return 15;
10489 case ARM_CPU_MODE_IRQ:
10490 return 17;
10491 case ARM_CPU_MODE_SVC:
10492 return 19;
10493 case ARM_CPU_MODE_ABT:
10494 return 21;
10495 case ARM_CPU_MODE_UND:
10496 return 23;
10497 case ARM_CPU_MODE_FIQ:
10498 return 29;
10499 default:
10500 g_assert_not_reached();
10501 }
10502 case 14:
10503 switch (mode) {
10504 case ARM_CPU_MODE_USR:
10505 case ARM_CPU_MODE_SYS:
10506 case ARM_CPU_MODE_HYP:
10507 return 14;
10508 case ARM_CPU_MODE_IRQ:
10509 return 16;
10510 case ARM_CPU_MODE_SVC:
10511 return 18;
10512 case ARM_CPU_MODE_ABT:
10513 return 20;
10514 case ARM_CPU_MODE_UND:
10515 return 22;
10516 case ARM_CPU_MODE_FIQ:
10517 return 30;
10518 default:
10519 g_assert_not_reached();
10520 }
10521 case 15:
10522 return 31;
10523 default:
10524 g_assert_not_reached();
10525 }
10526}
10527
f944a854
RC
10528static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10529{
10530 uint32_t ret = cpsr_read(env);
10531
10532 /* Move DIT to the correct location for SPSR_ELx */
10533 if (ret & CPSR_DIT) {
10534 ret &= ~CPSR_DIT;
10535 ret |= PSTATE_DIT;
10536 }
10537 /* Merge PSTATE.SS into SPSR_ELx */
10538 ret |= env->pstate & PSTATE_SS;
10539
10540 return ret;
10541}
10542
7ac61020
PM
10543static bool syndrome_is_sync_extabt(uint32_t syndrome)
10544{
10545 /* Return true if this syndrome value is a synchronous external abort */
10546 switch (syn_get_ec(syndrome)) {
10547 case EC_INSNABORT:
10548 case EC_INSNABORT_SAME_EL:
10549 case EC_DATAABORT:
10550 case EC_DATAABORT_SAME_EL:
10551 /* Look at fault status code for all the synchronous ext abort cases */
10552 switch (syndrome & 0x3f) {
10553 case 0x10:
10554 case 0x13:
10555 case 0x14:
10556 case 0x15:
10557 case 0x16:
10558 case 0x17:
10559 return true;
10560 default:
10561 return false;
10562 }
10563 default:
10564 return false;
10565 }
10566}
10567
966f758c
PM
10568/* Handle exception entry to a target EL which is using AArch64 */
10569static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
10570{
10571 ARMCPU *cpu = ARM_CPU(cs);
10572 CPUARMState *env = &cpu->env;
10573 unsigned int new_el = env->exception.target_el;
10574 target_ulong addr = env->cp15.vbar_el[new_el];
10575 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 10576 unsigned int old_mode;
0ab5953b 10577 unsigned int cur_el = arm_current_el(env);
a65dabf7 10578 int rt;
0ab5953b 10579
9a05f7b6
RH
10580 /*
10581 * Note that new_el can never be 0. If cur_el is 0, then
10582 * el0_a64 is is_a64(), else el0_a64 is ignored.
10583 */
10584 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 10585
0ab5953b 10586 if (cur_el < new_el) {
9b37a28c
FR
10587 /*
10588 * Entry vector offset depends on whether the implemented EL
3d6f7617
PM
10589 * immediately lower than the target level is using AArch32 or AArch64
10590 */
10591 bool is_aa64;
cb092fbb 10592 uint64_t hcr;
3d6f7617
PM
10593
10594 switch (new_el) {
10595 case 3:
10596 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10597 break;
10598 case 2:
cb092fbb
RH
10599 hcr = arm_hcr_el2_eff(env);
10600 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10601 is_aa64 = (hcr & HCR_RW) != 0;
10602 break;
10603 }
10604 /* fall through */
3d6f7617
PM
10605 case 1:
10606 is_aa64 = is_a64(env);
10607 break;
10608 default:
10609 g_assert_not_reached();
10610 }
10611
10612 if (is_aa64) {
f3a9b694
PM
10613 addr += 0x400;
10614 } else {
10615 addr += 0x600;
10616 }
10617 } else if (pstate_read(env) & PSTATE_SP) {
10618 addr += 0x200;
10619 }
10620
f3a9b694
PM
10621 switch (cs->exception_index) {
10622 case EXCP_PREFETCH_ABORT:
10623 case EXCP_DATA_ABORT:
7ac61020
PM
10624 /*
10625 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10626 * to be taken to the SError vector entrypoint.
10627 */
10628 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10629 syndrome_is_sync_extabt(env->exception.syndrome)) {
10630 addr += 0x180;
10631 }
f3a9b694
PM
10632 env->cp15.far_el[new_el] = env->exception.vaddress;
10633 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10634 env->cp15.far_el[new_el]);
10635 /* fall through */
10636 case EXCP_BKPT:
10637 case EXCP_UDEF:
10638 case EXCP_SWI:
10639 case EXCP_HVC:
10640 case EXCP_HYP_TRAP:
10641 case EXCP_SMC:
a65dabf7
PM
10642 switch (syn_get_ec(env->exception.syndrome)) {
10643 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10644 /*
10645 * QEMU internal FP/SIMD syndromes from AArch32 include the
10646 * TA and coproc fields which are only exposed if the exception
10647 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10648 * AArch64 format syndrome.
10649 */
10650 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10651 break;
10652 case EC_CP14RTTRAP:
10653 case EC_CP15RTTRAP:
10654 case EC_CP14DTTRAP:
10655 /*
10656 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10657 * the raw register field from the insn; when taking this to
10658 * AArch64 we must convert it to the AArch64 view of the register
10659 * number. Notice that we read a 4-bit AArch32 register number and
10660 * write back a 5-bit AArch64 one.
10661 */
10662 rt = extract32(env->exception.syndrome, 5, 4);
10663 rt = aarch64_regnum(env, rt);
10664 env->exception.syndrome = deposit32(env->exception.syndrome,
10665 5, 5, rt);
10666 break;
10667 case EC_CP15RRTTRAP:
10668 case EC_CP14RRTTRAP:
10669 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10670 rt = extract32(env->exception.syndrome, 5, 4);
10671 rt = aarch64_regnum(env, rt);
10672 env->exception.syndrome = deposit32(env->exception.syndrome,
10673 5, 5, rt);
10674 rt = extract32(env->exception.syndrome, 10, 4);
10675 rt = aarch64_regnum(env, rt);
10676 env->exception.syndrome = deposit32(env->exception.syndrome,
10677 10, 5, rt);
10678 break;
4be42f40 10679 }
f3a9b694
PM
10680 env->cp15.esr_el[new_el] = env->exception.syndrome;
10681 break;
10682 case EXCP_IRQ:
10683 case EXCP_VIRQ:
10684 addr += 0x80;
10685 break;
10686 case EXCP_FIQ:
10687 case EXCP_VFIQ:
10688 addr += 0x100;
10689 break;
3c29632f
RH
10690 case EXCP_VSERR:
10691 addr += 0x180;
10692 /* Construct the SError syndrome from IDS and ISS fields. */
10693 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10694 env->cp15.esr_el[new_el] = env->exception.syndrome;
10695 break;
f3a9b694
PM
10696 default:
10697 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10698 }
10699
10700 if (is_a64(env)) {
4a2696c0 10701 old_mode = pstate_read(env);
f3a9b694
PM
10702 aarch64_save_sp(env, arm_current_el(env));
10703 env->elr_el[new_el] = env->pc;
10704 } else {
f944a854 10705 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10706 env->elr_el[new_el] = env->regs[15];
10707
10708 aarch64_sync_32_to_64(env);
10709
10710 env->condexec_bits = 0;
10711 }
4a2696c0
RH
10712 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10713
f3a9b694
PM
10714 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10715 env->elr_el[new_el]);
10716
4a2696c0
RH
10717 if (cpu_isar_feature(aa64_pan, cpu)) {
10718 /* The value of PSTATE.PAN is normally preserved, except when ... */
10719 new_mode |= old_mode & PSTATE_PAN;
10720 switch (new_el) {
10721 case 2:
10722 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10723 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10724 != (HCR_E2H | HCR_TGE)) {
10725 break;
10726 }
10727 /* fall through */
10728 case 1:
10729 /* ... the target is EL1 ... */
10730 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10731 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10732 new_mode |= PSTATE_PAN;
10733 }
10734 break;
10735 }
10736 }
34669338
RH
10737 if (cpu_isar_feature(aa64_mte, cpu)) {
10738 new_mode |= PSTATE_TCO;
10739 }
4a2696c0 10740
f2f68a78
RC
10741 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10742 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10743 new_mode |= PSTATE_SSBS;
10744 } else {
10745 new_mode &= ~PSTATE_SSBS;
10746 }
10747 }
10748
f3a9b694 10749 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10750 env->aarch64 = true;
f3a9b694 10751 aarch64_restore_sp(env, new_el);
a8a79c7a 10752 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10753
10754 env->pc = addr;
10755
10756 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10757 new_el, env->pc, pstate_read(env));
966f758c
PM
10758}
10759
ed6e6ba9
AB
10760/*
10761 * Do semihosting call and set the appropriate return value. All the
10762 * permission and validity checks have been done at translate time.
10763 *
10764 * We only see semihosting exceptions in TCG only as they are not
10765 * trapped to the hypervisor in KVM.
10766 */
91f78c58 10767#ifdef CONFIG_TCG
ed6e6ba9
AB
10768static void handle_semihosting(CPUState *cs)
10769{
904c04de
PM
10770 ARMCPU *cpu = ARM_CPU(cs);
10771 CPUARMState *env = &cpu->env;
10772
10773 if (is_a64(env)) {
ed6e6ba9
AB
10774 qemu_log_mask(CPU_LOG_INT,
10775 "...handling as semihosting call 0x%" PRIx64 "\n",
10776 env->xregs[0]);
ed3a06b1 10777 do_common_semihosting(cs);
4ff5ef9e 10778 env->pc += 4;
904c04de 10779 } else {
904c04de
PM
10780 qemu_log_mask(CPU_LOG_INT,
10781 "...handling as semihosting call 0x%x\n",
10782 env->regs[0]);
ed3a06b1 10783 do_common_semihosting(cs);
4ff5ef9e 10784 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10785 }
10786}
ed6e6ba9 10787#endif
904c04de 10788
9b37a28c
FR
10789/*
10790 * Handle a CPU exception for A and R profile CPUs.
966f758c
PM
10791 * Do any appropriate logging, handle PSCI calls, and then hand off
10792 * to the AArch64-entry or AArch32-entry function depending on the
10793 * target exception level's register width.
853bfef4
CF
10794 *
10795 * Note: this is used for both TCG (as the do_interrupt tcg op),
10796 * and KVM to re-inject guest debug exceptions, and to
10797 * inject a Synchronous-External-Abort.
966f758c
PM
10798 */
10799void arm_cpu_do_interrupt(CPUState *cs)
10800{
10801 ARMCPU *cpu = ARM_CPU(cs);
10802 CPUARMState *env = &cpu->env;
10803 unsigned int new_el = env->exception.target_el;
10804
531c60a9 10805 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 10806
fc6177af 10807 arm_log_exception(cs);
966f758c
PM
10808 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10809 new_el);
10810 if (qemu_loglevel_mask(CPU_LOG_INT)
10811 && !excp_is_internal(cs->exception_index)) {
6568da45 10812 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 10813 syn_get_ec(env->exception.syndrome),
966f758c
PM
10814 env->exception.syndrome);
10815 }
10816
10817 if (arm_is_psci_call(cpu, cs->exception_index)) {
10818 arm_handle_psci_call(cpu);
10819 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10820 return;
10821 }
10822
ed6e6ba9
AB
10823 /*
10824 * Semihosting semantics depend on the register width of the code
10825 * that caused the exception, not the target exception level, so
10826 * must be handled here.
966f758c 10827 */
ed6e6ba9
AB
10828#ifdef CONFIG_TCG
10829 if (cs->exception_index == EXCP_SEMIHOST) {
10830 handle_semihosting(cs);
904c04de
PM
10831 return;
10832 }
ed6e6ba9 10833#endif
904c04de 10834
9b37a28c
FR
10835 /*
10836 * Hooks may change global state so BQL should be held, also the
b5c53d1b
AL
10837 * BQL needs to be held for any modification of
10838 * cs->interrupt_request.
10839 */
10840 g_assert(qemu_mutex_iothread_locked());
10841
10842 arm_call_pre_el_change_hook(cpu);
10843
904c04de
PM
10844 assert(!excp_is_internal(cs->exception_index));
10845 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
10846 arm_cpu_do_interrupt_aarch64(cs);
10847 } else {
10848 arm_cpu_do_interrupt_aarch32(cs);
10849 }
f3a9b694 10850
bd7d00fc
PM
10851 arm_call_el_change_hook(cpu);
10852
f3a9b694
PM
10853 if (!kvm_enabled()) {
10854 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10855 }
10856}
c47eaf9f 10857#endif /* !CONFIG_USER_ONLY */
0480f69a 10858
aaec1432
RH
10859uint64_t arm_sctlr(CPUARMState *env, int el)
10860{
10861 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10862 if (el == 0) {
10863 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
d902ae75 10864 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
aaec1432
RH
10865 }
10866 return env->cp15.sctlr_el[el];
10867}
c47eaf9f 10868
8ae08860 10869int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
10870{
10871 if (regime_has_2_ranges(mmu_idx)) {
10872 return extract64(tcr, 37, 2);
edc05dd4 10873 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
10874 return 0; /* VTCR_EL2 */
10875 } else {
3e270f67
RH
10876 /* Replicate the single TBI bit so we always have 2 bits. */
10877 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
10878 }
10879}
10880
8ae08860 10881int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
10882{
10883 if (regime_has_2_ranges(mmu_idx)) {
10884 return extract64(tcr, 51, 2);
edc05dd4 10885 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
10886 return 0; /* VTCR_EL2 */
10887 } else {
3e270f67
RH
10888 /* Replicate the single TBID bit so we always have 2 bits. */
10889 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
10890 }
10891}
10892
81ae05fa
RH
10893static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10894{
10895 if (regime_has_2_ranges(mmu_idx)) {
10896 return extract64(tcr, 57, 2);
10897 } else {
10898 /* Replicate the single TCMA bit so we always have 2 bits. */
10899 return extract32(tcr, 30, 1) * 3;
10900 }
10901}
10902
104f703d
PM
10903static ARMGranuleSize tg0_to_gran_size(int tg)
10904{
10905 switch (tg) {
10906 case 0:
10907 return Gran4K;
10908 case 1:
10909 return Gran64K;
10910 case 2:
10911 return Gran16K;
10912 default:
10913 return GranInvalid;
10914 }
10915}
10916
10917static ARMGranuleSize tg1_to_gran_size(int tg)
10918{
10919 switch (tg) {
10920 case 1:
10921 return Gran16K;
10922 case 2:
10923 return Gran4K;
10924 case 3:
10925 return Gran64K;
10926 default:
10927 return GranInvalid;
10928 }
10929}
10930
10931static inline bool have4k(ARMCPU *cpu, bool stage2)
10932{
10933 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10934 : cpu_isar_feature(aa64_tgran4, cpu);
10935}
10936
10937static inline bool have16k(ARMCPU *cpu, bool stage2)
10938{
10939 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10940 : cpu_isar_feature(aa64_tgran16, cpu);
10941}
10942
10943static inline bool have64k(ARMCPU *cpu, bool stage2)
10944{
10945 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10946 : cpu_isar_feature(aa64_tgran64, cpu);
10947}
10948
10949static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10950 bool stage2)
10951{
10952 switch (gran) {
10953 case Gran4K:
10954 if (have4k(cpu, stage2)) {
10955 return gran;
10956 }
10957 break;
10958 case Gran16K:
10959 if (have16k(cpu, stage2)) {
10960 return gran;
10961 }
10962 break;
10963 case Gran64K:
10964 if (have64k(cpu, stage2)) {
10965 return gran;
10966 }
10967 break;
10968 case GranInvalid:
10969 break;
10970 }
10971 /*
10972 * If the guest selects a granule size that isn't implemented,
10973 * the architecture requires that we behave as if it selected one
10974 * that is (with an IMPDEF choice of which one to pick). We choose
10975 * to implement the smallest supported granule size.
10976 */
10977 if (have4k(cpu, stage2)) {
10978 return Gran4K;
10979 }
10980 if (have16k(cpu, stage2)) {
10981 return Gran16K;
10982 }
10983 assert(have64k(cpu, stage2));
10984 return Gran64K;
10985}
10986
b830a5ee
RH
10987ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10988 ARMMMUIdx mmu_idx, bool data)
ba97be9f 10989{
c1547bba 10990 uint64_t tcr = regime_tcr(env, mmu_idx);
89739227 10991 bool epd, hpd, tsz_oob, ds, ha, hd;
ef56c242 10992 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
104f703d 10993 ARMGranuleSize gran;
ef56c242 10994 ARMCPU *cpu = env_archcpu(env);
edc05dd4 10995 bool stage2 = regime_is_stage2(mmu_idx);
ba97be9f 10996
339370b9 10997 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10998 select = 0;
ba97be9f 10999 tsz = extract32(tcr, 0, 6);
104f703d
PM
11000 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11001 if (stage2) {
ba97be9f 11002 /* VTCR_EL2 */
b830a5ee 11003 hpd = false;
ba97be9f 11004 } else {
ba97be9f
RH
11005 hpd = extract32(tcr, 24, 1);
11006 }
11007 epd = false;
ef56c242 11008 sh = extract32(tcr, 12, 2);
f4ecc015 11009 ps = extract32(tcr, 16, 3);
89739227
RH
11010 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11011 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11012 ds = extract64(tcr, 32, 1);
ba97be9f 11013 } else {
e4c93e44
PM
11014 bool e0pd;
11015
71d18164
RH
11016 /*
11017 * Bit 55 is always between the two regions, and is canonical for
11018 * determining if address tagging is enabled.
11019 */
11020 select = extract64(va, 55, 1);
11021 if (!select) {
11022 tsz = extract32(tcr, 0, 6);
104f703d 11023 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
71d18164 11024 epd = extract32(tcr, 7, 1);
ef56c242 11025 sh = extract32(tcr, 12, 2);
71d18164 11026 hpd = extract64(tcr, 41, 1);
e4c93e44 11027 e0pd = extract64(tcr, 55, 1);
71d18164 11028 } else {
71d18164 11029 tsz = extract32(tcr, 16, 6);
104f703d 11030 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
71d18164 11031 epd = extract32(tcr, 23, 1);
ef56c242 11032 sh = extract32(tcr, 28, 2);
71d18164 11033 hpd = extract64(tcr, 42, 1);
e4c93e44 11034 e0pd = extract64(tcr, 56, 1);
71d18164 11035 }
f4ecc015 11036 ps = extract64(tcr, 32, 3);
89739227
RH
11037 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11038 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11039 ds = extract64(tcr, 59, 1);
e4c93e44
PM
11040
11041 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11042 regime_is_user(env, mmu_idx)) {
11043 epd = true;
11044 }
ba97be9f 11045 }
c36c65ea 11046
104f703d 11047 gran = sanitize_gran_size(cpu, gran, stage2);
104f703d 11048
ef56c242 11049 if (cpu_isar_feature(aa64_st, cpu)) {
3c003f70 11050 max_tsz = 48 - (gran == Gran64K);
c36c65ea
RDC
11051 } else {
11052 max_tsz = 39;
11053 }
0af312b6 11054
ef56c242
RH
11055 /*
11056 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11057 * adjust the effective value of DS, as documented.
11058 */
0af312b6 11059 min_tsz = 16;
3c003f70 11060 if (gran == Gran64K) {
ef56c242
RH
11061 if (cpu_isar_feature(aa64_lva, cpu)) {
11062 min_tsz = 12;
11063 }
11064 ds = false;
11065 } else if (ds) {
edc05dd4 11066 if (regime_is_stage2(mmu_idx)) {
3c003f70 11067 if (gran == Gran16K) {
ef56c242
RH
11068 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11069 } else {
11070 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11071 }
edc05dd4 11072 } else {
3c003f70 11073 if (gran == Gran16K) {
ef56c242
RH
11074 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11075 } else {
11076 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11077 }
ef56c242
RH
11078 }
11079 if (ds) {
0af312b6
RH
11080 min_tsz = 12;
11081 }
11082 }
c36c65ea 11083
ebf93ce7
RH
11084 if (tsz > max_tsz) {
11085 tsz = max_tsz;
11086 tsz_oob = true;
11087 } else if (tsz < min_tsz) {
11088 tsz = min_tsz;
11089 tsz_oob = true;
11090 } else {
11091 tsz_oob = false;
11092 }
ba97be9f 11093
b830a5ee
RH
11094 /* Present TBI as a composite with TBID. */
11095 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11096 if (!data) {
11097 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11098 }
11099 tbi = (tbi >> select) & 1;
11100
ba97be9f
RH
11101 return (ARMVAParameters) {
11102 .tsz = tsz,
f4ecc015 11103 .ps = ps,
ef56c242 11104 .sh = sh,
ba97be9f
RH
11105 .select = select,
11106 .tbi = tbi,
11107 .epd = epd,
11108 .hpd = hpd,
ebf93ce7 11109 .tsz_oob = tsz_oob,
ef56c242 11110 .ds = ds,
89739227
RH
11111 .ha = ha,
11112 .hd = ha && hd,
3c003f70 11113 .gran = gran,
ba97be9f
RH
11114 };
11115}
11116
9b37a28c
FR
11117/*
11118 * Note that signed overflow is undefined in C. The following routines are
11119 * careful to use unsigned types where modulo arithmetic is required.
11120 * Failure to do so _will_ break on newer gcc.
11121 */
6ddbc6e4
PB
11122
11123/* Signed saturating arithmetic. */
11124
1654b2d6 11125/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11126static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11127{
11128 uint16_t res;
11129
11130 res = a + b;
11131 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
f927dbda 11132 if (a & 0x8000) {
6ddbc6e4 11133 res = 0x8000;
f927dbda 11134 } else {
6ddbc6e4 11135 res = 0x7fff;
f927dbda 11136 }
6ddbc6e4
PB
11137 }
11138 return res;
11139}
11140
1654b2d6 11141/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11142static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11143{
11144 uint8_t res;
11145
11146 res = a + b;
11147 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
f927dbda 11148 if (a & 0x80) {
6ddbc6e4 11149 res = 0x80;
f927dbda 11150 } else {
6ddbc6e4 11151 res = 0x7f;
f927dbda 11152 }
6ddbc6e4
PB
11153 }
11154 return res;
11155}
11156
1654b2d6 11157/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11158static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11159{
11160 uint16_t res;
11161
11162 res = a - b;
11163 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
f927dbda 11164 if (a & 0x8000) {
6ddbc6e4 11165 res = 0x8000;
f927dbda 11166 } else {
6ddbc6e4 11167 res = 0x7fff;
f927dbda 11168 }
6ddbc6e4
PB
11169 }
11170 return res;
11171}
11172
1654b2d6 11173/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11174static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11175{
11176 uint8_t res;
11177
11178 res = a - b;
11179 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
f927dbda 11180 if (a & 0x80) {
6ddbc6e4 11181 res = 0x80;
f927dbda 11182 } else {
6ddbc6e4 11183 res = 0x7f;
f927dbda 11184 }
6ddbc6e4
PB
11185 }
11186 return res;
11187}
11188
11189#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11190#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11191#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11192#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11193#define PFX q
11194
11195#include "op_addsub.h"
11196
11197/* Unsigned saturating arithmetic. */
460a09c1 11198static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11199{
11200 uint16_t res;
11201 res = a + b;
f927dbda 11202 if (res < a) {
6ddbc6e4 11203 res = 0xffff;
f927dbda 11204 }
6ddbc6e4
PB
11205 return res;
11206}
11207
460a09c1 11208static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11209{
f927dbda 11210 if (a > b) {
6ddbc6e4 11211 return a - b;
f927dbda 11212 } else {
6ddbc6e4 11213 return 0;
f927dbda 11214 }
6ddbc6e4
PB
11215}
11216
11217static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11218{
11219 uint8_t res;
11220 res = a + b;
f927dbda 11221 if (res < a) {
6ddbc6e4 11222 res = 0xff;
f927dbda 11223 }
6ddbc6e4
PB
11224 return res;
11225}
11226
11227static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11228{
f927dbda 11229 if (a > b) {
6ddbc6e4 11230 return a - b;
f927dbda 11231 } else {
6ddbc6e4 11232 return 0;
f927dbda 11233 }
6ddbc6e4
PB
11234}
11235
11236#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11237#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11238#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11239#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11240#define PFX uq
11241
11242#include "op_addsub.h"
11243
11244/* Signed modulo arithmetic. */
11245#define SARITH16(a, b, n, op) do { \
11246 int32_t sum; \
db6e2e65 11247 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11248 RESULT(sum, n, 16); \
11249 if (sum >= 0) \
11250 ge |= 3 << (n * 2); \
04215eb1 11251 } while (0)
6ddbc6e4
PB
11252
11253#define SARITH8(a, b, n, op) do { \
11254 int32_t sum; \
db6e2e65 11255 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11256 RESULT(sum, n, 8); \
11257 if (sum >= 0) \
11258 ge |= 1 << n; \
04215eb1 11259 } while (0)
6ddbc6e4
PB
11260
11261
11262#define ADD16(a, b, n) SARITH16(a, b, n, +)
11263#define SUB16(a, b, n) SARITH16(a, b, n, -)
11264#define ADD8(a, b, n) SARITH8(a, b, n, +)
11265#define SUB8(a, b, n) SARITH8(a, b, n, -)
11266#define PFX s
11267#define ARITH_GE
11268
11269#include "op_addsub.h"
11270
11271/* Unsigned modulo arithmetic. */
11272#define ADD16(a, b, n) do { \
11273 uint32_t sum; \
11274 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11275 RESULT(sum, n, 16); \
a87aa10b 11276 if ((sum >> 16) == 1) \
6ddbc6e4 11277 ge |= 3 << (n * 2); \
04215eb1 11278 } while (0)
6ddbc6e4
PB
11279
11280#define ADD8(a, b, n) do { \
11281 uint32_t sum; \
11282 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11283 RESULT(sum, n, 8); \
a87aa10b
AZ
11284 if ((sum >> 8) == 1) \
11285 ge |= 1 << n; \
04215eb1 11286 } while (0)
6ddbc6e4
PB
11287
11288#define SUB16(a, b, n) do { \
11289 uint32_t sum; \
11290 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11291 RESULT(sum, n, 16); \
11292 if ((sum >> 16) == 0) \
11293 ge |= 3 << (n * 2); \
04215eb1 11294 } while (0)
6ddbc6e4
PB
11295
11296#define SUB8(a, b, n) do { \
11297 uint32_t sum; \
11298 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11299 RESULT(sum, n, 8); \
11300 if ((sum >> 8) == 0) \
a87aa10b 11301 ge |= 1 << n; \
04215eb1 11302 } while (0)
6ddbc6e4
PB
11303
11304#define PFX u
11305#define ARITH_GE
11306
11307#include "op_addsub.h"
11308
11309/* Halved signed arithmetic. */
11310#define ADD16(a, b, n) \
11311 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11312#define SUB16(a, b, n) \
11313 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11314#define ADD8(a, b, n) \
11315 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11316#define SUB8(a, b, n) \
11317 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11318#define PFX sh
11319
11320#include "op_addsub.h"
11321
11322/* Halved unsigned arithmetic. */
11323#define ADD16(a, b, n) \
11324 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11325#define SUB16(a, b, n) \
11326 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11327#define ADD8(a, b, n) \
11328 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11329#define SUB8(a, b, n) \
11330 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11331#define PFX uh
11332
11333#include "op_addsub.h"
11334
11335static inline uint8_t do_usad(uint8_t a, uint8_t b)
11336{
f927dbda 11337 if (a > b) {
6ddbc6e4 11338 return a - b;
f927dbda 11339 } else {
6ddbc6e4 11340 return b - a;
f927dbda 11341 }
6ddbc6e4
PB
11342}
11343
11344/* Unsigned sum of absolute byte differences. */
11345uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11346{
11347 uint32_t sum;
11348 sum = do_usad(a, b);
11349 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 11350 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
11351 sum += do_usad(a >> 24, b >> 24);
11352 return sum;
11353}
11354
11355/* For ARMv6 SEL instruction. */
11356uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11357{
11358 uint32_t mask;
11359
11360 mask = 0;
f927dbda 11361 if (flags & 1) {
6ddbc6e4 11362 mask |= 0xff;
f927dbda
FR
11363 }
11364 if (flags & 2) {
6ddbc6e4 11365 mask |= 0xff00;
f927dbda
FR
11366 }
11367 if (flags & 4) {
6ddbc6e4 11368 mask |= 0xff0000;
f927dbda
FR
11369 }
11370 if (flags & 8) {
6ddbc6e4 11371 mask |= 0xff000000;
f927dbda 11372 }
6ddbc6e4
PB
11373 return (a & mask) | (b & ~mask);
11374}
11375
9b37a28c
FR
11376/*
11377 * CRC helpers.
aa633469
PM
11378 * The upper bytes of val (above the number specified by 'bytes') must have
11379 * been zeroed out by the caller.
11380 */
eb0ecd5a
WN
11381uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11382{
11383 uint8_t buf[4];
11384
aa633469 11385 stl_le_p(buf, val);
eb0ecd5a
WN
11386
11387 /* zlib crc32 converts the accumulator and output to one's complement. */
11388 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11389}
11390
11391uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11392{
11393 uint8_t buf[4];
11394
aa633469 11395 stl_le_p(buf, val);
eb0ecd5a
WN
11396
11397 /* Linux crc32c converts the output to one's complement. */
11398 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11399}
a9e01311 11400
9b37a28c
FR
11401/*
11402 * Return the exception level to which FP-disabled exceptions should
a9e01311
RH
11403 * be taken, or 0 if FP is enabled.
11404 */
ced31551 11405int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11406{
55faa212 11407#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
11408 uint64_t hcr_el2;
11409
9b37a28c
FR
11410 /*
11411 * CPACR and the CPTR registers don't exist before v6, so FP is
a9e01311
RH
11412 * always accessible
11413 */
11414 if (!arm_feature(env, ARM_FEATURE_V6)) {
11415 return 0;
11416 }
11417
d87513c0
PM
11418 if (arm_feature(env, ARM_FEATURE_M)) {
11419 /* CPACR can cause a NOCP UsageFault taken to current security state */
11420 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11421 return 1;
11422 }
11423
11424 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11425 if (!extract32(env->v7m.nsacr, 10, 1)) {
11426 /* FP insns cause a NOCP UsageFault taken to Secure */
11427 return 3;
11428 }
11429 }
11430
11431 return 0;
11432 }
11433
d5a6fa2d
RH
11434 hcr_el2 = arm_hcr_el2_eff(env);
11435
9b37a28c
FR
11436 /*
11437 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
a9e01311
RH
11438 * 0, 2 : trap EL0 and EL1/PL1 accesses
11439 * 1 : trap only EL0 accesses
11440 * 3 : trap no accesses
c2ddb7cf 11441 * This register is ignored if E2H+TGE are both set.
a9e01311 11442 */
d5a6fa2d 11443 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
fab8ad39 11444 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
c2ddb7cf
RH
11445
11446 switch (fpen) {
02e1de14
RH
11447 case 1:
11448 if (cur_el != 0) {
11449 break;
11450 }
11451 /* fall through */
c2ddb7cf
RH
11452 case 0:
11453 case 2:
02e1de14
RH
11454 /* Trap from Secure PL0 or PL1 to Secure PL1. */
11455 if (!arm_el_is_aa64(env, 3)
11456 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
a9e01311
RH
11457 return 3;
11458 }
02e1de14 11459 if (cur_el <= 1) {
c2ddb7cf
RH
11460 return 1;
11461 }
11462 break;
a9e01311 11463 }
a9e01311
RH
11464 }
11465
fc1120a7
PM
11466 /*
11467 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11468 * to control non-secure access to the FPU. It doesn't have any
11469 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11470 */
11471 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11472 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11473 if (!extract32(env->cp15.nsacr, 10, 1)) {
11474 /* FP insns act as UNDEF */
11475 return cur_el == 2 ? 2 : 1;
11476 }
11477 }
11478
d5a6fa2d
RH
11479 /*
11480 * CPTR_EL2 is present in v7VE or v8, and changes format
11481 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 11482 */
d5a6fa2d
RH
11483 if (cur_el <= 2) {
11484 if (hcr_el2 & HCR_E2H) {
fab8ad39 11485 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
d5a6fa2d
RH
11486 case 1:
11487 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11488 break;
11489 }
11490 /* fall through */
11491 case 0:
11492 case 2:
11493 return 2;
11494 }
11495 } else if (arm_is_el2_enabled(env)) {
fab8ad39 11496 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
d5a6fa2d
RH
11497 return 2;
11498 }
11499 }
a9e01311
RH
11500 }
11501
11502 /* CPTR_EL3 : present in v8 */
fab8ad39 11503 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
a9e01311
RH
11504 /* Trap all FP ops to EL3 */
11505 return 3;
11506 }
55faa212 11507#endif
a9e01311
RH
11508 return 0;
11509}
11510
b9f6033c
RH
11511/* Return the exception level we're running at if this is our mmu_idx */
11512int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11513{
11514 if (mmu_idx & ARM_MMU_IDX_M) {
11515 return mmu_idx & ARM_MMU_IDX_M_PRIV;
11516 }
11517
11518 switch (mmu_idx) {
11519 case ARMMMUIdx_E10_0:
11520 case ARMMMUIdx_E20_0:
b9f6033c
RH
11521 return 0;
11522 case ARMMMUIdx_E10_1:
452ef8cb 11523 case ARMMMUIdx_E10_1_PAN:
b9f6033c
RH
11524 return 1;
11525 case ARMMMUIdx_E2:
11526 case ARMMMUIdx_E20_2:
452ef8cb 11527 case ARMMMUIdx_E20_2_PAN:
b9f6033c 11528 return 2;
d902ae75 11529 case ARMMMUIdx_E3:
b9f6033c
RH
11530 return 3;
11531 default:
11532 g_assert_not_reached();
11533 }
11534}
11535
7aab5a8c 11536#ifndef CONFIG_TCG
65e4655c
RH
11537ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11538{
7aab5a8c 11539 g_assert_not_reached();
65e4655c 11540}
7aab5a8c 11541#endif
65e4655c 11542
6f2d9d74
TK
11543static bool arm_pan_enabled(CPUARMState *env)
11544{
11545 if (is_a64(env)) {
11546 return env->pstate & PSTATE_PAN;
11547 } else {
11548 return env->uncached_cpsr & CPSR_PAN;
11549 }
11550}
11551
164690b2 11552ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 11553{
b6ad6062
RDC
11554 ARMMMUIdx idx;
11555 uint64_t hcr;
11556
65e4655c 11557 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 11558 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
11559 }
11560
6003d980 11561 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
11562 switch (el) {
11563 case 0:
b6ad6062
RDC
11564 hcr = arm_hcr_el2_eff(env);
11565 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11566 idx = ARMMMUIdx_E20_0;
11567 } else {
11568 idx = ARMMMUIdx_E10_0;
6003d980 11569 }
b6ad6062 11570 break;
b9f6033c 11571 case 1:
6f2d9d74 11572 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11573 idx = ARMMMUIdx_E10_1_PAN;
11574 } else {
11575 idx = ARMMMUIdx_E10_1;
66412260 11576 }
b6ad6062 11577 break;
b9f6033c 11578 case 2:
6003d980 11579 /* Note that TGE does not apply at EL2. */
b6ad6062 11580 if (arm_hcr_el2_eff(env) & HCR_E2H) {
6f2d9d74 11581 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11582 idx = ARMMMUIdx_E20_2_PAN;
11583 } else {
11584 idx = ARMMMUIdx_E20_2;
66412260 11585 }
b6ad6062
RDC
11586 } else {
11587 idx = ARMMMUIdx_E2;
6003d980 11588 }
b6ad6062 11589 break;
b9f6033c 11590 case 3:
d902ae75 11591 return ARMMMUIdx_E3;
b9f6033c
RH
11592 default:
11593 g_assert_not_reached();
65e4655c 11594 }
b6ad6062 11595
b6ad6062 11596 return idx;
50494a27
RH
11597}
11598
164690b2
RH
11599ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11600{
11601 return arm_mmu_idx_el(env, arm_current_el(env));
11602}
11603
3902bfc6
RH
11604static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11605 ARMMMUIdx mmu_idx,
11606 CPUARMTBFlags flags)
fdd1b228 11607{
a729a46b
RH
11608 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11609 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 11610
fdd1b228 11611 if (arm_singlestep_active(env)) {
a729a46b 11612 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228
RH
11613 }
11614 return flags;
11615}
11616
3902bfc6
RH
11617static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11618 ARMMMUIdx mmu_idx,
11619 CPUARMTBFlags flags)
43eccfb6 11620{
8061a649
RH
11621 bool sctlr_b = arm_sctlr_b(env);
11622
11623 if (sctlr_b) {
a729a46b 11624 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
11625 }
11626 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 11627 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 11628 }
a729a46b 11629 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
11630
11631 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11632}
11633
3902bfc6
RH
11634static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11635 ARMMMUIdx mmu_idx)
6e33ced5 11636{
3902bfc6 11637 CPUARMTBFlags flags = {};
4479ec30
RH
11638 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11639
11640 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11641 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11642 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11643 }
6e33ced5
RH
11644
11645 if (arm_v7m_is_handler_mode(env)) {
a729a46b 11646 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
11647 }
11648
11649 /*
11650 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11651 * is suppressing them because the requested execution priority
11652 * is less than 0.
11653 */
11654 if (arm_feature(env, ARM_FEATURE_V8) &&
11655 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 11656 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 11657 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
11658 }
11659
a393dee0
RH
11660 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11661 DP_TBFLAG_M32(flags, SECURE, 1);
11662 }
11663
6e33ced5
RH
11664 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11665}
11666
3902bfc6
RH
11667static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11668 ARMMMUIdx mmu_idx)
c747224c 11669{
8480e933 11670 CPUARMTBFlags flags = {};
4479ec30
RH
11671 int el = arm_current_el(env);
11672
11673 if (arm_sctlr(env, el) & SCTLR_A) {
11674 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11675 }
0a54d68e
RH
11676
11677 if (arm_el_is_aa64(env, 1)) {
a729a46b 11678 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 11679 }
5bb0a20b 11680
4479ec30 11681 if (el < 2 && env->cp15.hstr_el2 &&
5bb0a20b 11682 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 11683 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
11684 }
11685
520d1621
PM
11686 if (env->uncached_cpsr & CPSR_IL) {
11687 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11688 }
11689
75fe8356
RH
11690 /*
11691 * The SME exception we are testing for is raised via
11692 * AArch64.CheckFPAdvSIMDEnabled(), as called from
11693 * AArch32.CheckAdvSIMDOrFPEnabled().
11694 */
11695 if (el == 0
11696 && FIELD_EX64(env->svcr, SVCR, SM)
11697 && (!arm_is_el2_enabled(env)
11698 || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11699 && arm_el_is_aa64(env, 1)
11700 && !sme_fa64(env, el)) {
11701 DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11702 }
11703
83f4baef 11704 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
11705}
11706
3902bfc6
RH
11707static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11708 ARMMMUIdx mmu_idx)
a9e01311 11709{
8480e933 11710 CPUARMTBFlags flags = {};
d4d7503a 11711 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
c1547bba 11712 uint64_t tcr = regime_tcr(env, mmu_idx);
d4d7503a
RH
11713 uint64_t sctlr;
11714 int tbii, tbid;
b9adaa70 11715
a729a46b 11716 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 11717
339370b9 11718 /* Get control bits for tagged addresses. */
b830a5ee
RH
11719 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11720 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 11721
a729a46b
RH
11722 DP_TBFLAG_A64(flags, TBII, tbii);
11723 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
11724
11725 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11726 int sve_el = sve_exception_el(env, el);
5d8634f5 11727
d4d7503a 11728 /*
397d922c
RH
11729 * If either FP or SVE are disabled, translator does not need len.
11730 * If SVE EL > FP EL, FP exception has precedence, and translator
11731 * does not need SVE EL. Save potential re-translations by forcing
11732 * the unneeded data to zero.
d4d7503a 11733 */
397d922c
RH
11734 if (fp_el != 0) {
11735 if (sve_el > fp_el) {
11736 sve_el = 0;
11737 }
11738 } else if (sve_el == 0) {
5ef3cc56 11739 DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
5d8634f5 11740 }
a729a46b 11741 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
d4d7503a 11742 }
6b2ca83e 11743 if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
5d7953ad 11744 int sme_el = sme_exception_el(env, el);
62151133 11745 bool sm = FIELD_EX64(env->svcr, SVCR, SM);
5d7953ad
RH
11746
11747 DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11748 if (sme_el == 0) {
11749 /* Similarly, do not compute SVL if SME is disabled. */
62151133
RH
11750 int svl = sve_vqm1_for_el_sm(env, el, true);
11751 DP_TBFLAG_A64(flags, SVL, svl);
11752 if (sm) {
11753 /* If SVE is disabled, we will not have set VL above. */
11754 DP_TBFLAG_A64(flags, VL, svl);
11755 }
5d7953ad 11756 }
62151133 11757 if (sm) {
a3637e88 11758 DP_TBFLAG_A64(flags, PSTATE_SM, 1);
75fe8356 11759 DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
a3637e88
RH
11760 }
11761 DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
6b2ca83e 11762 }
1db5e96c 11763
aaec1432 11764 sctlr = regime_sctlr(env, stage1);
1db5e96c 11765
4479ec30
RH
11766 if (sctlr & SCTLR_A) {
11767 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11768 }
11769
8061a649 11770 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 11771 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
11772 }
11773
d4d7503a
RH
11774 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11775 /*
11776 * In order to save space in flags, we record only whether
11777 * pauth is "inactive", meaning all insns are implemented as
11778 * a nop, or "active" when some action must be performed.
11779 * The decision of which action to take is left to a helper.
11780 */
11781 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 11782 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 11783 }
d4d7503a 11784 }
0816ef1b 11785
d4d7503a
RH
11786 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11787 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11788 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 11789 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 11790 }
d4d7503a 11791 }
08f1434a 11792
cc28fc30 11793 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
11794 if (!(env->pstate & PSTATE_UAO)) {
11795 switch (mmu_idx) {
11796 case ARMMMUIdx_E10_1:
11797 case ARMMMUIdx_E10_1_PAN:
7a8014ab 11798 /* TODO: ARMv8.3-NV */
a729a46b 11799 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
11800 break;
11801 case ARMMMUIdx_E20_2:
11802 case ARMMMUIdx_E20_2_PAN:
7a8014ab
RH
11803 /*
11804 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11805 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11806 */
11807 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 11808 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
11809 }
11810 break;
11811 default:
11812 break;
cc28fc30 11813 }
cc28fc30
RH
11814 }
11815
520d1621
PM
11816 if (env->pstate & PSTATE_IL) {
11817 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11818 }
11819
81ae05fa
RH
11820 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11821 /*
11822 * Set MTE_ACTIVE if any access may be Checked, and leave clear
11823 * if all accesses must be Unchecked:
11824 * 1) If no TBI, then there are no tags in the address to check,
11825 * 2) If Tag Check Override, then all accesses are Unchecked,
11826 * 3) If Tag Check Fail == 0, then Checked access have no effect,
11827 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11828 */
11829 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 11830 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
11831 if (tbid
11832 && !(env->pstate & PSTATE_TCO)
11833 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 11834 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
11835 }
11836 }
11837 /* And again for unprivileged accesses, if required. */
a729a46b 11838 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
11839 && tbid
11840 && !(env->pstate & PSTATE_TCO)
2d928adf 11841 && (sctlr & SCTLR_TCF0)
81ae05fa 11842 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 11843 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
11844 }
11845 /* Cache TCMA as well as TBI. */
a729a46b 11846 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
11847 }
11848
d4d7503a
RH
11849 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11850}
11851
3902bfc6 11852static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
11853{
11854 int el = arm_current_el(env);
11855 int fp_el = fp_exception_el(env, el);
164690b2 11856 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
11857
11858 if (is_a64(env)) {
11859 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11860 } else if (arm_feature(env, ARM_FEATURE_M)) {
11861 return rebuild_hflags_m32(env, fp_el, mmu_idx);
11862 } else {
11863 return rebuild_hflags_a32(env, fp_el, mmu_idx);
11864 }
11865}
11866
11867void arm_rebuild_hflags(CPUARMState *env)
11868{
11869 env->hflags = rebuild_hflags_internal(env);
11870}
11871
19717e9b
PM
11872/*
11873 * If we have triggered a EL state change we can't rely on the
11874 * translator having passed it to us, we need to recompute.
11875 */
11876void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11877{
11878 int el = arm_current_el(env);
11879 int fp_el = fp_exception_el(env, el);
11880 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 11881
19717e9b
PM
11882 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11883}
11884
14f3c588
RH
11885void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11886{
11887 int fp_el = fp_exception_el(env, el);
11888 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11889
11890 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11891}
11892
f80741d1
AB
11893/*
11894 * If we have triggered a EL state change we can't rely on the
563152e0 11895 * translator having passed it to us, we need to recompute.
f80741d1
AB
11896 */
11897void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11898{
11899 int el = arm_current_el(env);
11900 int fp_el = fp_exception_el(env, el);
11901 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11902 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11903}
11904
14f3c588
RH
11905void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11906{
11907 int fp_el = fp_exception_el(env, el);
11908 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11909
11910 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11911}
11912
11913void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11914{
11915 int fp_el = fp_exception_el(env, el);
11916 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11917
11918 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11919}
11920
0ee8b24a
PMD
11921static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11922{
11923#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
11924 CPUARMTBFlags c = env->hflags;
11925 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 11926
a378206a
RH
11927 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11928 fprintf(stderr, "TCG hflags mismatch "
11929 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11930 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11931 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
11932 abort();
11933 }
11934#endif
11935}
11936
26702213
PM
11937static bool mve_no_pred(CPUARMState *env)
11938{
11939 /*
11940 * Return true if there is definitely no predication of MVE
11941 * instructions by VPR or LTPSIZE. (Returning false even if there
11942 * isn't any predication is OK; generated code will just be
11943 * a little worse.)
11944 * If the CPU does not implement MVE then this TB flag is always 0.
11945 *
11946 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11947 * logic in gen_update_fp_context() needs to be updated to match.
11948 *
11949 * We do not include the effect of the ECI bits here -- they are
11950 * tracked in other TB flags. This simplifies the logic for
11951 * "when did we emit code that changes the MVE_NO_PRED TB flag
11952 * and thus need to end the TB?".
11953 */
11954 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11955 return false;
11956 }
11957 if (env->v7m.vpr) {
11958 return false;
11959 }
11960 if (env->v7m.ltpsize < 4) {
11961 return false;
11962 }
11963 return true;
11964}
11965
d4d7503a
RH
11966void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11967 target_ulong *cs_base, uint32_t *pflags)
11968{
3902bfc6 11969 CPUARMTBFlags flags;
d4d7503a 11970
0ee8b24a 11971 assert_hflags_rebuild_correctly(env);
3902bfc6 11972 flags = env->hflags;
3d74e2e9 11973
a729a46b 11974 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 11975 *pc = env->pc;
d4d7503a 11976 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 11977 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 11978 }
a9e01311
RH
11979 } else {
11980 *pc = env->regs[15];
6e33ced5
RH
11981
11982 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
11983 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11984 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11985 != env->v7m.secure) {
a729a46b 11986 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
11987 }
11988
11989 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11990 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11991 (env->v7m.secure &&
11992 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11993 /*
11994 * ASPEN is set, but FPCA/SFPA indicate that there is no
11995 * active FP context; we must create a new FP context before
11996 * executing any FP insn.
11997 */
a729a46b 11998 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
11999 }
12000
12001 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12002 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 12003 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 12004 }
26702213
PM
12005
12006 if (mve_no_pred(env)) {
12007 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12008 }
6e33ced5 12009 } else {
bbad7c62
RH
12010 /*
12011 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12012 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12013 */
12014 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 12015 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 12016 } else {
a729a46b
RH
12017 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12018 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 12019 }
0a54d68e 12020 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 12021 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 12022 }
6e33ced5
RH
12023 }
12024
a729a46b
RH
12025 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12026 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 12027 }
a9e01311 12028
60e12c37
RH
12029 /*
12030 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12031 * states defined in the ARM ARM for software singlestep:
12032 * SS_ACTIVE PSTATE.SS State
12033 * 0 x Inactive (the TB flag for SS is always 0)
12034 * 1 0 Active-pending
12035 * 1 1 Active-not-pending
ae6eb1e9 12036 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 12037 */
a729a46b
RH
12038 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12039 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 12040 }
a9e01311 12041
3902bfc6 12042 *pflags = flags.flags;
a378206a 12043 *cs_base = flags.flags2;
a9e01311 12044}
0ab5953b
RH
12045
12046#ifdef TARGET_AARCH64
12047/*
12048 * The manual says that when SVE is enabled and VQ is widened the
12049 * implementation is allowed to zero the previously inaccessible
12050 * portion of the registers. The corollary to that is that when
12051 * SVE is enabled and VQ is narrowed we are also allowed to zero
12052 * the now inaccessible portion of the registers.
12053 *
12054 * The intent of this is that no predicate bit beyond VQ is ever set.
12055 * Which means that some operations on predicate registers themselves
12056 * may operate on full uint64_t or even unrolled across the maximum
12057 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12058 * may well be cheaper than conditionals to restrict the operation
12059 * to the relevant portion of a uint16_t[16].
12060 */
12061void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12062{
12063 int i, j;
12064 uint64_t pmask;
12065
12066 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12067 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12068
12069 /* Zap the high bits of the zregs. */
12070 for (i = 0; i < 32; i++) {
12071 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12072 }
12073
12074 /* Zap the high bits of the pregs and ffr. */
12075 pmask = 0;
12076 if (vq & 3) {
12077 pmask = ~(-1ULL << (16 * (vq & 3)));
12078 }
12079 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12080 for (i = 0; i < 17; ++i) {
12081 env->vfp.pregs[i].p[j] &= pmask;
12082 }
12083 pmask = 0;
12084 }
12085}
12086
6a775fd6
RH
12087static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12088{
12089 int exc_el;
12090
12091 if (sm) {
12092 exc_el = sme_exception_el(env, el);
12093 } else {
12094 exc_el = sve_exception_el(env, el);
12095 }
12096 if (exc_el) {
12097 return 0; /* disabled */
12098 }
12099 return sve_vqm1_for_el_sm(env, el, sm);
12100}
12101
0ab5953b
RH
12102/*
12103 * Notice a change in SVE vector size when changing EL.
12104 */
9a05f7b6
RH
12105void aarch64_sve_change_el(CPUARMState *env, int old_el,
12106 int new_el, bool el0_a64)
0ab5953b 12107{
2fc0cc0e 12108 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12109 int old_len, new_len;
6a775fd6 12110 bool old_a64, new_a64, sm;
0ab5953b
RH
12111
12112 /* Nothing to do if no SVE. */
cd208a1c 12113 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12114 return;
12115 }
12116
12117 /* Nothing to do if FP is disabled in either EL. */
12118 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12119 return;
12120 }
12121
04fbce76
RH
12122 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12123 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12124
12125 /*
12126 * Both AArch64.TakeException and AArch64.ExceptionReturn
12127 * invoke ResetSVEState when taking an exception from, or
12128 * returning to, AArch32 state when PSTATE.SM is enabled.
12129 */
6a775fd6
RH
12130 sm = FIELD_EX64(env->svcr, SVCR, SM);
12131 if (old_a64 != new_a64 && sm) {
04fbce76
RH
12132 arm_reset_sve_state(env);
12133 return;
12134 }
12135
0ab5953b
RH
12136 /*
12137 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12138 * at ELx, or not available because the EL is in AArch32 state, then
12139 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12140 * has an effective value of 0".
12141 *
12142 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12143 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12144 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12145 * we already have the correct register contents when encountering the
12146 * vq0->vq0 transition between EL0->EL1.
12147 */
6a775fd6
RH
12148 old_len = new_len = 0;
12149 if (old_a64) {
12150 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12151 }
12152 if (new_a64) {
12153 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12154 }
0ab5953b
RH
12155
12156 /* When changing vector length, clear inaccessible state. */
12157 if (new_len < old_len) {
12158 aarch64_sve_narrow_vq(env, new_len + 1);
12159 }
12160}
12161#endif