1 /* 2 * Signal handling 3 * 4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> 5 * Copyright (C) 2008-2009 PetaLogix 6 * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au> 7 * Copyright (C) 2001 NEC Corporation 8 * Copyright (C) 2001 Miles Bader <miles@gnu.org> 9 * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima 10 * Copyright (C) 1991,1992 Linus Torvalds 11 * 12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 13 * 14 * This file was was derived from the sh version, arch/sh/kernel/signal.c 15 * 16 * This file is subject to the terms and conditions of the GNU General 17 * Public License. See the file COPYING in the main directory of this 18 * archive for more details. 19 */ 20 21 #include <linux/sched.h> 22 #include <linux/mm.h> 23 #include <linux/smp.h> 24 #include <linux/kernel.h> 25 #include <linux/signal.h> 26 #include <linux/errno.h> 27 #include <linux/wait.h> 28 #include <linux/ptrace.h> 29 #include <linux/unistd.h> 30 #include <linux/stddef.h> 31 #include <linux/personality.h> 32 #include <linux/percpu.h> 33 #include <linux/linkage.h> 34 #include <linux/tracehook.h> 35 #include <asm/entry.h> 36 #include <asm/ucontext.h> 37 #include <linux/uaccess.h> 38 #include <asm/pgtable.h> 39 #include <asm/pgalloc.h> 40 #include <linux/syscalls.h> 41 #include <asm/cacheflush.h> 42 #include <asm/syscalls.h> 43 44 /* 45 * Do a signal return; undo the signal stack. 46 */ 47 struct sigframe { 48 struct sigcontext sc; 49 unsigned long extramask[_NSIG_WORDS-1]; 50 unsigned long tramp[2]; /* signal trampoline */ 51 }; 52 53 struct rt_sigframe { 54 struct siginfo info; 55 struct ucontext uc; 56 unsigned long tramp[2]; /* signal trampoline */ 57 }; 58 59 static int restore_sigcontext(struct pt_regs *regs, 60 struct sigcontext __user *sc, int *rval_p) 61 { 62 unsigned int err = 0; 63 64 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); } 65 COPY(r0); 66 COPY(r1); 67 COPY(r2); COPY(r3); COPY(r4); COPY(r5); 68 COPY(r6); COPY(r7); COPY(r8); COPY(r9); 69 COPY(r10); COPY(r11); COPY(r12); COPY(r13); 70 COPY(r14); COPY(r15); COPY(r16); COPY(r17); 71 COPY(r18); COPY(r19); COPY(r20); COPY(r21); 72 COPY(r22); COPY(r23); COPY(r24); COPY(r25); 73 COPY(r26); COPY(r27); COPY(r28); COPY(r29); 74 COPY(r30); COPY(r31); 75 COPY(pc); COPY(ear); COPY(esr); COPY(fsr); 76 #undef COPY 77 78 *rval_p = regs->r3; 79 80 return err; 81 } 82 83 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs) 84 { 85 struct rt_sigframe __user *frame = 86 (struct rt_sigframe __user *)(regs->r1); 87 88 sigset_t set; 89 int rval; 90 91 /* Always make any pending restarted system calls return -EINTR */ 92 current_thread_info()->restart_block.fn = do_no_restart_syscall; 93 94 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 95 goto badframe; 96 97 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 98 goto badframe; 99 100 set_current_blocked(&set); 101 102 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval)) 103 goto badframe; 104 105 if (restore_altstack(&frame->uc.uc_stack)) 106 goto badframe; 107 108 return rval; 109 110 badframe: 111 force_sig(SIGSEGV, current); 112 return 0; 113 } 114 115 /* 116 * Set up a signal frame. 117 */ 118 119 static int 120 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, 121 unsigned long mask) 122 { 123 int err = 0; 124 125 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); } 126 COPY(r0); 127 COPY(r1); 128 COPY(r2); COPY(r3); COPY(r4); COPY(r5); 129 COPY(r6); COPY(r7); COPY(r8); COPY(r9); 130 COPY(r10); COPY(r11); COPY(r12); COPY(r13); 131 COPY(r14); COPY(r15); COPY(r16); COPY(r17); 132 COPY(r18); COPY(r19); COPY(r20); COPY(r21); 133 COPY(r22); COPY(r23); COPY(r24); COPY(r25); 134 COPY(r26); COPY(r27); COPY(r28); COPY(r29); 135 COPY(r30); COPY(r31); 136 COPY(pc); COPY(ear); COPY(esr); COPY(fsr); 137 #undef COPY 138 139 err |= __put_user(mask, &sc->oldmask); 140 141 return err; 142 } 143 144 /* 145 * Determine which stack to use.. 146 */ 147 static inline void __user * 148 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size) 149 { 150 /* Default to using normal stack */ 151 unsigned long sp = regs->r1; 152 153 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp)) 154 sp = current->sas_ss_sp + current->sas_ss_size; 155 156 return (void __user *)((sp - frame_size) & -8UL); 157 } 158 159 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 160 sigset_t *set, struct pt_regs *regs) 161 { 162 struct rt_sigframe __user *frame; 163 int err = 0; 164 int signal; 165 unsigned long address = 0; 166 #ifdef CONFIG_MMU 167 pmd_t *pmdp; 168 pte_t *ptep; 169 #endif 170 171 frame = get_sigframe(ka, regs, sizeof(*frame)); 172 173 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 174 goto give_sigsegv; 175 176 signal = current_thread_info()->exec_domain 177 && current_thread_info()->exec_domain->signal_invmap 178 && sig < 32 179 ? current_thread_info()->exec_domain->signal_invmap[sig] 180 : sig; 181 182 if (info) 183 err |= copy_siginfo_to_user(&frame->info, info); 184 185 /* Create the ucontext. */ 186 err |= __put_user(0, &frame->uc.uc_flags); 187 err |= __put_user(NULL, &frame->uc.uc_link); 188 err |= __save_altstack(&frame->uc.uc_stack, regs->r1); 189 err |= setup_sigcontext(&frame->uc.uc_mcontext, 190 regs, set->sig[0]); 191 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 192 193 /* Set up to return from userspace. If provided, use a stub 194 already in userspace. */ 195 /* minus 8 is offset to cater for "rtsd r15,8" */ 196 /* addi r12, r0, __NR_sigreturn */ 197 err |= __put_user(0x31800000 | __NR_rt_sigreturn , 198 frame->tramp + 0); 199 /* brki r14, 0x8 */ 200 err |= __put_user(0xb9cc0008, frame->tramp + 1); 201 202 /* Return from sighandler will jump to the tramp. 203 Negative 8 offset because return is rtsd r15, 8 */ 204 regs->r15 = ((unsigned long)frame->tramp)-8; 205 206 address = ((unsigned long)frame->tramp); 207 #ifdef CONFIG_MMU 208 pmdp = pmd_offset(pud_offset( 209 pgd_offset(current->mm, address), 210 address), address); 211 212 preempt_disable(); 213 ptep = pte_offset_map(pmdp, address); 214 if (pte_present(*ptep)) { 215 address = (unsigned long) page_address(pte_page(*ptep)); 216 /* MS: I need add offset in page */ 217 address += ((unsigned long)frame->tramp) & ~PAGE_MASK; 218 /* MS address is virtual */ 219 address = __virt_to_phys(address); 220 invalidate_icache_range(address, address + 8); 221 flush_dcache_range(address, address + 8); 222 } 223 pte_unmap(ptep); 224 preempt_enable(); 225 #else 226 flush_icache_range(address, address + 8); 227 flush_dcache_range(address, address + 8); 228 #endif 229 if (err) 230 goto give_sigsegv; 231 232 /* Set up registers for signal handler */ 233 regs->r1 = (unsigned long) frame; 234 235 /* Signal handler args: */ 236 regs->r5 = signal; /* arg 0: signum */ 237 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */ 238 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */ 239 /* Offset to handle microblaze rtid r14, 0 */ 240 regs->pc = (unsigned long)ka->sa.sa_handler; 241 242 set_fs(USER_DS); 243 244 #ifdef DEBUG_SIG 245 pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n", 246 current->comm, current->pid, frame, regs->pc); 247 #endif 248 249 return 0; 250 251 give_sigsegv: 252 force_sigsegv(sig, current); 253 return -EFAULT; 254 } 255 256 /* Handle restarting system calls */ 257 static inline void 258 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler) 259 { 260 switch (regs->r3) { 261 case -ERESTART_RESTARTBLOCK: 262 case -ERESTARTNOHAND: 263 if (!has_handler) 264 goto do_restart; 265 regs->r3 = -EINTR; 266 break; 267 case -ERESTARTSYS: 268 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) { 269 regs->r3 = -EINTR; 270 break; 271 } 272 /* fallthrough */ 273 case -ERESTARTNOINTR: 274 do_restart: 275 /* offset of 4 bytes to re-execute trap (brki) instruction */ 276 regs->pc -= 4; 277 break; 278 } 279 } 280 281 /* 282 * OK, we're invoking a handler 283 */ 284 285 static void 286 handle_signal(unsigned long sig, struct k_sigaction *ka, 287 siginfo_t *info, struct pt_regs *regs) 288 { 289 sigset_t *oldset = sigmask_to_save(); 290 int ret; 291 292 /* Set up the stack frame */ 293 if (ka->sa.sa_flags & SA_SIGINFO) 294 ret = setup_rt_frame(sig, ka, info, oldset, regs); 295 else 296 ret = setup_rt_frame(sig, ka, NULL, oldset, regs); 297 298 if (ret) 299 return; 300 301 signal_delivered(sig, info, ka, regs, 302 test_thread_flag(TIF_SINGLESTEP)); 303 } 304 305 /* 306 * Note that 'init' is a special process: it doesn't get signals it doesn't 307 * want to handle. Thus you cannot kill init even with a SIGKILL even by 308 * mistake. 309 * 310 * Note that we go through the signals twice: once to check the signals that 311 * the kernel can handle, and then we build all the user-level signal handling 312 * stack-frames in one go after that. 313 */ 314 static void do_signal(struct pt_regs *regs, int in_syscall) 315 { 316 siginfo_t info; 317 int signr; 318 struct k_sigaction ka; 319 #ifdef DEBUG_SIG 320 pr_info("do signal: %p %d\n", regs, in_syscall); 321 pr_info("do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1, 322 regs->r12, current_thread_info()->flags); 323 #endif 324 325 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 326 if (signr > 0) { 327 /* Whee! Actually deliver the signal. */ 328 if (in_syscall) 329 handle_restart(regs, &ka, 1); 330 handle_signal(signr, &ka, &info, regs); 331 return; 332 } 333 334 if (in_syscall) 335 handle_restart(regs, NULL, 0); 336 337 /* 338 * If there's no signal to deliver, we just put the saved sigmask 339 * back. 340 */ 341 restore_saved_sigmask(); 342 } 343 344 asmlinkage void do_notify_resume(struct pt_regs *regs, int in_syscall) 345 { 346 if (test_thread_flag(TIF_SIGPENDING)) 347 do_signal(regs, in_syscall); 348 349 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME)) 350 tracehook_notify_resume(regs); 351 } 352