linux-user/nios2: Only initialize SP and PC in target_cpu_copy_regs
[qemu/rayw.git] / linux-user / nios2 / cpu_loop.c
blobc5e68ac04894125abbff5729ca3f64354bbf2efb
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);
36 switch (trapnr) {
37 case EXCP_INTERRUPT:
38 /* just indicate that signals should be handled asap */
39 break;
41 case EXCP_TRAP:
43 * TODO: This advance should be done in the translator, as
44 * hardware produces an advanced pc as part of all exceptions.
46 env->pc += 4;
48 switch (env->error_code) {
49 case 0:
50 qemu_log_mask(CPU_LOG_INT, "\nSyscall\n");
52 ret = do_syscall(env, env->regs[2],
53 env->regs[4], env->regs[5], env->regs[6],
54 env->regs[7], env->regs[8], env->regs[9],
55 0, 0);
57 if (ret == -QEMU_ESIGRETURN) {
58 /* rt_sigreturn has set all state. */
59 break;
61 if (ret == -QEMU_ERESTARTSYS) {
62 env->pc -= 4;
63 break;
66 * See the code after translate_rc_and_ret: all negative
67 * values are errors (aided by userspace restricted to 2G),
68 * errno is returned positive in r2, and error indication
69 * is a boolean in r7.
71 env->regs[2] = abs(ret);
72 env->regs[7] = ret < 0;
73 break;
75 case 1:
76 qemu_log_mask(CPU_LOG_INT, "\nTrap 1\n");
77 force_sig_fault(TARGET_SIGUSR1, 0, env->pc);
78 break;
79 case 2:
80 qemu_log_mask(CPU_LOG_INT, "\nTrap 2\n");
81 force_sig_fault(TARGET_SIGUSR2, 0, env->pc);
82 break;
83 case 31:
84 qemu_log_mask(CPU_LOG_INT, "\nTrap 31\n");
85 /* Match kernel's breakpoint_c(). */
86 env->pc -= 4;
87 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
88 break;
89 default:
90 qemu_log_mask(CPU_LOG_INT, "\nTrap %d\n", env->error_code);
91 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP, env->pc);
92 break;
94 case 16: /* QEMU specific, for __kuser_cmpxchg */
96 abi_ptr g = env->regs[4];
97 uint32_t *h, n, o;
99 if (g & 0x3) {
100 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, g);
101 break;
103 ret = page_get_flags(g);
104 if (!(ret & PAGE_VALID)) {
105 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, g);
106 break;
108 if (!(ret & PAGE_READ) || !(ret & PAGE_WRITE)) {
109 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_ACCERR, g);
110 break;
112 h = g2h(cs, g);
113 o = env->regs[5];
114 n = env->regs[6];
115 env->regs[2] = qatomic_cmpxchg(h, o, n) - o;
117 break;
119 break;
121 case EXCP_DEBUG:
122 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
123 break;
124 default:
125 EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
126 trapnr);
127 abort();
130 process_pending_signals(env);
134 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
136 env->regs[R_SP] = regs->sp;
137 env->pc = regs->ea;