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