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 void *aux
= sf
->uc
.uc_mcontext
.__reserved
;
102 err
= __copy_from_user(&set
, &sf
->uc
.uc_sigmask
, sizeof(set
));
104 set_current_blocked(&set
);
106 for (i
= 0; i
< 31; i
++)
107 __get_user_error(regs
->regs
[i
], &sf
->uc
.uc_mcontext
.regs
[i
],
109 __get_user_error(regs
->sp
, &sf
->uc
.uc_mcontext
.sp
, err
);
110 __get_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.pc
, err
);
111 __get_user_error(regs
->pstate
, &sf
->uc
.uc_mcontext
.pstate
, err
);
114 * Avoid sys_rt_sigreturn() restarting.
116 regs
->syscallno
= ~0UL;
118 err
|= !valid_user_regs(®s
->user_regs
);
121 struct fpsimd_context
*fpsimd_ctx
=
122 container_of(aux
, struct fpsimd_context
, head
);
123 err
|= restore_fpsimd_context(fpsimd_ctx
);
129 asmlinkage
long sys_rt_sigreturn(struct pt_regs
*regs
)
131 struct rt_sigframe __user
*frame
;
133 /* Always make any pending restarted system calls return -EINTR */
134 current
->restart_block
.fn
= do_no_restart_syscall
;
137 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
138 * be word aligned here.
143 frame
= (struct rt_sigframe __user
*)regs
->sp
;
145 if (!access_ok(VERIFY_READ
, frame
, sizeof (*frame
)))
148 if (restore_sigframe(regs
, frame
))
151 if (restore_altstack(&frame
->uc
.uc_stack
))
154 return regs
->regs
[0];
157 if (show_unhandled_signals
)
158 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
159 current
->comm
, task_pid_nr(current
), __func__
,
161 force_sig(SIGSEGV
, current
);
165 static int setup_sigframe(struct rt_sigframe __user
*sf
,
166 struct pt_regs
*regs
, sigset_t
*set
)
169 void *aux
= sf
->uc
.uc_mcontext
.__reserved
;
170 struct _aarch64_ctx
*end
;
172 /* set up the stack frame for unwinding */
173 __put_user_error(regs
->regs
[29], &sf
->fp
, err
);
174 __put_user_error(regs
->regs
[30], &sf
->lr
, err
);
176 for (i
= 0; i
< 31; i
++)
177 __put_user_error(regs
->regs
[i
], &sf
->uc
.uc_mcontext
.regs
[i
],
179 __put_user_error(regs
->sp
, &sf
->uc
.uc_mcontext
.sp
, err
);
180 __put_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.pc
, err
);
181 __put_user_error(regs
->pstate
, &sf
->uc
.uc_mcontext
.pstate
, err
);
183 __put_user_error(current
->thread
.fault_address
, &sf
->uc
.uc_mcontext
.fault_address
, err
);
185 err
|= __copy_to_user(&sf
->uc
.uc_sigmask
, set
, sizeof(*set
));
188 struct fpsimd_context
*fpsimd_ctx
=
189 container_of(aux
, struct fpsimd_context
, head
);
190 err
|= preserve_fpsimd_context(fpsimd_ctx
);
191 aux
+= sizeof(*fpsimd_ctx
);
194 /* fault information, if valid */
195 if (current
->thread
.fault_code
) {
196 struct esr_context
*esr_ctx
=
197 container_of(aux
, struct esr_context
, head
);
198 __put_user_error(ESR_MAGIC
, &esr_ctx
->head
.magic
, err
);
199 __put_user_error(sizeof(*esr_ctx
), &esr_ctx
->head
.size
, err
);
200 __put_user_error(current
->thread
.fault_code
, &esr_ctx
->esr
, err
);
201 aux
+= sizeof(*esr_ctx
);
204 /* set the "end" magic */
206 __put_user_error(0, &end
->magic
, err
);
207 __put_user_error(0, &end
->size
, err
);
212 static struct rt_sigframe __user
*get_sigframe(struct ksignal
*ksig
,
213 struct pt_regs
*regs
)
215 unsigned long sp
, sp_top
;
216 struct rt_sigframe __user
*frame
;
218 sp
= sp_top
= sigsp(regs
->sp
, ksig
);
220 sp
= (sp
- sizeof(struct rt_sigframe
)) & ~15;
221 frame
= (struct rt_sigframe __user
*)sp
;
224 * Check that we can actually write to the signal frame.
226 if (!access_ok(VERIFY_WRITE
, frame
, sp_top
- sp
))
232 static void setup_return(struct pt_regs
*regs
, struct k_sigaction
*ka
,
233 void __user
*frame
, int usig
)
235 __sigrestore_t sigtramp
;
237 regs
->regs
[0] = usig
;
238 regs
->sp
= (unsigned long)frame
;
239 regs
->regs
[29] = regs
->sp
+ offsetof(struct rt_sigframe
, fp
);
240 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
242 if (ka
->sa
.sa_flags
& SA_RESTORER
)
243 sigtramp
= ka
->sa
.sa_restorer
;
245 sigtramp
= VDSO_SYMBOL(current
->mm
->context
.vdso
, sigtramp
);
247 regs
->regs
[30] = (unsigned long)sigtramp
;
250 static int setup_rt_frame(int usig
, struct ksignal
*ksig
, sigset_t
*set
,
251 struct pt_regs
*regs
)
253 struct rt_sigframe __user
*frame
;
256 frame
= get_sigframe(ksig
, regs
);
260 __put_user_error(0, &frame
->uc
.uc_flags
, err
);
261 __put_user_error(NULL
, &frame
->uc
.uc_link
, err
);
263 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
264 err
|= setup_sigframe(frame
, regs
, set
);
266 setup_return(regs
, &ksig
->ka
, frame
, usig
);
267 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
) {
268 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
269 regs
->regs
[1] = (unsigned long)&frame
->info
;
270 regs
->regs
[2] = (unsigned long)&frame
->uc
;
277 static void setup_restart_syscall(struct pt_regs
*regs
)
279 if (is_compat_task())
280 compat_setup_restart_syscall(regs
);
282 regs
->regs
[8] = __NR_restart_syscall
;
286 * OK, we're invoking a handler
288 static void handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
290 struct task_struct
*tsk
= current
;
291 sigset_t
*oldset
= sigmask_to_save();
292 int usig
= ksig
->sig
;
296 * Set up the stack frame
298 if (is_compat_task()) {
299 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
300 ret
= compat_setup_rt_frame(usig
, ksig
, oldset
, regs
);
302 ret
= compat_setup_frame(usig
, ksig
, oldset
, regs
);
304 ret
= setup_rt_frame(usig
, ksig
, oldset
, regs
);
308 * Check that the resulting registers are actually sane.
310 ret
|= !valid_user_regs(®s
->user_regs
);
313 * Fast forward the stepping logic so we step into the signal
317 user_fastforward_single_step(tsk
);
319 signal_setup_done(ret
, ksig
, 0);
323 * Note that 'init' is a special process: it doesn't get signals it doesn't
324 * want to handle. Thus you cannot kill init even with a SIGKILL even by
327 * Note that we go through the signals twice: once to check the signals that
328 * the kernel can handle, and then we build all the user-level signal handling
329 * stack-frames in one go after that.
331 static void do_signal(struct pt_regs
*regs
)
333 unsigned long continue_addr
= 0, restart_addr
= 0;
335 int syscall
= (int)regs
->syscallno
;
339 * If we were from a system call, check for system call restarting...
342 continue_addr
= regs
->pc
;
343 restart_addr
= continue_addr
- (compat_thumb_mode(regs
) ? 2 : 4);
344 retval
= regs
->regs
[0];
347 * Avoid additional syscall restarting via ret_to_user.
349 regs
->syscallno
= ~0UL;
352 * Prepare for system call restart. We do this here so that a
353 * debugger will see the already changed PC.
356 case -ERESTARTNOHAND
:
358 case -ERESTARTNOINTR
:
359 case -ERESTART_RESTARTBLOCK
:
360 regs
->regs
[0] = regs
->orig_x0
;
361 regs
->pc
= restart_addr
;
367 * Get the signal to deliver. When running under ptrace, at this point
368 * the debugger may change all of our registers.
370 if (get_signal(&ksig
)) {
372 * Depending on the signal settings, we may need to revert the
373 * decision to restart the system call, but skip this if a
374 * debugger has chosen to restart at a different PC.
376 if (regs
->pc
== restart_addr
&&
377 (retval
== -ERESTARTNOHAND
||
378 retval
== -ERESTART_RESTARTBLOCK
||
379 (retval
== -ERESTARTSYS
&&
380 !(ksig
.ka
.sa
.sa_flags
& SA_RESTART
)))) {
381 regs
->regs
[0] = -EINTR
;
382 regs
->pc
= continue_addr
;
385 handle_signal(&ksig
, regs
);
390 * Handle restarting a different system call. As above, if a debugger
391 * has chosen to restart at a different PC, ignore the restart.
393 if (syscall
>= 0 && regs
->pc
== restart_addr
) {
394 if (retval
== -ERESTART_RESTARTBLOCK
)
395 setup_restart_syscall(regs
);
396 user_rewind_single_step(current
);
399 restore_saved_sigmask();
402 asmlinkage
void do_notify_resume(struct pt_regs
*regs
,
403 unsigned int thread_flags
)
405 if (thread_flags
& _TIF_SIGPENDING
)
408 if (thread_flags
& _TIF_NOTIFY_RESUME
) {
409 clear_thread_flag(TIF_NOTIFY_RESUME
);
410 tracehook_notify_resume(regs
);
413 if (thread_flags
& _TIF_FOREIGN_FPSTATE
)
414 fpsimd_restore_current_state();