Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / linux-user / nios2 / cpu_loop.c
blob7fe08c87501ab0d0df4e81fd6c1c6e73909ad822
1 /*
2 * qemu user cpu loop
4 * Copyright (c) 2003-2008 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu.h"
22 #include "user-internals.h"
23 #include "cpu_loop-common.h"
24 #include "signal-common.h"
26 void cpu_loop(CPUNios2State *env)
28 CPUState *cs = env_cpu(env);
29 int trapnr, ret;
31 for (;;) {
32 cpu_exec_start(cs);
33 trapnr = cpu_exec(cs);
34 cpu_exec_end(cs);
35 process_queued_cpu_work(cs);
37 switch (trapnr) {
38 case EXCP_INTERRUPT:
39 /* just indicate that signals should be handled asap */
40 break;
42 case EXCP_DIV:
43 /* Match kernel's handle_diverror_c(). */
44 env->pc -= 4;
45 force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->pc);
46 break;
48 case EXCP_UNALIGN:
49 case EXCP_UNALIGND:
50 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN,
51 env->ctrl[CR_BADADDR]);
52 break;
54 case EXCP_ILLEGAL:
55 case EXCP_UNIMPL:
56 /* Match kernel's handle_illegal_c(). */
57 env->pc -= 4;
58 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC, env->pc);
59 break;
60 case EXCP_SUPERI:
61 /* Match kernel's handle_supervisor_instr(). */
62 env->pc -= 4;
63 force_sig_fault(TARGET_SIGILL, TARGET_ILL_PRVOPC, env->pc);
64 break;
66 case EXCP_TRAP:
67 switch (env->error_code) {
68 case 0:
69 qemu_log_mask(CPU_LOG_INT, "\nSyscall\n");
71 ret = do_syscall(env, env->regs[2],
72 env->regs[4], env->regs[5], env->regs[6],
73 env->regs[7], env->regs[8], env->regs[9],
74 0, 0);
76 if (ret == -QEMU_ESIGRETURN) {
77 /* rt_sigreturn has set all state. */
78 break;
80 if (ret == -QEMU_ERESTARTSYS) {
81 env->pc -= 4;
82 break;
85 * See the code after translate_rc_and_ret: all negative
86 * values are errors (aided by userspace restricted to 2G),
87 * errno is returned positive in r2, and error indication
88 * is a boolean in r7.
90 env->regs[2] = abs(ret);
91 env->regs[7] = ret < 0;
92 break;
94 case 1:
95 qemu_log_mask(CPU_LOG_INT, "\nTrap 1\n");
96 force_sig_fault(TARGET_SIGUSR1, 0, env->pc);
97 break;
98 case 2:
99 qemu_log_mask(CPU_LOG_INT, "\nTrap 2\n");
100 force_sig_fault(TARGET_SIGUSR2, 0, env->pc);
101 break;
102 case 31:
103 qemu_log_mask(CPU_LOG_INT, "\nTrap 31\n");
104 /* Match kernel's breakpoint_c(). */
105 env->pc -= 4;
106 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
107 break;
108 default:
109 qemu_log_mask(CPU_LOG_INT, "\nTrap %d\n", env->error_code);
110 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP, env->pc);
111 break;
113 case 16: /* QEMU specific, for __kuser_cmpxchg */
115 abi_ptr g = env->regs[4];
116 uint32_t *h, n, o;
118 if (g & 0x3) {
119 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, g);
120 break;
122 ret = page_get_flags(g);
123 if (!(ret & PAGE_VALID)) {
124 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, g);
125 break;
127 if (!(ret & PAGE_READ) || !(ret & PAGE_WRITE)) {
128 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_ACCERR, g);
129 break;
131 h = g2h(cs, g);
132 o = env->regs[5];
133 n = env->regs[6];
134 env->regs[2] = qatomic_cmpxchg(h, o, n) - o;
136 break;
138 break;
140 case EXCP_DEBUG:
141 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
142 break;
143 default:
144 EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
145 trapnr);
146 abort();
149 process_pending_signals(env);
153 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
155 env->regs[R_SP] = regs->sp;
156 env->pc = regs->ea;