1 /*
2 * qemu user cpu loop
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "qemu.h"
22 #include "user-internals.h"
23 #include "elf.h"
24 #include "user/cpu_loop.h"
25 #include "signal-common.h"
26 #include "semihosting/common-semi.h"
27 #include "exec/page-protection.h"
28 #include "exec/mmap-lock.h"
29 #include "user/page-protection.h"
30 #include "target/arm/syndrome.h"
31
32 #define get_user_code_u32(x, gaddr, env) \
33 ({ abi_long __r = get_user_u32((x), (gaddr)); \
34 if (!__r && bswap_code(arm_sctlr_b(env))) { \
35 (x) = bswap32(x); \
36 } \
37 __r; \
38 })
39
40 /*
41 * Note that if we need to do data accesses here, they should do a
42 * bswap if arm_cpu_bswap_data() returns true.
43 */
44
45 /*
46 * Similar to code in accel/tcg/user-exec.c, but outside the execution loop.
47 * Must be called with mmap_lock.
48 * We get the PC of the entry address - which is as good as anything,
49 * on a real kernel what you get depends on which mode it uses.
50 */
atomic_mmu_lookup(CPUArchState * env,uint32_t addr,int size)51 static void *atomic_mmu_lookup(CPUArchState *env, uint32_t addr, int size)
52 {
53 int need_flags = PAGE_READ | PAGE_WRITE_ORG | PAGE_VALID;
54 int page_flags;
55
56 /* Enforce guest required alignment. */
57 if (unlikely(addr & (size - 1))) {
58 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
59 return NULL;
60 }
61
62 page_flags = page_get_flags(addr);
63 if (unlikely((page_flags & need_flags) != need_flags)) {
64 force_sig_fault(TARGET_SIGSEGV,
65 page_flags & PAGE_VALID ?
66 TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR, addr);
67 return NULL;
68 }
69
70 return g2h(env_cpu(env), addr);
71 }
72
73 /*
74 * See the Linux kernel's Documentation/arm/kernel_user_helpers.rst
75 * Input:
76 * r0 = oldval
77 * r1 = newval
78 * r2 = pointer to target value
79 *
80 * Output:
81 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
82 * C set if *ptr was changed, clear if no exchange happened
83 */
arm_kernel_cmpxchg32_helper(CPUARMState * env)84 static void arm_kernel_cmpxchg32_helper(CPUARMState *env)
85 {
86 uint32_t oldval, newval, val, addr, cpsr, *host_addr;
87
88 /* Swap if host != guest endianness, for the host cmpxchg below */
89 oldval = tswap32(env->regs[0]);
90 newval = tswap32(env->regs[1]);
91 addr = env->regs[2];
92
93 mmap_lock();
94 host_addr = atomic_mmu_lookup(env, addr, 4);
95 if (!host_addr) {
96 mmap_unlock();
97 return;
98 }
99
100 val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
101 mmap_unlock();
102
103 cpsr = (val == oldval) * CPSR_C;
104 cpsr_write(env, cpsr, CPSR_C, CPSRWriteByInstr);
105 env->regs[0] = cpsr ? 0 : -1;
106 }
107
108 /*
109 * See the Linux kernel's Documentation/arm/kernel_user_helpers.rst
110 * Input:
111 * r0 = pointer to oldval
112 * r1 = pointer to newval
113 * r2 = pointer to target value
114 *
115 * Output:
116 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
117 * C set if *ptr was changed, clear if no exchange happened
118 *
119 * Note segv's in kernel helpers are a bit tricky, we can set the
120 * data address sensibly but the PC address is just the entry point.
121 */
arm_kernel_cmpxchg64_helper(CPUARMState * env)122 static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
123 {
124 uint64_t oldval, newval, val;
125 uint32_t addr, cpsr;
126 uint64_t *host_addr;
127
128 addr = env->regs[0];
129 if (get_user_u64(oldval, addr)) {
130 goto segv;
131 }
132
133 addr = env->regs[1];
134 if (get_user_u64(newval, addr)) {
135 goto segv;
136 }
137
138 mmap_lock();
139 addr = env->regs[2];
140 host_addr = atomic_mmu_lookup(env, addr, 8);
141 if (!host_addr) {
142 mmap_unlock();
143 return;
144 }
145
146 /* Swap if host != guest endianness, for the host cmpxchg below */
147 oldval = tswap64(oldval);
148 newval = tswap64(newval);
149
150 #ifdef CONFIG_ATOMIC64
151 val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
152 cpsr = (val == oldval) * CPSR_C;
153 #else
154 /*
155 * This only works between threads, not between processes, but since
156 * the host has no 64-bit cmpxchg, it is the best that we can do.
157 */
158 start_exclusive();
159 val = *host_addr;
160 if (val == oldval) {
161 *host_addr = newval;
162 cpsr = CPSR_C;
163 } else {
164 cpsr = 0;
165 }
166 end_exclusive();
167 #endif
168 mmap_unlock();
169
170 cpsr_write(env, cpsr, CPSR_C, CPSRWriteByInstr);
171 env->regs[0] = cpsr ? 0 : -1;
172 return;
173
174 segv:
175 force_sig_fault(TARGET_SIGSEGV,
176 page_get_flags(addr) & PAGE_VALID ?
177 TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR, addr);
178 }
179
180 /* Handle a jump to the kernel code page. */
181 static int
do_kernel_trap(CPUARMState * env)182 do_kernel_trap(CPUARMState *env)
183 {
184 uint32_t addr;
185
186 switch (env->regs[15]) {
187 case 0xffff0fa0: /* __kernel_memory_barrier */
188 smp_mb();
189 break;
190 case 0xffff0fc0: /* __kernel_cmpxchg */
191 arm_kernel_cmpxchg32_helper(env);
192 break;
193 case 0xffff0fe0: /* __kernel_get_tls */
194 env->regs[0] = cpu_get_tls(env);
195 break;
196 case 0xffff0f60: /* __kernel_cmpxchg64 */
197 arm_kernel_cmpxchg64_helper(env);
198 break;
199
200 default:
201 return 1;
202 }
203 /* Jump back to the caller. */
204 addr = env->regs[14];
205 if (addr & 1) {
206 env->thumb = true;
207 addr &= ~1;
208 }
209 env->regs[15] = addr;
210
211 return 0;
212 }
213
insn_is_linux_bkpt(uint32_t opcode,bool is_thumb)214 static bool insn_is_linux_bkpt(uint32_t opcode, bool is_thumb)
215 {
216 /*
217 * Return true if this insn is one of the three magic UDF insns
218 * which the kernel treats as breakpoint insns.
219 */
220 if (!is_thumb) {
221 return (opcode & 0x0fffffff) == 0x07f001f0;
222 } else {
223 /*
224 * Note that we get the two halves of the 32-bit T32 insn
225 * in the opposite order to the value the kernel uses in
226 * its undef_hook struct.
227 */
228 return ((opcode & 0xffff) == 0xde01) || (opcode == 0xa000f7f0);
229 }
230 }
231
emulate_arm_fpa11(CPUARMState * env,uint32_t opcode)232 static bool emulate_arm_fpa11(CPUARMState *env, uint32_t opcode)
233 {
234 TaskState *ts = get_task_state(env_cpu(env));
235 int rc = EmulateAll(opcode, &ts->fpa, env);
236 int raise, enabled;
237
238 if (rc == 0) {
239 /* Illegal instruction */
240 return false;
241 }
242 if (rc > 0) {
243 /* Everything ok. */
244 env->regs[15] += 4;
245 return true;
246 }
247
248 /* FP exception */
249 rc = -rc;
250 raise = 0;
251
252 /* Translate softfloat flags to FPSR flags */
253 if (rc & float_flag_invalid) {
254 raise |= BIT_IOC;
255 }
256 if (rc & float_flag_divbyzero) {
257 raise |= BIT_DZC;
258 }
259 if (rc & float_flag_overflow) {
260 raise |= BIT_OFC;
261 }
262 if (rc & float_flag_underflow) {
263 raise |= BIT_UFC;
264 }
265 if (rc & float_flag_inexact) {
266 raise |= BIT_IXC;
267 }
268
269 /* Accumulate unenabled exceptions */
270 enabled = ts->fpa.fpsr >> 16;
271 ts->fpa.fpsr |= raise & ~enabled;
272
273 if (raise & enabled) {
274 /*
275 * The kernel's nwfpe emulator does not pass a real si_code.
276 * It merely uses send_sig(SIGFPE, current, 1), which results in
277 * __send_signal() filling out SI_KERNEL with pid and uid 0 (under
278 * the "SEND_SIG_PRIV" case). That's what our force_sig() does.
279 */
280 force_sig(TARGET_SIGFPE);
281 } else {
282 env->regs[15] += 4;
283 }
284 return true;
285 }
286
cpu_loop(CPUARMState * env)287 void cpu_loop(CPUARMState *env)
288 {
289 CPUState *cs = env_cpu(env);
290 int trapnr, si_signo, si_code;
291 unsigned int n, insn;
292 abi_ulong ret;
293
294 for(;;) {
295 cpu_exec_start(cs);
296 trapnr = cpu_exec(cs);
297 cpu_exec_end(cs);
298 process_queued_cpu_work(cs);
299
300 switch(trapnr) {
301 case EXCP_UDEF:
302 case EXCP_NOCP:
303 case EXCP_INVSTATE:
304 {
305 uint32_t opcode;
306
307 /* we handle the FPU emulation here, as Linux */
308 /* we get the opcode */
309 /* FIXME - what to do if get_user() fails? */
310 get_user_code_u32(opcode, env->regs[15], env);
311
312 /*
313 * The Linux kernel treats some UDF patterns specially
314 * to use as breakpoints (instead of the architectural
315 * bkpt insn). These should trigger a SIGTRAP rather
316 * than SIGILL.
317 */
318 if (insn_is_linux_bkpt(opcode, env->thumb)) {
319 goto excp_debug;
320 }
321
322 if (!env->thumb && emulate_arm_fpa11(env, opcode)) {
323 break;
324 }
325
326 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN,
327 env->regs[15]);
328 }
329 break;
330 case EXCP_SWI:
331 {
332 env->eabi = true;
333 /* system call */
334 if (env->thumb) {
335 /* Thumb is always EABI style with syscall number in r7 */
336 n = env->regs[7];
337 } else {
338 /*
339 * Equivalent of kernel CONFIG_OABI_COMPAT: read the
340 * Arm SVC insn to extract the immediate, which is the
341 * syscall number in OABI.
342 */
343 /* FIXME - what to do if get_user() fails? */
344 get_user_code_u32(insn, env->regs[15] - 4, env);
345 n = insn & 0xffffff;
346 if (n == 0) {
347 /* zero immediate: EABI, syscall number in r7 */
348 n = env->regs[7];
349 } else {
350 /*
351 * This XOR matches the kernel code: an immediate
352 * in the valid range (0x900000 .. 0x9fffff) is
353 * converted into the correct EABI-style syscall
354 * number; invalid immediates end up as values
355 * > 0xfffff and are handled below as out-of-range.
356 */
357 n ^= ARM_SYSCALL_BASE;
358 env->eabi = false;
359 }
360 }
361
362 if (n > ARM_NR_BASE) {
363 switch (n) {
364 case ARM_NR_cacheflush:
365 /* nop */
366 env->regs[0] = 0;
367 break;
368 case ARM_NR_set_tls:
369 cpu_set_tls(env, env->regs[0]);
370 env->regs[0] = 0;
371 break;
372 case ARM_NR_breakpoint:
373 env->regs[15] -= env->thumb ? 2 : 4;
374 goto excp_debug;
375 case ARM_NR_get_tls:
376 env->regs[0] = cpu_get_tls(env);
377 break;
378 default:
379 if (n < 0xf0800) {
380 /*
381 * Syscalls 0xf0000..0xf07ff (or 0x9f0000..
382 * 0x9f07ff in OABI numbering) are defined
383 * to return -ENOSYS rather than raising
384 * SIGILL. Note that we have already
385 * removed the 0x900000 prefix.
386 */
387 qemu_log_mask(LOG_UNIMP,
388 "qemu: Unsupported ARM syscall: 0x%x\n",
389 n);
390 env->regs[0] = -TARGET_ENOSYS;
391 } else {
392 /*
393 * Otherwise SIGILL. This includes any SWI with
394 * immediate not originally 0x9fxxxx, because
395 * of the earlier XOR.
396 * Like the real kernel, we report the addr of the
397 * SWI in the siginfo si_addr but leave the PC
398 * pointing at the insn after the SWI.
399 */
400 abi_ulong faultaddr = env->regs[15];
401 faultaddr -= env->thumb ? 2 : 4;
402 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP,
403 faultaddr);
404 }
405 break;
406 }
407 } else {
408 ret = do_syscall(env,
409 n,
410 env->regs[0],
411 env->regs[1],
412 env->regs[2],
413 env->regs[3],
414 env->regs[4],
415 env->regs[5],
416 0, 0);
417 if (ret == -QEMU_ERESTARTSYS) {
418 env->regs[15] -= env->thumb ? 2 : 4;
419 } else if (ret != -QEMU_ESIGRETURN) {
420 env->regs[0] = ret;
421 }
422 }
423 }
424 break;
425 case EXCP_SEMIHOST:
426 do_common_semihosting(cs);
427 env->regs[15] += env->thumb ? 2 : 4;
428 break;
429 case EXCP_INTERRUPT:
430 /* just indicate that signals should be handled asap */
431 break;
432 case EXCP_PREFETCH_ABORT:
433 case EXCP_DATA_ABORT:
434 /* For user-only we don't set TTBCR_EAE, so look at the FSR. */
435 switch (env->exception.fsr & 0x1f) {
436 case 0x1: /* Alignment */
437 si_signo = TARGET_SIGBUS;
438 si_code = TARGET_BUS_ADRALN;
439 break;
440 case 0x3: /* Access flag fault, level 1 */
441 case 0x6: /* Access flag fault, level 2 */
442 case 0x9: /* Domain fault, level 1 */
443 case 0xb: /* Domain fault, level 2 */
444 case 0xd: /* Permission fault, level 1 */
445 case 0xf: /* Permission fault, level 2 */
446 si_signo = TARGET_SIGSEGV;
447 si_code = TARGET_SEGV_ACCERR;
448 break;
449 case 0x5: /* Translation fault, level 1 */
450 case 0x7: /* Translation fault, level 2 */
451 si_signo = TARGET_SIGSEGV;
452 si_code = TARGET_SEGV_MAPERR;
453 break;
454 default:
455 g_assert_not_reached();
456 }
457 force_sig_fault(si_signo, si_code, env->exception.vaddress);
458 break;
459 case EXCP_DEBUG:
460 case EXCP_BKPT:
461 excp_debug:
462 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->regs[15]);
463 break;
464 case EXCP_KERNEL_TRAP:
465 if (do_kernel_trap(env))
466 goto error;
467 break;
468 case EXCP_YIELD:
469 /* nothing to do here for user-mode, just resume guest code */
470 break;
471 case EXCP_ATOMIC:
472 cpu_exec_step_atomic(cs);
473 break;
474 default:
475 error:
476 EXCP_DUMP(env, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
477 abort();
478 }
479 process_pending_signals(env);
480 }
481 }
482
target_cpu_copy_regs(CPUArchState * env,target_pt_regs * regs)483 void target_cpu_copy_regs(CPUArchState *env, target_pt_regs *regs)
484 {
485 CPUState *cpu = env_cpu(env);
486 TaskState *ts = get_task_state(cpu);
487 struct image_info *info = ts->info;
488 int i;
489
490 cpsr_write(env, regs->uregs[16], CPSR_USER | CPSR_EXEC,
491 CPSRWriteByInstr);
492 for(i = 0; i < 16; i++) {
493 env->regs[i] = regs->uregs[i];
494 }
495 #if TARGET_BIG_ENDIAN
496 /* Enable BE8. */
497 if (EF_ARM_EABI_VERSION(info->elf_flags) >= EF_ARM_EABI_VER4
498 && (info->elf_flags & EF_ARM_BE8)) {
499 env->uncached_cpsr |= CPSR_E;
500 env->cp15.sctlr_el[1] |= SCTLR_E0E;
501 } else {
502 env->cp15.sctlr_el[1] |= SCTLR_B;
503 }
504 arm_rebuild_hflags(env);
505 #endif
506
507 ts->stack_base = info->start_stack;
508 ts->heap_base = info->brk;
509 /* This will be filled in on the first SYS_HEAPINFO call. */
510 ts->heap_limit = 0;
511 }
512