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