| Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * File:         arch/blackfin/kernel/signal.c | 
|  | 3 | * Based on: | 
|  | 4 | * Author: | 
|  | 5 | * | 
|  | 6 | * Created: | 
|  | 7 | * Description: | 
|  | 8 | * | 
|  | 9 | * Modified: | 
|  | 10 | *               Copyright 2004-2006 Analog Devices Inc. | 
|  | 11 | * | 
|  | 12 | * Bugs:         Enter bugs at http://blackfin.uclinux.org/ | 
|  | 13 | * | 
|  | 14 | * This program is free software; you can redistribute it and/or modify | 
|  | 15 | * it under the terms of the GNU General Public License as published by | 
|  | 16 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 17 | * (at your option) any later version. | 
|  | 18 | * | 
|  | 19 | * This program is distributed in the hope that it will be useful, | 
|  | 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 22 | * GNU General Public License for more details. | 
|  | 23 | * | 
|  | 24 | * You should have received a copy of the GNU General Public License | 
|  | 25 | * along with this program; if not, see the file COPYING, or write | 
|  | 26 | * to the Free Software Foundation, Inc., | 
|  | 27 | * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | 
|  | 28 | */ | 
|  | 29 |  | 
|  | 30 | #include <linux/signal.h> | 
|  | 31 | #include <linux/syscalls.h> | 
|  | 32 | #include <linux/ptrace.h> | 
|  | 33 | #include <linux/tty.h> | 
|  | 34 | #include <linux/personality.h> | 
|  | 35 | #include <linux/binfmts.h> | 
|  | 36 | #include <linux/freezer.h> | 
| Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 37 | #include <linux/uaccess.h> | 
| Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 38 |  | 
| Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 39 | #include <asm/cacheflush.h> | 
|  | 40 | #include <asm/ucontext.h> | 
|  | 41 |  | 
|  | 42 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) | 
|  | 43 |  | 
|  | 44 | struct fdpic_func_descriptor { | 
|  | 45 | unsigned long	text; | 
|  | 46 | unsigned long	GOT; | 
|  | 47 | }; | 
|  | 48 |  | 
|  | 49 | struct rt_sigframe { | 
|  | 50 | int sig; | 
|  | 51 | struct siginfo *pinfo; | 
|  | 52 | void *puc; | 
|  | 53 | char retcode[8]; | 
|  | 54 | struct siginfo info; | 
|  | 55 | struct ucontext uc; | 
|  | 56 | }; | 
|  | 57 |  | 
|  | 58 | asmlinkage int sys_sigaltstack(const stack_t * uss, stack_t * uoss) | 
|  | 59 | { | 
|  | 60 | return do_sigaltstack(uss, uoss, rdusp()); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | static inline int | 
|  | 64 | rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, int *pr0) | 
|  | 65 | { | 
|  | 66 | unsigned long usp = 0; | 
|  | 67 | int err = 0; | 
|  | 68 |  | 
|  | 69 | #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x) | 
|  | 70 |  | 
|  | 71 | /* restore passed registers */ | 
|  | 72 | RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3); | 
|  | 73 | RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7); | 
|  | 74 | RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3); | 
|  | 75 | RESTORE(p4); RESTORE(p5); | 
|  | 76 | err |= __get_user(usp, &sc->sc_usp); | 
|  | 77 | wrusp(usp); | 
|  | 78 | RESTORE(a0w); RESTORE(a1w); | 
|  | 79 | RESTORE(a0x); RESTORE(a1x); | 
|  | 80 | RESTORE(astat); | 
|  | 81 | RESTORE(rets); | 
|  | 82 | RESTORE(pc); | 
|  | 83 | RESTORE(retx); | 
|  | 84 | RESTORE(fp); | 
|  | 85 | RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3); | 
|  | 86 | RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3); | 
|  | 87 | RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3); | 
|  | 88 | RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3); | 
|  | 89 | RESTORE(lc0); RESTORE(lc1); | 
|  | 90 | RESTORE(lt0); RESTORE(lt1); | 
|  | 91 | RESTORE(lb0); RESTORE(lb1); | 
|  | 92 | RESTORE(seqstat); | 
|  | 93 |  | 
|  | 94 | regs->orig_p0 = -1;	/* disable syscall checks */ | 
|  | 95 |  | 
|  | 96 | *pr0 = regs->r0; | 
|  | 97 | return err; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | asmlinkage int do_rt_sigreturn(unsigned long __unused) | 
|  | 101 | { | 
|  | 102 | struct pt_regs *regs = (struct pt_regs *)__unused; | 
|  | 103 | unsigned long usp = rdusp(); | 
|  | 104 | struct rt_sigframe *frame = (struct rt_sigframe *)(usp); | 
|  | 105 | sigset_t set; | 
|  | 106 | int r0; | 
|  | 107 |  | 
|  | 108 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | 
|  | 109 | goto badframe; | 
|  | 110 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | 
|  | 111 | goto badframe; | 
|  | 112 |  | 
|  | 113 | sigdelsetmask(&set, ~_BLOCKABLE); | 
|  | 114 | spin_lock_irq(¤t->sighand->siglock); | 
|  | 115 | current->blocked = set; | 
|  | 116 | recalc_sigpending(); | 
|  | 117 | spin_unlock_irq(¤t->sighand->siglock); | 
|  | 118 |  | 
|  | 119 | if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0)) | 
|  | 120 | goto badframe; | 
|  | 121 |  | 
|  | 122 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT) | 
|  | 123 | goto badframe; | 
|  | 124 |  | 
|  | 125 | return r0; | 
|  | 126 |  | 
| Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 127 | badframe: | 
| Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 128 | force_sig(SIGSEGV, current); | 
|  | 129 | return 0; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs) | 
|  | 133 | { | 
|  | 134 | int err = 0; | 
|  | 135 |  | 
|  | 136 | #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x) | 
|  | 137 |  | 
|  | 138 | SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3); | 
|  | 139 | SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7); | 
|  | 140 | SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3); | 
|  | 141 | SETUP(p4); SETUP(p5); | 
|  | 142 | err |= __put_user(rdusp(), &sc->sc_usp); | 
|  | 143 | SETUP(a0w); SETUP(a1w); | 
|  | 144 | SETUP(a0x); SETUP(a1x); | 
|  | 145 | SETUP(astat); | 
|  | 146 | SETUP(rets); | 
|  | 147 | SETUP(pc); | 
|  | 148 | SETUP(retx); | 
|  | 149 | SETUP(fp); | 
|  | 150 | SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3); | 
|  | 151 | SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3); | 
|  | 152 | SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3); | 
|  | 153 | SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3); | 
|  | 154 | SETUP(lc0); SETUP(lc1); | 
|  | 155 | SETUP(lt0); SETUP(lt1); | 
|  | 156 | SETUP(lb0); SETUP(lb1); | 
|  | 157 | SETUP(seqstat); | 
|  | 158 |  | 
|  | 159 | return err; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | static inline void push_cache(unsigned long vaddr, unsigned int len) | 
|  | 163 | { | 
|  | 164 | flush_icache_range(vaddr, vaddr + len); | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, | 
|  | 168 | size_t frame_size) | 
|  | 169 | { | 
|  | 170 | unsigned long usp; | 
|  | 171 |  | 
|  | 172 | /* Default to using normal stack.  */ | 
|  | 173 | usp = rdusp(); | 
|  | 174 |  | 
|  | 175 | /* This is the X/Open sanctioned signal stack switching.  */ | 
|  | 176 | if (ka->sa.sa_flags & SA_ONSTACK) { | 
|  | 177 | if (!on_sig_stack(usp)) | 
|  | 178 | usp = current->sas_ss_sp + current->sas_ss_size; | 
|  | 179 | } | 
|  | 180 | return (void *)((usp - frame_size) & -8UL); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | static int | 
|  | 184 | setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info, | 
|  | 185 | sigset_t * set, struct pt_regs *regs) | 
|  | 186 | { | 
|  | 187 | struct rt_sigframe *frame; | 
|  | 188 | int err = 0; | 
|  | 189 |  | 
|  | 190 | frame = get_sigframe(ka, regs, sizeof(*frame)); | 
|  | 191 |  | 
|  | 192 | err |= __put_user((current_thread_info()->exec_domain | 
|  | 193 | && current_thread_info()->exec_domain->signal_invmap | 
|  | 194 | && sig < 32 | 
|  | 195 | ? current_thread_info()->exec_domain-> | 
|  | 196 | signal_invmap[sig] : sig), &frame->sig); | 
|  | 197 |  | 
|  | 198 | err |= __put_user(&frame->info, &frame->pinfo); | 
|  | 199 | err |= __put_user(&frame->uc, &frame->puc); | 
|  | 200 | err |= copy_siginfo_to_user(&frame->info, info); | 
|  | 201 |  | 
|  | 202 | /* Create the ucontext.  */ | 
|  | 203 | err |= __put_user(0, &frame->uc.uc_flags); | 
|  | 204 | err |= __put_user(0, &frame->uc.uc_link); | 
|  | 205 | err |= | 
|  | 206 | __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); | 
|  | 207 | err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags); | 
|  | 208 | err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); | 
|  | 209 | err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs); | 
|  | 210 | err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | 
|  | 211 |  | 
|  | 212 | /* Set up to return from userspace.  */ | 
|  | 213 | err |= __put_user(0x28, &(frame->retcode[0])); | 
|  | 214 | err |= __put_user(0xe1, &(frame->retcode[1])); | 
|  | 215 | err |= __put_user(0xad, &(frame->retcode[2])); | 
|  | 216 | err |= __put_user(0x00, &(frame->retcode[3])); | 
|  | 217 | err |= __put_user(0xa0, &(frame->retcode[4])); | 
|  | 218 | err |= __put_user(0x00, &(frame->retcode[5])); | 
|  | 219 |  | 
|  | 220 | if (err) | 
|  | 221 | goto give_sigsegv; | 
|  | 222 |  | 
|  | 223 | push_cache((unsigned long)&frame->retcode, sizeof(frame->retcode)); | 
|  | 224 |  | 
|  | 225 | /* Set up registers for signal handler */ | 
|  | 226 | wrusp((unsigned long)frame); | 
|  | 227 | if (get_personality & FDPIC_FUNCPTRS) { | 
|  | 228 | struct fdpic_func_descriptor __user *funcptr = | 
|  | 229 | (struct fdpic_func_descriptor *) ka->sa.sa_handler; | 
|  | 230 | __get_user(regs->pc, &funcptr->text); | 
|  | 231 | __get_user(regs->p3, &funcptr->GOT); | 
|  | 232 | } else | 
|  | 233 | regs->pc = (unsigned long)ka->sa.sa_handler; | 
|  | 234 | regs->rets = (unsigned long)(frame->retcode); | 
|  | 235 |  | 
|  | 236 | regs->r0 = frame->sig; | 
|  | 237 | regs->r1 = (unsigned long)(&frame->info); | 
|  | 238 | regs->r2 = (unsigned long)(&frame->uc); | 
|  | 239 |  | 
|  | 240 | return 0; | 
|  | 241 |  | 
| Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 242 | give_sigsegv: | 
| Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 243 | if (sig == SIGSEGV) | 
|  | 244 | ka->sa.sa_handler = SIG_DFL; | 
|  | 245 | force_sig(SIGSEGV, current); | 
|  | 246 | return -EFAULT; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | static inline void | 
|  | 250 | handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler) | 
|  | 251 | { | 
|  | 252 | switch (regs->r0) { | 
|  | 253 | case -ERESTARTNOHAND: | 
|  | 254 | if (!has_handler) | 
|  | 255 | goto do_restart; | 
|  | 256 | regs->r0 = -EINTR; | 
|  | 257 | break; | 
|  | 258 |  | 
|  | 259 | case -ERESTARTSYS: | 
|  | 260 | if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) { | 
|  | 261 | regs->r0 = -EINTR; | 
|  | 262 | break; | 
|  | 263 | } | 
|  | 264 | /* fallthrough */ | 
|  | 265 | case -ERESTARTNOINTR: | 
| Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 266 | do_restart: | 
| Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 267 | regs->p0 = regs->orig_p0; | 
|  | 268 | regs->r0 = regs->orig_r0; | 
|  | 269 | regs->pc -= 2; | 
|  | 270 | break; | 
|  | 271 | } | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | /* | 
|  | 275 | * OK, we're invoking a handler | 
|  | 276 | */ | 
|  | 277 | static int | 
|  | 278 | handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka, | 
|  | 279 | sigset_t *oldset, struct pt_regs *regs) | 
|  | 280 | { | 
|  | 281 | int ret; | 
|  | 282 |  | 
|  | 283 | /* are we from a system call? to see pt_regs->orig_p0 */ | 
|  | 284 | if (regs->orig_p0 >= 0) | 
|  | 285 | /* If so, check system call restarting.. */ | 
|  | 286 | handle_restart(regs, ka, 1); | 
|  | 287 |  | 
|  | 288 | /* set up the stack frame */ | 
|  | 289 | ret = setup_rt_frame(sig, ka, info, oldset, regs); | 
|  | 290 |  | 
|  | 291 | if (ret == 0) { | 
|  | 292 | spin_lock_irq(¤t->sighand->siglock); | 
|  | 293 | sigorsets(¤t->blocked, ¤t->blocked, | 
|  | 294 | &ka->sa.sa_mask); | 
|  | 295 | if (!(ka->sa.sa_flags & SA_NODEFER)) | 
|  | 296 | sigaddset(¤t->blocked, sig); | 
|  | 297 | recalc_sigpending(); | 
|  | 298 | spin_unlock_irq(¤t->sighand->siglock); | 
|  | 299 | } | 
|  | 300 | return ret; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | /* | 
|  | 304 | * Note that 'init' is a special process: it doesn't get signals it doesn't | 
|  | 305 | * want to handle. Thus you cannot kill init even with a SIGKILL even by | 
|  | 306 | * mistake. | 
|  | 307 | * | 
|  | 308 | * Note that we go through the signals twice: once to check the signals | 
|  | 309 | * that the kernel can handle, and then we build all the user-level signal | 
|  | 310 | * handling stack-frames in one go after that. | 
|  | 311 | */ | 
|  | 312 | asmlinkage void do_signal(struct pt_regs *regs) | 
|  | 313 | { | 
|  | 314 | siginfo_t info; | 
|  | 315 | int signr; | 
|  | 316 | struct k_sigaction ka; | 
|  | 317 | sigset_t *oldset; | 
|  | 318 |  | 
|  | 319 | current->thread.esp0 = (unsigned long)regs; | 
|  | 320 |  | 
|  | 321 | if (try_to_freeze()) | 
|  | 322 | goto no_signal; | 
|  | 323 |  | 
|  | 324 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) | 
|  | 325 | oldset = ¤t->saved_sigmask; | 
|  | 326 | else | 
|  | 327 | oldset = ¤t->blocked; | 
|  | 328 |  | 
|  | 329 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); | 
|  | 330 | if (signr > 0) { | 
|  | 331 | /* Whee!  Actually deliver the signal.  */ | 
|  | 332 | if (handle_signal(signr, &info, &ka, oldset, regs) == 0) { | 
|  | 333 | /* a signal was successfully delivered; the saved | 
|  | 334 | * sigmask will have been stored in the signal frame, | 
|  | 335 | * and will be restored by sigreturn, so we can simply | 
|  | 336 | * clear the TIF_RESTORE_SIGMASK flag */ | 
|  | 337 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) | 
|  | 338 | clear_thread_flag(TIF_RESTORE_SIGMASK); | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | return; | 
|  | 342 | } | 
|  | 343 |  | 
| Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 344 | no_signal: | 
| Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 345 | /* Did we come from a system call? */ | 
|  | 346 | if (regs->orig_p0 >= 0) | 
|  | 347 | /* Restart the system call - no handlers present */ | 
|  | 348 | handle_restart(regs, NULL, 0); | 
|  | 349 |  | 
|  | 350 | /* if there's no signal to deliver, we just put the saved sigmask | 
|  | 351 | * back */ | 
|  | 352 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) { | 
|  | 353 | clear_thread_flag(TIF_RESTORE_SIGMASK); | 
|  | 354 | sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); | 
|  | 355 | } | 
|  | 356 | } |