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