1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/sched.h>
16#include <linux/mm.h>
17#include <linux/smp.h>
18#include <linux/kernel.h>
19#include <linux/signal.h>
20#include <linux/errno.h>
21#include <linux/wait.h>
22#include <linux/unistd.h>
23#include <linux/stddef.h>
24#include <linux/elf.h>
25#include <linux/ptrace.h>
26#include <linux/ratelimit.h>
27
28#include <asm/sigcontext.h>
29#include <asm/ucontext.h>
30#include <asm/uaccess.h>
31#include <asm/pgtable.h>
32#include <asm/unistd.h>
33#include <asm/cacheflush.h>
34#include <asm/syscalls.h>
35#include <asm/vdso.h>
36#include <asm/switch_to.h>
37
38#include "signal.h"
39
40#define DEBUG_SIG 0
41
42#define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
43#define FP_REGS_SIZE sizeof(elf_fpregset_t)
44
45#define TRAMP_TRACEBACK 3
46#define TRAMP_SIZE 6
47
48
49
50
51
52
53
54
55
56struct rt_sigframe {
57
58 struct ucontext uc;
59 unsigned long _unused[2];
60 unsigned int tramp[TRAMP_SIZE];
61 struct siginfo __user *pinfo;
62 void __user *puc;
63 struct siginfo info;
64
65 char abigap[288];
66} __attribute__ ((aligned (16)));
67
68static const char fmt32[] = KERN_INFO \
69 "%s[%d]: bad frame in %s: %08lx nip %08lx lr %08lx\n";
70static const char fmt64[] = KERN_INFO \
71 "%s[%d]: bad frame in %s: %016lx nip %016lx lr %016lx\n";
72
73
74
75
76
77static long setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
78 int signr, sigset_t *set, unsigned long handler,
79 int ctx_has_vsx_region)
80{
81
82
83
84
85
86
87
88
89#ifdef CONFIG_ALTIVEC
90 elf_vrreg_t __user *v_regs = (elf_vrreg_t __user *)(((unsigned long)sc->vmx_reserve + 15) & ~0xful);
91#endif
92 unsigned long msr = regs->msr;
93 long err = 0;
94
95 flush_fp_to_thread(current);
96
97#ifdef CONFIG_ALTIVEC
98 err |= __put_user(v_regs, &sc->v_regs);
99
100
101 if (current->thread.used_vr) {
102 flush_altivec_to_thread(current);
103
104 err |= __copy_to_user(v_regs, current->thread.vr, 33 * sizeof(vector128));
105
106
107
108 msr |= MSR_VEC;
109 }
110
111
112
113 err |= __put_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
114#else
115 err |= __put_user(0, &sc->v_regs);
116#endif
117 flush_fp_to_thread(current);
118
119 err |= copy_fpr_to_user(&sc->fp_regs, current);
120#ifdef CONFIG_VSX
121
122
123
124
125
126 if (current->thread.used_vsr && ctx_has_vsx_region) {
127 __giveup_vsx(current);
128 v_regs += ELF_NVRREG;
129 err |= copy_vsx_to_user(v_regs, current);
130
131
132
133 msr |= MSR_VSX;
134 }
135#endif
136 err |= __put_user(&sc->gp_regs, &sc->regs);
137 WARN_ON(!FULL_REGS(regs));
138 err |= __copy_to_user(&sc->gp_regs, regs, GP_REGS_SIZE);
139 err |= __put_user(msr, &sc->gp_regs[PT_MSR]);
140 err |= __put_user(signr, &sc->signal);
141 err |= __put_user(handler, &sc->handler);
142 if (set != NULL)
143 err |= __put_user(set->sig[0], &sc->oldmask);
144
145 return err;
146}
147
148
149
150
151
152static long restore_sigcontext(struct pt_regs *regs, sigset_t *set, int sig,
153 struct sigcontext __user *sc)
154{
155#ifdef CONFIG_ALTIVEC
156 elf_vrreg_t __user *v_regs;
157#endif
158 unsigned long err = 0;
159 unsigned long save_r13 = 0;
160 unsigned long msr;
161#ifdef CONFIG_VSX
162 int i;
163#endif
164
165
166 if (!sig)
167 save_r13 = regs->gpr[13];
168
169
170 err |= __copy_from_user(regs->gpr, sc->gp_regs, sizeof(regs->gpr));
171 err |= __get_user(regs->nip, &sc->gp_regs[PT_NIP]);
172
173 err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
174 if (sig)
175 regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
176 err |= __get_user(regs->orig_gpr3, &sc->gp_regs[PT_ORIG_R3]);
177 err |= __get_user(regs->ctr, &sc->gp_regs[PT_CTR]);
178 err |= __get_user(regs->link, &sc->gp_regs[PT_LNK]);
179 err |= __get_user(regs->xer, &sc->gp_regs[PT_XER]);
180 err |= __get_user(regs->ccr, &sc->gp_regs[PT_CCR]);
181
182 regs->trap = 0;
183 err |= __get_user(regs->dar, &sc->gp_regs[PT_DAR]);
184 err |= __get_user(regs->dsisr, &sc->gp_regs[PT_DSISR]);
185 err |= __get_user(regs->result, &sc->gp_regs[PT_RESULT]);
186
187 if (!sig)
188 regs->gpr[13] = save_r13;
189 if (set != NULL)
190 err |= __get_user(set->sig[0], &sc->oldmask);
191
192
193
194
195
196
197
198
199 discard_lazy_cpu_state();
200
201
202
203
204
205
206 regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX);
207
208#ifdef CONFIG_ALTIVEC
209 err |= __get_user(v_regs, &sc->v_regs);
210 if (err)
211 return err;
212 if (v_regs && !access_ok(VERIFY_READ, v_regs, 34 * sizeof(vector128)))
213 return -EFAULT;
214
215 if (v_regs != 0 && (msr & MSR_VEC) != 0)
216 err |= __copy_from_user(current->thread.vr, v_regs,
217 33 * sizeof(vector128));
218 else if (current->thread.used_vr)
219 memset(current->thread.vr, 0, 33 * sizeof(vector128));
220
221 if (v_regs != 0)
222 err |= __get_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
223 else
224 current->thread.vrsave = 0;
225#endif
226
227 err |= copy_fpr_from_user(current, &sc->fp_regs);
228#ifdef CONFIG_VSX
229
230
231
232
233
234 v_regs += ELF_NVRREG;
235 if ((msr & MSR_VSX) != 0)
236 err |= copy_vsx_from_user(current, v_regs);
237 else
238 for (i = 0; i < 32 ; i++)
239 current->thread.fpr[i][TS_VSRLOWOFFSET] = 0;
240#endif
241 return err;
242}
243
244
245
246
247static long setup_trampoline(unsigned int syscall, unsigned int __user *tramp)
248{
249 int i;
250 long err = 0;
251
252
253 err |= __put_user(0x38210000UL | (__SIGNAL_FRAMESIZE & 0xffff), &tramp[0]);
254
255 err |= __put_user(0x38000000UL | (syscall & 0xffff), &tramp[1]);
256
257 err |= __put_user(0x44000002UL, &tramp[2]);
258
259
260 for (i=TRAMP_TRACEBACK; i < TRAMP_SIZE ;i++)
261 err |= __put_user(0, &tramp[i]);
262
263 if (!err)
264 flush_icache_range((unsigned long) &tramp[0],
265 (unsigned long) &tramp[TRAMP_SIZE]);
266
267 return err;
268}
269
270
271
272
273
274#define UCONTEXTSIZEWITHOUTVSX \
275 (sizeof(struct ucontext) - 32*sizeof(long))
276
277
278
279
280int sys_swapcontext(struct ucontext __user *old_ctx,
281 struct ucontext __user *new_ctx,
282 long ctx_size, long r6, long r7, long r8, struct pt_regs *regs)
283{
284 unsigned char tmp;
285 sigset_t set;
286 unsigned long new_msr = 0;
287 int ctx_has_vsx_region = 0;
288
289 if (new_ctx &&
290 get_user(new_msr, &new_ctx->uc_mcontext.gp_regs[PT_MSR]))
291 return -EFAULT;
292
293
294
295
296 if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
297 return -EINVAL;
298
299
300
301
302 if ((ctx_size < sizeof(struct ucontext)) &&
303 (new_msr & MSR_VSX))
304 return -EINVAL;
305
306 if (ctx_size >= sizeof(struct ucontext))
307 ctx_has_vsx_region = 1;
308
309 if (old_ctx != NULL) {
310 if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
311 || setup_sigcontext(&old_ctx->uc_mcontext, regs, 0, NULL, 0,
312 ctx_has_vsx_region)
313 || __copy_to_user(&old_ctx->uc_sigmask,
314 ¤t->blocked, sizeof(sigset_t)))
315 return -EFAULT;
316 }
317 if (new_ctx == NULL)
318 return 0;
319 if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
320 || __get_user(tmp, (u8 __user *) new_ctx)
321 || __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
322 return -EFAULT;
323
324
325
326
327
328
329
330
331
332
333
334
335
336 if (__copy_from_user(&set, &new_ctx->uc_sigmask, sizeof(set)))
337 do_exit(SIGSEGV);
338 restore_sigmask(&set);
339 if (restore_sigcontext(regs, NULL, 0, &new_ctx->uc_mcontext))
340 do_exit(SIGSEGV);
341
342
343 set_thread_flag(TIF_RESTOREALL);
344 return 0;
345}
346
347
348
349
350
351
352int sys_rt_sigreturn(unsigned long r3, unsigned long r4, unsigned long r5,
353 unsigned long r6, unsigned long r7, unsigned long r8,
354 struct pt_regs *regs)
355{
356 struct ucontext __user *uc = (struct ucontext __user *)regs->gpr[1];
357 sigset_t set;
358
359
360 current_thread_info()->restart_block.fn = do_no_restart_syscall;
361
362 if (!access_ok(VERIFY_READ, uc, sizeof(*uc)))
363 goto badframe;
364
365 if (__copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
366 goto badframe;
367 restore_sigmask(&set);
368 if (restore_sigcontext(regs, NULL, 1, &uc->uc_mcontext))
369 goto badframe;
370
371
372
373
374 do_sigaltstack(&uc->uc_stack, NULL, regs->gpr[1]);
375
376 set_thread_flag(TIF_RESTOREALL);
377 return 0;
378
379badframe:
380#if DEBUG_SIG
381 printk("badframe in sys_rt_sigreturn, regs=%p uc=%p &uc->uc_mcontext=%p\n",
382 regs, uc, &uc->uc_mcontext);
383#endif
384 if (show_unhandled_signals)
385 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
386 current->comm, current->pid, "rt_sigreturn",
387 (long)uc, regs->nip, regs->link);
388
389 force_sig(SIGSEGV, current);
390 return 0;
391}
392
393int handle_rt_signal64(int signr, struct k_sigaction *ka, siginfo_t *info,
394 sigset_t *set, struct pt_regs *regs)
395{
396
397
398
399
400
401 func_descr_t __user *funct_desc_ptr;
402 struct rt_sigframe __user *frame;
403 unsigned long newsp = 0;
404 long err = 0;
405
406 frame = get_sigframe(ka, regs, sizeof(*frame), 0);
407 if (unlikely(frame == NULL))
408 goto badframe;
409
410 err |= __put_user(&frame->info, &frame->pinfo);
411 err |= __put_user(&frame->uc, &frame->puc);
412 err |= copy_siginfo_to_user(&frame->info, info);
413 if (err)
414 goto badframe;
415
416
417 err |= __put_user(0, &frame->uc.uc_flags);
418 err |= __put_user(0, &frame->uc.uc_link);
419 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
420 err |= __put_user(sas_ss_flags(regs->gpr[1]),
421 &frame->uc.uc_stack.ss_flags);
422 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
423 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, signr, NULL,
424 (unsigned long)ka->sa.sa_handler, 1);
425 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
426 if (err)
427 goto badframe;
428
429
430 current->thread.fpscr.val = 0;
431
432
433 if (vdso64_rt_sigtramp && current->mm->context.vdso_base) {
434 regs->link = current->mm->context.vdso_base + vdso64_rt_sigtramp;
435 } else {
436 err |= setup_trampoline(__NR_rt_sigreturn, &frame->tramp[0]);
437 if (err)
438 goto badframe;
439 regs->link = (unsigned long) &frame->tramp[0];
440 }
441 funct_desc_ptr = (func_descr_t __user *) ka->sa.sa_handler;
442
443
444 newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
445 err |= put_user(regs->gpr[1], (unsigned long __user *)newsp);
446
447
448 err |= get_user(regs->nip, &funct_desc_ptr->entry);
449
450 regs->msr &= ~MSR_LE;
451 regs->gpr[1] = newsp;
452 err |= get_user(regs->gpr[2], &funct_desc_ptr->toc);
453 regs->gpr[3] = signr;
454 regs->result = 0;
455 if (ka->sa.sa_flags & SA_SIGINFO) {
456 err |= get_user(regs->gpr[4], (unsigned long __user *)&frame->pinfo);
457 err |= get_user(regs->gpr[5], (unsigned long __user *)&frame->puc);
458 regs->gpr[6] = (unsigned long) frame;
459 } else {
460 regs->gpr[4] = (unsigned long)&frame->uc.uc_mcontext;
461 }
462 if (err)
463 goto badframe;
464
465 return 1;
466
467badframe:
468#if DEBUG_SIG
469 printk("badframe in setup_rt_frame, regs=%p frame=%p newsp=%lx\n",
470 regs, frame, newsp);
471#endif
472 if (show_unhandled_signals)
473 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
474 current->comm, current->pid, "setup_rt_frame",
475 (long)frame, regs->nip, regs->link);
476
477 force_sigsegv(signr, current);
478 return 0;
479}
480