Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/arch/cris/kernel/signal.c | |
3 | * | |
4 | * Based on arch/i386/kernel/signal.c by | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson * | |
7 | * | |
8 | * Ideas also taken from arch/arm. | |
9 | * | |
a4858d4d | 10 | * Copyright (C) 2000-2007 Axis Communications AB |
1da177e4 LT |
11 | * |
12 | * Authors: Bjorn Wesen (bjornw@axis.com) | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <linux/sched.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/smp.h> | |
1da177e4 LT |
19 | #include <linux/kernel.h> |
20 | #include <linux/signal.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/wait.h> | |
23 | #include <linux/ptrace.h> | |
24 | #include <linux/unistd.h> | |
25 | #include <linux/stddef.h> | |
26 | ||
27 | #include <asm/processor.h> | |
28 | #include <asm/ucontext.h> | |
7c0f6ba6 | 29 | #include <linux/uaccess.h> |
b1a154db | 30 | #include <arch/system.h> |
1da177e4 LT |
31 | |
32 | #define DEBUG_SIG 0 | |
33 | ||
1da177e4 LT |
34 | /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */ |
35 | /* manipulate regs so that upon return, it will be re-executed */ | |
36 | ||
37 | /* We rely on that pc points to the instruction after "break 13", so the | |
38 | * library must never do strange things like putting it in a delay slot. | |
39 | */ | |
40 | #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2; | |
41 | ||
a4858d4d | 42 | void do_signal(int canrestart, struct pt_regs *regs); |
1da177e4 | 43 | |
1da177e4 LT |
44 | /* |
45 | * Do a signal return; undo the signal stack. | |
46 | */ | |
47 | ||
48 | struct sigframe { | |
49 | struct sigcontext sc; | |
50 | unsigned long extramask[_NSIG_WORDS-1]; | |
51 | unsigned char retcode[8]; /* trampoline code */ | |
52 | }; | |
53 | ||
54 | struct rt_sigframe { | |
55 | struct siginfo *pinfo; | |
56 | void *puc; | |
57 | struct siginfo info; | |
58 | struct ucontext uc; | |
59 | unsigned char retcode[8]; /* trampoline code */ | |
60 | }; | |
61 | ||
62 | ||
63 | static int | |
64 | restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) | |
65 | { | |
66 | unsigned int err = 0; | |
67 | unsigned long old_usp; | |
68 | ||
69 | /* Always make any pending restarted system calls return -EINTR */ | |
f56141e3 | 70 | current->restart_block.fn = do_no_restart_syscall; |
1da177e4 LT |
71 | |
72 | /* restore the regs from &sc->regs (same as sc, since regs is first) | |
73 | * (sc is already checked for VERIFY_READ since the sigframe was | |
74 | * checked in sys_sigreturn previously) | |
75 | */ | |
76 | ||
77 | if (__copy_from_user(regs, sc, sizeof(struct pt_regs))) | |
78 | goto badframe; | |
79 | ||
80 | /* make sure the U-flag is set so user-mode cannot fool us */ | |
81 | ||
82 | regs->dccr |= 1 << 8; | |
83 | ||
84 | /* restore the old USP as it was before we stacked the sc etc. | |
85 | * (we cannot just pop the sigcontext since we aligned the sp and | |
86 | * stuff after pushing it) | |
87 | */ | |
88 | ||
89 | err |= __get_user(old_usp, &sc->usp); | |
90 | ||
91 | wrusp(old_usp); | |
92 | ||
93 | /* TODO: the other ports use regs->orig_XX to disable syscall checks | |
94 | * after this completes, but we don't use that mechanism. maybe we can | |
a4858d4d | 95 | * use it now ? |
1da177e4 LT |
96 | */ |
97 | ||
98 | return err; | |
99 | ||
100 | badframe: | |
101 | return 1; | |
102 | } | |
103 | ||
e6a6d210 | 104 | asmlinkage int sys_sigreturn(void) |
1da177e4 | 105 | { |
e6a6d210 | 106 | struct pt_regs *regs = current_pt_regs(); |
1da177e4 LT |
107 | struct sigframe __user *frame = (struct sigframe *)rdusp(); |
108 | sigset_t set; | |
109 | ||
110 | /* | |
111 | * Since we stacked the signal on a dword boundary, | |
112 | * then frame should be dword aligned here. If it's | |
113 | * not, then the user is trying to mess with us. | |
114 | */ | |
115 | if (((long)frame) & 3) | |
116 | goto badframe; | |
117 | ||
118 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
119 | goto badframe; | |
120 | if (__get_user(set.sig[0], &frame->sc.oldmask) | |
121 | || (_NSIG_WORDS > 1 | |
122 | && __copy_from_user(&set.sig[1], frame->extramask, | |
123 | sizeof(frame->extramask)))) | |
124 | goto badframe; | |
125 | ||
f3b5e822 | 126 | set_current_blocked(&set); |
a4858d4d | 127 | |
1da177e4 LT |
128 | if (restore_sigcontext(regs, &frame->sc)) |
129 | goto badframe; | |
130 | ||
131 | /* TODO: SIGTRAP when single-stepping as in arm ? */ | |
132 | ||
133 | return regs->r10; | |
134 | ||
135 | badframe: | |
136 | force_sig(SIGSEGV, current); | |
137 | return 0; | |
a4858d4d | 138 | } |
1da177e4 | 139 | |
e6a6d210 | 140 | asmlinkage int sys_rt_sigreturn(void) |
1da177e4 | 141 | { |
e6a6d210 | 142 | struct pt_regs *regs = current_pt_regs(); |
1da177e4 LT |
143 | struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp(); |
144 | sigset_t set; | |
145 | ||
146 | /* | |
147 | * Since we stacked the signal on a dword boundary, | |
148 | * then frame should be dword aligned here. If it's | |
149 | * not, then the user is trying to mess with us. | |
150 | */ | |
151 | if (((long)frame) & 3) | |
152 | goto badframe; | |
153 | ||
154 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
155 | goto badframe; | |
156 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | |
157 | goto badframe; | |
158 | ||
f3b5e822 | 159 | set_current_blocked(&set); |
a4858d4d | 160 | |
1da177e4 LT |
161 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) |
162 | goto badframe; | |
163 | ||
d970e428 | 164 | if (restore_altstack(&frame->uc.uc_stack)) |
1da177e4 LT |
165 | goto badframe; |
166 | ||
167 | return regs->r10; | |
168 | ||
169 | badframe: | |
170 | force_sig(SIGSEGV, current); | |
171 | return 0; | |
a4858d4d | 172 | } |
1da177e4 LT |
173 | |
174 | /* | |
175 | * Set up a signal frame. | |
176 | */ | |
177 | ||
a4858d4d JN |
178 | static int setup_sigcontext(struct sigcontext __user *sc, |
179 | struct pt_regs *regs, unsigned long mask) | |
1da177e4 LT |
180 | { |
181 | int err = 0; | |
182 | unsigned long usp = rdusp(); | |
183 | ||
184 | /* copy the regs. they are first in sc so we can use sc directly */ | |
185 | ||
186 | err |= __copy_to_user(sc, regs, sizeof(struct pt_regs)); | |
187 | ||
188 | /* Set the frametype to CRIS_FRAME_NORMAL for the execution of | |
189 | the signal handler. The frametype will be restored to its previous | |
190 | value in restore_sigcontext. */ | |
191 | regs->frametype = CRIS_FRAME_NORMAL; | |
192 | ||
193 | /* then some other stuff */ | |
194 | ||
195 | err |= __put_user(mask, &sc->oldmask); | |
196 | ||
197 | err |= __put_user(usp, &sc->usp); | |
198 | ||
199 | return err; | |
200 | } | |
201 | ||
a4858d4d JN |
202 | /* Figure out where we want to put the new signal frame |
203 | * - usually on the stack. */ | |
1da177e4 LT |
204 | |
205 | static inline void __user * | |
8215ade8 | 206 | get_sigframe(struct ksignal *ksig, size_t frame_size) |
1da177e4 | 207 | { |
8215ade8 | 208 | unsigned long sp = sigsp(rdusp(), ksig); |
1da177e4 LT |
209 | |
210 | /* make sure the frame is dword-aligned */ | |
211 | ||
212 | sp &= ~3; | |
213 | ||
214 | return (void __user*)(sp - frame_size); | |
215 | } | |
216 | ||
217 | /* grab and setup a signal frame. | |
a4858d4d | 218 | * |
1da177e4 LT |
219 | * basically we stack a lot of state info, and arrange for the |
220 | * user-mode program to return to the kernel using either a | |
221 | * trampoline which performs the syscall sigreturn, or a provided | |
222 | * user-mode trampoline. | |
223 | */ | |
224 | ||
fa019772 RW |
225 | static int setup_frame(struct ksignal *ksig, sigset_t *set, |
226 | struct pt_regs *regs) | |
1da177e4 LT |
227 | { |
228 | struct sigframe __user *frame; | |
229 | unsigned long return_ip; | |
230 | int err = 0; | |
231 | ||
8215ade8 | 232 | frame = get_sigframe(ksig, sizeof(*frame)); |
1da177e4 LT |
233 | |
234 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
fa019772 | 235 | return -EFAULT; |
1da177e4 LT |
236 | |
237 | err |= setup_sigcontext(&frame->sc, regs, set->sig[0]); | |
238 | if (err) | |
fa019772 | 239 | return -EFAULT; |
1da177e4 LT |
240 | |
241 | if (_NSIG_WORDS > 1) { | |
242 | err |= __copy_to_user(frame->extramask, &set->sig[1], | |
243 | sizeof(frame->extramask)); | |
244 | } | |
245 | if (err) | |
fa019772 | 246 | return -EFAULT; |
1da177e4 LT |
247 | |
248 | /* Set up to return from userspace. If provided, use a stub | |
249 | already in userspace. */ | |
fa019772 RW |
250 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
251 | return_ip = (unsigned long)ksig->ka.sa.sa_restorer; | |
1da177e4 LT |
252 | } else { |
253 | /* trampoline - the desired return ip is the retcode itself */ | |
254 | return_ip = (unsigned long)&frame->retcode; | |
255 | /* This is movu.w __NR_sigreturn, r9; break 13; */ | |
256 | err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0)); | |
257 | err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2)); | |
258 | err |= __put_user(0xe93d, (short __user*)(frame->retcode+4)); | |
259 | } | |
260 | ||
261 | if (err) | |
fa019772 | 262 | return -EFAULT; |
1da177e4 LT |
263 | |
264 | /* Set up registers for signal handler */ | |
265 | ||
fa019772 | 266 | regs->irp = (unsigned long) ksig->ka.sa.sa_handler; /* what we enter NOW */ |
1da177e4 | 267 | regs->srp = return_ip; /* what we enter LATER */ |
fa019772 | 268 | regs->r10 = ksig->sig; /* first argument is signo */ |
1da177e4 LT |
269 | |
270 | /* actually move the usp to reflect the stacked frame */ | |
271 | ||
272 | wrusp((unsigned long)frame); | |
273 | ||
a4858d4d | 274 | return 0; |
1da177e4 LT |
275 | } |
276 | ||
fa019772 RW |
277 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
278 | struct pt_regs *regs) | |
1da177e4 LT |
279 | { |
280 | struct rt_sigframe __user *frame; | |
281 | unsigned long return_ip; | |
282 | int err = 0; | |
283 | ||
8215ade8 | 284 | frame = get_sigframe(ksig, sizeof(*frame)); |
1da177e4 LT |
285 | |
286 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
fa019772 | 287 | return -EFAULT; |
1da177e4 LT |
288 | |
289 | err |= __put_user(&frame->info, &frame->pinfo); | |
290 | err |= __put_user(&frame->uc, &frame->puc); | |
fa019772 | 291 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
1da177e4 | 292 | if (err) |
fa019772 | 293 | return -EFAULT; |
1da177e4 LT |
294 | |
295 | /* Clear all the bits of the ucontext we don't use. */ | |
296 | err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext)); | |
297 | ||
298 | err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]); | |
299 | ||
300 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | |
301 | ||
9df794d9 AV |
302 | err |= __save_altstack(&frame->uc.uc_stack, rdusp()); |
303 | ||
1da177e4 | 304 | if (err) |
fa019772 | 305 | return -EFAULT; |
1da177e4 LT |
306 | |
307 | /* Set up to return from userspace. If provided, use a stub | |
308 | already in userspace. */ | |
fa019772 RW |
309 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
310 | return_ip = (unsigned long)ksig->ka.sa.sa_restorer; | |
1da177e4 LT |
311 | } else { |
312 | /* trampoline - the desired return ip is the retcode itself */ | |
313 | return_ip = (unsigned long)&frame->retcode; | |
314 | /* This is movu.w __NR_rt_sigreturn, r9; break 13; */ | |
a4858d4d JN |
315 | err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0)); |
316 | err |= __put_user(__NR_rt_sigreturn, | |
317 | (short __user *)(frame->retcode+2)); | |
318 | err |= __put_user(0xe93d, (short __user *)(frame->retcode+4)); | |
1da177e4 LT |
319 | } |
320 | ||
321 | if (err) | |
fa019772 | 322 | return -EFAULT; |
1da177e4 | 323 | |
1da177e4 LT |
324 | /* Set up registers for signal handler */ |
325 | ||
a4858d4d | 326 | /* What we enter NOW */ |
fa019772 | 327 | regs->irp = (unsigned long) ksig->ka.sa.sa_handler; |
a4858d4d JN |
328 | /* What we enter LATER */ |
329 | regs->srp = return_ip; | |
330 | /* First argument is signo */ | |
fa019772 | 331 | regs->r10 = ksig->sig; |
a4858d4d JN |
332 | /* Second argument is (siginfo_t *) */ |
333 | regs->r11 = (unsigned long)&frame->info; | |
334 | /* Third argument is unused */ | |
335 | regs->r12 = 0; | |
336 | ||
337 | /* Actually move the usp to reflect the stacked frame */ | |
1da177e4 LT |
338 | wrusp((unsigned long)frame); |
339 | ||
a4858d4d | 340 | return 0; |
1da177e4 LT |
341 | } |
342 | ||
343 | /* | |
344 | * OK, we're invoking a handler | |
a4858d4d | 345 | */ |
1da177e4 | 346 | |
fa019772 RW |
347 | static inline void handle_signal(int canrestart, struct ksignal *ksig, |
348 | struct pt_regs *regs) | |
1da177e4 | 349 | { |
b7f9a11a | 350 | sigset_t *oldset = sigmask_to_save(); |
a4858d4d JN |
351 | int ret; |
352 | ||
1da177e4 LT |
353 | /* Are we from a system call? */ |
354 | if (canrestart) { | |
355 | /* If so, check system call restarting.. */ | |
356 | switch (regs->r10) { | |
a4858d4d JN |
357 | case -ERESTART_RESTARTBLOCK: |
358 | case -ERESTARTNOHAND: | |
359 | /* ERESTARTNOHAND means that the syscall should | |
360 | * only be restarted if there was no handler for | |
361 | * the signal, and since we only get here if there | |
362 | * is a handler, we don't restart */ | |
363 | regs->r10 = -EINTR; | |
364 | break; | |
365 | case -ERESTARTSYS: | |
366 | /* ERESTARTSYS means to restart the syscall if | |
367 | * there is no handler or the handler was | |
368 | * registered with SA_RESTART */ | |
fa019772 | 369 | if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { |
1da177e4 LT |
370 | regs->r10 = -EINTR; |
371 | break; | |
a4858d4d JN |
372 | } |
373 | /* fallthrough */ | |
374 | case -ERESTARTNOINTR: | |
375 | /* ERESTARTNOINTR means that the syscall should | |
376 | * be called again after the signal handler returns. */ | |
377 | RESTART_CRIS_SYS(regs); | |
1da177e4 LT |
378 | } |
379 | } | |
380 | ||
381 | /* Set up the stack frame */ | |
fa019772 RW |
382 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
383 | ret = setup_rt_frame(ksig, oldset, regs); | |
1da177e4 | 384 | else |
fa019772 | 385 | ret = setup_frame(ksig, oldset, regs); |
a4858d4d | 386 | |
fa019772 | 387 | signal_setup_done(ret, ksig, 0); |
1da177e4 LT |
388 | } |
389 | ||
390 | /* | |
391 | * Note that 'init' is a special process: it doesn't get signals it doesn't | |
392 | * want to handle. Thus you cannot kill init even with a SIGKILL even by | |
393 | * mistake. | |
394 | * | |
395 | * Also note that the regs structure given here as an argument, is the latest | |
396 | * pushed pt_regs. It may or may not be the same as the first pushed registers | |
397 | * when the initial usermode->kernelmode transition took place. Therefore | |
398 | * we can use user_mode(regs) to see if we came directly from kernel or user | |
399 | * mode below. | |
400 | */ | |
401 | ||
a4858d4d | 402 | void do_signal(int canrestart, struct pt_regs *regs) |
1da177e4 | 403 | { |
fa019772 | 404 | struct ksignal ksig; |
1da177e4 LT |
405 | |
406 | /* | |
407 | * We want the common case to go fast, which | |
408 | * is why we may in certain cases get here from | |
409 | * kernel mode. Just return without doing anything | |
410 | * if so. | |
411 | */ | |
412 | if (!user_mode(regs)) | |
a4858d4d | 413 | return; |
1da177e4 | 414 | |
fa019772 | 415 | if (get_signal(&ksig)) { |
1da177e4 | 416 | /* Whee! Actually deliver the signal. */ |
fa019772 | 417 | handle_signal(canrestart, &ksig, regs); |
a4858d4d | 418 | return; |
1da177e4 LT |
419 | } |
420 | ||
421 | /* Did we come from a system call? */ | |
422 | if (canrestart) { | |
423 | /* Restart the system call - no handlers present */ | |
424 | if (regs->r10 == -ERESTARTNOHAND || | |
425 | regs->r10 == -ERESTARTSYS || | |
426 | regs->r10 == -ERESTARTNOINTR) { | |
427 | RESTART_CRIS_SYS(regs); | |
428 | } | |
a4858d4d | 429 | if (regs->r10 == -ERESTART_RESTARTBLOCK) { |
33dc0ad7 | 430 | regs->r9 = __NR_restart_syscall; |
1da177e4 LT |
431 | regs->irp -= 2; |
432 | } | |
433 | } | |
a4858d4d JN |
434 | |
435 | /* if there's no signal to deliver, we just put the saved sigmask | |
436 | * back */ | |
51a7b448 | 437 | restore_saved_sigmask(); |
1da177e4 | 438 | } |