Merge tag 'pm+acpi-3.16-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-block.git] / arch / arm64 / kernel / signal.c
CommitLineData
2c020ed8
CM
1/*
2 * Based on arch/arm/kernel/signal.c
3 *
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
6 *
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.
10 *
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.
15 *
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/>.
18 */
19
fd92d4a5 20#include <linux/compat.h>
2c020ed8
CM
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>
28
2c020ed8
CM
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>
37
38/*
39 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
40 */
41struct rt_sigframe {
42 struct siginfo info;
43 struct ucontext uc;
304ef4e8
WD
44 u64 fp;
45 u64 lr;
2c020ed8
CM
46};
47
48static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
49{
50 struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
51 int err;
52
53 /* dump the hardware registers to the fpsimd_state structure */
c51f9269 54 fpsimd_preserve_current_state();
2c020ed8
CM
55
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);
60
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);
64
65 return err ? -EFAULT : 0;
66}
67
68static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
69{
70 struct fpsimd_state fpsimd;
71 __u32 magic, size;
72 int err = 0;
73
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;
81
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);
87
88 /* load the hardware registers from the fpsimd_state structure */
c51f9269
AB
89 if (!err)
90 fpsimd_update_current_state(&fpsimd);
2c020ed8
CM
91
92 return err ? -EFAULT : 0;
93}
94
95static int restore_sigframe(struct pt_regs *regs,
96 struct rt_sigframe __user *sf)
97{
98 sigset_t set;
99 int i, err;
0e0276d1 100 void *aux = sf->uc.uc_mcontext.__reserved;
2c020ed8
CM
101
102 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
103 if (err == 0)
104 set_current_blocked(&set);
105
106 for (i = 0; i < 31; i++)
107 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
108 err);
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);
112
113 /*
114 * Avoid sys_rt_sigreturn() restarting.
115 */
116 regs->syscallno = ~0UL;
117
118 err |= !valid_user_regs(&regs->user_regs);
119
0e0276d1
CM
120 if (err == 0) {
121 struct fpsimd_context *fpsimd_ctx =
122 container_of(aux, struct fpsimd_context, head);
123 err |= restore_fpsimd_context(fpsimd_ctx);
124 }
2c020ed8
CM
125
126 return err;
127}
128
129asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
130{
131 struct rt_sigframe __user *frame;
132
133 /* Always make any pending restarted system calls return -EINTR */
134 current_thread_info()->restart_block.fn = do_no_restart_syscall;
135
136 /*
137 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
138 * be word aligned here.
139 */
140 if (regs->sp & 15)
141 goto badframe;
142
143 frame = (struct rt_sigframe __user *)regs->sp;
144
145 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
146 goto badframe;
147
148 if (restore_sigframe(regs, frame))
149 goto badframe;
150
207bdae4 151 if (restore_altstack(&frame->uc.uc_stack))
2c020ed8
CM
152 goto badframe;
153
154 return regs->regs[0];
155
156badframe:
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__,
160 regs->pc, regs->sp);
161 force_sig(SIGSEGV, current);
162 return 0;
163}
164
2c020ed8
CM
165static int setup_sigframe(struct rt_sigframe __user *sf,
166 struct pt_regs *regs, sigset_t *set)
167{
168 int i, err = 0;
0e0276d1
CM
169 void *aux = sf->uc.uc_mcontext.__reserved;
170 struct _aarch64_ctx *end;
2c020ed8 171
304ef4e8
WD
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);
175
2c020ed8
CM
176 for (i = 0; i < 31; i++)
177 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
178 err);
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);
182
183 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
184
185 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
186
0e0276d1
CM
187 if (err == 0) {
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);
192 }
2c020ed8 193
15af1942
CM
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);
202 }
203
2c020ed8 204 /* set the "end" magic */
0e0276d1
CM
205 end = aux;
206 __put_user_error(0, &end->magic, err);
207 __put_user_error(0, &end->size, err);
2c020ed8
CM
208
209 return err;
210}
211
b64e1c61
WD
212static struct rt_sigframe __user *get_sigframe(struct k_sigaction *ka,
213 struct pt_regs *regs)
2c020ed8
CM
214{
215 unsigned long sp, sp_top;
b64e1c61 216 struct rt_sigframe __user *frame;
2c020ed8
CM
217
218 sp = sp_top = regs->sp;
219
220 /*
221 * This is the X/Open sanctioned signal stack switching.
222 */
223 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
224 sp = sp_top = current->sas_ss_sp + current->sas_ss_size;
225
b64e1c61
WD
226 sp = (sp - sizeof(struct rt_sigframe)) & ~15;
227 frame = (struct rt_sigframe __user *)sp;
2c020ed8
CM
228
229 /*
230 * Check that we can actually write to the signal frame.
231 */
232 if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
233 frame = NULL;
234
235 return frame;
236}
237
304ef4e8
WD
238static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
239 void __user *frame, int usig)
2c020ed8 240{
2c020ed8 241 __sigrestore_t sigtramp;
2c020ed8
CM
242
243 regs->regs[0] = usig;
2c020ed8 244 regs->sp = (unsigned long)frame;
304ef4e8 245 regs->regs[29] = regs->sp + offsetof(struct rt_sigframe, fp);
2c020ed8
CM
246 regs->pc = (unsigned long)ka->sa.sa_handler;
247
248 if (ka->sa.sa_flags & SA_RESTORER)
249 sigtramp = ka->sa.sa_restorer;
250 else
251 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
252
253 regs->regs[30] = (unsigned long)sigtramp;
2c020ed8
CM
254}
255
256static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
257 sigset_t *set, struct pt_regs *regs)
258{
259 struct rt_sigframe __user *frame;
2c020ed8
CM
260 int err = 0;
261
b64e1c61 262 frame = get_sigframe(ka, regs);
2c020ed8
CM
263 if (!frame)
264 return 1;
265
266 __put_user_error(0, &frame->uc.uc_flags, err);
267 __put_user_error(NULL, &frame->uc.uc_link, err);
268
207bdae4 269 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
2c020ed8 270 err |= setup_sigframe(frame, regs, set);
304ef4e8
WD
271 if (err == 0) {
272 setup_return(regs, ka, frame, usig);
273 if (ka->sa.sa_flags & SA_SIGINFO) {
274 err |= copy_siginfo_to_user(&frame->info, info);
275 regs->regs[1] = (unsigned long)&frame->info;
276 regs->regs[2] = (unsigned long)&frame->uc;
277 }
2c020ed8
CM
278 }
279
280 return err;
281}
282
283static void setup_restart_syscall(struct pt_regs *regs)
284{
285 if (is_compat_task())
286 compat_setup_restart_syscall(regs);
287 else
288 regs->regs[8] = __NR_restart_syscall;
289}
290
291/*
292 * OK, we're invoking a handler
293 */
294static void handle_signal(unsigned long sig, struct k_sigaction *ka,
295 siginfo_t *info, struct pt_regs *regs)
296{
297 struct thread_info *thread = current_thread_info();
298 struct task_struct *tsk = current;
299 sigset_t *oldset = sigmask_to_save();
300 int usig = sig;
301 int ret;
302
303 /*
304 * translate the signal
305 */
306 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
307 usig = thread->exec_domain->signal_invmap[usig];
308
309 /*
310 * Set up the stack frame
311 */
312 if (is_compat_task()) {
313 if (ka->sa.sa_flags & SA_SIGINFO)
314 ret = compat_setup_rt_frame(usig, ka, info, oldset,
315 regs);
316 else
317 ret = compat_setup_frame(usig, ka, oldset, regs);
318 } else {
319 ret = setup_rt_frame(usig, ka, info, oldset, regs);
320 }
321
322 /*
323 * Check that the resulting registers are actually sane.
324 */
325 ret |= !valid_user_regs(&regs->user_regs);
326
327 if (ret != 0) {
328 force_sigsegv(sig, tsk);
329 return;
330 }
331
332 /*
333 * Fast forward the stepping logic so we step into the signal
334 * handler.
335 */
336 user_fastforward_single_step(tsk);
337
338 signal_delivered(sig, info, ka, regs, 0);
339}
340
341/*
342 * Note that 'init' is a special process: it doesn't get signals it doesn't
343 * want to handle. Thus you cannot kill init even with a SIGKILL even by
344 * mistake.
345 *
346 * Note that we go through the signals twice: once to check the signals that
347 * the kernel can handle, and then we build all the user-level signal handling
348 * stack-frames in one go after that.
349 */
350static void do_signal(struct pt_regs *regs)
351{
352 unsigned long continue_addr = 0, restart_addr = 0;
353 struct k_sigaction ka;
354 siginfo_t info;
355 int signr, retval = 0;
356 int syscall = (int)regs->syscallno;
357
358 /*
359 * If we were from a system call, check for system call restarting...
360 */
361 if (syscall >= 0) {
362 continue_addr = regs->pc;
363 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
364 retval = regs->regs[0];
365
366 /*
367 * Avoid additional syscall restarting via ret_to_user.
368 */
369 regs->syscallno = ~0UL;
370
371 /*
372 * Prepare for system call restart. We do this here so that a
373 * debugger will see the already changed PC.
374 */
375 switch (retval) {
376 case -ERESTARTNOHAND:
377 case -ERESTARTSYS:
378 case -ERESTARTNOINTR:
379 case -ERESTART_RESTARTBLOCK:
380 regs->regs[0] = regs->orig_x0;
381 regs->pc = restart_addr;
382 break;
383 }
384 }
385
386 /*
387 * Get the signal to deliver. When running under ptrace, at this point
388 * the debugger may change all of our registers.
389 */
390 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
391 if (signr > 0) {
392 /*
393 * Depending on the signal settings, we may need to revert the
394 * decision to restart the system call, but skip this if a
395 * debugger has chosen to restart at a different PC.
396 */
397 if (regs->pc == restart_addr &&
398 (retval == -ERESTARTNOHAND ||
399 retval == -ERESTART_RESTARTBLOCK ||
400 (retval == -ERESTARTSYS &&
401 !(ka.sa.sa_flags & SA_RESTART)))) {
402 regs->regs[0] = -EINTR;
403 regs->pc = continue_addr;
404 }
405
406 handle_signal(signr, &ka, &info, regs);
407 return;
408 }
409
410 /*
411 * Handle restarting a different system call. As above, if a debugger
412 * has chosen to restart at a different PC, ignore the restart.
413 */
414 if (syscall >= 0 && regs->pc == restart_addr) {
415 if (retval == -ERESTART_RESTARTBLOCK)
416 setup_restart_syscall(regs);
417 user_rewind_single_step(current);
418 }
419
420 restore_saved_sigmask();
421}
422
423asmlinkage void do_notify_resume(struct pt_regs *regs,
424 unsigned int thread_flags)
425{
426 if (thread_flags & _TIF_SIGPENDING)
427 do_signal(regs);
428
429 if (thread_flags & _TIF_NOTIFY_RESUME) {
430 clear_thread_flag(TIF_NOTIFY_RESUME);
431 tracehook_notify_resume(regs);
432 }
005f78cd
AB
433
434 if (thread_flags & _TIF_FOREIGN_FPSTATE)
435 fpsimd_restore_current_state();
436
2c020ed8 437}