blob: b68ccadd2ff4ba52217a703de590d00803af9b95 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08003 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08007 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Ingo Molnar7e907f42008-03-06 10:33:08 +01009#include <linux/sched.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010010#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080011#include <linux/smp.h>
12#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080013#include <linux/errno.h>
14#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080015#include <linux/tracehook.h>
16#include <linux/unistd.h>
17#include <linux/stddef.h>
18#include <linux/personality.h>
19#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030020#include <linux/user-return-notifier.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/processor.h>
23#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080025#include <asm/fpu-internal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010026#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020027#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080028#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080029
30#ifdef CONFIG_X86_64
31#include <asm/proto.h>
32#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080033#include <asm/sys_ia32.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080034#endif /* CONFIG_X86_64 */
35
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070036#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053037#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010038
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080039#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Harvey Harrison1a176802008-02-08 12:09:59 -080041#ifdef CONFIG_X86_32
42# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
43#else
44# define FIX_EFLAGS __FIX_EFLAGS
45#endif
46
Tejun Heod9a89a22009-02-09 22:17:40 +090047#define COPY(x) do { \
48 get_user_ex(regs->x, &sc->x); \
49} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080050
Tejun Heod9a89a22009-02-09 22:17:40 +090051#define GET_SEG(seg) ({ \
52 unsigned short tmp; \
53 get_user_ex(tmp, &sc->seg); \
54 tmp; \
55})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080056
Tejun Heod9a89a22009-02-09 22:17:40 +090057#define COPY_SEG(seg) do { \
58 regs->seg = GET_SEG(seg); \
59} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080060
Tejun Heod9a89a22009-02-09 22:17:40 +090061#define COPY_SEG_CPL3(seg) do { \
62 regs->seg = GET_SEG(seg) | 3; \
63} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080064
H. Peter Anvin85139422012-02-19 07:43:09 -080065int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
66 unsigned long *pax)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080067{
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
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080075 get_user_try {
76
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080077#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090078 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080079 COPY_SEG(fs);
80 COPY_SEG(es);
81 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080082#endif /* CONFIG_X86_32 */
83
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080084 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
85 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080086
87#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080088 COPY(r8);
89 COPY(r9);
90 COPY(r10);
91 COPY(r11);
92 COPY(r12);
93 COPY(r13);
94 COPY(r14);
95 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080096#endif /* CONFIG_X86_64 */
97
98#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080099 COPY_SEG_CPL3(cs);
100 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800101#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800102 /* 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800106#endif /* CONFIG_X86_32 */
107
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800108 get_user_ex(tmpflags, &sc->flags);
109 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
110 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800111
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800112 get_user_ex(buf, &sc->fpstate);
113 err |= restore_i387_xstate(buf);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800114
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800115 get_user_ex(*pax, &sc->ax);
116 } get_user_catch(err);
117
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800118 return err;
119}
120
H. Peter Anvin85139422012-02-19 07:43:09 -0800121int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
122 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800123{
124 int err = 0;
125
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800126 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800127
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800128#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900129 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800130 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800133#endif /* CONFIG_X86_32 */
134
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800135 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800143#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800144 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800152#endif /* CONFIG_X86_64 */
153
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530154 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800155 put_user_ex(current->thread.error_code, &sc->err);
156 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800157#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800158 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800163 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800167#endif /* CONFIG_X86_32 */
168
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800169 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800170
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800171 /* non-iBCS2 extensions.. */
172 put_user_ex(mask, &sc->oldmask);
173 put_user_ex(current->thread.cr2, &sc->cr2);
174 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800175
176 return err;
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Set up a signal frame.
181 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800182
183/*
184 * Determine which stack to use..
185 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800186static 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
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800200static inline void __user *
201get_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;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700206 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800207
208#ifdef CONFIG_X86_64
209 /* redzone */
210 sp -= 128;
211#endif /* CONFIG_X86_64 */
212
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700213 if (!onsigstack) {
214 /* This is the X/Open sanctioned signal stack switching. */
215 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700216 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700217 sp = current->sas_ss_sp + current->sas_ss_size;
218 } else {
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800219#ifdef CONFIG_X86_32
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700220 /* 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;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800225#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700226 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800227 }
228
229 if (used_math()) {
230 sp -= sig_xstate_size;
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800231#ifdef CONFIG_X86_64
232 sp = round_down(sp, 64);
233#endif /* CONFIG_X86_64 */
234 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800235 }
236
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700237 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;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800251}
252
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800253#ifdef CONFIG_X86_32
254static 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
264static 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Ingo Molnar7e907f42008-03-06 10:33:08 +0100276static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700277__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
278 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100281 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700283 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700285 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700288 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700290 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700291 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700293 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700294 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700297 if (__copy_to_user(&frame->extramask, &set->sig[1],
298 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700299 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700302 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100303 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100304 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100305 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 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);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100313 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
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 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800319 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700322 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100325 regs->sp = (unsigned long)frame;
326 regs->ip = (unsigned long)ka->sa.sa_handler;
327 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800328 regs->dx = 0;
329 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100331 regs->ds = __USER_DS;
332 regs->es = __USER_DS;
333 regs->ss = __USER_DS;
334 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Howells283828f2006-01-18 17:44:00 -0800336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700339static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
340 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100343 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700345 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700347 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700350 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800352 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800358 /* 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800372 /* 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);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100377
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800378 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700389 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100392 regs->sp = (unsigned long)frame;
393 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700394 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100395 regs->dx = (unsigned long)&frame->info;
396 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100398 regs->ds = __USER_DS;
399 regs->es = __USER_DS;
400 regs->ss = __USER_DS;
401 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells283828f2006-01-18 17:44:00 -0800403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800405#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800406static 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
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800414 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800415
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
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800424 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));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800437
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800438 /* 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);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800448
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
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800473#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800474/*
475 * Atomically swap in the new signal mask, and wait for a signal.
476 */
477asmlinkage int
478sys_sigsuspend(int history0, int history1, old_sigset_t mask)
479{
Oleg Nesterov39822942011-07-10 21:27:27 +0200480 sigset_t blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200481 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -0400482 return sigsuspend(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800483}
484
485asmlinkage int
486sys_sigaction(int sig, const struct old_sigaction __user *act,
487 struct old_sigaction __user *oact)
488{
489 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800490 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800491
492 if (act) {
493 old_sigset_t mask;
494
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800495 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800496 return -EFAULT;
497
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800498 get_user_try {
499 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
500 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
501 get_user_ex(mask, &act->sa_mask);
502 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
503 } get_user_catch(ret);
504
505 if (ret)
506 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800507 siginitset(&new_ka.sa.sa_mask, mask);
508 }
509
510 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
511
512 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800513 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800514 return -EFAULT;
515
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800516 put_user_try {
517 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
518 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
519 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
520 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
521 } put_user_catch(ret);
522
523 if (ret)
524 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800525 }
526
527 return ret;
528}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800529#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800530
Brian Gerst052acad2009-12-09 19:01:54 -0500531long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800532sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
533 struct pt_regs *regs)
534{
535 return do_sigaltstack(uss, uoss, regs->sp);
536}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800537
538/*
539 * Do a signal return; undo the signal stack.
540 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800541#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500542unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800543{
544 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800545 unsigned long ax;
546 sigset_t set;
547
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800548 frame = (struct sigframe __user *)(regs->sp - 8);
549
550 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
551 goto badframe;
552 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
553 && __copy_from_user(&set.sig[1], &frame->extramask,
554 sizeof(frame->extramask))))
555 goto badframe;
556
557 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterov39822942011-07-10 21:27:27 +0200558 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800559
560 if (restore_sigcontext(regs, &frame->sc, &ax))
561 goto badframe;
562 return ax;
563
564badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800565 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800566
567 return 0;
568}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800569#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800570
H. Peter Anvin74452502009-02-11 16:31:40 -0800571long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800572{
573 struct rt_sigframe __user *frame;
574 unsigned long ax;
575 sigset_t set;
576
577 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
578 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
579 goto badframe;
580 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
581 goto badframe;
582
583 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200584 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800585
586 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
587 goto badframe;
588
589 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
590 goto badframe;
591
592 return ax;
593
594badframe:
595 signal_fault(regs, frame, "rt_sigreturn");
596 return 0;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100600 * OK, we're invoking a handler:
601 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700602static int signr_convert(int sig)
603{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700604#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700605 struct thread_info *info = current_thread_info();
606
607 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
608 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700609#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700610 return sig;
611}
612
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700613#ifdef CONFIG_X86_32
614
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700615#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700616#define ia32_setup_frame __setup_frame
617#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700618
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700619#else /* !CONFIG_X86_32 */
620
621#ifdef CONFIG_IA32_EMULATION
622#define is_ia32 test_thread_flag(TIF_IA32)
623#else /* !CONFIG_IA32_EMULATION */
624#define is_ia32 0
625#endif /* CONFIG_IA32_EMULATION */
626
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800627#ifdef CONFIG_X86_X32_ABI
628#define is_x32 test_thread_flag(TIF_X32)
629
630static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
631 siginfo_t *info, compat_sigset_t *set,
632 struct pt_regs *regs);
633#else /* !CONFIG_X86_X32_ABI */
634#define is_x32 0
635#endif /* CONFIG_X86_X32_ABI */
636
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800637int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
638 sigset_t *set, struct pt_regs *regs);
639int ia32_setup_frame(int sig, struct k_sigaction *ka,
640 sigset_t *set, struct pt_regs *regs);
641
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700642#endif /* CONFIG_X86_32 */
643
Roland McGrath7c1def12005-06-23 00:08:21 -0700644static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700645setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200646 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700647{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700648 int usig = signr_convert(sig);
Oleg Nesterov9b429622011-07-10 20:22:03 +0200649 sigset_t *set = &current->blocked;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700650 int ret;
651
Oleg Nesterov9b429622011-07-10 20:22:03 +0200652 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
653 set = &current->saved_sigmask;
654
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700655 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700656 if (is_ia32) {
657 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700658 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700659 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700660 ret = ia32_setup_frame(usig, ka, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800661#ifdef CONFIG_X86_X32_ABI
662 } else if (is_x32) {
663 ret = x32_setup_rt_frame(usig, ka, info,
664 (compat_sigset_t *)set, regs);
665#endif
666 } else {
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700667 ret = __setup_rt_frame(sig, ka, info, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800668 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700669
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700670 if (ret) {
671 force_sigsegv(sig, current);
672 return -EFAULT;
673 }
674
Oleg Nesterov9b429622011-07-10 20:22:03 +0200675 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700676 return ret;
677}
678
679static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200681 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Roland McGrath7c1def12005-06-23 00:08:21 -0700683 int ret;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700686 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700688 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800689 case -ERESTART_RESTARTBLOCK:
690 case -ERESTARTNOHAND:
691 regs->ax = -EINTR;
692 break;
693
694 case -ERESTARTSYS:
695 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100696 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800698 }
699 /* fallthrough */
700 case -ERESTARTNOINTR:
701 regs->ax = regs->orig_ax;
702 regs->ip -= 2;
703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 }
706
707 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100708 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
709 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100711 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100712 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100713 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Oleg Nesterov9b429622011-07-10 20:22:03 +0200715 ret = setup_rt_frame(sig, ka, info, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Ingo Molnar7e907f42008-03-06 10:33:08 +0100717 if (ret)
718 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700719
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700720 /*
721 * Clear the direction flag as per the ABI for function entry.
722 */
723 regs->flags &= ~X86_EFLAGS_DF;
724
725 /*
726 * Clear TF when entering the signal handler, but
727 * notify any tracer that was single-stepping it.
728 * The tracer may want to single-step inside the
729 * handler too.
730 */
731 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700732
Matt Fleming5e6292c2012-01-10 15:11:17 -0800733 block_sigmask(ka, sig);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100734
Roland McGrath36a03302008-03-14 17:46:38 -0700735 tracehook_signal_handler(sig, info, ka, regs,
736 test_thread_flag(TIF_SINGLESTEP));
737
Ingo Molnar7e907f42008-03-06 10:33:08 +0100738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700741#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700742#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700743#else /* !CONFIG_X86_32 */
744#define NR_restart_syscall \
745 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
746#endif /* CONFIG_X86_32 */
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/*
749 * Note that 'init' is a special process: it doesn't get signals it doesn't
750 * want to handle. Thus you cannot kill init even with a SIGKILL even by
751 * mistake.
752 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100753static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800755 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 siginfo_t info;
757 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800760 * We want the common case to go fast, which is why we may in certain
761 * cases get here from kernel mode. Just return without doing anything
762 * if so.
763 * X86_32: vm86 regs switched out by assembly code before reaching
764 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700766 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800767 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
770 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100771 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200772 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800773 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700777 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700779 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800780 case -ERESTARTNOHAND:
781 case -ERESTARTSYS:
782 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100783 regs->ax = regs->orig_ax;
784 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800785 break;
786
787 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700788 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100789 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 }
David Howells283828f2006-01-18 17:44:00 -0800793
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800794 /*
795 * If there's no signal to deliver, we just put the saved sigmask
796 * back.
797 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700798 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
799 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Oleg Nesterov9b429622011-07-10 20:22:03 +0200800 set_current_blocked(&current->saved_sigmask);
David Howells283828f2006-01-18 17:44:00 -0800801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
805 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800806 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100808void
809do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Andi Kleenc1ebf832009-07-09 00:31:41 +0200811#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700812 /* notify userspace of pending MCEs */
813 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200814 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700815#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700818 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800819 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100820
Roland McGrath59e52132008-04-19 19:10:57 -0700821 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
822 clear_thread_flag(TIF_NOTIFY_RESUME);
823 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100824 if (current->replacement_session_keyring)
825 key_replace_session_keyring();
Roland McGrath59e52132008-04-19 19:10:57 -0700826 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300827 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
828 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700829
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700830#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700832#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700834
835void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
836{
837 struct task_struct *me = current;
838
839 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800840 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700841 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800842 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700843 me->comm, me->pid, where, frame,
844 regs->ip, regs->sp, regs->orig_ax);
845 print_vma_addr(" in ", regs->ip);
846 printk(KERN_CONT "\n");
847 }
848
849 force_sig(SIGSEGV, me);
850}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800851
852#ifdef CONFIG_X86_X32_ABI
853static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
854 siginfo_t *info, compat_sigset_t *set,
855 struct pt_regs *regs)
856{
857 struct rt_sigframe_x32 __user *frame;
858 void __user *restorer;
859 int err = 0;
860 void __user *fpstate = NULL;
861
862 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
863
864 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
865 return -EFAULT;
866
867 if (ka->sa.sa_flags & SA_SIGINFO) {
868 if (copy_siginfo_to_user32(&frame->info, info))
869 return -EFAULT;
870 }
871
872 put_user_try {
873 /* Create the ucontext. */
874 if (cpu_has_xsave)
875 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
876 else
877 put_user_ex(0, &frame->uc.uc_flags);
878 put_user_ex(0, &frame->uc.uc_link);
879 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
880 put_user_ex(sas_ss_flags(regs->sp),
881 &frame->uc.uc_stack.ss_flags);
882 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
883 put_user_ex(0, &frame->uc.uc__pad0);
884 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
885 regs, set->sig[0]);
886 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
887
888 if (ka->sa.sa_flags & SA_RESTORER) {
889 restorer = ka->sa.sa_restorer;
890 } else {
891 /* could use a vstub here */
892 restorer = NULL;
893 err |= -EFAULT;
894 }
895 put_user_ex(restorer, &frame->pretcode);
896 } put_user_catch(err);
897
898 if (err)
899 return -EFAULT;
900
901 /* Set up registers for signal handler */
902 regs->sp = (unsigned long) frame;
903 regs->ip = (unsigned long) ka->sa.sa_handler;
904
905 /* We use the x32 calling convention here... */
906 regs->di = sig;
907 regs->si = (unsigned long) &frame->info;
908 regs->dx = (unsigned long) &frame->uc;
909
910 loadsegment(ds, __USER_DS);
911 loadsegment(es, __USER_DS);
912
913 regs->cs = __USER_CS;
914 regs->ss = __USER_DS;
915
916 return 0;
917}
918
919asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
920{
921 struct rt_sigframe_x32 __user *frame;
922 sigset_t set;
923 unsigned long ax;
924 struct pt_regs tregs;
925
926 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
927
928 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
929 goto badframe;
930 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
931 goto badframe;
932
933 sigdelsetmask(&set, ~_BLOCKABLE);
934 set_current_blocked(&set);
935
936 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
937 goto badframe;
938
939 tregs = *regs;
940 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
941 goto badframe;
942
943 return ax;
944
945badframe:
946 signal_fault(regs, frame, "x32 rt_sigreturn");
947 return 0;
948}
949#endif