]> git.proxmox.com Git - mirror_qemu.git/blame - target-arm/helper.c
target-arm: A64: Generalize update_spsel for the various ELs
[mirror_qemu.git] / target-arm / helper.c
CommitLineData
b5ff1b31 1#include "cpu.h"
ccd38087 2#include "internals.h"
022c62cb 3#include "exec/gdbstub.h"
7b59220e 4#include "helper.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
WN
9#include "qemu/crc32c.h"
10#include <zlib.h> /* For crc32 */
0b03bdfc 11
4a501606 12#ifndef CONFIG_USER_ONLY
aca3f40b
PM
13#include "exec/softmmu_exec.h"
14
2c8dd318 15static inline int get_phys_addr(CPUARMState *env, target_ulong address,
4a501606 16 int access_type, int is_user,
a8170e5e 17 hwaddr *phys_ptr, int *prot,
4a501606 18 target_ulong *page_size);
7c2cb42b
AF
19
20/* Definitions for the PMCCNTR and PMCR registers */
21#define PMCRD 0x8
22#define PMCRC 0x4
23#define PMCRE 0x1
4a501606
PM
24#endif
25
0ecb72a5 26static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
27{
28 int nregs;
29
30 /* VFP data registers are always little-endian. */
31 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
32 if (reg < nregs) {
33 stfq_le_p(buf, env->vfp.regs[reg]);
34 return 8;
35 }
36 if (arm_feature(env, ARM_FEATURE_NEON)) {
37 /* Aliases for Q regs. */
38 nregs += 16;
39 if (reg < nregs) {
40 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
41 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
42 return 16;
43 }
44 }
45 switch (reg - nregs) {
46 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
47 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
48 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
49 }
50 return 0;
51}
52
0ecb72a5 53static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
54{
55 int nregs;
56
57 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
58 if (reg < nregs) {
59 env->vfp.regs[reg] = ldfq_le_p(buf);
60 return 8;
61 }
62 if (arm_feature(env, ARM_FEATURE_NEON)) {
63 nregs += 16;
64 if (reg < nregs) {
65 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
66 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
67 return 16;
68 }
69 }
70 switch (reg - nregs) {
71 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
72 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 73 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
74 }
75 return 0;
76}
77
6a669427
PM
78static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
79{
80 switch (reg) {
81 case 0 ... 31:
82 /* 128 bit FP register */
83 stfq_le_p(buf, env->vfp.regs[reg * 2]);
84 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
85 return 16;
86 case 32:
87 /* FPSR */
88 stl_p(buf, vfp_get_fpsr(env));
89 return 4;
90 case 33:
91 /* FPCR */
92 stl_p(buf, vfp_get_fpcr(env));
93 return 4;
94 default:
95 return 0;
96 }
97}
98
99static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
100{
101 switch (reg) {
102 case 0 ... 31:
103 /* 128 bit FP register */
104 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
105 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
106 return 16;
107 case 32:
108 /* FPSR */
109 vfp_set_fpsr(env, ldl_p(buf));
110 return 4;
111 case 33:
112 /* FPCR */
113 vfp_set_fpcr(env, ldl_p(buf));
114 return 4;
115 default:
116 return 0;
117 }
118}
119
c4241c7d 120static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 121{
67ed771d 122 if (cpreg_field_is_64bit(ri)) {
c4241c7d 123 return CPREG_FIELD64(env, ri);
22d9e1a9 124 } else {
c4241c7d 125 return CPREG_FIELD32(env, ri);
22d9e1a9 126 }
d4e6df63
PM
127}
128
c4241c7d
PM
129static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
130 uint64_t value)
d4e6df63 131{
67ed771d 132 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
133 CPREG_FIELD64(env, ri) = value;
134 } else {
135 CPREG_FIELD32(env, ri) = value;
136 }
d4e6df63
PM
137}
138
59a1c327 139static uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 140{
59a1c327 141 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 142 if (ri->type & ARM_CP_CONST) {
59a1c327 143 return ri->resetvalue;
721fae12 144 } else if (ri->raw_readfn) {
59a1c327 145 return ri->raw_readfn(env, ri);
721fae12 146 } else if (ri->readfn) {
59a1c327 147 return ri->readfn(env, ri);
721fae12 148 } else {
59a1c327 149 return raw_read(env, ri);
721fae12 150 }
721fae12
PM
151}
152
59a1c327 153static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 154 uint64_t v)
721fae12
PM
155{
156 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
157 * Note that constant registers are treated as write-ignored; the
158 * caller should check for success by whether a readback gives the
159 * value written.
160 */
161 if (ri->type & ARM_CP_CONST) {
59a1c327 162 return;
721fae12 163 } else if (ri->raw_writefn) {
c4241c7d 164 ri->raw_writefn(env, ri, v);
721fae12 165 } else if (ri->writefn) {
c4241c7d 166 ri->writefn(env, ri, v);
721fae12 167 } else {
afb2530f 168 raw_write(env, ri, v);
721fae12 169 }
721fae12
PM
170}
171
172bool write_cpustate_to_list(ARMCPU *cpu)
173{
174 /* Write the coprocessor state from cpu->env to the (index,value) list. */
175 int i;
176 bool ok = true;
177
178 for (i = 0; i < cpu->cpreg_array_len; i++) {
179 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
180 const ARMCPRegInfo *ri;
59a1c327 181
60322b39 182 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
183 if (!ri) {
184 ok = false;
185 continue;
186 }
187 if (ri->type & ARM_CP_NO_MIGRATE) {
188 continue;
189 }
59a1c327 190 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
191 }
192 return ok;
193}
194
195bool write_list_to_cpustate(ARMCPU *cpu)
196{
197 int i;
198 bool ok = true;
199
200 for (i = 0; i < cpu->cpreg_array_len; i++) {
201 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
202 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
203 const ARMCPRegInfo *ri;
204
60322b39 205 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
206 if (!ri) {
207 ok = false;
208 continue;
209 }
210 if (ri->type & ARM_CP_NO_MIGRATE) {
211 continue;
212 }
213 /* Write value and confirm it reads back as written
214 * (to catch read-only registers and partially read-only
215 * registers where the incoming migration value doesn't match)
216 */
59a1c327
PM
217 write_raw_cp_reg(&cpu->env, ri, v);
218 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
219 ok = false;
220 }
221 }
222 return ok;
223}
224
225static void add_cpreg_to_list(gpointer key, gpointer opaque)
226{
227 ARMCPU *cpu = opaque;
228 uint64_t regidx;
229 const ARMCPRegInfo *ri;
230
231 regidx = *(uint32_t *)key;
60322b39 232 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
233
234 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
235 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
236 /* The value array need not be initialized at this point */
237 cpu->cpreg_array_len++;
238 }
239}
240
241static void count_cpreg(gpointer key, gpointer opaque)
242{
243 ARMCPU *cpu = opaque;
244 uint64_t regidx;
245 const ARMCPRegInfo *ri;
246
247 regidx = *(uint32_t *)key;
60322b39 248 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
249
250 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
251 cpu->cpreg_array_len++;
252 }
253}
254
255static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
256{
cbf239b7
AR
257 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
258 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 259
cbf239b7
AR
260 if (aidx > bidx) {
261 return 1;
262 }
263 if (aidx < bidx) {
264 return -1;
265 }
266 return 0;
721fae12
PM
267}
268
82a3a118
PM
269static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
270{
271 GList **plist = udata;
272
273 *plist = g_list_prepend(*plist, key);
274}
275
721fae12
PM
276void init_cpreg_list(ARMCPU *cpu)
277{
278 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
279 * Note that we require cpreg_tuples[] to be sorted by key ID.
280 */
82a3a118 281 GList *keys = NULL;
721fae12
PM
282 int arraylen;
283
82a3a118
PM
284 g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
285
721fae12
PM
286 keys = g_list_sort(keys, cpreg_key_compare);
287
288 cpu->cpreg_array_len = 0;
289
290 g_list_foreach(keys, count_cpreg, cpu);
291
292 arraylen = cpu->cpreg_array_len;
293 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
294 cpu->cpreg_values = g_new(uint64_t, arraylen);
295 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
296 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
297 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
298 cpu->cpreg_array_len = 0;
299
300 g_list_foreach(keys, add_cpreg_to_list, cpu);
301
302 assert(cpu->cpreg_array_len == arraylen);
303
304 g_list_free(keys);
305}
306
014406b5
PM
307/* Return true if extended addresses are enabled.
308 * This is always the case if our translation regime is 64 bit,
309 * but depends on TTBCR.EAE for 32 bit.
310 */
311static inline bool extended_addresses_enabled(CPUARMState *env)
312{
313 return arm_el_is_aa64(env, 1)
314 || ((arm_feature(env, ARM_FEATURE_LPAE)
315 && (env->cp15.c2_control & (1U << 31))));
316}
317
c4241c7d 318static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 319{
00c8cb0a
AF
320 ARMCPU *cpu = arm_env_get_cpu(env);
321
c983fe6c 322 env->cp15.c3 = value;
00c8cb0a 323 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
324}
325
c4241c7d 326static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 327{
00c8cb0a
AF
328 ARMCPU *cpu = arm_env_get_cpu(env);
329
08de207b
PM
330 if (env->cp15.c13_fcse != value) {
331 /* Unlike real hardware the qemu TLB uses virtual addresses,
332 * not modified virtual addresses, so this causes a TLB flush.
333 */
00c8cb0a 334 tlb_flush(CPU(cpu), 1);
08de207b
PM
335 env->cp15.c13_fcse = value;
336 }
08de207b 337}
c4241c7d
PM
338
339static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
340 uint64_t value)
08de207b 341{
00c8cb0a
AF
342 ARMCPU *cpu = arm_env_get_cpu(env);
343
014406b5
PM
344 if (env->cp15.contextidr_el1 != value && !arm_feature(env, ARM_FEATURE_MPU)
345 && !extended_addresses_enabled(env)) {
08de207b
PM
346 /* For VMSA (when not using the LPAE long descriptor page table
347 * format) this register includes the ASID, so do a TLB flush.
348 * For PMSA it is purely a process ID and no action is needed.
349 */
00c8cb0a 350 tlb_flush(CPU(cpu), 1);
08de207b 351 }
014406b5 352 env->cp15.contextidr_el1 = value;
08de207b
PM
353}
354
c4241c7d
PM
355static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
356 uint64_t value)
d929823f
PM
357{
358 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
359 ARMCPU *cpu = arm_env_get_cpu(env);
360
361 tlb_flush(CPU(cpu), 1);
d929823f
PM
362}
363
c4241c7d
PM
364static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
365 uint64_t value)
d929823f
PM
366{
367 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
368 ARMCPU *cpu = arm_env_get_cpu(env);
369
370 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
371}
372
c4241c7d
PM
373static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
374 uint64_t value)
d929823f
PM
375{
376 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
377 ARMCPU *cpu = arm_env_get_cpu(env);
378
379 tlb_flush(CPU(cpu), value == 0);
d929823f
PM
380}
381
c4241c7d
PM
382static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
383 uint64_t value)
d929823f
PM
384{
385 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
386 ARMCPU *cpu = arm_env_get_cpu(env);
387
388 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
389}
390
e9aa6c21
PM
391static const ARMCPRegInfo cp_reginfo[] = {
392 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
393 * version" bits will read as a reserved value, which should cause
394 * Linux to not try to use the debug hardware.
395 */
396 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
397 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
08de207b
PM
398 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
399 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
d4e6df63 400 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
014406b5
PM
401 { .name = "CONTEXTIDR", .state = ARM_CP_STATE_BOTH,
402 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
403 .access = PL1_RW,
404 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el1),
d4e6df63 405 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
406 REGINFO_SENTINEL
407};
408
409static const ARMCPRegInfo not_v8_cp_reginfo[] = {
410 /* NB: Some of these registers exist in v8 but with more precise
411 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
412 */
413 /* MMU Domain access control / MPU write buffer control */
414 { .name = "DACR", .cp = 15,
415 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
416 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
417 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
4fdd17dd
PM
418 /* ??? This covers not just the impdef TLB lockdown registers but also
419 * some v7VMSA registers relating to TEX remap, so it is overly broad.
420 */
421 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
422 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
d929823f
PM
423 /* MMU TLB control. Note that the wildcarding means we cover not just
424 * the unified TLB ops but also the dside/iside/inner-shareable variants.
425 */
426 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
d4e6df63
PM
427 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
428 .type = ARM_CP_NO_MIGRATE },
d929823f 429 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
d4e6df63
PM
430 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
431 .type = ARM_CP_NO_MIGRATE },
d929823f 432 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
d4e6df63
PM
433 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
434 .type = ARM_CP_NO_MIGRATE },
d929823f 435 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
d4e6df63
PM
436 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
437 .type = ARM_CP_NO_MIGRATE },
c4804214
PM
438 /* Cache maintenance ops; some of this space may be overridden later. */
439 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
440 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
441 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
442 REGINFO_SENTINEL
443};
444
7d57f408
PM
445static const ARMCPRegInfo not_v6_cp_reginfo[] = {
446 /* Not all pre-v6 cores implemented this WFI, so this is slightly
447 * over-broad.
448 */
449 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
450 .access = PL1_W, .type = ARM_CP_WFI },
451 REGINFO_SENTINEL
452};
453
454static const ARMCPRegInfo not_v7_cp_reginfo[] = {
455 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
456 * is UNPREDICTABLE; we choose to NOP as most implementations do).
457 */
458 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
459 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
460 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
461 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
462 * OMAPCP will override this space.
463 */
464 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
465 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
466 .resetvalue = 0 },
467 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
468 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
469 .resetvalue = 0 },
776d4e5c
PM
470 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
471 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
d4e6df63
PM
472 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
473 .resetvalue = 0 },
7d57f408
PM
474 REGINFO_SENTINEL
475};
476
c4241c7d
PM
477static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
478 uint64_t value)
2771db27 479{
f0aff255
FA
480 uint32_t mask = 0;
481
482 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
483 if (!arm_feature(env, ARM_FEATURE_V8)) {
484 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
485 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
486 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
487 */
488 if (arm_feature(env, ARM_FEATURE_VFP)) {
489 /* VFP coprocessor: cp10 & cp11 [23:20] */
490 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
491
492 if (!arm_feature(env, ARM_FEATURE_NEON)) {
493 /* ASEDIS [31] bit is RAO/WI */
494 value |= (1 << 31);
495 }
496
497 /* VFPv3 and upwards with NEON implement 32 double precision
498 * registers (D0-D31).
499 */
500 if (!arm_feature(env, ARM_FEATURE_NEON) ||
501 !arm_feature(env, ARM_FEATURE_VFP3)) {
502 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
503 value |= (1 << 30);
504 }
505 }
506 value &= mask;
2771db27 507 }
f0aff255 508 env->cp15.c1_coproc = value;
2771db27
PM
509}
510
7d57f408
PM
511static const ARMCPRegInfo v6_cp_reginfo[] = {
512 /* prefetch by MVA in v6, NOP in v7 */
513 { .name = "MVA_prefetch",
514 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
515 .access = PL1_W, .type = ARM_CP_NOP },
516 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
517 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 518 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 519 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 520 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 521 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 522 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264
RH
523 .access = PL1_RW,
524 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el1),
06d76f31
PM
525 .resetvalue = 0, },
526 /* Watchpoint Fault Address Register : should actually only be present
527 * for 1136, 1176, 11MPCore.
528 */
529 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
530 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8
PM
531 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
532 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
2771db27
PM
533 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
534 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
535 REGINFO_SENTINEL
536};
537
fcd25206 538static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
200ac0ef 539{
3b163b01 540 /* Performance monitor registers user accessibility is controlled
fcd25206 541 * by PMUSERENR.
200ac0ef
PM
542 */
543 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
fcd25206 544 return CP_ACCESS_TRAP;
200ac0ef 545 }
fcd25206 546 return CP_ACCESS_OK;
200ac0ef
PM
547}
548
7c2cb42b 549#ifndef CONFIG_USER_ONLY
c4241c7d
PM
550static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
551 uint64_t value)
200ac0ef 552{
7c2cb42b
AF
553 /* Don't computer the number of ticks in user mode */
554 uint32_t temp_ticks;
555
556 temp_ticks = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) *
557 get_ticks_per_sec() / 1000000;
558
559 if (env->cp15.c9_pmcr & PMCRE) {
560 /* If the counter is enabled */
561 if (env->cp15.c9_pmcr & PMCRD) {
562 /* Increment once every 64 processor clock cycles */
563 env->cp15.c15_ccnt = (temp_ticks/64) - env->cp15.c15_ccnt;
564 } else {
565 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
566 }
567 }
568
569 if (value & PMCRC) {
570 /* The counter has been reset */
571 env->cp15.c15_ccnt = 0;
572 }
573
200ac0ef
PM
574 /* only the DP, X, D and E bits are writable */
575 env->cp15.c9_pmcr &= ~0x39;
576 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b
AF
577
578 if (env->cp15.c9_pmcr & PMCRE) {
579 if (env->cp15.c9_pmcr & PMCRD) {
580 /* Increment once every 64 processor clock cycles */
581 temp_ticks /= 64;
582 }
583 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
584 }
585}
586
587static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
588{
589 uint32_t total_ticks;
590
591 if (!(env->cp15.c9_pmcr & PMCRE)) {
592 /* Counter is disabled, do not change value */
593 return env->cp15.c15_ccnt;
594 }
595
596 total_ticks = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) *
597 get_ticks_per_sec() / 1000000;
598
599 if (env->cp15.c9_pmcr & PMCRD) {
600 /* Increment once every 64 processor clock cycles */
601 total_ticks /= 64;
602 }
603 return total_ticks - env->cp15.c15_ccnt;
604}
605
606static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
607 uint64_t value)
608{
609 uint32_t total_ticks;
610
611 if (!(env->cp15.c9_pmcr & PMCRE)) {
612 /* Counter is disabled, set the absolute value */
613 env->cp15.c15_ccnt = value;
614 return;
615 }
616
617 total_ticks = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) *
618 get_ticks_per_sec() / 1000000;
619
620 if (env->cp15.c9_pmcr & PMCRD) {
621 /* Increment once every 64 processor clock cycles */
622 total_ticks /= 64;
623 }
624 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 625}
7c2cb42b 626#endif
200ac0ef 627
c4241c7d 628static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
629 uint64_t value)
630{
200ac0ef
PM
631 value &= (1 << 31);
632 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
633}
634
c4241c7d
PM
635static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
636 uint64_t value)
200ac0ef 637{
200ac0ef
PM
638 value &= (1 << 31);
639 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
640}
641
c4241c7d
PM
642static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
643 uint64_t value)
200ac0ef 644{
200ac0ef 645 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
646}
647
c4241c7d
PM
648static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
649 uint64_t value)
200ac0ef 650{
200ac0ef 651 env->cp15.c9_pmxevtyper = value & 0xff;
200ac0ef
PM
652}
653
c4241c7d 654static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
655 uint64_t value)
656{
657 env->cp15.c9_pmuserenr = value & 1;
200ac0ef
PM
658}
659
c4241c7d
PM
660static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
661 uint64_t value)
200ac0ef
PM
662{
663 /* We have no event counters so only the C bit can be changed */
664 value &= (1 << 31);
665 env->cp15.c9_pminten |= value;
200ac0ef
PM
666}
667
c4241c7d
PM
668static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
669 uint64_t value)
200ac0ef
PM
670{
671 value &= (1 << 31);
672 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
673}
674
c4241c7d
PM
675static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
676 uint64_t value)
8641136c 677{
a505d7fe
PM
678 /* Note that even though the AArch64 view of this register has bits
679 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
680 * architectural requirements for bits which are RES0 only in some
681 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
682 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
683 */
68fdb6c5 684 env->cp15.vbar_el[1] = value & ~0x1FULL;
8641136c
NR
685}
686
c4241c7d 687static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
688{
689 ARMCPU *cpu = arm_env_get_cpu(env);
c4241c7d 690 return cpu->ccsidr[env->cp15.c0_cssel];
776d4e5c
PM
691}
692
c4241c7d
PM
693static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
694 uint64_t value)
776d4e5c
PM
695{
696 env->cp15.c0_cssel = value & 0xf;
776d4e5c
PM
697}
698
1090b9c6
PM
699static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
700{
701 CPUState *cs = ENV_GET_CPU(env);
702 uint64_t ret = 0;
703
704 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
705 ret |= CPSR_I;
706 }
707 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
708 ret |= CPSR_F;
709 }
710 /* External aborts are not possible in QEMU so A bit is always clear */
711 return ret;
712}
713
e9aa6c21
PM
714static const ARMCPRegInfo v7_cp_reginfo[] = {
715 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
716 * debug components
717 */
718 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
719 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
091fd17c 720 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
e9aa6c21 721 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7d57f408
PM
722 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
723 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
724 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
725 /* Performance monitors are implementation defined in v7,
726 * but with an ARM recommended set of registers, which we
727 * follow (although we don't actually implement any counters)
728 *
729 * Performance registers fall into three categories:
730 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
731 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
732 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
733 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
734 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
735 */
736 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
737 .access = PL0_RW, .resetvalue = 0,
738 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
739 .writefn = pmcntenset_write,
740 .accessfn = pmreg_access,
741 .raw_writefn = raw_write },
200ac0ef
PM
742 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
743 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
744 .accessfn = pmreg_access,
745 .writefn = pmcntenclr_write,
d4e6df63 746 .type = ARM_CP_NO_MIGRATE },
200ac0ef
PM
747 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
748 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
749 .accessfn = pmreg_access,
750 .writefn = pmovsr_write,
751 .raw_writefn = raw_write },
752 /* Unimplemented so WI. */
200ac0ef 753 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
fcd25206 754 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
200ac0ef 755 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
fcd25206 756 * We choose to RAZ/WI.
200ac0ef
PM
757 */
758 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
fcd25206
PM
759 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
760 .accessfn = pmreg_access },
7c2cb42b 761#ifndef CONFIG_USER_ONLY
200ac0ef 762 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b
AF
763 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
764 .readfn = pmccntr_read, .writefn = pmccntr_write,
fcd25206 765 .accessfn = pmreg_access },
7c2cb42b 766#endif
200ac0ef
PM
767 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
768 .access = PL0_RW,
769 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
fcd25206
PM
770 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
771 .raw_writefn = raw_write },
772 /* Unimplemented, RAZ/WI. */
200ac0ef 773 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206
PM
774 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
775 .accessfn = pmreg_access },
200ac0ef
PM
776 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
777 .access = PL0_R | PL1_RW,
778 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
779 .resetvalue = 0,
d4e6df63 780 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef
PM
781 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
782 .access = PL1_RW,
783 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
784 .resetvalue = 0,
d4e6df63 785 .writefn = pmintenset_write, .raw_writefn = raw_write },
200ac0ef 786 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
d4e6df63 787 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
200ac0ef 788 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
d4e6df63 789 .resetvalue = 0, .writefn = pmintenclr_write, },
a505d7fe
PM
790 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
791 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8641136c 792 .access = PL1_RW, .writefn = vbar_write,
68fdb6c5 793 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[1]),
8641136c 794 .resetvalue = 0 },
2771db27
PM
795 { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
796 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
797 .resetvalue = 0, },
7da845b0
PM
798 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
799 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
d4e6df63 800 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
7da845b0
PM
801 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
802 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
776d4e5c
PM
803 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
804 .writefn = csselr_write, .resetvalue = 0 },
805 /* Auxiliary ID register: this actually has an IMPDEF value but for now
806 * just RAZ for all cores:
807 */
0ff644a7
PM
808 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
809 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 810 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
811 /* Auxiliary fault status registers: these also are IMPDEF, and we
812 * choose to RAZ/WI for all cores.
813 */
814 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
815 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
816 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
817 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
818 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
819 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
820 /* MAIR can just read-as-written because we don't implement caches
821 * and so don't need to care about memory attributes.
822 */
823 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
824 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
825 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el1),
826 .resetvalue = 0 },
827 /* For non-long-descriptor page tables these are PRRR and NMRR;
828 * regardless they still act as reads-as-written for QEMU.
829 * The override is necessary because of the overly-broad TLB_LOCKDOWN
830 * definition.
831 */
832 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
833 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
834 .fieldoffset = offsetoflow32(CPUARMState, cp15.mair_el1),
835 .resetfn = arm_cp_reset_ignore },
836 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
837 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
838 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el1),
839 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
840 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
841 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
842 .type = ARM_CP_NO_MIGRATE, .access = PL1_R, .readfn = isr_read },
e9aa6c21
PM
843 REGINFO_SENTINEL
844};
845
c4241c7d
PM
846static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
847 uint64_t value)
c326b979
PM
848{
849 value &= 1;
850 env->teecr = value;
c326b979
PM
851}
852
c4241c7d 853static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
c326b979 854{
c326b979 855 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
92611c00 856 return CP_ACCESS_TRAP;
c326b979 857 }
92611c00 858 return CP_ACCESS_OK;
c326b979
PM
859}
860
861static const ARMCPRegInfo t2ee_cp_reginfo[] = {
862 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
863 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
864 .resetvalue = 0,
865 .writefn = teecr_write },
866 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
867 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 868 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
869 REGINFO_SENTINEL
870};
871
4d31c596 872static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
873 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
874 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
875 .access = PL0_RW,
876 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el0), .resetvalue = 0 },
4d31c596
PM
877 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
878 .access = PL0_RW,
e4fe830b
PM
879 .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidr_el0),
880 .resetfn = arm_cp_reset_ignore },
881 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
882 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
883 .access = PL0_R|PL1_W,
884 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el0), .resetvalue = 0 },
4d31c596
PM
885 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
886 .access = PL0_R|PL1_W,
e4fe830b
PM
887 .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidrro_el0),
888 .resetfn = arm_cp_reset_ignore },
889 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_BOTH,
890 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 891 .access = PL1_RW,
e4fe830b 892 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el1), .resetvalue = 0 },
4d31c596
PM
893 REGINFO_SENTINEL
894};
895
55d284af
PM
896#ifndef CONFIG_USER_ONLY
897
00108f2d
PM
898static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
899{
900 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
901 if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
902 return CP_ACCESS_TRAP;
903 }
904 return CP_ACCESS_OK;
905}
906
907static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
908{
909 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
910 if (arm_current_pl(env) == 0 &&
911 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
912 return CP_ACCESS_TRAP;
913 }
914 return CP_ACCESS_OK;
915}
916
917static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
918{
919 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
920 * EL0[PV]TEN is zero.
921 */
922 if (arm_current_pl(env) == 0 &&
923 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
924 return CP_ACCESS_TRAP;
925 }
926 return CP_ACCESS_OK;
927}
928
929static CPAccessResult gt_pct_access(CPUARMState *env,
930 const ARMCPRegInfo *ri)
931{
932 return gt_counter_access(env, GTIMER_PHYS);
933}
934
935static CPAccessResult gt_vct_access(CPUARMState *env,
936 const ARMCPRegInfo *ri)
937{
938 return gt_counter_access(env, GTIMER_VIRT);
939}
940
941static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
942{
943 return gt_timer_access(env, GTIMER_PHYS);
944}
945
946static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
947{
948 return gt_timer_access(env, GTIMER_VIRT);
949}
950
55d284af
PM
951static uint64_t gt_get_countervalue(CPUARMState *env)
952{
bc72ad67 953 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
954}
955
956static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
957{
958 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
959
960 if (gt->ctl & 1) {
961 /* Timer enabled: calculate and set current ISTATUS, irq, and
962 * reset timer to when ISTATUS next has to change
963 */
964 uint64_t count = gt_get_countervalue(&cpu->env);
965 /* Note that this must be unsigned 64 bit arithmetic: */
966 int istatus = count >= gt->cval;
967 uint64_t nexttick;
968
969 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
970 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
971 (istatus && !(gt->ctl & 2)));
972 if (istatus) {
973 /* Next transition is when count rolls back over to zero */
974 nexttick = UINT64_MAX;
975 } else {
976 /* Next transition is when we hit cval */
977 nexttick = gt->cval;
978 }
979 /* Note that the desired next expiry time might be beyond the
980 * signed-64-bit range of a QEMUTimer -- in this case we just
981 * set the timer for as far in the future as possible. When the
982 * timer expires we will reset the timer for any remaining period.
983 */
984 if (nexttick > INT64_MAX / GTIMER_SCALE) {
985 nexttick = INT64_MAX / GTIMER_SCALE;
986 }
bc72ad67 987 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af
PM
988 } else {
989 /* Timer disabled: ISTATUS and timer output always clear */
990 gt->ctl &= ~4;
991 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 992 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
993 }
994}
995
55d284af
PM
996static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
997{
998 ARMCPU *cpu = arm_env_get_cpu(env);
999 int timeridx = ri->opc1 & 1;
1000
bc72ad67 1001 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1002}
1003
c4241c7d 1004static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1005{
c4241c7d 1006 return gt_get_countervalue(env);
55d284af
PM
1007}
1008
c4241c7d
PM
1009static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1010 uint64_t value)
55d284af
PM
1011{
1012 int timeridx = ri->opc1 & 1;
1013
1014 env->cp15.c14_timer[timeridx].cval = value;
1015 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1016}
c4241c7d
PM
1017
1018static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af
PM
1019{
1020 int timeridx = ri->crm & 1;
1021
c4241c7d
PM
1022 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1023 gt_get_countervalue(env));
55d284af
PM
1024}
1025
c4241c7d
PM
1026static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1027 uint64_t value)
55d284af
PM
1028{
1029 int timeridx = ri->crm & 1;
1030
1031 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
1032 + sextract64(value, 0, 32);
1033 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1034}
1035
c4241c7d
PM
1036static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1037 uint64_t value)
55d284af
PM
1038{
1039 ARMCPU *cpu = arm_env_get_cpu(env);
1040 int timeridx = ri->crm & 1;
1041 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1042
1043 env->cp15.c14_timer[timeridx].ctl = value & 3;
1044 if ((oldval ^ value) & 1) {
1045 /* Enable toggled */
1046 gt_recalc_timer(cpu, timeridx);
1047 } else if ((oldval & value) & 2) {
1048 /* IMASK toggled: don't need to recalculate,
1049 * just set the interrupt line based on ISTATUS
1050 */
1051 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1052 (oldval & 4) && (value & 2));
1053 }
55d284af
PM
1054}
1055
1056void arm_gt_ptimer_cb(void *opaque)
1057{
1058 ARMCPU *cpu = opaque;
1059
1060 gt_recalc_timer(cpu, GTIMER_PHYS);
1061}
1062
1063void arm_gt_vtimer_cb(void *opaque)
1064{
1065 ARMCPU *cpu = opaque;
1066
1067 gt_recalc_timer(cpu, GTIMER_VIRT);
1068}
1069
1070static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1071 /* Note that CNTFRQ is purely reads-as-written for the benefit
1072 * of software; writing it doesn't actually change the timer frequency.
1073 * Our reset value matches the fixed frequency we implement the timer at.
1074 */
1075 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
a7adc4b7
PM
1076 .type = ARM_CP_NO_MIGRATE,
1077 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1078 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1079 .resetfn = arm_cp_reset_ignore,
1080 },
1081 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1082 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1083 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1084 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1085 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1086 },
1087 /* overall control: mostly access permissions */
a7adc4b7
PM
1088 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1089 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1090 .access = PL1_RW,
1091 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1092 .resetvalue = 0,
1093 },
1094 /* per-timer control */
1095 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
a7adc4b7
PM
1096 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
1097 .accessfn = gt_ptimer_access,
1098 .fieldoffset = offsetoflow32(CPUARMState,
1099 cp15.c14_timer[GTIMER_PHYS].ctl),
1100 .resetfn = arm_cp_reset_ignore,
1101 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1102 },
1103 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1104 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1105 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1106 .accessfn = gt_ptimer_access,
55d284af
PM
1107 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1108 .resetvalue = 0,
00108f2d 1109 .writefn = gt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1110 },
1111 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
a7adc4b7
PM
1112 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
1113 .accessfn = gt_vtimer_access,
1114 .fieldoffset = offsetoflow32(CPUARMState,
1115 cp15.c14_timer[GTIMER_VIRT].ctl),
1116 .resetfn = arm_cp_reset_ignore,
1117 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1118 },
1119 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1120 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1121 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1122 .accessfn = gt_vtimer_access,
55d284af
PM
1123 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1124 .resetvalue = 0,
00108f2d 1125 .writefn = gt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1126 },
1127 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1128 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1129 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1130 .accessfn = gt_ptimer_access,
55d284af
PM
1131 .readfn = gt_tval_read, .writefn = gt_tval_write,
1132 },
a7adc4b7
PM
1133 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1134 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
1135 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1136 .readfn = gt_tval_read, .writefn = gt_tval_write,
1137 },
55d284af
PM
1138 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
1139 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1140 .accessfn = gt_vtimer_access,
55d284af
PM
1141 .readfn = gt_tval_read, .writefn = gt_tval_write,
1142 },
a7adc4b7
PM
1143 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1144 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
1145 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1146 .readfn = gt_tval_read, .writefn = gt_tval_write,
1147 },
55d284af
PM
1148 /* The counter itself */
1149 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
1150 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
00108f2d 1151 .accessfn = gt_pct_access,
a7adc4b7
PM
1152 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1153 },
1154 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1155 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
1156 .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
1157 .accessfn = gt_pct_access,
55d284af
PM
1158 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1159 },
1160 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
1161 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
00108f2d 1162 .accessfn = gt_vct_access,
a7adc4b7
PM
1163 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1164 },
1165 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1166 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
1167 .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
1168 .accessfn = gt_vct_access,
55d284af
PM
1169 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1170 },
1171 /* Comparison value, indicating when the timer goes off */
1172 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1173 .access = PL1_RW | PL0_R,
a7adc4b7 1174 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
55d284af 1175 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
a7adc4b7
PM
1176 .accessfn = gt_ptimer_access, .resetfn = arm_cp_reset_ignore,
1177 .writefn = gt_cval_write, .raw_writefn = raw_write,
1178 },
1179 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1180 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1181 .access = PL1_RW | PL0_R,
1182 .type = ARM_CP_IO,
1183 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1184 .resetvalue = 0, .accessfn = gt_vtimer_access,
00108f2d 1185 .writefn = gt_cval_write, .raw_writefn = raw_write,
55d284af
PM
1186 },
1187 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1188 .access = PL1_RW | PL0_R,
a7adc4b7 1189 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
55d284af 1190 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
a7adc4b7
PM
1191 .accessfn = gt_vtimer_access, .resetfn = arm_cp_reset_ignore,
1192 .writefn = gt_cval_write, .raw_writefn = raw_write,
1193 },
1194 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1195 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1196 .access = PL1_RW | PL0_R,
1197 .type = ARM_CP_IO,
1198 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1199 .resetvalue = 0, .accessfn = gt_vtimer_access,
00108f2d 1200 .writefn = gt_cval_write, .raw_writefn = raw_write,
55d284af
PM
1201 },
1202 REGINFO_SENTINEL
1203};
1204
1205#else
1206/* In user-mode none of the generic timer registers are accessible,
bc72ad67 1207 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
1208 * so instead just don't register any of them.
1209 */
6cc7a3ae 1210static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
1211 REGINFO_SENTINEL
1212};
1213
55d284af
PM
1214#endif
1215
c4241c7d 1216static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 1217{
891a2fe7 1218 if (arm_feature(env, ARM_FEATURE_LPAE)) {
19525524 1219 env->cp15.par_el1 = value;
891a2fe7 1220 } else if (arm_feature(env, ARM_FEATURE_V7)) {
19525524 1221 env->cp15.par_el1 = value & 0xfffff6ff;
4a501606 1222 } else {
19525524 1223 env->cp15.par_el1 = value & 0xfffff1ff;
4a501606 1224 }
4a501606
PM
1225}
1226
1227#ifndef CONFIG_USER_ONLY
1228/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 1229
92611c00
PM
1230static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
1231{
1232 if (ri->opc2 & 4) {
1233 /* Other states are only available with TrustZone; in
1234 * a non-TZ implementation these registers don't exist
1235 * at all, which is an Uncategorized trap. This underdecoding
1236 * is safe because the reginfo is NO_MIGRATE.
1237 */
1238 return CP_ACCESS_TRAP_UNCATEGORIZED;
1239 }
1240 return CP_ACCESS_OK;
1241}
1242
c4241c7d 1243static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 1244{
a8170e5e 1245 hwaddr phys_addr;
4a501606
PM
1246 target_ulong page_size;
1247 int prot;
1248 int ret, is_user = ri->opc2 & 2;
1249 int access_type = ri->opc2 & 1;
1250
4a501606
PM
1251 ret = get_phys_addr(env, value, access_type, is_user,
1252 &phys_addr, &prot, &page_size);
702a9357
PM
1253 if (extended_addresses_enabled(env)) {
1254 /* ret is a DFSR/IFSR value for the long descriptor
1255 * translation table format, but with WnR always clear.
1256 * Convert it to a 64-bit PAR.
1257 */
1258 uint64_t par64 = (1 << 11); /* LPAE bit always set */
1259 if (ret == 0) {
1260 par64 |= phys_addr & ~0xfffULL;
1261 /* We don't set the ATTR or SH fields in the PAR. */
4a501606 1262 } else {
702a9357
PM
1263 par64 |= 1; /* F */
1264 par64 |= (ret & 0x3f) << 1; /* FS */
1265 /* Note that S2WLK and FSTAGE are always zero, because we don't
1266 * implement virtualization and therefore there can't be a stage 2
1267 * fault.
1268 */
4a501606 1269 }
19525524 1270 env->cp15.par_el1 = par64;
4a501606 1271 } else {
702a9357
PM
1272 /* ret is a DFSR/IFSR value for the short descriptor
1273 * translation table format (with WnR always clear).
1274 * Convert it to a 32-bit PAR.
1275 */
1276 if (ret == 0) {
1277 /* We do not set any attribute bits in the PAR */
1278 if (page_size == (1 << 24)
1279 && arm_feature(env, ARM_FEATURE_V7)) {
19525524 1280 env->cp15.par_el1 = (phys_addr & 0xff000000) | 1 << 1;
702a9357 1281 } else {
19525524 1282 env->cp15.par_el1 = phys_addr & 0xfffff000;
702a9357
PM
1283 }
1284 } else {
19525524 1285 env->cp15.par_el1 = ((ret & (1 << 10)) >> 5) |
775fda92 1286 ((ret & (1 << 12)) >> 6) |
702a9357
PM
1287 ((ret & 0xf) << 1) | 1;
1288 }
4a501606 1289 }
4a501606
PM
1290}
1291#endif
1292
1293static const ARMCPRegInfo vapa_cp_reginfo[] = {
1294 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1295 .access = PL1_RW, .resetvalue = 0,
19525524 1296 .fieldoffset = offsetoflow32(CPUARMState, cp15.par_el1),
4a501606
PM
1297 .writefn = par_write },
1298#ifndef CONFIG_USER_ONLY
1299 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00
PM
1300 .access = PL1_W, .accessfn = ats_access,
1301 .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
4a501606
PM
1302#endif
1303 REGINFO_SENTINEL
1304};
1305
18032bec
PM
1306/* Return basic MPU access permission bits. */
1307static uint32_t simple_mpu_ap_bits(uint32_t val)
1308{
1309 uint32_t ret;
1310 uint32_t mask;
1311 int i;
1312 ret = 0;
1313 mask = 3;
1314 for (i = 0; i < 16; i += 2) {
1315 ret |= (val >> i) & mask;
1316 mask <<= 2;
1317 }
1318 return ret;
1319}
1320
1321/* Pad basic MPU access permission bits to extended format. */
1322static uint32_t extended_mpu_ap_bits(uint32_t val)
1323{
1324 uint32_t ret;
1325 uint32_t mask;
1326 int i;
1327 ret = 0;
1328 mask = 3;
1329 for (i = 0; i < 16; i += 2) {
1330 ret |= (val & mask) << i;
1331 mask <<= 2;
1332 }
1333 return ret;
1334}
1335
c4241c7d
PM
1336static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1337 uint64_t value)
18032bec 1338{
7e09797c 1339 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
1340}
1341
c4241c7d 1342static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 1343{
7e09797c 1344 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
1345}
1346
c4241c7d
PM
1347static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1348 uint64_t value)
18032bec 1349{
7e09797c 1350 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
1351}
1352
c4241c7d 1353static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 1354{
7e09797c 1355 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
1356}
1357
1358static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1359 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
d4e6df63 1360 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
7e09797c
PM
1361 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1362 .resetvalue = 0,
18032bec
PM
1363 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1364 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
d4e6df63 1365 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
7e09797c
PM
1366 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1367 .resetvalue = 0,
18032bec
PM
1368 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1369 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1370 .access = PL1_RW,
7e09797c
PM
1371 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1372 .resetvalue = 0, },
18032bec
PM
1373 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1374 .access = PL1_RW,
7e09797c
PM
1375 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1376 .resetvalue = 0, },
ecce5c3c
PM
1377 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1378 .access = PL1_RW,
1379 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1380 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1381 .access = PL1_RW,
1382 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 1383 /* Protection region base and size registers */
e508a92b
PM
1384 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
1385 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1386 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
1387 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
1388 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1389 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
1390 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
1391 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1392 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
1393 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
1394 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1395 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
1396 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
1397 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1398 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
1399 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
1400 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1401 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
1402 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
1403 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1404 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
1405 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
1406 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1407 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
1408 REGINFO_SENTINEL
1409};
1410
c4241c7d
PM
1411static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1412 uint64_t value)
ecce5c3c 1413{
2ebcebe2
PM
1414 int maskshift = extract32(value, 0, 3);
1415
74f1c6dd 1416 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {
e42c4db3 1417 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
e42c4db3
PM
1418 } else {
1419 value &= 7;
1420 }
1421 /* Note that we always calculate c2_mask and c2_base_mask, but
1422 * they are only used for short-descriptor tables (ie if EAE is 0);
1423 * for long-descriptor tables the TTBCR fields are used differently
1424 * and the c2_mask and c2_base_mask values are meaningless.
1425 */
ecce5c3c 1426 env->cp15.c2_control = value;
2ebcebe2
PM
1427 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1428 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
1429}
1430
c4241c7d
PM
1431static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1432 uint64_t value)
d4e6df63 1433{
00c8cb0a
AF
1434 ARMCPU *cpu = arm_env_get_cpu(env);
1435
d4e6df63
PM
1436 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1437 /* With LPAE the TTBCR could result in a change of ASID
1438 * via the TTBCR.A1 bit, so do a TLB flush.
1439 */
00c8cb0a 1440 tlb_flush(CPU(cpu), 1);
d4e6df63 1441 }
c4241c7d 1442 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
1443}
1444
ecce5c3c
PM
1445static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1446{
1447 env->cp15.c2_base_mask = 0xffffc000u;
1448 env->cp15.c2_control = 0;
1449 env->cp15.c2_mask = 0;
1450}
1451
cb2e37df
PM
1452static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453 uint64_t value)
1454{
00c8cb0a
AF
1455 ARMCPU *cpu = arm_env_get_cpu(env);
1456
cb2e37df 1457 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
00c8cb0a 1458 tlb_flush(CPU(cpu), 1);
cb2e37df
PM
1459 env->cp15.c2_control = value;
1460}
1461
327ed10f
PM
1462static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1463 uint64_t value)
1464{
1465 /* 64 bit accesses to the TTBRs can change the ASID and so we
1466 * must flush the TLB.
1467 */
1468 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
1469 ARMCPU *cpu = arm_env_get_cpu(env);
1470
1471 tlb_flush(CPU(cpu), 1);
327ed10f
PM
1472 }
1473 raw_write(env, ri, value);
1474}
1475
18032bec
PM
1476static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1477 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
6cd8a264 1478 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
d81c519c 1479 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 1480 .resetfn = arm_cp_reset_ignore, },
18032bec
PM
1481 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1482 .access = PL1_RW,
6cd8a264
RH
1483 .fieldoffset = offsetof(CPUARMState, cp15.ifsr_el2), .resetvalue = 0, },
1484 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
1485 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
1486 .access = PL1_RW,
d81c519c 1487 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f
PM
1488 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
1489 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1490 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
1491 .writefn = vmsa_ttbr_write, .resetvalue = 0 },
1492 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
1493 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1494 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
1495 .writefn = vmsa_ttbr_write, .resetvalue = 0 },
cb2e37df
PM
1496 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
1497 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1498 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
1499 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
ecce5c3c 1500 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
cb2e37df
PM
1501 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1502 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, .writefn = vmsa_ttbcr_write,
1503 .resetfn = arm_cp_reset_ignore, .raw_writefn = vmsa_ttbcr_raw_write,
1504 .fieldoffset = offsetoflow32(CPUARMState, cp15.c2_control) },
6cd8a264
RH
1505 /* 64-bit FAR; this entry also gives us the AArch32 DFAR */
1506 { .name = "FAR_EL1", .state = ARM_CP_STATE_BOTH,
1507 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1508 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el1),
06d76f31 1509 .resetvalue = 0, },
18032bec
PM
1510 REGINFO_SENTINEL
1511};
1512
c4241c7d
PM
1513static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1514 uint64_t value)
1047b9d7
PM
1515{
1516 env->cp15.c15_ticonfig = value & 0xe7;
1517 /* The OS_TYPE bit in this register changes the reported CPUID! */
1518 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1519 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
1520}
1521
c4241c7d
PM
1522static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1523 uint64_t value)
1047b9d7
PM
1524{
1525 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
1526}
1527
c4241c7d
PM
1528static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1529 uint64_t value)
1047b9d7
PM
1530{
1531 /* Wait-for-interrupt (deprecated) */
c3affe56 1532 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
1533}
1534
c4241c7d
PM
1535static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536 uint64_t value)
c4804214
PM
1537{
1538 /* On OMAP there are registers indicating the max/min index of dcache lines
1539 * containing a dirty line; cache flush operations have to reset these.
1540 */
1541 env->cp15.c15_i_max = 0x000;
1542 env->cp15.c15_i_min = 0xff0;
c4804214
PM
1543}
1544
18032bec
PM
1545static const ARMCPRegInfo omap_cp_reginfo[] = {
1546 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1547 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 1548 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 1549 .resetvalue = 0, },
1047b9d7
PM
1550 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1551 .access = PL1_RW, .type = ARM_CP_NOP },
1552 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1553 .access = PL1_RW,
1554 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1555 .writefn = omap_ticonfig_write },
1556 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1557 .access = PL1_RW,
1558 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1559 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1560 .access = PL1_RW, .resetvalue = 0xff0,
1561 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1562 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1563 .access = PL1_RW,
1564 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1565 .writefn = omap_threadid_write },
1566 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1567 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
d4e6df63 1568 .type = ARM_CP_NO_MIGRATE,
1047b9d7
PM
1569 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1570 /* TODO: Peripheral port remap register:
1571 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1572 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1573 * when MMU is off.
1574 */
c4804214 1575 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63
PM
1576 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1577 .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
c4804214 1578 .writefn = omap_cachemaint_write },
34f90529
PM
1579 { .name = "C9", .cp = 15, .crn = 9,
1580 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1581 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
1582 REGINFO_SENTINEL
1583};
1584
c4241c7d
PM
1585static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1586 uint64_t value)
1047b9d7
PM
1587{
1588 value &= 0x3fff;
1589 if (env->cp15.c15_cpar != value) {
1590 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1591 tb_flush(env);
1592 env->cp15.c15_cpar = value;
1593 }
1047b9d7
PM
1594}
1595
1596static const ARMCPRegInfo xscale_cp_reginfo[] = {
1597 { .name = "XSCALE_CPAR",
1598 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1599 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1600 .writefn = xscale_cpar_write, },
2771db27
PM
1601 { .name = "XSCALE_AUXCR",
1602 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1603 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1604 .resetvalue = 0, },
3b771579
PM
1605 /* XScale specific cache-lockdown: since we have no cache we NOP these
1606 * and hope the guest does not really rely on cache behaviour.
1607 */
1608 { .name = "XSCALE_LOCK_ICACHE_LINE",
1609 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
1610 .access = PL1_W, .type = ARM_CP_NOP },
1611 { .name = "XSCALE_UNLOCK_ICACHE",
1612 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
1613 .access = PL1_W, .type = ARM_CP_NOP },
1614 { .name = "XSCALE_DCACHE_LOCK",
1615 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
1616 .access = PL1_RW, .type = ARM_CP_NOP },
1617 { .name = "XSCALE_UNLOCK_DCACHE",
1618 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
1619 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
1620 REGINFO_SENTINEL
1621};
1622
1623static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1624 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1625 * implementation of this implementation-defined space.
1626 * Ideally this should eventually disappear in favour of actually
1627 * implementing the correct behaviour for all cores.
1628 */
1629 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1630 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87
PC
1631 .access = PL1_RW,
1632 .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE | ARM_CP_OVERRIDE,
d4e6df63 1633 .resetvalue = 0 },
18032bec
PM
1634 REGINFO_SENTINEL
1635};
1636
c4804214
PM
1637static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1638 /* Cache status: RAZ because we have no cache so it's always clean */
1639 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
d4e6df63
PM
1640 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1641 .resetvalue = 0 },
c4804214
PM
1642 REGINFO_SENTINEL
1643};
1644
1645static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1646 /* We never have a a block transfer operation in progress */
1647 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
d4e6df63
PM
1648 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1649 .resetvalue = 0 },
30b05bba
PM
1650 /* The cache ops themselves: these all NOP for QEMU */
1651 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1652 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1653 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1654 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1655 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1656 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1657 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1658 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1659 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1660 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1661 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1662 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
1663 REGINFO_SENTINEL
1664};
1665
1666static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1667 /* The cache test-and-clean instructions always return (1 << 30)
1668 * to indicate that there are no dirty cache lines.
1669 */
1670 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
d4e6df63
PM
1671 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1672 .resetvalue = (1 << 30) },
c4804214 1673 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
d4e6df63
PM
1674 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1675 .resetvalue = (1 << 30) },
c4804214
PM
1676 REGINFO_SENTINEL
1677};
1678
34f90529
PM
1679static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1680 /* Ignore ReadBuffer accesses */
1681 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1682 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63
PM
1683 .access = PL1_RW, .resetvalue = 0,
1684 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
34f90529
PM
1685 REGINFO_SENTINEL
1686};
1687
c4241c7d 1688static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
81bdde9d 1689{
55e5c285
AF
1690 CPUState *cs = CPU(arm_env_get_cpu(env));
1691 uint32_t mpidr = cs->cpu_index;
4b7fff2f
PM
1692 /* We don't support setting cluster ID ([8..11]) (known as Aff1
1693 * in later ARM ARM versions), or any of the higher affinity level fields,
81bdde9d
PM
1694 * so these bits always RAZ.
1695 */
1696 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 1697 mpidr |= (1U << 31);
81bdde9d
PM
1698 /* Cores which are uniprocessor (non-coherent)
1699 * but still implement the MP extensions set
1700 * bit 30. (For instance, A9UP.) However we do
1701 * not currently model any of those cores.
1702 */
1703 }
c4241c7d 1704 return mpidr;
81bdde9d
PM
1705}
1706
1707static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
1708 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
1709 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
d4e6df63 1710 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
81bdde9d
PM
1711 REGINFO_SENTINEL
1712};
1713
7ac681cf 1714static const ARMCPRegInfo lpae_cp_reginfo[] = {
b90372ad 1715 /* NOP AMAIR0/1: the override is because these clash with the rather
7ac681cf
PM
1716 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1717 */
b0fe2427
PM
1718 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
1719 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
7ac681cf
PM
1720 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1721 .resetvalue = 0 },
b0fe2427 1722 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf
PM
1723 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1724 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1725 .resetvalue = 0 },
f9fc619a
PM
1726 /* 64 bit access versions of the (dummy) debug registers */
1727 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
1728 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1729 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
1730 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
891a2fe7
PM
1731 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1732 .access = PL1_RW, .type = ARM_CP_64BIT,
19525524 1733 .fieldoffset = offsetof(CPUARMState, cp15.par_el1), .resetvalue = 0 },
891a2fe7 1734 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
327ed10f
PM
1735 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
1736 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
1737 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
891a2fe7 1738 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
327ed10f
PM
1739 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
1740 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
1741 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
7ac681cf
PM
1742 REGINFO_SENTINEL
1743};
1744
c4241c7d 1745static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 1746{
c4241c7d 1747 return vfp_get_fpcr(env);
b0d2b7d0
PM
1748}
1749
c4241c7d
PM
1750static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1751 uint64_t value)
b0d2b7d0
PM
1752{
1753 vfp_set_fpcr(env, value);
b0d2b7d0
PM
1754}
1755
c4241c7d 1756static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 1757{
c4241c7d 1758 return vfp_get_fpsr(env);
b0d2b7d0
PM
1759}
1760
c4241c7d
PM
1761static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1762 uint64_t value)
b0d2b7d0
PM
1763{
1764 vfp_set_fpsr(env, value);
b0d2b7d0
PM
1765}
1766
c2b820fe
PM
1767static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
1768{
1769 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UMA)) {
1770 return CP_ACCESS_TRAP;
1771 }
1772 return CP_ACCESS_OK;
1773}
1774
1775static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
1776 uint64_t value)
1777{
1778 env->daif = value & PSTATE_DAIF;
1779}
1780
8af35c37
PM
1781static CPAccessResult aa64_cacheop_access(CPUARMState *env,
1782 const ARMCPRegInfo *ri)
1783{
1784 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
1785 * SCTLR_EL1.UCI is set.
1786 */
1787 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCI)) {
1788 return CP_ACCESS_TRAP;
1789 }
1790 return CP_ACCESS_OK;
1791}
1792
168aa23b
PM
1793static void tlbi_aa64_va_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1795{
1796 /* Invalidate by VA (AArch64 version) */
31b030d4 1797 ARMCPU *cpu = arm_env_get_cpu(env);
168aa23b 1798 uint64_t pageaddr = value << 12;
31b030d4 1799 tlb_flush_page(CPU(cpu), pageaddr);
168aa23b
PM
1800}
1801
1802static void tlbi_aa64_vaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
1803 uint64_t value)
1804{
1805 /* Invalidate by VA, all ASIDs (AArch64 version) */
31b030d4 1806 ARMCPU *cpu = arm_env_get_cpu(env);
168aa23b 1807 uint64_t pageaddr = value << 12;
31b030d4 1808 tlb_flush_page(CPU(cpu), pageaddr);
168aa23b
PM
1809}
1810
1811static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1812 uint64_t value)
1813{
1814 /* Invalidate by ASID (AArch64 version) */
00c8cb0a 1815 ARMCPU *cpu = arm_env_get_cpu(env);
168aa23b 1816 int asid = extract64(value, 48, 16);
00c8cb0a 1817 tlb_flush(CPU(cpu), asid == 0);
168aa23b
PM
1818}
1819
aca3f40b
PM
1820static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
1821{
1822 /* We don't implement EL2, so the only control on DC ZVA is the
1823 * bit in the SCTLR which can prohibit access for EL0.
1824 */
1825 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_DZE)) {
1826 return CP_ACCESS_TRAP;
1827 }
1828 return CP_ACCESS_OK;
1829}
1830
1831static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
1832{
1833 ARMCPU *cpu = arm_env_get_cpu(env);
1834 int dzp_bit = 1 << 4;
1835
1836 /* DZP indicates whether DC ZVA access is allowed */
1837 if (aa64_zva_access(env, NULL) != CP_ACCESS_OK) {
1838 dzp_bit = 0;
1839 }
1840 return cpu->dcz_blocksize | dzp_bit;
1841}
1842
f502cfc2
PM
1843static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
1844{
1845 if (!env->pstate & PSTATE_SP) {
1846 /* Access to SP_EL0 is undefined if it's being used as
1847 * the stack pointer.
1848 */
1849 return CP_ACCESS_TRAP_UNCATEGORIZED;
1850 }
1851 return CP_ACCESS_OK;
1852}
1853
1854static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
1855{
1856 return env->pstate & PSTATE_SP;
1857}
1858
1859static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
1860{
1861 update_spsel(env, val);
1862}
1863
b0d2b7d0
PM
1864static const ARMCPRegInfo v8_cp_reginfo[] = {
1865 /* Minimal set of EL0-visible registers. This will need to be expanded
1866 * significantly for system emulation of AArch64 CPUs.
1867 */
1868 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
1869 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
1870 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
1871 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
1872 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
1873 .type = ARM_CP_NO_MIGRATE,
1874 .access = PL0_RW, .accessfn = aa64_daif_access,
1875 .fieldoffset = offsetof(CPUARMState, daif),
1876 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
1877 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
1878 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
1879 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
1880 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
1881 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
1882 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
1883 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
1884 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
aca3f40b
PM
1885 .access = PL0_R, .type = ARM_CP_NO_MIGRATE,
1886 .readfn = aa64_dczid_read },
1887 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
1888 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
1889 .access = PL0_W, .type = ARM_CP_DC_ZVA,
1890#ifndef CONFIG_USER_ONLY
1891 /* Avoid overhead of an access check that always passes in user-mode */
1892 .accessfn = aa64_zva_access,
1893#endif
1894 },
0eef9d98
PM
1895 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
1896 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
1897 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
1898 /* Cache ops: all NOPs since we don't emulate caches */
1899 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
1900 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
1901 .access = PL1_W, .type = ARM_CP_NOP },
1902 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
1903 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
1904 .access = PL1_W, .type = ARM_CP_NOP },
1905 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
1906 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
1907 .access = PL0_W, .type = ARM_CP_NOP,
1908 .accessfn = aa64_cacheop_access },
1909 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
1910 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1911 .access = PL1_W, .type = ARM_CP_NOP },
1912 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
1913 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1914 .access = PL1_W, .type = ARM_CP_NOP },
1915 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
1916 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
1917 .access = PL0_W, .type = ARM_CP_NOP,
1918 .accessfn = aa64_cacheop_access },
1919 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
1920 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1921 .access = PL1_W, .type = ARM_CP_NOP },
1922 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
1923 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
1924 .access = PL0_W, .type = ARM_CP_NOP,
1925 .accessfn = aa64_cacheop_access },
1926 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
1927 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
1928 .access = PL0_W, .type = ARM_CP_NOP,
1929 .accessfn = aa64_cacheop_access },
1930 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
1931 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1932 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
1933 /* TLBI operations */
1934 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 1935 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
168aa23b
PM
1936 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1937 .writefn = tlbiall_write },
1938 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 1939 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
168aa23b
PM
1940 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1941 .writefn = tlbi_aa64_va_write },
1942 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 1943 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
168aa23b
PM
1944 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1945 .writefn = tlbi_aa64_asid_write },
1946 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 1947 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
168aa23b
PM
1948 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1949 .writefn = tlbi_aa64_vaa_write },
1950 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 1951 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
168aa23b
PM
1952 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1953 .writefn = tlbi_aa64_va_write },
1954 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 1955 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
168aa23b
PM
1956 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1957 .writefn = tlbi_aa64_vaa_write },
1958 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 1959 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
168aa23b
PM
1960 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1961 .writefn = tlbiall_write },
1962 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 1963 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
168aa23b
PM
1964 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1965 .writefn = tlbi_aa64_va_write },
1966 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 1967 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
168aa23b
PM
1968 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1969 .writefn = tlbi_aa64_asid_write },
1970 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 1971 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
168aa23b
PM
1972 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1973 .writefn = tlbi_aa64_vaa_write },
1974 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 1975 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
168aa23b
PM
1976 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1977 .writefn = tlbi_aa64_va_write },
1978 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 1979 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
168aa23b
PM
1980 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1981 .writefn = tlbi_aa64_vaa_write },
19525524
PM
1982#ifndef CONFIG_USER_ONLY
1983 /* 64 bit address translation operations */
1984 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
1985 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
1986 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
1987 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
1988 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
1989 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
1990 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
1991 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
1992 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
1993 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
1994 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
1995 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
1996#endif
9449fdf6
PM
1997 /* 32 bit TLB invalidates, Inner Shareable */
1998 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1999 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2000 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2001 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2002 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2003 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2004 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2005 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2006 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2007 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2008 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2009 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2010 /* 32 bit ITLB invalidates */
2011 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2012 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2013 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2014 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2015 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2016 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2017 /* 32 bit DTLB invalidates */
2018 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2019 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2020 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2021 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2022 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2023 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2024 /* 32 bit TLB invalidates */
2025 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2026 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2027 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2028 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2029 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2030 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2031 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2032 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2033 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2034 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2035 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2036 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2037 /* 32 bit cache operations */
2038 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2039 .type = ARM_CP_NOP, .access = PL1_W },
2040 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
2041 .type = ARM_CP_NOP, .access = PL1_W },
2042 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2043 .type = ARM_CP_NOP, .access = PL1_W },
2044 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2045 .type = ARM_CP_NOP, .access = PL1_W },
2046 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
2047 .type = ARM_CP_NOP, .access = PL1_W },
2048 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
2049 .type = ARM_CP_NOP, .access = PL1_W },
2050 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2051 .type = ARM_CP_NOP, .access = PL1_W },
2052 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2053 .type = ARM_CP_NOP, .access = PL1_W },
2054 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
2055 .type = ARM_CP_NOP, .access = PL1_W },
2056 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2057 .type = ARM_CP_NOP, .access = PL1_W },
2058 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2059 .type = ARM_CP_NOP, .access = PL1_W },
2060 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
2061 .type = ARM_CP_NOP, .access = PL1_W },
2062 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2063 .type = ARM_CP_NOP, .access = PL1_W },
2064 /* MMU Domain access control / MPU write buffer control */
2065 { .name = "DACR", .cp = 15,
2066 .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
2067 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
2068 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
91e24069
PM
2069 /* Dummy implementation of monitor debug system control register:
2070 * we don't support debug.
2071 */
2072 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_AA64,
2073 .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2074 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cd5c11b8
PM
2075 /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
2076 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_AA64,
2077 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
2078 .access = PL1_W, .type = ARM_CP_NOP },
a0618a19
PM
2079 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
2080 .type = ARM_CP_NO_MIGRATE,
2081 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
2082 .access = PL1_RW,
2083 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9
PM
2084 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
2085 .type = ARM_CP_NO_MIGRATE,
2086 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
2087 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[0]) },
f502cfc2
PM
2088 /* We rely on the access checks not allowing the guest to write to the
2089 * state field when SPSel indicates that it's being used as the stack
2090 * pointer.
2091 */
2092 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
2093 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
2094 .access = PL1_RW, .accessfn = sp_el0_access,
2095 .type = ARM_CP_NO_MIGRATE,
2096 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
2097 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
2098 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
2099 .type = ARM_CP_NO_MIGRATE,
2100 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
b0d2b7d0
PM
2101 REGINFO_SENTINEL
2102};
2103
3b685ba7
EI
2104static const ARMCPRegInfo v8_el2_cp_reginfo[] = {
2105 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
2106 .type = ARM_CP_NO_MIGRATE,
2107 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
2108 .access = PL2_RW,
2109 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
2110 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
2111 .type = ARM_CP_NO_MIGRATE,
2112 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
2113 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
2114 REGINFO_SENTINEL
2115};
2116
81547d66
EI
2117static const ARMCPRegInfo v8_el3_cp_reginfo[] = {
2118 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
2119 .type = ARM_CP_NO_MIGRATE,
2120 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
2121 .access = PL3_RW,
2122 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
2123 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
2124 .type = ARM_CP_NO_MIGRATE,
2125 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
2126 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
2127 REGINFO_SENTINEL
2128};
2129
c4241c7d
PM
2130static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2131 uint64_t value)
2771db27 2132{
00c8cb0a
AF
2133 ARMCPU *cpu = arm_env_get_cpu(env);
2134
2f0d8631
PM
2135 if (env->cp15.c1_sys == value) {
2136 /* Skip the TLB flush if nothing actually changed; Linux likes
2137 * to do a lot of pointless SCTLR writes.
2138 */
2139 return;
2140 }
2141
2771db27
PM
2142 env->cp15.c1_sys = value;
2143 /* ??? Lots of these bits are not implemented. */
2144 /* This may enable/disable the MMU, so do a TLB flush. */
00c8cb0a 2145 tlb_flush(CPU(cpu), 1);
2771db27
PM
2146}
2147
7da845b0
PM
2148static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2149{
2150 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
2151 * but the AArch32 CTR has its own reginfo struct)
2152 */
2153 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCT)) {
2154 return CP_ACCESS_TRAP;
2155 }
2156 return CP_ACCESS_OK;
2157}
2158
0b45451e
PM
2159static void define_aarch64_debug_regs(ARMCPU *cpu)
2160{
2161 /* Define breakpoint and watchpoint registers. These do nothing
2162 * but read as written, for now.
2163 */
2164 int i;
2165
2166 for (i = 0; i < 16; i++) {
2167 ARMCPRegInfo dbgregs[] = {
2168 { .name = "DBGBVR", .state = ARM_CP_STATE_AA64,
2169 .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
2170 .access = PL1_RW,
2171 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]) },
2172 { .name = "DBGBCR", .state = ARM_CP_STATE_AA64,
2173 .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
2174 .access = PL1_RW,
2175 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]) },
2176 { .name = "DBGWVR", .state = ARM_CP_STATE_AA64,
2177 .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
2178 .access = PL1_RW,
2179 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]) },
2180 { .name = "DBGWCR", .state = ARM_CP_STATE_AA64,
2181 .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
2182 .access = PL1_RW,
2183 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]) },
2184 REGINFO_SENTINEL
2185 };
2186 define_arm_cp_regs(cpu, dbgregs);
2187 }
2188}
2189
2ceb98c0
PM
2190void register_cp_regs_for_features(ARMCPU *cpu)
2191{
2192 /* Register all the coprocessor registers based on feature bits */
2193 CPUARMState *env = &cpu->env;
2194 if (arm_feature(env, ARM_FEATURE_M)) {
2195 /* M profile has no coprocessor registers */
2196 return;
2197 }
2198
e9aa6c21 2199 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
2200 if (!arm_feature(env, ARM_FEATURE_V8)) {
2201 /* Must go early as it is full of wildcards that may be
2202 * overridden by later definitions.
2203 */
2204 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
2205 }
2206
7d57f408 2207 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
2208 /* The ID registers all have impdef reset values */
2209 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
2210 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
2211 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
2212 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2213 .resetvalue = cpu->id_pfr0 },
0ff644a7
PM
2214 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
2215 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
2216 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2217 .resetvalue = cpu->id_pfr1 },
0ff644a7
PM
2218 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
2219 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
2220 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2221 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
2222 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
2223 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
2224 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2225 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
2226 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
2227 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
2228 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2229 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
2230 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
2231 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
2232 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2233 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
2234 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
2235 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
2236 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2237 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
2238 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
2239 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
2240 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2241 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
2242 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
2243 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
2244 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2245 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
2246 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
2247 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
2248 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2249 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
2250 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
2251 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2252 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2253 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
2254 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
2255 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
2256 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2257 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
2258 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
2259 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
2260 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2261 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
2262 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
2263 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
2264 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
2265 .resetvalue = cpu->id_isar5 },
2266 /* 6..7 are as yet unallocated and must RAZ */
2267 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
2268 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
2269 .resetvalue = 0 },
2270 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
2271 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
2272 .resetvalue = 0 },
2273 REGINFO_SENTINEL
2274 };
2275 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
2276 define_arm_cp_regs(cpu, v6_cp_reginfo);
2277 } else {
2278 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
2279 }
4d31c596
PM
2280 if (arm_feature(env, ARM_FEATURE_V6K)) {
2281 define_arm_cp_regs(cpu, v6k_cp_reginfo);
2282 }
e9aa6c21 2283 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 2284 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
2285 * field as main ID register, and we implement only the cycle
2286 * count register.
200ac0ef 2287 */
7c2cb42b 2288#ifndef CONFIG_USER_ONLY
200ac0ef
PM
2289 ARMCPRegInfo pmcr = {
2290 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
2291 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
d6d60581 2292 .type = ARM_CP_IO,
200ac0ef 2293 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
2294 .accessfn = pmreg_access, .writefn = pmcr_write,
2295 .raw_writefn = raw_write,
200ac0ef 2296 };
7c2cb42b
AF
2297 define_one_arm_cp_reg(cpu, &pmcr);
2298#endif
776d4e5c 2299 ARMCPRegInfo clidr = {
7da845b0
PM
2300 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
2301 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
2302 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
2303 };
776d4e5c 2304 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 2305 define_arm_cp_regs(cpu, v7_cp_reginfo);
7d57f408
PM
2306 } else {
2307 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 2308 }
b0d2b7d0 2309 if (arm_feature(env, ARM_FEATURE_V8)) {
e60cef86
PM
2310 /* AArch64 ID registers, which all have impdef reset values */
2311 ARMCPRegInfo v8_idregs[] = {
2312 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
2313 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
2314 .access = PL1_R, .type = ARM_CP_CONST,
2315 .resetvalue = cpu->id_aa64pfr0 },
2316 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
2317 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
2318 .access = PL1_R, .type = ARM_CP_CONST,
2319 .resetvalue = cpu->id_aa64pfr1},
2320 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
2321 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
2322 .access = PL1_R, .type = ARM_CP_CONST,
9225d739
PM
2323 /* We mask out the PMUVer field, beacuse we don't currently
2324 * implement the PMU. Not advertising it prevents the guest
2325 * from trying to use it and getting UNDEFs on registers we
2326 * don't implement.
2327 */
2328 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
e60cef86
PM
2329 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
2330 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
2331 .access = PL1_R, .type = ARM_CP_CONST,
2332 .resetvalue = cpu->id_aa64dfr1 },
2333 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
2334 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
2335 .access = PL1_R, .type = ARM_CP_CONST,
2336 .resetvalue = cpu->id_aa64afr0 },
2337 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
2338 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
2339 .access = PL1_R, .type = ARM_CP_CONST,
2340 .resetvalue = cpu->id_aa64afr1 },
2341 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
2342 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
2343 .access = PL1_R, .type = ARM_CP_CONST,
2344 .resetvalue = cpu->id_aa64isar0 },
2345 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
2346 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
2347 .access = PL1_R, .type = ARM_CP_CONST,
2348 .resetvalue = cpu->id_aa64isar1 },
2349 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
2350 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
2351 .access = PL1_R, .type = ARM_CP_CONST,
2352 .resetvalue = cpu->id_aa64mmfr0 },
2353 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
2354 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
2355 .access = PL1_R, .type = ARM_CP_CONST,
2356 .resetvalue = cpu->id_aa64mmfr1 },
a50c0f51
PM
2357 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
2358 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
2359 .access = PL1_R, .type = ARM_CP_CONST,
2360 .resetvalue = cpu->mvfr0 },
2361 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
2362 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
2363 .access = PL1_R, .type = ARM_CP_CONST,
2364 .resetvalue = cpu->mvfr1 },
2365 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
2366 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
2367 .access = PL1_R, .type = ARM_CP_CONST,
2368 .resetvalue = cpu->mvfr2 },
e60cef86
PM
2369 REGINFO_SENTINEL
2370 };
3933443e
PM
2371 ARMCPRegInfo rvbar = {
2372 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
2373 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
2374 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
2375 };
2376 define_one_arm_cp_reg(cpu, &rvbar);
e60cef86 2377 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0 2378 define_arm_cp_regs(cpu, v8_cp_reginfo);
0b45451e 2379 define_aarch64_debug_regs(cpu);
b0d2b7d0 2380 }
3b685ba7
EI
2381 if (arm_feature(env, ARM_FEATURE_EL2)) {
2382 define_arm_cp_regs(cpu, v8_el2_cp_reginfo);
2383 }
81547d66
EI
2384 if (arm_feature(env, ARM_FEATURE_EL3)) {
2385 define_arm_cp_regs(cpu, v8_el3_cp_reginfo);
2386 }
18032bec
PM
2387 if (arm_feature(env, ARM_FEATURE_MPU)) {
2388 /* These are the MPU registers prior to PMSAv6. Any new
2389 * PMSA core later than the ARM946 will require that we
2390 * implement the PMSAv6 or PMSAv7 registers, which are
2391 * completely different.
2392 */
2393 assert(!arm_feature(env, ARM_FEATURE_V6));
2394 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
2395 } else {
2396 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
2397 }
c326b979
PM
2398 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
2399 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
2400 }
6cc7a3ae
PM
2401 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2402 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
2403 }
4a501606
PM
2404 if (arm_feature(env, ARM_FEATURE_VAPA)) {
2405 define_arm_cp_regs(cpu, vapa_cp_reginfo);
2406 }
c4804214
PM
2407 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
2408 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
2409 }
2410 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
2411 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
2412 }
2413 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
2414 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
2415 }
18032bec
PM
2416 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2417 define_arm_cp_regs(cpu, omap_cp_reginfo);
2418 }
34f90529
PM
2419 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
2420 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
2421 }
1047b9d7
PM
2422 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2423 define_arm_cp_regs(cpu, xscale_cp_reginfo);
2424 }
2425 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
2426 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
2427 }
7ac681cf
PM
2428 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2429 define_arm_cp_regs(cpu, lpae_cp_reginfo);
2430 }
7884849c
PM
2431 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
2432 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
2433 * be read-only (ie write causes UNDEF exception).
2434 */
2435 {
00a29f3d
PM
2436 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
2437 /* Pre-v8 MIDR space.
2438 * Note that the MIDR isn't a simple constant register because
7884849c
PM
2439 * of the TI925 behaviour where writes to another register can
2440 * cause the MIDR value to change.
97ce8d61
PC
2441 *
2442 * Unimplemented registers in the c15 0 0 0 space default to
2443 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
2444 * and friends override accordingly.
7884849c
PM
2445 */
2446 { .name = "MIDR",
97ce8d61 2447 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 2448 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 2449 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
97ce8d61
PC
2450 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
2451 .type = ARM_CP_OVERRIDE },
7884849c
PM
2452 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
2453 { .name = "DUMMY",
2454 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
2455 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2456 { .name = "DUMMY",
2457 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
2458 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2459 { .name = "DUMMY",
2460 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
2461 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2462 { .name = "DUMMY",
2463 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
2464 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2465 { .name = "DUMMY",
2466 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
2467 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2468 REGINFO_SENTINEL
2469 };
00a29f3d
PM
2470 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
2471 /* v8 MIDR -- the wildcard isn't necessary, and nor is the
2472 * variable-MIDR TI925 behaviour. Instead we have a single
2473 * (strictly speaking IMPDEF) alias of the MIDR, REVIDR.
2474 */
2475 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
2476 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
2477 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
2478 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
2479 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
2480 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
2481 REGINFO_SENTINEL
2482 };
2483 ARMCPRegInfo id_cp_reginfo[] = {
2484 /* These are common to v8 and pre-v8 */
2485 { .name = "CTR",
2486 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
2487 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
2488 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
2489 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
2490 .access = PL0_R, .accessfn = ctr_el0_access,
2491 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
2492 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
2493 { .name = "TCMTR",
2494 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
2495 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2496 { .name = "TLBTR",
2497 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
2498 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2499 REGINFO_SENTINEL
2500 };
7884849c
PM
2501 ARMCPRegInfo crn0_wi_reginfo = {
2502 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
2503 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
2504 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
2505 };
2506 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
2507 arm_feature(env, ARM_FEATURE_STRONGARM)) {
2508 ARMCPRegInfo *r;
2509 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
2510 * whole space. Then update the specific ID registers to allow write
2511 * access, so that they ignore writes rather than causing them to
2512 * UNDEF.
7884849c
PM
2513 */
2514 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
2515 for (r = id_pre_v8_midr_cp_reginfo;
2516 r->type != ARM_CP_SENTINEL; r++) {
2517 r->access = PL1_RW;
2518 }
7884849c
PM
2519 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
2520 r->access = PL1_RW;
7884849c 2521 }
7884849c 2522 }
00a29f3d
PM
2523 if (arm_feature(env, ARM_FEATURE_V8)) {
2524 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
2525 } else {
2526 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
2527 }
a703eda1 2528 define_arm_cp_regs(cpu, id_cp_reginfo);
7884849c
PM
2529 }
2530
97ce8d61
PC
2531 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
2532 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
2533 }
2534
2771db27
PM
2535 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
2536 ARMCPRegInfo auxcr = {
2eef0bf8
PM
2537 .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
2538 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
2771db27
PM
2539 .access = PL1_RW, .type = ARM_CP_CONST,
2540 .resetvalue = cpu->reset_auxcr
2541 };
2542 define_one_arm_cp_reg(cpu, &auxcr);
2543 }
2544
d8ba780b 2545 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
2546 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
2547 /* 32 bit view is [31:18] 0...0 [43:32]. */
2548 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
2549 | extract64(cpu->reset_cbar, 32, 12);
2550 ARMCPRegInfo cbar_reginfo[] = {
2551 { .name = "CBAR",
2552 .type = ARM_CP_CONST,
2553 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
2554 .access = PL1_R, .resetvalue = cpu->reset_cbar },
2555 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
2556 .type = ARM_CP_CONST,
2557 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
2558 .access = PL1_R, .resetvalue = cbar32 },
2559 REGINFO_SENTINEL
2560 };
2561 /* We don't implement a r/w 64 bit CBAR currently */
2562 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
2563 define_arm_cp_regs(cpu, cbar_reginfo);
2564 } else {
2565 ARMCPRegInfo cbar = {
2566 .name = "CBAR",
2567 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
2568 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
2569 .fieldoffset = offsetof(CPUARMState,
2570 cp15.c15_config_base_address)
2571 };
2572 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
2573 cbar.access = PL1_R;
2574 cbar.fieldoffset = 0;
2575 cbar.type = ARM_CP_CONST;
2576 }
2577 define_one_arm_cp_reg(cpu, &cbar);
2578 }
d8ba780b
PC
2579 }
2580
2771db27
PM
2581 /* Generic registers whose values depend on the implementation */
2582 {
2583 ARMCPRegInfo sctlr = {
5ebafdf3
PM
2584 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
2585 .opc0 = 3, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
2771db27 2586 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
d4e6df63
PM
2587 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
2588 .raw_writefn = raw_write,
2771db27
PM
2589 };
2590 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2591 /* Normally we would always end the TB on an SCTLR write, but Linux
2592 * arch/arm/mach-pxa/sleep.S expects two instructions following
2593 * an MMU enable to execute from cache. Imitate this behaviour.
2594 */
2595 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
2596 }
2597 define_one_arm_cp_reg(cpu, &sctlr);
2598 }
2ceb98c0
PM
2599}
2600
778c3a06 2601ARMCPU *cpu_arm_init(const char *cpu_model)
40f137e1 2602{
9262685b 2603 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
14969266
AF
2604}
2605
2606void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
2607{
22169d41 2608 CPUState *cs = CPU(cpu);
14969266
AF
2609 CPUARMState *env = &cpu->env;
2610
6a669427
PM
2611 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
2612 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
2613 aarch64_fpu_gdb_set_reg,
2614 34, "aarch64-fpu.xml", 0);
2615 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 2616 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
2617 51, "arm-neon.xml", 0);
2618 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 2619 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
2620 35, "arm-vfp3.xml", 0);
2621 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 2622 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
2623 19, "arm-vfp.xml", 0);
2624 }
40f137e1
PB
2625}
2626
777dc784
PM
2627/* Sort alphabetically by type name, except for "any". */
2628static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 2629{
777dc784
PM
2630 ObjectClass *class_a = (ObjectClass *)a;
2631 ObjectClass *class_b = (ObjectClass *)b;
2632 const char *name_a, *name_b;
5adb4839 2633
777dc784
PM
2634 name_a = object_class_get_name(class_a);
2635 name_b = object_class_get_name(class_b);
51492fd1 2636 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 2637 return 1;
51492fd1 2638 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
2639 return -1;
2640 } else {
2641 return strcmp(name_a, name_b);
5adb4839
PB
2642 }
2643}
2644
777dc784 2645static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 2646{
777dc784 2647 ObjectClass *oc = data;
92a31361 2648 CPUListState *s = user_data;
51492fd1
AF
2649 const char *typename;
2650 char *name;
3371d272 2651
51492fd1
AF
2652 typename = object_class_get_name(oc);
2653 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 2654 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
2655 name);
2656 g_free(name);
777dc784
PM
2657}
2658
2659void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
2660{
92a31361 2661 CPUListState s = {
777dc784
PM
2662 .file = f,
2663 .cpu_fprintf = cpu_fprintf,
2664 };
2665 GSList *list;
2666
2667 list = object_class_get_list(TYPE_ARM_CPU, false);
2668 list = g_slist_sort(list, arm_cpu_list_compare);
2669 (*cpu_fprintf)(f, "Available CPUs:\n");
2670 g_slist_foreach(list, arm_cpu_list_entry, &s);
2671 g_slist_free(list);
a96c0514
PM
2672#ifdef CONFIG_KVM
2673 /* The 'host' CPU type is dynamically registered only if KVM is
2674 * enabled, so we have to special-case it here:
2675 */
2676 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
2677#endif
40f137e1
PB
2678}
2679
78027bb6
CR
2680static void arm_cpu_add_definition(gpointer data, gpointer user_data)
2681{
2682 ObjectClass *oc = data;
2683 CpuDefinitionInfoList **cpu_list = user_data;
2684 CpuDefinitionInfoList *entry;
2685 CpuDefinitionInfo *info;
2686 const char *typename;
2687
2688 typename = object_class_get_name(oc);
2689 info = g_malloc0(sizeof(*info));
2690 info->name = g_strndup(typename,
2691 strlen(typename) - strlen("-" TYPE_ARM_CPU));
2692
2693 entry = g_malloc0(sizeof(*entry));
2694 entry->value = info;
2695 entry->next = *cpu_list;
2696 *cpu_list = entry;
2697}
2698
2699CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
2700{
2701 CpuDefinitionInfoList *cpu_list = NULL;
2702 GSList *list;
2703
2704 list = object_class_get_list(TYPE_ARM_CPU, false);
2705 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
2706 g_slist_free(list);
2707
2708 return cpu_list;
2709}
2710
6e6efd61 2711static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
f5a0a5a5
PM
2712 void *opaque, int state,
2713 int crm, int opc1, int opc2)
6e6efd61
PM
2714{
2715 /* Private utility function for define_one_arm_cp_reg_with_opaque():
2716 * add a single reginfo struct to the hash table.
2717 */
2718 uint32_t *key = g_new(uint32_t, 1);
2719 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
2720 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
f5a0a5a5
PM
2721 if (r->state == ARM_CP_STATE_BOTH && state == ARM_CP_STATE_AA32) {
2722 /* The AArch32 view of a shared register sees the lower 32 bits
2723 * of a 64 bit backing field. It is not migratable as the AArch64
2724 * view handles that. AArch64 also handles reset.
2725 * We assume it is a cp15 register.
2726 */
2727 r2->cp = 15;
2728 r2->type |= ARM_CP_NO_MIGRATE;
2729 r2->resetfn = arm_cp_reset_ignore;
2730#ifdef HOST_WORDS_BIGENDIAN
2731 if (r2->fieldoffset) {
2732 r2->fieldoffset += sizeof(uint32_t);
2733 }
2734#endif
2735 }
2736 if (state == ARM_CP_STATE_AA64) {
2737 /* To allow abbreviation of ARMCPRegInfo
2738 * definitions, we treat cp == 0 as equivalent to
2739 * the value for "standard guest-visible sysreg".
2740 */
2741 if (r->cp == 0) {
2742 r2->cp = CP_REG_ARM64_SYSREG_CP;
2743 }
2744 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
2745 r2->opc0, opc1, opc2);
2746 } else {
2747 *key = ENCODE_CP_REG(r2->cp, is64, r2->crn, crm, opc1, opc2);
2748 }
6e6efd61
PM
2749 if (opaque) {
2750 r2->opaque = opaque;
2751 }
67ed771d
PM
2752 /* reginfo passed to helpers is correct for the actual access,
2753 * and is never ARM_CP_STATE_BOTH:
2754 */
2755 r2->state = state;
6e6efd61
PM
2756 /* Make sure reginfo passed to helpers for wildcarded regs
2757 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
2758 */
2759 r2->crm = crm;
2760 r2->opc1 = opc1;
2761 r2->opc2 = opc2;
2762 /* By convention, for wildcarded registers only the first
2763 * entry is used for migration; the others are marked as
2764 * NO_MIGRATE so we don't try to transfer the register
2765 * multiple times. Special registers (ie NOP/WFI) are
2766 * never migratable.
2767 */
2768 if ((r->type & ARM_CP_SPECIAL) ||
2769 ((r->crm == CP_ANY) && crm != 0) ||
2770 ((r->opc1 == CP_ANY) && opc1 != 0) ||
2771 ((r->opc2 == CP_ANY) && opc2 != 0)) {
2772 r2->type |= ARM_CP_NO_MIGRATE;
2773 }
2774
2775 /* Overriding of an existing definition must be explicitly
2776 * requested.
2777 */
2778 if (!(r->type & ARM_CP_OVERRIDE)) {
2779 ARMCPRegInfo *oldreg;
2780 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
2781 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
2782 fprintf(stderr, "Register redefined: cp=%d %d bit "
2783 "crn=%d crm=%d opc1=%d opc2=%d, "
2784 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
2785 r2->crn, r2->crm, r2->opc1, r2->opc2,
2786 oldreg->name, r2->name);
2787 g_assert_not_reached();
2788 }
2789 }
2790 g_hash_table_insert(cpu->cp_regs, key, r2);
2791}
2792
2793
4b6a83fb
PM
2794void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
2795 const ARMCPRegInfo *r, void *opaque)
2796{
2797 /* Define implementations of coprocessor registers.
2798 * We store these in a hashtable because typically
2799 * there are less than 150 registers in a space which
2800 * is 16*16*16*8*8 = 262144 in size.
2801 * Wildcarding is supported for the crm, opc1 and opc2 fields.
2802 * If a register is defined twice then the second definition is
2803 * used, so this can be used to define some generic registers and
2804 * then override them with implementation specific variations.
2805 * At least one of the original and the second definition should
2806 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
2807 * against accidental use.
f5a0a5a5
PM
2808 *
2809 * The state field defines whether the register is to be
2810 * visible in the AArch32 or AArch64 execution state. If the
2811 * state is set to ARM_CP_STATE_BOTH then we synthesise a
2812 * reginfo structure for the AArch32 view, which sees the lower
2813 * 32 bits of the 64 bit register.
2814 *
2815 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
2816 * be wildcarded. AArch64 registers are always considered to be 64
2817 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
2818 * the register, if any.
4b6a83fb 2819 */
f5a0a5a5 2820 int crm, opc1, opc2, state;
4b6a83fb
PM
2821 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
2822 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
2823 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
2824 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
2825 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
2826 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
2827 /* 64 bit registers have only CRm and Opc1 fields */
2828 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
2829 /* op0 only exists in the AArch64 encodings */
2830 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
2831 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
2832 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
2833 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
2834 * encodes a minimum access level for the register. We roll this
2835 * runtime check into our general permission check code, so check
2836 * here that the reginfo's specified permissions are strict enough
2837 * to encompass the generic architectural permission check.
2838 */
2839 if (r->state != ARM_CP_STATE_AA32) {
2840 int mask = 0;
2841 switch (r->opc1) {
2842 case 0: case 1: case 2:
2843 /* min_EL EL1 */
2844 mask = PL1_RW;
2845 break;
2846 case 3:
2847 /* min_EL EL0 */
2848 mask = PL0_RW;
2849 break;
2850 case 4:
2851 /* min_EL EL2 */
2852 mask = PL2_RW;
2853 break;
2854 case 5:
2855 /* unallocated encoding, so not possible */
2856 assert(false);
2857 break;
2858 case 6:
2859 /* min_EL EL3 */
2860 mask = PL3_RW;
2861 break;
2862 case 7:
2863 /* min_EL EL1, secure mode only (we don't check the latter) */
2864 mask = PL1_RW;
2865 break;
2866 default:
2867 /* broken reginfo with out-of-range opc1 */
2868 assert(false);
2869 break;
2870 }
2871 /* assert our permissions are not too lax (stricter is fine) */
2872 assert((r->access & ~mask) == 0);
2873 }
2874
4b6a83fb
PM
2875 /* Check that the register definition has enough info to handle
2876 * reads and writes if they are permitted.
2877 */
2878 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
2879 if (r->access & PL3_R) {
2880 assert(r->fieldoffset || r->readfn);
2881 }
2882 if (r->access & PL3_W) {
2883 assert(r->fieldoffset || r->writefn);
2884 }
2885 }
2886 /* Bad type field probably means missing sentinel at end of reg list */
2887 assert(cptype_valid(r->type));
2888 for (crm = crmmin; crm <= crmmax; crm++) {
2889 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
2890 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
2891 for (state = ARM_CP_STATE_AA32;
2892 state <= ARM_CP_STATE_AA64; state++) {
2893 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
2894 continue;
2895 }
2896 add_cpreg_to_hashtable(cpu, r, opaque, state,
2897 crm, opc1, opc2);
2898 }
4b6a83fb
PM
2899 }
2900 }
2901 }
2902}
2903
2904void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
2905 const ARMCPRegInfo *regs, void *opaque)
2906{
2907 /* Define a whole list of registers */
2908 const ARMCPRegInfo *r;
2909 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
2910 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
2911 }
2912}
2913
60322b39 2914const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 2915{
60322b39 2916 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
2917}
2918
c4241c7d
PM
2919void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
2920 uint64_t value)
4b6a83fb
PM
2921{
2922 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
2923}
2924
c4241c7d 2925uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
2926{
2927 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
2928 return 0;
2929}
2930
f5a0a5a5
PM
2931void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
2932{
2933 /* Helper coprocessor reset function for do-nothing-on-reset registers */
2934}
2935
0ecb72a5 2936static int bad_mode_switch(CPUARMState *env, int mode)
37064a8b
PM
2937{
2938 /* Return true if it is not valid for us to switch to
2939 * this CPU mode (ie all the UNPREDICTABLE cases in
2940 * the ARM ARM CPSRWriteByInstr pseudocode).
2941 */
2942 switch (mode) {
2943 case ARM_CPU_MODE_USR:
2944 case ARM_CPU_MODE_SYS:
2945 case ARM_CPU_MODE_SVC:
2946 case ARM_CPU_MODE_ABT:
2947 case ARM_CPU_MODE_UND:
2948 case ARM_CPU_MODE_IRQ:
2949 case ARM_CPU_MODE_FIQ:
2950 return 0;
2951 default:
2952 return 1;
2953 }
2954}
2955
2f4a40e5
AZ
2956uint32_t cpsr_read(CPUARMState *env)
2957{
2958 int ZF;
6fbe23d5
PB
2959 ZF = (env->ZF == 0);
2960 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
2961 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2962 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2963 | ((env->condexec_bits & 0xfc) << 8)
af519934 2964 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
2965}
2966
2967void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2968{
2f4a40e5 2969 if (mask & CPSR_NZCV) {
6fbe23d5
PB
2970 env->ZF = (~val) & CPSR_Z;
2971 env->NF = val;
2f4a40e5
AZ
2972 env->CF = (val >> 29) & 1;
2973 env->VF = (val << 3) & 0x80000000;
2974 }
2975 if (mask & CPSR_Q)
2976 env->QF = ((val & CPSR_Q) != 0);
2977 if (mask & CPSR_T)
2978 env->thumb = ((val & CPSR_T) != 0);
2979 if (mask & CPSR_IT_0_1) {
2980 env->condexec_bits &= ~3;
2981 env->condexec_bits |= (val >> 25) & 3;
2982 }
2983 if (mask & CPSR_IT_2_7) {
2984 env->condexec_bits &= 3;
2985 env->condexec_bits |= (val >> 8) & 0xfc;
2986 }
2987 if (mask & CPSR_GE) {
2988 env->GE = (val >> 16) & 0xf;
2989 }
2990
4cc35614
PM
2991 env->daif &= ~(CPSR_AIF & mask);
2992 env->daif |= val & CPSR_AIF & mask;
2993
2f4a40e5 2994 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
37064a8b
PM
2995 if (bad_mode_switch(env, val & CPSR_M)) {
2996 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2997 * We choose to ignore the attempt and leave the CPSR M field
2998 * untouched.
2999 */
3000 mask &= ~CPSR_M;
3001 } else {
3002 switch_mode(env, val & CPSR_M);
3003 }
2f4a40e5
AZ
3004 }
3005 mask &= ~CACHED_CPSR_BITS;
3006 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
3007}
3008
b26eefb6
PB
3009/* Sign/zero extend */
3010uint32_t HELPER(sxtb16)(uint32_t x)
3011{
3012 uint32_t res;
3013 res = (uint16_t)(int8_t)x;
3014 res |= (uint32_t)(int8_t)(x >> 16) << 16;
3015 return res;
3016}
3017
3018uint32_t HELPER(uxtb16)(uint32_t x)
3019{
3020 uint32_t res;
3021 res = (uint16_t)(uint8_t)x;
3022 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
3023 return res;
3024}
3025
f51bbbfe
PB
3026uint32_t HELPER(clz)(uint32_t x)
3027{
7bbcb0af 3028 return clz32(x);
f51bbbfe
PB
3029}
3030
3670669c
PB
3031int32_t HELPER(sdiv)(int32_t num, int32_t den)
3032{
3033 if (den == 0)
3034 return 0;
686eeb93
AJ
3035 if (num == INT_MIN && den == -1)
3036 return INT_MIN;
3670669c
PB
3037 return num / den;
3038}
3039
3040uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
3041{
3042 if (den == 0)
3043 return 0;
3044 return num / den;
3045}
3046
3047uint32_t HELPER(rbit)(uint32_t x)
3048{
3049 x = ((x & 0xff000000) >> 24)
3050 | ((x & 0x00ff0000) >> 8)
3051 | ((x & 0x0000ff00) << 8)
3052 | ((x & 0x000000ff) << 24);
3053 x = ((x & 0xf0f0f0f0) >> 4)
3054 | ((x & 0x0f0f0f0f) << 4);
3055 x = ((x & 0x88888888) >> 3)
3056 | ((x & 0x44444444) >> 1)
3057 | ((x & 0x22222222) << 1)
3058 | ((x & 0x11111111) << 3);
3059 return x;
3060}
3061
5fafdf24 3062#if defined(CONFIG_USER_ONLY)
b5ff1b31 3063
97a8ea5a 3064void arm_cpu_do_interrupt(CPUState *cs)
b5ff1b31 3065{
27103424 3066 cs->exception_index = -1;
b5ff1b31
FB
3067}
3068
7510454e
AF
3069int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
3070 int mmu_idx)
b5ff1b31 3071{
7510454e
AF
3072 ARMCPU *cpu = ARM_CPU(cs);
3073 CPUARMState *env = &cpu->env;
3074
abf1172f 3075 env->exception.vaddress = address;
b5ff1b31 3076 if (rw == 2) {
27103424 3077 cs->exception_index = EXCP_PREFETCH_ABORT;
b5ff1b31 3078 } else {
27103424 3079 cs->exception_index = EXCP_DATA_ABORT;
b5ff1b31
FB
3080 }
3081 return 1;
3082}
3083
9ee6e8bb 3084/* These should probably raise undefined insn exceptions. */
0ecb72a5 3085void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 3086{
a47dddd7
AF
3087 ARMCPU *cpu = arm_env_get_cpu(env);
3088
3089 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
3090}
3091
0ecb72a5 3092uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 3093{
a47dddd7
AF
3094 ARMCPU *cpu = arm_env_get_cpu(env);
3095
3096 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
3097 return 0;
3098}
3099
0ecb72a5 3100void switch_mode(CPUARMState *env, int mode)
b5ff1b31 3101{
a47dddd7
AF
3102 ARMCPU *cpu = arm_env_get_cpu(env);
3103
3104 if (mode != ARM_CPU_MODE_USR) {
3105 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
3106 }
b5ff1b31
FB
3107}
3108
0ecb72a5 3109void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
9ee6e8bb 3110{
a47dddd7
AF
3111 ARMCPU *cpu = arm_env_get_cpu(env);
3112
3113 cpu_abort(CPU(cpu), "banked r13 write\n");
9ee6e8bb
PB
3114}
3115
0ecb72a5 3116uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
9ee6e8bb 3117{
a47dddd7
AF
3118 ARMCPU *cpu = arm_env_get_cpu(env);
3119
3120 cpu_abort(CPU(cpu), "banked r13 read\n");
9ee6e8bb
PB
3121 return 0;
3122}
3123
b5ff1b31
FB
3124#else
3125
3126/* Map CPU modes onto saved register banks. */
494b00c7 3127int bank_number(int mode)
b5ff1b31
FB
3128{
3129 switch (mode) {
3130 case ARM_CPU_MODE_USR:
3131 case ARM_CPU_MODE_SYS:
3132 return 0;
3133 case ARM_CPU_MODE_SVC:
3134 return 1;
3135 case ARM_CPU_MODE_ABT:
3136 return 2;
3137 case ARM_CPU_MODE_UND:
3138 return 3;
3139 case ARM_CPU_MODE_IRQ:
3140 return 4;
3141 case ARM_CPU_MODE_FIQ:
3142 return 5;
28c9457d
EI
3143 case ARM_CPU_MODE_HYP:
3144 return 6;
3145 case ARM_CPU_MODE_MON:
3146 return 7;
b5ff1b31 3147 }
f5206413 3148 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
b5ff1b31
FB
3149}
3150
0ecb72a5 3151void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
3152{
3153 int old_mode;
3154 int i;
3155
3156 old_mode = env->uncached_cpsr & CPSR_M;
3157 if (mode == old_mode)
3158 return;
3159
3160 if (old_mode == ARM_CPU_MODE_FIQ) {
3161 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 3162 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
3163 } else if (mode == ARM_CPU_MODE_FIQ) {
3164 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 3165 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
3166 }
3167
f5206413 3168 i = bank_number(old_mode);
b5ff1b31
FB
3169 env->banked_r13[i] = env->regs[13];
3170 env->banked_r14[i] = env->regs[14];
3171 env->banked_spsr[i] = env->spsr;
3172
f5206413 3173 i = bank_number(mode);
b5ff1b31
FB
3174 env->regs[13] = env->banked_r13[i];
3175 env->regs[14] = env->banked_r14[i];
3176 env->spsr = env->banked_spsr[i];
3177}
3178
9ee6e8bb
PB
3179static void v7m_push(CPUARMState *env, uint32_t val)
3180{
70d74660
AF
3181 CPUState *cs = CPU(arm_env_get_cpu(env));
3182
9ee6e8bb 3183 env->regs[13] -= 4;
ab1da857 3184 stl_phys(cs->as, env->regs[13], val);
9ee6e8bb
PB
3185}
3186
3187static uint32_t v7m_pop(CPUARMState *env)
3188{
70d74660 3189 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb 3190 uint32_t val;
70d74660 3191
fdfba1a2 3192 val = ldl_phys(cs->as, env->regs[13]);
9ee6e8bb
PB
3193 env->regs[13] += 4;
3194 return val;
3195}
3196
3197/* Switch to V7M main or process stack pointer. */
3198static void switch_v7m_sp(CPUARMState *env, int process)
3199{
3200 uint32_t tmp;
3201 if (env->v7m.current_sp != process) {
3202 tmp = env->v7m.other_sp;
3203 env->v7m.other_sp = env->regs[13];
3204 env->regs[13] = tmp;
3205 env->v7m.current_sp = process;
3206 }
3207}
3208
3209static void do_v7m_exception_exit(CPUARMState *env)
3210{
3211 uint32_t type;
3212 uint32_t xpsr;
3213
3214 type = env->regs[15];
3215 if (env->v7m.exception != 0)
983fe826 3216 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
9ee6e8bb
PB
3217
3218 /* Switch to the target stack. */
3219 switch_v7m_sp(env, (type & 4) != 0);
3220 /* Pop registers. */
3221 env->regs[0] = v7m_pop(env);
3222 env->regs[1] = v7m_pop(env);
3223 env->regs[2] = v7m_pop(env);
3224 env->regs[3] = v7m_pop(env);
3225 env->regs[12] = v7m_pop(env);
3226 env->regs[14] = v7m_pop(env);
3227 env->regs[15] = v7m_pop(env);
3228 xpsr = v7m_pop(env);
3229 xpsr_write(env, xpsr, 0xfffffdff);
3230 /* Undo stack alignment. */
3231 if (xpsr & 0x200)
3232 env->regs[13] |= 4;
3233 /* ??? The exception return type specifies Thread/Handler mode. However
3234 this is also implied by the xPSR value. Not sure what to do
3235 if there is a mismatch. */
3236 /* ??? Likewise for mismatches between the CONTROL register and the stack
3237 pointer. */
3238}
3239
e6f010cc 3240void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 3241{
e6f010cc
AF
3242 ARMCPU *cpu = ARM_CPU(cs);
3243 CPUARMState *env = &cpu->env;
9ee6e8bb
PB
3244 uint32_t xpsr = xpsr_read(env);
3245 uint32_t lr;
3246 uint32_t addr;
3247
27103424 3248 arm_log_exception(cs->exception_index);
3f1beaca 3249
9ee6e8bb
PB
3250 lr = 0xfffffff1;
3251 if (env->v7m.current_sp)
3252 lr |= 4;
3253 if (env->v7m.exception == 0)
3254 lr |= 8;
3255
3256 /* For exceptions we just mark as pending on the NVIC, and let that
3257 handle it. */
3258 /* TODO: Need to escalate if the current priority is higher than the
3259 one we're raising. */
27103424 3260 switch (cs->exception_index) {
9ee6e8bb 3261 case EXCP_UDEF:
983fe826 3262 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
9ee6e8bb
PB
3263 return;
3264 case EXCP_SWI:
314e2296 3265 /* The PC already points to the next instruction. */
983fe826 3266 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
9ee6e8bb
PB
3267 return;
3268 case EXCP_PREFETCH_ABORT:
3269 case EXCP_DATA_ABORT:
abf1172f
PM
3270 /* TODO: if we implemented the MPU registers, this is where we
3271 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
3272 */
983fe826 3273 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
9ee6e8bb
PB
3274 return;
3275 case EXCP_BKPT:
2ad207d4
PB
3276 if (semihosting_enabled) {
3277 int nr;
d31dd73e 3278 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2ad207d4
PB
3279 if (nr == 0xab) {
3280 env->regs[15] += 2;
3281 env->regs[0] = do_arm_semihosting(env);
3f1beaca 3282 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2ad207d4
PB
3283 return;
3284 }
3285 }
983fe826 3286 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
9ee6e8bb
PB
3287 return;
3288 case EXCP_IRQ:
983fe826 3289 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
9ee6e8bb
PB
3290 break;
3291 case EXCP_EXCEPTION_EXIT:
3292 do_v7m_exception_exit(env);
3293 return;
3294 default:
a47dddd7 3295 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
3296 return; /* Never happens. Keep compiler happy. */
3297 }
3298
3299 /* Align stack pointer. */
3300 /* ??? Should only do this if Configuration Control Register
3301 STACKALIGN bit is set. */
3302 if (env->regs[13] & 4) {
ab19b0ec 3303 env->regs[13] -= 4;
9ee6e8bb
PB
3304 xpsr |= 0x200;
3305 }
6c95676b 3306 /* Switch to the handler mode. */
9ee6e8bb
PB
3307 v7m_push(env, xpsr);
3308 v7m_push(env, env->regs[15]);
3309 v7m_push(env, env->regs[14]);
3310 v7m_push(env, env->regs[12]);
3311 v7m_push(env, env->regs[3]);
3312 v7m_push(env, env->regs[2]);
3313 v7m_push(env, env->regs[1]);
3314 v7m_push(env, env->regs[0]);
3315 switch_v7m_sp(env, 0);
c98d174c
PM
3316 /* Clear IT bits */
3317 env->condexec_bits = 0;
9ee6e8bb 3318 env->regs[14] = lr;
fdfba1a2 3319 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
9ee6e8bb
PB
3320 env->regs[15] = addr & 0xfffffffe;
3321 env->thumb = addr & 1;
3322}
3323
b5ff1b31 3324/* Handle a CPU exception. */
97a8ea5a 3325void arm_cpu_do_interrupt(CPUState *cs)
b5ff1b31 3326{
97a8ea5a
AF
3327 ARMCPU *cpu = ARM_CPU(cs);
3328 CPUARMState *env = &cpu->env;
b5ff1b31
FB
3329 uint32_t addr;
3330 uint32_t mask;
3331 int new_mode;
3332 uint32_t offset;
3333
e6f010cc
AF
3334 assert(!IS_M(env));
3335
27103424 3336 arm_log_exception(cs->exception_index);
3f1beaca 3337
b5ff1b31 3338 /* TODO: Vectored interrupt controller. */
27103424 3339 switch (cs->exception_index) {
b5ff1b31
FB
3340 case EXCP_UDEF:
3341 new_mode = ARM_CPU_MODE_UND;
3342 addr = 0x04;
3343 mask = CPSR_I;
3344 if (env->thumb)
3345 offset = 2;
3346 else
3347 offset = 4;
3348 break;
3349 case EXCP_SWI:
8e71621f
PB
3350 if (semihosting_enabled) {
3351 /* Check for semihosting interrupt. */
3352 if (env->thumb) {
d31dd73e
BS
3353 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
3354 & 0xff;
8e71621f 3355 } else {
d31dd73e 3356 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
d8fd2954 3357 & 0xffffff;
8e71621f
PB
3358 }
3359 /* Only intercept calls from privileged modes, to provide some
3360 semblance of security. */
3361 if (((mask == 0x123456 && !env->thumb)
3362 || (mask == 0xab && env->thumb))
3363 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
3364 env->regs[0] = do_arm_semihosting(env);
3f1beaca 3365 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
8e71621f
PB
3366 return;
3367 }
3368 }
b5ff1b31
FB
3369 new_mode = ARM_CPU_MODE_SVC;
3370 addr = 0x08;
3371 mask = CPSR_I;
601d70b9 3372 /* The PC already points to the next instruction. */
b5ff1b31
FB
3373 offset = 0;
3374 break;
06c949e6 3375 case EXCP_BKPT:
9ee6e8bb 3376 /* See if this is a semihosting syscall. */
2ad207d4 3377 if (env->thumb && semihosting_enabled) {
d31dd73e 3378 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
9ee6e8bb
PB
3379 if (mask == 0xab
3380 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
3381 env->regs[15] += 2;
3382 env->regs[0] = do_arm_semihosting(env);
3f1beaca 3383 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
9ee6e8bb
PB
3384 return;
3385 }
3386 }
abf1172f 3387 env->exception.fsr = 2;
9ee6e8bb
PB
3388 /* Fall through to prefetch abort. */
3389 case EXCP_PREFETCH_ABORT:
6cd8a264
RH
3390 env->cp15.ifsr_el2 = env->exception.fsr;
3391 env->cp15.far_el1 = deposit64(env->cp15.far_el1, 32, 32,
3392 env->exception.vaddress);
3f1beaca 3393 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
6cd8a264 3394 env->cp15.ifsr_el2, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
3395 new_mode = ARM_CPU_MODE_ABT;
3396 addr = 0x0c;
3397 mask = CPSR_A | CPSR_I;
3398 offset = 4;
3399 break;
3400 case EXCP_DATA_ABORT:
d81c519c 3401 env->cp15.esr_el[1] = env->exception.fsr;
6cd8a264
RH
3402 env->cp15.far_el1 = deposit64(env->cp15.far_el1, 0, 32,
3403 env->exception.vaddress);
3f1beaca 3404 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
d81c519c 3405 (uint32_t)env->cp15.esr_el[1],
6cd8a264 3406 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
3407 new_mode = ARM_CPU_MODE_ABT;
3408 addr = 0x10;
3409 mask = CPSR_A | CPSR_I;
3410 offset = 8;
3411 break;
3412 case EXCP_IRQ:
3413 new_mode = ARM_CPU_MODE_IRQ;
3414 addr = 0x18;
3415 /* Disable IRQ and imprecise data aborts. */
3416 mask = CPSR_A | CPSR_I;
3417 offset = 4;
3418 break;
3419 case EXCP_FIQ:
3420 new_mode = ARM_CPU_MODE_FIQ;
3421 addr = 0x1c;
3422 /* Disable FIQ, IRQ and imprecise data aborts. */
3423 mask = CPSR_A | CPSR_I | CPSR_F;
3424 offset = 4;
3425 break;
3426 default:
a47dddd7 3427 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
3428 return; /* Never happens. Keep compiler happy. */
3429 }
3430 /* High vectors. */
76e3e1bc 3431 if (env->cp15.c1_sys & SCTLR_V) {
8641136c 3432 /* when enabled, base address cannot be remapped. */
b5ff1b31 3433 addr += 0xffff0000;
8641136c
NR
3434 } else {
3435 /* ARM v7 architectures provide a vector base address register to remap
3436 * the interrupt vector table.
3437 * This register is only followed in non-monitor mode, and has a secure
3438 * and un-secure copy. Since the cpu is always in a un-secure operation
3439 * and is never in monitor mode this feature is always active.
3440 * Note: only bits 31:5 are valid.
3441 */
68fdb6c5 3442 addr += env->cp15.vbar_el[1];
b5ff1b31
FB
3443 }
3444 switch_mode (env, new_mode);
3445 env->spsr = cpsr_read(env);
9ee6e8bb
PB
3446 /* Clear IT bits. */
3447 env->condexec_bits = 0;
30a8cac1 3448 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 3449 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
4cc35614 3450 env->daif |= mask;
be5e7a76
DES
3451 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
3452 * and we should just guard the thumb mode on V4 */
3453 if (arm_feature(env, ARM_FEATURE_V4T)) {
76e3e1bc 3454 env->thumb = (env->cp15.c1_sys & SCTLR_TE) != 0;
be5e7a76 3455 }
b5ff1b31
FB
3456 env->regs[14] = env->regs[15] + offset;
3457 env->regs[15] = addr;
259186a7 3458 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
b5ff1b31
FB
3459}
3460
3461/* Check section/page access permissions.
3462 Returns the page protection flags, or zero if the access is not
3463 permitted. */
0ecb72a5 3464static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
dd4ebc2e 3465 int access_type, int is_user)
b5ff1b31 3466{
9ee6e8bb
PB
3467 int prot_ro;
3468
dd4ebc2e 3469 if (domain_prot == 3) {
b5ff1b31 3470 return PAGE_READ | PAGE_WRITE;
dd4ebc2e 3471 }
b5ff1b31 3472
9ee6e8bb
PB
3473 if (access_type == 1)
3474 prot_ro = 0;
3475 else
3476 prot_ro = PAGE_READ;
3477
b5ff1b31
FB
3478 switch (ap) {
3479 case 0:
99f678a6
PM
3480 if (arm_feature(env, ARM_FEATURE_V7)) {
3481 return 0;
3482 }
78600320 3483 if (access_type == 1)
b5ff1b31 3484 return 0;
76e3e1bc
PM
3485 switch (env->cp15.c1_sys & (SCTLR_S | SCTLR_R)) {
3486 case SCTLR_S:
b5ff1b31 3487 return is_user ? 0 : PAGE_READ;
76e3e1bc 3488 case SCTLR_R:
b5ff1b31
FB
3489 return PAGE_READ;
3490 default:
3491 return 0;
3492 }
3493 case 1:
3494 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
3495 case 2:
3496 if (is_user)
9ee6e8bb 3497 return prot_ro;
b5ff1b31
FB
3498 else
3499 return PAGE_READ | PAGE_WRITE;
3500 case 3:
3501 return PAGE_READ | PAGE_WRITE;
d4934d18 3502 case 4: /* Reserved. */
9ee6e8bb
PB
3503 return 0;
3504 case 5:
3505 return is_user ? 0 : prot_ro;
3506 case 6:
3507 return prot_ro;
d4934d18 3508 case 7:
0ab06d83 3509 if (!arm_feature (env, ARM_FEATURE_V6K))
d4934d18
PB
3510 return 0;
3511 return prot_ro;
b5ff1b31
FB
3512 default:
3513 abort();
3514 }
3515}
3516
0ecb72a5 3517static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
b2fa1797
PB
3518{
3519 uint32_t table;
3520
3521 if (address & env->cp15.c2_mask)
327ed10f 3522 table = env->cp15.ttbr1_el1 & 0xffffc000;
b2fa1797 3523 else
327ed10f 3524 table = env->cp15.ttbr0_el1 & env->cp15.c2_base_mask;
b2fa1797
PB
3525
3526 table |= (address >> 18) & 0x3ffc;
3527 return table;
3528}
3529
0ecb72a5 3530static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
a8170e5e 3531 int is_user, hwaddr *phys_ptr,
77a71dd1 3532 int *prot, target_ulong *page_size)
b5ff1b31 3533{
70d74660 3534 CPUState *cs = CPU(arm_env_get_cpu(env));
b5ff1b31
FB
3535 int code;
3536 uint32_t table;
3537 uint32_t desc;
3538 int type;
3539 int ap;
3540 int domain;
dd4ebc2e 3541 int domain_prot;
a8170e5e 3542 hwaddr phys_addr;
b5ff1b31 3543
9ee6e8bb
PB
3544 /* Pagetable walk. */
3545 /* Lookup l1 descriptor. */
b2fa1797 3546 table = get_level1_table_address(env, address);
fdfba1a2 3547 desc = ldl_phys(cs->as, table);
9ee6e8bb 3548 type = (desc & 3);
dd4ebc2e
JCD
3549 domain = (desc >> 5) & 0x0f;
3550 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
9ee6e8bb 3551 if (type == 0) {
601d70b9 3552 /* Section translation fault. */
9ee6e8bb
PB
3553 code = 5;
3554 goto do_fault;
3555 }
dd4ebc2e 3556 if (domain_prot == 0 || domain_prot == 2) {
9ee6e8bb
PB
3557 if (type == 2)
3558 code = 9; /* Section domain fault. */
3559 else
3560 code = 11; /* Page domain fault. */
3561 goto do_fault;
3562 }
3563 if (type == 2) {
3564 /* 1Mb section. */
3565 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
3566 ap = (desc >> 10) & 3;
3567 code = 13;
d4c430a8 3568 *page_size = 1024 * 1024;
9ee6e8bb
PB
3569 } else {
3570 /* Lookup l2 entry. */
3571 if (type == 1) {
3572 /* Coarse pagetable. */
3573 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
3574 } else {
3575 /* Fine pagetable. */
3576 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
3577 }
fdfba1a2 3578 desc = ldl_phys(cs->as, table);
9ee6e8bb
PB
3579 switch (desc & 3) {
3580 case 0: /* Page translation fault. */
3581 code = 7;
3582 goto do_fault;
3583 case 1: /* 64k page. */
3584 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
3585 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 3586 *page_size = 0x10000;
ce819861 3587 break;
9ee6e8bb
PB
3588 case 2: /* 4k page. */
3589 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 3590 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 3591 *page_size = 0x1000;
ce819861 3592 break;
9ee6e8bb
PB
3593 case 3: /* 1k page. */
3594 if (type == 1) {
3595 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3596 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
3597 } else {
3598 /* Page translation fault. */
3599 code = 7;
3600 goto do_fault;
3601 }
3602 } else {
3603 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
3604 }
3605 ap = (desc >> 4) & 3;
d4c430a8 3606 *page_size = 0x400;
ce819861
PB
3607 break;
3608 default:
9ee6e8bb
PB
3609 /* Never happens, but compiler isn't smart enough to tell. */
3610 abort();
ce819861 3611 }
9ee6e8bb
PB
3612 code = 15;
3613 }
dd4ebc2e 3614 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
9ee6e8bb
PB
3615 if (!*prot) {
3616 /* Access permission fault. */
3617 goto do_fault;
3618 }
3ad493fc 3619 *prot |= PAGE_EXEC;
9ee6e8bb
PB
3620 *phys_ptr = phys_addr;
3621 return 0;
3622do_fault:
3623 return code | (domain << 4);
3624}
3625
0ecb72a5 3626static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
a8170e5e 3627 int is_user, hwaddr *phys_ptr,
77a71dd1 3628 int *prot, target_ulong *page_size)
9ee6e8bb 3629{
70d74660 3630 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb
PB
3631 int code;
3632 uint32_t table;
3633 uint32_t desc;
3634 uint32_t xn;
de9b05b8 3635 uint32_t pxn = 0;
9ee6e8bb
PB
3636 int type;
3637 int ap;
de9b05b8 3638 int domain = 0;
dd4ebc2e 3639 int domain_prot;
a8170e5e 3640 hwaddr phys_addr;
9ee6e8bb
PB
3641
3642 /* Pagetable walk. */
3643 /* Lookup l1 descriptor. */
b2fa1797 3644 table = get_level1_table_address(env, address);
fdfba1a2 3645 desc = ldl_phys(cs->as, table);
9ee6e8bb 3646 type = (desc & 3);
de9b05b8
PM
3647 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
3648 /* Section translation fault, or attempt to use the encoding
3649 * which is Reserved on implementations without PXN.
3650 */
9ee6e8bb 3651 code = 5;
9ee6e8bb 3652 goto do_fault;
de9b05b8
PM
3653 }
3654 if ((type == 1) || !(desc & (1 << 18))) {
3655 /* Page or Section. */
dd4ebc2e 3656 domain = (desc >> 5) & 0x0f;
9ee6e8bb 3657 }
dd4ebc2e
JCD
3658 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
3659 if (domain_prot == 0 || domain_prot == 2) {
de9b05b8 3660 if (type != 1) {
9ee6e8bb 3661 code = 9; /* Section domain fault. */
de9b05b8 3662 } else {
9ee6e8bb 3663 code = 11; /* Page domain fault. */
de9b05b8 3664 }
9ee6e8bb
PB
3665 goto do_fault;
3666 }
de9b05b8 3667 if (type != 1) {
9ee6e8bb
PB
3668 if (desc & (1 << 18)) {
3669 /* Supersection. */
3670 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
d4c430a8 3671 *page_size = 0x1000000;
b5ff1b31 3672 } else {
9ee6e8bb
PB
3673 /* Section. */
3674 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 3675 *page_size = 0x100000;
b5ff1b31 3676 }
9ee6e8bb
PB
3677 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
3678 xn = desc & (1 << 4);
de9b05b8 3679 pxn = desc & 1;
9ee6e8bb
PB
3680 code = 13;
3681 } else {
de9b05b8
PM
3682 if (arm_feature(env, ARM_FEATURE_PXN)) {
3683 pxn = (desc >> 2) & 1;
3684 }
9ee6e8bb
PB
3685 /* Lookup l2 entry. */
3686 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
fdfba1a2 3687 desc = ldl_phys(cs->as, table);
9ee6e8bb
PB
3688 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
3689 switch (desc & 3) {
3690 case 0: /* Page translation fault. */
3691 code = 7;
b5ff1b31 3692 goto do_fault;
9ee6e8bb
PB
3693 case 1: /* 64k page. */
3694 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
3695 xn = desc & (1 << 15);
d4c430a8 3696 *page_size = 0x10000;
9ee6e8bb
PB
3697 break;
3698 case 2: case 3: /* 4k page. */
3699 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
3700 xn = desc & 1;
d4c430a8 3701 *page_size = 0x1000;
9ee6e8bb
PB
3702 break;
3703 default:
3704 /* Never happens, but compiler isn't smart enough to tell. */
3705 abort();
b5ff1b31 3706 }
9ee6e8bb
PB
3707 code = 15;
3708 }
dd4ebc2e 3709 if (domain_prot == 3) {
c0034328
JR
3710 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3711 } else {
de9b05b8
PM
3712 if (pxn && !is_user) {
3713 xn = 1;
3714 }
c0034328
JR
3715 if (xn && access_type == 2)
3716 goto do_fault;
9ee6e8bb 3717
c0034328 3718 /* The simplified model uses AP[0] as an access control bit. */
76e3e1bc 3719 if ((env->cp15.c1_sys & SCTLR_AFE) && (ap & 1) == 0) {
c0034328
JR
3720 /* Access flag fault. */
3721 code = (code == 15) ? 6 : 3;
3722 goto do_fault;
3723 }
dd4ebc2e 3724 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
c0034328
JR
3725 if (!*prot) {
3726 /* Access permission fault. */
3727 goto do_fault;
3728 }
3729 if (!xn) {
3730 *prot |= PAGE_EXEC;
3731 }
3ad493fc 3732 }
9ee6e8bb 3733 *phys_ptr = phys_addr;
b5ff1b31
FB
3734 return 0;
3735do_fault:
3736 return code | (domain << 4);
3737}
3738
3dde962f
PM
3739/* Fault type for long-descriptor MMU fault reporting; this corresponds
3740 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
3741 */
3742typedef enum {
3743 translation_fault = 1,
3744 access_fault = 2,
3745 permission_fault = 3,
3746} MMUFaultType;
3747
2c8dd318 3748static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,
3dde962f 3749 int access_type, int is_user,
a8170e5e 3750 hwaddr *phys_ptr, int *prot,
3dde962f
PM
3751 target_ulong *page_size_ptr)
3752{
70d74660 3753 CPUState *cs = CPU(arm_env_get_cpu(env));
3dde962f
PM
3754 /* Read an LPAE long-descriptor translation table. */
3755 MMUFaultType fault_type = translation_fault;
3756 uint32_t level = 1;
3757 uint32_t epd;
2c8dd318
RH
3758 int32_t tsz;
3759 uint32_t tg;
3dde962f
PM
3760 uint64_t ttbr;
3761 int ttbr_select;
2c8dd318 3762 hwaddr descaddr, descmask;
3dde962f
PM
3763 uint32_t tableattrs;
3764 target_ulong page_size;
3765 uint32_t attrs;
2c8dd318
RH
3766 int32_t granule_sz = 9;
3767 int32_t va_size = 32;
3768 int32_t tbi = 0;
3769
3770 if (arm_el_is_aa64(env, 1)) {
3771 va_size = 64;
3772 if (extract64(address, 55, 1))
3773 tbi = extract64(env->cp15.c2_control, 38, 1);
3774 else
3775 tbi = extract64(env->cp15.c2_control, 37, 1);
3776 tbi *= 8;
3777 }
3dde962f
PM
3778
3779 /* Determine whether this address is in the region controlled by
3780 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
3781 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
3782 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
3783 */
2c8dd318
RH
3784 uint32_t t0sz = extract32(env->cp15.c2_control, 0, 6);
3785 if (arm_el_is_aa64(env, 1)) {
3786 t0sz = MIN(t0sz, 39);
3787 t0sz = MAX(t0sz, 16);
3788 }
3789 uint32_t t1sz = extract32(env->cp15.c2_control, 16, 6);
3790 if (arm_el_is_aa64(env, 1)) {
3791 t1sz = MIN(t1sz, 39);
3792 t1sz = MAX(t1sz, 16);
3793 }
3794 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
3dde962f
PM
3795 /* there is a ttbr0 region and we are in it (high bits all zero) */
3796 ttbr_select = 0;
2c8dd318 3797 } else if (t1sz && !extract64(~address, va_size - t1sz, t1sz - tbi)) {
3dde962f
PM
3798 /* there is a ttbr1 region and we are in it (high bits all one) */
3799 ttbr_select = 1;
3800 } else if (!t0sz) {
3801 /* ttbr0 region is "everything not in the ttbr1 region" */
3802 ttbr_select = 0;
3803 } else if (!t1sz) {
3804 /* ttbr1 region is "everything not in the ttbr0 region" */
3805 ttbr_select = 1;
3806 } else {
3807 /* in the gap between the two regions, this is a Translation fault */
3808 fault_type = translation_fault;
3809 goto do_fault;
3810 }
3811
3812 /* Note that QEMU ignores shareability and cacheability attributes,
3813 * so we don't need to do anything with the SH, ORGN, IRGN fields
3814 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
3815 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
3816 * implement any ASID-like capability so we can ignore it (instead
3817 * we will always flush the TLB any time the ASID is changed).
3818 */
3819 if (ttbr_select == 0) {
327ed10f 3820 ttbr = env->cp15.ttbr0_el1;
3dde962f
PM
3821 epd = extract32(env->cp15.c2_control, 7, 1);
3822 tsz = t0sz;
2c8dd318
RH
3823
3824 tg = extract32(env->cp15.c2_control, 14, 2);
3825 if (tg == 1) { /* 64KB pages */
3826 granule_sz = 13;
3827 }
3828 if (tg == 2) { /* 16KB pages */
3829 granule_sz = 11;
3830 }
3dde962f 3831 } else {
327ed10f 3832 ttbr = env->cp15.ttbr1_el1;
3dde962f
PM
3833 epd = extract32(env->cp15.c2_control, 23, 1);
3834 tsz = t1sz;
2c8dd318
RH
3835
3836 tg = extract32(env->cp15.c2_control, 30, 2);
3837 if (tg == 3) { /* 64KB pages */
3838 granule_sz = 13;
3839 }
3840 if (tg == 1) { /* 16KB pages */
3841 granule_sz = 11;
3842 }
3dde962f
PM
3843 }
3844
3845 if (epd) {
3846 /* Translation table walk disabled => Translation fault on TLB miss */
3847 goto do_fault;
3848 }
3849
2c8dd318
RH
3850 /* The starting level depends on the virtual address size which can be
3851 * up to 48-bits and the translation granule size.
3dde962f 3852 */
2c8dd318
RH
3853 if ((va_size - tsz) > (granule_sz * 4 + 3)) {
3854 level = 0;
3855 } else if ((va_size - tsz) > (granule_sz * 3 + 3)) {
3856 level = 1;
3dde962f 3857 } else {
2c8dd318 3858 level = 2;
3dde962f
PM
3859 }
3860
3861 /* Clear the vaddr bits which aren't part of the within-region address,
3862 * so that we don't have to special case things when calculating the
3863 * first descriptor address.
3864 */
2c8dd318
RH
3865 if (tsz) {
3866 address &= (1ULL << (va_size - tsz)) - 1;
3867 }
3868
3869 descmask = (1ULL << (granule_sz + 3)) - 1;
3dde962f
PM
3870
3871 /* Now we can extract the actual base address from the TTBR */
2c8dd318
RH
3872 descaddr = extract64(ttbr, 0, 48);
3873 descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
3dde962f
PM
3874
3875 tableattrs = 0;
3876 for (;;) {
3877 uint64_t descriptor;
3878
2c8dd318
RH
3879 descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
3880 descaddr &= ~7ULL;
2c17449b 3881 descriptor = ldq_phys(cs->as, descaddr);
3dde962f
PM
3882 if (!(descriptor & 1) ||
3883 (!(descriptor & 2) && (level == 3))) {
3884 /* Invalid, or the Reserved level 3 encoding */
3885 goto do_fault;
3886 }
3887 descaddr = descriptor & 0xfffffff000ULL;
3888
3889 if ((descriptor & 2) && (level < 3)) {
3890 /* Table entry. The top five bits are attributes which may
3891 * propagate down through lower levels of the table (and
3892 * which are all arranged so that 0 means "no effect", so
3893 * we can gather them up by ORing in the bits at each level).
3894 */
3895 tableattrs |= extract64(descriptor, 59, 5);
3896 level++;
3897 continue;
3898 }
3899 /* Block entry at level 1 or 2, or page entry at level 3.
3900 * These are basically the same thing, although the number
3901 * of bits we pull in from the vaddr varies.
3902 */
2c8dd318 3903 page_size = (1 << ((granule_sz * (4 - level)) + 3));
3dde962f
PM
3904 descaddr |= (address & (page_size - 1));
3905 /* Extract attributes from the descriptor and merge with table attrs */
2c8dd318
RH
3906 if (arm_feature(env, ARM_FEATURE_V8)) {
3907 attrs = extract64(descriptor, 2, 10)
3908 | (extract64(descriptor, 53, 11) << 10);
3909 } else {
3910 attrs = extract64(descriptor, 2, 10)
3911 | (extract64(descriptor, 52, 12) << 10);
3912 }
3dde962f
PM
3913 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3914 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
3915 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
3916 * means "force PL1 access only", which means forcing AP[1] to 0.
3917 */
3918 if (extract32(tableattrs, 2, 1)) {
3919 attrs &= ~(1 << 4);
3920 }
3921 /* Since we're always in the Non-secure state, NSTable is ignored. */
3922 break;
3923 }
3924 /* Here descaddr is the final physical address, and attributes
3925 * are all in attrs.
3926 */
3927 fault_type = access_fault;
3928 if ((attrs & (1 << 8)) == 0) {
3929 /* Access flag */
3930 goto do_fault;
3931 }
3932 fault_type = permission_fault;
3933 if (is_user && !(attrs & (1 << 4))) {
3934 /* Unprivileged access not enabled */
3935 goto do_fault;
3936 }
3937 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3938 if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) {
3939 /* XN or PXN */
3940 if (access_type == 2) {
3941 goto do_fault;
3942 }
3943 *prot &= ~PAGE_EXEC;
3944 }
3945 if (attrs & (1 << 5)) {
3946 /* Write access forbidden */
3947 if (access_type == 1) {
3948 goto do_fault;
3949 }
3950 *prot &= ~PAGE_WRITE;
3951 }
3952
3953 *phys_ptr = descaddr;
3954 *page_size_ptr = page_size;
3955 return 0;
3956
3957do_fault:
3958 /* Long-descriptor format IFSR/DFSR value */
3959 return (1 << 9) | (fault_type << 2) | level;
3960}
3961
77a71dd1
PM
3962static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
3963 int access_type, int is_user,
a8170e5e 3964 hwaddr *phys_ptr, int *prot)
9ee6e8bb
PB
3965{
3966 int n;
3967 uint32_t mask;
3968 uint32_t base;
3969
3970 *phys_ptr = address;
3971 for (n = 7; n >= 0; n--) {
3972 base = env->cp15.c6_region[n];
3973 if ((base & 1) == 0)
3974 continue;
3975 mask = 1 << ((base >> 1) & 0x1f);
3976 /* Keep this shift separate from the above to avoid an
3977 (undefined) << 32. */
3978 mask = (mask << 1) - 1;
3979 if (((base ^ address) & ~mask) == 0)
3980 break;
3981 }
3982 if (n < 0)
3983 return 2;
3984
3985 if (access_type == 2) {
7e09797c 3986 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 3987 } else {
7e09797c 3988 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
3989 }
3990 mask = (mask >> (n * 4)) & 0xf;
3991 switch (mask) {
3992 case 0:
3993 return 1;
3994 case 1:
3995 if (is_user)
3996 return 1;
3997 *prot = PAGE_READ | PAGE_WRITE;
3998 break;
3999 case 2:
4000 *prot = PAGE_READ;
4001 if (!is_user)
4002 *prot |= PAGE_WRITE;
4003 break;
4004 case 3:
4005 *prot = PAGE_READ | PAGE_WRITE;
4006 break;
4007 case 5:
4008 if (is_user)
4009 return 1;
4010 *prot = PAGE_READ;
4011 break;
4012 case 6:
4013 *prot = PAGE_READ;
4014 break;
4015 default:
4016 /* Bad permission. */
4017 return 1;
4018 }
3ad493fc 4019 *prot |= PAGE_EXEC;
9ee6e8bb
PB
4020 return 0;
4021}
4022
702a9357
PM
4023/* get_phys_addr - get the physical address for this virtual address
4024 *
4025 * Find the physical address corresponding to the given virtual address,
4026 * by doing a translation table walk on MMU based systems or using the
4027 * MPU state on MPU based systems.
4028 *
4029 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
4030 * prot and page_size are not filled in, and the return value provides
4031 * information on why the translation aborted, in the format of a
4032 * DFSR/IFSR fault register, with the following caveats:
4033 * * we honour the short vs long DFSR format differences.
4034 * * the WnR bit is never set (the caller must do this).
4035 * * for MPU based systems we don't bother to return a full FSR format
4036 * value.
4037 *
4038 * @env: CPUARMState
4039 * @address: virtual address to get physical address for
4040 * @access_type: 0 for read, 1 for write, 2 for execute
4041 * @is_user: 0 for privileged access, 1 for user
4042 * @phys_ptr: set to the physical address corresponding to the virtual address
4043 * @prot: set to the permissions for the page containing phys_ptr
4044 * @page_size: set to the size of the page containing phys_ptr
4045 */
2c8dd318 4046static inline int get_phys_addr(CPUARMState *env, target_ulong address,
9ee6e8bb 4047 int access_type, int is_user,
a8170e5e 4048 hwaddr *phys_ptr, int *prot,
d4c430a8 4049 target_ulong *page_size)
9ee6e8bb
PB
4050{
4051 /* Fast Context Switch Extension. */
4052 if (address < 0x02000000)
4053 address += env->cp15.c13_fcse;
4054
76e3e1bc 4055 if ((env->cp15.c1_sys & SCTLR_M) == 0) {
9ee6e8bb
PB
4056 /* MMU/MPU disabled. */
4057 *phys_ptr = address;
3ad493fc 4058 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 4059 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb
PB
4060 return 0;
4061 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
d4c430a8 4062 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb
PB
4063 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
4064 prot);
3dde962f
PM
4065 } else if (extended_addresses_enabled(env)) {
4066 return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
4067 prot, page_size);
76e3e1bc 4068 } else if (env->cp15.c1_sys & SCTLR_XP) {
9ee6e8bb 4069 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
d4c430a8 4070 prot, page_size);
9ee6e8bb
PB
4071 } else {
4072 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
d4c430a8 4073 prot, page_size);
9ee6e8bb
PB
4074 }
4075}
4076
7510454e
AF
4077int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
4078 int access_type, int mmu_idx)
b5ff1b31 4079{
7510454e
AF
4080 ARMCPU *cpu = ARM_CPU(cs);
4081 CPUARMState *env = &cpu->env;
a8170e5e 4082 hwaddr phys_addr;
d4c430a8 4083 target_ulong page_size;
b5ff1b31 4084 int prot;
6ebbf390 4085 int ret, is_user;
00892383
RH
4086 uint32_t syn;
4087 bool same_el = (arm_current_pl(env) != 0);
b5ff1b31 4088
6ebbf390 4089 is_user = mmu_idx == MMU_USER_IDX;
d4c430a8
PB
4090 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
4091 &page_size);
b5ff1b31
FB
4092 if (ret == 0) {
4093 /* Map a single [sub]page. */
a8170e5e 4094 phys_addr &= ~(hwaddr)0x3ff;
2c8dd318 4095 address &= ~(target_ulong)0x3ff;
0c591eb0 4096 tlb_set_page(cs, address, phys_addr, prot, mmu_idx, page_size);
d4c430a8 4097 return 0;
b5ff1b31
FB
4098 }
4099
00892383
RH
4100 /* AArch64 syndrome does not have an LPAE bit */
4101 syn = ret & ~(1 << 9);
4102
4103 /* For insn and data aborts we assume there is no instruction syndrome
4104 * information; this is always true for exceptions reported to EL1.
4105 */
b5ff1b31 4106 if (access_type == 2) {
00892383 4107 syn = syn_insn_abort(same_el, 0, 0, syn);
27103424 4108 cs->exception_index = EXCP_PREFETCH_ABORT;
b5ff1b31 4109 } else {
00892383 4110 syn = syn_data_abort(same_el, 0, 0, 0, access_type == 1, syn);
abf1172f
PM
4111 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) {
4112 ret |= (1 << 11);
4113 }
27103424 4114 cs->exception_index = EXCP_DATA_ABORT;
b5ff1b31 4115 }
00892383
RH
4116
4117 env->exception.syndrome = syn;
abf1172f
PM
4118 env->exception.vaddress = address;
4119 env->exception.fsr = ret;
b5ff1b31
FB
4120 return 1;
4121}
4122
00b941e5 4123hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
b5ff1b31 4124{
00b941e5 4125 ARMCPU *cpu = ARM_CPU(cs);
a8170e5e 4126 hwaddr phys_addr;
d4c430a8 4127 target_ulong page_size;
b5ff1b31
FB
4128 int prot;
4129 int ret;
4130
00b941e5 4131 ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
b5ff1b31 4132
00b941e5 4133 if (ret != 0) {
b5ff1b31 4134 return -1;
00b941e5 4135 }
b5ff1b31
FB
4136
4137 return phys_addr;
4138}
4139
0ecb72a5 4140void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
9ee6e8bb 4141{
39ea3d4e
PM
4142 if ((env->uncached_cpsr & CPSR_M) == mode) {
4143 env->regs[13] = val;
4144 } else {
f5206413 4145 env->banked_r13[bank_number(mode)] = val;
39ea3d4e 4146 }
9ee6e8bb
PB
4147}
4148
0ecb72a5 4149uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
9ee6e8bb 4150{
39ea3d4e
PM
4151 if ((env->uncached_cpsr & CPSR_M) == mode) {
4152 return env->regs[13];
4153 } else {
f5206413 4154 return env->banked_r13[bank_number(mode)];
39ea3d4e 4155 }
9ee6e8bb
PB
4156}
4157
0ecb72a5 4158uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 4159{
a47dddd7
AF
4160 ARMCPU *cpu = arm_env_get_cpu(env);
4161
9ee6e8bb
PB
4162 switch (reg) {
4163 case 0: /* APSR */
4164 return xpsr_read(env) & 0xf8000000;
4165 case 1: /* IAPSR */
4166 return xpsr_read(env) & 0xf80001ff;
4167 case 2: /* EAPSR */
4168 return xpsr_read(env) & 0xff00fc00;
4169 case 3: /* xPSR */
4170 return xpsr_read(env) & 0xff00fdff;
4171 case 5: /* IPSR */
4172 return xpsr_read(env) & 0x000001ff;
4173 case 6: /* EPSR */
4174 return xpsr_read(env) & 0x0700fc00;
4175 case 7: /* IEPSR */
4176 return xpsr_read(env) & 0x0700edff;
4177 case 8: /* MSP */
4178 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
4179 case 9: /* PSP */
4180 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
4181 case 16: /* PRIMASK */
4cc35614 4182 return (env->daif & PSTATE_I) != 0;
82845826
SH
4183 case 17: /* BASEPRI */
4184 case 18: /* BASEPRI_MAX */
9ee6e8bb 4185 return env->v7m.basepri;
82845826 4186 case 19: /* FAULTMASK */
4cc35614 4187 return (env->daif & PSTATE_F) != 0;
9ee6e8bb
PB
4188 case 20: /* CONTROL */
4189 return env->v7m.control;
4190 default:
4191 /* ??? For debugging only. */
a47dddd7 4192 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
9ee6e8bb
PB
4193 return 0;
4194 }
4195}
4196
0ecb72a5 4197void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 4198{
a47dddd7
AF
4199 ARMCPU *cpu = arm_env_get_cpu(env);
4200
9ee6e8bb
PB
4201 switch (reg) {
4202 case 0: /* APSR */
4203 xpsr_write(env, val, 0xf8000000);
4204 break;
4205 case 1: /* IAPSR */
4206 xpsr_write(env, val, 0xf8000000);
4207 break;
4208 case 2: /* EAPSR */
4209 xpsr_write(env, val, 0xfe00fc00);
4210 break;
4211 case 3: /* xPSR */
4212 xpsr_write(env, val, 0xfe00fc00);
4213 break;
4214 case 5: /* IPSR */
4215 /* IPSR bits are readonly. */
4216 break;
4217 case 6: /* EPSR */
4218 xpsr_write(env, val, 0x0600fc00);
4219 break;
4220 case 7: /* IEPSR */
4221 xpsr_write(env, val, 0x0600fc00);
4222 break;
4223 case 8: /* MSP */
4224 if (env->v7m.current_sp)
4225 env->v7m.other_sp = val;
4226 else
4227 env->regs[13] = val;
4228 break;
4229 case 9: /* PSP */
4230 if (env->v7m.current_sp)
4231 env->regs[13] = val;
4232 else
4233 env->v7m.other_sp = val;
4234 break;
4235 case 16: /* PRIMASK */
4cc35614
PM
4236 if (val & 1) {
4237 env->daif |= PSTATE_I;
4238 } else {
4239 env->daif &= ~PSTATE_I;
4240 }
9ee6e8bb 4241 break;
82845826 4242 case 17: /* BASEPRI */
9ee6e8bb
PB
4243 env->v7m.basepri = val & 0xff;
4244 break;
82845826 4245 case 18: /* BASEPRI_MAX */
9ee6e8bb
PB
4246 val &= 0xff;
4247 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
4248 env->v7m.basepri = val;
4249 break;
82845826 4250 case 19: /* FAULTMASK */
4cc35614
PM
4251 if (val & 1) {
4252 env->daif |= PSTATE_F;
4253 } else {
4254 env->daif &= ~PSTATE_F;
4255 }
82845826 4256 break;
9ee6e8bb
PB
4257 case 20: /* CONTROL */
4258 env->v7m.control = val & 3;
4259 switch_v7m_sp(env, (val & 2) != 0);
4260 break;
4261 default:
4262 /* ??? For debugging only. */
a47dddd7 4263 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
9ee6e8bb
PB
4264 return;
4265 }
4266}
4267
b5ff1b31 4268#endif
6ddbc6e4 4269
aca3f40b
PM
4270void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
4271{
4272 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
4273 * Note that we do not implement the (architecturally mandated)
4274 * alignment fault for attempts to use this on Device memory
4275 * (which matches the usual QEMU behaviour of not implementing either
4276 * alignment faults or any memory attribute handling).
4277 */
4278
4279 ARMCPU *cpu = arm_env_get_cpu(env);
4280 uint64_t blocklen = 4 << cpu->dcz_blocksize;
4281 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
4282
4283#ifndef CONFIG_USER_ONLY
4284 {
4285 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
4286 * the block size so we might have to do more than one TLB lookup.
4287 * We know that in fact for any v8 CPU the page size is at least 4K
4288 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
4289 * 1K as an artefact of legacy v5 subpage support being present in the
4290 * same QEMU executable.
4291 */
4292 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
4293 void *hostaddr[maxidx];
4294 int try, i;
4295
4296 for (try = 0; try < 2; try++) {
4297
4298 for (i = 0; i < maxidx; i++) {
4299 hostaddr[i] = tlb_vaddr_to_host(env,
4300 vaddr + TARGET_PAGE_SIZE * i,
4301 1, cpu_mmu_index(env));
4302 if (!hostaddr[i]) {
4303 break;
4304 }
4305 }
4306 if (i == maxidx) {
4307 /* If it's all in the TLB it's fair game for just writing to;
4308 * we know we don't need to update dirty status, etc.
4309 */
4310 for (i = 0; i < maxidx - 1; i++) {
4311 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
4312 }
4313 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
4314 return;
4315 }
4316 /* OK, try a store and see if we can populate the tlb. This
4317 * might cause an exception if the memory isn't writable,
4318 * in which case we will longjmp out of here. We must for
4319 * this purpose use the actual register value passed to us
4320 * so that we get the fault address right.
4321 */
4322 helper_ret_stb_mmu(env, vaddr_in, 0, cpu_mmu_index(env), GETRA());
4323 /* Now we can populate the other TLB entries, if any */
4324 for (i = 0; i < maxidx; i++) {
4325 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
4326 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
4327 helper_ret_stb_mmu(env, va, 0, cpu_mmu_index(env), GETRA());
4328 }
4329 }
4330 }
4331
4332 /* Slow path (probably attempt to do this to an I/O device or
4333 * similar, or clearing of a block of code we have translations
4334 * cached for). Just do a series of byte writes as the architecture
4335 * demands. It's not worth trying to use a cpu_physical_memory_map(),
4336 * memset(), unmap() sequence here because:
4337 * + we'd need to account for the blocksize being larger than a page
4338 * + the direct-RAM access case is almost always going to be dealt
4339 * with in the fastpath code above, so there's no speed benefit
4340 * + we would have to deal with the map returning NULL because the
4341 * bounce buffer was in use
4342 */
4343 for (i = 0; i < blocklen; i++) {
4344 helper_ret_stb_mmu(env, vaddr + i, 0, cpu_mmu_index(env), GETRA());
4345 }
4346 }
4347#else
4348 memset(g2h(vaddr), 0, blocklen);
4349#endif
4350}
4351
6ddbc6e4
PB
4352/* Note that signed overflow is undefined in C. The following routines are
4353 careful to use unsigned types where modulo arithmetic is required.
4354 Failure to do so _will_ break on newer gcc. */
4355
4356/* Signed saturating arithmetic. */
4357
1654b2d6 4358/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
4359static inline uint16_t add16_sat(uint16_t a, uint16_t b)
4360{
4361 uint16_t res;
4362
4363 res = a + b;
4364 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
4365 if (a & 0x8000)
4366 res = 0x8000;
4367 else
4368 res = 0x7fff;
4369 }
4370 return res;
4371}
4372
1654b2d6 4373/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
4374static inline uint8_t add8_sat(uint8_t a, uint8_t b)
4375{
4376 uint8_t res;
4377
4378 res = a + b;
4379 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
4380 if (a & 0x80)
4381 res = 0x80;
4382 else
4383 res = 0x7f;
4384 }
4385 return res;
4386}
4387
1654b2d6 4388/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
4389static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
4390{
4391 uint16_t res;
4392
4393 res = a - b;
4394 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
4395 if (a & 0x8000)
4396 res = 0x8000;
4397 else
4398 res = 0x7fff;
4399 }
4400 return res;
4401}
4402
1654b2d6 4403/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
4404static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
4405{
4406 uint8_t res;
4407
4408 res = a - b;
4409 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
4410 if (a & 0x80)
4411 res = 0x80;
4412 else
4413 res = 0x7f;
4414 }
4415 return res;
4416}
4417
4418#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
4419#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
4420#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
4421#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
4422#define PFX q
4423
4424#include "op_addsub.h"
4425
4426/* Unsigned saturating arithmetic. */
460a09c1 4427static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
4428{
4429 uint16_t res;
4430 res = a + b;
4431 if (res < a)
4432 res = 0xffff;
4433 return res;
4434}
4435
460a09c1 4436static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 4437{
4c4fd3f8 4438 if (a > b)
6ddbc6e4
PB
4439 return a - b;
4440 else
4441 return 0;
4442}
4443
4444static inline uint8_t add8_usat(uint8_t a, uint8_t b)
4445{
4446 uint8_t res;
4447 res = a + b;
4448 if (res < a)
4449 res = 0xff;
4450 return res;
4451}
4452
4453static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
4454{
4c4fd3f8 4455 if (a > b)
6ddbc6e4
PB
4456 return a - b;
4457 else
4458 return 0;
4459}
4460
4461#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
4462#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
4463#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
4464#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
4465#define PFX uq
4466
4467#include "op_addsub.h"
4468
4469/* Signed modulo arithmetic. */
4470#define SARITH16(a, b, n, op) do { \
4471 int32_t sum; \
db6e2e65 4472 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
4473 RESULT(sum, n, 16); \
4474 if (sum >= 0) \
4475 ge |= 3 << (n * 2); \
4476 } while(0)
4477
4478#define SARITH8(a, b, n, op) do { \
4479 int32_t sum; \
db6e2e65 4480 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
4481 RESULT(sum, n, 8); \
4482 if (sum >= 0) \
4483 ge |= 1 << n; \
4484 } while(0)
4485
4486
4487#define ADD16(a, b, n) SARITH16(a, b, n, +)
4488#define SUB16(a, b, n) SARITH16(a, b, n, -)
4489#define ADD8(a, b, n) SARITH8(a, b, n, +)
4490#define SUB8(a, b, n) SARITH8(a, b, n, -)
4491#define PFX s
4492#define ARITH_GE
4493
4494#include "op_addsub.h"
4495
4496/* Unsigned modulo arithmetic. */
4497#define ADD16(a, b, n) do { \
4498 uint32_t sum; \
4499 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
4500 RESULT(sum, n, 16); \
a87aa10b 4501 if ((sum >> 16) == 1) \
6ddbc6e4
PB
4502 ge |= 3 << (n * 2); \
4503 } while(0)
4504
4505#define ADD8(a, b, n) do { \
4506 uint32_t sum; \
4507 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
4508 RESULT(sum, n, 8); \
a87aa10b
AZ
4509 if ((sum >> 8) == 1) \
4510 ge |= 1 << n; \
6ddbc6e4
PB
4511 } while(0)
4512
4513#define SUB16(a, b, n) do { \
4514 uint32_t sum; \
4515 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
4516 RESULT(sum, n, 16); \
4517 if ((sum >> 16) == 0) \
4518 ge |= 3 << (n * 2); \
4519 } while(0)
4520
4521#define SUB8(a, b, n) do { \
4522 uint32_t sum; \
4523 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
4524 RESULT(sum, n, 8); \
4525 if ((sum >> 8) == 0) \
a87aa10b 4526 ge |= 1 << n; \
6ddbc6e4
PB
4527 } while(0)
4528
4529#define PFX u
4530#define ARITH_GE
4531
4532#include "op_addsub.h"
4533
4534/* Halved signed arithmetic. */
4535#define ADD16(a, b, n) \
4536 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
4537#define SUB16(a, b, n) \
4538 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
4539#define ADD8(a, b, n) \
4540 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
4541#define SUB8(a, b, n) \
4542 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
4543#define PFX sh
4544
4545#include "op_addsub.h"
4546
4547/* Halved unsigned arithmetic. */
4548#define ADD16(a, b, n) \
4549 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
4550#define SUB16(a, b, n) \
4551 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
4552#define ADD8(a, b, n) \
4553 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
4554#define SUB8(a, b, n) \
4555 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
4556#define PFX uh
4557
4558#include "op_addsub.h"
4559
4560static inline uint8_t do_usad(uint8_t a, uint8_t b)
4561{
4562 if (a > b)
4563 return a - b;
4564 else
4565 return b - a;
4566}
4567
4568/* Unsigned sum of absolute byte differences. */
4569uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
4570{
4571 uint32_t sum;
4572 sum = do_usad(a, b);
4573 sum += do_usad(a >> 8, b >> 8);
4574 sum += do_usad(a >> 16, b >>16);
4575 sum += do_usad(a >> 24, b >> 24);
4576 return sum;
4577}
4578
4579/* For ARMv6 SEL instruction. */
4580uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
4581{
4582 uint32_t mask;
4583
4584 mask = 0;
4585 if (flags & 1)
4586 mask |= 0xff;
4587 if (flags & 2)
4588 mask |= 0xff00;
4589 if (flags & 4)
4590 mask |= 0xff0000;
4591 if (flags & 8)
4592 mask |= 0xff000000;
4593 return (a & mask) | (b & ~mask);
4594}
4595
b90372ad
PM
4596/* VFP support. We follow the convention used for VFP instructions:
4597 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
4598 "d" suffix. */
4599
4600/* Convert host exception flags to vfp form. */
4601static inline int vfp_exceptbits_from_host(int host_bits)
4602{
4603 int target_bits = 0;
4604
4605 if (host_bits & float_flag_invalid)
4606 target_bits |= 1;
4607 if (host_bits & float_flag_divbyzero)
4608 target_bits |= 2;
4609 if (host_bits & float_flag_overflow)
4610 target_bits |= 4;
36802b6b 4611 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
4612 target_bits |= 8;
4613 if (host_bits & float_flag_inexact)
4614 target_bits |= 0x10;
cecd8504
PM
4615 if (host_bits & float_flag_input_denormal)
4616 target_bits |= 0x80;
4373f3ce
PB
4617 return target_bits;
4618}
4619
0ecb72a5 4620uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
4621{
4622 int i;
4623 uint32_t fpscr;
4624
4625 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
4626 | (env->vfp.vec_len << 16)
4627 | (env->vfp.vec_stride << 20);
4628 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 4629 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
4630 fpscr |= vfp_exceptbits_from_host(i);
4631 return fpscr;
4632}
4633
0ecb72a5 4634uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
4635{
4636 return HELPER(vfp_get_fpscr)(env);
4637}
4638
4373f3ce
PB
4639/* Convert vfp exception flags to target form. */
4640static inline int vfp_exceptbits_to_host(int target_bits)
4641{
4642 int host_bits = 0;
4643
4644 if (target_bits & 1)
4645 host_bits |= float_flag_invalid;
4646 if (target_bits & 2)
4647 host_bits |= float_flag_divbyzero;
4648 if (target_bits & 4)
4649 host_bits |= float_flag_overflow;
4650 if (target_bits & 8)
4651 host_bits |= float_flag_underflow;
4652 if (target_bits & 0x10)
4653 host_bits |= float_flag_inexact;
cecd8504
PM
4654 if (target_bits & 0x80)
4655 host_bits |= float_flag_input_denormal;
4373f3ce
PB
4656 return host_bits;
4657}
4658
0ecb72a5 4659void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
4660{
4661 int i;
4662 uint32_t changed;
4663
4664 changed = env->vfp.xregs[ARM_VFP_FPSCR];
4665 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
4666 env->vfp.vec_len = (val >> 16) & 7;
4667 env->vfp.vec_stride = (val >> 20) & 3;
4668
4669 changed ^= val;
4670 if (changed & (3 << 22)) {
4671 i = (val >> 22) & 3;
4672 switch (i) {
4d3da0f3 4673 case FPROUNDING_TIEEVEN:
4373f3ce
PB
4674 i = float_round_nearest_even;
4675 break;
4d3da0f3 4676 case FPROUNDING_POSINF:
4373f3ce
PB
4677 i = float_round_up;
4678 break;
4d3da0f3 4679 case FPROUNDING_NEGINF:
4373f3ce
PB
4680 i = float_round_down;
4681 break;
4d3da0f3 4682 case FPROUNDING_ZERO:
4373f3ce
PB
4683 i = float_round_to_zero;
4684 break;
4685 }
4686 set_float_rounding_mode(i, &env->vfp.fp_status);
4687 }
cecd8504 4688 if (changed & (1 << 24)) {
fe76d976 4689 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
4690 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
4691 }
5c7908ed
PB
4692 if (changed & (1 << 25))
4693 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 4694
b12c390b 4695 i = vfp_exceptbits_to_host(val);
4373f3ce 4696 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 4697 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
4698}
4699
0ecb72a5 4700void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
4701{
4702 HELPER(vfp_set_fpscr)(env, val);
4703}
4704
4373f3ce
PB
4705#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
4706
4707#define VFP_BINOP(name) \
ae1857ec 4708float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 4709{ \
ae1857ec
PM
4710 float_status *fpst = fpstp; \
4711 return float32_ ## name(a, b, fpst); \
4373f3ce 4712} \
ae1857ec 4713float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 4714{ \
ae1857ec
PM
4715 float_status *fpst = fpstp; \
4716 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
4717}
4718VFP_BINOP(add)
4719VFP_BINOP(sub)
4720VFP_BINOP(mul)
4721VFP_BINOP(div)
f71a2ae5
PM
4722VFP_BINOP(min)
4723VFP_BINOP(max)
4724VFP_BINOP(minnum)
4725VFP_BINOP(maxnum)
4373f3ce
PB
4726#undef VFP_BINOP
4727
4728float32 VFP_HELPER(neg, s)(float32 a)
4729{
4730 return float32_chs(a);
4731}
4732
4733float64 VFP_HELPER(neg, d)(float64 a)
4734{
66230e0d 4735 return float64_chs(a);
4373f3ce
PB
4736}
4737
4738float32 VFP_HELPER(abs, s)(float32 a)
4739{
4740 return float32_abs(a);
4741}
4742
4743float64 VFP_HELPER(abs, d)(float64 a)
4744{
66230e0d 4745 return float64_abs(a);
4373f3ce
PB
4746}
4747
0ecb72a5 4748float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
4749{
4750 return float32_sqrt(a, &env->vfp.fp_status);
4751}
4752
0ecb72a5 4753float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
4754{
4755 return float64_sqrt(a, &env->vfp.fp_status);
4756}
4757
4758/* XXX: check quiet/signaling case */
4759#define DO_VFP_cmp(p, type) \
0ecb72a5 4760void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
4761{ \
4762 uint32_t flags; \
4763 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
4764 case 0: flags = 0x6; break; \
4765 case -1: flags = 0x8; break; \
4766 case 1: flags = 0x2; break; \
4767 default: case 2: flags = 0x3; break; \
4768 } \
4769 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
4770 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
4771} \
0ecb72a5 4772void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
4773{ \
4774 uint32_t flags; \
4775 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
4776 case 0: flags = 0x6; break; \
4777 case -1: flags = 0x8; break; \
4778 case 1: flags = 0x2; break; \
4779 default: case 2: flags = 0x3; break; \
4780 } \
4781 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
4782 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
4783}
4784DO_VFP_cmp(s, float32)
4785DO_VFP_cmp(d, float64)
4786#undef DO_VFP_cmp
4787
5500b06c 4788/* Integer to float and float to integer conversions */
4373f3ce 4789
5500b06c
PM
4790#define CONV_ITOF(name, fsz, sign) \
4791 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
4792{ \
4793 float_status *fpst = fpstp; \
85836979 4794 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
4795}
4796
5500b06c
PM
4797#define CONV_FTOI(name, fsz, sign, round) \
4798uint32_t HELPER(name)(float##fsz x, void *fpstp) \
4799{ \
4800 float_status *fpst = fpstp; \
4801 if (float##fsz##_is_any_nan(x)) { \
4802 float_raise(float_flag_invalid, fpst); \
4803 return 0; \
4804 } \
4805 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
4806}
4807
5500b06c
PM
4808#define FLOAT_CONVS(name, p, fsz, sign) \
4809CONV_ITOF(vfp_##name##to##p, fsz, sign) \
4810CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
4811CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 4812
5500b06c
PM
4813FLOAT_CONVS(si, s, 32, )
4814FLOAT_CONVS(si, d, 64, )
4815FLOAT_CONVS(ui, s, 32, u)
4816FLOAT_CONVS(ui, d, 64, u)
4373f3ce 4817
5500b06c
PM
4818#undef CONV_ITOF
4819#undef CONV_FTOI
4820#undef FLOAT_CONVS
4373f3ce
PB
4821
4822/* floating point conversion */
0ecb72a5 4823float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 4824{
2d627737
PM
4825 float64 r = float32_to_float64(x, &env->vfp.fp_status);
4826 /* ARM requires that S<->D conversion of any kind of NaN generates
4827 * a quiet NaN by forcing the most significant frac bit to 1.
4828 */
4829 return float64_maybe_silence_nan(r);
4373f3ce
PB
4830}
4831
0ecb72a5 4832float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 4833{
2d627737
PM
4834 float32 r = float64_to_float32(x, &env->vfp.fp_status);
4835 /* ARM requires that S<->D conversion of any kind of NaN generates
4836 * a quiet NaN by forcing the most significant frac bit to 1.
4837 */
4838 return float32_maybe_silence_nan(r);
4373f3ce
PB
4839}
4840
4841/* VFP3 fixed point conversion. */
16d5b3ca 4842#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
4843float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
4844 void *fpstp) \
4373f3ce 4845{ \
5500b06c 4846 float_status *fpst = fpstp; \
622465e1 4847 float##fsz tmp; \
8ed697e8 4848 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 4849 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
4850}
4851
abe66f70
PM
4852/* Notice that we want only input-denormal exception flags from the
4853 * scalbn operation: the other possible flags (overflow+inexact if
4854 * we overflow to infinity, output-denormal) aren't correct for the
4855 * complete scale-and-convert operation.
4856 */
16d5b3ca
WN
4857#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
4858uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
4859 uint32_t shift, \
4860 void *fpstp) \
4373f3ce 4861{ \
5500b06c 4862 float_status *fpst = fpstp; \
abe66f70 4863 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
4864 float##fsz tmp; \
4865 if (float##fsz##_is_any_nan(x)) { \
5500b06c 4866 float_raise(float_flag_invalid, fpst); \
622465e1 4867 return 0; \
09d9487f 4868 } \
5500b06c 4869 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
4870 old_exc_flags |= get_float_exception_flags(fpst) \
4871 & float_flag_input_denormal; \
4872 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 4873 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
4874}
4875
16d5b3ca
WN
4876#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
4877VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
4878VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
4879VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
4880
4881#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
4882VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
4883VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 4884
8ed697e8
WN
4885VFP_CONV_FIX(sh, d, 64, 64, int16)
4886VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 4887VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
4888VFP_CONV_FIX(uh, d, 64, 64, uint16)
4889VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 4890VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
4891VFP_CONV_FIX(sh, s, 32, 32, int16)
4892VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 4893VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
4894VFP_CONV_FIX(uh, s, 32, 32, uint16)
4895VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 4896VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 4897#undef VFP_CONV_FIX
16d5b3ca
WN
4898#undef VFP_CONV_FIX_FLOAT
4899#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 4900
52a1f6a3
AG
4901/* Set the current fp rounding mode and return the old one.
4902 * The argument is a softfloat float_round_ value.
4903 */
4904uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
4905{
4906 float_status *fp_status = &env->vfp.fp_status;
4907
4908 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
4909 set_float_rounding_mode(rmode, fp_status);
4910
4911 return prev_rmode;
4912}
4913
43630e58
WN
4914/* Set the current fp rounding mode in the standard fp status and return
4915 * the old one. This is for NEON instructions that need to change the
4916 * rounding mode but wish to use the standard FPSCR values for everything
4917 * else. Always set the rounding mode back to the correct value after
4918 * modifying it.
4919 * The argument is a softfloat float_round_ value.
4920 */
4921uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
4922{
4923 float_status *fp_status = &env->vfp.standard_fp_status;
4924
4925 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
4926 set_float_rounding_mode(rmode, fp_status);
4927
4928 return prev_rmode;
4929}
4930
60011498 4931/* Half precision conversions. */
0ecb72a5 4932static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 4933{
60011498 4934 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
4935 float32 r = float16_to_float32(make_float16(a), ieee, s);
4936 if (ieee) {
4937 return float32_maybe_silence_nan(r);
4938 }
4939 return r;
60011498
PB
4940}
4941
0ecb72a5 4942static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 4943{
60011498 4944 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
4945 float16 r = float32_to_float16(a, ieee, s);
4946 if (ieee) {
4947 r = float16_maybe_silence_nan(r);
4948 }
4949 return float16_val(r);
60011498
PB
4950}
4951
0ecb72a5 4952float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
4953{
4954 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
4955}
4956
0ecb72a5 4957uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
4958{
4959 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
4960}
4961
0ecb72a5 4962float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
4963{
4964 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
4965}
4966
0ecb72a5 4967uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
4968{
4969 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
4970}
4971
8900aad2
PM
4972float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
4973{
4974 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
4975 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
4976 if (ieee) {
4977 return float64_maybe_silence_nan(r);
4978 }
4979 return r;
4980}
4981
4982uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
4983{
4984 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
4985 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
4986 if (ieee) {
4987 r = float16_maybe_silence_nan(r);
4988 }
4989 return float16_val(r);
4990}
4991
dda3ec49 4992#define float32_two make_float32(0x40000000)
6aae3df1
PM
4993#define float32_three make_float32(0x40400000)
4994#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 4995
0ecb72a5 4996float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 4997{
dda3ec49
PM
4998 float_status *s = &env->vfp.standard_fp_status;
4999 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
5000 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
5001 if (!(float32_is_zero(a) || float32_is_zero(b))) {
5002 float_raise(float_flag_input_denormal, s);
5003 }
dda3ec49
PM
5004 return float32_two;
5005 }
5006 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
5007}
5008
0ecb72a5 5009float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 5010{
71826966 5011 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
5012 float32 product;
5013 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
5014 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
5015 if (!(float32_is_zero(a) || float32_is_zero(b))) {
5016 float_raise(float_flag_input_denormal, s);
5017 }
6aae3df1 5018 return float32_one_point_five;
9ea62f57 5019 }
6aae3df1
PM
5020 product = float32_mul(a, b, s);
5021 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
5022}
5023
8f8e3aa4
PB
5024/* NEON helpers. */
5025
56bf4fe2
CL
5026/* Constants 256 and 512 are used in some helpers; we avoid relying on
5027 * int->float conversions at run-time. */
5028#define float64_256 make_float64(0x4070000000000000LL)
5029#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
5030#define float32_maxnorm make_float32(0x7f7fffff)
5031#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 5032
b6d4443a
AB
5033/* Reciprocal functions
5034 *
5035 * The algorithm that must be used to calculate the estimate
5036 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 5037 */
b6d4443a
AB
5038
5039static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 5040{
1146a817
PM
5041 /* These calculations mustn't set any fp exception flags,
5042 * so we use a local copy of the fp_status.
5043 */
b6d4443a 5044 float_status dummy_status = *real_fp_status;
1146a817 5045 float_status *s = &dummy_status;
fe0e4872
CL
5046 /* q = (int)(a * 512.0) */
5047 float64 q = float64_mul(float64_512, a, s);
5048 int64_t q_int = float64_to_int64_round_to_zero(q, s);
5049
5050 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
5051 q = int64_to_float64(q_int, s);
5052 q = float64_add(q, float64_half, s);
5053 q = float64_div(q, float64_512, s);
5054 q = float64_div(float64_one, q, s);
5055
5056 /* s = (int)(256.0 * r + 0.5) */
5057 q = float64_mul(q, float64_256, s);
5058 q = float64_add(q, float64_half, s);
5059 q_int = float64_to_int64_round_to_zero(q, s);
5060
5061 /* return (double)s / 256.0 */
5062 return float64_div(int64_to_float64(q_int, s), float64_256, s);
5063}
5064
b6d4443a
AB
5065/* Common wrapper to call recip_estimate */
5066static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 5067{
b6d4443a
AB
5068 uint64_t val64 = float64_val(num);
5069 uint64_t frac = extract64(val64, 0, 52);
5070 int64_t exp = extract64(val64, 52, 11);
5071 uint64_t sbit;
5072 float64 scaled, estimate;
fe0e4872 5073
b6d4443a
AB
5074 /* Generate the scaled number for the estimate function */
5075 if (exp == 0) {
5076 if (extract64(frac, 51, 1) == 0) {
5077 exp = -1;
5078 frac = extract64(frac, 0, 50) << 2;
5079 } else {
5080 frac = extract64(frac, 0, 51) << 1;
5081 }
5082 }
fe0e4872 5083
b6d4443a
AB
5084 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
5085 scaled = make_float64((0x3feULL << 52)
5086 | extract64(frac, 44, 8) << 44);
5087
5088 estimate = recip_estimate(scaled, fpst);
5089
5090 /* Build new result */
5091 val64 = float64_val(estimate);
5092 sbit = 0x8000000000000000ULL & val64;
5093 exp = off - exp;
5094 frac = extract64(val64, 0, 52);
5095
5096 if (exp == 0) {
5097 frac = 1ULL << 51 | extract64(frac, 1, 51);
5098 } else if (exp == -1) {
5099 frac = 1ULL << 50 | extract64(frac, 2, 50);
5100 exp = 0;
5101 }
5102
5103 return make_float64(sbit | (exp << 52) | frac);
5104}
5105
5106static bool round_to_inf(float_status *fpst, bool sign_bit)
5107{
5108 switch (fpst->float_rounding_mode) {
5109 case float_round_nearest_even: /* Round to Nearest */
5110 return true;
5111 case float_round_up: /* Round to +Inf */
5112 return !sign_bit;
5113 case float_round_down: /* Round to -Inf */
5114 return sign_bit;
5115 case float_round_to_zero: /* Round to Zero */
5116 return false;
5117 }
5118
5119 g_assert_not_reached();
5120}
5121
5122float32 HELPER(recpe_f32)(float32 input, void *fpstp)
5123{
5124 float_status *fpst = fpstp;
5125 float32 f32 = float32_squash_input_denormal(input, fpst);
5126 uint32_t f32_val = float32_val(f32);
5127 uint32_t f32_sbit = 0x80000000ULL & f32_val;
5128 int32_t f32_exp = extract32(f32_val, 23, 8);
5129 uint32_t f32_frac = extract32(f32_val, 0, 23);
5130 float64 f64, r64;
5131 uint64_t r64_val;
5132 int64_t r64_exp;
5133 uint64_t r64_frac;
5134
5135 if (float32_is_any_nan(f32)) {
5136 float32 nan = f32;
5137 if (float32_is_signaling_nan(f32)) {
5138 float_raise(float_flag_invalid, fpst);
5139 nan = float32_maybe_silence_nan(f32);
fe0e4872 5140 }
b6d4443a
AB
5141 if (fpst->default_nan_mode) {
5142 nan = float32_default_nan;
43fe9bdb 5143 }
b6d4443a
AB
5144 return nan;
5145 } else if (float32_is_infinity(f32)) {
5146 return float32_set_sign(float32_zero, float32_is_neg(f32));
5147 } else if (float32_is_zero(f32)) {
5148 float_raise(float_flag_divbyzero, fpst);
5149 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5150 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
5151 /* Abs(value) < 2.0^-128 */
5152 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5153 if (round_to_inf(fpst, f32_sbit)) {
5154 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5155 } else {
5156 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
5157 }
5158 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
5159 float_raise(float_flag_underflow, fpst);
5160 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
5161 }
5162
fe0e4872 5163
b6d4443a
AB
5164 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
5165 r64 = call_recip_estimate(f64, 253, fpst);
5166 r64_val = float64_val(r64);
5167 r64_exp = extract64(r64_val, 52, 11);
5168 r64_frac = extract64(r64_val, 0, 52);
5169
5170 /* result = sign : result_exp<7:0> : fraction<51:29>; */
5171 return make_float32(f32_sbit |
5172 (r64_exp & 0xff) << 23 |
5173 extract64(r64_frac, 29, 24));
5174}
5175
5176float64 HELPER(recpe_f64)(float64 input, void *fpstp)
5177{
5178 float_status *fpst = fpstp;
5179 float64 f64 = float64_squash_input_denormal(input, fpst);
5180 uint64_t f64_val = float64_val(f64);
5181 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
5182 int64_t f64_exp = extract64(f64_val, 52, 11);
5183 float64 r64;
5184 uint64_t r64_val;
5185 int64_t r64_exp;
5186 uint64_t r64_frac;
5187
5188 /* Deal with any special cases */
5189 if (float64_is_any_nan(f64)) {
5190 float64 nan = f64;
5191 if (float64_is_signaling_nan(f64)) {
5192 float_raise(float_flag_invalid, fpst);
5193 nan = float64_maybe_silence_nan(f64);
5194 }
5195 if (fpst->default_nan_mode) {
5196 nan = float64_default_nan;
5197 }
5198 return nan;
5199 } else if (float64_is_infinity(f64)) {
5200 return float64_set_sign(float64_zero, float64_is_neg(f64));
5201 } else if (float64_is_zero(f64)) {
5202 float_raise(float_flag_divbyzero, fpst);
5203 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5204 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
5205 /* Abs(value) < 2.0^-1024 */
5206 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5207 if (round_to_inf(fpst, f64_sbit)) {
5208 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5209 } else {
5210 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
5211 }
5212 } else if (f64_exp >= 1023 && fpst->flush_to_zero) {
5213 float_raise(float_flag_underflow, fpst);
5214 return float64_set_sign(float64_zero, float64_is_neg(f64));
5215 }
fe0e4872 5216
b6d4443a
AB
5217 r64 = call_recip_estimate(f64, 2045, fpst);
5218 r64_val = float64_val(r64);
5219 r64_exp = extract64(r64_val, 52, 11);
5220 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 5221
b6d4443a
AB
5222 /* result = sign : result_exp<10:0> : fraction<51:0> */
5223 return make_float64(f64_sbit |
5224 ((r64_exp & 0x7ff) << 52) |
5225 r64_frac);
4373f3ce
PB
5226}
5227
e07be5d2
CL
5228/* The algorithm that must be used to calculate the estimate
5229 * is specified by the ARM ARM.
5230 */
c2fb418e 5231static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 5232{
1146a817
PM
5233 /* These calculations mustn't set any fp exception flags,
5234 * so we use a local copy of the fp_status.
5235 */
c2fb418e 5236 float_status dummy_status = *real_fp_status;
1146a817 5237 float_status *s = &dummy_status;
e07be5d2
CL
5238 float64 q;
5239 int64_t q_int;
5240
5241 if (float64_lt(a, float64_half, s)) {
5242 /* range 0.25 <= a < 0.5 */
5243
5244 /* a in units of 1/512 rounded down */
5245 /* q0 = (int)(a * 512.0); */
5246 q = float64_mul(float64_512, a, s);
5247 q_int = float64_to_int64_round_to_zero(q, s);
5248
5249 /* reciprocal root r */
5250 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
5251 q = int64_to_float64(q_int, s);
5252 q = float64_add(q, float64_half, s);
5253 q = float64_div(q, float64_512, s);
5254 q = float64_sqrt(q, s);
5255 q = float64_div(float64_one, q, s);
5256 } else {
5257 /* range 0.5 <= a < 1.0 */
5258
5259 /* a in units of 1/256 rounded down */
5260 /* q1 = (int)(a * 256.0); */
5261 q = float64_mul(float64_256, a, s);
5262 int64_t q_int = float64_to_int64_round_to_zero(q, s);
5263
5264 /* reciprocal root r */
5265 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
5266 q = int64_to_float64(q_int, s);
5267 q = float64_add(q, float64_half, s);
5268 q = float64_div(q, float64_256, s);
5269 q = float64_sqrt(q, s);
5270 q = float64_div(float64_one, q, s);
5271 }
5272 /* r in units of 1/256 rounded to nearest */
5273 /* s = (int)(256.0 * r + 0.5); */
5274
5275 q = float64_mul(q, float64_256,s );
5276 q = float64_add(q, float64_half, s);
5277 q_int = float64_to_int64_round_to_zero(q, s);
5278
5279 /* return (double)s / 256.0;*/
5280 return float64_div(int64_to_float64(q_int, s), float64_256, s);
5281}
5282
c2fb418e 5283float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 5284{
c2fb418e
AB
5285 float_status *s = fpstp;
5286 float32 f32 = float32_squash_input_denormal(input, s);
5287 uint32_t val = float32_val(f32);
5288 uint32_t f32_sbit = 0x80000000 & val;
5289 int32_t f32_exp = extract32(val, 23, 8);
5290 uint32_t f32_frac = extract32(val, 0, 23);
5291 uint64_t f64_frac;
5292 uint64_t val64;
e07be5d2
CL
5293 int result_exp;
5294 float64 f64;
e07be5d2 5295
c2fb418e
AB
5296 if (float32_is_any_nan(f32)) {
5297 float32 nan = f32;
5298 if (float32_is_signaling_nan(f32)) {
e07be5d2 5299 float_raise(float_flag_invalid, s);
c2fb418e 5300 nan = float32_maybe_silence_nan(f32);
e07be5d2 5301 }
c2fb418e
AB
5302 if (s->default_nan_mode) {
5303 nan = float32_default_nan;
43fe9bdb 5304 }
c2fb418e
AB
5305 return nan;
5306 } else if (float32_is_zero(f32)) {
e07be5d2 5307 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
5308 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5309 } else if (float32_is_neg(f32)) {
e07be5d2
CL
5310 float_raise(float_flag_invalid, s);
5311 return float32_default_nan;
c2fb418e 5312 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
5313 return float32_zero;
5314 }
5315
c2fb418e 5316 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 5317 * preserving the parity of the exponent. */
c2fb418e
AB
5318
5319 f64_frac = ((uint64_t) f32_frac) << 29;
5320 if (f32_exp == 0) {
5321 while (extract64(f64_frac, 51, 1) == 0) {
5322 f64_frac = f64_frac << 1;
5323 f32_exp = f32_exp-1;
5324 }
5325 f64_frac = extract64(f64_frac, 0, 51) << 1;
5326 }
5327
5328 if (extract64(f32_exp, 0, 1) == 0) {
5329 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 5330 | (0x3feULL << 52)
c2fb418e 5331 | f64_frac);
e07be5d2 5332 } else {
c2fb418e 5333 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 5334 | (0x3fdULL << 52)
c2fb418e 5335 | f64_frac);
e07be5d2
CL
5336 }
5337
c2fb418e 5338 result_exp = (380 - f32_exp) / 2;
e07be5d2 5339
c2fb418e 5340 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
5341
5342 val64 = float64_val(f64);
5343
26cc6abf 5344 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
5345 | ((val64 >> 29) & 0x7fffff);
5346 return make_float32(val);
4373f3ce
PB
5347}
5348
c2fb418e
AB
5349float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
5350{
5351 float_status *s = fpstp;
5352 float64 f64 = float64_squash_input_denormal(input, s);
5353 uint64_t val = float64_val(f64);
5354 uint64_t f64_sbit = 0x8000000000000000ULL & val;
5355 int64_t f64_exp = extract64(val, 52, 11);
5356 uint64_t f64_frac = extract64(val, 0, 52);
5357 int64_t result_exp;
5358 uint64_t result_frac;
5359
5360 if (float64_is_any_nan(f64)) {
5361 float64 nan = f64;
5362 if (float64_is_signaling_nan(f64)) {
5363 float_raise(float_flag_invalid, s);
5364 nan = float64_maybe_silence_nan(f64);
5365 }
5366 if (s->default_nan_mode) {
5367 nan = float64_default_nan;
5368 }
5369 return nan;
5370 } else if (float64_is_zero(f64)) {
5371 float_raise(float_flag_divbyzero, s);
5372 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5373 } else if (float64_is_neg(f64)) {
5374 float_raise(float_flag_invalid, s);
5375 return float64_default_nan;
5376 } else if (float64_is_infinity(f64)) {
5377 return float64_zero;
5378 }
5379
5380 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
5381 * preserving the parity of the exponent. */
5382
5383 if (f64_exp == 0) {
5384 while (extract64(f64_frac, 51, 1) == 0) {
5385 f64_frac = f64_frac << 1;
5386 f64_exp = f64_exp - 1;
5387 }
5388 f64_frac = extract64(f64_frac, 0, 51) << 1;
5389 }
5390
5391 if (extract64(f64_exp, 0, 1) == 0) {
5392 f64 = make_float64(f64_sbit
5393 | (0x3feULL << 52)
5394 | f64_frac);
5395 } else {
5396 f64 = make_float64(f64_sbit
5397 | (0x3fdULL << 52)
5398 | f64_frac);
5399 }
5400
5401 result_exp = (3068 - f64_exp) / 2;
5402
5403 f64 = recip_sqrt_estimate(f64, s);
5404
5405 result_frac = extract64(float64_val(f64), 0, 52);
5406
5407 return make_float64(f64_sbit |
5408 ((result_exp & 0x7ff) << 52) |
5409 result_frac);
5410}
5411
b6d4443a 5412uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 5413{
b6d4443a 5414 float_status *s = fpstp;
fe0e4872
CL
5415 float64 f64;
5416
5417 if ((a & 0x80000000) == 0) {
5418 return 0xffffffff;
5419 }
5420
5421 f64 = make_float64((0x3feULL << 52)
5422 | ((int64_t)(a & 0x7fffffff) << 21));
5423
b6d4443a 5424 f64 = recip_estimate(f64, s);
fe0e4872
CL
5425
5426 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
5427}
5428
c2fb418e 5429uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 5430{
c2fb418e 5431 float_status *fpst = fpstp;
e07be5d2
CL
5432 float64 f64;
5433
5434 if ((a & 0xc0000000) == 0) {
5435 return 0xffffffff;
5436 }
5437
5438 if (a & 0x80000000) {
5439 f64 = make_float64((0x3feULL << 52)
5440 | ((uint64_t)(a & 0x7fffffff) << 21));
5441 } else { /* bits 31-30 == '01' */
5442 f64 = make_float64((0x3fdULL << 52)
5443 | ((uint64_t)(a & 0x3fffffff) << 22));
5444 }
5445
c2fb418e 5446 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
5447
5448 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 5449}
fe1479c3 5450
da97f52c
PM
5451/* VFPv4 fused multiply-accumulate */
5452float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
5453{
5454 float_status *fpst = fpstp;
5455 return float32_muladd(a, b, c, 0, fpst);
5456}
5457
5458float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
5459{
5460 float_status *fpst = fpstp;
5461 return float64_muladd(a, b, c, 0, fpst);
5462}
d9b0848d
PM
5463
5464/* ARMv8 round to integral */
5465float32 HELPER(rints_exact)(float32 x, void *fp_status)
5466{
5467 return float32_round_to_int(x, fp_status);
5468}
5469
5470float64 HELPER(rintd_exact)(float64 x, void *fp_status)
5471{
5472 return float64_round_to_int(x, fp_status);
5473}
5474
5475float32 HELPER(rints)(float32 x, void *fp_status)
5476{
5477 int old_flags = get_float_exception_flags(fp_status), new_flags;
5478 float32 ret;
5479
5480 ret = float32_round_to_int(x, fp_status);
5481
5482 /* Suppress any inexact exceptions the conversion produced */
5483 if (!(old_flags & float_flag_inexact)) {
5484 new_flags = get_float_exception_flags(fp_status);
5485 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
5486 }
5487
5488 return ret;
5489}
5490
5491float64 HELPER(rintd)(float64 x, void *fp_status)
5492{
5493 int old_flags = get_float_exception_flags(fp_status), new_flags;
5494 float64 ret;
5495
5496 ret = float64_round_to_int(x, fp_status);
5497
5498 new_flags = get_float_exception_flags(fp_status);
5499
5500 /* Suppress any inexact exceptions the conversion produced */
5501 if (!(old_flags & float_flag_inexact)) {
5502 new_flags = get_float_exception_flags(fp_status);
5503 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
5504 }
5505
5506 return ret;
5507}
9972da66
WN
5508
5509/* Convert ARM rounding mode to softfloat */
5510int arm_rmode_to_sf(int rmode)
5511{
5512 switch (rmode) {
5513 case FPROUNDING_TIEAWAY:
5514 rmode = float_round_ties_away;
5515 break;
5516 case FPROUNDING_ODD:
5517 /* FIXME: add support for TIEAWAY and ODD */
5518 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
5519 rmode);
5520 case FPROUNDING_TIEEVEN:
5521 default:
5522 rmode = float_round_nearest_even;
5523 break;
5524 case FPROUNDING_POSINF:
5525 rmode = float_round_up;
5526 break;
5527 case FPROUNDING_NEGINF:
5528 rmode = float_round_down;
5529 break;
5530 case FPROUNDING_ZERO:
5531 rmode = float_round_to_zero;
5532 break;
5533 }
5534 return rmode;
5535}
eb0ecd5a
WN
5536
5537static void crc_init_buffer(uint8_t *buf, uint32_t val, uint32_t bytes)
5538{
5539 memset(buf, 0, 4);
5540
5541 if (bytes == 1) {
5542 buf[0] = val & 0xff;
5543 } else if (bytes == 2) {
5544 buf[0] = val & 0xff;
5545 buf[1] = (val >> 8) & 0xff;
5546 } else {
5547 buf[0] = val & 0xff;
5548 buf[1] = (val >> 8) & 0xff;
5549 buf[2] = (val >> 16) & 0xff;
5550 buf[3] = (val >> 24) & 0xff;
5551 }
5552}
5553
5554uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
5555{
5556 uint8_t buf[4];
5557
5558 crc_init_buffer(buf, val, bytes);
5559
5560 /* zlib crc32 converts the accumulator and output to one's complement. */
5561 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
5562}
5563
5564uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
5565{
5566 uint8_t buf[4];
5567
5568 crc_init_buffer(buf, val, bytes);
5569
5570 /* Linux crc32c converts the output to one's complement. */
5571 return crc32c(acc, buf, bytes) ^ 0xffffffff;
5572}