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