target/s390x: Fix SRDA CC calculation
[qemu/ar7.git] / linux-user / nios2 / signal.c
blob517cd392701c1072bc9f587ef9c243027b9f6562
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 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/>.
19 #include "qemu/osdep.h"
20 #include "qemu.h"
21 #include "user-internals.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
25 #define MCONTEXT_VERSION 2
27 struct target_sigcontext {
28 int version;
29 unsigned long gregs[32];
32 struct target_ucontext {
33 abi_ulong tuc_flags;
34 abi_ulong tuc_link;
35 target_stack_t tuc_stack;
36 struct target_sigcontext tuc_mcontext;
37 target_sigset_t tuc_sigmask; /* mask last for extensibility */
40 struct target_rt_sigframe {
41 struct target_siginfo info;
42 struct target_ucontext uc;
45 static void rt_setup_ucontext(struct target_ucontext *uc, CPUNios2State *env)
47 unsigned long *gregs = uc->tuc_mcontext.gregs;
49 __put_user(MCONTEXT_VERSION, &uc->tuc_mcontext.version);
50 __put_user(env->regs[1], &gregs[0]);
51 __put_user(env->regs[2], &gregs[1]);
52 __put_user(env->regs[3], &gregs[2]);
53 __put_user(env->regs[4], &gregs[3]);
54 __put_user(env->regs[5], &gregs[4]);
55 __put_user(env->regs[6], &gregs[5]);
56 __put_user(env->regs[7], &gregs[6]);
57 __put_user(env->regs[8], &gregs[7]);
58 __put_user(env->regs[9], &gregs[8]);
59 __put_user(env->regs[10], &gregs[9]);
60 __put_user(env->regs[11], &gregs[10]);
61 __put_user(env->regs[12], &gregs[11]);
62 __put_user(env->regs[13], &gregs[12]);
63 __put_user(env->regs[14], &gregs[13]);
64 __put_user(env->regs[15], &gregs[14]);
65 __put_user(env->regs[16], &gregs[15]);
66 __put_user(env->regs[17], &gregs[16]);
67 __put_user(env->regs[18], &gregs[17]);
68 __put_user(env->regs[19], &gregs[18]);
69 __put_user(env->regs[20], &gregs[19]);
70 __put_user(env->regs[21], &gregs[20]);
71 __put_user(env->regs[22], &gregs[21]);
72 __put_user(env->regs[23], &gregs[22]);
73 __put_user(env->regs[R_RA], &gregs[23]);
74 __put_user(env->regs[R_FP], &gregs[24]);
75 __put_user(env->regs[R_GP], &gregs[25]);
76 __put_user(env->regs[R_PC], &gregs[27]);
77 __put_user(env->regs[R_SP], &gregs[28]);
80 static int rt_restore_ucontext(CPUNios2State *env, struct target_ucontext *uc,
81 int *pr2)
83 int temp;
84 unsigned long *gregs = uc->tuc_mcontext.gregs;
86 /* Always make any pending restarted system calls return -EINTR */
87 /* current->restart_block.fn = do_no_restart_syscall; */
89 __get_user(temp, &uc->tuc_mcontext.version);
90 if (temp != MCONTEXT_VERSION) {
91 return 1;
94 /* restore passed registers */
95 __get_user(env->regs[1], &gregs[0]);
96 __get_user(env->regs[2], &gregs[1]);
97 __get_user(env->regs[3], &gregs[2]);
98 __get_user(env->regs[4], &gregs[3]);
99 __get_user(env->regs[5], &gregs[4]);
100 __get_user(env->regs[6], &gregs[5]);
101 __get_user(env->regs[7], &gregs[6]);
102 __get_user(env->regs[8], &gregs[7]);
103 __get_user(env->regs[9], &gregs[8]);
104 __get_user(env->regs[10], &gregs[9]);
105 __get_user(env->regs[11], &gregs[10]);
106 __get_user(env->regs[12], &gregs[11]);
107 __get_user(env->regs[13], &gregs[12]);
108 __get_user(env->regs[14], &gregs[13]);
109 __get_user(env->regs[15], &gregs[14]);
110 __get_user(env->regs[16], &gregs[15]);
111 __get_user(env->regs[17], &gregs[16]);
112 __get_user(env->regs[18], &gregs[17]);
113 __get_user(env->regs[19], &gregs[18]);
114 __get_user(env->regs[20], &gregs[19]);
115 __get_user(env->regs[21], &gregs[20]);
116 __get_user(env->regs[22], &gregs[21]);
117 __get_user(env->regs[23], &gregs[22]);
118 /* gregs[23] is handled below */
119 /* Verify, should this be settable */
120 __get_user(env->regs[R_FP], &gregs[24]);
121 /* Verify, should this be settable */
122 __get_user(env->regs[R_GP], &gregs[25]);
123 /* Not really necessary no user settable bits */
124 __get_user(temp, &gregs[26]);
125 __get_user(env->regs[R_PC], &gregs[27]);
127 __get_user(env->regs[R_RA], &gregs[23]);
128 __get_user(env->regs[R_SP], &gregs[28]);
130 target_restore_altstack(&uc->tuc_stack, env);
132 *pr2 = env->regs[2];
133 return 0;
136 static abi_ptr get_sigframe(struct target_sigaction *ka, CPUNios2State *env,
137 size_t frame_size)
139 unsigned long usp;
141 /* This is the X/Open sanctioned signal stack switching. */
142 usp = target_sigsp(get_sp_from_cpustate(env), ka);
144 /* Verify, is it 32 or 64 bit aligned */
145 return (usp - frame_size) & -8;
148 void setup_rt_frame(int sig, struct target_sigaction *ka,
149 target_siginfo_t *info,
150 target_sigset_t *set,
151 CPUNios2State *env)
153 struct target_rt_sigframe *frame;
154 abi_ptr frame_addr;
155 int i;
157 frame_addr = get_sigframe(ka, env, sizeof(*frame));
158 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
159 force_sigsegv(sig);
160 return;
163 tswap_siginfo(&frame->info, info);
165 /* Create the ucontext. */
166 __put_user(0, &frame->uc.tuc_flags);
167 __put_user(0, &frame->uc.tuc_link);
168 target_save_altstack(&frame->uc.tuc_stack, env);
169 rt_setup_ucontext(&frame->uc, env);
170 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
171 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
174 /* Set up to return from userspace; jump to fixed address sigreturn
175 trampoline on kuser page. */
176 env->regs[R_RA] = (unsigned long) (0x1044);
178 /* Set up registers for signal handler */
179 env->regs[R_SP] = frame_addr;
180 env->regs[4] = sig;
181 env->regs[5] = frame_addr + offsetof(struct target_rt_sigframe, info);
182 env->regs[6] = frame_addr + offsetof(struct target_rt_sigframe, uc);
183 env->regs[R_PC] = ka->_sa_handler;
185 unlock_user_struct(frame, frame_addr, 1);
188 long do_sigreturn(CPUNios2State *env)
190 trace_user_do_sigreturn(env, 0);
191 qemu_log_mask(LOG_UNIMP, "do_sigreturn: not implemented\n");
192 return -TARGET_ENOSYS;
195 long do_rt_sigreturn(CPUNios2State *env)
197 /* Verify, can we follow the stack back */
198 abi_ulong frame_addr = env->regs[R_SP];
199 struct target_rt_sigframe *frame;
200 sigset_t set;
201 int rval;
203 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
204 goto badframe;
207 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
208 set_sigmask(&set);
210 if (rt_restore_ucontext(env, &frame->uc, &rval)) {
211 goto badframe;
214 unlock_user_struct(frame, frame_addr, 0);
215 return rval;
217 badframe:
218 unlock_user_struct(frame, frame_addr, 0);
219 force_sig(TARGET_SIGSEGV);
220 return 0;