xref: /openbmc/linux/arch/arm/kernel/signal.c (revision 87c2ce3b)
1 /*
2  *  linux/arch/arm/kernel/signal.c
3  *
4  *  Copyright (C) 1995-2002 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/config.h>
11 #include <linux/errno.h>
12 #include <linux/signal.h>
13 #include <linux/ptrace.h>
14 #include <linux/personality.h>
15 
16 #include <asm/cacheflush.h>
17 #include <asm/ucontext.h>
18 #include <asm/uaccess.h>
19 #include <asm/unistd.h>
20 
21 #include "ptrace.h"
22 #include "signal.h"
23 
24 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
25 
26 /*
27  * For ARM syscalls, we encode the syscall number into the instruction.
28  */
29 #define SWI_SYS_SIGRETURN	(0xef000000|(__NR_sigreturn))
30 #define SWI_SYS_RT_SIGRETURN	(0xef000000|(__NR_rt_sigreturn))
31 
32 /*
33  * For Thumb syscalls, we pass the syscall number via r7.  We therefore
34  * need two 16-bit instructions.
35  */
36 #define SWI_THUMB_SIGRETURN	(0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
37 #define SWI_THUMB_RT_SIGRETURN	(0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
38 
39 const unsigned long sigreturn_codes[4] = {
40 	SWI_SYS_SIGRETURN,	SWI_THUMB_SIGRETURN,
41 	SWI_SYS_RT_SIGRETURN,	SWI_THUMB_RT_SIGRETURN
42 };
43 
44 static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
45 
46 /*
47  * atomically swap in the new signal mask, and wait for a signal.
48  */
49 asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
50 {
51 	sigset_t saveset;
52 
53 	mask &= _BLOCKABLE;
54 	spin_lock_irq(&current->sighand->siglock);
55 	saveset = current->blocked;
56 	siginitset(&current->blocked, mask);
57 	recalc_sigpending();
58 	spin_unlock_irq(&current->sighand->siglock);
59 	regs->ARM_r0 = -EINTR;
60 
61 	while (1) {
62 		current->state = TASK_INTERRUPTIBLE;
63 		schedule();
64 		if (do_signal(&saveset, regs, 0))
65 			return regs->ARM_r0;
66 	}
67 }
68 
69 asmlinkage int
70 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
71 {
72 	sigset_t saveset, newset;
73 
74 	/* XXX: Don't preclude handling different sized sigset_t's. */
75 	if (sigsetsize != sizeof(sigset_t))
76 		return -EINVAL;
77 
78 	if (copy_from_user(&newset, unewset, sizeof(newset)))
79 		return -EFAULT;
80 	sigdelsetmask(&newset, ~_BLOCKABLE);
81 
82 	spin_lock_irq(&current->sighand->siglock);
83 	saveset = current->blocked;
84 	current->blocked = newset;
85 	recalc_sigpending();
86 	spin_unlock_irq(&current->sighand->siglock);
87 	regs->ARM_r0 = -EINTR;
88 
89 	while (1) {
90 		current->state = TASK_INTERRUPTIBLE;
91 		schedule();
92 		if (do_signal(&saveset, regs, 0))
93 			return regs->ARM_r0;
94 	}
95 }
96 
97 asmlinkage int
98 sys_sigaction(int sig, const struct old_sigaction __user *act,
99 	      struct old_sigaction __user *oact)
100 {
101 	struct k_sigaction new_ka, old_ka;
102 	int ret;
103 
104 	if (act) {
105 		old_sigset_t mask;
106 		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
107 		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
108 		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
109 			return -EFAULT;
110 		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
111 		__get_user(mask, &act->sa_mask);
112 		siginitset(&new_ka.sa.sa_mask, mask);
113 	}
114 
115 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
116 
117 	if (!ret && oact) {
118 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
119 		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
120 		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
121 			return -EFAULT;
122 		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
123 		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
124 	}
125 
126 	return ret;
127 }
128 
129 #ifdef CONFIG_IWMMXT
130 
131 /* iwmmxt_area is 0x98 bytes long, preceeded by 8 bytes of signature */
132 #define IWMMXT_STORAGE_SIZE	(0x98 + 8)
133 #define IWMMXT_MAGIC0		0x12ef842a
134 #define IWMMXT_MAGIC1		0x1c07ca71
135 
136 struct iwmmxt_sigframe {
137 	unsigned long	magic0;
138 	unsigned long	magic1;
139 	unsigned long	storage[0x98/4];
140 };
141 
142 static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
143 {
144 	char kbuf[sizeof(*frame) + 8];
145 	struct iwmmxt_sigframe *kframe;
146 
147 	/* the iWMMXt context must be 64 bit aligned */
148 	kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
149 	kframe->magic0 = IWMMXT_MAGIC0;
150 	kframe->magic1 = IWMMXT_MAGIC1;
151 	iwmmxt_task_copy(current_thread_info(), &kframe->storage);
152 	return __copy_to_user(frame, kframe, sizeof(*frame));
153 }
154 
155 static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
156 {
157 	char kbuf[sizeof(*frame) + 8];
158 	struct iwmmxt_sigframe *kframe;
159 
160 	/* the iWMMXt context must be 64 bit aligned */
161 	kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
162 	if (__copy_from_user(kframe, frame, sizeof(*frame)))
163 		return -1;
164 	if (kframe->magic0 != IWMMXT_MAGIC0 ||
165 	    kframe->magic1 != IWMMXT_MAGIC1)
166 		return -1;
167 	iwmmxt_task_restore(current_thread_info(), &kframe->storage);
168 	return 0;
169 }
170 
171 #endif
172 
173 /*
174  * Auxiliary signal frame.  This saves stuff like FP state.
175  * The layout of this structure is not part of the user ABI.
176  */
177 struct aux_sigframe {
178 #ifdef CONFIG_IWMMXT
179 	struct iwmmxt_sigframe	iwmmxt;
180 #endif
181 #ifdef CONFIG_VFP
182 	union vfp_state		vfp;
183 #endif
184 };
185 
186 /*
187  * Do a signal return; undo the signal stack.  These are aligned to 64-bit.
188  */
189 struct sigframe {
190 	struct sigcontext sc;
191 	unsigned long extramask[_NSIG_WORDS-1];
192 	unsigned long retcode;
193 	struct aux_sigframe aux __attribute__((aligned(8)));
194 };
195 
196 struct rt_sigframe {
197 	struct siginfo __user *pinfo;
198 	void __user *puc;
199 	struct siginfo info;
200 	struct ucontext uc;
201 	unsigned long retcode;
202 	struct aux_sigframe aux __attribute__((aligned(8)));
203 };
204 
205 static int
206 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
207 		   struct aux_sigframe __user *aux)
208 {
209 	int err = 0;
210 
211 	__get_user_error(regs->ARM_r0, &sc->arm_r0, err);
212 	__get_user_error(regs->ARM_r1, &sc->arm_r1, err);
213 	__get_user_error(regs->ARM_r2, &sc->arm_r2, err);
214 	__get_user_error(regs->ARM_r3, &sc->arm_r3, err);
215 	__get_user_error(regs->ARM_r4, &sc->arm_r4, err);
216 	__get_user_error(regs->ARM_r5, &sc->arm_r5, err);
217 	__get_user_error(regs->ARM_r6, &sc->arm_r6, err);
218 	__get_user_error(regs->ARM_r7, &sc->arm_r7, err);
219 	__get_user_error(regs->ARM_r8, &sc->arm_r8, err);
220 	__get_user_error(regs->ARM_r9, &sc->arm_r9, err);
221 	__get_user_error(regs->ARM_r10, &sc->arm_r10, err);
222 	__get_user_error(regs->ARM_fp, &sc->arm_fp, err);
223 	__get_user_error(regs->ARM_ip, &sc->arm_ip, err);
224 	__get_user_error(regs->ARM_sp, &sc->arm_sp, err);
225 	__get_user_error(regs->ARM_lr, &sc->arm_lr, err);
226 	__get_user_error(regs->ARM_pc, &sc->arm_pc, err);
227 	__get_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
228 
229 	err |= !valid_user_regs(regs);
230 
231 #ifdef CONFIG_IWMMXT
232 	if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
233 		err |= restore_iwmmxt_context(&aux->iwmmxt);
234 #endif
235 #ifdef CONFIG_VFP
236 //	if (err == 0)
237 //		err |= vfp_restore_state(&aux->vfp);
238 #endif
239 
240 	return err;
241 }
242 
243 asmlinkage int sys_sigreturn(struct pt_regs *regs)
244 {
245 	struct sigframe __user *frame;
246 	sigset_t set;
247 
248 	/* Always make any pending restarted system calls return -EINTR */
249 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
250 
251 	/*
252 	 * Since we stacked the signal on a 64-bit boundary,
253 	 * then 'sp' should be word aligned here.  If it's
254 	 * not, then the user is trying to mess with us.
255 	 */
256 	if (regs->ARM_sp & 7)
257 		goto badframe;
258 
259 	frame = (struct sigframe __user *)regs->ARM_sp;
260 
261 	if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
262 		goto badframe;
263 	if (__get_user(set.sig[0], &frame->sc.oldmask)
264 	    || (_NSIG_WORDS > 1
265 	        && __copy_from_user(&set.sig[1], &frame->extramask,
266 				    sizeof(frame->extramask))))
267 		goto badframe;
268 
269 	sigdelsetmask(&set, ~_BLOCKABLE);
270 	spin_lock_irq(&current->sighand->siglock);
271 	current->blocked = set;
272 	recalc_sigpending();
273 	spin_unlock_irq(&current->sighand->siglock);
274 
275 	if (restore_sigcontext(regs, &frame->sc, &frame->aux))
276 		goto badframe;
277 
278 	/* Send SIGTRAP if we're single-stepping */
279 	if (current->ptrace & PT_SINGLESTEP) {
280 		ptrace_cancel_bpt(current);
281 		send_sig(SIGTRAP, current, 1);
282 	}
283 
284 	return regs->ARM_r0;
285 
286 badframe:
287 	force_sig(SIGSEGV, current);
288 	return 0;
289 }
290 
291 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
292 {
293 	struct rt_sigframe __user *frame;
294 	sigset_t set;
295 
296 	/* Always make any pending restarted system calls return -EINTR */
297 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
298 
299 	/*
300 	 * Since we stacked the signal on a 64-bit boundary,
301 	 * then 'sp' should be word aligned here.  If it's
302 	 * not, then the user is trying to mess with us.
303 	 */
304 	if (regs->ARM_sp & 7)
305 		goto badframe;
306 
307 	frame = (struct rt_sigframe __user *)regs->ARM_sp;
308 
309 	if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
310 		goto badframe;
311 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
312 		goto badframe;
313 
314 	sigdelsetmask(&set, ~_BLOCKABLE);
315 	spin_lock_irq(&current->sighand->siglock);
316 	current->blocked = set;
317 	recalc_sigpending();
318 	spin_unlock_irq(&current->sighand->siglock);
319 
320 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &frame->aux))
321 		goto badframe;
322 
323 	if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
324 		goto badframe;
325 
326 	/* Send SIGTRAP if we're single-stepping */
327 	if (current->ptrace & PT_SINGLESTEP) {
328 		ptrace_cancel_bpt(current);
329 		send_sig(SIGTRAP, current, 1);
330 	}
331 
332 	return regs->ARM_r0;
333 
334 badframe:
335 	force_sig(SIGSEGV, current);
336 	return 0;
337 }
338 
339 static int
340 setup_sigcontext(struct sigcontext __user *sc, struct aux_sigframe __user *aux,
341 		 struct pt_regs *regs, unsigned long mask)
342 {
343 	int err = 0;
344 
345 	__put_user_error(regs->ARM_r0, &sc->arm_r0, err);
346 	__put_user_error(regs->ARM_r1, &sc->arm_r1, err);
347 	__put_user_error(regs->ARM_r2, &sc->arm_r2, err);
348 	__put_user_error(regs->ARM_r3, &sc->arm_r3, err);
349 	__put_user_error(regs->ARM_r4, &sc->arm_r4, err);
350 	__put_user_error(regs->ARM_r5, &sc->arm_r5, err);
351 	__put_user_error(regs->ARM_r6, &sc->arm_r6, err);
352 	__put_user_error(regs->ARM_r7, &sc->arm_r7, err);
353 	__put_user_error(regs->ARM_r8, &sc->arm_r8, err);
354 	__put_user_error(regs->ARM_r9, &sc->arm_r9, err);
355 	__put_user_error(regs->ARM_r10, &sc->arm_r10, err);
356 	__put_user_error(regs->ARM_fp, &sc->arm_fp, err);
357 	__put_user_error(regs->ARM_ip, &sc->arm_ip, err);
358 	__put_user_error(regs->ARM_sp, &sc->arm_sp, err);
359 	__put_user_error(regs->ARM_lr, &sc->arm_lr, err);
360 	__put_user_error(regs->ARM_pc, &sc->arm_pc, err);
361 	__put_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
362 
363 	__put_user_error(current->thread.trap_no, &sc->trap_no, err);
364 	__put_user_error(current->thread.error_code, &sc->error_code, err);
365 	__put_user_error(current->thread.address, &sc->fault_address, err);
366 	__put_user_error(mask, &sc->oldmask, err);
367 
368 #ifdef CONFIG_IWMMXT
369 	if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
370 		err |= preserve_iwmmxt_context(&aux->iwmmxt);
371 #endif
372 #ifdef CONFIG_VFP
373 //	if (err == 0)
374 //		err |= vfp_save_state(&aux->vfp);
375 #endif
376 
377 	return err;
378 }
379 
380 static inline void __user *
381 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
382 {
383 	unsigned long sp = regs->ARM_sp;
384 	void __user *frame;
385 
386 	/*
387 	 * This is the X/Open sanctioned signal stack switching.
388 	 */
389 	if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
390 		sp = current->sas_ss_sp + current->sas_ss_size;
391 
392 	/*
393 	 * ATPCS B01 mandates 8-byte alignment
394 	 */
395 	frame = (void __user *)((sp - framesize) & ~7);
396 
397 	/*
398 	 * Check that we can actually write to the signal frame.
399 	 */
400 	if (!access_ok(VERIFY_WRITE, frame, framesize))
401 		frame = NULL;
402 
403 	return frame;
404 }
405 
406 static int
407 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
408 	     unsigned long __user *rc, void __user *frame, int usig)
409 {
410 	unsigned long handler = (unsigned long)ka->sa.sa_handler;
411 	unsigned long retcode;
412 	int thumb = 0;
413 	unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
414 
415 	/*
416 	 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
417 	 */
418 	if (ka->sa.sa_flags & SA_THIRTYTWO)
419 		cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
420 
421 #ifdef CONFIG_ARM_THUMB
422 	if (elf_hwcap & HWCAP_THUMB) {
423 		/*
424 		 * The LSB of the handler determines if we're going to
425 		 * be using THUMB or ARM mode for this signal handler.
426 		 */
427 		thumb = handler & 1;
428 
429 		if (thumb)
430 			cpsr |= PSR_T_BIT;
431 		else
432 			cpsr &= ~PSR_T_BIT;
433 	}
434 #endif
435 
436 	if (ka->sa.sa_flags & SA_RESTORER) {
437 		retcode = (unsigned long)ka->sa.sa_restorer;
438 	} else {
439 		unsigned int idx = thumb;
440 
441 		if (ka->sa.sa_flags & SA_SIGINFO)
442 			idx += 2;
443 
444 		if (__put_user(sigreturn_codes[idx], rc))
445 			return 1;
446 
447 		if (cpsr & MODE32_BIT) {
448 			/*
449 			 * 32-bit code can use the new high-page
450 			 * signal return code support.
451 			 */
452 			retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
453 		} else {
454 			/*
455 			 * Ensure that the instruction cache sees
456 			 * the return code written onto the stack.
457 			 */
458 			flush_icache_range((unsigned long)rc,
459 					   (unsigned long)(rc + 1));
460 
461 			retcode = ((unsigned long)rc) + thumb;
462 		}
463 	}
464 
465 	regs->ARM_r0 = usig;
466 	regs->ARM_sp = (unsigned long)frame;
467 	regs->ARM_lr = retcode;
468 	regs->ARM_pc = handler;
469 	regs->ARM_cpsr = cpsr;
470 
471 	return 0;
472 }
473 
474 static int
475 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
476 {
477 	struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
478 	int err = 0;
479 
480 	if (!frame)
481 		return 1;
482 
483 	err |= setup_sigcontext(&frame->sc, &frame->aux, regs, set->sig[0]);
484 
485 	if (_NSIG_WORDS > 1) {
486 		err |= __copy_to_user(frame->extramask, &set->sig[1],
487 				      sizeof(frame->extramask));
488 	}
489 
490 	if (err == 0)
491 		err = setup_return(regs, ka, &frame->retcode, frame, usig);
492 
493 	return err;
494 }
495 
496 static int
497 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
498 	       sigset_t *set, struct pt_regs *regs)
499 {
500 	struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
501 	stack_t stack;
502 	int err = 0;
503 
504 	if (!frame)
505 		return 1;
506 
507 	__put_user_error(&frame->info, &frame->pinfo, err);
508 	__put_user_error(&frame->uc, &frame->puc, err);
509 	err |= copy_siginfo_to_user(&frame->info, info);
510 
511 	__put_user_error(0, &frame->uc.uc_flags, err);
512 	__put_user_error(NULL, &frame->uc.uc_link, err);
513 
514 	memset(&stack, 0, sizeof(stack));
515 	stack.ss_sp = (void __user *)current->sas_ss_sp;
516 	stack.ss_flags = sas_ss_flags(regs->ARM_sp);
517 	stack.ss_size = current->sas_ss_size;
518 	err |= __copy_to_user(&frame->uc.uc_stack, &stack, sizeof(stack));
519 
520 	err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->aux,
521 				regs, set->sig[0]);
522 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
523 
524 	if (err == 0)
525 		err = setup_return(regs, ka, &frame->retcode, frame, usig);
526 
527 	if (err == 0) {
528 		/*
529 		 * For realtime signals we must also set the second and third
530 		 * arguments for the signal handler.
531 		 *   -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
532 		 */
533 		regs->ARM_r1 = (unsigned long)&frame->info;
534 		regs->ARM_r2 = (unsigned long)&frame->uc;
535 	}
536 
537 	return err;
538 }
539 
540 static inline void restart_syscall(struct pt_regs *regs)
541 {
542 	regs->ARM_r0 = regs->ARM_ORIG_r0;
543 	regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
544 }
545 
546 /*
547  * OK, we're invoking a handler
548  */
549 static void
550 handle_signal(unsigned long sig, struct k_sigaction *ka,
551 	      siginfo_t *info, sigset_t *oldset,
552 	      struct pt_regs * regs, int syscall)
553 {
554 	struct thread_info *thread = current_thread_info();
555 	struct task_struct *tsk = current;
556 	int usig = sig;
557 	int ret;
558 
559 	/*
560 	 * If we were from a system call, check for system call restarting...
561 	 */
562 	if (syscall) {
563 		switch (regs->ARM_r0) {
564 		case -ERESTART_RESTARTBLOCK:
565 		case -ERESTARTNOHAND:
566 			regs->ARM_r0 = -EINTR;
567 			break;
568 		case -ERESTARTSYS:
569 			if (!(ka->sa.sa_flags & SA_RESTART)) {
570 				regs->ARM_r0 = -EINTR;
571 				break;
572 			}
573 			/* fallthrough */
574 		case -ERESTARTNOINTR:
575 			restart_syscall(regs);
576 		}
577 	}
578 
579 	/*
580 	 * translate the signal
581 	 */
582 	if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
583 		usig = thread->exec_domain->signal_invmap[usig];
584 
585 	/*
586 	 * Set up the stack frame
587 	 */
588 	if (ka->sa.sa_flags & SA_SIGINFO)
589 		ret = setup_rt_frame(usig, ka, info, oldset, regs);
590 	else
591 		ret = setup_frame(usig, ka, oldset, regs);
592 
593 	/*
594 	 * Check that the resulting registers are actually sane.
595 	 */
596 	ret |= !valid_user_regs(regs);
597 
598 	if (ret != 0) {
599 		force_sigsegv(sig, tsk);
600 		return;
601 	}
602 
603 	/*
604 	 * Block the signal if we were successful.
605 	 */
606 	spin_lock_irq(&tsk->sighand->siglock);
607 	sigorsets(&tsk->blocked, &tsk->blocked,
608 		  &ka->sa.sa_mask);
609 	if (!(ka->sa.sa_flags & SA_NODEFER))
610 		sigaddset(&tsk->blocked, sig);
611 	recalc_sigpending();
612 	spin_unlock_irq(&tsk->sighand->siglock);
613 
614 }
615 
616 /*
617  * Note that 'init' is a special process: it doesn't get signals it doesn't
618  * want to handle. Thus you cannot kill init even with a SIGKILL even by
619  * mistake.
620  *
621  * Note that we go through the signals twice: once to check the signals that
622  * the kernel can handle, and then we build all the user-level signal handling
623  * stack-frames in one go after that.
624  */
625 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
626 {
627 	struct k_sigaction ka;
628 	siginfo_t info;
629 	int signr;
630 
631 	/*
632 	 * We want the common case to go fast, which
633 	 * is why we may in certain cases get here from
634 	 * kernel mode. Just return without doing anything
635 	 * if so.
636 	 */
637 	if (!user_mode(regs))
638 		return 0;
639 
640 	if (try_to_freeze())
641 		goto no_signal;
642 
643 	if (current->ptrace & PT_SINGLESTEP)
644 		ptrace_cancel_bpt(current);
645 
646 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
647 	if (signr > 0) {
648 		handle_signal(signr, &ka, &info, oldset, regs, syscall);
649 		if (current->ptrace & PT_SINGLESTEP)
650 			ptrace_set_bpt(current);
651 		return 1;
652 	}
653 
654  no_signal:
655 	/*
656 	 * No signal to deliver to the process - restart the syscall.
657 	 */
658 	if (syscall) {
659 		if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
660 			if (thumb_mode(regs)) {
661 				regs->ARM_r7 = __NR_restart_syscall;
662 				regs->ARM_pc -= 2;
663 			} else {
664 				u32 __user *usp;
665 
666 				regs->ARM_sp -= 12;
667 				usp = (u32 __user *)regs->ARM_sp;
668 
669 				put_user(regs->ARM_pc, &usp[0]);
670 				/* swi __NR_restart_syscall */
671 				put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
672 				/* ldr	pc, [sp], #12 */
673 				put_user(0xe49df00c, &usp[2]);
674 
675 				flush_icache_range((unsigned long)usp,
676 						   (unsigned long)(usp + 3));
677 
678 				regs->ARM_pc = regs->ARM_sp + 4;
679 			}
680 		}
681 		if (regs->ARM_r0 == -ERESTARTNOHAND ||
682 		    regs->ARM_r0 == -ERESTARTSYS ||
683 		    regs->ARM_r0 == -ERESTARTNOINTR) {
684 			restart_syscall(regs);
685 		}
686 	}
687 	if (current->ptrace & PT_SINGLESTEP)
688 		ptrace_set_bpt(current);
689 	return 0;
690 }
691 
692 asmlinkage void
693 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
694 {
695 	if (thread_flags & _TIF_SIGPENDING)
696 		do_signal(&current->blocked, regs, syscall);
697 }
698