blob: 6621429381c71ff5e1625e28049069a40317ec33 [file] [log] [blame]
bellardb5ff1b32005-11-26 10:38:39 +00001#include "cpu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +01002#include "exec/gdbstub.h"
Lluís7b592202011-04-13 18:38:24 +02003#include "helper.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01004#include "qemu/host-utils.h"
Cole Robinson78027bb2013-09-10 19:09:33 +01005#include "sysemu/arch_init.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +01006#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01007#include "qemu/bitops.h"
Peter Maydell0b03bdf2012-01-25 12:42:29 +00008
Peter Maydell4a501602012-06-20 11:57:16 +00009#ifndef CONFIG_USER_ONLY
10static inline int get_phys_addr(CPUARMState *env, uint32_t address,
11 int access_type, int is_user,
Avi Kivitya8170e52012-10-23 12:30:10 +020012 hwaddr *phys_ptr, int *prot,
Peter Maydell4a501602012-06-20 11:57:16 +000013 target_ulong *page_size);
14#endif
15
Andreas Färber0ecb72a2012-03-14 01:38:21 +010016static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000017{
18 int nregs;
19
20 /* VFP data registers are always little-endian. */
21 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
22 if (reg < nregs) {
23 stfq_le_p(buf, env->vfp.regs[reg]);
24 return 8;
25 }
26 if (arm_feature(env, ARM_FEATURE_NEON)) {
27 /* Aliases for Q regs. */
28 nregs += 16;
29 if (reg < nregs) {
30 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
31 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
32 return 16;
33 }
34 }
35 switch (reg - nregs) {
36 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
37 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
38 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
39 }
40 return 0;
41}
42
Andreas Färber0ecb72a2012-03-14 01:38:21 +010043static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000044{
45 int nregs;
46
47 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
48 if (reg < nregs) {
49 env->vfp.regs[reg] = ldfq_le_p(buf);
50 return 8;
51 }
52 if (arm_feature(env, ARM_FEATURE_NEON)) {
53 nregs += 16;
54 if (reg < nregs) {
55 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
56 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
57 return 16;
58 }
59 }
60 switch (reg - nregs) {
61 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
62 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +020063 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +000064 }
65 return 0;
66}
67
Peter Maydell6a669422013-12-17 19:42:32 +000068static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
69{
70 switch (reg) {
71 case 0 ... 31:
72 /* 128 bit FP register */
73 stfq_le_p(buf, env->vfp.regs[reg * 2]);
74 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
75 return 16;
76 case 32:
77 /* FPSR */
78 stl_p(buf, vfp_get_fpsr(env));
79 return 4;
80 case 33:
81 /* FPCR */
82 stl_p(buf, vfp_get_fpcr(env));
83 return 4;
84 default:
85 return 0;
86 }
87}
88
89static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
90{
91 switch (reg) {
92 case 0 ... 31:
93 /* 128 bit FP register */
94 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
95 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
96 return 16;
97 case 32:
98 /* FPSR */
99 vfp_set_fpsr(env, ldl_p(buf));
100 return 4;
101 case 33:
102 /* FPCR */
103 vfp_set_fpcr(env, ldl_p(buf));
104 return 4;
105 default:
106 return 0;
107 }
108}
109
Peter Maydelld4e6df62013-06-25 18:16:07 +0100110static int raw_read(CPUARMState *env, const ARMCPRegInfo *ri,
111 uint64_t *value)
112{
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100113 if (ri->type & ARM_CP_64BIT) {
114 *value = CPREG_FIELD64(env, ri);
115 } else {
116 *value = CPREG_FIELD32(env, ri);
117 }
Peter Maydelld4e6df62013-06-25 18:16:07 +0100118 return 0;
119}
120
121static int raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
122 uint64_t value)
123{
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100124 if (ri->type & ARM_CP_64BIT) {
125 CPREG_FIELD64(env, ri) = value;
126 } else {
127 CPREG_FIELD32(env, ri) = value;
128 }
Peter Maydelld4e6df62013-06-25 18:16:07 +0100129 return 0;
130}
131
Peter Maydell721fae12013-06-25 18:16:07 +0100132static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
133 uint64_t *v)
134{
135 /* Raw read of a coprocessor register (as needed for migration, etc)
136 * return true on success, false if the read is impossible for some reason.
137 */
138 if (ri->type & ARM_CP_CONST) {
139 *v = ri->resetvalue;
140 } else if (ri->raw_readfn) {
141 return (ri->raw_readfn(env, ri, v) == 0);
142 } else if (ri->readfn) {
143 return (ri->readfn(env, ri, v) == 0);
144 } else {
145 if (ri->type & ARM_CP_64BIT) {
146 *v = CPREG_FIELD64(env, ri);
147 } else {
148 *v = CPREG_FIELD32(env, ri);
149 }
150 }
151 return true;
152}
153
154static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
155 int64_t v)
156{
157 /* Raw write of a coprocessor register (as needed for migration, etc).
158 * Return true on success, false if the write is impossible for some reason.
159 * Note that constant registers are treated as write-ignored; the
160 * caller should check for success by whether a readback gives the
161 * value written.
162 */
163 if (ri->type & ARM_CP_CONST) {
164 return true;
165 } else if (ri->raw_writefn) {
166 return (ri->raw_writefn(env, ri, v) == 0);
167 } else if (ri->writefn) {
168 return (ri->writefn(env, ri, v) == 0);
169 } else {
170 if (ri->type & ARM_CP_64BIT) {
171 CPREG_FIELD64(env, ri) = v;
172 } else {
173 CPREG_FIELD32(env, ri) = v;
174 }
175 }
176 return true;
177}
178
179bool write_cpustate_to_list(ARMCPU *cpu)
180{
181 /* Write the coprocessor state from cpu->env to the (index,value) list. */
182 int i;
183 bool ok = true;
184
185 for (i = 0; i < cpu->cpreg_array_len; i++) {
186 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
187 const ARMCPRegInfo *ri;
188 uint64_t v;
Peter Maydell8ca70eb2013-12-22 22:32:30 +0000189 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100190 if (!ri) {
191 ok = false;
192 continue;
193 }
194 if (ri->type & ARM_CP_NO_MIGRATE) {
195 continue;
196 }
197 if (!read_raw_cp_reg(&cpu->env, ri, &v)) {
198 ok = false;
199 continue;
200 }
201 cpu->cpreg_values[i] = v;
202 }
203 return ok;
204}
205
206bool write_list_to_cpustate(ARMCPU *cpu)
207{
208 int i;
209 bool ok = true;
210
211 for (i = 0; i < cpu->cpreg_array_len; i++) {
212 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
213 uint64_t v = cpu->cpreg_values[i];
214 uint64_t readback;
215 const ARMCPRegInfo *ri;
216
Peter Maydell8ca70eb2013-12-22 22:32:30 +0000217 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100218 if (!ri) {
219 ok = false;
220 continue;
221 }
222 if (ri->type & ARM_CP_NO_MIGRATE) {
223 continue;
224 }
225 /* Write value and confirm it reads back as written
226 * (to catch read-only registers and partially read-only
227 * registers where the incoming migration value doesn't match)
228 */
229 if (!write_raw_cp_reg(&cpu->env, ri, v) ||
230 !read_raw_cp_reg(&cpu->env, ri, &readback) ||
231 readback != v) {
232 ok = false;
233 }
234 }
235 return ok;
236}
237
238static void add_cpreg_to_list(gpointer key, gpointer opaque)
239{
240 ARMCPU *cpu = opaque;
241 uint64_t regidx;
242 const ARMCPRegInfo *ri;
243
244 regidx = *(uint32_t *)key;
Peter Maydell8ca70eb2013-12-22 22:32:30 +0000245 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100246
247 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
248 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
249 /* The value array need not be initialized at this point */
250 cpu->cpreg_array_len++;
251 }
252}
253
254static void count_cpreg(gpointer key, gpointer opaque)
255{
256 ARMCPU *cpu = opaque;
257 uint64_t regidx;
258 const ARMCPRegInfo *ri;
259
260 regidx = *(uint32_t *)key;
Peter Maydell8ca70eb2013-12-22 22:32:30 +0000261 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100262
263 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
264 cpu->cpreg_array_len++;
265 }
266}
267
268static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
269{
Alvise Rigocbf239b2013-10-11 19:38:44 +0200270 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
271 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
Peter Maydell721fae12013-06-25 18:16:07 +0100272
Alvise Rigocbf239b2013-10-11 19:38:44 +0200273 if (aidx > bidx) {
274 return 1;
275 }
276 if (aidx < bidx) {
277 return -1;
278 }
279 return 0;
Peter Maydell721fae12013-06-25 18:16:07 +0100280}
281
Peter Maydell82a3a112013-07-01 12:40:19 +0100282static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
283{
284 GList **plist = udata;
285
286 *plist = g_list_prepend(*plist, key);
287}
288
Peter Maydell721fae12013-06-25 18:16:07 +0100289void init_cpreg_list(ARMCPU *cpu)
290{
291 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
292 * Note that we require cpreg_tuples[] to be sorted by key ID.
293 */
Peter Maydell82a3a112013-07-01 12:40:19 +0100294 GList *keys = NULL;
Peter Maydell721fae12013-06-25 18:16:07 +0100295 int arraylen;
296
Peter Maydell82a3a112013-07-01 12:40:19 +0100297 g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
298
Peter Maydell721fae12013-06-25 18:16:07 +0100299 keys = g_list_sort(keys, cpreg_key_compare);
300
301 cpu->cpreg_array_len = 0;
302
303 g_list_foreach(keys, count_cpreg, cpu);
304
305 arraylen = cpu->cpreg_array_len;
306 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
307 cpu->cpreg_values = g_new(uint64_t, arraylen);
308 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
309 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
310 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
311 cpu->cpreg_array_len = 0;
312
313 g_list_foreach(keys, add_cpreg_to_list, cpu);
314
315 assert(cpu->cpreg_array_len == arraylen);
316
317 g_list_free(keys);
318}
319
Peter Maydellc983fe62012-06-20 11:57:13 +0000320static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
321{
322 env->cp15.c3 = value;
323 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
324 return 0;
325}
326
Peter Maydell08de2072012-06-20 11:57:14 +0000327static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
328{
329 if (env->cp15.c13_fcse != value) {
330 /* Unlike real hardware the qemu TLB uses virtual addresses,
331 * not modified virtual addresses, so this causes a TLB flush.
332 */
333 tlb_flush(env, 1);
334 env->cp15.c13_fcse = value;
335 }
336 return 0;
337}
338static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
339 uint64_t value)
340{
341 if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
342 /* For VMSA (when not using the LPAE long descriptor page table
343 * format) this register includes the ASID, so do a TLB flush.
344 * For PMSA it is purely a process ID and no action is needed.
345 */
346 tlb_flush(env, 1);
347 }
348 env->cp15.c13_context = value;
349 return 0;
350}
351
Peter Maydelld9298232012-06-20 11:57:16 +0000352static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
353 uint64_t value)
354{
355 /* Invalidate all (TLBIALL) */
356 tlb_flush(env, 1);
357 return 0;
358}
359
360static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
361 uint64_t value)
362{
363 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
364 tlb_flush_page(env, value & TARGET_PAGE_MASK);
365 return 0;
366}
367
368static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
369 uint64_t value)
370{
371 /* Invalidate by ASID (TLBIASID) */
372 tlb_flush(env, value == 0);
373 return 0;
374}
375
376static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
377 uint64_t value)
378{
379 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
380 tlb_flush_page(env, value & TARGET_PAGE_MASK);
381 return 0;
382}
383
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000384static const ARMCPRegInfo cp_reginfo[] = {
385 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
386 * version" bits will read as a reserved value, which should cause
387 * Linux to not try to use the debug hardware.
388 */
389 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
390 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellc983fe62012-06-20 11:57:13 +0000391 /* MMU Domain access control / MPU write buffer control */
392 { .name = "DACR", .cp = 15,
393 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
394 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100395 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
Peter Maydell08de2072012-06-20 11:57:14 +0000396 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
397 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100398 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
Peter Maydell08de2072012-06-20 11:57:14 +0000399 { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
400 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100401 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
Peter Maydell4fdd17d2012-06-20 11:57:15 +0000402 /* ??? This covers not just the impdef TLB lockdown registers but also
403 * some v7VMSA registers relating to TEX remap, so it is overly broad.
404 */
405 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
406 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
Peter Maydelld9298232012-06-20 11:57:16 +0000407 /* MMU TLB control. Note that the wildcarding means we cover not just
408 * the unified TLB ops but also the dside/iside/inner-shareable variants.
409 */
410 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100411 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
412 .type = ARM_CP_NO_MIGRATE },
Peter Maydelld9298232012-06-20 11:57:16 +0000413 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100414 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
415 .type = ARM_CP_NO_MIGRATE },
Peter Maydelld9298232012-06-20 11:57:16 +0000416 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100417 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
418 .type = ARM_CP_NO_MIGRATE },
Peter Maydelld9298232012-06-20 11:57:16 +0000419 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100420 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
421 .type = ARM_CP_NO_MIGRATE },
Peter Maydellc4804212012-06-20 11:57:17 +0000422 /* Cache maintenance ops; some of this space may be overridden later. */
423 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
424 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
425 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000426 REGINFO_SENTINEL
427};
428
Peter Maydell7d57f402012-06-20 11:57:11 +0000429static const ARMCPRegInfo not_v6_cp_reginfo[] = {
430 /* Not all pre-v6 cores implemented this WFI, so this is slightly
431 * over-broad.
432 */
433 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
434 .access = PL1_W, .type = ARM_CP_WFI },
435 REGINFO_SENTINEL
436};
437
438static const ARMCPRegInfo not_v7_cp_reginfo[] = {
439 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
440 * is UNPREDICTABLE; we choose to NOP as most implementations do).
441 */
442 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
443 .access = PL1_W, .type = ARM_CP_WFI },
Peter Maydell34f90522012-06-20 11:57:18 +0000444 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
445 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
446 * OMAPCP will override this space.
447 */
448 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
449 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
450 .resetvalue = 0 },
451 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
452 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
453 .resetvalue = 0 },
Peter Maydell776d4e52012-06-20 11:57:19 +0000454 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
455 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100456 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
457 .resetvalue = 0 },
Peter Maydell7d57f402012-06-20 11:57:11 +0000458 REGINFO_SENTINEL
459};
460
Peter Maydell2771db22012-06-20 11:57:18 +0000461static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
462{
463 if (env->cp15.c1_coproc != value) {
464 env->cp15.c1_coproc = value;
465 /* ??? Is this safe when called from within a TB? */
466 tb_flush(env);
467 }
468 return 0;
469}
470
Peter Maydell7d57f402012-06-20 11:57:11 +0000471static const ARMCPRegInfo v6_cp_reginfo[] = {
472 /* prefetch by MVA in v6, NOP in v7 */
473 { .name = "MVA_prefetch",
474 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
475 .access = PL1_W, .type = ARM_CP_NOP },
476 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
477 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell091fd172012-07-12 10:58:36 +0000478 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
Peter Maydell7d57f402012-06-20 11:57:11 +0000479 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell091fd172012-07-12 10:58:36 +0000480 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
Peter Maydell7d57f402012-06-20 11:57:11 +0000481 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell06d76f32012-06-20 11:57:17 +0000482 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
483 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
484 .resetvalue = 0, },
485 /* Watchpoint Fault Address Register : should actually only be present
486 * for 1136, 1176, 11MPCore.
487 */
488 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
489 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
Peter Maydell2771db22012-06-20 11:57:18 +0000490 { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
491 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
492 .resetvalue = 0, .writefn = cpacr_write },
Peter Maydell7d57f402012-06-20 11:57:11 +0000493 REGINFO_SENTINEL
494};
495
Peter Maydelld4e6df62013-06-25 18:16:07 +0100496
Peter Maydell200ac0e2012-06-20 11:57:12 +0000497static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
498 uint64_t *value)
499{
500 /* Generic performance monitor register read function for where
501 * user access may be allowed by PMUSERENR.
502 */
503 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
504 return EXCP_UDEF;
505 }
506 *value = CPREG_FIELD32(env, ri);
507 return 0;
508}
509
510static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
511 uint64_t value)
512{
513 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
514 return EXCP_UDEF;
515 }
516 /* only the DP, X, D and E bits are writable */
517 env->cp15.c9_pmcr &= ~0x39;
518 env->cp15.c9_pmcr |= (value & 0x39);
519 return 0;
520}
521
522static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
523 uint64_t value)
524{
525 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
526 return EXCP_UDEF;
527 }
528 value &= (1 << 31);
529 env->cp15.c9_pmcnten |= value;
530 return 0;
531}
532
533static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
534 uint64_t value)
535{
536 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
537 return EXCP_UDEF;
538 }
539 value &= (1 << 31);
540 env->cp15.c9_pmcnten &= ~value;
541 return 0;
542}
543
544static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
545 uint64_t value)
546{
547 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
548 return EXCP_UDEF;
549 }
550 env->cp15.c9_pmovsr &= ~value;
551 return 0;
552}
553
554static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
555 uint64_t value)
556{
557 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
558 return EXCP_UDEF;
559 }
560 env->cp15.c9_pmxevtyper = value & 0xff;
561 return 0;
562}
563
564static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566{
567 env->cp15.c9_pmuserenr = value & 1;
568 return 0;
569}
570
571static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
572 uint64_t value)
573{
574 /* We have no event counters so only the C bit can be changed */
575 value &= (1 << 31);
576 env->cp15.c9_pminten |= value;
577 return 0;
578}
579
580static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
581 uint64_t value)
582{
583 value &= (1 << 31);
584 env->cp15.c9_pminten &= ~value;
585 return 0;
586}
587
Nathan Rossi86411362013-10-25 15:44:38 +0100588static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
589 uint64_t value)
590{
591 env->cp15.c12_vbar = value & ~0x1Ful;
592 return 0;
593}
594
Peter Maydell776d4e52012-06-20 11:57:19 +0000595static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
596 uint64_t *value)
597{
598 ARMCPU *cpu = arm_env_get_cpu(env);
599 *value = cpu->ccsidr[env->cp15.c0_cssel];
600 return 0;
601}
602
603static int csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
604 uint64_t value)
605{
606 env->cp15.c0_cssel = value & 0xf;
607 return 0;
608}
609
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000610static const ARMCPRegInfo v7_cp_reginfo[] = {
611 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
612 * debug components
613 */
614 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
615 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell091fd172012-07-12 10:58:36 +0000616 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000617 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell7d57f402012-06-20 11:57:11 +0000618 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
619 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
620 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000621 /* Performance monitors are implementation defined in v7,
622 * but with an ARM recommended set of registers, which we
623 * follow (although we don't actually implement any counters)
624 *
625 * Performance registers fall into three categories:
626 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
627 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
628 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
629 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
630 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
631 */
632 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
633 .access = PL0_RW, .resetvalue = 0,
634 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100635 .readfn = pmreg_read, .writefn = pmcntenset_write,
636 .raw_readfn = raw_read, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000637 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
638 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100639 .readfn = pmreg_read, .writefn = pmcntenclr_write,
640 .type = ARM_CP_NO_MIGRATE },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000641 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
642 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100643 .readfn = pmreg_read, .writefn = pmovsr_write,
644 .raw_readfn = raw_read, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000645 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
646 * respect PMUSERENR.
647 */
648 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
649 .access = PL0_W, .type = ARM_CP_NOP },
650 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
651 * We choose to RAZ/WI. XXX should respect PMUSERENR.
652 */
653 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
654 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
655 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
656 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
657 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
658 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
659 .access = PL0_RW,
660 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100661 .readfn = pmreg_read, .writefn = pmxevtyper_write,
662 .raw_readfn = raw_read, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000663 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
664 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
665 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
666 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
667 .access = PL0_R | PL1_RW,
668 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
669 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100670 .writefn = pmuserenr_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000671 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
672 .access = PL1_RW,
673 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
674 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100675 .writefn = pmintenset_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000676 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100677 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
Peter Maydell200ac0e2012-06-20 11:57:12 +0000678 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100679 .resetvalue = 0, .writefn = pmintenclr_write, },
Nathan Rossi86411362013-10-25 15:44:38 +0100680 { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
681 .access = PL1_RW, .writefn = vbar_write,
682 .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
683 .resetvalue = 0 },
Peter Maydell2771db22012-06-20 11:57:18 +0000684 { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
685 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
686 .resetvalue = 0, },
Peter Maydell776d4e52012-06-20 11:57:19 +0000687 { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100688 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
Peter Maydell776d4e52012-06-20 11:57:19 +0000689 { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
690 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
691 .writefn = csselr_write, .resetvalue = 0 },
692 /* Auxiliary ID register: this actually has an IMPDEF value but for now
693 * just RAZ for all cores:
694 */
695 { .name = "AIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 7,
696 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000697 REGINFO_SENTINEL
698};
699
Peter Maydellc326b972012-06-20 11:57:10 +0000700static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
701{
702 value &= 1;
703 env->teecr = value;
704 return 0;
705}
706
707static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
708 uint64_t *value)
709{
710 /* This is a helper function because the user access rights
711 * depend on the value of the TEECR.
712 */
713 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
714 return EXCP_UDEF;
715 }
716 *value = env->teehbr;
717 return 0;
718}
719
720static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
721 uint64_t value)
722{
723 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
724 return EXCP_UDEF;
725 }
726 env->teehbr = value;
727 return 0;
728}
729
730static const ARMCPRegInfo t2ee_cp_reginfo[] = {
731 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
732 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
733 .resetvalue = 0,
734 .writefn = teecr_write },
735 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
736 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100737 .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write,
Peter Maydellc326b972012-06-20 11:57:10 +0000738 .readfn = teehbr_read, .writefn = teehbr_write },
739 REGINFO_SENTINEL
740};
741
Peter Maydell4d31c592012-06-20 11:57:11 +0000742static const ARMCPRegInfo v6k_cp_reginfo[] = {
743 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
744 .access = PL0_RW,
745 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
746 .resetvalue = 0 },
747 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
748 .access = PL0_R|PL1_W,
749 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
750 .resetvalue = 0 },
751 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
752 .access = PL1_RW,
753 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
754 .resetvalue = 0 },
755 REGINFO_SENTINEL
756};
757
Peter Maydell55d284a2013-08-20 14:54:31 +0100758#ifndef CONFIG_USER_ONLY
759
760static uint64_t gt_get_countervalue(CPUARMState *env)
761{
Alex Blighbc72ad62013-08-21 16:03:08 +0100762 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
Peter Maydell55d284a2013-08-20 14:54:31 +0100763}
764
765static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
766{
767 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
768
769 if (gt->ctl & 1) {
770 /* Timer enabled: calculate and set current ISTATUS, irq, and
771 * reset timer to when ISTATUS next has to change
772 */
773 uint64_t count = gt_get_countervalue(&cpu->env);
774 /* Note that this must be unsigned 64 bit arithmetic: */
775 int istatus = count >= gt->cval;
776 uint64_t nexttick;
777
778 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
779 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
780 (istatus && !(gt->ctl & 2)));
781 if (istatus) {
782 /* Next transition is when count rolls back over to zero */
783 nexttick = UINT64_MAX;
784 } else {
785 /* Next transition is when we hit cval */
786 nexttick = gt->cval;
787 }
788 /* Note that the desired next expiry time might be beyond the
789 * signed-64-bit range of a QEMUTimer -- in this case we just
790 * set the timer for as far in the future as possible. When the
791 * timer expires we will reset the timer for any remaining period.
792 */
793 if (nexttick > INT64_MAX / GTIMER_SCALE) {
794 nexttick = INT64_MAX / GTIMER_SCALE;
795 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100796 timer_mod(cpu->gt_timer[timeridx], nexttick);
Peter Maydell55d284a2013-08-20 14:54:31 +0100797 } else {
798 /* Timer disabled: ISTATUS and timer output always clear */
799 gt->ctl &= ~4;
800 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
Alex Blighbc72ad62013-08-21 16:03:08 +0100801 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell55d284a2013-08-20 14:54:31 +0100802 }
803}
804
805static int gt_cntfrq_read(CPUARMState *env, const ARMCPRegInfo *ri,
806 uint64_t *value)
807{
808 /* Not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
809 if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
810 return EXCP_UDEF;
811 }
812 *value = env->cp15.c14_cntfrq;
813 return 0;
814}
815
816static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
817{
818 ARMCPU *cpu = arm_env_get_cpu(env);
819 int timeridx = ri->opc1 & 1;
820
Alex Blighbc72ad62013-08-21 16:03:08 +0100821 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell55d284a2013-08-20 14:54:31 +0100822}
823
824static int gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri,
825 uint64_t *value)
826{
827 int timeridx = ri->opc1 & 1;
828
829 if (arm_current_pl(env) == 0 &&
830 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
831 return EXCP_UDEF;
832 }
833 *value = gt_get_countervalue(env);
834 return 0;
835}
836
837static int gt_cval_read(CPUARMState *env, const ARMCPRegInfo *ri,
838 uint64_t *value)
839{
840 int timeridx = ri->opc1 & 1;
841
842 if (arm_current_pl(env) == 0 &&
843 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
844 return EXCP_UDEF;
845 }
846 *value = env->cp15.c14_timer[timeridx].cval;
847 return 0;
848}
849
850static int gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
851 uint64_t value)
852{
853 int timeridx = ri->opc1 & 1;
854
855 env->cp15.c14_timer[timeridx].cval = value;
856 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
857 return 0;
858}
859static int gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
860 uint64_t *value)
861{
862 int timeridx = ri->crm & 1;
863
864 if (arm_current_pl(env) == 0 &&
865 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
866 return EXCP_UDEF;
867 }
868 *value = (uint32_t)(env->cp15.c14_timer[timeridx].cval -
869 gt_get_countervalue(env));
870 return 0;
871}
872
873static int gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
874 uint64_t value)
875{
876 int timeridx = ri->crm & 1;
877
878 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
879 + sextract64(value, 0, 32);
880 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
881 return 0;
882}
883
884static int gt_ctl_read(CPUARMState *env, const ARMCPRegInfo *ri,
885 uint64_t *value)
886{
887 int timeridx = ri->crm & 1;
888
889 if (arm_current_pl(env) == 0 &&
890 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
891 return EXCP_UDEF;
892 }
893 *value = env->cp15.c14_timer[timeridx].ctl;
894 return 0;
895}
896
897static int gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
898 uint64_t value)
899{
900 ARMCPU *cpu = arm_env_get_cpu(env);
901 int timeridx = ri->crm & 1;
902 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
903
904 env->cp15.c14_timer[timeridx].ctl = value & 3;
905 if ((oldval ^ value) & 1) {
906 /* Enable toggled */
907 gt_recalc_timer(cpu, timeridx);
908 } else if ((oldval & value) & 2) {
909 /* IMASK toggled: don't need to recalculate,
910 * just set the interrupt line based on ISTATUS
911 */
912 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
913 (oldval & 4) && (value & 2));
914 }
915 return 0;
916}
917
918void arm_gt_ptimer_cb(void *opaque)
919{
920 ARMCPU *cpu = opaque;
921
922 gt_recalc_timer(cpu, GTIMER_PHYS);
923}
924
925void arm_gt_vtimer_cb(void *opaque)
926{
927 ARMCPU *cpu = opaque;
928
929 gt_recalc_timer(cpu, GTIMER_VIRT);
930}
931
Peter Maydell6cc7a3a2012-06-20 11:57:12 +0000932static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
Peter Maydell55d284a2013-08-20 14:54:31 +0100933 /* Note that CNTFRQ is purely reads-as-written for the benefit
934 * of software; writing it doesn't actually change the timer frequency.
935 * Our reset value matches the fixed frequency we implement the timer at.
936 */
937 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
938 .access = PL1_RW | PL0_R,
939 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
940 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
941 .readfn = gt_cntfrq_read, .raw_readfn = raw_read,
942 },
943 /* overall control: mostly access permissions */
944 { .name = "CNTKCTL", .cp = 15, .crn = 14, .crm = 1, .opc1 = 0, .opc2 = 0,
945 .access = PL1_RW,
946 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
947 .resetvalue = 0,
948 },
949 /* per-timer control */
950 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
951 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
952 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
953 .resetvalue = 0,
954 .readfn = gt_ctl_read, .writefn = gt_ctl_write,
955 .raw_readfn = raw_read, .raw_writefn = raw_write,
956 },
957 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
958 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
959 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
960 .resetvalue = 0,
961 .readfn = gt_ctl_read, .writefn = gt_ctl_write,
962 .raw_readfn = raw_read, .raw_writefn = raw_write,
963 },
964 /* TimerValue views: a 32 bit downcounting view of the underlying state */
965 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
966 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
967 .readfn = gt_tval_read, .writefn = gt_tval_write,
968 },
969 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
970 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
971 .readfn = gt_tval_read, .writefn = gt_tval_write,
972 },
973 /* The counter itself */
974 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
975 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
976 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
977 },
978 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
979 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
980 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
981 },
982 /* Comparison value, indicating when the timer goes off */
983 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
984 .access = PL1_RW | PL0_R,
985 .type = ARM_CP_64BIT | ARM_CP_IO,
986 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
987 .resetvalue = 0,
988 .readfn = gt_cval_read, .writefn = gt_cval_write,
989 .raw_readfn = raw_read, .raw_writefn = raw_write,
990 },
991 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
992 .access = PL1_RW | PL0_R,
993 .type = ARM_CP_64BIT | ARM_CP_IO,
994 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
995 .resetvalue = 0,
996 .readfn = gt_cval_read, .writefn = gt_cval_write,
997 .raw_readfn = raw_read, .raw_writefn = raw_write,
998 },
Peter Maydell6cc7a3a2012-06-20 11:57:12 +0000999 REGINFO_SENTINEL
1000};
1001
Peter Maydell55d284a2013-08-20 14:54:31 +01001002#else
1003/* In user-mode none of the generic timer registers are accessible,
Alex Blighbc72ad62013-08-21 16:03:08 +01001004 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
Peter Maydell55d284a2013-08-20 14:54:31 +01001005 * so instead just don't register any of them.
1006 */
1007static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1008 REGINFO_SENTINEL
1009};
1010
1011#endif
1012
Peter Maydell4a501602012-06-20 11:57:16 +00001013static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1014{
Peter Maydell891a2fe2012-07-12 10:59:09 +00001015 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1016 env->cp15.c7_par = value;
1017 } else if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell4a501602012-06-20 11:57:16 +00001018 env->cp15.c7_par = value & 0xfffff6ff;
1019 } else {
1020 env->cp15.c7_par = value & 0xfffff1ff;
1021 }
1022 return 0;
1023}
1024
1025#ifndef CONFIG_USER_ONLY
1026/* get_phys_addr() isn't present for user-mode-only targets */
Peter Maydell702a9352012-07-12 10:59:10 +00001027
1028/* Return true if extended addresses are enabled, ie this is an
1029 * LPAE implementation and we are using the long-descriptor translation
1030 * table format because the TTBCR EAE bit is set.
1031 */
1032static inline bool extended_addresses_enabled(CPUARMState *env)
1033{
1034 return arm_feature(env, ARM_FEATURE_LPAE)
Peter Maydell78dbbbe2013-09-10 19:09:32 +01001035 && (env->cp15.c2_control & (1U << 31));
Peter Maydell702a9352012-07-12 10:59:10 +00001036}
1037
Peter Maydell4a501602012-06-20 11:57:16 +00001038static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1039{
Avi Kivitya8170e52012-10-23 12:30:10 +02001040 hwaddr phys_addr;
Peter Maydell4a501602012-06-20 11:57:16 +00001041 target_ulong page_size;
1042 int prot;
1043 int ret, is_user = ri->opc2 & 2;
1044 int access_type = ri->opc2 & 1;
1045
1046 if (ri->opc2 & 4) {
1047 /* Other states are only available with TrustZone */
1048 return EXCP_UDEF;
1049 }
1050 ret = get_phys_addr(env, value, access_type, is_user,
1051 &phys_addr, &prot, &page_size);
Peter Maydell702a9352012-07-12 10:59:10 +00001052 if (extended_addresses_enabled(env)) {
1053 /* ret is a DFSR/IFSR value for the long descriptor
1054 * translation table format, but with WnR always clear.
1055 * Convert it to a 64-bit PAR.
1056 */
1057 uint64_t par64 = (1 << 11); /* LPAE bit always set */
1058 if (ret == 0) {
1059 par64 |= phys_addr & ~0xfffULL;
1060 /* We don't set the ATTR or SH fields in the PAR. */
Peter Maydell4a501602012-06-20 11:57:16 +00001061 } else {
Peter Maydell702a9352012-07-12 10:59:10 +00001062 par64 |= 1; /* F */
1063 par64 |= (ret & 0x3f) << 1; /* FS */
1064 /* Note that S2WLK and FSTAGE are always zero, because we don't
1065 * implement virtualization and therefore there can't be a stage 2
1066 * fault.
1067 */
Peter Maydell4a501602012-06-20 11:57:16 +00001068 }
Peter Maydell702a9352012-07-12 10:59:10 +00001069 env->cp15.c7_par = par64;
1070 env->cp15.c7_par_hi = par64 >> 32;
Peter Maydell4a501602012-06-20 11:57:16 +00001071 } else {
Peter Maydell702a9352012-07-12 10:59:10 +00001072 /* ret is a DFSR/IFSR value for the short descriptor
1073 * translation table format (with WnR always clear).
1074 * Convert it to a 32-bit PAR.
1075 */
1076 if (ret == 0) {
1077 /* We do not set any attribute bits in the PAR */
1078 if (page_size == (1 << 24)
1079 && arm_feature(env, ARM_FEATURE_V7)) {
1080 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1081 } else {
1082 env->cp15.c7_par = phys_addr & 0xfffff000;
1083 }
1084 } else {
1085 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1086 ((ret & (12 << 1)) >> 6) |
1087 ((ret & 0xf) << 1) | 1;
1088 }
1089 env->cp15.c7_par_hi = 0;
Peter Maydell4a501602012-06-20 11:57:16 +00001090 }
1091 return 0;
1092}
1093#endif
1094
1095static const ARMCPRegInfo vapa_cp_reginfo[] = {
1096 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1097 .access = PL1_RW, .resetvalue = 0,
1098 .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
1099 .writefn = par_write },
1100#ifndef CONFIG_USER_ONLY
1101 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001102 .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
Peter Maydell4a501602012-06-20 11:57:16 +00001103#endif
1104 REGINFO_SENTINEL
1105};
1106
Peter Maydell18032be2012-06-20 11:57:13 +00001107/* Return basic MPU access permission bits. */
1108static uint32_t simple_mpu_ap_bits(uint32_t val)
1109{
1110 uint32_t ret;
1111 uint32_t mask;
1112 int i;
1113 ret = 0;
1114 mask = 3;
1115 for (i = 0; i < 16; i += 2) {
1116 ret |= (val >> i) & mask;
1117 mask <<= 2;
1118 }
1119 return ret;
1120}
1121
1122/* Pad basic MPU access permission bits to extended format. */
1123static uint32_t extended_mpu_ap_bits(uint32_t val)
1124{
1125 uint32_t ret;
1126 uint32_t mask;
1127 int i;
1128 ret = 0;
1129 mask = 3;
1130 for (i = 0; i < 16; i += 2) {
1131 ret |= (val & mask) << i;
1132 mask <<= 2;
1133 }
1134 return ret;
1135}
1136
1137static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1138 uint64_t value)
1139{
1140 env->cp15.c5_data = extended_mpu_ap_bits(value);
1141 return 0;
1142}
1143
1144static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1145 uint64_t *value)
1146{
1147 *value = simple_mpu_ap_bits(env->cp15.c5_data);
1148 return 0;
1149}
1150
1151static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1152 uint64_t value)
1153{
1154 env->cp15.c5_insn = extended_mpu_ap_bits(value);
1155 return 0;
1156}
1157
1158static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1159 uint64_t *value)
1160{
1161 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
1162 return 0;
1163}
1164
Peter Maydell06d76f32012-06-20 11:57:17 +00001165static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
1166 uint64_t *value)
1167{
Stefan Weil599d64f2012-09-04 07:35:57 +02001168 if (ri->crm >= 8) {
Peter Maydell06d76f32012-06-20 11:57:17 +00001169 return EXCP_UDEF;
1170 }
1171 *value = env->cp15.c6_region[ri->crm];
1172 return 0;
1173}
1174
1175static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
1176 uint64_t value)
1177{
Stefan Weil599d64f2012-09-04 07:35:57 +02001178 if (ri->crm >= 8) {
Peter Maydell06d76f32012-06-20 11:57:17 +00001179 return EXCP_UDEF;
1180 }
1181 env->cp15.c6_region[ri->crm] = value;
1182 return 0;
1183}
1184
Peter Maydell18032be2012-06-20 11:57:13 +00001185static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1186 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001187 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
Peter Maydell18032be2012-06-20 11:57:13 +00001188 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
1189 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1190 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001191 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
Peter Maydell18032be2012-06-20 11:57:13 +00001192 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
1193 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1194 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1195 .access = PL1_RW,
1196 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1197 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1198 .access = PL1_RW,
1199 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00001200 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1201 .access = PL1_RW,
1202 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1203 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1204 .access = PL1_RW,
1205 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
Peter Maydell06d76f32012-06-20 11:57:17 +00001206 /* Protection region base and size registers */
1207 { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
1208 .opc2 = CP_ANY, .access = PL1_RW,
1209 .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
Peter Maydell18032be2012-06-20 11:57:13 +00001210 REGINFO_SENTINEL
1211};
1212
Peter Maydelld4e6df62013-06-25 18:16:07 +01001213static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1214 uint64_t value)
Peter Maydellecce5c32012-06-20 11:57:14 +00001215{
Peter Maydell2ebcebe2013-06-27 16:38:47 +01001216 int maskshift = extract32(value, 0, 3);
1217
Sergey Fedorov74f1c6d2013-12-10 10:41:49 +04001218 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {
Peter Maydelle42c4db2012-07-12 10:59:11 +00001219 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
Peter Maydelle42c4db2012-07-12 10:59:11 +00001220 } else {
1221 value &= 7;
1222 }
1223 /* Note that we always calculate c2_mask and c2_base_mask, but
1224 * they are only used for short-descriptor tables (ie if EAE is 0);
1225 * for long-descriptor tables the TTBCR fields are used differently
1226 * and the c2_mask and c2_base_mask values are meaningless.
1227 */
Peter Maydellecce5c32012-06-20 11:57:14 +00001228 env->cp15.c2_control = value;
Peter Maydell2ebcebe2013-06-27 16:38:47 +01001229 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1230 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
Peter Maydellecce5c32012-06-20 11:57:14 +00001231 return 0;
1232}
1233
Peter Maydelld4e6df62013-06-25 18:16:07 +01001234static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1235 uint64_t value)
1236{
1237 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1238 /* With LPAE the TTBCR could result in a change of ASID
1239 * via the TTBCR.A1 bit, so do a TLB flush.
1240 */
1241 tlb_flush(env, 1);
1242 }
1243 return vmsa_ttbcr_raw_write(env, ri, value);
1244}
1245
Peter Maydellecce5c32012-06-20 11:57:14 +00001246static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1247{
1248 env->cp15.c2_base_mask = 0xffffc000u;
1249 env->cp15.c2_control = 0;
1250 env->cp15.c2_mask = 0;
1251}
1252
Peter Maydell18032be2012-06-20 11:57:13 +00001253static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1254 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1255 .access = PL1_RW,
1256 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1257 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1258 .access = PL1_RW,
1259 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00001260 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1261 .access = PL1_RW,
1262 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
1263 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1264 .access = PL1_RW,
Peter Maydell81a60ad2012-07-12 10:58:36 +00001265 .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00001266 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1267 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001268 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
Peter Maydellecce5c32012-06-20 11:57:14 +00001269 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
Peter Maydell06d76f32012-06-20 11:57:17 +00001270 { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1271 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
1272 .resetvalue = 0, },
Peter Maydell18032be2012-06-20 11:57:13 +00001273 REGINFO_SENTINEL
1274};
1275
Peter Maydell1047b9d2012-06-20 11:57:15 +00001276static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1277 uint64_t value)
1278{
1279 env->cp15.c15_ticonfig = value & 0xe7;
1280 /* The OS_TYPE bit in this register changes the reported CPUID! */
1281 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1282 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1283 return 0;
1284}
1285
1286static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1287 uint64_t value)
1288{
1289 env->cp15.c15_threadid = value & 0xffff;
1290 return 0;
1291}
1292
1293static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1294 uint64_t value)
1295{
1296 /* Wait-for-interrupt (deprecated) */
Andreas Färberc3affe52013-01-18 15:03:43 +01001297 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
Peter Maydell1047b9d2012-06-20 11:57:15 +00001298 return 0;
1299}
1300
Peter Maydellc4804212012-06-20 11:57:17 +00001301static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1302 uint64_t value)
1303{
1304 /* On OMAP there are registers indicating the max/min index of dcache lines
1305 * containing a dirty line; cache flush operations have to reset these.
1306 */
1307 env->cp15.c15_i_max = 0x000;
1308 env->cp15.c15_i_min = 0xff0;
1309 return 0;
1310}
1311
Peter Maydell18032be2012-06-20 11:57:13 +00001312static const ARMCPRegInfo omap_cp_reginfo[] = {
1313 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1314 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1315 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
Peter Maydell1047b9d2012-06-20 11:57:15 +00001316 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1317 .access = PL1_RW, .type = ARM_CP_NOP },
1318 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1319 .access = PL1_RW,
1320 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1321 .writefn = omap_ticonfig_write },
1322 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1323 .access = PL1_RW,
1324 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1325 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1326 .access = PL1_RW, .resetvalue = 0xff0,
1327 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1328 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1329 .access = PL1_RW,
1330 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1331 .writefn = omap_threadid_write },
1332 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1333 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001334 .type = ARM_CP_NO_MIGRATE,
Peter Maydell1047b9d2012-06-20 11:57:15 +00001335 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1336 /* TODO: Peripheral port remap register:
1337 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1338 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1339 * when MMU is off.
1340 */
Peter Maydellc4804212012-06-20 11:57:17 +00001341 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001342 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1343 .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
Peter Maydellc4804212012-06-20 11:57:17 +00001344 .writefn = omap_cachemaint_write },
Peter Maydell34f90522012-06-20 11:57:18 +00001345 { .name = "C9", .cp = 15, .crn = 9,
1346 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1347 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
Peter Maydell1047b9d2012-06-20 11:57:15 +00001348 REGINFO_SENTINEL
1349};
1350
1351static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1352 uint64_t value)
1353{
1354 value &= 0x3fff;
1355 if (env->cp15.c15_cpar != value) {
1356 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1357 tb_flush(env);
1358 env->cp15.c15_cpar = value;
1359 }
1360 return 0;
1361}
1362
1363static const ARMCPRegInfo xscale_cp_reginfo[] = {
1364 { .name = "XSCALE_CPAR",
1365 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1366 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1367 .writefn = xscale_cpar_write, },
Peter Maydell2771db22012-06-20 11:57:18 +00001368 { .name = "XSCALE_AUXCR",
1369 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1370 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1371 .resetvalue = 0, },
Peter Maydell1047b9d2012-06-20 11:57:15 +00001372 REGINFO_SENTINEL
1373};
1374
1375static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1376 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1377 * implementation of this implementation-defined space.
1378 * Ideally this should eventually disappear in favour of actually
1379 * implementing the correct behaviour for all cores.
1380 */
1381 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1382 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Crosthwaite3671cd82013-12-17 19:42:27 +00001383 .access = PL1_RW,
1384 .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE | ARM_CP_OVERRIDE,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001385 .resetvalue = 0 },
Peter Maydell18032be2012-06-20 11:57:13 +00001386 REGINFO_SENTINEL
1387};
1388
Peter Maydellc4804212012-06-20 11:57:17 +00001389static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1390 /* Cache status: RAZ because we have no cache so it's always clean */
1391 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001392 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1393 .resetvalue = 0 },
Peter Maydellc4804212012-06-20 11:57:17 +00001394 REGINFO_SENTINEL
1395};
1396
1397static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1398 /* We never have a a block transfer operation in progress */
1399 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001400 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1401 .resetvalue = 0 },
Peter Maydell30b05bb2012-06-20 11:57:22 +00001402 /* The cache ops themselves: these all NOP for QEMU */
1403 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1404 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1405 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1406 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1407 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1408 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1409 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1410 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1411 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1412 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1413 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1414 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
Peter Maydellc4804212012-06-20 11:57:17 +00001415 REGINFO_SENTINEL
1416};
1417
1418static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1419 /* The cache test-and-clean instructions always return (1 << 30)
1420 * to indicate that there are no dirty cache lines.
1421 */
1422 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001423 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1424 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00001425 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001426 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1427 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00001428 REGINFO_SENTINEL
1429};
1430
Peter Maydell34f90522012-06-20 11:57:18 +00001431static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1432 /* Ignore ReadBuffer accesses */
1433 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1434 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001435 .access = PL1_RW, .resetvalue = 0,
1436 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
Peter Maydell34f90522012-06-20 11:57:18 +00001437 REGINFO_SENTINEL
1438};
1439
Peter Maydell81bdde92012-06-20 11:57:20 +00001440static int mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1441 uint64_t *value)
1442{
Andreas Färber55e5c282012-12-17 06:18:02 +01001443 CPUState *cs = CPU(arm_env_get_cpu(env));
1444 uint32_t mpidr = cs->cpu_index;
Peter Maydell81bdde92012-06-20 11:57:20 +00001445 /* We don't support setting cluster ID ([8..11])
1446 * so these bits always RAZ.
1447 */
1448 if (arm_feature(env, ARM_FEATURE_V7MP)) {
Peter Maydell78dbbbe2013-09-10 19:09:32 +01001449 mpidr |= (1U << 31);
Peter Maydell81bdde92012-06-20 11:57:20 +00001450 /* Cores which are uniprocessor (non-coherent)
1451 * but still implement the MP extensions set
1452 * bit 30. (For instance, A9UP.) However we do
1453 * not currently model any of those cores.
1454 */
1455 }
1456 *value = mpidr;
1457 return 0;
1458}
1459
1460static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1461 { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001462 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
Peter Maydell81bdde92012-06-20 11:57:20 +00001463 REGINFO_SENTINEL
1464};
1465
Peter Maydell891a2fe2012-07-12 10:59:09 +00001466static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1467{
1468 *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1469 return 0;
1470}
1471
1472static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1473{
1474 env->cp15.c7_par_hi = value >> 32;
1475 env->cp15.c7_par = value;
1476 return 0;
1477}
1478
1479static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1480{
1481 env->cp15.c7_par_hi = 0;
1482 env->cp15.c7_par = 0;
1483}
1484
1485static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri,
1486 uint64_t *value)
1487{
1488 *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1489 return 0;
1490}
1491
Peter Maydelld4e6df62013-06-25 18:16:07 +01001492static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1493 uint64_t value)
Peter Maydell891a2fe2012-07-12 10:59:09 +00001494{
1495 env->cp15.c2_base0_hi = value >> 32;
1496 env->cp15.c2_base0 = value;
Peter Maydelld4e6df62013-06-25 18:16:07 +01001497 return 0;
1498}
1499
1500static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1501 uint64_t value)
1502{
Peter Maydell891a2fe2012-07-12 10:59:09 +00001503 /* Writes to the 64 bit format TTBRs may change the ASID */
1504 tlb_flush(env, 1);
Peter Maydelld4e6df62013-06-25 18:16:07 +01001505 return ttbr064_raw_write(env, ri, value);
Peter Maydell891a2fe2012-07-12 10:59:09 +00001506}
1507
1508static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1509{
1510 env->cp15.c2_base0_hi = 0;
1511 env->cp15.c2_base0 = 0;
1512}
1513
1514static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri,
1515 uint64_t *value)
1516{
1517 *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1518 return 0;
1519}
1520
1521static int ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri,
1522 uint64_t value)
1523{
1524 env->cp15.c2_base1_hi = value >> 32;
1525 env->cp15.c2_base1 = value;
1526 return 0;
1527}
1528
1529static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1530{
1531 env->cp15.c2_base1_hi = 0;
1532 env->cp15.c2_base1 = 0;
1533}
1534
Peter Maydell7ac681c2012-07-12 10:59:07 +00001535static const ARMCPRegInfo lpae_cp_reginfo[] = {
Peter Maydellb90372a2012-08-06 17:42:18 +01001536 /* NOP AMAIR0/1: the override is because these clash with the rather
Peter Maydell7ac681c2012-07-12 10:59:07 +00001537 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1538 */
1539 { .name = "AMAIR0", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1540 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1541 .resetvalue = 0 },
1542 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1543 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1544 .resetvalue = 0 },
Peter Maydellf9fc6192012-07-12 10:59:08 +00001545 /* 64 bit access versions of the (dummy) debug registers */
1546 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
1547 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1548 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
1549 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
Peter Maydell891a2fe2012-07-12 10:59:09 +00001550 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1551 .access = PL1_RW, .type = ARM_CP_64BIT,
1552 .readfn = par64_read, .writefn = par64_write, .resetfn = par64_reset },
1553 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1554 .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr064_read,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001555 .writefn = ttbr064_write, .raw_writefn = ttbr064_raw_write,
1556 .resetfn = ttbr064_reset },
Peter Maydell891a2fe2012-07-12 10:59:09 +00001557 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1558 .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr164_read,
1559 .writefn = ttbr164_write, .resetfn = ttbr164_reset },
Peter Maydell7ac681c2012-07-12 10:59:07 +00001560 REGINFO_SENTINEL
1561};
1562
Peter Maydell2771db22012-06-20 11:57:18 +00001563static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1564{
1565 env->cp15.c1_sys = value;
1566 /* ??? Lots of these bits are not implemented. */
1567 /* This may enable/disable the MMU, so do a TLB flush. */
1568 tlb_flush(env, 1);
1569 return 0;
1570}
1571
Peter Maydell2ceb98c2012-06-20 11:57:09 +00001572void register_cp_regs_for_features(ARMCPU *cpu)
1573{
1574 /* Register all the coprocessor registers based on feature bits */
1575 CPUARMState *env = &cpu->env;
1576 if (arm_feature(env, ARM_FEATURE_M)) {
1577 /* M profile has no coprocessor registers */
1578 return;
1579 }
1580
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001581 define_arm_cp_regs(cpu, cp_reginfo);
Peter Maydell7d57f402012-06-20 11:57:11 +00001582 if (arm_feature(env, ARM_FEATURE_V6)) {
Peter Maydell8515a092012-06-20 11:57:19 +00001583 /* The ID registers all have impdef reset values */
1584 ARMCPRegInfo v6_idregs[] = {
1585 { .name = "ID_PFR0", .cp = 15, .crn = 0, .crm = 1,
1586 .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1587 .resetvalue = cpu->id_pfr0 },
1588 { .name = "ID_PFR1", .cp = 15, .crn = 0, .crm = 1,
1589 .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1590 .resetvalue = cpu->id_pfr1 },
1591 { .name = "ID_DFR0", .cp = 15, .crn = 0, .crm = 1,
1592 .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1593 .resetvalue = cpu->id_dfr0 },
1594 { .name = "ID_AFR0", .cp = 15, .crn = 0, .crm = 1,
1595 .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1596 .resetvalue = cpu->id_afr0 },
1597 { .name = "ID_MMFR0", .cp = 15, .crn = 0, .crm = 1,
1598 .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1599 .resetvalue = cpu->id_mmfr0 },
1600 { .name = "ID_MMFR1", .cp = 15, .crn = 0, .crm = 1,
1601 .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1602 .resetvalue = cpu->id_mmfr1 },
1603 { .name = "ID_MMFR2", .cp = 15, .crn = 0, .crm = 1,
1604 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1605 .resetvalue = cpu->id_mmfr2 },
1606 { .name = "ID_MMFR3", .cp = 15, .crn = 0, .crm = 1,
1607 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1608 .resetvalue = cpu->id_mmfr3 },
1609 { .name = "ID_ISAR0", .cp = 15, .crn = 0, .crm = 2,
1610 .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1611 .resetvalue = cpu->id_isar0 },
1612 { .name = "ID_ISAR1", .cp = 15, .crn = 0, .crm = 2,
1613 .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1614 .resetvalue = cpu->id_isar1 },
1615 { .name = "ID_ISAR2", .cp = 15, .crn = 0, .crm = 2,
1616 .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1617 .resetvalue = cpu->id_isar2 },
1618 { .name = "ID_ISAR3", .cp = 15, .crn = 0, .crm = 2,
1619 .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1620 .resetvalue = cpu->id_isar3 },
1621 { .name = "ID_ISAR4", .cp = 15, .crn = 0, .crm = 2,
1622 .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1623 .resetvalue = cpu->id_isar4 },
1624 { .name = "ID_ISAR5", .cp = 15, .crn = 0, .crm = 2,
1625 .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1626 .resetvalue = cpu->id_isar5 },
1627 /* 6..7 are as yet unallocated and must RAZ */
1628 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
1629 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1630 .resetvalue = 0 },
1631 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
1632 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1633 .resetvalue = 0 },
1634 REGINFO_SENTINEL
1635 };
1636 define_arm_cp_regs(cpu, v6_idregs);
Peter Maydell7d57f402012-06-20 11:57:11 +00001637 define_arm_cp_regs(cpu, v6_cp_reginfo);
1638 } else {
1639 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
1640 }
Peter Maydell4d31c592012-06-20 11:57:11 +00001641 if (arm_feature(env, ARM_FEATURE_V6K)) {
1642 define_arm_cp_regs(cpu, v6k_cp_reginfo);
1643 }
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001644 if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell200ac0e2012-06-20 11:57:12 +00001645 /* v7 performance monitor control register: same implementor
1646 * field as main ID register, and we implement no event counters.
1647 */
1648 ARMCPRegInfo pmcr = {
1649 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
1650 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
1651 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
Peter Maydelld4e6df62013-06-25 18:16:07 +01001652 .readfn = pmreg_read, .writefn = pmcr_write,
1653 .raw_readfn = raw_read, .raw_writefn = raw_write,
Peter Maydell200ac0e2012-06-20 11:57:12 +00001654 };
Peter Maydell776d4e52012-06-20 11:57:19 +00001655 ARMCPRegInfo clidr = {
1656 .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
1657 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
1658 };
Peter Maydell200ac0e2012-06-20 11:57:12 +00001659 define_one_arm_cp_reg(cpu, &pmcr);
Peter Maydell776d4e52012-06-20 11:57:19 +00001660 define_one_arm_cp_reg(cpu, &clidr);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001661 define_arm_cp_regs(cpu, v7_cp_reginfo);
Peter Maydell7d57f402012-06-20 11:57:11 +00001662 } else {
1663 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001664 }
Peter Maydell18032be2012-06-20 11:57:13 +00001665 if (arm_feature(env, ARM_FEATURE_MPU)) {
1666 /* These are the MPU registers prior to PMSAv6. Any new
1667 * PMSA core later than the ARM946 will require that we
1668 * implement the PMSAv6 or PMSAv7 registers, which are
1669 * completely different.
1670 */
1671 assert(!arm_feature(env, ARM_FEATURE_V6));
1672 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
1673 } else {
1674 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
1675 }
Peter Maydellc326b972012-06-20 11:57:10 +00001676 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
1677 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
1678 }
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00001679 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1680 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
1681 }
Peter Maydell4a501602012-06-20 11:57:16 +00001682 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1683 define_arm_cp_regs(cpu, vapa_cp_reginfo);
1684 }
Peter Maydellc4804212012-06-20 11:57:17 +00001685 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
1686 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
1687 }
1688 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
1689 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
1690 }
1691 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
1692 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
1693 }
Peter Maydell18032be2012-06-20 11:57:13 +00001694 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1695 define_arm_cp_regs(cpu, omap_cp_reginfo);
1696 }
Peter Maydell34f90522012-06-20 11:57:18 +00001697 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
1698 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
1699 }
Peter Maydell1047b9d2012-06-20 11:57:15 +00001700 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1701 define_arm_cp_regs(cpu, xscale_cp_reginfo);
1702 }
1703 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
1704 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
1705 }
Peter Maydell7ac681c2012-07-12 10:59:07 +00001706 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1707 define_arm_cp_regs(cpu, lpae_cp_reginfo);
1708 }
Peter Maydell78848492012-06-20 11:57:20 +00001709 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
1710 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
1711 * be read-only (ie write causes UNDEF exception).
1712 */
1713 {
1714 ARMCPRegInfo id_cp_reginfo[] = {
1715 /* Note that the MIDR isn't a simple constant register because
1716 * of the TI925 behaviour where writes to another register can
1717 * cause the MIDR value to change.
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001718 *
1719 * Unimplemented registers in the c15 0 0 0 space default to
1720 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
1721 * and friends override accordingly.
Peter Maydell78848492012-06-20 11:57:20 +00001722 */
1723 { .name = "MIDR",
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001724 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydell78848492012-06-20 11:57:20 +00001725 .access = PL1_R, .resetvalue = cpu->midr,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001726 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001727 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
1728 .type = ARM_CP_OVERRIDE },
Peter Maydell78848492012-06-20 11:57:20 +00001729 { .name = "CTR",
1730 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
1731 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
1732 { .name = "TCMTR",
1733 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
1734 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1735 { .name = "TLBTR",
1736 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
1737 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1738 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
1739 { .name = "DUMMY",
1740 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
1741 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1742 { .name = "DUMMY",
1743 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
1744 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1745 { .name = "DUMMY",
1746 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
1747 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1748 { .name = "DUMMY",
1749 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
1750 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1751 { .name = "DUMMY",
1752 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
1753 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1754 REGINFO_SENTINEL
1755 };
1756 ARMCPRegInfo crn0_wi_reginfo = {
1757 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
1758 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
1759 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
1760 };
1761 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
1762 arm_feature(env, ARM_FEATURE_STRONGARM)) {
1763 ARMCPRegInfo *r;
1764 /* Register the blanket "writes ignored" value first to cover the
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10001765 * whole space. Then update the specific ID registers to allow write
1766 * access, so that they ignore writes rather than causing them to
1767 * UNDEF.
Peter Maydell78848492012-06-20 11:57:20 +00001768 */
1769 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
1770 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
1771 r->access = PL1_RW;
Peter Maydell78848492012-06-20 11:57:20 +00001772 }
Peter Maydell78848492012-06-20 11:57:20 +00001773 }
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10001774 define_arm_cp_regs(cpu, id_cp_reginfo);
Peter Maydell78848492012-06-20 11:57:20 +00001775 }
1776
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001777 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
1778 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
1779 }
1780
Peter Maydell2771db22012-06-20 11:57:18 +00001781 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
1782 ARMCPRegInfo auxcr = {
1783 .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
1784 .access = PL1_RW, .type = ARM_CP_CONST,
1785 .resetvalue = cpu->reset_auxcr
1786 };
1787 define_one_arm_cp_reg(cpu, &auxcr);
1788 }
1789
Peter Crosthwaited8ba7802013-12-17 19:42:28 +00001790 if (arm_feature(env, ARM_FEATURE_CBAR)) {
1791 ARMCPRegInfo cbar = {
1792 .name = "CBAR", .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
1793 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
1794 .fieldoffset = offsetof(CPUARMState, cp15.c15_config_base_address)
1795 };
1796 define_one_arm_cp_reg(cpu, &cbar);
1797 }
1798
Peter Maydell2771db22012-06-20 11:57:18 +00001799 /* Generic registers whose values depend on the implementation */
1800 {
1801 ARMCPRegInfo sctlr = {
1802 .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
1803 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
Peter Maydelld4e6df62013-06-25 18:16:07 +01001804 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
1805 .raw_writefn = raw_write,
Peter Maydell2771db22012-06-20 11:57:18 +00001806 };
1807 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1808 /* Normally we would always end the TB on an SCTLR write, but Linux
1809 * arch/arm/mach-pxa/sleep.S expects two instructions following
1810 * an MMU enable to execute from cache. Imitate this behaviour.
1811 */
1812 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
1813 }
1814 define_one_arm_cp_reg(cpu, &sctlr);
1815 }
Peter Maydell2ceb98c2012-06-20 11:57:09 +00001816}
1817
Andreas Färber778c3a02012-04-20 07:39:14 +00001818ARMCPU *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +00001819{
Andreas Färberdec9c2d2012-03-29 04:50:31 +00001820 ARMCPU *cpu;
Andreas Färber5900d6b2013-01-21 16:11:43 +01001821 ObjectClass *oc;
pbrook40f137e2006-02-20 00:33:36 +00001822
Andreas Färber5900d6b2013-01-21 16:11:43 +01001823 oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1824 if (!oc) {
bellardaaed9092007-11-10 15:15:54 +00001825 return NULL;
Peter Maydell777dc782012-04-20 17:58:31 +00001826 }
Andreas Färber5900d6b2013-01-21 16:11:43 +01001827 cpu = ARM_CPU(object_new(object_class_get_name(oc)));
Andreas Färber14969262013-01-05 10:18:18 +01001828
1829 /* TODO this should be set centrally, once possible */
1830 object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
Peter Maydell777dc782012-04-20 17:58:31 +00001831
Andreas Färber14969262013-01-05 10:18:18 +01001832 return cpu;
1833}
1834
1835void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
1836{
Andreas Färber22169d42013-06-28 21:27:39 +02001837 CPUState *cs = CPU(cpu);
Andreas Färber14969262013-01-05 10:18:18 +01001838 CPUARMState *env = &cpu->env;
1839
Peter Maydell6a669422013-12-17 19:42:32 +00001840 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
1841 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
1842 aarch64_fpu_gdb_set_reg,
1843 34, "aarch64-fpu.xml", 0);
1844 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
Andreas Färber22169d42013-06-28 21:27:39 +02001845 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00001846 51, "arm-neon.xml", 0);
1847 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
Andreas Färber22169d42013-06-28 21:27:39 +02001848 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00001849 35, "arm-vfp3.xml", 0);
1850 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
Andreas Färber22169d42013-06-28 21:27:39 +02001851 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00001852 19, "arm-vfp.xml", 0);
1853 }
pbrook40f137e2006-02-20 00:33:36 +00001854}
1855
Peter Maydell777dc782012-04-20 17:58:31 +00001856/* Sort alphabetically by type name, except for "any". */
1857static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
pbrook5adb4832007-03-08 03:15:18 +00001858{
Peter Maydell777dc782012-04-20 17:58:31 +00001859 ObjectClass *class_a = (ObjectClass *)a;
1860 ObjectClass *class_b = (ObjectClass *)b;
1861 const char *name_a, *name_b;
pbrook5adb4832007-03-08 03:15:18 +00001862
Peter Maydell777dc782012-04-20 17:58:31 +00001863 name_a = object_class_get_name(class_a);
1864 name_b = object_class_get_name(class_b);
Andreas Färber51492fd2013-01-27 17:30:10 +01001865 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00001866 return 1;
Andreas Färber51492fd2013-01-27 17:30:10 +01001867 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00001868 return -1;
1869 } else {
1870 return strcmp(name_a, name_b);
pbrook5adb4832007-03-08 03:15:18 +00001871 }
1872}
1873
Peter Maydell777dc782012-04-20 17:58:31 +00001874static void arm_cpu_list_entry(gpointer data, gpointer user_data)
pbrook40f137e2006-02-20 00:33:36 +00001875{
Peter Maydell777dc782012-04-20 17:58:31 +00001876 ObjectClass *oc = data;
Andreas Färber92a31362012-12-16 02:17:02 +01001877 CPUListState *s = user_data;
Andreas Färber51492fd2013-01-27 17:30:10 +01001878 const char *typename;
1879 char *name;
pbrook3371d272007-03-08 03:04:12 +00001880
Andreas Färber51492fd2013-01-27 17:30:10 +01001881 typename = object_class_get_name(oc);
1882 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
Peter Maydell777dc782012-04-20 17:58:31 +00001883 (*s->cpu_fprintf)(s->file, " %s\n",
Andreas Färber51492fd2013-01-27 17:30:10 +01001884 name);
1885 g_free(name);
Peter Maydell777dc782012-04-20 17:58:31 +00001886}
1887
1888void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1889{
Andreas Färber92a31362012-12-16 02:17:02 +01001890 CPUListState s = {
Peter Maydell777dc782012-04-20 17:58:31 +00001891 .file = f,
1892 .cpu_fprintf = cpu_fprintf,
1893 };
1894 GSList *list;
1895
1896 list = object_class_get_list(TYPE_ARM_CPU, false);
1897 list = g_slist_sort(list, arm_cpu_list_compare);
1898 (*cpu_fprintf)(f, "Available CPUs:\n");
1899 g_slist_foreach(list, arm_cpu_list_entry, &s);
1900 g_slist_free(list);
Peter Maydella96c0512013-11-22 17:17:17 +00001901#ifdef CONFIG_KVM
1902 /* The 'host' CPU type is dynamically registered only if KVM is
1903 * enabled, so we have to special-case it here:
1904 */
1905 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
1906#endif
pbrook40f137e2006-02-20 00:33:36 +00001907}
1908
Cole Robinson78027bb2013-09-10 19:09:33 +01001909static void arm_cpu_add_definition(gpointer data, gpointer user_data)
1910{
1911 ObjectClass *oc = data;
1912 CpuDefinitionInfoList **cpu_list = user_data;
1913 CpuDefinitionInfoList *entry;
1914 CpuDefinitionInfo *info;
1915 const char *typename;
1916
1917 typename = object_class_get_name(oc);
1918 info = g_malloc0(sizeof(*info));
1919 info->name = g_strndup(typename,
1920 strlen(typename) - strlen("-" TYPE_ARM_CPU));
1921
1922 entry = g_malloc0(sizeof(*entry));
1923 entry->value = info;
1924 entry->next = *cpu_list;
1925 *cpu_list = entry;
1926}
1927
1928CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1929{
1930 CpuDefinitionInfoList *cpu_list = NULL;
1931 GSList *list;
1932
1933 list = object_class_get_list(TYPE_ARM_CPU, false);
1934 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
1935 g_slist_free(list);
1936
1937 return cpu_list;
1938}
1939
Peter Maydell49c199d2013-12-22 22:32:30 +00001940static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
Peter Maydelldd61e562013-12-22 22:32:30 +00001941 void *opaque, int state,
1942 int crm, int opc1, int opc2)
Peter Maydell49c199d2013-12-22 22:32:30 +00001943{
1944 /* Private utility function for define_one_arm_cp_reg_with_opaque():
1945 * add a single reginfo struct to the hash table.
1946 */
1947 uint32_t *key = g_new(uint32_t, 1);
1948 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
1949 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
Peter Maydelldd61e562013-12-22 22:32:30 +00001950 if (r->state == ARM_CP_STATE_BOTH && state == ARM_CP_STATE_AA32) {
1951 /* The AArch32 view of a shared register sees the lower 32 bits
1952 * of a 64 bit backing field. It is not migratable as the AArch64
1953 * view handles that. AArch64 also handles reset.
1954 * We assume it is a cp15 register.
1955 */
1956 r2->cp = 15;
1957 r2->type |= ARM_CP_NO_MIGRATE;
1958 r2->resetfn = arm_cp_reset_ignore;
1959#ifdef HOST_WORDS_BIGENDIAN
1960 if (r2->fieldoffset) {
1961 r2->fieldoffset += sizeof(uint32_t);
1962 }
1963#endif
1964 }
1965 if (state == ARM_CP_STATE_AA64) {
1966 /* To allow abbreviation of ARMCPRegInfo
1967 * definitions, we treat cp == 0 as equivalent to
1968 * the value for "standard guest-visible sysreg".
1969 */
1970 if (r->cp == 0) {
1971 r2->cp = CP_REG_ARM64_SYSREG_CP;
1972 }
1973 *key = ENCODE_AA64_CP_REG(r2->cp, r->crn, crm,
1974 r->opc0, opc1, opc2);
1975 } else {
1976 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
1977 }
Peter Maydell49c199d2013-12-22 22:32:30 +00001978 if (opaque) {
1979 r2->opaque = opaque;
1980 }
1981 /* Make sure reginfo passed to helpers for wildcarded regs
1982 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
1983 */
1984 r2->crm = crm;
1985 r2->opc1 = opc1;
1986 r2->opc2 = opc2;
1987 /* By convention, for wildcarded registers only the first
1988 * entry is used for migration; the others are marked as
1989 * NO_MIGRATE so we don't try to transfer the register
1990 * multiple times. Special registers (ie NOP/WFI) are
1991 * never migratable.
1992 */
1993 if ((r->type & ARM_CP_SPECIAL) ||
1994 ((r->crm == CP_ANY) && crm != 0) ||
1995 ((r->opc1 == CP_ANY) && opc1 != 0) ||
1996 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1997 r2->type |= ARM_CP_NO_MIGRATE;
1998 }
1999
2000 /* Overriding of an existing definition must be explicitly
2001 * requested.
2002 */
2003 if (!(r->type & ARM_CP_OVERRIDE)) {
2004 ARMCPRegInfo *oldreg;
2005 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
2006 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
2007 fprintf(stderr, "Register redefined: cp=%d %d bit "
2008 "crn=%d crm=%d opc1=%d opc2=%d, "
2009 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
2010 r2->crn, r2->crm, r2->opc1, r2->opc2,
2011 oldreg->name, r2->name);
2012 g_assert_not_reached();
2013 }
2014 }
2015 g_hash_table_insert(cpu->cp_regs, key, r2);
2016}
2017
2018
Peter Maydell4b6a83f2012-06-20 11:57:06 +00002019void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
2020 const ARMCPRegInfo *r, void *opaque)
2021{
2022 /* Define implementations of coprocessor registers.
2023 * We store these in a hashtable because typically
2024 * there are less than 150 registers in a space which
2025 * is 16*16*16*8*8 = 262144 in size.
2026 * Wildcarding is supported for the crm, opc1 and opc2 fields.
2027 * If a register is defined twice then the second definition is
2028 * used, so this can be used to define some generic registers and
2029 * then override them with implementation specific variations.
2030 * At least one of the original and the second definition should
2031 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
2032 * against accidental use.
Peter Maydelldd61e562013-12-22 22:32:30 +00002033 *
2034 * The state field defines whether the register is to be
2035 * visible in the AArch32 or AArch64 execution state. If the
2036 * state is set to ARM_CP_STATE_BOTH then we synthesise a
2037 * reginfo structure for the AArch32 view, which sees the lower
2038 * 32 bits of the 64 bit register.
2039 *
2040 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
2041 * be wildcarded. AArch64 registers are always considered to be 64
2042 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
2043 * the register, if any.
Peter Maydell4b6a83f2012-06-20 11:57:06 +00002044 */
Peter Maydelldd61e562013-12-22 22:32:30 +00002045 int crm, opc1, opc2, state;
Peter Maydell4b6a83f2012-06-20 11:57:06 +00002046 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
2047 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
2048 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
2049 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
2050 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
2051 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
2052 /* 64 bit registers have only CRm and Opc1 fields */
2053 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
Peter Maydelldd61e562013-12-22 22:32:30 +00002054 /* op0 only exists in the AArch64 encodings */
2055 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
2056 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
2057 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
2058 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
2059 * encodes a minimum access level for the register. We roll this
2060 * runtime check into our general permission check code, so check
2061 * here that the reginfo's specified permissions are strict enough
2062 * to encompass the generic architectural permission check.
2063 */
2064 if (r->state != ARM_CP_STATE_AA32) {
2065 int mask = 0;
2066 switch (r->opc1) {
2067 case 0: case 1: case 2:
2068 /* min_EL EL1 */
2069 mask = PL1_RW;
2070 break;
2071 case 3:
2072 /* min_EL EL0 */
2073 mask = PL0_RW;
2074 break;
2075 case 4:
2076 /* min_EL EL2 */
2077 mask = PL2_RW;
2078 break;
2079 case 5:
2080 /* unallocated encoding, so not possible */
2081 assert(false);
2082 break;
2083 case 6:
2084 /* min_EL EL3 */
2085 mask = PL3_RW;
2086 break;
2087 case 7:
2088 /* min_EL EL1, secure mode only (we don't check the latter) */
2089 mask = PL1_RW;
2090 break;
2091 default:
2092 /* broken reginfo with out-of-range opc1 */
2093 assert(false);
2094 break;
2095 }
2096 /* assert our permissions are not too lax (stricter is fine) */
2097 assert((r->access & ~mask) == 0);
2098 }
2099
Peter Maydell4b6a83f2012-06-20 11:57:06 +00002100 /* Check that the register definition has enough info to handle
2101 * reads and writes if they are permitted.
2102 */
2103 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
2104 if (r->access & PL3_R) {
2105 assert(r->fieldoffset || r->readfn);
2106 }
2107 if (r->access & PL3_W) {
2108 assert(r->fieldoffset || r->writefn);
2109 }
2110 }
2111 /* Bad type field probably means missing sentinel at end of reg list */
2112 assert(cptype_valid(r->type));
2113 for (crm = crmmin; crm <= crmmax; crm++) {
2114 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
2115 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
Peter Maydelldd61e562013-12-22 22:32:30 +00002116 for (state = ARM_CP_STATE_AA32;
2117 state <= ARM_CP_STATE_AA64; state++) {
2118 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
2119 continue;
2120 }
2121 add_cpreg_to_hashtable(cpu, r, opaque, state,
2122 crm, opc1, opc2);
2123 }
Peter Maydell4b6a83f2012-06-20 11:57:06 +00002124 }
2125 }
2126 }
2127}
2128
2129void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
2130 const ARMCPRegInfo *regs, void *opaque)
2131{
2132 /* Define a whole list of registers */
2133 const ARMCPRegInfo *r;
2134 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
2135 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
2136 }
2137}
2138
Peter Maydell8ca70eb2013-12-22 22:32:30 +00002139const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
Peter Maydell4b6a83f2012-06-20 11:57:06 +00002140{
Peter Maydell8ca70eb2013-12-22 22:32:30 +00002141 return g_hash_table_lookup(cpregs, &encoded_cp);
Peter Maydell4b6a83f2012-06-20 11:57:06 +00002142}
2143
2144int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
2145 uint64_t value)
2146{
2147 /* Helper coprocessor write function for write-ignore registers */
2148 return 0;
2149}
2150
2151int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
2152{
2153 /* Helper coprocessor write function for read-as-zero registers */
2154 *value = 0;
2155 return 0;
2156}
2157
Peter Maydelldd61e562013-12-22 22:32:30 +00002158void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
2159{
2160 /* Helper coprocessor reset function for do-nothing-on-reset registers */
2161}
2162
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002163static int bad_mode_switch(CPUARMState *env, int mode)
Peter Maydell37064a82012-01-05 15:49:06 +00002164{
2165 /* Return true if it is not valid for us to switch to
2166 * this CPU mode (ie all the UNPREDICTABLE cases in
2167 * the ARM ARM CPSRWriteByInstr pseudocode).
2168 */
2169 switch (mode) {
2170 case ARM_CPU_MODE_USR:
2171 case ARM_CPU_MODE_SYS:
2172 case ARM_CPU_MODE_SVC:
2173 case ARM_CPU_MODE_ABT:
2174 case ARM_CPU_MODE_UND:
2175 case ARM_CPU_MODE_IRQ:
2176 case ARM_CPU_MODE_FIQ:
2177 return 0;
2178 default:
2179 return 1;
2180 }
2181}
2182
balrog2f4a40e2007-11-13 01:50:15 +00002183uint32_t cpsr_read(CPUARMState *env)
2184{
2185 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +00002186 ZF = (env->ZF == 0);
2187 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +00002188 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2189 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2190 | ((env->condexec_bits & 0xfc) << 8)
2191 | (env->GE << 16);
2192}
2193
2194void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2195{
balrog2f4a40e2007-11-13 01:50:15 +00002196 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +00002197 env->ZF = (~val) & CPSR_Z;
2198 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +00002199 env->CF = (val >> 29) & 1;
2200 env->VF = (val << 3) & 0x80000000;
2201 }
2202 if (mask & CPSR_Q)
2203 env->QF = ((val & CPSR_Q) != 0);
2204 if (mask & CPSR_T)
2205 env->thumb = ((val & CPSR_T) != 0);
2206 if (mask & CPSR_IT_0_1) {
2207 env->condexec_bits &= ~3;
2208 env->condexec_bits |= (val >> 25) & 3;
2209 }
2210 if (mask & CPSR_IT_2_7) {
2211 env->condexec_bits &= 3;
2212 env->condexec_bits |= (val >> 8) & 0xfc;
2213 }
2214 if (mask & CPSR_GE) {
2215 env->GE = (val >> 16) & 0xf;
2216 }
2217
2218 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
Peter Maydell37064a82012-01-05 15:49:06 +00002219 if (bad_mode_switch(env, val & CPSR_M)) {
2220 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2221 * We choose to ignore the attempt and leave the CPSR M field
2222 * untouched.
2223 */
2224 mask &= ~CPSR_M;
2225 } else {
2226 switch_mode(env, val & CPSR_M);
2227 }
balrog2f4a40e2007-11-13 01:50:15 +00002228 }
2229 mask &= ~CACHED_CPSR_BITS;
2230 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2231}
2232
pbrookb26eefb2008-03-31 03:44:26 +00002233/* Sign/zero extend */
2234uint32_t HELPER(sxtb16)(uint32_t x)
2235{
2236 uint32_t res;
2237 res = (uint16_t)(int8_t)x;
2238 res |= (uint32_t)(int8_t)(x >> 16) << 16;
2239 return res;
2240}
2241
2242uint32_t HELPER(uxtb16)(uint32_t x)
2243{
2244 uint32_t res;
2245 res = (uint16_t)(uint8_t)x;
2246 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
2247 return res;
2248}
2249
pbrookf51bbbf2008-03-31 03:45:13 +00002250uint32_t HELPER(clz)(uint32_t x)
2251{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02002252 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +00002253}
2254
pbrook36706692008-03-31 03:46:19 +00002255int32_t HELPER(sdiv)(int32_t num, int32_t den)
2256{
2257 if (den == 0)
2258 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +02002259 if (num == INT_MIN && den == -1)
2260 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +00002261 return num / den;
2262}
2263
2264uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
2265{
2266 if (den == 0)
2267 return 0;
2268 return num / den;
2269}
2270
2271uint32_t HELPER(rbit)(uint32_t x)
2272{
2273 x = ((x & 0xff000000) >> 24)
2274 | ((x & 0x00ff0000) >> 8)
2275 | ((x & 0x0000ff00) << 8)
2276 | ((x & 0x000000ff) << 24);
2277 x = ((x & 0xf0f0f0f0) >> 4)
2278 | ((x & 0x0f0f0f0f) << 4);
2279 x = ((x & 0x88888888) >> 3)
2280 | ((x & 0x44444444) >> 1)
2281 | ((x & 0x22222222) << 1)
2282 | ((x & 0x11111111) << 3);
2283 return x;
2284}
2285
ths5fafdf22007-09-16 21:08:06 +00002286#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +00002287
Andreas Färber97a8ea52013-02-02 10:57:51 +01002288void arm_cpu_do_interrupt(CPUState *cs)
bellardb5ff1b32005-11-26 10:38:39 +00002289{
Andreas Färber97a8ea52013-02-02 10:57:51 +01002290 ARMCPU *cpu = ARM_CPU(cs);
2291 CPUARMState *env = &cpu->env;
2292
bellardb5ff1b32005-11-26 10:38:39 +00002293 env->exception_index = -1;
2294}
2295
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002296int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +00002297 int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00002298{
2299 if (rw == 2) {
2300 env->exception_index = EXCP_PREFETCH_ABORT;
2301 env->cp15.c6_insn = address;
2302 } else {
2303 env->exception_index = EXCP_DATA_ABORT;
2304 env->cp15.c6_data = address;
2305 }
2306 return 1;
2307}
2308
pbrook9ee6e8b2007-11-11 00:04:49 +00002309/* These should probably raise undefined insn exceptions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002310void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002311{
2312 cpu_abort(env, "v7m_mrs %d\n", reg);
2313}
2314
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002315uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002316{
2317 cpu_abort(env, "v7m_mrs %d\n", reg);
2318 return 0;
2319}
2320
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002321void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00002322{
2323 if (mode != ARM_CPU_MODE_USR)
2324 cpu_abort(env, "Tried to switch out of user mode\n");
2325}
2326
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002327void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002328{
2329 cpu_abort(env, "banked r13 write\n");
2330}
2331
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002332uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002333{
2334 cpu_abort(env, "banked r13 read\n");
2335 return 0;
2336}
2337
bellardb5ff1b32005-11-26 10:38:39 +00002338#else
2339
2340/* Map CPU modes onto saved register banks. */
Christoffer Dall494b00c2013-03-05 00:34:41 +00002341int bank_number(int mode)
bellardb5ff1b32005-11-26 10:38:39 +00002342{
2343 switch (mode) {
2344 case ARM_CPU_MODE_USR:
2345 case ARM_CPU_MODE_SYS:
2346 return 0;
2347 case ARM_CPU_MODE_SVC:
2348 return 1;
2349 case ARM_CPU_MODE_ABT:
2350 return 2;
2351 case ARM_CPU_MODE_UND:
2352 return 3;
2353 case ARM_CPU_MODE_IRQ:
2354 return 4;
2355 case ARM_CPU_MODE_FIQ:
2356 return 5;
2357 }
Peter Maydellf5206412013-03-05 00:34:40 +00002358 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
bellardb5ff1b32005-11-26 10:38:39 +00002359}
2360
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002361void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00002362{
2363 int old_mode;
2364 int i;
2365
2366 old_mode = env->uncached_cpsr & CPSR_M;
2367 if (mode == old_mode)
2368 return;
2369
2370 if (old_mode == ARM_CPU_MODE_FIQ) {
2371 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00002372 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00002373 } else if (mode == ARM_CPU_MODE_FIQ) {
2374 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00002375 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00002376 }
2377
Peter Maydellf5206412013-03-05 00:34:40 +00002378 i = bank_number(old_mode);
bellardb5ff1b32005-11-26 10:38:39 +00002379 env->banked_r13[i] = env->regs[13];
2380 env->banked_r14[i] = env->regs[14];
2381 env->banked_spsr[i] = env->spsr;
2382
Peter Maydellf5206412013-03-05 00:34:40 +00002383 i = bank_number(mode);
bellardb5ff1b32005-11-26 10:38:39 +00002384 env->regs[13] = env->banked_r13[i];
2385 env->regs[14] = env->banked_r14[i];
2386 env->spsr = env->banked_spsr[i];
2387}
2388
pbrook9ee6e8b2007-11-11 00:04:49 +00002389static void v7m_push(CPUARMState *env, uint32_t val)
2390{
2391 env->regs[13] -= 4;
2392 stl_phys(env->regs[13], val);
2393}
2394
2395static uint32_t v7m_pop(CPUARMState *env)
2396{
2397 uint32_t val;
2398 val = ldl_phys(env->regs[13]);
2399 env->regs[13] += 4;
2400 return val;
2401}
2402
2403/* Switch to V7M main or process stack pointer. */
2404static void switch_v7m_sp(CPUARMState *env, int process)
2405{
2406 uint32_t tmp;
2407 if (env->v7m.current_sp != process) {
2408 tmp = env->v7m.other_sp;
2409 env->v7m.other_sp = env->regs[13];
2410 env->regs[13] = tmp;
2411 env->v7m.current_sp = process;
2412 }
2413}
2414
2415static void do_v7m_exception_exit(CPUARMState *env)
2416{
2417 uint32_t type;
2418 uint32_t xpsr;
2419
2420 type = env->regs[15];
2421 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +01002422 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +00002423
2424 /* Switch to the target stack. */
2425 switch_v7m_sp(env, (type & 4) != 0);
2426 /* Pop registers. */
2427 env->regs[0] = v7m_pop(env);
2428 env->regs[1] = v7m_pop(env);
2429 env->regs[2] = v7m_pop(env);
2430 env->regs[3] = v7m_pop(env);
2431 env->regs[12] = v7m_pop(env);
2432 env->regs[14] = v7m_pop(env);
2433 env->regs[15] = v7m_pop(env);
2434 xpsr = v7m_pop(env);
2435 xpsr_write(env, xpsr, 0xfffffdff);
2436 /* Undo stack alignment. */
2437 if (xpsr & 0x200)
2438 env->regs[13] |= 4;
2439 /* ??? The exception return type specifies Thread/Handler mode. However
2440 this is also implied by the xPSR value. Not sure what to do
2441 if there is a mismatch. */
2442 /* ??? Likewise for mismatches between the CONTROL register and the stack
2443 pointer. */
2444}
2445
Peter Maydell3f1beac2013-08-20 14:54:28 +01002446/* Exception names for debug logging; note that not all of these
2447 * precisely correspond to architectural exceptions.
2448 */
2449static const char * const excnames[] = {
2450 [EXCP_UDEF] = "Undefined Instruction",
2451 [EXCP_SWI] = "SVC",
2452 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
2453 [EXCP_DATA_ABORT] = "Data Abort",
2454 [EXCP_IRQ] = "IRQ",
2455 [EXCP_FIQ] = "FIQ",
2456 [EXCP_BKPT] = "Breakpoint",
2457 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
2458 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
2459 [EXCP_STREX] = "QEMU intercept of STREX",
2460};
2461
2462static inline void arm_log_exception(int idx)
2463{
2464 if (qemu_loglevel_mask(CPU_LOG_INT)) {
2465 const char *exc = NULL;
2466
2467 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
2468 exc = excnames[idx];
2469 }
2470 if (!exc) {
2471 exc = "unknown";
2472 }
2473 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
2474 }
2475}
2476
Andreas Färbere6f010c2013-02-02 12:33:14 +01002477void arm_v7m_cpu_do_interrupt(CPUState *cs)
pbrook9ee6e8b2007-11-11 00:04:49 +00002478{
Andreas Färbere6f010c2013-02-02 12:33:14 +01002479 ARMCPU *cpu = ARM_CPU(cs);
2480 CPUARMState *env = &cpu->env;
pbrook9ee6e8b2007-11-11 00:04:49 +00002481 uint32_t xpsr = xpsr_read(env);
2482 uint32_t lr;
2483 uint32_t addr;
2484
Peter Maydell3f1beac2013-08-20 14:54:28 +01002485 arm_log_exception(env->exception_index);
2486
pbrook9ee6e8b2007-11-11 00:04:49 +00002487 lr = 0xfffffff1;
2488 if (env->v7m.current_sp)
2489 lr |= 4;
2490 if (env->v7m.exception == 0)
2491 lr |= 8;
2492
2493 /* For exceptions we just mark as pending on the NVIC, and let that
2494 handle it. */
2495 /* TODO: Need to escalate if the current priority is higher than the
2496 one we're raising. */
2497 switch (env->exception_index) {
2498 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +01002499 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +00002500 return;
2501 case EXCP_SWI:
Alex_Rozenman@mentor.com314e2292013-01-11 15:21:22 +00002502 /* The PC already points to the next instruction. */
Paul Brook983fe822010-04-05 19:34:51 +01002503 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +00002504 return;
2505 case EXCP_PREFETCH_ABORT:
2506 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +01002507 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +00002508 return;
2509 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +00002510 if (semihosting_enabled) {
2511 int nr;
Blue Swirld31dd732012-09-04 20:25:59 +00002512 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
pbrook2ad207d2007-11-24 23:22:11 +00002513 if (nr == 0xab) {
2514 env->regs[15] += 2;
2515 env->regs[0] = do_arm_semihosting(env);
Peter Maydell3f1beac2013-08-20 14:54:28 +01002516 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
pbrook2ad207d2007-11-24 23:22:11 +00002517 return;
2518 }
2519 }
Paul Brook983fe822010-04-05 19:34:51 +01002520 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +00002521 return;
2522 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +01002523 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +00002524 break;
2525 case EXCP_EXCEPTION_EXIT:
2526 do_v7m_exception_exit(env);
2527 return;
2528 default:
2529 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2530 return; /* Never happens. Keep compiler happy. */
2531 }
2532
2533 /* Align stack pointer. */
2534 /* ??? Should only do this if Configuration Control Register
2535 STACKALIGN bit is set. */
2536 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +00002537 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +00002538 xpsr |= 0x200;
2539 }
balrog6c956762008-04-13 00:57:49 +00002540 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002541 v7m_push(env, xpsr);
2542 v7m_push(env, env->regs[15]);
2543 v7m_push(env, env->regs[14]);
2544 v7m_push(env, env->regs[12]);
2545 v7m_push(env, env->regs[3]);
2546 v7m_push(env, env->regs[2]);
2547 v7m_push(env, env->regs[1]);
2548 v7m_push(env, env->regs[0]);
2549 switch_v7m_sp(env, 0);
Peter Maydellc98d1742012-03-14 12:26:10 +00002550 /* Clear IT bits */
2551 env->condexec_bits = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002552 env->regs[14] = lr;
2553 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
2554 env->regs[15] = addr & 0xfffffffe;
2555 env->thumb = addr & 1;
2556}
2557
bellardb5ff1b32005-11-26 10:38:39 +00002558/* Handle a CPU exception. */
Andreas Färber97a8ea52013-02-02 10:57:51 +01002559void arm_cpu_do_interrupt(CPUState *cs)
bellardb5ff1b32005-11-26 10:38:39 +00002560{
Andreas Färber97a8ea52013-02-02 10:57:51 +01002561 ARMCPU *cpu = ARM_CPU(cs);
2562 CPUARMState *env = &cpu->env;
bellardb5ff1b32005-11-26 10:38:39 +00002563 uint32_t addr;
2564 uint32_t mask;
2565 int new_mode;
2566 uint32_t offset;
2567
Andreas Färbere6f010c2013-02-02 12:33:14 +01002568 assert(!IS_M(env));
2569
Peter Maydell3f1beac2013-08-20 14:54:28 +01002570 arm_log_exception(env->exception_index);
2571
bellardb5ff1b32005-11-26 10:38:39 +00002572 /* TODO: Vectored interrupt controller. */
2573 switch (env->exception_index) {
2574 case EXCP_UDEF:
2575 new_mode = ARM_CPU_MODE_UND;
2576 addr = 0x04;
2577 mask = CPSR_I;
2578 if (env->thumb)
2579 offset = 2;
2580 else
2581 offset = 4;
2582 break;
2583 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +00002584 if (semihosting_enabled) {
2585 /* Check for semihosting interrupt. */
2586 if (env->thumb) {
Blue Swirld31dd732012-09-04 20:25:59 +00002587 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
2588 & 0xff;
pbrook8e716212007-01-20 17:12:09 +00002589 } else {
Blue Swirld31dd732012-09-04 20:25:59 +00002590 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
Paul Brookd8fd2952012-03-30 18:02:50 +01002591 & 0xffffff;
pbrook8e716212007-01-20 17:12:09 +00002592 }
2593 /* Only intercept calls from privileged modes, to provide some
2594 semblance of security. */
2595 if (((mask == 0x123456 && !env->thumb)
2596 || (mask == 0xab && env->thumb))
2597 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2598 env->regs[0] = do_arm_semihosting(env);
Peter Maydell3f1beac2013-08-20 14:54:28 +01002599 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
pbrook8e716212007-01-20 17:12:09 +00002600 return;
2601 }
2602 }
bellardb5ff1b32005-11-26 10:38:39 +00002603 new_mode = ARM_CPU_MODE_SVC;
2604 addr = 0x08;
2605 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +00002606 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +00002607 offset = 0;
2608 break;
pbrook06c949e2006-02-04 19:35:26 +00002609 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +00002610 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +00002611 if (env->thumb && semihosting_enabled) {
Blue Swirld31dd732012-09-04 20:25:59 +00002612 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
pbrook9ee6e8b2007-11-11 00:04:49 +00002613 if (mask == 0xab
2614 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2615 env->regs[15] += 2;
2616 env->regs[0] = do_arm_semihosting(env);
Peter Maydell3f1beac2013-08-20 14:54:28 +01002617 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
pbrook9ee6e8b2007-11-11 00:04:49 +00002618 return;
2619 }
2620 }
Alex Zuepke81c05da2011-06-03 18:42:17 +02002621 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +00002622 /* Fall through to prefetch abort. */
2623 case EXCP_PREFETCH_ABORT:
Peter Maydell3f1beac2013-08-20 14:54:28 +01002624 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
2625 env->cp15.c5_insn, env->cp15.c6_insn);
bellardb5ff1b32005-11-26 10:38:39 +00002626 new_mode = ARM_CPU_MODE_ABT;
2627 addr = 0x0c;
2628 mask = CPSR_A | CPSR_I;
2629 offset = 4;
2630 break;
2631 case EXCP_DATA_ABORT:
Peter Maydell3f1beac2013-08-20 14:54:28 +01002632 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
2633 env->cp15.c5_data, env->cp15.c6_data);
bellardb5ff1b32005-11-26 10:38:39 +00002634 new_mode = ARM_CPU_MODE_ABT;
2635 addr = 0x10;
2636 mask = CPSR_A | CPSR_I;
2637 offset = 8;
2638 break;
2639 case EXCP_IRQ:
2640 new_mode = ARM_CPU_MODE_IRQ;
2641 addr = 0x18;
2642 /* Disable IRQ and imprecise data aborts. */
2643 mask = CPSR_A | CPSR_I;
2644 offset = 4;
2645 break;
2646 case EXCP_FIQ:
2647 new_mode = ARM_CPU_MODE_FIQ;
2648 addr = 0x1c;
2649 /* Disable FIQ, IRQ and imprecise data aborts. */
2650 mask = CPSR_A | CPSR_I | CPSR_F;
2651 offset = 4;
2652 break;
2653 default:
2654 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2655 return; /* Never happens. Keep compiler happy. */
2656 }
2657 /* High vectors. */
2658 if (env->cp15.c1_sys & (1 << 13)) {
Nathan Rossi86411362013-10-25 15:44:38 +01002659 /* when enabled, base address cannot be remapped. */
bellardb5ff1b32005-11-26 10:38:39 +00002660 addr += 0xffff0000;
Nathan Rossi86411362013-10-25 15:44:38 +01002661 } else {
2662 /* ARM v7 architectures provide a vector base address register to remap
2663 * the interrupt vector table.
2664 * This register is only followed in non-monitor mode, and has a secure
2665 * and un-secure copy. Since the cpu is always in a un-secure operation
2666 * and is never in monitor mode this feature is always active.
2667 * Note: only bits 31:5 are valid.
2668 */
2669 addr += env->cp15.c12_vbar;
bellardb5ff1b32005-11-26 10:38:39 +00002670 }
2671 switch_mode (env, new_mode);
2672 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +00002673 /* Clear IT bits. */
2674 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +05302675 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +00002676 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +00002677 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +04002678 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
2679 * and we should just guard the thumb mode on V4 */
2680 if (arm_feature(env, ARM_FEATURE_V4T)) {
2681 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
2682 }
bellardb5ff1b32005-11-26 10:38:39 +00002683 env->regs[14] = env->regs[15] + offset;
2684 env->regs[15] = addr;
Andreas Färber259186a2013-01-17 18:51:17 +01002685 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
bellardb5ff1b32005-11-26 10:38:39 +00002686}
2687
2688/* Check section/page access permissions.
2689 Returns the page protection flags, or zero if the access is not
2690 permitted. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002691static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002692 int access_type, int is_user)
bellardb5ff1b32005-11-26 10:38:39 +00002693{
pbrook9ee6e8b2007-11-11 00:04:49 +00002694 int prot_ro;
2695
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002696 if (domain_prot == 3) {
bellardb5ff1b32005-11-26 10:38:39 +00002697 return PAGE_READ | PAGE_WRITE;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002698 }
bellardb5ff1b32005-11-26 10:38:39 +00002699
pbrook9ee6e8b2007-11-11 00:04:49 +00002700 if (access_type == 1)
2701 prot_ro = 0;
2702 else
2703 prot_ro = PAGE_READ;
2704
bellardb5ff1b32005-11-26 10:38:39 +00002705 switch (ap) {
2706 case 0:
pbrook78600322006-09-09 14:36:26 +00002707 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +00002708 return 0;
2709 switch ((env->cp15.c1_sys >> 8) & 3) {
2710 case 1:
2711 return is_user ? 0 : PAGE_READ;
2712 case 2:
2713 return PAGE_READ;
2714 default:
2715 return 0;
2716 }
2717 case 1:
2718 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
2719 case 2:
2720 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +00002721 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00002722 else
2723 return PAGE_READ | PAGE_WRITE;
2724 case 3:
2725 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +00002726 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002727 return 0;
2728 case 5:
2729 return is_user ? 0 : prot_ro;
2730 case 6:
2731 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +00002732 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +00002733 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +00002734 return 0;
2735 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00002736 default:
2737 abort();
2738 }
2739}
2740
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002741static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
pbrookb2fa1792008-10-22 19:22:30 +00002742{
2743 uint32_t table;
2744
2745 if (address & env->cp15.c2_mask)
2746 table = env->cp15.c2_base1 & 0xffffc000;
2747 else
2748 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
2749
2750 table |= (address >> 18) & 0x3ffc;
2751 return table;
2752}
2753
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002754static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
Avi Kivitya8170e52012-10-23 12:30:10 +02002755 int is_user, hwaddr *phys_ptr,
Peter Maydell77a71dd2012-07-12 10:59:09 +00002756 int *prot, target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +00002757{
2758 int code;
2759 uint32_t table;
2760 uint32_t desc;
2761 int type;
2762 int ap;
2763 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002764 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +02002765 hwaddr phys_addr;
bellardb5ff1b32005-11-26 10:38:39 +00002766
pbrook9ee6e8b2007-11-11 00:04:49 +00002767 /* Pagetable walk. */
2768 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00002769 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00002770 desc = ldl_phys(table);
2771 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002772 domain = (desc >> 5) & 0x0f;
2773 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +00002774 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00002775 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002776 code = 5;
2777 goto do_fault;
2778 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002779 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002780 if (type == 2)
2781 code = 9; /* Section domain fault. */
2782 else
2783 code = 11; /* Page domain fault. */
2784 goto do_fault;
2785 }
2786 if (type == 2) {
2787 /* 1Mb section. */
2788 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2789 ap = (desc >> 10) & 3;
2790 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00002791 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00002792 } else {
2793 /* Lookup l2 entry. */
2794 if (type == 1) {
2795 /* Coarse pagetable. */
2796 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2797 } else {
2798 /* Fine pagetable. */
2799 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
2800 }
2801 desc = ldl_phys(table);
2802 switch (desc & 3) {
2803 case 0: /* Page translation fault. */
2804 code = 7;
2805 goto do_fault;
2806 case 1: /* 64k page. */
2807 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2808 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00002809 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002810 break;
2811 case 2: /* 4k page. */
2812 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2813 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00002814 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002815 break;
2816 case 3: /* 1k page. */
2817 if (type == 1) {
2818 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2819 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2820 } else {
2821 /* Page translation fault. */
2822 code = 7;
2823 goto do_fault;
2824 }
2825 } else {
2826 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
2827 }
2828 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00002829 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +00002830 break;
2831 default:
2832 /* Never happens, but compiler isn't smart enough to tell. */
2833 abort();
2834 }
2835 code = 15;
2836 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002837 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
pbrook9ee6e8b2007-11-11 00:04:49 +00002838 if (!*prot) {
2839 /* Access permission fault. */
2840 goto do_fault;
2841 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05302842 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00002843 *phys_ptr = phys_addr;
2844 return 0;
2845do_fault:
2846 return code | (domain << 4);
2847}
2848
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002849static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
Avi Kivitya8170e52012-10-23 12:30:10 +02002850 int is_user, hwaddr *phys_ptr,
Peter Maydell77a71dd2012-07-12 10:59:09 +00002851 int *prot, target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00002852{
2853 int code;
2854 uint32_t table;
2855 uint32_t desc;
2856 uint32_t xn;
Peter Maydellde9b05b2012-07-12 10:59:05 +00002857 uint32_t pxn = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002858 int type;
2859 int ap;
Peter Maydellde9b05b2012-07-12 10:59:05 +00002860 int domain = 0;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002861 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +02002862 hwaddr phys_addr;
pbrook9ee6e8b2007-11-11 00:04:49 +00002863
2864 /* Pagetable walk. */
2865 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00002866 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00002867 desc = ldl_phys(table);
2868 type = (desc & 3);
Peter Maydellde9b05b2012-07-12 10:59:05 +00002869 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
2870 /* Section translation fault, or attempt to use the encoding
2871 * which is Reserved on implementations without PXN.
2872 */
pbrook9ee6e8b2007-11-11 00:04:49 +00002873 code = 5;
pbrook9ee6e8b2007-11-11 00:04:49 +00002874 goto do_fault;
Peter Maydellde9b05b2012-07-12 10:59:05 +00002875 }
2876 if ((type == 1) || !(desc & (1 << 18))) {
2877 /* Page or Section. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002878 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +00002879 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002880 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2881 if (domain_prot == 0 || domain_prot == 2) {
Peter Maydellde9b05b2012-07-12 10:59:05 +00002882 if (type != 1) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002883 code = 9; /* Section domain fault. */
Peter Maydellde9b05b2012-07-12 10:59:05 +00002884 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00002885 code = 11; /* Page domain fault. */
Peter Maydellde9b05b2012-07-12 10:59:05 +00002886 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002887 goto do_fault;
2888 }
Peter Maydellde9b05b2012-07-12 10:59:05 +00002889 if (type != 1) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002890 if (desc & (1 << 18)) {
2891 /* Supersection. */
2892 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00002893 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002894 } else {
2895 /* Section. */
2896 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00002897 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002898 }
2899 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
2900 xn = desc & (1 << 4);
Peter Maydellde9b05b2012-07-12 10:59:05 +00002901 pxn = desc & 1;
pbrook9ee6e8b2007-11-11 00:04:49 +00002902 code = 13;
2903 } else {
Peter Maydellde9b05b2012-07-12 10:59:05 +00002904 if (arm_feature(env, ARM_FEATURE_PXN)) {
2905 pxn = (desc >> 2) & 1;
2906 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002907 /* Lookup l2 entry. */
2908 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2909 desc = ldl_phys(table);
2910 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
2911 switch (desc & 3) {
2912 case 0: /* Page translation fault. */
2913 code = 7;
2914 goto do_fault;
2915 case 1: /* 64k page. */
2916 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2917 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00002918 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002919 break;
2920 case 2: case 3: /* 4k page. */
2921 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2922 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00002923 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002924 break;
2925 default:
2926 /* Never happens, but compiler isn't smart enough to tell. */
2927 abort();
2928 }
2929 code = 15;
2930 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002931 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02002932 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2933 } else {
Peter Maydellde9b05b2012-07-12 10:59:05 +00002934 if (pxn && !is_user) {
2935 xn = 1;
2936 }
Juha Riihimäkic0034322010-12-08 13:15:16 +02002937 if (xn && access_type == 2)
2938 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00002939
Juha Riihimäkic0034322010-12-08 13:15:16 +02002940 /* The simplified model uses AP[0] as an access control bit. */
2941 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
2942 /* Access flag fault. */
2943 code = (code == 15) ? 6 : 3;
2944 goto do_fault;
2945 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002946 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
Juha Riihimäkic0034322010-12-08 13:15:16 +02002947 if (!*prot) {
2948 /* Access permission fault. */
2949 goto do_fault;
2950 }
2951 if (!xn) {
2952 *prot |= PAGE_EXEC;
2953 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05302954 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002955 *phys_ptr = phys_addr;
2956 return 0;
2957do_fault:
2958 return code | (domain << 4);
2959}
2960
Peter Maydell3dde9622012-07-12 10:59:12 +00002961/* Fault type for long-descriptor MMU fault reporting; this corresponds
2962 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
2963 */
2964typedef enum {
2965 translation_fault = 1,
2966 access_fault = 2,
2967 permission_fault = 3,
2968} MMUFaultType;
2969
2970static int get_phys_addr_lpae(CPUARMState *env, uint32_t address,
2971 int access_type, int is_user,
Avi Kivitya8170e52012-10-23 12:30:10 +02002972 hwaddr *phys_ptr, int *prot,
Peter Maydell3dde9622012-07-12 10:59:12 +00002973 target_ulong *page_size_ptr)
2974{
2975 /* Read an LPAE long-descriptor translation table. */
2976 MMUFaultType fault_type = translation_fault;
2977 uint32_t level = 1;
2978 uint32_t epd;
2979 uint32_t tsz;
2980 uint64_t ttbr;
2981 int ttbr_select;
2982 int n;
Avi Kivitya8170e52012-10-23 12:30:10 +02002983 hwaddr descaddr;
Peter Maydell3dde9622012-07-12 10:59:12 +00002984 uint32_t tableattrs;
2985 target_ulong page_size;
2986 uint32_t attrs;
2987
2988 /* Determine whether this address is in the region controlled by
2989 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
2990 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
2991 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
2992 */
2993 uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3);
2994 uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3);
2995 if (t0sz && !extract32(address, 32 - t0sz, t0sz)) {
2996 /* there is a ttbr0 region and we are in it (high bits all zero) */
2997 ttbr_select = 0;
2998 } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) {
2999 /* there is a ttbr1 region and we are in it (high bits all one) */
3000 ttbr_select = 1;
3001 } else if (!t0sz) {
3002 /* ttbr0 region is "everything not in the ttbr1 region" */
3003 ttbr_select = 0;
3004 } else if (!t1sz) {
3005 /* ttbr1 region is "everything not in the ttbr0 region" */
3006 ttbr_select = 1;
3007 } else {
3008 /* in the gap between the two regions, this is a Translation fault */
3009 fault_type = translation_fault;
3010 goto do_fault;
3011 }
3012
3013 /* Note that QEMU ignores shareability and cacheability attributes,
3014 * so we don't need to do anything with the SH, ORGN, IRGN fields
3015 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
3016 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
3017 * implement any ASID-like capability so we can ignore it (instead
3018 * we will always flush the TLB any time the ASID is changed).
3019 */
3020 if (ttbr_select == 0) {
3021 ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
3022 epd = extract32(env->cp15.c2_control, 7, 1);
3023 tsz = t0sz;
3024 } else {
3025 ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
3026 epd = extract32(env->cp15.c2_control, 23, 1);
3027 tsz = t1sz;
3028 }
3029
3030 if (epd) {
3031 /* Translation table walk disabled => Translation fault on TLB miss */
3032 goto do_fault;
3033 }
3034
3035 /* If the region is small enough we will skip straight to a 2nd level
3036 * lookup. This affects the number of bits of the address used in
3037 * combination with the TTBR to find the first descriptor. ('n' here
3038 * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
3039 * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
3040 */
3041 if (tsz > 1) {
3042 level = 2;
3043 n = 14 - tsz;
3044 } else {
3045 n = 5 - tsz;
3046 }
3047
3048 /* Clear the vaddr bits which aren't part of the within-region address,
3049 * so that we don't have to special case things when calculating the
3050 * first descriptor address.
3051 */
3052 address &= (0xffffffffU >> tsz);
3053
3054 /* Now we can extract the actual base address from the TTBR */
3055 descaddr = extract64(ttbr, 0, 40);
3056 descaddr &= ~((1ULL << n) - 1);
3057
3058 tableattrs = 0;
3059 for (;;) {
3060 uint64_t descriptor;
3061
3062 descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
3063 descriptor = ldq_phys(descaddr);
3064 if (!(descriptor & 1) ||
3065 (!(descriptor & 2) && (level == 3))) {
3066 /* Invalid, or the Reserved level 3 encoding */
3067 goto do_fault;
3068 }
3069 descaddr = descriptor & 0xfffffff000ULL;
3070
3071 if ((descriptor & 2) && (level < 3)) {
3072 /* Table entry. The top five bits are attributes which may
3073 * propagate down through lower levels of the table (and
3074 * which are all arranged so that 0 means "no effect", so
3075 * we can gather them up by ORing in the bits at each level).
3076 */
3077 tableattrs |= extract64(descriptor, 59, 5);
3078 level++;
3079 continue;
3080 }
3081 /* Block entry at level 1 or 2, or page entry at level 3.
3082 * These are basically the same thing, although the number
3083 * of bits we pull in from the vaddr varies.
3084 */
3085 page_size = (1 << (39 - (9 * level)));
3086 descaddr |= (address & (page_size - 1));
3087 /* Extract attributes from the descriptor and merge with table attrs */
3088 attrs = extract64(descriptor, 2, 10)
3089 | (extract64(descriptor, 52, 12) << 10);
3090 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3091 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
3092 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
3093 * means "force PL1 access only", which means forcing AP[1] to 0.
3094 */
3095 if (extract32(tableattrs, 2, 1)) {
3096 attrs &= ~(1 << 4);
3097 }
3098 /* Since we're always in the Non-secure state, NSTable is ignored. */
3099 break;
3100 }
3101 /* Here descaddr is the final physical address, and attributes
3102 * are all in attrs.
3103 */
3104 fault_type = access_fault;
3105 if ((attrs & (1 << 8)) == 0) {
3106 /* Access flag */
3107 goto do_fault;
3108 }
3109 fault_type = permission_fault;
3110 if (is_user && !(attrs & (1 << 4))) {
3111 /* Unprivileged access not enabled */
3112 goto do_fault;
3113 }
3114 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3115 if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) {
3116 /* XN or PXN */
3117 if (access_type == 2) {
3118 goto do_fault;
3119 }
3120 *prot &= ~PAGE_EXEC;
3121 }
3122 if (attrs & (1 << 5)) {
3123 /* Write access forbidden */
3124 if (access_type == 1) {
3125 goto do_fault;
3126 }
3127 *prot &= ~PAGE_WRITE;
3128 }
3129
3130 *phys_ptr = descaddr;
3131 *page_size_ptr = page_size;
3132 return 0;
3133
3134do_fault:
3135 /* Long-descriptor format IFSR/DFSR value */
3136 return (1 << 9) | (fault_type << 2) | level;
3137}
3138
Peter Maydell77a71dd2012-07-12 10:59:09 +00003139static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
3140 int access_type, int is_user,
Avi Kivitya8170e52012-10-23 12:30:10 +02003141 hwaddr *phys_ptr, int *prot)
pbrook9ee6e8b2007-11-11 00:04:49 +00003142{
3143 int n;
3144 uint32_t mask;
3145 uint32_t base;
3146
3147 *phys_ptr = address;
3148 for (n = 7; n >= 0; n--) {
3149 base = env->cp15.c6_region[n];
3150 if ((base & 1) == 0)
3151 continue;
3152 mask = 1 << ((base >> 1) & 0x1f);
3153 /* Keep this shift separate from the above to avoid an
3154 (undefined) << 32. */
3155 mask = (mask << 1) - 1;
3156 if (((base ^ address) & ~mask) == 0)
3157 break;
3158 }
3159 if (n < 0)
3160 return 2;
3161
3162 if (access_type == 2) {
3163 mask = env->cp15.c5_insn;
3164 } else {
3165 mask = env->cp15.c5_data;
3166 }
3167 mask = (mask >> (n * 4)) & 0xf;
3168 switch (mask) {
3169 case 0:
3170 return 1;
3171 case 1:
3172 if (is_user)
3173 return 1;
3174 *prot = PAGE_READ | PAGE_WRITE;
3175 break;
3176 case 2:
3177 *prot = PAGE_READ;
3178 if (!is_user)
3179 *prot |= PAGE_WRITE;
3180 break;
3181 case 3:
3182 *prot = PAGE_READ | PAGE_WRITE;
3183 break;
3184 case 5:
3185 if (is_user)
3186 return 1;
3187 *prot = PAGE_READ;
3188 break;
3189 case 6:
3190 *prot = PAGE_READ;
3191 break;
3192 default:
3193 /* Bad permission. */
3194 return 1;
3195 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05303196 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00003197 return 0;
3198}
3199
Peter Maydell702a9352012-07-12 10:59:10 +00003200/* get_phys_addr - get the physical address for this virtual address
3201 *
3202 * Find the physical address corresponding to the given virtual address,
3203 * by doing a translation table walk on MMU based systems or using the
3204 * MPU state on MPU based systems.
3205 *
3206 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
3207 * prot and page_size are not filled in, and the return value provides
3208 * information on why the translation aborted, in the format of a
3209 * DFSR/IFSR fault register, with the following caveats:
3210 * * we honour the short vs long DFSR format differences.
3211 * * the WnR bit is never set (the caller must do this).
3212 * * for MPU based systems we don't bother to return a full FSR format
3213 * value.
3214 *
3215 * @env: CPUARMState
3216 * @address: virtual address to get physical address for
3217 * @access_type: 0 for read, 1 for write, 2 for execute
3218 * @is_user: 0 for privileged access, 1 for user
3219 * @phys_ptr: set to the physical address corresponding to the virtual address
3220 * @prot: set to the permissions for the page containing phys_ptr
3221 * @page_size: set to the size of the page containing phys_ptr
3222 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003223static inline int get_phys_addr(CPUARMState *env, uint32_t address,
pbrook9ee6e8b2007-11-11 00:04:49 +00003224 int access_type, int is_user,
Avi Kivitya8170e52012-10-23 12:30:10 +02003225 hwaddr *phys_ptr, int *prot,
Paul Brookd4c430a2010-03-17 02:14:28 +00003226 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00003227{
bellardb5ff1b32005-11-26 10:38:39 +00003228 /* Fast Context Switch Extension. */
3229 if (address < 0x02000000)
3230 address += env->cp15.c13_fcse;
3231
3232 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00003233 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00003234 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05303235 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00003236 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00003237 return 0;
pbrookce819862007-05-08 02:30:40 +00003238 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00003239 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00003240 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
3241 prot);
Peter Maydell3dde9622012-07-12 10:59:12 +00003242 } else if (extended_addresses_enabled(env)) {
3243 return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
3244 prot, page_size);
pbrook9ee6e8b2007-11-11 00:04:49 +00003245 } else if (env->cp15.c1_sys & (1 << 23)) {
3246 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00003247 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003248 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00003249 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00003250 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003251 }
bellardb5ff1b32005-11-26 10:38:39 +00003252}
3253
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003254int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
Blue Swirl97b348e2011-08-01 16:12:17 +00003255 int access_type, int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00003256{
Avi Kivitya8170e52012-10-23 12:30:10 +02003257 hwaddr phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00003258 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00003259 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00003260 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00003261
j_mayer6ebbf392007-10-14 07:07:08 +00003262 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00003263 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
3264 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003265 if (ret == 0) {
3266 /* Map a single [sub]page. */
Avi Kivitya8170e52012-10-23 12:30:10 +02003267 phys_addr &= ~(hwaddr)0x3ff;
bellardb5ff1b32005-11-26 10:38:39 +00003268 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05303269 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00003270 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00003271 }
3272
3273 if (access_type == 2) {
3274 env->cp15.c5_insn = ret;
3275 env->cp15.c6_insn = address;
3276 env->exception_index = EXCP_PREFETCH_ABORT;
3277 } else {
3278 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00003279 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
3280 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00003281 env->cp15.c6_data = address;
3282 env->exception_index = EXCP_DATA_ABORT;
3283 }
3284 return 1;
3285}
3286
Andreas Färber00b941e2013-06-29 18:55:54 +02003287hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
bellardb5ff1b32005-11-26 10:38:39 +00003288{
Andreas Färber00b941e2013-06-29 18:55:54 +02003289 ARMCPU *cpu = ARM_CPU(cs);
Avi Kivitya8170e52012-10-23 12:30:10 +02003290 hwaddr phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00003291 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00003292 int prot;
3293 int ret;
3294
Andreas Färber00b941e2013-06-29 18:55:54 +02003295 ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003296
Andreas Färber00b941e2013-06-29 18:55:54 +02003297 if (ret != 0) {
bellardb5ff1b32005-11-26 10:38:39 +00003298 return -1;
Andreas Färber00b941e2013-06-29 18:55:54 +02003299 }
bellardb5ff1b32005-11-26 10:38:39 +00003300
3301 return phys_addr;
3302}
3303
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003304void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00003305{
Peter Maydell39ea3d42011-01-14 20:39:18 +01003306 if ((env->uncached_cpsr & CPSR_M) == mode) {
3307 env->regs[13] = val;
3308 } else {
Peter Maydellf5206412013-03-05 00:34:40 +00003309 env->banked_r13[bank_number(mode)] = val;
Peter Maydell39ea3d42011-01-14 20:39:18 +01003310 }
pbrook9ee6e8b2007-11-11 00:04:49 +00003311}
3312
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003313uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00003314{
Peter Maydell39ea3d42011-01-14 20:39:18 +01003315 if ((env->uncached_cpsr & CPSR_M) == mode) {
3316 return env->regs[13];
3317 } else {
Peter Maydellf5206412013-03-05 00:34:40 +00003318 return env->banked_r13[bank_number(mode)];
Peter Maydell39ea3d42011-01-14 20:39:18 +01003319 }
pbrook9ee6e8b2007-11-11 00:04:49 +00003320}
3321
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003322uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00003323{
3324 switch (reg) {
3325 case 0: /* APSR */
3326 return xpsr_read(env) & 0xf8000000;
3327 case 1: /* IAPSR */
3328 return xpsr_read(env) & 0xf80001ff;
3329 case 2: /* EAPSR */
3330 return xpsr_read(env) & 0xff00fc00;
3331 case 3: /* xPSR */
3332 return xpsr_read(env) & 0xff00fdff;
3333 case 5: /* IPSR */
3334 return xpsr_read(env) & 0x000001ff;
3335 case 6: /* EPSR */
3336 return xpsr_read(env) & 0x0700fc00;
3337 case 7: /* IEPSR */
3338 return xpsr_read(env) & 0x0700edff;
3339 case 8: /* MSP */
3340 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
3341 case 9: /* PSP */
3342 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
3343 case 16: /* PRIMASK */
3344 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00003345 case 17: /* BASEPRI */
3346 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00003347 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00003348 case 19: /* FAULTMASK */
3349 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00003350 case 20: /* CONTROL */
3351 return env->v7m.control;
3352 default:
3353 /* ??? For debugging only. */
3354 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
3355 return 0;
3356 }
3357}
3358
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003359void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00003360{
3361 switch (reg) {
3362 case 0: /* APSR */
3363 xpsr_write(env, val, 0xf8000000);
3364 break;
3365 case 1: /* IAPSR */
3366 xpsr_write(env, val, 0xf8000000);
3367 break;
3368 case 2: /* EAPSR */
3369 xpsr_write(env, val, 0xfe00fc00);
3370 break;
3371 case 3: /* xPSR */
3372 xpsr_write(env, val, 0xfe00fc00);
3373 break;
3374 case 5: /* IPSR */
3375 /* IPSR bits are readonly. */
3376 break;
3377 case 6: /* EPSR */
3378 xpsr_write(env, val, 0x0600fc00);
3379 break;
3380 case 7: /* IEPSR */
3381 xpsr_write(env, val, 0x0600fc00);
3382 break;
3383 case 8: /* MSP */
3384 if (env->v7m.current_sp)
3385 env->v7m.other_sp = val;
3386 else
3387 env->regs[13] = val;
3388 break;
3389 case 9: /* PSP */
3390 if (env->v7m.current_sp)
3391 env->regs[13] = val;
3392 else
3393 env->v7m.other_sp = val;
3394 break;
3395 case 16: /* PRIMASK */
3396 if (val & 1)
3397 env->uncached_cpsr |= CPSR_I;
3398 else
3399 env->uncached_cpsr &= ~CPSR_I;
3400 break;
Sebastian Huber82845822011-05-29 02:58:41 +00003401 case 17: /* BASEPRI */
3402 env->v7m.basepri = val & 0xff;
3403 break;
3404 case 18: /* BASEPRI_MAX */
3405 val &= 0xff;
3406 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
3407 env->v7m.basepri = val;
3408 break;
3409 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00003410 if (val & 1)
3411 env->uncached_cpsr |= CPSR_F;
3412 else
3413 env->uncached_cpsr &= ~CPSR_F;
3414 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00003415 case 20: /* CONTROL */
3416 env->v7m.control = val & 3;
3417 switch_v7m_sp(env, (val & 2) != 0);
3418 break;
3419 default:
3420 /* ??? For debugging only. */
3421 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
3422 return;
3423 }
3424}
3425
bellardb5ff1b32005-11-26 10:38:39 +00003426#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00003427
3428/* Note that signed overflow is undefined in C. The following routines are
3429 careful to use unsigned types where modulo arithmetic is required.
3430 Failure to do so _will_ break on newer gcc. */
3431
3432/* Signed saturating arithmetic. */
3433
aurel321654b2d2008-04-11 04:55:07 +00003434/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003435static inline uint16_t add16_sat(uint16_t a, uint16_t b)
3436{
3437 uint16_t res;
3438
3439 res = a + b;
3440 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
3441 if (a & 0x8000)
3442 res = 0x8000;
3443 else
3444 res = 0x7fff;
3445 }
3446 return res;
3447}
3448
aurel321654b2d2008-04-11 04:55:07 +00003449/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003450static inline uint8_t add8_sat(uint8_t a, uint8_t b)
3451{
3452 uint8_t res;
3453
3454 res = a + b;
3455 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
3456 if (a & 0x80)
3457 res = 0x80;
3458 else
3459 res = 0x7f;
3460 }
3461 return res;
3462}
3463
aurel321654b2d2008-04-11 04:55:07 +00003464/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003465static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
3466{
3467 uint16_t res;
3468
3469 res = a - b;
3470 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
3471 if (a & 0x8000)
3472 res = 0x8000;
3473 else
3474 res = 0x7fff;
3475 }
3476 return res;
3477}
3478
aurel321654b2d2008-04-11 04:55:07 +00003479/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003480static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
3481{
3482 uint8_t res;
3483
3484 res = a - b;
3485 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
3486 if (a & 0x80)
3487 res = 0x80;
3488 else
3489 res = 0x7f;
3490 }
3491 return res;
3492}
3493
3494#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3495#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3496#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
3497#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
3498#define PFX q
3499
3500#include "op_addsub.h"
3501
3502/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00003503static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003504{
3505 uint16_t res;
3506 res = a + b;
3507 if (res < a)
3508 res = 0xffff;
3509 return res;
3510}
3511
pbrook460a09c2008-05-01 12:04:35 +00003512static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003513{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08003514 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003515 return a - b;
3516 else
3517 return 0;
3518}
3519
3520static inline uint8_t add8_usat(uint8_t a, uint8_t b)
3521{
3522 uint8_t res;
3523 res = a + b;
3524 if (res < a)
3525 res = 0xff;
3526 return res;
3527}
3528
3529static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
3530{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08003531 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003532 return a - b;
3533 else
3534 return 0;
3535}
3536
3537#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3538#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3539#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
3540#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
3541#define PFX uq
3542
3543#include "op_addsub.h"
3544
3545/* Signed modulo arithmetic. */
3546#define SARITH16(a, b, n, op) do { \
3547 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00003548 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00003549 RESULT(sum, n, 16); \
3550 if (sum >= 0) \
3551 ge |= 3 << (n * 2); \
3552 } while(0)
3553
3554#define SARITH8(a, b, n, op) do { \
3555 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00003556 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00003557 RESULT(sum, n, 8); \
3558 if (sum >= 0) \
3559 ge |= 1 << n; \
3560 } while(0)
3561
3562
3563#define ADD16(a, b, n) SARITH16(a, b, n, +)
3564#define SUB16(a, b, n) SARITH16(a, b, n, -)
3565#define ADD8(a, b, n) SARITH8(a, b, n, +)
3566#define SUB8(a, b, n) SARITH8(a, b, n, -)
3567#define PFX s
3568#define ARITH_GE
3569
3570#include "op_addsub.h"
3571
3572/* Unsigned modulo arithmetic. */
3573#define ADD16(a, b, n) do { \
3574 uint32_t sum; \
3575 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3576 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00003577 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00003578 ge |= 3 << (n * 2); \
3579 } while(0)
3580
3581#define ADD8(a, b, n) do { \
3582 uint32_t sum; \
3583 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
3584 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00003585 if ((sum >> 8) == 1) \
3586 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00003587 } while(0)
3588
3589#define SUB16(a, b, n) do { \
3590 uint32_t sum; \
3591 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
3592 RESULT(sum, n, 16); \
3593 if ((sum >> 16) == 0) \
3594 ge |= 3 << (n * 2); \
3595 } while(0)
3596
3597#define SUB8(a, b, n) do { \
3598 uint32_t sum; \
3599 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
3600 RESULT(sum, n, 8); \
3601 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00003602 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00003603 } while(0)
3604
3605#define PFX u
3606#define ARITH_GE
3607
3608#include "op_addsub.h"
3609
3610/* Halved signed arithmetic. */
3611#define ADD16(a, b, n) \
3612 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
3613#define SUB16(a, b, n) \
3614 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
3615#define ADD8(a, b, n) \
3616 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
3617#define SUB8(a, b, n) \
3618 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
3619#define PFX sh
3620
3621#include "op_addsub.h"
3622
3623/* Halved unsigned arithmetic. */
3624#define ADD16(a, b, n) \
3625 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3626#define SUB16(a, b, n) \
3627 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3628#define ADD8(a, b, n) \
3629 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3630#define SUB8(a, b, n) \
3631 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3632#define PFX uh
3633
3634#include "op_addsub.h"
3635
3636static inline uint8_t do_usad(uint8_t a, uint8_t b)
3637{
3638 if (a > b)
3639 return a - b;
3640 else
3641 return b - a;
3642}
3643
3644/* Unsigned sum of absolute byte differences. */
3645uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
3646{
3647 uint32_t sum;
3648 sum = do_usad(a, b);
3649 sum += do_usad(a >> 8, b >> 8);
3650 sum += do_usad(a >> 16, b >>16);
3651 sum += do_usad(a >> 24, b >> 24);
3652 return sum;
3653}
3654
3655/* For ARMv6 SEL instruction. */
3656uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
3657{
3658 uint32_t mask;
3659
3660 mask = 0;
3661 if (flags & 1)
3662 mask |= 0xff;
3663 if (flags & 2)
3664 mask |= 0xff00;
3665 if (flags & 4)
3666 mask |= 0xff0000;
3667 if (flags & 8)
3668 mask |= 0xff000000;
3669 return (a & mask) | (b & ~mask);
3670}
3671
Peter Maydellb90372a2012-08-06 17:42:18 +01003672/* VFP support. We follow the convention used for VFP instructions:
3673 Single precision routines have a "s" suffix, double precision a
pbrook4373f3c2008-03-31 03:47:19 +00003674 "d" suffix. */
3675
3676/* Convert host exception flags to vfp form. */
3677static inline int vfp_exceptbits_from_host(int host_bits)
3678{
3679 int target_bits = 0;
3680
3681 if (host_bits & float_flag_invalid)
3682 target_bits |= 1;
3683 if (host_bits & float_flag_divbyzero)
3684 target_bits |= 2;
3685 if (host_bits & float_flag_overflow)
3686 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01003687 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00003688 target_bits |= 8;
3689 if (host_bits & float_flag_inexact)
3690 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00003691 if (host_bits & float_flag_input_denormal)
3692 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00003693 return target_bits;
3694}
3695
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003696uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003697{
3698 int i;
3699 uint32_t fpscr;
3700
3701 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
3702 | (env->vfp.vec_len << 16)
3703 | (env->vfp.vec_stride << 20);
3704 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01003705 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00003706 fpscr |= vfp_exceptbits_from_host(i);
3707 return fpscr;
3708}
3709
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003710uint32_t vfp_get_fpscr(CPUARMState *env)
Peter Maydell01653292010-11-24 15:20:04 +00003711{
3712 return HELPER(vfp_get_fpscr)(env);
3713}
3714
pbrook4373f3c2008-03-31 03:47:19 +00003715/* Convert vfp exception flags to target form. */
3716static inline int vfp_exceptbits_to_host(int target_bits)
3717{
3718 int host_bits = 0;
3719
3720 if (target_bits & 1)
3721 host_bits |= float_flag_invalid;
3722 if (target_bits & 2)
3723 host_bits |= float_flag_divbyzero;
3724 if (target_bits & 4)
3725 host_bits |= float_flag_overflow;
3726 if (target_bits & 8)
3727 host_bits |= float_flag_underflow;
3728 if (target_bits & 0x10)
3729 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00003730 if (target_bits & 0x80)
3731 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00003732 return host_bits;
3733}
3734
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003735void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
pbrook4373f3c2008-03-31 03:47:19 +00003736{
3737 int i;
3738 uint32_t changed;
3739
3740 changed = env->vfp.xregs[ARM_VFP_FPSCR];
3741 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
3742 env->vfp.vec_len = (val >> 16) & 7;
3743 env->vfp.vec_stride = (val >> 20) & 3;
3744
3745 changed ^= val;
3746 if (changed & (3 << 22)) {
3747 i = (val >> 22) & 3;
3748 switch (i) {
3749 case 0:
3750 i = float_round_nearest_even;
3751 break;
3752 case 1:
3753 i = float_round_up;
3754 break;
3755 case 2:
3756 i = float_round_down;
3757 break;
3758 case 3:
3759 i = float_round_to_zero;
3760 break;
3761 }
3762 set_float_rounding_mode(i, &env->vfp.fp_status);
3763 }
Peter Maydellcecd8502011-01-06 19:37:55 +00003764 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00003765 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00003766 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3767 }
pbrook5c7908e2008-12-19 13:53:37 +00003768 if (changed & (1 << 25))
3769 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00003770
Peter Maydellb12c3902011-01-06 19:37:54 +00003771 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00003772 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01003773 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00003774}
3775
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003776void vfp_set_fpscr(CPUARMState *env, uint32_t val)
Peter Maydell01653292010-11-24 15:20:04 +00003777{
3778 HELPER(vfp_set_fpscr)(env, val);
3779}
3780
pbrook4373f3c2008-03-31 03:47:19 +00003781#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
3782
3783#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00003784float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003785{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00003786 float_status *fpst = fpstp; \
3787 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003788} \
Peter Maydellae1857e2011-05-25 14:51:48 +00003789float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003790{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00003791 float_status *fpst = fpstp; \
3792 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003793}
3794VFP_BINOP(add)
3795VFP_BINOP(sub)
3796VFP_BINOP(mul)
3797VFP_BINOP(div)
3798#undef VFP_BINOP
3799
3800float32 VFP_HELPER(neg, s)(float32 a)
3801{
3802 return float32_chs(a);
3803}
3804
3805float64 VFP_HELPER(neg, d)(float64 a)
3806{
balrog66230e02008-04-20 00:58:01 +00003807 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00003808}
3809
3810float32 VFP_HELPER(abs, s)(float32 a)
3811{
3812 return float32_abs(a);
3813}
3814
3815float64 VFP_HELPER(abs, d)(float64 a)
3816{
balrog66230e02008-04-20 00:58:01 +00003817 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00003818}
3819
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003820float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003821{
3822 return float32_sqrt(a, &env->vfp.fp_status);
3823}
3824
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003825float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003826{
3827 return float64_sqrt(a, &env->vfp.fp_status);
3828}
3829
3830/* XXX: check quiet/signaling case */
3831#define DO_VFP_cmp(p, type) \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003832void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00003833{ \
3834 uint32_t flags; \
3835 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
3836 case 0: flags = 0x6; break; \
3837 case -1: flags = 0x8; break; \
3838 case 1: flags = 0x2; break; \
3839 default: case 2: flags = 0x3; break; \
3840 } \
3841 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3842 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3843} \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003844void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00003845{ \
3846 uint32_t flags; \
3847 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
3848 case 0: flags = 0x6; break; \
3849 case -1: flags = 0x8; break; \
3850 case 1: flags = 0x2; break; \
3851 default: case 2: flags = 0x3; break; \
3852 } \
3853 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3854 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3855}
3856DO_VFP_cmp(s, float32)
3857DO_VFP_cmp(d, float64)
3858#undef DO_VFP_cmp
3859
Peter Maydell5500b062011-05-19 14:46:19 +01003860/* Integer to float and float to integer conversions */
3861
3862#define CONV_ITOF(name, fsz, sign) \
3863 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
3864{ \
3865 float_status *fpst = fpstp; \
Peter Maydell85836972012-01-25 11:49:46 +00003866 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003867}
3868
Peter Maydell5500b062011-05-19 14:46:19 +01003869#define CONV_FTOI(name, fsz, sign, round) \
3870uint32_t HELPER(name)(float##fsz x, void *fpstp) \
3871{ \
3872 float_status *fpst = fpstp; \
3873 if (float##fsz##_is_any_nan(x)) { \
3874 float_raise(float_flag_invalid, fpst); \
3875 return 0; \
3876 } \
3877 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003878}
3879
Peter Maydell5500b062011-05-19 14:46:19 +01003880#define FLOAT_CONVS(name, p, fsz, sign) \
3881CONV_ITOF(vfp_##name##to##p, fsz, sign) \
3882CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
3883CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00003884
Peter Maydell5500b062011-05-19 14:46:19 +01003885FLOAT_CONVS(si, s, 32, )
3886FLOAT_CONVS(si, d, 64, )
3887FLOAT_CONVS(ui, s, 32, u)
3888FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00003889
Peter Maydell5500b062011-05-19 14:46:19 +01003890#undef CONV_ITOF
3891#undef CONV_FTOI
3892#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00003893
3894/* floating point conversion */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003895float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003896{
Peter Maydell2d627732010-12-07 15:37:34 +00003897 float64 r = float32_to_float64(x, &env->vfp.fp_status);
3898 /* ARM requires that S<->D conversion of any kind of NaN generates
3899 * a quiet NaN by forcing the most significant frac bit to 1.
3900 */
3901 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00003902}
3903
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003904float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003905{
Peter Maydell2d627732010-12-07 15:37:34 +00003906 float32 r = float64_to_float32(x, &env->vfp.fp_status);
3907 /* ARM requires that S<->D conversion of any kind of NaN generates
3908 * a quiet NaN by forcing the most significant frac bit to 1.
3909 */
3910 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00003911}
3912
3913/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00003914#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01003915float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
3916 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003917{ \
Peter Maydell5500b062011-05-19 14:46:19 +01003918 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00003919 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01003920 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
3921 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003922} \
Peter Maydell5500b062011-05-19 14:46:19 +01003923uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
3924 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003925{ \
Peter Maydell5500b062011-05-19 14:46:19 +01003926 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00003927 float##fsz tmp; \
3928 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01003929 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00003930 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00003931 } \
Peter Maydell5500b062011-05-19 14:46:19 +01003932 tmp = float##fsz##_scalbn(x, shift, fpst); \
3933 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003934}
3935
Peter Maydell622465e2011-03-14 07:23:11 +00003936VFP_CONV_FIX(sh, d, 64, int16, )
3937VFP_CONV_FIX(sl, d, 64, int32, )
3938VFP_CONV_FIX(uh, d, 64, uint16, u)
3939VFP_CONV_FIX(ul, d, 64, uint32, u)
3940VFP_CONV_FIX(sh, s, 32, int16, )
3941VFP_CONV_FIX(sl, s, 32, int32, )
3942VFP_CONV_FIX(uh, s, 32, uint16, u)
3943VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00003944#undef VFP_CONV_FIX
3945
Paul Brook60011492009-11-19 16:45:20 +00003946/* Half precision conversions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003947static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00003948{
Paul Brook60011492009-11-19 16:45:20 +00003949 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00003950 float32 r = float16_to_float32(make_float16(a), ieee, s);
3951 if (ieee) {
3952 return float32_maybe_silence_nan(r);
3953 }
3954 return r;
Paul Brook60011492009-11-19 16:45:20 +00003955}
3956
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003957static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00003958{
Paul Brook60011492009-11-19 16:45:20 +00003959 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00003960 float16 r = float32_to_float16(a, ieee, s);
3961 if (ieee) {
3962 r = float16_maybe_silence_nan(r);
3963 }
3964 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00003965}
3966
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003967float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003968{
3969 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
3970}
3971
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003972uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003973{
3974 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
3975}
3976
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003977float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003978{
3979 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
3980}
3981
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003982uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003983{
3984 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
3985}
3986
Peter Maydelldda3ec42011-03-14 15:37:12 +00003987#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00003988#define float32_three make_float32(0x40400000)
3989#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00003990
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003991float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003992{
Peter Maydelldda3ec42011-03-14 15:37:12 +00003993 float_status *s = &env->vfp.standard_fp_status;
3994 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3995 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003996 if (!(float32_is_zero(a) || float32_is_zero(b))) {
3997 float_raise(float_flag_input_denormal, s);
3998 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00003999 return float32_two;
4000 }
4001 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00004002}
4003
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004004float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00004005{
Peter Maydell71826962011-01-14 20:39:18 +01004006 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01004007 float32 product;
4008 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
4009 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01004010 if (!(float32_is_zero(a) || float32_is_zero(b))) {
4011 float_raise(float_flag_input_denormal, s);
4012 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00004013 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01004014 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00004015 product = float32_mul(a, b, s);
4016 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00004017}
4018
pbrook8f8e3aa2008-03-31 03:48:01 +00004019/* NEON helpers. */
4020
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01004021/* Constants 256 and 512 are used in some helpers; we avoid relying on
4022 * int->float conversions at run-time. */
4023#define float64_256 make_float64(0x4070000000000000LL)
4024#define float64_512 make_float64(0x4080000000000000LL)
4025
Christophe Lyonfe0e4872011-02-21 17:38:47 +01004026/* The algorithm that must be used to calculate the estimate
4027 * is specified by the ARM ARM.
4028 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004029static float64 recip_estimate(float64 a, CPUARMState *env)
Christophe Lyonfe0e4872011-02-21 17:38:47 +01004030{
Peter Maydell1146a812011-05-19 14:46:14 +01004031 /* These calculations mustn't set any fp exception flags,
4032 * so we use a local copy of the fp_status.
4033 */
4034 float_status dummy_status = env->vfp.standard_fp_status;
4035 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01004036 /* q = (int)(a * 512.0) */
4037 float64 q = float64_mul(float64_512, a, s);
4038 int64_t q_int = float64_to_int64_round_to_zero(q, s);
4039
4040 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
4041 q = int64_to_float64(q_int, s);
4042 q = float64_add(q, float64_half, s);
4043 q = float64_div(q, float64_512, s);
4044 q = float64_div(float64_one, q, s);
4045
4046 /* s = (int)(256.0 * r + 0.5) */
4047 q = float64_mul(q, float64_256, s);
4048 q = float64_add(q, float64_half, s);
4049 q_int = float64_to_int64_round_to_zero(q, s);
4050
4051 /* return (double)s / 256.0 */
4052 return float64_div(int64_to_float64(q_int, s), float64_256, s);
4053}
4054
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004055float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00004056{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01004057 float_status *s = &env->vfp.standard_fp_status;
4058 float64 f64;
4059 uint32_t val32 = float32_val(a);
4060
4061 int result_exp;
4062 int a_exp = (val32 & 0x7f800000) >> 23;
4063 int sign = val32 & 0x80000000;
4064
4065 if (float32_is_any_nan(a)) {
4066 if (float32_is_signaling_nan(a)) {
4067 float_raise(float_flag_invalid, s);
4068 }
4069 return float32_default_nan;
4070 } else if (float32_is_infinity(a)) {
4071 return float32_set_sign(float32_zero, float32_is_neg(a));
4072 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01004073 if (!float32_is_zero(a)) {
4074 float_raise(float_flag_input_denormal, s);
4075 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01004076 float_raise(float_flag_divbyzero, s);
4077 return float32_set_sign(float32_infinity, float32_is_neg(a));
4078 } else if (a_exp >= 253) {
4079 float_raise(float_flag_underflow, s);
4080 return float32_set_sign(float32_zero, float32_is_neg(a));
4081 }
4082
4083 f64 = make_float64((0x3feULL << 52)
4084 | ((int64_t)(val32 & 0x7fffff) << 29));
4085
4086 result_exp = 253 - a_exp;
4087
4088 f64 = recip_estimate(f64, env);
4089
4090 val32 = sign
4091 | ((result_exp & 0xff) << 23)
4092 | ((float64_val(f64) >> 29) & 0x7fffff);
4093 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00004094}
4095
Christophe Lyone07be5d2011-02-21 17:38:48 +01004096/* The algorithm that must be used to calculate the estimate
4097 * is specified by the ARM ARM.
4098 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004099static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
Christophe Lyone07be5d2011-02-21 17:38:48 +01004100{
Peter Maydell1146a812011-05-19 14:46:14 +01004101 /* These calculations mustn't set any fp exception flags,
4102 * so we use a local copy of the fp_status.
4103 */
4104 float_status dummy_status = env->vfp.standard_fp_status;
4105 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01004106 float64 q;
4107 int64_t q_int;
4108
4109 if (float64_lt(a, float64_half, s)) {
4110 /* range 0.25 <= a < 0.5 */
4111
4112 /* a in units of 1/512 rounded down */
4113 /* q0 = (int)(a * 512.0); */
4114 q = float64_mul(float64_512, a, s);
4115 q_int = float64_to_int64_round_to_zero(q, s);
4116
4117 /* reciprocal root r */
4118 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
4119 q = int64_to_float64(q_int, s);
4120 q = float64_add(q, float64_half, s);
4121 q = float64_div(q, float64_512, s);
4122 q = float64_sqrt(q, s);
4123 q = float64_div(float64_one, q, s);
4124 } else {
4125 /* range 0.5 <= a < 1.0 */
4126
4127 /* a in units of 1/256 rounded down */
4128 /* q1 = (int)(a * 256.0); */
4129 q = float64_mul(float64_256, a, s);
4130 int64_t q_int = float64_to_int64_round_to_zero(q, s);
4131
4132 /* reciprocal root r */
4133 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
4134 q = int64_to_float64(q_int, s);
4135 q = float64_add(q, float64_half, s);
4136 q = float64_div(q, float64_256, s);
4137 q = float64_sqrt(q, s);
4138 q = float64_div(float64_one, q, s);
4139 }
4140 /* r in units of 1/256 rounded to nearest */
4141 /* s = (int)(256.0 * r + 0.5); */
4142
4143 q = float64_mul(q, float64_256,s );
4144 q = float64_add(q, float64_half, s);
4145 q_int = float64_to_int64_round_to_zero(q, s);
4146
4147 /* return (double)s / 256.0;*/
4148 return float64_div(int64_to_float64(q_int, s), float64_256, s);
4149}
4150
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004151float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00004152{
Christophe Lyone07be5d2011-02-21 17:38:48 +01004153 float_status *s = &env->vfp.standard_fp_status;
4154 int result_exp;
4155 float64 f64;
4156 uint32_t val;
4157 uint64_t val64;
4158
4159 val = float32_val(a);
4160
4161 if (float32_is_any_nan(a)) {
4162 if (float32_is_signaling_nan(a)) {
4163 float_raise(float_flag_invalid, s);
4164 }
4165 return float32_default_nan;
4166 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01004167 if (!float32_is_zero(a)) {
4168 float_raise(float_flag_input_denormal, s);
4169 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01004170 float_raise(float_flag_divbyzero, s);
4171 return float32_set_sign(float32_infinity, float32_is_neg(a));
4172 } else if (float32_is_neg(a)) {
4173 float_raise(float_flag_invalid, s);
4174 return float32_default_nan;
4175 } else if (float32_is_infinity(a)) {
4176 return float32_zero;
4177 }
4178
4179 /* Normalize to a double-precision value between 0.25 and 1.0,
4180 * preserving the parity of the exponent. */
4181 if ((val & 0x800000) == 0) {
4182 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
4183 | (0x3feULL << 52)
4184 | ((uint64_t)(val & 0x7fffff) << 29));
4185 } else {
4186 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
4187 | (0x3fdULL << 52)
4188 | ((uint64_t)(val & 0x7fffff) << 29));
4189 }
4190
4191 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
4192
4193 f64 = recip_sqrt_estimate(f64, env);
4194
4195 val64 = float64_val(f64);
4196
Christophe LYON26cc6ab2011-10-19 16:14:05 +00004197 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01004198 | ((val64 >> 29) & 0x7fffff);
4199 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00004200}
4201
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004202uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00004203{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01004204 float64 f64;
4205
4206 if ((a & 0x80000000) == 0) {
4207 return 0xffffffff;
4208 }
4209
4210 f64 = make_float64((0x3feULL << 52)
4211 | ((int64_t)(a & 0x7fffffff) << 21));
4212
4213 f64 = recip_estimate (f64, env);
4214
4215 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00004216}
4217
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004218uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00004219{
Christophe Lyone07be5d2011-02-21 17:38:48 +01004220 float64 f64;
4221
4222 if ((a & 0xc0000000) == 0) {
4223 return 0xffffffff;
4224 }
4225
4226 if (a & 0x80000000) {
4227 f64 = make_float64((0x3feULL << 52)
4228 | ((uint64_t)(a & 0x7fffffff) << 21));
4229 } else { /* bits 31-30 == '01' */
4230 f64 = make_float64((0x3fdULL << 52)
4231 | ((uint64_t)(a & 0x3fffffff) << 22));
4232 }
4233
4234 f64 = recip_sqrt_estimate(f64, env);
4235
4236 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00004237}
pbrookfe1479c2008-12-19 13:18:36 +00004238
Peter Maydellda97f522011-10-19 16:14:07 +00004239/* VFPv4 fused multiply-accumulate */
4240float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
4241{
4242 float_status *fpst = fpstp;
4243 return float32_muladd(a, b, c, 0, fpst);
4244}
4245
4246float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
4247{
4248 float_status *fpst = fpstp;
4249 return float64_muladd(a, b, c, 0, fpst);
4250}
Will Newton40cfacd2013-12-06 17:01:41 +00004251
4252/* ARMv8 VMAXNM/VMINNM */
4253float32 VFP_HELPER(maxnm, s)(float32 a, float32 b, void *fpstp)
4254{
4255 float_status *fpst = fpstp;
4256 return float32_maxnum(a, b, fpst);
4257}
4258
4259float64 VFP_HELPER(maxnm, d)(float64 a, float64 b, void *fpstp)
4260{
4261 float_status *fpst = fpstp;
4262 return float64_maxnum(a, b, fpst);
4263}
4264
4265float32 VFP_HELPER(minnm, s)(float32 a, float32 b, void *fpstp)
4266{
4267 float_status *fpst = fpstp;
4268 return float32_minnum(a, b, fpst);
4269}
4270
4271float64 VFP_HELPER(minnm, d)(float64 a, float64 b, void *fpstp)
4272{
4273 float_status *fpst = fpstp;
4274 return float64_minnum(a, b, fpst);
4275}