| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 1991, 1992  Linus Torvalds | 
 | 3 |  * Copyright 2010 Tilera Corporation. All Rights Reserved. | 
 | 4 |  * | 
 | 5 |  *   This program is free software; you can redistribute it and/or | 
 | 6 |  *   modify it under the terms of the GNU General Public License | 
 | 7 |  *   as published by the Free Software Foundation, version 2. | 
 | 8 |  * | 
 | 9 |  *   This program is distributed in the hope that it will be useful, but | 
 | 10 |  *   WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 11 |  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | 
 | 12 |  *   NON INFRINGEMENT.  See the GNU General Public License for | 
 | 13 |  *   more details. | 
 | 14 |  */ | 
 | 15 |  | 
 | 16 | #include <linux/sched.h> | 
 | 17 | #include <linux/mm.h> | 
 | 18 | #include <linux/smp.h> | 
 | 19 | #include <linux/smp_lock.h> | 
 | 20 | #include <linux/kernel.h> | 
 | 21 | #include <linux/signal.h> | 
 | 22 | #include <linux/errno.h> | 
 | 23 | #include <linux/wait.h> | 
 | 24 | #include <linux/unistd.h> | 
 | 25 | #include <linux/stddef.h> | 
 | 26 | #include <linux/personality.h> | 
 | 27 | #include <linux/suspend.h> | 
 | 28 | #include <linux/ptrace.h> | 
 | 29 | #include <linux/elf.h> | 
 | 30 | #include <linux/compat.h> | 
 | 31 | #include <linux/syscalls.h> | 
 | 32 | #include <linux/uaccess.h> | 
 | 33 | #include <asm/processor.h> | 
 | 34 | #include <asm/ucontext.h> | 
 | 35 | #include <asm/sigframe.h> | 
| Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 36 | #include <asm/syscalls.h> | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 37 | #include <arch/interrupts.h> | 
 | 38 |  | 
 | 39 | #define DEBUG_SIG 0 | 
 | 40 |  | 
 | 41 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) | 
 | 42 |  | 
 | 43 |  | 
| Chris Metcalf | 139ef32 | 2010-06-07 08:48:13 -0400 | [diff] [blame] | 44 | long _sys_sigaltstack(const stack_t __user *uss, | 
| Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 45 | 		      stack_t __user *uoss, struct pt_regs *regs) | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 46 | { | 
 | 47 | 	return do_sigaltstack(uss, uoss, regs->sp); | 
 | 48 | } | 
 | 49 |  | 
 | 50 |  | 
 | 51 | /* | 
 | 52 |  * Do a signal return; undo the signal stack. | 
 | 53 |  */ | 
 | 54 |  | 
 | 55 | int restore_sigcontext(struct pt_regs *regs, | 
 | 56 | 		       struct sigcontext __user *sc, long *pr0) | 
 | 57 | { | 
 | 58 | 	int err = 0; | 
 | 59 | 	int i; | 
 | 60 |  | 
 | 61 | 	/* Always make any pending restarted system calls return -EINTR */ | 
 | 62 | 	current_thread_info()->restart_block.fn = do_no_restart_syscall; | 
 | 63 |  | 
 | 64 | 	for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i) | 
 | 65 | 		err |= __get_user(((long *)regs)[i], | 
| Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 66 | 				  &((long __user *)(&sc->regs))[i]); | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 67 |  | 
 | 68 | 	regs->faultnum = INT_SWINT_1_SIGRETURN; | 
 | 69 |  | 
 | 70 | 	err |= __get_user(*pr0, &sc->regs.regs[0]); | 
 | 71 | 	return err; | 
 | 72 | } | 
 | 73 |  | 
| Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 74 | /* sigreturn() returns long since it restores r0 in the interrupted code. */ | 
 | 75 | long _sys_rt_sigreturn(struct pt_regs *regs) | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 76 | { | 
 | 77 | 	struct rt_sigframe __user *frame = | 
 | 78 | 		(struct rt_sigframe __user *)(regs->sp); | 
 | 79 | 	sigset_t set; | 
 | 80 | 	long r0; | 
 | 81 |  | 
 | 82 | 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | 
 | 83 | 		goto badframe; | 
 | 84 | 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | 
 | 85 | 		goto badframe; | 
 | 86 |  | 
 | 87 | 	sigdelsetmask(&set, ~_BLOCKABLE); | 
 | 88 | 	spin_lock_irq(¤t->sighand->siglock); | 
 | 89 | 	current->blocked = set; | 
 | 90 | 	recalc_sigpending(); | 
 | 91 | 	spin_unlock_irq(¤t->sighand->siglock); | 
 | 92 |  | 
 | 93 | 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0)) | 
 | 94 | 		goto badframe; | 
 | 95 |  | 
 | 96 | 	if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT) | 
 | 97 | 		goto badframe; | 
 | 98 |  | 
 | 99 | 	return r0; | 
 | 100 |  | 
 | 101 | badframe: | 
 | 102 | 	force_sig(SIGSEGV, current); | 
 | 103 | 	return 0; | 
 | 104 | } | 
 | 105 |  | 
 | 106 | /* | 
 | 107 |  * Set up a signal frame. | 
 | 108 |  */ | 
 | 109 |  | 
 | 110 | int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs) | 
 | 111 | { | 
 | 112 | 	int i, err = 0; | 
 | 113 |  | 
 | 114 | 	for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i) | 
 | 115 | 		err |= __put_user(((long *)regs)[i], | 
| Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 116 | 				  &((long __user *)(&sc->regs))[i]); | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 117 |  | 
 | 118 | 	return err; | 
 | 119 | } | 
 | 120 |  | 
 | 121 | /* | 
 | 122 |  * Determine which stack to use.. | 
 | 123 |  */ | 
 | 124 | static inline void __user *get_sigframe(struct k_sigaction *ka, | 
 | 125 | 					struct pt_regs *regs, | 
 | 126 | 					size_t frame_size) | 
 | 127 | { | 
 | 128 | 	unsigned long sp; | 
 | 129 |  | 
 | 130 | 	/* Default to using normal stack */ | 
 | 131 | 	sp = regs->sp; | 
 | 132 |  | 
 | 133 | 	/* | 
 | 134 | 	 * If we are on the alternate signal stack and would overflow | 
 | 135 | 	 * it, don't.  Return an always-bogus address instead so we | 
 | 136 | 	 * will die with SIGSEGV. | 
 | 137 | 	 */ | 
 | 138 | 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) | 
| Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 139 | 		return (void __user __force *)-1UL; | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 140 |  | 
 | 141 | 	/* This is the X/Open sanctioned signal stack switching.  */ | 
 | 142 | 	if (ka->sa.sa_flags & SA_ONSTACK) { | 
 | 143 | 		if (sas_ss_flags(sp) == 0) | 
 | 144 | 			sp = current->sas_ss_sp + current->sas_ss_size; | 
 | 145 | 	} | 
 | 146 |  | 
 | 147 | 	sp -= frame_size; | 
 | 148 | 	/* | 
 | 149 | 	 * Align the stack pointer according to the TILE ABI, | 
 | 150 | 	 * i.e. so that on function entry (sp & 15) == 0. | 
 | 151 | 	 */ | 
 | 152 | 	sp &= -16UL; | 
 | 153 | 	return (void __user *) sp; | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, | 
 | 157 | 			   sigset_t *set, struct pt_regs *regs) | 
 | 158 | { | 
 | 159 | 	unsigned long restorer; | 
 | 160 | 	struct rt_sigframe __user *frame; | 
 | 161 | 	int err = 0; | 
 | 162 | 	int usig; | 
 | 163 |  | 
 | 164 | 	frame = get_sigframe(ka, regs, sizeof(*frame)); | 
 | 165 |  | 
 | 166 | 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | 
 | 167 | 		goto give_sigsegv; | 
 | 168 |  | 
 | 169 | 	usig = current_thread_info()->exec_domain | 
 | 170 | 		&& current_thread_info()->exec_domain->signal_invmap | 
 | 171 | 		&& sig < 32 | 
 | 172 | 		? current_thread_info()->exec_domain->signal_invmap[sig] | 
 | 173 | 		: sig; | 
 | 174 |  | 
 | 175 | 	/* Always write at least the signal number for the stack backtracer. */ | 
 | 176 | 	if (ka->sa.sa_flags & SA_SIGINFO) { | 
 | 177 | 		/* At sigreturn time, restore the callee-save registers too. */ | 
 | 178 | 		err |= copy_siginfo_to_user(&frame->info, info); | 
 | 179 | 		regs->flags |= PT_FLAGS_RESTORE_REGS; | 
 | 180 | 	} else { | 
 | 181 | 		err |= __put_user(info->si_signo, &frame->info.si_signo); | 
 | 182 | 	} | 
 | 183 |  | 
 | 184 | 	/* Create the ucontext.  */ | 
 | 185 | 	err |= __clear_user(&frame->save_area, sizeof(frame->save_area)); | 
 | 186 | 	err |= __put_user(0, &frame->uc.uc_flags); | 
| Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 187 | 	err |= __put_user(NULL, &frame->uc.uc_link); | 
 | 188 | 	err |= __put_user((void __user *)(current->sas_ss_sp), | 
| Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 189 | 			  &frame->uc.uc_stack.ss_sp); | 
 | 190 | 	err |= __put_user(sas_ss_flags(regs->sp), | 
 | 191 | 			  &frame->uc.uc_stack.ss_flags); | 
 | 192 | 	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); | 
 | 193 | 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs); | 
 | 194 | 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | 
 | 195 | 	if (err) | 
 | 196 | 		goto give_sigsegv; | 
 | 197 |  | 
 | 198 | 	restorer = VDSO_BASE; | 
 | 199 | 	if (ka->sa.sa_flags & SA_RESTORER) | 
 | 200 | 		restorer = (unsigned long) ka->sa.sa_restorer; | 
 | 201 |  | 
 | 202 | 	/* | 
 | 203 | 	 * Set up registers for signal handler. | 
 | 204 | 	 * Registers that we don't modify keep the value they had from | 
 | 205 | 	 * user-space at the time we took the signal. | 
 | 206 | 	 */ | 
 | 207 | 	regs->pc = (unsigned long) ka->sa.sa_handler; | 
 | 208 | 	regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */ | 
 | 209 | 	regs->sp = (unsigned long) frame; | 
 | 210 | 	regs->lr = restorer; | 
 | 211 | 	regs->regs[0] = (unsigned long) usig; | 
 | 212 |  | 
 | 213 | 	if (ka->sa.sa_flags & SA_SIGINFO) { | 
 | 214 | 		/* Need extra arguments, so mark to restore caller-saves. */ | 
 | 215 | 		regs->regs[1] = (unsigned long) &frame->info; | 
 | 216 | 		regs->regs[2] = (unsigned long) &frame->uc; | 
 | 217 | 		regs->flags |= PT_FLAGS_CALLER_SAVES; | 
 | 218 | 	} | 
 | 219 |  | 
 | 220 | 	/* | 
 | 221 | 	 * Notify any tracer that was single-stepping it. | 
 | 222 | 	 * The tracer may want to single-step inside the | 
 | 223 | 	 * handler too. | 
 | 224 | 	 */ | 
 | 225 | 	if (test_thread_flag(TIF_SINGLESTEP)) | 
 | 226 | 		ptrace_notify(SIGTRAP); | 
 | 227 |  | 
 | 228 | 	return 0; | 
 | 229 |  | 
 | 230 | give_sigsegv: | 
 | 231 | 	force_sigsegv(sig, current); | 
 | 232 | 	return -EFAULT; | 
 | 233 | } | 
 | 234 |  | 
 | 235 | /* | 
 | 236 |  * OK, we're invoking a handler | 
 | 237 |  */ | 
 | 238 |  | 
 | 239 | static int handle_signal(unsigned long sig, siginfo_t *info, | 
 | 240 | 			 struct k_sigaction *ka, sigset_t *oldset, | 
 | 241 | 			 struct pt_regs *regs) | 
 | 242 | { | 
 | 243 | 	int ret; | 
 | 244 |  | 
 | 245 |  | 
 | 246 | 	/* Are we from a system call? */ | 
 | 247 | 	if (regs->faultnum == INT_SWINT_1) { | 
 | 248 | 		/* If so, check system call restarting.. */ | 
 | 249 | 		switch (regs->regs[0]) { | 
 | 250 | 		case -ERESTART_RESTARTBLOCK: | 
 | 251 | 		case -ERESTARTNOHAND: | 
 | 252 | 			regs->regs[0] = -EINTR; | 
 | 253 | 			break; | 
 | 254 |  | 
 | 255 | 		case -ERESTARTSYS: | 
 | 256 | 			if (!(ka->sa.sa_flags & SA_RESTART)) { | 
 | 257 | 				regs->regs[0] = -EINTR; | 
 | 258 | 				break; | 
 | 259 | 			} | 
 | 260 | 			/* fallthrough */ | 
 | 261 | 		case -ERESTARTNOINTR: | 
 | 262 | 			/* Reload caller-saves to restore r0..r5 and r10. */ | 
 | 263 | 			regs->flags |= PT_FLAGS_CALLER_SAVES; | 
 | 264 | 			regs->regs[0] = regs->orig_r0; | 
 | 265 | 			regs->pc -= 8; | 
 | 266 | 		} | 
 | 267 | 	} | 
 | 268 |  | 
 | 269 | 	/* Set up the stack frame */ | 
 | 270 | #ifdef CONFIG_COMPAT | 
 | 271 | 	if (is_compat_task()) | 
 | 272 | 		ret = compat_setup_rt_frame(sig, ka, info, oldset, regs); | 
 | 273 | 	else | 
 | 274 | #endif | 
 | 275 | 		ret = setup_rt_frame(sig, ka, info, oldset, regs); | 
 | 276 | 	if (ret == 0) { | 
 | 277 | 		/* This code is only called from system calls or from | 
 | 278 | 		 * the work_pending path in the return-to-user code, and | 
 | 279 | 		 * either way we can re-enable interrupts unconditionally. | 
 | 280 | 		 */ | 
 | 281 | 		spin_lock_irq(¤t->sighand->siglock); | 
 | 282 | 		sigorsets(¤t->blocked, | 
 | 283 | 			  ¤t->blocked, &ka->sa.sa_mask); | 
 | 284 | 		if (!(ka->sa.sa_flags & SA_NODEFER)) | 
 | 285 | 			sigaddset(¤t->blocked, sig); | 
 | 286 | 		recalc_sigpending(); | 
 | 287 | 		spin_unlock_irq(¤t->sighand->siglock); | 
 | 288 | 	} | 
 | 289 |  | 
 | 290 | 	return ret; | 
 | 291 | } | 
 | 292 |  | 
 | 293 | /* | 
 | 294 |  * Note that 'init' is a special process: it doesn't get signals it doesn't | 
 | 295 |  * want to handle. Thus you cannot kill init even with a SIGKILL even by | 
 | 296 |  * mistake. | 
 | 297 |  */ | 
 | 298 | void do_signal(struct pt_regs *regs) | 
 | 299 | { | 
 | 300 | 	siginfo_t info; | 
 | 301 | 	int signr; | 
 | 302 | 	struct k_sigaction ka; | 
 | 303 | 	sigset_t *oldset; | 
 | 304 |  | 
 | 305 | 	/* | 
 | 306 | 	 * i386 will check if we're coming from kernel mode and bail out | 
 | 307 | 	 * here.  In my experience this just turns weird crashes into | 
 | 308 | 	 * weird spin-hangs.  But if we find a case where this seems | 
 | 309 | 	 * helpful, we can reinstate the check on "!user_mode(regs)". | 
 | 310 | 	 */ | 
 | 311 |  | 
 | 312 | 	if (current_thread_info()->status & TS_RESTORE_SIGMASK) | 
 | 313 | 		oldset = ¤t->saved_sigmask; | 
 | 314 | 	else | 
 | 315 | 		oldset = ¤t->blocked; | 
 | 316 |  | 
 | 317 | 	signr = get_signal_to_deliver(&info, &ka, regs, NULL); | 
 | 318 | 	if (signr > 0) { | 
 | 319 | 		/* Whee! Actually deliver the signal.  */ | 
 | 320 | 		if (handle_signal(signr, &info, &ka, oldset, regs) == 0) { | 
 | 321 | 			/* | 
 | 322 | 			 * A signal was successfully delivered; the saved | 
 | 323 | 			 * sigmask will have been stored in the signal frame, | 
 | 324 | 			 * and will be restored by sigreturn, so we can simply | 
 | 325 | 			 * clear the TS_RESTORE_SIGMASK flag. | 
 | 326 | 			 */ | 
 | 327 | 			current_thread_info()->status &= ~TS_RESTORE_SIGMASK; | 
 | 328 | 		} | 
 | 329 |  | 
 | 330 | 		return; | 
 | 331 | 	} | 
 | 332 |  | 
 | 333 | 	/* Did we come from a system call? */ | 
 | 334 | 	if (regs->faultnum == INT_SWINT_1) { | 
 | 335 | 		/* Restart the system call - no handlers present */ | 
 | 336 | 		switch (regs->regs[0]) { | 
 | 337 | 		case -ERESTARTNOHAND: | 
 | 338 | 		case -ERESTARTSYS: | 
 | 339 | 		case -ERESTARTNOINTR: | 
 | 340 | 			regs->flags |= PT_FLAGS_CALLER_SAVES; | 
 | 341 | 			regs->regs[0] = regs->orig_r0; | 
 | 342 | 			regs->pc -= 8; | 
 | 343 | 			break; | 
 | 344 |  | 
 | 345 | 		case -ERESTART_RESTARTBLOCK: | 
 | 346 | 			regs->flags |= PT_FLAGS_CALLER_SAVES; | 
 | 347 | 			regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall; | 
 | 348 | 			regs->pc -= 8; | 
 | 349 | 			break; | 
 | 350 | 		} | 
 | 351 | 	} | 
 | 352 |  | 
 | 353 | 	/* If there's no signal to deliver, just put the saved sigmask back. */ | 
 | 354 | 	if (current_thread_info()->status & TS_RESTORE_SIGMASK) { | 
 | 355 | 		current_thread_info()->status &= ~TS_RESTORE_SIGMASK; | 
 | 356 | 		sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); | 
 | 357 | 	} | 
 | 358 | } |