blob: 2a98be7436863220401439a1d4bc134ba5429e94 [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 Maydelld4e6df62013-06-25 18:16:07 +010068static int raw_read(CPUARMState *env, const ARMCPRegInfo *ri,
69 uint64_t *value)
70{
Peter Maydell22d9e1a2013-08-20 14:54:31 +010071 if (ri->type & ARM_CP_64BIT) {
72 *value = CPREG_FIELD64(env, ri);
73 } else {
74 *value = CPREG_FIELD32(env, ri);
75 }
Peter Maydelld4e6df62013-06-25 18:16:07 +010076 return 0;
77}
78
79static int raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
80 uint64_t value)
81{
Peter Maydell22d9e1a2013-08-20 14:54:31 +010082 if (ri->type & ARM_CP_64BIT) {
83 CPREG_FIELD64(env, ri) = value;
84 } else {
85 CPREG_FIELD32(env, ri) = value;
86 }
Peter Maydelld4e6df62013-06-25 18:16:07 +010087 return 0;
88}
89
Peter Maydell721fae12013-06-25 18:16:07 +010090static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
91 uint64_t *v)
92{
93 /* Raw read of a coprocessor register (as needed for migration, etc)
94 * return true on success, false if the read is impossible for some reason.
95 */
96 if (ri->type & ARM_CP_CONST) {
97 *v = ri->resetvalue;
98 } else if (ri->raw_readfn) {
99 return (ri->raw_readfn(env, ri, v) == 0);
100 } else if (ri->readfn) {
101 return (ri->readfn(env, ri, v) == 0);
102 } else {
103 if (ri->type & ARM_CP_64BIT) {
104 *v = CPREG_FIELD64(env, ri);
105 } else {
106 *v = CPREG_FIELD32(env, ri);
107 }
108 }
109 return true;
110}
111
112static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
113 int64_t v)
114{
115 /* Raw write of a coprocessor register (as needed for migration, etc).
116 * Return true on success, false if the write is impossible for some reason.
117 * Note that constant registers are treated as write-ignored; the
118 * caller should check for success by whether a readback gives the
119 * value written.
120 */
121 if (ri->type & ARM_CP_CONST) {
122 return true;
123 } else if (ri->raw_writefn) {
124 return (ri->raw_writefn(env, ri, v) == 0);
125 } else if (ri->writefn) {
126 return (ri->writefn(env, ri, v) == 0);
127 } else {
128 if (ri->type & ARM_CP_64BIT) {
129 CPREG_FIELD64(env, ri) = v;
130 } else {
131 CPREG_FIELD32(env, ri) = v;
132 }
133 }
134 return true;
135}
136
137bool write_cpustate_to_list(ARMCPU *cpu)
138{
139 /* Write the coprocessor state from cpu->env to the (index,value) list. */
140 int i;
141 bool ok = true;
142
143 for (i = 0; i < cpu->cpreg_array_len; i++) {
144 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
145 const ARMCPRegInfo *ri;
146 uint64_t v;
147 ri = get_arm_cp_reginfo(cpu, regidx);
148 if (!ri) {
149 ok = false;
150 continue;
151 }
152 if (ri->type & ARM_CP_NO_MIGRATE) {
153 continue;
154 }
155 if (!read_raw_cp_reg(&cpu->env, ri, &v)) {
156 ok = false;
157 continue;
158 }
159 cpu->cpreg_values[i] = v;
160 }
161 return ok;
162}
163
164bool write_list_to_cpustate(ARMCPU *cpu)
165{
166 int i;
167 bool ok = true;
168
169 for (i = 0; i < cpu->cpreg_array_len; i++) {
170 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
171 uint64_t v = cpu->cpreg_values[i];
172 uint64_t readback;
173 const ARMCPRegInfo *ri;
174
175 ri = get_arm_cp_reginfo(cpu, regidx);
176 if (!ri) {
177 ok = false;
178 continue;
179 }
180 if (ri->type & ARM_CP_NO_MIGRATE) {
181 continue;
182 }
183 /* Write value and confirm it reads back as written
184 * (to catch read-only registers and partially read-only
185 * registers where the incoming migration value doesn't match)
186 */
187 if (!write_raw_cp_reg(&cpu->env, ri, v) ||
188 !read_raw_cp_reg(&cpu->env, ri, &readback) ||
189 readback != v) {
190 ok = false;
191 }
192 }
193 return ok;
194}
195
196static void add_cpreg_to_list(gpointer key, gpointer opaque)
197{
198 ARMCPU *cpu = opaque;
199 uint64_t regidx;
200 const ARMCPRegInfo *ri;
201
202 regidx = *(uint32_t *)key;
203 ri = get_arm_cp_reginfo(cpu, regidx);
204
205 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
206 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207 /* The value array need not be initialized at this point */
208 cpu->cpreg_array_len++;
209 }
210}
211
212static void count_cpreg(gpointer key, gpointer opaque)
213{
214 ARMCPU *cpu = opaque;
215 uint64_t regidx;
216 const ARMCPRegInfo *ri;
217
218 regidx = *(uint32_t *)key;
219 ri = get_arm_cp_reginfo(cpu, regidx);
220
221 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
222 cpu->cpreg_array_len++;
223 }
224}
225
226static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
227{
228 uint32_t aidx = *(uint32_t *)a;
229 uint32_t bidx = *(uint32_t *)b;
230
231 return aidx - bidx;
232}
233
Peter Maydell82a3a112013-07-01 12:40:19 +0100234static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
235{
236 GList **plist = udata;
237
238 *plist = g_list_prepend(*plist, key);
239}
240
Peter Maydell721fae12013-06-25 18:16:07 +0100241void init_cpreg_list(ARMCPU *cpu)
242{
243 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
244 * Note that we require cpreg_tuples[] to be sorted by key ID.
245 */
Peter Maydell82a3a112013-07-01 12:40:19 +0100246 GList *keys = NULL;
Peter Maydell721fae12013-06-25 18:16:07 +0100247 int arraylen;
248
Peter Maydell82a3a112013-07-01 12:40:19 +0100249 g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
250
Peter Maydell721fae12013-06-25 18:16:07 +0100251 keys = g_list_sort(keys, cpreg_key_compare);
252
253 cpu->cpreg_array_len = 0;
254
255 g_list_foreach(keys, count_cpreg, cpu);
256
257 arraylen = cpu->cpreg_array_len;
258 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
259 cpu->cpreg_values = g_new(uint64_t, arraylen);
260 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
261 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
262 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
263 cpu->cpreg_array_len = 0;
264
265 g_list_foreach(keys, add_cpreg_to_list, cpu);
266
267 assert(cpu->cpreg_array_len == arraylen);
268
269 g_list_free(keys);
270}
271
Peter Maydellc983fe62012-06-20 11:57:13 +0000272static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
273{
274 env->cp15.c3 = value;
275 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
276 return 0;
277}
278
Peter Maydell08de2072012-06-20 11:57:14 +0000279static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
280{
281 if (env->cp15.c13_fcse != value) {
282 /* Unlike real hardware the qemu TLB uses virtual addresses,
283 * not modified virtual addresses, so this causes a TLB flush.
284 */
285 tlb_flush(env, 1);
286 env->cp15.c13_fcse = value;
287 }
288 return 0;
289}
290static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
291 uint64_t value)
292{
293 if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
294 /* For VMSA (when not using the LPAE long descriptor page table
295 * format) this register includes the ASID, so do a TLB flush.
296 * For PMSA it is purely a process ID and no action is needed.
297 */
298 tlb_flush(env, 1);
299 }
300 env->cp15.c13_context = value;
301 return 0;
302}
303
Peter Maydelld9298232012-06-20 11:57:16 +0000304static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
305 uint64_t value)
306{
307 /* Invalidate all (TLBIALL) */
308 tlb_flush(env, 1);
309 return 0;
310}
311
312static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
313 uint64_t value)
314{
315 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
316 tlb_flush_page(env, value & TARGET_PAGE_MASK);
317 return 0;
318}
319
320static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
321 uint64_t value)
322{
323 /* Invalidate by ASID (TLBIASID) */
324 tlb_flush(env, value == 0);
325 return 0;
326}
327
328static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
329 uint64_t value)
330{
331 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
332 tlb_flush_page(env, value & TARGET_PAGE_MASK);
333 return 0;
334}
335
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000336static const ARMCPRegInfo cp_reginfo[] = {
337 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
338 * version" bits will read as a reserved value, which should cause
339 * Linux to not try to use the debug hardware.
340 */
341 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
342 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellc983fe62012-06-20 11:57:13 +0000343 /* MMU Domain access control / MPU write buffer control */
344 { .name = "DACR", .cp = 15,
345 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
346 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100347 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
Peter Maydell08de2072012-06-20 11:57:14 +0000348 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
349 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100350 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
Peter Maydell08de2072012-06-20 11:57:14 +0000351 { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
352 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100353 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
Peter Maydell4fdd17d2012-06-20 11:57:15 +0000354 /* ??? This covers not just the impdef TLB lockdown registers but also
355 * some v7VMSA registers relating to TEX remap, so it is overly broad.
356 */
357 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
358 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
Peter Maydelld9298232012-06-20 11:57:16 +0000359 /* MMU TLB control. Note that the wildcarding means we cover not just
360 * the unified TLB ops but also the dside/iside/inner-shareable variants.
361 */
362 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100363 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
364 .type = ARM_CP_NO_MIGRATE },
Peter Maydelld9298232012-06-20 11:57:16 +0000365 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100366 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
367 .type = ARM_CP_NO_MIGRATE },
Peter Maydelld9298232012-06-20 11:57:16 +0000368 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100369 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
370 .type = ARM_CP_NO_MIGRATE },
Peter Maydelld9298232012-06-20 11:57:16 +0000371 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100372 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
373 .type = ARM_CP_NO_MIGRATE },
Peter Maydellc4804212012-06-20 11:57:17 +0000374 /* Cache maintenance ops; some of this space may be overridden later. */
375 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
376 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
377 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000378 REGINFO_SENTINEL
379};
380
Peter Maydell7d57f402012-06-20 11:57:11 +0000381static const ARMCPRegInfo not_v6_cp_reginfo[] = {
382 /* Not all pre-v6 cores implemented this WFI, so this is slightly
383 * over-broad.
384 */
385 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
386 .access = PL1_W, .type = ARM_CP_WFI },
387 REGINFO_SENTINEL
388};
389
390static const ARMCPRegInfo not_v7_cp_reginfo[] = {
391 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
392 * is UNPREDICTABLE; we choose to NOP as most implementations do).
393 */
394 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
395 .access = PL1_W, .type = ARM_CP_WFI },
Peter Maydell34f90522012-06-20 11:57:18 +0000396 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
397 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
398 * OMAPCP will override this space.
399 */
400 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
401 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
402 .resetvalue = 0 },
403 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
404 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
405 .resetvalue = 0 },
Peter Maydell776d4e52012-06-20 11:57:19 +0000406 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
407 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100408 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
409 .resetvalue = 0 },
Peter Maydell7d57f402012-06-20 11:57:11 +0000410 REGINFO_SENTINEL
411};
412
Peter Maydell2771db22012-06-20 11:57:18 +0000413static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
414{
415 if (env->cp15.c1_coproc != value) {
416 env->cp15.c1_coproc = value;
417 /* ??? Is this safe when called from within a TB? */
418 tb_flush(env);
419 }
420 return 0;
421}
422
Peter Maydell7d57f402012-06-20 11:57:11 +0000423static const ARMCPRegInfo v6_cp_reginfo[] = {
424 /* prefetch by MVA in v6, NOP in v7 */
425 { .name = "MVA_prefetch",
426 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
427 .access = PL1_W, .type = ARM_CP_NOP },
428 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
429 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell091fd172012-07-12 10:58:36 +0000430 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
Peter Maydell7d57f402012-06-20 11:57:11 +0000431 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell091fd172012-07-12 10:58:36 +0000432 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
Peter Maydell7d57f402012-06-20 11:57:11 +0000433 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell06d76f32012-06-20 11:57:17 +0000434 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
435 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
436 .resetvalue = 0, },
437 /* Watchpoint Fault Address Register : should actually only be present
438 * for 1136, 1176, 11MPCore.
439 */
440 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
441 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
Peter Maydell2771db22012-06-20 11:57:18 +0000442 { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
443 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
444 .resetvalue = 0, .writefn = cpacr_write },
Peter Maydell7d57f402012-06-20 11:57:11 +0000445 REGINFO_SENTINEL
446};
447
Peter Maydelld4e6df62013-06-25 18:16:07 +0100448
Peter Maydell200ac0e2012-06-20 11:57:12 +0000449static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
450 uint64_t *value)
451{
452 /* Generic performance monitor register read function for where
453 * user access may be allowed by PMUSERENR.
454 */
455 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
456 return EXCP_UDEF;
457 }
458 *value = CPREG_FIELD32(env, ri);
459 return 0;
460}
461
462static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
463 uint64_t value)
464{
465 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
466 return EXCP_UDEF;
467 }
468 /* only the DP, X, D and E bits are writable */
469 env->cp15.c9_pmcr &= ~0x39;
470 env->cp15.c9_pmcr |= (value & 0x39);
471 return 0;
472}
473
474static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
475 uint64_t value)
476{
477 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
478 return EXCP_UDEF;
479 }
480 value &= (1 << 31);
481 env->cp15.c9_pmcnten |= value;
482 return 0;
483}
484
485static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
486 uint64_t value)
487{
488 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
489 return EXCP_UDEF;
490 }
491 value &= (1 << 31);
492 env->cp15.c9_pmcnten &= ~value;
493 return 0;
494}
495
496static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
497 uint64_t value)
498{
499 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
500 return EXCP_UDEF;
501 }
502 env->cp15.c9_pmovsr &= ~value;
503 return 0;
504}
505
506static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
507 uint64_t value)
508{
509 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
510 return EXCP_UDEF;
511 }
512 env->cp15.c9_pmxevtyper = value & 0xff;
513 return 0;
514}
515
516static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
517 uint64_t value)
518{
519 env->cp15.c9_pmuserenr = value & 1;
520 return 0;
521}
522
523static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
524 uint64_t value)
525{
526 /* We have no event counters so only the C bit can be changed */
527 value &= (1 << 31);
528 env->cp15.c9_pminten |= value;
529 return 0;
530}
531
532static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
533 uint64_t value)
534{
535 value &= (1 << 31);
536 env->cp15.c9_pminten &= ~value;
537 return 0;
538}
539
Peter Maydell776d4e52012-06-20 11:57:19 +0000540static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
541 uint64_t *value)
542{
543 ARMCPU *cpu = arm_env_get_cpu(env);
544 *value = cpu->ccsidr[env->cp15.c0_cssel];
545 return 0;
546}
547
548static int csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
549 uint64_t value)
550{
551 env->cp15.c0_cssel = value & 0xf;
552 return 0;
553}
554
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000555static const ARMCPRegInfo v7_cp_reginfo[] = {
556 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
557 * debug components
558 */
559 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
560 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell091fd172012-07-12 10:58:36 +0000561 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000562 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell7d57f402012-06-20 11:57:11 +0000563 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
564 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
565 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000566 /* Performance monitors are implementation defined in v7,
567 * but with an ARM recommended set of registers, which we
568 * follow (although we don't actually implement any counters)
569 *
570 * Performance registers fall into three categories:
571 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
572 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
573 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
574 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
575 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
576 */
577 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
578 .access = PL0_RW, .resetvalue = 0,
579 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100580 .readfn = pmreg_read, .writefn = pmcntenset_write,
581 .raw_readfn = raw_read, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000582 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
583 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100584 .readfn = pmreg_read, .writefn = pmcntenclr_write,
585 .type = ARM_CP_NO_MIGRATE },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000586 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
587 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100588 .readfn = pmreg_read, .writefn = pmovsr_write,
589 .raw_readfn = raw_read, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000590 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
591 * respect PMUSERENR.
592 */
593 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
594 .access = PL0_W, .type = ARM_CP_NOP },
595 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
596 * We choose to RAZ/WI. XXX should respect PMUSERENR.
597 */
598 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
599 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
600 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
601 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
602 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
603 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
604 .access = PL0_RW,
605 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100606 .readfn = pmreg_read, .writefn = pmxevtyper_write,
607 .raw_readfn = raw_read, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000608 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
609 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
610 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
611 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
612 .access = PL0_R | PL1_RW,
613 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
614 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100615 .writefn = pmuserenr_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000616 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
617 .access = PL1_RW,
618 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
619 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100620 .writefn = pmintenset_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000621 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100622 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
Peter Maydell200ac0e2012-06-20 11:57:12 +0000623 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100624 .resetvalue = 0, .writefn = pmintenclr_write, },
Peter Maydell2771db22012-06-20 11:57:18 +0000625 { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
626 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
627 .resetvalue = 0, },
Peter Maydell776d4e52012-06-20 11:57:19 +0000628 { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100629 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
Peter Maydell776d4e52012-06-20 11:57:19 +0000630 { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
631 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
632 .writefn = csselr_write, .resetvalue = 0 },
633 /* Auxiliary ID register: this actually has an IMPDEF value but for now
634 * just RAZ for all cores:
635 */
636 { .name = "AIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 7,
637 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000638 REGINFO_SENTINEL
639};
640
Peter Maydellc326b972012-06-20 11:57:10 +0000641static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
642{
643 value &= 1;
644 env->teecr = value;
645 return 0;
646}
647
648static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
649 uint64_t *value)
650{
651 /* This is a helper function because the user access rights
652 * depend on the value of the TEECR.
653 */
654 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
655 return EXCP_UDEF;
656 }
657 *value = env->teehbr;
658 return 0;
659}
660
661static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
662 uint64_t value)
663{
664 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
665 return EXCP_UDEF;
666 }
667 env->teehbr = value;
668 return 0;
669}
670
671static const ARMCPRegInfo t2ee_cp_reginfo[] = {
672 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
673 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
674 .resetvalue = 0,
675 .writefn = teecr_write },
676 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
677 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100678 .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write,
Peter Maydellc326b972012-06-20 11:57:10 +0000679 .readfn = teehbr_read, .writefn = teehbr_write },
680 REGINFO_SENTINEL
681};
682
Peter Maydell4d31c592012-06-20 11:57:11 +0000683static const ARMCPRegInfo v6k_cp_reginfo[] = {
684 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
685 .access = PL0_RW,
686 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
687 .resetvalue = 0 },
688 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
689 .access = PL0_R|PL1_W,
690 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
691 .resetvalue = 0 },
692 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
693 .access = PL1_RW,
694 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
695 .resetvalue = 0 },
696 REGINFO_SENTINEL
697};
698
Peter Maydell55d284a2013-08-20 14:54:31 +0100699#ifndef CONFIG_USER_ONLY
700
701static uint64_t gt_get_countervalue(CPUARMState *env)
702{
Alex Blighbc72ad62013-08-21 16:03:08 +0100703 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
Peter Maydell55d284a2013-08-20 14:54:31 +0100704}
705
706static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
707{
708 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
709
710 if (gt->ctl & 1) {
711 /* Timer enabled: calculate and set current ISTATUS, irq, and
712 * reset timer to when ISTATUS next has to change
713 */
714 uint64_t count = gt_get_countervalue(&cpu->env);
715 /* Note that this must be unsigned 64 bit arithmetic: */
716 int istatus = count >= gt->cval;
717 uint64_t nexttick;
718
719 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
720 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
721 (istatus && !(gt->ctl & 2)));
722 if (istatus) {
723 /* Next transition is when count rolls back over to zero */
724 nexttick = UINT64_MAX;
725 } else {
726 /* Next transition is when we hit cval */
727 nexttick = gt->cval;
728 }
729 /* Note that the desired next expiry time might be beyond the
730 * signed-64-bit range of a QEMUTimer -- in this case we just
731 * set the timer for as far in the future as possible. When the
732 * timer expires we will reset the timer for any remaining period.
733 */
734 if (nexttick > INT64_MAX / GTIMER_SCALE) {
735 nexttick = INT64_MAX / GTIMER_SCALE;
736 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100737 timer_mod(cpu->gt_timer[timeridx], nexttick);
Peter Maydell55d284a2013-08-20 14:54:31 +0100738 } else {
739 /* Timer disabled: ISTATUS and timer output always clear */
740 gt->ctl &= ~4;
741 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
Alex Blighbc72ad62013-08-21 16:03:08 +0100742 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell55d284a2013-08-20 14:54:31 +0100743 }
744}
745
746static int gt_cntfrq_read(CPUARMState *env, const ARMCPRegInfo *ri,
747 uint64_t *value)
748{
749 /* Not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
750 if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
751 return EXCP_UDEF;
752 }
753 *value = env->cp15.c14_cntfrq;
754 return 0;
755}
756
757static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
758{
759 ARMCPU *cpu = arm_env_get_cpu(env);
760 int timeridx = ri->opc1 & 1;
761
Alex Blighbc72ad62013-08-21 16:03:08 +0100762 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell55d284a2013-08-20 14:54:31 +0100763}
764
765static int gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri,
766 uint64_t *value)
767{
768 int timeridx = ri->opc1 & 1;
769
770 if (arm_current_pl(env) == 0 &&
771 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
772 return EXCP_UDEF;
773 }
774 *value = gt_get_countervalue(env);
775 return 0;
776}
777
778static int gt_cval_read(CPUARMState *env, const ARMCPRegInfo *ri,
779 uint64_t *value)
780{
781 int timeridx = ri->opc1 & 1;
782
783 if (arm_current_pl(env) == 0 &&
784 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
785 return EXCP_UDEF;
786 }
787 *value = env->cp15.c14_timer[timeridx].cval;
788 return 0;
789}
790
791static int gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
792 uint64_t value)
793{
794 int timeridx = ri->opc1 & 1;
795
796 env->cp15.c14_timer[timeridx].cval = value;
797 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
798 return 0;
799}
800static int gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
801 uint64_t *value)
802{
803 int timeridx = ri->crm & 1;
804
805 if (arm_current_pl(env) == 0 &&
806 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
807 return EXCP_UDEF;
808 }
809 *value = (uint32_t)(env->cp15.c14_timer[timeridx].cval -
810 gt_get_countervalue(env));
811 return 0;
812}
813
814static int gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
815 uint64_t value)
816{
817 int timeridx = ri->crm & 1;
818
819 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
820 + sextract64(value, 0, 32);
821 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
822 return 0;
823}
824
825static int gt_ctl_read(CPUARMState *env, const ARMCPRegInfo *ri,
826 uint64_t *value)
827{
828 int timeridx = ri->crm & 1;
829
830 if (arm_current_pl(env) == 0 &&
831 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
832 return EXCP_UDEF;
833 }
834 *value = env->cp15.c14_timer[timeridx].ctl;
835 return 0;
836}
837
838static int gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
839 uint64_t value)
840{
841 ARMCPU *cpu = arm_env_get_cpu(env);
842 int timeridx = ri->crm & 1;
843 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
844
845 env->cp15.c14_timer[timeridx].ctl = value & 3;
846 if ((oldval ^ value) & 1) {
847 /* Enable toggled */
848 gt_recalc_timer(cpu, timeridx);
849 } else if ((oldval & value) & 2) {
850 /* IMASK toggled: don't need to recalculate,
851 * just set the interrupt line based on ISTATUS
852 */
853 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
854 (oldval & 4) && (value & 2));
855 }
856 return 0;
857}
858
859void arm_gt_ptimer_cb(void *opaque)
860{
861 ARMCPU *cpu = opaque;
862
863 gt_recalc_timer(cpu, GTIMER_PHYS);
864}
865
866void arm_gt_vtimer_cb(void *opaque)
867{
868 ARMCPU *cpu = opaque;
869
870 gt_recalc_timer(cpu, GTIMER_VIRT);
871}
872
Peter Maydell6cc7a3a2012-06-20 11:57:12 +0000873static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
Peter Maydell55d284a2013-08-20 14:54:31 +0100874 /* Note that CNTFRQ is purely reads-as-written for the benefit
875 * of software; writing it doesn't actually change the timer frequency.
876 * Our reset value matches the fixed frequency we implement the timer at.
877 */
878 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
879 .access = PL1_RW | PL0_R,
880 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
881 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
882 .readfn = gt_cntfrq_read, .raw_readfn = raw_read,
883 },
884 /* overall control: mostly access permissions */
885 { .name = "CNTKCTL", .cp = 15, .crn = 14, .crm = 1, .opc1 = 0, .opc2 = 0,
886 .access = PL1_RW,
887 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
888 .resetvalue = 0,
889 },
890 /* per-timer control */
891 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
892 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
893 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
894 .resetvalue = 0,
895 .readfn = gt_ctl_read, .writefn = gt_ctl_write,
896 .raw_readfn = raw_read, .raw_writefn = raw_write,
897 },
898 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
899 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
900 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
901 .resetvalue = 0,
902 .readfn = gt_ctl_read, .writefn = gt_ctl_write,
903 .raw_readfn = raw_read, .raw_writefn = raw_write,
904 },
905 /* TimerValue views: a 32 bit downcounting view of the underlying state */
906 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
907 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
908 .readfn = gt_tval_read, .writefn = gt_tval_write,
909 },
910 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
911 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
912 .readfn = gt_tval_read, .writefn = gt_tval_write,
913 },
914 /* The counter itself */
915 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
916 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
917 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
918 },
919 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
920 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
921 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
922 },
923 /* Comparison value, indicating when the timer goes off */
924 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
925 .access = PL1_RW | PL0_R,
926 .type = ARM_CP_64BIT | ARM_CP_IO,
927 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
928 .resetvalue = 0,
929 .readfn = gt_cval_read, .writefn = gt_cval_write,
930 .raw_readfn = raw_read, .raw_writefn = raw_write,
931 },
932 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
933 .access = PL1_RW | PL0_R,
934 .type = ARM_CP_64BIT | ARM_CP_IO,
935 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
936 .resetvalue = 0,
937 .readfn = gt_cval_read, .writefn = gt_cval_write,
938 .raw_readfn = raw_read, .raw_writefn = raw_write,
939 },
Peter Maydell6cc7a3a2012-06-20 11:57:12 +0000940 REGINFO_SENTINEL
941};
942
Peter Maydell55d284a2013-08-20 14:54:31 +0100943#else
944/* In user-mode none of the generic timer registers are accessible,
Alex Blighbc72ad62013-08-21 16:03:08 +0100945 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
Peter Maydell55d284a2013-08-20 14:54:31 +0100946 * so instead just don't register any of them.
947 */
948static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
949 REGINFO_SENTINEL
950};
951
952#endif
953
Peter Maydell4a501602012-06-20 11:57:16 +0000954static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
955{
Peter Maydell891a2fe2012-07-12 10:59:09 +0000956 if (arm_feature(env, ARM_FEATURE_LPAE)) {
957 env->cp15.c7_par = value;
958 } else if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell4a501602012-06-20 11:57:16 +0000959 env->cp15.c7_par = value & 0xfffff6ff;
960 } else {
961 env->cp15.c7_par = value & 0xfffff1ff;
962 }
963 return 0;
964}
965
966#ifndef CONFIG_USER_ONLY
967/* get_phys_addr() isn't present for user-mode-only targets */
Peter Maydell702a9352012-07-12 10:59:10 +0000968
969/* Return true if extended addresses are enabled, ie this is an
970 * LPAE implementation and we are using the long-descriptor translation
971 * table format because the TTBCR EAE bit is set.
972 */
973static inline bool extended_addresses_enabled(CPUARMState *env)
974{
975 return arm_feature(env, ARM_FEATURE_LPAE)
Peter Maydell78dbbbe2013-09-10 19:09:32 +0100976 && (env->cp15.c2_control & (1U << 31));
Peter Maydell702a9352012-07-12 10:59:10 +0000977}
978
Peter Maydell4a501602012-06-20 11:57:16 +0000979static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
980{
Avi Kivitya8170e52012-10-23 12:30:10 +0200981 hwaddr phys_addr;
Peter Maydell4a501602012-06-20 11:57:16 +0000982 target_ulong page_size;
983 int prot;
984 int ret, is_user = ri->opc2 & 2;
985 int access_type = ri->opc2 & 1;
986
987 if (ri->opc2 & 4) {
988 /* Other states are only available with TrustZone */
989 return EXCP_UDEF;
990 }
991 ret = get_phys_addr(env, value, access_type, is_user,
992 &phys_addr, &prot, &page_size);
Peter Maydell702a9352012-07-12 10:59:10 +0000993 if (extended_addresses_enabled(env)) {
994 /* ret is a DFSR/IFSR value for the long descriptor
995 * translation table format, but with WnR always clear.
996 * Convert it to a 64-bit PAR.
997 */
998 uint64_t par64 = (1 << 11); /* LPAE bit always set */
999 if (ret == 0) {
1000 par64 |= phys_addr & ~0xfffULL;
1001 /* We don't set the ATTR or SH fields in the PAR. */
Peter Maydell4a501602012-06-20 11:57:16 +00001002 } else {
Peter Maydell702a9352012-07-12 10:59:10 +00001003 par64 |= 1; /* F */
1004 par64 |= (ret & 0x3f) << 1; /* FS */
1005 /* Note that S2WLK and FSTAGE are always zero, because we don't
1006 * implement virtualization and therefore there can't be a stage 2
1007 * fault.
1008 */
Peter Maydell4a501602012-06-20 11:57:16 +00001009 }
Peter Maydell702a9352012-07-12 10:59:10 +00001010 env->cp15.c7_par = par64;
1011 env->cp15.c7_par_hi = par64 >> 32;
Peter Maydell4a501602012-06-20 11:57:16 +00001012 } else {
Peter Maydell702a9352012-07-12 10:59:10 +00001013 /* ret is a DFSR/IFSR value for the short descriptor
1014 * translation table format (with WnR always clear).
1015 * Convert it to a 32-bit PAR.
1016 */
1017 if (ret == 0) {
1018 /* We do not set any attribute bits in the PAR */
1019 if (page_size == (1 << 24)
1020 && arm_feature(env, ARM_FEATURE_V7)) {
1021 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1022 } else {
1023 env->cp15.c7_par = phys_addr & 0xfffff000;
1024 }
1025 } else {
1026 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1027 ((ret & (12 << 1)) >> 6) |
1028 ((ret & 0xf) << 1) | 1;
1029 }
1030 env->cp15.c7_par_hi = 0;
Peter Maydell4a501602012-06-20 11:57:16 +00001031 }
1032 return 0;
1033}
1034#endif
1035
1036static const ARMCPRegInfo vapa_cp_reginfo[] = {
1037 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1038 .access = PL1_RW, .resetvalue = 0,
1039 .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
1040 .writefn = par_write },
1041#ifndef CONFIG_USER_ONLY
1042 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001043 .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
Peter Maydell4a501602012-06-20 11:57:16 +00001044#endif
1045 REGINFO_SENTINEL
1046};
1047
Peter Maydell18032be2012-06-20 11:57:13 +00001048/* Return basic MPU access permission bits. */
1049static uint32_t simple_mpu_ap_bits(uint32_t val)
1050{
1051 uint32_t ret;
1052 uint32_t mask;
1053 int i;
1054 ret = 0;
1055 mask = 3;
1056 for (i = 0; i < 16; i += 2) {
1057 ret |= (val >> i) & mask;
1058 mask <<= 2;
1059 }
1060 return ret;
1061}
1062
1063/* Pad basic MPU access permission bits to extended format. */
1064static uint32_t extended_mpu_ap_bits(uint32_t val)
1065{
1066 uint32_t ret;
1067 uint32_t mask;
1068 int i;
1069 ret = 0;
1070 mask = 3;
1071 for (i = 0; i < 16; i += 2) {
1072 ret |= (val & mask) << i;
1073 mask <<= 2;
1074 }
1075 return ret;
1076}
1077
1078static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1079 uint64_t value)
1080{
1081 env->cp15.c5_data = extended_mpu_ap_bits(value);
1082 return 0;
1083}
1084
1085static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1086 uint64_t *value)
1087{
1088 *value = simple_mpu_ap_bits(env->cp15.c5_data);
1089 return 0;
1090}
1091
1092static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1093 uint64_t value)
1094{
1095 env->cp15.c5_insn = extended_mpu_ap_bits(value);
1096 return 0;
1097}
1098
1099static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1100 uint64_t *value)
1101{
1102 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
1103 return 0;
1104}
1105
Peter Maydell06d76f32012-06-20 11:57:17 +00001106static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
1107 uint64_t *value)
1108{
Stefan Weil599d64f2012-09-04 07:35:57 +02001109 if (ri->crm >= 8) {
Peter Maydell06d76f32012-06-20 11:57:17 +00001110 return EXCP_UDEF;
1111 }
1112 *value = env->cp15.c6_region[ri->crm];
1113 return 0;
1114}
1115
1116static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
1117 uint64_t value)
1118{
Stefan Weil599d64f2012-09-04 07:35:57 +02001119 if (ri->crm >= 8) {
Peter Maydell06d76f32012-06-20 11:57:17 +00001120 return EXCP_UDEF;
1121 }
1122 env->cp15.c6_region[ri->crm] = value;
1123 return 0;
1124}
1125
Peter Maydell18032be2012-06-20 11:57:13 +00001126static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1127 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001128 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
Peter Maydell18032be2012-06-20 11:57:13 +00001129 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
1130 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1131 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001132 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
Peter Maydell18032be2012-06-20 11:57:13 +00001133 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
1134 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1135 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1136 .access = PL1_RW,
1137 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1138 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1139 .access = PL1_RW,
1140 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00001141 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1142 .access = PL1_RW,
1143 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1144 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1145 .access = PL1_RW,
1146 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
Peter Maydell06d76f32012-06-20 11:57:17 +00001147 /* Protection region base and size registers */
1148 { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
1149 .opc2 = CP_ANY, .access = PL1_RW,
1150 .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
Peter Maydell18032be2012-06-20 11:57:13 +00001151 REGINFO_SENTINEL
1152};
1153
Peter Maydelld4e6df62013-06-25 18:16:07 +01001154static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1155 uint64_t value)
Peter Maydellecce5c32012-06-20 11:57:14 +00001156{
Peter Maydell2ebcebe2013-06-27 16:38:47 +01001157 int maskshift = extract32(value, 0, 3);
1158
Peter Maydelle42c4db2012-07-12 10:59:11 +00001159 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1160 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
Peter Maydelle42c4db2012-07-12 10:59:11 +00001161 } else {
1162 value &= 7;
1163 }
1164 /* Note that we always calculate c2_mask and c2_base_mask, but
1165 * they are only used for short-descriptor tables (ie if EAE is 0);
1166 * for long-descriptor tables the TTBCR fields are used differently
1167 * and the c2_mask and c2_base_mask values are meaningless.
1168 */
Peter Maydellecce5c32012-06-20 11:57:14 +00001169 env->cp15.c2_control = value;
Peter Maydell2ebcebe2013-06-27 16:38:47 +01001170 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1171 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
Peter Maydellecce5c32012-06-20 11:57:14 +00001172 return 0;
1173}
1174
Peter Maydelld4e6df62013-06-25 18:16:07 +01001175static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1176 uint64_t value)
1177{
1178 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1179 /* With LPAE the TTBCR could result in a change of ASID
1180 * via the TTBCR.A1 bit, so do a TLB flush.
1181 */
1182 tlb_flush(env, 1);
1183 }
1184 return vmsa_ttbcr_raw_write(env, ri, value);
1185}
1186
Peter Maydellecce5c32012-06-20 11:57:14 +00001187static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1188{
1189 env->cp15.c2_base_mask = 0xffffc000u;
1190 env->cp15.c2_control = 0;
1191 env->cp15.c2_mask = 0;
1192}
1193
Peter Maydell18032be2012-06-20 11:57:13 +00001194static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1195 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1196 .access = PL1_RW,
1197 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1198 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1199 .access = PL1_RW,
1200 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00001201 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1202 .access = PL1_RW,
1203 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
1204 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1205 .access = PL1_RW,
Peter Maydell81a60ad2012-07-12 10:58:36 +00001206 .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00001207 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1208 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001209 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
Peter Maydellecce5c32012-06-20 11:57:14 +00001210 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
Peter Maydell06d76f32012-06-20 11:57:17 +00001211 { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1212 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
1213 .resetvalue = 0, },
Peter Maydell18032be2012-06-20 11:57:13 +00001214 REGINFO_SENTINEL
1215};
1216
Peter Maydell1047b9d2012-06-20 11:57:15 +00001217static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1218 uint64_t value)
1219{
1220 env->cp15.c15_ticonfig = value & 0xe7;
1221 /* The OS_TYPE bit in this register changes the reported CPUID! */
1222 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1223 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1224 return 0;
1225}
1226
1227static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1228 uint64_t value)
1229{
1230 env->cp15.c15_threadid = value & 0xffff;
1231 return 0;
1232}
1233
1234static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1235 uint64_t value)
1236{
1237 /* Wait-for-interrupt (deprecated) */
Andreas Färberc3affe52013-01-18 15:03:43 +01001238 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
Peter Maydell1047b9d2012-06-20 11:57:15 +00001239 return 0;
1240}
1241
Peter Maydellc4804212012-06-20 11:57:17 +00001242static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1243 uint64_t value)
1244{
1245 /* On OMAP there are registers indicating the max/min index of dcache lines
1246 * containing a dirty line; cache flush operations have to reset these.
1247 */
1248 env->cp15.c15_i_max = 0x000;
1249 env->cp15.c15_i_min = 0xff0;
1250 return 0;
1251}
1252
Peter Maydell18032be2012-06-20 11:57:13 +00001253static const ARMCPRegInfo omap_cp_reginfo[] = {
1254 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1255 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1256 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
Peter Maydell1047b9d2012-06-20 11:57:15 +00001257 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1258 .access = PL1_RW, .type = ARM_CP_NOP },
1259 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1260 .access = PL1_RW,
1261 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1262 .writefn = omap_ticonfig_write },
1263 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1264 .access = PL1_RW,
1265 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1266 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1267 .access = PL1_RW, .resetvalue = 0xff0,
1268 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1269 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1270 .access = PL1_RW,
1271 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1272 .writefn = omap_threadid_write },
1273 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1274 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001275 .type = ARM_CP_NO_MIGRATE,
Peter Maydell1047b9d2012-06-20 11:57:15 +00001276 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1277 /* TODO: Peripheral port remap register:
1278 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1279 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1280 * when MMU is off.
1281 */
Peter Maydellc4804212012-06-20 11:57:17 +00001282 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001283 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1284 .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
Peter Maydellc4804212012-06-20 11:57:17 +00001285 .writefn = omap_cachemaint_write },
Peter Maydell34f90522012-06-20 11:57:18 +00001286 { .name = "C9", .cp = 15, .crn = 9,
1287 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1288 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
Peter Maydell1047b9d2012-06-20 11:57:15 +00001289 REGINFO_SENTINEL
1290};
1291
1292static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1293 uint64_t value)
1294{
1295 value &= 0x3fff;
1296 if (env->cp15.c15_cpar != value) {
1297 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1298 tb_flush(env);
1299 env->cp15.c15_cpar = value;
1300 }
1301 return 0;
1302}
1303
1304static const ARMCPRegInfo xscale_cp_reginfo[] = {
1305 { .name = "XSCALE_CPAR",
1306 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1307 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1308 .writefn = xscale_cpar_write, },
Peter Maydell2771db22012-06-20 11:57:18 +00001309 { .name = "XSCALE_AUXCR",
1310 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1311 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1312 .resetvalue = 0, },
Peter Maydell1047b9d2012-06-20 11:57:15 +00001313 REGINFO_SENTINEL
1314};
1315
1316static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1317 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1318 * implementation of this implementation-defined space.
1319 * Ideally this should eventually disappear in favour of actually
1320 * implementing the correct behaviour for all cores.
1321 */
1322 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1323 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001324 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1325 .resetvalue = 0 },
Peter Maydell18032be2012-06-20 11:57:13 +00001326 REGINFO_SENTINEL
1327};
1328
Peter Maydellc4804212012-06-20 11:57:17 +00001329static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1330 /* Cache status: RAZ because we have no cache so it's always clean */
1331 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001332 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1333 .resetvalue = 0 },
Peter Maydellc4804212012-06-20 11:57:17 +00001334 REGINFO_SENTINEL
1335};
1336
1337static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1338 /* We never have a a block transfer operation in progress */
1339 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001340 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1341 .resetvalue = 0 },
Peter Maydell30b05bb2012-06-20 11:57:22 +00001342 /* The cache ops themselves: these all NOP for QEMU */
1343 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1344 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1345 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1346 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1347 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1348 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1349 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1350 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1351 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1352 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1353 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1354 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
Peter Maydellc4804212012-06-20 11:57:17 +00001355 REGINFO_SENTINEL
1356};
1357
1358static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1359 /* The cache test-and-clean instructions always return (1 << 30)
1360 * to indicate that there are no dirty cache lines.
1361 */
1362 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001363 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1364 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00001365 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001366 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1367 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00001368 REGINFO_SENTINEL
1369};
1370
Peter Maydell34f90522012-06-20 11:57:18 +00001371static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1372 /* Ignore ReadBuffer accesses */
1373 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1374 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001375 .access = PL1_RW, .resetvalue = 0,
1376 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
Peter Maydell34f90522012-06-20 11:57:18 +00001377 REGINFO_SENTINEL
1378};
1379
Peter Maydell81bdde92012-06-20 11:57:20 +00001380static int mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1381 uint64_t *value)
1382{
Andreas Färber55e5c282012-12-17 06:18:02 +01001383 CPUState *cs = CPU(arm_env_get_cpu(env));
1384 uint32_t mpidr = cs->cpu_index;
Peter Maydell81bdde92012-06-20 11:57:20 +00001385 /* We don't support setting cluster ID ([8..11])
1386 * so these bits always RAZ.
1387 */
1388 if (arm_feature(env, ARM_FEATURE_V7MP)) {
Peter Maydell78dbbbe2013-09-10 19:09:32 +01001389 mpidr |= (1U << 31);
Peter Maydell81bdde92012-06-20 11:57:20 +00001390 /* Cores which are uniprocessor (non-coherent)
1391 * but still implement the MP extensions set
1392 * bit 30. (For instance, A9UP.) However we do
1393 * not currently model any of those cores.
1394 */
1395 }
1396 *value = mpidr;
1397 return 0;
1398}
1399
1400static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1401 { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001402 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
Peter Maydell81bdde92012-06-20 11:57:20 +00001403 REGINFO_SENTINEL
1404};
1405
Peter Maydell891a2fe2012-07-12 10:59:09 +00001406static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1407{
1408 *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1409 return 0;
1410}
1411
1412static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1413{
1414 env->cp15.c7_par_hi = value >> 32;
1415 env->cp15.c7_par = value;
1416 return 0;
1417}
1418
1419static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1420{
1421 env->cp15.c7_par_hi = 0;
1422 env->cp15.c7_par = 0;
1423}
1424
1425static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri,
1426 uint64_t *value)
1427{
1428 *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1429 return 0;
1430}
1431
Peter Maydelld4e6df62013-06-25 18:16:07 +01001432static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1433 uint64_t value)
Peter Maydell891a2fe2012-07-12 10:59:09 +00001434{
1435 env->cp15.c2_base0_hi = value >> 32;
1436 env->cp15.c2_base0 = value;
Peter Maydelld4e6df62013-06-25 18:16:07 +01001437 return 0;
1438}
1439
1440static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1441 uint64_t value)
1442{
Peter Maydell891a2fe2012-07-12 10:59:09 +00001443 /* Writes to the 64 bit format TTBRs may change the ASID */
1444 tlb_flush(env, 1);
Peter Maydelld4e6df62013-06-25 18:16:07 +01001445 return ttbr064_raw_write(env, ri, value);
Peter Maydell891a2fe2012-07-12 10:59:09 +00001446}
1447
1448static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1449{
1450 env->cp15.c2_base0_hi = 0;
1451 env->cp15.c2_base0 = 0;
1452}
1453
1454static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri,
1455 uint64_t *value)
1456{
1457 *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1458 return 0;
1459}
1460
1461static int ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri,
1462 uint64_t value)
1463{
1464 env->cp15.c2_base1_hi = value >> 32;
1465 env->cp15.c2_base1 = value;
1466 return 0;
1467}
1468
1469static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1470{
1471 env->cp15.c2_base1_hi = 0;
1472 env->cp15.c2_base1 = 0;
1473}
1474
Peter Maydell7ac681c2012-07-12 10:59:07 +00001475static const ARMCPRegInfo lpae_cp_reginfo[] = {
Peter Maydellb90372a2012-08-06 17:42:18 +01001476 /* NOP AMAIR0/1: the override is because these clash with the rather
Peter Maydell7ac681c2012-07-12 10:59:07 +00001477 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1478 */
1479 { .name = "AMAIR0", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1480 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1481 .resetvalue = 0 },
1482 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1483 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1484 .resetvalue = 0 },
Peter Maydellf9fc6192012-07-12 10:59:08 +00001485 /* 64 bit access versions of the (dummy) debug registers */
1486 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
1487 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1488 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
1489 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
Peter Maydell891a2fe2012-07-12 10:59:09 +00001490 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1491 .access = PL1_RW, .type = ARM_CP_64BIT,
1492 .readfn = par64_read, .writefn = par64_write, .resetfn = par64_reset },
1493 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1494 .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr064_read,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001495 .writefn = ttbr064_write, .raw_writefn = ttbr064_raw_write,
1496 .resetfn = ttbr064_reset },
Peter Maydell891a2fe2012-07-12 10:59:09 +00001497 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1498 .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr164_read,
1499 .writefn = ttbr164_write, .resetfn = ttbr164_reset },
Peter Maydell7ac681c2012-07-12 10:59:07 +00001500 REGINFO_SENTINEL
1501};
1502
Peter Maydell2771db22012-06-20 11:57:18 +00001503static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1504{
1505 env->cp15.c1_sys = value;
1506 /* ??? Lots of these bits are not implemented. */
1507 /* This may enable/disable the MMU, so do a TLB flush. */
1508 tlb_flush(env, 1);
1509 return 0;
1510}
1511
Peter Maydell2ceb98c2012-06-20 11:57:09 +00001512void register_cp_regs_for_features(ARMCPU *cpu)
1513{
1514 /* Register all the coprocessor registers based on feature bits */
1515 CPUARMState *env = &cpu->env;
1516 if (arm_feature(env, ARM_FEATURE_M)) {
1517 /* M profile has no coprocessor registers */
1518 return;
1519 }
1520
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001521 define_arm_cp_regs(cpu, cp_reginfo);
Peter Maydell7d57f402012-06-20 11:57:11 +00001522 if (arm_feature(env, ARM_FEATURE_V6)) {
Peter Maydell8515a092012-06-20 11:57:19 +00001523 /* The ID registers all have impdef reset values */
1524 ARMCPRegInfo v6_idregs[] = {
1525 { .name = "ID_PFR0", .cp = 15, .crn = 0, .crm = 1,
1526 .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1527 .resetvalue = cpu->id_pfr0 },
1528 { .name = "ID_PFR1", .cp = 15, .crn = 0, .crm = 1,
1529 .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1530 .resetvalue = cpu->id_pfr1 },
1531 { .name = "ID_DFR0", .cp = 15, .crn = 0, .crm = 1,
1532 .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1533 .resetvalue = cpu->id_dfr0 },
1534 { .name = "ID_AFR0", .cp = 15, .crn = 0, .crm = 1,
1535 .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1536 .resetvalue = cpu->id_afr0 },
1537 { .name = "ID_MMFR0", .cp = 15, .crn = 0, .crm = 1,
1538 .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1539 .resetvalue = cpu->id_mmfr0 },
1540 { .name = "ID_MMFR1", .cp = 15, .crn = 0, .crm = 1,
1541 .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1542 .resetvalue = cpu->id_mmfr1 },
1543 { .name = "ID_MMFR2", .cp = 15, .crn = 0, .crm = 1,
1544 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1545 .resetvalue = cpu->id_mmfr2 },
1546 { .name = "ID_MMFR3", .cp = 15, .crn = 0, .crm = 1,
1547 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1548 .resetvalue = cpu->id_mmfr3 },
1549 { .name = "ID_ISAR0", .cp = 15, .crn = 0, .crm = 2,
1550 .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1551 .resetvalue = cpu->id_isar0 },
1552 { .name = "ID_ISAR1", .cp = 15, .crn = 0, .crm = 2,
1553 .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1554 .resetvalue = cpu->id_isar1 },
1555 { .name = "ID_ISAR2", .cp = 15, .crn = 0, .crm = 2,
1556 .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1557 .resetvalue = cpu->id_isar2 },
1558 { .name = "ID_ISAR3", .cp = 15, .crn = 0, .crm = 2,
1559 .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1560 .resetvalue = cpu->id_isar3 },
1561 { .name = "ID_ISAR4", .cp = 15, .crn = 0, .crm = 2,
1562 .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1563 .resetvalue = cpu->id_isar4 },
1564 { .name = "ID_ISAR5", .cp = 15, .crn = 0, .crm = 2,
1565 .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1566 .resetvalue = cpu->id_isar5 },
1567 /* 6..7 are as yet unallocated and must RAZ */
1568 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
1569 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1570 .resetvalue = 0 },
1571 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
1572 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1573 .resetvalue = 0 },
1574 REGINFO_SENTINEL
1575 };
1576 define_arm_cp_regs(cpu, v6_idregs);
Peter Maydell7d57f402012-06-20 11:57:11 +00001577 define_arm_cp_regs(cpu, v6_cp_reginfo);
1578 } else {
1579 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
1580 }
Peter Maydell4d31c592012-06-20 11:57:11 +00001581 if (arm_feature(env, ARM_FEATURE_V6K)) {
1582 define_arm_cp_regs(cpu, v6k_cp_reginfo);
1583 }
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001584 if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell200ac0e2012-06-20 11:57:12 +00001585 /* v7 performance monitor control register: same implementor
1586 * field as main ID register, and we implement no event counters.
1587 */
1588 ARMCPRegInfo pmcr = {
1589 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
1590 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
1591 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
Peter Maydelld4e6df62013-06-25 18:16:07 +01001592 .readfn = pmreg_read, .writefn = pmcr_write,
1593 .raw_readfn = raw_read, .raw_writefn = raw_write,
Peter Maydell200ac0e2012-06-20 11:57:12 +00001594 };
Peter Maydell776d4e52012-06-20 11:57:19 +00001595 ARMCPRegInfo clidr = {
1596 .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
1597 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
1598 };
Peter Maydell200ac0e2012-06-20 11:57:12 +00001599 define_one_arm_cp_reg(cpu, &pmcr);
Peter Maydell776d4e52012-06-20 11:57:19 +00001600 define_one_arm_cp_reg(cpu, &clidr);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001601 define_arm_cp_regs(cpu, v7_cp_reginfo);
Peter Maydell7d57f402012-06-20 11:57:11 +00001602 } else {
1603 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001604 }
Peter Maydell18032be2012-06-20 11:57:13 +00001605 if (arm_feature(env, ARM_FEATURE_MPU)) {
1606 /* These are the MPU registers prior to PMSAv6. Any new
1607 * PMSA core later than the ARM946 will require that we
1608 * implement the PMSAv6 or PMSAv7 registers, which are
1609 * completely different.
1610 */
1611 assert(!arm_feature(env, ARM_FEATURE_V6));
1612 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
1613 } else {
1614 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
1615 }
Peter Maydellc326b972012-06-20 11:57:10 +00001616 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
1617 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
1618 }
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00001619 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1620 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
1621 }
Peter Maydell4a501602012-06-20 11:57:16 +00001622 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1623 define_arm_cp_regs(cpu, vapa_cp_reginfo);
1624 }
Peter Maydellc4804212012-06-20 11:57:17 +00001625 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
1626 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
1627 }
1628 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
1629 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
1630 }
1631 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
1632 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
1633 }
Peter Maydell18032be2012-06-20 11:57:13 +00001634 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1635 define_arm_cp_regs(cpu, omap_cp_reginfo);
1636 }
Peter Maydell34f90522012-06-20 11:57:18 +00001637 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
1638 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
1639 }
Peter Maydell1047b9d2012-06-20 11:57:15 +00001640 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1641 define_arm_cp_regs(cpu, xscale_cp_reginfo);
1642 }
1643 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
1644 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
1645 }
Peter Maydell7ac681c2012-07-12 10:59:07 +00001646 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1647 define_arm_cp_regs(cpu, lpae_cp_reginfo);
1648 }
Peter Maydell78848492012-06-20 11:57:20 +00001649 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
1650 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
1651 * be read-only (ie write causes UNDEF exception).
1652 */
1653 {
1654 ARMCPRegInfo id_cp_reginfo[] = {
1655 /* Note that the MIDR isn't a simple constant register because
1656 * of the TI925 behaviour where writes to another register can
1657 * cause the MIDR value to change.
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001658 *
1659 * Unimplemented registers in the c15 0 0 0 space default to
1660 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
1661 * and friends override accordingly.
Peter Maydell78848492012-06-20 11:57:20 +00001662 */
1663 { .name = "MIDR",
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001664 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydell78848492012-06-20 11:57:20 +00001665 .access = PL1_R, .resetvalue = cpu->midr,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001666 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001667 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
1668 .type = ARM_CP_OVERRIDE },
Peter Maydell78848492012-06-20 11:57:20 +00001669 { .name = "CTR",
1670 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
1671 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
1672 { .name = "TCMTR",
1673 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
1674 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1675 { .name = "TLBTR",
1676 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
1677 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1678 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
1679 { .name = "DUMMY",
1680 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
1681 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1682 { .name = "DUMMY",
1683 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
1684 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1685 { .name = "DUMMY",
1686 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
1687 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1688 { .name = "DUMMY",
1689 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
1690 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1691 { .name = "DUMMY",
1692 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
1693 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1694 REGINFO_SENTINEL
1695 };
1696 ARMCPRegInfo crn0_wi_reginfo = {
1697 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
1698 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
1699 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
1700 };
1701 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
1702 arm_feature(env, ARM_FEATURE_STRONGARM)) {
1703 ARMCPRegInfo *r;
1704 /* Register the blanket "writes ignored" value first to cover the
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10001705 * whole space. Then update the specific ID registers to allow write
1706 * access, so that they ignore writes rather than causing them to
1707 * UNDEF.
Peter Maydell78848492012-06-20 11:57:20 +00001708 */
1709 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
1710 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
1711 r->access = PL1_RW;
Peter Maydell78848492012-06-20 11:57:20 +00001712 }
Peter Maydell78848492012-06-20 11:57:20 +00001713 }
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10001714 define_arm_cp_regs(cpu, id_cp_reginfo);
Peter Maydell78848492012-06-20 11:57:20 +00001715 }
1716
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10001717 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
1718 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
1719 }
1720
Peter Maydell2771db22012-06-20 11:57:18 +00001721 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
1722 ARMCPRegInfo auxcr = {
1723 .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
1724 .access = PL1_RW, .type = ARM_CP_CONST,
1725 .resetvalue = cpu->reset_auxcr
1726 };
1727 define_one_arm_cp_reg(cpu, &auxcr);
1728 }
1729
1730 /* Generic registers whose values depend on the implementation */
1731 {
1732 ARMCPRegInfo sctlr = {
1733 .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
1734 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
Peter Maydelld4e6df62013-06-25 18:16:07 +01001735 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
1736 .raw_writefn = raw_write,
Peter Maydell2771db22012-06-20 11:57:18 +00001737 };
1738 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1739 /* Normally we would always end the TB on an SCTLR write, but Linux
1740 * arch/arm/mach-pxa/sleep.S expects two instructions following
1741 * an MMU enable to execute from cache. Imitate this behaviour.
1742 */
1743 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
1744 }
1745 define_one_arm_cp_reg(cpu, &sctlr);
1746 }
Peter Maydell2ceb98c2012-06-20 11:57:09 +00001747}
1748
Andreas Färber778c3a02012-04-20 07:39:14 +00001749ARMCPU *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +00001750{
Andreas Färberdec9c2d2012-03-29 04:50:31 +00001751 ARMCPU *cpu;
pbrook40f137e2006-02-20 00:33:36 +00001752 CPUARMState *env;
Andreas Färber5900d6b2013-01-21 16:11:43 +01001753 ObjectClass *oc;
pbrook40f137e2006-02-20 00:33:36 +00001754
Andreas Färber5900d6b2013-01-21 16:11:43 +01001755 oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1756 if (!oc) {
bellardaaed9092007-11-10 15:15:54 +00001757 return NULL;
Peter Maydell777dc782012-04-20 17:58:31 +00001758 }
Andreas Färber5900d6b2013-01-21 16:11:43 +01001759 cpu = ARM_CPU(object_new(object_class_get_name(oc)));
Andreas Färberdec9c2d2012-03-29 04:50:31 +00001760 env = &cpu->env;
Peter Maydell777dc782012-04-20 17:58:31 +00001761 env->cpu_model_str = cpu_model;
Andreas Färber14969262013-01-05 10:18:18 +01001762
1763 /* TODO this should be set centrally, once possible */
1764 object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
Peter Maydell777dc782012-04-20 17:58:31 +00001765
Andreas Färber14969262013-01-05 10:18:18 +01001766 return cpu;
1767}
1768
1769void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
1770{
Andreas Färber22169d42013-06-28 21:27:39 +02001771 CPUState *cs = CPU(cpu);
Andreas Färber14969262013-01-05 10:18:18 +01001772 CPUARMState *env = &cpu->env;
1773
pbrook56aebc82008-10-11 17:55:29 +00001774 if (arm_feature(env, ARM_FEATURE_NEON)) {
Andreas Färber22169d42013-06-28 21:27:39 +02001775 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00001776 51, "arm-neon.xml", 0);
1777 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
Andreas Färber22169d42013-06-28 21:27:39 +02001778 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00001779 35, "arm-vfp3.xml", 0);
1780 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
Andreas Färber22169d42013-06-28 21:27:39 +02001781 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00001782 19, "arm-vfp.xml", 0);
1783 }
pbrook40f137e2006-02-20 00:33:36 +00001784}
1785
Peter Maydell777dc782012-04-20 17:58:31 +00001786/* Sort alphabetically by type name, except for "any". */
1787static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
pbrook5adb4832007-03-08 03:15:18 +00001788{
Peter Maydell777dc782012-04-20 17:58:31 +00001789 ObjectClass *class_a = (ObjectClass *)a;
1790 ObjectClass *class_b = (ObjectClass *)b;
1791 const char *name_a, *name_b;
pbrook5adb4832007-03-08 03:15:18 +00001792
Peter Maydell777dc782012-04-20 17:58:31 +00001793 name_a = object_class_get_name(class_a);
1794 name_b = object_class_get_name(class_b);
Andreas Färber51492fd2013-01-27 17:30:10 +01001795 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00001796 return 1;
Andreas Färber51492fd2013-01-27 17:30:10 +01001797 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00001798 return -1;
1799 } else {
1800 return strcmp(name_a, name_b);
pbrook5adb4832007-03-08 03:15:18 +00001801 }
1802}
1803
Peter Maydell777dc782012-04-20 17:58:31 +00001804static void arm_cpu_list_entry(gpointer data, gpointer user_data)
pbrook40f137e2006-02-20 00:33:36 +00001805{
Peter Maydell777dc782012-04-20 17:58:31 +00001806 ObjectClass *oc = data;
Andreas Färber92a31362012-12-16 02:17:02 +01001807 CPUListState *s = user_data;
Andreas Färber51492fd2013-01-27 17:30:10 +01001808 const char *typename;
1809 char *name;
pbrook3371d272007-03-08 03:04:12 +00001810
Andreas Färber51492fd2013-01-27 17:30:10 +01001811 typename = object_class_get_name(oc);
1812 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
Peter Maydell777dc782012-04-20 17:58:31 +00001813 (*s->cpu_fprintf)(s->file, " %s\n",
Andreas Färber51492fd2013-01-27 17:30:10 +01001814 name);
1815 g_free(name);
Peter Maydell777dc782012-04-20 17:58:31 +00001816}
1817
1818void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1819{
Andreas Färber92a31362012-12-16 02:17:02 +01001820 CPUListState s = {
Peter Maydell777dc782012-04-20 17:58:31 +00001821 .file = f,
1822 .cpu_fprintf = cpu_fprintf,
1823 };
1824 GSList *list;
1825
1826 list = object_class_get_list(TYPE_ARM_CPU, false);
1827 list = g_slist_sort(list, arm_cpu_list_compare);
1828 (*cpu_fprintf)(f, "Available CPUs:\n");
1829 g_slist_foreach(list, arm_cpu_list_entry, &s);
1830 g_slist_free(list);
pbrook40f137e2006-02-20 00:33:36 +00001831}
1832
Cole Robinson78027bb2013-09-10 19:09:33 +01001833static void arm_cpu_add_definition(gpointer data, gpointer user_data)
1834{
1835 ObjectClass *oc = data;
1836 CpuDefinitionInfoList **cpu_list = user_data;
1837 CpuDefinitionInfoList *entry;
1838 CpuDefinitionInfo *info;
1839 const char *typename;
1840
1841 typename = object_class_get_name(oc);
1842 info = g_malloc0(sizeof(*info));
1843 info->name = g_strndup(typename,
1844 strlen(typename) - strlen("-" TYPE_ARM_CPU));
1845
1846 entry = g_malloc0(sizeof(*entry));
1847 entry->value = info;
1848 entry->next = *cpu_list;
1849 *cpu_list = entry;
1850}
1851
1852CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1853{
1854 CpuDefinitionInfoList *cpu_list = NULL;
1855 GSList *list;
1856
1857 list = object_class_get_list(TYPE_ARM_CPU, false);
1858 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
1859 g_slist_free(list);
1860
1861 return cpu_list;
1862}
1863
Peter Maydell4b6a83f2012-06-20 11:57:06 +00001864void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
1865 const ARMCPRegInfo *r, void *opaque)
1866{
1867 /* Define implementations of coprocessor registers.
1868 * We store these in a hashtable because typically
1869 * there are less than 150 registers in a space which
1870 * is 16*16*16*8*8 = 262144 in size.
1871 * Wildcarding is supported for the crm, opc1 and opc2 fields.
1872 * If a register is defined twice then the second definition is
1873 * used, so this can be used to define some generic registers and
1874 * then override them with implementation specific variations.
1875 * At least one of the original and the second definition should
1876 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
1877 * against accidental use.
1878 */
1879 int crm, opc1, opc2;
1880 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
1881 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
1882 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
1883 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
1884 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
1885 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
1886 /* 64 bit registers have only CRm and Opc1 fields */
1887 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
1888 /* Check that the register definition has enough info to handle
1889 * reads and writes if they are permitted.
1890 */
1891 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
1892 if (r->access & PL3_R) {
1893 assert(r->fieldoffset || r->readfn);
1894 }
1895 if (r->access & PL3_W) {
1896 assert(r->fieldoffset || r->writefn);
1897 }
1898 }
1899 /* Bad type field probably means missing sentinel at end of reg list */
1900 assert(cptype_valid(r->type));
1901 for (crm = crmmin; crm <= crmmax; crm++) {
1902 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
1903 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
1904 uint32_t *key = g_new(uint32_t, 1);
1905 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
1906 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
1907 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
Peter Crosthwaite204a9c42013-07-10 14:22:59 +10001908 if (opaque) {
1909 r2->opaque = opaque;
1910 }
Peter Maydell4b6a83f2012-06-20 11:57:06 +00001911 /* Make sure reginfo passed to helpers for wildcarded regs
1912 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
1913 */
1914 r2->crm = crm;
1915 r2->opc1 = opc1;
1916 r2->opc2 = opc2;
Peter Maydell7023ec72013-06-25 18:16:06 +01001917 /* By convention, for wildcarded registers only the first
1918 * entry is used for migration; the others are marked as
1919 * NO_MIGRATE so we don't try to transfer the register
1920 * multiple times. Special registers (ie NOP/WFI) are
1921 * never migratable.
1922 */
1923 if ((r->type & ARM_CP_SPECIAL) ||
1924 ((r->crm == CP_ANY) && crm != 0) ||
1925 ((r->opc1 == CP_ANY) && opc1 != 0) ||
1926 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1927 r2->type |= ARM_CP_NO_MIGRATE;
1928 }
1929
Peter Maydell4b6a83f2012-06-20 11:57:06 +00001930 /* Overriding of an existing definition must be explicitly
1931 * requested.
1932 */
1933 if (!(r->type & ARM_CP_OVERRIDE)) {
1934 ARMCPRegInfo *oldreg;
1935 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
1936 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
1937 fprintf(stderr, "Register redefined: cp=%d %d bit "
1938 "crn=%d crm=%d opc1=%d opc2=%d, "
1939 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
1940 r2->crn, r2->crm, r2->opc1, r2->opc2,
1941 oldreg->name, r2->name);
Stefan Weildfc6f862013-07-25 18:21:28 +02001942 g_assert_not_reached();
Peter Maydell4b6a83f2012-06-20 11:57:06 +00001943 }
1944 }
1945 g_hash_table_insert(cpu->cp_regs, key, r2);
1946 }
1947 }
1948 }
1949}
1950
1951void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1952 const ARMCPRegInfo *regs, void *opaque)
1953{
1954 /* Define a whole list of registers */
1955 const ARMCPRegInfo *r;
1956 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
1957 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
1958 }
1959}
1960
1961const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1962{
1963 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1964}
1965
1966int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1967 uint64_t value)
1968{
1969 /* Helper coprocessor write function for write-ignore registers */
1970 return 0;
1971}
1972
1973int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1974{
1975 /* Helper coprocessor write function for read-as-zero registers */
1976 *value = 0;
1977 return 0;
1978}
1979
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001980static int bad_mode_switch(CPUARMState *env, int mode)
Peter Maydell37064a82012-01-05 15:49:06 +00001981{
1982 /* Return true if it is not valid for us to switch to
1983 * this CPU mode (ie all the UNPREDICTABLE cases in
1984 * the ARM ARM CPSRWriteByInstr pseudocode).
1985 */
1986 switch (mode) {
1987 case ARM_CPU_MODE_USR:
1988 case ARM_CPU_MODE_SYS:
1989 case ARM_CPU_MODE_SVC:
1990 case ARM_CPU_MODE_ABT:
1991 case ARM_CPU_MODE_UND:
1992 case ARM_CPU_MODE_IRQ:
1993 case ARM_CPU_MODE_FIQ:
1994 return 0;
1995 default:
1996 return 1;
1997 }
1998}
1999
balrog2f4a40e2007-11-13 01:50:15 +00002000uint32_t cpsr_read(CPUARMState *env)
2001{
2002 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +00002003 ZF = (env->ZF == 0);
2004 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +00002005 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2006 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2007 | ((env->condexec_bits & 0xfc) << 8)
2008 | (env->GE << 16);
2009}
2010
2011void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2012{
balrog2f4a40e2007-11-13 01:50:15 +00002013 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +00002014 env->ZF = (~val) & CPSR_Z;
2015 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +00002016 env->CF = (val >> 29) & 1;
2017 env->VF = (val << 3) & 0x80000000;
2018 }
2019 if (mask & CPSR_Q)
2020 env->QF = ((val & CPSR_Q) != 0);
2021 if (mask & CPSR_T)
2022 env->thumb = ((val & CPSR_T) != 0);
2023 if (mask & CPSR_IT_0_1) {
2024 env->condexec_bits &= ~3;
2025 env->condexec_bits |= (val >> 25) & 3;
2026 }
2027 if (mask & CPSR_IT_2_7) {
2028 env->condexec_bits &= 3;
2029 env->condexec_bits |= (val >> 8) & 0xfc;
2030 }
2031 if (mask & CPSR_GE) {
2032 env->GE = (val >> 16) & 0xf;
2033 }
2034
2035 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
Peter Maydell37064a82012-01-05 15:49:06 +00002036 if (bad_mode_switch(env, val & CPSR_M)) {
2037 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2038 * We choose to ignore the attempt and leave the CPSR M field
2039 * untouched.
2040 */
2041 mask &= ~CPSR_M;
2042 } else {
2043 switch_mode(env, val & CPSR_M);
2044 }
balrog2f4a40e2007-11-13 01:50:15 +00002045 }
2046 mask &= ~CACHED_CPSR_BITS;
2047 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2048}
2049
pbrookb26eefb2008-03-31 03:44:26 +00002050/* Sign/zero extend */
2051uint32_t HELPER(sxtb16)(uint32_t x)
2052{
2053 uint32_t res;
2054 res = (uint16_t)(int8_t)x;
2055 res |= (uint32_t)(int8_t)(x >> 16) << 16;
2056 return res;
2057}
2058
2059uint32_t HELPER(uxtb16)(uint32_t x)
2060{
2061 uint32_t res;
2062 res = (uint16_t)(uint8_t)x;
2063 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
2064 return res;
2065}
2066
pbrookf51bbbf2008-03-31 03:45:13 +00002067uint32_t HELPER(clz)(uint32_t x)
2068{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02002069 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +00002070}
2071
pbrook36706692008-03-31 03:46:19 +00002072int32_t HELPER(sdiv)(int32_t num, int32_t den)
2073{
2074 if (den == 0)
2075 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +02002076 if (num == INT_MIN && den == -1)
2077 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +00002078 return num / den;
2079}
2080
2081uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
2082{
2083 if (den == 0)
2084 return 0;
2085 return num / den;
2086}
2087
2088uint32_t HELPER(rbit)(uint32_t x)
2089{
2090 x = ((x & 0xff000000) >> 24)
2091 | ((x & 0x00ff0000) >> 8)
2092 | ((x & 0x0000ff00) << 8)
2093 | ((x & 0x000000ff) << 24);
2094 x = ((x & 0xf0f0f0f0) >> 4)
2095 | ((x & 0x0f0f0f0f) << 4);
2096 x = ((x & 0x88888888) >> 3)
2097 | ((x & 0x44444444) >> 1)
2098 | ((x & 0x22222222) << 1)
2099 | ((x & 0x11111111) << 3);
2100 return x;
2101}
2102
ths5fafdf22007-09-16 21:08:06 +00002103#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +00002104
Andreas Färber97a8ea52013-02-02 10:57:51 +01002105void arm_cpu_do_interrupt(CPUState *cs)
bellardb5ff1b32005-11-26 10:38:39 +00002106{
Andreas Färber97a8ea52013-02-02 10:57:51 +01002107 ARMCPU *cpu = ARM_CPU(cs);
2108 CPUARMState *env = &cpu->env;
2109
bellardb5ff1b32005-11-26 10:38:39 +00002110 env->exception_index = -1;
2111}
2112
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002113int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +00002114 int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00002115{
2116 if (rw == 2) {
2117 env->exception_index = EXCP_PREFETCH_ABORT;
2118 env->cp15.c6_insn = address;
2119 } else {
2120 env->exception_index = EXCP_DATA_ABORT;
2121 env->cp15.c6_data = address;
2122 }
2123 return 1;
2124}
2125
pbrook9ee6e8b2007-11-11 00:04:49 +00002126/* These should probably raise undefined insn exceptions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002127void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002128{
2129 cpu_abort(env, "v7m_mrs %d\n", reg);
2130}
2131
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002132uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002133{
2134 cpu_abort(env, "v7m_mrs %d\n", reg);
2135 return 0;
2136}
2137
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002138void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00002139{
2140 if (mode != ARM_CPU_MODE_USR)
2141 cpu_abort(env, "Tried to switch out of user mode\n");
2142}
2143
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002144void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002145{
2146 cpu_abort(env, "banked r13 write\n");
2147}
2148
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002149uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002150{
2151 cpu_abort(env, "banked r13 read\n");
2152 return 0;
2153}
2154
bellardb5ff1b32005-11-26 10:38:39 +00002155#else
2156
2157/* Map CPU modes onto saved register banks. */
Christoffer Dall494b00c2013-03-05 00:34:41 +00002158int bank_number(int mode)
bellardb5ff1b32005-11-26 10:38:39 +00002159{
2160 switch (mode) {
2161 case ARM_CPU_MODE_USR:
2162 case ARM_CPU_MODE_SYS:
2163 return 0;
2164 case ARM_CPU_MODE_SVC:
2165 return 1;
2166 case ARM_CPU_MODE_ABT:
2167 return 2;
2168 case ARM_CPU_MODE_UND:
2169 return 3;
2170 case ARM_CPU_MODE_IRQ:
2171 return 4;
2172 case ARM_CPU_MODE_FIQ:
2173 return 5;
2174 }
Peter Maydellf5206412013-03-05 00:34:40 +00002175 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
bellardb5ff1b32005-11-26 10:38:39 +00002176}
2177
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002178void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00002179{
2180 int old_mode;
2181 int i;
2182
2183 old_mode = env->uncached_cpsr & CPSR_M;
2184 if (mode == old_mode)
2185 return;
2186
2187 if (old_mode == ARM_CPU_MODE_FIQ) {
2188 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00002189 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00002190 } else if (mode == ARM_CPU_MODE_FIQ) {
2191 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00002192 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00002193 }
2194
Peter Maydellf5206412013-03-05 00:34:40 +00002195 i = bank_number(old_mode);
bellardb5ff1b32005-11-26 10:38:39 +00002196 env->banked_r13[i] = env->regs[13];
2197 env->banked_r14[i] = env->regs[14];
2198 env->banked_spsr[i] = env->spsr;
2199
Peter Maydellf5206412013-03-05 00:34:40 +00002200 i = bank_number(mode);
bellardb5ff1b32005-11-26 10:38:39 +00002201 env->regs[13] = env->banked_r13[i];
2202 env->regs[14] = env->banked_r14[i];
2203 env->spsr = env->banked_spsr[i];
2204}
2205
pbrook9ee6e8b2007-11-11 00:04:49 +00002206static void v7m_push(CPUARMState *env, uint32_t val)
2207{
2208 env->regs[13] -= 4;
2209 stl_phys(env->regs[13], val);
2210}
2211
2212static uint32_t v7m_pop(CPUARMState *env)
2213{
2214 uint32_t val;
2215 val = ldl_phys(env->regs[13]);
2216 env->regs[13] += 4;
2217 return val;
2218}
2219
2220/* Switch to V7M main or process stack pointer. */
2221static void switch_v7m_sp(CPUARMState *env, int process)
2222{
2223 uint32_t tmp;
2224 if (env->v7m.current_sp != process) {
2225 tmp = env->v7m.other_sp;
2226 env->v7m.other_sp = env->regs[13];
2227 env->regs[13] = tmp;
2228 env->v7m.current_sp = process;
2229 }
2230}
2231
2232static void do_v7m_exception_exit(CPUARMState *env)
2233{
2234 uint32_t type;
2235 uint32_t xpsr;
2236
2237 type = env->regs[15];
2238 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +01002239 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +00002240
2241 /* Switch to the target stack. */
2242 switch_v7m_sp(env, (type & 4) != 0);
2243 /* Pop registers. */
2244 env->regs[0] = v7m_pop(env);
2245 env->regs[1] = v7m_pop(env);
2246 env->regs[2] = v7m_pop(env);
2247 env->regs[3] = v7m_pop(env);
2248 env->regs[12] = v7m_pop(env);
2249 env->regs[14] = v7m_pop(env);
2250 env->regs[15] = v7m_pop(env);
2251 xpsr = v7m_pop(env);
2252 xpsr_write(env, xpsr, 0xfffffdff);
2253 /* Undo stack alignment. */
2254 if (xpsr & 0x200)
2255 env->regs[13] |= 4;
2256 /* ??? The exception return type specifies Thread/Handler mode. However
2257 this is also implied by the xPSR value. Not sure what to do
2258 if there is a mismatch. */
2259 /* ??? Likewise for mismatches between the CONTROL register and the stack
2260 pointer. */
2261}
2262
Peter Maydell3f1beac2013-08-20 14:54:28 +01002263/* Exception names for debug logging; note that not all of these
2264 * precisely correspond to architectural exceptions.
2265 */
2266static const char * const excnames[] = {
2267 [EXCP_UDEF] = "Undefined Instruction",
2268 [EXCP_SWI] = "SVC",
2269 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
2270 [EXCP_DATA_ABORT] = "Data Abort",
2271 [EXCP_IRQ] = "IRQ",
2272 [EXCP_FIQ] = "FIQ",
2273 [EXCP_BKPT] = "Breakpoint",
2274 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
2275 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
2276 [EXCP_STREX] = "QEMU intercept of STREX",
2277};
2278
2279static inline void arm_log_exception(int idx)
2280{
2281 if (qemu_loglevel_mask(CPU_LOG_INT)) {
2282 const char *exc = NULL;
2283
2284 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
2285 exc = excnames[idx];
2286 }
2287 if (!exc) {
2288 exc = "unknown";
2289 }
2290 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
2291 }
2292}
2293
Andreas Färbere6f010c2013-02-02 12:33:14 +01002294void arm_v7m_cpu_do_interrupt(CPUState *cs)
pbrook9ee6e8b2007-11-11 00:04:49 +00002295{
Andreas Färbere6f010c2013-02-02 12:33:14 +01002296 ARMCPU *cpu = ARM_CPU(cs);
2297 CPUARMState *env = &cpu->env;
pbrook9ee6e8b2007-11-11 00:04:49 +00002298 uint32_t xpsr = xpsr_read(env);
2299 uint32_t lr;
2300 uint32_t addr;
2301
Peter Maydell3f1beac2013-08-20 14:54:28 +01002302 arm_log_exception(env->exception_index);
2303
pbrook9ee6e8b2007-11-11 00:04:49 +00002304 lr = 0xfffffff1;
2305 if (env->v7m.current_sp)
2306 lr |= 4;
2307 if (env->v7m.exception == 0)
2308 lr |= 8;
2309
2310 /* For exceptions we just mark as pending on the NVIC, and let that
2311 handle it. */
2312 /* TODO: Need to escalate if the current priority is higher than the
2313 one we're raising. */
2314 switch (env->exception_index) {
2315 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +01002316 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +00002317 return;
2318 case EXCP_SWI:
Alex_Rozenman@mentor.com314e2292013-01-11 15:21:22 +00002319 /* The PC already points to the next instruction. */
Paul Brook983fe822010-04-05 19:34:51 +01002320 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +00002321 return;
2322 case EXCP_PREFETCH_ABORT:
2323 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +01002324 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +00002325 return;
2326 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +00002327 if (semihosting_enabled) {
2328 int nr;
Blue Swirld31dd732012-09-04 20:25:59 +00002329 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
pbrook2ad207d2007-11-24 23:22:11 +00002330 if (nr == 0xab) {
2331 env->regs[15] += 2;
2332 env->regs[0] = do_arm_semihosting(env);
Peter Maydell3f1beac2013-08-20 14:54:28 +01002333 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
pbrook2ad207d2007-11-24 23:22:11 +00002334 return;
2335 }
2336 }
Paul Brook983fe822010-04-05 19:34:51 +01002337 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +00002338 return;
2339 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +01002340 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +00002341 break;
2342 case EXCP_EXCEPTION_EXIT:
2343 do_v7m_exception_exit(env);
2344 return;
2345 default:
2346 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2347 return; /* Never happens. Keep compiler happy. */
2348 }
2349
2350 /* Align stack pointer. */
2351 /* ??? Should only do this if Configuration Control Register
2352 STACKALIGN bit is set. */
2353 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +00002354 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +00002355 xpsr |= 0x200;
2356 }
balrog6c956762008-04-13 00:57:49 +00002357 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002358 v7m_push(env, xpsr);
2359 v7m_push(env, env->regs[15]);
2360 v7m_push(env, env->regs[14]);
2361 v7m_push(env, env->regs[12]);
2362 v7m_push(env, env->regs[3]);
2363 v7m_push(env, env->regs[2]);
2364 v7m_push(env, env->regs[1]);
2365 v7m_push(env, env->regs[0]);
2366 switch_v7m_sp(env, 0);
Peter Maydellc98d1742012-03-14 12:26:10 +00002367 /* Clear IT bits */
2368 env->condexec_bits = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002369 env->regs[14] = lr;
2370 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
2371 env->regs[15] = addr & 0xfffffffe;
2372 env->thumb = addr & 1;
2373}
2374
bellardb5ff1b32005-11-26 10:38:39 +00002375/* Handle a CPU exception. */
Andreas Färber97a8ea52013-02-02 10:57:51 +01002376void arm_cpu_do_interrupt(CPUState *cs)
bellardb5ff1b32005-11-26 10:38:39 +00002377{
Andreas Färber97a8ea52013-02-02 10:57:51 +01002378 ARMCPU *cpu = ARM_CPU(cs);
2379 CPUARMState *env = &cpu->env;
bellardb5ff1b32005-11-26 10:38:39 +00002380 uint32_t addr;
2381 uint32_t mask;
2382 int new_mode;
2383 uint32_t offset;
2384
Andreas Färbere6f010c2013-02-02 12:33:14 +01002385 assert(!IS_M(env));
2386
Peter Maydell3f1beac2013-08-20 14:54:28 +01002387 arm_log_exception(env->exception_index);
2388
bellardb5ff1b32005-11-26 10:38:39 +00002389 /* TODO: Vectored interrupt controller. */
2390 switch (env->exception_index) {
2391 case EXCP_UDEF:
2392 new_mode = ARM_CPU_MODE_UND;
2393 addr = 0x04;
2394 mask = CPSR_I;
2395 if (env->thumb)
2396 offset = 2;
2397 else
2398 offset = 4;
2399 break;
2400 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +00002401 if (semihosting_enabled) {
2402 /* Check for semihosting interrupt. */
2403 if (env->thumb) {
Blue Swirld31dd732012-09-04 20:25:59 +00002404 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
2405 & 0xff;
pbrook8e716212007-01-20 17:12:09 +00002406 } else {
Blue Swirld31dd732012-09-04 20:25:59 +00002407 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
Paul Brookd8fd2952012-03-30 18:02:50 +01002408 & 0xffffff;
pbrook8e716212007-01-20 17:12:09 +00002409 }
2410 /* Only intercept calls from privileged modes, to provide some
2411 semblance of security. */
2412 if (((mask == 0x123456 && !env->thumb)
2413 || (mask == 0xab && env->thumb))
2414 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2415 env->regs[0] = do_arm_semihosting(env);
Peter Maydell3f1beac2013-08-20 14:54:28 +01002416 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
pbrook8e716212007-01-20 17:12:09 +00002417 return;
2418 }
2419 }
bellardb5ff1b32005-11-26 10:38:39 +00002420 new_mode = ARM_CPU_MODE_SVC;
2421 addr = 0x08;
2422 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +00002423 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +00002424 offset = 0;
2425 break;
pbrook06c949e2006-02-04 19:35:26 +00002426 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +00002427 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +00002428 if (env->thumb && semihosting_enabled) {
Blue Swirld31dd732012-09-04 20:25:59 +00002429 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
pbrook9ee6e8b2007-11-11 00:04:49 +00002430 if (mask == 0xab
2431 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2432 env->regs[15] += 2;
2433 env->regs[0] = do_arm_semihosting(env);
Peter Maydell3f1beac2013-08-20 14:54:28 +01002434 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
pbrook9ee6e8b2007-11-11 00:04:49 +00002435 return;
2436 }
2437 }
Alex Zuepke81c05da2011-06-03 18:42:17 +02002438 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +00002439 /* Fall through to prefetch abort. */
2440 case EXCP_PREFETCH_ABORT:
Peter Maydell3f1beac2013-08-20 14:54:28 +01002441 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
2442 env->cp15.c5_insn, env->cp15.c6_insn);
bellardb5ff1b32005-11-26 10:38:39 +00002443 new_mode = ARM_CPU_MODE_ABT;
2444 addr = 0x0c;
2445 mask = CPSR_A | CPSR_I;
2446 offset = 4;
2447 break;
2448 case EXCP_DATA_ABORT:
Peter Maydell3f1beac2013-08-20 14:54:28 +01002449 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
2450 env->cp15.c5_data, env->cp15.c6_data);
bellardb5ff1b32005-11-26 10:38:39 +00002451 new_mode = ARM_CPU_MODE_ABT;
2452 addr = 0x10;
2453 mask = CPSR_A | CPSR_I;
2454 offset = 8;
2455 break;
2456 case EXCP_IRQ:
2457 new_mode = ARM_CPU_MODE_IRQ;
2458 addr = 0x18;
2459 /* Disable IRQ and imprecise data aborts. */
2460 mask = CPSR_A | CPSR_I;
2461 offset = 4;
2462 break;
2463 case EXCP_FIQ:
2464 new_mode = ARM_CPU_MODE_FIQ;
2465 addr = 0x1c;
2466 /* Disable FIQ, IRQ and imprecise data aborts. */
2467 mask = CPSR_A | CPSR_I | CPSR_F;
2468 offset = 4;
2469 break;
2470 default:
2471 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2472 return; /* Never happens. Keep compiler happy. */
2473 }
2474 /* High vectors. */
2475 if (env->cp15.c1_sys & (1 << 13)) {
2476 addr += 0xffff0000;
2477 }
2478 switch_mode (env, new_mode);
2479 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +00002480 /* Clear IT bits. */
2481 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +05302482 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +00002483 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +00002484 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +04002485 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
2486 * and we should just guard the thumb mode on V4 */
2487 if (arm_feature(env, ARM_FEATURE_V4T)) {
2488 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
2489 }
bellardb5ff1b32005-11-26 10:38:39 +00002490 env->regs[14] = env->regs[15] + offset;
2491 env->regs[15] = addr;
Andreas Färber259186a2013-01-17 18:51:17 +01002492 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
bellardb5ff1b32005-11-26 10:38:39 +00002493}
2494
2495/* Check section/page access permissions.
2496 Returns the page protection flags, or zero if the access is not
2497 permitted. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002498static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002499 int access_type, int is_user)
bellardb5ff1b32005-11-26 10:38:39 +00002500{
pbrook9ee6e8b2007-11-11 00:04:49 +00002501 int prot_ro;
2502
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002503 if (domain_prot == 3) {
bellardb5ff1b32005-11-26 10:38:39 +00002504 return PAGE_READ | PAGE_WRITE;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002505 }
bellardb5ff1b32005-11-26 10:38:39 +00002506
pbrook9ee6e8b2007-11-11 00:04:49 +00002507 if (access_type == 1)
2508 prot_ro = 0;
2509 else
2510 prot_ro = PAGE_READ;
2511
bellardb5ff1b32005-11-26 10:38:39 +00002512 switch (ap) {
2513 case 0:
pbrook78600322006-09-09 14:36:26 +00002514 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +00002515 return 0;
2516 switch ((env->cp15.c1_sys >> 8) & 3) {
2517 case 1:
2518 return is_user ? 0 : PAGE_READ;
2519 case 2:
2520 return PAGE_READ;
2521 default:
2522 return 0;
2523 }
2524 case 1:
2525 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
2526 case 2:
2527 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +00002528 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00002529 else
2530 return PAGE_READ | PAGE_WRITE;
2531 case 3:
2532 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +00002533 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002534 return 0;
2535 case 5:
2536 return is_user ? 0 : prot_ro;
2537 case 6:
2538 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +00002539 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +00002540 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +00002541 return 0;
2542 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00002543 default:
2544 abort();
2545 }
2546}
2547
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002548static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
pbrookb2fa1792008-10-22 19:22:30 +00002549{
2550 uint32_t table;
2551
2552 if (address & env->cp15.c2_mask)
2553 table = env->cp15.c2_base1 & 0xffffc000;
2554 else
2555 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
2556
2557 table |= (address >> 18) & 0x3ffc;
2558 return table;
2559}
2560
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002561static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
Avi Kivitya8170e52012-10-23 12:30:10 +02002562 int is_user, hwaddr *phys_ptr,
Peter Maydell77a71dd2012-07-12 10:59:09 +00002563 int *prot, target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +00002564{
2565 int code;
2566 uint32_t table;
2567 uint32_t desc;
2568 int type;
2569 int ap;
2570 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002571 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +02002572 hwaddr phys_addr;
bellardb5ff1b32005-11-26 10:38:39 +00002573
pbrook9ee6e8b2007-11-11 00:04:49 +00002574 /* Pagetable walk. */
2575 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00002576 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00002577 desc = ldl_phys(table);
2578 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002579 domain = (desc >> 5) & 0x0f;
2580 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +00002581 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00002582 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002583 code = 5;
2584 goto do_fault;
2585 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002586 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002587 if (type == 2)
2588 code = 9; /* Section domain fault. */
2589 else
2590 code = 11; /* Page domain fault. */
2591 goto do_fault;
2592 }
2593 if (type == 2) {
2594 /* 1Mb section. */
2595 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2596 ap = (desc >> 10) & 3;
2597 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00002598 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00002599 } else {
2600 /* Lookup l2 entry. */
2601 if (type == 1) {
2602 /* Coarse pagetable. */
2603 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2604 } else {
2605 /* Fine pagetable. */
2606 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
2607 }
2608 desc = ldl_phys(table);
2609 switch (desc & 3) {
2610 case 0: /* Page translation fault. */
2611 code = 7;
2612 goto do_fault;
2613 case 1: /* 64k page. */
2614 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2615 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00002616 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002617 break;
2618 case 2: /* 4k page. */
2619 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2620 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00002621 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002622 break;
2623 case 3: /* 1k page. */
2624 if (type == 1) {
2625 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2626 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2627 } else {
2628 /* Page translation fault. */
2629 code = 7;
2630 goto do_fault;
2631 }
2632 } else {
2633 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
2634 }
2635 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00002636 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +00002637 break;
2638 default:
2639 /* Never happens, but compiler isn't smart enough to tell. */
2640 abort();
2641 }
2642 code = 15;
2643 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002644 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
pbrook9ee6e8b2007-11-11 00:04:49 +00002645 if (!*prot) {
2646 /* Access permission fault. */
2647 goto do_fault;
2648 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05302649 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00002650 *phys_ptr = phys_addr;
2651 return 0;
2652do_fault:
2653 return code | (domain << 4);
2654}
2655
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002656static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
Avi Kivitya8170e52012-10-23 12:30:10 +02002657 int is_user, hwaddr *phys_ptr,
Peter Maydell77a71dd2012-07-12 10:59:09 +00002658 int *prot, target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00002659{
2660 int code;
2661 uint32_t table;
2662 uint32_t desc;
2663 uint32_t xn;
Peter Maydellde9b05b2012-07-12 10:59:05 +00002664 uint32_t pxn = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002665 int type;
2666 int ap;
Peter Maydellde9b05b2012-07-12 10:59:05 +00002667 int domain = 0;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002668 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +02002669 hwaddr phys_addr;
pbrook9ee6e8b2007-11-11 00:04:49 +00002670
2671 /* Pagetable walk. */
2672 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00002673 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00002674 desc = ldl_phys(table);
2675 type = (desc & 3);
Peter Maydellde9b05b2012-07-12 10:59:05 +00002676 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
2677 /* Section translation fault, or attempt to use the encoding
2678 * which is Reserved on implementations without PXN.
2679 */
pbrook9ee6e8b2007-11-11 00:04:49 +00002680 code = 5;
pbrook9ee6e8b2007-11-11 00:04:49 +00002681 goto do_fault;
Peter Maydellde9b05b2012-07-12 10:59:05 +00002682 }
2683 if ((type == 1) || !(desc & (1 << 18))) {
2684 /* Page or Section. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002685 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +00002686 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002687 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2688 if (domain_prot == 0 || domain_prot == 2) {
Peter Maydellde9b05b2012-07-12 10:59:05 +00002689 if (type != 1) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002690 code = 9; /* Section domain fault. */
Peter Maydellde9b05b2012-07-12 10:59:05 +00002691 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00002692 code = 11; /* Page domain fault. */
Peter Maydellde9b05b2012-07-12 10:59:05 +00002693 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002694 goto do_fault;
2695 }
Peter Maydellde9b05b2012-07-12 10:59:05 +00002696 if (type != 1) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002697 if (desc & (1 << 18)) {
2698 /* Supersection. */
2699 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00002700 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002701 } else {
2702 /* Section. */
2703 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00002704 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002705 }
2706 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
2707 xn = desc & (1 << 4);
Peter Maydellde9b05b2012-07-12 10:59:05 +00002708 pxn = desc & 1;
pbrook9ee6e8b2007-11-11 00:04:49 +00002709 code = 13;
2710 } else {
Peter Maydellde9b05b2012-07-12 10:59:05 +00002711 if (arm_feature(env, ARM_FEATURE_PXN)) {
2712 pxn = (desc >> 2) & 1;
2713 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002714 /* Lookup l2 entry. */
2715 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2716 desc = ldl_phys(table);
2717 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
2718 switch (desc & 3) {
2719 case 0: /* Page translation fault. */
2720 code = 7;
2721 goto do_fault;
2722 case 1: /* 64k page. */
2723 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2724 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00002725 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002726 break;
2727 case 2: case 3: /* 4k page. */
2728 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2729 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00002730 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00002731 break;
2732 default:
2733 /* Never happens, but compiler isn't smart enough to tell. */
2734 abort();
2735 }
2736 code = 15;
2737 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002738 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02002739 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2740 } else {
Peter Maydellde9b05b2012-07-12 10:59:05 +00002741 if (pxn && !is_user) {
2742 xn = 1;
2743 }
Juha Riihimäkic0034322010-12-08 13:15:16 +02002744 if (xn && access_type == 2)
2745 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00002746
Juha Riihimäkic0034322010-12-08 13:15:16 +02002747 /* The simplified model uses AP[0] as an access control bit. */
2748 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
2749 /* Access flag fault. */
2750 code = (code == 15) ? 6 : 3;
2751 goto do_fault;
2752 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00002753 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
Juha Riihimäkic0034322010-12-08 13:15:16 +02002754 if (!*prot) {
2755 /* Access permission fault. */
2756 goto do_fault;
2757 }
2758 if (!xn) {
2759 *prot |= PAGE_EXEC;
2760 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05302761 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002762 *phys_ptr = phys_addr;
2763 return 0;
2764do_fault:
2765 return code | (domain << 4);
2766}
2767
Peter Maydell3dde9622012-07-12 10:59:12 +00002768/* Fault type for long-descriptor MMU fault reporting; this corresponds
2769 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
2770 */
2771typedef enum {
2772 translation_fault = 1,
2773 access_fault = 2,
2774 permission_fault = 3,
2775} MMUFaultType;
2776
2777static int get_phys_addr_lpae(CPUARMState *env, uint32_t address,
2778 int access_type, int is_user,
Avi Kivitya8170e52012-10-23 12:30:10 +02002779 hwaddr *phys_ptr, int *prot,
Peter Maydell3dde9622012-07-12 10:59:12 +00002780 target_ulong *page_size_ptr)
2781{
2782 /* Read an LPAE long-descriptor translation table. */
2783 MMUFaultType fault_type = translation_fault;
2784 uint32_t level = 1;
2785 uint32_t epd;
2786 uint32_t tsz;
2787 uint64_t ttbr;
2788 int ttbr_select;
2789 int n;
Avi Kivitya8170e52012-10-23 12:30:10 +02002790 hwaddr descaddr;
Peter Maydell3dde9622012-07-12 10:59:12 +00002791 uint32_t tableattrs;
2792 target_ulong page_size;
2793 uint32_t attrs;
2794
2795 /* Determine whether this address is in the region controlled by
2796 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
2797 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
2798 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
2799 */
2800 uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3);
2801 uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3);
2802 if (t0sz && !extract32(address, 32 - t0sz, t0sz)) {
2803 /* there is a ttbr0 region and we are in it (high bits all zero) */
2804 ttbr_select = 0;
2805 } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) {
2806 /* there is a ttbr1 region and we are in it (high bits all one) */
2807 ttbr_select = 1;
2808 } else if (!t0sz) {
2809 /* ttbr0 region is "everything not in the ttbr1 region" */
2810 ttbr_select = 0;
2811 } else if (!t1sz) {
2812 /* ttbr1 region is "everything not in the ttbr0 region" */
2813 ttbr_select = 1;
2814 } else {
2815 /* in the gap between the two regions, this is a Translation fault */
2816 fault_type = translation_fault;
2817 goto do_fault;
2818 }
2819
2820 /* Note that QEMU ignores shareability and cacheability attributes,
2821 * so we don't need to do anything with the SH, ORGN, IRGN fields
2822 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
2823 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
2824 * implement any ASID-like capability so we can ignore it (instead
2825 * we will always flush the TLB any time the ASID is changed).
2826 */
2827 if (ttbr_select == 0) {
2828 ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
2829 epd = extract32(env->cp15.c2_control, 7, 1);
2830 tsz = t0sz;
2831 } else {
2832 ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
2833 epd = extract32(env->cp15.c2_control, 23, 1);
2834 tsz = t1sz;
2835 }
2836
2837 if (epd) {
2838 /* Translation table walk disabled => Translation fault on TLB miss */
2839 goto do_fault;
2840 }
2841
2842 /* If the region is small enough we will skip straight to a 2nd level
2843 * lookup. This affects the number of bits of the address used in
2844 * combination with the TTBR to find the first descriptor. ('n' here
2845 * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
2846 * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
2847 */
2848 if (tsz > 1) {
2849 level = 2;
2850 n = 14 - tsz;
2851 } else {
2852 n = 5 - tsz;
2853 }
2854
2855 /* Clear the vaddr bits which aren't part of the within-region address,
2856 * so that we don't have to special case things when calculating the
2857 * first descriptor address.
2858 */
2859 address &= (0xffffffffU >> tsz);
2860
2861 /* Now we can extract the actual base address from the TTBR */
2862 descaddr = extract64(ttbr, 0, 40);
2863 descaddr &= ~((1ULL << n) - 1);
2864
2865 tableattrs = 0;
2866 for (;;) {
2867 uint64_t descriptor;
2868
2869 descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
2870 descriptor = ldq_phys(descaddr);
2871 if (!(descriptor & 1) ||
2872 (!(descriptor & 2) && (level == 3))) {
2873 /* Invalid, or the Reserved level 3 encoding */
2874 goto do_fault;
2875 }
2876 descaddr = descriptor & 0xfffffff000ULL;
2877
2878 if ((descriptor & 2) && (level < 3)) {
2879 /* Table entry. The top five bits are attributes which may
2880 * propagate down through lower levels of the table (and
2881 * which are all arranged so that 0 means "no effect", so
2882 * we can gather them up by ORing in the bits at each level).
2883 */
2884 tableattrs |= extract64(descriptor, 59, 5);
2885 level++;
2886 continue;
2887 }
2888 /* Block entry at level 1 or 2, or page entry at level 3.
2889 * These are basically the same thing, although the number
2890 * of bits we pull in from the vaddr varies.
2891 */
2892 page_size = (1 << (39 - (9 * level)));
2893 descaddr |= (address & (page_size - 1));
2894 /* Extract attributes from the descriptor and merge with table attrs */
2895 attrs = extract64(descriptor, 2, 10)
2896 | (extract64(descriptor, 52, 12) << 10);
2897 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
2898 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
2899 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
2900 * means "force PL1 access only", which means forcing AP[1] to 0.
2901 */
2902 if (extract32(tableattrs, 2, 1)) {
2903 attrs &= ~(1 << 4);
2904 }
2905 /* Since we're always in the Non-secure state, NSTable is ignored. */
2906 break;
2907 }
2908 /* Here descaddr is the final physical address, and attributes
2909 * are all in attrs.
2910 */
2911 fault_type = access_fault;
2912 if ((attrs & (1 << 8)) == 0) {
2913 /* Access flag */
2914 goto do_fault;
2915 }
2916 fault_type = permission_fault;
2917 if (is_user && !(attrs & (1 << 4))) {
2918 /* Unprivileged access not enabled */
2919 goto do_fault;
2920 }
2921 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2922 if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) {
2923 /* XN or PXN */
2924 if (access_type == 2) {
2925 goto do_fault;
2926 }
2927 *prot &= ~PAGE_EXEC;
2928 }
2929 if (attrs & (1 << 5)) {
2930 /* Write access forbidden */
2931 if (access_type == 1) {
2932 goto do_fault;
2933 }
2934 *prot &= ~PAGE_WRITE;
2935 }
2936
2937 *phys_ptr = descaddr;
2938 *page_size_ptr = page_size;
2939 return 0;
2940
2941do_fault:
2942 /* Long-descriptor format IFSR/DFSR value */
2943 return (1 << 9) | (fault_type << 2) | level;
2944}
2945
Peter Maydell77a71dd2012-07-12 10:59:09 +00002946static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
2947 int access_type, int is_user,
Avi Kivitya8170e52012-10-23 12:30:10 +02002948 hwaddr *phys_ptr, int *prot)
pbrook9ee6e8b2007-11-11 00:04:49 +00002949{
2950 int n;
2951 uint32_t mask;
2952 uint32_t base;
2953
2954 *phys_ptr = address;
2955 for (n = 7; n >= 0; n--) {
2956 base = env->cp15.c6_region[n];
2957 if ((base & 1) == 0)
2958 continue;
2959 mask = 1 << ((base >> 1) & 0x1f);
2960 /* Keep this shift separate from the above to avoid an
2961 (undefined) << 32. */
2962 mask = (mask << 1) - 1;
2963 if (((base ^ address) & ~mask) == 0)
2964 break;
2965 }
2966 if (n < 0)
2967 return 2;
2968
2969 if (access_type == 2) {
2970 mask = env->cp15.c5_insn;
2971 } else {
2972 mask = env->cp15.c5_data;
2973 }
2974 mask = (mask >> (n * 4)) & 0xf;
2975 switch (mask) {
2976 case 0:
2977 return 1;
2978 case 1:
2979 if (is_user)
2980 return 1;
2981 *prot = PAGE_READ | PAGE_WRITE;
2982 break;
2983 case 2:
2984 *prot = PAGE_READ;
2985 if (!is_user)
2986 *prot |= PAGE_WRITE;
2987 break;
2988 case 3:
2989 *prot = PAGE_READ | PAGE_WRITE;
2990 break;
2991 case 5:
2992 if (is_user)
2993 return 1;
2994 *prot = PAGE_READ;
2995 break;
2996 case 6:
2997 *prot = PAGE_READ;
2998 break;
2999 default:
3000 /* Bad permission. */
3001 return 1;
3002 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05303003 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00003004 return 0;
3005}
3006
Peter Maydell702a9352012-07-12 10:59:10 +00003007/* get_phys_addr - get the physical address for this virtual address
3008 *
3009 * Find the physical address corresponding to the given virtual address,
3010 * by doing a translation table walk on MMU based systems or using the
3011 * MPU state on MPU based systems.
3012 *
3013 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
3014 * prot and page_size are not filled in, and the return value provides
3015 * information on why the translation aborted, in the format of a
3016 * DFSR/IFSR fault register, with the following caveats:
3017 * * we honour the short vs long DFSR format differences.
3018 * * the WnR bit is never set (the caller must do this).
3019 * * for MPU based systems we don't bother to return a full FSR format
3020 * value.
3021 *
3022 * @env: CPUARMState
3023 * @address: virtual address to get physical address for
3024 * @access_type: 0 for read, 1 for write, 2 for execute
3025 * @is_user: 0 for privileged access, 1 for user
3026 * @phys_ptr: set to the physical address corresponding to the virtual address
3027 * @prot: set to the permissions for the page containing phys_ptr
3028 * @page_size: set to the size of the page containing phys_ptr
3029 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003030static inline int get_phys_addr(CPUARMState *env, uint32_t address,
pbrook9ee6e8b2007-11-11 00:04:49 +00003031 int access_type, int is_user,
Avi Kivitya8170e52012-10-23 12:30:10 +02003032 hwaddr *phys_ptr, int *prot,
Paul Brookd4c430a2010-03-17 02:14:28 +00003033 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00003034{
bellardb5ff1b32005-11-26 10:38:39 +00003035 /* Fast Context Switch Extension. */
3036 if (address < 0x02000000)
3037 address += env->cp15.c13_fcse;
3038
3039 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00003040 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00003041 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05303042 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00003043 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00003044 return 0;
pbrookce819862007-05-08 02:30:40 +00003045 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00003046 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00003047 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
3048 prot);
Peter Maydell3dde9622012-07-12 10:59:12 +00003049 } else if (extended_addresses_enabled(env)) {
3050 return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
3051 prot, page_size);
pbrook9ee6e8b2007-11-11 00:04:49 +00003052 } else if (env->cp15.c1_sys & (1 << 23)) {
3053 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00003054 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003055 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00003056 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00003057 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003058 }
bellardb5ff1b32005-11-26 10:38:39 +00003059}
3060
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003061int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
Blue Swirl97b348e2011-08-01 16:12:17 +00003062 int access_type, int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00003063{
Avi Kivitya8170e52012-10-23 12:30:10 +02003064 hwaddr phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00003065 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00003066 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00003067 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00003068
j_mayer6ebbf392007-10-14 07:07:08 +00003069 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00003070 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
3071 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003072 if (ret == 0) {
3073 /* Map a single [sub]page. */
Avi Kivitya8170e52012-10-23 12:30:10 +02003074 phys_addr &= ~(hwaddr)0x3ff;
bellardb5ff1b32005-11-26 10:38:39 +00003075 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05303076 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00003077 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00003078 }
3079
3080 if (access_type == 2) {
3081 env->cp15.c5_insn = ret;
3082 env->cp15.c6_insn = address;
3083 env->exception_index = EXCP_PREFETCH_ABORT;
3084 } else {
3085 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00003086 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
3087 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00003088 env->cp15.c6_data = address;
3089 env->exception_index = EXCP_DATA_ABORT;
3090 }
3091 return 1;
3092}
3093
Andreas Färber00b941e2013-06-29 18:55:54 +02003094hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
bellardb5ff1b32005-11-26 10:38:39 +00003095{
Andreas Färber00b941e2013-06-29 18:55:54 +02003096 ARMCPU *cpu = ARM_CPU(cs);
Avi Kivitya8170e52012-10-23 12:30:10 +02003097 hwaddr phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00003098 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00003099 int prot;
3100 int ret;
3101
Andreas Färber00b941e2013-06-29 18:55:54 +02003102 ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00003103
Andreas Färber00b941e2013-06-29 18:55:54 +02003104 if (ret != 0) {
bellardb5ff1b32005-11-26 10:38:39 +00003105 return -1;
Andreas Färber00b941e2013-06-29 18:55:54 +02003106 }
bellardb5ff1b32005-11-26 10:38:39 +00003107
3108 return phys_addr;
3109}
3110
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003111void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00003112{
Peter Maydell39ea3d42011-01-14 20:39:18 +01003113 if ((env->uncached_cpsr & CPSR_M) == mode) {
3114 env->regs[13] = val;
3115 } else {
Peter Maydellf5206412013-03-05 00:34:40 +00003116 env->banked_r13[bank_number(mode)] = val;
Peter Maydell39ea3d42011-01-14 20:39:18 +01003117 }
pbrook9ee6e8b2007-11-11 00:04:49 +00003118}
3119
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003120uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00003121{
Peter Maydell39ea3d42011-01-14 20:39:18 +01003122 if ((env->uncached_cpsr & CPSR_M) == mode) {
3123 return env->regs[13];
3124 } else {
Peter Maydellf5206412013-03-05 00:34:40 +00003125 return env->banked_r13[bank_number(mode)];
Peter Maydell39ea3d42011-01-14 20:39:18 +01003126 }
pbrook9ee6e8b2007-11-11 00:04:49 +00003127}
3128
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003129uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00003130{
3131 switch (reg) {
3132 case 0: /* APSR */
3133 return xpsr_read(env) & 0xf8000000;
3134 case 1: /* IAPSR */
3135 return xpsr_read(env) & 0xf80001ff;
3136 case 2: /* EAPSR */
3137 return xpsr_read(env) & 0xff00fc00;
3138 case 3: /* xPSR */
3139 return xpsr_read(env) & 0xff00fdff;
3140 case 5: /* IPSR */
3141 return xpsr_read(env) & 0x000001ff;
3142 case 6: /* EPSR */
3143 return xpsr_read(env) & 0x0700fc00;
3144 case 7: /* IEPSR */
3145 return xpsr_read(env) & 0x0700edff;
3146 case 8: /* MSP */
3147 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
3148 case 9: /* PSP */
3149 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
3150 case 16: /* PRIMASK */
3151 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00003152 case 17: /* BASEPRI */
3153 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00003154 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00003155 case 19: /* FAULTMASK */
3156 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00003157 case 20: /* CONTROL */
3158 return env->v7m.control;
3159 default:
3160 /* ??? For debugging only. */
3161 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
3162 return 0;
3163 }
3164}
3165
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003166void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00003167{
3168 switch (reg) {
3169 case 0: /* APSR */
3170 xpsr_write(env, val, 0xf8000000);
3171 break;
3172 case 1: /* IAPSR */
3173 xpsr_write(env, val, 0xf8000000);
3174 break;
3175 case 2: /* EAPSR */
3176 xpsr_write(env, val, 0xfe00fc00);
3177 break;
3178 case 3: /* xPSR */
3179 xpsr_write(env, val, 0xfe00fc00);
3180 break;
3181 case 5: /* IPSR */
3182 /* IPSR bits are readonly. */
3183 break;
3184 case 6: /* EPSR */
3185 xpsr_write(env, val, 0x0600fc00);
3186 break;
3187 case 7: /* IEPSR */
3188 xpsr_write(env, val, 0x0600fc00);
3189 break;
3190 case 8: /* MSP */
3191 if (env->v7m.current_sp)
3192 env->v7m.other_sp = val;
3193 else
3194 env->regs[13] = val;
3195 break;
3196 case 9: /* PSP */
3197 if (env->v7m.current_sp)
3198 env->regs[13] = val;
3199 else
3200 env->v7m.other_sp = val;
3201 break;
3202 case 16: /* PRIMASK */
3203 if (val & 1)
3204 env->uncached_cpsr |= CPSR_I;
3205 else
3206 env->uncached_cpsr &= ~CPSR_I;
3207 break;
Sebastian Huber82845822011-05-29 02:58:41 +00003208 case 17: /* BASEPRI */
3209 env->v7m.basepri = val & 0xff;
3210 break;
3211 case 18: /* BASEPRI_MAX */
3212 val &= 0xff;
3213 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
3214 env->v7m.basepri = val;
3215 break;
3216 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00003217 if (val & 1)
3218 env->uncached_cpsr |= CPSR_F;
3219 else
3220 env->uncached_cpsr &= ~CPSR_F;
3221 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00003222 case 20: /* CONTROL */
3223 env->v7m.control = val & 3;
3224 switch_v7m_sp(env, (val & 2) != 0);
3225 break;
3226 default:
3227 /* ??? For debugging only. */
3228 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
3229 return;
3230 }
3231}
3232
bellardb5ff1b32005-11-26 10:38:39 +00003233#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00003234
3235/* Note that signed overflow is undefined in C. The following routines are
3236 careful to use unsigned types where modulo arithmetic is required.
3237 Failure to do so _will_ break on newer gcc. */
3238
3239/* Signed saturating arithmetic. */
3240
aurel321654b2d2008-04-11 04:55:07 +00003241/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003242static inline uint16_t add16_sat(uint16_t a, uint16_t b)
3243{
3244 uint16_t res;
3245
3246 res = a + b;
3247 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
3248 if (a & 0x8000)
3249 res = 0x8000;
3250 else
3251 res = 0x7fff;
3252 }
3253 return res;
3254}
3255
aurel321654b2d2008-04-11 04:55:07 +00003256/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003257static inline uint8_t add8_sat(uint8_t a, uint8_t b)
3258{
3259 uint8_t res;
3260
3261 res = a + b;
3262 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
3263 if (a & 0x80)
3264 res = 0x80;
3265 else
3266 res = 0x7f;
3267 }
3268 return res;
3269}
3270
aurel321654b2d2008-04-11 04:55:07 +00003271/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003272static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
3273{
3274 uint16_t res;
3275
3276 res = a - b;
3277 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
3278 if (a & 0x8000)
3279 res = 0x8000;
3280 else
3281 res = 0x7fff;
3282 }
3283 return res;
3284}
3285
aurel321654b2d2008-04-11 04:55:07 +00003286/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00003287static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
3288{
3289 uint8_t res;
3290
3291 res = a - b;
3292 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
3293 if (a & 0x80)
3294 res = 0x80;
3295 else
3296 res = 0x7f;
3297 }
3298 return res;
3299}
3300
3301#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3302#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3303#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
3304#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
3305#define PFX q
3306
3307#include "op_addsub.h"
3308
3309/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00003310static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003311{
3312 uint16_t res;
3313 res = a + b;
3314 if (res < a)
3315 res = 0xffff;
3316 return res;
3317}
3318
pbrook460a09c2008-05-01 12:04:35 +00003319static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003320{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08003321 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003322 return a - b;
3323 else
3324 return 0;
3325}
3326
3327static inline uint8_t add8_usat(uint8_t a, uint8_t b)
3328{
3329 uint8_t res;
3330 res = a + b;
3331 if (res < a)
3332 res = 0xff;
3333 return res;
3334}
3335
3336static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
3337{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08003338 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00003339 return a - b;
3340 else
3341 return 0;
3342}
3343
3344#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3345#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3346#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
3347#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
3348#define PFX uq
3349
3350#include "op_addsub.h"
3351
3352/* Signed modulo arithmetic. */
3353#define SARITH16(a, b, n, op) do { \
3354 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00003355 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00003356 RESULT(sum, n, 16); \
3357 if (sum >= 0) \
3358 ge |= 3 << (n * 2); \
3359 } while(0)
3360
3361#define SARITH8(a, b, n, op) do { \
3362 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00003363 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00003364 RESULT(sum, n, 8); \
3365 if (sum >= 0) \
3366 ge |= 1 << n; \
3367 } while(0)
3368
3369
3370#define ADD16(a, b, n) SARITH16(a, b, n, +)
3371#define SUB16(a, b, n) SARITH16(a, b, n, -)
3372#define ADD8(a, b, n) SARITH8(a, b, n, +)
3373#define SUB8(a, b, n) SARITH8(a, b, n, -)
3374#define PFX s
3375#define ARITH_GE
3376
3377#include "op_addsub.h"
3378
3379/* Unsigned modulo arithmetic. */
3380#define ADD16(a, b, n) do { \
3381 uint32_t sum; \
3382 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3383 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00003384 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00003385 ge |= 3 << (n * 2); \
3386 } while(0)
3387
3388#define ADD8(a, b, n) do { \
3389 uint32_t sum; \
3390 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
3391 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00003392 if ((sum >> 8) == 1) \
3393 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00003394 } while(0)
3395
3396#define SUB16(a, b, n) do { \
3397 uint32_t sum; \
3398 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
3399 RESULT(sum, n, 16); \
3400 if ((sum >> 16) == 0) \
3401 ge |= 3 << (n * 2); \
3402 } while(0)
3403
3404#define SUB8(a, b, n) do { \
3405 uint32_t sum; \
3406 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
3407 RESULT(sum, n, 8); \
3408 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00003409 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00003410 } while(0)
3411
3412#define PFX u
3413#define ARITH_GE
3414
3415#include "op_addsub.h"
3416
3417/* Halved signed arithmetic. */
3418#define ADD16(a, b, n) \
3419 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
3420#define SUB16(a, b, n) \
3421 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
3422#define ADD8(a, b, n) \
3423 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
3424#define SUB8(a, b, n) \
3425 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
3426#define PFX sh
3427
3428#include "op_addsub.h"
3429
3430/* Halved unsigned arithmetic. */
3431#define ADD16(a, b, n) \
3432 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3433#define SUB16(a, b, n) \
3434 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3435#define ADD8(a, b, n) \
3436 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3437#define SUB8(a, b, n) \
3438 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3439#define PFX uh
3440
3441#include "op_addsub.h"
3442
3443static inline uint8_t do_usad(uint8_t a, uint8_t b)
3444{
3445 if (a > b)
3446 return a - b;
3447 else
3448 return b - a;
3449}
3450
3451/* Unsigned sum of absolute byte differences. */
3452uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
3453{
3454 uint32_t sum;
3455 sum = do_usad(a, b);
3456 sum += do_usad(a >> 8, b >> 8);
3457 sum += do_usad(a >> 16, b >>16);
3458 sum += do_usad(a >> 24, b >> 24);
3459 return sum;
3460}
3461
3462/* For ARMv6 SEL instruction. */
3463uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
3464{
3465 uint32_t mask;
3466
3467 mask = 0;
3468 if (flags & 1)
3469 mask |= 0xff;
3470 if (flags & 2)
3471 mask |= 0xff00;
3472 if (flags & 4)
3473 mask |= 0xff0000;
3474 if (flags & 8)
3475 mask |= 0xff000000;
3476 return (a & mask) | (b & ~mask);
3477}
3478
Peter Maydellb90372a2012-08-06 17:42:18 +01003479/* VFP support. We follow the convention used for VFP instructions:
3480 Single precision routines have a "s" suffix, double precision a
pbrook4373f3c2008-03-31 03:47:19 +00003481 "d" suffix. */
3482
3483/* Convert host exception flags to vfp form. */
3484static inline int vfp_exceptbits_from_host(int host_bits)
3485{
3486 int target_bits = 0;
3487
3488 if (host_bits & float_flag_invalid)
3489 target_bits |= 1;
3490 if (host_bits & float_flag_divbyzero)
3491 target_bits |= 2;
3492 if (host_bits & float_flag_overflow)
3493 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01003494 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00003495 target_bits |= 8;
3496 if (host_bits & float_flag_inexact)
3497 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00003498 if (host_bits & float_flag_input_denormal)
3499 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00003500 return target_bits;
3501}
3502
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003503uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003504{
3505 int i;
3506 uint32_t fpscr;
3507
3508 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
3509 | (env->vfp.vec_len << 16)
3510 | (env->vfp.vec_stride << 20);
3511 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01003512 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00003513 fpscr |= vfp_exceptbits_from_host(i);
3514 return fpscr;
3515}
3516
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003517uint32_t vfp_get_fpscr(CPUARMState *env)
Peter Maydell01653292010-11-24 15:20:04 +00003518{
3519 return HELPER(vfp_get_fpscr)(env);
3520}
3521
pbrook4373f3c2008-03-31 03:47:19 +00003522/* Convert vfp exception flags to target form. */
3523static inline int vfp_exceptbits_to_host(int target_bits)
3524{
3525 int host_bits = 0;
3526
3527 if (target_bits & 1)
3528 host_bits |= float_flag_invalid;
3529 if (target_bits & 2)
3530 host_bits |= float_flag_divbyzero;
3531 if (target_bits & 4)
3532 host_bits |= float_flag_overflow;
3533 if (target_bits & 8)
3534 host_bits |= float_flag_underflow;
3535 if (target_bits & 0x10)
3536 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00003537 if (target_bits & 0x80)
3538 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00003539 return host_bits;
3540}
3541
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003542void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
pbrook4373f3c2008-03-31 03:47:19 +00003543{
3544 int i;
3545 uint32_t changed;
3546
3547 changed = env->vfp.xregs[ARM_VFP_FPSCR];
3548 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
3549 env->vfp.vec_len = (val >> 16) & 7;
3550 env->vfp.vec_stride = (val >> 20) & 3;
3551
3552 changed ^= val;
3553 if (changed & (3 << 22)) {
3554 i = (val >> 22) & 3;
3555 switch (i) {
3556 case 0:
3557 i = float_round_nearest_even;
3558 break;
3559 case 1:
3560 i = float_round_up;
3561 break;
3562 case 2:
3563 i = float_round_down;
3564 break;
3565 case 3:
3566 i = float_round_to_zero;
3567 break;
3568 }
3569 set_float_rounding_mode(i, &env->vfp.fp_status);
3570 }
Peter Maydellcecd8502011-01-06 19:37:55 +00003571 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00003572 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00003573 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3574 }
pbrook5c7908e2008-12-19 13:53:37 +00003575 if (changed & (1 << 25))
3576 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00003577
Peter Maydellb12c3902011-01-06 19:37:54 +00003578 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00003579 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01003580 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00003581}
3582
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003583void vfp_set_fpscr(CPUARMState *env, uint32_t val)
Peter Maydell01653292010-11-24 15:20:04 +00003584{
3585 HELPER(vfp_set_fpscr)(env, val);
3586}
3587
pbrook4373f3c2008-03-31 03:47:19 +00003588#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
3589
3590#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00003591float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003592{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00003593 float_status *fpst = fpstp; \
3594 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003595} \
Peter Maydellae1857e2011-05-25 14:51:48 +00003596float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003597{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00003598 float_status *fpst = fpstp; \
3599 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003600}
3601VFP_BINOP(add)
3602VFP_BINOP(sub)
3603VFP_BINOP(mul)
3604VFP_BINOP(div)
3605#undef VFP_BINOP
3606
3607float32 VFP_HELPER(neg, s)(float32 a)
3608{
3609 return float32_chs(a);
3610}
3611
3612float64 VFP_HELPER(neg, d)(float64 a)
3613{
balrog66230e02008-04-20 00:58:01 +00003614 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00003615}
3616
3617float32 VFP_HELPER(abs, s)(float32 a)
3618{
3619 return float32_abs(a);
3620}
3621
3622float64 VFP_HELPER(abs, d)(float64 a)
3623{
balrog66230e02008-04-20 00:58:01 +00003624 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00003625}
3626
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003627float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003628{
3629 return float32_sqrt(a, &env->vfp.fp_status);
3630}
3631
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003632float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003633{
3634 return float64_sqrt(a, &env->vfp.fp_status);
3635}
3636
3637/* XXX: check quiet/signaling case */
3638#define DO_VFP_cmp(p, type) \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003639void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00003640{ \
3641 uint32_t flags; \
3642 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
3643 case 0: flags = 0x6; break; \
3644 case -1: flags = 0x8; break; \
3645 case 1: flags = 0x2; break; \
3646 default: case 2: flags = 0x3; break; \
3647 } \
3648 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3649 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3650} \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003651void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00003652{ \
3653 uint32_t flags; \
3654 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
3655 case 0: flags = 0x6; break; \
3656 case -1: flags = 0x8; break; \
3657 case 1: flags = 0x2; break; \
3658 default: case 2: flags = 0x3; break; \
3659 } \
3660 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3661 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3662}
3663DO_VFP_cmp(s, float32)
3664DO_VFP_cmp(d, float64)
3665#undef DO_VFP_cmp
3666
Peter Maydell5500b062011-05-19 14:46:19 +01003667/* Integer to float and float to integer conversions */
3668
3669#define CONV_ITOF(name, fsz, sign) \
3670 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
3671{ \
3672 float_status *fpst = fpstp; \
Peter Maydell85836972012-01-25 11:49:46 +00003673 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003674}
3675
Peter Maydell5500b062011-05-19 14:46:19 +01003676#define CONV_FTOI(name, fsz, sign, round) \
3677uint32_t HELPER(name)(float##fsz x, void *fpstp) \
3678{ \
3679 float_status *fpst = fpstp; \
3680 if (float##fsz##_is_any_nan(x)) { \
3681 float_raise(float_flag_invalid, fpst); \
3682 return 0; \
3683 } \
3684 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003685}
3686
Peter Maydell5500b062011-05-19 14:46:19 +01003687#define FLOAT_CONVS(name, p, fsz, sign) \
3688CONV_ITOF(vfp_##name##to##p, fsz, sign) \
3689CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
3690CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00003691
Peter Maydell5500b062011-05-19 14:46:19 +01003692FLOAT_CONVS(si, s, 32, )
3693FLOAT_CONVS(si, d, 64, )
3694FLOAT_CONVS(ui, s, 32, u)
3695FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00003696
Peter Maydell5500b062011-05-19 14:46:19 +01003697#undef CONV_ITOF
3698#undef CONV_FTOI
3699#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00003700
3701/* floating point conversion */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003702float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003703{
Peter Maydell2d627732010-12-07 15:37:34 +00003704 float64 r = float32_to_float64(x, &env->vfp.fp_status);
3705 /* ARM requires that S<->D conversion of any kind of NaN generates
3706 * a quiet NaN by forcing the most significant frac bit to 1.
3707 */
3708 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00003709}
3710
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003711float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003712{
Peter Maydell2d627732010-12-07 15:37:34 +00003713 float32 r = float64_to_float32(x, &env->vfp.fp_status);
3714 /* ARM requires that S<->D conversion of any kind of NaN generates
3715 * a quiet NaN by forcing the most significant frac bit to 1.
3716 */
3717 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00003718}
3719
3720/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00003721#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01003722float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
3723 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003724{ \
Peter Maydell5500b062011-05-19 14:46:19 +01003725 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00003726 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01003727 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
3728 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003729} \
Peter Maydell5500b062011-05-19 14:46:19 +01003730uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
3731 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00003732{ \
Peter Maydell5500b062011-05-19 14:46:19 +01003733 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00003734 float##fsz tmp; \
3735 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01003736 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00003737 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00003738 } \
Peter Maydell5500b062011-05-19 14:46:19 +01003739 tmp = float##fsz##_scalbn(x, shift, fpst); \
3740 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00003741}
3742
Peter Maydell622465e2011-03-14 07:23:11 +00003743VFP_CONV_FIX(sh, d, 64, int16, )
3744VFP_CONV_FIX(sl, d, 64, int32, )
3745VFP_CONV_FIX(uh, d, 64, uint16, u)
3746VFP_CONV_FIX(ul, d, 64, uint32, u)
3747VFP_CONV_FIX(sh, s, 32, int16, )
3748VFP_CONV_FIX(sl, s, 32, int32, )
3749VFP_CONV_FIX(uh, s, 32, uint16, u)
3750VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00003751#undef VFP_CONV_FIX
3752
Paul Brook60011492009-11-19 16:45:20 +00003753/* Half precision conversions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003754static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00003755{
Paul Brook60011492009-11-19 16:45:20 +00003756 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00003757 float32 r = float16_to_float32(make_float16(a), ieee, s);
3758 if (ieee) {
3759 return float32_maybe_silence_nan(r);
3760 }
3761 return r;
Paul Brook60011492009-11-19 16:45:20 +00003762}
3763
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003764static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00003765{
Paul Brook60011492009-11-19 16:45:20 +00003766 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00003767 float16 r = float32_to_float16(a, ieee, s);
3768 if (ieee) {
3769 r = float16_maybe_silence_nan(r);
3770 }
3771 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00003772}
3773
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003774float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003775{
3776 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
3777}
3778
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003779uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003780{
3781 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
3782}
3783
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003784float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003785{
3786 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
3787}
3788
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003789uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00003790{
3791 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
3792}
3793
Peter Maydelldda3ec42011-03-14 15:37:12 +00003794#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00003795#define float32_three make_float32(0x40400000)
3796#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00003797
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003798float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003799{
Peter Maydelldda3ec42011-03-14 15:37:12 +00003800 float_status *s = &env->vfp.standard_fp_status;
3801 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3802 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003803 if (!(float32_is_zero(a) || float32_is_zero(b))) {
3804 float_raise(float_flag_input_denormal, s);
3805 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00003806 return float32_two;
3807 }
3808 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00003809}
3810
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003811float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003812{
Peter Maydell71826962011-01-14 20:39:18 +01003813 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01003814 float32 product;
3815 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3816 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003817 if (!(float32_is_zero(a) || float32_is_zero(b))) {
3818 float_raise(float_flag_input_denormal, s);
3819 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00003820 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01003821 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00003822 product = float32_mul(a, b, s);
3823 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00003824}
3825
pbrook8f8e3aa2008-03-31 03:48:01 +00003826/* NEON helpers. */
3827
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01003828/* Constants 256 and 512 are used in some helpers; we avoid relying on
3829 * int->float conversions at run-time. */
3830#define float64_256 make_float64(0x4070000000000000LL)
3831#define float64_512 make_float64(0x4080000000000000LL)
3832
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003833/* The algorithm that must be used to calculate the estimate
3834 * is specified by the ARM ARM.
3835 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003836static float64 recip_estimate(float64 a, CPUARMState *env)
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003837{
Peter Maydell1146a812011-05-19 14:46:14 +01003838 /* These calculations mustn't set any fp exception flags,
3839 * so we use a local copy of the fp_status.
3840 */
3841 float_status dummy_status = env->vfp.standard_fp_status;
3842 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003843 /* q = (int)(a * 512.0) */
3844 float64 q = float64_mul(float64_512, a, s);
3845 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3846
3847 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3848 q = int64_to_float64(q_int, s);
3849 q = float64_add(q, float64_half, s);
3850 q = float64_div(q, float64_512, s);
3851 q = float64_div(float64_one, q, s);
3852
3853 /* s = (int)(256.0 * r + 0.5) */
3854 q = float64_mul(q, float64_256, s);
3855 q = float64_add(q, float64_half, s);
3856 q_int = float64_to_int64_round_to_zero(q, s);
3857
3858 /* return (double)s / 256.0 */
3859 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3860}
3861
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003862float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003863{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003864 float_status *s = &env->vfp.standard_fp_status;
3865 float64 f64;
3866 uint32_t val32 = float32_val(a);
3867
3868 int result_exp;
3869 int a_exp = (val32 & 0x7f800000) >> 23;
3870 int sign = val32 & 0x80000000;
3871
3872 if (float32_is_any_nan(a)) {
3873 if (float32_is_signaling_nan(a)) {
3874 float_raise(float_flag_invalid, s);
3875 }
3876 return float32_default_nan;
3877 } else if (float32_is_infinity(a)) {
3878 return float32_set_sign(float32_zero, float32_is_neg(a));
3879 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003880 if (!float32_is_zero(a)) {
3881 float_raise(float_flag_input_denormal, s);
3882 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003883 float_raise(float_flag_divbyzero, s);
3884 return float32_set_sign(float32_infinity, float32_is_neg(a));
3885 } else if (a_exp >= 253) {
3886 float_raise(float_flag_underflow, s);
3887 return float32_set_sign(float32_zero, float32_is_neg(a));
3888 }
3889
3890 f64 = make_float64((0x3feULL << 52)
3891 | ((int64_t)(val32 & 0x7fffff) << 29));
3892
3893 result_exp = 253 - a_exp;
3894
3895 f64 = recip_estimate(f64, env);
3896
3897 val32 = sign
3898 | ((result_exp & 0xff) << 23)
3899 | ((float64_val(f64) >> 29) & 0x7fffff);
3900 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00003901}
3902
Christophe Lyone07be5d2011-02-21 17:38:48 +01003903/* The algorithm that must be used to calculate the estimate
3904 * is specified by the ARM ARM.
3905 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003906static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
Christophe Lyone07be5d2011-02-21 17:38:48 +01003907{
Peter Maydell1146a812011-05-19 14:46:14 +01003908 /* These calculations mustn't set any fp exception flags,
3909 * so we use a local copy of the fp_status.
3910 */
3911 float_status dummy_status = env->vfp.standard_fp_status;
3912 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01003913 float64 q;
3914 int64_t q_int;
3915
3916 if (float64_lt(a, float64_half, s)) {
3917 /* range 0.25 <= a < 0.5 */
3918
3919 /* a in units of 1/512 rounded down */
3920 /* q0 = (int)(a * 512.0); */
3921 q = float64_mul(float64_512, a, s);
3922 q_int = float64_to_int64_round_to_zero(q, s);
3923
3924 /* reciprocal root r */
3925 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
3926 q = int64_to_float64(q_int, s);
3927 q = float64_add(q, float64_half, s);
3928 q = float64_div(q, float64_512, s);
3929 q = float64_sqrt(q, s);
3930 q = float64_div(float64_one, q, s);
3931 } else {
3932 /* range 0.5 <= a < 1.0 */
3933
3934 /* a in units of 1/256 rounded down */
3935 /* q1 = (int)(a * 256.0); */
3936 q = float64_mul(float64_256, a, s);
3937 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3938
3939 /* reciprocal root r */
3940 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3941 q = int64_to_float64(q_int, s);
3942 q = float64_add(q, float64_half, s);
3943 q = float64_div(q, float64_256, s);
3944 q = float64_sqrt(q, s);
3945 q = float64_div(float64_one, q, s);
3946 }
3947 /* r in units of 1/256 rounded to nearest */
3948 /* s = (int)(256.0 * r + 0.5); */
3949
3950 q = float64_mul(q, float64_256,s );
3951 q = float64_add(q, float64_half, s);
3952 q_int = float64_to_int64_round_to_zero(q, s);
3953
3954 /* return (double)s / 256.0;*/
3955 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3956}
3957
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003958float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003959{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003960 float_status *s = &env->vfp.standard_fp_status;
3961 int result_exp;
3962 float64 f64;
3963 uint32_t val;
3964 uint64_t val64;
3965
3966 val = float32_val(a);
3967
3968 if (float32_is_any_nan(a)) {
3969 if (float32_is_signaling_nan(a)) {
3970 float_raise(float_flag_invalid, s);
3971 }
3972 return float32_default_nan;
3973 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003974 if (!float32_is_zero(a)) {
3975 float_raise(float_flag_input_denormal, s);
3976 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01003977 float_raise(float_flag_divbyzero, s);
3978 return float32_set_sign(float32_infinity, float32_is_neg(a));
3979 } else if (float32_is_neg(a)) {
3980 float_raise(float_flag_invalid, s);
3981 return float32_default_nan;
3982 } else if (float32_is_infinity(a)) {
3983 return float32_zero;
3984 }
3985
3986 /* Normalize to a double-precision value between 0.25 and 1.0,
3987 * preserving the parity of the exponent. */
3988 if ((val & 0x800000) == 0) {
3989 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3990 | (0x3feULL << 52)
3991 | ((uint64_t)(val & 0x7fffff) << 29));
3992 } else {
3993 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3994 | (0x3fdULL << 52)
3995 | ((uint64_t)(val & 0x7fffff) << 29));
3996 }
3997
3998 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3999
4000 f64 = recip_sqrt_estimate(f64, env);
4001
4002 val64 = float64_val(f64);
4003
Christophe LYON26cc6ab2011-10-19 16:14:05 +00004004 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01004005 | ((val64 >> 29) & 0x7fffff);
4006 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00004007}
4008
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004009uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00004010{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01004011 float64 f64;
4012
4013 if ((a & 0x80000000) == 0) {
4014 return 0xffffffff;
4015 }
4016
4017 f64 = make_float64((0x3feULL << 52)
4018 | ((int64_t)(a & 0x7fffffff) << 21));
4019
4020 f64 = recip_estimate (f64, env);
4021
4022 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00004023}
4024
Andreas Färber0ecb72a2012-03-14 01:38:21 +01004025uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00004026{
Christophe Lyone07be5d2011-02-21 17:38:48 +01004027 float64 f64;
4028
4029 if ((a & 0xc0000000) == 0) {
4030 return 0xffffffff;
4031 }
4032
4033 if (a & 0x80000000) {
4034 f64 = make_float64((0x3feULL << 52)
4035 | ((uint64_t)(a & 0x7fffffff) << 21));
4036 } else { /* bits 31-30 == '01' */
4037 f64 = make_float64((0x3fdULL << 52)
4038 | ((uint64_t)(a & 0x3fffffff) << 22));
4039 }
4040
4041 f64 = recip_sqrt_estimate(f64, env);
4042
4043 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00004044}
pbrookfe1479c2008-12-19 13:18:36 +00004045
Peter Maydellda97f522011-10-19 16:14:07 +00004046/* VFPv4 fused multiply-accumulate */
4047float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
4048{
4049 float_status *fpst = fpstp;
4050 return float32_muladd(a, b, c, 0, fpst);
4051}
4052
4053float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
4054{
4055 float_status *fpst = fpstp;
4056 return float64_muladd(a, b, c, 0, fpst);
4057}