blob: 932612887e92ebb241e2e6e0cdcadf9695960fa6 [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 */
Joe Perchesc767a542012-05-21 19:50:07 -07009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Ingo Molnar7e907f42008-03-06 10:33:08 +010012#include <linux/sched.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010013#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080014#include <linux/smp.h>
15#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080016#include <linux/errno.h>
17#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080018#include <linux/tracehook.h>
19#include <linux/unistd.h>
20#include <linux/stddef.h>
21#include <linux/personality.h>
22#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030023#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053024#include <linux/uprobes.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/processor.h>
27#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080029#include <asm/fpu-internal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010030#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020031#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080032#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080033
34#ifdef CONFIG_X86_64
35#include <asm/proto.h>
36#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080037#include <asm/sys_ia32.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080038#endif /* CONFIG_X86_64 */
39
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070040#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053041#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010042
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080043#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Harvey Harrison1a176802008-02-08 12:09:59 -080045#ifdef CONFIG_X86_32
46# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
47#else
48# define FIX_EFLAGS __FIX_EFLAGS
49#endif
50
Tejun Heod9a89a22009-02-09 22:17:40 +090051#define COPY(x) do { \
52 get_user_ex(regs->x, &sc->x); \
53} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080054
Tejun Heod9a89a22009-02-09 22:17:40 +090055#define GET_SEG(seg) ({ \
56 unsigned short tmp; \
57 get_user_ex(tmp, &sc->seg); \
58 tmp; \
59})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080060
Tejun Heod9a89a22009-02-09 22:17:40 +090061#define COPY_SEG(seg) do { \
62 regs->seg = GET_SEG(seg); \
63} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080064
Tejun Heod9a89a22009-02-09 22:17:40 +090065#define COPY_SEG_CPL3(seg) do { \
66 regs->seg = GET_SEG(seg) | 3; \
67} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080068
H. Peter Anvin85139422012-02-19 07:43:09 -080069int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
70 unsigned long *pax)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080071{
72 void __user *buf;
73 unsigned int tmpflags;
74 unsigned int err = 0;
75
76 /* Always make any pending restarted system calls return -EINTR */
77 current_thread_info()->restart_block.fn = do_no_restart_syscall;
78
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080079 get_user_try {
80
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080081#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090082 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080083 COPY_SEG(fs);
84 COPY_SEG(es);
85 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080086#endif /* CONFIG_X86_32 */
87
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080088 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
89 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080090
91#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080092 COPY(r8);
93 COPY(r9);
94 COPY(r10);
95 COPY(r11);
96 COPY(r12);
97 COPY(r13);
98 COPY(r14);
99 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800100#endif /* CONFIG_X86_64 */
101
102#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800103 COPY_SEG_CPL3(cs);
104 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800105#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800106 /* Kernel saves and restores only the CS segment register on signals,
107 * which is the bare minimum needed to allow mixed 32/64-bit code.
108 * App's signal handler can save/restore other segments if needed. */
109 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800110#endif /* CONFIG_X86_32 */
111
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800112 get_user_ex(tmpflags, &sc->flags);
113 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
114 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800115
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800116 get_user_ex(buf, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800117
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800118 get_user_ex(*pax, &sc->ax);
119 } get_user_catch(err);
120
H. Peter Anvin5e883532012-09-21 12:43:15 -0700121 err |= restore_i387_xstate(buf);
122
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800123 return err;
124}
125
H. Peter Anvin85139422012-02-19 07:43:09 -0800126int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
127 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800128{
129 int err = 0;
130
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800131 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800132
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800133#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900134 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800135 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
136 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
137 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800138#endif /* CONFIG_X86_32 */
139
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800140 put_user_ex(regs->di, &sc->di);
141 put_user_ex(regs->si, &sc->si);
142 put_user_ex(regs->bp, &sc->bp);
143 put_user_ex(regs->sp, &sc->sp);
144 put_user_ex(regs->bx, &sc->bx);
145 put_user_ex(regs->dx, &sc->dx);
146 put_user_ex(regs->cx, &sc->cx);
147 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800148#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800149 put_user_ex(regs->r8, &sc->r8);
150 put_user_ex(regs->r9, &sc->r9);
151 put_user_ex(regs->r10, &sc->r10);
152 put_user_ex(regs->r11, &sc->r11);
153 put_user_ex(regs->r12, &sc->r12);
154 put_user_ex(regs->r13, &sc->r13);
155 put_user_ex(regs->r14, &sc->r14);
156 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800157#endif /* CONFIG_X86_64 */
158
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530159 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800160 put_user_ex(current->thread.error_code, &sc->err);
161 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800163 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
164 put_user_ex(regs->flags, &sc->flags);
165 put_user_ex(regs->sp, &sc->sp_at_signal);
166 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800167#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800168 put_user_ex(regs->flags, &sc->flags);
169 put_user_ex(regs->cs, &sc->cs);
170 put_user_ex(0, &sc->gs);
171 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800172#endif /* CONFIG_X86_32 */
173
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800174 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800175
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800176 /* non-iBCS2 extensions.. */
177 put_user_ex(mask, &sc->oldmask);
178 put_user_ex(current->thread.cr2, &sc->cr2);
179 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800180
181 return err;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Set up a signal frame.
186 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800187
188/*
189 * Determine which stack to use..
190 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800191static unsigned long align_sigframe(unsigned long sp)
192{
193#ifdef CONFIG_X86_32
194 /*
195 * Align the stack pointer according to the i386 ABI,
196 * i.e. so that on function entry ((sp + 4) & 15) == 0.
197 */
198 sp = ((sp + 4) & -16ul) - 4;
199#else /* !CONFIG_X86_32 */
200 sp = round_down(sp, 16) - 8;
201#endif
202 return sp;
203}
204
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800205static inline void __user *
206get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
207 void __user **fpstate)
208{
209 /* Default to using normal stack */
210 unsigned long sp = regs->sp;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700211 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800212
213#ifdef CONFIG_X86_64
214 /* redzone */
215 sp -= 128;
216#endif /* CONFIG_X86_64 */
217
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700218 if (!onsigstack) {
219 /* This is the X/Open sanctioned signal stack switching. */
220 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700221 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700222 sp = current->sas_ss_sp + current->sas_ss_size;
223 } else {
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800224#ifdef CONFIG_X86_32
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700225 /* This is the legacy signal stack switching. */
226 if ((regs->ss & 0xffff) != __USER_DS &&
227 !(ka->sa.sa_flags & SA_RESTORER) &&
228 ka->sa.sa_restorer)
229 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800230#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700231 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800232 }
233
234 if (used_math()) {
235 sp -= sig_xstate_size;
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800236#ifdef CONFIG_X86_64
237 sp = round_down(sp, 64);
238#endif /* CONFIG_X86_64 */
239 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800240 }
241
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700242 sp = align_sigframe(sp - frame_size);
243
244 /*
245 * If we are on the alternate signal stack and would overflow it, don't.
246 * Return an always-bogus address instead so we will die with SIGSEGV.
247 */
248 if (onsigstack && !likely(on_sig_stack(sp)))
249 return (void __user *)-1L;
250
251 /* save i387 state */
252 if (used_math() && save_i387_xstate(*fpstate) < 0)
253 return (void __user *)-1L;
254
255 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800256}
257
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800258#ifdef CONFIG_X86_32
259static const struct {
260 u16 poplmovl;
261 u32 val;
262 u16 int80;
263} __attribute__((packed)) retcode = {
264 0xb858, /* popl %eax; movl $..., %eax */
265 __NR_sigreturn,
266 0x80cd, /* int $0x80 */
267};
268
269static const struct {
270 u8 movl;
271 u32 val;
272 u16 int80;
273 u8 pad;
274} __attribute__((packed)) rt_retcode = {
275 0xb8, /* movl $..., %eax */
276 __NR_rt_sigreturn,
277 0x80cd, /* int $0x80 */
278 0
279};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Ingo Molnar7e907f42008-03-06 10:33:08 +0100281static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700282__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
283 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100286 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700288 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700290 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700293 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700295 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700296 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700298 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700299 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700302 if (__copy_to_user(&frame->extramask, &set->sig[1],
303 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700304 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700307 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100308 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100309 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100310 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (ka->sa.sa_flags & SA_RESTORER)
312 restorer = ka->sa.sa_restorer;
313
314 /* Set up to return from userspace. */
315 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100318 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
320 * WE DO NOT USE IT ANY MORE! It's only left here for historical
321 * reasons and because gdb uses it as a signature to notice
322 * signal handler stack frames.
323 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800324 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700327 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100330 regs->sp = (unsigned long)frame;
331 regs->ip = (unsigned long)ka->sa.sa_handler;
332 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800333 regs->dx = 0;
334 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100336 regs->ds = __USER_DS;
337 regs->es = __USER_DS;
338 regs->ss = __USER_DS;
339 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
David Howells283828f2006-01-18 17:44:00 -0800341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700344static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
345 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100348 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700350 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700352 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700355 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800357 put_user_try {
358 put_user_ex(sig, &frame->sig);
359 put_user_ex(&frame->info, &frame->pinfo);
360 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800362 /* Create the ucontext. */
363 if (cpu_has_xsave)
364 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
365 else
366 put_user_ex(0, &frame->uc.uc_flags);
367 put_user_ex(0, &frame->uc.uc_link);
368 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
369 put_user_ex(sas_ss_flags(regs->sp),
370 &frame->uc.uc_stack.ss_flags);
371 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800373 /* Set up to return from userspace. */
374 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
375 if (ka->sa.sa_flags & SA_RESTORER)
376 restorer = ka->sa.sa_restorer;
377 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100378
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800379 /*
380 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
381 *
382 * WE DO NOT USE IT ANY MORE! It's only left here for historical
383 * reasons and because gdb uses it as a signature to notice
384 * signal handler stack frames.
385 */
386 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
387 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
H. Peter Anvin5e883532012-09-21 12:43:15 -0700389 err |= copy_siginfo_to_user(&frame->info, info);
390 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
391 regs, set->sig[0]);
392 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700395 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100398 regs->sp = (unsigned long)frame;
399 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700400 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100401 regs->dx = (unsigned long)&frame->info;
402 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100404 regs->ds = __USER_DS;
405 regs->es = __USER_DS;
406 regs->ss = __USER_DS;
407 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
David Howells283828f2006-01-18 17:44:00 -0800409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800411#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800412static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
413 sigset_t *set, struct pt_regs *regs)
414{
415 struct rt_sigframe __user *frame;
416 void __user *fp = NULL;
417 int err = 0;
418 struct task_struct *me = current;
419
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800420 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800421
422 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
423 return -EFAULT;
424
425 if (ka->sa.sa_flags & SA_SIGINFO) {
426 if (copy_siginfo_to_user(&frame->info, info))
427 return -EFAULT;
428 }
429
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800430 put_user_try {
431 /* Create the ucontext. */
432 if (cpu_has_xsave)
433 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
434 else
435 put_user_ex(0, &frame->uc.uc_flags);
436 put_user_ex(0, &frame->uc.uc_link);
437 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
438 put_user_ex(sas_ss_flags(regs->sp),
439 &frame->uc.uc_stack.ss_flags);
440 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800441
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800442 /* Set up to return from userspace. If provided, use a stub
443 already in userspace. */
444 /* x86-64 should always use SA_RESTORER. */
445 if (ka->sa.sa_flags & SA_RESTORER) {
446 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
447 } else {
448 /* could use a vstub here */
449 err |= -EFAULT;
450 }
451 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800452
H. Peter Anvin5e883532012-09-21 12:43:15 -0700453 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
454 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
455
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800456 if (err)
457 return -EFAULT;
458
459 /* Set up registers for signal handler */
460 regs->di = sig;
461 /* In case the signal handler was declared without prototypes */
462 regs->ax = 0;
463
464 /* This also works for non SA_SIGINFO handlers because they expect the
465 next argument after the signal number on the stack. */
466 regs->si = (unsigned long)&frame->info;
467 regs->dx = (unsigned long)&frame->uc;
468 regs->ip = (unsigned long) ka->sa.sa_handler;
469
470 regs->sp = (unsigned long)frame;
471
472 /* Set up the CS register to run signal handlers in 64-bit mode,
473 even if the handler happens to be interrupting 32-bit code. */
474 regs->cs = __USER_CS;
475
476 return 0;
477}
478#endif /* CONFIG_X86_32 */
479
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800480#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800481/*
482 * Atomically swap in the new signal mask, and wait for a signal.
483 */
484asmlinkage int
485sys_sigsuspend(int history0, int history1, old_sigset_t mask)
486{
Oleg Nesterov39822942011-07-10 21:27:27 +0200487 sigset_t blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200488 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -0400489 return sigsuspend(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800490}
491
492asmlinkage int
493sys_sigaction(int sig, const struct old_sigaction __user *act,
494 struct old_sigaction __user *oact)
495{
496 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800497 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800498
499 if (act) {
500 old_sigset_t mask;
501
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800502 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800503 return -EFAULT;
504
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800505 get_user_try {
506 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
507 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
508 get_user_ex(mask, &act->sa_mask);
509 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
510 } get_user_catch(ret);
511
512 if (ret)
513 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800514 siginitset(&new_ka.sa.sa_mask, mask);
515 }
516
517 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
518
519 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800520 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800521 return -EFAULT;
522
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800523 put_user_try {
524 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
525 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
526 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
527 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
528 } put_user_catch(ret);
529
530 if (ret)
531 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800532 }
533
534 return ret;
535}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800536#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800537
Brian Gerst052acad2009-12-09 19:01:54 -0500538long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800539sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
540 struct pt_regs *regs)
541{
542 return do_sigaltstack(uss, uoss, regs->sp);
543}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800544
545/*
546 * Do a signal return; undo the signal stack.
547 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800548#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500549unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800550{
551 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800552 unsigned long ax;
553 sigset_t set;
554
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800555 frame = (struct sigframe __user *)(regs->sp - 8);
556
557 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
558 goto badframe;
559 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
560 && __copy_from_user(&set.sig[1], &frame->extramask,
561 sizeof(frame->extramask))))
562 goto badframe;
563
Oleg Nesterov39822942011-07-10 21:27:27 +0200564 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800565
566 if (restore_sigcontext(regs, &frame->sc, &ax))
567 goto badframe;
568 return ax;
569
570badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800571 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800572
573 return 0;
574}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800575#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800576
H. Peter Anvin74452502009-02-11 16:31:40 -0800577long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800578{
579 struct rt_sigframe __user *frame;
580 unsigned long ax;
581 sigset_t set;
582
583 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
584 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
585 goto badframe;
586 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
587 goto badframe;
588
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200589 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800590
591 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
592 goto badframe;
593
594 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
595 goto badframe;
596
597 return ax;
598
599badframe:
600 signal_fault(regs, frame, "rt_sigreturn");
601 return 0;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100605 * OK, we're invoking a handler:
606 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700607static int signr_convert(int sig)
608{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700609#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700610 struct thread_info *info = current_thread_info();
611
612 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
613 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700614#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700615 return sig;
616}
617
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700618#ifdef CONFIG_X86_32
619
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700620#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700621#define ia32_setup_frame __setup_frame
622#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700623
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700624#else /* !CONFIG_X86_32 */
625
626#ifdef CONFIG_IA32_EMULATION
627#define is_ia32 test_thread_flag(TIF_IA32)
628#else /* !CONFIG_IA32_EMULATION */
629#define is_ia32 0
630#endif /* CONFIG_IA32_EMULATION */
631
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800632#ifdef CONFIG_X86_X32_ABI
633#define is_x32 test_thread_flag(TIF_X32)
634
635static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
636 siginfo_t *info, compat_sigset_t *set,
637 struct pt_regs *regs);
638#else /* !CONFIG_X86_X32_ABI */
639#define is_x32 0
640#endif /* CONFIG_X86_X32_ABI */
641
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800642int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
643 sigset_t *set, struct pt_regs *regs);
644int ia32_setup_frame(int sig, struct k_sigaction *ka,
645 sigset_t *set, struct pt_regs *regs);
646
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700647#endif /* CONFIG_X86_32 */
648
Roland McGrath7c1def12005-06-23 00:08:21 -0700649static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700650setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200651 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700652{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700653 int usig = signr_convert(sig);
Al Virob7f9a112012-05-02 09:59:21 -0400654 sigset_t *set = sigmask_to_save();
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700655
656 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700657 if (is_ia32) {
658 if (ka->sa.sa_flags & SA_SIGINFO)
Al Viroa610d6e2012-05-21 23:42:15 -0400659 return ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700660 else
Al Viroa610d6e2012-05-21 23:42:15 -0400661 return ia32_setup_frame(usig, ka, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800662#ifdef CONFIG_X86_X32_ABI
663 } else if (is_x32) {
Al Viroa610d6e2012-05-21 23:42:15 -0400664 return x32_setup_rt_frame(usig, ka, info,
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800665 (compat_sigset_t *)set, regs);
666#endif
667 } else {
Al Viroa610d6e2012-05-21 23:42:15 -0400668 return __setup_rt_frame(sig, ka, info, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800669 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700670}
671
Al Viroa610d6e2012-05-21 23:42:15 -0400672static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200674 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700677 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700679 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800680 case -ERESTART_RESTARTBLOCK:
681 case -ERESTARTNOHAND:
682 regs->ax = -EINTR;
683 break;
684
685 case -ERESTARTSYS:
686 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100687 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800689 }
690 /* fallthrough */
691 case -ERESTARTNOINTR:
692 regs->ax = regs->orig_ax;
693 regs->ip -= 2;
694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 }
697
698 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100699 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
700 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100702 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100703 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100704 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Al Viroa610d6e2012-05-21 23:42:15 -0400706 if (setup_rt_frame(sig, ka, info, regs) < 0) {
707 force_sigsegv(sig, current);
708 return;
709 }
Roland McGrath7c1def12005-06-23 00:08:21 -0700710
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700711 /*
712 * Clear the direction flag as per the ABI for function entry.
713 */
714 regs->flags &= ~X86_EFLAGS_DF;
715
716 /*
717 * Clear TF when entering the signal handler, but
718 * notify any tracer that was single-stepping it.
719 * The tracer may want to single-step inside the
720 * handler too.
721 */
722 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700723
Al Viroefee9842012-04-28 02:04:15 -0400724 signal_delivered(sig, info, ka, regs,
725 test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700728#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700729#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700730#else /* !CONFIG_X86_32 */
731#define NR_restart_syscall \
732 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
733#endif /* CONFIG_X86_32 */
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Note that 'init' is a special process: it doesn't get signals it doesn't
737 * want to handle. Thus you cannot kill init even with a SIGKILL even by
738 * mistake.
739 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100740static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800742 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 siginfo_t info;
744 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
747 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100748 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200749 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800750 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700754 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700756 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800757 case -ERESTARTNOHAND:
758 case -ERESTARTSYS:
759 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100760 regs->ax = regs->orig_ax;
761 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800762 break;
763
764 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700765 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100766 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 }
David Howells283828f2006-01-18 17:44:00 -0800770
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800771 /*
772 * If there's no signal to deliver, we just put the saved sigmask
773 * back.
774 */
Al Viro51a7b442012-05-21 23:33:55 -0400775 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778/*
779 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800780 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100782void
783do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Andi Kleenc1ebf832009-07-09 00:31:41 +0200785#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700786 /* notify userspace of pending MCEs */
787 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200788 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700789#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
790
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530791 if (thread_info_flags & _TIF_UPROBE) {
792 clear_thread_flag(TIF_UPROBE);
793 uprobe_notify_resume(regs);
794 }
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700797 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800798 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100799
Roland McGrath59e52132008-04-19 19:10:57 -0700800 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
801 clear_thread_flag(TIF_NOTIFY_RESUME);
802 tracehook_notify_resume(regs);
803 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300804 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
805 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700806
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700807#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700809#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700811
812void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
813{
814 struct task_struct *me = current;
815
816 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800817 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700818 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800819 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700820 me->comm, me->pid, where, frame,
821 regs->ip, regs->sp, regs->orig_ax);
822 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700823 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700824 }
825
826 force_sig(SIGSEGV, me);
827}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800828
829#ifdef CONFIG_X86_X32_ABI
830static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
831 siginfo_t *info, compat_sigset_t *set,
832 struct pt_regs *regs)
833{
834 struct rt_sigframe_x32 __user *frame;
835 void __user *restorer;
836 int err = 0;
837 void __user *fpstate = NULL;
838
839 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
840
841 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
842 return -EFAULT;
843
844 if (ka->sa.sa_flags & SA_SIGINFO) {
845 if (copy_siginfo_to_user32(&frame->info, info))
846 return -EFAULT;
847 }
848
849 put_user_try {
850 /* Create the ucontext. */
851 if (cpu_has_xsave)
852 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
853 else
854 put_user_ex(0, &frame->uc.uc_flags);
855 put_user_ex(0, &frame->uc.uc_link);
856 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
857 put_user_ex(sas_ss_flags(regs->sp),
858 &frame->uc.uc_stack.ss_flags);
859 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
860 put_user_ex(0, &frame->uc.uc__pad0);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800861
862 if (ka->sa.sa_flags & SA_RESTORER) {
863 restorer = ka->sa.sa_restorer;
864 } else {
865 /* could use a vstub here */
866 restorer = NULL;
867 err |= -EFAULT;
868 }
869 put_user_ex(restorer, &frame->pretcode);
870 } put_user_catch(err);
871
H. Peter Anvin5e883532012-09-21 12:43:15 -0700872 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
873 regs, set->sig[0]);
874 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
875
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800876 if (err)
877 return -EFAULT;
878
879 /* Set up registers for signal handler */
880 regs->sp = (unsigned long) frame;
881 regs->ip = (unsigned long) ka->sa.sa_handler;
882
883 /* We use the x32 calling convention here... */
884 regs->di = sig;
885 regs->si = (unsigned long) &frame->info;
886 regs->dx = (unsigned long) &frame->uc;
887
888 loadsegment(ds, __USER_DS);
889 loadsegment(es, __USER_DS);
890
891 regs->cs = __USER_CS;
892 regs->ss = __USER_DS;
893
894 return 0;
895}
896
897asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
898{
899 struct rt_sigframe_x32 __user *frame;
900 sigset_t set;
901 unsigned long ax;
902 struct pt_regs tregs;
903
904 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
905
906 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
907 goto badframe;
908 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
909 goto badframe;
910
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800911 set_current_blocked(&set);
912
913 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
914 goto badframe;
915
916 tregs = *regs;
917 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
918 goto badframe;
919
920 return ax;
921
922badframe:
923 signal_fault(regs, frame, "x32 rt_sigreturn");
924 return 0;
925}
926#endif