dma: imx-sdma: Fix warnings for LPAE builds
[linux-2.6.git] / arch / arm64 / kernel / signal.c
blob890a591f75dd1848e28e1ea62413775be5394c6d
1 /*
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/errno.h>
21 #include <linux/signal.h>
22 #include <linux/personality.h>
23 #include <linux/freezer.h>
24 #include <linux/uaccess.h>
25 #include <linux/tracehook.h>
26 #include <linux/ratelimit.h>
28 #include <asm/compat.h>
29 #include <asm/debug-monitors.h>
30 #include <asm/elf.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>
36 #include <asm/vdso.h>
39 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
41 struct rt_sigframe {
42 struct siginfo info;
43 struct ucontext uc;
44 u64 fp;
45 u64 lr;
48 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
50 struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
51 int err;
53 /* dump the hardware registers to the fpsimd_state structure */
54 fpsimd_save_state(fpsimd);
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;
71 __u32 magic, size;
72 int err = 0;
74 /* check the magic/size information */
75 __get_user_error(magic, &ctx->head.magic, err);
76 __get_user_error(size, &ctx->head.size, err);
77 if (err)
78 return -EFAULT;
79 if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
80 return -EINVAL;
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 */
89 if (!err) {
90 preempt_disable();
91 fpsimd_load_state(&fpsimd);
92 preempt_enable();
95 return err ? -EFAULT : 0;
98 static int restore_sigframe(struct pt_regs *regs,
99 struct rt_sigframe __user *sf)
101 sigset_t set;
102 int i, err;
103 struct aux_context __user *aux =
104 (struct aux_context __user *)sf->uc.uc_mcontext.__reserved;
106 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
107 if (err == 0)
108 set_current_blocked(&set);
110 for (i = 0; i < 31; i++)
111 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
112 err);
113 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
114 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
115 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
118 * Avoid sys_rt_sigreturn() restarting.
120 regs->syscallno = ~0UL;
122 err |= !valid_user_regs(&regs->user_regs);
124 if (err == 0)
125 err |= restore_fpsimd_context(&aux->fpsimd);
127 return err;
130 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
132 struct rt_sigframe __user *frame;
134 /* Always make any pending restarted system calls return -EINTR */
135 current_thread_info()->restart_block.fn = do_no_restart_syscall;
138 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
139 * be word aligned here.
141 if (regs->sp & 15)
142 goto badframe;
144 frame = (struct rt_sigframe __user *)regs->sp;
146 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
147 goto badframe;
149 if (restore_sigframe(regs, frame))
150 goto badframe;
152 if (restore_altstack(&frame->uc.uc_stack))
153 goto badframe;
155 return regs->regs[0];
157 badframe:
158 if (show_unhandled_signals)
159 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
160 current->comm, task_pid_nr(current), __func__,
161 regs->pc, regs->sp);
162 force_sig(SIGSEGV, current);
163 return 0;
166 static int setup_sigframe(struct rt_sigframe __user *sf,
167 struct pt_regs *regs, sigset_t *set)
169 int i, err = 0;
170 struct aux_context __user *aux =
171 (struct aux_context __user *)sf->uc.uc_mcontext.__reserved;
173 /* set up the stack frame for unwinding */
174 __put_user_error(regs->regs[29], &sf->fp, err);
175 __put_user_error(regs->regs[30], &sf->lr, err);
177 for (i = 0; i < 31; i++)
178 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
179 err);
180 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
181 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
182 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
184 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
186 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
188 if (err == 0)
189 err |= preserve_fpsimd_context(&aux->fpsimd);
191 /* set the "end" magic */
192 __put_user_error(0, &aux->end.magic, err);
193 __put_user_error(0, &aux->end.size, err);
195 return err;
198 static struct rt_sigframe __user *get_sigframe(struct k_sigaction *ka,
199 struct pt_regs *regs)
201 unsigned long sp, sp_top;
202 struct rt_sigframe __user *frame;
204 sp = sp_top = regs->sp;
207 * This is the X/Open sanctioned signal stack switching.
209 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
210 sp = sp_top = current->sas_ss_sp + current->sas_ss_size;
212 sp = (sp - sizeof(struct rt_sigframe)) & ~15;
213 frame = (struct rt_sigframe __user *)sp;
216 * Check that we can actually write to the signal frame.
218 if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
219 frame = NULL;
221 return frame;
224 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
225 void __user *frame, int usig)
227 __sigrestore_t sigtramp;
229 regs->regs[0] = usig;
230 regs->sp = (unsigned long)frame;
231 regs->regs[29] = regs->sp + offsetof(struct rt_sigframe, fp);
232 regs->pc = (unsigned long)ka->sa.sa_handler;
234 if (ka->sa.sa_flags & SA_RESTORER)
235 sigtramp = ka->sa.sa_restorer;
236 else
237 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
239 regs->regs[30] = (unsigned long)sigtramp;
242 static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
243 sigset_t *set, struct pt_regs *regs)
245 struct rt_sigframe __user *frame;
246 int err = 0;
248 frame = get_sigframe(ka, regs);
249 if (!frame)
250 return 1;
252 __put_user_error(0, &frame->uc.uc_flags, err);
253 __put_user_error(NULL, &frame->uc.uc_link, err);
255 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
256 err |= setup_sigframe(frame, regs, set);
257 if (err == 0) {
258 setup_return(regs, ka, frame, usig);
259 if (ka->sa.sa_flags & SA_SIGINFO) {
260 err |= copy_siginfo_to_user(&frame->info, info);
261 regs->regs[1] = (unsigned long)&frame->info;
262 regs->regs[2] = (unsigned long)&frame->uc;
266 return err;
269 static void setup_restart_syscall(struct pt_regs *regs)
271 if (is_compat_task())
272 compat_setup_restart_syscall(regs);
273 else
274 regs->regs[8] = __NR_restart_syscall;
278 * OK, we're invoking a handler
280 static void handle_signal(unsigned long sig, struct k_sigaction *ka,
281 siginfo_t *info, struct pt_regs *regs)
283 struct thread_info *thread = current_thread_info();
284 struct task_struct *tsk = current;
285 sigset_t *oldset = sigmask_to_save();
286 int usig = sig;
287 int ret;
290 * translate the signal
292 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
293 usig = thread->exec_domain->signal_invmap[usig];
296 * Set up the stack frame
298 if (is_compat_task()) {
299 if (ka->sa.sa_flags & SA_SIGINFO)
300 ret = compat_setup_rt_frame(usig, ka, info, oldset,
301 regs);
302 else
303 ret = compat_setup_frame(usig, ka, oldset, regs);
304 } else {
305 ret = setup_rt_frame(usig, ka, info, oldset, regs);
309 * Check that the resulting registers are actually sane.
311 ret |= !valid_user_regs(&regs->user_regs);
313 if (ret != 0) {
314 force_sigsegv(sig, tsk);
315 return;
319 * Fast forward the stepping logic so we step into the signal
320 * handler.
322 user_fastforward_single_step(tsk);
324 signal_delivered(sig, info, ka, regs, 0);
328 * Note that 'init' is a special process: it doesn't get signals it doesn't
329 * want to handle. Thus you cannot kill init even with a SIGKILL even by
330 * mistake.
332 * Note that we go through the signals twice: once to check the signals that
333 * the kernel can handle, and then we build all the user-level signal handling
334 * stack-frames in one go after that.
336 static void do_signal(struct pt_regs *regs)
338 unsigned long continue_addr = 0, restart_addr = 0;
339 struct k_sigaction ka;
340 siginfo_t info;
341 int signr, retval = 0;
342 int syscall = (int)regs->syscallno;
345 * If we were from a system call, check for system call restarting...
347 if (syscall >= 0) {
348 continue_addr = regs->pc;
349 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
350 retval = regs->regs[0];
353 * Avoid additional syscall restarting via ret_to_user.
355 regs->syscallno = ~0UL;
358 * Prepare for system call restart. We do this here so that a
359 * debugger will see the already changed PC.
361 switch (retval) {
362 case -ERESTARTNOHAND:
363 case -ERESTARTSYS:
364 case -ERESTARTNOINTR:
365 case -ERESTART_RESTARTBLOCK:
366 regs->regs[0] = regs->orig_x0;
367 regs->pc = restart_addr;
368 break;
373 * Get the signal to deliver. When running under ptrace, at this point
374 * the debugger may change all of our registers.
376 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
377 if (signr > 0) {
379 * Depending on the signal settings, we may need to revert the
380 * decision to restart the system call, but skip this if a
381 * debugger has chosen to restart at a different PC.
383 if (regs->pc == restart_addr &&
384 (retval == -ERESTARTNOHAND ||
385 retval == -ERESTART_RESTARTBLOCK ||
386 (retval == -ERESTARTSYS &&
387 !(ka.sa.sa_flags & SA_RESTART)))) {
388 regs->regs[0] = -EINTR;
389 regs->pc = continue_addr;
392 handle_signal(signr, &ka, &info, regs);
393 return;
397 * Handle restarting a different system call. As above, if a debugger
398 * has chosen to restart at a different PC, ignore the restart.
400 if (syscall >= 0 && regs->pc == restart_addr) {
401 if (retval == -ERESTART_RESTARTBLOCK)
402 setup_restart_syscall(regs);
403 user_rewind_single_step(current);
406 restore_saved_sigmask();
409 asmlinkage void do_notify_resume(struct pt_regs *regs,
410 unsigned int thread_flags)
412 if (thread_flags & _TIF_SIGPENDING)
413 do_signal(regs);
415 if (thread_flags & _TIF_NOTIFY_RESUME) {
416 clear_thread_flag(TIF_NOTIFY_RESUME);
417 tracehook_notify_resume(regs);