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