2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
15 #include <linux/sched.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 <arch/interrupts.h>
37 struct compat_sigaction {
38 compat_uptr_t sa_handler;
39 compat_ulong_t sa_flags;
40 compat_uptr_t sa_restorer;
41 sigset_t sa_mask; /* mask last for extensibility */
44 struct compat_sigaltstack {
47 compat_size_t ss_size;
50 struct compat_ucontext {
51 compat_ulong_t uc_flags;
52 compat_uptr_t uc_link;
53 struct compat_sigaltstack uc_stack;
54 struct sigcontext uc_mcontext;
55 sigset_t uc_sigmask; /* mask last for extensibility */
58 struct compat_siginfo {
64 int _pad[SI_PAD_SIZE];
68 unsigned int _pid; /* sender's pid */
69 unsigned int _uid; /* sender's uid */
74 compat_timer_t _tid; /* timer id */
75 int _overrun; /* overrun count */
76 compat_sigval_t _sigval; /* same as below */
77 int _sys_private; /* not to be passed to user */
78 int _overrun_incr; /* amount to add to overrun */
81 /* POSIX.1b signals */
83 unsigned int _pid; /* sender's pid */
84 unsigned int _uid; /* sender's uid */
85 compat_sigval_t _sigval;
90 unsigned int _pid; /* which child */
91 unsigned int _uid; /* sender's uid */
92 int _status; /* exit code */
93 compat_clock_t _utime;
94 compat_clock_t _stime;
97 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
99 unsigned int _addr; /* faulting insn/memory ref. */
100 #ifdef __ARCH_SI_TRAPNO
101 int _trapno; /* TRAP # which caused the signal */
107 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
113 struct compat_rt_sigframe {
114 unsigned char save_area[C_ABI_SAVE_AREA_SIZE]; /* caller save area */
115 struct compat_siginfo info;
116 struct compat_ucontext uc;
119 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
121 long compat_sys_rt_sigaction(int sig, struct compat_sigaction __user *act,
122 struct compat_sigaction __user *oact,
125 struct k_sigaction new_sa, old_sa;
128 /* XXX: Don't preclude handling different sized sigset_t's. */
129 if (sigsetsize != sizeof(sigset_t))
133 compat_uptr_t handler, restorer;
135 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
136 __get_user(handler, &act->sa_handler) ||
137 __get_user(new_sa.sa.sa_flags, &act->sa_flags) ||
138 __get_user(restorer, &act->sa_restorer) ||
139 __copy_from_user(&new_sa.sa.sa_mask, &act->sa_mask,
142 new_sa.sa.sa_handler = compat_ptr(handler);
143 new_sa.sa.sa_restorer = compat_ptr(restorer);
146 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
149 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
150 __put_user(ptr_to_compat(old_sa.sa.sa_handler),
151 &oact->sa_handler) ||
152 __put_user(ptr_to_compat(old_sa.sa.sa_restorer),
153 &oact->sa_restorer) ||
154 __put_user(old_sa.sa.sa_flags, &oact->sa_flags) ||
155 __copy_to_user(&oact->sa_mask, &old_sa.sa.sa_mask,
163 long compat_sys_rt_sigqueueinfo(int pid, int sig,
164 struct compat_siginfo __user *uinfo)
168 mm_segment_t old_fs = get_fs();
170 if (copy_siginfo_from_user32(&info, uinfo))
173 ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
178 int copy_siginfo_to_user32(struct compat_siginfo __user *to, siginfo_t *from)
182 if (!access_ok(VERIFY_WRITE, to, sizeof(struct compat_siginfo)))
185 /* If you change siginfo_t structure, please make sure that
186 this code is fixed accordingly.
187 It should never copy any pad contained in the structure
188 to avoid security leaks, but must copy the generic
189 3 ints plus the relevant union member. */
190 err = __put_user(from->si_signo, &to->si_signo);
191 err |= __put_user(from->si_errno, &to->si_errno);
192 err |= __put_user((short)from->si_code, &to->si_code);
194 if (from->si_code < 0) {
195 err |= __put_user(from->si_pid, &to->si_pid);
196 err |= __put_user(from->si_uid, &to->si_uid);
197 err |= __put_user(ptr_to_compat(from->si_ptr), &to->si_ptr);
200 * First 32bits of unions are always present:
201 * si_pid === si_band === si_tid === si_addr(LS half)
203 err |= __put_user(from->_sifields._pad[0],
204 &to->_sifields._pad[0]);
205 switch (from->si_code >> 16) {
206 case __SI_FAULT >> 16:
208 case __SI_CHLD >> 16:
209 err |= __put_user(from->si_utime, &to->si_utime);
210 err |= __put_user(from->si_stime, &to->si_stime);
211 err |= __put_user(from->si_status, &to->si_status);
214 case __SI_KILL >> 16:
215 err |= __put_user(from->si_uid, &to->si_uid);
217 case __SI_POLL >> 16:
218 err |= __put_user(from->si_fd, &to->si_fd);
220 case __SI_TIMER >> 16:
221 err |= __put_user(from->si_overrun, &to->si_overrun);
222 err |= __put_user(ptr_to_compat(from->si_ptr),
225 /* This is not generated by the kernel as of now. */
227 case __SI_MESGQ >> 16:
228 err |= __put_user(from->si_uid, &to->si_uid);
229 err |= __put_user(from->si_int, &to->si_int);
236 int copy_siginfo_from_user32(siginfo_t *to, struct compat_siginfo __user *from)
241 if (!access_ok(VERIFY_READ, from, sizeof(struct compat_siginfo)))
244 err = __get_user(to->si_signo, &from->si_signo);
245 err |= __get_user(to->si_errno, &from->si_errno);
246 err |= __get_user(to->si_code, &from->si_code);
248 err |= __get_user(to->si_pid, &from->si_pid);
249 err |= __get_user(to->si_uid, &from->si_uid);
250 err |= __get_user(ptr32, &from->si_ptr);
251 to->si_ptr = compat_ptr(ptr32);
256 long _compat_sys_sigaltstack(const struct compat_sigaltstack __user *uss_ptr,
257 struct compat_sigaltstack __user *uoss_ptr,
258 struct pt_regs *regs)
267 memset(&uss, 0, sizeof(stack_t));
268 if (!access_ok(VERIFY_READ, uss_ptr, sizeof(*uss_ptr)) ||
269 __get_user(ptr, &uss_ptr->ss_sp) ||
270 __get_user(uss.ss_flags, &uss_ptr->ss_flags) ||
271 __get_user(uss.ss_size, &uss_ptr->ss_size))
273 uss.ss_sp = compat_ptr(ptr);
277 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
278 (unsigned long)compat_ptr(regs->sp));
280 if (ret >= 0 && uoss_ptr) {
281 if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(*uoss_ptr)) ||
282 __put_user(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp) ||
283 __put_user(uoss.ss_flags, &uoss_ptr->ss_flags) ||
284 __put_user(uoss.ss_size, &uoss_ptr->ss_size))
290 long _compat_sys_rt_sigreturn(struct pt_regs *regs)
292 struct compat_rt_sigframe __user *frame =
293 (struct compat_rt_sigframe __user *) compat_ptr(regs->sp);
297 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
299 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
302 sigdelsetmask(&set, ~_BLOCKABLE);
303 spin_lock_irq(¤t->sighand->siglock);
304 current->blocked = set;
306 spin_unlock_irq(¤t->sighand->siglock);
308 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
311 if (_compat_sys_sigaltstack(&frame->uc.uc_stack, NULL, regs) != 0)
317 force_sig(SIGSEGV, current);
322 * Determine which stack to use..
324 static inline void __user *compat_get_sigframe(struct k_sigaction *ka,
325 struct pt_regs *regs,
330 /* Default to using normal stack */
331 sp = (unsigned long)compat_ptr(regs->sp);
334 * If we are on the alternate signal stack and would overflow
335 * it, don't. Return an always-bogus address instead so we
336 * will die with SIGSEGV.
338 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
339 return (void __user *) -1L;
341 /* This is the X/Open sanctioned signal stack switching. */
342 if (ka->sa.sa_flags & SA_ONSTACK) {
343 if (sas_ss_flags(sp) == 0)
344 sp = current->sas_ss_sp + current->sas_ss_size;
349 * Align the stack pointer according to the TILE ABI,
350 * i.e. so that on function entry (sp & 15) == 0.
353 return (void __user *) sp;
356 int compat_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
357 sigset_t *set, struct pt_regs *regs)
359 unsigned long restorer;
360 struct compat_rt_sigframe __user *frame;
364 frame = compat_get_sigframe(ka, regs, sizeof(*frame));
366 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
369 usig = current_thread_info()->exec_domain
370 && current_thread_info()->exec_domain->signal_invmap
372 ? current_thread_info()->exec_domain->signal_invmap[sig]
375 /* Always write at least the signal number for the stack backtracer. */
376 if (ka->sa.sa_flags & SA_SIGINFO) {
377 /* At sigreturn time, restore the callee-save registers too. */
378 err |= copy_siginfo_to_user32(&frame->info, info);
379 regs->flags |= PT_FLAGS_RESTORE_REGS;
381 err |= __put_user(info->si_signo, &frame->info.si_signo);
384 /* Create the ucontext. */
385 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
386 err |= __put_user(0, &frame->uc.uc_flags);
387 err |= __put_user(0, &frame->uc.uc_link);
388 err |= __put_user(ptr_to_compat((void *)(current->sas_ss_sp)),
389 &frame->uc.uc_stack.ss_sp);
390 err |= __put_user(sas_ss_flags(regs->sp),
391 &frame->uc.uc_stack.ss_flags);
392 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
393 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
394 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
398 restorer = VDSO_BASE;
399 if (ka->sa.sa_flags & SA_RESTORER)
400 restorer = ptr_to_compat_reg(ka->sa.sa_restorer);
403 * Set up registers for signal handler.
404 * Registers that we don't modify keep the value they had from
405 * user-space at the time we took the signal.
407 regs->pc = ptr_to_compat_reg(ka->sa.sa_handler);
408 regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
409 regs->sp = ptr_to_compat_reg(frame);
411 regs->regs[0] = (unsigned long) usig;
413 if (ka->sa.sa_flags & SA_SIGINFO) {
414 /* Need extra arguments, so mark to restore caller-saves. */
415 regs->regs[1] = ptr_to_compat_reg(&frame->info);
416 regs->regs[2] = ptr_to_compat_reg(&frame->uc);
417 regs->flags |= PT_FLAGS_CALLER_SAVES;
421 * Notify any tracer that was single-stepping it.
422 * The tracer may want to single-step inside the
425 if (test_thread_flag(TIF_SINGLESTEP))
426 ptrace_notify(SIGTRAP);
431 force_sigsegv(sig, current);