]> Git Repo - qemu.git/blame - target-arm/helper.c
Merge remote-tracking branch 'remotes/gkurz/tags/for-upstream' into staging
[qemu.git] / target-arm / helper.c
CommitLineData
74c21bd0 1#include "qemu/osdep.h"
b5ff1b31 2#include "cpu.h"
ccd38087 3#include "internals.h"
022c62cb 4#include "exec/gdbstub.h"
2ef6175a 5#include "exec/helper-proto.h"
1de7afc9 6#include "qemu/host-utils.h"
78027bb6 7#include "sysemu/arch_init.h"
9c17d615 8#include "sysemu/sysemu.h"
1de7afc9 9#include "qemu/bitops.h"
eb0ecd5a 10#include "qemu/crc32c.h"
63c91552 11#include "exec/exec-all.h"
f08b6170 12#include "exec/cpu_ldst.h"
1d854765 13#include "arm_ldst.h"
eb0ecd5a 14#include <zlib.h> /* For crc32 */
cfe67cef 15#include "exec/semihost.h"
f3a9b694 16#include "sysemu/kvm.h"
0b03bdfc 17
352c98e5
LV
18#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
19
4a501606 20#ifndef CONFIG_USER_ONLY
af51f566
EI
21static bool get_phys_addr(CPUARMState *env, target_ulong address,
22 int access_type, ARMMMUIdx mmu_idx,
23 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
24 target_ulong *page_size, uint32_t *fsr,
25 ARMMMUFaultInfo *fi);
7c2cb42b 26
37785977
EI
27static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
28 int access_type, ARMMMUIdx mmu_idx,
29 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
30 target_ulong *page_size_ptr, uint32_t *fsr,
31 ARMMMUFaultInfo *fi);
32
7c2cb42b
AF
33/* Definitions for the PMCCNTR and PMCR registers */
34#define PMCRD 0x8
35#define PMCRC 0x4
36#define PMCRE 0x1
4a501606
PM
37#endif
38
0ecb72a5 39static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
40{
41 int nregs;
42
43 /* VFP data registers are always little-endian. */
44 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
45 if (reg < nregs) {
46 stfq_le_p(buf, env->vfp.regs[reg]);
47 return 8;
48 }
49 if (arm_feature(env, ARM_FEATURE_NEON)) {
50 /* Aliases for Q regs. */
51 nregs += 16;
52 if (reg < nregs) {
53 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
54 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
55 return 16;
56 }
57 }
58 switch (reg - nregs) {
59 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
60 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
61 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
62 }
63 return 0;
64}
65
0ecb72a5 66static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
67{
68 int nregs;
69
70 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
71 if (reg < nregs) {
72 env->vfp.regs[reg] = ldfq_le_p(buf);
73 return 8;
74 }
75 if (arm_feature(env, ARM_FEATURE_NEON)) {
76 nregs += 16;
77 if (reg < nregs) {
78 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
79 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
80 return 16;
81 }
82 }
83 switch (reg - nregs) {
84 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
85 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 86 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
87 }
88 return 0;
89}
90
6a669427
PM
91static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
92{
93 switch (reg) {
94 case 0 ... 31:
95 /* 128 bit FP register */
96 stfq_le_p(buf, env->vfp.regs[reg * 2]);
97 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
98 return 16;
99 case 32:
100 /* FPSR */
101 stl_p(buf, vfp_get_fpsr(env));
102 return 4;
103 case 33:
104 /* FPCR */
105 stl_p(buf, vfp_get_fpcr(env));
106 return 4;
107 default:
108 return 0;
109 }
110}
111
112static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
113{
114 switch (reg) {
115 case 0 ... 31:
116 /* 128 bit FP register */
117 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
118 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
119 return 16;
120 case 32:
121 /* FPSR */
122 vfp_set_fpsr(env, ldl_p(buf));
123 return 4;
124 case 33:
125 /* FPCR */
126 vfp_set_fpcr(env, ldl_p(buf));
127 return 4;
128 default:
129 return 0;
130 }
131}
132
c4241c7d 133static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 134{
375421cc 135 assert(ri->fieldoffset);
67ed771d 136 if (cpreg_field_is_64bit(ri)) {
c4241c7d 137 return CPREG_FIELD64(env, ri);
22d9e1a9 138 } else {
c4241c7d 139 return CPREG_FIELD32(env, ri);
22d9e1a9 140 }
d4e6df63
PM
141}
142
c4241c7d
PM
143static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
144 uint64_t value)
d4e6df63 145{
375421cc 146 assert(ri->fieldoffset);
67ed771d 147 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
148 CPREG_FIELD64(env, ri) = value;
149 } else {
150 CPREG_FIELD32(env, ri) = value;
151 }
d4e6df63
PM
152}
153
11f136ee
FA
154static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
155{
156 return (char *)env + ri->fieldoffset;
157}
158
49a66191 159uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 160{
59a1c327 161 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 162 if (ri->type & ARM_CP_CONST) {
59a1c327 163 return ri->resetvalue;
721fae12 164 } else if (ri->raw_readfn) {
59a1c327 165 return ri->raw_readfn(env, ri);
721fae12 166 } else if (ri->readfn) {
59a1c327 167 return ri->readfn(env, ri);
721fae12 168 } else {
59a1c327 169 return raw_read(env, ri);
721fae12 170 }
721fae12
PM
171}
172
59a1c327 173static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 174 uint64_t v)
721fae12
PM
175{
176 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
177 * Note that constant registers are treated as write-ignored; the
178 * caller should check for success by whether a readback gives the
179 * value written.
180 */
181 if (ri->type & ARM_CP_CONST) {
59a1c327 182 return;
721fae12 183 } else if (ri->raw_writefn) {
c4241c7d 184 ri->raw_writefn(env, ri, v);
721fae12 185 } else if (ri->writefn) {
c4241c7d 186 ri->writefn(env, ri, v);
721fae12 187 } else {
afb2530f 188 raw_write(env, ri, v);
721fae12 189 }
721fae12
PM
190}
191
375421cc
PM
192static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
193{
194 /* Return true if the regdef would cause an assertion if you called
195 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
196 * program bug for it not to have the NO_RAW flag).
197 * NB that returning false here doesn't necessarily mean that calling
198 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
199 * read/write access functions which are safe for raw use" from "has
200 * read/write access functions which have side effects but has forgotten
201 * to provide raw access functions".
202 * The tests here line up with the conditions in read/write_raw_cp_reg()
203 * and assertions in raw_read()/raw_write().
204 */
205 if ((ri->type & ARM_CP_CONST) ||
206 ri->fieldoffset ||
207 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
208 return false;
209 }
210 return true;
211}
212
721fae12
PM
213bool write_cpustate_to_list(ARMCPU *cpu)
214{
215 /* Write the coprocessor state from cpu->env to the (index,value) list. */
216 int i;
217 bool ok = true;
218
219 for (i = 0; i < cpu->cpreg_array_len; i++) {
220 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
221 const ARMCPRegInfo *ri;
59a1c327 222
60322b39 223 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
224 if (!ri) {
225 ok = false;
226 continue;
227 }
7a0e58fa 228 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
229 continue;
230 }
59a1c327 231 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
232 }
233 return ok;
234}
235
236bool write_list_to_cpustate(ARMCPU *cpu)
237{
238 int i;
239 bool ok = true;
240
241 for (i = 0; i < cpu->cpreg_array_len; i++) {
242 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
243 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
244 const ARMCPRegInfo *ri;
245
60322b39 246 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
247 if (!ri) {
248 ok = false;
249 continue;
250 }
7a0e58fa 251 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
252 continue;
253 }
254 /* Write value and confirm it reads back as written
255 * (to catch read-only registers and partially read-only
256 * registers where the incoming migration value doesn't match)
257 */
59a1c327
PM
258 write_raw_cp_reg(&cpu->env, ri, v);
259 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
260 ok = false;
261 }
262 }
263 return ok;
264}
265
266static void add_cpreg_to_list(gpointer key, gpointer opaque)
267{
268 ARMCPU *cpu = opaque;
269 uint64_t regidx;
270 const ARMCPRegInfo *ri;
271
272 regidx = *(uint32_t *)key;
60322b39 273 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 274
7a0e58fa 275 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
276 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
277 /* The value array need not be initialized at this point */
278 cpu->cpreg_array_len++;
279 }
280}
281
282static void count_cpreg(gpointer key, gpointer opaque)
283{
284 ARMCPU *cpu = opaque;
285 uint64_t regidx;
286 const ARMCPRegInfo *ri;
287
288 regidx = *(uint32_t *)key;
60322b39 289 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 290
7a0e58fa 291 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
292 cpu->cpreg_array_len++;
293 }
294}
295
296static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
297{
cbf239b7
AR
298 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
299 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 300
cbf239b7
AR
301 if (aidx > bidx) {
302 return 1;
303 }
304 if (aidx < bidx) {
305 return -1;
306 }
307 return 0;
721fae12
PM
308}
309
310void init_cpreg_list(ARMCPU *cpu)
311{
312 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
313 * Note that we require cpreg_tuples[] to be sorted by key ID.
314 */
57b6d95e 315 GList *keys;
721fae12
PM
316 int arraylen;
317
57b6d95e 318 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
319 keys = g_list_sort(keys, cpreg_key_compare);
320
321 cpu->cpreg_array_len = 0;
322
323 g_list_foreach(keys, count_cpreg, cpu);
324
325 arraylen = cpu->cpreg_array_len;
326 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
327 cpu->cpreg_values = g_new(uint64_t, arraylen);
328 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
329 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
330 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
331 cpu->cpreg_array_len = 0;
332
333 g_list_foreach(keys, add_cpreg_to_list, cpu);
334
335 assert(cpu->cpreg_array_len == arraylen);
336
337 g_list_free(keys);
338}
339
68e9c2fe
EI
340/*
341 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
342 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
343 *
344 * access_el3_aa32ns: Used to check AArch32 register views.
345 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
346 */
347static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
348 const ARMCPRegInfo *ri,
349 bool isread)
68e9c2fe
EI
350{
351 bool secure = arm_is_secure_below_el3(env);
352
353 assert(!arm_el_is_aa64(env, 3));
354 if (secure) {
355 return CP_ACCESS_TRAP_UNCATEGORIZED;
356 }
357 return CP_ACCESS_OK;
358}
359
360static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
361 const ARMCPRegInfo *ri,
362 bool isread)
68e9c2fe
EI
363{
364 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 365 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
366 }
367 return CP_ACCESS_OK;
368}
369
5513c3ab
PM
370/* Some secure-only AArch32 registers trap to EL3 if used from
371 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
372 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
373 * We assume that the .access field is set to PL1_RW.
374 */
375static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
376 const ARMCPRegInfo *ri,
377 bool isread)
5513c3ab
PM
378{
379 if (arm_current_el(env) == 3) {
380 return CP_ACCESS_OK;
381 }
382 if (arm_is_secure_below_el3(env)) {
383 return CP_ACCESS_TRAP_EL3;
384 }
385 /* This will be EL1 NS and EL2 NS, which just UNDEF */
386 return CP_ACCESS_TRAP_UNCATEGORIZED;
387}
388
187f678d
PM
389/* Check for traps to "powerdown debug" registers, which are controlled
390 * by MDCR.TDOSA
391 */
392static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
393 bool isread)
394{
395 int el = arm_current_el(env);
396
397 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
398 && !arm_is_secure_below_el3(env)) {
399 return CP_ACCESS_TRAP_EL2;
400 }
401 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
402 return CP_ACCESS_TRAP_EL3;
403 }
404 return CP_ACCESS_OK;
405}
406
91b0a238
PM
407/* Check for traps to "debug ROM" registers, which are controlled
408 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
409 */
410static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
411 bool isread)
412{
413 int el = arm_current_el(env);
414
415 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
416 && !arm_is_secure_below_el3(env)) {
417 return CP_ACCESS_TRAP_EL2;
418 }
419 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
420 return CP_ACCESS_TRAP_EL3;
421 }
422 return CP_ACCESS_OK;
423}
424
d6c8cf81
PM
425/* Check for traps to general debug registers, which are controlled
426 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
427 */
428static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
429 bool isread)
430{
431 int el = arm_current_el(env);
432
433 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
434 && !arm_is_secure_below_el3(env)) {
435 return CP_ACCESS_TRAP_EL2;
436 }
437 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
438 return CP_ACCESS_TRAP_EL3;
439 }
440 return CP_ACCESS_OK;
441}
442
1fce1ba9
PM
443/* Check for traps to performance monitor registers, which are controlled
444 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
445 */
446static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
447 bool isread)
448{
449 int el = arm_current_el(env);
450
451 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
452 && !arm_is_secure_below_el3(env)) {
453 return CP_ACCESS_TRAP_EL2;
454 }
455 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
456 return CP_ACCESS_TRAP_EL3;
457 }
458 return CP_ACCESS_OK;
459}
460
c4241c7d 461static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 462{
00c8cb0a
AF
463 ARMCPU *cpu = arm_env_get_cpu(env);
464
8d5c773e 465 raw_write(env, ri, value);
00c8cb0a 466 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
467}
468
c4241c7d 469static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 470{
00c8cb0a
AF
471 ARMCPU *cpu = arm_env_get_cpu(env);
472
8d5c773e 473 if (raw_read(env, ri) != value) {
08de207b
PM
474 /* Unlike real hardware the qemu TLB uses virtual addresses,
475 * not modified virtual addresses, so this causes a TLB flush.
476 */
00c8cb0a 477 tlb_flush(CPU(cpu), 1);
8d5c773e 478 raw_write(env, ri, value);
08de207b 479 }
08de207b 480}
c4241c7d
PM
481
482static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
483 uint64_t value)
08de207b 484{
00c8cb0a
AF
485 ARMCPU *cpu = arm_env_get_cpu(env);
486
8d5c773e 487 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
014406b5 488 && !extended_addresses_enabled(env)) {
08de207b
PM
489 /* For VMSA (when not using the LPAE long descriptor page table
490 * format) this register includes the ASID, so do a TLB flush.
491 * For PMSA it is purely a process ID and no action is needed.
492 */
00c8cb0a 493 tlb_flush(CPU(cpu), 1);
08de207b 494 }
8d5c773e 495 raw_write(env, ri, value);
08de207b
PM
496}
497
c4241c7d
PM
498static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
499 uint64_t value)
d929823f
PM
500{
501 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
502 ARMCPU *cpu = arm_env_get_cpu(env);
503
504 tlb_flush(CPU(cpu), 1);
d929823f
PM
505}
506
c4241c7d
PM
507static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
508 uint64_t value)
d929823f
PM
509{
510 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
511 ARMCPU *cpu = arm_env_get_cpu(env);
512
513 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
514}
515
c4241c7d
PM
516static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
517 uint64_t value)
d929823f
PM
518{
519 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
520 ARMCPU *cpu = arm_env_get_cpu(env);
521
522 tlb_flush(CPU(cpu), value == 0);
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) */
31b030d4
AF
529 ARMCPU *cpu = arm_env_get_cpu(env);
530
531 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
532}
533
fa439fc5
PM
534/* IS variants of TLB operations must affect all cores */
535static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
536 uint64_t value)
537{
538 CPUState *other_cs;
539
540 CPU_FOREACH(other_cs) {
541 tlb_flush(other_cs, 1);
542 }
543}
544
545static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
546 uint64_t value)
547{
548 CPUState *other_cs;
549
550 CPU_FOREACH(other_cs) {
551 tlb_flush(other_cs, value == 0);
552 }
553}
554
555static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
556 uint64_t value)
557{
558 CPUState *other_cs;
559
560 CPU_FOREACH(other_cs) {
561 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
562 }
563}
564
565static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
567{
568 CPUState *other_cs;
569
570 CPU_FOREACH(other_cs) {
571 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
572 }
573}
574
e9aa6c21 575static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
576 /* Define the secure and non-secure FCSE identifier CP registers
577 * separately because there is no secure bank in V8 (no _EL3). This allows
578 * the secure register to be properly reset and migrated. There is also no
579 * v8 EL1 version of the register so the non-secure instance stands alone.
580 */
581 { .name = "FCSEIDR(NS)",
582 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
583 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
584 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
585 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
586 { .name = "FCSEIDR(S)",
587 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
588 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
589 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 590 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
591 /* Define the secure and non-secure context identifier CP registers
592 * separately because there is no secure bank in V8 (no _EL3). This allows
593 * the secure register to be properly reset and migrated. In the
594 * non-secure case, the 32-bit register will have reset and migration
595 * disabled during registration as it is handled by the 64-bit instance.
596 */
597 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 598 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
599 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
600 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
601 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
602 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
603 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
604 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
605 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 606 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
607 REGINFO_SENTINEL
608};
609
610static const ARMCPRegInfo not_v8_cp_reginfo[] = {
611 /* NB: Some of these registers exist in v8 but with more precise
612 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
613 */
614 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
615 { .name = "DACR",
616 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
617 .access = PL1_RW, .resetvalue = 0,
618 .writefn = dacr_write, .raw_writefn = raw_write,
619 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
620 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
621 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
622 * For v6 and v5, these mappings are overly broad.
4fdd17dd 623 */
a903c449
EI
624 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
625 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
626 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
627 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
628 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
629 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
630 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 631 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
632 /* Cache maintenance ops; some of this space may be overridden later. */
633 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
634 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
635 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
636 REGINFO_SENTINEL
637};
638
7d57f408
PM
639static const ARMCPRegInfo not_v6_cp_reginfo[] = {
640 /* Not all pre-v6 cores implemented this WFI, so this is slightly
641 * over-broad.
642 */
643 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
644 .access = PL1_W, .type = ARM_CP_WFI },
645 REGINFO_SENTINEL
646};
647
648static const ARMCPRegInfo not_v7_cp_reginfo[] = {
649 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
650 * is UNPREDICTABLE; we choose to NOP as most implementations do).
651 */
652 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
653 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
654 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
655 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
656 * OMAPCP will override this space.
657 */
658 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
659 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
660 .resetvalue = 0 },
661 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
662 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
663 .resetvalue = 0 },
776d4e5c
PM
664 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
665 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 666 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 667 .resetvalue = 0 },
50300698
PM
668 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
669 * implementing it as RAZ means the "debug architecture version" bits
670 * will read as a reserved value, which should cause Linux to not try
671 * to use the debug hardware.
672 */
673 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
674 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
675 /* MMU TLB control. Note that the wildcarding means we cover not just
676 * the unified TLB ops but also the dside/iside/inner-shareable variants.
677 */
678 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
679 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 680 .type = ARM_CP_NO_RAW },
995939a6
PM
681 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
682 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 683 .type = ARM_CP_NO_RAW },
995939a6
PM
684 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
685 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 686 .type = ARM_CP_NO_RAW },
995939a6
PM
687 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
688 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 689 .type = ARM_CP_NO_RAW },
a903c449
EI
690 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
691 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
692 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
693 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
694 REGINFO_SENTINEL
695};
696
c4241c7d
PM
697static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
698 uint64_t value)
2771db27 699{
f0aff255
FA
700 uint32_t mask = 0;
701
702 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
703 if (!arm_feature(env, ARM_FEATURE_V8)) {
704 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
705 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
706 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
707 */
708 if (arm_feature(env, ARM_FEATURE_VFP)) {
709 /* VFP coprocessor: cp10 & cp11 [23:20] */
710 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
711
712 if (!arm_feature(env, ARM_FEATURE_NEON)) {
713 /* ASEDIS [31] bit is RAO/WI */
714 value |= (1 << 31);
715 }
716
717 /* VFPv3 and upwards with NEON implement 32 double precision
718 * registers (D0-D31).
719 */
720 if (!arm_feature(env, ARM_FEATURE_NEON) ||
721 !arm_feature(env, ARM_FEATURE_VFP3)) {
722 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
723 value |= (1 << 30);
724 }
725 }
726 value &= mask;
2771db27 727 }
7ebd5f2e 728 env->cp15.cpacr_el1 = value;
2771db27
PM
729}
730
3f208fd7
PM
731static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
732 bool isread)
c6f19164
GB
733{
734 if (arm_feature(env, ARM_FEATURE_V8)) {
735 /* Check if CPACR accesses are to be trapped to EL2 */
736 if (arm_current_el(env) == 1 &&
737 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
738 return CP_ACCESS_TRAP_EL2;
739 /* Check if CPACR accesses are to be trapped to EL3 */
740 } else if (arm_current_el(env) < 3 &&
741 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
742 return CP_ACCESS_TRAP_EL3;
743 }
744 }
745
746 return CP_ACCESS_OK;
747}
748
3f208fd7
PM
749static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
750 bool isread)
c6f19164
GB
751{
752 /* Check if CPTR accesses are set to trap to EL3 */
753 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
754 return CP_ACCESS_TRAP_EL3;
755 }
756
757 return CP_ACCESS_OK;
758}
759
7d57f408
PM
760static const ARMCPRegInfo v6_cp_reginfo[] = {
761 /* prefetch by MVA in v6, NOP in v7 */
762 { .name = "MVA_prefetch",
763 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
764 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
765 /* We need to break the TB after ISB to execute self-modifying code
766 * correctly and also to take any pending interrupts immediately.
767 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
768 */
7d57f408 769 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 770 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 771 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 772 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 773 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 774 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 775 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 776 .access = PL1_RW,
b848ce2b
FA
777 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
778 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
779 .resetvalue = 0, },
780 /* Watchpoint Fault Address Register : should actually only be present
781 * for 1136, 1176, 11MPCore.
782 */
783 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
784 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 785 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 786 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 787 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 788 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
789 REGINFO_SENTINEL
790};
791
3f208fd7
PM
792static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
793 bool isread)
200ac0ef 794{
3b163b01 795 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
796 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
797 * trapping to EL2 or EL3 for other accesses.
200ac0ef 798 */
1fce1ba9
PM
799 int el = arm_current_el(env);
800
801 if (el == 0 && !env->cp15.c9_pmuserenr) {
fcd25206 802 return CP_ACCESS_TRAP;
200ac0ef 803 }
1fce1ba9
PM
804 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
805 && !arm_is_secure_below_el3(env)) {
806 return CP_ACCESS_TRAP_EL2;
807 }
808 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
809 return CP_ACCESS_TRAP_EL3;
810 }
811
fcd25206 812 return CP_ACCESS_OK;
200ac0ef
PM
813}
814
7c2cb42b 815#ifndef CONFIG_USER_ONLY
87124fde
AF
816
817static inline bool arm_ccnt_enabled(CPUARMState *env)
818{
819 /* This does not support checking PMCCFILTR_EL0 register */
820
821 if (!(env->cp15.c9_pmcr & PMCRE)) {
822 return false;
823 }
824
825 return true;
826}
827
ec7b4ce4
AF
828void pmccntr_sync(CPUARMState *env)
829{
830 uint64_t temp_ticks;
831
352c98e5
LV
832 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
833 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
834
835 if (env->cp15.c9_pmcr & PMCRD) {
836 /* Increment once every 64 processor clock cycles */
837 temp_ticks /= 64;
838 }
839
840 if (arm_ccnt_enabled(env)) {
841 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
842 }
843}
844
c4241c7d
PM
845static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
846 uint64_t value)
200ac0ef 847{
942a155b 848 pmccntr_sync(env);
7c2cb42b
AF
849
850 if (value & PMCRC) {
851 /* The counter has been reset */
852 env->cp15.c15_ccnt = 0;
853 }
854
200ac0ef
PM
855 /* only the DP, X, D and E bits are writable */
856 env->cp15.c9_pmcr &= ~0x39;
857 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 858
942a155b 859 pmccntr_sync(env);
7c2cb42b
AF
860}
861
862static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
863{
c92c0687 864 uint64_t total_ticks;
7c2cb42b 865
942a155b 866 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
867 /* Counter is disabled, do not change value */
868 return env->cp15.c15_ccnt;
869 }
870
352c98e5
LV
871 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
872 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
873
874 if (env->cp15.c9_pmcr & PMCRD) {
875 /* Increment once every 64 processor clock cycles */
876 total_ticks /= 64;
877 }
878 return total_ticks - env->cp15.c15_ccnt;
879}
880
881static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
882 uint64_t value)
883{
c92c0687 884 uint64_t total_ticks;
7c2cb42b 885
942a155b 886 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
887 /* Counter is disabled, set the absolute value */
888 env->cp15.c15_ccnt = value;
889 return;
890 }
891
352c98e5
LV
892 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
893 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
894
895 if (env->cp15.c9_pmcr & PMCRD) {
896 /* Increment once every 64 processor clock cycles */
897 total_ticks /= 64;
898 }
899 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 900}
421c7ebd
PC
901
902static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
903 uint64_t value)
904{
905 uint64_t cur_val = pmccntr_read(env, NULL);
906
907 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
908}
909
ec7b4ce4
AF
910#else /* CONFIG_USER_ONLY */
911
912void pmccntr_sync(CPUARMState *env)
913{
914}
915
7c2cb42b 916#endif
200ac0ef 917
0614601c
AF
918static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
919 uint64_t value)
920{
921 pmccntr_sync(env);
922 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
923 pmccntr_sync(env);
924}
925
c4241c7d 926static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
927 uint64_t value)
928{
200ac0ef
PM
929 value &= (1 << 31);
930 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
931}
932
c4241c7d
PM
933static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
934 uint64_t value)
200ac0ef 935{
200ac0ef
PM
936 value &= (1 << 31);
937 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
938}
939
c4241c7d
PM
940static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
941 uint64_t value)
200ac0ef 942{
200ac0ef 943 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
944}
945
c4241c7d
PM
946static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
947 uint64_t value)
200ac0ef 948{
200ac0ef 949 env->cp15.c9_pmxevtyper = value & 0xff;
200ac0ef
PM
950}
951
c4241c7d 952static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
953 uint64_t value)
954{
955 env->cp15.c9_pmuserenr = value & 1;
200ac0ef
PM
956}
957
c4241c7d
PM
958static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
959 uint64_t value)
200ac0ef
PM
960{
961 /* We have no event counters so only the C bit can be changed */
962 value &= (1 << 31);
963 env->cp15.c9_pminten |= value;
200ac0ef
PM
964}
965
c4241c7d
PM
966static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
967 uint64_t value)
200ac0ef
PM
968{
969 value &= (1 << 31);
970 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
971}
972
c4241c7d
PM
973static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
974 uint64_t value)
8641136c 975{
a505d7fe
PM
976 /* Note that even though the AArch64 view of this register has bits
977 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
978 * architectural requirements for bits which are RES0 only in some
979 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
980 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
981 */
855ea66d 982 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
983}
984
64e0e2de
EI
985static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
986{
987 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
988 * For bits that vary between AArch32/64, code needs to check the
989 * current execution mode before directly using the feature bit.
990 */
991 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
992
993 if (!arm_feature(env, ARM_FEATURE_EL2)) {
994 valid_mask &= ~SCR_HCE;
995
996 /* On ARMv7, SMD (or SCD as it is called in v7) is only
997 * supported if EL2 exists. The bit is UNK/SBZP when
998 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
999 * when EL2 is unavailable.
4eb27640 1000 * On ARMv8, this bit is always available.
64e0e2de 1001 */
4eb27640
GB
1002 if (arm_feature(env, ARM_FEATURE_V7) &&
1003 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1004 valid_mask &= ~SCR_SMD;
1005 }
1006 }
1007
1008 /* Clear all-context RES0 bits. */
1009 value &= valid_mask;
1010 raw_write(env, ri, value);
1011}
1012
c4241c7d 1013static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1014{
1015 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1016
1017 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1018 * bank
1019 */
1020 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1021 ri->secure & ARM_CP_SECSTATE_S);
1022
1023 return cpu->ccsidr[index];
776d4e5c
PM
1024}
1025
c4241c7d
PM
1026static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1027 uint64_t value)
776d4e5c 1028{
8d5c773e 1029 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1030}
1031
1090b9c6
PM
1032static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1033{
1034 CPUState *cs = ENV_GET_CPU(env);
1035 uint64_t ret = 0;
1036
1037 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1038 ret |= CPSR_I;
1039 }
1040 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1041 ret |= CPSR_F;
1042 }
1043 /* External aborts are not possible in QEMU so A bit is always clear */
1044 return ret;
1045}
1046
e9aa6c21 1047static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1048 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1049 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1050 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1051 /* Performance monitors are implementation defined in v7,
1052 * but with an ARM recommended set of registers, which we
1053 * follow (although we don't actually implement any counters)
1054 *
1055 * Performance registers fall into three categories:
1056 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1057 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1058 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1059 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1060 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1061 */
1062 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1063 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1064 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1065 .writefn = pmcntenset_write,
1066 .accessfn = pmreg_access,
1067 .raw_writefn = raw_write },
8521466b
AF
1068 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1069 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1070 .access = PL0_RW, .accessfn = pmreg_access,
1071 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1072 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1073 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1074 .access = PL0_RW,
1075 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1076 .accessfn = pmreg_access,
1077 .writefn = pmcntenclr_write,
7a0e58fa 1078 .type = ARM_CP_ALIAS },
8521466b
AF
1079 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1080 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1081 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1082 .type = ARM_CP_ALIAS,
8521466b
AF
1083 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1084 .writefn = pmcntenclr_write },
200ac0ef
PM
1085 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1086 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1087 .accessfn = pmreg_access,
1088 .writefn = pmovsr_write,
1089 .raw_writefn = raw_write },
978364f1
AF
1090 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1091 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1092 .access = PL0_RW, .accessfn = pmreg_access,
1093 .type = ARM_CP_ALIAS,
1094 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1095 .writefn = pmovsr_write,
1096 .raw_writefn = raw_write },
fcd25206 1097 /* Unimplemented so WI. */
200ac0ef 1098 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
fcd25206 1099 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
200ac0ef 1100 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
fcd25206 1101 * We choose to RAZ/WI.
200ac0ef
PM
1102 */
1103 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
fcd25206
PM
1104 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1105 .accessfn = pmreg_access },
7c2cb42b 1106#ifndef CONFIG_USER_ONLY
200ac0ef 1107 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 1108 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 1109 .readfn = pmccntr_read, .writefn = pmccntr_write32,
fcd25206 1110 .accessfn = pmreg_access },
8521466b
AF
1111 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1112 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1113 .access = PL0_RW, .accessfn = pmreg_access,
1114 .type = ARM_CP_IO,
1115 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1116#endif
8521466b
AF
1117 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1118 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1119 .writefn = pmccfiltr_write,
8521466b
AF
1120 .access = PL0_RW, .accessfn = pmreg_access,
1121 .type = ARM_CP_IO,
1122 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1123 .resetvalue = 0, },
200ac0ef
PM
1124 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1125 .access = PL0_RW,
1126 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
fcd25206
PM
1127 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
1128 .raw_writefn = raw_write },
1129 /* Unimplemented, RAZ/WI. */
200ac0ef 1130 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206
PM
1131 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1132 .accessfn = pmreg_access },
200ac0ef 1133 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1134 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1135 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1136 .resetvalue = 0,
d4e6df63 1137 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1138 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1139 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1140 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1141 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1142 .resetvalue = 0,
1143 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1144 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1145 .access = PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1146 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1147 .resetvalue = 0,
d4e6df63 1148 .writefn = pmintenset_write, .raw_writefn = raw_write },
200ac0ef 1149 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1150 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1151 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1152 .writefn = pmintenclr_write, },
978364f1
AF
1153 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1154 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1155 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1156 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1157 .writefn = pmintenclr_write },
a505d7fe
PM
1158 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
1159 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8641136c 1160 .access = PL1_RW, .writefn = vbar_write,
fb6c91ba
GB
1161 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
1162 offsetof(CPUARMState, cp15.vbar_ns) },
8641136c 1163 .resetvalue = 0 },
7da845b0
PM
1164 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1165 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1166 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1167 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1168 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1169 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1170 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1171 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1172 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1173 * just RAZ for all cores:
1174 */
0ff644a7
PM
1175 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1176 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1177 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1178 /* Auxiliary fault status registers: these also are IMPDEF, and we
1179 * choose to RAZ/WI for all cores.
1180 */
1181 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1182 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1183 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1184 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1185 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1186 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1187 /* MAIR can just read-as-written because we don't implement caches
1188 * and so don't need to care about memory attributes.
1189 */
1190 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1191 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1192 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1193 .resetvalue = 0 },
4cfb8ad8
PM
1194 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1195 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1196 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1197 .resetvalue = 0 },
b0fe2427
PM
1198 /* For non-long-descriptor page tables these are PRRR and NMRR;
1199 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1200 */
1281f8e3 1201 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1202 * allows them to assign the correct fieldoffset based on the endianness
1203 * handled in the field definitions.
1204 */
a903c449 1205 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1206 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1207 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1208 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1209 .resetfn = arm_cp_reset_ignore },
a903c449 1210 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1211 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1212 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1213 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1214 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1215 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1216 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1217 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1218 /* 32 bit ITLB invalidates */
1219 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1220 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1221 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1222 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1223 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1224 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1225 /* 32 bit DTLB invalidates */
1226 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1227 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1228 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1229 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1230 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1231 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1232 /* 32 bit TLB invalidates */
1233 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1234 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1235 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1236 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1237 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1238 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1239 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1240 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1241 REGINFO_SENTINEL
1242};
1243
1244static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1245 /* 32 bit TLB invalidates, Inner Shareable */
1246 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1247 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1248 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1249 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1250 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1251 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1252 .writefn = tlbiasid_is_write },
995939a6 1253 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1254 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1255 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1256 REGINFO_SENTINEL
1257};
1258
c4241c7d
PM
1259static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1260 uint64_t value)
c326b979
PM
1261{
1262 value &= 1;
1263 env->teecr = value;
c326b979
PM
1264}
1265
3f208fd7
PM
1266static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1267 bool isread)
c326b979 1268{
dcbff19b 1269 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1270 return CP_ACCESS_TRAP;
c326b979 1271 }
92611c00 1272 return CP_ACCESS_OK;
c326b979
PM
1273}
1274
1275static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1276 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1277 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1278 .resetvalue = 0,
1279 .writefn = teecr_write },
1280 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1281 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1282 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1283 REGINFO_SENTINEL
1284};
1285
4d31c596 1286static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1287 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1288 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1289 .access = PL0_RW,
54bf36ed 1290 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1291 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1292 .access = PL0_RW,
54bf36ed
FA
1293 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1294 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1295 .resetfn = arm_cp_reset_ignore },
1296 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1297 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1298 .access = PL0_R|PL1_W,
54bf36ed
FA
1299 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1300 .resetvalue = 0},
4d31c596
PM
1301 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1302 .access = PL0_R|PL1_W,
54bf36ed
FA
1303 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1304 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1305 .resetfn = arm_cp_reset_ignore },
54bf36ed 1306 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1307 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1308 .access = PL1_RW,
54bf36ed
FA
1309 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1310 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1311 .access = PL1_RW,
1312 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1313 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1314 .resetvalue = 0 },
4d31c596
PM
1315 REGINFO_SENTINEL
1316};
1317
55d284af
PM
1318#ifndef CONFIG_USER_ONLY
1319
3f208fd7
PM
1320static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1321 bool isread)
00108f2d 1322{
75502672
PM
1323 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1324 * Writable only at the highest implemented exception level.
1325 */
1326 int el = arm_current_el(env);
1327
1328 switch (el) {
1329 case 0:
1330 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1331 return CP_ACCESS_TRAP;
1332 }
1333 break;
1334 case 1:
1335 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1336 arm_is_secure_below_el3(env)) {
1337 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1338 return CP_ACCESS_TRAP_UNCATEGORIZED;
1339 }
1340 break;
1341 case 2:
1342 case 3:
1343 break;
00108f2d 1344 }
75502672
PM
1345
1346 if (!isread && el < arm_highest_el(env)) {
1347 return CP_ACCESS_TRAP_UNCATEGORIZED;
1348 }
1349
00108f2d
PM
1350 return CP_ACCESS_OK;
1351}
1352
3f208fd7
PM
1353static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1354 bool isread)
00108f2d 1355{
0b6440af
EI
1356 unsigned int cur_el = arm_current_el(env);
1357 bool secure = arm_is_secure(env);
1358
00108f2d 1359 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1360 if (cur_el == 0 &&
00108f2d
PM
1361 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1362 return CP_ACCESS_TRAP;
1363 }
0b6440af
EI
1364
1365 if (arm_feature(env, ARM_FEATURE_EL2) &&
1366 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1367 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1368 return CP_ACCESS_TRAP_EL2;
1369 }
00108f2d
PM
1370 return CP_ACCESS_OK;
1371}
1372
3f208fd7
PM
1373static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1374 bool isread)
00108f2d 1375{
0b6440af
EI
1376 unsigned int cur_el = arm_current_el(env);
1377 bool secure = arm_is_secure(env);
1378
00108f2d
PM
1379 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1380 * EL0[PV]TEN is zero.
1381 */
0b6440af 1382 if (cur_el == 0 &&
00108f2d
PM
1383 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1384 return CP_ACCESS_TRAP;
1385 }
0b6440af
EI
1386
1387 if (arm_feature(env, ARM_FEATURE_EL2) &&
1388 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1389 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1390 return CP_ACCESS_TRAP_EL2;
1391 }
00108f2d
PM
1392 return CP_ACCESS_OK;
1393}
1394
1395static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1396 const ARMCPRegInfo *ri,
1397 bool isread)
00108f2d 1398{
3f208fd7 1399 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1400}
1401
1402static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1403 const ARMCPRegInfo *ri,
1404 bool isread)
00108f2d 1405{
3f208fd7 1406 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1407}
1408
3f208fd7
PM
1409static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1410 bool isread)
00108f2d 1411{
3f208fd7 1412 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1413}
1414
3f208fd7
PM
1415static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1416 bool isread)
00108f2d 1417{
3f208fd7 1418 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1419}
1420
b4d3978c 1421static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1422 const ARMCPRegInfo *ri,
1423 bool isread)
b4d3978c
PM
1424{
1425 /* The AArch64 register view of the secure physical timer is
1426 * always accessible from EL3, and configurably accessible from
1427 * Secure EL1.
1428 */
1429 switch (arm_current_el(env)) {
1430 case 1:
1431 if (!arm_is_secure(env)) {
1432 return CP_ACCESS_TRAP;
1433 }
1434 if (!(env->cp15.scr_el3 & SCR_ST)) {
1435 return CP_ACCESS_TRAP_EL3;
1436 }
1437 return CP_ACCESS_OK;
1438 case 0:
1439 case 2:
1440 return CP_ACCESS_TRAP;
1441 case 3:
1442 return CP_ACCESS_OK;
1443 default:
1444 g_assert_not_reached();
1445 }
1446}
1447
55d284af
PM
1448static uint64_t gt_get_countervalue(CPUARMState *env)
1449{
bc72ad67 1450 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1451}
1452
1453static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1454{
1455 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1456
1457 if (gt->ctl & 1) {
1458 /* Timer enabled: calculate and set current ISTATUS, irq, and
1459 * reset timer to when ISTATUS next has to change
1460 */
edac4d8a
EI
1461 uint64_t offset = timeridx == GTIMER_VIRT ?
1462 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1463 uint64_t count = gt_get_countervalue(&cpu->env);
1464 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1465 int istatus = count - offset >= gt->cval;
55d284af
PM
1466 uint64_t nexttick;
1467
1468 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1469 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1470 (istatus && !(gt->ctl & 2)));
1471 if (istatus) {
1472 /* Next transition is when count rolls back over to zero */
1473 nexttick = UINT64_MAX;
1474 } else {
1475 /* Next transition is when we hit cval */
edac4d8a 1476 nexttick = gt->cval + offset;
55d284af
PM
1477 }
1478 /* Note that the desired next expiry time might be beyond the
1479 * signed-64-bit range of a QEMUTimer -- in this case we just
1480 * set the timer for as far in the future as possible. When the
1481 * timer expires we will reset the timer for any remaining period.
1482 */
1483 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1484 nexttick = INT64_MAX / GTIMER_SCALE;
1485 }
bc72ad67 1486 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af
PM
1487 } else {
1488 /* Timer disabled: ISTATUS and timer output always clear */
1489 gt->ctl &= ~4;
1490 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1491 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1492 }
1493}
1494
0e3eca4c
EI
1495static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1496 int timeridx)
55d284af
PM
1497{
1498 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1499
bc72ad67 1500 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1501}
1502
c4241c7d 1503static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1504{
c4241c7d 1505 return gt_get_countervalue(env);
55d284af
PM
1506}
1507
edac4d8a
EI
1508static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1509{
1510 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1511}
1512
c4241c7d 1513static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1514 int timeridx,
c4241c7d 1515 uint64_t value)
55d284af 1516{
55d284af
PM
1517 env->cp15.c14_timer[timeridx].cval = value;
1518 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1519}
c4241c7d 1520
0e3eca4c
EI
1521static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1522 int timeridx)
55d284af 1523{
edac4d8a 1524 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1525
c4241c7d 1526 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1527 (gt_get_countervalue(env) - offset));
55d284af
PM
1528}
1529
c4241c7d 1530static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1531 int timeridx,
c4241c7d 1532 uint64_t value)
55d284af 1533{
edac4d8a 1534 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1535
edac4d8a 1536 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1537 sextract64(value, 0, 32);
55d284af 1538 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1539}
1540
c4241c7d 1541static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1542 int timeridx,
c4241c7d 1543 uint64_t value)
55d284af
PM
1544{
1545 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1546 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1547
d3afacc7 1548 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1549 if ((oldval ^ value) & 1) {
1550 /* Enable toggled */
1551 gt_recalc_timer(cpu, timeridx);
d3afacc7 1552 } else if ((oldval ^ value) & 2) {
55d284af
PM
1553 /* IMASK toggled: don't need to recalculate,
1554 * just set the interrupt line based on ISTATUS
1555 */
1556 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
d3afacc7 1557 (oldval & 4) && !(value & 2));
55d284af 1558 }
55d284af
PM
1559}
1560
0e3eca4c
EI
1561static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1562{
1563 gt_timer_reset(env, ri, GTIMER_PHYS);
1564}
1565
1566static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1567 uint64_t value)
1568{
1569 gt_cval_write(env, ri, GTIMER_PHYS, value);
1570}
1571
1572static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1573{
1574 return gt_tval_read(env, ri, GTIMER_PHYS);
1575}
1576
1577static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1578 uint64_t value)
1579{
1580 gt_tval_write(env, ri, GTIMER_PHYS, value);
1581}
1582
1583static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1584 uint64_t value)
1585{
1586 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1587}
1588
1589static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1590{
1591 gt_timer_reset(env, ri, GTIMER_VIRT);
1592}
1593
1594static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1595 uint64_t value)
1596{
1597 gt_cval_write(env, ri, GTIMER_VIRT, value);
1598}
1599
1600static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1601{
1602 return gt_tval_read(env, ri, GTIMER_VIRT);
1603}
1604
1605static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1606 uint64_t value)
1607{
1608 gt_tval_write(env, ri, GTIMER_VIRT, value);
1609}
1610
1611static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1612 uint64_t value)
1613{
1614 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1615}
1616
edac4d8a
EI
1617static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618 uint64_t value)
1619{
1620 ARMCPU *cpu = arm_env_get_cpu(env);
1621
1622 raw_write(env, ri, value);
1623 gt_recalc_timer(cpu, GTIMER_VIRT);
1624}
1625
b0e66d95
EI
1626static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1627{
1628 gt_timer_reset(env, ri, GTIMER_HYP);
1629}
1630
1631static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1632 uint64_t value)
1633{
1634 gt_cval_write(env, ri, GTIMER_HYP, value);
1635}
1636
1637static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1638{
1639 return gt_tval_read(env, ri, GTIMER_HYP);
1640}
1641
1642static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1643 uint64_t value)
1644{
1645 gt_tval_write(env, ri, GTIMER_HYP, value);
1646}
1647
1648static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1649 uint64_t value)
1650{
1651 gt_ctl_write(env, ri, GTIMER_HYP, value);
1652}
1653
b4d3978c
PM
1654static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1655{
1656 gt_timer_reset(env, ri, GTIMER_SEC);
1657}
1658
1659static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1660 uint64_t value)
1661{
1662 gt_cval_write(env, ri, GTIMER_SEC, value);
1663}
1664
1665static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1666{
1667 return gt_tval_read(env, ri, GTIMER_SEC);
1668}
1669
1670static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1671 uint64_t value)
1672{
1673 gt_tval_write(env, ri, GTIMER_SEC, value);
1674}
1675
1676static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1677 uint64_t value)
1678{
1679 gt_ctl_write(env, ri, GTIMER_SEC, value);
1680}
1681
55d284af
PM
1682void arm_gt_ptimer_cb(void *opaque)
1683{
1684 ARMCPU *cpu = opaque;
1685
1686 gt_recalc_timer(cpu, GTIMER_PHYS);
1687}
1688
1689void arm_gt_vtimer_cb(void *opaque)
1690{
1691 ARMCPU *cpu = opaque;
1692
1693 gt_recalc_timer(cpu, GTIMER_VIRT);
1694}
1695
b0e66d95
EI
1696void arm_gt_htimer_cb(void *opaque)
1697{
1698 ARMCPU *cpu = opaque;
1699
1700 gt_recalc_timer(cpu, GTIMER_HYP);
1701}
1702
b4d3978c
PM
1703void arm_gt_stimer_cb(void *opaque)
1704{
1705 ARMCPU *cpu = opaque;
1706
1707 gt_recalc_timer(cpu, GTIMER_SEC);
1708}
1709
55d284af
PM
1710static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1711 /* Note that CNTFRQ is purely reads-as-written for the benefit
1712 * of software; writing it doesn't actually change the timer frequency.
1713 * Our reset value matches the fixed frequency we implement the timer at.
1714 */
1715 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1716 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1717 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1718 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1719 },
1720 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1721 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1722 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1723 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1724 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1725 },
1726 /* overall control: mostly access permissions */
a7adc4b7
PM
1727 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1728 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1729 .access = PL1_RW,
1730 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1731 .resetvalue = 0,
1732 },
1733 /* per-timer control */
1734 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1735 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1736 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1737 .accessfn = gt_ptimer_access,
1738 .fieldoffset = offsetoflow32(CPUARMState,
1739 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1740 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1741 },
9ff9dd3c
PM
1742 { .name = "CNTP_CTL(S)",
1743 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1744 .secure = ARM_CP_SECSTATE_S,
1745 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1746 .accessfn = gt_ptimer_access,
1747 .fieldoffset = offsetoflow32(CPUARMState,
1748 cp15.c14_timer[GTIMER_SEC].ctl),
1749 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1750 },
a7adc4b7
PM
1751 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1752 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1753 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1754 .accessfn = gt_ptimer_access,
55d284af
PM
1755 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1756 .resetvalue = 0,
0e3eca4c 1757 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1758 },
1759 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1760 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1761 .accessfn = gt_vtimer_access,
1762 .fieldoffset = offsetoflow32(CPUARMState,
1763 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1764 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1765 },
1766 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1767 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1768 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1769 .accessfn = gt_vtimer_access,
55d284af
PM
1770 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1771 .resetvalue = 0,
0e3eca4c 1772 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1773 },
1774 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1775 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 1776 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1777 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1778 .accessfn = gt_ptimer_access,
0e3eca4c 1779 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 1780 },
9ff9dd3c
PM
1781 { .name = "CNTP_TVAL(S)",
1782 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1783 .secure = ARM_CP_SECSTATE_S,
1784 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1785 .accessfn = gt_ptimer_access,
1786 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1787 },
a7adc4b7
PM
1788 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1789 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 1790 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
1791 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
1792 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 1793 },
55d284af 1794 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 1795 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1796 .accessfn = gt_vtimer_access,
0e3eca4c 1797 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 1798 },
a7adc4b7
PM
1799 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1800 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 1801 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
1802 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
1803 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 1804 },
55d284af
PM
1805 /* The counter itself */
1806 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 1807 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1808 .accessfn = gt_pct_access,
a7adc4b7
PM
1809 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1810 },
1811 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1812 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 1813 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 1814 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
1815 },
1816 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 1817 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1818 .accessfn = gt_vct_access,
edac4d8a 1819 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
1820 },
1821 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1822 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 1823 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 1824 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
1825 },
1826 /* Comparison value, indicating when the timer goes off */
1827 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 1828 .secure = ARM_CP_SECSTATE_NS,
55d284af 1829 .access = PL1_RW | PL0_R,
7a0e58fa 1830 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1831 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 1832 .accessfn = gt_ptimer_access,
0e3eca4c 1833 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 1834 },
9ff9dd3c
PM
1835 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
1836 .secure = ARM_CP_SECSTATE_S,
1837 .access = PL1_RW | PL0_R,
1838 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1839 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1840 .accessfn = gt_ptimer_access,
1841 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1842 },
a7adc4b7
PM
1843 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1844 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1845 .access = PL1_RW | PL0_R,
1846 .type = ARM_CP_IO,
1847 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 1848 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 1849 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
1850 },
1851 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1852 .access = PL1_RW | PL0_R,
7a0e58fa 1853 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1854 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 1855 .accessfn = gt_vtimer_access,
0e3eca4c 1856 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
1857 },
1858 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1859 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1860 .access = PL1_RW | PL0_R,
1861 .type = ARM_CP_IO,
1862 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1863 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 1864 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 1865 },
b4d3978c
PM
1866 /* Secure timer -- this is actually restricted to only EL3
1867 * and configurably Secure-EL1 via the accessfn.
1868 */
1869 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
1870 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
1871 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
1872 .accessfn = gt_stimer_access,
1873 .readfn = gt_sec_tval_read,
1874 .writefn = gt_sec_tval_write,
1875 .resetfn = gt_sec_timer_reset,
1876 },
1877 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
1878 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
1879 .type = ARM_CP_IO, .access = PL1_RW,
1880 .accessfn = gt_stimer_access,
1881 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
1882 .resetvalue = 0,
1883 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1884 },
1885 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
1886 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
1887 .type = ARM_CP_IO, .access = PL1_RW,
1888 .accessfn = gt_stimer_access,
1889 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1890 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1891 },
55d284af
PM
1892 REGINFO_SENTINEL
1893};
1894
1895#else
1896/* In user-mode none of the generic timer registers are accessible,
bc72ad67 1897 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
1898 * so instead just don't register any of them.
1899 */
6cc7a3ae 1900static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
1901 REGINFO_SENTINEL
1902};
1903
55d284af
PM
1904#endif
1905
c4241c7d 1906static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 1907{
891a2fe7 1908 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 1909 raw_write(env, ri, value);
891a2fe7 1910 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 1911 raw_write(env, ri, value & 0xfffff6ff);
4a501606 1912 } else {
8d5c773e 1913 raw_write(env, ri, value & 0xfffff1ff);
4a501606 1914 }
4a501606
PM
1915}
1916
1917#ifndef CONFIG_USER_ONLY
1918/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 1919
3f208fd7
PM
1920static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
1921 bool isread)
92611c00
PM
1922{
1923 if (ri->opc2 & 4) {
87562e4f
PM
1924 /* The ATS12NSO* operations must trap to EL3 if executed in
1925 * Secure EL1 (which can only happen if EL3 is AArch64).
1926 * They are simply UNDEF if executed from NS EL1.
1927 * They function normally from EL2 or EL3.
92611c00 1928 */
87562e4f
PM
1929 if (arm_current_el(env) == 1) {
1930 if (arm_is_secure_below_el3(env)) {
1931 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
1932 }
1933 return CP_ACCESS_TRAP_UNCATEGORIZED;
1934 }
92611c00
PM
1935 }
1936 return CP_ACCESS_OK;
1937}
1938
060e8a48 1939static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
d3649702 1940 int access_type, ARMMMUIdx mmu_idx)
4a501606 1941{
a8170e5e 1942 hwaddr phys_addr;
4a501606
PM
1943 target_ulong page_size;
1944 int prot;
b7cc4e82
PC
1945 uint32_t fsr;
1946 bool ret;
01c097f7 1947 uint64_t par64;
8bf5b6a9 1948 MemTxAttrs attrs = {};
e14b5a23 1949 ARMMMUFaultInfo fi = {};
4a501606 1950
d3649702 1951 ret = get_phys_addr(env, value, access_type, mmu_idx,
e14b5a23 1952 &phys_addr, &attrs, &prot, &page_size, &fsr, &fi);
702a9357 1953 if (extended_addresses_enabled(env)) {
b7cc4e82 1954 /* fsr is a DFSR/IFSR value for the long descriptor
702a9357
PM
1955 * translation table format, but with WnR always clear.
1956 * Convert it to a 64-bit PAR.
1957 */
01c097f7 1958 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 1959 if (!ret) {
702a9357 1960 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
1961 if (!attrs.secure) {
1962 par64 |= (1 << 9); /* NS */
1963 }
702a9357 1964 /* We don't set the ATTR or SH fields in the PAR. */
4a501606 1965 } else {
702a9357 1966 par64 |= 1; /* F */
b7cc4e82 1967 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
1968 /* Note that S2WLK and FSTAGE are always zero, because we don't
1969 * implement virtualization and therefore there can't be a stage 2
1970 * fault.
1971 */
4a501606
PM
1972 }
1973 } else {
b7cc4e82 1974 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
1975 * translation table format (with WnR always clear).
1976 * Convert it to a 32-bit PAR.
1977 */
b7cc4e82 1978 if (!ret) {
702a9357
PM
1979 /* We do not set any attribute bits in the PAR */
1980 if (page_size == (1 << 24)
1981 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 1982 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 1983 } else {
01c097f7 1984 par64 = phys_addr & 0xfffff000;
702a9357 1985 }
8bf5b6a9
PM
1986 if (!attrs.secure) {
1987 par64 |= (1 << 9); /* NS */
1988 }
702a9357 1989 } else {
b7cc4e82
PC
1990 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
1991 ((fsr & 0xf) << 1) | 1;
702a9357 1992 }
4a501606 1993 }
060e8a48
PM
1994 return par64;
1995}
1996
1997static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1998{
060e8a48
PM
1999 int access_type = ri->opc2 & 1;
2000 uint64_t par64;
d3649702
PM
2001 ARMMMUIdx mmu_idx;
2002 int el = arm_current_el(env);
2003 bool secure = arm_is_secure_below_el3(env);
060e8a48 2004
d3649702
PM
2005 switch (ri->opc2 & 6) {
2006 case 0:
2007 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2008 switch (el) {
2009 case 3:
2010 mmu_idx = ARMMMUIdx_S1E3;
2011 break;
2012 case 2:
2013 mmu_idx = ARMMMUIdx_S1NSE1;
2014 break;
2015 case 1:
2016 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2017 break;
2018 default:
2019 g_assert_not_reached();
2020 }
2021 break;
2022 case 2:
2023 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2024 switch (el) {
2025 case 3:
2026 mmu_idx = ARMMMUIdx_S1SE0;
2027 break;
2028 case 2:
2029 mmu_idx = ARMMMUIdx_S1NSE0;
2030 break;
2031 case 1:
2032 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2033 break;
2034 default:
2035 g_assert_not_reached();
2036 }
2037 break;
2038 case 4:
2039 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2040 mmu_idx = ARMMMUIdx_S12NSE1;
2041 break;
2042 case 6:
2043 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2044 mmu_idx = ARMMMUIdx_S12NSE0;
2045 break;
2046 default:
2047 g_assert_not_reached();
2048 }
2049
2050 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2051
2052 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2053}
060e8a48 2054
14db7fe0
PM
2055static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2056 uint64_t value)
2057{
2058 int access_type = ri->opc2 & 1;
2059 uint64_t par64;
2060
2061 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2062
2063 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2064}
2065
3f208fd7
PM
2066static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2067 bool isread)
2a47df95
PM
2068{
2069 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2070 return CP_ACCESS_TRAP;
2071 }
2072 return CP_ACCESS_OK;
2073}
2074
060e8a48
PM
2075static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2076 uint64_t value)
2077{
060e8a48 2078 int access_type = ri->opc2 & 1;
d3649702
PM
2079 ARMMMUIdx mmu_idx;
2080 int secure = arm_is_secure_below_el3(env);
2081
2082 switch (ri->opc2 & 6) {
2083 case 0:
2084 switch (ri->opc1) {
2085 case 0: /* AT S1E1R, AT S1E1W */
2086 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2087 break;
2088 case 4: /* AT S1E2R, AT S1E2W */
2089 mmu_idx = ARMMMUIdx_S1E2;
2090 break;
2091 case 6: /* AT S1E3R, AT S1E3W */
2092 mmu_idx = ARMMMUIdx_S1E3;
2093 break;
2094 default:
2095 g_assert_not_reached();
2096 }
2097 break;
2098 case 2: /* AT S1E0R, AT S1E0W */
2099 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2100 break;
2101 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2102 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2103 break;
2104 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2105 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2106 break;
2107 default:
2108 g_assert_not_reached();
2109 }
060e8a48 2110
d3649702 2111 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2112}
4a501606
PM
2113#endif
2114
2115static const ARMCPRegInfo vapa_cp_reginfo[] = {
2116 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2117 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2118 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2119 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2120 .writefn = par_write },
2121#ifndef CONFIG_USER_ONLY
87562e4f 2122 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2123 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2124 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2125 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2126#endif
2127 REGINFO_SENTINEL
2128};
2129
18032bec
PM
2130/* Return basic MPU access permission bits. */
2131static uint32_t simple_mpu_ap_bits(uint32_t val)
2132{
2133 uint32_t ret;
2134 uint32_t mask;
2135 int i;
2136 ret = 0;
2137 mask = 3;
2138 for (i = 0; i < 16; i += 2) {
2139 ret |= (val >> i) & mask;
2140 mask <<= 2;
2141 }
2142 return ret;
2143}
2144
2145/* Pad basic MPU access permission bits to extended format. */
2146static uint32_t extended_mpu_ap_bits(uint32_t val)
2147{
2148 uint32_t ret;
2149 uint32_t mask;
2150 int i;
2151 ret = 0;
2152 mask = 3;
2153 for (i = 0; i < 16; i += 2) {
2154 ret |= (val & mask) << i;
2155 mask <<= 2;
2156 }
2157 return ret;
2158}
2159
c4241c7d
PM
2160static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2161 uint64_t value)
18032bec 2162{
7e09797c 2163 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2164}
2165
c4241c7d 2166static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2167{
7e09797c 2168 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2169}
2170
c4241c7d
PM
2171static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2172 uint64_t value)
18032bec 2173{
7e09797c 2174 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2175}
2176
c4241c7d 2177static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2178{
7e09797c 2179 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2180}
2181
6cb0b013
PC
2182static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2183{
2184 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2185
2186 if (!u32p) {
2187 return 0;
2188 }
2189
2190 u32p += env->cp15.c6_rgnr;
2191 return *u32p;
2192}
2193
2194static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2195 uint64_t value)
2196{
2197 ARMCPU *cpu = arm_env_get_cpu(env);
2198 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2199
2200 if (!u32p) {
2201 return;
2202 }
2203
2204 u32p += env->cp15.c6_rgnr;
2205 tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */
2206 *u32p = value;
2207}
2208
2209static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2210{
2211 ARMCPU *cpu = arm_env_get_cpu(env);
2212 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2213
2214 if (!u32p) {
2215 return;
2216 }
2217
2218 memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion);
2219}
2220
2221static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2222 uint64_t value)
2223{
2224 ARMCPU *cpu = arm_env_get_cpu(env);
2225 uint32_t nrgs = cpu->pmsav7_dregion;
2226
2227 if (value >= nrgs) {
2228 qemu_log_mask(LOG_GUEST_ERROR,
2229 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2230 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2231 return;
2232 }
2233
2234 raw_write(env, ri, value);
2235}
2236
2237static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2238 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2239 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2240 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2241 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2242 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2243 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2244 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2245 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2246 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2247 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2248 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2249 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2250 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2251 .access = PL1_RW,
2252 .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr),
2253 .writefn = pmsav7_rgnr_write },
2254 REGINFO_SENTINEL
2255};
2256
18032bec
PM
2257static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2258 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2259 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2260 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2261 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2262 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2263 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2264 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2265 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2266 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2267 .access = PL1_RW,
7e09797c
PM
2268 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2269 .resetvalue = 0, },
18032bec
PM
2270 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2271 .access = PL1_RW,
7e09797c
PM
2272 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2273 .resetvalue = 0, },
ecce5c3c
PM
2274 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2275 .access = PL1_RW,
2276 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2277 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2278 .access = PL1_RW,
2279 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2280 /* Protection region base and size registers */
e508a92b
PM
2281 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2282 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2283 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2284 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2285 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2286 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2287 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2288 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2289 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2290 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2291 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2292 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2293 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2294 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2295 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2296 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2297 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2298 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2299 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2300 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2301 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2302 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2303 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2304 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2305 REGINFO_SENTINEL
2306};
2307
c4241c7d
PM
2308static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2309 uint64_t value)
ecce5c3c 2310{
11f136ee 2311 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2312 int maskshift = extract32(value, 0, 3);
2313
e389be16
FA
2314 if (!arm_feature(env, ARM_FEATURE_V8)) {
2315 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2316 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2317 * using Long-desciptor translation table format */
2318 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2319 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2320 /* In an implementation that includes the Security Extensions
2321 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2322 * Short-descriptor translation table format.
2323 */
2324 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2325 } else {
2326 value &= TTBCR_N;
2327 }
e42c4db3 2328 }
e389be16 2329
b6af0975 2330 /* Update the masks corresponding to the TCR bank being written
11f136ee 2331 * Note that we always calculate mask and base_mask, but
e42c4db3 2332 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2333 * for long-descriptor tables the TCR fields are used differently
2334 * and the mask and base_mask values are meaningless.
e42c4db3 2335 */
11f136ee
FA
2336 tcr->raw_tcr = value;
2337 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2338 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2339}
2340
c4241c7d
PM
2341static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2342 uint64_t value)
d4e6df63 2343{
00c8cb0a
AF
2344 ARMCPU *cpu = arm_env_get_cpu(env);
2345
d4e6df63
PM
2346 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2347 /* With LPAE the TTBCR could result in a change of ASID
2348 * via the TTBCR.A1 bit, so do a TLB flush.
2349 */
00c8cb0a 2350 tlb_flush(CPU(cpu), 1);
d4e6df63 2351 }
c4241c7d 2352 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2353}
2354
ecce5c3c
PM
2355static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2356{
11f136ee
FA
2357 TCR *tcr = raw_ptr(env, ri);
2358
2359 /* Reset both the TCR as well as the masks corresponding to the bank of
2360 * the TCR being reset.
2361 */
2362 tcr->raw_tcr = 0;
2363 tcr->mask = 0;
2364 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2365}
2366
cb2e37df
PM
2367static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2368 uint64_t value)
2369{
00c8cb0a 2370 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2371 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2372
cb2e37df 2373 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
00c8cb0a 2374 tlb_flush(CPU(cpu), 1);
11f136ee 2375 tcr->raw_tcr = value;
cb2e37df
PM
2376}
2377
327ed10f
PM
2378static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2379 uint64_t value)
2380{
2381 /* 64 bit accesses to the TTBRs can change the ASID and so we
2382 * must flush the TLB.
2383 */
2384 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2385 ARMCPU *cpu = arm_env_get_cpu(env);
2386
2387 tlb_flush(CPU(cpu), 1);
327ed10f
PM
2388 }
2389 raw_write(env, ri, value);
2390}
2391
b698e9cf
EI
2392static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2393 uint64_t value)
2394{
2395 ARMCPU *cpu = arm_env_get_cpu(env);
2396 CPUState *cs = CPU(cpu);
2397
2398 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2399 if (raw_read(env, ri) != value) {
2400 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2401 ARMMMUIdx_S2NS, -1);
2402 raw_write(env, ri, value);
2403 }
2404}
2405
8e5d75c9 2406static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2407 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2408 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2409 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2410 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2411 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2412 .access = PL1_RW, .resetvalue = 0,
2413 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2414 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2415 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2416 .access = PL1_RW, .resetvalue = 0,
2417 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2418 offsetof(CPUARMState, cp15.dfar_ns) } },
2419 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2420 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2421 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2422 .resetvalue = 0, },
2423 REGINFO_SENTINEL
2424};
2425
2426static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2427 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2428 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2429 .access = PL1_RW,
d81c519c 2430 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2431 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2432 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2433 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2434 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2435 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2436 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2437 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2438 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2439 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2440 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2441 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2442 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2443 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2444 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2445 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2446 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2447 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2448 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2449 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2450 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2451 REGINFO_SENTINEL
2452};
2453
c4241c7d
PM
2454static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2455 uint64_t value)
1047b9d7
PM
2456{
2457 env->cp15.c15_ticonfig = value & 0xe7;
2458 /* The OS_TYPE bit in this register changes the reported CPUID! */
2459 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2460 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2461}
2462
c4241c7d
PM
2463static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2464 uint64_t value)
1047b9d7
PM
2465{
2466 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2467}
2468
c4241c7d
PM
2469static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2470 uint64_t value)
1047b9d7
PM
2471{
2472 /* Wait-for-interrupt (deprecated) */
c3affe56 2473 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2474}
2475
c4241c7d
PM
2476static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2477 uint64_t value)
c4804214
PM
2478{
2479 /* On OMAP there are registers indicating the max/min index of dcache lines
2480 * containing a dirty line; cache flush operations have to reset these.
2481 */
2482 env->cp15.c15_i_max = 0x000;
2483 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2484}
2485
18032bec
PM
2486static const ARMCPRegInfo omap_cp_reginfo[] = {
2487 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2488 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2489 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2490 .resetvalue = 0, },
1047b9d7
PM
2491 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2492 .access = PL1_RW, .type = ARM_CP_NOP },
2493 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2494 .access = PL1_RW,
2495 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2496 .writefn = omap_ticonfig_write },
2497 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2498 .access = PL1_RW,
2499 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2500 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2501 .access = PL1_RW, .resetvalue = 0xff0,
2502 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2503 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2504 .access = PL1_RW,
2505 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2506 .writefn = omap_threadid_write },
2507 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2508 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2509 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2510 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2511 /* TODO: Peripheral port remap register:
2512 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2513 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2514 * when MMU is off.
2515 */
c4804214 2516 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2517 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2518 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2519 .writefn = omap_cachemaint_write },
34f90529
PM
2520 { .name = "C9", .cp = 15, .crn = 9,
2521 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2522 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2523 REGINFO_SENTINEL
2524};
2525
c4241c7d
PM
2526static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2527 uint64_t value)
1047b9d7 2528{
c0f4af17 2529 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2530}
2531
2532static const ARMCPRegInfo xscale_cp_reginfo[] = {
2533 { .name = "XSCALE_CPAR",
2534 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2535 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2536 .writefn = xscale_cpar_write, },
2771db27
PM
2537 { .name = "XSCALE_AUXCR",
2538 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2539 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2540 .resetvalue = 0, },
3b771579
PM
2541 /* XScale specific cache-lockdown: since we have no cache we NOP these
2542 * and hope the guest does not really rely on cache behaviour.
2543 */
2544 { .name = "XSCALE_LOCK_ICACHE_LINE",
2545 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2546 .access = PL1_W, .type = ARM_CP_NOP },
2547 { .name = "XSCALE_UNLOCK_ICACHE",
2548 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2549 .access = PL1_W, .type = ARM_CP_NOP },
2550 { .name = "XSCALE_DCACHE_LOCK",
2551 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2552 .access = PL1_RW, .type = ARM_CP_NOP },
2553 { .name = "XSCALE_UNLOCK_DCACHE",
2554 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2555 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2556 REGINFO_SENTINEL
2557};
2558
2559static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2560 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2561 * implementation of this implementation-defined space.
2562 * Ideally this should eventually disappear in favour of actually
2563 * implementing the correct behaviour for all cores.
2564 */
2565 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2566 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2567 .access = PL1_RW,
7a0e58fa 2568 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2569 .resetvalue = 0 },
18032bec
PM
2570 REGINFO_SENTINEL
2571};
2572
c4804214
PM
2573static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2574 /* Cache status: RAZ because we have no cache so it's always clean */
2575 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2576 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2577 .resetvalue = 0 },
c4804214
PM
2578 REGINFO_SENTINEL
2579};
2580
2581static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2582 /* We never have a a block transfer operation in progress */
2583 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2584 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2585 .resetvalue = 0 },
30b05bba
PM
2586 /* The cache ops themselves: these all NOP for QEMU */
2587 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2588 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2589 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2590 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2591 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2592 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2593 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2594 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2595 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2596 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2597 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2598 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2599 REGINFO_SENTINEL
2600};
2601
2602static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2603 /* The cache test-and-clean instructions always return (1 << 30)
2604 * to indicate that there are no dirty cache lines.
2605 */
2606 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2607 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2608 .resetvalue = (1 << 30) },
c4804214 2609 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2610 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2611 .resetvalue = (1 << 30) },
c4804214
PM
2612 REGINFO_SENTINEL
2613};
2614
34f90529
PM
2615static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2616 /* Ignore ReadBuffer accesses */
2617 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2618 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2619 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2620 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2621 REGINFO_SENTINEL
2622};
2623
731de9e6
EI
2624static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2625{
2626 ARMCPU *cpu = arm_env_get_cpu(env);
2627 unsigned int cur_el = arm_current_el(env);
2628 bool secure = arm_is_secure(env);
2629
2630 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2631 return env->cp15.vpidr_el2;
2632 }
2633 return raw_read(env, ri);
2634}
2635
06a7e647 2636static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2637{
eb5e1d3c
PF
2638 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2639 uint64_t mpidr = cpu->mp_affinity;
2640
81bdde9d 2641 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2642 mpidr |= (1U << 31);
81bdde9d
PM
2643 /* Cores which are uniprocessor (non-coherent)
2644 * but still implement the MP extensions set
a8e81b31 2645 * bit 30. (For instance, Cortex-R5).
81bdde9d 2646 */
a8e81b31
PC
2647 if (cpu->mp_is_up) {
2648 mpidr |= (1u << 30);
2649 }
81bdde9d 2650 }
c4241c7d 2651 return mpidr;
81bdde9d
PM
2652}
2653
06a7e647
EI
2654static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2655{
f0d574d6
EI
2656 unsigned int cur_el = arm_current_el(env);
2657 bool secure = arm_is_secure(env);
2658
2659 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2660 return env->cp15.vmpidr_el2;
2661 }
06a7e647
EI
2662 return mpidr_read_val(env);
2663}
2664
81bdde9d 2665static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2666 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2667 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2668 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2669 REGINFO_SENTINEL
2670};
2671
7ac681cf 2672static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2673 /* NOP AMAIR0/1 */
b0fe2427
PM
2674 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2675 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2676 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2677 .resetvalue = 0 },
b0fe2427 2678 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2679 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2680 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2681 .resetvalue = 0 },
891a2fe7 2682 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2683 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2684 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2685 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2686 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2687 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2688 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2689 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2690 .writefn = vmsa_ttbr_write, },
891a2fe7 2691 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2692 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2693 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2694 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2695 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2696 REGINFO_SENTINEL
2697};
2698
c4241c7d 2699static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2700{
c4241c7d 2701 return vfp_get_fpcr(env);
b0d2b7d0
PM
2702}
2703
c4241c7d
PM
2704static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2705 uint64_t value)
b0d2b7d0
PM
2706{
2707 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2708}
2709
c4241c7d 2710static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2711{
c4241c7d 2712 return vfp_get_fpsr(env);
b0d2b7d0
PM
2713}
2714
c4241c7d
PM
2715static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2716 uint64_t value)
b0d2b7d0
PM
2717{
2718 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2719}
2720
3f208fd7
PM
2721static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2722 bool isread)
c2b820fe 2723{
137feaa9 2724 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2725 return CP_ACCESS_TRAP;
2726 }
2727 return CP_ACCESS_OK;
2728}
2729
2730static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2731 uint64_t value)
2732{
2733 env->daif = value & PSTATE_DAIF;
2734}
2735
8af35c37 2736static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
2737 const ARMCPRegInfo *ri,
2738 bool isread)
8af35c37
PM
2739{
2740 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2741 * SCTLR_EL1.UCI is set.
2742 */
137feaa9 2743 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2744 return CP_ACCESS_TRAP;
2745 }
2746 return CP_ACCESS_OK;
2747}
2748
dbb1fb27
AB
2749/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2750 * Page D4-1736 (DDI0487A.b)
2751 */
2752
fd3ed969
PM
2753static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2754 uint64_t value)
168aa23b 2755{
31b030d4 2756 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969 2757 CPUState *cs = CPU(cpu);
dbb1fb27 2758
fd3ed969
PM
2759 if (arm_is_secure_below_el3(env)) {
2760 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2761 } else {
2762 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2763 }
168aa23b
PM
2764}
2765
fd3ed969
PM
2766static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2767 uint64_t value)
168aa23b 2768{
fd3ed969
PM
2769 bool sec = arm_is_secure_below_el3(env);
2770 CPUState *other_cs;
dbb1fb27 2771
fd3ed969
PM
2772 CPU_FOREACH(other_cs) {
2773 if (sec) {
2774 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2775 } else {
2776 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2777 ARMMMUIdx_S12NSE0, -1);
2778 }
2779 }
168aa23b
PM
2780}
2781
fd3ed969
PM
2782static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2783 uint64_t value)
168aa23b 2784{
fd3ed969
PM
2785 /* Note that the 'ALL' scope must invalidate both stage 1 and
2786 * stage 2 translations, whereas most other scopes only invalidate
2787 * stage 1 translations.
2788 */
00c8cb0a 2789 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
2790 CPUState *cs = CPU(cpu);
2791
2792 if (arm_is_secure_below_el3(env)) {
2793 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2794 } else {
2795 if (arm_feature(env, ARM_FEATURE_EL2)) {
2796 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2797 ARMMMUIdx_S2NS, -1);
2798 } else {
2799 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2800 }
2801 }
168aa23b
PM
2802}
2803
fd3ed969 2804static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
2805 uint64_t value)
2806{
fd3ed969
PM
2807 ARMCPU *cpu = arm_env_get_cpu(env);
2808 CPUState *cs = CPU(cpu);
2809
2810 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2811}
2812
43efaa33
PM
2813static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2814 uint64_t value)
2815{
2816 ARMCPU *cpu = arm_env_get_cpu(env);
2817 CPUState *cs = CPU(cpu);
2818
2819 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2820}
2821
fd3ed969
PM
2822static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823 uint64_t value)
2824{
2825 /* Note that the 'ALL' scope must invalidate both stage 1 and
2826 * stage 2 translations, whereas most other scopes only invalidate
2827 * stage 1 translations.
2828 */
2829 bool sec = arm_is_secure_below_el3(env);
2830 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
fa439fc5 2831 CPUState *other_cs;
fa439fc5
PM
2832
2833 CPU_FOREACH(other_cs) {
fd3ed969
PM
2834 if (sec) {
2835 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2836 } else if (has_el2) {
2837 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2838 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2839 } else {
2840 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2841 ARMMMUIdx_S12NSE0, -1);
2842 }
fa439fc5
PM
2843 }
2844}
2845
2bfb9d75
PM
2846static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2847 uint64_t value)
2848{
2849 CPUState *other_cs;
2850
2851 CPU_FOREACH(other_cs) {
2852 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2853 }
2854}
2855
43efaa33
PM
2856static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2857 uint64_t value)
2858{
2859 CPUState *other_cs;
2860
2861 CPU_FOREACH(other_cs) {
2862 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2863 }
2864}
2865
fd3ed969
PM
2866static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2867 uint64_t value)
2868{
2869 /* Invalidate by VA, EL1&0 (AArch64 version).
2870 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2871 * since we don't support flush-for-specific-ASID-only or
2872 * flush-last-level-only.
2873 */
2874 ARMCPU *cpu = arm_env_get_cpu(env);
2875 CPUState *cs = CPU(cpu);
2876 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2877
2878 if (arm_is_secure_below_el3(env)) {
2879 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2880 ARMMMUIdx_S1SE0, -1);
2881 } else {
2882 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2883 ARMMMUIdx_S12NSE0, -1);
2884 }
2885}
2886
2887static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2888 uint64_t value)
fa439fc5 2889{
fd3ed969
PM
2890 /* Invalidate by VA, EL2
2891 * Currently handles both VAE2 and VALE2, since we don't support
2892 * flush-last-level-only.
2893 */
2894 ARMCPU *cpu = arm_env_get_cpu(env);
2895 CPUState *cs = CPU(cpu);
2896 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2897
2898 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2899}
2900
43efaa33
PM
2901static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2902 uint64_t value)
2903{
2904 /* Invalidate by VA, EL3
2905 * Currently handles both VAE3 and VALE3, since we don't support
2906 * flush-last-level-only.
2907 */
2908 ARMCPU *cpu = arm_env_get_cpu(env);
2909 CPUState *cs = CPU(cpu);
2910 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2911
2912 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2913}
2914
fd3ed969
PM
2915static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2916 uint64_t value)
2917{
2918 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
2919 CPUState *other_cs;
2920 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2921
2922 CPU_FOREACH(other_cs) {
fd3ed969
PM
2923 if (sec) {
2924 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2925 ARMMMUIdx_S1SE0, -1);
2926 } else {
2927 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2928 ARMMMUIdx_S12NSE0, -1);
2929 }
fa439fc5
PM
2930 }
2931}
2932
fd3ed969
PM
2933static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2934 uint64_t value)
fa439fc5
PM
2935{
2936 CPUState *other_cs;
fd3ed969 2937 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5
PM
2938
2939 CPU_FOREACH(other_cs) {
fd3ed969 2940 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
fa439fc5
PM
2941 }
2942}
2943
43efaa33
PM
2944static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2945 uint64_t value)
2946{
2947 CPUState *other_cs;
2948 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2949
2950 CPU_FOREACH(other_cs) {
2951 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2952 }
2953}
2954
cea66e91
PM
2955static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2956 uint64_t value)
2957{
2958 /* Invalidate by IPA. This has to invalidate any structures that
2959 * contain only stage 2 translation information, but does not need
2960 * to apply to structures that contain combined stage 1 and stage 2
2961 * translation information.
2962 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2963 */
2964 ARMCPU *cpu = arm_env_get_cpu(env);
2965 CPUState *cs = CPU(cpu);
2966 uint64_t pageaddr;
2967
2968 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2969 return;
2970 }
2971
2972 pageaddr = sextract64(value << 12, 0, 48);
2973
2974 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2975}
2976
2977static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2978 uint64_t value)
2979{
2980 CPUState *other_cs;
2981 uint64_t pageaddr;
2982
2983 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2984 return;
2985 }
2986
2987 pageaddr = sextract64(value << 12, 0, 48);
2988
2989 CPU_FOREACH(other_cs) {
2990 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2991 }
2992}
2993
3f208fd7
PM
2994static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
2995 bool isread)
aca3f40b
PM
2996{
2997 /* We don't implement EL2, so the only control on DC ZVA is the
2998 * bit in the SCTLR which can prohibit access for EL0.
2999 */
137feaa9 3000 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3001 return CP_ACCESS_TRAP;
3002 }
3003 return CP_ACCESS_OK;
3004}
3005
3006static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3007{
3008 ARMCPU *cpu = arm_env_get_cpu(env);
3009 int dzp_bit = 1 << 4;
3010
3011 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3012 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3013 dzp_bit = 0;
3014 }
3015 return cpu->dcz_blocksize | dzp_bit;
3016}
3017
3f208fd7
PM
3018static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3019 bool isread)
f502cfc2 3020{
cdcf1405 3021 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3022 /* Access to SP_EL0 is undefined if it's being used as
3023 * the stack pointer.
3024 */
3025 return CP_ACCESS_TRAP_UNCATEGORIZED;
3026 }
3027 return CP_ACCESS_OK;
3028}
3029
3030static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3031{
3032 return env->pstate & PSTATE_SP;
3033}
3034
3035static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3036{
3037 update_spsel(env, val);
3038}
3039
137feaa9
FA
3040static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3041 uint64_t value)
3042{
3043 ARMCPU *cpu = arm_env_get_cpu(env);
3044
3045 if (raw_read(env, ri) == value) {
3046 /* Skip the TLB flush if nothing actually changed; Linux likes
3047 * to do a lot of pointless SCTLR writes.
3048 */
3049 return;
3050 }
3051
3052 raw_write(env, ri, value);
3053 /* ??? Lots of these bits are not implemented. */
3054 /* This may enable/disable the MMU, so do a TLB flush. */
3055 tlb_flush(CPU(cpu), 1);
3056}
3057
3f208fd7
PM
3058static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3059 bool isread)
03fbf20f
PM
3060{
3061 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3062 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3063 }
3064 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3065 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3066 }
3067 return CP_ACCESS_OK;
3068}
3069
a8d64e73
PM
3070static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3071 uint64_t value)
3072{
3073 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3074}
3075
b0d2b7d0
PM
3076static const ARMCPRegInfo v8_cp_reginfo[] = {
3077 /* Minimal set of EL0-visible registers. This will need to be expanded
3078 * significantly for system emulation of AArch64 CPUs.
3079 */
3080 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3081 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3082 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3083 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3084 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3085 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3086 .access = PL0_RW, .accessfn = aa64_daif_access,
3087 .fieldoffset = offsetof(CPUARMState, daif),
3088 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3089 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3090 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3091 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3092 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3093 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3094 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3095 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3096 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3097 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3098 .readfn = aa64_dczid_read },
3099 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3100 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3101 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3102#ifndef CONFIG_USER_ONLY
3103 /* Avoid overhead of an access check that always passes in user-mode */
3104 .accessfn = aa64_zva_access,
3105#endif
3106 },
0eef9d98
PM
3107 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3108 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3109 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3110 /* Cache ops: all NOPs since we don't emulate caches */
3111 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3112 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3113 .access = PL1_W, .type = ARM_CP_NOP },
3114 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3115 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3116 .access = PL1_W, .type = ARM_CP_NOP },
3117 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3118 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3119 .access = PL0_W, .type = ARM_CP_NOP,
3120 .accessfn = aa64_cacheop_access },
3121 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3122 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3123 .access = PL1_W, .type = ARM_CP_NOP },
3124 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3125 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3126 .access = PL1_W, .type = ARM_CP_NOP },
3127 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3128 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3129 .access = PL0_W, .type = ARM_CP_NOP,
3130 .accessfn = aa64_cacheop_access },
3131 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3132 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3133 .access = PL1_W, .type = ARM_CP_NOP },
3134 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3135 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3136 .access = PL0_W, .type = ARM_CP_NOP,
3137 .accessfn = aa64_cacheop_access },
3138 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3139 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3140 .access = PL0_W, .type = ARM_CP_NOP,
3141 .accessfn = aa64_cacheop_access },
3142 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3143 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3144 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3145 /* TLBI operations */
3146 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3147 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3148 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3149 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3150 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3151 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3152 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3153 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3154 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3155 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3156 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3157 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3158 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3159 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3160 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3161 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3162 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3163 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3164 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3165 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3166 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3167 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3168 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3169 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3170 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3171 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3172 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3173 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3174 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3175 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3176 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3177 .writefn = tlbi_aa64_vae1_write },
168aa23b 3178 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3179 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3180 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3181 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3182 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3183 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3184 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3185 .writefn = tlbi_aa64_vae1_write },
168aa23b 3186 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3187 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3188 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3189 .writefn = tlbi_aa64_vae1_write },
168aa23b 3190 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3191 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3192 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3193 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3194 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3195 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3196 .access = PL2_W, .type = ARM_CP_NO_RAW,
3197 .writefn = tlbi_aa64_ipas2e1is_write },
3198 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3199 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3200 .access = PL2_W, .type = ARM_CP_NO_RAW,
3201 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3202 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3203 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3204 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3205 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3206 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3207 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3208 .access = PL2_W, .type = ARM_CP_NO_RAW,
3209 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3210 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3211 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3212 .access = PL2_W, .type = ARM_CP_NO_RAW,
3213 .writefn = tlbi_aa64_ipas2e1_write },
3214 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3215 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3216 .access = PL2_W, .type = ARM_CP_NO_RAW,
3217 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3218 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3219 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3220 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3221 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3222 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3223 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3224 .access = PL2_W, .type = ARM_CP_NO_RAW,
3225 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3226#ifndef CONFIG_USER_ONLY
3227 /* 64 bit address translation operations */
3228 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3229 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3230 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3231 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3232 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3233 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3234 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3235 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3236 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3237 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3238 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3239 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3240 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3241 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3242 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3243 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3244 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3245 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3246 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3247 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3248 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3249 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3250 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3251 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3252 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3253 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3254 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3255 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3256 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3257 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3258 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3259 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3260 .type = ARM_CP_ALIAS,
3261 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3262 .access = PL1_RW, .resetvalue = 0,
3263 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3264 .writefn = par_write },
19525524 3265#endif
995939a6 3266 /* TLB invalidate last level of translation table walk */
9449fdf6 3267 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3268 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3269 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3270 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3271 .writefn = tlbimvaa_is_write },
9449fdf6 3272 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3273 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3274 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3275 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
9449fdf6
PM
3276 /* 32 bit cache operations */
3277 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3278 .type = ARM_CP_NOP, .access = PL1_W },
3279 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3280 .type = ARM_CP_NOP, .access = PL1_W },
3281 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3282 .type = ARM_CP_NOP, .access = PL1_W },
3283 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3284 .type = ARM_CP_NOP, .access = PL1_W },
3285 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3286 .type = ARM_CP_NOP, .access = PL1_W },
3287 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3288 .type = ARM_CP_NOP, .access = PL1_W },
3289 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3290 .type = ARM_CP_NOP, .access = PL1_W },
3291 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3292 .type = ARM_CP_NOP, .access = PL1_W },
3293 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3294 .type = ARM_CP_NOP, .access = PL1_W },
3295 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3296 .type = ARM_CP_NOP, .access = PL1_W },
3297 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3298 .type = ARM_CP_NOP, .access = PL1_W },
3299 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3300 .type = ARM_CP_NOP, .access = PL1_W },
3301 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3302 .type = ARM_CP_NOP, .access = PL1_W },
3303 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3304 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3305 .access = PL1_RW, .resetvalue = 0,
3306 .writefn = dacr_write, .raw_writefn = raw_write,
3307 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3308 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3309 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3310 .type = ARM_CP_ALIAS,
a0618a19 3311 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3312 .access = PL1_RW,
3313 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3314 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3315 .type = ARM_CP_ALIAS,
a65f1de9 3316 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3317 .access = PL1_RW,
3318 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3319 /* We rely on the access checks not allowing the guest to write to the
3320 * state field when SPSel indicates that it's being used as the stack
3321 * pointer.
3322 */
3323 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3324 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3325 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3326 .type = ARM_CP_ALIAS,
f502cfc2 3327 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3328 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3329 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3330 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3331 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3332 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3333 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3334 .type = ARM_CP_NO_RAW,
f502cfc2 3335 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3336 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3337 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3338 .type = ARM_CP_ALIAS,
3339 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3340 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3341 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3342 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3343 .access = PL2_RW, .resetvalue = 0,
3344 .writefn = dacr_write, .raw_writefn = raw_write,
3345 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3346 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3347 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3348 .access = PL2_RW, .resetvalue = 0,
3349 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3350 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3351 .type = ARM_CP_ALIAS,
3352 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3353 .access = PL2_RW,
3354 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3355 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3356 .type = ARM_CP_ALIAS,
3357 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3358 .access = PL2_RW,
3359 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3360 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3361 .type = ARM_CP_ALIAS,
3362 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3363 .access = PL2_RW,
3364 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3365 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3366 .type = ARM_CP_ALIAS,
3367 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3368 .access = PL2_RW,
3369 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3370 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3372 .resetvalue = 0,
3373 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3374 { .name = "SDCR", .type = ARM_CP_ALIAS,
3375 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3376 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3377 .writefn = sdcr_write,
3378 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3379 REGINFO_SENTINEL
3380};
3381
d42e3c26 3382/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3383static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3384 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3385 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3386 .access = PL2_RW,
3387 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3388 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3389 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3390 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3391 .access = PL2_RW,
3392 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3393 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3394 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3395 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3396 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3397 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3398 .access = PL2_RW, .type = ARM_CP_CONST,
3399 .resetvalue = 0 },
3400 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3401 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3402 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3403 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3404 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3405 .access = PL2_RW, .type = ARM_CP_CONST,
3406 .resetvalue = 0 },
3407 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3408 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3409 .access = PL2_RW, .type = ARM_CP_CONST,
3410 .resetvalue = 0 },
37cd6c24
PM
3411 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3412 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3413 .access = PL2_RW, .type = ARM_CP_CONST,
3414 .resetvalue = 0 },
3415 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3416 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3417 .access = PL2_RW, .type = ARM_CP_CONST,
3418 .resetvalue = 0 },
06ec4c8c
EI
3419 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3420 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3421 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3422 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3423 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3424 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3425 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3426 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3427 .cp = 15, .opc1 = 6, .crm = 2,
3428 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3429 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3430 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3432 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3433 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3434 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3435 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3436 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3437 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3438 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3439 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3440 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3441 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3442 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3443 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3444 .resetvalue = 0 },
0b6440af
EI
3445 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3446 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3447 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3448 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3449 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3450 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3451 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3452 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3453 .resetvalue = 0 },
b0e66d95
EI
3454 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3455 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3456 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3457 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3458 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3459 .resetvalue = 0 },
3460 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3461 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3462 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3463 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3464 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3465 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3466 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3467 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3468 .access = PL2_RW, .accessfn = access_tda,
3469 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3470 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3471 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3472 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3473 .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3474 REGINFO_SENTINEL
3475};
3476
f149e3e8
EI
3477static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3478{
3479 ARMCPU *cpu = arm_env_get_cpu(env);
3480 uint64_t valid_mask = HCR_MASK;
3481
3482 if (arm_feature(env, ARM_FEATURE_EL3)) {
3483 valid_mask &= ~HCR_HCD;
3484 } else {
3485 valid_mask &= ~HCR_TSC;
3486 }
3487
3488 /* Clear RES0 bits. */
3489 value &= valid_mask;
3490
3491 /* These bits change the MMU setup:
3492 * HCR_VM enables stage 2 translation
3493 * HCR_PTW forbids certain page-table setups
3494 * HCR_DC Disables stage1 and enables stage2 translation
3495 */
3496 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3497 tlb_flush(CPU(cpu), 1);
3498 }
3499 raw_write(env, ri, value);
3500}
3501
4771cd01 3502static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3503 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3504 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3505 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3506 .writefn = hcr_write },
3b685ba7 3507 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3508 .type = ARM_CP_ALIAS,
3b685ba7
EI
3509 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3510 .access = PL2_RW,
3511 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3512 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3513 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3514 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3515 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3516 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3517 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3518 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3519 .type = ARM_CP_ALIAS,
3b685ba7 3520 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3521 .access = PL2_RW,
3522 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3523 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3524 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3525 .access = PL2_RW, .writefn = vbar_write,
3526 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3527 .resetvalue = 0 },
884b4dee
GB
3528 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3529 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3530 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3531 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3532 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3533 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3534 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3535 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3536 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3537 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3538 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3539 .resetvalue = 0 },
3540 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3541 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3542 .access = PL2_RW, .type = ARM_CP_ALIAS,
3543 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3544 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3545 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3546 .access = PL2_RW, .type = ARM_CP_CONST,
3547 .resetvalue = 0 },
3548 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3549 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3550 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3551 .access = PL2_RW, .type = ARM_CP_CONST,
3552 .resetvalue = 0 },
37cd6c24
PM
3553 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3554 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3555 .access = PL2_RW, .type = ARM_CP_CONST,
3556 .resetvalue = 0 },
3557 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3558 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3559 .access = PL2_RW, .type = ARM_CP_CONST,
3560 .resetvalue = 0 },
06ec4c8c
EI
3561 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3562 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3563 .access = PL2_RW,
3564 /* no .writefn needed as this can't cause an ASID change;
3565 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3566 */
06ec4c8c 3567 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3568 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3569 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 3570 .type = ARM_CP_ALIAS,
68e9c2fe
EI
3571 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3572 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3573 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3574 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
3575 .access = PL2_RW,
3576 /* no .writefn needed as this can't cause an ASID change;
3577 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3578 */
68e9c2fe 3579 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3580 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3581 .cp = 15, .opc1 = 6, .crm = 2,
3582 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3583 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3584 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3585 .writefn = vttbr_write },
3586 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3587 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3588 .access = PL2_RW, .writefn = vttbr_write,
3589 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3590 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3591 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3592 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3593 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3594 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3595 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3596 .access = PL2_RW, .resetvalue = 0,
3597 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3598 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3599 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3600 .access = PL2_RW, .resetvalue = 0,
3601 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3602 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3603 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3604 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
51da9014
EI
3605 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3606 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3607 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3608 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3609 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3610 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3611 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3612 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3613 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3614 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3615 .access = PL2_W, .type = ARM_CP_NO_RAW,
3616 .writefn = tlbi_aa64_vae2_write },
3617 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3618 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3619 .access = PL2_W, .type = ARM_CP_NO_RAW,
3620 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3621 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3622 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3623 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3624 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3625 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3626 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3627 .access = PL2_W, .type = ARM_CP_NO_RAW,
3628 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3629#ifndef CONFIG_USER_ONLY
2a47df95
PM
3630 /* Unlike the other EL2-related AT operations, these must
3631 * UNDEF from EL3 if EL2 is not implemented, which is why we
3632 * define them here rather than with the rest of the AT ops.
3633 */
3634 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3635 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3636 .access = PL2_W, .accessfn = at_s1e2_access,
3637 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3638 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3639 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3640 .access = PL2_W, .accessfn = at_s1e2_access,
3641 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3642 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3643 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3644 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3645 * to behave as if SCR.NS was 1.
3646 */
3647 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3648 .access = PL2_W,
3649 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3650 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3651 .access = PL2_W,
3652 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3653 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3654 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3655 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3656 * reset values as IMPDEF. We choose to reset to 3 to comply with
3657 * both ARMv7 and ARMv8.
3658 */
3659 .access = PL2_RW, .resetvalue = 3,
3660 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3661 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3662 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3663 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3664 .writefn = gt_cntvoff_write,
3665 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3666 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3667 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3668 .writefn = gt_cntvoff_write,
3669 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3670 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3671 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3672 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3673 .type = ARM_CP_IO, .access = PL2_RW,
3674 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3675 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3676 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3677 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3678 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3679 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3680 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 3681 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
3682 .resetfn = gt_hyp_timer_reset,
3683 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3684 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3685 .type = ARM_CP_IO,
3686 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3687 .access = PL2_RW,
3688 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3689 .resetvalue = 0,
3690 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 3691#endif
14cc7b54
SF
3692 /* The only field of MDCR_EL2 that has a defined architectural reset value
3693 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3694 * don't impelment any PMU event counters, so using zero as a reset
3695 * value for MDCR_EL2 is okay
3696 */
3697 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3698 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3699 .access = PL2_RW, .resetvalue = 0,
3700 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
3701 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3702 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3703 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3704 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3705 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3706 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3707 .access = PL2_RW,
3708 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3b685ba7
EI
3709 REGINFO_SENTINEL
3710};
3711
2f027fc5
PM
3712static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
3713 bool isread)
3714{
3715 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
3716 * At Secure EL1 it traps to EL3.
3717 */
3718 if (arm_current_el(env) == 3) {
3719 return CP_ACCESS_OK;
3720 }
3721 if (arm_is_secure_below_el3(env)) {
3722 return CP_ACCESS_TRAP_EL3;
3723 }
3724 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
3725 if (isread) {
3726 return CP_ACCESS_OK;
3727 }
3728 return CP_ACCESS_TRAP_UNCATEGORIZED;
3729}
3730
60fb1a87
GB
3731static const ARMCPRegInfo el3_cp_reginfo[] = {
3732 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3733 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3734 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3735 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 3736 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 3737 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
3738 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3739 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 3740 .writefn = scr_write },
60fb1a87
GB
3741 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3742 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3743 .access = PL3_RW, .resetvalue = 0,
3744 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3745 { .name = "SDER",
3746 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3747 .access = PL3_RW, .resetvalue = 0,
3748 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 3749 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
3750 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3751 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 3752 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
3753 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3754 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3755 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3756 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
3757 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3758 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3759 .access = PL3_RW,
3760 /* no .writefn needed as this can't cause an ASID change;
3761 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3762 */
11f136ee 3763 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 3764 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3765 .type = ARM_CP_ALIAS,
81547d66
EI
3766 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3767 .access = PL3_RW,
3768 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 3769 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3770 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3771 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
3772 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3773 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3774 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 3775 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3776 .type = ARM_CP_ALIAS,
81547d66 3777 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3778 .access = PL3_RW,
3779 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
3780 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3781 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3782 .access = PL3_RW, .writefn = vbar_write,
3783 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3784 .resetvalue = 0 },
c6f19164
GB
3785 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3786 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3787 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3788 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
3789 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3790 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3791 .access = PL3_RW, .resetvalue = 0,
3792 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
3793 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3794 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3795 .access = PL3_RW, .type = ARM_CP_CONST,
3796 .resetvalue = 0 },
37cd6c24
PM
3797 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3798 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3799 .access = PL3_RW, .type = ARM_CP_CONST,
3800 .resetvalue = 0 },
3801 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3802 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3803 .access = PL3_RW, .type = ARM_CP_CONST,
3804 .resetvalue = 0 },
43efaa33
PM
3805 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3806 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3807 .access = PL3_W, .type = ARM_CP_NO_RAW,
3808 .writefn = tlbi_aa64_alle3is_write },
3809 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3810 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3811 .access = PL3_W, .type = ARM_CP_NO_RAW,
3812 .writefn = tlbi_aa64_vae3is_write },
3813 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3814 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3815 .access = PL3_W, .type = ARM_CP_NO_RAW,
3816 .writefn = tlbi_aa64_vae3is_write },
3817 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3818 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3819 .access = PL3_W, .type = ARM_CP_NO_RAW,
3820 .writefn = tlbi_aa64_alle3_write },
3821 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3822 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3823 .access = PL3_W, .type = ARM_CP_NO_RAW,
3824 .writefn = tlbi_aa64_vae3_write },
3825 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3826 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3827 .access = PL3_W, .type = ARM_CP_NO_RAW,
3828 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
3829 REGINFO_SENTINEL
3830};
3831
3f208fd7
PM
3832static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3833 bool isread)
7da845b0
PM
3834{
3835 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3836 * but the AArch32 CTR has its own reginfo struct)
3837 */
137feaa9 3838 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
3839 return CP_ACCESS_TRAP;
3840 }
3841 return CP_ACCESS_OK;
3842}
3843
1424ca8d
DM
3844static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3845 uint64_t value)
3846{
3847 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
3848 * read via a bit in OSLSR_EL1.
3849 */
3850 int oslock;
3851
3852 if (ri->state == ARM_CP_STATE_AA32) {
3853 oslock = (value == 0xC5ACCE55);
3854 } else {
3855 oslock = value & 1;
3856 }
3857
3858 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
3859}
3860
50300698 3861static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 3862 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
3863 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3864 * unlike DBGDRAR it is never accessible from EL0.
3865 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3866 * accessor.
50300698
PM
3867 */
3868 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
3869 .access = PL0_R, .accessfn = access_tdra,
3870 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
3871 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3872 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
3873 .access = PL1_R, .accessfn = access_tdra,
3874 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 3875 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
3876 .access = PL0_R, .accessfn = access_tdra,
3877 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 3878 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
3879 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3880 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 3881 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
3882 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3883 .resetvalue = 0 },
5e8b12ff
PM
3884 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3885 * We don't implement the configurable EL0 access.
3886 */
3887 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3888 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 3889 .type = ARM_CP_ALIAS,
d6c8cf81 3890 .access = PL1_R, .accessfn = access_tda,
b061a82b 3891 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
3892 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3893 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 3894 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 3895 .accessfn = access_tdosa,
1424ca8d
DM
3896 .writefn = oslar_write },
3897 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
3898 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
3899 .access = PL1_R, .resetvalue = 10,
187f678d 3900 .accessfn = access_tdosa,
1424ca8d 3901 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
3902 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3903 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3904 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
3905 .access = PL1_RW, .accessfn = access_tdosa,
3906 .type = ARM_CP_NOP },
5e8b12ff
PM
3907 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3908 * implement vector catch debug events yet.
3909 */
3910 { .name = "DBGVCR",
3911 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
3912 .access = PL1_RW, .accessfn = access_tda,
3913 .type = ARM_CP_NOP },
50300698
PM
3914 REGINFO_SENTINEL
3915};
3916
3917static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3918 /* 64 bit access versions of the (dummy) debug registers */
3919 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3920 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3921 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3922 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3923 REGINFO_SENTINEL
3924};
3925
9ee98ce8
PM
3926void hw_watchpoint_update(ARMCPU *cpu, int n)
3927{
3928 CPUARMState *env = &cpu->env;
3929 vaddr len = 0;
3930 vaddr wvr = env->cp15.dbgwvr[n];
3931 uint64_t wcr = env->cp15.dbgwcr[n];
3932 int mask;
3933 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3934
3935 if (env->cpu_watchpoint[n]) {
3936 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3937 env->cpu_watchpoint[n] = NULL;
3938 }
3939
3940 if (!extract64(wcr, 0, 1)) {
3941 /* E bit clear : watchpoint disabled */
3942 return;
3943 }
3944
3945 switch (extract64(wcr, 3, 2)) {
3946 case 0:
3947 /* LSC 00 is reserved and must behave as if the wp is disabled */
3948 return;
3949 case 1:
3950 flags |= BP_MEM_READ;
3951 break;
3952 case 2:
3953 flags |= BP_MEM_WRITE;
3954 break;
3955 case 3:
3956 flags |= BP_MEM_ACCESS;
3957 break;
3958 }
3959
3960 /* Attempts to use both MASK and BAS fields simultaneously are
3961 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3962 * thus generating a watchpoint for every byte in the masked region.
3963 */
3964 mask = extract64(wcr, 24, 4);
3965 if (mask == 1 || mask == 2) {
3966 /* Reserved values of MASK; we must act as if the mask value was
3967 * some non-reserved value, or as if the watchpoint were disabled.
3968 * We choose the latter.
3969 */
3970 return;
3971 } else if (mask) {
3972 /* Watchpoint covers an aligned area up to 2GB in size */
3973 len = 1ULL << mask;
3974 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3975 * whether the watchpoint fires when the unmasked bits match; we opt
3976 * to generate the exceptions.
3977 */
3978 wvr &= ~(len - 1);
3979 } else {
3980 /* Watchpoint covers bytes defined by the byte address select bits */
3981 int bas = extract64(wcr, 5, 8);
3982 int basstart;
3983
3984 if (bas == 0) {
3985 /* This must act as if the watchpoint is disabled */
3986 return;
3987 }
3988
3989 if (extract64(wvr, 2, 1)) {
3990 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3991 * ignored, and BAS[3:0] define which bytes to watch.
3992 */
3993 bas &= 0xf;
3994 }
3995 /* The BAS bits are supposed to be programmed to indicate a contiguous
3996 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3997 * we fire for each byte in the word/doubleword addressed by the WVR.
3998 * We choose to ignore any non-zero bits after the first range of 1s.
3999 */
4000 basstart = ctz32(bas);
4001 len = cto32(bas >> basstart);
4002 wvr += basstart;
4003 }
4004
4005 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4006 &env->cpu_watchpoint[n]);
4007}
4008
4009void hw_watchpoint_update_all(ARMCPU *cpu)
4010{
4011 int i;
4012 CPUARMState *env = &cpu->env;
4013
4014 /* Completely clear out existing QEMU watchpoints and our array, to
4015 * avoid possible stale entries following migration load.
4016 */
4017 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4018 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4019
4020 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4021 hw_watchpoint_update(cpu, i);
4022 }
4023}
4024
4025static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4026 uint64_t value)
4027{
4028 ARMCPU *cpu = arm_env_get_cpu(env);
4029 int i = ri->crm;
4030
4031 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4032 * register reads and behaves as if values written are sign extended.
4033 * Bits [1:0] are RES0.
4034 */
4035 value = sextract64(value, 0, 49) & ~3ULL;
4036
4037 raw_write(env, ri, value);
4038 hw_watchpoint_update(cpu, i);
4039}
4040
4041static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4042 uint64_t value)
4043{
4044 ARMCPU *cpu = arm_env_get_cpu(env);
4045 int i = ri->crm;
4046
4047 raw_write(env, ri, value);
4048 hw_watchpoint_update(cpu, i);
4049}
4050
46747d15
PM
4051void hw_breakpoint_update(ARMCPU *cpu, int n)
4052{
4053 CPUARMState *env = &cpu->env;
4054 uint64_t bvr = env->cp15.dbgbvr[n];
4055 uint64_t bcr = env->cp15.dbgbcr[n];
4056 vaddr addr;
4057 int bt;
4058 int flags = BP_CPU;
4059
4060 if (env->cpu_breakpoint[n]) {
4061 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4062 env->cpu_breakpoint[n] = NULL;
4063 }
4064
4065 if (!extract64(bcr, 0, 1)) {
4066 /* E bit clear : watchpoint disabled */
4067 return;
4068 }
4069
4070 bt = extract64(bcr, 20, 4);
4071
4072 switch (bt) {
4073 case 4: /* unlinked address mismatch (reserved if AArch64) */
4074 case 5: /* linked address mismatch (reserved if AArch64) */
4075 qemu_log_mask(LOG_UNIMP,
4076 "arm: address mismatch breakpoint types not implemented");
4077 return;
4078 case 0: /* unlinked address match */
4079 case 1: /* linked address match */
4080 {
4081 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4082 * we behave as if the register was sign extended. Bits [1:0] are
4083 * RES0. The BAS field is used to allow setting breakpoints on 16
4084 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4085 * a bp will fire if the addresses covered by the bp and the addresses
4086 * covered by the insn overlap but the insn doesn't start at the
4087 * start of the bp address range. We choose to require the insn and
4088 * the bp to have the same address. The constraints on writing to
4089 * BAS enforced in dbgbcr_write mean we have only four cases:
4090 * 0b0000 => no breakpoint
4091 * 0b0011 => breakpoint on addr
4092 * 0b1100 => breakpoint on addr + 2
4093 * 0b1111 => breakpoint on addr
4094 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4095 */
4096 int bas = extract64(bcr, 5, 4);
4097 addr = sextract64(bvr, 0, 49) & ~3ULL;
4098 if (bas == 0) {
4099 return;
4100 }
4101 if (bas == 0xc) {
4102 addr += 2;
4103 }
4104 break;
4105 }
4106 case 2: /* unlinked context ID match */
4107 case 8: /* unlinked VMID match (reserved if no EL2) */
4108 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4109 qemu_log_mask(LOG_UNIMP,
4110 "arm: unlinked context breakpoint types not implemented");
4111 return;
4112 case 9: /* linked VMID match (reserved if no EL2) */
4113 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4114 case 3: /* linked context ID match */
4115 default:
4116 /* We must generate no events for Linked context matches (unless
4117 * they are linked to by some other bp/wp, which is handled in
4118 * updates for the linking bp/wp). We choose to also generate no events
4119 * for reserved values.
4120 */
4121 return;
4122 }
4123
4124 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4125}
4126
4127void hw_breakpoint_update_all(ARMCPU *cpu)
4128{
4129 int i;
4130 CPUARMState *env = &cpu->env;
4131
4132 /* Completely clear out existing QEMU breakpoints and our array, to
4133 * avoid possible stale entries following migration load.
4134 */
4135 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4136 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4137
4138 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4139 hw_breakpoint_update(cpu, i);
4140 }
4141}
4142
4143static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4144 uint64_t value)
4145{
4146 ARMCPU *cpu = arm_env_get_cpu(env);
4147 int i = ri->crm;
4148
4149 raw_write(env, ri, value);
4150 hw_breakpoint_update(cpu, i);
4151}
4152
4153static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4154 uint64_t value)
4155{
4156 ARMCPU *cpu = arm_env_get_cpu(env);
4157 int i = ri->crm;
4158
4159 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4160 * copy of BAS[0].
4161 */
4162 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4163 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4164
4165 raw_write(env, ri, value);
4166 hw_breakpoint_update(cpu, i);
4167}
4168
50300698 4169static void define_debug_regs(ARMCPU *cpu)
0b45451e 4170{
50300698
PM
4171 /* Define v7 and v8 architectural debug registers.
4172 * These are just dummy implementations for now.
0b45451e
PM
4173 */
4174 int i;
3ff6fc91 4175 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4176 ARMCPRegInfo dbgdidr = {
4177 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4178 .access = PL0_R, .accessfn = access_tda,
4179 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4180 };
4181
3ff6fc91 4182 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4183 brps = extract32(cpu->dbgdidr, 24, 4);
4184 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4185 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4186
4187 assert(ctx_cmps <= brps);
48eb3ae6
PM
4188
4189 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4190 * of the debug registers such as number of breakpoints;
4191 * check that if they both exist then they agree.
4192 */
4193 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4194 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4195 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4196 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4197 }
0b45451e 4198
48eb3ae6 4199 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4200 define_arm_cp_regs(cpu, debug_cp_reginfo);
4201
4202 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4203 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4204 }
4205
48eb3ae6 4206 for (i = 0; i < brps + 1; i++) {
0b45451e 4207 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4208 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4209 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4210 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4211 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4212 .writefn = dbgbvr_write, .raw_writefn = raw_write
4213 },
10aae104
PM
4214 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4215 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4216 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4217 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4218 .writefn = dbgbcr_write, .raw_writefn = raw_write
4219 },
48eb3ae6
PM
4220 REGINFO_SENTINEL
4221 };
4222 define_arm_cp_regs(cpu, dbgregs);
4223 }
4224
4225 for (i = 0; i < wrps + 1; i++) {
4226 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4227 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4228 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4229 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4230 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4231 .writefn = dbgwvr_write, .raw_writefn = raw_write
4232 },
10aae104
PM
4233 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4234 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4235 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4236 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4237 .writefn = dbgwcr_write, .raw_writefn = raw_write
4238 },
4239 REGINFO_SENTINEL
0b45451e
PM
4240 };
4241 define_arm_cp_regs(cpu, dbgregs);
4242 }
4243}
4244
2ceb98c0
PM
4245void register_cp_regs_for_features(ARMCPU *cpu)
4246{
4247 /* Register all the coprocessor registers based on feature bits */
4248 CPUARMState *env = &cpu->env;
4249 if (arm_feature(env, ARM_FEATURE_M)) {
4250 /* M profile has no coprocessor registers */
4251 return;
4252 }
4253
e9aa6c21 4254 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4255 if (!arm_feature(env, ARM_FEATURE_V8)) {
4256 /* Must go early as it is full of wildcards that may be
4257 * overridden by later definitions.
4258 */
4259 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4260 }
4261
7d57f408 4262 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4263 /* The ID registers all have impdef reset values */
4264 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4265 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4266 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4267 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4268 .resetvalue = cpu->id_pfr0 },
0ff644a7
PM
4269 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4270 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4271 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4272 .resetvalue = cpu->id_pfr1 },
0ff644a7
PM
4273 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4274 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4275 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4276 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4277 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4278 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4279 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4280 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4281 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4282 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4283 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4284 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4285 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4286 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4287 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4288 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4289 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4290 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4291 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4292 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4293 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4294 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4295 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4296 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4297 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4298 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4299 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4300 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4301 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4302 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4303 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4304 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4305 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4306 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4307 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4308 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4309 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4310 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4311 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4312 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4313 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4314 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4315 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4316 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4317 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4318 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4319 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4320 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4321 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4322 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4323 .access = PL1_R, .type = ARM_CP_CONST,
4324 .resetvalue = cpu->id_mmfr4 },
4325 /* 7 is as yet unallocated and must RAZ */
4326 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4327 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4328 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4329 .resetvalue = 0 },
4330 REGINFO_SENTINEL
4331 };
4332 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4333 define_arm_cp_regs(cpu, v6_cp_reginfo);
4334 } else {
4335 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4336 }
4d31c596
PM
4337 if (arm_feature(env, ARM_FEATURE_V6K)) {
4338 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4339 }
5e5cf9e3
PC
4340 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4341 !arm_feature(env, ARM_FEATURE_MPU)) {
995939a6
PM
4342 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4343 }
e9aa6c21 4344 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4345 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4346 * field as main ID register, and we implement only the cycle
4347 * count register.
200ac0ef 4348 */
7c2cb42b 4349#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4350 ARMCPRegInfo pmcr = {
4351 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4352 .access = PL0_RW,
7a0e58fa 4353 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4354 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4355 .accessfn = pmreg_access, .writefn = pmcr_write,
4356 .raw_writefn = raw_write,
200ac0ef 4357 };
8521466b
AF
4358 ARMCPRegInfo pmcr64 = {
4359 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4360 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4361 .access = PL0_RW, .accessfn = pmreg_access,
4362 .type = ARM_CP_IO,
4363 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4364 .resetvalue = cpu->midr & 0xff000000,
4365 .writefn = pmcr_write, .raw_writefn = raw_write,
4366 };
7c2cb42b 4367 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4368 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4369#endif
776d4e5c 4370 ARMCPRegInfo clidr = {
7da845b0
PM
4371 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4372 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4373 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4374 };
776d4e5c 4375 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4376 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4377 define_debug_regs(cpu);
7d57f408
PM
4378 } else {
4379 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4380 }
b0d2b7d0 4381 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4382 /* AArch64 ID registers, which all have impdef reset values.
4383 * Note that within the ID register ranges the unused slots
4384 * must all RAZ, not UNDEF; future architecture versions may
4385 * define new registers here.
4386 */
e60cef86
PM
4387 ARMCPRegInfo v8_idregs[] = {
4388 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4389 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4390 .access = PL1_R, .type = ARM_CP_CONST,
4391 .resetvalue = cpu->id_aa64pfr0 },
4392 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4393 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4394 .access = PL1_R, .type = ARM_CP_CONST,
4395 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4396 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4397 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4398 .access = PL1_R, .type = ARM_CP_CONST,
4399 .resetvalue = 0 },
4400 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4401 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4402 .access = PL1_R, .type = ARM_CP_CONST,
4403 .resetvalue = 0 },
4404 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4405 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4406 .access = PL1_R, .type = ARM_CP_CONST,
4407 .resetvalue = 0 },
4408 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4409 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4410 .access = PL1_R, .type = ARM_CP_CONST,
4411 .resetvalue = 0 },
4412 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4413 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4414 .access = PL1_R, .type = ARM_CP_CONST,
4415 .resetvalue = 0 },
4416 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4417 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4418 .access = PL1_R, .type = ARM_CP_CONST,
4419 .resetvalue = 0 },
e60cef86
PM
4420 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4421 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4422 .access = PL1_R, .type = ARM_CP_CONST,
5d831be2 4423 /* We mask out the PMUVer field, because we don't currently
9225d739
PM
4424 * implement the PMU. Not advertising it prevents the guest
4425 * from trying to use it and getting UNDEFs on registers we
4426 * don't implement.
4427 */
4428 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
e60cef86
PM
4429 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4430 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4431 .access = PL1_R, .type = ARM_CP_CONST,
4432 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4433 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4434 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4435 .access = PL1_R, .type = ARM_CP_CONST,
4436 .resetvalue = 0 },
4437 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4438 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4439 .access = PL1_R, .type = ARM_CP_CONST,
4440 .resetvalue = 0 },
e60cef86
PM
4441 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4442 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4443 .access = PL1_R, .type = ARM_CP_CONST,
4444 .resetvalue = cpu->id_aa64afr0 },
4445 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4446 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4447 .access = PL1_R, .type = ARM_CP_CONST,
4448 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4449 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4450 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4451 .access = PL1_R, .type = ARM_CP_CONST,
4452 .resetvalue = 0 },
4453 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4454 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4455 .access = PL1_R, .type = ARM_CP_CONST,
4456 .resetvalue = 0 },
e60cef86
PM
4457 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4458 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4459 .access = PL1_R, .type = ARM_CP_CONST,
4460 .resetvalue = cpu->id_aa64isar0 },
4461 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4462 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4463 .access = PL1_R, .type = ARM_CP_CONST,
4464 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4465 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4466 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4467 .access = PL1_R, .type = ARM_CP_CONST,
4468 .resetvalue = 0 },
4469 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4470 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4471 .access = PL1_R, .type = ARM_CP_CONST,
4472 .resetvalue = 0 },
4473 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4474 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4475 .access = PL1_R, .type = ARM_CP_CONST,
4476 .resetvalue = 0 },
4477 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4478 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4479 .access = PL1_R, .type = ARM_CP_CONST,
4480 .resetvalue = 0 },
4481 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4482 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4483 .access = PL1_R, .type = ARM_CP_CONST,
4484 .resetvalue = 0 },
4485 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4486 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4487 .access = PL1_R, .type = ARM_CP_CONST,
4488 .resetvalue = 0 },
e60cef86
PM
4489 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4490 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4491 .access = PL1_R, .type = ARM_CP_CONST,
4492 .resetvalue = cpu->id_aa64mmfr0 },
4493 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4494 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4495 .access = PL1_R, .type = ARM_CP_CONST,
4496 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
4497 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4498 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4499 .access = PL1_R, .type = ARM_CP_CONST,
4500 .resetvalue = 0 },
4501 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4502 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4503 .access = PL1_R, .type = ARM_CP_CONST,
4504 .resetvalue = 0 },
4505 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4506 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4507 .access = PL1_R, .type = ARM_CP_CONST,
4508 .resetvalue = 0 },
4509 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4510 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4511 .access = PL1_R, .type = ARM_CP_CONST,
4512 .resetvalue = 0 },
4513 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4514 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4515 .access = PL1_R, .type = ARM_CP_CONST,
4516 .resetvalue = 0 },
4517 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4518 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4519 .access = PL1_R, .type = ARM_CP_CONST,
4520 .resetvalue = 0 },
a50c0f51
PM
4521 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4522 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4523 .access = PL1_R, .type = ARM_CP_CONST,
4524 .resetvalue = cpu->mvfr0 },
4525 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4526 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4527 .access = PL1_R, .type = ARM_CP_CONST,
4528 .resetvalue = cpu->mvfr1 },
4529 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4530 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4531 .access = PL1_R, .type = ARM_CP_CONST,
4532 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
4533 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4534 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4535 .access = PL1_R, .type = ARM_CP_CONST,
4536 .resetvalue = 0 },
4537 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4538 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4539 .access = PL1_R, .type = ARM_CP_CONST,
4540 .resetvalue = 0 },
4541 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4542 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4543 .access = PL1_R, .type = ARM_CP_CONST,
4544 .resetvalue = 0 },
4545 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4546 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4547 .access = PL1_R, .type = ARM_CP_CONST,
4548 .resetvalue = 0 },
4549 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4550 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4551 .access = PL1_R, .type = ARM_CP_CONST,
4552 .resetvalue = 0 },
4054bfa9
AF
4553 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4554 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4555 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4556 .resetvalue = cpu->pmceid0 },
4557 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4558 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4559 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4560 .resetvalue = cpu->pmceid0 },
4561 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4562 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4563 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4564 .resetvalue = cpu->pmceid1 },
4565 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4566 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4567 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4568 .resetvalue = cpu->pmceid1 },
e60cef86
PM
4569 REGINFO_SENTINEL
4570 };
be8e8128
GB
4571 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4572 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4573 !arm_feature(env, ARM_FEATURE_EL2)) {
4574 ARMCPRegInfo rvbar = {
4575 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4576 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4577 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4578 };
4579 define_one_arm_cp_reg(cpu, &rvbar);
4580 }
e60cef86 4581 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
4582 define_arm_cp_regs(cpu, v8_cp_reginfo);
4583 }
3b685ba7 4584 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 4585 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
4586 ARMCPRegInfo vpidr_regs[] = {
4587 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4588 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4589 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4590 .resetvalue = cpu->midr,
4591 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4592 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4593 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4594 .access = PL2_RW, .resetvalue = cpu->midr,
4595 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4596 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4597 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4598 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4599 .resetvalue = vmpidr_def,
4600 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4601 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4602 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4603 .access = PL2_RW,
4604 .resetvalue = vmpidr_def,
4605 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
4606 REGINFO_SENTINEL
4607 };
4608 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4609 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
4610 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4611 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4612 ARMCPRegInfo rvbar = {
4613 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4614 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4615 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4616 };
4617 define_one_arm_cp_reg(cpu, &rvbar);
4618 }
d42e3c26
EI
4619 } else {
4620 /* If EL2 is missing but higher ELs are enabled, we need to
4621 * register the no_el2 reginfos.
4622 */
4623 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
4624 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4625 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
4626 */
4627 ARMCPRegInfo vpidr_regs[] = {
4628 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4629 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4630 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4631 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4632 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4633 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4634 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4635 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4636 .type = ARM_CP_NO_RAW,
4637 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
4638 REGINFO_SENTINEL
4639 };
4640 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4641 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 4642 }
3b685ba7 4643 }
81547d66 4644 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 4645 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
4646 ARMCPRegInfo el3_regs[] = {
4647 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4648 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4649 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
4650 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
4651 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
4652 .access = PL3_RW,
4653 .raw_writefn = raw_write, .writefn = sctlr_write,
4654 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
4655 .resetvalue = cpu->reset_sctlr },
4656 REGINFO_SENTINEL
be8e8128 4657 };
e24fdd23
PM
4658
4659 define_arm_cp_regs(cpu, el3_regs);
81547d66 4660 }
2f027fc5
PM
4661 /* The behaviour of NSACR is sufficiently various that we don't
4662 * try to describe it in a single reginfo:
4663 * if EL3 is 64 bit, then trap to EL3 from S EL1,
4664 * reads as constant 0xc00 from NS EL1 and NS EL2
4665 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
4666 * if v7 without EL3, register doesn't exist
4667 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
4668 */
4669 if (arm_feature(env, ARM_FEATURE_EL3)) {
4670 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4671 ARMCPRegInfo nsacr = {
4672 .name = "NSACR", .type = ARM_CP_CONST,
4673 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4674 .access = PL1_RW, .accessfn = nsacr_access,
4675 .resetvalue = 0xc00
4676 };
4677 define_one_arm_cp_reg(cpu, &nsacr);
4678 } else {
4679 ARMCPRegInfo nsacr = {
4680 .name = "NSACR",
4681 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4682 .access = PL3_RW | PL1_R,
4683 .resetvalue = 0,
4684 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
4685 };
4686 define_one_arm_cp_reg(cpu, &nsacr);
4687 }
4688 } else {
4689 if (arm_feature(env, ARM_FEATURE_V8)) {
4690 ARMCPRegInfo nsacr = {
4691 .name = "NSACR", .type = ARM_CP_CONST,
4692 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4693 .access = PL1_R,
4694 .resetvalue = 0xc00
4695 };
4696 define_one_arm_cp_reg(cpu, &nsacr);
4697 }
4698 }
4699
18032bec 4700 if (arm_feature(env, ARM_FEATURE_MPU)) {
6cb0b013
PC
4701 if (arm_feature(env, ARM_FEATURE_V6)) {
4702 /* PMSAv6 not implemented */
4703 assert(arm_feature(env, ARM_FEATURE_V7));
4704 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4705 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4706 } else {
4707 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4708 }
18032bec 4709 } else {
8e5d75c9 4710 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
4711 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4712 }
c326b979
PM
4713 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4714 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4715 }
6cc7a3ae
PM
4716 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4717 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4718 }
4a501606
PM
4719 if (arm_feature(env, ARM_FEATURE_VAPA)) {
4720 define_arm_cp_regs(cpu, vapa_cp_reginfo);
4721 }
c4804214
PM
4722 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4723 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4724 }
4725 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4726 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4727 }
4728 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4729 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4730 }
18032bec
PM
4731 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4732 define_arm_cp_regs(cpu, omap_cp_reginfo);
4733 }
34f90529
PM
4734 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4735 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4736 }
1047b9d7
PM
4737 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4738 define_arm_cp_regs(cpu, xscale_cp_reginfo);
4739 }
4740 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4741 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4742 }
7ac681cf
PM
4743 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4744 define_arm_cp_regs(cpu, lpae_cp_reginfo);
4745 }
7884849c
PM
4746 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4747 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4748 * be read-only (ie write causes UNDEF exception).
4749 */
4750 {
00a29f3d
PM
4751 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4752 /* Pre-v8 MIDR space.
4753 * Note that the MIDR isn't a simple constant register because
7884849c
PM
4754 * of the TI925 behaviour where writes to another register can
4755 * cause the MIDR value to change.
97ce8d61
PC
4756 *
4757 * Unimplemented registers in the c15 0 0 0 space default to
4758 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4759 * and friends override accordingly.
7884849c
PM
4760 */
4761 { .name = "MIDR",
97ce8d61 4762 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 4763 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 4764 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 4765 .readfn = midr_read,
97ce8d61
PC
4766 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4767 .type = ARM_CP_OVERRIDE },
7884849c
PM
4768 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4769 { .name = "DUMMY",
4770 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4771 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4772 { .name = "DUMMY",
4773 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4774 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4775 { .name = "DUMMY",
4776 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4777 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4778 { .name = "DUMMY",
4779 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4780 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4781 { .name = "DUMMY",
4782 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4783 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4784 REGINFO_SENTINEL
4785 };
00a29f3d 4786 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
4787 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
4789 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
4790 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4791 .readfn = midr_read },
ac00c79f
SF
4792 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4793 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4794 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4795 .access = PL1_R, .resetvalue = cpu->midr },
4796 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4797 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4798 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
4799 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 4801 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
4802 REGINFO_SENTINEL
4803 };
4804 ARMCPRegInfo id_cp_reginfo[] = {
4805 /* These are common to v8 and pre-v8 */
4806 { .name = "CTR",
4807 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4808 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4809 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4811 .access = PL0_R, .accessfn = ctr_el0_access,
4812 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4813 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4814 { .name = "TCMTR",
4815 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4816 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
4817 REGINFO_SENTINEL
4818 };
8085ce63
PC
4819 /* TLBTR is specific to VMSA */
4820 ARMCPRegInfo id_tlbtr_reginfo = {
4821 .name = "TLBTR",
4822 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4823 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4824 };
3281af81
PC
4825 /* MPUIR is specific to PMSA V6+ */
4826 ARMCPRegInfo id_mpuir_reginfo = {
4827 .name = "MPUIR",
4828 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4829 .access = PL1_R, .type = ARM_CP_CONST,
4830 .resetvalue = cpu->pmsav7_dregion << 8
4831 };
7884849c
PM
4832 ARMCPRegInfo crn0_wi_reginfo = {
4833 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4834 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4835 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4836 };
4837 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4838 arm_feature(env, ARM_FEATURE_STRONGARM)) {
4839 ARMCPRegInfo *r;
4840 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
4841 * whole space. Then update the specific ID registers to allow write
4842 * access, so that they ignore writes rather than causing them to
4843 * UNDEF.
7884849c
PM
4844 */
4845 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
4846 for (r = id_pre_v8_midr_cp_reginfo;
4847 r->type != ARM_CP_SENTINEL; r++) {
4848 r->access = PL1_RW;
4849 }
7884849c
PM
4850 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4851 r->access = PL1_RW;
7884849c 4852 }
8085ce63 4853 id_tlbtr_reginfo.access = PL1_RW;
3281af81 4854 id_tlbtr_reginfo.access = PL1_RW;
7884849c 4855 }
00a29f3d
PM
4856 if (arm_feature(env, ARM_FEATURE_V8)) {
4857 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4858 } else {
4859 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4860 }
a703eda1 4861 define_arm_cp_regs(cpu, id_cp_reginfo);
8085ce63
PC
4862 if (!arm_feature(env, ARM_FEATURE_MPU)) {
4863 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
4864 } else if (arm_feature(env, ARM_FEATURE_V7)) {
4865 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 4866 }
7884849c
PM
4867 }
4868
97ce8d61
PC
4869 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4870 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4871 }
4872
2771db27 4873 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
4874 ARMCPRegInfo auxcr_reginfo[] = {
4875 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4876 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4877 .access = PL1_RW, .type = ARM_CP_CONST,
4878 .resetvalue = cpu->reset_auxcr },
4879 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4880 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4881 .access = PL2_RW, .type = ARM_CP_CONST,
4882 .resetvalue = 0 },
4883 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4885 .access = PL3_RW, .type = ARM_CP_CONST,
4886 .resetvalue = 0 },
4887 REGINFO_SENTINEL
2771db27 4888 };
834a6c69 4889 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
4890 }
4891
d8ba780b 4892 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
4893 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4894 /* 32 bit view is [31:18] 0...0 [43:32]. */
4895 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4896 | extract64(cpu->reset_cbar, 32, 12);
4897 ARMCPRegInfo cbar_reginfo[] = {
4898 { .name = "CBAR",
4899 .type = ARM_CP_CONST,
4900 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4901 .access = PL1_R, .resetvalue = cpu->reset_cbar },
4902 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4903 .type = ARM_CP_CONST,
4904 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4905 .access = PL1_R, .resetvalue = cbar32 },
4906 REGINFO_SENTINEL
4907 };
4908 /* We don't implement a r/w 64 bit CBAR currently */
4909 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4910 define_arm_cp_regs(cpu, cbar_reginfo);
4911 } else {
4912 ARMCPRegInfo cbar = {
4913 .name = "CBAR",
4914 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4915 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4916 .fieldoffset = offsetof(CPUARMState,
4917 cp15.c15_config_base_address)
4918 };
4919 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4920 cbar.access = PL1_R;
4921 cbar.fieldoffset = 0;
4922 cbar.type = ARM_CP_CONST;
4923 }
4924 define_one_arm_cp_reg(cpu, &cbar);
4925 }
d8ba780b
PC
4926 }
4927
2771db27
PM
4928 /* Generic registers whose values depend on the implementation */
4929 {
4930 ARMCPRegInfo sctlr = {
5ebafdf3 4931 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
4932 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4933 .access = PL1_RW,
4934 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4935 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
4936 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4937 .raw_writefn = raw_write,
2771db27
PM
4938 };
4939 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4940 /* Normally we would always end the TB on an SCTLR write, but Linux
4941 * arch/arm/mach-pxa/sleep.S expects two instructions following
4942 * an MMU enable to execute from cache. Imitate this behaviour.
4943 */
4944 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4945 }
4946 define_one_arm_cp_reg(cpu, &sctlr);
4947 }
2ceb98c0
PM
4948}
4949
778c3a06 4950ARMCPU *cpu_arm_init(const char *cpu_model)
40f137e1 4951{
9262685b 4952 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
14969266
AF
4953}
4954
4955void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4956{
22169d41 4957 CPUState *cs = CPU(cpu);
14969266
AF
4958 CPUARMState *env = &cpu->env;
4959
6a669427
PM
4960 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4961 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4962 aarch64_fpu_gdb_set_reg,
4963 34, "aarch64-fpu.xml", 0);
4964 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 4965 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4966 51, "arm-neon.xml", 0);
4967 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 4968 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4969 35, "arm-vfp3.xml", 0);
4970 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 4971 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4972 19, "arm-vfp.xml", 0);
4973 }
40f137e1
PB
4974}
4975
777dc784
PM
4976/* Sort alphabetically by type name, except for "any". */
4977static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 4978{
777dc784
PM
4979 ObjectClass *class_a = (ObjectClass *)a;
4980 ObjectClass *class_b = (ObjectClass *)b;
4981 const char *name_a, *name_b;
5adb4839 4982
777dc784
PM
4983 name_a = object_class_get_name(class_a);
4984 name_b = object_class_get_name(class_b);
51492fd1 4985 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 4986 return 1;
51492fd1 4987 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
4988 return -1;
4989 } else {
4990 return strcmp(name_a, name_b);
5adb4839
PB
4991 }
4992}
4993
777dc784 4994static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 4995{
777dc784 4996 ObjectClass *oc = data;
92a31361 4997 CPUListState *s = user_data;
51492fd1
AF
4998 const char *typename;
4999 char *name;
3371d272 5000
51492fd1
AF
5001 typename = object_class_get_name(oc);
5002 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 5003 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
5004 name);
5005 g_free(name);
777dc784
PM
5006}
5007
5008void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5009{
92a31361 5010 CPUListState s = {
777dc784
PM
5011 .file = f,
5012 .cpu_fprintf = cpu_fprintf,
5013 };
5014 GSList *list;
5015
5016 list = object_class_get_list(TYPE_ARM_CPU, false);
5017 list = g_slist_sort(list, arm_cpu_list_compare);
5018 (*cpu_fprintf)(f, "Available CPUs:\n");
5019 g_slist_foreach(list, arm_cpu_list_entry, &s);
5020 g_slist_free(list);
a96c0514
PM
5021#ifdef CONFIG_KVM
5022 /* The 'host' CPU type is dynamically registered only if KVM is
5023 * enabled, so we have to special-case it here:
5024 */
5025 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5026#endif
40f137e1
PB
5027}
5028
78027bb6
CR
5029static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5030{
5031 ObjectClass *oc = data;
5032 CpuDefinitionInfoList **cpu_list = user_data;
5033 CpuDefinitionInfoList *entry;
5034 CpuDefinitionInfo *info;
5035 const char *typename;
5036
5037 typename = object_class_get_name(oc);
5038 info = g_malloc0(sizeof(*info));
5039 info->name = g_strndup(typename,
5040 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5041
5042 entry = g_malloc0(sizeof(*entry));
5043 entry->value = info;
5044 entry->next = *cpu_list;
5045 *cpu_list = entry;
5046}
5047
5048CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5049{
5050 CpuDefinitionInfoList *cpu_list = NULL;
5051 GSList *list;
5052
5053 list = object_class_get_list(TYPE_ARM_CPU, false);
5054 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5055 g_slist_free(list);
5056
5057 return cpu_list;
5058}
5059
6e6efd61 5060static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5061 void *opaque, int state, int secstate,
f5a0a5a5 5062 int crm, int opc1, int opc2)
6e6efd61
PM
5063{
5064 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5065 * add a single reginfo struct to the hash table.
5066 */
5067 uint32_t *key = g_new(uint32_t, 1);
5068 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5069 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5070 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5071
5072 /* Reset the secure state to the specific incoming state. This is
5073 * necessary as the register may have been defined with both states.
5074 */
5075 r2->secure = secstate;
5076
5077 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5078 /* Register is banked (using both entries in array).
5079 * Overwriting fieldoffset as the array is only used to define
5080 * banked registers but later only fieldoffset is used.
f5a0a5a5 5081 */
3f3c82a5
FA
5082 r2->fieldoffset = r->bank_fieldoffsets[ns];
5083 }
5084
5085 if (state == ARM_CP_STATE_AA32) {
5086 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5087 /* If the register is banked then we don't need to migrate or
5088 * reset the 32-bit instance in certain cases:
5089 *
5090 * 1) If the register has both 32-bit and 64-bit instances then we
5091 * can count on the 64-bit instance taking care of the
5092 * non-secure bank.
5093 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5094 * taking care of the secure bank. This requires that separate
5095 * 32 and 64-bit definitions are provided.
5096 */
5097 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5098 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5099 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5100 }
5101 } else if ((secstate != r->secure) && !ns) {
5102 /* The register is not banked so we only want to allow migration of
5103 * the non-secure instance.
5104 */
7a0e58fa 5105 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5106 }
3f3c82a5
FA
5107
5108 if (r->state == ARM_CP_STATE_BOTH) {
5109 /* We assume it is a cp15 register if the .cp field is left unset.
5110 */
5111 if (r2->cp == 0) {
5112 r2->cp = 15;
5113 }
5114
f5a0a5a5 5115#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5116 if (r2->fieldoffset) {
5117 r2->fieldoffset += sizeof(uint32_t);
5118 }
f5a0a5a5 5119#endif
3f3c82a5 5120 }
f5a0a5a5
PM
5121 }
5122 if (state == ARM_CP_STATE_AA64) {
5123 /* To allow abbreviation of ARMCPRegInfo
5124 * definitions, we treat cp == 0 as equivalent to
5125 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5126 * STATE_BOTH definitions are also always "standard
5127 * sysreg" in their AArch64 view (the .cp value may
5128 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5129 */
58a1d8ce 5130 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5131 r2->cp = CP_REG_ARM64_SYSREG_CP;
5132 }
5133 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5134 r2->opc0, opc1, opc2);
5135 } else {
51a79b03 5136 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5137 }
6e6efd61
PM
5138 if (opaque) {
5139 r2->opaque = opaque;
5140 }
67ed771d
PM
5141 /* reginfo passed to helpers is correct for the actual access,
5142 * and is never ARM_CP_STATE_BOTH:
5143 */
5144 r2->state = state;
6e6efd61
PM
5145 /* Make sure reginfo passed to helpers for wildcarded regs
5146 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5147 */
5148 r2->crm = crm;
5149 r2->opc1 = opc1;
5150 r2->opc2 = opc2;
5151 /* By convention, for wildcarded registers only the first
5152 * entry is used for migration; the others are marked as
7a0e58fa 5153 * ALIAS so we don't try to transfer the register
6e6efd61 5154 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5155 * never migratable and not even raw-accessible.
6e6efd61 5156 */
7a0e58fa
PM
5157 if ((r->type & ARM_CP_SPECIAL)) {
5158 r2->type |= ARM_CP_NO_RAW;
5159 }
5160 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5161 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5162 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 5163 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
5164 }
5165
375421cc
PM
5166 /* Check that raw accesses are either forbidden or handled. Note that
5167 * we can't assert this earlier because the setup of fieldoffset for
5168 * banked registers has to be done first.
5169 */
5170 if (!(r2->type & ARM_CP_NO_RAW)) {
5171 assert(!raw_accessors_invalid(r2));
5172 }
5173
6e6efd61
PM
5174 /* Overriding of an existing definition must be explicitly
5175 * requested.
5176 */
5177 if (!(r->type & ARM_CP_OVERRIDE)) {
5178 ARMCPRegInfo *oldreg;
5179 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5180 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5181 fprintf(stderr, "Register redefined: cp=%d %d bit "
5182 "crn=%d crm=%d opc1=%d opc2=%d, "
5183 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5184 r2->crn, r2->crm, r2->opc1, r2->opc2,
5185 oldreg->name, r2->name);
5186 g_assert_not_reached();
5187 }
5188 }
5189 g_hash_table_insert(cpu->cp_regs, key, r2);
5190}
5191
5192
4b6a83fb
PM
5193void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5194 const ARMCPRegInfo *r, void *opaque)
5195{
5196 /* Define implementations of coprocessor registers.
5197 * We store these in a hashtable because typically
5198 * there are less than 150 registers in a space which
5199 * is 16*16*16*8*8 = 262144 in size.
5200 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5201 * If a register is defined twice then the second definition is
5202 * used, so this can be used to define some generic registers and
5203 * then override them with implementation specific variations.
5204 * At least one of the original and the second definition should
5205 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5206 * against accidental use.
f5a0a5a5
PM
5207 *
5208 * The state field defines whether the register is to be
5209 * visible in the AArch32 or AArch64 execution state. If the
5210 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5211 * reginfo structure for the AArch32 view, which sees the lower
5212 * 32 bits of the 64 bit register.
5213 *
5214 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5215 * be wildcarded. AArch64 registers are always considered to be 64
5216 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5217 * the register, if any.
4b6a83fb 5218 */
f5a0a5a5 5219 int crm, opc1, opc2, state;
4b6a83fb
PM
5220 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5221 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5222 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5223 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5224 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5225 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5226 /* 64 bit registers have only CRm and Opc1 fields */
5227 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5228 /* op0 only exists in the AArch64 encodings */
5229 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5230 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5231 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5232 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5233 * encodes a minimum access level for the register. We roll this
5234 * runtime check into our general permission check code, so check
5235 * here that the reginfo's specified permissions are strict enough
5236 * to encompass the generic architectural permission check.
5237 */
5238 if (r->state != ARM_CP_STATE_AA32) {
5239 int mask = 0;
5240 switch (r->opc1) {
5241 case 0: case 1: case 2:
5242 /* min_EL EL1 */
5243 mask = PL1_RW;
5244 break;
5245 case 3:
5246 /* min_EL EL0 */
5247 mask = PL0_RW;
5248 break;
5249 case 4:
5250 /* min_EL EL2 */
5251 mask = PL2_RW;
5252 break;
5253 case 5:
5254 /* unallocated encoding, so not possible */
5255 assert(false);
5256 break;
5257 case 6:
5258 /* min_EL EL3 */
5259 mask = PL3_RW;
5260 break;
5261 case 7:
5262 /* min_EL EL1, secure mode only (we don't check the latter) */
5263 mask = PL1_RW;
5264 break;
5265 default:
5266 /* broken reginfo with out-of-range opc1 */
5267 assert(false);
5268 break;
5269 }
5270 /* assert our permissions are not too lax (stricter is fine) */
5271 assert((r->access & ~mask) == 0);
5272 }
5273
4b6a83fb
PM
5274 /* Check that the register definition has enough info to handle
5275 * reads and writes if they are permitted.
5276 */
5277 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5278 if (r->access & PL3_R) {
3f3c82a5
FA
5279 assert((r->fieldoffset ||
5280 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5281 r->readfn);
4b6a83fb
PM
5282 }
5283 if (r->access & PL3_W) {
3f3c82a5
FA
5284 assert((r->fieldoffset ||
5285 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5286 r->writefn);
4b6a83fb
PM
5287 }
5288 }
5289 /* Bad type field probably means missing sentinel at end of reg list */
5290 assert(cptype_valid(r->type));
5291 for (crm = crmmin; crm <= crmmax; crm++) {
5292 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5293 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5294 for (state = ARM_CP_STATE_AA32;
5295 state <= ARM_CP_STATE_AA64; state++) {
5296 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5297 continue;
5298 }
3f3c82a5
FA
5299 if (state == ARM_CP_STATE_AA32) {
5300 /* Under AArch32 CP registers can be common
5301 * (same for secure and non-secure world) or banked.
5302 */
5303 switch (r->secure) {
5304 case ARM_CP_SECSTATE_S:
5305 case ARM_CP_SECSTATE_NS:
5306 add_cpreg_to_hashtable(cpu, r, opaque, state,
5307 r->secure, crm, opc1, opc2);
5308 break;
5309 default:
5310 add_cpreg_to_hashtable(cpu, r, opaque, state,
5311 ARM_CP_SECSTATE_S,
5312 crm, opc1, opc2);
5313 add_cpreg_to_hashtable(cpu, r, opaque, state,
5314 ARM_CP_SECSTATE_NS,
5315 crm, opc1, opc2);
5316 break;
5317 }
5318 } else {
5319 /* AArch64 registers get mapped to non-secure instance
5320 * of AArch32 */
5321 add_cpreg_to_hashtable(cpu, r, opaque, state,
5322 ARM_CP_SECSTATE_NS,
5323 crm, opc1, opc2);
5324 }
f5a0a5a5 5325 }
4b6a83fb
PM
5326 }
5327 }
5328 }
5329}
5330
5331void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5332 const ARMCPRegInfo *regs, void *opaque)
5333{
5334 /* Define a whole list of registers */
5335 const ARMCPRegInfo *r;
5336 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5337 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5338 }
5339}
5340
60322b39 5341const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5342{
60322b39 5343 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5344}
5345
c4241c7d
PM
5346void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5347 uint64_t value)
4b6a83fb
PM
5348{
5349 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5350}
5351
c4241c7d 5352uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5353{
5354 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5355 return 0;
5356}
5357
f5a0a5a5
PM
5358void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5359{
5360 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5361}
5362
af393ffc 5363static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5364{
5365 /* Return true if it is not valid for us to switch to
5366 * this CPU mode (ie all the UNPREDICTABLE cases in
5367 * the ARM ARM CPSRWriteByInstr pseudocode).
5368 */
af393ffc
PM
5369
5370 /* Changes to or from Hyp via MSR and CPS are illegal. */
5371 if (write_type == CPSRWriteByInstr &&
5372 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5373 mode == ARM_CPU_MODE_HYP)) {
5374 return 1;
5375 }
5376
37064a8b
PM
5377 switch (mode) {
5378 case ARM_CPU_MODE_USR:
10eacda7 5379 return 0;
37064a8b
PM
5380 case ARM_CPU_MODE_SYS:
5381 case ARM_CPU_MODE_SVC:
5382 case ARM_CPU_MODE_ABT:
5383 case ARM_CPU_MODE_UND:
5384 case ARM_CPU_MODE_IRQ:
5385 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5386 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5387 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5388 */
10eacda7
PM
5389 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5390 * and CPS are treated as illegal mode changes.
5391 */
5392 if (write_type == CPSRWriteByInstr &&
5393 (env->cp15.hcr_el2 & HCR_TGE) &&
5394 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5395 !arm_is_secure_below_el3(env)) {
5396 return 1;
5397 }
37064a8b 5398 return 0;
e6c8fc07
PM
5399 case ARM_CPU_MODE_HYP:
5400 return !arm_feature(env, ARM_FEATURE_EL2)
5401 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5402 case ARM_CPU_MODE_MON:
58ae2d1f 5403 return arm_current_el(env) < 3;
37064a8b
PM
5404 default:
5405 return 1;
5406 }
5407}
5408
2f4a40e5
AZ
5409uint32_t cpsr_read(CPUARMState *env)
5410{
5411 int ZF;
6fbe23d5
PB
5412 ZF = (env->ZF == 0);
5413 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5414 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5415 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5416 | ((env->condexec_bits & 0xfc) << 8)
af519934 5417 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5418}
5419
50866ba5
PM
5420void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5421 CPSRWriteType write_type)
2f4a40e5 5422{
6e8801f9
FA
5423 uint32_t changed_daif;
5424
2f4a40e5 5425 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5426 env->ZF = (~val) & CPSR_Z;
5427 env->NF = val;
2f4a40e5
AZ
5428 env->CF = (val >> 29) & 1;
5429 env->VF = (val << 3) & 0x80000000;
5430 }
5431 if (mask & CPSR_Q)
5432 env->QF = ((val & CPSR_Q) != 0);
5433 if (mask & CPSR_T)
5434 env->thumb = ((val & CPSR_T) != 0);
5435 if (mask & CPSR_IT_0_1) {
5436 env->condexec_bits &= ~3;
5437 env->condexec_bits |= (val >> 25) & 3;
5438 }
5439 if (mask & CPSR_IT_2_7) {
5440 env->condexec_bits &= 3;
5441 env->condexec_bits |= (val >> 8) & 0xfc;
5442 }
5443 if (mask & CPSR_GE) {
5444 env->GE = (val >> 16) & 0xf;
5445 }
5446
6e8801f9
FA
5447 /* In a V7 implementation that includes the security extensions but does
5448 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5449 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5450 * bits respectively.
5451 *
5452 * In a V8 implementation, it is permitted for privileged software to
5453 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5454 */
f8c88bbc 5455 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
5456 arm_feature(env, ARM_FEATURE_EL3) &&
5457 !arm_feature(env, ARM_FEATURE_EL2) &&
5458 !arm_is_secure(env)) {
5459
5460 changed_daif = (env->daif ^ val) & mask;
5461
5462 if (changed_daif & CPSR_A) {
5463 /* Check to see if we are allowed to change the masking of async
5464 * abort exceptions from a non-secure state.
5465 */
5466 if (!(env->cp15.scr_el3 & SCR_AW)) {
5467 qemu_log_mask(LOG_GUEST_ERROR,
5468 "Ignoring attempt to switch CPSR_A flag from "
5469 "non-secure world with SCR.AW bit clear\n");
5470 mask &= ~CPSR_A;
5471 }
5472 }
5473
5474 if (changed_daif & CPSR_F) {
5475 /* Check to see if we are allowed to change the masking of FIQ
5476 * exceptions from a non-secure state.
5477 */
5478 if (!(env->cp15.scr_el3 & SCR_FW)) {
5479 qemu_log_mask(LOG_GUEST_ERROR,
5480 "Ignoring attempt to switch CPSR_F flag from "
5481 "non-secure world with SCR.FW bit clear\n");
5482 mask &= ~CPSR_F;
5483 }
5484
5485 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5486 * If this bit is set software is not allowed to mask
5487 * FIQs, but is allowed to set CPSR_F to 0.
5488 */
5489 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5490 (val & CPSR_F)) {
5491 qemu_log_mask(LOG_GUEST_ERROR,
5492 "Ignoring attempt to enable CPSR_F flag "
5493 "(non-maskable FIQ [NMFI] support enabled)\n");
5494 mask &= ~CPSR_F;
5495 }
5496 }
5497 }
5498
4cc35614
PM
5499 env->daif &= ~(CPSR_AIF & mask);
5500 env->daif |= val & CPSR_AIF & mask;
5501
f8c88bbc
PM
5502 if (write_type != CPSRWriteRaw &&
5503 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
5504 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5505 /* Note that we can only get here in USR mode if this is a
5506 * gdb stub write; for this case we follow the architectural
5507 * behaviour for guest writes in USR mode of ignoring an attempt
5508 * to switch mode. (Those are caught by translate.c for writes
5509 * triggered by guest instructions.)
5510 */
5511 mask &= ~CPSR_M;
5512 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
5513 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5514 * v7, and has defined behaviour in v8:
5515 * + leave CPSR.M untouched
5516 * + allow changes to the other CPSR fields
5517 * + set PSTATE.IL
5518 * For user changes via the GDB stub, we don't set PSTATE.IL,
5519 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
5520 */
5521 mask &= ~CPSR_M;
81907a58
PM
5522 if (write_type != CPSRWriteByGDBStub &&
5523 arm_feature(env, ARM_FEATURE_V8)) {
5524 mask |= CPSR_IL;
5525 val |= CPSR_IL;
5526 }
37064a8b
PM
5527 } else {
5528 switch_mode(env, val & CPSR_M);
5529 }
2f4a40e5
AZ
5530 }
5531 mask &= ~CACHED_CPSR_BITS;
5532 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5533}
5534
b26eefb6
PB
5535/* Sign/zero extend */
5536uint32_t HELPER(sxtb16)(uint32_t x)
5537{
5538 uint32_t res;
5539 res = (uint16_t)(int8_t)x;
5540 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5541 return res;
5542}
5543
5544uint32_t HELPER(uxtb16)(uint32_t x)
5545{
5546 uint32_t res;
5547 res = (uint16_t)(uint8_t)x;
5548 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5549 return res;
5550}
5551
f51bbbfe
PB
5552uint32_t HELPER(clz)(uint32_t x)
5553{
7bbcb0af 5554 return clz32(x);
f51bbbfe
PB
5555}
5556
3670669c
PB
5557int32_t HELPER(sdiv)(int32_t num, int32_t den)
5558{
5559 if (den == 0)
5560 return 0;
686eeb93
AJ
5561 if (num == INT_MIN && den == -1)
5562 return INT_MIN;
3670669c
PB
5563 return num / den;
5564}
5565
5566uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5567{
5568 if (den == 0)
5569 return 0;
5570 return num / den;
5571}
5572
5573uint32_t HELPER(rbit)(uint32_t x)
5574{
42fedbca 5575 return revbit32(x);
3670669c
PB
5576}
5577
5fafdf24 5578#if defined(CONFIG_USER_ONLY)
b5ff1b31 5579
9ee6e8bb 5580/* These should probably raise undefined insn exceptions. */
0ecb72a5 5581void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 5582{
a47dddd7
AF
5583 ARMCPU *cpu = arm_env_get_cpu(env);
5584
5585 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
5586}
5587
0ecb72a5 5588uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 5589{
a47dddd7
AF
5590 ARMCPU *cpu = arm_env_get_cpu(env);
5591
5592 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
5593 return 0;
5594}
5595
0ecb72a5 5596void switch_mode(CPUARMState *env, int mode)
b5ff1b31 5597{
a47dddd7
AF
5598 ARMCPU *cpu = arm_env_get_cpu(env);
5599
5600 if (mode != ARM_CPU_MODE_USR) {
5601 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5602 }
b5ff1b31
FB
5603}
5604
012a906b
GB
5605uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5606 uint32_t cur_el, bool secure)
9e729b57
EI
5607{
5608 return 1;
5609}
5610
ce02049d
GB
5611void aarch64_sync_64_to_32(CPUARMState *env)
5612{
5613 g_assert_not_reached();
5614}
5615
b5ff1b31
FB
5616#else
5617
0ecb72a5 5618void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
5619{
5620 int old_mode;
5621 int i;
5622
5623 old_mode = env->uncached_cpsr & CPSR_M;
5624 if (mode == old_mode)
5625 return;
5626
5627 if (old_mode == ARM_CPU_MODE_FIQ) {
5628 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5629 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5630 } else if (mode == ARM_CPU_MODE_FIQ) {
5631 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5632 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5633 }
5634
f5206413 5635 i = bank_number(old_mode);
b5ff1b31
FB
5636 env->banked_r13[i] = env->regs[13];
5637 env->banked_r14[i] = env->regs[14];
5638 env->banked_spsr[i] = env->spsr;
5639
f5206413 5640 i = bank_number(mode);
b5ff1b31
FB
5641 env->regs[13] = env->banked_r13[i];
5642 env->regs[14] = env->banked_r14[i];
5643 env->spsr = env->banked_spsr[i];
5644}
5645
0eeb17d6
GB
5646/* Physical Interrupt Target EL Lookup Table
5647 *
5648 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5649 *
5650 * The below multi-dimensional table is used for looking up the target
5651 * exception level given numerous condition criteria. Specifically, the
5652 * target EL is based on SCR and HCR routing controls as well as the
5653 * currently executing EL and secure state.
5654 *
5655 * Dimensions:
5656 * target_el_table[2][2][2][2][2][4]
5657 * | | | | | +--- Current EL
5658 * | | | | +------ Non-secure(0)/Secure(1)
5659 * | | | +--------- HCR mask override
5660 * | | +------------ SCR exec state control
5661 * | +--------------- SCR mask override
5662 * +------------------ 32-bit(0)/64-bit(1) EL3
5663 *
5664 * The table values are as such:
5665 * 0-3 = EL0-EL3
5666 * -1 = Cannot occur
5667 *
5668 * The ARM ARM target EL table includes entries indicating that an "exception
5669 * is not taken". The two cases where this is applicable are:
5670 * 1) An exception is taken from EL3 but the SCR does not have the exception
5671 * routed to EL3.
5672 * 2) An exception is taken from EL2 but the HCR does not have the exception
5673 * routed to EL2.
5674 * In these two cases, the below table contain a target of EL1. This value is
5675 * returned as it is expected that the consumer of the table data will check
5676 * for "target EL >= current EL" to ensure the exception is not taken.
5677 *
5678 * SCR HCR
5679 * 64 EA AMO From
5680 * BIT IRQ IMO Non-secure Secure
5681 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
5682 */
82c39f6a 5683static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
5684 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5685 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
5686 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5687 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
5688 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5689 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
5690 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5691 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
5692 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
5693 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
5694 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
5695 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
5696 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5697 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
5698 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5699 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
5700};
5701
5702/*
5703 * Determine the target EL for physical exceptions
5704 */
012a906b
GB
5705uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5706 uint32_t cur_el, bool secure)
0eeb17d6
GB
5707{
5708 CPUARMState *env = cs->env_ptr;
2cde031f 5709 int rw;
0eeb17d6
GB
5710 int scr;
5711 int hcr;
5712 int target_el;
2cde031f
SS
5713 /* Is the highest EL AArch64? */
5714 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
5715
5716 if (arm_feature(env, ARM_FEATURE_EL3)) {
5717 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5718 } else {
5719 /* Either EL2 is the highest EL (and so the EL2 register width
5720 * is given by is64); or there is no EL2 or EL3, in which case
5721 * the value of 'rw' does not affect the table lookup anyway.
5722 */
5723 rw = is64;
5724 }
0eeb17d6
GB
5725
5726 switch (excp_idx) {
5727 case EXCP_IRQ:
5728 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5729 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5730 break;
5731 case EXCP_FIQ:
5732 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5733 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5734 break;
5735 default:
5736 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5737 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5738 break;
5739 };
5740
5741 /* If HCR.TGE is set then HCR is treated as being 1 */
5742 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5743
5744 /* Perform a table-lookup for the target EL given the current state */
5745 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5746
5747 assert(target_el > 0);
5748
5749 return target_el;
5750}
5751
9ee6e8bb
PB
5752static void v7m_push(CPUARMState *env, uint32_t val)
5753{
70d74660
AF
5754 CPUState *cs = CPU(arm_env_get_cpu(env));
5755
9ee6e8bb 5756 env->regs[13] -= 4;
ab1da857 5757 stl_phys(cs->as, env->regs[13], val);
9ee6e8bb
PB
5758}
5759
5760static uint32_t v7m_pop(CPUARMState *env)
5761{
70d74660 5762 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb 5763 uint32_t val;
70d74660 5764
fdfba1a2 5765 val = ldl_phys(cs->as, env->regs[13]);
9ee6e8bb
PB
5766 env->regs[13] += 4;
5767 return val;
5768}
5769
5770/* Switch to V7M main or process stack pointer. */
5771static void switch_v7m_sp(CPUARMState *env, int process)
5772{
5773 uint32_t tmp;
5774 if (env->v7m.current_sp != process) {
5775 tmp = env->v7m.other_sp;
5776 env->v7m.other_sp = env->regs[13];
5777 env->regs[13] = tmp;
5778 env->v7m.current_sp = process;
5779 }
5780}
5781
5782static void do_v7m_exception_exit(CPUARMState *env)
5783{
5784 uint32_t type;
5785 uint32_t xpsr;
5786
5787 type = env->regs[15];
5788 if (env->v7m.exception != 0)
983fe826 5789 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
9ee6e8bb
PB
5790
5791 /* Switch to the target stack. */
5792 switch_v7m_sp(env, (type & 4) != 0);
5793 /* Pop registers. */
5794 env->regs[0] = v7m_pop(env);
5795 env->regs[1] = v7m_pop(env);
5796 env->regs[2] = v7m_pop(env);
5797 env->regs[3] = v7m_pop(env);
5798 env->regs[12] = v7m_pop(env);
5799 env->regs[14] = v7m_pop(env);
5800 env->regs[15] = v7m_pop(env);
fcf83ab1
PM
5801 if (env->regs[15] & 1) {
5802 qemu_log_mask(LOG_GUEST_ERROR,
5803 "M profile return from interrupt with misaligned "
5804 "PC is UNPREDICTABLE\n");
5805 /* Actual hardware seems to ignore the lsbit, and there are several
5806 * RTOSes out there which incorrectly assume the r15 in the stack
5807 * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5808 */
5809 env->regs[15] &= ~1U;
5810 }
9ee6e8bb
PB
5811 xpsr = v7m_pop(env);
5812 xpsr_write(env, xpsr, 0xfffffdff);
5813 /* Undo stack alignment. */
5814 if (xpsr & 0x200)
5815 env->regs[13] |= 4;
5816 /* ??? The exception return type specifies Thread/Handler mode. However
5817 this is also implied by the xPSR value. Not sure what to do
5818 if there is a mismatch. */
5819 /* ??? Likewise for mismatches between the CONTROL register and the stack
5820 pointer. */
5821}
5822
27a7ea8a
PB
5823static void arm_log_exception(int idx)
5824{
5825 if (qemu_loglevel_mask(CPU_LOG_INT)) {
5826 const char *exc = NULL;
5827
5828 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
5829 exc = excnames[idx];
5830 }
5831 if (!exc) {
5832 exc = "unknown";
5833 }
5834 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
5835 }
5836}
5837
e6f010cc 5838void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 5839{
e6f010cc
AF
5840 ARMCPU *cpu = ARM_CPU(cs);
5841 CPUARMState *env = &cpu->env;
9ee6e8bb
PB
5842 uint32_t xpsr = xpsr_read(env);
5843 uint32_t lr;
5844 uint32_t addr;
5845
27103424 5846 arm_log_exception(cs->exception_index);
3f1beaca 5847
9ee6e8bb
PB
5848 lr = 0xfffffff1;
5849 if (env->v7m.current_sp)
5850 lr |= 4;
5851 if (env->v7m.exception == 0)
5852 lr |= 8;
5853
5854 /* For exceptions we just mark as pending on the NVIC, and let that
5855 handle it. */
5856 /* TODO: Need to escalate if the current priority is higher than the
5857 one we're raising. */
27103424 5858 switch (cs->exception_index) {
9ee6e8bb 5859 case EXCP_UDEF:
983fe826 5860 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
9ee6e8bb
PB
5861 return;
5862 case EXCP_SWI:
314e2296 5863 /* The PC already points to the next instruction. */
983fe826 5864 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
9ee6e8bb
PB
5865 return;
5866 case EXCP_PREFETCH_ABORT:
5867 case EXCP_DATA_ABORT:
abf1172f
PM
5868 /* TODO: if we implemented the MPU registers, this is where we
5869 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5870 */
983fe826 5871 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
9ee6e8bb
PB
5872 return;
5873 case EXCP_BKPT:
cfe67cef 5874 if (semihosting_enabled()) {
2ad207d4 5875 int nr;
f9fd40eb 5876 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
5877 if (nr == 0xab) {
5878 env->regs[15] += 2;
205ace55
CC
5879 qemu_log_mask(CPU_LOG_INT,
5880 "...handling as semihosting call 0x%x\n",
5881 env->regs[0]);
2ad207d4
PB
5882 env->regs[0] = do_arm_semihosting(env);
5883 return;
5884 }
5885 }
983fe826 5886 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
9ee6e8bb
PB
5887 return;
5888 case EXCP_IRQ:
983fe826 5889 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
9ee6e8bb
PB
5890 break;
5891 case EXCP_EXCEPTION_EXIT:
5892 do_v7m_exception_exit(env);
5893 return;
5894 default:
a47dddd7 5895 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
5896 return; /* Never happens. Keep compiler happy. */
5897 }
5898
5899 /* Align stack pointer. */
5900 /* ??? Should only do this if Configuration Control Register
5901 STACKALIGN bit is set. */
5902 if (env->regs[13] & 4) {
ab19b0ec 5903 env->regs[13] -= 4;
9ee6e8bb
PB
5904 xpsr |= 0x200;
5905 }
6c95676b 5906 /* Switch to the handler mode. */
9ee6e8bb
PB
5907 v7m_push(env, xpsr);
5908 v7m_push(env, env->regs[15]);
5909 v7m_push(env, env->regs[14]);
5910 v7m_push(env, env->regs[12]);
5911 v7m_push(env, env->regs[3]);
5912 v7m_push(env, env->regs[2]);
5913 v7m_push(env, env->regs[1]);
5914 v7m_push(env, env->regs[0]);
5915 switch_v7m_sp(env, 0);
c98d174c
PM
5916 /* Clear IT bits */
5917 env->condexec_bits = 0;
9ee6e8bb 5918 env->regs[14] = lr;
fdfba1a2 5919 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
9ee6e8bb
PB
5920 env->regs[15] = addr & 0xfffffffe;
5921 env->thumb = addr & 1;
5922}
5923
ce02049d
GB
5924/* Function used to synchronize QEMU's AArch64 register set with AArch32
5925 * register set. This is necessary when switching between AArch32 and AArch64
5926 * execution state.
5927 */
5928void aarch64_sync_32_to_64(CPUARMState *env)
5929{
5930 int i;
5931 uint32_t mode = env->uncached_cpsr & CPSR_M;
5932
5933 /* We can blanket copy R[0:7] to X[0:7] */
5934 for (i = 0; i < 8; i++) {
5935 env->xregs[i] = env->regs[i];
5936 }
5937
5938 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5939 * Otherwise, they come from the banked user regs.
5940 */
5941 if (mode == ARM_CPU_MODE_FIQ) {
5942 for (i = 8; i < 13; i++) {
5943 env->xregs[i] = env->usr_regs[i - 8];
5944 }
5945 } else {
5946 for (i = 8; i < 13; i++) {
5947 env->xregs[i] = env->regs[i];
5948 }
5949 }
5950
5951 /* Registers x13-x23 are the various mode SP and FP registers. Registers
5952 * r13 and r14 are only copied if we are in that mode, otherwise we copy
5953 * from the mode banked register.
5954 */
5955 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5956 env->xregs[13] = env->regs[13];
5957 env->xregs[14] = env->regs[14];
5958 } else {
5959 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5960 /* HYP is an exception in that it is copied from r14 */
5961 if (mode == ARM_CPU_MODE_HYP) {
5962 env->xregs[14] = env->regs[14];
5963 } else {
5964 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5965 }
5966 }
5967
5968 if (mode == ARM_CPU_MODE_HYP) {
5969 env->xregs[15] = env->regs[13];
5970 } else {
5971 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5972 }
5973
5974 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
5975 env->xregs[16] = env->regs[14];
5976 env->xregs[17] = env->regs[13];
ce02049d 5977 } else {
3a9148d0
SS
5978 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5979 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
5980 }
5981
5982 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
5983 env->xregs[18] = env->regs[14];
5984 env->xregs[19] = env->regs[13];
ce02049d 5985 } else {
3a9148d0
SS
5986 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5987 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
5988 }
5989
5990 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
5991 env->xregs[20] = env->regs[14];
5992 env->xregs[21] = env->regs[13];
ce02049d 5993 } else {
3a9148d0
SS
5994 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5995 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
5996 }
5997
5998 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
5999 env->xregs[22] = env->regs[14];
6000 env->xregs[23] = env->regs[13];
ce02049d 6001 } else {
3a9148d0
SS
6002 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
6003 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
6004 }
6005
6006 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
6007 * mode, then we can copy from r8-r14. Otherwise, we copy from the
6008 * FIQ bank for r8-r14.
6009 */
6010 if (mode == ARM_CPU_MODE_FIQ) {
6011 for (i = 24; i < 31; i++) {
6012 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
6013 }
6014 } else {
6015 for (i = 24; i < 29; i++) {
6016 env->xregs[i] = env->fiq_regs[i - 24];
6017 }
6018 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
6019 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
6020 }
6021
6022 env->pc = env->regs[15];
6023}
6024
6025/* Function used to synchronize QEMU's AArch32 register set with AArch64
6026 * register set. This is necessary when switching between AArch32 and AArch64
6027 * execution state.
6028 */
6029void aarch64_sync_64_to_32(CPUARMState *env)
6030{
6031 int i;
6032 uint32_t mode = env->uncached_cpsr & CPSR_M;
6033
6034 /* We can blanket copy X[0:7] to R[0:7] */
6035 for (i = 0; i < 8; i++) {
6036 env->regs[i] = env->xregs[i];
6037 }
6038
6039 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
6040 * Otherwise, we copy x8-x12 into the banked user regs.
6041 */
6042 if (mode == ARM_CPU_MODE_FIQ) {
6043 for (i = 8; i < 13; i++) {
6044 env->usr_regs[i - 8] = env->xregs[i];
6045 }
6046 } else {
6047 for (i = 8; i < 13; i++) {
6048 env->regs[i] = env->xregs[i];
6049 }
6050 }
6051
6052 /* Registers r13 & r14 depend on the current mode.
6053 * If we are in a given mode, we copy the corresponding x registers to r13
6054 * and r14. Otherwise, we copy the x register to the banked r13 and r14
6055 * for the mode.
6056 */
6057 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
6058 env->regs[13] = env->xregs[13];
6059 env->regs[14] = env->xregs[14];
6060 } else {
6061 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
6062
6063 /* HYP is an exception in that it does not have its own banked r14 but
6064 * shares the USR r14
6065 */
6066 if (mode == ARM_CPU_MODE_HYP) {
6067 env->regs[14] = env->xregs[14];
6068 } else {
6069 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
6070 }
6071 }
6072
6073 if (mode == ARM_CPU_MODE_HYP) {
6074 env->regs[13] = env->xregs[15];
6075 } else {
6076 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
6077 }
6078
6079 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
6080 env->regs[14] = env->xregs[16];
6081 env->regs[13] = env->xregs[17];
ce02049d 6082 } else {
3a9148d0
SS
6083 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
6084 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
6085 }
6086
6087 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
6088 env->regs[14] = env->xregs[18];
6089 env->regs[13] = env->xregs[19];
ce02049d 6090 } else {
3a9148d0
SS
6091 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
6092 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
6093 }
6094
6095 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
6096 env->regs[14] = env->xregs[20];
6097 env->regs[13] = env->xregs[21];
ce02049d 6098 } else {
3a9148d0
SS
6099 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
6100 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
6101 }
6102
6103 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
6104 env->regs[14] = env->xregs[22];
6105 env->regs[13] = env->xregs[23];
ce02049d 6106 } else {
3a9148d0
SS
6107 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
6108 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
6109 }
6110
6111 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
6112 * mode, then we can copy to r8-r14. Otherwise, we copy to the
6113 * FIQ bank for r8-r14.
6114 */
6115 if (mode == ARM_CPU_MODE_FIQ) {
6116 for (i = 24; i < 31; i++) {
6117 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
6118 }
6119 } else {
6120 for (i = 24; i < 29; i++) {
6121 env->fiq_regs[i - 24] = env->xregs[i];
6122 }
6123 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
6124 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
6125 }
6126
6127 env->regs[15] = env->pc;
6128}
6129
966f758c 6130static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 6131{
97a8ea5a
AF
6132 ARMCPU *cpu = ARM_CPU(cs);
6133 CPUARMState *env = &cpu->env;
b5ff1b31
FB
6134 uint32_t addr;
6135 uint32_t mask;
6136 int new_mode;
6137 uint32_t offset;
16a906fd 6138 uint32_t moe;
b5ff1b31 6139
16a906fd
PM
6140 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
6141 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
6142 case EC_BREAKPOINT:
6143 case EC_BREAKPOINT_SAME_EL:
6144 moe = 1;
6145 break;
6146 case EC_WATCHPOINT:
6147 case EC_WATCHPOINT_SAME_EL:
6148 moe = 10;
6149 break;
6150 case EC_AA32_BKPT:
6151 moe = 3;
6152 break;
6153 case EC_VECTORCATCH:
6154 moe = 5;
6155 break;
6156 default:
6157 moe = 0;
6158 break;
6159 }
6160
6161 if (moe) {
6162 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
6163 }
6164
b5ff1b31 6165 /* TODO: Vectored interrupt controller. */
27103424 6166 switch (cs->exception_index) {
b5ff1b31
FB
6167 case EXCP_UDEF:
6168 new_mode = ARM_CPU_MODE_UND;
6169 addr = 0x04;
6170 mask = CPSR_I;
6171 if (env->thumb)
6172 offset = 2;
6173 else
6174 offset = 4;
6175 break;
6176 case EXCP_SWI:
6177 new_mode = ARM_CPU_MODE_SVC;
6178 addr = 0x08;
6179 mask = CPSR_I;
601d70b9 6180 /* The PC already points to the next instruction. */
b5ff1b31
FB
6181 offset = 0;
6182 break;
06c949e6 6183 case EXCP_BKPT:
abf1172f 6184 env->exception.fsr = 2;
9ee6e8bb
PB
6185 /* Fall through to prefetch abort. */
6186 case EXCP_PREFETCH_ABORT:
88ca1c2d 6187 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 6188 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 6189 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 6190 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
6191 new_mode = ARM_CPU_MODE_ABT;
6192 addr = 0x0c;
6193 mask = CPSR_A | CPSR_I;
6194 offset = 4;
6195 break;
6196 case EXCP_DATA_ABORT:
4a7e2d73 6197 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 6198 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 6199 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 6200 env->exception.fsr,
6cd8a264 6201 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
6202 new_mode = ARM_CPU_MODE_ABT;
6203 addr = 0x10;
6204 mask = CPSR_A | CPSR_I;
6205 offset = 8;
6206 break;
6207 case EXCP_IRQ:
6208 new_mode = ARM_CPU_MODE_IRQ;
6209 addr = 0x18;
6210 /* Disable IRQ and imprecise data aborts. */
6211 mask = CPSR_A | CPSR_I;
6212 offset = 4;
de38d23b
FA
6213 if (env->cp15.scr_el3 & SCR_IRQ) {
6214 /* IRQ routed to monitor mode */
6215 new_mode = ARM_CPU_MODE_MON;
6216 mask |= CPSR_F;
6217 }
b5ff1b31
FB
6218 break;
6219 case EXCP_FIQ:
6220 new_mode = ARM_CPU_MODE_FIQ;
6221 addr = 0x1c;
6222 /* Disable FIQ, IRQ and imprecise data aborts. */
6223 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
6224 if (env->cp15.scr_el3 & SCR_FIQ) {
6225 /* FIQ routed to monitor mode */
6226 new_mode = ARM_CPU_MODE_MON;
6227 }
b5ff1b31
FB
6228 offset = 4;
6229 break;
dbe9d163
FA
6230 case EXCP_SMC:
6231 new_mode = ARM_CPU_MODE_MON;
6232 addr = 0x08;
6233 mask = CPSR_A | CPSR_I | CPSR_F;
6234 offset = 0;
6235 break;
b5ff1b31 6236 default:
a47dddd7 6237 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
6238 return; /* Never happens. Keep compiler happy. */
6239 }
e89e51a1
FA
6240
6241 if (new_mode == ARM_CPU_MODE_MON) {
6242 addr += env->cp15.mvbar;
137feaa9 6243 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 6244 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 6245 addr += 0xffff0000;
8641136c
NR
6246 } else {
6247 /* ARM v7 architectures provide a vector base address register to remap
6248 * the interrupt vector table.
e89e51a1 6249 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
6250 * Note: only bits 31:5 are valid.
6251 */
fb6c91ba 6252 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 6253 }
dbe9d163
FA
6254
6255 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
6256 env->cp15.scr_el3 &= ~SCR_NS;
6257 }
6258
b5ff1b31 6259 switch_mode (env, new_mode);
662cefb7
PM
6260 /* For exceptions taken to AArch32 we must clear the SS bit in both
6261 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
6262 */
6263 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 6264 env->spsr = cpsr_read(env);
9ee6e8bb
PB
6265 /* Clear IT bits. */
6266 env->condexec_bits = 0;
30a8cac1 6267 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 6268 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
6269 /* Set new mode endianness */
6270 env->uncached_cpsr &= ~CPSR_E;
6271 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
6272 env->uncached_cpsr |= ~CPSR_E;
6273 }
4cc35614 6274 env->daif |= mask;
be5e7a76
DES
6275 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
6276 * and we should just guard the thumb mode on V4 */
6277 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 6278 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 6279 }
b5ff1b31
FB
6280 env->regs[14] = env->regs[15] + offset;
6281 env->regs[15] = addr;
b5ff1b31
FB
6282}
6283
966f758c
PM
6284/* Handle exception entry to a target EL which is using AArch64 */
6285static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
6286{
6287 ARMCPU *cpu = ARM_CPU(cs);
6288 CPUARMState *env = &cpu->env;
6289 unsigned int new_el = env->exception.target_el;
6290 target_ulong addr = env->cp15.vbar_el[new_el];
6291 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
6292
6293 if (arm_current_el(env) < new_el) {
3d6f7617
PM
6294 /* Entry vector offset depends on whether the implemented EL
6295 * immediately lower than the target level is using AArch32 or AArch64
6296 */
6297 bool is_aa64;
6298
6299 switch (new_el) {
6300 case 3:
6301 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
6302 break;
6303 case 2:
6304 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
6305 break;
6306 case 1:
6307 is_aa64 = is_a64(env);
6308 break;
6309 default:
6310 g_assert_not_reached();
6311 }
6312
6313 if (is_aa64) {
f3a9b694
PM
6314 addr += 0x400;
6315 } else {
6316 addr += 0x600;
6317 }
6318 } else if (pstate_read(env) & PSTATE_SP) {
6319 addr += 0x200;
6320 }
6321
f3a9b694
PM
6322 switch (cs->exception_index) {
6323 case EXCP_PREFETCH_ABORT:
6324 case EXCP_DATA_ABORT:
6325 env->cp15.far_el[new_el] = env->exception.vaddress;
6326 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
6327 env->cp15.far_el[new_el]);
6328 /* fall through */
6329 case EXCP_BKPT:
6330 case EXCP_UDEF:
6331 case EXCP_SWI:
6332 case EXCP_HVC:
6333 case EXCP_HYP_TRAP:
6334 case EXCP_SMC:
6335 env->cp15.esr_el[new_el] = env->exception.syndrome;
6336 break;
6337 case EXCP_IRQ:
6338 case EXCP_VIRQ:
6339 addr += 0x80;
6340 break;
6341 case EXCP_FIQ:
6342 case EXCP_VFIQ:
6343 addr += 0x100;
6344 break;
6345 case EXCP_SEMIHOST:
6346 qemu_log_mask(CPU_LOG_INT,
6347 "...handling as semihosting call 0x%" PRIx64 "\n",
6348 env->xregs[0]);
6349 env->xregs[0] = do_arm_semihosting(env);
6350 return;
6351 default:
6352 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
6353 }
6354
6355 if (is_a64(env)) {
6356 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
6357 aarch64_save_sp(env, arm_current_el(env));
6358 env->elr_el[new_el] = env->pc;
6359 } else {
6360 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
6361 if (!env->thumb) {
6362 env->cp15.esr_el[new_el] |= 1 << 25;
6363 }
6364 env->elr_el[new_el] = env->regs[15];
6365
6366 aarch64_sync_32_to_64(env);
6367
6368 env->condexec_bits = 0;
6369 }
6370 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
6371 env->elr_el[new_el]);
6372
6373 pstate_write(env, PSTATE_DAIF | new_mode);
6374 env->aarch64 = 1;
6375 aarch64_restore_sp(env, new_el);
6376
6377 env->pc = addr;
6378
6379 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
6380 new_el, env->pc, pstate_read(env));
966f758c
PM
6381}
6382
904c04de
PM
6383static inline bool check_for_semihosting(CPUState *cs)
6384{
6385 /* Check whether this exception is a semihosting call; if so
6386 * then handle it and return true; otherwise return false.
6387 */
6388 ARMCPU *cpu = ARM_CPU(cs);
6389 CPUARMState *env = &cpu->env;
6390
6391 if (is_a64(env)) {
6392 if (cs->exception_index == EXCP_SEMIHOST) {
6393 /* This is always the 64-bit semihosting exception.
6394 * The "is this usermode" and "is semihosting enabled"
6395 * checks have been done at translate time.
6396 */
6397 qemu_log_mask(CPU_LOG_INT,
6398 "...handling as semihosting call 0x%" PRIx64 "\n",
6399 env->xregs[0]);
6400 env->xregs[0] = do_arm_semihosting(env);
6401 return true;
6402 }
6403 return false;
6404 } else {
6405 uint32_t imm;
6406
6407 /* Only intercept calls from privileged modes, to provide some
6408 * semblance of security.
6409 */
6410 if (!semihosting_enabled() ||
6411 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)) {
6412 return false;
6413 }
6414
6415 switch (cs->exception_index) {
6416 case EXCP_SWI:
6417 /* Check for semihosting interrupt. */
6418 if (env->thumb) {
f9fd40eb 6419 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
6420 & 0xff;
6421 if (imm == 0xab) {
6422 break;
6423 }
6424 } else {
f9fd40eb 6425 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
6426 & 0xffffff;
6427 if (imm == 0x123456) {
6428 break;
6429 }
6430 }
6431 return false;
6432 case EXCP_BKPT:
6433 /* See if this is a semihosting syscall. */
6434 if (env->thumb) {
f9fd40eb 6435 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
6436 & 0xff;
6437 if (imm == 0xab) {
6438 env->regs[15] += 2;
6439 break;
6440 }
6441 }
6442 return false;
6443 default:
6444 return false;
6445 }
6446
6447 qemu_log_mask(CPU_LOG_INT,
6448 "...handling as semihosting call 0x%x\n",
6449 env->regs[0]);
6450 env->regs[0] = do_arm_semihosting(env);
6451 return true;
6452 }
6453}
6454
966f758c
PM
6455/* Handle a CPU exception for A and R profile CPUs.
6456 * Do any appropriate logging, handle PSCI calls, and then hand off
6457 * to the AArch64-entry or AArch32-entry function depending on the
6458 * target exception level's register width.
6459 */
6460void arm_cpu_do_interrupt(CPUState *cs)
6461{
6462 ARMCPU *cpu = ARM_CPU(cs);
6463 CPUARMState *env = &cpu->env;
6464 unsigned int new_el = env->exception.target_el;
6465
6466 assert(!IS_M(env));
6467
6468 arm_log_exception(cs->exception_index);
6469 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
6470 new_el);
6471 if (qemu_loglevel_mask(CPU_LOG_INT)
6472 && !excp_is_internal(cs->exception_index)) {
6473 qemu_log_mask(CPU_LOG_INT, "...with ESR %x/0x%" PRIx32 "\n",
6474 env->exception.syndrome >> ARM_EL_EC_SHIFT,
6475 env->exception.syndrome);
6476 }
6477
6478 if (arm_is_psci_call(cpu, cs->exception_index)) {
6479 arm_handle_psci_call(cpu);
6480 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
6481 return;
6482 }
6483
904c04de
PM
6484 /* Semihosting semantics depend on the register width of the
6485 * code that caused the exception, not the target exception level,
6486 * so must be handled here.
966f758c 6487 */
904c04de
PM
6488 if (check_for_semihosting(cs)) {
6489 return;
6490 }
6491
6492 assert(!excp_is_internal(cs->exception_index));
6493 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
6494 arm_cpu_do_interrupt_aarch64(cs);
6495 } else {
6496 arm_cpu_do_interrupt_aarch32(cs);
6497 }
f3a9b694
PM
6498
6499 if (!kvm_enabled()) {
6500 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
6501 }
6502}
0480f69a
PM
6503
6504/* Return the exception level which controls this address translation regime */
6505static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
6506{
6507 switch (mmu_idx) {
6508 case ARMMMUIdx_S2NS:
6509 case ARMMMUIdx_S1E2:
6510 return 2;
6511 case ARMMMUIdx_S1E3:
6512 return 3;
6513 case ARMMMUIdx_S1SE0:
6514 return arm_el_is_aa64(env, 3) ? 1 : 3;
6515 case ARMMMUIdx_S1SE1:
6516 case ARMMMUIdx_S1NSE0:
6517 case ARMMMUIdx_S1NSE1:
6518 return 1;
6519 default:
6520 g_assert_not_reached();
6521 }
6522}
6523
8bf5b6a9
PM
6524/* Return true if this address translation regime is secure */
6525static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
6526{
6527 switch (mmu_idx) {
6528 case ARMMMUIdx_S12NSE0:
6529 case ARMMMUIdx_S12NSE1:
6530 case ARMMMUIdx_S1NSE0:
6531 case ARMMMUIdx_S1NSE1:
6532 case ARMMMUIdx_S1E2:
6533 case ARMMMUIdx_S2NS:
6534 return false;
6535 case ARMMMUIdx_S1E3:
6536 case ARMMMUIdx_S1SE0:
6537 case ARMMMUIdx_S1SE1:
6538 return true;
6539 default:
6540 g_assert_not_reached();
6541 }
6542}
6543
0480f69a
PM
6544/* Return the SCTLR value which controls this address translation regime */
6545static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
6546{
6547 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
6548}
6549
6550/* Return true if the specified stage of address translation is disabled */
6551static inline bool regime_translation_disabled(CPUARMState *env,
6552 ARMMMUIdx mmu_idx)
6553{
6554 if (mmu_idx == ARMMMUIdx_S2NS) {
6555 return (env->cp15.hcr_el2 & HCR_VM) == 0;
6556 }
6557 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
6558}
6559
73462ddd
PC
6560static inline bool regime_translation_big_endian(CPUARMState *env,
6561 ARMMMUIdx mmu_idx)
6562{
6563 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
6564}
6565
0480f69a
PM
6566/* Return the TCR controlling this translation regime */
6567static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
6568{
6569 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 6570 return &env->cp15.vtcr_el2;
0480f69a
PM
6571 }
6572 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
6573}
6574
aef878be
GB
6575/* Return the TTBR associated with this translation regime */
6576static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
6577 int ttbrn)
6578{
6579 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 6580 return env->cp15.vttbr_el2;
aef878be
GB
6581 }
6582 if (ttbrn == 0) {
6583 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
6584 } else {
6585 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
6586 }
6587}
6588
0480f69a
PM
6589/* Return true if the translation regime is using LPAE format page tables */
6590static inline bool regime_using_lpae_format(CPUARMState *env,
6591 ARMMMUIdx mmu_idx)
6592{
6593 int el = regime_el(env, mmu_idx);
6594 if (el == 2 || arm_el_is_aa64(env, el)) {
6595 return true;
6596 }
6597 if (arm_feature(env, ARM_FEATURE_LPAE)
6598 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
6599 return true;
6600 }
6601 return false;
6602}
6603
deb2db99
AR
6604/* Returns true if the stage 1 translation regime is using LPAE format page
6605 * tables. Used when raising alignment exceptions, whose FSR changes depending
6606 * on whether the long or short descriptor format is in use. */
6607bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 6608{
deb2db99
AR
6609 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6610 mmu_idx += ARMMMUIdx_S1NSE0;
6611 }
6612
30901475
AB
6613 return regime_using_lpae_format(env, mmu_idx);
6614}
6615
0480f69a
PM
6616static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
6617{
6618 switch (mmu_idx) {
6619 case ARMMMUIdx_S1SE0:
6620 case ARMMMUIdx_S1NSE0:
6621 return true;
6622 default:
6623 return false;
6624 case ARMMMUIdx_S12NSE0:
6625 case ARMMMUIdx_S12NSE1:
6626 g_assert_not_reached();
6627 }
6628}
6629
0fbf5238
AJ
6630/* Translate section/page access permissions to page
6631 * R/W protection flags
d76951b6
AJ
6632 *
6633 * @env: CPUARMState
6634 * @mmu_idx: MMU index indicating required translation regime
6635 * @ap: The 3-bit access permissions (AP[2:0])
6636 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
6637 */
6638static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
6639 int ap, int domain_prot)
6640{
554b0b09
PM
6641 bool is_user = regime_is_user(env, mmu_idx);
6642
6643 if (domain_prot == 3) {
6644 return PAGE_READ | PAGE_WRITE;
6645 }
6646
554b0b09
PM
6647 switch (ap) {
6648 case 0:
6649 if (arm_feature(env, ARM_FEATURE_V7)) {
6650 return 0;
6651 }
554b0b09
PM
6652 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
6653 case SCTLR_S:
6654 return is_user ? 0 : PAGE_READ;
6655 case SCTLR_R:
6656 return PAGE_READ;
6657 default:
6658 return 0;
6659 }
6660 case 1:
6661 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6662 case 2:
87c3d486 6663 if (is_user) {
0fbf5238 6664 return PAGE_READ;
87c3d486 6665 } else {
554b0b09 6666 return PAGE_READ | PAGE_WRITE;
87c3d486 6667 }
554b0b09
PM
6668 case 3:
6669 return PAGE_READ | PAGE_WRITE;
6670 case 4: /* Reserved. */
6671 return 0;
6672 case 5:
0fbf5238 6673 return is_user ? 0 : PAGE_READ;
554b0b09 6674 case 6:
0fbf5238 6675 return PAGE_READ;
554b0b09 6676 case 7:
87c3d486 6677 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 6678 return 0;
87c3d486 6679 }
0fbf5238 6680 return PAGE_READ;
554b0b09 6681 default:
0fbf5238 6682 g_assert_not_reached();
554b0b09 6683 }
b5ff1b31
FB
6684}
6685
d76951b6
AJ
6686/* Translate section/page access permissions to page
6687 * R/W protection flags.
6688 *
d76951b6 6689 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 6690 * @is_user: TRUE if accessing from PL0
d76951b6 6691 */
d8e052b3 6692static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 6693{
d76951b6
AJ
6694 switch (ap) {
6695 case 0:
6696 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6697 case 1:
6698 return PAGE_READ | PAGE_WRITE;
6699 case 2:
6700 return is_user ? 0 : PAGE_READ;
6701 case 3:
6702 return PAGE_READ;
6703 default:
6704 g_assert_not_reached();
6705 }
6706}
6707
d8e052b3
AJ
6708static inline int
6709simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
6710{
6711 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
6712}
6713
6ab1a5ee
EI
6714/* Translate S2 section/page access permissions to protection flags
6715 *
6716 * @env: CPUARMState
6717 * @s2ap: The 2-bit stage2 access permissions (S2AP)
6718 * @xn: XN (execute-never) bit
6719 */
6720static int get_S2prot(CPUARMState *env, int s2ap, int xn)
6721{
6722 int prot = 0;
6723
6724 if (s2ap & 1) {
6725 prot |= PAGE_READ;
6726 }
6727 if (s2ap & 2) {
6728 prot |= PAGE_WRITE;
6729 }
6730 if (!xn) {
dfda6837
SS
6731 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
6732 prot |= PAGE_EXEC;
6733 }
6ab1a5ee
EI
6734 }
6735 return prot;
6736}
6737
d8e052b3
AJ
6738/* Translate section/page access permissions to protection flags
6739 *
6740 * @env: CPUARMState
6741 * @mmu_idx: MMU index indicating required translation regime
6742 * @is_aa64: TRUE if AArch64
6743 * @ap: The 2-bit simple AP (AP[2:1])
6744 * @ns: NS (non-secure) bit
6745 * @xn: XN (execute-never) bit
6746 * @pxn: PXN (privileged execute-never) bit
6747 */
6748static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
6749 int ap, int ns, int xn, int pxn)
6750{
6751 bool is_user = regime_is_user(env, mmu_idx);
6752 int prot_rw, user_rw;
6753 bool have_wxn;
6754 int wxn = 0;
6755
6756 assert(mmu_idx != ARMMMUIdx_S2NS);
6757
6758 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
6759 if (is_user) {
6760 prot_rw = user_rw;
6761 } else {
6762 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
6763 }
6764
6765 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
6766 return prot_rw;
6767 }
6768
6769 /* TODO have_wxn should be replaced with
6770 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
6771 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
6772 * compatible processors have EL2, which is required for [U]WXN.
6773 */
6774 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
6775
6776 if (have_wxn) {
6777 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
6778 }
6779
6780 if (is_aa64) {
6781 switch (regime_el(env, mmu_idx)) {
6782 case 1:
6783 if (!is_user) {
6784 xn = pxn || (user_rw & PAGE_WRITE);
6785 }
6786 break;
6787 case 2:
6788 case 3:
6789 break;
6790 }
6791 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6792 switch (regime_el(env, mmu_idx)) {
6793 case 1:
6794 case 3:
6795 if (is_user) {
6796 xn = xn || !(user_rw & PAGE_READ);
6797 } else {
6798 int uwxn = 0;
6799 if (have_wxn) {
6800 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6801 }
6802 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6803 (uwxn && (user_rw & PAGE_WRITE));
6804 }
6805 break;
6806 case 2:
6807 break;
6808 }
6809 } else {
6810 xn = wxn = 0;
6811 }
6812
6813 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6814 return prot_rw;
6815 }
6816 return prot_rw | PAGE_EXEC;
6817}
6818
0480f69a
PM
6819static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6820 uint32_t *table, uint32_t address)
b2fa1797 6821{
0480f69a 6822 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 6823 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 6824
11f136ee
FA
6825 if (address & tcr->mask) {
6826 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
6827 /* Translation table walk disabled for TTBR1 */
6828 return false;
6829 }
aef878be 6830 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 6831 } else {
11f136ee 6832 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
6833 /* Translation table walk disabled for TTBR0 */
6834 return false;
6835 }
aef878be 6836 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
6837 }
6838 *table |= (address >> 18) & 0x3ffc;
6839 return true;
b2fa1797
PB
6840}
6841
37785977
EI
6842/* Translate a S1 pagetable walk through S2 if needed. */
6843static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
6844 hwaddr addr, MemTxAttrs txattrs,
6845 uint32_t *fsr,
6846 ARMMMUFaultInfo *fi)
6847{
6848 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
6849 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
6850 target_ulong s2size;
6851 hwaddr s2pa;
6852 int s2prot;
6853 int ret;
6854
6855 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
6856 &txattrs, &s2prot, &s2size, fsr, fi);
6857 if (ret) {
6858 fi->s2addr = addr;
6859 fi->stage2 = true;
6860 fi->s1ptw = true;
6861 return ~0;
6862 }
6863 addr = s2pa;
6864 }
6865 return addr;
6866}
6867
ebca90e4
PM
6868/* All loads done in the course of a page table walk go through here.
6869 * TODO: rather than ignoring errors from physical memory reads (which
6870 * are external aborts in ARM terminology) we should propagate this
6871 * error out so that we can turn it into a Data Abort if this walk
6872 * was being done for a CPU load/store or an address translation instruction
6873 * (but not if it was for a debug access).
6874 */
a614e698
EI
6875static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6876 ARMMMUIdx mmu_idx, uint32_t *fsr,
6877 ARMMMUFaultInfo *fi)
ebca90e4 6878{
a614e698
EI
6879 ARMCPU *cpu = ARM_CPU(cs);
6880 CPUARMState *env = &cpu->env;
ebca90e4 6881 MemTxAttrs attrs = {};
5ce4ff65 6882 AddressSpace *as;
ebca90e4
PM
6883
6884 attrs.secure = is_secure;
5ce4ff65 6885 as = arm_addressspace(cs, attrs);
a614e698
EI
6886 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6887 if (fi->s1ptw) {
6888 return 0;
6889 }
73462ddd
PC
6890 if (regime_translation_big_endian(env, mmu_idx)) {
6891 return address_space_ldl_be(as, addr, attrs, NULL);
6892 } else {
6893 return address_space_ldl_le(as, addr, attrs, NULL);
6894 }
ebca90e4
PM
6895}
6896
37785977
EI
6897static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6898 ARMMMUIdx mmu_idx, uint32_t *fsr,
6899 ARMMMUFaultInfo *fi)
ebca90e4 6900{
37785977
EI
6901 ARMCPU *cpu = ARM_CPU(cs);
6902 CPUARMState *env = &cpu->env;
ebca90e4 6903 MemTxAttrs attrs = {};
5ce4ff65 6904 AddressSpace *as;
ebca90e4
PM
6905
6906 attrs.secure = is_secure;
5ce4ff65 6907 as = arm_addressspace(cs, attrs);
37785977
EI
6908 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6909 if (fi->s1ptw) {
6910 return 0;
6911 }
73462ddd
PC
6912 if (regime_translation_big_endian(env, mmu_idx)) {
6913 return address_space_ldq_be(as, addr, attrs, NULL);
6914 } else {
6915 return address_space_ldq_le(as, addr, attrs, NULL);
6916 }
ebca90e4
PM
6917}
6918
b7cc4e82
PC
6919static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6920 int access_type, ARMMMUIdx mmu_idx,
6921 hwaddr *phys_ptr, int *prot,
e14b5a23
EI
6922 target_ulong *page_size, uint32_t *fsr,
6923 ARMMMUFaultInfo *fi)
b5ff1b31 6924{
70d74660 6925 CPUState *cs = CPU(arm_env_get_cpu(env));
b5ff1b31
FB
6926 int code;
6927 uint32_t table;
6928 uint32_t desc;
6929 int type;
6930 int ap;
e389be16 6931 int domain = 0;
dd4ebc2e 6932 int domain_prot;
a8170e5e 6933 hwaddr phys_addr;
0480f69a 6934 uint32_t dacr;
b5ff1b31 6935
9ee6e8bb
PB
6936 /* Pagetable walk. */
6937 /* Lookup l1 descriptor. */
0480f69a 6938 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
6939 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6940 code = 5;
6941 goto do_fault;
6942 }
a614e698
EI
6943 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6944 mmu_idx, fsr, fi);
9ee6e8bb 6945 type = (desc & 3);
dd4ebc2e 6946 domain = (desc >> 5) & 0x0f;
0480f69a
PM
6947 if (regime_el(env, mmu_idx) == 1) {
6948 dacr = env->cp15.dacr_ns;
6949 } else {
6950 dacr = env->cp15.dacr_s;
6951 }
6952 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 6953 if (type == 0) {
601d70b9 6954 /* Section translation fault. */
9ee6e8bb
PB
6955 code = 5;
6956 goto do_fault;
6957 }
dd4ebc2e 6958 if (domain_prot == 0 || domain_prot == 2) {
9ee6e8bb
PB
6959 if (type == 2)
6960 code = 9; /* Section domain fault. */
6961 else
6962 code = 11; /* Page domain fault. */
6963 goto do_fault;
6964 }
6965 if (type == 2) {
6966 /* 1Mb section. */
6967 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6968 ap = (desc >> 10) & 3;
6969 code = 13;
d4c430a8 6970 *page_size = 1024 * 1024;
9ee6e8bb
PB
6971 } else {
6972 /* Lookup l2 entry. */
554b0b09
PM
6973 if (type == 1) {
6974 /* Coarse pagetable. */
6975 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6976 } else {
6977 /* Fine pagetable. */
6978 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6979 }
a614e698
EI
6980 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6981 mmu_idx, fsr, fi);
9ee6e8bb
PB
6982 switch (desc & 3) {
6983 case 0: /* Page translation fault. */
6984 code = 7;
6985 goto do_fault;
6986 case 1: /* 64k page. */
6987 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6988 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 6989 *page_size = 0x10000;
ce819861 6990 break;
9ee6e8bb
PB
6991 case 2: /* 4k page. */
6992 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 6993 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 6994 *page_size = 0x1000;
ce819861 6995 break;
fc1891c7 6996 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 6997 if (type == 1) {
fc1891c7
PM
6998 /* ARMv6/XScale extended small page format */
6999 if (arm_feature(env, ARM_FEATURE_XSCALE)
7000 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 7001 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 7002 *page_size = 0x1000;
554b0b09 7003 } else {
fc1891c7
PM
7004 /* UNPREDICTABLE in ARMv5; we choose to take a
7005 * page translation fault.
7006 */
554b0b09
PM
7007 code = 7;
7008 goto do_fault;
7009 }
7010 } else {
7011 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 7012 *page_size = 0x400;
554b0b09 7013 }
9ee6e8bb 7014 ap = (desc >> 4) & 3;
ce819861
PB
7015 break;
7016 default:
9ee6e8bb
PB
7017 /* Never happens, but compiler isn't smart enough to tell. */
7018 abort();
ce819861 7019 }
9ee6e8bb
PB
7020 code = 15;
7021 }
0fbf5238
AJ
7022 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
7023 *prot |= *prot ? PAGE_EXEC : 0;
7024 if (!(*prot & (1 << access_type))) {
9ee6e8bb
PB
7025 /* Access permission fault. */
7026 goto do_fault;
7027 }
7028 *phys_ptr = phys_addr;
b7cc4e82 7029 return false;
9ee6e8bb 7030do_fault:
b7cc4e82
PC
7031 *fsr = code | (domain << 4);
7032 return true;
9ee6e8bb
PB
7033}
7034
b7cc4e82
PC
7035static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
7036 int access_type, ARMMMUIdx mmu_idx,
7037 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
7038 target_ulong *page_size, uint32_t *fsr,
7039 ARMMMUFaultInfo *fi)
9ee6e8bb 7040{
70d74660 7041 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb
PB
7042 int code;
7043 uint32_t table;
7044 uint32_t desc;
7045 uint32_t xn;
de9b05b8 7046 uint32_t pxn = 0;
9ee6e8bb
PB
7047 int type;
7048 int ap;
de9b05b8 7049 int domain = 0;
dd4ebc2e 7050 int domain_prot;
a8170e5e 7051 hwaddr phys_addr;
0480f69a 7052 uint32_t dacr;
8bf5b6a9 7053 bool ns;
9ee6e8bb
PB
7054
7055 /* Pagetable walk. */
7056 /* Lookup l1 descriptor. */
0480f69a 7057 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
7058 /* Section translation fault if page walk is disabled by PD0 or PD1 */
7059 code = 5;
7060 goto do_fault;
7061 }
a614e698
EI
7062 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7063 mmu_idx, fsr, fi);
9ee6e8bb 7064 type = (desc & 3);
de9b05b8
PM
7065 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
7066 /* Section translation fault, or attempt to use the encoding
7067 * which is Reserved on implementations without PXN.
7068 */
9ee6e8bb 7069 code = 5;
9ee6e8bb 7070 goto do_fault;
de9b05b8
PM
7071 }
7072 if ((type == 1) || !(desc & (1 << 18))) {
7073 /* Page or Section. */
dd4ebc2e 7074 domain = (desc >> 5) & 0x0f;
9ee6e8bb 7075 }
0480f69a
PM
7076 if (regime_el(env, mmu_idx) == 1) {
7077 dacr = env->cp15.dacr_ns;
7078 } else {
7079 dacr = env->cp15.dacr_s;
7080 }
7081 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 7082 if (domain_prot == 0 || domain_prot == 2) {
de9b05b8 7083 if (type != 1) {
9ee6e8bb 7084 code = 9; /* Section domain fault. */
de9b05b8 7085 } else {
9ee6e8bb 7086 code = 11; /* Page domain fault. */
de9b05b8 7087 }
9ee6e8bb
PB
7088 goto do_fault;
7089 }
de9b05b8 7090 if (type != 1) {
9ee6e8bb
PB
7091 if (desc & (1 << 18)) {
7092 /* Supersection. */
7093 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
7094 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
7095 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 7096 *page_size = 0x1000000;
b5ff1b31 7097 } else {
9ee6e8bb
PB
7098 /* Section. */
7099 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 7100 *page_size = 0x100000;
b5ff1b31 7101 }
9ee6e8bb
PB
7102 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
7103 xn = desc & (1 << 4);
de9b05b8 7104 pxn = desc & 1;
9ee6e8bb 7105 code = 13;
8bf5b6a9 7106 ns = extract32(desc, 19, 1);
9ee6e8bb 7107 } else {
de9b05b8
PM
7108 if (arm_feature(env, ARM_FEATURE_PXN)) {
7109 pxn = (desc >> 2) & 1;
7110 }
8bf5b6a9 7111 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
7112 /* Lookup l2 entry. */
7113 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698
EI
7114 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7115 mmu_idx, fsr, fi);
9ee6e8bb
PB
7116 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
7117 switch (desc & 3) {
7118 case 0: /* Page translation fault. */
7119 code = 7;
b5ff1b31 7120 goto do_fault;
9ee6e8bb
PB
7121 case 1: /* 64k page. */
7122 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
7123 xn = desc & (1 << 15);
d4c430a8 7124 *page_size = 0x10000;
9ee6e8bb
PB
7125 break;
7126 case 2: case 3: /* 4k page. */
7127 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
7128 xn = desc & 1;
d4c430a8 7129 *page_size = 0x1000;
9ee6e8bb
PB
7130 break;
7131 default:
7132 /* Never happens, but compiler isn't smart enough to tell. */
7133 abort();
b5ff1b31 7134 }
9ee6e8bb
PB
7135 code = 15;
7136 }
dd4ebc2e 7137 if (domain_prot == 3) {
c0034328
JR
7138 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
7139 } else {
0480f69a 7140 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
7141 xn = 1;
7142 }
c0034328
JR
7143 if (xn && access_type == 2)
7144 goto do_fault;
9ee6e8bb 7145
d76951b6
AJ
7146 if (arm_feature(env, ARM_FEATURE_V6K) &&
7147 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
7148 /* The simplified model uses AP[0] as an access control bit. */
7149 if ((ap & 1) == 0) {
7150 /* Access flag fault. */
7151 code = (code == 15) ? 6 : 3;
7152 goto do_fault;
7153 }
7154 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
7155 } else {
7156 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 7157 }
0fbf5238
AJ
7158 if (*prot && !xn) {
7159 *prot |= PAGE_EXEC;
7160 }
7161 if (!(*prot & (1 << access_type))) {
c0034328
JR
7162 /* Access permission fault. */
7163 goto do_fault;
7164 }
3ad493fc 7165 }
8bf5b6a9
PM
7166 if (ns) {
7167 /* The NS bit will (as required by the architecture) have no effect if
7168 * the CPU doesn't support TZ or this is a non-secure translation
7169 * regime, because the attribute will already be non-secure.
7170 */
7171 attrs->secure = false;
7172 }
9ee6e8bb 7173 *phys_ptr = phys_addr;
b7cc4e82 7174 return false;
b5ff1b31 7175do_fault:
b7cc4e82
PC
7176 *fsr = code | (domain << 4);
7177 return true;
b5ff1b31
FB
7178}
7179
3dde962f
PM
7180/* Fault type for long-descriptor MMU fault reporting; this corresponds
7181 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
7182 */
7183typedef enum {
7184 translation_fault = 1,
7185 access_fault = 2,
7186 permission_fault = 3,
7187} MMUFaultType;
7188
1853d5a9 7189/*
a0e966c9 7190 * check_s2_mmu_setup
1853d5a9
EI
7191 * @cpu: ARMCPU
7192 * @is_aa64: True if the translation regime is in AArch64 state
7193 * @startlevel: Suggested starting level
7194 * @inputsize: Bitsize of IPAs
7195 * @stride: Page-table stride (See the ARM ARM)
7196 *
a0e966c9
EI
7197 * Returns true if the suggested S2 translation parameters are OK and
7198 * false otherwise.
1853d5a9 7199 */
a0e966c9
EI
7200static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
7201 int inputsize, int stride)
1853d5a9 7202{
98d68ec2
EI
7203 const int grainsize = stride + 3;
7204 int startsizecheck;
7205
1853d5a9
EI
7206 /* Negative levels are never allowed. */
7207 if (level < 0) {
7208 return false;
7209 }
7210
98d68ec2
EI
7211 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
7212 if (startsizecheck < 1 || startsizecheck > stride + 4) {
7213 return false;
7214 }
7215
1853d5a9 7216 if (is_aa64) {
3526423e 7217 CPUARMState *env = &cpu->env;
1853d5a9
EI
7218 unsigned int pamax = arm_pamax(cpu);
7219
7220 switch (stride) {
7221 case 13: /* 64KB Pages. */
7222 if (level == 0 || (level == 1 && pamax <= 42)) {
7223 return false;
7224 }
7225 break;
7226 case 11: /* 16KB Pages. */
7227 if (level == 0 || (level == 1 && pamax <= 40)) {
7228 return false;
7229 }
7230 break;
7231 case 9: /* 4KB Pages. */
7232 if (level == 0 && pamax <= 42) {
7233 return false;
7234 }
7235 break;
7236 default:
7237 g_assert_not_reached();
7238 }
3526423e
EI
7239
7240 /* Inputsize checks. */
7241 if (inputsize > pamax &&
7242 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
7243 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
7244 return false;
7245 }
1853d5a9 7246 } else {
1853d5a9
EI
7247 /* AArch32 only supports 4KB pages. Assert on that. */
7248 assert(stride == 9);
7249
7250 if (level == 0) {
7251 return false;
7252 }
1853d5a9
EI
7253 }
7254 return true;
7255}
7256
b7cc4e82
PC
7257static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
7258 int access_type, ARMMMUIdx mmu_idx,
7259 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
e14b5a23
EI
7260 target_ulong *page_size_ptr, uint32_t *fsr,
7261 ARMMMUFaultInfo *fi)
3dde962f 7262{
1853d5a9
EI
7263 ARMCPU *cpu = arm_env_get_cpu(env);
7264 CPUState *cs = CPU(cpu);
3dde962f
PM
7265 /* Read an LPAE long-descriptor translation table. */
7266 MMUFaultType fault_type = translation_fault;
1b4093ea 7267 uint32_t level;
0c5fbf3b 7268 uint32_t epd = 0;
1f4c8c18 7269 int32_t t0sz, t1sz;
2c8dd318 7270 uint32_t tg;
3dde962f
PM
7271 uint64_t ttbr;
7272 int ttbr_select;
dddb5223 7273 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f
PM
7274 uint32_t tableattrs;
7275 target_ulong page_size;
7276 uint32_t attrs;
973a5434 7277 int32_t stride = 9;
1b4093ea 7278 int32_t va_size;
4ca6a051 7279 int inputsize;
2c8dd318 7280 int32_t tbi = 0;
0480f69a 7281 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 7282 int ap, ns, xn, pxn;
88e8add8
GB
7283 uint32_t el = regime_el(env, mmu_idx);
7284 bool ttbr1_valid = true;
6109769a 7285 uint64_t descaddrmask;
0480f69a
PM
7286
7287 /* TODO:
88e8add8
GB
7288 * This code does not handle the different format TCR for VTCR_EL2.
7289 * This code also does not support shareability levels.
7290 * Attribute and permission bit handling should also be checked when adding
7291 * support for those page table walks.
0480f69a 7292 */
88e8add8 7293 if (arm_el_is_aa64(env, el)) {
1b4093ea 7294 level = 0;
2c8dd318 7295 va_size = 64;
88e8add8 7296 if (el > 1) {
1edee470
EI
7297 if (mmu_idx != ARMMMUIdx_S2NS) {
7298 tbi = extract64(tcr->raw_tcr, 20, 1);
7299 }
88e8add8
GB
7300 } else {
7301 if (extract64(address, 55, 1)) {
7302 tbi = extract64(tcr->raw_tcr, 38, 1);
7303 } else {
7304 tbi = extract64(tcr->raw_tcr, 37, 1);
7305 }
7306 }
2c8dd318 7307 tbi *= 8;
88e8add8
GB
7308
7309 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
7310 * invalid.
7311 */
7312 if (el > 1) {
7313 ttbr1_valid = false;
7314 }
d0a2cbce 7315 } else {
1b4093ea
SS
7316 level = 1;
7317 va_size = 32;
d0a2cbce
PM
7318 /* There is no TTBR1 for EL2 */
7319 if (el == 2) {
7320 ttbr1_valid = false;
7321 }
2c8dd318 7322 }
3dde962f
PM
7323
7324 /* Determine whether this address is in the region controlled by
7325 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
7326 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
7327 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
7328 */
0480f69a 7329 if (va_size == 64) {
4ee38098
EI
7330 /* AArch64 translation. */
7331 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
7332 t0sz = MIN(t0sz, 39);
7333 t0sz = MAX(t0sz, 16);
4ee38098
EI
7334 } else if (mmu_idx != ARMMMUIdx_S2NS) {
7335 /* AArch32 stage 1 translation. */
7336 t0sz = extract32(tcr->raw_tcr, 0, 3);
7337 } else {
7338 /* AArch32 stage 2 translation. */
7339 bool sext = extract32(tcr->raw_tcr, 4, 1);
7340 bool sign = extract32(tcr->raw_tcr, 3, 1);
7341 t0sz = sextract32(tcr->raw_tcr, 0, 4);
7342
7343 /* If the sign-extend bit is not the same as t0sz[3], the result
7344 * is unpredictable. Flag this as a guest error. */
7345 if (sign != sext) {
7346 qemu_log_mask(LOG_GUEST_ERROR,
7347 "AArch32: VTCR.S / VTCR.T0SZ[3] missmatch\n");
7348 }
2c8dd318 7349 }
1f4c8c18 7350 t1sz = extract32(tcr->raw_tcr, 16, 6);
0480f69a 7351 if (va_size == 64) {
2c8dd318
RH
7352 t1sz = MIN(t1sz, 39);
7353 t1sz = MAX(t1sz, 16);
7354 }
7355 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
3dde962f
PM
7356 /* there is a ttbr0 region and we are in it (high bits all zero) */
7357 ttbr_select = 0;
88e8add8
GB
7358 } else if (ttbr1_valid && t1sz &&
7359 !extract64(~address, va_size - t1sz, t1sz - tbi)) {
3dde962f
PM
7360 /* there is a ttbr1 region and we are in it (high bits all one) */
7361 ttbr_select = 1;
7362 } else if (!t0sz) {
7363 /* ttbr0 region is "everything not in the ttbr1 region" */
7364 ttbr_select = 0;
88e8add8 7365 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
7366 /* ttbr1 region is "everything not in the ttbr0 region" */
7367 ttbr_select = 1;
7368 } else {
7369 /* in the gap between the two regions, this is a Translation fault */
7370 fault_type = translation_fault;
7371 goto do_fault;
7372 }
7373
7374 /* Note that QEMU ignores shareability and cacheability attributes,
7375 * so we don't need to do anything with the SH, ORGN, IRGN fields
7376 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
7377 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
7378 * implement any ASID-like capability so we can ignore it (instead
7379 * we will always flush the TLB any time the ASID is changed).
7380 */
7381 if (ttbr_select == 0) {
aef878be 7382 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
7383 if (el < 2) {
7384 epd = extract32(tcr->raw_tcr, 7, 1);
7385 }
4ca6a051 7386 inputsize = va_size - t0sz;
2c8dd318 7387
11f136ee 7388 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 7389 if (tg == 1) { /* 64KB pages */
973a5434 7390 stride = 13;
2c8dd318
RH
7391 }
7392 if (tg == 2) { /* 16KB pages */
973a5434 7393 stride = 11;
2c8dd318 7394 }
3dde962f 7395 } else {
88e8add8
GB
7396 /* We should only be here if TTBR1 is valid */
7397 assert(ttbr1_valid);
7398
aef878be 7399 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 7400 epd = extract32(tcr->raw_tcr, 23, 1);
4ca6a051 7401 inputsize = va_size - t1sz;
2c8dd318 7402
11f136ee 7403 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 7404 if (tg == 3) { /* 64KB pages */
973a5434 7405 stride = 13;
2c8dd318
RH
7406 }
7407 if (tg == 1) { /* 16KB pages */
973a5434 7408 stride = 11;
2c8dd318 7409 }
3dde962f
PM
7410 }
7411
0480f69a 7412 /* Here we should have set up all the parameters for the translation:
973a5434 7413 * va_size, inputsize, ttbr, epd, stride, tbi
0480f69a
PM
7414 */
7415
3dde962f 7416 if (epd) {
88e8add8
GB
7417 /* Translation table walk disabled => Translation fault on TLB miss
7418 * Note: This is always 0 on 64-bit EL2 and EL3.
7419 */
3dde962f
PM
7420 goto do_fault;
7421 }
7422
1853d5a9
EI
7423 if (mmu_idx != ARMMMUIdx_S2NS) {
7424 /* The starting level depends on the virtual address size (which can
7425 * be up to 48 bits) and the translation granule size. It indicates
7426 * the number of strides (stride bits at a time) needed to
7427 * consume the bits of the input address. In the pseudocode this is:
7428 * level = 4 - RoundUp((inputsize - grainsize) / stride)
7429 * where their 'inputsize' is our 'inputsize', 'grainsize' is
7430 * our 'stride + 3' and 'stride' is our 'stride'.
7431 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
7432 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
7433 * = 4 - (inputsize - 4) / stride;
7434 */
7435 level = 4 - (inputsize - 4) / stride;
7436 } else {
7437 /* For stage 2 translations the starting level is specified by the
7438 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
7439 */
1b4093ea
SS
7440 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
7441 uint32_t startlevel;
1853d5a9
EI
7442 bool ok;
7443
7444 if (va_size == 32 || stride == 9) {
7445 /* AArch32 or 4KB pages */
1b4093ea 7446 startlevel = 2 - sl0;
1853d5a9
EI
7447 } else {
7448 /* 16KB or 64KB pages */
1b4093ea 7449 startlevel = 3 - sl0;
1853d5a9
EI
7450 }
7451
7452 /* Check that the starting level is valid. */
1b4093ea
SS
7453 ok = check_s2_mmu_setup(cpu, va_size == 64, startlevel,
7454 inputsize, stride);
1853d5a9 7455 if (!ok) {
1853d5a9
EI
7456 fault_type = translation_fault;
7457 goto do_fault;
7458 }
1b4093ea 7459 level = startlevel;
1853d5a9 7460 }
3dde962f 7461
dddb5223
SS
7462 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
7463 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
7464
7465 /* Now we can extract the actual base address from the TTBR */
2c8dd318 7466 descaddr = extract64(ttbr, 0, 48);
dddb5223 7467 descaddr &= ~indexmask;
3dde962f 7468
6109769a 7469 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
7470 * but up to bit 47 for ARMv8, but we use the descaddrmask
7471 * up to bit 39 for AArch32, because we don't need other bits in that case
7472 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 7473 */
dddb5223
SS
7474 descaddrmask = ((1ull << (va_size == 64 ? 48 : 40)) - 1) &
7475 ~indexmask_grainsize;
6109769a 7476
ebca90e4
PM
7477 /* Secure accesses start with the page table in secure memory and
7478 * can be downgraded to non-secure at any step. Non-secure accesses
7479 * remain non-secure. We implement this by just ORing in the NSTable/NS
7480 * bits at each step.
7481 */
7482 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
7483 for (;;) {
7484 uint64_t descriptor;
ebca90e4 7485 bool nstable;
3dde962f 7486
dddb5223 7487 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 7488 descaddr &= ~7ULL;
ebca90e4 7489 nstable = extract32(tableattrs, 4, 1);
37785977
EI
7490 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi);
7491 if (fi->s1ptw) {
7492 goto do_fault;
7493 }
7494
3dde962f
PM
7495 if (!(descriptor & 1) ||
7496 (!(descriptor & 2) && (level == 3))) {
7497 /* Invalid, or the Reserved level 3 encoding */
7498 goto do_fault;
7499 }
6109769a 7500 descaddr = descriptor & descaddrmask;
3dde962f
PM
7501
7502 if ((descriptor & 2) && (level < 3)) {
7503 /* Table entry. The top five bits are attributes which may
7504 * propagate down through lower levels of the table (and
7505 * which are all arranged so that 0 means "no effect", so
7506 * we can gather them up by ORing in the bits at each level).
7507 */
7508 tableattrs |= extract64(descriptor, 59, 5);
7509 level++;
dddb5223 7510 indexmask = indexmask_grainsize;
3dde962f
PM
7511 continue;
7512 }
7513 /* Block entry at level 1 or 2, or page entry at level 3.
7514 * These are basically the same thing, although the number
7515 * of bits we pull in from the vaddr varies.
7516 */
973a5434 7517 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 7518 descaddr |= (address & (page_size - 1));
6ab1a5ee 7519 /* Extract attributes from the descriptor */
d615efac
IC
7520 attrs = extract64(descriptor, 2, 10)
7521 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
7522
7523 if (mmu_idx == ARMMMUIdx_S2NS) {
7524 /* Stage 2 table descriptors do not include any attribute fields */
7525 break;
7526 }
7527 /* Merge in attributes from table descriptors */
3dde962f
PM
7528 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
7529 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
7530 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
7531 * means "force PL1 access only", which means forcing AP[1] to 0.
7532 */
7533 if (extract32(tableattrs, 2, 1)) {
7534 attrs &= ~(1 << 4);
7535 }
ebca90e4 7536 attrs |= nstable << 3; /* NS */
3dde962f
PM
7537 break;
7538 }
7539 /* Here descaddr is the final physical address, and attributes
7540 * are all in attrs.
7541 */
7542 fault_type = access_fault;
7543 if ((attrs & (1 << 8)) == 0) {
7544 /* Access flag */
7545 goto do_fault;
7546 }
d8e052b3
AJ
7547
7548 ap = extract32(attrs, 4, 2);
d8e052b3 7549 xn = extract32(attrs, 12, 1);
d8e052b3 7550
6ab1a5ee
EI
7551 if (mmu_idx == ARMMMUIdx_S2NS) {
7552 ns = true;
7553 *prot = get_S2prot(env, ap, xn);
7554 } else {
7555 ns = extract32(attrs, 3, 1);
7556 pxn = extract32(attrs, 11, 1);
7557 *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
7558 }
d8e052b3 7559
3dde962f 7560 fault_type = permission_fault;
d8e052b3 7561 if (!(*prot & (1 << access_type))) {
3dde962f
PM
7562 goto do_fault;
7563 }
3dde962f 7564
8bf5b6a9
PM
7565 if (ns) {
7566 /* The NS bit will (as required by the architecture) have no effect if
7567 * the CPU doesn't support TZ or this is a non-secure translation
7568 * regime, because the attribute will already be non-secure.
7569 */
7570 txattrs->secure = false;
7571 }
3dde962f
PM
7572 *phys_ptr = descaddr;
7573 *page_size_ptr = page_size;
b7cc4e82 7574 return false;
3dde962f
PM
7575
7576do_fault:
7577 /* Long-descriptor format IFSR/DFSR value */
b7cc4e82 7578 *fsr = (1 << 9) | (fault_type << 2) | level;
37785977
EI
7579 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
7580 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 7581 return true;
3dde962f
PM
7582}
7583
f6bda88f
PC
7584static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
7585 ARMMMUIdx mmu_idx,
7586 int32_t address, int *prot)
7587{
7588 *prot = PAGE_READ | PAGE_WRITE;
7589 switch (address) {
7590 case 0xF0000000 ... 0xFFFFFFFF:
7591 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
7592 *prot |= PAGE_EXEC;
7593 }
7594 break;
7595 case 0x00000000 ... 0x7FFFFFFF:
7596 *prot |= PAGE_EXEC;
7597 break;
7598 }
7599
7600}
7601
7602static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
7603 int access_type, ARMMMUIdx mmu_idx,
7604 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
7605{
7606 ARMCPU *cpu = arm_env_get_cpu(env);
7607 int n;
7608 bool is_user = regime_is_user(env, mmu_idx);
7609
7610 *phys_ptr = address;
7611 *prot = 0;
7612
7613 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
7614 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7615 } else { /* MPU enabled */
7616 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
7617 /* region search */
7618 uint32_t base = env->pmsav7.drbar[n];
7619 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
7620 uint32_t rmask;
7621 bool srdis = false;
7622
7623 if (!(env->pmsav7.drsr[n] & 0x1)) {
7624 continue;
7625 }
7626
7627 if (!rsize) {
7628 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
7629 continue;
7630 }
7631 rsize++;
7632 rmask = (1ull << rsize) - 1;
7633
7634 if (base & rmask) {
7635 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
7636 "to DRSR region size, mask = %" PRIx32,
7637 base, rmask);
7638 continue;
7639 }
7640
7641 if (address < base || address > base + rmask) {
7642 continue;
7643 }
7644
7645 /* Region matched */
7646
7647 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
7648 int i, snd;
7649 uint32_t srdis_mask;
7650
7651 rsize -= 3; /* sub region size (power of 2) */
7652 snd = ((address - base) >> rsize) & 0x7;
7653 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
7654
7655 srdis_mask = srdis ? 0x3 : 0x0;
7656 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
7657 /* This will check in groups of 2, 4 and then 8, whether
7658 * the subregion bits are consistent. rsize is incremented
7659 * back up to give the region size, considering consistent
7660 * adjacent subregions as one region. Stop testing if rsize
7661 * is already big enough for an entire QEMU page.
7662 */
7663 int snd_rounded = snd & ~(i - 1);
7664 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
7665 snd_rounded + 8, i);
7666 if (srdis_mask ^ srdis_multi) {
7667 break;
7668 }
7669 srdis_mask = (srdis_mask << i) | srdis_mask;
7670 rsize++;
7671 }
7672 }
7673 if (rsize < TARGET_PAGE_BITS) {
7674 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
7675 "alignment of %" PRIu32 " bits. Minimum is %d\n",
7676 rsize, TARGET_PAGE_BITS);
7677 continue;
7678 }
7679 if (srdis) {
7680 continue;
7681 }
7682 break;
7683 }
7684
7685 if (n == -1) { /* no hits */
7686 if (cpu->pmsav7_dregion &&
7687 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
7688 /* background fault */
7689 *fsr = 0;
7690 return true;
7691 }
7692 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7693 } else { /* a MPU hit! */
7694 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
7695
7696 if (is_user) { /* User mode AP bit decoding */
7697 switch (ap) {
7698 case 0:
7699 case 1:
7700 case 5:
7701 break; /* no access */
7702 case 3:
7703 *prot |= PAGE_WRITE;
7704 /* fall through */
7705 case 2:
7706 case 6:
7707 *prot |= PAGE_READ | PAGE_EXEC;
7708 break;
7709 default:
7710 qemu_log_mask(LOG_GUEST_ERROR,
7711 "Bad value for AP bits in DRACR %"
7712 PRIx32 "\n", ap);
7713 }
7714 } else { /* Priv. mode AP bits decoding */
7715 switch (ap) {
7716 case 0:
7717 break; /* no access */
7718 case 1:
7719 case 2:
7720 case 3:
7721 *prot |= PAGE_WRITE;
7722 /* fall through */
7723 case 5:
7724 case 6:
7725 *prot |= PAGE_READ | PAGE_EXEC;
7726 break;
7727 default:
7728 qemu_log_mask(LOG_GUEST_ERROR,
7729 "Bad value for AP bits in DRACR %"
7730 PRIx32 "\n", ap);
7731 }
7732 }
7733
7734 /* execute never */
7735 if (env->pmsav7.dracr[n] & (1 << 12)) {
7736 *prot &= ~PAGE_EXEC;
7737 }
7738 }
7739 }
7740
7741 *fsr = 0x00d; /* Permission fault */
7742 return !(*prot & (1 << access_type));
7743}
7744
13689d43
PC
7745static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
7746 int access_type, ARMMMUIdx mmu_idx,
7747 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9ee6e8bb
PB
7748{
7749 int n;
7750 uint32_t mask;
7751 uint32_t base;
0480f69a 7752 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb
PB
7753
7754 *phys_ptr = address;
7755 for (n = 7; n >= 0; n--) {
554b0b09 7756 base = env->cp15.c6_region[n];
87c3d486 7757 if ((base & 1) == 0) {
554b0b09 7758 continue;
87c3d486 7759 }
554b0b09
PM
7760 mask = 1 << ((base >> 1) & 0x1f);
7761 /* Keep this shift separate from the above to avoid an
7762 (undefined) << 32. */
7763 mask = (mask << 1) - 1;
87c3d486 7764 if (((base ^ address) & ~mask) == 0) {
554b0b09 7765 break;
87c3d486 7766 }
9ee6e8bb 7767 }
87c3d486 7768 if (n < 0) {
b7cc4e82
PC
7769 *fsr = 2;
7770 return true;
87c3d486 7771 }
9ee6e8bb
PB
7772
7773 if (access_type == 2) {
7e09797c 7774 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 7775 } else {
7e09797c 7776 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
7777 }
7778 mask = (mask >> (n * 4)) & 0xf;
7779 switch (mask) {
7780 case 0:
b7cc4e82
PC
7781 *fsr = 1;
7782 return true;
9ee6e8bb 7783 case 1:
87c3d486 7784 if (is_user) {
b7cc4e82
PC
7785 *fsr = 1;
7786 return true;
87c3d486 7787 }
554b0b09
PM
7788 *prot = PAGE_READ | PAGE_WRITE;
7789 break;
9ee6e8bb 7790 case 2:
554b0b09 7791 *prot = PAGE_READ;
87c3d486 7792 if (!is_user) {
554b0b09 7793 *prot |= PAGE_WRITE;
87c3d486 7794 }
554b0b09 7795 break;
9ee6e8bb 7796 case 3:
554b0b09
PM
7797 *prot = PAGE_READ | PAGE_WRITE;
7798 break;
9ee6e8bb 7799 case 5:
87c3d486 7800 if (is_user) {
b7cc4e82
PC
7801 *fsr = 1;
7802 return true;
87c3d486 7803 }
554b0b09
PM
7804 *prot = PAGE_READ;
7805 break;
9ee6e8bb 7806 case 6:
554b0b09
PM
7807 *prot = PAGE_READ;
7808 break;
9ee6e8bb 7809 default:
554b0b09 7810 /* Bad permission. */
b7cc4e82
PC
7811 *fsr = 1;
7812 return true;
9ee6e8bb 7813 }
3ad493fc 7814 *prot |= PAGE_EXEC;
b7cc4e82 7815 return false;
9ee6e8bb
PB
7816}
7817
702a9357
PM
7818/* get_phys_addr - get the physical address for this virtual address
7819 *
7820 * Find the physical address corresponding to the given virtual address,
7821 * by doing a translation table walk on MMU based systems or using the
7822 * MPU state on MPU based systems.
7823 *
b7cc4e82
PC
7824 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
7825 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
7826 * information on why the translation aborted, in the format of a
7827 * DFSR/IFSR fault register, with the following caveats:
7828 * * we honour the short vs long DFSR format differences.
7829 * * the WnR bit is never set (the caller must do this).
f6bda88f 7830 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
7831 * value.
7832 *
7833 * @env: CPUARMState
7834 * @address: virtual address to get physical address for
7835 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 7836 * @mmu_idx: MMU index indicating required translation regime
702a9357 7837 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 7838 * @attrs: set to the memory transaction attributes to use
702a9357
PM
7839 * @prot: set to the permissions for the page containing phys_ptr
7840 * @page_size: set to the size of the page containing phys_ptr
b7cc4e82 7841 * @fsr: set to the DFSR/IFSR value on failure
702a9357 7842 */
af51f566
EI
7843static bool get_phys_addr(CPUARMState *env, target_ulong address,
7844 int access_type, ARMMMUIdx mmu_idx,
7845 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
7846 target_ulong *page_size, uint32_t *fsr,
7847 ARMMMUFaultInfo *fi)
9ee6e8bb 7848{
0480f69a 7849 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
7850 /* Call ourselves recursively to do the stage 1 and then stage 2
7851 * translations.
0480f69a 7852 */
9b539263
EI
7853 if (arm_feature(env, ARM_FEATURE_EL2)) {
7854 hwaddr ipa;
7855 int s2_prot;
7856 int ret;
7857
7858 ret = get_phys_addr(env, address, access_type,
7859 mmu_idx + ARMMMUIdx_S1NSE0, &ipa, attrs,
7860 prot, page_size, fsr, fi);
7861
7862 /* If S1 fails or S2 is disabled, return early. */
7863 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
7864 *phys_ptr = ipa;
7865 return ret;
7866 }
7867
7868 /* S1 is done. Now do S2 translation. */
7869 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
7870 phys_ptr, attrs, &s2_prot,
7871 page_size, fsr, fi);
7872 fi->s2addr = ipa;
7873 /* Combine the S1 and S2 perms. */
7874 *prot &= s2_prot;
7875 return ret;
7876 } else {
7877 /*
7878 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
7879 */
7880 mmu_idx += ARMMMUIdx_S1NSE0;
7881 }
0480f69a 7882 }
d3649702 7883
8bf5b6a9
PM
7884 /* The page table entries may downgrade secure to non-secure, but
7885 * cannot upgrade an non-secure translation regime's attributes
7886 * to secure.
7887 */
7888 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 7889 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 7890
0480f69a
PM
7891 /* Fast Context Switch Extension. This doesn't exist at all in v8.
7892 * In v7 and earlier it affects all stage 1 translations.
7893 */
7894 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
7895 && !arm_feature(env, ARM_FEATURE_V8)) {
7896 if (regime_el(env, mmu_idx) == 3) {
7897 address += env->cp15.fcseidr_s;
7898 } else {
7899 address += env->cp15.fcseidr_ns;
7900 }
54bf36ed 7901 }
9ee6e8bb 7902
f6bda88f
PC
7903 /* pmsav7 has special handling for when MPU is disabled so call it before
7904 * the common MMU/MPU disabled check below.
7905 */
7906 if (arm_feature(env, ARM_FEATURE_MPU) &&
7907 arm_feature(env, ARM_FEATURE_V7)) {
7908 *page_size = TARGET_PAGE_SIZE;
7909 return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
7910 phys_ptr, prot, fsr);
7911 }
7912
0480f69a 7913 if (regime_translation_disabled(env, mmu_idx)) {
9ee6e8bb
PB
7914 /* MMU/MPU disabled. */
7915 *phys_ptr = address;
3ad493fc 7916 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 7917 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 7918 return 0;
0480f69a
PM
7919 }
7920
7921 if (arm_feature(env, ARM_FEATURE_MPU)) {
f6bda88f 7922 /* Pre-v7 MPU */
d4c430a8 7923 *page_size = TARGET_PAGE_SIZE;
13689d43
PC
7924 return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
7925 phys_ptr, prot, fsr);
0480f69a
PM
7926 }
7927
7928 if (regime_using_lpae_format(env, mmu_idx)) {
7929 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 7930 attrs, prot, page_size, fsr, fi);
0480f69a
PM
7931 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
7932 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 7933 attrs, prot, page_size, fsr, fi);
9ee6e8bb 7934 } else {
0480f69a 7935 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 7936 prot, page_size, fsr, fi);
9ee6e8bb
PB
7937 }
7938}
7939
8c6084bf 7940/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
7941 * to the TLB. Return false on success, or true on failure. Populate
7942 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 7943 */
b7cc4e82 7944bool arm_tlb_fill(CPUState *cs, vaddr address,
e14b5a23
EI
7945 int access_type, int mmu_idx, uint32_t *fsr,
7946 ARMMMUFaultInfo *fi)
b5ff1b31 7947{
7510454e
AF
7948 ARMCPU *cpu = ARM_CPU(cs);
7949 CPUARMState *env = &cpu->env;
a8170e5e 7950 hwaddr phys_addr;
d4c430a8 7951 target_ulong page_size;
b5ff1b31 7952 int prot;
d3649702 7953 int ret;
8bf5b6a9 7954 MemTxAttrs attrs = {};
b5ff1b31 7955
8bf5b6a9 7956 ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
e14b5a23 7957 &attrs, &prot, &page_size, fsr, fi);
b7cc4e82 7958 if (!ret) {
b5ff1b31 7959 /* Map a single [sub]page. */
dcd82c11
AB
7960 phys_addr &= TARGET_PAGE_MASK;
7961 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
7962 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
7963 prot, mmu_idx, page_size);
d4c430a8 7964 return 0;
b5ff1b31
FB
7965 }
7966
8c6084bf 7967 return ret;
b5ff1b31
FB
7968}
7969
0faea0c7
PM
7970hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
7971 MemTxAttrs *attrs)
b5ff1b31 7972{
00b941e5 7973 ARMCPU *cpu = ARM_CPU(cs);
d3649702 7974 CPUARMState *env = &cpu->env;
a8170e5e 7975 hwaddr phys_addr;
d4c430a8 7976 target_ulong page_size;
b5ff1b31 7977 int prot;
b7cc4e82
PC
7978 bool ret;
7979 uint32_t fsr;
e14b5a23 7980 ARMMMUFaultInfo fi = {};
b5ff1b31 7981
0faea0c7
PM
7982 *attrs = (MemTxAttrs) {};
7983
97ed5ccd 7984 ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
0faea0c7 7985 attrs, &prot, &page_size, &fsr, &fi);
b5ff1b31 7986
b7cc4e82 7987 if (ret) {
b5ff1b31 7988 return -1;
00b941e5 7989 }
b5ff1b31
FB
7990 return phys_addr;
7991}
7992
0ecb72a5 7993uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 7994{
a47dddd7
AF
7995 ARMCPU *cpu = arm_env_get_cpu(env);
7996
9ee6e8bb
PB
7997 switch (reg) {
7998 case 0: /* APSR */
7999 return xpsr_read(env) & 0xf8000000;
8000 case 1: /* IAPSR */
8001 return xpsr_read(env) & 0xf80001ff;
8002 case 2: /* EAPSR */
8003 return xpsr_read(env) & 0xff00fc00;
8004 case 3: /* xPSR */
8005 return xpsr_read(env) & 0xff00fdff;
8006 case 5: /* IPSR */
8007 return xpsr_read(env) & 0x000001ff;
8008 case 6: /* EPSR */
8009 return xpsr_read(env) & 0x0700fc00;
8010 case 7: /* IEPSR */
8011 return xpsr_read(env) & 0x0700edff;
8012 case 8: /* MSP */
8013 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
8014 case 9: /* PSP */
8015 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
8016 case 16: /* PRIMASK */
4cc35614 8017 return (env->daif & PSTATE_I) != 0;
82845826
SH
8018 case 17: /* BASEPRI */
8019 case 18: /* BASEPRI_MAX */
9ee6e8bb 8020 return env->v7m.basepri;
82845826 8021 case 19: /* FAULTMASK */
4cc35614 8022 return (env->daif & PSTATE_F) != 0;
9ee6e8bb
PB
8023 case 20: /* CONTROL */
8024 return env->v7m.control;
8025 default:
8026 /* ??? For debugging only. */
a47dddd7 8027 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
9ee6e8bb
PB
8028 return 0;
8029 }
8030}
8031
0ecb72a5 8032void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 8033{
a47dddd7
AF
8034 ARMCPU *cpu = arm_env_get_cpu(env);
8035
9ee6e8bb
PB
8036 switch (reg) {
8037 case 0: /* APSR */
8038 xpsr_write(env, val, 0xf8000000);
8039 break;
8040 case 1: /* IAPSR */
8041 xpsr_write(env, val, 0xf8000000);
8042 break;
8043 case 2: /* EAPSR */
8044 xpsr_write(env, val, 0xfe00fc00);
8045 break;
8046 case 3: /* xPSR */
8047 xpsr_write(env, val, 0xfe00fc00);
8048 break;
8049 case 5: /* IPSR */
8050 /* IPSR bits are readonly. */
8051 break;
8052 case 6: /* EPSR */
8053 xpsr_write(env, val, 0x0600fc00);
8054 break;
8055 case 7: /* IEPSR */
8056 xpsr_write(env, val, 0x0600fc00);
8057 break;
8058 case 8: /* MSP */
8059 if (env->v7m.current_sp)
8060 env->v7m.other_sp = val;
8061 else
8062 env->regs[13] = val;
8063 break;
8064 case 9: /* PSP */
8065 if (env->v7m.current_sp)
8066 env->regs[13] = val;
8067 else
8068 env->v7m.other_sp = val;
8069 break;
8070 case 16: /* PRIMASK */
4cc35614
PM
8071 if (val & 1) {
8072 env->daif |= PSTATE_I;
8073 } else {
8074 env->daif &= ~PSTATE_I;
8075 }
9ee6e8bb 8076 break;
82845826 8077 case 17: /* BASEPRI */
9ee6e8bb
PB
8078 env->v7m.basepri = val & 0xff;
8079 break;
82845826 8080 case 18: /* BASEPRI_MAX */
9ee6e8bb
PB
8081 val &= 0xff;
8082 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
8083 env->v7m.basepri = val;
8084 break;
82845826 8085 case 19: /* FAULTMASK */
4cc35614
PM
8086 if (val & 1) {
8087 env->daif |= PSTATE_F;
8088 } else {
8089 env->daif &= ~PSTATE_F;
8090 }
82845826 8091 break;
9ee6e8bb
PB
8092 case 20: /* CONTROL */
8093 env->v7m.control = val & 3;
8094 switch_v7m_sp(env, (val & 2) != 0);
8095 break;
8096 default:
8097 /* ??? For debugging only. */
a47dddd7 8098 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
9ee6e8bb
PB
8099 return;
8100 }
8101}
8102
b5ff1b31 8103#endif
6ddbc6e4 8104
aca3f40b
PM
8105void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
8106{
8107 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
8108 * Note that we do not implement the (architecturally mandated)
8109 * alignment fault for attempts to use this on Device memory
8110 * (which matches the usual QEMU behaviour of not implementing either
8111 * alignment faults or any memory attribute handling).
8112 */
8113
8114 ARMCPU *cpu = arm_env_get_cpu(env);
8115 uint64_t blocklen = 4 << cpu->dcz_blocksize;
8116 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
8117
8118#ifndef CONFIG_USER_ONLY
8119 {
8120 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
8121 * the block size so we might have to do more than one TLB lookup.
8122 * We know that in fact for any v8 CPU the page size is at least 4K
8123 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
8124 * 1K as an artefact of legacy v5 subpage support being present in the
8125 * same QEMU executable.
8126 */
8127 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
8128 void *hostaddr[maxidx];
8129 int try, i;
97ed5ccd 8130 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 8131 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
8132
8133 for (try = 0; try < 2; try++) {
8134
8135 for (i = 0; i < maxidx; i++) {
8136 hostaddr[i] = tlb_vaddr_to_host(env,
8137 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 8138 1, mmu_idx);
aca3f40b
PM
8139 if (!hostaddr[i]) {
8140 break;
8141 }
8142 }
8143 if (i == maxidx) {
8144 /* If it's all in the TLB it's fair game for just writing to;
8145 * we know we don't need to update dirty status, etc.
8146 */
8147 for (i = 0; i < maxidx - 1; i++) {
8148 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
8149 }
8150 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
8151 return;
8152 }
8153 /* OK, try a store and see if we can populate the tlb. This
8154 * might cause an exception if the memory isn't writable,
8155 * in which case we will longjmp out of here. We must for
8156 * this purpose use the actual register value passed to us
8157 * so that we get the fault address right.
8158 */
3972ef6f 8159 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
aca3f40b
PM
8160 /* Now we can populate the other TLB entries, if any */
8161 for (i = 0; i < maxidx; i++) {
8162 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
8163 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
3972ef6f 8164 helper_ret_stb_mmu(env, va, 0, oi, GETRA());
aca3f40b
PM
8165 }
8166 }
8167 }
8168
8169 /* Slow path (probably attempt to do this to an I/O device or
8170 * similar, or clearing of a block of code we have translations
8171 * cached for). Just do a series of byte writes as the architecture
8172 * demands. It's not worth trying to use a cpu_physical_memory_map(),
8173 * memset(), unmap() sequence here because:
8174 * + we'd need to account for the blocksize being larger than a page
8175 * + the direct-RAM access case is almost always going to be dealt
8176 * with in the fastpath code above, so there's no speed benefit
8177 * + we would have to deal with the map returning NULL because the
8178 * bounce buffer was in use
8179 */
8180 for (i = 0; i < blocklen; i++) {
3972ef6f 8181 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
aca3f40b
PM
8182 }
8183 }
8184#else
8185 memset(g2h(vaddr), 0, blocklen);
8186#endif
8187}
8188
6ddbc6e4
PB
8189/* Note that signed overflow is undefined in C. The following routines are
8190 careful to use unsigned types where modulo arithmetic is required.
8191 Failure to do so _will_ break on newer gcc. */
8192
8193/* Signed saturating arithmetic. */
8194
1654b2d6 8195/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
8196static inline uint16_t add16_sat(uint16_t a, uint16_t b)
8197{
8198 uint16_t res;
8199
8200 res = a + b;
8201 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
8202 if (a & 0x8000)
8203 res = 0x8000;
8204 else
8205 res = 0x7fff;
8206 }
8207 return res;
8208}
8209
1654b2d6 8210/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
8211static inline uint8_t add8_sat(uint8_t a, uint8_t b)
8212{
8213 uint8_t res;
8214
8215 res = a + b;
8216 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
8217 if (a & 0x80)
8218 res = 0x80;
8219 else
8220 res = 0x7f;
8221 }
8222 return res;
8223}
8224
1654b2d6 8225/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
8226static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
8227{
8228 uint16_t res;
8229
8230 res = a - b;
8231 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
8232 if (a & 0x8000)
8233 res = 0x8000;
8234 else
8235 res = 0x7fff;
8236 }
8237 return res;
8238}
8239
1654b2d6 8240/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
8241static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
8242{
8243 uint8_t res;
8244
8245 res = a - b;
8246 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
8247 if (a & 0x80)
8248 res = 0x80;
8249 else
8250 res = 0x7f;
8251 }
8252 return res;
8253}
8254
8255#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
8256#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
8257#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
8258#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
8259#define PFX q
8260
8261#include "op_addsub.h"
8262
8263/* Unsigned saturating arithmetic. */
460a09c1 8264static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
8265{
8266 uint16_t res;
8267 res = a + b;
8268 if (res < a)
8269 res = 0xffff;
8270 return res;
8271}
8272
460a09c1 8273static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 8274{
4c4fd3f8 8275 if (a > b)
6ddbc6e4
PB
8276 return a - b;
8277 else
8278 return 0;
8279}
8280
8281static inline uint8_t add8_usat(uint8_t a, uint8_t b)
8282{
8283 uint8_t res;
8284 res = a + b;
8285 if (res < a)
8286 res = 0xff;
8287 return res;
8288}
8289
8290static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
8291{
4c4fd3f8 8292 if (a > b)
6ddbc6e4
PB
8293 return a - b;
8294 else
8295 return 0;
8296}
8297
8298#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
8299#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
8300#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
8301#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
8302#define PFX uq
8303
8304#include "op_addsub.h"
8305
8306/* Signed modulo arithmetic. */
8307#define SARITH16(a, b, n, op) do { \
8308 int32_t sum; \
db6e2e65 8309 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
8310 RESULT(sum, n, 16); \
8311 if (sum >= 0) \
8312 ge |= 3 << (n * 2); \
8313 } while(0)
8314
8315#define SARITH8(a, b, n, op) do { \
8316 int32_t sum; \
db6e2e65 8317 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
8318 RESULT(sum, n, 8); \
8319 if (sum >= 0) \
8320 ge |= 1 << n; \
8321 } while(0)
8322
8323
8324#define ADD16(a, b, n) SARITH16(a, b, n, +)
8325#define SUB16(a, b, n) SARITH16(a, b, n, -)
8326#define ADD8(a, b, n) SARITH8(a, b, n, +)
8327#define SUB8(a, b, n) SARITH8(a, b, n, -)
8328#define PFX s
8329#define ARITH_GE
8330
8331#include "op_addsub.h"
8332
8333/* Unsigned modulo arithmetic. */
8334#define ADD16(a, b, n) do { \
8335 uint32_t sum; \
8336 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
8337 RESULT(sum, n, 16); \
a87aa10b 8338 if ((sum >> 16) == 1) \
6ddbc6e4
PB
8339 ge |= 3 << (n * 2); \
8340 } while(0)
8341
8342#define ADD8(a, b, n) do { \
8343 uint32_t sum; \
8344 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
8345 RESULT(sum, n, 8); \
a87aa10b
AZ
8346 if ((sum >> 8) == 1) \
8347 ge |= 1 << n; \
6ddbc6e4
PB
8348 } while(0)
8349
8350#define SUB16(a, b, n) do { \
8351 uint32_t sum; \
8352 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
8353 RESULT(sum, n, 16); \
8354 if ((sum >> 16) == 0) \
8355 ge |= 3 << (n * 2); \
8356 } while(0)
8357
8358#define SUB8(a, b, n) do { \
8359 uint32_t sum; \
8360 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
8361 RESULT(sum, n, 8); \
8362 if ((sum >> 8) == 0) \
a87aa10b 8363 ge |= 1 << n; \
6ddbc6e4
PB
8364 } while(0)
8365
8366#define PFX u
8367#define ARITH_GE
8368
8369#include "op_addsub.h"
8370
8371/* Halved signed arithmetic. */
8372#define ADD16(a, b, n) \
8373 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
8374#define SUB16(a, b, n) \
8375 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
8376#define ADD8(a, b, n) \
8377 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
8378#define SUB8(a, b, n) \
8379 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
8380#define PFX sh
8381
8382#include "op_addsub.h"
8383
8384/* Halved unsigned arithmetic. */
8385#define ADD16(a, b, n) \
8386 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8387#define SUB16(a, b, n) \
8388 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8389#define ADD8(a, b, n) \
8390 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8391#define SUB8(a, b, n) \
8392 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8393#define PFX uh
8394
8395#include "op_addsub.h"
8396
8397static inline uint8_t do_usad(uint8_t a, uint8_t b)
8398{
8399 if (a > b)
8400 return a - b;
8401 else
8402 return b - a;
8403}
8404
8405/* Unsigned sum of absolute byte differences. */
8406uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
8407{
8408 uint32_t sum;
8409 sum = do_usad(a, b);
8410 sum += do_usad(a >> 8, b >> 8);
8411 sum += do_usad(a >> 16, b >>16);
8412 sum += do_usad(a >> 24, b >> 24);
8413 return sum;
8414}
8415
8416/* For ARMv6 SEL instruction. */
8417uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
8418{
8419 uint32_t mask;
8420
8421 mask = 0;
8422 if (flags & 1)
8423 mask |= 0xff;
8424 if (flags & 2)
8425 mask |= 0xff00;
8426 if (flags & 4)
8427 mask |= 0xff0000;
8428 if (flags & 8)
8429 mask |= 0xff000000;
8430 return (a & mask) | (b & ~mask);
8431}
8432
b90372ad
PM
8433/* VFP support. We follow the convention used for VFP instructions:
8434 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
8435 "d" suffix. */
8436
8437/* Convert host exception flags to vfp form. */
8438static inline int vfp_exceptbits_from_host(int host_bits)
8439{
8440 int target_bits = 0;
8441
8442 if (host_bits & float_flag_invalid)
8443 target_bits |= 1;
8444 if (host_bits & float_flag_divbyzero)
8445 target_bits |= 2;
8446 if (host_bits & float_flag_overflow)
8447 target_bits |= 4;
36802b6b 8448 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
8449 target_bits |= 8;
8450 if (host_bits & float_flag_inexact)
8451 target_bits |= 0x10;
cecd8504
PM
8452 if (host_bits & float_flag_input_denormal)
8453 target_bits |= 0x80;
4373f3ce
PB
8454 return target_bits;
8455}
8456
0ecb72a5 8457uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
8458{
8459 int i;
8460 uint32_t fpscr;
8461
8462 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
8463 | (env->vfp.vec_len << 16)
8464 | (env->vfp.vec_stride << 20);
8465 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 8466 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
8467 fpscr |= vfp_exceptbits_from_host(i);
8468 return fpscr;
8469}
8470
0ecb72a5 8471uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
8472{
8473 return HELPER(vfp_get_fpscr)(env);
8474}
8475
4373f3ce
PB
8476/* Convert vfp exception flags to target form. */
8477static inline int vfp_exceptbits_to_host(int target_bits)
8478{
8479 int host_bits = 0;
8480
8481 if (target_bits & 1)
8482 host_bits |= float_flag_invalid;
8483 if (target_bits & 2)
8484 host_bits |= float_flag_divbyzero;
8485 if (target_bits & 4)
8486 host_bits |= float_flag_overflow;
8487 if (target_bits & 8)
8488 host_bits |= float_flag_underflow;
8489 if (target_bits & 0x10)
8490 host_bits |= float_flag_inexact;
cecd8504
PM
8491 if (target_bits & 0x80)
8492 host_bits |= float_flag_input_denormal;
4373f3ce
PB
8493 return host_bits;
8494}
8495
0ecb72a5 8496void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
8497{
8498 int i;
8499 uint32_t changed;
8500
8501 changed = env->vfp.xregs[ARM_VFP_FPSCR];
8502 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
8503 env->vfp.vec_len = (val >> 16) & 7;
8504 env->vfp.vec_stride = (val >> 20) & 3;
8505
8506 changed ^= val;
8507 if (changed & (3 << 22)) {
8508 i = (val >> 22) & 3;
8509 switch (i) {
4d3da0f3 8510 case FPROUNDING_TIEEVEN:
4373f3ce
PB
8511 i = float_round_nearest_even;
8512 break;
4d3da0f3 8513 case FPROUNDING_POSINF:
4373f3ce
PB
8514 i = float_round_up;
8515 break;
4d3da0f3 8516 case FPROUNDING_NEGINF:
4373f3ce
PB
8517 i = float_round_down;
8518 break;
4d3da0f3 8519 case FPROUNDING_ZERO:
4373f3ce
PB
8520 i = float_round_to_zero;
8521 break;
8522 }
8523 set_float_rounding_mode(i, &env->vfp.fp_status);
8524 }
cecd8504 8525 if (changed & (1 << 24)) {
fe76d976 8526 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
8527 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
8528 }
5c7908ed
PB
8529 if (changed & (1 << 25))
8530 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 8531
b12c390b 8532 i = vfp_exceptbits_to_host(val);
4373f3ce 8533 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 8534 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
8535}
8536
0ecb72a5 8537void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
8538{
8539 HELPER(vfp_set_fpscr)(env, val);
8540}
8541
4373f3ce
PB
8542#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
8543
8544#define VFP_BINOP(name) \
ae1857ec 8545float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 8546{ \
ae1857ec
PM
8547 float_status *fpst = fpstp; \
8548 return float32_ ## name(a, b, fpst); \
4373f3ce 8549} \
ae1857ec 8550float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 8551{ \
ae1857ec
PM
8552 float_status *fpst = fpstp; \
8553 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
8554}
8555VFP_BINOP(add)
8556VFP_BINOP(sub)
8557VFP_BINOP(mul)
8558VFP_BINOP(div)
f71a2ae5
PM
8559VFP_BINOP(min)
8560VFP_BINOP(max)
8561VFP_BINOP(minnum)
8562VFP_BINOP(maxnum)
4373f3ce
PB
8563#undef VFP_BINOP
8564
8565float32 VFP_HELPER(neg, s)(float32 a)
8566{
8567 return float32_chs(a);
8568}
8569
8570float64 VFP_HELPER(neg, d)(float64 a)
8571{
66230e0d 8572 return float64_chs(a);
4373f3ce
PB
8573}
8574
8575float32 VFP_HELPER(abs, s)(float32 a)
8576{
8577 return float32_abs(a);
8578}
8579
8580float64 VFP_HELPER(abs, d)(float64 a)
8581{
66230e0d 8582 return float64_abs(a);
4373f3ce
PB
8583}
8584
0ecb72a5 8585float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
8586{
8587 return float32_sqrt(a, &env->vfp.fp_status);
8588}
8589
0ecb72a5 8590float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
8591{
8592 return float64_sqrt(a, &env->vfp.fp_status);
8593}
8594
8595/* XXX: check quiet/signaling case */
8596#define DO_VFP_cmp(p, type) \
0ecb72a5 8597void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
8598{ \
8599 uint32_t flags; \
8600 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
8601 case 0: flags = 0x6; break; \
8602 case -1: flags = 0x8; break; \
8603 case 1: flags = 0x2; break; \
8604 default: case 2: flags = 0x3; break; \
8605 } \
8606 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8607 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8608} \
0ecb72a5 8609void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
8610{ \
8611 uint32_t flags; \
8612 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
8613 case 0: flags = 0x6; break; \
8614 case -1: flags = 0x8; break; \
8615 case 1: flags = 0x2; break; \
8616 default: case 2: flags = 0x3; break; \
8617 } \
8618 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8619 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8620}
8621DO_VFP_cmp(s, float32)
8622DO_VFP_cmp(d, float64)
8623#undef DO_VFP_cmp
8624
5500b06c 8625/* Integer to float and float to integer conversions */
4373f3ce 8626
5500b06c
PM
8627#define CONV_ITOF(name, fsz, sign) \
8628 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
8629{ \
8630 float_status *fpst = fpstp; \
85836979 8631 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
8632}
8633
5500b06c
PM
8634#define CONV_FTOI(name, fsz, sign, round) \
8635uint32_t HELPER(name)(float##fsz x, void *fpstp) \
8636{ \
8637 float_status *fpst = fpstp; \
8638 if (float##fsz##_is_any_nan(x)) { \
8639 float_raise(float_flag_invalid, fpst); \
8640 return 0; \
8641 } \
8642 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
8643}
8644
5500b06c
PM
8645#define FLOAT_CONVS(name, p, fsz, sign) \
8646CONV_ITOF(vfp_##name##to##p, fsz, sign) \
8647CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
8648CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 8649
5500b06c
PM
8650FLOAT_CONVS(si, s, 32, )
8651FLOAT_CONVS(si, d, 64, )
8652FLOAT_CONVS(ui, s, 32, u)
8653FLOAT_CONVS(ui, d, 64, u)
4373f3ce 8654
5500b06c
PM
8655#undef CONV_ITOF
8656#undef CONV_FTOI
8657#undef FLOAT_CONVS
4373f3ce
PB
8658
8659/* floating point conversion */
0ecb72a5 8660float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 8661{
2d627737
PM
8662 float64 r = float32_to_float64(x, &env->vfp.fp_status);
8663 /* ARM requires that S<->D conversion of any kind of NaN generates
8664 * a quiet NaN by forcing the most significant frac bit to 1.
8665 */
8666 return float64_maybe_silence_nan(r);
4373f3ce
PB
8667}
8668
0ecb72a5 8669float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 8670{
2d627737
PM
8671 float32 r = float64_to_float32(x, &env->vfp.fp_status);
8672 /* ARM requires that S<->D conversion of any kind of NaN generates
8673 * a quiet NaN by forcing the most significant frac bit to 1.
8674 */
8675 return float32_maybe_silence_nan(r);
4373f3ce
PB
8676}
8677
8678/* VFP3 fixed point conversion. */
16d5b3ca 8679#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
8680float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
8681 void *fpstp) \
4373f3ce 8682{ \
5500b06c 8683 float_status *fpst = fpstp; \
622465e1 8684 float##fsz tmp; \
8ed697e8 8685 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 8686 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
8687}
8688
abe66f70
PM
8689/* Notice that we want only input-denormal exception flags from the
8690 * scalbn operation: the other possible flags (overflow+inexact if
8691 * we overflow to infinity, output-denormal) aren't correct for the
8692 * complete scale-and-convert operation.
8693 */
16d5b3ca
WN
8694#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
8695uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
8696 uint32_t shift, \
8697 void *fpstp) \
4373f3ce 8698{ \
5500b06c 8699 float_status *fpst = fpstp; \
abe66f70 8700 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
8701 float##fsz tmp; \
8702 if (float##fsz##_is_any_nan(x)) { \
5500b06c 8703 float_raise(float_flag_invalid, fpst); \
622465e1 8704 return 0; \
09d9487f 8705 } \
5500b06c 8706 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
8707 old_exc_flags |= get_float_exception_flags(fpst) \
8708 & float_flag_input_denormal; \
8709 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 8710 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
8711}
8712
16d5b3ca
WN
8713#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
8714VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
8715VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
8716VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
8717
8718#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
8719VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8720VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 8721
8ed697e8
WN
8722VFP_CONV_FIX(sh, d, 64, 64, int16)
8723VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 8724VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
8725VFP_CONV_FIX(uh, d, 64, 64, uint16)
8726VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 8727VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
8728VFP_CONV_FIX(sh, s, 32, 32, int16)
8729VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 8730VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
8731VFP_CONV_FIX(uh, s, 32, 32, uint16)
8732VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 8733VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 8734#undef VFP_CONV_FIX
16d5b3ca
WN
8735#undef VFP_CONV_FIX_FLOAT
8736#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 8737
52a1f6a3
AG
8738/* Set the current fp rounding mode and return the old one.
8739 * The argument is a softfloat float_round_ value.
8740 */
8741uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
8742{
8743 float_status *fp_status = &env->vfp.fp_status;
8744
8745 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8746 set_float_rounding_mode(rmode, fp_status);
8747
8748 return prev_rmode;
8749}
8750
43630e58
WN
8751/* Set the current fp rounding mode in the standard fp status and return
8752 * the old one. This is for NEON instructions that need to change the
8753 * rounding mode but wish to use the standard FPSCR values for everything
8754 * else. Always set the rounding mode back to the correct value after
8755 * modifying it.
8756 * The argument is a softfloat float_round_ value.
8757 */
8758uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
8759{
8760 float_status *fp_status = &env->vfp.standard_fp_status;
8761
8762 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8763 set_float_rounding_mode(rmode, fp_status);
8764
8765 return prev_rmode;
8766}
8767
60011498 8768/* Half precision conversions. */
0ecb72a5 8769static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 8770{
60011498 8771 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
8772 float32 r = float16_to_float32(make_float16(a), ieee, s);
8773 if (ieee) {
8774 return float32_maybe_silence_nan(r);
8775 }
8776 return r;
60011498
PB
8777}
8778
0ecb72a5 8779static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 8780{
60011498 8781 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
8782 float16 r = float32_to_float16(a, ieee, s);
8783 if (ieee) {
8784 r = float16_maybe_silence_nan(r);
8785 }
8786 return float16_val(r);
60011498
PB
8787}
8788
0ecb72a5 8789float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
8790{
8791 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
8792}
8793
0ecb72a5 8794uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
8795{
8796 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
8797}
8798
0ecb72a5 8799float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
8800{
8801 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
8802}
8803
0ecb72a5 8804uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
8805{
8806 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
8807}
8808
8900aad2
PM
8809float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
8810{
8811 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8812 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
8813 if (ieee) {
8814 return float64_maybe_silence_nan(r);
8815 }
8816 return r;
8817}
8818
8819uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
8820{
8821 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8822 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
8823 if (ieee) {
8824 r = float16_maybe_silence_nan(r);
8825 }
8826 return float16_val(r);
8827}
8828
dda3ec49 8829#define float32_two make_float32(0x40000000)
6aae3df1
PM
8830#define float32_three make_float32(0x40400000)
8831#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 8832
0ecb72a5 8833float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 8834{
dda3ec49
PM
8835 float_status *s = &env->vfp.standard_fp_status;
8836 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8837 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
8838 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8839 float_raise(float_flag_input_denormal, s);
8840 }
dda3ec49
PM
8841 return float32_two;
8842 }
8843 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
8844}
8845
0ecb72a5 8846float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 8847{
71826966 8848 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
8849 float32 product;
8850 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8851 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
8852 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8853 float_raise(float_flag_input_denormal, s);
8854 }
6aae3df1 8855 return float32_one_point_five;
9ea62f57 8856 }
6aae3df1
PM
8857 product = float32_mul(a, b, s);
8858 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
8859}
8860
8f8e3aa4
PB
8861/* NEON helpers. */
8862
56bf4fe2
CL
8863/* Constants 256 and 512 are used in some helpers; we avoid relying on
8864 * int->float conversions at run-time. */
8865#define float64_256 make_float64(0x4070000000000000LL)
8866#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
8867#define float32_maxnorm make_float32(0x7f7fffff)
8868#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 8869
b6d4443a
AB
8870/* Reciprocal functions
8871 *
8872 * The algorithm that must be used to calculate the estimate
8873 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 8874 */
b6d4443a
AB
8875
8876static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 8877{
1146a817
PM
8878 /* These calculations mustn't set any fp exception flags,
8879 * so we use a local copy of the fp_status.
8880 */
b6d4443a 8881 float_status dummy_status = *real_fp_status;
1146a817 8882 float_status *s = &dummy_status;
fe0e4872
CL
8883 /* q = (int)(a * 512.0) */
8884 float64 q = float64_mul(float64_512, a, s);
8885 int64_t q_int = float64_to_int64_round_to_zero(q, s);
8886
8887 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
8888 q = int64_to_float64(q_int, s);
8889 q = float64_add(q, float64_half, s);
8890 q = float64_div(q, float64_512, s);
8891 q = float64_div(float64_one, q, s);
8892
8893 /* s = (int)(256.0 * r + 0.5) */
8894 q = float64_mul(q, float64_256, s);
8895 q = float64_add(q, float64_half, s);
8896 q_int = float64_to_int64_round_to_zero(q, s);
8897
8898 /* return (double)s / 256.0 */
8899 return float64_div(int64_to_float64(q_int, s), float64_256, s);
8900}
8901
b6d4443a
AB
8902/* Common wrapper to call recip_estimate */
8903static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 8904{
b6d4443a
AB
8905 uint64_t val64 = float64_val(num);
8906 uint64_t frac = extract64(val64, 0, 52);
8907 int64_t exp = extract64(val64, 52, 11);
8908 uint64_t sbit;
8909 float64 scaled, estimate;
fe0e4872 8910
b6d4443a
AB
8911 /* Generate the scaled number for the estimate function */
8912 if (exp == 0) {
8913 if (extract64(frac, 51, 1) == 0) {
8914 exp = -1;
8915 frac = extract64(frac, 0, 50) << 2;
8916 } else {
8917 frac = extract64(frac, 0, 51) << 1;
8918 }
8919 }
fe0e4872 8920
b6d4443a
AB
8921 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
8922 scaled = make_float64((0x3feULL << 52)
8923 | extract64(frac, 44, 8) << 44);
8924
8925 estimate = recip_estimate(scaled, fpst);
8926
8927 /* Build new result */
8928 val64 = float64_val(estimate);
8929 sbit = 0x8000000000000000ULL & val64;
8930 exp = off - exp;
8931 frac = extract64(val64, 0, 52);
8932
8933 if (exp == 0) {
8934 frac = 1ULL << 51 | extract64(frac, 1, 51);
8935 } else if (exp == -1) {
8936 frac = 1ULL << 50 | extract64(frac, 2, 50);
8937 exp = 0;
8938 }
8939
8940 return make_float64(sbit | (exp << 52) | frac);
8941}
8942
8943static bool round_to_inf(float_status *fpst, bool sign_bit)
8944{
8945 switch (fpst->float_rounding_mode) {
8946 case float_round_nearest_even: /* Round to Nearest */
8947 return true;
8948 case float_round_up: /* Round to +Inf */
8949 return !sign_bit;
8950 case float_round_down: /* Round to -Inf */
8951 return sign_bit;
8952 case float_round_to_zero: /* Round to Zero */
8953 return false;
8954 }
8955
8956 g_assert_not_reached();
8957}
8958
8959float32 HELPER(recpe_f32)(float32 input, void *fpstp)
8960{
8961 float_status *fpst = fpstp;
8962 float32 f32 = float32_squash_input_denormal(input, fpst);
8963 uint32_t f32_val = float32_val(f32);
8964 uint32_t f32_sbit = 0x80000000ULL & f32_val;
8965 int32_t f32_exp = extract32(f32_val, 23, 8);
8966 uint32_t f32_frac = extract32(f32_val, 0, 23);
8967 float64 f64, r64;
8968 uint64_t r64_val;
8969 int64_t r64_exp;
8970 uint64_t r64_frac;
8971
8972 if (float32_is_any_nan(f32)) {
8973 float32 nan = f32;
8974 if (float32_is_signaling_nan(f32)) {
8975 float_raise(float_flag_invalid, fpst);
8976 nan = float32_maybe_silence_nan(f32);
fe0e4872 8977 }
b6d4443a
AB
8978 if (fpst->default_nan_mode) {
8979 nan = float32_default_nan;
43fe9bdb 8980 }
b6d4443a
AB
8981 return nan;
8982 } else if (float32_is_infinity(f32)) {
8983 return float32_set_sign(float32_zero, float32_is_neg(f32));
8984 } else if (float32_is_zero(f32)) {
8985 float_raise(float_flag_divbyzero, fpst);
8986 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8987 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
8988 /* Abs(value) < 2.0^-128 */
8989 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8990 if (round_to_inf(fpst, f32_sbit)) {
8991 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8992 } else {
8993 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
8994 }
8995 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
8996 float_raise(float_flag_underflow, fpst);
8997 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
8998 }
8999
fe0e4872 9000
b6d4443a
AB
9001 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
9002 r64 = call_recip_estimate(f64, 253, fpst);
9003 r64_val = float64_val(r64);
9004 r64_exp = extract64(r64_val, 52, 11);
9005 r64_frac = extract64(r64_val, 0, 52);
9006
9007 /* result = sign : result_exp<7:0> : fraction<51:29>; */
9008 return make_float32(f32_sbit |
9009 (r64_exp & 0xff) << 23 |
9010 extract64(r64_frac, 29, 24));
9011}
9012
9013float64 HELPER(recpe_f64)(float64 input, void *fpstp)
9014{
9015 float_status *fpst = fpstp;
9016 float64 f64 = float64_squash_input_denormal(input, fpst);
9017 uint64_t f64_val = float64_val(f64);
9018 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
9019 int64_t f64_exp = extract64(f64_val, 52, 11);
9020 float64 r64;
9021 uint64_t r64_val;
9022 int64_t r64_exp;
9023 uint64_t r64_frac;
9024
9025 /* Deal with any special cases */
9026 if (float64_is_any_nan(f64)) {
9027 float64 nan = f64;
9028 if (float64_is_signaling_nan(f64)) {
9029 float_raise(float_flag_invalid, fpst);
9030 nan = float64_maybe_silence_nan(f64);
9031 }
9032 if (fpst->default_nan_mode) {
9033 nan = float64_default_nan;
9034 }
9035 return nan;
9036 } else if (float64_is_infinity(f64)) {
9037 return float64_set_sign(float64_zero, float64_is_neg(f64));
9038 } else if (float64_is_zero(f64)) {
9039 float_raise(float_flag_divbyzero, fpst);
9040 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9041 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
9042 /* Abs(value) < 2.0^-1024 */
9043 float_raise(float_flag_overflow | float_flag_inexact, fpst);
9044 if (round_to_inf(fpst, f64_sbit)) {
9045 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9046 } else {
9047 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
9048 }
fc1792e9 9049 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
9050 float_raise(float_flag_underflow, fpst);
9051 return float64_set_sign(float64_zero, float64_is_neg(f64));
9052 }
fe0e4872 9053
b6d4443a
AB
9054 r64 = call_recip_estimate(f64, 2045, fpst);
9055 r64_val = float64_val(r64);
9056 r64_exp = extract64(r64_val, 52, 11);
9057 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 9058
b6d4443a
AB
9059 /* result = sign : result_exp<10:0> : fraction<51:0> */
9060 return make_float64(f64_sbit |
9061 ((r64_exp & 0x7ff) << 52) |
9062 r64_frac);
4373f3ce
PB
9063}
9064
e07be5d2
CL
9065/* The algorithm that must be used to calculate the estimate
9066 * is specified by the ARM ARM.
9067 */
c2fb418e 9068static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 9069{
1146a817
PM
9070 /* These calculations mustn't set any fp exception flags,
9071 * so we use a local copy of the fp_status.
9072 */
c2fb418e 9073 float_status dummy_status = *real_fp_status;
1146a817 9074 float_status *s = &dummy_status;
e07be5d2
CL
9075 float64 q;
9076 int64_t q_int;
9077
9078 if (float64_lt(a, float64_half, s)) {
9079 /* range 0.25 <= a < 0.5 */
9080
9081 /* a in units of 1/512 rounded down */
9082 /* q0 = (int)(a * 512.0); */
9083 q = float64_mul(float64_512, a, s);
9084 q_int = float64_to_int64_round_to_zero(q, s);
9085
9086 /* reciprocal root r */
9087 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
9088 q = int64_to_float64(q_int, s);
9089 q = float64_add(q, float64_half, s);
9090 q = float64_div(q, float64_512, s);
9091 q = float64_sqrt(q, s);
9092 q = float64_div(float64_one, q, s);
9093 } else {
9094 /* range 0.5 <= a < 1.0 */
9095
9096 /* a in units of 1/256 rounded down */
9097 /* q1 = (int)(a * 256.0); */
9098 q = float64_mul(float64_256, a, s);
9099 int64_t q_int = float64_to_int64_round_to_zero(q, s);
9100
9101 /* reciprocal root r */
9102 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
9103 q = int64_to_float64(q_int, s);
9104 q = float64_add(q, float64_half, s);
9105 q = float64_div(q, float64_256, s);
9106 q = float64_sqrt(q, s);
9107 q = float64_div(float64_one, q, s);
9108 }
9109 /* r in units of 1/256 rounded to nearest */
9110 /* s = (int)(256.0 * r + 0.5); */
9111
9112 q = float64_mul(q, float64_256,s );
9113 q = float64_add(q, float64_half, s);
9114 q_int = float64_to_int64_round_to_zero(q, s);
9115
9116 /* return (double)s / 256.0;*/
9117 return float64_div(int64_to_float64(q_int, s), float64_256, s);
9118}
9119
c2fb418e 9120float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 9121{
c2fb418e
AB
9122 float_status *s = fpstp;
9123 float32 f32 = float32_squash_input_denormal(input, s);
9124 uint32_t val = float32_val(f32);
9125 uint32_t f32_sbit = 0x80000000 & val;
9126 int32_t f32_exp = extract32(val, 23, 8);
9127 uint32_t f32_frac = extract32(val, 0, 23);
9128 uint64_t f64_frac;
9129 uint64_t val64;
e07be5d2
CL
9130 int result_exp;
9131 float64 f64;
e07be5d2 9132
c2fb418e
AB
9133 if (float32_is_any_nan(f32)) {
9134 float32 nan = f32;
9135 if (float32_is_signaling_nan(f32)) {
e07be5d2 9136 float_raise(float_flag_invalid, s);
c2fb418e 9137 nan = float32_maybe_silence_nan(f32);
e07be5d2 9138 }
c2fb418e
AB
9139 if (s->default_nan_mode) {
9140 nan = float32_default_nan;
43fe9bdb 9141 }
c2fb418e
AB
9142 return nan;
9143 } else if (float32_is_zero(f32)) {
e07be5d2 9144 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
9145 return float32_set_sign(float32_infinity, float32_is_neg(f32));
9146 } else if (float32_is_neg(f32)) {
e07be5d2
CL
9147 float_raise(float_flag_invalid, s);
9148 return float32_default_nan;
c2fb418e 9149 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
9150 return float32_zero;
9151 }
9152
c2fb418e 9153 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 9154 * preserving the parity of the exponent. */
c2fb418e
AB
9155
9156 f64_frac = ((uint64_t) f32_frac) << 29;
9157 if (f32_exp == 0) {
9158 while (extract64(f64_frac, 51, 1) == 0) {
9159 f64_frac = f64_frac << 1;
9160 f32_exp = f32_exp-1;
9161 }
9162 f64_frac = extract64(f64_frac, 0, 51) << 1;
9163 }
9164
9165 if (extract64(f32_exp, 0, 1) == 0) {
9166 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 9167 | (0x3feULL << 52)
c2fb418e 9168 | f64_frac);
e07be5d2 9169 } else {
c2fb418e 9170 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 9171 | (0x3fdULL << 52)
c2fb418e 9172 | f64_frac);
e07be5d2
CL
9173 }
9174
c2fb418e 9175 result_exp = (380 - f32_exp) / 2;
e07be5d2 9176
c2fb418e 9177 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
9178
9179 val64 = float64_val(f64);
9180
26cc6abf 9181 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
9182 | ((val64 >> 29) & 0x7fffff);
9183 return make_float32(val);
4373f3ce
PB
9184}
9185
c2fb418e
AB
9186float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
9187{
9188 float_status *s = fpstp;
9189 float64 f64 = float64_squash_input_denormal(input, s);
9190 uint64_t val = float64_val(f64);
9191 uint64_t f64_sbit = 0x8000000000000000ULL & val;
9192 int64_t f64_exp = extract64(val, 52, 11);
9193 uint64_t f64_frac = extract64(val, 0, 52);
9194 int64_t result_exp;
9195 uint64_t result_frac;
9196
9197 if (float64_is_any_nan(f64)) {
9198 float64 nan = f64;
9199 if (float64_is_signaling_nan(f64)) {
9200 float_raise(float_flag_invalid, s);
9201 nan = float64_maybe_silence_nan(f64);
9202 }
9203 if (s->default_nan_mode) {
9204 nan = float64_default_nan;
9205 }
9206 return nan;
9207 } else if (float64_is_zero(f64)) {
9208 float_raise(float_flag_divbyzero, s);
9209 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9210 } else if (float64_is_neg(f64)) {
9211 float_raise(float_flag_invalid, s);
9212 return float64_default_nan;
9213 } else if (float64_is_infinity(f64)) {
9214 return float64_zero;
9215 }
9216
9217 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
9218 * preserving the parity of the exponent. */
9219
9220 if (f64_exp == 0) {
9221 while (extract64(f64_frac, 51, 1) == 0) {
9222 f64_frac = f64_frac << 1;
9223 f64_exp = f64_exp - 1;
9224 }
9225 f64_frac = extract64(f64_frac, 0, 51) << 1;
9226 }
9227
9228 if (extract64(f64_exp, 0, 1) == 0) {
9229 f64 = make_float64(f64_sbit
9230 | (0x3feULL << 52)
9231 | f64_frac);
9232 } else {
9233 f64 = make_float64(f64_sbit
9234 | (0x3fdULL << 52)
9235 | f64_frac);
9236 }
9237
9238 result_exp = (3068 - f64_exp) / 2;
9239
9240 f64 = recip_sqrt_estimate(f64, s);
9241
9242 result_frac = extract64(float64_val(f64), 0, 52);
9243
9244 return make_float64(f64_sbit |
9245 ((result_exp & 0x7ff) << 52) |
9246 result_frac);
9247}
9248
b6d4443a 9249uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 9250{
b6d4443a 9251 float_status *s = fpstp;
fe0e4872
CL
9252 float64 f64;
9253
9254 if ((a & 0x80000000) == 0) {
9255 return 0xffffffff;
9256 }
9257
9258 f64 = make_float64((0x3feULL << 52)
9259 | ((int64_t)(a & 0x7fffffff) << 21));
9260
b6d4443a 9261 f64 = recip_estimate(f64, s);
fe0e4872
CL
9262
9263 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
9264}
9265
c2fb418e 9266uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 9267{
c2fb418e 9268 float_status *fpst = fpstp;
e07be5d2
CL
9269 float64 f64;
9270
9271 if ((a & 0xc0000000) == 0) {
9272 return 0xffffffff;
9273 }
9274
9275 if (a & 0x80000000) {
9276 f64 = make_float64((0x3feULL << 52)
9277 | ((uint64_t)(a & 0x7fffffff) << 21));
9278 } else { /* bits 31-30 == '01' */
9279 f64 = make_float64((0x3fdULL << 52)
9280 | ((uint64_t)(a & 0x3fffffff) << 22));
9281 }
9282
c2fb418e 9283 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
9284
9285 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 9286}
fe1479c3 9287
da97f52c
PM
9288/* VFPv4 fused multiply-accumulate */
9289float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
9290{
9291 float_status *fpst = fpstp;
9292 return float32_muladd(a, b, c, 0, fpst);
9293}
9294
9295float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
9296{
9297 float_status *fpst = fpstp;
9298 return float64_muladd(a, b, c, 0, fpst);
9299}
d9b0848d
PM
9300
9301/* ARMv8 round to integral */
9302float32 HELPER(rints_exact)(float32 x, void *fp_status)
9303{
9304 return float32_round_to_int(x, fp_status);
9305}
9306
9307float64 HELPER(rintd_exact)(float64 x, void *fp_status)
9308{
9309 return float64_round_to_int(x, fp_status);
9310}
9311
9312float32 HELPER(rints)(float32 x, void *fp_status)
9313{
9314 int old_flags = get_float_exception_flags(fp_status), new_flags;
9315 float32 ret;
9316
9317 ret = float32_round_to_int(x, fp_status);
9318
9319 /* Suppress any inexact exceptions the conversion produced */
9320 if (!(old_flags & float_flag_inexact)) {
9321 new_flags = get_float_exception_flags(fp_status);
9322 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9323 }
9324
9325 return ret;
9326}
9327
9328float64 HELPER(rintd)(float64 x, void *fp_status)
9329{
9330 int old_flags = get_float_exception_flags(fp_status), new_flags;
9331 float64 ret;
9332
9333 ret = float64_round_to_int(x, fp_status);
9334
9335 new_flags = get_float_exception_flags(fp_status);
9336
9337 /* Suppress any inexact exceptions the conversion produced */
9338 if (!(old_flags & float_flag_inexact)) {
9339 new_flags = get_float_exception_flags(fp_status);
9340 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9341 }
9342
9343 return ret;
9344}
9972da66
WN
9345
9346/* Convert ARM rounding mode to softfloat */
9347int arm_rmode_to_sf(int rmode)
9348{
9349 switch (rmode) {
9350 case FPROUNDING_TIEAWAY:
9351 rmode = float_round_ties_away;
9352 break;
9353 case FPROUNDING_ODD:
9354 /* FIXME: add support for TIEAWAY and ODD */
9355 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
9356 rmode);
9357 case FPROUNDING_TIEEVEN:
9358 default:
9359 rmode = float_round_nearest_even;
9360 break;
9361 case FPROUNDING_POSINF:
9362 rmode = float_round_up;
9363 break;
9364 case FPROUNDING_NEGINF:
9365 rmode = float_round_down;
9366 break;
9367 case FPROUNDING_ZERO:
9368 rmode = float_round_to_zero;
9369 break;
9370 }
9371 return rmode;
9372}
eb0ecd5a 9373
aa633469
PM
9374/* CRC helpers.
9375 * The upper bytes of val (above the number specified by 'bytes') must have
9376 * been zeroed out by the caller.
9377 */
eb0ecd5a
WN
9378uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
9379{
9380 uint8_t buf[4];
9381
aa633469 9382 stl_le_p(buf, val);
eb0ecd5a
WN
9383
9384 /* zlib crc32 converts the accumulator and output to one's complement. */
9385 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
9386}
9387
9388uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
9389{
9390 uint8_t buf[4];
9391
aa633469 9392 stl_le_p(buf, val);
eb0ecd5a
WN
9393
9394 /* Linux crc32c converts the output to one's complement. */
9395 return crc32c(acc, buf, bytes) ^ 0xffffffff;
9396}
This page took 2.409648 seconds and 4 git commands to generate.