]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/s390/kernel/signal.c
s390/compat,signal: change return values to -EFAULT
[karo-tx-linux.git] / arch / s390 / kernel / signal.c
1 /*
2  *    Copyright IBM Corp. 1999, 2006
3  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
4  *
5  *    Based on Intel version
6  * 
7  *  Copyright (C) 1991, 1992  Linus Torvalds
8  *
9  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
10  */
11
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/personality.h>
24 #include <linux/binfmts.h>
25 #include <linux/tracehook.h>
26 #include <linux/syscalls.h>
27 #include <linux/compat.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/lowcore.h>
31 #include <asm/switch_to.h>
32 #include "entry.h"
33
34 typedef struct 
35 {
36         __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
37         struct sigcontext sc;
38         _sigregs sregs;
39         int signo;
40         __u8 retcode[S390_SYSCALL_SIZE];
41 } sigframe;
42
43 typedef struct 
44 {
45         __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
46         __u8 retcode[S390_SYSCALL_SIZE];
47         struct siginfo info;
48         struct ucontext uc;
49 } rt_sigframe;
50
51 /* Returns non-zero on fault. */
52 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
53 {
54         _sigregs user_sregs;
55
56         save_access_regs(current->thread.acrs);
57
58         /* Copy a 'clean' PSW mask to the user to avoid leaking
59            information about whether PER is currently on.  */
60         user_sregs.regs.psw.mask = PSW_USER_BITS |
61                 (regs->psw.mask & PSW_MASK_USER);
62         user_sregs.regs.psw.addr = regs->psw.addr;
63         memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
64         memcpy(&user_sregs.regs.acrs, current->thread.acrs,
65                sizeof(sregs->regs.acrs));
66         /* 
67          * We have to store the fp registers to current->thread.fp_regs
68          * to merge them with the emulated registers.
69          */
70         save_fp_regs(&current->thread.fp_regs);
71         memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
72                sizeof(s390_fp_regs));
73         if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs)))
74                 return -EFAULT;
75         return 0;
76 }
77
78 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
79 {
80         _sigregs user_sregs;
81
82         /* Alwys make any pending restarted system call return -EINTR */
83         current_thread_info()->restart_block.fn = do_no_restart_syscall;
84
85         if (__copy_from_user(&user_sregs, sregs, sizeof(_sigregs)))
86                 return -EFAULT;
87         /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
88         regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
89                 (user_sregs.regs.psw.mask & PSW_MASK_USER);
90         /* Check for invalid user address space control. */
91         if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
92                 regs->psw.mask = PSW_ASC_PRIMARY |
93                         (regs->psw.mask & ~PSW_MASK_ASC);
94         /* Check for invalid amode */
95         if (regs->psw.mask & PSW_MASK_EA)
96                 regs->psw.mask |= PSW_MASK_BA;
97         regs->psw.addr = user_sregs.regs.psw.addr;
98         memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
99         memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
100                sizeof(sregs->regs.acrs));
101         restore_access_regs(current->thread.acrs);
102
103         memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
104                sizeof(s390_fp_regs));
105         current->thread.fp_regs.fpc &= FPC_VALID_MASK;
106
107         restore_fp_regs(&current->thread.fp_regs);
108         clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */
109         return 0;
110 }
111
112 SYSCALL_DEFINE0(sigreturn)
113 {
114         struct pt_regs *regs = task_pt_regs(current);
115         sigframe __user *frame = (sigframe __user *)regs->gprs[15];
116         sigset_t set;
117
118         if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
119                 goto badframe;
120         set_current_blocked(&set);
121         if (restore_sigregs(regs, &frame->sregs))
122                 goto badframe;
123         return regs->gprs[2];
124 badframe:
125         force_sig(SIGSEGV, current);
126         return 0;
127 }
128
129 SYSCALL_DEFINE0(rt_sigreturn)
130 {
131         struct pt_regs *regs = task_pt_regs(current);
132         rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
133         sigset_t set;
134
135         if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
136                 goto badframe;
137         set_current_blocked(&set);
138         if (restore_sigregs(regs, &frame->uc.uc_mcontext))
139                 goto badframe;
140         if (restore_altstack(&frame->uc.uc_stack))
141                 goto badframe;
142         return regs->gprs[2];
143 badframe:
144         force_sig(SIGSEGV, current);
145         return 0;
146 }
147
148 /*
149  * Set up a signal frame.
150  */
151
152
153 /*
154  * Determine which stack to use..
155  */
156 static inline void __user *
157 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
158 {
159         unsigned long sp;
160
161         /* Default to using normal stack */
162         sp = regs->gprs[15];
163
164         /* Overflow on alternate signal stack gives SIGSEGV. */
165         if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
166                 return (void __user *) -1UL;
167
168         /* This is the X/Open sanctioned signal stack switching.  */
169         if (ka->sa.sa_flags & SA_ONSTACK) {
170                 if (! sas_ss_flags(sp))
171                         sp = current->sas_ss_sp + current->sas_ss_size;
172         }
173
174         return (void __user *)((sp - frame_size) & -8ul);
175 }
176
177 static inline int map_signal(int sig)
178 {
179         if (current_thread_info()->exec_domain
180             && current_thread_info()->exec_domain->signal_invmap
181             && sig < 32)
182                 return current_thread_info()->exec_domain->signal_invmap[sig];
183         else
184                 return sig;
185 }
186
187 static int setup_frame(int sig, struct k_sigaction *ka,
188                        sigset_t *set, struct pt_regs * regs)
189 {
190         sigframe __user *frame;
191
192         frame = get_sigframe(ka, regs, sizeof(sigframe));
193
194         if (frame == (void __user *) -1UL)
195                 goto give_sigsegv;
196
197         if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
198                 goto give_sigsegv;
199
200         if (save_sigregs(regs, &frame->sregs))
201                 goto give_sigsegv;
202         if (__put_user(&frame->sregs, &frame->sc.sregs))
203                 goto give_sigsegv;
204
205         /* Set up to return from userspace.  If provided, use a stub
206            already in userspace.  */
207         if (ka->sa.sa_flags & SA_RESTORER) {
208                 regs->gprs[14] = (unsigned long)
209                         ka->sa.sa_restorer | PSW_ADDR_AMODE;
210         } else {
211                 regs->gprs[14] = (unsigned long)
212                         frame->retcode | PSW_ADDR_AMODE;
213                 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
214                                (u16 __user *)(frame->retcode)))
215                         goto give_sigsegv;
216         }
217
218         /* Set up backchain. */
219         if (__put_user(regs->gprs[15], (addr_t __user *) frame))
220                 goto give_sigsegv;
221
222         /* Set up registers for signal handler */
223         regs->gprs[15] = (unsigned long) frame;
224         /* Force default amode and default user address space control. */
225         regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
226                 (PSW_USER_BITS & PSW_MASK_ASC) |
227                 (regs->psw.mask & ~PSW_MASK_ASC);
228         regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
229
230         regs->gprs[2] = map_signal(sig);
231         regs->gprs[3] = (unsigned long) &frame->sc;
232
233         /* We forgot to include these in the sigcontext.
234            To avoid breaking binary compatibility, they are passed as args. */
235         if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
236             sig == SIGTRAP || sig == SIGFPE) {
237                 /* set extra registers only for synchronous signals */
238                 regs->gprs[4] = regs->int_code & 127;
239                 regs->gprs[5] = regs->int_parm_long;
240                 regs->gprs[6] = task_thread_info(current)->last_break;
241         }
242
243         /* Place signal number on stack to allow backtrace from handler.  */
244         if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
245                 goto give_sigsegv;
246         return 0;
247
248 give_sigsegv:
249         force_sigsegv(sig, current);
250         return -EFAULT;
251 }
252
253 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
254                            sigset_t *set, struct pt_regs * regs)
255 {
256         int err = 0;
257         rt_sigframe __user *frame;
258
259         frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
260
261         if (frame == (void __user *) -1UL)
262                 goto give_sigsegv;
263
264         if (copy_siginfo_to_user(&frame->info, info))
265                 goto give_sigsegv;
266
267         /* Create the ucontext.  */
268         err |= __put_user(0, &frame->uc.uc_flags);
269         err |= __put_user(NULL, &frame->uc.uc_link);
270         err |= __save_altstack(&frame->uc.uc_stack, regs->gprs[15]);
271         err |= save_sigregs(regs, &frame->uc.uc_mcontext);
272         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
273         if (err)
274                 goto give_sigsegv;
275
276         /* Set up to return from userspace.  If provided, use a stub
277            already in userspace.  */
278         if (ka->sa.sa_flags & SA_RESTORER) {
279                 regs->gprs[14] = (unsigned long)
280                         ka->sa.sa_restorer | PSW_ADDR_AMODE;
281         } else {
282                 regs->gprs[14] = (unsigned long)
283                         frame->retcode | PSW_ADDR_AMODE;
284                 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
285                                (u16 __user *)(frame->retcode)))
286                         goto give_sigsegv;
287         }
288
289         /* Set up backchain. */
290         if (__put_user(regs->gprs[15], (addr_t __user *) frame))
291                 goto give_sigsegv;
292
293         /* Set up registers for signal handler */
294         regs->gprs[15] = (unsigned long) frame;
295         /* Force default amode and default user address space control. */
296         regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
297                 (PSW_USER_BITS & PSW_MASK_ASC) |
298                 (regs->psw.mask & ~PSW_MASK_ASC);
299         regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
300
301         regs->gprs[2] = map_signal(sig);
302         regs->gprs[3] = (unsigned long) &frame->info;
303         regs->gprs[4] = (unsigned long) &frame->uc;
304         regs->gprs[5] = task_thread_info(current)->last_break;
305         return 0;
306
307 give_sigsegv:
308         force_sigsegv(sig, current);
309         return -EFAULT;
310 }
311
312 static void handle_signal(unsigned long sig, struct k_sigaction *ka,
313                          siginfo_t *info, sigset_t *oldset,
314                          struct pt_regs *regs)
315 {
316         int ret;
317
318         /* Set up the stack frame */
319         if (ka->sa.sa_flags & SA_SIGINFO)
320                 ret = setup_rt_frame(sig, ka, info, oldset, regs);
321         else
322                 ret = setup_frame(sig, ka, oldset, regs);
323         if (ret)
324                 return;
325         signal_delivered(sig, info, ka, regs,
326                                  test_thread_flag(TIF_SINGLE_STEP));
327 }
328
329 /*
330  * Note that 'init' is a special process: it doesn't get signals it doesn't
331  * want to handle. Thus you cannot kill init even with a SIGKILL even by
332  * mistake.
333  *
334  * Note that we go through the signals twice: once to check the signals that
335  * the kernel can handle, and then we build all the user-level signal handling
336  * stack-frames in one go after that.
337  */
338 void do_signal(struct pt_regs *regs)
339 {
340         siginfo_t info;
341         int signr;
342         struct k_sigaction ka;
343         sigset_t *oldset = sigmask_to_save();
344
345         /*
346          * Get signal to deliver. When running under ptrace, at this point
347          * the debugger may change all our registers, including the system
348          * call information.
349          */
350         current_thread_info()->system_call =
351                 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
352         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
353
354         if (signr > 0) {
355                 /* Whee!  Actually deliver the signal.  */
356                 if (current_thread_info()->system_call) {
357                         regs->int_code = current_thread_info()->system_call;
358                         /* Check for system call restarting. */
359                         switch (regs->gprs[2]) {
360                         case -ERESTART_RESTARTBLOCK:
361                         case -ERESTARTNOHAND:
362                                 regs->gprs[2] = -EINTR;
363                                 break;
364                         case -ERESTARTSYS:
365                                 if (!(ka.sa.sa_flags & SA_RESTART)) {
366                                         regs->gprs[2] = -EINTR;
367                                         break;
368                                 }
369                         /* fallthrough */
370                         case -ERESTARTNOINTR:
371                                 regs->gprs[2] = regs->orig_gpr2;
372                                 regs->psw.addr =
373                                         __rewind_psw(regs->psw,
374                                                      regs->int_code >> 16);
375                                 break;
376                         }
377                 }
378                 /* No longer in a system call */
379                 clear_thread_flag(TIF_SYSCALL);
380
381                 if (is_compat_task())
382                         handle_signal32(signr, &ka, &info, oldset, regs);
383                 else
384                         handle_signal(signr, &ka, &info, oldset, regs);
385                 return;
386         }
387
388         /* No handlers present - check for system call restart */
389         clear_thread_flag(TIF_SYSCALL);
390         if (current_thread_info()->system_call) {
391                 regs->int_code = current_thread_info()->system_call;
392                 switch (regs->gprs[2]) {
393                 case -ERESTART_RESTARTBLOCK:
394                         /* Restart with sys_restart_syscall */
395                         regs->int_code = __NR_restart_syscall;
396                 /* fallthrough */
397                 case -ERESTARTNOHAND:
398                 case -ERESTARTSYS:
399                 case -ERESTARTNOINTR:
400                         /* Restart system call with magic TIF bit. */
401                         regs->gprs[2] = regs->orig_gpr2;
402                         set_thread_flag(TIF_SYSCALL);
403                         if (test_thread_flag(TIF_SINGLE_STEP))
404                                 set_thread_flag(TIF_PER_TRAP);
405                         break;
406                 }
407         }
408
409         /*
410          * If there's no signal to deliver, we just put the saved sigmask back.
411          */
412         restore_saved_sigmask();
413 }
414
415 void do_notify_resume(struct pt_regs *regs)
416 {
417         clear_thread_flag(TIF_NOTIFY_RESUME);
418         tracehook_notify_resume(regs);
419 }