Path: blob/master/arch/cris/arch-v10/kernel/signal.c
15125 views
/*1* linux/arch/cris/kernel/signal.c2*3* Based on arch/i386/kernel/signal.c by4* Copyright (C) 1991, 1992 Linus Torvalds5* 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *6*7* Ideas also taken from arch/arm.8*9* Copyright (C) 2000-2007 Axis Communications AB10*11* Authors: Bjorn Wesen ([email protected])12*13*/1415#include <linux/sched.h>16#include <linux/mm.h>17#include <linux/smp.h>18#include <linux/kernel.h>19#include <linux/signal.h>20#include <linux/errno.h>21#include <linux/wait.h>22#include <linux/ptrace.h>23#include <linux/unistd.h>24#include <linux/stddef.h>2526#include <asm/processor.h>27#include <asm/ucontext.h>28#include <asm/uaccess.h>2930#define DEBUG_SIG 03132#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))3334/* 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 */3637/* We rely on that pc points to the instruction after "break 13", so the38* 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;4142void do_signal(int canrestart, struct pt_regs *regs);4344/*45* Atomically swap in the new signal mask, and wait for a signal. Define46* dummy arguments to be able to reach the regs argument. (Note that this47* arrangement relies on old_sigset_t occupying one register.)48*/49int sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,50long srp, struct pt_regs *regs)51{52mask &= _BLOCKABLE;53spin_lock_irq(¤t->sighand->siglock);54current->saved_sigmask = current->blocked;55siginitset(¤t->blocked, mask);56recalc_sigpending();57spin_unlock_irq(¤t->sighand->siglock);58current->state = TASK_INTERRUPTIBLE;59schedule();60set_thread_flag(TIF_RESTORE_SIGMASK);61return -ERESTARTNOHAND;62}6364int sys_sigaction(int sig, const struct old_sigaction __user *act,65struct old_sigaction *oact)66{67struct k_sigaction new_ka, old_ka;68int ret;6970if (act) {71old_sigset_t mask;72if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||73__get_user(new_ka.sa.sa_handler, &act->sa_handler) ||74__get_user(new_ka.sa.sa_restorer, &act->sa_restorer))75return -EFAULT;76__get_user(new_ka.sa.sa_flags, &act->sa_flags);77__get_user(mask, &act->sa_mask);78siginitset(&new_ka.sa.sa_mask, mask);79}8081ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);8283if (!ret && oact) {84if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||85__put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||86__put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))87return -EFAULT;88__put_user(old_ka.sa.sa_flags, &oact->sa_flags);89__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);90}9192return ret;93}9495int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)96{97return do_sigaltstack(uss, uoss, rdusp());98}99100101/*102* Do a signal return; undo the signal stack.103*/104105struct sigframe {106struct sigcontext sc;107unsigned long extramask[_NSIG_WORDS-1];108unsigned char retcode[8]; /* trampoline code */109};110111struct rt_sigframe {112struct siginfo *pinfo;113void *puc;114struct siginfo info;115struct ucontext uc;116unsigned char retcode[8]; /* trampoline code */117};118119120static int121restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)122{123unsigned int err = 0;124unsigned long old_usp;125126/* Always make any pending restarted system calls return -EINTR */127current_thread_info()->restart_block.fn = do_no_restart_syscall;128129/* restore the regs from &sc->regs (same as sc, since regs is first)130* (sc is already checked for VERIFY_READ since the sigframe was131* checked in sys_sigreturn previously)132*/133134if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))135goto badframe;136137/* make sure the U-flag is set so user-mode cannot fool us */138139regs->dccr |= 1 << 8;140141/* restore the old USP as it was before we stacked the sc etc.142* (we cannot just pop the sigcontext since we aligned the sp and143* stuff after pushing it)144*/145146err |= __get_user(old_usp, &sc->usp);147148wrusp(old_usp);149150/* TODO: the other ports use regs->orig_XX to disable syscall checks151* after this completes, but we don't use that mechanism. maybe we can152* use it now ?153*/154155return err;156157badframe:158return 1;159}160161/* Define dummy arguments to be able to reach the regs argument. */162163asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,164long srp, struct pt_regs *regs)165{166struct sigframe __user *frame = (struct sigframe *)rdusp();167sigset_t set;168169/*170* Since we stacked the signal on a dword boundary,171* then frame should be dword aligned here. If it's172* not, then the user is trying to mess with us.173*/174if (((long)frame) & 3)175goto badframe;176177if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))178goto badframe;179if (__get_user(set.sig[0], &frame->sc.oldmask)180|| (_NSIG_WORDS > 1181&& __copy_from_user(&set.sig[1], frame->extramask,182sizeof(frame->extramask))))183goto badframe;184185sigdelsetmask(&set, ~_BLOCKABLE);186spin_lock_irq(¤t->sighand->siglock);187current->blocked = set;188recalc_sigpending();189spin_unlock_irq(¤t->sighand->siglock);190191if (restore_sigcontext(regs, &frame->sc))192goto badframe;193194/* TODO: SIGTRAP when single-stepping as in arm ? */195196return regs->r10;197198badframe:199force_sig(SIGSEGV, current);200return 0;201}202203/* Define dummy arguments to be able to reach the regs argument. */204205asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,206long mof, long srp, struct pt_regs *regs)207{208struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();209sigset_t set;210211/*212* Since we stacked the signal on a dword boundary,213* then frame should be dword aligned here. If it's214* not, then the user is trying to mess with us.215*/216if (((long)frame) & 3)217goto badframe;218219if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))220goto badframe;221if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))222goto badframe;223224sigdelsetmask(&set, ~_BLOCKABLE);225spin_lock_irq(¤t->sighand->siglock);226current->blocked = set;227recalc_sigpending();228spin_unlock_irq(¤t->sighand->siglock);229230if (restore_sigcontext(regs, &frame->uc.uc_mcontext))231goto badframe;232233if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)234goto badframe;235236return regs->r10;237238badframe:239force_sig(SIGSEGV, current);240return 0;241}242243/*244* Set up a signal frame.245*/246247static int setup_sigcontext(struct sigcontext __user *sc,248struct pt_regs *regs, unsigned long mask)249{250int err = 0;251unsigned long usp = rdusp();252253/* copy the regs. they are first in sc so we can use sc directly */254255err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));256257/* Set the frametype to CRIS_FRAME_NORMAL for the execution of258the signal handler. The frametype will be restored to its previous259value in restore_sigcontext. */260regs->frametype = CRIS_FRAME_NORMAL;261262/* then some other stuff */263264err |= __put_user(mask, &sc->oldmask);265266err |= __put_user(usp, &sc->usp);267268return err;269}270271/* Figure out where we want to put the new signal frame272* - usually on the stack. */273274static inline void __user *275get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)276{277unsigned long sp = rdusp();278279/* This is the X/Open sanctioned signal stack switching. */280if (ka->sa.sa_flags & SA_ONSTACK) {281if (! on_sig_stack(sp))282sp = current->sas_ss_sp + current->sas_ss_size;283}284285/* make sure the frame is dword-aligned */286287sp &= ~3;288289return (void __user*)(sp - frame_size);290}291292/* grab and setup a signal frame.293*294* basically we stack a lot of state info, and arrange for the295* user-mode program to return to the kernel using either a296* trampoline which performs the syscall sigreturn, or a provided297* user-mode trampoline.298*/299300static int setup_frame(int sig, struct k_sigaction *ka,301sigset_t *set, struct pt_regs *regs)302{303struct sigframe __user *frame;304unsigned long return_ip;305int err = 0;306307frame = get_sigframe(ka, regs, sizeof(*frame));308309if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))310goto give_sigsegv;311312err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);313if (err)314goto give_sigsegv;315316if (_NSIG_WORDS > 1) {317err |= __copy_to_user(frame->extramask, &set->sig[1],318sizeof(frame->extramask));319}320if (err)321goto give_sigsegv;322323/* Set up to return from userspace. If provided, use a stub324already in userspace. */325if (ka->sa.sa_flags & SA_RESTORER) {326return_ip = (unsigned long)ka->sa.sa_restorer;327} else {328/* trampoline - the desired return ip is the retcode itself */329return_ip = (unsigned long)&frame->retcode;330/* This is movu.w __NR_sigreturn, r9; break 13; */331err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));332err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));333err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));334}335336if (err)337goto give_sigsegv;338339/* Set up registers for signal handler */340341regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */342regs->srp = return_ip; /* what we enter LATER */343regs->r10 = sig; /* first argument is signo */344345/* actually move the usp to reflect the stacked frame */346347wrusp((unsigned long)frame);348349return 0;350351give_sigsegv:352force_sigsegv(sig, current);353return -EFAULT;354}355356static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,357sigset_t *set, struct pt_regs *regs)358{359struct rt_sigframe __user *frame;360unsigned long return_ip;361int err = 0;362363frame = get_sigframe(ka, regs, sizeof(*frame));364365if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))366goto give_sigsegv;367368err |= __put_user(&frame->info, &frame->pinfo);369err |= __put_user(&frame->uc, &frame->puc);370err |= copy_siginfo_to_user(&frame->info, info);371if (err)372goto give_sigsegv;373374/* Clear all the bits of the ucontext we don't use. */375err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));376377err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);378379err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));380381if (err)382goto give_sigsegv;383384/* Set up to return from userspace. If provided, use a stub385already in userspace. */386if (ka->sa.sa_flags & SA_RESTORER) {387return_ip = (unsigned long)ka->sa.sa_restorer;388} else {389/* trampoline - the desired return ip is the retcode itself */390return_ip = (unsigned long)&frame->retcode;391/* This is movu.w __NR_rt_sigreturn, r9; break 13; */392err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));393err |= __put_user(__NR_rt_sigreturn,394(short __user *)(frame->retcode+2));395err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));396}397398if (err)399goto give_sigsegv;400401/* TODO what is the current->exec_domain stuff and invmap ? */402403/* Set up registers for signal handler */404405/* What we enter NOW */406regs->irp = (unsigned long) ka->sa.sa_handler;407/* What we enter LATER */408regs->srp = return_ip;409/* First argument is signo */410regs->r10 = sig;411/* Second argument is (siginfo_t *) */412regs->r11 = (unsigned long)&frame->info;413/* Third argument is unused */414regs->r12 = 0;415416/* Actually move the usp to reflect the stacked frame */417wrusp((unsigned long)frame);418419return 0;420421give_sigsegv:422force_sigsegv(sig, current);423return -EFAULT;424}425426/*427* OK, we're invoking a handler428*/429430static inline int handle_signal(int canrestart, unsigned long sig,431siginfo_t *info, struct k_sigaction *ka,432sigset_t *oldset, struct pt_regs *regs)433{434int ret;435436/* Are we from a system call? */437if (canrestart) {438/* If so, check system call restarting.. */439switch (regs->r10) {440case -ERESTART_RESTARTBLOCK:441case -ERESTARTNOHAND:442/* ERESTARTNOHAND means that the syscall should443* only be restarted if there was no handler for444* the signal, and since we only get here if there445* is a handler, we don't restart */446regs->r10 = -EINTR;447break;448case -ERESTARTSYS:449/* ERESTARTSYS means to restart the syscall if450* there is no handler or the handler was451* registered with SA_RESTART */452if (!(ka->sa.sa_flags & SA_RESTART)) {453regs->r10 = -EINTR;454break;455}456/* fallthrough */457case -ERESTARTNOINTR:458/* ERESTARTNOINTR means that the syscall should459* be called again after the signal handler returns. */460RESTART_CRIS_SYS(regs);461}462}463464/* Set up the stack frame */465if (ka->sa.sa_flags & SA_SIGINFO)466ret = setup_rt_frame(sig, ka, info, oldset, regs);467else468ret = setup_frame(sig, ka, oldset, regs);469470if (ret == 0) {471spin_lock_irq(¤t->sighand->siglock);472sigorsets(¤t->blocked, ¤t->blocked,473&ka->sa.sa_mask);474if (!(ka->sa.sa_flags & SA_NODEFER))475sigaddset(¤t->blocked, sig);476recalc_sigpending();477spin_unlock_irq(¤t->sighand->siglock);478}479return ret;480}481482/*483* Note that 'init' is a special process: it doesn't get signals it doesn't484* want to handle. Thus you cannot kill init even with a SIGKILL even by485* mistake.486*487* Also note that the regs structure given here as an argument, is the latest488* pushed pt_regs. It may or may not be the same as the first pushed registers489* when the initial usermode->kernelmode transition took place. Therefore490* we can use user_mode(regs) to see if we came directly from kernel or user491* mode below.492*/493494void do_signal(int canrestart, struct pt_regs *regs)495{496siginfo_t info;497int signr;498struct k_sigaction ka;499sigset_t *oldset;500501/*502* We want the common case to go fast, which503* is why we may in certain cases get here from504* kernel mode. Just return without doing anything505* if so.506*/507if (!user_mode(regs))508return;509510if (test_thread_flag(TIF_RESTORE_SIGMASK))511oldset = ¤t->saved_sigmask;512else513oldset = ¤t->blocked;514515signr = get_signal_to_deliver(&info, &ka, regs, NULL);516if (signr > 0) {517/* Whee! Actually deliver the signal. */518if (handle_signal(canrestart, signr, &info, &ka,519oldset, regs)) {520/* a signal was successfully delivered; the saved521* sigmask will have been stored in the signal frame,522* and will be restored by sigreturn, so we can simply523* clear the TIF_RESTORE_SIGMASK flag */524if (test_thread_flag(TIF_RESTORE_SIGMASK))525clear_thread_flag(TIF_RESTORE_SIGMASK);526}527return;528}529530/* Did we come from a system call? */531if (canrestart) {532/* Restart the system call - no handlers present */533if (regs->r10 == -ERESTARTNOHAND ||534regs->r10 == -ERESTARTSYS ||535regs->r10 == -ERESTARTNOINTR) {536RESTART_CRIS_SYS(regs);537}538if (regs->r10 == -ERESTART_RESTARTBLOCK) {539regs->r9 = __NR_restart_syscall;540regs->irp -= 2;541}542}543544/* if there's no signal to deliver, we just put the saved sigmask545* back */546if (test_thread_flag(TIF_RESTORE_SIGMASK)) {547clear_thread_flag(TIF_RESTORE_SIGMASK);548sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);549}550}551552553