1/* 2 * linux/arch/m32r/kernel/signal.c 3 * 4 * Copyright (c) 2003 Hitoshi Yamamoto 5 * 6 * Taken from i386 version. 7 * Copyright (C) 1991, 1992 Linus Torvalds 8 * 9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 10 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes 11 */ 12 13#include <linux/sched.h> 14#include <linux/mm.h> 15#include <linux/smp.h> 16#include <linux/kernel.h> 17#include <linux/signal.h> 18#include <linux/errno.h> 19#include <linux/wait.h> 20#include <linux/unistd.h> 21#include <linux/stddef.h> 22#include <linux/personality.h> 23#include <linux/freezer.h> 24#include <asm/cacheflush.h> 25#include <asm/ucontext.h> 26#include <asm/uaccess.h> 27 28#define DEBUG_SIG 0 29 30#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 31 32int do_signal(struct pt_regs *, sigset_t *); 33 34asmlinkage int 35sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, 36 unsigned long r2, unsigned long r3, unsigned long r4, 37 unsigned long r5, unsigned long r6, struct pt_regs *regs) 38{ 39 sigset_t saveset, newset; 40 41 if (sigsetsize != sizeof(sigset_t)) 42 return -EINVAL; 43 44 if (copy_from_user(&newset, unewset, sizeof(newset))) 45 return -EFAULT; 46 sigdelsetmask(&newset, ~_BLOCKABLE); 47 48 spin_lock_irq(¤t->sighand->siglock); 49 saveset = current->blocked; 50 current->blocked = newset; 51 recalc_sigpending(); 52 spin_unlock_irq(¤t->sighand->siglock); 53 54 regs->r0 = -EINTR; 55 while (1) { 56 current->state = TASK_INTERRUPTIBLE; 57 schedule(); 58 if (do_signal(regs, &saveset)) 59 return regs->r0; 60 } 61} 62 63asmlinkage int 64sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, 65 unsigned long r2, unsigned long r3, unsigned long r4, 66 unsigned long r5, unsigned long r6, struct pt_regs *regs) 67{ 68 return do_sigaltstack(uss, uoss, regs->spu); 69} 70 71 72/* 73 * Do a signal return; undo the signal stack. 74 */ 75 76struct rt_sigframe 77{ 78 int sig; 79 struct siginfo __user *pinfo; 80 void __user *puc; 81 struct siginfo info; 82 struct ucontext uc; 83// struct _fpstate fpstate; 84}; 85 86static int 87restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, 88 int *r0_p) 89{ 90 unsigned int err = 0; 91 92 /* Always make any pending restarted system calls return -EINTR */ 93 current_thread_info()->restart_block.fn = do_no_restart_syscall; 94 95#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x) 96 COPY(r4); 97 COPY(r5); 98 COPY(r6); 99 COPY(pt_regs); 100 /* COPY(r0); Skip r0 */ 101 COPY(r1); 102 COPY(r2); 103 COPY(r3); 104 COPY(r7); 105 COPY(r8); 106 COPY(r9); 107 COPY(r10); 108 COPY(r11); 109 COPY(r12); 110 COPY(acc0h); 111 COPY(acc0l); 112 COPY(acc1h); /* ISA_DSP_LEVEL2 only */ 113 COPY(acc1l); /* ISA_DSP_LEVEL2 only */ 114 COPY(psw); 115 COPY(bpc); 116 COPY(bbpsw); 117 COPY(bbpc); 118 COPY(spu); 119 COPY(fp); 120 COPY(lr); 121 COPY(spi); 122#undef COPY 123 124 regs->syscall_nr = -1; /* disable syscall checks */ 125 err |= __get_user(*r0_p, &sc->sc_r0); 126 127 return err; 128} 129 130asmlinkage int 131sys_rt_sigreturn(unsigned long r0, unsigned long r1, 132 unsigned long r2, unsigned long r3, unsigned long r4, 133 unsigned long r5, unsigned long r6, struct pt_regs *regs) 134{ 135 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu; 136 sigset_t set; 137 int result; 138 139 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 140 goto badframe; 141 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 142 goto badframe; 143 144 sigdelsetmask(&set, ~_BLOCKABLE); 145 spin_lock_irq(¤t->sighand->siglock); 146 current->blocked = set; 147 recalc_sigpending(); 148 spin_unlock_irq(¤t->sighand->siglock); 149 150 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result)) 151 goto badframe; 152 153 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT) 154 goto badframe; 155 156 return result; 157 158badframe: 159 force_sig(SIGSEGV, current); 160 return 0; 161} 162 163/* 164 * Set up a signal frame. 165 */ 166 167static int 168setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, 169 unsigned long mask) 170{ 171 int err = 0; 172 173#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x) 174 COPY(r4); 175 COPY(r5); 176 COPY(r6); 177 COPY(pt_regs); 178 COPY(r0); 179 COPY(r1); 180 COPY(r2); 181 COPY(r3); 182 COPY(r7); 183 COPY(r8); 184 COPY(r9); 185 COPY(r10); 186 COPY(r11); 187 COPY(r12); 188 COPY(acc0h); 189 COPY(acc0l); 190 COPY(acc1h); /* ISA_DSP_LEVEL2 only */ 191 COPY(acc1l); /* ISA_DSP_LEVEL2 only */ 192 COPY(psw); 193 COPY(bpc); 194 COPY(bbpsw); 195 COPY(bbpc); 196 COPY(spu); 197 COPY(fp); 198 COPY(lr); 199 COPY(spi); 200#undef COPY 201 202 err |= __put_user(mask, &sc->oldmask); 203 204 return err; 205} 206 207/* 208 * Determine which stack to use.. 209 */ 210static inline void __user * 211get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size) 212{ 213 /* This is the X/Open sanctioned signal stack switching. */ 214 if (ka->sa.sa_flags & SA_ONSTACK) { 215 if (sas_ss_flags(sp) == 0) 216 sp = current->sas_ss_sp + current->sas_ss_size; 217 } 218 219 return (void __user *)((sp - frame_size) & -8ul); 220} 221 222static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 223 sigset_t *set, struct pt_regs *regs) 224{ 225 struct rt_sigframe __user *frame; 226 int err = 0; 227 int signal; 228 229 frame = get_sigframe(ka, regs->spu, sizeof(*frame)); 230 231 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 232 goto give_sigsegv; 233 234 signal = current_thread_info()->exec_domain 235 && current_thread_info()->exec_domain->signal_invmap 236 && sig < 32 237 ? current_thread_info()->exec_domain->signal_invmap[sig] 238 : sig; 239 240 err |= __put_user(signal, &frame->sig); 241 if (err) 242 goto give_sigsegv; 243 244 err |= __put_user(&frame->info, &frame->pinfo); 245 err |= __put_user(&frame->uc, &frame->puc); 246 err |= copy_siginfo_to_user(&frame->info, info); 247 if (err) 248 goto give_sigsegv; 249 250 /* Create the ucontext. */ 251 err |= __put_user(0, &frame->uc.uc_flags); 252 err |= __put_user(0, &frame->uc.uc_link); 253 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); 254 err |= __put_user(sas_ss_flags(regs->spu), 255 &frame->uc.uc_stack.ss_flags); 256 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); 257 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]); 258 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 259 if (err) 260 goto give_sigsegv; 261 262 /* Set up to return from userspace. */ 263 regs->lr = (unsigned long)ka->sa.sa_restorer; 264 265 /* Set up registers for signal handler */ 266 regs->spu = (unsigned long)frame; 267 regs->r0 = signal; /* Arg for signal handler */ 268 regs->r1 = (unsigned long)&frame->info; 269 regs->r2 = (unsigned long)&frame->uc; 270 regs->bpc = (unsigned long)ka->sa.sa_handler; 271 272 set_fs(USER_DS); 273 274#if DEBUG_SIG 275 printk("SIG deliver (%s:%d): sp=%p pc=%p\n", 276 current->comm, current->pid, frame, regs->pc); 277#endif 278 279 return; 280 281give_sigsegv: 282 force_sigsegv(sig, current); 283} 284 285/* 286 * OK, we're invoking a handler 287 */ 288 289static void 290handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info, 291 sigset_t *oldset, struct pt_regs *regs) 292{ 293 unsigned short inst; 294 295 /* Are we from a system call? */ 296 if (regs->syscall_nr >= 0) { 297 /* If so, check system call restarting.. */ 298 switch (regs->r0) { 299 case -ERESTART_RESTARTBLOCK: 300 case -ERESTARTNOHAND: 301 regs->r0 = -EINTR; 302 break; 303 304 case -ERESTARTSYS: 305 if (!(ka->sa.sa_flags & SA_RESTART)) { 306 regs->r0 = -EINTR; 307 break; 308 } 309 /* fallthrough */ 310 case -ERESTARTNOINTR: 311 regs->r0 = regs->orig_r0; 312 inst = *(unsigned short *)(regs->bpc - 2); 313 if ((inst & 0xfff0) == 0x10f0) /* trap ? */ 314 regs->bpc -= 2; 315 else 316 regs->bpc -= 4; 317 } 318 } 319 320 /* Set up the stack frame */ 321 setup_rt_frame(sig, ka, info, oldset, regs); 322 323 spin_lock_irq(¤t->sighand->siglock); 324 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask); 325 if (!(ka->sa.sa_flags & SA_NODEFER)) 326 sigaddset(¤t->blocked,sig); 327 recalc_sigpending(); 328 spin_unlock_irq(¤t->sighand->siglock); 329} 330 331/* 332 * Note that 'init' is a special process: it doesn't get signals it doesn't 333 * want to handle. Thus you cannot kill init even with a SIGKILL even by 334 * mistake. 335 */ 336int do_signal(struct pt_regs *regs, sigset_t *oldset) 337{ 338 siginfo_t info; 339 int signr; 340 struct k_sigaction ka; 341 unsigned short inst; 342 343 /* 344 * We want the common case to go fast, which 345 * is why we may in certain cases get here from 346 * kernel mode. Just return without doing anything 347 * if so. 348 */ 349 if (!user_mode(regs)) 350 return 1; 351 352 if (try_to_freeze()) 353 goto no_signal; 354 355 if (!oldset) 356 oldset = ¤t->blocked; 357 358 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 359 if (signr > 0) { 360 /* Reenable any watchpoints before delivering the 361 * signal to user space. The processor register will 362 * have been cleared if the watchpoint triggered 363 * inside the kernel. 364 */ 365 366 /* Whee! Actually deliver the signal. */ 367 handle_signal(signr, &ka, &info, oldset, regs); 368 return 1; 369 } 370 371 no_signal: 372 /* Did we come from a system call? */ 373 if (regs->syscall_nr >= 0) { 374 /* Restart the system call - no handlers present */ 375 if (regs->r0 == -ERESTARTNOHAND || 376 regs->r0 == -ERESTARTSYS || 377 regs->r0 == -ERESTARTNOINTR) { 378 regs->r0 = regs->orig_r0; 379 inst = *(unsigned short *)(regs->bpc - 2); 380 if ((inst & 0xfff0) == 0x10f0) /* trap ? */ 381 regs->bpc -= 2; 382 else 383 regs->bpc -= 4; 384 } 385 if (regs->r0 == -ERESTART_RESTARTBLOCK){ 386 regs->r0 = regs->orig_r0; 387 regs->r7 = __NR_restart_syscall; 388 inst = *(unsigned short *)(regs->bpc - 2); 389 if ((inst & 0xfff0) == 0x10f0) /* trap ? */ 390 regs->bpc -= 2; 391 else 392 regs->bpc -= 4; 393 } 394 } 395 return 0; 396} 397 398/* 399 * notification of userspace execution resumption 400 * - triggered by current->work.notify_resume 401 */ 402void do_notify_resume(struct pt_regs *regs, sigset_t *oldset, 403 __u32 thread_info_flags) 404{ 405 /* Pending single-step? */ 406 if (thread_info_flags & _TIF_SINGLESTEP) 407 clear_thread_flag(TIF_SINGLESTEP); 408 409 /* deal with pending signal delivery */ 410 if (thread_info_flags & _TIF_SIGPENDING) 411 do_signal(regs,oldset); 412 413 clear_thread_flag(TIF_IRET); 414} 415