2 * Based on arch/arm/kernel/signal.c
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/compat.h>
21 #include <linux/errno.h>
22 #include <linux/signal.h>
23 #include <linux/personality.h>
24 #include <linux/freezer.h>
25 #include <linux/uaccess.h>
26 #include <linux/tracehook.h>
27 #include <linux/ratelimit.h>
29 #include <asm/debug-monitors.h>
31 #include <asm/cacheflush.h>
32 #include <asm/ucontext.h>
33 #include <asm/unistd.h>
34 #include <asm/fpsimd.h>
35 #include <asm/signal32.h>
39 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
48 static int preserve_fpsimd_context(struct fpsimd_context __user
*ctx
)
50 struct fpsimd_state
*fpsimd
= ¤t
->thread
.fpsimd_state
;
53 /* dump the hardware registers to the fpsimd_state structure */
54 fpsimd_preserve_current_state();
56 /* copy the FP and status/control registers */
57 err
= __copy_to_user(ctx
->vregs
, fpsimd
->vregs
, sizeof(fpsimd
->vregs
));
58 __put_user_error(fpsimd
->fpsr
, &ctx
->fpsr
, err
);
59 __put_user_error(fpsimd
->fpcr
, &ctx
->fpcr
, err
);
61 /* copy the magic/size information */
62 __put_user_error(FPSIMD_MAGIC
, &ctx
->head
.magic
, err
);
63 __put_user_error(sizeof(struct fpsimd_context
), &ctx
->head
.size
, err
);
65 return err
? -EFAULT
: 0;
68 static int restore_fpsimd_context(struct fpsimd_context __user
*ctx
)
70 struct fpsimd_state fpsimd
;
74 /* check the magic/size information */
75 __get_user_error(magic
, &ctx
->head
.magic
, err
);
76 __get_user_error(size
, &ctx
->head
.size
, err
);
79 if (magic
!= FPSIMD_MAGIC
|| size
!= sizeof(struct fpsimd_context
))
82 /* copy the FP and status/control registers */
83 err
= __copy_from_user(fpsimd
.vregs
, ctx
->vregs
,
84 sizeof(fpsimd
.vregs
));
85 __get_user_error(fpsimd
.fpsr
, &ctx
->fpsr
, err
);
86 __get_user_error(fpsimd
.fpcr
, &ctx
->fpcr
, err
);
88 /* load the hardware registers from the fpsimd_state structure */
90 fpsimd_update_current_state(&fpsimd
);
92 return err
? -EFAULT
: 0;
95 static int restore_sigframe(struct pt_regs
*regs
,
96 struct rt_sigframe __user
*sf
)
100 struct aux_context __user
*aux
=
101 (struct aux_context __user
*)sf
->uc
.uc_mcontext
.__reserved
;
103 err
= __copy_from_user(&set
, &sf
->uc
.uc_sigmask
, sizeof(set
));
105 set_current_blocked(&set
);
107 for (i
= 0; i
< 31; i
++)
108 __get_user_error(regs
->regs
[i
], &sf
->uc
.uc_mcontext
.regs
[i
],
110 __get_user_error(regs
->sp
, &sf
->uc
.uc_mcontext
.sp
, err
);
111 __get_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.pc
, err
);
112 __get_user_error(regs
->pstate
, &sf
->uc
.uc_mcontext
.pstate
, err
);
115 * Avoid sys_rt_sigreturn() restarting.
117 regs
->syscallno
= ~0UL;
119 err
|= !valid_user_regs(®s
->user_regs
);
122 err
|= restore_fpsimd_context(&aux
->fpsimd
);
127 asmlinkage
long sys_rt_sigreturn(struct pt_regs
*regs
)
129 struct rt_sigframe __user
*frame
;
131 /* Always make any pending restarted system calls return -EINTR */
132 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
135 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
136 * be word aligned here.
141 frame
= (struct rt_sigframe __user
*)regs
->sp
;
143 if (!access_ok(VERIFY_READ
, frame
, sizeof (*frame
)))
146 if (restore_sigframe(regs
, frame
))
149 if (restore_altstack(&frame
->uc
.uc_stack
))
152 return regs
->regs
[0];
155 if (show_unhandled_signals
)
156 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
157 current
->comm
, task_pid_nr(current
), __func__
,
159 force_sig(SIGSEGV
, current
);
163 static int setup_sigframe(struct rt_sigframe __user
*sf
,
164 struct pt_regs
*regs
, sigset_t
*set
)
167 struct aux_context __user
*aux
=
168 (struct aux_context __user
*)sf
->uc
.uc_mcontext
.__reserved
;
170 /* set up the stack frame for unwinding */
171 __put_user_error(regs
->regs
[29], &sf
->fp
, err
);
172 __put_user_error(regs
->regs
[30], &sf
->lr
, err
);
174 for (i
= 0; i
< 31; i
++)
175 __put_user_error(regs
->regs
[i
], &sf
->uc
.uc_mcontext
.regs
[i
],
177 __put_user_error(regs
->sp
, &sf
->uc
.uc_mcontext
.sp
, err
);
178 __put_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.pc
, err
);
179 __put_user_error(regs
->pstate
, &sf
->uc
.uc_mcontext
.pstate
, err
);
181 __put_user_error(current
->thread
.fault_address
, &sf
->uc
.uc_mcontext
.fault_address
, err
);
183 err
|= __copy_to_user(&sf
->uc
.uc_sigmask
, set
, sizeof(*set
));
186 err
|= preserve_fpsimd_context(&aux
->fpsimd
);
188 /* set the "end" magic */
189 __put_user_error(0, &aux
->end
.magic
, err
);
190 __put_user_error(0, &aux
->end
.size
, err
);
195 static struct rt_sigframe __user
*get_sigframe(struct k_sigaction
*ka
,
196 struct pt_regs
*regs
)
198 unsigned long sp
, sp_top
;
199 struct rt_sigframe __user
*frame
;
201 sp
= sp_top
= regs
->sp
;
204 * This is the X/Open sanctioned signal stack switching.
206 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
))
207 sp
= sp_top
= current
->sas_ss_sp
+ current
->sas_ss_size
;
209 sp
= (sp
- sizeof(struct rt_sigframe
)) & ~15;
210 frame
= (struct rt_sigframe __user
*)sp
;
213 * Check that we can actually write to the signal frame.
215 if (!access_ok(VERIFY_WRITE
, frame
, sp_top
- sp
))
221 static void setup_return(struct pt_regs
*regs
, struct k_sigaction
*ka
,
222 void __user
*frame
, int usig
)
224 __sigrestore_t sigtramp
;
226 regs
->regs
[0] = usig
;
227 regs
->sp
= (unsigned long)frame
;
228 regs
->regs
[29] = regs
->sp
+ offsetof(struct rt_sigframe
, fp
);
229 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
231 if (ka
->sa
.sa_flags
& SA_RESTORER
)
232 sigtramp
= ka
->sa
.sa_restorer
;
234 sigtramp
= VDSO_SYMBOL(current
->mm
->context
.vdso
, sigtramp
);
236 regs
->regs
[30] = (unsigned long)sigtramp
;
239 static int setup_rt_frame(int usig
, struct k_sigaction
*ka
, siginfo_t
*info
,
240 sigset_t
*set
, struct pt_regs
*regs
)
242 struct rt_sigframe __user
*frame
;
245 frame
= get_sigframe(ka
, regs
);
249 __put_user_error(0, &frame
->uc
.uc_flags
, err
);
250 __put_user_error(NULL
, &frame
->uc
.uc_link
, err
);
252 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
253 err
|= setup_sigframe(frame
, regs
, set
);
255 setup_return(regs
, ka
, frame
, usig
);
256 if (ka
->sa
.sa_flags
& SA_SIGINFO
) {
257 err
|= copy_siginfo_to_user(&frame
->info
, info
);
258 regs
->regs
[1] = (unsigned long)&frame
->info
;
259 regs
->regs
[2] = (unsigned long)&frame
->uc
;
266 static void setup_restart_syscall(struct pt_regs
*regs
)
268 if (is_compat_task())
269 compat_setup_restart_syscall(regs
);
271 regs
->regs
[8] = __NR_restart_syscall
;
275 * OK, we're invoking a handler
277 static void handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
278 siginfo_t
*info
, struct pt_regs
*regs
)
280 struct thread_info
*thread
= current_thread_info();
281 struct task_struct
*tsk
= current
;
282 sigset_t
*oldset
= sigmask_to_save();
287 * translate the signal
289 if (usig
< 32 && thread
->exec_domain
&& thread
->exec_domain
->signal_invmap
)
290 usig
= thread
->exec_domain
->signal_invmap
[usig
];
293 * Set up the stack frame
295 if (is_compat_task()) {
296 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
297 ret
= compat_setup_rt_frame(usig
, ka
, info
, oldset
,
300 ret
= compat_setup_frame(usig
, ka
, oldset
, regs
);
302 ret
= setup_rt_frame(usig
, ka
, info
, oldset
, regs
);
306 * Check that the resulting registers are actually sane.
308 ret
|= !valid_user_regs(®s
->user_regs
);
311 force_sigsegv(sig
, tsk
);
316 * Fast forward the stepping logic so we step into the signal
319 user_fastforward_single_step(tsk
);
321 signal_delivered(sig
, info
, ka
, regs
, 0);
325 * Note that 'init' is a special process: it doesn't get signals it doesn't
326 * want to handle. Thus you cannot kill init even with a SIGKILL even by
329 * Note that we go through the signals twice: once to check the signals that
330 * the kernel can handle, and then we build all the user-level signal handling
331 * stack-frames in one go after that.
333 static void do_signal(struct pt_regs
*regs
)
335 unsigned long continue_addr
= 0, restart_addr
= 0;
336 struct k_sigaction ka
;
338 int signr
, retval
= 0;
339 int syscall
= (int)regs
->syscallno
;
342 * If we were from a system call, check for system call restarting...
345 continue_addr
= regs
->pc
;
346 restart_addr
= continue_addr
- (compat_thumb_mode(regs
) ? 2 : 4);
347 retval
= regs
->regs
[0];
350 * Avoid additional syscall restarting via ret_to_user.
352 regs
->syscallno
= ~0UL;
355 * Prepare for system call restart. We do this here so that a
356 * debugger will see the already changed PC.
359 case -ERESTARTNOHAND
:
361 case -ERESTARTNOINTR
:
362 case -ERESTART_RESTARTBLOCK
:
363 regs
->regs
[0] = regs
->orig_x0
;
364 regs
->pc
= restart_addr
;
370 * Get the signal to deliver. When running under ptrace, at this point
371 * the debugger may change all of our registers.
373 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
376 * Depending on the signal settings, we may need to revert the
377 * decision to restart the system call, but skip this if a
378 * debugger has chosen to restart at a different PC.
380 if (regs
->pc
== restart_addr
&&
381 (retval
== -ERESTARTNOHAND
||
382 retval
== -ERESTART_RESTARTBLOCK
||
383 (retval
== -ERESTARTSYS
&&
384 !(ka
.sa
.sa_flags
& SA_RESTART
)))) {
385 regs
->regs
[0] = -EINTR
;
386 regs
->pc
= continue_addr
;
389 handle_signal(signr
, &ka
, &info
, regs
);
394 * Handle restarting a different system call. As above, if a debugger
395 * has chosen to restart at a different PC, ignore the restart.
397 if (syscall
>= 0 && regs
->pc
== restart_addr
) {
398 if (retval
== -ERESTART_RESTARTBLOCK
)
399 setup_restart_syscall(regs
);
400 user_rewind_single_step(current
);
403 restore_saved_sigmask();
406 asmlinkage
void do_notify_resume(struct pt_regs
*regs
,
407 unsigned int thread_flags
)
409 if (thread_flags
& _TIF_SIGPENDING
)
412 if (thread_flags
& _TIF_NOTIFY_RESUME
) {
413 clear_thread_flag(TIF_NOTIFY_RESUME
);
414 tracehook_notify_resume(regs
);
417 if (thread_flags
& _TIF_FOREIGN_FPSTATE
)
418 fpsimd_restore_current_state();