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 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/resume_user_mode.h> 35 #include <asm/entry.h> 36 #include <asm/registers.h> 37 #include <asm/ucontext.h> 38 #include <linux/uaccess.h> 39 #include <linux/syscalls.h> 40 #include <asm/cacheflush.h> 41 #include <asm/syscalls.h> 42 43 /* 44 * Do a signal return; undo the signal stack. 45 */ 46 struct sigframe { 47 struct sigcontext sc; 48 unsigned long extramask[_NSIG_WORDS-1]; 49 unsigned long tramp[2]; /* signal trampoline */ 50 }; 51 52 struct rt_sigframe { 53 /* 54 * Home area for the handler's register arguments: the MicroBlaze 55 * ABI reserves [r1+0] for the return address and lets the callee 56 * store r5..r10 at [r1+4]..[r1+24], and r1 points at this frame 57 * when the handler is entered. Without the gap those stores 58 * corrupt frame->info. 59 */ 60 unsigned long abi_gap[7]; 61 struct siginfo info; 62 struct ucontext uc; 63 unsigned long tramp[2]; /* signal trampoline */ 64 }; 65 66 static int restore_sigcontext(struct pt_regs *regs, 67 struct sigcontext __user *sc, int *rval_p) 68 { 69 unsigned int err = 0; 70 71 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); } 72 COPY(r0); 73 COPY(r1); 74 COPY(r2); COPY(r3); COPY(r4); COPY(r5); 75 COPY(r6); COPY(r7); COPY(r8); COPY(r9); 76 COPY(r10); COPY(r11); COPY(r12); COPY(r13); 77 COPY(r14); COPY(r15); COPY(r16); COPY(r17); 78 COPY(r18); COPY(r19); COPY(r20); COPY(r21); 79 COPY(r22); COPY(r23); COPY(r24); COPY(r25); 80 COPY(r26); COPY(r27); COPY(r28); COPY(r29); 81 COPY(r30); COPY(r31); 82 COPY(pc); COPY(ear); COPY(esr); COPY(fsr); 83 #undef COPY 84 85 /* 86 * The frame is user-writable, so restore only the user-writable 87 * status flags (carry) and keep the kernel-controlled MSR bits 88 * (UMS/VMS/IE/EE/...) from regs->msr: rtbd derives the resumed mode 89 * from UMS/VMS, so a verbatim restore would hand userspace the 90 * privileged return state. Same idea as x86 masking the restored 91 * EFLAGS to FIX_EFLAGS. 92 */ 93 { 94 unsigned long msr; 95 96 err |= __get_user(msr, &sc->regs.msr); 97 regs->msr = (regs->msr & ~(MSR_C | MSR_CC)) | 98 (msr & (MSR_C | MSR_CC)); 99 } 100 101 *rval_p = regs->r3; 102 103 return err; 104 } 105 106 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs) 107 { 108 struct rt_sigframe __user *frame = 109 (struct rt_sigframe __user *)(regs->r1); 110 111 sigset_t set; 112 int rval; 113 114 /* Always make any pending restarted system calls return -EINTR */ 115 current->restart_block.fn = do_no_restart_syscall; 116 117 if (!access_ok(frame, sizeof(*frame))) 118 goto badframe; 119 120 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 121 goto badframe; 122 123 set_current_blocked(&set); 124 125 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval)) 126 goto badframe; 127 128 if (restore_altstack(&frame->uc.uc_stack)) 129 goto badframe; 130 131 return rval; 132 133 badframe: 134 force_sig(SIGSEGV); 135 return 0; 136 } 137 138 /* 139 * Set up a signal frame. 140 */ 141 142 static int 143 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, 144 unsigned long mask) 145 { 146 int err = 0; 147 148 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); } 149 COPY(r0); 150 COPY(r1); 151 COPY(r2); COPY(r3); COPY(r4); COPY(r5); 152 COPY(r6); COPY(r7); COPY(r8); COPY(r9); 153 COPY(r10); COPY(r11); COPY(r12); COPY(r13); 154 COPY(r14); COPY(r15); COPY(r16); COPY(r17); 155 COPY(r18); COPY(r19); COPY(r20); COPY(r21); 156 COPY(r22); COPY(r23); COPY(r24); COPY(r25); 157 COPY(r26); COPY(r27); COPY(r28); COPY(r29); 158 COPY(r30); COPY(r31); 159 COPY(pc); COPY(ear); COPY(esr); COPY(fsr); 160 COPY(msr); /* restore_sigcontext() accepts only carry state */ 161 #undef COPY 162 163 err |= __put_user(mask, &sc->oldmask); 164 165 return err; 166 } 167 168 /* 169 * Determine which stack to use.. 170 */ 171 static inline void __user * 172 get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size) 173 { 174 /* Default to using normal stack */ 175 unsigned long sp = sigsp(regs->r1, ksig); 176 177 return (void __user *)((sp - frame_size) & -8UL); 178 } 179 180 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, 181 struct pt_regs *regs) 182 { 183 struct rt_sigframe __user *frame; 184 int err = 0, sig = ksig->sig; 185 unsigned long address = 0; 186 pmd_t *pmdp; 187 pte_t *ptep; 188 189 frame = get_sigframe(ksig, regs, sizeof(*frame)); 190 191 if (!access_ok(frame, sizeof(*frame))) 192 return -EFAULT; 193 194 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 195 err |= copy_siginfo_to_user(&frame->info, &ksig->info); 196 197 /* Create the ucontext. */ 198 err |= __put_user(0, &frame->uc.uc_flags); 199 err |= __put_user(NULL, &frame->uc.uc_link); 200 err |= __save_altstack(&frame->uc.uc_stack, regs->r1); 201 err |= setup_sigcontext(&frame->uc.uc_mcontext, 202 regs, set->sig[0]); 203 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 204 205 /* Set up to return from userspace. If provided, use a stub 206 already in userspace. */ 207 /* minus 8 is offset to cater for "rtsd r15,8" */ 208 /* addi r12, r0, __NR_sigreturn */ 209 err |= __put_user(0x31800000 | __NR_rt_sigreturn , 210 frame->tramp + 0); 211 /* brki r14, 0x8 */ 212 err |= __put_user(0xb9cc0008, frame->tramp + 1); 213 214 /* Return from sighandler will jump to the tramp. 215 Negative 8 offset because return is rtsd r15, 8 */ 216 regs->r15 = ((unsigned long)frame->tramp)-8; 217 218 address = ((unsigned long)frame->tramp); 219 pmdp = pmd_off(current->mm, address); 220 221 preempt_disable(); 222 ptep = pte_offset_map(pmdp, address); 223 if (ptep && pte_present(*ptep)) { 224 address = (unsigned long) page_address(pte_page(*ptep)); 225 /* MS: I need add offset in page */ 226 address += ((unsigned long)frame->tramp) & ~PAGE_MASK; 227 /* MS address is virtual */ 228 address = __virt_to_phys(address); 229 invalidate_icache_range(address, address + 8); 230 flush_dcache_range(address, address + 8); 231 } 232 if (ptep) 233 pte_unmap(ptep); 234 preempt_enable(); 235 if (err) 236 return -EFAULT; 237 238 /* Set up registers for signal handler */ 239 regs->r1 = (unsigned long) frame; 240 241 /* Signal handler args: */ 242 regs->r5 = sig; /* arg 0: signum */ 243 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */ 244 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */ 245 /* Offset to handle microblaze rtid r14, 0 */ 246 regs->pc = (unsigned long)ksig->ka.sa.sa_handler; 247 248 #ifdef DEBUG_SIG 249 pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n", 250 current->comm, current->pid, frame, regs->pc); 251 #endif 252 253 return 0; 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(struct ksignal *ksig, struct pt_regs *regs) 287 { 288 sigset_t *oldset = sigmask_to_save(); 289 int ret; 290 291 /* Set up the stack frame */ 292 ret = setup_rt_frame(ksig, oldset, regs); 293 294 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); 295 } 296 297 /* 298 * Note that 'init' is a special process: it doesn't get signals it doesn't 299 * want to handle. Thus you cannot kill init even with a SIGKILL even by 300 * mistake. 301 * 302 * Note that we go through the signals twice: once to check the signals that 303 * the kernel can handle, and then we build all the user-level signal handling 304 * stack-frames in one go after that. 305 */ 306 static void do_signal(struct pt_regs *regs, int in_syscall) 307 { 308 struct ksignal ksig; 309 310 #ifdef DEBUG_SIG 311 pr_info("do signal: %p %d\n", regs, in_syscall); 312 pr_info("do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1, 313 regs->r12, read_thread_flags()); 314 #endif 315 316 if (get_signal(&ksig)) { 317 /* Whee! Actually deliver the signal. */ 318 if (in_syscall) 319 handle_restart(regs, &ksig.ka, 1); 320 handle_signal(&ksig, regs); 321 return; 322 } 323 324 if (in_syscall) 325 handle_restart(regs, NULL, 0); 326 327 /* 328 * If there's no signal to deliver, we just put the saved sigmask 329 * back. 330 */ 331 restore_saved_sigmask(); 332 } 333 334 asmlinkage void do_notify_resume(struct pt_regs *regs, int in_syscall) 335 { 336 if (test_thread_flag(TIF_SIGPENDING) || 337 test_thread_flag(TIF_NOTIFY_SIGNAL)) 338 do_signal(regs, in_syscall); 339 340 if (test_thread_flag(TIF_NOTIFY_RESUME)) 341 resume_user_mode_work(regs); 342 } 343