Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | /*
* Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
* Licensed under the GPL
*/
#include "linux/config.h"
#include "linux/stddef.h"
#include "linux/sys.h"
#include "linux/sched.h"
#include "linux/wait.h"
#include "linux/kernel.h"
#include "linux/smp_lock.h"
#include "linux/module.h"
#include "linux/slab.h"
#include "linux/tty.h"
#include "linux/binfmts.h"
#include "linux/ptrace.h"
#include "asm/signal.h"
#include "asm/uaccess.h"
#include "asm/unistd.h"
#include "user_util.h"
#include "asm/ucontext.h"
#include "kern_util.h"
#include "signal_kern.h"
#include "signal_user.h"
#include "kern.h"
#include "frame_kern.h"
#include "sigcontext.h"
#include "mode.h"
EXPORT_SYMBOL(block_signals);
EXPORT_SYMBOL(unblock_signals);
static void force_segv(int sig)
{
if(sig == SIGSEGV){
struct k_sigaction *ka;
ka = ¤t->sig->action[SIGSEGV - 1];
ka->sa.sa_handler = SIG_DFL;
}
force_sig(SIGSEGV, current);
}
#define _S(nr) (1<<((nr)-1))
#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
/*
* OK, we're invoking a handler
*/
static int handle_signal(struct pt_regs *regs, unsigned long signr,
struct k_sigaction *ka, siginfo_t *info,
sigset_t *oldset, int error)
{
__sighandler_t handler;
void (*restorer)(void);
unsigned long sp;
sigset_t save;
int err, ret;
ret = 0;
switch(error){
case -ERESTART_RESTARTBLOCK:
current_thread_info()->restart_block.fn =
do_no_restart_syscall;
case -ERESTARTNOHAND:
ret = -EINTR;
break;
case -ERESTARTSYS:
if (!(ka->sa.sa_flags & SA_RESTART)) {
ret = -EINTR;
break;
}
/* fallthrough */
case -ERESTARTNOINTR:
PT_REGS_RESTART_SYSCALL(regs);
PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
/* This is because of the UM_SET_SYSCALL_RETURN and the fact
* that on i386 the system call number and return value are
* in the same register. When the system call restarts, %eax
* had better have the system call number in it. Since the
* return value doesn't matter (except that it shouldn't be
* -ERESTART*), we'll stick the system call number there.
*/
ret = PT_REGS_SYSCALL_NR(regs);
break;
}
handler = ka->sa.sa_handler;
save = *oldset;
if (ka->sa.sa_flags & SA_ONESHOT)
ka->sa.sa_handler = SIG_DFL;
if (!(ka->sa.sa_flags & SA_NODEFER)) {
spin_lock_irq(¤t->sighand->siglock);
sigorsets(¤t->blocked, ¤t->blocked,
&ka->sa.sa_mask);
sigaddset(¤t->blocked, signr);
recalc_sigpending();
spin_unlock_irq(¤t->sighand->siglock);
}
sp = PT_REGS_SP(regs);
if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
sp = current->sas_ss_sp + current->sas_ss_size;
if(error != 0) PT_REGS_SET_SYSCALL_RETURN(regs, ret);
if (ka->sa.sa_flags & SA_RESTORER) restorer = ka->sa.sa_restorer;
else restorer = NULL;
if(ka->sa.sa_flags & SA_SIGINFO)
err = setup_signal_stack_si(sp, signr, (unsigned long) handler,
restorer, regs, info, &save);
else
err = setup_signal_stack_sc(sp, signr, (unsigned long) handler,
restorer, regs, &save);
if(err) goto segv;
return(0);
segv:
force_segv(signr);
return(1);
}
static int kern_do_signal(struct pt_regs *regs, sigset_t *oldset, int error)
{
siginfo_t info;
struct k_sigaction *ka;
int err, sig;
if (!oldset)
oldset = ¤t->blocked;
sig = get_signal_to_deliver(&info, regs, NULL);
if(sig == 0)
return(0);
/* Whee! Actually deliver the signal. */
ka = ¤t->sig->action[sig -1 ];
err = handle_signal(regs, sig, ka, &info, oldset, error);
if(!err) return(1);
/* Did we come from a system call? */
if(PT_REGS_SYSCALL_NR(regs) >= 0){
/* Restart the system call - no handlers present */
if(PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOHAND ||
PT_REGS_SYSCALL_RET(regs) == -ERESTARTSYS ||
PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOINTR){
PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
PT_REGS_RESTART_SYSCALL(regs);
}
else if(PT_REGS_SYSCALL_RET(regs) == -ERESTART_RESTARTBLOCK){
PT_REGS_SYSCALL_RET(regs) = __NR_restart_syscall;
PT_REGS_RESTART_SYSCALL(regs);
}
}
/* This closes a way to execute a system call on the host. If
* you set a breakpoint on a system call instruction and singlestep
* from it, the tracing thread used to PTRACE_SINGLESTEP the process
* rather than PTRACE_SYSCALL it, allowing the system call to execute
* on the host. The tracing thread will check this flag and
* PTRACE_SYSCALL if necessary.
*/
if((current->ptrace & PT_DTRACE) &&
is_syscall(PT_REGS_IP(¤t->thread.regs)))
(void) CHOOSE_MODE(current->thread.mode.tt.singlestep_syscall = 1, 0);
return(0);
}
int do_signal(int error)
{
return(kern_do_signal(¤t->thread.regs, NULL, error));
}
/*
* Atomically swap in the new signal mask, and wait for a signal.
*/
int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
{
sigset_t saveset;
mask &= _BLOCKABLE;
spin_lock_irq(¤t->sighand->siglock);
saveset = current->blocked;
siginitset(¤t->blocked, mask);
recalc_sigpending();
spin_unlock_irq(¤t->sighand->siglock);
while (1) {
current->state = TASK_INTERRUPTIBLE;
schedule();
if(kern_do_signal(¤t->thread.regs, &saveset, -EINTR))
return(-EINTR);
}
}
int sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize)
{
sigset_t saveset, newset;
/* XXX: Don't preclude handling different sized sigset_t's. */
if (sigsetsize != sizeof(sigset_t))
return -EINVAL;
if (copy_from_user(&newset, unewset, sizeof(newset)))
return -EFAULT;
sigdelsetmask(&newset, ~_BLOCKABLE);
spin_lock_irq(¤t->sighand->siglock);
saveset = current->blocked;
current->blocked = newset;
recalc_sigpending();
spin_unlock_irq(¤t->sighand->siglock);
while (1) {
current->state = TASK_INTERRUPTIBLE;
schedule();
if (kern_do_signal(¤t->thread.regs, &saveset, -EINTR))
return(-EINTR);
}
}
static int copy_sc_from_user(struct pt_regs *to, void *from,
struct arch_frame_data *arch)
{
int ret;
ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from, arch),
copy_sc_from_user_skas(&to->regs, from));
return(ret);
}
int sys_sigreturn(struct pt_regs regs)
{
void *sc = sp_to_sc(PT_REGS_SP(¤t->thread.regs));
void *mask = sp_to_mask(PT_REGS_SP(¤t->thread.regs));
int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
spin_lock_irq(¤t->sighand->siglock);
copy_from_user(¤t->blocked.sig[0], sc_sigmask(sc),
sizeof(current->blocked.sig[0]));
copy_from_user(¤t->blocked.sig[1], mask, sig_size);
sigdelsetmask(¤t->blocked, ~_BLOCKABLE);
recalc_sigpending();
spin_unlock_irq(¤t->sighand->siglock);
copy_sc_from_user(¤t->thread.regs, sc,
&signal_frame_sc.common.arch);
return(PT_REGS_SYSCALL_RET(¤t->thread.regs));
}
int sys_rt_sigreturn(struct pt_regs regs)
{
struct ucontext *uc = sp_to_uc(PT_REGS_SP(¤t->thread.regs));
void *fp;
int sig_size = _NSIG_WORDS * sizeof(unsigned long);
spin_lock_irq(¤t->sighand->siglock);
copy_from_user(¤t->blocked, &uc->uc_sigmask, sig_size);
sigdelsetmask(¤t->blocked, ~_BLOCKABLE);
recalc_sigpending();
spin_unlock_irq(¤t->sighand->siglock);
fp = (void *) (((unsigned long) uc) + sizeof(struct ucontext));
copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext,
&signal_frame_si.common.arch);
return(PT_REGS_SYSCALL_RET(¤t->thread.regs));
}
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/
|