]>
Commit | Line | Data |
---|---|---|
b5ff1b31 | 1 | #include "cpu.h" |
9ee6e8bb | 2 | #include "gdbstub.h" |
7b59220e | 3 | #include "helper.h" |
7bbcb0af | 4 | #include "host-utils.h" |
0b03bdfc PM |
5 | #include "sysemu.h" |
6 | ||
0ecb72a5 | 7 | static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) |
56aebc89 PB |
8 | { |
9 | int nregs; | |
10 | ||
11 | /* VFP data registers are always little-endian. */ | |
12 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; | |
13 | if (reg < nregs) { | |
14 | stfq_le_p(buf, env->vfp.regs[reg]); | |
15 | return 8; | |
16 | } | |
17 | if (arm_feature(env, ARM_FEATURE_NEON)) { | |
18 | /* Aliases for Q regs. */ | |
19 | nregs += 16; | |
20 | if (reg < nregs) { | |
21 | stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]); | |
22 | stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]); | |
23 | return 16; | |
24 | } | |
25 | } | |
26 | switch (reg - nregs) { | |
27 | case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; | |
28 | case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4; | |
29 | case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; | |
30 | } | |
31 | return 0; | |
32 | } | |
33 | ||
0ecb72a5 | 34 | static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) |
56aebc89 PB |
35 | { |
36 | int nregs; | |
37 | ||
38 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; | |
39 | if (reg < nregs) { | |
40 | env->vfp.regs[reg] = ldfq_le_p(buf); | |
41 | return 8; | |
42 | } | |
43 | if (arm_feature(env, ARM_FEATURE_NEON)) { | |
44 | nregs += 16; | |
45 | if (reg < nregs) { | |
46 | env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf); | |
47 | env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8); | |
48 | return 16; | |
49 | } | |
50 | } | |
51 | switch (reg - nregs) { | |
52 | case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; | |
53 | case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4; | |
71b3c3de | 54 | case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; |
56aebc89 PB |
55 | } |
56 | return 0; | |
57 | } | |
58 | ||
2ceb98c0 PM |
59 | void register_cp_regs_for_features(ARMCPU *cpu) |
60 | { | |
61 | /* Register all the coprocessor registers based on feature bits */ | |
62 | CPUARMState *env = &cpu->env; | |
63 | if (arm_feature(env, ARM_FEATURE_M)) { | |
64 | /* M profile has no coprocessor registers */ | |
65 | return; | |
66 | } | |
67 | ||
68 | } | |
69 | ||
778c3a06 | 70 | ARMCPU *cpu_arm_init(const char *cpu_model) |
40f137e1 | 71 | { |
dec9c2d4 | 72 | ARMCPU *cpu; |
40f137e1 | 73 | CPUARMState *env; |
b26eefb6 | 74 | static int inited = 0; |
40f137e1 | 75 | |
777dc784 | 76 | if (!object_class_by_name(cpu_model)) { |
aaed909a | 77 | return NULL; |
777dc784 PM |
78 | } |
79 | cpu = ARM_CPU(object_new(cpu_model)); | |
dec9c2d4 | 80 | env = &cpu->env; |
777dc784 | 81 | env->cpu_model_str = cpu_model; |
581be094 | 82 | arm_cpu_realize(cpu); |
777dc784 | 83 | |
f4fc247b | 84 | if (tcg_enabled() && !inited) { |
b26eefb6 PB |
85 | inited = 1; |
86 | arm_translate_init(); | |
87 | } | |
88 | ||
df90dadb | 89 | cpu_reset(CPU(cpu)); |
56aebc89 PB |
90 | if (arm_feature(env, ARM_FEATURE_NEON)) { |
91 | gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg, | |
92 | 51, "arm-neon.xml", 0); | |
93 | } else if (arm_feature(env, ARM_FEATURE_VFP3)) { | |
94 | gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg, | |
95 | 35, "arm-vfp3.xml", 0); | |
96 | } else if (arm_feature(env, ARM_FEATURE_VFP)) { | |
97 | gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg, | |
98 | 19, "arm-vfp.xml", 0); | |
99 | } | |
0bf46a40 | 100 | qemu_init_vcpu(env); |
778c3a06 | 101 | return cpu; |
40f137e1 PB |
102 | } |
103 | ||
777dc784 PM |
104 | typedef struct ARMCPUListState { |
105 | fprintf_function cpu_fprintf; | |
106 | FILE *file; | |
107 | } ARMCPUListState; | |
3371d272 | 108 | |
777dc784 PM |
109 | /* Sort alphabetically by type name, except for "any". */ |
110 | static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) | |
5adb4839 | 111 | { |
777dc784 PM |
112 | ObjectClass *class_a = (ObjectClass *)a; |
113 | ObjectClass *class_b = (ObjectClass *)b; | |
114 | const char *name_a, *name_b; | |
5adb4839 | 115 | |
777dc784 PM |
116 | name_a = object_class_get_name(class_a); |
117 | name_b = object_class_get_name(class_b); | |
118 | if (strcmp(name_a, "any") == 0) { | |
119 | return 1; | |
120 | } else if (strcmp(name_b, "any") == 0) { | |
121 | return -1; | |
122 | } else { | |
123 | return strcmp(name_a, name_b); | |
5adb4839 PB |
124 | } |
125 | } | |
126 | ||
777dc784 | 127 | static void arm_cpu_list_entry(gpointer data, gpointer user_data) |
40f137e1 | 128 | { |
777dc784 PM |
129 | ObjectClass *oc = data; |
130 | ARMCPUListState *s = user_data; | |
3371d272 | 131 | |
777dc784 PM |
132 | (*s->cpu_fprintf)(s->file, " %s\n", |
133 | object_class_get_name(oc)); | |
134 | } | |
135 | ||
136 | void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf) | |
137 | { | |
138 | ARMCPUListState s = { | |
139 | .file = f, | |
140 | .cpu_fprintf = cpu_fprintf, | |
141 | }; | |
142 | GSList *list; | |
143 | ||
144 | list = object_class_get_list(TYPE_ARM_CPU, false); | |
145 | list = g_slist_sort(list, arm_cpu_list_compare); | |
146 | (*cpu_fprintf)(f, "Available CPUs:\n"); | |
147 | g_slist_foreach(list, arm_cpu_list_entry, &s); | |
148 | g_slist_free(list); | |
40f137e1 PB |
149 | } |
150 | ||
4b6a83fb PM |
151 | void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, |
152 | const ARMCPRegInfo *r, void *opaque) | |
153 | { | |
154 | /* Define implementations of coprocessor registers. | |
155 | * We store these in a hashtable because typically | |
156 | * there are less than 150 registers in a space which | |
157 | * is 16*16*16*8*8 = 262144 in size. | |
158 | * Wildcarding is supported for the crm, opc1 and opc2 fields. | |
159 | * If a register is defined twice then the second definition is | |
160 | * used, so this can be used to define some generic registers and | |
161 | * then override them with implementation specific variations. | |
162 | * At least one of the original and the second definition should | |
163 | * include ARM_CP_OVERRIDE in its type bits -- this is just a guard | |
164 | * against accidental use. | |
165 | */ | |
166 | int crm, opc1, opc2; | |
167 | int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; | |
168 | int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; | |
169 | int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; | |
170 | int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; | |
171 | int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; | |
172 | int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; | |
173 | /* 64 bit registers have only CRm and Opc1 fields */ | |
174 | assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); | |
175 | /* Check that the register definition has enough info to handle | |
176 | * reads and writes if they are permitted. | |
177 | */ | |
178 | if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { | |
179 | if (r->access & PL3_R) { | |
180 | assert(r->fieldoffset || r->readfn); | |
181 | } | |
182 | if (r->access & PL3_W) { | |
183 | assert(r->fieldoffset || r->writefn); | |
184 | } | |
185 | } | |
186 | /* Bad type field probably means missing sentinel at end of reg list */ | |
187 | assert(cptype_valid(r->type)); | |
188 | for (crm = crmmin; crm <= crmmax; crm++) { | |
189 | for (opc1 = opc1min; opc1 <= opc1max; opc1++) { | |
190 | for (opc2 = opc2min; opc2 <= opc2max; opc2++) { | |
191 | uint32_t *key = g_new(uint32_t, 1); | |
192 | ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); | |
193 | int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; | |
194 | *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2); | |
195 | r2->opaque = opaque; | |
196 | /* Make sure reginfo passed to helpers for wildcarded regs | |
197 | * has the correct crm/opc1/opc2 for this reg, not CP_ANY: | |
198 | */ | |
199 | r2->crm = crm; | |
200 | r2->opc1 = opc1; | |
201 | r2->opc2 = opc2; | |
202 | /* Overriding of an existing definition must be explicitly | |
203 | * requested. | |
204 | */ | |
205 | if (!(r->type & ARM_CP_OVERRIDE)) { | |
206 | ARMCPRegInfo *oldreg; | |
207 | oldreg = g_hash_table_lookup(cpu->cp_regs, key); | |
208 | if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { | |
209 | fprintf(stderr, "Register redefined: cp=%d %d bit " | |
210 | "crn=%d crm=%d opc1=%d opc2=%d, " | |
211 | "was %s, now %s\n", r2->cp, 32 + 32 * is64, | |
212 | r2->crn, r2->crm, r2->opc1, r2->opc2, | |
213 | oldreg->name, r2->name); | |
214 | assert(0); | |
215 | } | |
216 | } | |
217 | g_hash_table_insert(cpu->cp_regs, key, r2); | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | void define_arm_cp_regs_with_opaque(ARMCPU *cpu, | |
224 | const ARMCPRegInfo *regs, void *opaque) | |
225 | { | |
226 | /* Define a whole list of registers */ | |
227 | const ARMCPRegInfo *r; | |
228 | for (r = regs; r->type != ARM_CP_SENTINEL; r++) { | |
229 | define_one_arm_cp_reg_with_opaque(cpu, r, opaque); | |
230 | } | |
231 | } | |
232 | ||
233 | const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp) | |
234 | { | |
235 | return g_hash_table_lookup(cpu->cp_regs, &encoded_cp); | |
236 | } | |
237 | ||
238 | int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, | |
239 | uint64_t value) | |
240 | { | |
241 | /* Helper coprocessor write function for write-ignore registers */ | |
242 | return 0; | |
243 | } | |
244 | ||
245 | int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value) | |
246 | { | |
247 | /* Helper coprocessor write function for read-as-zero registers */ | |
248 | *value = 0; | |
249 | return 0; | |
250 | } | |
251 | ||
0ecb72a5 | 252 | static int bad_mode_switch(CPUARMState *env, int mode) |
37064a8b PM |
253 | { |
254 | /* Return true if it is not valid for us to switch to | |
255 | * this CPU mode (ie all the UNPREDICTABLE cases in | |
256 | * the ARM ARM CPSRWriteByInstr pseudocode). | |
257 | */ | |
258 | switch (mode) { | |
259 | case ARM_CPU_MODE_USR: | |
260 | case ARM_CPU_MODE_SYS: | |
261 | case ARM_CPU_MODE_SVC: | |
262 | case ARM_CPU_MODE_ABT: | |
263 | case ARM_CPU_MODE_UND: | |
264 | case ARM_CPU_MODE_IRQ: | |
265 | case ARM_CPU_MODE_FIQ: | |
266 | return 0; | |
267 | default: | |
268 | return 1; | |
269 | } | |
270 | } | |
271 | ||
2f4a40e5 AZ |
272 | uint32_t cpsr_read(CPUARMState *env) |
273 | { | |
274 | int ZF; | |
6fbe23d5 PB |
275 | ZF = (env->ZF == 0); |
276 | return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | | |
2f4a40e5 AZ |
277 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) |
278 | | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) | |
279 | | ((env->condexec_bits & 0xfc) << 8) | |
280 | | (env->GE << 16); | |
281 | } | |
282 | ||
283 | void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask) | |
284 | { | |
2f4a40e5 | 285 | if (mask & CPSR_NZCV) { |
6fbe23d5 PB |
286 | env->ZF = (~val) & CPSR_Z; |
287 | env->NF = val; | |
2f4a40e5 AZ |
288 | env->CF = (val >> 29) & 1; |
289 | env->VF = (val << 3) & 0x80000000; | |
290 | } | |
291 | if (mask & CPSR_Q) | |
292 | env->QF = ((val & CPSR_Q) != 0); | |
293 | if (mask & CPSR_T) | |
294 | env->thumb = ((val & CPSR_T) != 0); | |
295 | if (mask & CPSR_IT_0_1) { | |
296 | env->condexec_bits &= ~3; | |
297 | env->condexec_bits |= (val >> 25) & 3; | |
298 | } | |
299 | if (mask & CPSR_IT_2_7) { | |
300 | env->condexec_bits &= 3; | |
301 | env->condexec_bits |= (val >> 8) & 0xfc; | |
302 | } | |
303 | if (mask & CPSR_GE) { | |
304 | env->GE = (val >> 16) & 0xf; | |
305 | } | |
306 | ||
307 | if ((env->uncached_cpsr ^ val) & mask & CPSR_M) { | |
37064a8b PM |
308 | if (bad_mode_switch(env, val & CPSR_M)) { |
309 | /* Attempt to switch to an invalid mode: this is UNPREDICTABLE. | |
310 | * We choose to ignore the attempt and leave the CPSR M field | |
311 | * untouched. | |
312 | */ | |
313 | mask &= ~CPSR_M; | |
314 | } else { | |
315 | switch_mode(env, val & CPSR_M); | |
316 | } | |
2f4a40e5 AZ |
317 | } |
318 | mask &= ~CACHED_CPSR_BITS; | |
319 | env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); | |
320 | } | |
321 | ||
b26eefb6 PB |
322 | /* Sign/zero extend */ |
323 | uint32_t HELPER(sxtb16)(uint32_t x) | |
324 | { | |
325 | uint32_t res; | |
326 | res = (uint16_t)(int8_t)x; | |
327 | res |= (uint32_t)(int8_t)(x >> 16) << 16; | |
328 | return res; | |
329 | } | |
330 | ||
331 | uint32_t HELPER(uxtb16)(uint32_t x) | |
332 | { | |
333 | uint32_t res; | |
334 | res = (uint16_t)(uint8_t)x; | |
335 | res |= (uint32_t)(uint8_t)(x >> 16) << 16; | |
336 | return res; | |
337 | } | |
338 | ||
f51bbbfe PB |
339 | uint32_t HELPER(clz)(uint32_t x) |
340 | { | |
7bbcb0af | 341 | return clz32(x); |
f51bbbfe PB |
342 | } |
343 | ||
3670669c PB |
344 | int32_t HELPER(sdiv)(int32_t num, int32_t den) |
345 | { | |
346 | if (den == 0) | |
347 | return 0; | |
686eeb93 AJ |
348 | if (num == INT_MIN && den == -1) |
349 | return INT_MIN; | |
3670669c PB |
350 | return num / den; |
351 | } | |
352 | ||
353 | uint32_t HELPER(udiv)(uint32_t num, uint32_t den) | |
354 | { | |
355 | if (den == 0) | |
356 | return 0; | |
357 | return num / den; | |
358 | } | |
359 | ||
360 | uint32_t HELPER(rbit)(uint32_t x) | |
361 | { | |
362 | x = ((x & 0xff000000) >> 24) | |
363 | | ((x & 0x00ff0000) >> 8) | |
364 | | ((x & 0x0000ff00) << 8) | |
365 | | ((x & 0x000000ff) << 24); | |
366 | x = ((x & 0xf0f0f0f0) >> 4) | |
367 | | ((x & 0x0f0f0f0f) << 4); | |
368 | x = ((x & 0x88888888) >> 3) | |
369 | | ((x & 0x44444444) >> 1) | |
370 | | ((x & 0x22222222) << 1) | |
371 | | ((x & 0x11111111) << 3); | |
372 | return x; | |
373 | } | |
374 | ||
ad69471c PB |
375 | uint32_t HELPER(abs)(uint32_t x) |
376 | { | |
377 | return ((int32_t)x < 0) ? -x : x; | |
378 | } | |
379 | ||
5fafdf24 | 380 | #if defined(CONFIG_USER_ONLY) |
b5ff1b31 | 381 | |
0ecb72a5 | 382 | void do_interrupt (CPUARMState *env) |
b5ff1b31 FB |
383 | { |
384 | env->exception_index = -1; | |
385 | } | |
386 | ||
0ecb72a5 | 387 | int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw, |
97b348e7 | 388 | int mmu_idx) |
b5ff1b31 FB |
389 | { |
390 | if (rw == 2) { | |
391 | env->exception_index = EXCP_PREFETCH_ABORT; | |
392 | env->cp15.c6_insn = address; | |
393 | } else { | |
394 | env->exception_index = EXCP_DATA_ABORT; | |
395 | env->cp15.c6_data = address; | |
396 | } | |
397 | return 1; | |
398 | } | |
399 | ||
0ecb72a5 | 400 | void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val) |
b5ff1b31 FB |
401 | { |
402 | cpu_abort(env, "cp15 insn %08x\n", insn); | |
403 | } | |
404 | ||
0ecb72a5 | 405 | uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn) |
b5ff1b31 FB |
406 | { |
407 | cpu_abort(env, "cp15 insn %08x\n", insn); | |
b5ff1b31 FB |
408 | } |
409 | ||
9ee6e8bb | 410 | /* These should probably raise undefined insn exceptions. */ |
0ecb72a5 | 411 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) |
9ee6e8bb PB |
412 | { |
413 | cpu_abort(env, "v7m_mrs %d\n", reg); | |
414 | } | |
415 | ||
0ecb72a5 | 416 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) |
9ee6e8bb PB |
417 | { |
418 | cpu_abort(env, "v7m_mrs %d\n", reg); | |
419 | return 0; | |
420 | } | |
421 | ||
0ecb72a5 | 422 | void switch_mode(CPUARMState *env, int mode) |
b5ff1b31 FB |
423 | { |
424 | if (mode != ARM_CPU_MODE_USR) | |
425 | cpu_abort(env, "Tried to switch out of user mode\n"); | |
426 | } | |
427 | ||
0ecb72a5 | 428 | void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val) |
9ee6e8bb PB |
429 | { |
430 | cpu_abort(env, "banked r13 write\n"); | |
431 | } | |
432 | ||
0ecb72a5 | 433 | uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode) |
9ee6e8bb PB |
434 | { |
435 | cpu_abort(env, "banked r13 read\n"); | |
436 | return 0; | |
437 | } | |
438 | ||
b5ff1b31 FB |
439 | #else |
440 | ||
441 | /* Map CPU modes onto saved register banks. */ | |
0ecb72a5 | 442 | static inline int bank_number(CPUARMState *env, int mode) |
b5ff1b31 FB |
443 | { |
444 | switch (mode) { | |
445 | case ARM_CPU_MODE_USR: | |
446 | case ARM_CPU_MODE_SYS: | |
447 | return 0; | |
448 | case ARM_CPU_MODE_SVC: | |
449 | return 1; | |
450 | case ARM_CPU_MODE_ABT: | |
451 | return 2; | |
452 | case ARM_CPU_MODE_UND: | |
453 | return 3; | |
454 | case ARM_CPU_MODE_IRQ: | |
455 | return 4; | |
456 | case ARM_CPU_MODE_FIQ: | |
457 | return 5; | |
458 | } | |
1b9e01c1 | 459 | cpu_abort(env, "Bad mode %x\n", mode); |
b5ff1b31 FB |
460 | return -1; |
461 | } | |
462 | ||
0ecb72a5 | 463 | void switch_mode(CPUARMState *env, int mode) |
b5ff1b31 FB |
464 | { |
465 | int old_mode; | |
466 | int i; | |
467 | ||
468 | old_mode = env->uncached_cpsr & CPSR_M; | |
469 | if (mode == old_mode) | |
470 | return; | |
471 | ||
472 | if (old_mode == ARM_CPU_MODE_FIQ) { | |
473 | memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
8637c67f | 474 | memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); |
b5ff1b31 FB |
475 | } else if (mode == ARM_CPU_MODE_FIQ) { |
476 | memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
8637c67f | 477 | memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); |
b5ff1b31 FB |
478 | } |
479 | ||
1b9e01c1 | 480 | i = bank_number(env, old_mode); |
b5ff1b31 FB |
481 | env->banked_r13[i] = env->regs[13]; |
482 | env->banked_r14[i] = env->regs[14]; | |
483 | env->banked_spsr[i] = env->spsr; | |
484 | ||
1b9e01c1 | 485 | i = bank_number(env, mode); |
b5ff1b31 FB |
486 | env->regs[13] = env->banked_r13[i]; |
487 | env->regs[14] = env->banked_r14[i]; | |
488 | env->spsr = env->banked_spsr[i]; | |
489 | } | |
490 | ||
9ee6e8bb PB |
491 | static void v7m_push(CPUARMState *env, uint32_t val) |
492 | { | |
493 | env->regs[13] -= 4; | |
494 | stl_phys(env->regs[13], val); | |
495 | } | |
496 | ||
497 | static uint32_t v7m_pop(CPUARMState *env) | |
498 | { | |
499 | uint32_t val; | |
500 | val = ldl_phys(env->regs[13]); | |
501 | env->regs[13] += 4; | |
502 | return val; | |
503 | } | |
504 | ||
505 | /* Switch to V7M main or process stack pointer. */ | |
506 | static void switch_v7m_sp(CPUARMState *env, int process) | |
507 | { | |
508 | uint32_t tmp; | |
509 | if (env->v7m.current_sp != process) { | |
510 | tmp = env->v7m.other_sp; | |
511 | env->v7m.other_sp = env->regs[13]; | |
512 | env->regs[13] = tmp; | |
513 | env->v7m.current_sp = process; | |
514 | } | |
515 | } | |
516 | ||
517 | static void do_v7m_exception_exit(CPUARMState *env) | |
518 | { | |
519 | uint32_t type; | |
520 | uint32_t xpsr; | |
521 | ||
522 | type = env->regs[15]; | |
523 | if (env->v7m.exception != 0) | |
983fe826 | 524 | armv7m_nvic_complete_irq(env->nvic, env->v7m.exception); |
9ee6e8bb PB |
525 | |
526 | /* Switch to the target stack. */ | |
527 | switch_v7m_sp(env, (type & 4) != 0); | |
528 | /* Pop registers. */ | |
529 | env->regs[0] = v7m_pop(env); | |
530 | env->regs[1] = v7m_pop(env); | |
531 | env->regs[2] = v7m_pop(env); | |
532 | env->regs[3] = v7m_pop(env); | |
533 | env->regs[12] = v7m_pop(env); | |
534 | env->regs[14] = v7m_pop(env); | |
535 | env->regs[15] = v7m_pop(env); | |
536 | xpsr = v7m_pop(env); | |
537 | xpsr_write(env, xpsr, 0xfffffdff); | |
538 | /* Undo stack alignment. */ | |
539 | if (xpsr & 0x200) | |
540 | env->regs[13] |= 4; | |
541 | /* ??? The exception return type specifies Thread/Handler mode. However | |
542 | this is also implied by the xPSR value. Not sure what to do | |
543 | if there is a mismatch. */ | |
544 | /* ??? Likewise for mismatches between the CONTROL register and the stack | |
545 | pointer. */ | |
546 | } | |
547 | ||
2b3ea315 | 548 | static void do_interrupt_v7m(CPUARMState *env) |
9ee6e8bb PB |
549 | { |
550 | uint32_t xpsr = xpsr_read(env); | |
551 | uint32_t lr; | |
552 | uint32_t addr; | |
553 | ||
554 | lr = 0xfffffff1; | |
555 | if (env->v7m.current_sp) | |
556 | lr |= 4; | |
557 | if (env->v7m.exception == 0) | |
558 | lr |= 8; | |
559 | ||
560 | /* For exceptions we just mark as pending on the NVIC, and let that | |
561 | handle it. */ | |
562 | /* TODO: Need to escalate if the current priority is higher than the | |
563 | one we're raising. */ | |
564 | switch (env->exception_index) { | |
565 | case EXCP_UDEF: | |
983fe826 | 566 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE); |
9ee6e8bb PB |
567 | return; |
568 | case EXCP_SWI: | |
569 | env->regs[15] += 2; | |
983fe826 | 570 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC); |
9ee6e8bb PB |
571 | return; |
572 | case EXCP_PREFETCH_ABORT: | |
573 | case EXCP_DATA_ABORT: | |
983fe826 | 574 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM); |
9ee6e8bb PB |
575 | return; |
576 | case EXCP_BKPT: | |
2ad207d4 PB |
577 | if (semihosting_enabled) { |
578 | int nr; | |
d8fd2954 | 579 | nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff; |
2ad207d4 PB |
580 | if (nr == 0xab) { |
581 | env->regs[15] += 2; | |
582 | env->regs[0] = do_arm_semihosting(env); | |
583 | return; | |
584 | } | |
585 | } | |
983fe826 | 586 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG); |
9ee6e8bb PB |
587 | return; |
588 | case EXCP_IRQ: | |
983fe826 | 589 | env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic); |
9ee6e8bb PB |
590 | break; |
591 | case EXCP_EXCEPTION_EXIT: | |
592 | do_v7m_exception_exit(env); | |
593 | return; | |
594 | default: | |
595 | cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index); | |
596 | return; /* Never happens. Keep compiler happy. */ | |
597 | } | |
598 | ||
599 | /* Align stack pointer. */ | |
600 | /* ??? Should only do this if Configuration Control Register | |
601 | STACKALIGN bit is set. */ | |
602 | if (env->regs[13] & 4) { | |
ab19b0ec | 603 | env->regs[13] -= 4; |
9ee6e8bb PB |
604 | xpsr |= 0x200; |
605 | } | |
6c95676b | 606 | /* Switch to the handler mode. */ |
9ee6e8bb PB |
607 | v7m_push(env, xpsr); |
608 | v7m_push(env, env->regs[15]); | |
609 | v7m_push(env, env->regs[14]); | |
610 | v7m_push(env, env->regs[12]); | |
611 | v7m_push(env, env->regs[3]); | |
612 | v7m_push(env, env->regs[2]); | |
613 | v7m_push(env, env->regs[1]); | |
614 | v7m_push(env, env->regs[0]); | |
615 | switch_v7m_sp(env, 0); | |
c98d174c PM |
616 | /* Clear IT bits */ |
617 | env->condexec_bits = 0; | |
9ee6e8bb PB |
618 | env->regs[14] = lr; |
619 | addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4); | |
620 | env->regs[15] = addr & 0xfffffffe; | |
621 | env->thumb = addr & 1; | |
622 | } | |
623 | ||
b5ff1b31 FB |
624 | /* Handle a CPU exception. */ |
625 | void do_interrupt(CPUARMState *env) | |
626 | { | |
627 | uint32_t addr; | |
628 | uint32_t mask; | |
629 | int new_mode; | |
630 | uint32_t offset; | |
631 | ||
9ee6e8bb PB |
632 | if (IS_M(env)) { |
633 | do_interrupt_v7m(env); | |
634 | return; | |
635 | } | |
b5ff1b31 FB |
636 | /* TODO: Vectored interrupt controller. */ |
637 | switch (env->exception_index) { | |
638 | case EXCP_UDEF: | |
639 | new_mode = ARM_CPU_MODE_UND; | |
640 | addr = 0x04; | |
641 | mask = CPSR_I; | |
642 | if (env->thumb) | |
643 | offset = 2; | |
644 | else | |
645 | offset = 4; | |
646 | break; | |
647 | case EXCP_SWI: | |
8e71621f PB |
648 | if (semihosting_enabled) { |
649 | /* Check for semihosting interrupt. */ | |
650 | if (env->thumb) { | |
d8fd2954 | 651 | mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff; |
8e71621f | 652 | } else { |
d8fd2954 PB |
653 | mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code) |
654 | & 0xffffff; | |
8e71621f PB |
655 | } |
656 | /* Only intercept calls from privileged modes, to provide some | |
657 | semblance of security. */ | |
658 | if (((mask == 0x123456 && !env->thumb) | |
659 | || (mask == 0xab && env->thumb)) | |
660 | && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) { | |
661 | env->regs[0] = do_arm_semihosting(env); | |
662 | return; | |
663 | } | |
664 | } | |
b5ff1b31 FB |
665 | new_mode = ARM_CPU_MODE_SVC; |
666 | addr = 0x08; | |
667 | mask = CPSR_I; | |
601d70b9 | 668 | /* The PC already points to the next instruction. */ |
b5ff1b31 FB |
669 | offset = 0; |
670 | break; | |
06c949e6 | 671 | case EXCP_BKPT: |
9ee6e8bb | 672 | /* See if this is a semihosting syscall. */ |
2ad207d4 | 673 | if (env->thumb && semihosting_enabled) { |
d8fd2954 | 674 | mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff; |
9ee6e8bb PB |
675 | if (mask == 0xab |
676 | && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) { | |
677 | env->regs[15] += 2; | |
678 | env->regs[0] = do_arm_semihosting(env); | |
679 | return; | |
680 | } | |
681 | } | |
81c05daf | 682 | env->cp15.c5_insn = 2; |
9ee6e8bb PB |
683 | /* Fall through to prefetch abort. */ |
684 | case EXCP_PREFETCH_ABORT: | |
b5ff1b31 FB |
685 | new_mode = ARM_CPU_MODE_ABT; |
686 | addr = 0x0c; | |
687 | mask = CPSR_A | CPSR_I; | |
688 | offset = 4; | |
689 | break; | |
690 | case EXCP_DATA_ABORT: | |
691 | new_mode = ARM_CPU_MODE_ABT; | |
692 | addr = 0x10; | |
693 | mask = CPSR_A | CPSR_I; | |
694 | offset = 8; | |
695 | break; | |
696 | case EXCP_IRQ: | |
697 | new_mode = ARM_CPU_MODE_IRQ; | |
698 | addr = 0x18; | |
699 | /* Disable IRQ and imprecise data aborts. */ | |
700 | mask = CPSR_A | CPSR_I; | |
701 | offset = 4; | |
702 | break; | |
703 | case EXCP_FIQ: | |
704 | new_mode = ARM_CPU_MODE_FIQ; | |
705 | addr = 0x1c; | |
706 | /* Disable FIQ, IRQ and imprecise data aborts. */ | |
707 | mask = CPSR_A | CPSR_I | CPSR_F; | |
708 | offset = 4; | |
709 | break; | |
710 | default: | |
711 | cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index); | |
712 | return; /* Never happens. Keep compiler happy. */ | |
713 | } | |
714 | /* High vectors. */ | |
715 | if (env->cp15.c1_sys & (1 << 13)) { | |
716 | addr += 0xffff0000; | |
717 | } | |
718 | switch_mode (env, new_mode); | |
719 | env->spsr = cpsr_read(env); | |
9ee6e8bb PB |
720 | /* Clear IT bits. */ |
721 | env->condexec_bits = 0; | |
30a8cac1 | 722 | /* Switch to the new mode, and to the correct instruction set. */ |
6d7e6326 | 723 | env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; |
b5ff1b31 | 724 | env->uncached_cpsr |= mask; |
be5e7a76 DES |
725 | /* this is a lie, as the was no c1_sys on V4T/V5, but who cares |
726 | * and we should just guard the thumb mode on V4 */ | |
727 | if (arm_feature(env, ARM_FEATURE_V4T)) { | |
728 | env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0; | |
729 | } | |
b5ff1b31 FB |
730 | env->regs[14] = env->regs[15] + offset; |
731 | env->regs[15] = addr; | |
732 | env->interrupt_request |= CPU_INTERRUPT_EXITTB; | |
733 | } | |
734 | ||
735 | /* Check section/page access permissions. | |
736 | Returns the page protection flags, or zero if the access is not | |
737 | permitted. */ | |
0ecb72a5 | 738 | static inline int check_ap(CPUARMState *env, int ap, int domain_prot, |
dd4ebc2e | 739 | int access_type, int is_user) |
b5ff1b31 | 740 | { |
9ee6e8bb PB |
741 | int prot_ro; |
742 | ||
dd4ebc2e | 743 | if (domain_prot == 3) { |
b5ff1b31 | 744 | return PAGE_READ | PAGE_WRITE; |
dd4ebc2e | 745 | } |
b5ff1b31 | 746 | |
9ee6e8bb PB |
747 | if (access_type == 1) |
748 | prot_ro = 0; | |
749 | else | |
750 | prot_ro = PAGE_READ; | |
751 | ||
b5ff1b31 FB |
752 | switch (ap) { |
753 | case 0: | |
78600320 | 754 | if (access_type == 1) |
b5ff1b31 FB |
755 | return 0; |
756 | switch ((env->cp15.c1_sys >> 8) & 3) { | |
757 | case 1: | |
758 | return is_user ? 0 : PAGE_READ; | |
759 | case 2: | |
760 | return PAGE_READ; | |
761 | default: | |
762 | return 0; | |
763 | } | |
764 | case 1: | |
765 | return is_user ? 0 : PAGE_READ | PAGE_WRITE; | |
766 | case 2: | |
767 | if (is_user) | |
9ee6e8bb | 768 | return prot_ro; |
b5ff1b31 FB |
769 | else |
770 | return PAGE_READ | PAGE_WRITE; | |
771 | case 3: | |
772 | return PAGE_READ | PAGE_WRITE; | |
d4934d18 | 773 | case 4: /* Reserved. */ |
9ee6e8bb PB |
774 | return 0; |
775 | case 5: | |
776 | return is_user ? 0 : prot_ro; | |
777 | case 6: | |
778 | return prot_ro; | |
d4934d18 | 779 | case 7: |
0ab06d83 | 780 | if (!arm_feature (env, ARM_FEATURE_V6K)) |
d4934d18 PB |
781 | return 0; |
782 | return prot_ro; | |
b5ff1b31 FB |
783 | default: |
784 | abort(); | |
785 | } | |
786 | } | |
787 | ||
0ecb72a5 | 788 | static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address) |
b2fa1797 PB |
789 | { |
790 | uint32_t table; | |
791 | ||
792 | if (address & env->cp15.c2_mask) | |
793 | table = env->cp15.c2_base1 & 0xffffc000; | |
794 | else | |
795 | table = env->cp15.c2_base0 & env->cp15.c2_base_mask; | |
796 | ||
797 | table |= (address >> 18) & 0x3ffc; | |
798 | return table; | |
799 | } | |
800 | ||
0ecb72a5 | 801 | static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type, |
d4c430a8 PB |
802 | int is_user, uint32_t *phys_ptr, int *prot, |
803 | target_ulong *page_size) | |
b5ff1b31 FB |
804 | { |
805 | int code; | |
806 | uint32_t table; | |
807 | uint32_t desc; | |
808 | int type; | |
809 | int ap; | |
810 | int domain; | |
dd4ebc2e | 811 | int domain_prot; |
b5ff1b31 FB |
812 | uint32_t phys_addr; |
813 | ||
9ee6e8bb PB |
814 | /* Pagetable walk. */ |
815 | /* Lookup l1 descriptor. */ | |
b2fa1797 | 816 | table = get_level1_table_address(env, address); |
9ee6e8bb PB |
817 | desc = ldl_phys(table); |
818 | type = (desc & 3); | |
dd4ebc2e JCD |
819 | domain = (desc >> 5) & 0x0f; |
820 | domain_prot = (env->cp15.c3 >> (domain * 2)) & 3; | |
9ee6e8bb | 821 | if (type == 0) { |
601d70b9 | 822 | /* Section translation fault. */ |
9ee6e8bb PB |
823 | code = 5; |
824 | goto do_fault; | |
825 | } | |
dd4ebc2e | 826 | if (domain_prot == 0 || domain_prot == 2) { |
9ee6e8bb PB |
827 | if (type == 2) |
828 | code = 9; /* Section domain fault. */ | |
829 | else | |
830 | code = 11; /* Page domain fault. */ | |
831 | goto do_fault; | |
832 | } | |
833 | if (type == 2) { | |
834 | /* 1Mb section. */ | |
835 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); | |
836 | ap = (desc >> 10) & 3; | |
837 | code = 13; | |
d4c430a8 | 838 | *page_size = 1024 * 1024; |
9ee6e8bb PB |
839 | } else { |
840 | /* Lookup l2 entry. */ | |
841 | if (type == 1) { | |
842 | /* Coarse pagetable. */ | |
843 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); | |
844 | } else { | |
845 | /* Fine pagetable. */ | |
846 | table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); | |
847 | } | |
848 | desc = ldl_phys(table); | |
849 | switch (desc & 3) { | |
850 | case 0: /* Page translation fault. */ | |
851 | code = 7; | |
852 | goto do_fault; | |
853 | case 1: /* 64k page. */ | |
854 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); | |
855 | ap = (desc >> (4 + ((address >> 13) & 6))) & 3; | |
d4c430a8 | 856 | *page_size = 0x10000; |
ce819861 | 857 | break; |
9ee6e8bb PB |
858 | case 2: /* 4k page. */ |
859 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
860 | ap = (desc >> (4 + ((address >> 13) & 6))) & 3; | |
d4c430a8 | 861 | *page_size = 0x1000; |
ce819861 | 862 | break; |
9ee6e8bb PB |
863 | case 3: /* 1k page. */ |
864 | if (type == 1) { | |
865 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { | |
866 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
867 | } else { | |
868 | /* Page translation fault. */ | |
869 | code = 7; | |
870 | goto do_fault; | |
871 | } | |
872 | } else { | |
873 | phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); | |
874 | } | |
875 | ap = (desc >> 4) & 3; | |
d4c430a8 | 876 | *page_size = 0x400; |
ce819861 PB |
877 | break; |
878 | default: | |
9ee6e8bb PB |
879 | /* Never happens, but compiler isn't smart enough to tell. */ |
880 | abort(); | |
ce819861 | 881 | } |
9ee6e8bb PB |
882 | code = 15; |
883 | } | |
dd4ebc2e | 884 | *prot = check_ap(env, ap, domain_prot, access_type, is_user); |
9ee6e8bb PB |
885 | if (!*prot) { |
886 | /* Access permission fault. */ | |
887 | goto do_fault; | |
888 | } | |
3ad493fc | 889 | *prot |= PAGE_EXEC; |
9ee6e8bb PB |
890 | *phys_ptr = phys_addr; |
891 | return 0; | |
892 | do_fault: | |
893 | return code | (domain << 4); | |
894 | } | |
895 | ||
0ecb72a5 | 896 | static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type, |
d4c430a8 PB |
897 | int is_user, uint32_t *phys_ptr, int *prot, |
898 | target_ulong *page_size) | |
9ee6e8bb PB |
899 | { |
900 | int code; | |
901 | uint32_t table; | |
902 | uint32_t desc; | |
903 | uint32_t xn; | |
904 | int type; | |
905 | int ap; | |
906 | int domain; | |
dd4ebc2e | 907 | int domain_prot; |
9ee6e8bb PB |
908 | uint32_t phys_addr; |
909 | ||
910 | /* Pagetable walk. */ | |
911 | /* Lookup l1 descriptor. */ | |
b2fa1797 | 912 | table = get_level1_table_address(env, address); |
9ee6e8bb PB |
913 | desc = ldl_phys(table); |
914 | type = (desc & 3); | |
915 | if (type == 0) { | |
601d70b9 | 916 | /* Section translation fault. */ |
9ee6e8bb PB |
917 | code = 5; |
918 | domain = 0; | |
919 | goto do_fault; | |
920 | } else if (type == 2 && (desc & (1 << 18))) { | |
921 | /* Supersection. */ | |
922 | domain = 0; | |
b5ff1b31 | 923 | } else { |
9ee6e8bb | 924 | /* Section or page. */ |
dd4ebc2e | 925 | domain = (desc >> 5) & 0x0f; |
9ee6e8bb | 926 | } |
dd4ebc2e JCD |
927 | domain_prot = (env->cp15.c3 >> (domain * 2)) & 3; |
928 | if (domain_prot == 0 || domain_prot == 2) { | |
9ee6e8bb PB |
929 | if (type == 2) |
930 | code = 9; /* Section domain fault. */ | |
931 | else | |
932 | code = 11; /* Page domain fault. */ | |
933 | goto do_fault; | |
934 | } | |
935 | if (type == 2) { | |
936 | if (desc & (1 << 18)) { | |
937 | /* Supersection. */ | |
938 | phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); | |
d4c430a8 | 939 | *page_size = 0x1000000; |
b5ff1b31 | 940 | } else { |
9ee6e8bb PB |
941 | /* Section. */ |
942 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); | |
d4c430a8 | 943 | *page_size = 0x100000; |
b5ff1b31 | 944 | } |
9ee6e8bb PB |
945 | ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); |
946 | xn = desc & (1 << 4); | |
947 | code = 13; | |
948 | } else { | |
949 | /* Lookup l2 entry. */ | |
950 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); | |
951 | desc = ldl_phys(table); | |
952 | ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); | |
953 | switch (desc & 3) { | |
954 | case 0: /* Page translation fault. */ | |
955 | code = 7; | |
b5ff1b31 | 956 | goto do_fault; |
9ee6e8bb PB |
957 | case 1: /* 64k page. */ |
958 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); | |
959 | xn = desc & (1 << 15); | |
d4c430a8 | 960 | *page_size = 0x10000; |
9ee6e8bb PB |
961 | break; |
962 | case 2: case 3: /* 4k page. */ | |
963 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
964 | xn = desc & 1; | |
d4c430a8 | 965 | *page_size = 0x1000; |
9ee6e8bb PB |
966 | break; |
967 | default: | |
968 | /* Never happens, but compiler isn't smart enough to tell. */ | |
969 | abort(); | |
b5ff1b31 | 970 | } |
9ee6e8bb PB |
971 | code = 15; |
972 | } | |
dd4ebc2e | 973 | if (domain_prot == 3) { |
c0034328 JR |
974 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
975 | } else { | |
976 | if (xn && access_type == 2) | |
977 | goto do_fault; | |
9ee6e8bb | 978 | |
c0034328 JR |
979 | /* The simplified model uses AP[0] as an access control bit. */ |
980 | if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) { | |
981 | /* Access flag fault. */ | |
982 | code = (code == 15) ? 6 : 3; | |
983 | goto do_fault; | |
984 | } | |
dd4ebc2e | 985 | *prot = check_ap(env, ap, domain_prot, access_type, is_user); |
c0034328 JR |
986 | if (!*prot) { |
987 | /* Access permission fault. */ | |
988 | goto do_fault; | |
989 | } | |
990 | if (!xn) { | |
991 | *prot |= PAGE_EXEC; | |
992 | } | |
3ad493fc | 993 | } |
9ee6e8bb | 994 | *phys_ptr = phys_addr; |
b5ff1b31 FB |
995 | return 0; |
996 | do_fault: | |
997 | return code | (domain << 4); | |
998 | } | |
999 | ||
0ecb72a5 | 1000 | static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type, |
9ee6e8bb PB |
1001 | int is_user, uint32_t *phys_ptr, int *prot) |
1002 | { | |
1003 | int n; | |
1004 | uint32_t mask; | |
1005 | uint32_t base; | |
1006 | ||
1007 | *phys_ptr = address; | |
1008 | for (n = 7; n >= 0; n--) { | |
1009 | base = env->cp15.c6_region[n]; | |
1010 | if ((base & 1) == 0) | |
1011 | continue; | |
1012 | mask = 1 << ((base >> 1) & 0x1f); | |
1013 | /* Keep this shift separate from the above to avoid an | |
1014 | (undefined) << 32. */ | |
1015 | mask = (mask << 1) - 1; | |
1016 | if (((base ^ address) & ~mask) == 0) | |
1017 | break; | |
1018 | } | |
1019 | if (n < 0) | |
1020 | return 2; | |
1021 | ||
1022 | if (access_type == 2) { | |
1023 | mask = env->cp15.c5_insn; | |
1024 | } else { | |
1025 | mask = env->cp15.c5_data; | |
1026 | } | |
1027 | mask = (mask >> (n * 4)) & 0xf; | |
1028 | switch (mask) { | |
1029 | case 0: | |
1030 | return 1; | |
1031 | case 1: | |
1032 | if (is_user) | |
1033 | return 1; | |
1034 | *prot = PAGE_READ | PAGE_WRITE; | |
1035 | break; | |
1036 | case 2: | |
1037 | *prot = PAGE_READ; | |
1038 | if (!is_user) | |
1039 | *prot |= PAGE_WRITE; | |
1040 | break; | |
1041 | case 3: | |
1042 | *prot = PAGE_READ | PAGE_WRITE; | |
1043 | break; | |
1044 | case 5: | |
1045 | if (is_user) | |
1046 | return 1; | |
1047 | *prot = PAGE_READ; | |
1048 | break; | |
1049 | case 6: | |
1050 | *prot = PAGE_READ; | |
1051 | break; | |
1052 | default: | |
1053 | /* Bad permission. */ | |
1054 | return 1; | |
1055 | } | |
3ad493fc | 1056 | *prot |= PAGE_EXEC; |
9ee6e8bb PB |
1057 | return 0; |
1058 | } | |
1059 | ||
0ecb72a5 | 1060 | static inline int get_phys_addr(CPUARMState *env, uint32_t address, |
9ee6e8bb | 1061 | int access_type, int is_user, |
d4c430a8 PB |
1062 | uint32_t *phys_ptr, int *prot, |
1063 | target_ulong *page_size) | |
9ee6e8bb PB |
1064 | { |
1065 | /* Fast Context Switch Extension. */ | |
1066 | if (address < 0x02000000) | |
1067 | address += env->cp15.c13_fcse; | |
1068 | ||
1069 | if ((env->cp15.c1_sys & 1) == 0) { | |
1070 | /* MMU/MPU disabled. */ | |
1071 | *phys_ptr = address; | |
3ad493fc | 1072 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
d4c430a8 | 1073 | *page_size = TARGET_PAGE_SIZE; |
9ee6e8bb PB |
1074 | return 0; |
1075 | } else if (arm_feature(env, ARM_FEATURE_MPU)) { | |
d4c430a8 | 1076 | *page_size = TARGET_PAGE_SIZE; |
9ee6e8bb PB |
1077 | return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr, |
1078 | prot); | |
1079 | } else if (env->cp15.c1_sys & (1 << 23)) { | |
1080 | return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr, | |
d4c430a8 | 1081 | prot, page_size); |
9ee6e8bb PB |
1082 | } else { |
1083 | return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr, | |
d4c430a8 | 1084 | prot, page_size); |
9ee6e8bb PB |
1085 | } |
1086 | } | |
1087 | ||
0ecb72a5 | 1088 | int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, |
97b348e7 | 1089 | int access_type, int mmu_idx) |
b5ff1b31 FB |
1090 | { |
1091 | uint32_t phys_addr; | |
d4c430a8 | 1092 | target_ulong page_size; |
b5ff1b31 | 1093 | int prot; |
6ebbf390 | 1094 | int ret, is_user; |
b5ff1b31 | 1095 | |
6ebbf390 | 1096 | is_user = mmu_idx == MMU_USER_IDX; |
d4c430a8 PB |
1097 | ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot, |
1098 | &page_size); | |
b5ff1b31 FB |
1099 | if (ret == 0) { |
1100 | /* Map a single [sub]page. */ | |
1101 | phys_addr &= ~(uint32_t)0x3ff; | |
1102 | address &= ~(uint32_t)0x3ff; | |
3ad493fc | 1103 | tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size); |
d4c430a8 | 1104 | return 0; |
b5ff1b31 FB |
1105 | } |
1106 | ||
1107 | if (access_type == 2) { | |
1108 | env->cp15.c5_insn = ret; | |
1109 | env->cp15.c6_insn = address; | |
1110 | env->exception_index = EXCP_PREFETCH_ABORT; | |
1111 | } else { | |
1112 | env->cp15.c5_data = ret; | |
9ee6e8bb PB |
1113 | if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) |
1114 | env->cp15.c5_data |= (1 << 11); | |
b5ff1b31 FB |
1115 | env->cp15.c6_data = address; |
1116 | env->exception_index = EXCP_DATA_ABORT; | |
1117 | } | |
1118 | return 1; | |
1119 | } | |
1120 | ||
0ecb72a5 | 1121 | target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr) |
b5ff1b31 FB |
1122 | { |
1123 | uint32_t phys_addr; | |
d4c430a8 | 1124 | target_ulong page_size; |
b5ff1b31 FB |
1125 | int prot; |
1126 | int ret; | |
1127 | ||
d4c430a8 | 1128 | ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size); |
b5ff1b31 FB |
1129 | |
1130 | if (ret != 0) | |
1131 | return -1; | |
1132 | ||
1133 | return phys_addr; | |
1134 | } | |
1135 | ||
ce819861 PB |
1136 | /* Return basic MPU access permission bits. */ |
1137 | static uint32_t simple_mpu_ap_bits(uint32_t val) | |
1138 | { | |
1139 | uint32_t ret; | |
1140 | uint32_t mask; | |
1141 | int i; | |
1142 | ret = 0; | |
1143 | mask = 3; | |
1144 | for (i = 0; i < 16; i += 2) { | |
1145 | ret |= (val >> i) & mask; | |
1146 | mask <<= 2; | |
1147 | } | |
1148 | return ret; | |
1149 | } | |
1150 | ||
1151 | /* Pad basic MPU access permission bits to extended format. */ | |
1152 | static uint32_t extended_mpu_ap_bits(uint32_t val) | |
1153 | { | |
1154 | uint32_t ret; | |
1155 | uint32_t mask; | |
1156 | int i; | |
1157 | ret = 0; | |
1158 | mask = 3; | |
1159 | for (i = 0; i < 16; i += 2) { | |
1160 | ret |= (val & mask) << i; | |
1161 | mask <<= 2; | |
1162 | } | |
1163 | return ret; | |
1164 | } | |
1165 | ||
0ecb72a5 | 1166 | void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val) |
b5ff1b31 | 1167 | { |
9ee6e8bb PB |
1168 | int op1; |
1169 | int op2; | |
1170 | int crm; | |
b5ff1b31 | 1171 | |
9ee6e8bb | 1172 | op1 = (insn >> 21) & 7; |
b5ff1b31 | 1173 | op2 = (insn >> 5) & 7; |
ce819861 | 1174 | crm = insn & 0xf; |
b5ff1b31 | 1175 | switch ((insn >> 16) & 0xf) { |
9ee6e8bb | 1176 | case 0: |
9ee6e8bb | 1177 | /* ID codes. */ |
610c3c8a AZ |
1178 | if (arm_feature(env, ARM_FEATURE_XSCALE)) |
1179 | break; | |
c3d2689d AZ |
1180 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1181 | break; | |
a49ea279 PB |
1182 | if (arm_feature(env, ARM_FEATURE_V7) |
1183 | && op1 == 2 && crm == 0 && op2 == 0) { | |
1184 | env->cp15.c0_cssel = val & 0xf; | |
1185 | break; | |
1186 | } | |
b5ff1b31 FB |
1187 | goto bad_reg; |
1188 | case 1: /* System configuration. */ | |
2be27624 RH |
1189 | if (arm_feature(env, ARM_FEATURE_V7) |
1190 | && op1 == 0 && crm == 1 && op2 == 0) { | |
1191 | env->cp15.c1_scr = val; | |
1192 | break; | |
1193 | } | |
c3d2689d AZ |
1194 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1195 | op2 = 0; | |
b5ff1b31 FB |
1196 | switch (op2) { |
1197 | case 0: | |
ce819861 | 1198 | if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0) |
c1713132 | 1199 | env->cp15.c1_sys = val; |
b5ff1b31 FB |
1200 | /* ??? Lots of these bits are not implemented. */ |
1201 | /* This may enable/disable the MMU, so do a TLB flush. */ | |
1202 | tlb_flush(env, 1); | |
1203 | break; | |
61cc8701 | 1204 | case 1: /* Auxiliary control register. */ |
610c3c8a AZ |
1205 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { |
1206 | env->cp15.c1_xscaleauxcr = val; | |
c1713132 | 1207 | break; |
610c3c8a | 1208 | } |
9ee6e8bb PB |
1209 | /* Not implemented. */ |
1210 | break; | |
b5ff1b31 | 1211 | case 2: |
610c3c8a AZ |
1212 | if (arm_feature(env, ARM_FEATURE_XSCALE)) |
1213 | goto bad_reg; | |
4be27dbb PB |
1214 | if (env->cp15.c1_coproc != val) { |
1215 | env->cp15.c1_coproc = val; | |
1216 | /* ??? Is this safe when called from within a TB? */ | |
1217 | tb_flush(env); | |
1218 | } | |
c1713132 | 1219 | break; |
b5ff1b31 FB |
1220 | default: |
1221 | goto bad_reg; | |
1222 | } | |
1223 | break; | |
ce819861 PB |
1224 | case 2: /* MMU Page table control / MPU cache control. */ |
1225 | if (arm_feature(env, ARM_FEATURE_MPU)) { | |
1226 | switch (op2) { | |
1227 | case 0: | |
1228 | env->cp15.c2_data = val; | |
1229 | break; | |
1230 | case 1: | |
1231 | env->cp15.c2_insn = val; | |
1232 | break; | |
1233 | default: | |
1234 | goto bad_reg; | |
1235 | } | |
1236 | } else { | |
9ee6e8bb PB |
1237 | switch (op2) { |
1238 | case 0: | |
1239 | env->cp15.c2_base0 = val; | |
1240 | break; | |
1241 | case 1: | |
1242 | env->cp15.c2_base1 = val; | |
1243 | break; | |
1244 | case 2: | |
b2fa1797 PB |
1245 | val &= 7; |
1246 | env->cp15.c2_control = val; | |
9ee6e8bb | 1247 | env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val); |
b2fa1797 | 1248 | env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val); |
9ee6e8bb PB |
1249 | break; |
1250 | default: | |
1251 | goto bad_reg; | |
1252 | } | |
ce819861 | 1253 | } |
b5ff1b31 | 1254 | break; |
ce819861 | 1255 | case 3: /* MMU Domain access control / MPU write buffer control. */ |
b5ff1b31 | 1256 | env->cp15.c3 = val; |
405ee3ad | 1257 | tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */ |
b5ff1b31 FB |
1258 | break; |
1259 | case 4: /* Reserved. */ | |
1260 | goto bad_reg; | |
ce819861 | 1261 | case 5: /* MMU Fault status / MPU access permission. */ |
c3d2689d AZ |
1262 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1263 | op2 = 0; | |
b5ff1b31 FB |
1264 | switch (op2) { |
1265 | case 0: | |
ce819861 PB |
1266 | if (arm_feature(env, ARM_FEATURE_MPU)) |
1267 | val = extended_mpu_ap_bits(val); | |
b5ff1b31 FB |
1268 | env->cp15.c5_data = val; |
1269 | break; | |
1270 | case 1: | |
ce819861 PB |
1271 | if (arm_feature(env, ARM_FEATURE_MPU)) |
1272 | val = extended_mpu_ap_bits(val); | |
b5ff1b31 FB |
1273 | env->cp15.c5_insn = val; |
1274 | break; | |
ce819861 PB |
1275 | case 2: |
1276 | if (!arm_feature(env, ARM_FEATURE_MPU)) | |
1277 | goto bad_reg; | |
1278 | env->cp15.c5_data = val; | |
b5ff1b31 | 1279 | break; |
ce819861 PB |
1280 | case 3: |
1281 | if (!arm_feature(env, ARM_FEATURE_MPU)) | |
1282 | goto bad_reg; | |
1283 | env->cp15.c5_insn = val; | |
b5ff1b31 FB |
1284 | break; |
1285 | default: | |
1286 | goto bad_reg; | |
1287 | } | |
1288 | break; | |
ce819861 PB |
1289 | case 6: /* MMU Fault address / MPU base/size. */ |
1290 | if (arm_feature(env, ARM_FEATURE_MPU)) { | |
1291 | if (crm >= 8) | |
1292 | goto bad_reg; | |
1293 | env->cp15.c6_region[crm] = val; | |
1294 | } else { | |
c3d2689d AZ |
1295 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1296 | op2 = 0; | |
ce819861 PB |
1297 | switch (op2) { |
1298 | case 0: | |
1299 | env->cp15.c6_data = val; | |
1300 | break; | |
9ee6e8bb PB |
1301 | case 1: /* ??? This is WFAR on armv6 */ |
1302 | case 2: | |
ce819861 PB |
1303 | env->cp15.c6_insn = val; |
1304 | break; | |
1305 | default: | |
1306 | goto bad_reg; | |
1307 | } | |
1308 | } | |
1309 | break; | |
b5ff1b31 | 1310 | case 7: /* Cache control. */ |
c3d2689d AZ |
1311 | env->cp15.c15_i_max = 0x000; |
1312 | env->cp15.c15_i_min = 0xff0; | |
f8bf8606 AL |
1313 | if (op1 != 0) { |
1314 | goto bad_reg; | |
1315 | } | |
1316 | /* No cache, so nothing to do except VA->PA translations. */ | |
906879a9 | 1317 | if (arm_feature(env, ARM_FEATURE_VAPA)) { |
f8bf8606 AL |
1318 | switch (crm) { |
1319 | case 4: | |
1320 | if (arm_feature(env, ARM_FEATURE_V7)) { | |
1321 | env->cp15.c7_par = val & 0xfffff6ff; | |
1322 | } else { | |
1323 | env->cp15.c7_par = val & 0xfffff1ff; | |
1324 | } | |
1325 | break; | |
1326 | case 8: { | |
1327 | uint32_t phys_addr; | |
1328 | target_ulong page_size; | |
1329 | int prot; | |
1330 | int ret, is_user = op2 & 2; | |
1331 | int access_type = op2 & 1; | |
1332 | ||
1333 | if (op2 & 4) { | |
1334 | /* Other states are only available with TrustZone */ | |
1335 | goto bad_reg; | |
1336 | } | |
1337 | ret = get_phys_addr(env, val, access_type, is_user, | |
1338 | &phys_addr, &prot, &page_size); | |
1339 | if (ret == 0) { | |
1340 | /* We do not set any attribute bits in the PAR */ | |
1341 | if (page_size == (1 << 24) | |
1342 | && arm_feature(env, ARM_FEATURE_V7)) { | |
1343 | env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1; | |
1344 | } else { | |
1345 | env->cp15.c7_par = phys_addr & 0xfffff000; | |
1346 | } | |
1347 | } else { | |
1348 | env->cp15.c7_par = ((ret & (10 << 1)) >> 5) | | |
1349 | ((ret & (12 << 1)) >> 6) | | |
1350 | ((ret & 0xf) << 1) | 1; | |
1351 | } | |
1352 | break; | |
1353 | } | |
1354 | } | |
1355 | } | |
b5ff1b31 FB |
1356 | break; |
1357 | case 8: /* MMU TLB control. */ | |
1358 | switch (op2) { | |
dc8714ca PM |
1359 | case 0: /* Invalidate all (TLBIALL) */ |
1360 | tlb_flush(env, 1); | |
b5ff1b31 | 1361 | break; |
dc8714ca | 1362 | case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ |
d4c430a8 | 1363 | tlb_flush_page(env, val & TARGET_PAGE_MASK); |
b5ff1b31 | 1364 | break; |
dc8714ca | 1365 | case 2: /* Invalidate by ASID (TLBIASID) */ |
9ee6e8bb PB |
1366 | tlb_flush(env, val == 0); |
1367 | break; | |
dc8714ca PM |
1368 | case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ |
1369 | tlb_flush_page(env, val & TARGET_PAGE_MASK); | |
9ee6e8bb | 1370 | break; |
b5ff1b31 FB |
1371 | default: |
1372 | goto bad_reg; | |
1373 | } | |
1374 | break; | |
ce819861 | 1375 | case 9: |
c3d2689d AZ |
1376 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1377 | break; | |
5bc95aa2 DES |
1378 | if (arm_feature(env, ARM_FEATURE_STRONGARM)) |
1379 | break; /* Ignore ReadBuffer access */ | |
ce819861 PB |
1380 | switch (crm) { |
1381 | case 0: /* Cache lockdown. */ | |
9ee6e8bb PB |
1382 | switch (op1) { |
1383 | case 0: /* L1 cache. */ | |
1384 | switch (op2) { | |
1385 | case 0: | |
1386 | env->cp15.c9_data = val; | |
1387 | break; | |
1388 | case 1: | |
1389 | env->cp15.c9_insn = val; | |
1390 | break; | |
1391 | default: | |
1392 | goto bad_reg; | |
1393 | } | |
1394 | break; | |
1395 | case 1: /* L2 cache. */ | |
1396 | /* Ignore writes to L2 lockdown/auxiliary registers. */ | |
1397 | break; | |
1398 | default: | |
1399 | goto bad_reg; | |
1400 | } | |
1401 | break; | |
ce819861 PB |
1402 | case 1: /* TCM memory region registers. */ |
1403 | /* Not implemented. */ | |
1404 | goto bad_reg; | |
74594c9d PM |
1405 | case 12: /* Performance monitor control */ |
1406 | /* Performance monitors are implementation defined in v7, | |
1407 | * but with an ARM recommended set of registers, which we | |
1408 | * follow (although we don't actually implement any counters) | |
1409 | */ | |
1410 | if (!arm_feature(env, ARM_FEATURE_V7)) { | |
1411 | goto bad_reg; | |
1412 | } | |
1413 | switch (op2) { | |
1414 | case 0: /* performance monitor control register */ | |
1415 | /* only the DP, X, D and E bits are writable */ | |
1416 | env->cp15.c9_pmcr &= ~0x39; | |
1417 | env->cp15.c9_pmcr |= (val & 0x39); | |
1418 | break; | |
1419 | case 1: /* Count enable set register */ | |
1420 | val &= (1 << 31); | |
1421 | env->cp15.c9_pmcnten |= val; | |
1422 | break; | |
1423 | case 2: /* Count enable clear */ | |
1424 | val &= (1 << 31); | |
1425 | env->cp15.c9_pmcnten &= ~val; | |
1426 | break; | |
1427 | case 3: /* Overflow flag status */ | |
1428 | env->cp15.c9_pmovsr &= ~val; | |
1429 | break; | |
1430 | case 4: /* Software increment */ | |
1431 | /* RAZ/WI since we don't implement the software-count event */ | |
1432 | break; | |
1433 | case 5: /* Event counter selection register */ | |
1434 | /* Since we don't implement any events, writing to this register | |
1435 | * is actually UNPREDICTABLE. So we choose to RAZ/WI. | |
1436 | */ | |
1437 | break; | |
1438 | default: | |
1439 | goto bad_reg; | |
1440 | } | |
1441 | break; | |
1442 | case 13: /* Performance counters */ | |
1443 | if (!arm_feature(env, ARM_FEATURE_V7)) { | |
1444 | goto bad_reg; | |
1445 | } | |
1446 | switch (op2) { | |
1447 | case 0: /* Cycle count register: not implemented, so RAZ/WI */ | |
1448 | break; | |
1449 | case 1: /* Event type select */ | |
1450 | env->cp15.c9_pmxevtyper = val & 0xff; | |
1451 | break; | |
1452 | case 2: /* Event count register */ | |
1453 | /* Unimplemented (we have no events), RAZ/WI */ | |
1454 | break; | |
1455 | default: | |
1456 | goto bad_reg; | |
1457 | } | |
1458 | break; | |
1459 | case 14: /* Performance monitor control */ | |
1460 | if (!arm_feature(env, ARM_FEATURE_V7)) { | |
1461 | goto bad_reg; | |
1462 | } | |
1463 | switch (op2) { | |
1464 | case 0: /* user enable */ | |
1465 | env->cp15.c9_pmuserenr = val & 1; | |
1466 | /* changes access rights for cp registers, so flush tbs */ | |
1467 | tb_flush(env); | |
1468 | break; | |
1469 | case 1: /* interrupt enable set */ | |
1470 | /* We have no event counters so only the C bit can be changed */ | |
1471 | val &= (1 << 31); | |
1472 | env->cp15.c9_pminten |= val; | |
1473 | break; | |
1474 | case 2: /* interrupt enable clear */ | |
1475 | val &= (1 << 31); | |
1476 | env->cp15.c9_pminten &= ~val; | |
1477 | break; | |
1478 | } | |
1479 | break; | |
b5ff1b31 FB |
1480 | default: |
1481 | goto bad_reg; | |
1482 | } | |
1483 | break; | |
1484 | case 10: /* MMU TLB lockdown. */ | |
1485 | /* ??? TLB lockdown not implemented. */ | |
1486 | break; | |
b5ff1b31 FB |
1487 | case 12: /* Reserved. */ |
1488 | goto bad_reg; | |
1489 | case 13: /* Process ID. */ | |
1490 | switch (op2) { | |
1491 | case 0: | |
d07edbfa PB |
1492 | /* Unlike real hardware the qemu TLB uses virtual addresses, |
1493 | not modified virtual addresses, so this causes a TLB flush. | |
1494 | */ | |
1495 | if (env->cp15.c13_fcse != val) | |
1496 | tlb_flush(env, 1); | |
1497 | env->cp15.c13_fcse = val; | |
b5ff1b31 FB |
1498 | break; |
1499 | case 1: | |
d07edbfa | 1500 | /* This changes the ASID, so do a TLB flush. */ |
ce819861 PB |
1501 | if (env->cp15.c13_context != val |
1502 | && !arm_feature(env, ARM_FEATURE_MPU)) | |
d07edbfa PB |
1503 | tlb_flush(env, 0); |
1504 | env->cp15.c13_context = val; | |
b5ff1b31 FB |
1505 | break; |
1506 | default: | |
1507 | goto bad_reg; | |
1508 | } | |
1509 | break; | |
0383ac00 PM |
1510 | case 14: /* Generic timer */ |
1511 | if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { | |
1512 | /* Dummy implementation: RAZ/WI for all */ | |
1513 | break; | |
1514 | } | |
b5ff1b31 FB |
1515 | goto bad_reg; |
1516 | case 15: /* Implementation specific. */ | |
c1713132 | 1517 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { |
ce819861 | 1518 | if (op2 == 0 && crm == 1) { |
2e23213f AZ |
1519 | if (env->cp15.c15_cpar != (val & 0x3fff)) { |
1520 | /* Changes cp0 to cp13 behavior, so needs a TB flush. */ | |
1521 | tb_flush(env); | |
1522 | env->cp15.c15_cpar = val & 0x3fff; | |
1523 | } | |
c1713132 AZ |
1524 | break; |
1525 | } | |
1526 | goto bad_reg; | |
1527 | } | |
c3d2689d AZ |
1528 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) { |
1529 | switch (crm) { | |
1530 | case 0: | |
1531 | break; | |
1532 | case 1: /* Set TI925T configuration. */ | |
1533 | env->cp15.c15_ticonfig = val & 0xe7; | |
1534 | env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */ | |
1535 | ARM_CPUID_TI915T : ARM_CPUID_TI925T; | |
1536 | break; | |
1537 | case 2: /* Set I_max. */ | |
1538 | env->cp15.c15_i_max = val; | |
1539 | break; | |
1540 | case 3: /* Set I_min. */ | |
1541 | env->cp15.c15_i_min = val; | |
1542 | break; | |
1543 | case 4: /* Set thread-ID. */ | |
1544 | env->cp15.c15_threadid = val & 0xffff; | |
1545 | break; | |
1546 | case 8: /* Wait-for-interrupt (deprecated). */ | |
1547 | cpu_interrupt(env, CPU_INTERRUPT_HALT); | |
1548 | break; | |
1549 | default: | |
1550 | goto bad_reg; | |
1551 | } | |
1552 | } | |
7da362d0 ML |
1553 | if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) { |
1554 | switch (crm) { | |
1555 | case 0: | |
1556 | if ((op1 == 0) && (op2 == 0)) { | |
1557 | env->cp15.c15_power_control = val; | |
1558 | } else if ((op1 == 0) && (op2 == 1)) { | |
1559 | env->cp15.c15_diagnostic = val; | |
1560 | } else if ((op1 == 0) && (op2 == 2)) { | |
1561 | env->cp15.c15_power_diagnostic = val; | |
1562 | } | |
1563 | default: | |
1564 | break; | |
1565 | } | |
1566 | } | |
b5ff1b31 FB |
1567 | break; |
1568 | } | |
1569 | return; | |
1570 | bad_reg: | |
1571 | /* ??? For debugging only. Should raise illegal instruction exception. */ | |
9ee6e8bb PB |
1572 | cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n", |
1573 | (insn >> 16) & 0xf, crm, op1, op2); | |
b5ff1b31 FB |
1574 | } |
1575 | ||
0ecb72a5 | 1576 | uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn) |
b5ff1b31 | 1577 | { |
9ee6e8bb PB |
1578 | int op1; |
1579 | int op2; | |
1580 | int crm; | |
b5ff1b31 | 1581 | |
9ee6e8bb | 1582 | op1 = (insn >> 21) & 7; |
b5ff1b31 | 1583 | op2 = (insn >> 5) & 7; |
c3d2689d | 1584 | crm = insn & 0xf; |
b5ff1b31 FB |
1585 | switch ((insn >> 16) & 0xf) { |
1586 | case 0: /* ID codes. */ | |
9ee6e8bb PB |
1587 | switch (op1) { |
1588 | case 0: | |
1589 | switch (crm) { | |
1590 | case 0: | |
1591 | switch (op2) { | |
1592 | case 0: /* Device ID. */ | |
1593 | return env->cp15.c0_cpuid; | |
1594 | case 1: /* Cache Type. */ | |
1595 | return env->cp15.c0_cachetype; | |
1596 | case 2: /* TCM status. */ | |
1597 | return 0; | |
1598 | case 3: /* TLB type register. */ | |
1599 | return 0; /* No lockable TLB entries. */ | |
607b4b08 PM |
1600 | case 5: /* MPIDR */ |
1601 | /* The MPIDR was standardised in v7; prior to | |
1602 | * this it was implemented only in the 11MPCore. | |
1603 | * For all other pre-v7 cores it does not exist. | |
1604 | */ | |
1605 | if (arm_feature(env, ARM_FEATURE_V7) || | |
1606 | ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) { | |
1607 | int mpidr = env->cpu_index; | |
1608 | /* We don't support setting cluster ID ([8..11]) | |
1609 | * so these bits always RAZ. | |
1610 | */ | |
1611 | if (arm_feature(env, ARM_FEATURE_V7MP)) { | |
1612 | mpidr |= (1 << 31); | |
1613 | /* Cores which are uniprocessor (non-coherent) | |
1614 | * but still implement the MP extensions set | |
1615 | * bit 30. (For instance, A9UP.) However we do | |
1616 | * not currently model any of those cores. | |
1617 | */ | |
1618 | } | |
1619 | return mpidr; | |
10055562 | 1620 | } |
607b4b08 | 1621 | /* otherwise fall through to the unimplemented-reg case */ |
9ee6e8bb PB |
1622 | default: |
1623 | goto bad_reg; | |
1624 | } | |
1625 | case 1: | |
1626 | if (!arm_feature(env, ARM_FEATURE_V6)) | |
1627 | goto bad_reg; | |
1628 | return env->cp15.c0_c1[op2]; | |
1629 | case 2: | |
1630 | if (!arm_feature(env, ARM_FEATURE_V6)) | |
1631 | goto bad_reg; | |
1632 | return env->cp15.c0_c2[op2]; | |
1633 | case 3: case 4: case 5: case 6: case 7: | |
1634 | return 0; | |
1635 | default: | |
1636 | goto bad_reg; | |
1637 | } | |
1638 | case 1: | |
1639 | /* These registers aren't documented on arm11 cores. However | |
1640 | Linux looks at them anyway. */ | |
1641 | if (!arm_feature(env, ARM_FEATURE_V6)) | |
1642 | goto bad_reg; | |
1643 | if (crm != 0) | |
1644 | goto bad_reg; | |
a49ea279 PB |
1645 | if (!arm_feature(env, ARM_FEATURE_V7)) |
1646 | return 0; | |
1647 | ||
1648 | switch (op2) { | |
1649 | case 0: | |
1650 | return env->cp15.c0_ccsid[env->cp15.c0_cssel]; | |
1651 | case 1: | |
1652 | return env->cp15.c0_clid; | |
1653 | case 7: | |
1654 | return 0; | |
1655 | } | |
1656 | goto bad_reg; | |
1657 | case 2: | |
1658 | if (op2 != 0 || crm != 0) | |
610c3c8a | 1659 | goto bad_reg; |
a49ea279 | 1660 | return env->cp15.c0_cssel; |
9ee6e8bb PB |
1661 | default: |
1662 | goto bad_reg; | |
b5ff1b31 FB |
1663 | } |
1664 | case 1: /* System configuration. */ | |
2be27624 RH |
1665 | if (arm_feature(env, ARM_FEATURE_V7) |
1666 | && op1 == 0 && crm == 1 && op2 == 0) { | |
1667 | return env->cp15.c1_scr; | |
1668 | } | |
c3d2689d AZ |
1669 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1670 | op2 = 0; | |
b5ff1b31 FB |
1671 | switch (op2) { |
1672 | case 0: /* Control register. */ | |
1673 | return env->cp15.c1_sys; | |
1674 | case 1: /* Auxiliary control register. */ | |
c1713132 | 1675 | if (arm_feature(env, ARM_FEATURE_XSCALE)) |
610c3c8a | 1676 | return env->cp15.c1_xscaleauxcr; |
9ee6e8bb PB |
1677 | if (!arm_feature(env, ARM_FEATURE_AUXCR)) |
1678 | goto bad_reg; | |
1679 | switch (ARM_CPUID(env)) { | |
1680 | case ARM_CPUID_ARM1026: | |
1681 | return 1; | |
1682 | case ARM_CPUID_ARM1136: | |
827df9f3 | 1683 | case ARM_CPUID_ARM1136_R2: |
7807eed9 | 1684 | case ARM_CPUID_ARM1176: |
9ee6e8bb PB |
1685 | return 7; |
1686 | case ARM_CPUID_ARM11MPCORE: | |
1687 | return 1; | |
1688 | case ARM_CPUID_CORTEXA8: | |
533d177a | 1689 | return 2; |
10055562 | 1690 | case ARM_CPUID_CORTEXA9: |
0b03bdfc | 1691 | case ARM_CPUID_CORTEXA15: |
10055562 | 1692 | return 0; |
9ee6e8bb PB |
1693 | default: |
1694 | goto bad_reg; | |
1695 | } | |
b5ff1b31 | 1696 | case 2: /* Coprocessor access register. */ |
610c3c8a AZ |
1697 | if (arm_feature(env, ARM_FEATURE_XSCALE)) |
1698 | goto bad_reg; | |
b5ff1b31 FB |
1699 | return env->cp15.c1_coproc; |
1700 | default: | |
1701 | goto bad_reg; | |
1702 | } | |
ce819861 PB |
1703 | case 2: /* MMU Page table control / MPU cache control. */ |
1704 | if (arm_feature(env, ARM_FEATURE_MPU)) { | |
1705 | switch (op2) { | |
1706 | case 0: | |
1707 | return env->cp15.c2_data; | |
1708 | break; | |
1709 | case 1: | |
1710 | return env->cp15.c2_insn; | |
1711 | break; | |
1712 | default: | |
1713 | goto bad_reg; | |
1714 | } | |
1715 | } else { | |
9ee6e8bb PB |
1716 | switch (op2) { |
1717 | case 0: | |
1718 | return env->cp15.c2_base0; | |
1719 | case 1: | |
1720 | return env->cp15.c2_base1; | |
1721 | case 2: | |
b2fa1797 | 1722 | return env->cp15.c2_control; |
9ee6e8bb PB |
1723 | default: |
1724 | goto bad_reg; | |
1725 | } | |
1726 | } | |
ce819861 | 1727 | case 3: /* MMU Domain access control / MPU write buffer control. */ |
b5ff1b31 FB |
1728 | return env->cp15.c3; |
1729 | case 4: /* Reserved. */ | |
1730 | goto bad_reg; | |
ce819861 | 1731 | case 5: /* MMU Fault status / MPU access permission. */ |
c3d2689d AZ |
1732 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1733 | op2 = 0; | |
b5ff1b31 FB |
1734 | switch (op2) { |
1735 | case 0: | |
ce819861 PB |
1736 | if (arm_feature(env, ARM_FEATURE_MPU)) |
1737 | return simple_mpu_ap_bits(env->cp15.c5_data); | |
b5ff1b31 FB |
1738 | return env->cp15.c5_data; |
1739 | case 1: | |
ce819861 | 1740 | if (arm_feature(env, ARM_FEATURE_MPU)) |
4de47793 | 1741 | return simple_mpu_ap_bits(env->cp15.c5_insn); |
ce819861 PB |
1742 | return env->cp15.c5_insn; |
1743 | case 2: | |
1744 | if (!arm_feature(env, ARM_FEATURE_MPU)) | |
1745 | goto bad_reg; | |
1746 | return env->cp15.c5_data; | |
1747 | case 3: | |
1748 | if (!arm_feature(env, ARM_FEATURE_MPU)) | |
1749 | goto bad_reg; | |
b5ff1b31 FB |
1750 | return env->cp15.c5_insn; |
1751 | default: | |
1752 | goto bad_reg; | |
1753 | } | |
9ee6e8bb | 1754 | case 6: /* MMU Fault address. */ |
ce819861 | 1755 | if (arm_feature(env, ARM_FEATURE_MPU)) { |
9ee6e8bb | 1756 | if (crm >= 8) |
ce819861 | 1757 | goto bad_reg; |
9ee6e8bb | 1758 | return env->cp15.c6_region[crm]; |
ce819861 | 1759 | } else { |
c3d2689d AZ |
1760 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) |
1761 | op2 = 0; | |
9ee6e8bb PB |
1762 | switch (op2) { |
1763 | case 0: | |
1764 | return env->cp15.c6_data; | |
1765 | case 1: | |
1766 | if (arm_feature(env, ARM_FEATURE_V6)) { | |
1767 | /* Watchpoint Fault Adrress. */ | |
1768 | return 0; /* Not implemented. */ | |
1769 | } else { | |
1770 | /* Instruction Fault Adrress. */ | |
1771 | /* Arm9 doesn't have an IFAR, but implementing it anyway | |
1772 | shouldn't do any harm. */ | |
1773 | return env->cp15.c6_insn; | |
1774 | } | |
1775 | case 2: | |
1776 | if (arm_feature(env, ARM_FEATURE_V6)) { | |
1777 | /* Instruction Fault Adrress. */ | |
1778 | return env->cp15.c6_insn; | |
1779 | } else { | |
1780 | goto bad_reg; | |
1781 | } | |
1782 | default: | |
1783 | goto bad_reg; | |
1784 | } | |
b5ff1b31 FB |
1785 | } |
1786 | case 7: /* Cache control. */ | |
f8bf8606 AL |
1787 | if (crm == 4 && op1 == 0 && op2 == 0) { |
1788 | return env->cp15.c7_par; | |
1789 | } | |
6fbe23d5 PB |
1790 | /* FIXME: Should only clear Z flag if destination is r15. */ |
1791 | env->ZF = 0; | |
b5ff1b31 FB |
1792 | return 0; |
1793 | case 8: /* MMU TLB control. */ | |
1794 | goto bad_reg; | |
74594c9d PM |
1795 | case 9: |
1796 | switch (crm) { | |
1797 | case 0: /* Cache lockdown */ | |
1798 | switch (op1) { | |
1799 | case 0: /* L1 cache. */ | |
1800 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) { | |
1801 | return 0; | |
1802 | } | |
1803 | switch (op2) { | |
1804 | case 0: | |
1805 | return env->cp15.c9_data; | |
1806 | case 1: | |
1807 | return env->cp15.c9_insn; | |
1808 | default: | |
1809 | goto bad_reg; | |
1810 | } | |
1811 | case 1: /* L2 cache */ | |
0b03bdfc PM |
1812 | /* L2 Lockdown and Auxiliary control. */ |
1813 | switch (op2) { | |
1814 | case 0: | |
1815 | /* L2 cache lockdown (A8 only) */ | |
1816 | return 0; | |
1817 | case 2: | |
1818 | /* L2 cache auxiliary control (A8) or control (A15) */ | |
1819 | if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) { | |
1820 | /* Linux wants the number of processors from here. | |
1821 | * Might as well set the interrupt-controller bit too. | |
1822 | */ | |
1823 | return ((smp_cpus - 1) << 24) | (1 << 23); | |
1824 | } | |
1825 | return 0; | |
1826 | case 3: | |
1827 | /* L2 cache extended control (A15) */ | |
1828 | return 0; | |
1829 | default: | |
74594c9d PM |
1830 | goto bad_reg; |
1831 | } | |
74594c9d PM |
1832 | default: |
1833 | goto bad_reg; | |
1834 | } | |
1835 | break; | |
1836 | case 12: /* Performance monitor control */ | |
1837 | if (!arm_feature(env, ARM_FEATURE_V7)) { | |
1838 | goto bad_reg; | |
1839 | } | |
9ee6e8bb | 1840 | switch (op2) { |
74594c9d PM |
1841 | case 0: /* performance monitor control register */ |
1842 | return env->cp15.c9_pmcr; | |
1843 | case 1: /* count enable set */ | |
1844 | case 2: /* count enable clear */ | |
1845 | return env->cp15.c9_pmcnten; | |
1846 | case 3: /* overflow flag status */ | |
1847 | return env->cp15.c9_pmovsr; | |
1848 | case 4: /* software increment */ | |
1849 | case 5: /* event counter selection register */ | |
1850 | return 0; /* Unimplemented, RAZ/WI */ | |
9ee6e8bb PB |
1851 | default: |
1852 | goto bad_reg; | |
1853 | } | |
74594c9d PM |
1854 | case 13: /* Performance counters */ |
1855 | if (!arm_feature(env, ARM_FEATURE_V7)) { | |
1856 | goto bad_reg; | |
1857 | } | |
1858 | switch (op2) { | |
1859 | case 1: /* Event type select */ | |
1860 | return env->cp15.c9_pmxevtyper; | |
1861 | case 0: /* Cycle count register */ | |
1862 | case 2: /* Event count register */ | |
1863 | /* Unimplemented, so RAZ/WI */ | |
1864 | return 0; | |
1865 | default: | |
9ee6e8bb | 1866 | goto bad_reg; |
74594c9d PM |
1867 | } |
1868 | case 14: /* Performance monitor control */ | |
1869 | if (!arm_feature(env, ARM_FEATURE_V7)) { | |
1870 | goto bad_reg; | |
1871 | } | |
1872 | switch (op2) { | |
1873 | case 0: /* user enable */ | |
1874 | return env->cp15.c9_pmuserenr; | |
1875 | case 1: /* interrupt enable set */ | |
1876 | case 2: /* interrupt enable clear */ | |
1877 | return env->cp15.c9_pminten; | |
1878 | default: | |
1879 | goto bad_reg; | |
1880 | } | |
b5ff1b31 FB |
1881 | default: |
1882 | goto bad_reg; | |
1883 | } | |
74594c9d | 1884 | break; |
b5ff1b31 FB |
1885 | case 10: /* MMU TLB lockdown. */ |
1886 | /* ??? TLB lockdown not implemented. */ | |
1887 | return 0; | |
1888 | case 11: /* TCM DMA control. */ | |
1889 | case 12: /* Reserved. */ | |
1890 | goto bad_reg; | |
1891 | case 13: /* Process ID. */ | |
1892 | switch (op2) { | |
1893 | case 0: | |
1894 | return env->cp15.c13_fcse; | |
1895 | case 1: | |
1896 | return env->cp15.c13_context; | |
1897 | default: | |
1898 | goto bad_reg; | |
1899 | } | |
0383ac00 PM |
1900 | case 14: /* Generic timer */ |
1901 | if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { | |
1902 | /* Dummy implementation: RAZ/WI for all */ | |
1903 | return 0; | |
1904 | } | |
b5ff1b31 FB |
1905 | goto bad_reg; |
1906 | case 15: /* Implementation specific. */ | |
c1713132 | 1907 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { |
c3d2689d | 1908 | if (op2 == 0 && crm == 1) |
c1713132 AZ |
1909 | return env->cp15.c15_cpar; |
1910 | ||
1911 | goto bad_reg; | |
1912 | } | |
c3d2689d AZ |
1913 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) { |
1914 | switch (crm) { | |
1915 | case 0: | |
1916 | return 0; | |
1917 | case 1: /* Read TI925T configuration. */ | |
1918 | return env->cp15.c15_ticonfig; | |
1919 | case 2: /* Read I_max. */ | |
1920 | return env->cp15.c15_i_max; | |
1921 | case 3: /* Read I_min. */ | |
1922 | return env->cp15.c15_i_min; | |
1923 | case 4: /* Read thread-ID. */ | |
1924 | return env->cp15.c15_threadid; | |
1925 | case 8: /* TI925T_status */ | |
1926 | return 0; | |
1927 | } | |
827df9f3 AZ |
1928 | /* TODO: Peripheral port remap register: |
1929 | * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt | |
1930 | * controller base address at $rn & ~0xfff and map size of | |
1931 | * 0x200 << ($rn & 0xfff), when MMU is off. */ | |
c3d2689d AZ |
1932 | goto bad_reg; |
1933 | } | |
7da362d0 ML |
1934 | if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) { |
1935 | switch (crm) { | |
1936 | case 0: | |
1937 | if ((op1 == 4) && (op2 == 0)) { | |
1938 | /* The config_base_address should hold the value of | |
1939 | * the peripheral base. ARM should get this from a CPU | |
1940 | * object property, but that support isn't available in | |
1941 | * December 2011. Default to 0 for now and board models | |
1942 | * that care can set it by a private hook */ | |
1943 | return env->cp15.c15_config_base_address; | |
1944 | } else if ((op1 == 0) && (op2 == 0)) { | |
1945 | /* power_control should be set to maximum latency. Again, | |
1946 | default to 0 and set by private hook */ | |
1947 | return env->cp15.c15_power_control; | |
1948 | } else if ((op1 == 0) && (op2 == 1)) { | |
1949 | return env->cp15.c15_diagnostic; | |
1950 | } else if ((op1 == 0) && (op2 == 2)) { | |
1951 | return env->cp15.c15_power_diagnostic; | |
1952 | } | |
1953 | break; | |
1954 | case 1: /* NEON Busy */ | |
1955 | return 0; | |
1956 | case 5: /* tlb lockdown */ | |
1957 | case 6: | |
1958 | case 7: | |
1959 | if ((op1 == 5) && (op2 == 2)) { | |
1960 | return 0; | |
1961 | } | |
1962 | break; | |
1963 | default: | |
1964 | break; | |
1965 | } | |
1966 | goto bad_reg; | |
1967 | } | |
b5ff1b31 FB |
1968 | return 0; |
1969 | } | |
1970 | bad_reg: | |
1971 | /* ??? For debugging only. Should raise illegal instruction exception. */ | |
9ee6e8bb PB |
1972 | cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n", |
1973 | (insn >> 16) & 0xf, crm, op1, op2); | |
b5ff1b31 FB |
1974 | return 0; |
1975 | } | |
1976 | ||
0ecb72a5 | 1977 | void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val) |
9ee6e8bb | 1978 | { |
39ea3d4e PM |
1979 | if ((env->uncached_cpsr & CPSR_M) == mode) { |
1980 | env->regs[13] = val; | |
1981 | } else { | |
1b9e01c1 | 1982 | env->banked_r13[bank_number(env, mode)] = val; |
39ea3d4e | 1983 | } |
9ee6e8bb PB |
1984 | } |
1985 | ||
0ecb72a5 | 1986 | uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode) |
9ee6e8bb | 1987 | { |
39ea3d4e PM |
1988 | if ((env->uncached_cpsr & CPSR_M) == mode) { |
1989 | return env->regs[13]; | |
1990 | } else { | |
1b9e01c1 | 1991 | return env->banked_r13[bank_number(env, mode)]; |
39ea3d4e | 1992 | } |
9ee6e8bb PB |
1993 | } |
1994 | ||
0ecb72a5 | 1995 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) |
9ee6e8bb PB |
1996 | { |
1997 | switch (reg) { | |
1998 | case 0: /* APSR */ | |
1999 | return xpsr_read(env) & 0xf8000000; | |
2000 | case 1: /* IAPSR */ | |
2001 | return xpsr_read(env) & 0xf80001ff; | |
2002 | case 2: /* EAPSR */ | |
2003 | return xpsr_read(env) & 0xff00fc00; | |
2004 | case 3: /* xPSR */ | |
2005 | return xpsr_read(env) & 0xff00fdff; | |
2006 | case 5: /* IPSR */ | |
2007 | return xpsr_read(env) & 0x000001ff; | |
2008 | case 6: /* EPSR */ | |
2009 | return xpsr_read(env) & 0x0700fc00; | |
2010 | case 7: /* IEPSR */ | |
2011 | return xpsr_read(env) & 0x0700edff; | |
2012 | case 8: /* MSP */ | |
2013 | return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13]; | |
2014 | case 9: /* PSP */ | |
2015 | return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp; | |
2016 | case 16: /* PRIMASK */ | |
2017 | return (env->uncached_cpsr & CPSR_I) != 0; | |
82845826 SH |
2018 | case 17: /* BASEPRI */ |
2019 | case 18: /* BASEPRI_MAX */ | |
9ee6e8bb | 2020 | return env->v7m.basepri; |
82845826 SH |
2021 | case 19: /* FAULTMASK */ |
2022 | return (env->uncached_cpsr & CPSR_F) != 0; | |
9ee6e8bb PB |
2023 | case 20: /* CONTROL */ |
2024 | return env->v7m.control; | |
2025 | default: | |
2026 | /* ??? For debugging only. */ | |
2027 | cpu_abort(env, "Unimplemented system register read (%d)\n", reg); | |
2028 | return 0; | |
2029 | } | |
2030 | } | |
2031 | ||
0ecb72a5 | 2032 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) |
9ee6e8bb PB |
2033 | { |
2034 | switch (reg) { | |
2035 | case 0: /* APSR */ | |
2036 | xpsr_write(env, val, 0xf8000000); | |
2037 | break; | |
2038 | case 1: /* IAPSR */ | |
2039 | xpsr_write(env, val, 0xf8000000); | |
2040 | break; | |
2041 | case 2: /* EAPSR */ | |
2042 | xpsr_write(env, val, 0xfe00fc00); | |
2043 | break; | |
2044 | case 3: /* xPSR */ | |
2045 | xpsr_write(env, val, 0xfe00fc00); | |
2046 | break; | |
2047 | case 5: /* IPSR */ | |
2048 | /* IPSR bits are readonly. */ | |
2049 | break; | |
2050 | case 6: /* EPSR */ | |
2051 | xpsr_write(env, val, 0x0600fc00); | |
2052 | break; | |
2053 | case 7: /* IEPSR */ | |
2054 | xpsr_write(env, val, 0x0600fc00); | |
2055 | break; | |
2056 | case 8: /* MSP */ | |
2057 | if (env->v7m.current_sp) | |
2058 | env->v7m.other_sp = val; | |
2059 | else | |
2060 | env->regs[13] = val; | |
2061 | break; | |
2062 | case 9: /* PSP */ | |
2063 | if (env->v7m.current_sp) | |
2064 | env->regs[13] = val; | |
2065 | else | |
2066 | env->v7m.other_sp = val; | |
2067 | break; | |
2068 | case 16: /* PRIMASK */ | |
2069 | if (val & 1) | |
2070 | env->uncached_cpsr |= CPSR_I; | |
2071 | else | |
2072 | env->uncached_cpsr &= ~CPSR_I; | |
2073 | break; | |
82845826 | 2074 | case 17: /* BASEPRI */ |
9ee6e8bb PB |
2075 | env->v7m.basepri = val & 0xff; |
2076 | break; | |
82845826 | 2077 | case 18: /* BASEPRI_MAX */ |
9ee6e8bb PB |
2078 | val &= 0xff; |
2079 | if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0)) | |
2080 | env->v7m.basepri = val; | |
2081 | break; | |
82845826 SH |
2082 | case 19: /* FAULTMASK */ |
2083 | if (val & 1) | |
2084 | env->uncached_cpsr |= CPSR_F; | |
2085 | else | |
2086 | env->uncached_cpsr &= ~CPSR_F; | |
2087 | break; | |
9ee6e8bb PB |
2088 | case 20: /* CONTROL */ |
2089 | env->v7m.control = val & 3; | |
2090 | switch_v7m_sp(env, (val & 2) != 0); | |
2091 | break; | |
2092 | default: | |
2093 | /* ??? For debugging only. */ | |
2094 | cpu_abort(env, "Unimplemented system register write (%d)\n", reg); | |
2095 | return; | |
2096 | } | |
2097 | } | |
2098 | ||
b5ff1b31 | 2099 | #endif |
6ddbc6e4 PB |
2100 | |
2101 | /* Note that signed overflow is undefined in C. The following routines are | |
2102 | careful to use unsigned types where modulo arithmetic is required. | |
2103 | Failure to do so _will_ break on newer gcc. */ | |
2104 | ||
2105 | /* Signed saturating arithmetic. */ | |
2106 | ||
1654b2d6 | 2107 | /* Perform 16-bit signed saturating addition. */ |
6ddbc6e4 PB |
2108 | static inline uint16_t add16_sat(uint16_t a, uint16_t b) |
2109 | { | |
2110 | uint16_t res; | |
2111 | ||
2112 | res = a + b; | |
2113 | if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { | |
2114 | if (a & 0x8000) | |
2115 | res = 0x8000; | |
2116 | else | |
2117 | res = 0x7fff; | |
2118 | } | |
2119 | return res; | |
2120 | } | |
2121 | ||
1654b2d6 | 2122 | /* Perform 8-bit signed saturating addition. */ |
6ddbc6e4 PB |
2123 | static inline uint8_t add8_sat(uint8_t a, uint8_t b) |
2124 | { | |
2125 | uint8_t res; | |
2126 | ||
2127 | res = a + b; | |
2128 | if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { | |
2129 | if (a & 0x80) | |
2130 | res = 0x80; | |
2131 | else | |
2132 | res = 0x7f; | |
2133 | } | |
2134 | return res; | |
2135 | } | |
2136 | ||
1654b2d6 | 2137 | /* Perform 16-bit signed saturating subtraction. */ |
6ddbc6e4 PB |
2138 | static inline uint16_t sub16_sat(uint16_t a, uint16_t b) |
2139 | { | |
2140 | uint16_t res; | |
2141 | ||
2142 | res = a - b; | |
2143 | if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { | |
2144 | if (a & 0x8000) | |
2145 | res = 0x8000; | |
2146 | else | |
2147 | res = 0x7fff; | |
2148 | } | |
2149 | return res; | |
2150 | } | |
2151 | ||
1654b2d6 | 2152 | /* Perform 8-bit signed saturating subtraction. */ |
6ddbc6e4 PB |
2153 | static inline uint8_t sub8_sat(uint8_t a, uint8_t b) |
2154 | { | |
2155 | uint8_t res; | |
2156 | ||
2157 | res = a - b; | |
2158 | if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { | |
2159 | if (a & 0x80) | |
2160 | res = 0x80; | |
2161 | else | |
2162 | res = 0x7f; | |
2163 | } | |
2164 | return res; | |
2165 | } | |
2166 | ||
2167 | #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); | |
2168 | #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); | |
2169 | #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); | |
2170 | #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); | |
2171 | #define PFX q | |
2172 | ||
2173 | #include "op_addsub.h" | |
2174 | ||
2175 | /* Unsigned saturating arithmetic. */ | |
460a09c1 | 2176 | static inline uint16_t add16_usat(uint16_t a, uint16_t b) |
6ddbc6e4 PB |
2177 | { |
2178 | uint16_t res; | |
2179 | res = a + b; | |
2180 | if (res < a) | |
2181 | res = 0xffff; | |
2182 | return res; | |
2183 | } | |
2184 | ||
460a09c1 | 2185 | static inline uint16_t sub16_usat(uint16_t a, uint16_t b) |
6ddbc6e4 | 2186 | { |
4c4fd3f8 | 2187 | if (a > b) |
6ddbc6e4 PB |
2188 | return a - b; |
2189 | else | |
2190 | return 0; | |
2191 | } | |
2192 | ||
2193 | static inline uint8_t add8_usat(uint8_t a, uint8_t b) | |
2194 | { | |
2195 | uint8_t res; | |
2196 | res = a + b; | |
2197 | if (res < a) | |
2198 | res = 0xff; | |
2199 | return res; | |
2200 | } | |
2201 | ||
2202 | static inline uint8_t sub8_usat(uint8_t a, uint8_t b) | |
2203 | { | |
4c4fd3f8 | 2204 | if (a > b) |
6ddbc6e4 PB |
2205 | return a - b; |
2206 | else | |
2207 | return 0; | |
2208 | } | |
2209 | ||
2210 | #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); | |
2211 | #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); | |
2212 | #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); | |
2213 | #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); | |
2214 | #define PFX uq | |
2215 | ||
2216 | #include "op_addsub.h" | |
2217 | ||
2218 | /* Signed modulo arithmetic. */ | |
2219 | #define SARITH16(a, b, n, op) do { \ | |
2220 | int32_t sum; \ | |
db6e2e65 | 2221 | sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ |
6ddbc6e4 PB |
2222 | RESULT(sum, n, 16); \ |
2223 | if (sum >= 0) \ | |
2224 | ge |= 3 << (n * 2); \ | |
2225 | } while(0) | |
2226 | ||
2227 | #define SARITH8(a, b, n, op) do { \ | |
2228 | int32_t sum; \ | |
db6e2e65 | 2229 | sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ |
6ddbc6e4 PB |
2230 | RESULT(sum, n, 8); \ |
2231 | if (sum >= 0) \ | |
2232 | ge |= 1 << n; \ | |
2233 | } while(0) | |
2234 | ||
2235 | ||
2236 | #define ADD16(a, b, n) SARITH16(a, b, n, +) | |
2237 | #define SUB16(a, b, n) SARITH16(a, b, n, -) | |
2238 | #define ADD8(a, b, n) SARITH8(a, b, n, +) | |
2239 | #define SUB8(a, b, n) SARITH8(a, b, n, -) | |
2240 | #define PFX s | |
2241 | #define ARITH_GE | |
2242 | ||
2243 | #include "op_addsub.h" | |
2244 | ||
2245 | /* Unsigned modulo arithmetic. */ | |
2246 | #define ADD16(a, b, n) do { \ | |
2247 | uint32_t sum; \ | |
2248 | sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ | |
2249 | RESULT(sum, n, 16); \ | |
a87aa10b | 2250 | if ((sum >> 16) == 1) \ |
6ddbc6e4 PB |
2251 | ge |= 3 << (n * 2); \ |
2252 | } while(0) | |
2253 | ||
2254 | #define ADD8(a, b, n) do { \ | |
2255 | uint32_t sum; \ | |
2256 | sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ | |
2257 | RESULT(sum, n, 8); \ | |
a87aa10b AZ |
2258 | if ((sum >> 8) == 1) \ |
2259 | ge |= 1 << n; \ | |
6ddbc6e4 PB |
2260 | } while(0) |
2261 | ||
2262 | #define SUB16(a, b, n) do { \ | |
2263 | uint32_t sum; \ | |
2264 | sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ | |
2265 | RESULT(sum, n, 16); \ | |
2266 | if ((sum >> 16) == 0) \ | |
2267 | ge |= 3 << (n * 2); \ | |
2268 | } while(0) | |
2269 | ||
2270 | #define SUB8(a, b, n) do { \ | |
2271 | uint32_t sum; \ | |
2272 | sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ | |
2273 | RESULT(sum, n, 8); \ | |
2274 | if ((sum >> 8) == 0) \ | |
a87aa10b | 2275 | ge |= 1 << n; \ |
6ddbc6e4 PB |
2276 | } while(0) |
2277 | ||
2278 | #define PFX u | |
2279 | #define ARITH_GE | |
2280 | ||
2281 | #include "op_addsub.h" | |
2282 | ||
2283 | /* Halved signed arithmetic. */ | |
2284 | #define ADD16(a, b, n) \ | |
2285 | RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) | |
2286 | #define SUB16(a, b, n) \ | |
2287 | RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) | |
2288 | #define ADD8(a, b, n) \ | |
2289 | RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) | |
2290 | #define SUB8(a, b, n) \ | |
2291 | RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) | |
2292 | #define PFX sh | |
2293 | ||
2294 | #include "op_addsub.h" | |
2295 | ||
2296 | /* Halved unsigned arithmetic. */ | |
2297 | #define ADD16(a, b, n) \ | |
2298 | RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) | |
2299 | #define SUB16(a, b, n) \ | |
2300 | RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) | |
2301 | #define ADD8(a, b, n) \ | |
2302 | RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) | |
2303 | #define SUB8(a, b, n) \ | |
2304 | RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) | |
2305 | #define PFX uh | |
2306 | ||
2307 | #include "op_addsub.h" | |
2308 | ||
2309 | static inline uint8_t do_usad(uint8_t a, uint8_t b) | |
2310 | { | |
2311 | if (a > b) | |
2312 | return a - b; | |
2313 | else | |
2314 | return b - a; | |
2315 | } | |
2316 | ||
2317 | /* Unsigned sum of absolute byte differences. */ | |
2318 | uint32_t HELPER(usad8)(uint32_t a, uint32_t b) | |
2319 | { | |
2320 | uint32_t sum; | |
2321 | sum = do_usad(a, b); | |
2322 | sum += do_usad(a >> 8, b >> 8); | |
2323 | sum += do_usad(a >> 16, b >>16); | |
2324 | sum += do_usad(a >> 24, b >> 24); | |
2325 | return sum; | |
2326 | } | |
2327 | ||
2328 | /* For ARMv6 SEL instruction. */ | |
2329 | uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) | |
2330 | { | |
2331 | uint32_t mask; | |
2332 | ||
2333 | mask = 0; | |
2334 | if (flags & 1) | |
2335 | mask |= 0xff; | |
2336 | if (flags & 2) | |
2337 | mask |= 0xff00; | |
2338 | if (flags & 4) | |
2339 | mask |= 0xff0000; | |
2340 | if (flags & 8) | |
2341 | mask |= 0xff000000; | |
2342 | return (a & mask) | (b & ~mask); | |
2343 | } | |
2344 | ||
5e3f878a PB |
2345 | uint32_t HELPER(logicq_cc)(uint64_t val) |
2346 | { | |
2347 | return (val >> 32) | (val != 0); | |
2348 | } | |
4373f3ce PB |
2349 | |
2350 | /* VFP support. We follow the convention used for VFP instrunctions: | |
2351 | Single precition routines have a "s" suffix, double precision a | |
2352 | "d" suffix. */ | |
2353 | ||
2354 | /* Convert host exception flags to vfp form. */ | |
2355 | static inline int vfp_exceptbits_from_host(int host_bits) | |
2356 | { | |
2357 | int target_bits = 0; | |
2358 | ||
2359 | if (host_bits & float_flag_invalid) | |
2360 | target_bits |= 1; | |
2361 | if (host_bits & float_flag_divbyzero) | |
2362 | target_bits |= 2; | |
2363 | if (host_bits & float_flag_overflow) | |
2364 | target_bits |= 4; | |
36802b6b | 2365 | if (host_bits & (float_flag_underflow | float_flag_output_denormal)) |
4373f3ce PB |
2366 | target_bits |= 8; |
2367 | if (host_bits & float_flag_inexact) | |
2368 | target_bits |= 0x10; | |
cecd8504 PM |
2369 | if (host_bits & float_flag_input_denormal) |
2370 | target_bits |= 0x80; | |
4373f3ce PB |
2371 | return target_bits; |
2372 | } | |
2373 | ||
0ecb72a5 | 2374 | uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env) |
4373f3ce PB |
2375 | { |
2376 | int i; | |
2377 | uint32_t fpscr; | |
2378 | ||
2379 | fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff) | |
2380 | | (env->vfp.vec_len << 16) | |
2381 | | (env->vfp.vec_stride << 20); | |
2382 | i = get_float_exception_flags(&env->vfp.fp_status); | |
3a492f3a | 2383 | i |= get_float_exception_flags(&env->vfp.standard_fp_status); |
4373f3ce PB |
2384 | fpscr |= vfp_exceptbits_from_host(i); |
2385 | return fpscr; | |
2386 | } | |
2387 | ||
0ecb72a5 | 2388 | uint32_t vfp_get_fpscr(CPUARMState *env) |
01653295 PM |
2389 | { |
2390 | return HELPER(vfp_get_fpscr)(env); | |
2391 | } | |
2392 | ||
4373f3ce PB |
2393 | /* Convert vfp exception flags to target form. */ |
2394 | static inline int vfp_exceptbits_to_host(int target_bits) | |
2395 | { | |
2396 | int host_bits = 0; | |
2397 | ||
2398 | if (target_bits & 1) | |
2399 | host_bits |= float_flag_invalid; | |
2400 | if (target_bits & 2) | |
2401 | host_bits |= float_flag_divbyzero; | |
2402 | if (target_bits & 4) | |
2403 | host_bits |= float_flag_overflow; | |
2404 | if (target_bits & 8) | |
2405 | host_bits |= float_flag_underflow; | |
2406 | if (target_bits & 0x10) | |
2407 | host_bits |= float_flag_inexact; | |
cecd8504 PM |
2408 | if (target_bits & 0x80) |
2409 | host_bits |= float_flag_input_denormal; | |
4373f3ce PB |
2410 | return host_bits; |
2411 | } | |
2412 | ||
0ecb72a5 | 2413 | void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val) |
4373f3ce PB |
2414 | { |
2415 | int i; | |
2416 | uint32_t changed; | |
2417 | ||
2418 | changed = env->vfp.xregs[ARM_VFP_FPSCR]; | |
2419 | env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff); | |
2420 | env->vfp.vec_len = (val >> 16) & 7; | |
2421 | env->vfp.vec_stride = (val >> 20) & 3; | |
2422 | ||
2423 | changed ^= val; | |
2424 | if (changed & (3 << 22)) { | |
2425 | i = (val >> 22) & 3; | |
2426 | switch (i) { | |
2427 | case 0: | |
2428 | i = float_round_nearest_even; | |
2429 | break; | |
2430 | case 1: | |
2431 | i = float_round_up; | |
2432 | break; | |
2433 | case 2: | |
2434 | i = float_round_down; | |
2435 | break; | |
2436 | case 3: | |
2437 | i = float_round_to_zero; | |
2438 | break; | |
2439 | } | |
2440 | set_float_rounding_mode(i, &env->vfp.fp_status); | |
2441 | } | |
cecd8504 | 2442 | if (changed & (1 << 24)) { |
fe76d976 | 2443 | set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); |
cecd8504 PM |
2444 | set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); |
2445 | } | |
5c7908ed PB |
2446 | if (changed & (1 << 25)) |
2447 | set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status); | |
4373f3ce | 2448 | |
b12c390b | 2449 | i = vfp_exceptbits_to_host(val); |
4373f3ce | 2450 | set_float_exception_flags(i, &env->vfp.fp_status); |
3a492f3a | 2451 | set_float_exception_flags(0, &env->vfp.standard_fp_status); |
4373f3ce PB |
2452 | } |
2453 | ||
0ecb72a5 | 2454 | void vfp_set_fpscr(CPUARMState *env, uint32_t val) |
01653295 PM |
2455 | { |
2456 | HELPER(vfp_set_fpscr)(env, val); | |
2457 | } | |
2458 | ||
4373f3ce PB |
2459 | #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p)) |
2460 | ||
2461 | #define VFP_BINOP(name) \ | |
ae1857ec | 2462 | float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \ |
4373f3ce | 2463 | { \ |
ae1857ec PM |
2464 | float_status *fpst = fpstp; \ |
2465 | return float32_ ## name(a, b, fpst); \ | |
4373f3ce | 2466 | } \ |
ae1857ec | 2467 | float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \ |
4373f3ce | 2468 | { \ |
ae1857ec PM |
2469 | float_status *fpst = fpstp; \ |
2470 | return float64_ ## name(a, b, fpst); \ | |
4373f3ce PB |
2471 | } |
2472 | VFP_BINOP(add) | |
2473 | VFP_BINOP(sub) | |
2474 | VFP_BINOP(mul) | |
2475 | VFP_BINOP(div) | |
2476 | #undef VFP_BINOP | |
2477 | ||
2478 | float32 VFP_HELPER(neg, s)(float32 a) | |
2479 | { | |
2480 | return float32_chs(a); | |
2481 | } | |
2482 | ||
2483 | float64 VFP_HELPER(neg, d)(float64 a) | |
2484 | { | |
66230e0d | 2485 | return float64_chs(a); |
4373f3ce PB |
2486 | } |
2487 | ||
2488 | float32 VFP_HELPER(abs, s)(float32 a) | |
2489 | { | |
2490 | return float32_abs(a); | |
2491 | } | |
2492 | ||
2493 | float64 VFP_HELPER(abs, d)(float64 a) | |
2494 | { | |
66230e0d | 2495 | return float64_abs(a); |
4373f3ce PB |
2496 | } |
2497 | ||
0ecb72a5 | 2498 | float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env) |
4373f3ce PB |
2499 | { |
2500 | return float32_sqrt(a, &env->vfp.fp_status); | |
2501 | } | |
2502 | ||
0ecb72a5 | 2503 | float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env) |
4373f3ce PB |
2504 | { |
2505 | return float64_sqrt(a, &env->vfp.fp_status); | |
2506 | } | |
2507 | ||
2508 | /* XXX: check quiet/signaling case */ | |
2509 | #define DO_VFP_cmp(p, type) \ | |
0ecb72a5 | 2510 | void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \ |
4373f3ce PB |
2511 | { \ |
2512 | uint32_t flags; \ | |
2513 | switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \ | |
2514 | case 0: flags = 0x6; break; \ | |
2515 | case -1: flags = 0x8; break; \ | |
2516 | case 1: flags = 0x2; break; \ | |
2517 | default: case 2: flags = 0x3; break; \ | |
2518 | } \ | |
2519 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ | |
2520 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ | |
2521 | } \ | |
0ecb72a5 | 2522 | void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \ |
4373f3ce PB |
2523 | { \ |
2524 | uint32_t flags; \ | |
2525 | switch(type ## _compare(a, b, &env->vfp.fp_status)) { \ | |
2526 | case 0: flags = 0x6; break; \ | |
2527 | case -1: flags = 0x8; break; \ | |
2528 | case 1: flags = 0x2; break; \ | |
2529 | default: case 2: flags = 0x3; break; \ | |
2530 | } \ | |
2531 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ | |
2532 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ | |
2533 | } | |
2534 | DO_VFP_cmp(s, float32) | |
2535 | DO_VFP_cmp(d, float64) | |
2536 | #undef DO_VFP_cmp | |
2537 | ||
5500b06c | 2538 | /* Integer to float and float to integer conversions */ |
4373f3ce | 2539 | |
5500b06c PM |
2540 | #define CONV_ITOF(name, fsz, sign) \ |
2541 | float##fsz HELPER(name)(uint32_t x, void *fpstp) \ | |
2542 | { \ | |
2543 | float_status *fpst = fpstp; \ | |
85836979 | 2544 | return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \ |
4373f3ce PB |
2545 | } |
2546 | ||
5500b06c PM |
2547 | #define CONV_FTOI(name, fsz, sign, round) \ |
2548 | uint32_t HELPER(name)(float##fsz x, void *fpstp) \ | |
2549 | { \ | |
2550 | float_status *fpst = fpstp; \ | |
2551 | if (float##fsz##_is_any_nan(x)) { \ | |
2552 | float_raise(float_flag_invalid, fpst); \ | |
2553 | return 0; \ | |
2554 | } \ | |
2555 | return float##fsz##_to_##sign##int32##round(x, fpst); \ | |
4373f3ce PB |
2556 | } |
2557 | ||
5500b06c PM |
2558 | #define FLOAT_CONVS(name, p, fsz, sign) \ |
2559 | CONV_ITOF(vfp_##name##to##p, fsz, sign) \ | |
2560 | CONV_FTOI(vfp_to##name##p, fsz, sign, ) \ | |
2561 | CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero) | |
4373f3ce | 2562 | |
5500b06c PM |
2563 | FLOAT_CONVS(si, s, 32, ) |
2564 | FLOAT_CONVS(si, d, 64, ) | |
2565 | FLOAT_CONVS(ui, s, 32, u) | |
2566 | FLOAT_CONVS(ui, d, 64, u) | |
4373f3ce | 2567 | |
5500b06c PM |
2568 | #undef CONV_ITOF |
2569 | #undef CONV_FTOI | |
2570 | #undef FLOAT_CONVS | |
4373f3ce PB |
2571 | |
2572 | /* floating point conversion */ | |
0ecb72a5 | 2573 | float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env) |
4373f3ce | 2574 | { |
2d627737 PM |
2575 | float64 r = float32_to_float64(x, &env->vfp.fp_status); |
2576 | /* ARM requires that S<->D conversion of any kind of NaN generates | |
2577 | * a quiet NaN by forcing the most significant frac bit to 1. | |
2578 | */ | |
2579 | return float64_maybe_silence_nan(r); | |
4373f3ce PB |
2580 | } |
2581 | ||
0ecb72a5 | 2582 | float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env) |
4373f3ce | 2583 | { |
2d627737 PM |
2584 | float32 r = float64_to_float32(x, &env->vfp.fp_status); |
2585 | /* ARM requires that S<->D conversion of any kind of NaN generates | |
2586 | * a quiet NaN by forcing the most significant frac bit to 1. | |
2587 | */ | |
2588 | return float32_maybe_silence_nan(r); | |
4373f3ce PB |
2589 | } |
2590 | ||
2591 | /* VFP3 fixed point conversion. */ | |
622465e1 | 2592 | #define VFP_CONV_FIX(name, p, fsz, itype, sign) \ |
5500b06c PM |
2593 | float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \ |
2594 | void *fpstp) \ | |
4373f3ce | 2595 | { \ |
5500b06c | 2596 | float_status *fpst = fpstp; \ |
622465e1 | 2597 | float##fsz tmp; \ |
5500b06c PM |
2598 | tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \ |
2599 | return float##fsz##_scalbn(tmp, -(int)shift, fpst); \ | |
4373f3ce | 2600 | } \ |
5500b06c PM |
2601 | uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \ |
2602 | void *fpstp) \ | |
4373f3ce | 2603 | { \ |
5500b06c | 2604 | float_status *fpst = fpstp; \ |
622465e1 PM |
2605 | float##fsz tmp; \ |
2606 | if (float##fsz##_is_any_nan(x)) { \ | |
5500b06c | 2607 | float_raise(float_flag_invalid, fpst); \ |
622465e1 | 2608 | return 0; \ |
09d9487f | 2609 | } \ |
5500b06c PM |
2610 | tmp = float##fsz##_scalbn(x, shift, fpst); \ |
2611 | return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \ | |
622465e1 PM |
2612 | } |
2613 | ||
2614 | VFP_CONV_FIX(sh, d, 64, int16, ) | |
2615 | VFP_CONV_FIX(sl, d, 64, int32, ) | |
2616 | VFP_CONV_FIX(uh, d, 64, uint16, u) | |
2617 | VFP_CONV_FIX(ul, d, 64, uint32, u) | |
2618 | VFP_CONV_FIX(sh, s, 32, int16, ) | |
2619 | VFP_CONV_FIX(sl, s, 32, int32, ) | |
2620 | VFP_CONV_FIX(uh, s, 32, uint16, u) | |
2621 | VFP_CONV_FIX(ul, s, 32, uint32, u) | |
4373f3ce PB |
2622 | #undef VFP_CONV_FIX |
2623 | ||
60011498 | 2624 | /* Half precision conversions. */ |
0ecb72a5 | 2625 | static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s) |
60011498 | 2626 | { |
60011498 | 2627 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; |
fb91678d PM |
2628 | float32 r = float16_to_float32(make_float16(a), ieee, s); |
2629 | if (ieee) { | |
2630 | return float32_maybe_silence_nan(r); | |
2631 | } | |
2632 | return r; | |
60011498 PB |
2633 | } |
2634 | ||
0ecb72a5 | 2635 | static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s) |
60011498 | 2636 | { |
60011498 | 2637 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; |
fb91678d PM |
2638 | float16 r = float32_to_float16(a, ieee, s); |
2639 | if (ieee) { | |
2640 | r = float16_maybe_silence_nan(r); | |
2641 | } | |
2642 | return float16_val(r); | |
60011498 PB |
2643 | } |
2644 | ||
0ecb72a5 | 2645 | float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) |
2d981da7 PM |
2646 | { |
2647 | return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status); | |
2648 | } | |
2649 | ||
0ecb72a5 | 2650 | uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env) |
2d981da7 PM |
2651 | { |
2652 | return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status); | |
2653 | } | |
2654 | ||
0ecb72a5 | 2655 | float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) |
2d981da7 PM |
2656 | { |
2657 | return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status); | |
2658 | } | |
2659 | ||
0ecb72a5 | 2660 | uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env) |
2d981da7 PM |
2661 | { |
2662 | return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status); | |
2663 | } | |
2664 | ||
dda3ec49 | 2665 | #define float32_two make_float32(0x40000000) |
6aae3df1 PM |
2666 | #define float32_three make_float32(0x40400000) |
2667 | #define float32_one_point_five make_float32(0x3fc00000) | |
dda3ec49 | 2668 | |
0ecb72a5 | 2669 | float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env) |
4373f3ce | 2670 | { |
dda3ec49 PM |
2671 | float_status *s = &env->vfp.standard_fp_status; |
2672 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || | |
2673 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { | |
43fe9bdb PM |
2674 | if (!(float32_is_zero(a) || float32_is_zero(b))) { |
2675 | float_raise(float_flag_input_denormal, s); | |
2676 | } | |
dda3ec49 PM |
2677 | return float32_two; |
2678 | } | |
2679 | return float32_sub(float32_two, float32_mul(a, b, s), s); | |
4373f3ce PB |
2680 | } |
2681 | ||
0ecb72a5 | 2682 | float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env) |
4373f3ce | 2683 | { |
71826966 | 2684 | float_status *s = &env->vfp.standard_fp_status; |
9ea62f57 PM |
2685 | float32 product; |
2686 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || | |
2687 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { | |
43fe9bdb PM |
2688 | if (!(float32_is_zero(a) || float32_is_zero(b))) { |
2689 | float_raise(float_flag_input_denormal, s); | |
2690 | } | |
6aae3df1 | 2691 | return float32_one_point_five; |
9ea62f57 | 2692 | } |
6aae3df1 PM |
2693 | product = float32_mul(a, b, s); |
2694 | return float32_div(float32_sub(float32_three, product, s), float32_two, s); | |
4373f3ce PB |
2695 | } |
2696 | ||
8f8e3aa4 PB |
2697 | /* NEON helpers. */ |
2698 | ||
56bf4fe2 CL |
2699 | /* Constants 256 and 512 are used in some helpers; we avoid relying on |
2700 | * int->float conversions at run-time. */ | |
2701 | #define float64_256 make_float64(0x4070000000000000LL) | |
2702 | #define float64_512 make_float64(0x4080000000000000LL) | |
2703 | ||
fe0e4872 CL |
2704 | /* The algorithm that must be used to calculate the estimate |
2705 | * is specified by the ARM ARM. | |
2706 | */ | |
0ecb72a5 | 2707 | static float64 recip_estimate(float64 a, CPUARMState *env) |
fe0e4872 | 2708 | { |
1146a817 PM |
2709 | /* These calculations mustn't set any fp exception flags, |
2710 | * so we use a local copy of the fp_status. | |
2711 | */ | |
2712 | float_status dummy_status = env->vfp.standard_fp_status; | |
2713 | float_status *s = &dummy_status; | |
fe0e4872 CL |
2714 | /* q = (int)(a * 512.0) */ |
2715 | float64 q = float64_mul(float64_512, a, s); | |
2716 | int64_t q_int = float64_to_int64_round_to_zero(q, s); | |
2717 | ||
2718 | /* r = 1.0 / (((double)q + 0.5) / 512.0) */ | |
2719 | q = int64_to_float64(q_int, s); | |
2720 | q = float64_add(q, float64_half, s); | |
2721 | q = float64_div(q, float64_512, s); | |
2722 | q = float64_div(float64_one, q, s); | |
2723 | ||
2724 | /* s = (int)(256.0 * r + 0.5) */ | |
2725 | q = float64_mul(q, float64_256, s); | |
2726 | q = float64_add(q, float64_half, s); | |
2727 | q_int = float64_to_int64_round_to_zero(q, s); | |
2728 | ||
2729 | /* return (double)s / 256.0 */ | |
2730 | return float64_div(int64_to_float64(q_int, s), float64_256, s); | |
2731 | } | |
2732 | ||
0ecb72a5 | 2733 | float32 HELPER(recpe_f32)(float32 a, CPUARMState *env) |
4373f3ce | 2734 | { |
fe0e4872 CL |
2735 | float_status *s = &env->vfp.standard_fp_status; |
2736 | float64 f64; | |
2737 | uint32_t val32 = float32_val(a); | |
2738 | ||
2739 | int result_exp; | |
2740 | int a_exp = (val32 & 0x7f800000) >> 23; | |
2741 | int sign = val32 & 0x80000000; | |
2742 | ||
2743 | if (float32_is_any_nan(a)) { | |
2744 | if (float32_is_signaling_nan(a)) { | |
2745 | float_raise(float_flag_invalid, s); | |
2746 | } | |
2747 | return float32_default_nan; | |
2748 | } else if (float32_is_infinity(a)) { | |
2749 | return float32_set_sign(float32_zero, float32_is_neg(a)); | |
2750 | } else if (float32_is_zero_or_denormal(a)) { | |
43fe9bdb PM |
2751 | if (!float32_is_zero(a)) { |
2752 | float_raise(float_flag_input_denormal, s); | |
2753 | } | |
fe0e4872 CL |
2754 | float_raise(float_flag_divbyzero, s); |
2755 | return float32_set_sign(float32_infinity, float32_is_neg(a)); | |
2756 | } else if (a_exp >= 253) { | |
2757 | float_raise(float_flag_underflow, s); | |
2758 | return float32_set_sign(float32_zero, float32_is_neg(a)); | |
2759 | } | |
2760 | ||
2761 | f64 = make_float64((0x3feULL << 52) | |
2762 | | ((int64_t)(val32 & 0x7fffff) << 29)); | |
2763 | ||
2764 | result_exp = 253 - a_exp; | |
2765 | ||
2766 | f64 = recip_estimate(f64, env); | |
2767 | ||
2768 | val32 = sign | |
2769 | | ((result_exp & 0xff) << 23) | |
2770 | | ((float64_val(f64) >> 29) & 0x7fffff); | |
2771 | return make_float32(val32); | |
4373f3ce PB |
2772 | } |
2773 | ||
e07be5d2 CL |
2774 | /* The algorithm that must be used to calculate the estimate |
2775 | * is specified by the ARM ARM. | |
2776 | */ | |
0ecb72a5 | 2777 | static float64 recip_sqrt_estimate(float64 a, CPUARMState *env) |
e07be5d2 | 2778 | { |
1146a817 PM |
2779 | /* These calculations mustn't set any fp exception flags, |
2780 | * so we use a local copy of the fp_status. | |
2781 | */ | |
2782 | float_status dummy_status = env->vfp.standard_fp_status; | |
2783 | float_status *s = &dummy_status; | |
e07be5d2 CL |
2784 | float64 q; |
2785 | int64_t q_int; | |
2786 | ||
2787 | if (float64_lt(a, float64_half, s)) { | |
2788 | /* range 0.25 <= a < 0.5 */ | |
2789 | ||
2790 | /* a in units of 1/512 rounded down */ | |
2791 | /* q0 = (int)(a * 512.0); */ | |
2792 | q = float64_mul(float64_512, a, s); | |
2793 | q_int = float64_to_int64_round_to_zero(q, s); | |
2794 | ||
2795 | /* reciprocal root r */ | |
2796 | /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */ | |
2797 | q = int64_to_float64(q_int, s); | |
2798 | q = float64_add(q, float64_half, s); | |
2799 | q = float64_div(q, float64_512, s); | |
2800 | q = float64_sqrt(q, s); | |
2801 | q = float64_div(float64_one, q, s); | |
2802 | } else { | |
2803 | /* range 0.5 <= a < 1.0 */ | |
2804 | ||
2805 | /* a in units of 1/256 rounded down */ | |
2806 | /* q1 = (int)(a * 256.0); */ | |
2807 | q = float64_mul(float64_256, a, s); | |
2808 | int64_t q_int = float64_to_int64_round_to_zero(q, s); | |
2809 | ||
2810 | /* reciprocal root r */ | |
2811 | /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */ | |
2812 | q = int64_to_float64(q_int, s); | |
2813 | q = float64_add(q, float64_half, s); | |
2814 | q = float64_div(q, float64_256, s); | |
2815 | q = float64_sqrt(q, s); | |
2816 | q = float64_div(float64_one, q, s); | |
2817 | } | |
2818 | /* r in units of 1/256 rounded to nearest */ | |
2819 | /* s = (int)(256.0 * r + 0.5); */ | |
2820 | ||
2821 | q = float64_mul(q, float64_256,s ); | |
2822 | q = float64_add(q, float64_half, s); | |
2823 | q_int = float64_to_int64_round_to_zero(q, s); | |
2824 | ||
2825 | /* return (double)s / 256.0;*/ | |
2826 | return float64_div(int64_to_float64(q_int, s), float64_256, s); | |
2827 | } | |
2828 | ||
0ecb72a5 | 2829 | float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env) |
4373f3ce | 2830 | { |
e07be5d2 CL |
2831 | float_status *s = &env->vfp.standard_fp_status; |
2832 | int result_exp; | |
2833 | float64 f64; | |
2834 | uint32_t val; | |
2835 | uint64_t val64; | |
2836 | ||
2837 | val = float32_val(a); | |
2838 | ||
2839 | if (float32_is_any_nan(a)) { | |
2840 | if (float32_is_signaling_nan(a)) { | |
2841 | float_raise(float_flag_invalid, s); | |
2842 | } | |
2843 | return float32_default_nan; | |
2844 | } else if (float32_is_zero_or_denormal(a)) { | |
43fe9bdb PM |
2845 | if (!float32_is_zero(a)) { |
2846 | float_raise(float_flag_input_denormal, s); | |
2847 | } | |
e07be5d2 CL |
2848 | float_raise(float_flag_divbyzero, s); |
2849 | return float32_set_sign(float32_infinity, float32_is_neg(a)); | |
2850 | } else if (float32_is_neg(a)) { | |
2851 | float_raise(float_flag_invalid, s); | |
2852 | return float32_default_nan; | |
2853 | } else if (float32_is_infinity(a)) { | |
2854 | return float32_zero; | |
2855 | } | |
2856 | ||
2857 | /* Normalize to a double-precision value between 0.25 and 1.0, | |
2858 | * preserving the parity of the exponent. */ | |
2859 | if ((val & 0x800000) == 0) { | |
2860 | f64 = make_float64(((uint64_t)(val & 0x80000000) << 32) | |
2861 | | (0x3feULL << 52) | |
2862 | | ((uint64_t)(val & 0x7fffff) << 29)); | |
2863 | } else { | |
2864 | f64 = make_float64(((uint64_t)(val & 0x80000000) << 32) | |
2865 | | (0x3fdULL << 52) | |
2866 | | ((uint64_t)(val & 0x7fffff) << 29)); | |
2867 | } | |
2868 | ||
2869 | result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2; | |
2870 | ||
2871 | f64 = recip_sqrt_estimate(f64, env); | |
2872 | ||
2873 | val64 = float64_val(f64); | |
2874 | ||
26cc6abf | 2875 | val = ((result_exp & 0xff) << 23) |
e07be5d2 CL |
2876 | | ((val64 >> 29) & 0x7fffff); |
2877 | return make_float32(val); | |
4373f3ce PB |
2878 | } |
2879 | ||
0ecb72a5 | 2880 | uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env) |
4373f3ce | 2881 | { |
fe0e4872 CL |
2882 | float64 f64; |
2883 | ||
2884 | if ((a & 0x80000000) == 0) { | |
2885 | return 0xffffffff; | |
2886 | } | |
2887 | ||
2888 | f64 = make_float64((0x3feULL << 52) | |
2889 | | ((int64_t)(a & 0x7fffffff) << 21)); | |
2890 | ||
2891 | f64 = recip_estimate (f64, env); | |
2892 | ||
2893 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); | |
4373f3ce PB |
2894 | } |
2895 | ||
0ecb72a5 | 2896 | uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env) |
4373f3ce | 2897 | { |
e07be5d2 CL |
2898 | float64 f64; |
2899 | ||
2900 | if ((a & 0xc0000000) == 0) { | |
2901 | return 0xffffffff; | |
2902 | } | |
2903 | ||
2904 | if (a & 0x80000000) { | |
2905 | f64 = make_float64((0x3feULL << 52) | |
2906 | | ((uint64_t)(a & 0x7fffffff) << 21)); | |
2907 | } else { /* bits 31-30 == '01' */ | |
2908 | f64 = make_float64((0x3fdULL << 52) | |
2909 | | ((uint64_t)(a & 0x3fffffff) << 22)); | |
2910 | } | |
2911 | ||
2912 | f64 = recip_sqrt_estimate(f64, env); | |
2913 | ||
2914 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); | |
4373f3ce | 2915 | } |
fe1479c3 | 2916 | |
da97f52c PM |
2917 | /* VFPv4 fused multiply-accumulate */ |
2918 | float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp) | |
2919 | { | |
2920 | float_status *fpst = fpstp; | |
2921 | return float32_muladd(a, b, c, 0, fpst); | |
2922 | } | |
2923 | ||
2924 | float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp) | |
2925 | { | |
2926 | float_status *fpst = fpstp; | |
2927 | return float64_muladd(a, b, c, 0, fpst); | |
2928 | } | |
2929 | ||
0ecb72a5 | 2930 | void HELPER(set_teecr)(CPUARMState *env, uint32_t val) |
fe1479c3 PB |
2931 | { |
2932 | val &= 1; | |
2933 | if (env->teecr != val) { | |
2934 | env->teecr = val; | |
2935 | tb_flush(env); | |
2936 | } | |
2937 | } |