Merge branches 'x86-fixes-for-linus' and 'x86-uv-for-linus' of git://git.kernel.org...
[deliverable/linux.git] / arch / tile / kernel / compat_signal.c
1 /*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/kernel.h>
20 #include <linux/signal.h>
21 #include <linux/errno.h>
22 #include <linux/wait.h>
23 #include <linux/unistd.h>
24 #include <linux/stddef.h>
25 #include <linux/personality.h>
26 #include <linux/suspend.h>
27 #include <linux/ptrace.h>
28 #include <linux/elf.h>
29 #include <linux/compat.h>
30 #include <linux/syscalls.h>
31 #include <linux/uaccess.h>
32 #include <asm/processor.h>
33 #include <asm/ucontext.h>
34 #include <asm/sigframe.h>
35 #include <asm/syscalls.h>
36 #include <arch/interrupts.h>
37
38 struct compat_sigaction {
39 compat_uptr_t sa_handler;
40 compat_ulong_t sa_flags;
41 compat_uptr_t sa_restorer;
42 sigset_t sa_mask __packed;
43 };
44
45 struct compat_sigaltstack {
46 compat_uptr_t ss_sp;
47 int ss_flags;
48 compat_size_t ss_size;
49 };
50
51 struct compat_ucontext {
52 compat_ulong_t uc_flags;
53 compat_uptr_t uc_link;
54 struct compat_sigaltstack uc_stack;
55 struct sigcontext uc_mcontext;
56 sigset_t uc_sigmask; /* mask last for extensibility */
57 };
58
59 #define COMPAT_SI_PAD_SIZE ((SI_MAX_SIZE - 3 * sizeof(int)) / sizeof(int))
60
61 struct compat_siginfo {
62 int si_signo;
63 int si_errno;
64 int si_code;
65
66 union {
67 int _pad[COMPAT_SI_PAD_SIZE];
68
69 /* kill() */
70 struct {
71 unsigned int _pid; /* sender's pid */
72 unsigned int _uid; /* sender's uid */
73 } _kill;
74
75 /* POSIX.1b timers */
76 struct {
77 compat_timer_t _tid; /* timer id */
78 int _overrun; /* overrun count */
79 compat_sigval_t _sigval; /* same as below */
80 int _sys_private; /* not to be passed to user */
81 int _overrun_incr; /* amount to add to overrun */
82 } _timer;
83
84 /* POSIX.1b signals */
85 struct {
86 unsigned int _pid; /* sender's pid */
87 unsigned int _uid; /* sender's uid */
88 compat_sigval_t _sigval;
89 } _rt;
90
91 /* SIGCHLD */
92 struct {
93 unsigned int _pid; /* which child */
94 unsigned int _uid; /* sender's uid */
95 int _status; /* exit code */
96 compat_clock_t _utime;
97 compat_clock_t _stime;
98 } _sigchld;
99
100 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
101 struct {
102 unsigned int _addr; /* faulting insn/memory ref. */
103 #ifdef __ARCH_SI_TRAPNO
104 int _trapno; /* TRAP # which caused the signal */
105 #endif
106 } _sigfault;
107
108 /* SIGPOLL */
109 struct {
110 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
111 int _fd;
112 } _sigpoll;
113 } _sifields;
114 };
115
116 struct compat_rt_sigframe {
117 unsigned char save_area[C_ABI_SAVE_AREA_SIZE]; /* caller save area */
118 struct compat_siginfo info;
119 struct compat_ucontext uc;
120 };
121
122 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
123
124 long compat_sys_rt_sigaction(int sig, struct compat_sigaction __user *act,
125 struct compat_sigaction __user *oact,
126 size_t sigsetsize)
127 {
128 struct k_sigaction new_sa, old_sa;
129 int ret = -EINVAL;
130
131 /* XXX: Don't preclude handling different sized sigset_t's. */
132 if (sigsetsize != sizeof(sigset_t))
133 goto out;
134
135 if (act) {
136 compat_uptr_t handler, restorer;
137
138 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
139 __get_user(handler, &act->sa_handler) ||
140 __get_user(new_sa.sa.sa_flags, &act->sa_flags) ||
141 __get_user(restorer, &act->sa_restorer) ||
142 __copy_from_user(&new_sa.sa.sa_mask, &act->sa_mask,
143 sizeof(sigset_t)))
144 return -EFAULT;
145 new_sa.sa.sa_handler = compat_ptr(handler);
146 new_sa.sa.sa_restorer = compat_ptr(restorer);
147 }
148
149 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
150
151 if (!ret && oact) {
152 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
153 __put_user(ptr_to_compat(old_sa.sa.sa_handler),
154 &oact->sa_handler) ||
155 __put_user(ptr_to_compat(old_sa.sa.sa_restorer),
156 &oact->sa_restorer) ||
157 __put_user(old_sa.sa.sa_flags, &oact->sa_flags) ||
158 __copy_to_user(&oact->sa_mask, &old_sa.sa.sa_mask,
159 sizeof(sigset_t)))
160 return -EFAULT;
161 }
162 out:
163 return ret;
164 }
165
166 long compat_sys_rt_sigqueueinfo(int pid, int sig,
167 struct compat_siginfo __user *uinfo)
168 {
169 siginfo_t info;
170 int ret;
171 mm_segment_t old_fs = get_fs();
172
173 if (copy_siginfo_from_user32(&info, uinfo))
174 return -EFAULT;
175 set_fs(KERNEL_DS);
176 ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __force __user *)&info);
177 set_fs(old_fs);
178 return ret;
179 }
180
181 int copy_siginfo_to_user32(struct compat_siginfo __user *to, siginfo_t *from)
182 {
183 int err;
184
185 if (!access_ok(VERIFY_WRITE, to, sizeof(struct compat_siginfo)))
186 return -EFAULT;
187
188 /* If you change siginfo_t structure, please make sure that
189 this code is fixed accordingly.
190 It should never copy any pad contained in the structure
191 to avoid security leaks, but must copy the generic
192 3 ints plus the relevant union member. */
193 err = __put_user(from->si_signo, &to->si_signo);
194 err |= __put_user(from->si_errno, &to->si_errno);
195 err |= __put_user((short)from->si_code, &to->si_code);
196
197 if (from->si_code < 0) {
198 err |= __put_user(from->si_pid, &to->si_pid);
199 err |= __put_user(from->si_uid, &to->si_uid);
200 err |= __put_user(ptr_to_compat(from->si_ptr), &to->si_ptr);
201 } else {
202 /*
203 * First 32bits of unions are always present:
204 * si_pid === si_band === si_tid === si_addr(LS half)
205 */
206 err |= __put_user(from->_sifields._pad[0],
207 &to->_sifields._pad[0]);
208 switch (from->si_code >> 16) {
209 case __SI_FAULT >> 16:
210 break;
211 case __SI_CHLD >> 16:
212 err |= __put_user(from->si_utime, &to->si_utime);
213 err |= __put_user(from->si_stime, &to->si_stime);
214 err |= __put_user(from->si_status, &to->si_status);
215 /* FALL THROUGH */
216 default:
217 case __SI_KILL >> 16:
218 err |= __put_user(from->si_uid, &to->si_uid);
219 break;
220 case __SI_POLL >> 16:
221 err |= __put_user(from->si_fd, &to->si_fd);
222 break;
223 case __SI_TIMER >> 16:
224 err |= __put_user(from->si_overrun, &to->si_overrun);
225 err |= __put_user(ptr_to_compat(from->si_ptr),
226 &to->si_ptr);
227 break;
228 /* This is not generated by the kernel as of now. */
229 case __SI_RT >> 16:
230 case __SI_MESGQ >> 16:
231 err |= __put_user(from->si_uid, &to->si_uid);
232 err |= __put_user(from->si_int, &to->si_int);
233 break;
234 }
235 }
236 return err;
237 }
238
239 int copy_siginfo_from_user32(siginfo_t *to, struct compat_siginfo __user *from)
240 {
241 int err;
242 u32 ptr32;
243
244 if (!access_ok(VERIFY_READ, from, sizeof(struct compat_siginfo)))
245 return -EFAULT;
246
247 err = __get_user(to->si_signo, &from->si_signo);
248 err |= __get_user(to->si_errno, &from->si_errno);
249 err |= __get_user(to->si_code, &from->si_code);
250
251 err |= __get_user(to->si_pid, &from->si_pid);
252 err |= __get_user(to->si_uid, &from->si_uid);
253 err |= __get_user(ptr32, &from->si_ptr);
254 to->si_ptr = compat_ptr(ptr32);
255
256 return err;
257 }
258
259 long compat_sys_sigaltstack(const struct compat_sigaltstack __user *uss_ptr,
260 struct compat_sigaltstack __user *uoss_ptr,
261 struct pt_regs *regs)
262 {
263 stack_t uss, uoss;
264 int ret;
265 mm_segment_t seg;
266
267 if (uss_ptr) {
268 u32 ptr;
269
270 memset(&uss, 0, sizeof(stack_t));
271 if (!access_ok(VERIFY_READ, uss_ptr, sizeof(*uss_ptr)) ||
272 __get_user(ptr, &uss_ptr->ss_sp) ||
273 __get_user(uss.ss_flags, &uss_ptr->ss_flags) ||
274 __get_user(uss.ss_size, &uss_ptr->ss_size))
275 return -EFAULT;
276 uss.ss_sp = compat_ptr(ptr);
277 }
278 seg = get_fs();
279 set_fs(KERNEL_DS);
280 ret = do_sigaltstack(uss_ptr ? (stack_t __user __force *)&uss : NULL,
281 (stack_t __user __force *)&uoss,
282 (unsigned long)compat_ptr(regs->sp));
283 set_fs(seg);
284 if (ret >= 0 && uoss_ptr) {
285 if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(*uoss_ptr)) ||
286 __put_user(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp) ||
287 __put_user(uoss.ss_flags, &uoss_ptr->ss_flags) ||
288 __put_user(uoss.ss_size, &uoss_ptr->ss_size))
289 ret = -EFAULT;
290 }
291 return ret;
292 }
293
294 long compat_sys_rt_sigreturn(struct pt_regs *regs)
295 {
296 struct compat_rt_sigframe __user *frame =
297 (struct compat_rt_sigframe __user *) compat_ptr(regs->sp);
298 sigset_t set;
299 long r0;
300
301 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
302 goto badframe;
303 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
304 goto badframe;
305
306 sigdelsetmask(&set, ~_BLOCKABLE);
307 spin_lock_irq(&current->sighand->siglock);
308 current->blocked = set;
309 recalc_sigpending();
310 spin_unlock_irq(&current->sighand->siglock);
311
312 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
313 goto badframe;
314
315 if (compat_sys_sigaltstack(&frame->uc.uc_stack, NULL, regs) != 0)
316 goto badframe;
317
318 return r0;
319
320 badframe:
321 force_sig(SIGSEGV, current);
322 return 0;
323 }
324
325 /*
326 * Determine which stack to use..
327 */
328 static inline void __user *compat_get_sigframe(struct k_sigaction *ka,
329 struct pt_regs *regs,
330 size_t frame_size)
331 {
332 unsigned long sp;
333
334 /* Default to using normal stack */
335 sp = (unsigned long)compat_ptr(regs->sp);
336
337 /*
338 * If we are on the alternate signal stack and would overflow
339 * it, don't. Return an always-bogus address instead so we
340 * will die with SIGSEGV.
341 */
342 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
343 return (void __user __force *)-1UL;
344
345 /* This is the X/Open sanctioned signal stack switching. */
346 if (ka->sa.sa_flags & SA_ONSTACK) {
347 if (sas_ss_flags(sp) == 0)
348 sp = current->sas_ss_sp + current->sas_ss_size;
349 }
350
351 sp -= frame_size;
352 /*
353 * Align the stack pointer according to the TILE ABI,
354 * i.e. so that on function entry (sp & 15) == 0.
355 */
356 sp &= -16UL;
357 return (void __user *) sp;
358 }
359
360 int compat_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
361 sigset_t *set, struct pt_regs *regs)
362 {
363 unsigned long restorer;
364 struct compat_rt_sigframe __user *frame;
365 int err = 0;
366 int usig;
367
368 frame = compat_get_sigframe(ka, regs, sizeof(*frame));
369
370 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
371 goto give_sigsegv;
372
373 usig = current_thread_info()->exec_domain
374 && current_thread_info()->exec_domain->signal_invmap
375 && sig < 32
376 ? current_thread_info()->exec_domain->signal_invmap[sig]
377 : sig;
378
379 /* Always write at least the signal number for the stack backtracer. */
380 if (ka->sa.sa_flags & SA_SIGINFO) {
381 /* At sigreturn time, restore the callee-save registers too. */
382 err |= copy_siginfo_to_user32(&frame->info, info);
383 regs->flags |= PT_FLAGS_RESTORE_REGS;
384 } else {
385 err |= __put_user(info->si_signo, &frame->info.si_signo);
386 }
387
388 /* Create the ucontext. */
389 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
390 err |= __put_user(0, &frame->uc.uc_flags);
391 err |= __put_user(0, &frame->uc.uc_link);
392 err |= __put_user(ptr_to_compat((void *)(current->sas_ss_sp)),
393 &frame->uc.uc_stack.ss_sp);
394 err |= __put_user(sas_ss_flags(regs->sp),
395 &frame->uc.uc_stack.ss_flags);
396 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
397 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
398 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
399 if (err)
400 goto give_sigsegv;
401
402 restorer = VDSO_BASE;
403 if (ka->sa.sa_flags & SA_RESTORER)
404 restorer = ptr_to_compat_reg(ka->sa.sa_restorer);
405
406 /*
407 * Set up registers for signal handler.
408 * Registers that we don't modify keep the value they had from
409 * user-space at the time we took the signal.
410 */
411 regs->pc = ptr_to_compat_reg(ka->sa.sa_handler);
412 regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
413 regs->sp = ptr_to_compat_reg(frame);
414 regs->lr = restorer;
415 regs->regs[0] = (unsigned long) usig;
416
417 if (ka->sa.sa_flags & SA_SIGINFO) {
418 /* Need extra arguments, so mark to restore caller-saves. */
419 regs->regs[1] = ptr_to_compat_reg(&frame->info);
420 regs->regs[2] = ptr_to_compat_reg(&frame->uc);
421 regs->flags |= PT_FLAGS_CALLER_SAVES;
422 }
423
424 /*
425 * Notify any tracer that was single-stepping it.
426 * The tracer may want to single-step inside the
427 * handler too.
428 */
429 if (test_thread_flag(TIF_SINGLESTEP))
430 ptrace_notify(SIGTRAP);
431
432 return 0;
433
434 give_sigsegv:
435 force_sigsegv(sig, current);
436 return -EFAULT;
437 }
This page took 0.039525 seconds and 6 git commands to generate.