linux-user: Add support for a group of btrfs ioctls used for snapshots
[qemu.git] / linux-user / nios2 / signal.c
1 /*
2 * Emulation of Linux signals
3 *
4 * Copyright (c) 2003 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 #include "qemu/osdep.h"
20 #include "qemu.h"
21 #include "signal-common.h"
22 #include "linux-user/trace.h"
23
24 #define MCONTEXT_VERSION 2
25
26 struct target_sigcontext {
27 int version;
28 unsigned long gregs[32];
29 };
30
31 struct target_ucontext {
32 abi_ulong tuc_flags;
33 abi_ulong tuc_link;
34 target_stack_t tuc_stack;
35 struct target_sigcontext tuc_mcontext;
36 target_sigset_t tuc_sigmask; /* mask last for extensibility */
37 };
38
39 struct target_rt_sigframe {
40 struct target_siginfo info;
41 struct target_ucontext uc;
42 };
43
44 static int rt_setup_ucontext(struct target_ucontext *uc, CPUNios2State *env)
45 {
46 unsigned long *gregs = uc->tuc_mcontext.gregs;
47
48 __put_user(MCONTEXT_VERSION, &uc->tuc_mcontext.version);
49 __put_user(env->regs[1], &gregs[0]);
50 __put_user(env->regs[2], &gregs[1]);
51 __put_user(env->regs[3], &gregs[2]);
52 __put_user(env->regs[4], &gregs[3]);
53 __put_user(env->regs[5], &gregs[4]);
54 __put_user(env->regs[6], &gregs[5]);
55 __put_user(env->regs[7], &gregs[6]);
56 __put_user(env->regs[8], &gregs[7]);
57 __put_user(env->regs[9], &gregs[8]);
58 __put_user(env->regs[10], &gregs[9]);
59 __put_user(env->regs[11], &gregs[10]);
60 __put_user(env->regs[12], &gregs[11]);
61 __put_user(env->regs[13], &gregs[12]);
62 __put_user(env->regs[14], &gregs[13]);
63 __put_user(env->regs[15], &gregs[14]);
64 __put_user(env->regs[16], &gregs[15]);
65 __put_user(env->regs[17], &gregs[16]);
66 __put_user(env->regs[18], &gregs[17]);
67 __put_user(env->regs[19], &gregs[18]);
68 __put_user(env->regs[20], &gregs[19]);
69 __put_user(env->regs[21], &gregs[20]);
70 __put_user(env->regs[22], &gregs[21]);
71 __put_user(env->regs[23], &gregs[22]);
72 __put_user(env->regs[R_RA], &gregs[23]);
73 __put_user(env->regs[R_FP], &gregs[24]);
74 __put_user(env->regs[R_GP], &gregs[25]);
75 __put_user(env->regs[R_EA], &gregs[27]);
76 __put_user(env->regs[R_SP], &gregs[28]);
77
78 return 0;
79 }
80
81 static int rt_restore_ucontext(CPUNios2State *env, struct target_ucontext *uc,
82 int *pr2)
83 {
84 int temp;
85 abi_ulong off, frame_addr = env->regs[R_SP];
86 unsigned long *gregs = uc->tuc_mcontext.gregs;
87 int err;
88
89 /* Always make any pending restarted system calls return -EINTR */
90 /* current->restart_block.fn = do_no_restart_syscall; */
91
92 __get_user(temp, &uc->tuc_mcontext.version);
93 if (temp != MCONTEXT_VERSION) {
94 return 1;
95 }
96
97 /* restore passed registers */
98 __get_user(env->regs[1], &gregs[0]);
99 __get_user(env->regs[2], &gregs[1]);
100 __get_user(env->regs[3], &gregs[2]);
101 __get_user(env->regs[4], &gregs[3]);
102 __get_user(env->regs[5], &gregs[4]);
103 __get_user(env->regs[6], &gregs[5]);
104 __get_user(env->regs[7], &gregs[6]);
105 __get_user(env->regs[8], &gregs[7]);
106 __get_user(env->regs[9], &gregs[8]);
107 __get_user(env->regs[10], &gregs[9]);
108 __get_user(env->regs[11], &gregs[10]);
109 __get_user(env->regs[12], &gregs[11]);
110 __get_user(env->regs[13], &gregs[12]);
111 __get_user(env->regs[14], &gregs[13]);
112 __get_user(env->regs[15], &gregs[14]);
113 __get_user(env->regs[16], &gregs[15]);
114 __get_user(env->regs[17], &gregs[16]);
115 __get_user(env->regs[18], &gregs[17]);
116 __get_user(env->regs[19], &gregs[18]);
117 __get_user(env->regs[20], &gregs[19]);
118 __get_user(env->regs[21], &gregs[20]);
119 __get_user(env->regs[22], &gregs[21]);
120 __get_user(env->regs[23], &gregs[22]);
121 /* gregs[23] is handled below */
122 /* Verify, should this be settable */
123 __get_user(env->regs[R_FP], &gregs[24]);
124 /* Verify, should this be settable */
125 __get_user(env->regs[R_GP], &gregs[25]);
126 /* Not really necessary no user settable bits */
127 __get_user(temp, &gregs[26]);
128 __get_user(env->regs[R_EA], &gregs[27]);
129
130 __get_user(env->regs[R_RA], &gregs[23]);
131 __get_user(env->regs[R_SP], &gregs[28]);
132
133 off = offsetof(struct target_rt_sigframe, uc.tuc_stack);
134 err = do_sigaltstack(frame_addr + off, 0, get_sp_from_cpustate(env));
135 if (err == -EFAULT) {
136 return 1;
137 }
138
139 *pr2 = env->regs[2];
140 return 0;
141 }
142
143 static void *get_sigframe(struct target_sigaction *ka, CPUNios2State *env,
144 size_t frame_size)
145 {
146 unsigned long usp;
147
148 /* This is the X/Open sanctioned signal stack switching. */
149 usp = target_sigsp(get_sp_from_cpustate(env), ka);
150
151 /* Verify, is it 32 or 64 bit aligned */
152 return (void *)((usp - frame_size) & -8UL);
153 }
154
155 void setup_rt_frame(int sig, struct target_sigaction *ka,
156 target_siginfo_t *info,
157 target_sigset_t *set,
158 CPUNios2State *env)
159 {
160 struct target_rt_sigframe *frame;
161 int i, err = 0;
162
163 frame = get_sigframe(ka, env, sizeof(*frame));
164
165 if (ka->sa_flags & SA_SIGINFO) {
166 tswap_siginfo(&frame->info, info);
167 }
168
169 /* Create the ucontext. */
170 __put_user(0, &frame->uc.tuc_flags);
171 __put_user(0, &frame->uc.tuc_link);
172 target_save_altstack(&frame->uc.tuc_stack, env);
173 err |= rt_setup_ucontext(&frame->uc, env);
174 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
175 __put_user((abi_ulong)set->sig[i],
176 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
177 }
178
179 if (err) {
180 goto give_sigsegv;
181 }
182
183 /* Set up to return from userspace; jump to fixed address sigreturn
184 trampoline on kuser page. */
185 env->regs[R_RA] = (unsigned long) (0x1044);
186
187 /* Set up registers for signal handler */
188 env->regs[R_SP] = (unsigned long) frame;
189 env->regs[4] = (unsigned long) sig;
190 env->regs[5] = (unsigned long) &frame->info;
191 env->regs[6] = (unsigned long) &frame->uc;
192 env->regs[R_EA] = (unsigned long) ka->_sa_handler;
193 return;
194
195 give_sigsegv:
196 if (sig == TARGET_SIGSEGV) {
197 ka->_sa_handler = TARGET_SIG_DFL;
198 }
199 force_sigsegv(sig);
200 return;
201 }
202
203 long do_sigreturn(CPUNios2State *env)
204 {
205 trace_user_do_sigreturn(env, 0);
206 qemu_log_mask(LOG_UNIMP, "do_sigreturn: not implemented\n");
207 return -TARGET_ENOSYS;
208 }
209
210 long do_rt_sigreturn(CPUNios2State *env)
211 {
212 /* Verify, can we follow the stack back */
213 abi_ulong frame_addr = env->regs[R_SP];
214 struct target_rt_sigframe *frame;
215 sigset_t set;
216 int rval;
217
218 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
219 goto badframe;
220 }
221
222 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
223 do_sigprocmask(SIG_SETMASK, &set, NULL);
224
225 if (rt_restore_ucontext(env, &frame->uc, &rval)) {
226 goto badframe;
227 }
228
229 unlock_user_struct(frame, frame_addr, 0);
230 return rval;
231
232 badframe:
233 unlock_user_struct(frame, frame_addr, 0);
234 force_sig(TARGET_SIGSEGV);
235 return 0;
236 }