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