Path: blob/master/arch/cris/arch-v32/kernel/signal.c
15125 views
/*1* Copyright (C) 2003, Axis Communications AB.2*/34#include <linux/sched.h>5#include <linux/mm.h>6#include <linux/slab.h>7#include <linux/kernel.h>8#include <linux/signal.h>9#include <linux/errno.h>10#include <linux/wait.h>11#include <linux/ptrace.h>12#include <linux/unistd.h>13#include <linux/stddef.h>14#include <linux/syscalls.h>15#include <linux/vmalloc.h>1617#include <asm/io.h>18#include <asm/processor.h>19#include <asm/ucontext.h>20#include <asm/uaccess.h>21#include <arch/ptrace.h>22#include <arch/hwregs/cpu_vect.h>2324extern unsigned long cris_signal_return_page;2526/* Flag to check if a signal is blockable. */27#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))2829/*30* A syscall in CRIS is really a "break 13" instruction, which is 231* bytes. The registers is manipulated so upon return the instruction32* will be executed again.33*34* This relies on that PC points to the instruction after the break call.35*/36#define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;3738/* Signal frames. */39struct signal_frame {40struct sigcontext sc;41unsigned long extramask[_NSIG_WORDS - 1];42unsigned char retcode[8]; /* Trampoline code. */43};4445struct rt_signal_frame {46struct siginfo *pinfo;47void *puc;48struct siginfo info;49struct ucontext uc;50unsigned char retcode[8]; /* Trampoline code. */51};5253void do_signal(int restart, struct pt_regs *regs);54void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,55struct pt_regs *regs);56/*57* Swap in the new signal mask, and wait for a signal. Define some58* dummy arguments to be able to reach the regs argument.59*/60int61sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,62long srp, struct pt_regs *regs)63{64mask &= _BLOCKABLE;65spin_lock_irq(¤t->sighand->siglock);66current->saved_sigmask = current->blocked;67siginitset(¤t->blocked, mask);68recalc_sigpending();69spin_unlock_irq(¤t->sighand->siglock);70current->state = TASK_INTERRUPTIBLE;71schedule();72set_thread_flag(TIF_RESTORE_SIGMASK);73return -ERESTARTNOHAND;74}7576int77sys_sigaction(int signal, const struct old_sigaction *act,78struct old_sigaction *oact)79{80int retval;81struct k_sigaction newk;82struct k_sigaction oldk;8384if (act) {85old_sigset_t mask;8687if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||88__get_user(newk.sa.sa_handler, &act->sa_handler) ||89__get_user(newk.sa.sa_restorer, &act->sa_restorer))90return -EFAULT;9192__get_user(newk.sa.sa_flags, &act->sa_flags);93__get_user(mask, &act->sa_mask);94siginitset(&newk.sa.sa_mask, mask);95}9697retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);9899if (!retval && oact) {100if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||101__put_user(oldk.sa.sa_handler, &oact->sa_handler) ||102__put_user(oldk.sa.sa_restorer, &oact->sa_restorer))103return -EFAULT;104105__put_user(oldk.sa.sa_flags, &oact->sa_flags);106__put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);107}108109return retval;110}111112int113sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)114{115return do_sigaltstack(uss, uoss, rdusp());116}117118static int119restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)120{121unsigned int err = 0;122unsigned long old_usp;123124/* Always make any pending restarted system calls return -EINTR */125current_thread_info()->restart_block.fn = do_no_restart_syscall;126127/*128* Restore the registers from &sc->regs. sc is already checked129* for VERIFY_READ since the signal_frame was previously130* checked in sys_sigreturn().131*/132if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))133goto badframe;134135/* Make that the user-mode flag is set. */136regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));137138/* Restore the old USP. */139err |= __get_user(old_usp, &sc->usp);140wrusp(old_usp);141142return err;143144badframe:145return 1;146}147148/* Define some dummy arguments to be able to reach the regs argument. */149asmlinkage int150sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,151struct pt_regs *regs)152{153sigset_t set;154struct signal_frame __user *frame;155unsigned long oldspc = regs->spc;156unsigned long oldccs = regs->ccs;157158frame = (struct signal_frame *) rdusp();159160/*161* Since the signal is stacked on a dword boundary, the frame162* should be dword aligned here as well. It it's not, then the163* user is trying some funny business.164*/165if (((long)frame) & 3)166goto badframe;167168if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))169goto badframe;170171if (__get_user(set.sig[0], &frame->sc.oldmask) ||172(_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],173frame->extramask,174sizeof(frame->extramask))))175goto badframe;176177sigdelsetmask(&set, ~_BLOCKABLE);178spin_lock_irq(¤t->sighand->siglock);179180current->blocked = set;181182recalc_sigpending();183spin_unlock_irq(¤t->sighand->siglock);184185if (restore_sigcontext(regs, &frame->sc))186goto badframe;187188keep_debug_flags(oldccs, oldspc, regs);189190return regs->r10;191192badframe:193force_sig(SIGSEGV, current);194return 0;195}196197/* Define some dummy variables to be able to reach the regs argument. */198asmlinkage int199sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,200struct pt_regs *regs)201{202sigset_t set;203struct rt_signal_frame __user *frame;204unsigned long oldspc = regs->spc;205unsigned long oldccs = regs->ccs;206207frame = (struct rt_signal_frame *) rdusp();208209/*210* Since the signal is stacked on a dword boundary, the frame211* should be dword aligned here as well. It it's not, then the212* user is trying some funny business.213*/214if (((long)frame) & 3)215goto badframe;216217if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))218goto badframe;219220if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))221goto badframe;222223sigdelsetmask(&set, ~_BLOCKABLE);224spin_lock_irq(¤t->sighand->siglock);225226current->blocked = set;227228recalc_sigpending();229spin_unlock_irq(¤t->sighand->siglock);230231if (restore_sigcontext(regs, &frame->uc.uc_mcontext))232goto badframe;233234if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)235goto badframe;236237keep_debug_flags(oldccs, oldspc, regs);238239return regs->r10;240241badframe:242force_sig(SIGSEGV, current);243return 0;244}245246/* Setup a signal frame. */247static int248setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,249unsigned long mask)250{251int err;252unsigned long usp;253254err = 0;255usp = rdusp();256257/*258* Copy the registers. They are located first in sc, so it's259* possible to use sc directly.260*/261err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));262263err |= __put_user(mask, &sc->oldmask);264err |= __put_user(usp, &sc->usp);265266return err;267}268269/* Figure out where to put the new signal frame - usually on the stack. */270static inline void __user *271get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)272{273unsigned long sp;274275sp = rdusp();276277/* This is the X/Open sanctioned signal stack switching. */278if (ka->sa.sa_flags & SA_ONSTACK) {279if (!on_sig_stack(sp))280sp = current->sas_ss_sp + current->sas_ss_size;281}282283/* Make sure the frame is dword-aligned. */284sp &= ~3;285286return (void __user *)(sp - frame_size);287}288289/* Grab and setup a signal frame.290*291* Basically a lot of state-info is stacked, and arranged for the292* user-mode program to return to the kernel using either a trampiline293* which performs the syscall sigreturn(), or a provided user-mode294* trampoline.295*/296static int297setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,298struct pt_regs * regs)299{300int err;301unsigned long return_ip;302struct signal_frame __user *frame;303304err = 0;305frame = get_sigframe(ka, regs, sizeof(*frame));306307if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))308goto give_sigsegv;309310err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);311312if (err)313goto give_sigsegv;314315if (_NSIG_WORDS > 1) {316err |= __copy_to_user(frame->extramask, &set->sig[1],317sizeof(frame->extramask));318}319320if (err)321goto give_sigsegv;322323/*324* Set up to return from user-space. If provided, use a stub325* already located in user-space.326*/327if (ka->sa.sa_flags & SA_RESTORER) {328return_ip = (unsigned long)ka->sa.sa_restorer;329} else {330/* Trampoline - the desired return ip is in the signal return page. */331return_ip = cris_signal_return_page;332333/*334* This is movu.w __NR_sigreturn, r9; break 13;335*336* WE DO NOT USE IT ANY MORE! It's only left here for historical337* reasons and because gdb uses it as a signature to notice338* signal handler stack frames.339*/340err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));341err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));342err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));343}344345if (err)346goto give_sigsegv;347348/*349* Set up registers for signal handler.350*351* Where the code enters now.352* Where the code enter later.353* First argument, signo.354*/355regs->erp = (unsigned long) ka->sa.sa_handler;356regs->srp = return_ip;357regs->r10 = sig;358359/* Actually move the USP to reflect the stacked frame. */360wrusp((unsigned long)frame);361362return 0;363364give_sigsegv:365if (sig == SIGSEGV)366ka->sa.sa_handler = SIG_DFL;367368force_sig(SIGSEGV, current);369return -EFAULT;370}371372static int373setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,374sigset_t *set, struct pt_regs * regs)375{376int err;377unsigned long return_ip;378struct rt_signal_frame __user *frame;379380err = 0;381frame = get_sigframe(ka, regs, sizeof(*frame));382383if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))384goto give_sigsegv;385386/* TODO: what is the current->exec_domain stuff and invmap ? */387388err |= __put_user(&frame->info, &frame->pinfo);389err |= __put_user(&frame->uc, &frame->puc);390err |= copy_siginfo_to_user(&frame->info, info);391392if (err)393goto give_sigsegv;394395/* Clear all the bits of the ucontext we don't use. */396err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));397err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);398err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));399400if (err)401goto give_sigsegv;402403/*404* Set up to return from user-space. If provided, use a stub405* already located in user-space.406*/407if (ka->sa.sa_flags & SA_RESTORER) {408return_ip = (unsigned long) ka->sa.sa_restorer;409} else {410/* Trampoline - the desired return ip is in the signal return page. */411return_ip = cris_signal_return_page + 6;412413/*414* This is movu.w __NR_rt_sigreturn, r9; break 13;415*416* WE DO NOT USE IT ANY MORE! It's only left here for historical417* reasons and because gdb uses it as a signature to notice418* signal handler stack frames.419*/420err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));421422err |= __put_user(__NR_rt_sigreturn,423(short __user*)(frame->retcode+2));424425err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));426}427428if (err)429goto give_sigsegv;430431/*432* Set up registers for signal handler.433*434* Where the code enters now.435* Where the code enters later.436* First argument is signo.437* Second argument is (siginfo_t *).438* Third argument is unused.439*/440regs->erp = (unsigned long) ka->sa.sa_handler;441regs->srp = return_ip;442regs->r10 = sig;443regs->r11 = (unsigned long) &frame->info;444regs->r12 = 0;445446/* Actually move the usp to reflect the stacked frame. */447wrusp((unsigned long)frame);448449return 0;450451give_sigsegv:452if (sig == SIGSEGV)453ka->sa.sa_handler = SIG_DFL;454455force_sig(SIGSEGV, current);456return -EFAULT;457}458459/* Invoke a signal handler to, well, handle the signal. */460static inline int461handle_signal(int canrestart, unsigned long sig,462siginfo_t *info, struct k_sigaction *ka,463sigset_t *oldset, struct pt_regs * regs)464{465int ret;466467/* Check if this got called from a system call. */468if (canrestart) {469/* If so, check system call restarting. */470switch (regs->r10) {471case -ERESTART_RESTARTBLOCK:472case -ERESTARTNOHAND:473/*474* This means that the syscall should475* only be restarted if there was no476* handler for the signal, and since477* this point isn't reached unless478* there is a handler, there's no need479* to restart.480*/481regs->r10 = -EINTR;482break;483484case -ERESTARTSYS:485/*486* This means restart the syscall if487* there is no handler, or the handler488* was registered with SA_RESTART.489*/490if (!(ka->sa.sa_flags & SA_RESTART)) {491regs->r10 = -EINTR;492break;493}494495/* Fall through. */496497case -ERESTARTNOINTR:498/*499* This means that the syscall should500* be called again after the signal501* handler returns.502*/503RESTART_CRIS_SYS(regs);504break;505}506}507508/* Set up the stack frame. */509if (ka->sa.sa_flags & SA_SIGINFO)510ret = setup_rt_frame(sig, ka, info, oldset, regs);511else512ret = setup_frame(sig, ka, oldset, regs);513514if (ka->sa.sa_flags & SA_ONESHOT)515ka->sa.sa_handler = SIG_DFL;516517if (ret == 0) {518spin_lock_irq(¤t->sighand->siglock);519sigorsets(¤t->blocked, ¤t->blocked,520&ka->sa.sa_mask);521if (!(ka->sa.sa_flags & SA_NODEFER))522sigaddset(¤t->blocked, sig);523recalc_sigpending();524spin_unlock_irq(¤t->sighand->siglock);525}526527return ret;528}529530/*531* Note that 'init' is a special process: it doesn't get signals it doesn't532* want to handle. Thus you cannot kill init even with a SIGKILL even by533* mistake.534*535* Also note that the regs structure given here as an argument, is the latest536* pushed pt_regs. It may or may not be the same as the first pushed registers537* when the initial usermode->kernelmode transition took place. Therefore538* we can use user_mode(regs) to see if we came directly from kernel or user539* mode below.540*/541void542do_signal(int canrestart, struct pt_regs *regs)543{544int signr;545siginfo_t info;546struct k_sigaction ka;547sigset_t *oldset;548549/*550* The common case should go fast, which is why this point is551* reached from kernel-mode. If that's the case, just return552* without doing anything.553*/554if (!user_mode(regs))555return;556557if (test_thread_flag(TIF_RESTORE_SIGMASK))558oldset = ¤t->saved_sigmask;559else560oldset = ¤t->blocked;561562signr = get_signal_to_deliver(&info, &ka, regs, NULL);563564if (signr > 0) {565/* Whee! Actually deliver the signal. */566if (handle_signal(canrestart, signr, &info, &ka,567oldset, regs)) {568/* a signal was successfully delivered; the saved569* sigmask will have been stored in the signal frame,570* and will be restored by sigreturn, so we can simply571* clear the TIF_RESTORE_SIGMASK flag */572if (test_thread_flag(TIF_RESTORE_SIGMASK))573clear_thread_flag(TIF_RESTORE_SIGMASK);574}575576return;577}578579/* Got here from a system call? */580if (canrestart) {581/* Restart the system call - no handlers present. */582if (regs->r10 == -ERESTARTNOHAND ||583regs->r10 == -ERESTARTSYS ||584regs->r10 == -ERESTARTNOINTR) {585RESTART_CRIS_SYS(regs);586}587588if (regs->r10 == -ERESTART_RESTARTBLOCK){589regs->r9 = __NR_restart_syscall;590regs->erp -= 2;591}592}593594/* if there's no signal to deliver, we just put the saved sigmask595* back */596if (test_thread_flag(TIF_RESTORE_SIGMASK)) {597clear_thread_flag(TIF_RESTORE_SIGMASK);598sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);599}600}601602asmlinkage void603ugdb_trap_user(struct thread_info *ti, int sig)604{605if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {606/* Zero single-step PC if the reason we stopped wasn't a single607step exception. This is to avoid relying on it when it isn't608reliable. */609user_regs(ti)->spc = 0;610}611/* FIXME: Filter out false h/w breakpoint hits (i.e. EDA612not within any configured h/w breakpoint range). Synchronize with613what already exists for kernel debugging. */614if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {615/* Break 8: subtract 2 from ERP unless in a delay slot. */616if (!(user_regs(ti)->erp & 0x1))617user_regs(ti)->erp -= 2;618}619sys_kill(ti->task->pid, sig);620}621622void623keep_debug_flags(unsigned long oldccs, unsigned long oldspc,624struct pt_regs *regs)625{626if (oldccs & (1 << Q_CCS_BITNR)) {627/* Pending single step due to single-stepping the break 13628in the signal trampoline: keep the Q flag. */629regs->ccs |= (1 << Q_CCS_BITNR);630/* S flag should be set - complain if it's not. */631if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {632printk("Q flag but no S flag?");633}634regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));635/* Assume the SPC is valid and interesting. */636regs->spc = oldspc;637638} else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {639/* If a h/w bp was set in the signal handler we need640to keep the S flag. */641regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));642/* Don't keep the old SPC though; if we got here due to643a single-step, the Q flag should have been set. */644} else if (regs->spc) {645/* If we were single-stepping *before* the signal was taken,646we don't want to restore that state now, because GDB will647have forgotten all about it. */648regs->spc = 0;649regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));650}651}652653/* Set up the trampolines on the signal return page. */654int __init655cris_init_signal(void)656{657u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);658659/* This is movu.w __NR_sigreturn, r9; break 13; */660data[0] = 0x9c5f;661data[1] = __NR_sigreturn;662data[2] = 0xe93d;663/* This is movu.w __NR_rt_sigreturn, r9; break 13; */664data[3] = 0x9c5f;665data[4] = __NR_rt_sigreturn;666data[5] = 0xe93d;667668/* Map to userspace with appropriate permissions (no write access...) */669cris_signal_return_page = (unsigned long)670__ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);671672return 0;673}674675__initcall(cris_init_signal);676677678