]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 | 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
e5fa2d06 | 3 | * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs |
1da177e4 LT |
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 | |
e5fa2d06 | 7 | * 2000-2002 x86-64 support by Andi Kleen |
1da177e4 | 8 | */ |
c767a54b JP |
9 | |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
11 | ||
5c9b3a0c HS |
12 | #include <linux/sched.h> |
13 | #include <linux/mm.h> | |
14 | #include <linux/smp.h> | |
1da177e4 | 15 | #include <linux/kernel.h> |
1da177e4 LT |
16 | #include <linux/errno.h> |
17 | #include <linux/wait.h> | |
36a03308 | 18 | #include <linux/tracehook.h> |
5c9b3a0c HS |
19 | #include <linux/unistd.h> |
20 | #include <linux/stddef.h> | |
21 | #include <linux/personality.h> | |
22 | #include <linux/uaccess.h> | |
7c68af6e | 23 | #include <linux/user-return-notifier.h> |
0326f5a9 | 24 | #include <linux/uprobes.h> |
7e907f48 | 25 | |
1da177e4 LT |
26 | #include <asm/processor.h> |
27 | #include <asm/ucontext.h> | |
1da177e4 | 28 | #include <asm/i387.h> |
1361b83a | 29 | #include <asm/fpu-internal.h> |
6c3652ef | 30 | #include <asm/vdso.h> |
4efc0670 | 31 | #include <asm/mce.h> |
f28f0c23 | 32 | #include <asm/sighandling.h> |
5c9b3a0c HS |
33 | |
34 | #ifdef CONFIG_X86_64 | |
35 | #include <asm/proto.h> | |
36 | #include <asm/ia32_unistd.h> | |
c5a37394 | 37 | #include <asm/sys_ia32.h> |
5c9b3a0c HS |
38 | #endif /* CONFIG_X86_64 */ |
39 | ||
bb57925f | 40 | #include <asm/syscall.h> |
bbc1f698 | 41 | #include <asm/syscalls.h> |
7e907f48 | 42 | |
41af86fa | 43 | #include <asm/sigframe.h> |
1da177e4 | 44 | |
1a176803 HH |
45 | #ifdef CONFIG_X86_32 |
46 | # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF) | |
47 | #else | |
48 | # define FIX_EFLAGS __FIX_EFLAGS | |
49 | #endif | |
50 | ||
d9a89a26 TH |
51 | #define COPY(x) do { \ |
52 | get_user_ex(regs->x, &sc->x); \ | |
53 | } while (0) | |
2601657d | 54 | |
d9a89a26 TH |
55 | #define GET_SEG(seg) ({ \ |
56 | unsigned short tmp; \ | |
57 | get_user_ex(tmp, &sc->seg); \ | |
58 | tmp; \ | |
59 | }) | |
2601657d | 60 | |
d9a89a26 TH |
61 | #define COPY_SEG(seg) do { \ |
62 | regs->seg = GET_SEG(seg); \ | |
63 | } while (0) | |
2601657d | 64 | |
d9a89a26 TH |
65 | #define COPY_SEG_CPL3(seg) do { \ |
66 | regs->seg = GET_SEG(seg) | 3; \ | |
67 | } while (0) | |
2601657d | 68 | |
85139422 PA |
69 | int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, |
70 | unsigned long *pax) | |
2601657d HS |
71 | { |
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 | ||
98e3d45e HS |
79 | get_user_try { |
80 | ||
2601657d | 81 | #ifdef CONFIG_X86_32 |
d9a89a26 | 82 | set_user_gs(regs, GET_SEG(gs)); |
98e3d45e HS |
83 | COPY_SEG(fs); |
84 | COPY_SEG(es); | |
85 | COPY_SEG(ds); | |
2601657d HS |
86 | #endif /* CONFIG_X86_32 */ |
87 | ||
98e3d45e HS |
88 | COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx); |
89 | COPY(dx); COPY(cx); COPY(ip); | |
2601657d HS |
90 | |
91 | #ifdef CONFIG_X86_64 | |
98e3d45e HS |
92 | COPY(r8); |
93 | COPY(r9); | |
94 | COPY(r10); | |
95 | COPY(r11); | |
96 | COPY(r12); | |
97 | COPY(r13); | |
98 | COPY(r14); | |
99 | COPY(r15); | |
2601657d HS |
100 | #endif /* CONFIG_X86_64 */ |
101 | ||
102 | #ifdef CONFIG_X86_32 | |
98e3d45e HS |
103 | COPY_SEG_CPL3(cs); |
104 | COPY_SEG_CPL3(ss); | |
2601657d | 105 | #else /* !CONFIG_X86_32 */ |
98e3d45e HS |
106 | /* 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); | |
2601657d HS |
110 | #endif /* CONFIG_X86_32 */ |
111 | ||
98e3d45e HS |
112 | get_user_ex(tmpflags, &sc->flags); |
113 | regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS); | |
114 | regs->orig_ax = -1; /* disable syscall checks */ | |
115 | ||
116 | get_user_ex(buf, &sc->fpstate); | |
2601657d | 117 | |
98e3d45e HS |
118 | get_user_ex(*pax, &sc->ax); |
119 | } get_user_catch(err); | |
2601657d | 120 | |
5e88353d PA |
121 | err |= restore_i387_xstate(buf); |
122 | ||
2601657d HS |
123 | return err; |
124 | } | |
125 | ||
85139422 PA |
126 | int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate, |
127 | struct pt_regs *regs, unsigned long mask) | |
2601657d HS |
128 | { |
129 | int err = 0; | |
130 | ||
98e3d45e | 131 | put_user_try { |
2601657d | 132 | |
98e3d45e | 133 | #ifdef CONFIG_X86_32 |
d9a89a26 | 134 | put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs); |
98e3d45e HS |
135 | 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); | |
2601657d HS |
138 | #endif /* CONFIG_X86_32 */ |
139 | ||
98e3d45e HS |
140 | 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); | |
2601657d | 148 | #ifdef CONFIG_X86_64 |
98e3d45e HS |
149 | 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); | |
2601657d HS |
157 | #endif /* CONFIG_X86_64 */ |
158 | ||
51e7dc70 | 159 | put_user_ex(current->thread.trap_nr, &sc->trapno); |
98e3d45e HS |
160 | put_user_ex(current->thread.error_code, &sc->err); |
161 | put_user_ex(regs->ip, &sc->ip); | |
2601657d | 162 | #ifdef CONFIG_X86_32 |
98e3d45e HS |
163 | 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); | |
2601657d | 167 | #else /* !CONFIG_X86_32 */ |
98e3d45e HS |
168 | 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); | |
2601657d HS |
172 | #endif /* CONFIG_X86_32 */ |
173 | ||
98e3d45e | 174 | put_user_ex(fpstate, &sc->fpstate); |
2601657d | 175 | |
98e3d45e HS |
176 | /* non-iBCS2 extensions.. */ |
177 | put_user_ex(mask, &sc->oldmask); | |
178 | put_user_ex(current->thread.cr2, &sc->cr2); | |
179 | } put_user_catch(err); | |
2601657d HS |
180 | |
181 | return err; | |
182 | } | |
183 | ||
1da177e4 | 184 | /* |
bfeb91a9 | 185 | * Set up a signal frame. |
1da177e4 | 186 | */ |
1da177e4 LT |
187 | |
188 | /* | |
189 | * Determine which stack to use.. | |
190 | */ | |
1fae0279 HS |
191 | static 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 | ||
1da177e4 | 205 | static inline void __user * |
3c1c7f10 | 206 | get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, |
144b0712 | 207 | void __user **fpstate) |
1da177e4 | 208 | { |
1da177e4 | 209 | /* Default to using normal stack */ |
75779f05 | 210 | unsigned long sp = regs->sp; |
14fc9fbc | 211 | int onsigstack = on_sig_stack(sp); |
75779f05 HS |
212 | |
213 | #ifdef CONFIG_X86_64 | |
214 | /* redzone */ | |
215 | sp -= 128; | |
216 | #endif /* CONFIG_X86_64 */ | |
1da177e4 | 217 | |
14fc9fbc HS |
218 | if (!onsigstack) { |
219 | /* This is the X/Open sanctioned signal stack switching. */ | |
220 | if (ka->sa.sa_flags & SA_ONSTACK) { | |
0f8f3089 | 221 | if (current->sas_ss_size) |
14fc9fbc HS |
222 | sp = current->sas_ss_sp + current->sas_ss_size; |
223 | } else { | |
75779f05 | 224 | #ifdef CONFIG_X86_32 |
14fc9fbc HS |
225 | /* 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; | |
75779f05 | 230 | #endif /* CONFIG_X86_32 */ |
14fc9fbc | 231 | } |
1da177e4 LT |
232 | } |
233 | ||
3c1c7f10 | 234 | if (used_math()) { |
75779f05 | 235 | sp -= sig_xstate_size; |
25051702 HS |
236 | #ifdef CONFIG_X86_64 |
237 | sp = round_down(sp, 64); | |
238 | #endif /* CONFIG_X86_64 */ | |
239 | *fpstate = (void __user *)sp; | |
3c1c7f10 SS |
240 | } |
241 | ||
14fc9fbc HS |
242 | 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; | |
1da177e4 LT |
256 | } |
257 | ||
75779f05 HS |
258 | #ifdef CONFIG_X86_32 |
259 | static 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 | ||
269 | static 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 | }; | |
280 | ||
7e907f48 | 281 | static int |
1d13024e HS |
282 | __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set, |
283 | struct pt_regs *regs) | |
1da177e4 | 284 | { |
1da177e4 | 285 | struct sigframe __user *frame; |
7e907f48 | 286 | void __user *restorer; |
1da177e4 | 287 | int err = 0; |
ab513701 | 288 | void __user *fpstate = NULL; |
1da177e4 | 289 | |
3c1c7f10 | 290 | frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate); |
1da177e4 LT |
291 | |
292 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
3d0aedd9 | 293 | return -EFAULT; |
1da177e4 | 294 | |
2ba48e16 | 295 | if (__put_user(sig, &frame->sig)) |
3d0aedd9 | 296 | return -EFAULT; |
1da177e4 | 297 | |
2ba48e16 | 298 | if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0])) |
3d0aedd9 | 299 | return -EFAULT; |
1da177e4 LT |
300 | |
301 | if (_NSIG_WORDS > 1) { | |
2ba48e16 HS |
302 | if (__copy_to_user(&frame->extramask, &set->sig[1], |
303 | sizeof(frame->extramask))) | |
3d0aedd9 | 304 | return -EFAULT; |
1da177e4 LT |
305 | } |
306 | ||
1a3e4ca4 | 307 | if (current->mm->context.vdso) |
6c3652ef | 308 | restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn); |
9fbbd4dd | 309 | else |
ade1af77 | 310 | restorer = &frame->retcode; |
1da177e4 LT |
311 | 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); | |
7e907f48 | 316 | |
1da177e4 | 317 | /* |
7e907f48 | 318 | * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80 |
1da177e4 LT |
319 | * |
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 | */ | |
4a612048 | 324 | err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode); |
1da177e4 LT |
325 | |
326 | if (err) | |
3d0aedd9 | 327 | return -EFAULT; |
1da177e4 LT |
328 | |
329 | /* Set up registers for signal handler */ | |
7e907f48 IM |
330 | regs->sp = (unsigned long)frame; |
331 | regs->ip = (unsigned long)ka->sa.sa_handler; | |
332 | regs->ax = (unsigned long)sig; | |
92bc2056 HH |
333 | regs->dx = 0; |
334 | regs->cx = 0; | |
1da177e4 | 335 | |
65ea5b03 PA |
336 | regs->ds = __USER_DS; |
337 | regs->es = __USER_DS; | |
338 | regs->ss = __USER_DS; | |
339 | regs->cs = __USER_CS; | |
1da177e4 | 340 | |
283828f3 | 341 | return 0; |
1da177e4 LT |
342 | } |
343 | ||
1d13024e HS |
344 | static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
345 | sigset_t *set, struct pt_regs *regs) | |
1da177e4 | 346 | { |
1da177e4 | 347 | struct rt_sigframe __user *frame; |
7e907f48 | 348 | void __user *restorer; |
1da177e4 | 349 | int err = 0; |
ab513701 | 350 | void __user *fpstate = NULL; |
1da177e4 | 351 | |
3c1c7f10 | 352 | frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate); |
1da177e4 LT |
353 | |
354 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
3d0aedd9 | 355 | return -EFAULT; |
1da177e4 | 356 | |
98e3d45e HS |
357 | 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); | |
98e3d45e HS |
361 | |
362 | /* 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); | |
98e3d45e HS |
372 | |
373 | /* 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); | |
378 | ||
379 | /* | |
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); | |
1da177e4 | 388 | |
5e88353d PA |
389 | 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 | ||
1da177e4 | 394 | if (err) |
3d0aedd9 | 395 | return -EFAULT; |
1da177e4 LT |
396 | |
397 | /* Set up registers for signal handler */ | |
7e907f48 IM |
398 | regs->sp = (unsigned long)frame; |
399 | regs->ip = (unsigned long)ka->sa.sa_handler; | |
13ad7725 | 400 | regs->ax = (unsigned long)sig; |
7e907f48 IM |
401 | regs->dx = (unsigned long)&frame->info; |
402 | regs->cx = (unsigned long)&frame->uc; | |
1da177e4 | 403 | |
65ea5b03 PA |
404 | regs->ds = __USER_DS; |
405 | regs->es = __USER_DS; | |
406 | regs->ss = __USER_DS; | |
407 | regs->cs = __USER_CS; | |
1da177e4 | 408 | |
283828f3 | 409 | return 0; |
1da177e4 | 410 | } |
bfeb91a9 | 411 | #else /* !CONFIG_X86_32 */ |
bfeb91a9 HS |
412 | static 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 | ||
97286a2b | 420 | frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp); |
bfeb91a9 HS |
421 | |
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 | ||
98e3d45e HS |
430 | 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); | |
98e3d45e HS |
441 | |
442 | /* 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); | |
bfeb91a9 | 452 | |
5e88353d PA |
453 | 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 | ||
bfeb91a9 HS |
456 | 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 | ||
e5fa2d06 | 480 | #ifdef CONFIG_X86_32 |
bfeb91a9 HS |
481 | /* |
482 | * Atomically swap in the new signal mask, and wait for a signal. | |
483 | */ | |
484 | asmlinkage int | |
485 | sys_sigsuspend(int history0, int history1, old_sigset_t mask) | |
486 | { | |
3982294b | 487 | sigset_t blocked; |
3982294b | 488 | siginitset(&blocked, mask); |
68f3f16d | 489 | return sigsuspend(&blocked); |
bfeb91a9 HS |
490 | } |
491 | ||
492 | asmlinkage int | |
493 | sys_sigaction(int sig, const struct old_sigaction __user *act, | |
494 | struct old_sigaction __user *oact) | |
495 | { | |
496 | struct k_sigaction new_ka, old_ka; | |
98e3d45e | 497 | int ret = 0; |
bfeb91a9 HS |
498 | |
499 | if (act) { | |
500 | old_sigset_t mask; | |
501 | ||
98e3d45e | 502 | if (!access_ok(VERIFY_READ, act, sizeof(*act))) |
bfeb91a9 HS |
503 | return -EFAULT; |
504 | ||
98e3d45e HS |
505 | 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; | |
bfeb91a9 HS |
514 | 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) { | |
98e3d45e | 520 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact))) |
bfeb91a9 HS |
521 | return -EFAULT; |
522 | ||
98e3d45e HS |
523 | 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; | |
bfeb91a9 HS |
532 | } |
533 | ||
534 | return ret; | |
535 | } | |
e5fa2d06 | 536 | #endif /* CONFIG_X86_32 */ |
bfeb91a9 | 537 | |
052acad4 | 538 | long |
bfeb91a9 HS |
539 | sys_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 | } | |
bfeb91a9 HS |
544 | |
545 | /* | |
546 | * Do a signal return; undo the signal stack. | |
547 | */ | |
e5fa2d06 | 548 | #ifdef CONFIG_X86_32 |
b12bdaf1 | 549 | unsigned long sys_sigreturn(struct pt_regs *regs) |
bfeb91a9 HS |
550 | { |
551 | struct sigframe __user *frame; | |
bfeb91a9 HS |
552 | unsigned long ax; |
553 | sigset_t set; | |
554 | ||
bfeb91a9 HS |
555 | 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 | ||
3982294b | 564 | set_current_blocked(&set); |
bfeb91a9 HS |
565 | |
566 | if (restore_sigcontext(regs, &frame->sc, &ax)) | |
567 | goto badframe; | |
568 | return ax; | |
569 | ||
570 | badframe: | |
ae417bb4 | 571 | signal_fault(regs, frame, "sigreturn"); |
bfeb91a9 HS |
572 | |
573 | return 0; | |
574 | } | |
e5fa2d06 | 575 | #endif /* CONFIG_X86_32 */ |
bfeb91a9 | 576 | |
74452509 | 577 | long sys_rt_sigreturn(struct pt_regs *regs) |
bfeb91a9 HS |
578 | { |
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 | ||
e9bd3f0f | 589 | set_current_blocked(&set); |
bfeb91a9 HS |
590 | |
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 | ||
599 | badframe: | |
600 | signal_fault(regs, frame, "rt_sigreturn"); | |
601 | return 0; | |
602 | } | |
603 | ||
1da177e4 | 604 | /* |
7e907f48 IM |
605 | * OK, we're invoking a handler: |
606 | */ | |
8d8c13bd HS |
607 | static int signr_convert(int sig) |
608 | { | |
96bf84b7 | 609 | #ifdef CONFIG_X86_32 |
8d8c13bd HS |
610 | 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]; | |
96bf84b7 | 614 | #endif /* CONFIG_X86_32 */ |
8d8c13bd HS |
615 | return sig; |
616 | } | |
617 | ||
cabf5035 HS |
618 | #ifdef CONFIG_X86_32 |
619 | ||
455edbc4 | 620 | #define is_ia32 1 |
4694d239 HS |
621 | #define ia32_setup_frame __setup_frame |
622 | #define ia32_setup_rt_frame __setup_rt_frame | |
455edbc4 | 623 | |
cabf5035 HS |
624 | #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 | ||
c5a37394 PA |
632 | #ifdef CONFIG_X86_X32_ABI |
633 | #define is_x32 test_thread_flag(TIF_X32) | |
634 | ||
635 | static 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 | ||
f5223763 HS |
642 | int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
643 | sigset_t *set, struct pt_regs *regs); | |
644 | int ia32_setup_frame(int sig, struct k_sigaction *ka, | |
645 | sigset_t *set, struct pt_regs *regs); | |
646 | ||
cabf5035 HS |
647 | #endif /* CONFIG_X86_32 */ |
648 | ||
1d13024e HS |
649 | static int |
650 | setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, | |
9b429620 | 651 | struct pt_regs *regs) |
1d13024e | 652 | { |
8d8c13bd | 653 | int usig = signr_convert(sig); |
b7f9a11a | 654 | sigset_t *set = sigmask_to_save(); |
1d13024e HS |
655 | |
656 | /* Set up the stack frame */ | |
455edbc4 HS |
657 | if (is_ia32) { |
658 | if (ka->sa.sa_flags & SA_SIGINFO) | |
a610d6e6 | 659 | return ia32_setup_rt_frame(usig, ka, info, set, regs); |
455edbc4 | 660 | else |
a610d6e6 | 661 | return ia32_setup_frame(usig, ka, set, regs); |
c5a37394 PA |
662 | #ifdef CONFIG_X86_X32_ABI |
663 | } else if (is_x32) { | |
a610d6e6 | 664 | return x32_setup_rt_frame(usig, ka, info, |
c5a37394 PA |
665 | (compat_sigset_t *)set, regs); |
666 | #endif | |
667 | } else { | |
a610d6e6 | 668 | return __setup_rt_frame(sig, ka, info, set, regs); |
c5a37394 | 669 | } |
1d13024e HS |
670 | } |
671 | ||
a610d6e6 | 672 | static void |
1da177e4 | 673 | handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, |
9b429620 | 674 | struct pt_regs *regs) |
1da177e4 LT |
675 | { |
676 | /* Are we from a system call? */ | |
bb57925f | 677 | if (syscall_get_nr(current, regs) >= 0) { |
1da177e4 | 678 | /* If so, check system call restarting.. */ |
bb57925f | 679 | switch (syscall_get_error(current, regs)) { |
ac66f3fd HH |
680 | case -ERESTART_RESTARTBLOCK: |
681 | case -ERESTARTNOHAND: | |
682 | regs->ax = -EINTR; | |
683 | break; | |
684 | ||
685 | case -ERESTARTSYS: | |
686 | if (!(ka->sa.sa_flags & SA_RESTART)) { | |
65ea5b03 | 687 | regs->ax = -EINTR; |
1da177e4 | 688 | break; |
ac66f3fd HH |
689 | } |
690 | /* fallthrough */ | |
691 | case -ERESTARTNOINTR: | |
692 | regs->ax = regs->orig_ax; | |
693 | regs->ip -= 2; | |
694 | break; | |
1da177e4 LT |
695 | } |
696 | } | |
697 | ||
698 | /* | |
e1f28773 RM |
699 | * 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. | |
1da177e4 | 701 | */ |
65ea5b03 | 702 | if (unlikely(regs->flags & X86_EFLAGS_TF) && |
e1f28773 | 703 | likely(test_and_clear_thread_flag(TIF_FORCED_TF))) |
65ea5b03 | 704 | regs->flags &= ~X86_EFLAGS_TF; |
1da177e4 | 705 | |
a610d6e6 AV |
706 | if (setup_rt_frame(sig, ka, info, regs) < 0) { |
707 | force_sigsegv(sig, current); | |
708 | return; | |
709 | } | |
7c1def16 | 710 | |
8b9c5ff3 RM |
711 | /* |
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; | |
8b9c5ff3 | 723 | |
efee984c AV |
724 | signal_delivered(sig, info, ka, regs, |
725 | test_thread_flag(TIF_SINGLESTEP)); | |
1da177e4 LT |
726 | } |
727 | ||
57917752 | 728 | #ifdef CONFIG_X86_32 |
8fcd8e20 | 729 | #define NR_restart_syscall __NR_restart_syscall |
57917752 HS |
730 | #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 | ||
1da177e4 LT |
735 | /* |
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 | */ | |
75604d7f | 740 | static void do_signal(struct pt_regs *regs) |
1da177e4 | 741 | { |
ac66f3fd | 742 | struct k_sigaction ka; |
1da177e4 LT |
743 | siginfo_t info; |
744 | int signr; | |
1da177e4 | 745 | |
1da177e4 LT |
746 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
747 | if (signr > 0) { | |
7e907f48 | 748 | /* Whee! Actually deliver the signal. */ |
9b429620 | 749 | handle_signal(signr, &info, &ka, regs); |
283828f3 | 750 | return; |
1da177e4 LT |
751 | } |
752 | ||
1da177e4 | 753 | /* Did we come from a system call? */ |
bb57925f | 754 | if (syscall_get_nr(current, regs) >= 0) { |
1da177e4 | 755 | /* Restart the system call - no handlers present */ |
bb57925f | 756 | switch (syscall_get_error(current, regs)) { |
283828f3 DH |
757 | case -ERESTARTNOHAND: |
758 | case -ERESTARTSYS: | |
759 | case -ERESTARTNOINTR: | |
65ea5b03 PA |
760 | regs->ax = regs->orig_ax; |
761 | regs->ip -= 2; | |
283828f3 DH |
762 | break; |
763 | ||
764 | case -ERESTART_RESTARTBLOCK: | |
8fcd8e20 | 765 | regs->ax = NR_restart_syscall; |
65ea5b03 | 766 | regs->ip -= 2; |
283828f3 | 767 | break; |
1da177e4 LT |
768 | } |
769 | } | |
283828f3 | 770 | |
ac66f3fd HH |
771 | /* |
772 | * If there's no signal to deliver, we just put the saved sigmask | |
773 | * back. | |
774 | */ | |
51a7b448 | 775 | restore_saved_sigmask(); |
1da177e4 LT |
776 | } |
777 | ||
778 | /* | |
779 | * notification of userspace execution resumption | |
283828f3 | 780 | * - triggered by the TIF_WORK_MASK flags |
1da177e4 | 781 | */ |
7e907f48 IM |
782 | void |
783 | do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags) | |
1da177e4 | 784 | { |
c1ebf835 | 785 | #ifdef CONFIG_X86_MCE |
ee847c54 HS |
786 | /* notify userspace of pending MCEs */ |
787 | if (thread_info_flags & _TIF_MCE_NOTIFY) | |
9b1beaf2 | 788 | mce_notify_process(); |
ee847c54 HS |
789 | #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */ |
790 | ||
0326f5a9 SD |
791 | if (thread_info_flags & _TIF_UPROBE) { |
792 | clear_thread_flag(TIF_UPROBE); | |
793 | uprobe_notify_resume(regs); | |
794 | } | |
795 | ||
1da177e4 | 796 | /* deal with pending signal delivery */ |
5a8da0ea | 797 | if (thread_info_flags & _TIF_SIGPENDING) |
283828f3 | 798 | do_signal(regs); |
8f4d37ec | 799 | |
59e52130 RM |
800 | if (thread_info_flags & _TIF_NOTIFY_RESUME) { |
801 | clear_thread_flag(TIF_NOTIFY_RESUME); | |
802 | tracehook_notify_resume(regs); | |
803 | } | |
7c68af6e AK |
804 | if (thread_info_flags & _TIF_USER_RETURN_NOTIFY) |
805 | fire_user_return_notifiers(); | |
59e52130 | 806 | |
ee847c54 | 807 | #ifdef CONFIG_X86_32 |
1da177e4 | 808 | clear_thread_flag(TIF_IRET); |
ee847c54 | 809 | #endif /* CONFIG_X86_32 */ |
1da177e4 | 810 | } |
72fa50f4 HS |
811 | |
812 | void 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()) { | |
ae417bb4 | 817 | printk("%s" |
72fa50f4 | 818 | "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx", |
ae417bb4 | 819 | task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG, |
72fa50f4 HS |
820 | me->comm, me->pid, where, frame, |
821 | regs->ip, regs->sp, regs->orig_ax); | |
822 | print_vma_addr(" in ", regs->ip); | |
c767a54b | 823 | pr_cont("\n"); |
72fa50f4 HS |
824 | } |
825 | ||
826 | force_sig(SIGSEGV, me); | |
827 | } | |
c5a37394 PA |
828 | |
829 | #ifdef CONFIG_X86_X32_ABI | |
830 | static 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); | |
c5a37394 PA |
861 | |
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 | ||
5e88353d PA |
872 | 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 | ||
c5a37394 PA |
876 | 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 | ||
897 | asmlinkage 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 | ||
c5a37394 PA |
911 | 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 | ||
922 | badframe: | |
923 | signal_fault(regs, frame, "x32 rt_sigreturn"); | |
924 | return 0; | |
925 | } | |
926 | #endif |