1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
4 *
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
7 * 2000-2002 x86-64 support by Andi Kleen
8 */
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/wait.h>
15 #include <linux/tracehook.h>
16 #include <linux/unistd.h>
17 #include <linux/stddef.h>
18 #include <linux/personality.h>
19 #include <linux/uaccess.h>
20 #include <linux/user-return-notifier.h>
21
22 #include <asm/processor.h>
23 #include <asm/ucontext.h>
24 #include <asm/i387.h>
25 #include <asm/fpu-internal.h>
26 #include <asm/vdso.h>
27 #include <asm/mce.h>
28 #include <asm/sighandling.h>
29
30 #ifdef CONFIG_X86_64
31 #include <asm/proto.h>
32 #include <asm/ia32_unistd.h>
33 #include <asm/sys_ia32.h>
34 #endif /* CONFIG_X86_64 */
35
36 #include <asm/syscall.h>
37 #include <asm/syscalls.h>
38
39 #include <asm/sigframe.h>
40
41 #ifdef CONFIG_X86_32
42 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
43 #else
44 # define FIX_EFLAGS __FIX_EFLAGS
45 #endif
46
47 #define COPY(x) do { \
48 get_user_ex(regs->x, &sc->x); \
49 } while (0)
50
51 #define GET_SEG(seg) ({ \
52 unsigned short tmp; \
53 get_user_ex(tmp, &sc->seg); \
54 tmp; \
55 })
56
57 #define COPY_SEG(seg) do { \
58 regs->seg = GET_SEG(seg); \
59 } while (0)
60
61 #define COPY_SEG_CPL3(seg) do { \
62 regs->seg = GET_SEG(seg) | 3; \
63 } while (0)
64
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * sc,unsigned long * pax)65 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
66 unsigned long *pax)
67 {
68 void __user *buf;
69 unsigned int tmpflags;
70 unsigned int err = 0;
71
72 /* Always make any pending restarted system calls return -EINTR */
73 current_thread_info()->restart_block.fn = do_no_restart_syscall;
74
75 get_user_try {
76
77 #ifdef CONFIG_X86_32
78 set_user_gs(regs, GET_SEG(gs));
79 COPY_SEG(fs);
80 COPY_SEG(es);
81 COPY_SEG(ds);
82 #endif /* CONFIG_X86_32 */
83
84 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
85 COPY(dx); COPY(cx); COPY(ip);
86
87 #ifdef CONFIG_X86_64
88 COPY(r8);
89 COPY(r9);
90 COPY(r10);
91 COPY(r11);
92 COPY(r12);
93 COPY(r13);
94 COPY(r14);
95 COPY(r15);
96 #endif /* CONFIG_X86_64 */
97
98 #ifdef CONFIG_X86_32
99 COPY_SEG_CPL3(cs);
100 COPY_SEG_CPL3(ss);
101 #else /* !CONFIG_X86_32 */
102 /* Kernel saves and restores only the CS segment register on signals,
103 * which is the bare minimum needed to allow mixed 32/64-bit code.
104 * App's signal handler can save/restore other segments if needed. */
105 COPY_SEG_CPL3(cs);
106 #endif /* CONFIG_X86_32 */
107
108 get_user_ex(tmpflags, &sc->flags);
109 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
110 regs->orig_ax = -1; /* disable syscall checks */
111
112 get_user_ex(buf, &sc->fpstate);
113 err |= restore_i387_xstate(buf);
114
115 get_user_ex(*pax, &sc->ax);
116 } get_user_catch(err);
117
118 return err;
119 }
120
setup_sigcontext(struct sigcontext __user * sc,void __user * fpstate,struct pt_regs * regs,unsigned long mask)121 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
122 struct pt_regs *regs, unsigned long mask)
123 {
124 int err = 0;
125
126 put_user_try {
127
128 #ifdef CONFIG_X86_32
129 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
130 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
131 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
132 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
133 #endif /* CONFIG_X86_32 */
134
135 put_user_ex(regs->di, &sc->di);
136 put_user_ex(regs->si, &sc->si);
137 put_user_ex(regs->bp, &sc->bp);
138 put_user_ex(regs->sp, &sc->sp);
139 put_user_ex(regs->bx, &sc->bx);
140 put_user_ex(regs->dx, &sc->dx);
141 put_user_ex(regs->cx, &sc->cx);
142 put_user_ex(regs->ax, &sc->ax);
143 #ifdef CONFIG_X86_64
144 put_user_ex(regs->r8, &sc->r8);
145 put_user_ex(regs->r9, &sc->r9);
146 put_user_ex(regs->r10, &sc->r10);
147 put_user_ex(regs->r11, &sc->r11);
148 put_user_ex(regs->r12, &sc->r12);
149 put_user_ex(regs->r13, &sc->r13);
150 put_user_ex(regs->r14, &sc->r14);
151 put_user_ex(regs->r15, &sc->r15);
152 #endif /* CONFIG_X86_64 */
153
154 put_user_ex(current->thread.trap_nr, &sc->trapno);
155 put_user_ex(current->thread.error_code, &sc->err);
156 put_user_ex(regs->ip, &sc->ip);
157 #ifdef CONFIG_X86_32
158 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
159 put_user_ex(regs->flags, &sc->flags);
160 put_user_ex(regs->sp, &sc->sp_at_signal);
161 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
162 #else /* !CONFIG_X86_32 */
163 put_user_ex(regs->flags, &sc->flags);
164 put_user_ex(regs->cs, &sc->cs);
165 put_user_ex(0, &sc->gs);
166 put_user_ex(0, &sc->fs);
167 #endif /* CONFIG_X86_32 */
168
169 put_user_ex(fpstate, &sc->fpstate);
170
171 /* non-iBCS2 extensions.. */
172 put_user_ex(mask, &sc->oldmask);
173 put_user_ex(current->thread.cr2, &sc->cr2);
174 } put_user_catch(err);
175
176 return err;
177 }
178
179 /*
180 * Set up a signal frame.
181 */
182
183 /*
184 * Determine which stack to use..
185 */
align_sigframe(unsigned long sp)186 static unsigned long align_sigframe(unsigned long sp)
187 {
188 #ifdef CONFIG_X86_32
189 /*
190 * Align the stack pointer according to the i386 ABI,
191 * i.e. so that on function entry ((sp + 4) & 15) == 0.
192 */
193 sp = ((sp + 4) & -16ul) - 4;
194 #else /* !CONFIG_X86_32 */
195 sp = round_down(sp, 16) - 8;
196 #endif
197 return sp;
198 }
199
200 static inline void __user *
get_sigframe(struct k_sigaction * ka,struct pt_regs * regs,size_t frame_size,void __user ** fpstate)201 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
202 void __user **fpstate)
203 {
204 /* Default to using normal stack */
205 unsigned long sp = regs->sp;
206 int onsigstack = on_sig_stack(sp);
207
208 #ifdef CONFIG_X86_64
209 /* redzone */
210 sp -= 128;
211 #endif /* CONFIG_X86_64 */
212
213 if (!onsigstack) {
214 /* This is the X/Open sanctioned signal stack switching. */
215 if (ka->sa.sa_flags & SA_ONSTACK) {
216 if (current->sas_ss_size)
217 sp = current->sas_ss_sp + current->sas_ss_size;
218 } else {
219 #ifdef CONFIG_X86_32
220 /* This is the legacy signal stack switching. */
221 if ((regs->ss & 0xffff) != __USER_DS &&
222 !(ka->sa.sa_flags & SA_RESTORER) &&
223 ka->sa.sa_restorer)
224 sp = (unsigned long) ka->sa.sa_restorer;
225 #endif /* CONFIG_X86_32 */
226 }
227 }
228
229 if (used_math()) {
230 sp -= sig_xstate_size;
231 #ifdef CONFIG_X86_64
232 sp = round_down(sp, 64);
233 #endif /* CONFIG_X86_64 */
234 *fpstate = (void __user *)sp;
235 }
236
237 sp = align_sigframe(sp - frame_size);
238
239 /*
240 * If we are on the alternate signal stack and would overflow it, don't.
241 * Return an always-bogus address instead so we will die with SIGSEGV.
242 */
243 if (onsigstack && !likely(on_sig_stack(sp)))
244 return (void __user *)-1L;
245
246 /* save i387 state */
247 if (used_math() && save_i387_xstate(*fpstate) < 0)
248 return (void __user *)-1L;
249
250 return (void __user *)sp;
251 }
252
253 #ifdef CONFIG_X86_32
254 static const struct {
255 u16 poplmovl;
256 u32 val;
257 u16 int80;
258 } __attribute__((packed)) retcode = {
259 0xb858, /* popl %eax; movl $..., %eax */
260 __NR_sigreturn,
261 0x80cd, /* int $0x80 */
262 };
263
264 static const struct {
265 u8 movl;
266 u32 val;
267 u16 int80;
268 u8 pad;
269 } __attribute__((packed)) rt_retcode = {
270 0xb8, /* movl $..., %eax */
271 __NR_rt_sigreturn,
272 0x80cd, /* int $0x80 */
273 0
274 };
275
276 static int
__setup_frame(int sig,struct k_sigaction * ka,sigset_t * set,struct pt_regs * regs)277 __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
278 struct pt_regs *regs)
279 {
280 struct sigframe __user *frame;
281 void __user *restorer;
282 int err = 0;
283 void __user *fpstate = NULL;
284
285 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
286
287 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288 return -EFAULT;
289
290 if (__put_user(sig, &frame->sig))
291 return -EFAULT;
292
293 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
294 return -EFAULT;
295
296 if (_NSIG_WORDS > 1) {
297 if (__copy_to_user(&frame->extramask, &set->sig[1],
298 sizeof(frame->extramask)))
299 return -EFAULT;
300 }
301
302 if (current->mm->context.vdso)
303 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
304 else
305 restorer = &frame->retcode;
306 if (ka->sa.sa_flags & SA_RESTORER)
307 restorer = ka->sa.sa_restorer;
308
309 /* Set up to return from userspace. */
310 err |= __put_user(restorer, &frame->pretcode);
311
312 /*
313 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
314 *
315 * WE DO NOT USE IT ANY MORE! It's only left here for historical
316 * reasons and because gdb uses it as a signature to notice
317 * signal handler stack frames.
318 */
319 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
320
321 if (err)
322 return -EFAULT;
323
324 /* Set up registers for signal handler */
325 regs->sp = (unsigned long)frame;
326 regs->ip = (unsigned long)ka->sa.sa_handler;
327 regs->ax = (unsigned long)sig;
328 regs->dx = 0;
329 regs->cx = 0;
330
331 regs->ds = __USER_DS;
332 regs->es = __USER_DS;
333 regs->ss = __USER_DS;
334 regs->cs = __USER_CS;
335
336 return 0;
337 }
338
__setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set,struct pt_regs * regs)339 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
340 sigset_t *set, struct pt_regs *regs)
341 {
342 struct rt_sigframe __user *frame;
343 void __user *restorer;
344 int err = 0;
345 void __user *fpstate = NULL;
346
347 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
348
349 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
350 return -EFAULT;
351
352 put_user_try {
353 put_user_ex(sig, &frame->sig);
354 put_user_ex(&frame->info, &frame->pinfo);
355 put_user_ex(&frame->uc, &frame->puc);
356 err |= copy_siginfo_to_user(&frame->info, info);
357
358 /* Create the ucontext. */
359 if (cpu_has_xsave)
360 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
361 else
362 put_user_ex(0, &frame->uc.uc_flags);
363 put_user_ex(0, &frame->uc.uc_link);
364 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
365 put_user_ex(sas_ss_flags(regs->sp),
366 &frame->uc.uc_stack.ss_flags);
367 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
368 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
369 regs, set->sig[0]);
370 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
371
372 /* Set up to return from userspace. */
373 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
374 if (ka->sa.sa_flags & SA_RESTORER)
375 restorer = ka->sa.sa_restorer;
376 put_user_ex(restorer, &frame->pretcode);
377
378 /*
379 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
380 *
381 * WE DO NOT USE IT ANY MORE! It's only left here for historical
382 * reasons and because gdb uses it as a signature to notice
383 * signal handler stack frames.
384 */
385 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
386 } put_user_catch(err);
387
388 if (err)
389 return -EFAULT;
390
391 /* Set up registers for signal handler */
392 regs->sp = (unsigned long)frame;
393 regs->ip = (unsigned long)ka->sa.sa_handler;
394 regs->ax = (unsigned long)sig;
395 regs->dx = (unsigned long)&frame->info;
396 regs->cx = (unsigned long)&frame->uc;
397
398 regs->ds = __USER_DS;
399 regs->es = __USER_DS;
400 regs->ss = __USER_DS;
401 regs->cs = __USER_CS;
402
403 return 0;
404 }
405 #else /* !CONFIG_X86_32 */
__setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set,struct pt_regs * regs)406 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
407 sigset_t *set, struct pt_regs *regs)
408 {
409 struct rt_sigframe __user *frame;
410 void __user *fp = NULL;
411 int err = 0;
412 struct task_struct *me = current;
413
414 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
415
416 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
417 return -EFAULT;
418
419 if (ka->sa.sa_flags & SA_SIGINFO) {
420 if (copy_siginfo_to_user(&frame->info, info))
421 return -EFAULT;
422 }
423
424 put_user_try {
425 /* Create the ucontext. */
426 if (cpu_has_xsave)
427 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
428 else
429 put_user_ex(0, &frame->uc.uc_flags);
430 put_user_ex(0, &frame->uc.uc_link);
431 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
432 put_user_ex(sas_ss_flags(regs->sp),
433 &frame->uc.uc_stack.ss_flags);
434 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
435 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
436 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
437
438 /* Set up to return from userspace. If provided, use a stub
439 already in userspace. */
440 /* x86-64 should always use SA_RESTORER. */
441 if (ka->sa.sa_flags & SA_RESTORER) {
442 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
443 } else {
444 /* could use a vstub here */
445 err |= -EFAULT;
446 }
447 } put_user_catch(err);
448
449 if (err)
450 return -EFAULT;
451
452 /* Set up registers for signal handler */
453 regs->di = sig;
454 /* In case the signal handler was declared without prototypes */
455 regs->ax = 0;
456
457 /* This also works for non SA_SIGINFO handlers because they expect the
458 next argument after the signal number on the stack. */
459 regs->si = (unsigned long)&frame->info;
460 regs->dx = (unsigned long)&frame->uc;
461 regs->ip = (unsigned long) ka->sa.sa_handler;
462
463 regs->sp = (unsigned long)frame;
464
465 /* Set up the CS register to run signal handlers in 64-bit mode,
466 even if the handler happens to be interrupting 32-bit code. */
467 regs->cs = __USER_CS;
468
469 return 0;
470 }
471 #endif /* CONFIG_X86_32 */
472
473 #ifdef CONFIG_X86_32
474 /*
475 * Atomically swap in the new signal mask, and wait for a signal.
476 */
477 asmlinkage int
sys_sigsuspend(int history0,int history1,old_sigset_t mask)478 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
479 {
480 sigset_t blocked;
481
482 current->saved_sigmask = current->blocked;
483
484 mask &= _BLOCKABLE;
485 siginitset(&blocked, mask);
486 set_current_blocked(&blocked);
487
488 current->state = TASK_INTERRUPTIBLE;
489 schedule();
490
491 set_restore_sigmask();
492 return -ERESTARTNOHAND;
493 }
494
495 asmlinkage int
sys_sigaction(int sig,const struct old_sigaction __user * act,struct old_sigaction __user * oact)496 sys_sigaction(int sig, const struct old_sigaction __user *act,
497 struct old_sigaction __user *oact)
498 {
499 struct k_sigaction new_ka, old_ka;
500 int ret = 0;
501
502 if (act) {
503 old_sigset_t mask;
504
505 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
506 return -EFAULT;
507
508 get_user_try {
509 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
510 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
511 get_user_ex(mask, &act->sa_mask);
512 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
513 } get_user_catch(ret);
514
515 if (ret)
516 return -EFAULT;
517 siginitset(&new_ka.sa.sa_mask, mask);
518 }
519
520 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
521
522 if (!ret && oact) {
523 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
524 return -EFAULT;
525
526 put_user_try {
527 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
528 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
529 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
530 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
531 } put_user_catch(ret);
532
533 if (ret)
534 return -EFAULT;
535 }
536
537 return ret;
538 }
539 #endif /* CONFIG_X86_32 */
540
541 long
sys_sigaltstack(const stack_t __user * uss,stack_t __user * uoss,struct pt_regs * regs)542 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
543 struct pt_regs *regs)
544 {
545 return do_sigaltstack(uss, uoss, regs->sp);
546 }
547
548 /*
549 * Do a signal return; undo the signal stack.
550 */
551 #ifdef CONFIG_X86_32
sys_sigreturn(struct pt_regs * regs)552 unsigned long sys_sigreturn(struct pt_regs *regs)
553 {
554 struct sigframe __user *frame;
555 unsigned long ax;
556 sigset_t set;
557
558 frame = (struct sigframe __user *)(regs->sp - 8);
559
560 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
561 goto badframe;
562 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
563 && __copy_from_user(&set.sig[1], &frame->extramask,
564 sizeof(frame->extramask))))
565 goto badframe;
566
567 sigdelsetmask(&set, ~_BLOCKABLE);
568 set_current_blocked(&set);
569
570 if (restore_sigcontext(regs, &frame->sc, &ax))
571 goto badframe;
572 return ax;
573
574 badframe:
575 signal_fault(regs, frame, "sigreturn");
576
577 return 0;
578 }
579 #endif /* CONFIG_X86_32 */
580
sys_rt_sigreturn(struct pt_regs * regs)581 long sys_rt_sigreturn(struct pt_regs *regs)
582 {
583 struct rt_sigframe __user *frame;
584 unsigned long ax;
585 sigset_t set;
586
587 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
588 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
589 goto badframe;
590 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
591 goto badframe;
592
593 sigdelsetmask(&set, ~_BLOCKABLE);
594 set_current_blocked(&set);
595
596 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
597 goto badframe;
598
599 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
600 goto badframe;
601
602 return ax;
603
604 badframe:
605 signal_fault(regs, frame, "rt_sigreturn");
606 return 0;
607 }
608
609 /*
610 * OK, we're invoking a handler:
611 */
signr_convert(int sig)612 static int signr_convert(int sig)
613 {
614 #ifdef CONFIG_X86_32
615 struct thread_info *info = current_thread_info();
616
617 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
618 return info->exec_domain->signal_invmap[sig];
619 #endif /* CONFIG_X86_32 */
620 return sig;
621 }
622
623 #ifdef CONFIG_X86_32
624
625 #define is_ia32 1
626 #define ia32_setup_frame __setup_frame
627 #define ia32_setup_rt_frame __setup_rt_frame
628
629 #else /* !CONFIG_X86_32 */
630
631 #ifdef CONFIG_IA32_EMULATION
632 #define is_ia32 test_thread_flag(TIF_IA32)
633 #else /* !CONFIG_IA32_EMULATION */
634 #define is_ia32 0
635 #endif /* CONFIG_IA32_EMULATION */
636
637 #ifdef CONFIG_X86_X32_ABI
638 #define is_x32 test_thread_flag(TIF_X32)
639
640 static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
641 siginfo_t *info, compat_sigset_t *set,
642 struct pt_regs *regs);
643 #else /* !CONFIG_X86_X32_ABI */
644 #define is_x32 0
645 #endif /* CONFIG_X86_X32_ABI */
646
647 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
648 sigset_t *set, struct pt_regs *regs);
649 int ia32_setup_frame(int sig, struct k_sigaction *ka,
650 sigset_t *set, struct pt_regs *regs);
651
652 #endif /* CONFIG_X86_32 */
653
654 static int
setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,struct pt_regs * regs)655 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
656 struct pt_regs *regs)
657 {
658 int usig = signr_convert(sig);
659 sigset_t *set = ¤t->blocked;
660 int ret;
661
662 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
663 set = ¤t->saved_sigmask;
664
665 /* Set up the stack frame */
666 if (is_ia32) {
667 if (ka->sa.sa_flags & SA_SIGINFO)
668 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
669 else
670 ret = ia32_setup_frame(usig, ka, set, regs);
671 #ifdef CONFIG_X86_X32_ABI
672 } else if (is_x32) {
673 ret = x32_setup_rt_frame(usig, ka, info,
674 (compat_sigset_t *)set, regs);
675 #endif
676 } else {
677 ret = __setup_rt_frame(sig, ka, info, set, regs);
678 }
679
680 if (ret) {
681 force_sigsegv(sig, current);
682 return -EFAULT;
683 }
684
685 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
686 return ret;
687 }
688
689 static int
handle_signal(unsigned long sig,siginfo_t * info,struct k_sigaction * ka,struct pt_regs * regs)690 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
691 struct pt_regs *regs)
692 {
693 int ret;
694
695 /* Are we from a system call? */
696 if (syscall_get_nr(current, regs) >= 0) {
697 /* If so, check system call restarting.. */
698 switch (syscall_get_error(current, regs)) {
699 case -ERESTART_RESTARTBLOCK:
700 case -ERESTARTNOHAND:
701 regs->ax = -EINTR;
702 break;
703
704 case -ERESTARTSYS:
705 if (!(ka->sa.sa_flags & SA_RESTART)) {
706 regs->ax = -EINTR;
707 break;
708 }
709 /* fallthrough */
710 case -ERESTARTNOINTR:
711 regs->ax = regs->orig_ax;
712 regs->ip -= 2;
713 break;
714 }
715 }
716
717 /*
718 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
719 * flag so that register information in the sigcontext is correct.
720 */
721 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
722 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
723 regs->flags &= ~X86_EFLAGS_TF;
724
725 ret = setup_rt_frame(sig, ka, info, regs);
726
727 if (ret)
728 return ret;
729
730 /*
731 * Clear the direction flag as per the ABI for function entry.
732 */
733 regs->flags &= ~X86_EFLAGS_DF;
734
735 /*
736 * Clear TF when entering the signal handler, but
737 * notify any tracer that was single-stepping it.
738 * The tracer may want to single-step inside the
739 * handler too.
740 */
741 regs->flags &= ~X86_EFLAGS_TF;
742
743 block_sigmask(ka, sig);
744
745 tracehook_signal_handler(sig, info, ka, regs,
746 test_thread_flag(TIF_SINGLESTEP));
747
748 return 0;
749 }
750
751 #ifdef CONFIG_X86_32
752 #define NR_restart_syscall __NR_restart_syscall
753 #else /* !CONFIG_X86_32 */
754 #define NR_restart_syscall \
755 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
756 #endif /* CONFIG_X86_32 */
757
758 /*
759 * Note that 'init' is a special process: it doesn't get signals it doesn't
760 * want to handle. Thus you cannot kill init even with a SIGKILL even by
761 * mistake.
762 */
do_signal(struct pt_regs * regs)763 static void do_signal(struct pt_regs *regs)
764 {
765 struct k_sigaction ka;
766 siginfo_t info;
767 int signr;
768
769 /*
770 * We want the common case to go fast, which is why we may in certain
771 * cases get here from kernel mode. Just return without doing anything
772 * if so.
773 * X86_32: vm86 regs switched out by assembly code before reaching
774 * here, so testing against kernel CS suffices.
775 */
776 if (!user_mode(regs))
777 return;
778
779 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
780 if (signr > 0) {
781 /* Whee! Actually deliver the signal. */
782 handle_signal(signr, &info, &ka, regs);
783 return;
784 }
785
786 /* Did we come from a system call? */
787 if (syscall_get_nr(current, regs) >= 0) {
788 /* Restart the system call - no handlers present */
789 switch (syscall_get_error(current, regs)) {
790 case -ERESTARTNOHAND:
791 case -ERESTARTSYS:
792 case -ERESTARTNOINTR:
793 regs->ax = regs->orig_ax;
794 regs->ip -= 2;
795 break;
796
797 case -ERESTART_RESTARTBLOCK:
798 regs->ax = NR_restart_syscall;
799 regs->ip -= 2;
800 break;
801 }
802 }
803
804 /*
805 * If there's no signal to deliver, we just put the saved sigmask
806 * back.
807 */
808 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
809 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
810 set_current_blocked(¤t->saved_sigmask);
811 }
812 }
813
814 /*
815 * notification of userspace execution resumption
816 * - triggered by the TIF_WORK_MASK flags
817 */
818 void
do_notify_resume(struct pt_regs * regs,void * unused,__u32 thread_info_flags)819 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
820 {
821 #ifdef CONFIG_X86_MCE
822 /* notify userspace of pending MCEs */
823 if (thread_info_flags & _TIF_MCE_NOTIFY)
824 mce_notify_process();
825 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
826
827 /* deal with pending signal delivery */
828 if (thread_info_flags & _TIF_SIGPENDING)
829 do_signal(regs);
830
831 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
832 clear_thread_flag(TIF_NOTIFY_RESUME);
833 tracehook_notify_resume(regs);
834 if (current->replacement_session_keyring)
835 key_replace_session_keyring();
836 }
837 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
838 fire_user_return_notifiers();
839
840 #ifdef CONFIG_X86_32
841 clear_thread_flag(TIF_IRET);
842 #endif /* CONFIG_X86_32 */
843 }
844
signal_fault(struct pt_regs * regs,void __user * frame,char * where)845 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
846 {
847 struct task_struct *me = current;
848
849 if (show_unhandled_signals && printk_ratelimit()) {
850 printk("%s"
851 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
852 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
853 me->comm, me->pid, where, frame,
854 regs->ip, regs->sp, regs->orig_ax);
855 print_vma_addr(" in ", regs->ip);
856 printk(KERN_CONT "\n");
857 }
858
859 force_sig(SIGSEGV, me);
860 }
861
862 #ifdef CONFIG_X86_X32_ABI
x32_setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,compat_sigset_t * set,struct pt_regs * regs)863 static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
864 siginfo_t *info, compat_sigset_t *set,
865 struct pt_regs *regs)
866 {
867 struct rt_sigframe_x32 __user *frame;
868 void __user *restorer;
869 int err = 0;
870 void __user *fpstate = NULL;
871
872 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
873
874 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
875 return -EFAULT;
876
877 if (ka->sa.sa_flags & SA_SIGINFO) {
878 if (copy_siginfo_to_user32(&frame->info, info))
879 return -EFAULT;
880 }
881
882 put_user_try {
883 /* Create the ucontext. */
884 if (cpu_has_xsave)
885 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
886 else
887 put_user_ex(0, &frame->uc.uc_flags);
888 put_user_ex(0, &frame->uc.uc_link);
889 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
890 put_user_ex(sas_ss_flags(regs->sp),
891 &frame->uc.uc_stack.ss_flags);
892 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
893 put_user_ex(0, &frame->uc.uc__pad0);
894 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
895 regs, set->sig[0]);
896 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
897
898 if (ka->sa.sa_flags & SA_RESTORER) {
899 restorer = ka->sa.sa_restorer;
900 } else {
901 /* could use a vstub here */
902 restorer = NULL;
903 err |= -EFAULT;
904 }
905 put_user_ex(restorer, &frame->pretcode);
906 } put_user_catch(err);
907
908 if (err)
909 return -EFAULT;
910
911 /* Set up registers for signal handler */
912 regs->sp = (unsigned long) frame;
913 regs->ip = (unsigned long) ka->sa.sa_handler;
914
915 /* We use the x32 calling convention here... */
916 regs->di = sig;
917 regs->si = (unsigned long) &frame->info;
918 regs->dx = (unsigned long) &frame->uc;
919
920 loadsegment(ds, __USER_DS);
921 loadsegment(es, __USER_DS);
922
923 regs->cs = __USER_CS;
924 regs->ss = __USER_DS;
925
926 return 0;
927 }
928
sys32_x32_rt_sigreturn(struct pt_regs * regs)929 asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
930 {
931 struct rt_sigframe_x32 __user *frame;
932 sigset_t set;
933 unsigned long ax;
934 struct pt_regs tregs;
935
936 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
937
938 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
939 goto badframe;
940 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
941 goto badframe;
942
943 sigdelsetmask(&set, ~_BLOCKABLE);
944 set_current_blocked(&set);
945
946 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
947 goto badframe;
948
949 tregs = *regs;
950 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
951 goto badframe;
952
953 return ax;
954
955 badframe:
956 signal_fault(regs, frame, "x32 rt_sigreturn");
957 return 0;
958 }
959 #endif
960