blob: af2f017617b471da64942c28391fcd34eebe84f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 PathScale, Inc.
3 * Licensed under the GPL
4 */
5
6#include "linux/stddef.h"
7#include "linux/errno.h"
8#include "linux/personality.h"
9#include "linux/ptrace.h"
10#include "asm/current.h"
11#include "asm/uaccess.h"
12#include "asm/sigcontext.h"
13#include "asm/ptrace.h"
14#include "asm/arch/ucontext.h"
15#include "choose-mode.h"
16#include "sysdep/ptrace.h"
17#include "frame_kern.h"
18
19#ifdef CONFIG_MODE_SKAS
20
21#include "skas.h"
22
23static int copy_sc_from_user_skas(struct pt_regs *regs,
Al Virob8719c32006-06-04 02:51:48 -070024 struct sigcontext __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 int err = 0;
27
28#define GETREG(regs, regno, sc, regname) \
29 __get_user((regs)->regs.skas.regs[(regno) / sizeof(unsigned long)], \
30 &(sc)->regname)
31
32 err |= GETREG(regs, R8, from, r8);
33 err |= GETREG(regs, R9, from, r9);
34 err |= GETREG(regs, R10, from, r10);
35 err |= GETREG(regs, R11, from, r11);
36 err |= GETREG(regs, R12, from, r12);
37 err |= GETREG(regs, R13, from, r13);
38 err |= GETREG(regs, R14, from, r14);
39 err |= GETREG(regs, R15, from, r15);
40 err |= GETREG(regs, RDI, from, rdi);
41 err |= GETREG(regs, RSI, from, rsi);
42 err |= GETREG(regs, RBP, from, rbp);
43 err |= GETREG(regs, RBX, from, rbx);
44 err |= GETREG(regs, RDX, from, rdx);
45 err |= GETREG(regs, RAX, from, rax);
46 err |= GETREG(regs, RCX, from, rcx);
47 err |= GETREG(regs, RSP, from, rsp);
48 err |= GETREG(regs, RIP, from, rip);
49 err |= GETREG(regs, EFLAGS, from, eflags);
50 err |= GETREG(regs, CS, from, cs);
51
52#undef GETREG
53
54 return(err);
55}
56
Al Virob8719c32006-06-04 02:51:48 -070057int copy_sc_to_user_skas(struct sigcontext __user *to,
58 struct _fpstate __user *to_fp,
Jeff Dike98c18232006-03-27 01:14:38 -080059 struct pt_regs *regs, unsigned long mask,
60 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Bodo Stroesserc5784552005-05-05 16:15:31 -070062 struct faultinfo * fi = &current->thread.arch.faultinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int err = 0;
64
65 err |= __put_user(0, &to->gs);
66 err |= __put_user(0, &to->fs);
67
68#define PUTREG(regs, regno, sc, regname) \
69 __put_user((regs)->regs.skas.regs[(regno) / sizeof(unsigned long)], \
70 &(sc)->regname)
71
72 err |= PUTREG(regs, RDI, to, rdi);
73 err |= PUTREG(regs, RSI, to, rsi);
74 err |= PUTREG(regs, RBP, to, rbp);
Jeff Dike98c18232006-03-27 01:14:38 -080075 /* Must use orignal RSP, which is passed in, rather than what's in
76 * the pt_regs, because that's already been updated to point at the
77 * signal frame.
78 */
79 err |= __put_user(sp, &to->rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 err |= PUTREG(regs, RBX, to, rbx);
81 err |= PUTREG(regs, RDX, to, rdx);
82 err |= PUTREG(regs, RCX, to, rcx);
83 err |= PUTREG(regs, RAX, to, rax);
84 err |= PUTREG(regs, R8, to, r8);
85 err |= PUTREG(regs, R9, to, r9);
86 err |= PUTREG(regs, R10, to, r10);
87 err |= PUTREG(regs, R11, to, r11);
88 err |= PUTREG(regs, R12, to, r12);
89 err |= PUTREG(regs, R13, to, r13);
90 err |= PUTREG(regs, R14, to, r14);
91 err |= PUTREG(regs, R15, to, r15);
92 err |= PUTREG(regs, CS, to, cs); /* XXX x86_64 doesn't do this */
Bodo Stroesserc5784552005-05-05 16:15:31 -070093
94 err |= __put_user(fi->cr2, &to->cr2);
95 err |= __put_user(fi->error_code, &to->err);
96 err |= __put_user(fi->trap_no, &to->trapno);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 err |= PUTREG(regs, RIP, to, rip);
99 err |= PUTREG(regs, EFLAGS, to, eflags);
100#undef PUTREG
101
102 err |= __put_user(mask, &to->oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 return(err);
105}
106
107#endif
108
109#ifdef CONFIG_MODE_TT
Al Virob8719c32006-06-04 02:51:48 -0700110int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext __user *from,
Jeff Dike98c18232006-03-27 01:14:38 -0800111 int fpsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Al Virob8719c32006-06-04 02:51:48 -0700113 struct _fpstate *to_fp;
114 struct _fpstate __user *from_fp;
Al Viroe54a5df2005-09-03 15:57:27 -0700115 unsigned long sigs;
116 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Al Viroe54a5df2005-09-03 15:57:27 -0700118 to_fp = to->fpstate;
119 sigs = to->oldmask;
120 err = copy_from_user(to, from, sizeof(*to));
121 from_fp = to->fpstate;
122 to->fpstate = to_fp;
123 to->oldmask = sigs;
124 if(to_fp != NULL)
125 err |= copy_from_user(to_fp, from_fp, fpsize);
126 return(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Al Virob8719c32006-06-04 02:51:48 -0700129int copy_sc_to_user_tt(struct sigcontext __user *to, struct _fpstate __user *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800130 struct sigcontext *from, int fpsize, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Al Virob8719c32006-06-04 02:51:48 -0700132 struct _fpstate __user *to_fp;
133 struct _fpstate *from_fp;
Al Viroe54a5df2005-09-03 15:57:27 -0700134 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Al Virob8719c32006-06-04 02:51:48 -0700136 to_fp = (fp ? fp : (struct _fpstate __user *) (to + 1));
Al Viroe54a5df2005-09-03 15:57:27 -0700137 from_fp = from->fpstate;
138 err = copy_to_user(to, from, sizeof(*to));
Jeff Dike98c18232006-03-27 01:14:38 -0800139 /* The SP in the sigcontext is the updated one for the signal
140 * delivery. The sp passed in is the original, and this needs
141 * to be restored, so we stick it in separately.
142 */
Paolo 'Blaisorblade' Giarrussof53389d2006-04-10 22:53:34 -0700143 err |= copy_to_user(&SC_SP(to), &sp, sizeof(sp));
Jeff Dike98c18232006-03-27 01:14:38 -0800144
Al Viroe54a5df2005-09-03 15:57:27 -0700145 if(from_fp != NULL){
146 err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate));
147 err |= copy_to_user(to_fp, from_fp, fpsize);
148 }
Jeff Dike98c18232006-03-27 01:14:38 -0800149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152#endif
153
154static int copy_sc_from_user(struct pt_regs *to, void __user *from)
155{
156 int ret;
157
158 ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from,
159 sizeof(struct _fpstate)),
160 copy_sc_from_user_skas(to, from));
161 return(ret);
162}
163
Al Virob8719c32006-06-04 02:51:48 -0700164static int copy_sc_to_user(struct sigcontext __user *to,
165 struct _fpstate __user *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800166 struct pt_regs *from, unsigned long mask,
167 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 return(CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
Jeff Dike98c18232006-03-27 01:14:38 -0800170 sizeof(*fp), sp),
171 copy_sc_to_user_skas(to, fp, from, mask, sp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174struct rt_sigframe
175{
Al Virob8719c32006-06-04 02:51:48 -0700176 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct ucontext uc;
178 struct siginfo info;
179};
180
181#define round_down(m, n) (((m) / (n)) * (n))
182
183int setup_signal_stack_si(unsigned long stack_top, int sig,
184 struct k_sigaction *ka, struct pt_regs * regs,
185 siginfo_t *info, sigset_t *set)
186{
187 struct rt_sigframe __user *frame;
188 struct _fpstate __user *fp = NULL;
Jeff Dike98c18232006-03-27 01:14:38 -0800189 unsigned long save_sp = PT_REGS_RSP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int err = 0;
191 struct task_struct *me = current;
192
193 frame = (struct rt_sigframe __user *)
Jeff Dike38966252007-01-30 14:36:17 -0800194 round_down(stack_top - sizeof(struct rt_sigframe), 16);
195 /* Subtract 128 for a red zone and 8 for proper alignment */
196 frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
199 goto out;
200
201#if 0 /* XXX */
202 if (save_i387(fp) < 0)
203 err |= -1;
204#endif
205 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
206 goto out;
207
208 if (ka->sa.sa_flags & SA_SIGINFO) {
209 err |= copy_siginfo_to_user(&frame->info, info);
210 if (err)
211 goto out;
212 }
213
Jeff Dike98c18232006-03-27 01:14:38 -0800214 /* Update SP now because the page fault handler refuses to extend
215 * the stack if the faulting address is too far below the current
216 * SP, which frame now certainly is. If there's an error, the original
217 * value is restored on the way out.
218 * When writing the sigcontext to the stack, we have to write the
219 * original value, so that's passed to copy_sc_to_user, which does
220 * the right thing with it.
221 */
222 PT_REGS_RSP(regs) = (unsigned long) frame;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* Create the ucontext. */
225 err |= __put_user(0, &frame->uc.uc_flags);
226 err |= __put_user(0, &frame->uc.uc_link);
227 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
Jeff Dike98c18232006-03-27 01:14:38 -0800228 err |= __put_user(sas_ss_flags(save_sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 &frame->uc.uc_stack.ss_flags);
230 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
Jeff Dike98c18232006-03-27 01:14:38 -0800231 err |= copy_sc_to_user(&frame->uc.uc_mcontext, fp, regs, set->sig[0],
232 save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
234 if (sizeof(*set) == 16) {
235 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
236 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
237 }
238 else
239 err |= __copy_to_user(&frame->uc.uc_sigmask, set,
240 sizeof(*set));
241
242 /* Set up to return from userspace. If provided, use a stub
243 already in userspace. */
244 /* x86-64 should always use SA_RESTORER. */
245 if (ka->sa.sa_flags & SA_RESTORER)
246 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
247 else
248 /* could use a vstub here */
Jeff Dike98c18232006-03-27 01:14:38 -0800249 goto restore_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (err)
Jeff Dike98c18232006-03-27 01:14:38 -0800252 goto restore_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Set up registers for signal handler */
255 {
256 struct exec_domain *ed = current_thread_info()->exec_domain;
257 if (unlikely(ed && ed->signal_invmap && sig < 32))
258 sig = ed->signal_invmap[sig];
259 }
260
261 PT_REGS_RDI(regs) = sig;
262 /* In case the signal handler was declared without prototypes */
263 PT_REGS_RAX(regs) = 0;
264
265 /* This also works for non SA_SIGINFO handlers because they expect the
266 next argument after the signal number on the stack. */
267 PT_REGS_RSI(regs) = (unsigned long) &frame->info;
268 PT_REGS_RDX(regs) = (unsigned long) &frame->uc;
269 PT_REGS_RIP(regs) = (unsigned long) ka->sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 out:
Jeff Dike98c18232006-03-27 01:14:38 -0800271 return err;
272
273restore_sp:
274 PT_REGS_RSP(regs) = save_sp;
275 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278long sys_rt_sigreturn(struct pt_regs *regs)
279{
280 unsigned long sp = PT_REGS_SP(&current->thread.regs);
281 struct rt_sigframe __user *frame =
282 (struct rt_sigframe __user *)(sp - 8);
283 struct ucontext __user *uc = &frame->uc;
284 sigset_t set;
285
286 if(copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
287 goto segfault;
288
289 sigdelsetmask(&set, ~_BLOCKABLE);
290
291 spin_lock_irq(&current->sighand->siglock);
292 current->blocked = set;
293 recalc_sigpending();
294 spin_unlock_irq(&current->sighand->siglock);
295
296 if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
297 goto segfault;
298
299 /* Avoid ERESTART handling */
300 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
301 return(PT_REGS_SYSCALL_RET(&current->thread.regs));
302
303 segfault:
304 force_sig(SIGSEGV, current);
305 return 0;
306}
307/*
308 * Overrides for Emacs so that we follow Linus's tabbing style.
309 * Emacs will notice this stuff at the end of the file and automatically
310 * adjust the settings for this buffer only. This must remain at the end
311 * of the file.
312 * ---------------------------------------------------------------------------
313 * Local variables:
314 * c-file-style: "linux"
315 * End:
316 */