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