blob: ec665862d93e93682b5137d37f2247f68fc9fe81 [file] [log] [blame]
Laurent Viviercd71c082018-04-11 20:56:33 +02001/*
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"
Peter Maydell3b249d22021-09-08 16:44:03 +010022#include "user-internals.h"
Laurent Vivierd9673512018-04-11 20:56:36 +020023#include "elf.h"
Laurent Viviercd71c082018-04-11 20:56:33 +020024#include "cpu_loop-common.h"
Peter Maydell2113aed2021-09-08 16:43:59 +010025#include "signal-common.h"
Philippe Mathieu-Daudé6b5fe132021-03-05 13:54:49 +000026#include "semihosting/common-semi.h"
Philippe Mathieu-Daudé74781c02023-12-06 20:27:32 +010027#include "exec/page-protection.h"
Richard Henderson39a099c2021-07-23 12:22:54 -100028#include "target/arm/syndrome.h"
Laurent Viviercd71c082018-04-11 20:56:33 +020029
Laurent Vivierd9673512018-04-11 20:56:36 +020030#define get_user_code_u32(x, gaddr, env) \
31 ({ abi_long __r = get_user_u32((x), (gaddr)); \
32 if (!__r && bswap_code(arm_sctlr_b(env))) { \
33 (x) = bswap32(x); \
34 } \
35 __r; \
36 })
37
38#define get_user_code_u16(x, gaddr, env) \
39 ({ abi_long __r = get_user_u16((x), (gaddr)); \
40 if (!__r && bswap_code(arm_sctlr_b(env))) { \
41 (x) = bswap16(x); \
42 } \
43 __r; \
44 })
45
46#define get_user_data_u32(x, gaddr, env) \
47 ({ abi_long __r = get_user_u32((x), (gaddr)); \
48 if (!__r && arm_cpu_bswap_data(env)) { \
49 (x) = bswap32(x); \
50 } \
51 __r; \
52 })
53
54#define get_user_data_u16(x, gaddr, env) \
55 ({ abi_long __r = get_user_u16((x), (gaddr)); \
56 if (!__r && arm_cpu_bswap_data(env)) { \
57 (x) = bswap16(x); \
58 } \
59 __r; \
60 })
61
62#define put_user_data_u32(x, gaddr, env) \
63 ({ typeof(x) __x = (x); \
64 if (arm_cpu_bswap_data(env)) { \
65 __x = bswap32(__x); \
66 } \
67 put_user_u32(__x, (gaddr)); \
68 })
69
70#define put_user_data_u16(x, gaddr, env) \
71 ({ typeof(x) __x = (x); \
72 if (arm_cpu_bswap_data(env)) { \
73 __x = bswap16(__x); \
74 } \
75 put_user_u16(__x, (gaddr)); \
76 })
77
Richard Henderson7f4f0d92022-03-22 17:58:38 -070078/*
79 * Similar to code in accel/tcg/user-exec.c, but outside the execution loop.
80 * Must be called with mmap_lock.
81 * We get the PC of the entry address - which is as good as anything,
82 * on a real kernel what you get depends on which mode it uses.
83 */
84static void *atomic_mmu_lookup(CPUArchState *env, uint32_t addr, int size)
85{
86 int need_flags = PAGE_READ | PAGE_WRITE_ORG | PAGE_VALID;
87 int page_flags;
88
89 /* Enforce guest required alignment. */
90 if (unlikely(addr & (size - 1))) {
91 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
92 return NULL;
93 }
94
95 page_flags = page_get_flags(addr);
96 if (unlikely((page_flags & need_flags) != need_flags)) {
97 force_sig_fault(TARGET_SIGSEGV,
98 page_flags & PAGE_VALID ?
99 TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR, addr);
100 return NULL;
101 }
102
103 return g2h(env_cpu(env), addr);
104}
105
106/*
107 * See the Linux kernel's Documentation/arm/kernel_user_helpers.rst
108 * Input:
109 * r0 = oldval
110 * r1 = newval
111 * r2 = pointer to target value
112 *
113 * Output:
114 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
115 * C set if *ptr was changed, clear if no exchange happened
116 */
117static void arm_kernel_cmpxchg32_helper(CPUARMState *env)
118{
119 uint32_t oldval, newval, val, addr, cpsr, *host_addr;
120
Helge Deller38dd78c2023-07-28 21:23:10 +0200121 /* Swap if host != guest endianness, for the host cmpxchg below */
122 oldval = tswap32(env->regs[0]);
123 newval = tswap32(env->regs[1]);
Richard Henderson7f4f0d92022-03-22 17:58:38 -0700124 addr = env->regs[2];
125
126 mmap_lock();
127 host_addr = atomic_mmu_lookup(env, addr, 4);
128 if (!host_addr) {
129 mmap_unlock();
130 return;
131 }
132
133 val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
134 mmap_unlock();
135
136 cpsr = (val == oldval) * CPSR_C;
137 cpsr_write(env, cpsr, CPSR_C, CPSRWriteByInstr);
138 env->regs[0] = cpsr ? 0 : -1;
139}
Laurent Vivierd9673512018-04-11 20:56:36 +0200140
141/*
Richard Henderson330ea9d2022-03-22 17:58:39 -0700142 * See the Linux kernel's Documentation/arm/kernel_user_helpers.rst
Laurent Vivierd9673512018-04-11 20:56:36 +0200143 * Input:
144 * r0 = pointer to oldval
145 * r1 = pointer to newval
146 * r2 = pointer to target value
147 *
148 * Output:
149 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
150 * C set if *ptr was changed, clear if no exchange happened
151 *
152 * Note segv's in kernel helpers are a bit tricky, we can set the
153 * data address sensibly but the PC address is just the entry point.
154 */
155static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
156{
157 uint64_t oldval, newval, val;
158 uint32_t addr, cpsr;
Richard Henderson330ea9d2022-03-22 17:58:39 -0700159 uint64_t *host_addr;
Laurent Vivierd9673512018-04-11 20:56:36 +0200160
Richard Henderson330ea9d2022-03-22 17:58:39 -0700161 addr = env->regs[0];
162 if (get_user_u64(oldval, addr)) {
163 goto segv;
164 }
Laurent Vivierd9673512018-04-11 20:56:36 +0200165
Richard Henderson330ea9d2022-03-22 17:58:39 -0700166 addr = env->regs[1];
167 if (get_user_u64(newval, addr)) {
168 goto segv;
169 }
170
171 mmap_lock();
Laurent Vivierd9673512018-04-11 20:56:36 +0200172 addr = env->regs[2];
Richard Henderson330ea9d2022-03-22 17:58:39 -0700173 host_addr = atomic_mmu_lookup(env, addr, 8);
174 if (!host_addr) {
175 mmap_unlock();
176 return;
Laurent Vivierd9673512018-04-11 20:56:36 +0200177 }
178
Helge Deller38dd78c2023-07-28 21:23:10 +0200179 /* Swap if host != guest endianness, for the host cmpxchg below */
180 oldval = tswap64(oldval);
181 newval = tswap64(newval);
182
Richard Henderson330ea9d2022-03-22 17:58:39 -0700183#ifdef CONFIG_ATOMIC64
184 val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
185 cpsr = (val == oldval) * CPSR_C;
186#else
187 /*
188 * This only works between threads, not between processes, but since
189 * the host has no 64-bit cmpxchg, it is the best that we can do.
190 */
191 start_exclusive();
192 val = *host_addr;
Laurent Vivierd9673512018-04-11 20:56:36 +0200193 if (val == oldval) {
Richard Henderson330ea9d2022-03-22 17:58:39 -0700194 *host_addr = newval;
195 cpsr = CPSR_C;
Laurent Vivierd9673512018-04-11 20:56:36 +0200196 } else {
Richard Henderson330ea9d2022-03-22 17:58:39 -0700197 cpsr = 0;
Laurent Vivierd9673512018-04-11 20:56:36 +0200198 }
Laurent Vivierd9673512018-04-11 20:56:36 +0200199 end_exclusive();
Richard Henderson330ea9d2022-03-22 17:58:39 -0700200#endif
201 mmap_unlock();
202
203 cpsr_write(env, cpsr, CPSR_C, CPSRWriteByInstr);
204 env->regs[0] = cpsr ? 0 : -1;
Laurent Vivierd9673512018-04-11 20:56:36 +0200205 return;
206
Richard Henderson330ea9d2022-03-22 17:58:39 -0700207 segv:
208 force_sig_fault(TARGET_SIGSEGV,
209 page_get_flags(addr) & PAGE_VALID ?
210 TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR, addr);
Laurent Vivierd9673512018-04-11 20:56:36 +0200211}
212
213/* Handle a jump to the kernel code page. */
214static int
215do_kernel_trap(CPUARMState *env)
216{
217 uint32_t addr;
Laurent Vivierd9673512018-04-11 20:56:36 +0200218
219 switch (env->regs[15]) {
220 case 0xffff0fa0: /* __kernel_memory_barrier */
Richard Henderson6e05e702022-03-22 17:58:37 -0700221 smp_mb();
Laurent Vivierd9673512018-04-11 20:56:36 +0200222 break;
223 case 0xffff0fc0: /* __kernel_cmpxchg */
Richard Henderson7f4f0d92022-03-22 17:58:38 -0700224 arm_kernel_cmpxchg32_helper(env);
Laurent Vivierd9673512018-04-11 20:56:36 +0200225 break;
226 case 0xffff0fe0: /* __kernel_get_tls */
227 env->regs[0] = cpu_get_tls(env);
228 break;
229 case 0xffff0f60: /* __kernel_cmpxchg64 */
230 arm_kernel_cmpxchg64_helper(env);
231 break;
232
233 default:
234 return 1;
235 }
236 /* Jump back to the caller. */
237 addr = env->regs[14];
238 if (addr & 1) {
Richard Henderson063bbd82022-04-17 10:43:35 -0700239 env->thumb = true;
Laurent Vivierd9673512018-04-11 20:56:36 +0200240 addr &= ~1;
241 }
242 env->regs[15] = addr;
243
244 return 0;
245}
246
Peter Maydellacebed92020-11-17 15:56:34 +0000247static bool insn_is_linux_bkpt(uint32_t opcode, bool is_thumb)
248{
249 /*
250 * Return true if this insn is one of the three magic UDF insns
251 * which the kernel treats as breakpoint insns.
252 */
253 if (!is_thumb) {
254 return (opcode & 0x0fffffff) == 0x07f001f0;
255 } else {
256 /*
257 * Note that we get the two halves of the 32-bit T32 insn
258 * in the opposite order to the value the kernel uses in
259 * its undef_hook struct.
260 */
261 return ((opcode & 0xffff) == 0xde01) || (opcode == 0xa000f7f0);
262 }
263}
264
Richard Hendersonc1438d62021-04-23 09:54:10 -0700265static bool emulate_arm_fpa11(CPUARMState *env, uint32_t opcode)
266{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000267 TaskState *ts = get_task_state(env_cpu(env));
Richard Hendersonc1438d62021-04-23 09:54:10 -0700268 int rc = EmulateAll(opcode, &ts->fpa, env);
Richard Henderson74081ae2021-04-23 09:54:13 -0700269 int raise, enabled;
Richard Hendersonc1438d62021-04-23 09:54:10 -0700270
271 if (rc == 0) {
272 /* Illegal instruction */
273 return false;
274 }
275 if (rc > 0) {
276 /* Everything ok. */
277 env->regs[15] += 4;
278 return true;
279 }
280
281 /* FP exception */
Richard Henderson74081ae2021-04-23 09:54:13 -0700282 rc = -rc;
283 raise = 0;
Richard Hendersonc1438d62021-04-23 09:54:10 -0700284
285 /* Translate softfloat flags to FPSR flags */
Richard Henderson74081ae2021-04-23 09:54:13 -0700286 if (rc & float_flag_invalid) {
287 raise |= BIT_IOC;
Richard Hendersonc1438d62021-04-23 09:54:10 -0700288 }
Richard Henderson74081ae2021-04-23 09:54:13 -0700289 if (rc & float_flag_divbyzero) {
290 raise |= BIT_DZC;
Richard Hendersonc1438d62021-04-23 09:54:10 -0700291 }
Richard Henderson74081ae2021-04-23 09:54:13 -0700292 if (rc & float_flag_overflow) {
293 raise |= BIT_OFC;
Richard Hendersonc1438d62021-04-23 09:54:10 -0700294 }
Richard Henderson74081ae2021-04-23 09:54:13 -0700295 if (rc & float_flag_underflow) {
296 raise |= BIT_UFC;
Richard Hendersonc1438d62021-04-23 09:54:10 -0700297 }
Richard Henderson74081ae2021-04-23 09:54:13 -0700298 if (rc & float_flag_inexact) {
299 raise |= BIT_IXC;
Richard Hendersonc1438d62021-04-23 09:54:10 -0700300 }
301
Richard Henderson74081ae2021-04-23 09:54:13 -0700302 /* Accumulate unenabled exceptions */
303 enabled = ts->fpa.fpsr >> 16;
304 ts->fpa.fpsr |= raise & ~enabled;
305
306 if (raise & enabled) {
Richard Henderson0a502852021-04-23 09:54:12 -0700307 /*
308 * The kernel's nwfpe emulator does not pass a real si_code.
Peter Maydellbabe6d52021-08-13 14:18:05 +0100309 * It merely uses send_sig(SIGFPE, current, 1), which results in
310 * __send_signal() filling out SI_KERNEL with pid and uid 0 (under
311 * the "SEND_SIG_PRIV" case). That's what our force_sig() does.
Richard Henderson0a502852021-04-23 09:54:12 -0700312 */
Peter Maydellbabe6d52021-08-13 14:18:05 +0100313 force_sig(TARGET_SIGFPE);
Richard Hendersonc1438d62021-04-23 09:54:10 -0700314 } else {
315 env->regs[15] += 4;
316 }
Richard Hendersonc1438d62021-04-23 09:54:10 -0700317 return true;
318}
319
Laurent Vivierd9673512018-04-11 20:56:36 +0200320void cpu_loop(CPUARMState *env)
321{
Richard Henderson2fc0cc02019-03-22 17:41:14 -0700322 CPUState *cs = env_cpu(env);
Richard Henderson39a099c2021-07-23 12:22:54 -1000323 int trapnr, si_signo, si_code;
Laurent Vivierd9673512018-04-11 20:56:36 +0200324 unsigned int n, insn;
Laurent Vivierd9673512018-04-11 20:56:36 +0200325 abi_ulong ret;
326
327 for(;;) {
328 cpu_exec_start(cs);
329 trapnr = cpu_exec(cs);
330 cpu_exec_end(cs);
331 process_queued_cpu_work(cs);
332
333 switch(trapnr) {
334 case EXCP_UDEF:
335 case EXCP_NOCP:
336 case EXCP_INVSTATE:
337 {
Laurent Vivierd9673512018-04-11 20:56:36 +0200338 uint32_t opcode;
Laurent Vivierd9673512018-04-11 20:56:36 +0200339
340 /* we handle the FPU emulation here, as Linux */
341 /* we get the opcode */
342 /* FIXME - what to do if get_user() fails? */
343 get_user_code_u32(opcode, env->regs[15], env);
344
Peter Maydellacebed92020-11-17 15:56:34 +0000345 /*
346 * The Linux kernel treats some UDF patterns specially
347 * to use as breakpoints (instead of the architectural
348 * bkpt insn). These should trigger a SIGTRAP rather
349 * than SIGILL.
350 */
351 if (insn_is_linux_bkpt(opcode, env->thumb)) {
352 goto excp_debug;
353 }
354
Richard Hendersond827f6d2021-04-23 09:54:11 -0700355 if (!env->thumb && emulate_arm_fpa11(env, opcode)) {
Richard Hendersonc1438d62021-04-23 09:54:10 -0700356 break;
Laurent Vivierd9673512018-04-11 20:56:36 +0200357 }
Richard Hendersonc1438d62021-04-23 09:54:10 -0700358
Peter Maydell4c90f0b2021-08-13 14:18:08 +0100359 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN,
360 env->regs[15]);
Laurent Vivierd9673512018-04-11 20:56:36 +0200361 }
362 break;
363 case EXCP_SWI:
Laurent Vivierd9673512018-04-11 20:56:36 +0200364 {
Philippe Mathieu-Daudéde4143f2023-02-06 23:34:56 +0100365 env->eabi = true;
Laurent Vivierd9673512018-04-11 20:56:36 +0200366 /* system call */
Peter Maydell13a0c212020-04-20 22:22:03 +0100367 if (env->thumb) {
Peter Maydell3986a172020-04-20 22:22:06 +0100368 /* Thumb is always EABI style with syscall number in r7 */
369 n = env->regs[7];
Laurent Vivierd9673512018-04-11 20:56:36 +0200370 } else {
Peter Maydell3986a172020-04-20 22:22:06 +0100371 /*
372 * Equivalent of kernel CONFIG_OABI_COMPAT: read the
373 * Arm SVC insn to extract the immediate, which is the
374 * syscall number in OABI.
375 */
Peter Maydell13a0c212020-04-20 22:22:03 +0100376 /* FIXME - what to do if get_user() fails? */
377 get_user_code_u32(insn, env->regs[15] - 4, env);
378 n = insn & 0xffffff;
Peter Maydell3986a172020-04-20 22:22:06 +0100379 if (n == 0) {
380 /* zero immediate: EABI, syscall number in r7 */
Laurent Vivierd9673512018-04-11 20:56:36 +0200381 n = env->regs[7];
382 } else {
Peter Maydell3986a172020-04-20 22:22:06 +0100383 /*
384 * This XOR matches the kernel code: an immediate
385 * in the valid range (0x900000 .. 0x9fffff) is
386 * converted into the correct EABI-style syscall
387 * number; invalid immediates end up as values
388 * > 0xfffff and are handled below as out-of-range.
389 */
390 n ^= ARM_SYSCALL_BASE;
Philippe Mathieu-Daudéde4143f2023-02-06 23:34:56 +0100391 env->eabi = false;
Laurent Vivierd9673512018-04-11 20:56:36 +0200392 }
Peter Maydell3986a172020-04-20 22:22:06 +0100393 }
394
395 if (n > ARM_NR_BASE) {
396 switch (n) {
397 case ARM_NR_cacheflush:
398 /* nop */
399 break;
400 case ARM_NR_set_tls:
401 cpu_set_tls(env, env->regs[0]);
402 env->regs[0] = 0;
403 break;
404 case ARM_NR_breakpoint:
405 env->regs[15] -= env->thumb ? 2 : 4;
406 goto excp_debug;
407 case ARM_NR_get_tls:
408 env->regs[0] = cpu_get_tls(env);
409 break;
410 default:
411 if (n < 0xf0800) {
412 /*
413 * Syscalls 0xf0000..0xf07ff (or 0x9f0000..
414 * 0x9f07ff in OABI numbering) are defined
415 * to return -ENOSYS rather than raising
416 * SIGILL. Note that we have already
417 * removed the 0x900000 prefix.
418 */
419 qemu_log_mask(LOG_UNIMP,
420 "qemu: Unsupported ARM syscall: 0x%x\n",
421 n);
422 env->regs[0] = -TARGET_ENOSYS;
423 } else {
424 /*
425 * Otherwise SIGILL. This includes any SWI with
426 * immediate not originally 0x9fxxxx, because
427 * of the earlier XOR.
Peter Maydell4c90f0b2021-08-13 14:18:08 +0100428 * Like the real kernel, we report the addr of the
429 * SWI in the siginfo si_addr but leave the PC
430 * pointing at the insn after the SWI.
Peter Maydell3986a172020-04-20 22:22:06 +0100431 */
Peter Maydell4c90f0b2021-08-13 14:18:08 +0100432 abi_ulong faultaddr = env->regs[15];
433 faultaddr -= env->thumb ? 2 : 4;
434 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP,
435 faultaddr);
Laurent Vivierd9673512018-04-11 20:56:36 +0200436 }
Peter Maydell3986a172020-04-20 22:22:06 +0100437 break;
Laurent Vivierd9673512018-04-11 20:56:36 +0200438 }
439 } else {
Peter Maydell3986a172020-04-20 22:22:06 +0100440 ret = do_syscall(env,
441 n,
442 env->regs[0],
443 env->regs[1],
444 env->regs[2],
445 env->regs[3],
446 env->regs[4],
447 env->regs[5],
448 0, 0);
Richard Hendersonaf254a22021-11-22 19:47:33 +0100449 if (ret == -QEMU_ERESTARTSYS) {
Peter Maydell3986a172020-04-20 22:22:06 +0100450 env->regs[15] -= env->thumb ? 2 : 4;
Richard Henderson57a0c932021-11-17 05:14:52 -0800451 } else if (ret != -QEMU_ESIGRETURN) {
Peter Maydell3986a172020-04-20 22:22:06 +0100452 env->regs[0] = ret;
453 }
Laurent Vivierd9673512018-04-11 20:56:36 +0200454 }
455 }
456 break;
457 case EXCP_SEMIHOST:
Richard Hendersoned3a06b2022-04-28 01:10:55 -0700458 do_common_semihosting(cs);
Alex Bennée4ff5ef92019-12-17 15:08:57 +0000459 env->regs[15] += env->thumb ? 2 : 4;
Laurent Vivierd9673512018-04-11 20:56:36 +0200460 break;
461 case EXCP_INTERRUPT:
462 /* just indicate that signals should be handled asap */
463 break;
464 case EXCP_PREFETCH_ABORT:
465 case EXCP_DATA_ABORT:
Richard Henderson39a099c2021-07-23 12:22:54 -1000466 /* For user-only we don't set TTBCR_EAE, so look at the FSR. */
467 switch (env->exception.fsr & 0x1f) {
468 case 0x1: /* Alignment */
469 si_signo = TARGET_SIGBUS;
470 si_code = TARGET_BUS_ADRALN;
471 break;
472 case 0x3: /* Access flag fault, level 1 */
473 case 0x6: /* Access flag fault, level 2 */
474 case 0x9: /* Domain fault, level 1 */
475 case 0xb: /* Domain fault, level 2 */
Peter Maydell5b602fc2022-01-14 18:25:35 +0000476 case 0xd: /* Permission fault, level 1 */
477 case 0xf: /* Permission fault, level 2 */
Richard Henderson39a099c2021-07-23 12:22:54 -1000478 si_signo = TARGET_SIGSEGV;
479 si_code = TARGET_SEGV_ACCERR;
480 break;
481 case 0x5: /* Translation fault, level 1 */
482 case 0x7: /* Translation fault, level 2 */
483 si_signo = TARGET_SIGSEGV;
484 si_code = TARGET_SEGV_MAPERR;
485 break;
486 default:
487 g_assert_not_reached();
488 }
489 force_sig_fault(si_signo, si_code, env->exception.vaddress);
Laurent Vivierd9673512018-04-11 20:56:36 +0200490 break;
491 case EXCP_DEBUG:
Peter Maydell13a0c212020-04-20 22:22:03 +0100492 case EXCP_BKPT:
Laurent Vivierd9673512018-04-11 20:56:36 +0200493 excp_debug:
Peter Maydell4c90f0b2021-08-13 14:18:08 +0100494 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->regs[15]);
Laurent Vivierd9673512018-04-11 20:56:36 +0200495 break;
496 case EXCP_KERNEL_TRAP:
497 if (do_kernel_trap(env))
498 goto error;
499 break;
500 case EXCP_YIELD:
501 /* nothing to do here for user-mode, just resume guest code */
502 break;
503 case EXCP_ATOMIC:
504 cpu_exec_step_atomic(cs);
505 break;
506 default:
507 error:
508 EXCP_DUMP(env, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
509 abort();
510 }
511 process_pending_signals(env);
512 }
513}
514
Laurent Viviercd71c082018-04-11 20:56:33 +0200515void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
516{
Richard Henderson29a0af62019-03-22 16:07:18 -0700517 CPUState *cpu = env_cpu(env);
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000518 TaskState *ts = get_task_state(cpu);
Laurent Vivierd9673512018-04-11 20:56:36 +0200519 struct image_info *info = ts->info;
520 int i;
521
522 cpsr_write(env, regs->uregs[16], CPSR_USER | CPSR_EXEC,
523 CPSRWriteByInstr);
524 for(i = 0; i < 16; i++) {
525 env->regs[i] = regs->uregs[i];
526 }
Marc-André Lureauee3eb3a2022-03-23 19:57:18 +0400527#if TARGET_BIG_ENDIAN
Laurent Vivierd9673512018-04-11 20:56:36 +0200528 /* Enable BE8. */
529 if (EF_ARM_EABI_VERSION(info->elf_flags) >= EF_ARM_EABI_VER4
530 && (info->elf_flags & EF_ARM_BE8)) {
531 env->uncached_cpsr |= CPSR_E;
532 env->cp15.sctlr_el[1] |= SCTLR_E0E;
533 } else {
534 env->cp15.sctlr_el[1] |= SCTLR_B;
535 }
Richard Henderson37bf16c2019-10-23 11:00:56 -0400536 arm_rebuild_hflags(env);
Laurent Vivierd9673512018-04-11 20:56:36 +0200537#endif
538
539 ts->stack_base = info->start_stack;
540 ts->heap_base = info->brk;
541 /* This will be filled in on the first SYS_HEAPINFO call. */
542 ts->heap_limit = 0;
Laurent Viviercd71c082018-04-11 20:56:33 +0200543}