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