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