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