diff options
Diffstat (limited to 'arch/x86/um/signal_64.c')
-rw-r--r-- | arch/x86/um/signal_64.c | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/arch/x86/um/signal_64.c b/arch/x86/um/signal_64.c new file mode 100644 index 000000000000..255b2ca0ce67 --- /dev/null +++ b/arch/x86/um/signal_64.c | |||
@@ -0,0 +1,255 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2003 PathScale, Inc. | ||
3 | * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | ||
4 | * Licensed under the GPL | ||
5 | */ | ||
6 | |||
7 | #include <linux/personality.h> | ||
8 | #include <linux/ptrace.h> | ||
9 | #include <linux/kernel.h> | ||
10 | #include <asm/unistd.h> | ||
11 | #include <asm/uaccess.h> | ||
12 | #include <asm/ucontext.h> | ||
13 | #include "frame_kern.h" | ||
14 | #include "skas.h" | ||
15 | |||
16 | static int copy_sc_from_user(struct pt_regs *regs, | ||
17 | struct sigcontext __user *from) | ||
18 | { | ||
19 | struct sigcontext sc; | ||
20 | struct user_i387_struct fp; | ||
21 | void __user *buf; | ||
22 | int err; | ||
23 | |||
24 | err = copy_from_user(&sc, from, sizeof(sc)); | ||
25 | if (err) | ||
26 | return err; | ||
27 | |||
28 | #define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname | ||
29 | |||
30 | GETREG(R8, r8); | ||
31 | GETREG(R9, r9); | ||
32 | GETREG(R10, r10); | ||
33 | GETREG(R11, r11); | ||
34 | GETREG(R12, r12); | ||
35 | GETREG(R13, r13); | ||
36 | GETREG(R14, r14); | ||
37 | GETREG(R15, r15); | ||
38 | GETREG(RDI, di); | ||
39 | GETREG(RSI, si); | ||
40 | GETREG(RBP, bp); | ||
41 | GETREG(RBX, bx); | ||
42 | GETREG(RDX, dx); | ||
43 | GETREG(RAX, ax); | ||
44 | GETREG(RCX, cx); | ||
45 | GETREG(SP, sp); | ||
46 | GETREG(IP, ip); | ||
47 | GETREG(EFLAGS, flags); | ||
48 | GETREG(CS, cs); | ||
49 | #undef GETREG | ||
50 | |||
51 | buf = sc.fpstate; | ||
52 | |||
53 | err = copy_from_user(&fp, buf, sizeof(struct user_i387_struct)); | ||
54 | if (err) | ||
55 | return 1; | ||
56 | |||
57 | err = restore_fp_registers(userspace_pid[current_thread_info()->cpu], | ||
58 | (unsigned long *) &fp); | ||
59 | if (err < 0) { | ||
60 | printk(KERN_ERR "copy_sc_from_user - " | ||
61 | "restore_fp_registers failed, errno = %d\n", | ||
62 | -err); | ||
63 | return 1; | ||
64 | } | ||
65 | |||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | static int copy_sc_to_user(struct sigcontext __user *to, | ||
70 | struct _fpstate __user *to_fp, struct pt_regs *regs, | ||
71 | unsigned long mask, unsigned long sp) | ||
72 | { | ||
73 | struct faultinfo * fi = ¤t->thread.arch.faultinfo; | ||
74 | struct sigcontext sc; | ||
75 | struct user_i387_struct fp; | ||
76 | int err = 0; | ||
77 | memset(&sc, 0, sizeof(struct sigcontext)); | ||
78 | |||
79 | #define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno] | ||
80 | |||
81 | PUTREG(RDI, di); | ||
82 | PUTREG(RSI, si); | ||
83 | PUTREG(RBP, bp); | ||
84 | /* | ||
85 | * Must use original RSP, which is passed in, rather than what's in | ||
86 | * signal frame. | ||
87 | */ | ||
88 | sc.sp = sp; | ||
89 | PUTREG(RBX, bx); | ||
90 | PUTREG(RDX, dx); | ||
91 | PUTREG(RCX, cx); | ||
92 | PUTREG(RAX, ax); | ||
93 | PUTREG(R8, r8); | ||
94 | PUTREG(R9, r9); | ||
95 | PUTREG(R10, r10); | ||
96 | PUTREG(R11, r11); | ||
97 | PUTREG(R12, r12); | ||
98 | PUTREG(R13, r13); | ||
99 | PUTREG(R14, r14); | ||
100 | PUTREG(R15, r15); | ||
101 | PUTREG(CS, cs); /* XXX x86_64 doesn't do this */ | ||
102 | |||
103 | sc.cr2 = fi->cr2; | ||
104 | sc.err = fi->error_code; | ||
105 | sc.trapno = fi->trap_no; | ||
106 | |||
107 | PUTREG(IP, ip); | ||
108 | PUTREG(EFLAGS, flags); | ||
109 | #undef PUTREG | ||
110 | |||
111 | sc.oldmask = mask; | ||
112 | |||
113 | err = copy_to_user(to, &sc, sizeof(struct sigcontext)); | ||
114 | if (err) | ||
115 | return 1; | ||
116 | |||
117 | err = save_fp_registers(userspace_pid[current_thread_info()->cpu], | ||
118 | (unsigned long *) &fp); | ||
119 | if (err < 0) { | ||
120 | printk(KERN_ERR "copy_sc_from_user - restore_fp_registers " | ||
121 | "failed, errno = %d\n", -err); | ||
122 | return 1; | ||
123 | } | ||
124 | |||
125 | if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct))) | ||
126 | return 1; | ||
127 | |||
128 | return err; | ||
129 | } | ||
130 | |||
131 | struct rt_sigframe | ||
132 | { | ||
133 | char __user *pretcode; | ||
134 | struct ucontext uc; | ||
135 | struct siginfo info; | ||
136 | struct _fpstate fpstate; | ||
137 | }; | ||
138 | |||
139 | int setup_signal_stack_si(unsigned long stack_top, int sig, | ||
140 | struct k_sigaction *ka, struct pt_regs * regs, | ||
141 | siginfo_t *info, sigset_t *set) | ||
142 | { | ||
143 | struct rt_sigframe __user *frame; | ||
144 | unsigned long save_sp = PT_REGS_RSP(regs); | ||
145 | int err = 0; | ||
146 | struct task_struct *me = current; | ||
147 | |||
148 | frame = (struct rt_sigframe __user *) | ||
149 | round_down(stack_top - sizeof(struct rt_sigframe), 16); | ||
150 | /* Subtract 128 for a red zone and 8 for proper alignment */ | ||
151 | frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8); | ||
152 | |||
153 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | ||
154 | goto out; | ||
155 | |||
156 | if (ka->sa.sa_flags & SA_SIGINFO) { | ||
157 | err |= copy_siginfo_to_user(&frame->info, info); | ||
158 | if (err) | ||
159 | goto out; | ||
160 | } | ||
161 | |||
162 | /* | ||
163 | * Update SP now because the page fault handler refuses to extend | ||
164 | * the stack if the faulting address is too far below the current | ||
165 | * SP, which frame now certainly is. If there's an error, the original | ||
166 | * value is restored on the way out. | ||
167 | * When writing the sigcontext to the stack, we have to write the | ||
168 | * original value, so that's passed to copy_sc_to_user, which does | ||
169 | * the right thing with it. | ||
170 | */ | ||
171 | PT_REGS_RSP(regs) = (unsigned long) frame; | ||
172 | |||
173 | /* Create the ucontext. */ | ||
174 | err |= __put_user(0, &frame->uc.uc_flags); | ||
175 | err |= __put_user(0, &frame->uc.uc_link); | ||
176 | err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp); | ||
177 | err |= __put_user(sas_ss_flags(save_sp), | ||
178 | &frame->uc.uc_stack.ss_flags); | ||
179 | err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size); | ||
180 | err |= copy_sc_to_user(&frame->uc.uc_mcontext, &frame->fpstate, regs, | ||
181 | set->sig[0], save_sp); | ||
182 | err |= __put_user(&frame->fpstate, &frame->uc.uc_mcontext.fpstate); | ||
183 | if (sizeof(*set) == 16) { | ||
184 | __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]); | ||
185 | __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]); | ||
186 | } | ||
187 | else | ||
188 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, | ||
189 | sizeof(*set)); | ||
190 | |||
191 | /* | ||
192 | * Set up to return from userspace. If provided, use a stub | ||
193 | * already in userspace. | ||
194 | */ | ||
195 | /* x86-64 should always use SA_RESTORER. */ | ||
196 | if (ka->sa.sa_flags & SA_RESTORER) | ||
197 | err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); | ||
198 | else | ||
199 | /* could use a vstub here */ | ||
200 | goto restore_sp; | ||
201 | |||
202 | if (err) | ||
203 | goto restore_sp; | ||
204 | |||
205 | /* Set up registers for signal handler */ | ||
206 | { | ||
207 | struct exec_domain *ed = current_thread_info()->exec_domain; | ||
208 | if (unlikely(ed && ed->signal_invmap && sig < 32)) | ||
209 | sig = ed->signal_invmap[sig]; | ||
210 | } | ||
211 | |||
212 | PT_REGS_RDI(regs) = sig; | ||
213 | /* In case the signal handler was declared without prototypes */ | ||
214 | PT_REGS_RAX(regs) = 0; | ||
215 | |||
216 | /* | ||
217 | * This also works for non SA_SIGINFO handlers because they expect the | ||
218 | * next argument after the signal number on the stack. | ||
219 | */ | ||
220 | PT_REGS_RSI(regs) = (unsigned long) &frame->info; | ||
221 | PT_REGS_RDX(regs) = (unsigned long) &frame->uc; | ||
222 | PT_REGS_RIP(regs) = (unsigned long) ka->sa.sa_handler; | ||
223 | out: | ||
224 | return err; | ||
225 | |||
226 | restore_sp: | ||
227 | PT_REGS_RSP(regs) = save_sp; | ||
228 | return err; | ||
229 | } | ||
230 | |||
231 | long sys_rt_sigreturn(struct pt_regs *regs) | ||
232 | { | ||
233 | unsigned long sp = PT_REGS_SP(¤t->thread.regs); | ||
234 | struct rt_sigframe __user *frame = | ||
235 | (struct rt_sigframe __user *)(sp - 8); | ||
236 | struct ucontext __user *uc = &frame->uc; | ||
237 | sigset_t set; | ||
238 | |||
239 | if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set))) | ||
240 | goto segfault; | ||
241 | |||
242 | sigdelsetmask(&set, ~_BLOCKABLE); | ||
243 | set_current_blocked(&set); | ||
244 | |||
245 | if (copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext)) | ||
246 | goto segfault; | ||
247 | |||
248 | /* Avoid ERESTART handling */ | ||
249 | PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1; | ||
250 | return PT_REGS_SYSCALL_RET(¤t->thread.regs); | ||
251 | |||
252 | segfault: | ||
253 | force_sig(SIGSEGV, current); | ||
254 | return 0; | ||
255 | } | ||