]> git.proxmox.com Git - qemu.git/blame - target-arm/helper.c
Allocate the -vmwarevga framebuffer in the right place in RAM.
[qemu.git] / target-arm / helper.c
CommitLineData
b5ff1b31
FB
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "cpu.h"
6#include "exec-all.h"
9ee6e8bb 7#include "gdbstub.h"
b26eefb6 8#include "helpers.h"
ca10f867 9#include "qemu-common.h"
9ee6e8bb
PB
10
11static uint32_t cortexa8_cp15_c0_c1[8] =
12{ 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
13
14static uint32_t cortexa8_cp15_c0_c2[8] =
15{ 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
16
17static uint32_t mpcore_cp15_c0_c1[8] =
18{ 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
19
20static uint32_t mpcore_cp15_c0_c2[8] =
21{ 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
22
23static uint32_t arm1136_cp15_c0_c1[8] =
24{ 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
25
26static uint32_t arm1136_cp15_c0_c2[8] =
27{ 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
b5ff1b31 28
aaed909a
FB
29static uint32_t cpu_arm_find_by_name(const char *name);
30
f3d6b95e
PB
31static inline void set_feature(CPUARMState *env, int feature)
32{
33 env->features |= 1u << feature;
34}
35
36static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
37{
38 env->cp15.c0_cpuid = id;
39 switch (id) {
40 case ARM_CPUID_ARM926:
41 set_feature(env, ARM_FEATURE_VFP);
42 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
c1713132 43 env->cp15.c0_cachetype = 0x1dd20d2;
610c3c8a 44 env->cp15.c1_sys = 0x00090078;
f3d6b95e 45 break;
ce819861
PB
46 case ARM_CPUID_ARM946:
47 set_feature(env, ARM_FEATURE_MPU);
48 env->cp15.c0_cachetype = 0x0f004006;
610c3c8a 49 env->cp15.c1_sys = 0x00000078;
ce819861 50 break;
f3d6b95e
PB
51 case ARM_CPUID_ARM1026:
52 set_feature(env, ARM_FEATURE_VFP);
53 set_feature(env, ARM_FEATURE_AUXCR);
54 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
c1713132 55 env->cp15.c0_cachetype = 0x1dd20d2;
610c3c8a 56 env->cp15.c1_sys = 0x00090078;
c1713132 57 break;
9ee6e8bb
PB
58 case ARM_CPUID_ARM1136:
59 set_feature(env, ARM_FEATURE_V6);
60 set_feature(env, ARM_FEATURE_VFP);
61 set_feature(env, ARM_FEATURE_AUXCR);
62 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
63 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
64 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
65 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
66 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
67 env->cp15.c0_cachetype = 0x1dd20d2;
68 break;
69 case ARM_CPUID_ARM11MPCORE:
70 set_feature(env, ARM_FEATURE_V6);
71 set_feature(env, ARM_FEATURE_V6K);
72 set_feature(env, ARM_FEATURE_VFP);
73 set_feature(env, ARM_FEATURE_AUXCR);
74 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
75 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
76 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
77 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
78 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
79 env->cp15.c0_cachetype = 0x1dd20d2;
80 break;
81 case ARM_CPUID_CORTEXA8:
82 set_feature(env, ARM_FEATURE_V6);
83 set_feature(env, ARM_FEATURE_V6K);
84 set_feature(env, ARM_FEATURE_V7);
85 set_feature(env, ARM_FEATURE_AUXCR);
86 set_feature(env, ARM_FEATURE_THUMB2);
87 set_feature(env, ARM_FEATURE_VFP);
88 set_feature(env, ARM_FEATURE_VFP3);
89 set_feature(env, ARM_FEATURE_NEON);
90 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
91 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
92 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
93 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
94 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
95 env->cp15.c0_cachetype = 0x1dd20d2;
96 break;
97 case ARM_CPUID_CORTEXM3:
98 set_feature(env, ARM_FEATURE_V6);
99 set_feature(env, ARM_FEATURE_THUMB2);
100 set_feature(env, ARM_FEATURE_V7);
101 set_feature(env, ARM_FEATURE_M);
102 set_feature(env, ARM_FEATURE_DIV);
103 break;
104 case ARM_CPUID_ANY: /* For userspace emulation. */
105 set_feature(env, ARM_FEATURE_V6);
106 set_feature(env, ARM_FEATURE_V6K);
107 set_feature(env, ARM_FEATURE_V7);
108 set_feature(env, ARM_FEATURE_THUMB2);
109 set_feature(env, ARM_FEATURE_VFP);
110 set_feature(env, ARM_FEATURE_VFP3);
111 set_feature(env, ARM_FEATURE_NEON);
112 set_feature(env, ARM_FEATURE_DIV);
113 break;
c3d2689d
AZ
114 case ARM_CPUID_TI915T:
115 case ARM_CPUID_TI925T:
116 set_feature(env, ARM_FEATURE_OMAPCP);
117 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
118 env->cp15.c0_cachetype = 0x5109149;
119 env->cp15.c1_sys = 0x00000070;
120 env->cp15.c15_i_max = 0x000;
121 env->cp15.c15_i_min = 0xff0;
122 break;
c1713132
AZ
123 case ARM_CPUID_PXA250:
124 case ARM_CPUID_PXA255:
125 case ARM_CPUID_PXA260:
126 case ARM_CPUID_PXA261:
127 case ARM_CPUID_PXA262:
128 set_feature(env, ARM_FEATURE_XSCALE);
129 /* JTAG_ID is ((id << 28) | 0x09265013) */
130 env->cp15.c0_cachetype = 0xd172172;
610c3c8a 131 env->cp15.c1_sys = 0x00000078;
c1713132
AZ
132 break;
133 case ARM_CPUID_PXA270_A0:
134 case ARM_CPUID_PXA270_A1:
135 case ARM_CPUID_PXA270_B0:
136 case ARM_CPUID_PXA270_B1:
137 case ARM_CPUID_PXA270_C0:
138 case ARM_CPUID_PXA270_C5:
139 set_feature(env, ARM_FEATURE_XSCALE);
140 /* JTAG_ID is ((id << 28) | 0x09265013) */
18c9b560
AZ
141 set_feature(env, ARM_FEATURE_IWMMXT);
142 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
c1713132 143 env->cp15.c0_cachetype = 0xd172172;
610c3c8a 144 env->cp15.c1_sys = 0x00000078;
f3d6b95e
PB
145 break;
146 default:
147 cpu_abort(env, "Bad CPU ID: %x\n", id);
148 break;
149 }
150}
151
40f137e1
PB
152void cpu_reset(CPUARMState *env)
153{
f3d6b95e
PB
154 uint32_t id;
155 id = env->cp15.c0_cpuid;
156 memset(env, 0, offsetof(CPUARMState, breakpoints));
157 if (id)
158 cpu_reset_model_id(env, id);
40f137e1
PB
159#if defined (CONFIG_USER_ONLY)
160 env->uncached_cpsr = ARM_CPU_MODE_USR;
161 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
162#else
163 /* SVC mode with interrupts disabled. */
164 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
9ee6e8bb
PB
165 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
166 clear at reset. */
167 if (IS_M(env))
168 env->uncached_cpsr &= ~CPSR_I;
40f137e1
PB
169 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
170#endif
171 env->regs[15] = 0;
f3d6b95e 172 tlb_flush(env, 1);
40f137e1
PB
173}
174
aaed909a 175CPUARMState *cpu_arm_init(const char *cpu_model)
40f137e1
PB
176{
177 CPUARMState *env;
aaed909a 178 uint32_t id;
b26eefb6 179 static int inited = 0;
40f137e1 180
aaed909a
FB
181 id = cpu_arm_find_by_name(cpu_model);
182 if (id == 0)
183 return NULL;
40f137e1
PB
184 env = qemu_mallocz(sizeof(CPUARMState));
185 if (!env)
186 return NULL;
187 cpu_exec_init(env);
b26eefb6
PB
188 if (!inited) {
189 inited = 1;
190 arm_translate_init();
191 }
192
01ba9816 193 env->cpu_model_str = cpu_model;
aaed909a 194 env->cp15.c0_cpuid = id;
40f137e1 195 cpu_reset(env);
40f137e1
PB
196 return env;
197}
198
3371d272
PB
199struct arm_cpu_t {
200 uint32_t id;
201 const char *name;
202};
203
204static const struct arm_cpu_t arm_cpu_names[] = {
205 { ARM_CPUID_ARM926, "arm926"},
ce819861 206 { ARM_CPUID_ARM946, "arm946"},
3371d272 207 { ARM_CPUID_ARM1026, "arm1026"},
9ee6e8bb
PB
208 { ARM_CPUID_ARM1136, "arm1136"},
209 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
210 { ARM_CPUID_CORTEXM3, "cortex-m3"},
211 { ARM_CPUID_CORTEXA8, "cortex-a8"},
c3d2689d 212 { ARM_CPUID_TI925T, "ti925t" },
c1713132
AZ
213 { ARM_CPUID_PXA250, "pxa250" },
214 { ARM_CPUID_PXA255, "pxa255" },
215 { ARM_CPUID_PXA260, "pxa260" },
216 { ARM_CPUID_PXA261, "pxa261" },
217 { ARM_CPUID_PXA262, "pxa262" },
218 { ARM_CPUID_PXA270, "pxa270" },
219 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
220 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
221 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
222 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
223 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
224 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
9ee6e8bb 225 { ARM_CPUID_ANY, "any"},
3371d272
PB
226 { 0, NULL}
227};
228
c732abe2 229void arm_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
5adb4839
PB
230{
231 int i;
232
c732abe2 233 (*cpu_fprintf)(f, "Available CPUs:\n");
5adb4839 234 for (i = 0; arm_cpu_names[i].name; i++) {
c732abe2 235 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
5adb4839
PB
236 }
237}
238
aaed909a
FB
239/* return 0 if not found */
240static uint32_t cpu_arm_find_by_name(const char *name)
40f137e1 241{
3371d272
PB
242 int i;
243 uint32_t id;
244
245 id = 0;
3371d272
PB
246 for (i = 0; arm_cpu_names[i].name; i++) {
247 if (strcmp(name, arm_cpu_names[i].name) == 0) {
248 id = arm_cpu_names[i].id;
249 break;
250 }
251 }
aaed909a 252 return id;
40f137e1
PB
253}
254
255void cpu_arm_close(CPUARMState *env)
256{
257 free(env);
258}
259
2f4a40e5
AZ
260uint32_t cpsr_read(CPUARMState *env)
261{
262 int ZF;
6fbe23d5
PB
263 ZF = (env->ZF == 0);
264 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
265 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
266 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
267 | ((env->condexec_bits & 0xfc) << 8)
268 | (env->GE << 16);
269}
270
271void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
272{
2f4a40e5 273 if (mask & CPSR_NZCV) {
6fbe23d5
PB
274 env->ZF = (~val) & CPSR_Z;
275 env->NF = val;
2f4a40e5
AZ
276 env->CF = (val >> 29) & 1;
277 env->VF = (val << 3) & 0x80000000;
278 }
279 if (mask & CPSR_Q)
280 env->QF = ((val & CPSR_Q) != 0);
281 if (mask & CPSR_T)
282 env->thumb = ((val & CPSR_T) != 0);
283 if (mask & CPSR_IT_0_1) {
284 env->condexec_bits &= ~3;
285 env->condexec_bits |= (val >> 25) & 3;
286 }
287 if (mask & CPSR_IT_2_7) {
288 env->condexec_bits &= 3;
289 env->condexec_bits |= (val >> 8) & 0xfc;
290 }
291 if (mask & CPSR_GE) {
292 env->GE = (val >> 16) & 0xf;
293 }
294
295 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
296 switch_mode(env, val & CPSR_M);
297 }
298 mask &= ~CACHED_CPSR_BITS;
299 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
300}
301
b26eefb6
PB
302/* Sign/zero extend */
303uint32_t HELPER(sxtb16)(uint32_t x)
304{
305 uint32_t res;
306 res = (uint16_t)(int8_t)x;
307 res |= (uint32_t)(int8_t)(x >> 16) << 16;
308 return res;
309}
310
311uint32_t HELPER(uxtb16)(uint32_t x)
312{
313 uint32_t res;
314 res = (uint16_t)(uint8_t)x;
315 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
316 return res;
317}
318
f51bbbfe
PB
319uint32_t HELPER(clz)(uint32_t x)
320{
321 int count;
322 for (count = 32; x; count--)
323 x >>= 1;
324 return count;
325}
326
3670669c
PB
327int32_t HELPER(sdiv)(int32_t num, int32_t den)
328{
329 if (den == 0)
330 return 0;
331 return num / den;
332}
333
334uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
335{
336 if (den == 0)
337 return 0;
338 return num / den;
339}
340
341uint32_t HELPER(rbit)(uint32_t x)
342{
343 x = ((x & 0xff000000) >> 24)
344 | ((x & 0x00ff0000) >> 8)
345 | ((x & 0x0000ff00) << 8)
346 | ((x & 0x000000ff) << 24);
347 x = ((x & 0xf0f0f0f0) >> 4)
348 | ((x & 0x0f0f0f0f) << 4);
349 x = ((x & 0x88888888) >> 3)
350 | ((x & 0x44444444) >> 1)
351 | ((x & 0x22222222) << 1)
352 | ((x & 0x11111111) << 3);
353 return x;
354}
355
ad69471c
PB
356uint32_t HELPER(abs)(uint32_t x)
357{
358 return ((int32_t)x < 0) ? -x : x;
359}
360
5fafdf24 361#if defined(CONFIG_USER_ONLY)
b5ff1b31
FB
362
363void do_interrupt (CPUState *env)
364{
365 env->exception_index = -1;
366}
367
9ee6e8bb
PB
368/* Structure used to record exclusive memory locations. */
369typedef struct mmon_state {
370 struct mmon_state *next;
371 CPUARMState *cpu_env;
372 uint32_t addr;
373} mmon_state;
374
375/* Chain of current locks. */
376static mmon_state* mmon_head = NULL;
377
b5ff1b31 378int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
6ebbf390 379 int mmu_idx, int is_softmmu)
b5ff1b31
FB
380{
381 if (rw == 2) {
382 env->exception_index = EXCP_PREFETCH_ABORT;
383 env->cp15.c6_insn = address;
384 } else {
385 env->exception_index = EXCP_DATA_ABORT;
386 env->cp15.c6_data = address;
387 }
388 return 1;
389}
390
9ee6e8bb
PB
391static void allocate_mmon_state(CPUState *env)
392{
393 env->mmon_entry = malloc(sizeof (mmon_state));
394 if (!env->mmon_entry)
395 abort();
396 memset (env->mmon_entry, 0, sizeof (mmon_state));
397 env->mmon_entry->cpu_env = env;
398 mmon_head = env->mmon_entry;
399}
400
401/* Flush any monitor locks for the specified address. */
402static void flush_mmon(uint32_t addr)
403{
404 mmon_state *mon;
405
406 for (mon = mmon_head; mon; mon = mon->next)
407 {
408 if (mon->addr != addr)
409 continue;
410
411 mon->addr = 0;
412 break;
413 }
414}
415
416/* Mark an address for exclusive access. */
8f8e3aa4 417void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
9ee6e8bb
PB
418{
419 if (!env->mmon_entry)
420 allocate_mmon_state(env);
421 /* Clear any previous locks. */
422 flush_mmon(addr);
423 env->mmon_entry->addr = addr;
424}
425
426/* Test if an exclusive address is still exclusive. Returns zero
427 if the address is still exclusive. */
8f8e3aa4 428uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
9ee6e8bb
PB
429{
430 int res;
431
432 if (!env->mmon_entry)
433 return 1;
434 if (env->mmon_entry->addr == addr)
435 res = 0;
436 else
437 res = 1;
438 flush_mmon(addr);
439 return res;
440}
441
8f8e3aa4 442void HELPER(clrex)(CPUState *env)
9ee6e8bb
PB
443{
444 if (!(env->mmon_entry && env->mmon_entry->addr))
445 return;
446 flush_mmon(env->mmon_entry->addr);
447}
448
9b3c35e0 449target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
b5ff1b31
FB
450{
451 return addr;
452}
453
454/* These should probably raise undefined insn exceptions. */
8984bd2e 455void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
c1713132
AZ
456{
457 int op1 = (insn >> 8) & 0xf;
458 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
459 return;
460}
461
8984bd2e 462uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
c1713132
AZ
463{
464 int op1 = (insn >> 8) & 0xf;
465 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
466 return 0;
467}
468
8984bd2e 469void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
b5ff1b31
FB
470{
471 cpu_abort(env, "cp15 insn %08x\n", insn);
472}
473
8984bd2e 474uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
b5ff1b31
FB
475{
476 cpu_abort(env, "cp15 insn %08x\n", insn);
477 return 0;
478}
479
9ee6e8bb 480/* These should probably raise undefined insn exceptions. */
8984bd2e 481void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
9ee6e8bb
PB
482{
483 cpu_abort(env, "v7m_mrs %d\n", reg);
484}
485
8984bd2e 486uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
9ee6e8bb
PB
487{
488 cpu_abort(env, "v7m_mrs %d\n", reg);
489 return 0;
490}
491
b5ff1b31
FB
492void switch_mode(CPUState *env, int mode)
493{
494 if (mode != ARM_CPU_MODE_USR)
495 cpu_abort(env, "Tried to switch out of user mode\n");
496}
497
b0109805 498void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
9ee6e8bb
PB
499{
500 cpu_abort(env, "banked r13 write\n");
501}
502
b0109805 503uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
9ee6e8bb
PB
504{
505 cpu_abort(env, "banked r13 read\n");
506 return 0;
507}
508
b5ff1b31
FB
509#else
510
8e71621f
PB
511extern int semihosting_enabled;
512
b5ff1b31
FB
513/* Map CPU modes onto saved register banks. */
514static inline int bank_number (int mode)
515{
516 switch (mode) {
517 case ARM_CPU_MODE_USR:
518 case ARM_CPU_MODE_SYS:
519 return 0;
520 case ARM_CPU_MODE_SVC:
521 return 1;
522 case ARM_CPU_MODE_ABT:
523 return 2;
524 case ARM_CPU_MODE_UND:
525 return 3;
526 case ARM_CPU_MODE_IRQ:
527 return 4;
528 case ARM_CPU_MODE_FIQ:
529 return 5;
530 }
531 cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
532 return -1;
533}
534
535void switch_mode(CPUState *env, int mode)
536{
537 int old_mode;
538 int i;
539
540 old_mode = env->uncached_cpsr & CPSR_M;
541 if (mode == old_mode)
542 return;
543
544 if (old_mode == ARM_CPU_MODE_FIQ) {
545 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 546 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
547 } else if (mode == ARM_CPU_MODE_FIQ) {
548 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 549 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
550 }
551
552 i = bank_number(old_mode);
553 env->banked_r13[i] = env->regs[13];
554 env->banked_r14[i] = env->regs[14];
555 env->banked_spsr[i] = env->spsr;
556
557 i = bank_number(mode);
558 env->regs[13] = env->banked_r13[i];
559 env->regs[14] = env->banked_r14[i];
560 env->spsr = env->banked_spsr[i];
561}
562
9ee6e8bb
PB
563static void v7m_push(CPUARMState *env, uint32_t val)
564{
565 env->regs[13] -= 4;
566 stl_phys(env->regs[13], val);
567}
568
569static uint32_t v7m_pop(CPUARMState *env)
570{
571 uint32_t val;
572 val = ldl_phys(env->regs[13]);
573 env->regs[13] += 4;
574 return val;
575}
576
577/* Switch to V7M main or process stack pointer. */
578static void switch_v7m_sp(CPUARMState *env, int process)
579{
580 uint32_t tmp;
581 if (env->v7m.current_sp != process) {
582 tmp = env->v7m.other_sp;
583 env->v7m.other_sp = env->regs[13];
584 env->regs[13] = tmp;
585 env->v7m.current_sp = process;
586 }
587}
588
589static void do_v7m_exception_exit(CPUARMState *env)
590{
591 uint32_t type;
592 uint32_t xpsr;
593
594 type = env->regs[15];
595 if (env->v7m.exception != 0)
596 armv7m_nvic_complete_irq(env->v7m.nvic, env->v7m.exception);
597
598 /* Switch to the target stack. */
599 switch_v7m_sp(env, (type & 4) != 0);
600 /* Pop registers. */
601 env->regs[0] = v7m_pop(env);
602 env->regs[1] = v7m_pop(env);
603 env->regs[2] = v7m_pop(env);
604 env->regs[3] = v7m_pop(env);
605 env->regs[12] = v7m_pop(env);
606 env->regs[14] = v7m_pop(env);
607 env->regs[15] = v7m_pop(env);
608 xpsr = v7m_pop(env);
609 xpsr_write(env, xpsr, 0xfffffdff);
610 /* Undo stack alignment. */
611 if (xpsr & 0x200)
612 env->regs[13] |= 4;
613 /* ??? The exception return type specifies Thread/Handler mode. However
614 this is also implied by the xPSR value. Not sure what to do
615 if there is a mismatch. */
616 /* ??? Likewise for mismatches between the CONTROL register and the stack
617 pointer. */
618}
619
620void do_interrupt_v7m(CPUARMState *env)
621{
622 uint32_t xpsr = xpsr_read(env);
623 uint32_t lr;
624 uint32_t addr;
625
626 lr = 0xfffffff1;
627 if (env->v7m.current_sp)
628 lr |= 4;
629 if (env->v7m.exception == 0)
630 lr |= 8;
631
632 /* For exceptions we just mark as pending on the NVIC, and let that
633 handle it. */
634 /* TODO: Need to escalate if the current priority is higher than the
635 one we're raising. */
636 switch (env->exception_index) {
637 case EXCP_UDEF:
638 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_USAGE);
639 return;
640 case EXCP_SWI:
641 env->regs[15] += 2;
642 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_SVC);
643 return;
644 case EXCP_PREFETCH_ABORT:
645 case EXCP_DATA_ABORT:
646 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_MEM);
647 return;
648 case EXCP_BKPT:
2ad207d4
PB
649 if (semihosting_enabled) {
650 int nr;
651 nr = lduw_code(env->regs[15]) & 0xff;
652 if (nr == 0xab) {
653 env->regs[15] += 2;
654 env->regs[0] = do_arm_semihosting(env);
655 return;
656 }
657 }
9ee6e8bb
PB
658 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_DEBUG);
659 return;
660 case EXCP_IRQ:
661 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->v7m.nvic);
662 break;
663 case EXCP_EXCEPTION_EXIT:
664 do_v7m_exception_exit(env);
665 return;
666 default:
667 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
668 return; /* Never happens. Keep compiler happy. */
669 }
670
671 /* Align stack pointer. */
672 /* ??? Should only do this if Configuration Control Register
673 STACKALIGN bit is set. */
674 if (env->regs[13] & 4) {
675 env->regs[13] += 4;
676 xpsr |= 0x200;
677 }
678 /* Switch to the hander mode. */
679 v7m_push(env, xpsr);
680 v7m_push(env, env->regs[15]);
681 v7m_push(env, env->regs[14]);
682 v7m_push(env, env->regs[12]);
683 v7m_push(env, env->regs[3]);
684 v7m_push(env, env->regs[2]);
685 v7m_push(env, env->regs[1]);
686 v7m_push(env, env->regs[0]);
687 switch_v7m_sp(env, 0);
688 env->uncached_cpsr &= ~CPSR_IT;
689 env->regs[14] = lr;
690 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
691 env->regs[15] = addr & 0xfffffffe;
692 env->thumb = addr & 1;
693}
694
b5ff1b31
FB
695/* Handle a CPU exception. */
696void do_interrupt(CPUARMState *env)
697{
698 uint32_t addr;
699 uint32_t mask;
700 int new_mode;
701 uint32_t offset;
702
9ee6e8bb
PB
703 if (IS_M(env)) {
704 do_interrupt_v7m(env);
705 return;
706 }
b5ff1b31
FB
707 /* TODO: Vectored interrupt controller. */
708 switch (env->exception_index) {
709 case EXCP_UDEF:
710 new_mode = ARM_CPU_MODE_UND;
711 addr = 0x04;
712 mask = CPSR_I;
713 if (env->thumb)
714 offset = 2;
715 else
716 offset = 4;
717 break;
718 case EXCP_SWI:
8e71621f
PB
719 if (semihosting_enabled) {
720 /* Check for semihosting interrupt. */
721 if (env->thumb) {
722 mask = lduw_code(env->regs[15] - 2) & 0xff;
723 } else {
724 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
725 }
726 /* Only intercept calls from privileged modes, to provide some
727 semblance of security. */
728 if (((mask == 0x123456 && !env->thumb)
729 || (mask == 0xab && env->thumb))
730 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
731 env->regs[0] = do_arm_semihosting(env);
732 return;
733 }
734 }
b5ff1b31
FB
735 new_mode = ARM_CPU_MODE_SVC;
736 addr = 0x08;
737 mask = CPSR_I;
738 /* The PC already points to the next instructon. */
739 offset = 0;
740 break;
06c949e6 741 case EXCP_BKPT:
9ee6e8bb 742 /* See if this is a semihosting syscall. */
2ad207d4 743 if (env->thumb && semihosting_enabled) {
9ee6e8bb
PB
744 mask = lduw_code(env->regs[15]) & 0xff;
745 if (mask == 0xab
746 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
747 env->regs[15] += 2;
748 env->regs[0] = do_arm_semihosting(env);
749 return;
750 }
751 }
752 /* Fall through to prefetch abort. */
753 case EXCP_PREFETCH_ABORT:
b5ff1b31
FB
754 new_mode = ARM_CPU_MODE_ABT;
755 addr = 0x0c;
756 mask = CPSR_A | CPSR_I;
757 offset = 4;
758 break;
759 case EXCP_DATA_ABORT:
760 new_mode = ARM_CPU_MODE_ABT;
761 addr = 0x10;
762 mask = CPSR_A | CPSR_I;
763 offset = 8;
764 break;
765 case EXCP_IRQ:
766 new_mode = ARM_CPU_MODE_IRQ;
767 addr = 0x18;
768 /* Disable IRQ and imprecise data aborts. */
769 mask = CPSR_A | CPSR_I;
770 offset = 4;
771 break;
772 case EXCP_FIQ:
773 new_mode = ARM_CPU_MODE_FIQ;
774 addr = 0x1c;
775 /* Disable FIQ, IRQ and imprecise data aborts. */
776 mask = CPSR_A | CPSR_I | CPSR_F;
777 offset = 4;
778 break;
779 default:
780 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
781 return; /* Never happens. Keep compiler happy. */
782 }
783 /* High vectors. */
784 if (env->cp15.c1_sys & (1 << 13)) {
785 addr += 0xffff0000;
786 }
787 switch_mode (env, new_mode);
788 env->spsr = cpsr_read(env);
9ee6e8bb
PB
789 /* Clear IT bits. */
790 env->condexec_bits = 0;
6d7e6326 791 /* Switch to the new mode, and switch to Arm mode. */
b5ff1b31 792 /* ??? Thumb interrupt handlers not implemented. */
6d7e6326 793 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
b5ff1b31 794 env->uncached_cpsr |= mask;
6d7e6326 795 env->thumb = 0;
b5ff1b31
FB
796 env->regs[14] = env->regs[15] + offset;
797 env->regs[15] = addr;
798 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
799}
800
801/* Check section/page access permissions.
802 Returns the page protection flags, or zero if the access is not
803 permitted. */
804static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
805 int is_user)
806{
9ee6e8bb
PB
807 int prot_ro;
808
b5ff1b31
FB
809 if (domain == 3)
810 return PAGE_READ | PAGE_WRITE;
811
9ee6e8bb
PB
812 if (access_type == 1)
813 prot_ro = 0;
814 else
815 prot_ro = PAGE_READ;
816
b5ff1b31
FB
817 switch (ap) {
818 case 0:
78600320 819 if (access_type == 1)
b5ff1b31
FB
820 return 0;
821 switch ((env->cp15.c1_sys >> 8) & 3) {
822 case 1:
823 return is_user ? 0 : PAGE_READ;
824 case 2:
825 return PAGE_READ;
826 default:
827 return 0;
828 }
829 case 1:
830 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
831 case 2:
832 if (is_user)
9ee6e8bb 833 return prot_ro;
b5ff1b31
FB
834 else
835 return PAGE_READ | PAGE_WRITE;
836 case 3:
837 return PAGE_READ | PAGE_WRITE;
9ee6e8bb
PB
838 case 4: case 7: /* Reserved. */
839 return 0;
840 case 5:
841 return is_user ? 0 : prot_ro;
842 case 6:
843 return prot_ro;
b5ff1b31
FB
844 default:
845 abort();
846 }
847}
848
9ee6e8bb
PB
849static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
850 int is_user, uint32_t *phys_ptr, int *prot)
b5ff1b31
FB
851{
852 int code;
853 uint32_t table;
854 uint32_t desc;
855 int type;
856 int ap;
857 int domain;
858 uint32_t phys_addr;
859
9ee6e8bb
PB
860 /* Pagetable walk. */
861 /* Lookup l1 descriptor. */
862 if (address & env->cp15.c2_mask)
863 table = env->cp15.c2_base1;
864 else
865 table = env->cp15.c2_base0;
866 table = (table & 0xffffc000) | ((address >> 18) & 0x3ffc);
867 desc = ldl_phys(table);
868 type = (desc & 3);
869 domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
870 if (type == 0) {
871 /* Secton translation fault. */
872 code = 5;
873 goto do_fault;
874 }
875 if (domain == 0 || domain == 2) {
876 if (type == 2)
877 code = 9; /* Section domain fault. */
878 else
879 code = 11; /* Page domain fault. */
880 goto do_fault;
881 }
882 if (type == 2) {
883 /* 1Mb section. */
884 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
885 ap = (desc >> 10) & 3;
886 code = 13;
887 } else {
888 /* Lookup l2 entry. */
889 if (type == 1) {
890 /* Coarse pagetable. */
891 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
892 } else {
893 /* Fine pagetable. */
894 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
895 }
896 desc = ldl_phys(table);
897 switch (desc & 3) {
898 case 0: /* Page translation fault. */
899 code = 7;
900 goto do_fault;
901 case 1: /* 64k page. */
902 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
903 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
ce819861 904 break;
9ee6e8bb
PB
905 case 2: /* 4k page. */
906 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
907 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
ce819861 908 break;
9ee6e8bb
PB
909 case 3: /* 1k page. */
910 if (type == 1) {
911 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
912 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
913 } else {
914 /* Page translation fault. */
915 code = 7;
916 goto do_fault;
917 }
918 } else {
919 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
920 }
921 ap = (desc >> 4) & 3;
ce819861
PB
922 break;
923 default:
9ee6e8bb
PB
924 /* Never happens, but compiler isn't smart enough to tell. */
925 abort();
ce819861 926 }
9ee6e8bb
PB
927 code = 15;
928 }
929 *prot = check_ap(env, ap, domain, access_type, is_user);
930 if (!*prot) {
931 /* Access permission fault. */
932 goto do_fault;
933 }
934 *phys_ptr = phys_addr;
935 return 0;
936do_fault:
937 return code | (domain << 4);
938}
939
940static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
941 int is_user, uint32_t *phys_ptr, int *prot)
942{
943 int code;
944 uint32_t table;
945 uint32_t desc;
946 uint32_t xn;
947 int type;
948 int ap;
949 int domain;
950 uint32_t phys_addr;
951
952 /* Pagetable walk. */
953 /* Lookup l1 descriptor. */
954 if (address & env->cp15.c2_mask)
955 table = env->cp15.c2_base1;
956 else
957 table = env->cp15.c2_base0;
958 table = (table & 0xffffc000) | ((address >> 18) & 0x3ffc);
959 desc = ldl_phys(table);
960 type = (desc & 3);
961 if (type == 0) {
962 /* Secton translation fault. */
963 code = 5;
964 domain = 0;
965 goto do_fault;
966 } else if (type == 2 && (desc & (1 << 18))) {
967 /* Supersection. */
968 domain = 0;
b5ff1b31 969 } else {
9ee6e8bb
PB
970 /* Section or page. */
971 domain = (desc >> 4) & 0x1e;
972 }
973 domain = (env->cp15.c3 >> domain) & 3;
974 if (domain == 0 || domain == 2) {
975 if (type == 2)
976 code = 9; /* Section domain fault. */
977 else
978 code = 11; /* Page domain fault. */
979 goto do_fault;
980 }
981 if (type == 2) {
982 if (desc & (1 << 18)) {
983 /* Supersection. */
984 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
b5ff1b31 985 } else {
9ee6e8bb
PB
986 /* Section. */
987 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
b5ff1b31 988 }
9ee6e8bb
PB
989 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
990 xn = desc & (1 << 4);
991 code = 13;
992 } else {
993 /* Lookup l2 entry. */
994 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
995 desc = ldl_phys(table);
996 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
997 switch (desc & 3) {
998 case 0: /* Page translation fault. */
999 code = 7;
b5ff1b31 1000 goto do_fault;
9ee6e8bb
PB
1001 case 1: /* 64k page. */
1002 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1003 xn = desc & (1 << 15);
1004 break;
1005 case 2: case 3: /* 4k page. */
1006 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1007 xn = desc & 1;
1008 break;
1009 default:
1010 /* Never happens, but compiler isn't smart enough to tell. */
1011 abort();
b5ff1b31 1012 }
9ee6e8bb
PB
1013 code = 15;
1014 }
1015 if (xn && access_type == 2)
1016 goto do_fault;
1017
1018 *prot = check_ap(env, ap, domain, access_type, is_user);
1019 if (!*prot) {
1020 /* Access permission fault. */
1021 goto do_fault;
b5ff1b31 1022 }
9ee6e8bb 1023 *phys_ptr = phys_addr;
b5ff1b31
FB
1024 return 0;
1025do_fault:
1026 return code | (domain << 4);
1027}
1028
9ee6e8bb
PB
1029static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1030 int is_user, uint32_t *phys_ptr, int *prot)
1031{
1032 int n;
1033 uint32_t mask;
1034 uint32_t base;
1035
1036 *phys_ptr = address;
1037 for (n = 7; n >= 0; n--) {
1038 base = env->cp15.c6_region[n];
1039 if ((base & 1) == 0)
1040 continue;
1041 mask = 1 << ((base >> 1) & 0x1f);
1042 /* Keep this shift separate from the above to avoid an
1043 (undefined) << 32. */
1044 mask = (mask << 1) - 1;
1045 if (((base ^ address) & ~mask) == 0)
1046 break;
1047 }
1048 if (n < 0)
1049 return 2;
1050
1051 if (access_type == 2) {
1052 mask = env->cp15.c5_insn;
1053 } else {
1054 mask = env->cp15.c5_data;
1055 }
1056 mask = (mask >> (n * 4)) & 0xf;
1057 switch (mask) {
1058 case 0:
1059 return 1;
1060 case 1:
1061 if (is_user)
1062 return 1;
1063 *prot = PAGE_READ | PAGE_WRITE;
1064 break;
1065 case 2:
1066 *prot = PAGE_READ;
1067 if (!is_user)
1068 *prot |= PAGE_WRITE;
1069 break;
1070 case 3:
1071 *prot = PAGE_READ | PAGE_WRITE;
1072 break;
1073 case 5:
1074 if (is_user)
1075 return 1;
1076 *prot = PAGE_READ;
1077 break;
1078 case 6:
1079 *prot = PAGE_READ;
1080 break;
1081 default:
1082 /* Bad permission. */
1083 return 1;
1084 }
1085 return 0;
1086}
1087
1088static inline int get_phys_addr(CPUState *env, uint32_t address,
1089 int access_type, int is_user,
1090 uint32_t *phys_ptr, int *prot)
1091{
1092 /* Fast Context Switch Extension. */
1093 if (address < 0x02000000)
1094 address += env->cp15.c13_fcse;
1095
1096 if ((env->cp15.c1_sys & 1) == 0) {
1097 /* MMU/MPU disabled. */
1098 *phys_ptr = address;
1099 *prot = PAGE_READ | PAGE_WRITE;
1100 return 0;
1101 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1102 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1103 prot);
1104 } else if (env->cp15.c1_sys & (1 << 23)) {
1105 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1106 prot);
1107 } else {
1108 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1109 prot);
1110 }
1111}
1112
b5ff1b31 1113int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
6ebbf390 1114 int access_type, int mmu_idx, int is_softmmu)
b5ff1b31
FB
1115{
1116 uint32_t phys_addr;
1117 int prot;
6ebbf390 1118 int ret, is_user;
b5ff1b31 1119
6ebbf390 1120 is_user = mmu_idx == MMU_USER_IDX;
b5ff1b31
FB
1121 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot);
1122 if (ret == 0) {
1123 /* Map a single [sub]page. */
1124 phys_addr &= ~(uint32_t)0x3ff;
1125 address &= ~(uint32_t)0x3ff;
6ebbf390 1126 return tlb_set_page (env, address, phys_addr, prot, mmu_idx,
b5ff1b31
FB
1127 is_softmmu);
1128 }
1129
1130 if (access_type == 2) {
1131 env->cp15.c5_insn = ret;
1132 env->cp15.c6_insn = address;
1133 env->exception_index = EXCP_PREFETCH_ABORT;
1134 } else {
1135 env->cp15.c5_data = ret;
9ee6e8bb
PB
1136 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1137 env->cp15.c5_data |= (1 << 11);
b5ff1b31
FB
1138 env->cp15.c6_data = address;
1139 env->exception_index = EXCP_DATA_ABORT;
1140 }
1141 return 1;
1142}
1143
9b3c35e0 1144target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
b5ff1b31
FB
1145{
1146 uint32_t phys_addr;
1147 int prot;
1148 int ret;
1149
1150 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot);
1151
1152 if (ret != 0)
1153 return -1;
1154
1155 return phys_addr;
1156}
1157
9ee6e8bb
PB
1158/* Not really implemented. Need to figure out a sane way of doing this.
1159 Maybe add generic watchpoint support and use that. */
1160
8f8e3aa4 1161void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
9ee6e8bb
PB
1162{
1163 env->mmon_addr = addr;
1164}
1165
8f8e3aa4 1166uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
9ee6e8bb
PB
1167{
1168 return (env->mmon_addr != addr);
1169}
1170
8f8e3aa4 1171void HELPER(clrex)(CPUState *env)
9ee6e8bb
PB
1172{
1173 env->mmon_addr = -1;
1174}
1175
8984bd2e 1176void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
c1713132
AZ
1177{
1178 int cp_num = (insn >> 8) & 0xf;
1179 int cp_info = (insn >> 5) & 7;
1180 int src = (insn >> 16) & 0xf;
1181 int operand = insn & 0xf;
1182
1183 if (env->cp[cp_num].cp_write)
1184 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1185 cp_info, src, operand, val);
1186}
1187
8984bd2e 1188uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
c1713132
AZ
1189{
1190 int cp_num = (insn >> 8) & 0xf;
1191 int cp_info = (insn >> 5) & 7;
1192 int dest = (insn >> 16) & 0xf;
1193 int operand = insn & 0xf;
1194
1195 if (env->cp[cp_num].cp_read)
1196 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1197 cp_info, dest, operand);
1198 return 0;
1199}
1200
ce819861
PB
1201/* Return basic MPU access permission bits. */
1202static uint32_t simple_mpu_ap_bits(uint32_t val)
1203{
1204 uint32_t ret;
1205 uint32_t mask;
1206 int i;
1207 ret = 0;
1208 mask = 3;
1209 for (i = 0; i < 16; i += 2) {
1210 ret |= (val >> i) & mask;
1211 mask <<= 2;
1212 }
1213 return ret;
1214}
1215
1216/* Pad basic MPU access permission bits to extended format. */
1217static uint32_t extended_mpu_ap_bits(uint32_t val)
1218{
1219 uint32_t ret;
1220 uint32_t mask;
1221 int i;
1222 ret = 0;
1223 mask = 3;
1224 for (i = 0; i < 16; i += 2) {
1225 ret |= (val & mask) << i;
1226 mask <<= 2;
1227 }
1228 return ret;
1229}
1230
8984bd2e 1231void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
b5ff1b31 1232{
9ee6e8bb
PB
1233 int op1;
1234 int op2;
1235 int crm;
b5ff1b31 1236
9ee6e8bb 1237 op1 = (insn >> 21) & 7;
b5ff1b31 1238 op2 = (insn >> 5) & 7;
ce819861 1239 crm = insn & 0xf;
b5ff1b31 1240 switch ((insn >> 16) & 0xf) {
9ee6e8bb
PB
1241 case 0:
1242 if (((insn >> 21) & 7) == 2) {
1243 /* ??? Select cache level. Ignore. */
1244 return;
1245 }
1246 /* ID codes. */
610c3c8a
AZ
1247 if (arm_feature(env, ARM_FEATURE_XSCALE))
1248 break;
c3d2689d
AZ
1249 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1250 break;
b5ff1b31
FB
1251 goto bad_reg;
1252 case 1: /* System configuration. */
c3d2689d
AZ
1253 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1254 op2 = 0;
b5ff1b31
FB
1255 switch (op2) {
1256 case 0:
ce819861 1257 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
c1713132 1258 env->cp15.c1_sys = val;
b5ff1b31
FB
1259 /* ??? Lots of these bits are not implemented. */
1260 /* This may enable/disable the MMU, so do a TLB flush. */
1261 tlb_flush(env, 1);
1262 break;
9ee6e8bb 1263 case 1: /* Auxiliary cotrol register. */
610c3c8a
AZ
1264 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1265 env->cp15.c1_xscaleauxcr = val;
c1713132 1266 break;
610c3c8a 1267 }
9ee6e8bb
PB
1268 /* Not implemented. */
1269 break;
b5ff1b31 1270 case 2:
610c3c8a
AZ
1271 if (arm_feature(env, ARM_FEATURE_XSCALE))
1272 goto bad_reg;
b5ff1b31
FB
1273 env->cp15.c1_coproc = val;
1274 /* ??? Is this safe when called from within a TB? */
1275 tb_flush(env);
c1713132 1276 break;
b5ff1b31
FB
1277 default:
1278 goto bad_reg;
1279 }
1280 break;
ce819861
PB
1281 case 2: /* MMU Page table control / MPU cache control. */
1282 if (arm_feature(env, ARM_FEATURE_MPU)) {
1283 switch (op2) {
1284 case 0:
1285 env->cp15.c2_data = val;
1286 break;
1287 case 1:
1288 env->cp15.c2_insn = val;
1289 break;
1290 default:
1291 goto bad_reg;
1292 }
1293 } else {
9ee6e8bb
PB
1294 switch (op2) {
1295 case 0:
1296 env->cp15.c2_base0 = val;
1297 break;
1298 case 1:
1299 env->cp15.c2_base1 = val;
1300 break;
1301 case 2:
1302 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1303 break;
1304 default:
1305 goto bad_reg;
1306 }
ce819861 1307 }
b5ff1b31 1308 break;
ce819861 1309 case 3: /* MMU Domain access control / MPU write buffer control. */
b5ff1b31 1310 env->cp15.c3 = val;
405ee3ad 1311 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
b5ff1b31
FB
1312 break;
1313 case 4: /* Reserved. */
1314 goto bad_reg;
ce819861 1315 case 5: /* MMU Fault status / MPU access permission. */
c3d2689d
AZ
1316 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1317 op2 = 0;
b5ff1b31
FB
1318 switch (op2) {
1319 case 0:
ce819861
PB
1320 if (arm_feature(env, ARM_FEATURE_MPU))
1321 val = extended_mpu_ap_bits(val);
b5ff1b31
FB
1322 env->cp15.c5_data = val;
1323 break;
1324 case 1:
ce819861
PB
1325 if (arm_feature(env, ARM_FEATURE_MPU))
1326 val = extended_mpu_ap_bits(val);
b5ff1b31
FB
1327 env->cp15.c5_insn = val;
1328 break;
ce819861
PB
1329 case 2:
1330 if (!arm_feature(env, ARM_FEATURE_MPU))
1331 goto bad_reg;
1332 env->cp15.c5_data = val;
b5ff1b31 1333 break;
ce819861
PB
1334 case 3:
1335 if (!arm_feature(env, ARM_FEATURE_MPU))
1336 goto bad_reg;
1337 env->cp15.c5_insn = val;
b5ff1b31
FB
1338 break;
1339 default:
1340 goto bad_reg;
1341 }
1342 break;
ce819861
PB
1343 case 6: /* MMU Fault address / MPU base/size. */
1344 if (arm_feature(env, ARM_FEATURE_MPU)) {
1345 if (crm >= 8)
1346 goto bad_reg;
1347 env->cp15.c6_region[crm] = val;
1348 } else {
c3d2689d
AZ
1349 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1350 op2 = 0;
ce819861
PB
1351 switch (op2) {
1352 case 0:
1353 env->cp15.c6_data = val;
1354 break;
9ee6e8bb
PB
1355 case 1: /* ??? This is WFAR on armv6 */
1356 case 2:
ce819861
PB
1357 env->cp15.c6_insn = val;
1358 break;
1359 default:
1360 goto bad_reg;
1361 }
1362 }
1363 break;
b5ff1b31 1364 case 7: /* Cache control. */
c3d2689d
AZ
1365 env->cp15.c15_i_max = 0x000;
1366 env->cp15.c15_i_min = 0xff0;
b5ff1b31 1367 /* No cache, so nothing to do. */
9ee6e8bb 1368 /* ??? MPCore has VA to PA translation functions. */
b5ff1b31
FB
1369 break;
1370 case 8: /* MMU TLB control. */
1371 switch (op2) {
1372 case 0: /* Invalidate all. */
1373 tlb_flush(env, 0);
1374 break;
1375 case 1: /* Invalidate single TLB entry. */
1376#if 0
1377 /* ??? This is wrong for large pages and sections. */
1378 /* As an ugly hack to make linux work we always flush a 4K
1379 pages. */
1380 val &= 0xfffff000;
1381 tlb_flush_page(env, val);
1382 tlb_flush_page(env, val + 0x400);
1383 tlb_flush_page(env, val + 0x800);
1384 tlb_flush_page(env, val + 0xc00);
1385#else
1386 tlb_flush(env, 1);
1387#endif
1388 break;
9ee6e8bb
PB
1389 case 2: /* Invalidate on ASID. */
1390 tlb_flush(env, val == 0);
1391 break;
1392 case 3: /* Invalidate single entry on MVA. */
1393 /* ??? This is like case 1, but ignores ASID. */
1394 tlb_flush(env, 1);
1395 break;
b5ff1b31
FB
1396 default:
1397 goto bad_reg;
1398 }
1399 break;
ce819861 1400 case 9:
c3d2689d
AZ
1401 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1402 break;
ce819861
PB
1403 switch (crm) {
1404 case 0: /* Cache lockdown. */
9ee6e8bb
PB
1405 switch (op1) {
1406 case 0: /* L1 cache. */
1407 switch (op2) {
1408 case 0:
1409 env->cp15.c9_data = val;
1410 break;
1411 case 1:
1412 env->cp15.c9_insn = val;
1413 break;
1414 default:
1415 goto bad_reg;
1416 }
1417 break;
1418 case 1: /* L2 cache. */
1419 /* Ignore writes to L2 lockdown/auxiliary registers. */
1420 break;
1421 default:
1422 goto bad_reg;
1423 }
1424 break;
ce819861
PB
1425 case 1: /* TCM memory region registers. */
1426 /* Not implemented. */
1427 goto bad_reg;
b5ff1b31
FB
1428 default:
1429 goto bad_reg;
1430 }
1431 break;
1432 case 10: /* MMU TLB lockdown. */
1433 /* ??? TLB lockdown not implemented. */
1434 break;
b5ff1b31
FB
1435 case 12: /* Reserved. */
1436 goto bad_reg;
1437 case 13: /* Process ID. */
1438 switch (op2) {
1439 case 0:
d07edbfa
PB
1440 /* Unlike real hardware the qemu TLB uses virtual addresses,
1441 not modified virtual addresses, so this causes a TLB flush.
1442 */
1443 if (env->cp15.c13_fcse != val)
1444 tlb_flush(env, 1);
1445 env->cp15.c13_fcse = val;
b5ff1b31
FB
1446 break;
1447 case 1:
d07edbfa 1448 /* This changes the ASID, so do a TLB flush. */
ce819861
PB
1449 if (env->cp15.c13_context != val
1450 && !arm_feature(env, ARM_FEATURE_MPU))
d07edbfa
PB
1451 tlb_flush(env, 0);
1452 env->cp15.c13_context = val;
b5ff1b31 1453 break;
9ee6e8bb
PB
1454 case 2:
1455 env->cp15.c13_tls1 = val;
1456 break;
1457 case 3:
1458 env->cp15.c13_tls2 = val;
1459 break;
1460 case 4:
1461 env->cp15.c13_tls3 = val;
1462 break;
b5ff1b31
FB
1463 default:
1464 goto bad_reg;
1465 }
1466 break;
1467 case 14: /* Reserved. */
1468 goto bad_reg;
1469 case 15: /* Implementation specific. */
c1713132 1470 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
ce819861 1471 if (op2 == 0 && crm == 1) {
2e23213f
AZ
1472 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1473 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1474 tb_flush(env);
1475 env->cp15.c15_cpar = val & 0x3fff;
1476 }
c1713132
AZ
1477 break;
1478 }
1479 goto bad_reg;
1480 }
c3d2689d
AZ
1481 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1482 switch (crm) {
1483 case 0:
1484 break;
1485 case 1: /* Set TI925T configuration. */
1486 env->cp15.c15_ticonfig = val & 0xe7;
1487 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1488 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1489 break;
1490 case 2: /* Set I_max. */
1491 env->cp15.c15_i_max = val;
1492 break;
1493 case 3: /* Set I_min. */
1494 env->cp15.c15_i_min = val;
1495 break;
1496 case 4: /* Set thread-ID. */
1497 env->cp15.c15_threadid = val & 0xffff;
1498 break;
1499 case 8: /* Wait-for-interrupt (deprecated). */
1500 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1501 break;
1502 default:
1503 goto bad_reg;
1504 }
1505 }
b5ff1b31
FB
1506 break;
1507 }
1508 return;
1509bad_reg:
1510 /* ??? For debugging only. Should raise illegal instruction exception. */
9ee6e8bb
PB
1511 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1512 (insn >> 16) & 0xf, crm, op1, op2);
b5ff1b31
FB
1513}
1514
8984bd2e 1515uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
b5ff1b31 1516{
9ee6e8bb
PB
1517 int op1;
1518 int op2;
1519 int crm;
b5ff1b31 1520
9ee6e8bb 1521 op1 = (insn >> 21) & 7;
b5ff1b31 1522 op2 = (insn >> 5) & 7;
c3d2689d 1523 crm = insn & 0xf;
b5ff1b31
FB
1524 switch ((insn >> 16) & 0xf) {
1525 case 0: /* ID codes. */
9ee6e8bb
PB
1526 switch (op1) {
1527 case 0:
1528 switch (crm) {
1529 case 0:
1530 switch (op2) {
1531 case 0: /* Device ID. */
1532 return env->cp15.c0_cpuid;
1533 case 1: /* Cache Type. */
1534 return env->cp15.c0_cachetype;
1535 case 2: /* TCM status. */
1536 return 0;
1537 case 3: /* TLB type register. */
1538 return 0; /* No lockable TLB entries. */
1539 case 5: /* CPU ID */
1540 return env->cpu_index;
1541 default:
1542 goto bad_reg;
1543 }
1544 case 1:
1545 if (!arm_feature(env, ARM_FEATURE_V6))
1546 goto bad_reg;
1547 return env->cp15.c0_c1[op2];
1548 case 2:
1549 if (!arm_feature(env, ARM_FEATURE_V6))
1550 goto bad_reg;
1551 return env->cp15.c0_c2[op2];
1552 case 3: case 4: case 5: case 6: case 7:
1553 return 0;
1554 default:
1555 goto bad_reg;
1556 }
1557 case 1:
1558 /* These registers aren't documented on arm11 cores. However
1559 Linux looks at them anyway. */
1560 if (!arm_feature(env, ARM_FEATURE_V6))
1561 goto bad_reg;
1562 if (crm != 0)
1563 goto bad_reg;
610c3c8a
AZ
1564 if (arm_feature(env, ARM_FEATURE_XSCALE))
1565 goto bad_reg;
b5ff1b31 1566 return 0;
9ee6e8bb
PB
1567 default:
1568 goto bad_reg;
b5ff1b31
FB
1569 }
1570 case 1: /* System configuration. */
c3d2689d
AZ
1571 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1572 op2 = 0;
b5ff1b31
FB
1573 switch (op2) {
1574 case 0: /* Control register. */
1575 return env->cp15.c1_sys;
1576 case 1: /* Auxiliary control register. */
c1713132 1577 if (arm_feature(env, ARM_FEATURE_XSCALE))
610c3c8a 1578 return env->cp15.c1_xscaleauxcr;
9ee6e8bb
PB
1579 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1580 goto bad_reg;
1581 switch (ARM_CPUID(env)) {
1582 case ARM_CPUID_ARM1026:
1583 return 1;
1584 case ARM_CPUID_ARM1136:
1585 return 7;
1586 case ARM_CPUID_ARM11MPCORE:
1587 return 1;
1588 case ARM_CPUID_CORTEXA8:
1589 return 0;
1590 default:
1591 goto bad_reg;
1592 }
b5ff1b31 1593 case 2: /* Coprocessor access register. */
610c3c8a
AZ
1594 if (arm_feature(env, ARM_FEATURE_XSCALE))
1595 goto bad_reg;
b5ff1b31
FB
1596 return env->cp15.c1_coproc;
1597 default:
1598 goto bad_reg;
1599 }
ce819861
PB
1600 case 2: /* MMU Page table control / MPU cache control. */
1601 if (arm_feature(env, ARM_FEATURE_MPU)) {
1602 switch (op2) {
1603 case 0:
1604 return env->cp15.c2_data;
1605 break;
1606 case 1:
1607 return env->cp15.c2_insn;
1608 break;
1609 default:
1610 goto bad_reg;
1611 }
1612 } else {
9ee6e8bb
PB
1613 switch (op2) {
1614 case 0:
1615 return env->cp15.c2_base0;
1616 case 1:
1617 return env->cp15.c2_base1;
1618 case 2:
1619 {
1620 int n;
1621 uint32_t mask;
1622 n = 0;
1623 mask = env->cp15.c2_mask;
1624 while (mask) {
1625 n++;
1626 mask <<= 1;
1627 }
1628 return n;
1629 }
1630 default:
1631 goto bad_reg;
1632 }
1633 }
ce819861 1634 case 3: /* MMU Domain access control / MPU write buffer control. */
b5ff1b31
FB
1635 return env->cp15.c3;
1636 case 4: /* Reserved. */
1637 goto bad_reg;
ce819861 1638 case 5: /* MMU Fault status / MPU access permission. */
c3d2689d
AZ
1639 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1640 op2 = 0;
b5ff1b31
FB
1641 switch (op2) {
1642 case 0:
ce819861
PB
1643 if (arm_feature(env, ARM_FEATURE_MPU))
1644 return simple_mpu_ap_bits(env->cp15.c5_data);
b5ff1b31
FB
1645 return env->cp15.c5_data;
1646 case 1:
ce819861
PB
1647 if (arm_feature(env, ARM_FEATURE_MPU))
1648 return simple_mpu_ap_bits(env->cp15.c5_data);
1649 return env->cp15.c5_insn;
1650 case 2:
1651 if (!arm_feature(env, ARM_FEATURE_MPU))
1652 goto bad_reg;
1653 return env->cp15.c5_data;
1654 case 3:
1655 if (!arm_feature(env, ARM_FEATURE_MPU))
1656 goto bad_reg;
b5ff1b31
FB
1657 return env->cp15.c5_insn;
1658 default:
1659 goto bad_reg;
1660 }
9ee6e8bb 1661 case 6: /* MMU Fault address. */
ce819861 1662 if (arm_feature(env, ARM_FEATURE_MPU)) {
9ee6e8bb 1663 if (crm >= 8)
ce819861 1664 goto bad_reg;
9ee6e8bb 1665 return env->cp15.c6_region[crm];
ce819861 1666 } else {
c3d2689d
AZ
1667 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1668 op2 = 0;
9ee6e8bb
PB
1669 switch (op2) {
1670 case 0:
1671 return env->cp15.c6_data;
1672 case 1:
1673 if (arm_feature(env, ARM_FEATURE_V6)) {
1674 /* Watchpoint Fault Adrress. */
1675 return 0; /* Not implemented. */
1676 } else {
1677 /* Instruction Fault Adrress. */
1678 /* Arm9 doesn't have an IFAR, but implementing it anyway
1679 shouldn't do any harm. */
1680 return env->cp15.c6_insn;
1681 }
1682 case 2:
1683 if (arm_feature(env, ARM_FEATURE_V6)) {
1684 /* Instruction Fault Adrress. */
1685 return env->cp15.c6_insn;
1686 } else {
1687 goto bad_reg;
1688 }
1689 default:
1690 goto bad_reg;
1691 }
b5ff1b31
FB
1692 }
1693 case 7: /* Cache control. */
6fbe23d5
PB
1694 /* FIXME: Should only clear Z flag if destination is r15. */
1695 env->ZF = 0;
b5ff1b31
FB
1696 return 0;
1697 case 8: /* MMU TLB control. */
1698 goto bad_reg;
1699 case 9: /* Cache lockdown. */
9ee6e8bb
PB
1700 switch (op1) {
1701 case 0: /* L1 cache. */
1702 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1703 return 0;
1704 switch (op2) {
1705 case 0:
1706 return env->cp15.c9_data;
1707 case 1:
1708 return env->cp15.c9_insn;
1709 default:
1710 goto bad_reg;
1711 }
1712 case 1: /* L2 cache */
1713 if (crm != 0)
1714 goto bad_reg;
1715 /* L2 Lockdown and Auxiliary control. */
c3d2689d 1716 return 0;
b5ff1b31
FB
1717 default:
1718 goto bad_reg;
1719 }
1720 case 10: /* MMU TLB lockdown. */
1721 /* ??? TLB lockdown not implemented. */
1722 return 0;
1723 case 11: /* TCM DMA control. */
1724 case 12: /* Reserved. */
1725 goto bad_reg;
1726 case 13: /* Process ID. */
1727 switch (op2) {
1728 case 0:
1729 return env->cp15.c13_fcse;
1730 case 1:
1731 return env->cp15.c13_context;
9ee6e8bb
PB
1732 case 2:
1733 return env->cp15.c13_tls1;
1734 case 3:
1735 return env->cp15.c13_tls2;
1736 case 4:
1737 return env->cp15.c13_tls3;
b5ff1b31
FB
1738 default:
1739 goto bad_reg;
1740 }
1741 case 14: /* Reserved. */
1742 goto bad_reg;
1743 case 15: /* Implementation specific. */
c1713132 1744 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
c3d2689d 1745 if (op2 == 0 && crm == 1)
c1713132
AZ
1746 return env->cp15.c15_cpar;
1747
1748 goto bad_reg;
1749 }
c3d2689d
AZ
1750 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1751 switch (crm) {
1752 case 0:
1753 return 0;
1754 case 1: /* Read TI925T configuration. */
1755 return env->cp15.c15_ticonfig;
1756 case 2: /* Read I_max. */
1757 return env->cp15.c15_i_max;
1758 case 3: /* Read I_min. */
1759 return env->cp15.c15_i_min;
1760 case 4: /* Read thread-ID. */
1761 return env->cp15.c15_threadid;
1762 case 8: /* TI925T_status */
1763 return 0;
1764 }
1765 goto bad_reg;
1766 }
b5ff1b31
FB
1767 return 0;
1768 }
1769bad_reg:
1770 /* ??? For debugging only. Should raise illegal instruction exception. */
9ee6e8bb
PB
1771 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1772 (insn >> 16) & 0xf, crm, op1, op2);
b5ff1b31
FB
1773 return 0;
1774}
1775
b0109805 1776void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
9ee6e8bb
PB
1777{
1778 env->banked_r13[bank_number(mode)] = val;
1779}
1780
b0109805 1781uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
9ee6e8bb
PB
1782{
1783 return env->banked_r13[bank_number(mode)];
1784}
1785
8984bd2e 1786uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
9ee6e8bb
PB
1787{
1788 switch (reg) {
1789 case 0: /* APSR */
1790 return xpsr_read(env) & 0xf8000000;
1791 case 1: /* IAPSR */
1792 return xpsr_read(env) & 0xf80001ff;
1793 case 2: /* EAPSR */
1794 return xpsr_read(env) & 0xff00fc00;
1795 case 3: /* xPSR */
1796 return xpsr_read(env) & 0xff00fdff;
1797 case 5: /* IPSR */
1798 return xpsr_read(env) & 0x000001ff;
1799 case 6: /* EPSR */
1800 return xpsr_read(env) & 0x0700fc00;
1801 case 7: /* IEPSR */
1802 return xpsr_read(env) & 0x0700edff;
1803 case 8: /* MSP */
1804 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
1805 case 9: /* PSP */
1806 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
1807 case 16: /* PRIMASK */
1808 return (env->uncached_cpsr & CPSR_I) != 0;
1809 case 17: /* FAULTMASK */
1810 return (env->uncached_cpsr & CPSR_F) != 0;
1811 case 18: /* BASEPRI */
1812 case 19: /* BASEPRI_MAX */
1813 return env->v7m.basepri;
1814 case 20: /* CONTROL */
1815 return env->v7m.control;
1816 default:
1817 /* ??? For debugging only. */
1818 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
1819 return 0;
1820 }
1821}
1822
8984bd2e 1823void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
9ee6e8bb
PB
1824{
1825 switch (reg) {
1826 case 0: /* APSR */
1827 xpsr_write(env, val, 0xf8000000);
1828 break;
1829 case 1: /* IAPSR */
1830 xpsr_write(env, val, 0xf8000000);
1831 break;
1832 case 2: /* EAPSR */
1833 xpsr_write(env, val, 0xfe00fc00);
1834 break;
1835 case 3: /* xPSR */
1836 xpsr_write(env, val, 0xfe00fc00);
1837 break;
1838 case 5: /* IPSR */
1839 /* IPSR bits are readonly. */
1840 break;
1841 case 6: /* EPSR */
1842 xpsr_write(env, val, 0x0600fc00);
1843 break;
1844 case 7: /* IEPSR */
1845 xpsr_write(env, val, 0x0600fc00);
1846 break;
1847 case 8: /* MSP */
1848 if (env->v7m.current_sp)
1849 env->v7m.other_sp = val;
1850 else
1851 env->regs[13] = val;
1852 break;
1853 case 9: /* PSP */
1854 if (env->v7m.current_sp)
1855 env->regs[13] = val;
1856 else
1857 env->v7m.other_sp = val;
1858 break;
1859 case 16: /* PRIMASK */
1860 if (val & 1)
1861 env->uncached_cpsr |= CPSR_I;
1862 else
1863 env->uncached_cpsr &= ~CPSR_I;
1864 break;
1865 case 17: /* FAULTMASK */
1866 if (val & 1)
1867 env->uncached_cpsr |= CPSR_F;
1868 else
1869 env->uncached_cpsr &= ~CPSR_F;
1870 break;
1871 case 18: /* BASEPRI */
1872 env->v7m.basepri = val & 0xff;
1873 break;
1874 case 19: /* BASEPRI_MAX */
1875 val &= 0xff;
1876 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
1877 env->v7m.basepri = val;
1878 break;
1879 case 20: /* CONTROL */
1880 env->v7m.control = val & 3;
1881 switch_v7m_sp(env, (val & 2) != 0);
1882 break;
1883 default:
1884 /* ??? For debugging only. */
1885 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
1886 return;
1887 }
1888}
1889
c1713132
AZ
1890void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
1891 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
1892 void *opaque)
1893{
1894 if (cpnum < 0 || cpnum > 14) {
1895 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
1896 return;
1897 }
1898
1899 env->cp[cpnum].cp_read = cp_read;
1900 env->cp[cpnum].cp_write = cp_write;
1901 env->cp[cpnum].opaque = opaque;
1902}
1903
b5ff1b31 1904#endif
6ddbc6e4
PB
1905
1906/* Note that signed overflow is undefined in C. The following routines are
1907 careful to use unsigned types where modulo arithmetic is required.
1908 Failure to do so _will_ break on newer gcc. */
1909
1910/* Signed saturating arithmetic. */
1911
1654b2d6 1912/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
1913static inline uint16_t add16_sat(uint16_t a, uint16_t b)
1914{
1915 uint16_t res;
1916
1917 res = a + b;
1918 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
1919 if (a & 0x8000)
1920 res = 0x8000;
1921 else
1922 res = 0x7fff;
1923 }
1924 return res;
1925}
1926
1654b2d6 1927/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
1928static inline uint8_t add8_sat(uint8_t a, uint8_t b)
1929{
1930 uint8_t res;
1931
1932 res = a + b;
1933 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
1934 if (a & 0x80)
1935 res = 0x80;
1936 else
1937 res = 0x7f;
1938 }
1939 return res;
1940}
1941
1654b2d6 1942/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
1943static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
1944{
1945 uint16_t res;
1946
1947 res = a - b;
1948 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
1949 if (a & 0x8000)
1950 res = 0x8000;
1951 else
1952 res = 0x7fff;
1953 }
1954 return res;
1955}
1956
1654b2d6 1957/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
1958static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
1959{
1960 uint8_t res;
1961
1962 res = a - b;
1963 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
1964 if (a & 0x80)
1965 res = 0x80;
1966 else
1967 res = 0x7f;
1968 }
1969 return res;
1970}
1971
1972#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
1973#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
1974#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
1975#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
1976#define PFX q
1977
1978#include "op_addsub.h"
1979
1980/* Unsigned saturating arithmetic. */
1981static inline uint16_t add16_usat(uint16_t a, uint8_t b)
1982{
1983 uint16_t res;
1984 res = a + b;
1985 if (res < a)
1986 res = 0xffff;
1987 return res;
1988}
1989
1990static inline uint16_t sub16_usat(uint16_t a, uint8_t b)
1991{
1992 if (a < b)
1993 return a - b;
1994 else
1995 return 0;
1996}
1997
1998static inline uint8_t add8_usat(uint8_t a, uint8_t b)
1999{
2000 uint8_t res;
2001 res = a + b;
2002 if (res < a)
2003 res = 0xff;
2004 return res;
2005}
2006
2007static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2008{
2009 if (a < b)
2010 return a - b;
2011 else
2012 return 0;
2013}
2014
2015#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2016#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2017#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2018#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2019#define PFX uq
2020
2021#include "op_addsub.h"
2022
2023/* Signed modulo arithmetic. */
2024#define SARITH16(a, b, n, op) do { \
2025 int32_t sum; \
2026 sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2027 RESULT(sum, n, 16); \
2028 if (sum >= 0) \
2029 ge |= 3 << (n * 2); \
2030 } while(0)
2031
2032#define SARITH8(a, b, n, op) do { \
2033 int32_t sum; \
2034 sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2035 RESULT(sum, n, 8); \
2036 if (sum >= 0) \
2037 ge |= 1 << n; \
2038 } while(0)
2039
2040
2041#define ADD16(a, b, n) SARITH16(a, b, n, +)
2042#define SUB16(a, b, n) SARITH16(a, b, n, -)
2043#define ADD8(a, b, n) SARITH8(a, b, n, +)
2044#define SUB8(a, b, n) SARITH8(a, b, n, -)
2045#define PFX s
2046#define ARITH_GE
2047
2048#include "op_addsub.h"
2049
2050/* Unsigned modulo arithmetic. */
2051#define ADD16(a, b, n) do { \
2052 uint32_t sum; \
2053 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2054 RESULT(sum, n, 16); \
2055 if ((sum >> 16) == 0) \
2056 ge |= 3 << (n * 2); \
2057 } while(0)
2058
2059#define ADD8(a, b, n) do { \
2060 uint32_t sum; \
2061 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2062 RESULT(sum, n, 8); \
2063 if ((sum >> 8) == 0) \
2064 ge |= 3 << (n * 2); \
2065 } while(0)
2066
2067#define SUB16(a, b, n) do { \
2068 uint32_t sum; \
2069 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2070 RESULT(sum, n, 16); \
2071 if ((sum >> 16) == 0) \
2072 ge |= 3 << (n * 2); \
2073 } while(0)
2074
2075#define SUB8(a, b, n) do { \
2076 uint32_t sum; \
2077 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2078 RESULT(sum, n, 8); \
2079 if ((sum >> 8) == 0) \
2080 ge |= 3 << (n * 2); \
2081 } while(0)
2082
2083#define PFX u
2084#define ARITH_GE
2085
2086#include "op_addsub.h"
2087
2088/* Halved signed arithmetic. */
2089#define ADD16(a, b, n) \
2090 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2091#define SUB16(a, b, n) \
2092 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2093#define ADD8(a, b, n) \
2094 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2095#define SUB8(a, b, n) \
2096 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2097#define PFX sh
2098
2099#include "op_addsub.h"
2100
2101/* Halved unsigned arithmetic. */
2102#define ADD16(a, b, n) \
2103 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2104#define SUB16(a, b, n) \
2105 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2106#define ADD8(a, b, n) \
2107 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2108#define SUB8(a, b, n) \
2109 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2110#define PFX uh
2111
2112#include "op_addsub.h"
2113
2114static inline uint8_t do_usad(uint8_t a, uint8_t b)
2115{
2116 if (a > b)
2117 return a - b;
2118 else
2119 return b - a;
2120}
2121
2122/* Unsigned sum of absolute byte differences. */
2123uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2124{
2125 uint32_t sum;
2126 sum = do_usad(a, b);
2127 sum += do_usad(a >> 8, b >> 8);
2128 sum += do_usad(a >> 16, b >>16);
2129 sum += do_usad(a >> 24, b >> 24);
2130 return sum;
2131}
2132
2133/* For ARMv6 SEL instruction. */
2134uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2135{
2136 uint32_t mask;
2137
2138 mask = 0;
2139 if (flags & 1)
2140 mask |= 0xff;
2141 if (flags & 2)
2142 mask |= 0xff00;
2143 if (flags & 4)
2144 mask |= 0xff0000;
2145 if (flags & 8)
2146 mask |= 0xff000000;
2147 return (a & mask) | (b & ~mask);
2148}
2149
5e3f878a
PB
2150uint32_t HELPER(logicq_cc)(uint64_t val)
2151{
2152 return (val >> 32) | (val != 0);
2153}
4373f3ce
PB
2154
2155/* VFP support. We follow the convention used for VFP instrunctions:
2156 Single precition routines have a "s" suffix, double precision a
2157 "d" suffix. */
2158
2159/* Convert host exception flags to vfp form. */
2160static inline int vfp_exceptbits_from_host(int host_bits)
2161{
2162 int target_bits = 0;
2163
2164 if (host_bits & float_flag_invalid)
2165 target_bits |= 1;
2166 if (host_bits & float_flag_divbyzero)
2167 target_bits |= 2;
2168 if (host_bits & float_flag_overflow)
2169 target_bits |= 4;
2170 if (host_bits & float_flag_underflow)
2171 target_bits |= 8;
2172 if (host_bits & float_flag_inexact)
2173 target_bits |= 0x10;
2174 return target_bits;
2175}
2176
2177uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2178{
2179 int i;
2180 uint32_t fpscr;
2181
2182 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2183 | (env->vfp.vec_len << 16)
2184 | (env->vfp.vec_stride << 20);
2185 i = get_float_exception_flags(&env->vfp.fp_status);
2186 fpscr |= vfp_exceptbits_from_host(i);
2187 return fpscr;
2188}
2189
2190/* Convert vfp exception flags to target form. */
2191static inline int vfp_exceptbits_to_host(int target_bits)
2192{
2193 int host_bits = 0;
2194
2195 if (target_bits & 1)
2196 host_bits |= float_flag_invalid;
2197 if (target_bits & 2)
2198 host_bits |= float_flag_divbyzero;
2199 if (target_bits & 4)
2200 host_bits |= float_flag_overflow;
2201 if (target_bits & 8)
2202 host_bits |= float_flag_underflow;
2203 if (target_bits & 0x10)
2204 host_bits |= float_flag_inexact;
2205 return host_bits;
2206}
2207
2208void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2209{
2210 int i;
2211 uint32_t changed;
2212
2213 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2214 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2215 env->vfp.vec_len = (val >> 16) & 7;
2216 env->vfp.vec_stride = (val >> 20) & 3;
2217
2218 changed ^= val;
2219 if (changed & (3 << 22)) {
2220 i = (val >> 22) & 3;
2221 switch (i) {
2222 case 0:
2223 i = float_round_nearest_even;
2224 break;
2225 case 1:
2226 i = float_round_up;
2227 break;
2228 case 2:
2229 i = float_round_down;
2230 break;
2231 case 3:
2232 i = float_round_to_zero;
2233 break;
2234 }
2235 set_float_rounding_mode(i, &env->vfp.fp_status);
2236 }
2237
2238 i = vfp_exceptbits_to_host((val >> 8) & 0x1f);
2239 set_float_exception_flags(i, &env->vfp.fp_status);
2240 /* XXX: FZ and DN are not implemented. */
2241}
2242
2243#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2244
2245#define VFP_BINOP(name) \
2246float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2247{ \
2248 return float32_ ## name (a, b, &env->vfp.fp_status); \
2249} \
2250float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2251{ \
2252 return float64_ ## name (a, b, &env->vfp.fp_status); \
2253}
2254VFP_BINOP(add)
2255VFP_BINOP(sub)
2256VFP_BINOP(mul)
2257VFP_BINOP(div)
2258#undef VFP_BINOP
2259
2260float32 VFP_HELPER(neg, s)(float32 a)
2261{
2262 return float32_chs(a);
2263}
2264
2265float64 VFP_HELPER(neg, d)(float64 a)
2266{
2267 return float32_chs(a);
2268}
2269
2270float32 VFP_HELPER(abs, s)(float32 a)
2271{
2272 return float32_abs(a);
2273}
2274
2275float64 VFP_HELPER(abs, d)(float64 a)
2276{
2277 return float32_abs(a);
2278}
2279
2280float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2281{
2282 return float32_sqrt(a, &env->vfp.fp_status);
2283}
2284
2285float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2286{
2287 return float64_sqrt(a, &env->vfp.fp_status);
2288}
2289
2290/* XXX: check quiet/signaling case */
2291#define DO_VFP_cmp(p, type) \
2292void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2293{ \
2294 uint32_t flags; \
2295 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2296 case 0: flags = 0x6; break; \
2297 case -1: flags = 0x8; break; \
2298 case 1: flags = 0x2; break; \
2299 default: case 2: flags = 0x3; break; \
2300 } \
2301 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2302 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2303} \
2304void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2305{ \
2306 uint32_t flags; \
2307 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2308 case 0: flags = 0x6; break; \
2309 case -1: flags = 0x8; break; \
2310 case 1: flags = 0x2; break; \
2311 default: case 2: flags = 0x3; break; \
2312 } \
2313 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2314 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2315}
2316DO_VFP_cmp(s, float32)
2317DO_VFP_cmp(d, float64)
2318#undef DO_VFP_cmp
2319
2320/* Helper routines to perform bitwise copies between float and int. */
2321static inline float32 vfp_itos(uint32_t i)
2322{
2323 union {
2324 uint32_t i;
2325 float32 s;
2326 } v;
2327
2328 v.i = i;
2329 return v.s;
2330}
2331
2332static inline uint32_t vfp_stoi(float32 s)
2333{
2334 union {
2335 uint32_t i;
2336 float32 s;
2337 } v;
2338
2339 v.s = s;
2340 return v.i;
2341}
2342
2343static inline float64 vfp_itod(uint64_t i)
2344{
2345 union {
2346 uint64_t i;
2347 float64 d;
2348 } v;
2349
2350 v.i = i;
2351 return v.d;
2352}
2353
2354static inline uint64_t vfp_dtoi(float64 d)
2355{
2356 union {
2357 uint64_t i;
2358 float64 d;
2359 } v;
2360
2361 v.d = d;
2362 return v.i;
2363}
2364
2365/* Integer to float conversion. */
2366float32 VFP_HELPER(uito, s)(float32 x, CPUState *env)
2367{
2368 return uint32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2369}
2370
2371float64 VFP_HELPER(uito, d)(float32 x, CPUState *env)
2372{
2373 return uint32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2374}
2375
2376float32 VFP_HELPER(sito, s)(float32 x, CPUState *env)
2377{
2378 return int32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2379}
2380
2381float64 VFP_HELPER(sito, d)(float32 x, CPUState *env)
2382{
2383 return int32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2384}
2385
2386/* Float to integer conversion. */
2387float32 VFP_HELPER(toui, s)(float32 x, CPUState *env)
2388{
2389 return vfp_itos(float32_to_uint32(x, &env->vfp.fp_status));
2390}
2391
2392float32 VFP_HELPER(toui, d)(float64 x, CPUState *env)
2393{
2394 return vfp_itos(float64_to_uint32(x, &env->vfp.fp_status));
2395}
2396
2397float32 VFP_HELPER(tosi, s)(float32 x, CPUState *env)
2398{
2399 return vfp_itos(float32_to_int32(x, &env->vfp.fp_status));
2400}
2401
2402float32 VFP_HELPER(tosi, d)(float64 x, CPUState *env)
2403{
2404 return vfp_itos(float64_to_int32(x, &env->vfp.fp_status));
2405}
2406
2407float32 VFP_HELPER(touiz, s)(float32 x, CPUState *env)
2408{
2409 return vfp_itos(float32_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2410}
2411
2412float32 VFP_HELPER(touiz, d)(float64 x, CPUState *env)
2413{
2414 return vfp_itos(float64_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2415}
2416
2417float32 VFP_HELPER(tosiz, s)(float32 x, CPUState *env)
2418{
2419 return vfp_itos(float32_to_int32_round_to_zero(x, &env->vfp.fp_status));
2420}
2421
2422float32 VFP_HELPER(tosiz, d)(float64 x, CPUState *env)
2423{
2424 return vfp_itos(float64_to_int32_round_to_zero(x, &env->vfp.fp_status));
2425}
2426
2427/* floating point conversion */
2428float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2429{
2430 return float32_to_float64(x, &env->vfp.fp_status);
2431}
2432
2433float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2434{
2435 return float64_to_float32(x, &env->vfp.fp_status);
2436}
2437
2438/* VFP3 fixed point conversion. */
2439#define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2440ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2441{ \
2442 ftype tmp; \
2443 tmp = sign##int32_to_##ftype ((itype)vfp_##p##toi(x), \
2444 &env->vfp.fp_status); \
2445 return ftype##_scalbn(tmp, shift, &env->vfp.fp_status); \
2446} \
2447ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2448{ \
2449 ftype tmp; \
2450 tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2451 return vfp_ito##p((itype)ftype##_to_##sign##int32_round_to_zero(tmp, \
2452 &env->vfp.fp_status)); \
2453}
2454
2455VFP_CONV_FIX(sh, d, float64, int16, )
2456VFP_CONV_FIX(sl, d, float64, int32, )
2457VFP_CONV_FIX(uh, d, float64, uint16, u)
2458VFP_CONV_FIX(ul, d, float64, uint32, u)
2459VFP_CONV_FIX(sh, s, float32, int16, )
2460VFP_CONV_FIX(sl, s, float32, int32, )
2461VFP_CONV_FIX(uh, s, float32, uint16, u)
2462VFP_CONV_FIX(ul, s, float32, uint32, u)
2463#undef VFP_CONV_FIX
2464
2465float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2466{
2467 float_status *s = &env->vfp.fp_status;
2468 float32 two = int32_to_float32(2, s);
2469 return float32_sub(two, float32_mul(a, b, s), s);
2470}
2471
2472float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2473{
2474 float_status *s = &env->vfp.fp_status;
2475 float32 three = int32_to_float32(3, s);
2476 return float32_sub(three, float32_mul(a, b, s), s);
2477}
2478
8f8e3aa4
PB
2479/* NEON helpers. */
2480
4373f3ce
PB
2481/* TODO: The architecture specifies the value that the estimate functions
2482 should return. We return the exact reciprocal/root instead. */
2483float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2484{
2485 float_status *s = &env->vfp.fp_status;
2486 float32 one = int32_to_float32(1, s);
2487 return float32_div(one, a, s);
2488}
2489
2490float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
2491{
2492 float_status *s = &env->vfp.fp_status;
2493 float32 one = int32_to_float32(1, s);
2494 return float32_div(one, float32_sqrt(a, s), s);
2495}
2496
2497uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
2498{
2499 float_status *s = &env->vfp.fp_status;
2500 float32 tmp;
2501 tmp = int32_to_float32(a, s);
2502 tmp = float32_scalbn(tmp, -32, s);
2503 tmp = helper_recpe_f32(tmp, env);
2504 tmp = float32_scalbn(tmp, 31, s);
2505 return float32_to_int32(tmp, s);
2506}
2507
2508uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
2509{
2510 float_status *s = &env->vfp.fp_status;
2511 float32 tmp;
2512 tmp = int32_to_float32(a, s);
2513 tmp = float32_scalbn(tmp, -32, s);
2514 tmp = helper_rsqrte_f32(tmp, env);
2515 tmp = float32_scalbn(tmp, 31, s);
2516 return float32_to_int32(tmp, s);
2517}