target/s390x: Fix SRDA CC calculation
[qemu/ar7.git] / linux-user / nios2 / cpu_loop.c
blob1e93ef34e649be1fe032cb3a7ccd25b342abf750
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 target_siginfo_t info;
30 int trapnr, ret;
32 for (;;) {
33 cpu_exec_start(cs);
34 trapnr = cpu_exec(cs);
35 cpu_exec_end(cs);
37 switch (trapnr) {
38 case EXCP_INTERRUPT:
39 /* just indicate that signals should be handled asap */
40 break;
42 case EXCP_TRAP:
43 switch (env->error_code) {
44 case 0:
45 qemu_log_mask(CPU_LOG_INT, "\nSyscall\n");
47 ret = do_syscall(env, env->regs[2],
48 env->regs[4], env->regs[5], env->regs[6],
49 env->regs[7], env->regs[8], env->regs[9],
50 0, 0);
52 if (env->regs[2] == 0) { /* FIXME: syscall 0 workaround */
53 ret = 0;
56 env->regs[2] = abs(ret);
57 /* Return value is 0..4096 */
58 env->regs[7] = ret > 0xfffff000u;
59 env->regs[R_PC] += 4;
60 break;
62 case 1:
63 qemu_log_mask(CPU_LOG_INT, "\nTrap 1\n");
64 force_sig_fault(TARGET_SIGUSR1, 0, env->regs[R_PC]);
65 break;
66 case 2:
67 qemu_log_mask(CPU_LOG_INT, "\nTrap 2\n");
68 force_sig_fault(TARGET_SIGUSR2, 0, env->regs[R_PC]);
69 break;
70 case 31:
71 qemu_log_mask(CPU_LOG_INT, "\nTrap 31\n");
72 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->regs[R_PC]);
73 break;
74 default:
75 qemu_log_mask(CPU_LOG_INT, "\nTrap %d\n", env->error_code);
76 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP,
77 env->regs[R_PC]);
78 break;
80 case 16: /* QEMU specific, for __kuser_cmpxchg */
82 abi_ptr g = env->regs[4];
83 uint32_t *h, n, o;
85 if (g & 0x3) {
86 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, g);
87 break;
89 ret = page_get_flags(g);
90 if (!(ret & PAGE_VALID)) {
91 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, g);
92 break;
94 if (!(ret & PAGE_READ) || !(ret & PAGE_WRITE)) {
95 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_ACCERR, g);
96 break;
98 h = g2h(cs, g);
99 o = env->regs[5];
100 n = env->regs[6];
101 env->regs[2] = qatomic_cmpxchg(h, o, n) - o;
102 env->regs[R_PC] += 4;
104 break;
106 break;
108 case EXCP_DEBUG:
109 info.si_signo = TARGET_SIGTRAP;
110 info.si_errno = 0;
111 info.si_code = TARGET_TRAP_BRKPT;
112 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
113 break;
114 case 0xaa:
116 info.si_signo = TARGET_SIGSEGV;
117 info.si_errno = 0;
118 /* TODO: check env->error_code */
119 info.si_code = TARGET_SEGV_MAPERR;
120 info._sifields._sigfault._addr = env->regs[R_PC];
121 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
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[0] = 0;
137 env->regs[1] = regs->r1;
138 env->regs[2] = regs->r2;
139 env->regs[3] = regs->r3;
140 env->regs[4] = regs->r4;
141 env->regs[5] = regs->r5;
142 env->regs[6] = regs->r6;
143 env->regs[7] = regs->r7;
144 env->regs[8] = regs->r8;
145 env->regs[9] = regs->r9;
146 env->regs[10] = regs->r10;
147 env->regs[11] = regs->r11;
148 env->regs[12] = regs->r12;
149 env->regs[13] = regs->r13;
150 env->regs[14] = regs->r14;
151 env->regs[15] = regs->r15;
152 /* TODO: unsigned long orig_r2; */
153 env->regs[R_RA] = regs->ra;
154 env->regs[R_FP] = regs->fp;
155 env->regs[R_SP] = regs->sp;
156 env->regs[R_GP] = regs->gp;
157 env->regs[CR_ESTATUS] = regs->estatus;
158 env->regs[R_PC] = regs->ea;
159 /* TODO: unsigned long orig_r7; */