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