Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | 7796931 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 6 | #include <linux/ptrace.h> |
| 7 | #include <asm/unistd.h> |
| 8 | #include <asm/uaccess.h> |
| 9 | #include <asm/ucontext.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "frame_kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "skas.h" |
| 12 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 13 | void copy_sc(struct uml_pt_regs *regs, void *from) |
Jeff Dike | 377fad3 | 2007-05-06 14:51:25 -0700 | [diff] [blame] | 14 | { |
| 15 | struct sigcontext *sc = from; |
| 16 | |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 17 | REGS_GS(regs->gp) = sc->gs; |
| 18 | REGS_FS(regs->gp) = sc->fs; |
| 19 | REGS_ES(regs->gp) = sc->es; |
| 20 | REGS_DS(regs->gp) = sc->ds; |
Jeff Dike | 7796931 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 21 | REGS_EDI(regs->gp) = sc->di; |
| 22 | REGS_ESI(regs->gp) = sc->si; |
| 23 | REGS_EBP(regs->gp) = sc->bp; |
| 24 | REGS_SP(regs->gp) = sc->sp; |
| 25 | REGS_EBX(regs->gp) = sc->bx; |
| 26 | REGS_EDX(regs->gp) = sc->dx; |
| 27 | REGS_ECX(regs->gp) = sc->cx; |
| 28 | REGS_EAX(regs->gp) = sc->ax; |
| 29 | REGS_IP(regs->gp) = sc->ip; |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 30 | REGS_CS(regs->gp) = sc->cs; |
Jeff Dike | 7796931 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 31 | REGS_EFLAGS(regs->gp) = sc->flags; |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 32 | REGS_SS(regs->gp) = sc->ss; |
Jeff Dike | 377fad3 | 2007-05-06 14:51:25 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 35 | /* |
| 36 | * FPU tag word conversions. |
| 37 | */ |
| 38 | |
| 39 | static inline unsigned short twd_i387_to_fxsr(unsigned short twd) |
| 40 | { |
| 41 | unsigned int tmp; /* to avoid 16 bit prefixes in the code */ |
| 42 | |
| 43 | /* Transform each pair of bits into 01 (valid) or 00 (empty) */ |
| 44 | tmp = ~twd; |
| 45 | tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */ |
| 46 | /* and move the valid bits to the lower byte. */ |
| 47 | tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */ |
| 48 | tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */ |
| 49 | tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */ |
| 50 | return tmp; |
| 51 | } |
| 52 | |
| 53 | static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct *fxsave) |
| 54 | { |
| 55 | struct _fpxreg *st = NULL; |
| 56 | unsigned long twd = (unsigned long) fxsave->twd; |
| 57 | unsigned long tag; |
| 58 | unsigned long ret = 0xffff0000; |
| 59 | int i; |
| 60 | |
| 61 | #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16); |
| 62 | |
| 63 | for (i = 0; i < 8; i++) { |
| 64 | if (twd & 0x1) { |
| 65 | st = (struct _fpxreg *) FPREG_ADDR(fxsave, i); |
| 66 | |
| 67 | switch (st->exponent & 0x7fff) { |
| 68 | case 0x7fff: |
| 69 | tag = 2; /* Special */ |
| 70 | break; |
| 71 | case 0x0000: |
| 72 | if ( !st->significand[0] && |
| 73 | !st->significand[1] && |
| 74 | !st->significand[2] && |
| 75 | !st->significand[3] ) { |
| 76 | tag = 1; /* Zero */ |
| 77 | } else { |
| 78 | tag = 2; /* Special */ |
| 79 | } |
| 80 | break; |
| 81 | default: |
| 82 | if (st->significand[3] & 0x8000) { |
| 83 | tag = 0; /* Valid */ |
| 84 | } else { |
| 85 | tag = 2; /* Special */ |
| 86 | } |
| 87 | break; |
| 88 | } |
| 89 | } else { |
| 90 | tag = 3; /* Empty */ |
| 91 | } |
| 92 | ret |= (tag << (2 * i)); |
| 93 | twd = twd >> 1; |
| 94 | } |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | static int convert_fxsr_to_user(struct _fpstate __user *buf, |
| 99 | struct user_fxsr_struct *fxsave) |
| 100 | { |
| 101 | unsigned long env[7]; |
| 102 | struct _fpreg __user *to; |
| 103 | struct _fpxreg *from; |
| 104 | int i; |
| 105 | |
| 106 | env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul; |
| 107 | env[1] = (unsigned long)fxsave->swd | 0xffff0000ul; |
| 108 | env[2] = twd_fxsr_to_i387(fxsave); |
| 109 | env[3] = fxsave->fip; |
| 110 | env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16); |
| 111 | env[5] = fxsave->foo; |
| 112 | env[6] = fxsave->fos; |
| 113 | |
| 114 | if (__copy_to_user(buf, env, 7 * sizeof(unsigned long))) |
| 115 | return 1; |
| 116 | |
| 117 | to = &buf->_st[0]; |
| 118 | from = (struct _fpxreg *) &fxsave->st_space[0]; |
| 119 | for (i = 0; i < 8; i++, to++, from++) { |
| 120 | unsigned long __user *t = (unsigned long __user *)to; |
| 121 | unsigned long *f = (unsigned long *)from; |
| 122 | |
| 123 | if (__put_user(*f, t) || |
| 124 | __put_user(*(f + 1), t + 1) || |
| 125 | __put_user(from->exponent, &to->exponent)) |
| 126 | return 1; |
| 127 | } |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int convert_fxsr_from_user(struct user_fxsr_struct *fxsave, |
| 132 | struct _fpstate __user *buf) |
| 133 | { |
| 134 | unsigned long env[7]; |
| 135 | struct _fpxreg *to; |
| 136 | struct _fpreg __user *from; |
| 137 | int i; |
| 138 | |
| 139 | if (copy_from_user( env, buf, 7 * sizeof(long))) |
| 140 | return 1; |
| 141 | |
| 142 | fxsave->cwd = (unsigned short)(env[0] & 0xffff); |
| 143 | fxsave->swd = (unsigned short)(env[1] & 0xffff); |
| 144 | fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff)); |
| 145 | fxsave->fip = env[3]; |
| 146 | fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16); |
| 147 | fxsave->fcs = (env[4] & 0xffff); |
| 148 | fxsave->foo = env[5]; |
| 149 | fxsave->fos = env[6]; |
| 150 | |
| 151 | to = (struct _fpxreg *) &fxsave->st_space[0]; |
| 152 | from = &buf->_st[0]; |
| 153 | for (i = 0; i < 8; i++, to++, from++) { |
| 154 | unsigned long *t = (unsigned long *)to; |
| 155 | unsigned long __user *f = (unsigned long __user *)from; |
| 156 | |
| 157 | if (__get_user(*t, f) || |
| 158 | __get_user(*(t + 1), f + 1) || |
| 159 | __get_user(to->exponent, &from->exponent)) |
| 160 | return 1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | extern int have_fpx_regs; |
| 166 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 167 | static int copy_sc_from_user(struct pt_regs *regs, |
| 168 | struct sigcontext __user *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 170 | struct sigcontext sc; |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 171 | int err, pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | err = copy_from_user(&sc, from, sizeof(sc)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 174 | if (err) |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 175 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 177 | pid = userspace_pid[current_thread_info()->cpu]; |
Jeff Dike | 377fad3 | 2007-05-06 14:51:25 -0700 | [diff] [blame] | 178 | copy_sc(®s->regs, &sc); |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 179 | if (have_fpx_regs) { |
| 180 | struct user_fxsr_struct fpx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 182 | err = copy_from_user(&fpx, &sc.fpstate->_fxsr_env[0], |
| 183 | sizeof(struct user_fxsr_struct)); |
| 184 | if (err) |
| 185 | return 1; |
| 186 | |
| 187 | err = convert_fxsr_from_user(&fpx, sc.fpstate); |
| 188 | if (err) |
| 189 | return 1; |
| 190 | |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 191 | err = restore_fpx_registers(pid, (unsigned long *) &fpx); |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 192 | if (err < 0) { |
| 193 | printk(KERN_ERR "copy_sc_from_user - " |
| 194 | "restore_fpx_registers failed, errno = %d\n", |
| 195 | -err); |
| 196 | return 1; |
| 197 | } |
| 198 | } |
| 199 | else { |
| 200 | struct user_i387_struct fp; |
| 201 | |
| 202 | err = copy_from_user(&fp, sc.fpstate, |
| 203 | sizeof(struct user_i387_struct)); |
| 204 | if (err) |
| 205 | return 1; |
| 206 | |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 207 | err = restore_fp_registers(pid, (unsigned long *) &fp); |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 208 | if (err < 0) { |
| 209 | printk(KERN_ERR "copy_sc_from_user - " |
| 210 | "restore_fp_registers failed, errno = %d\n", |
| 211 | -err); |
| 212 | return 1; |
| 213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 216 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 219 | static int copy_sc_to_user(struct sigcontext __user *to, |
| 220 | struct _fpstate __user *to_fp, struct pt_regs *regs, |
| 221 | unsigned long sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 223 | struct sigcontext sc; |
Bodo Stroesser | c578455 | 2005-05-05 16:15:31 -0700 | [diff] [blame] | 224 | struct faultinfo * fi = ¤t->thread.arch.faultinfo; |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 225 | int err, pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 227 | sc.gs = REGS_GS(regs->regs.gp); |
| 228 | sc.fs = REGS_FS(regs->regs.gp); |
| 229 | sc.es = REGS_ES(regs->regs.gp); |
| 230 | sc.ds = REGS_DS(regs->regs.gp); |
Jeff Dike | 7796931 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 231 | sc.di = REGS_EDI(regs->regs.gp); |
| 232 | sc.si = REGS_ESI(regs->regs.gp); |
| 233 | sc.bp = REGS_EBP(regs->regs.gp); |
| 234 | sc.sp = sp; |
| 235 | sc.bx = REGS_EBX(regs->regs.gp); |
| 236 | sc.dx = REGS_EDX(regs->regs.gp); |
| 237 | sc.cx = REGS_ECX(regs->regs.gp); |
| 238 | sc.ax = REGS_EAX(regs->regs.gp); |
| 239 | sc.ip = REGS_IP(regs->regs.gp); |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 240 | sc.cs = REGS_CS(regs->regs.gp); |
Jeff Dike | 7796931 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 241 | sc.flags = REGS_EFLAGS(regs->regs.gp); |
| 242 | sc.sp_at_signal = regs->regs.gp[UESP]; |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 243 | sc.ss = regs->regs.gp[SS]; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 244 | sc.cr2 = fi->cr2; |
| 245 | sc.err = fi->error_code; |
| 246 | sc.trapno = fi->trap_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 248 | to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | sc.fpstate = to_fp; |
| 250 | |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 251 | pid = userspace_pid[current_thread_info()->cpu]; |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 252 | if (have_fpx_regs) { |
| 253 | struct user_fxsr_struct fpx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 255 | err = save_fpx_registers(pid, (unsigned long *) &fpx); |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 256 | if (err < 0){ |
| 257 | printk(KERN_ERR "copy_sc_to_user - save_fpx_registers " |
| 258 | "failed, errno = %d\n", err); |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | err = convert_fxsr_to_user(to_fp, &fpx); |
| 263 | if (err) |
| 264 | return 1; |
| 265 | |
| 266 | err |= __put_user(fpx.swd, &to_fp->status); |
| 267 | err |= __put_user(X86_FXSR_MAGIC, &to_fp->magic); |
| 268 | if (err) |
| 269 | return 1; |
| 270 | |
| 271 | if (copy_to_user(&to_fp->_fxsr_env[0], &fpx, |
| 272 | sizeof(struct user_fxsr_struct))) |
| 273 | return 1; |
| 274 | } |
| 275 | else { |
| 276 | struct user_i387_struct fp; |
| 277 | |
Jeff Dike | a5a678c | 2008-02-04 22:30:54 -0800 | [diff] [blame] | 278 | err = save_fp_registers(pid, (unsigned long *) &fp); |
Jeff Dike | a5f6096 | 2007-10-16 01:27:15 -0700 | [diff] [blame] | 279 | if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct))) |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | return copy_to_user(to, &sc, sizeof(sc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 286 | static int copy_ucontext_to_user(struct ucontext __user *uc, |
| 287 | struct _fpstate __user *fp, sigset_t *set, |
| 288 | unsigned long sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | int err = 0; |
| 291 | |
| 292 | err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp); |
| 293 | err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags); |
| 294 | err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size); |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 295 | err |= copy_sc_to_user(&uc->uc_mcontext, fp, ¤t->thread.regs, sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set)); |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 297 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | struct sigframe |
| 301 | { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 302 | char __user *pretcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | int sig; |
| 304 | struct sigcontext sc; |
| 305 | struct _fpstate fpstate; |
| 306 | unsigned long extramask[_NSIG_WORDS-1]; |
| 307 | char retcode[8]; |
| 308 | }; |
| 309 | |
| 310 | struct rt_sigframe |
| 311 | { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 312 | char __user *pretcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | int sig; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 314 | struct siginfo __user *pinfo; |
| 315 | void __user *puc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | struct siginfo info; |
| 317 | struct ucontext uc; |
| 318 | struct _fpstate fpstate; |
| 319 | char retcode[8]; |
| 320 | }; |
| 321 | |
| 322 | int setup_signal_stack_sc(unsigned long stack_top, int sig, |
| 323 | struct k_sigaction *ka, struct pt_regs *regs, |
| 324 | sigset_t *mask) |
| 325 | { |
| 326 | struct sigframe __user *frame; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 327 | void __user *restorer; |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 328 | unsigned long save_sp = PT_REGS_SP(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | int err = 0; |
| 330 | |
Jeff Dike | 3896625 | 2007-01-30 14:36:17 -0800 | [diff] [blame] | 331 | /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */ |
| 332 | stack_top = ((stack_top + 4) & -16UL) - 4; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 333 | frame = (struct sigframe __user *) stack_top - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 335 | return 1; |
| 336 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 337 | restorer = frame->retcode; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 338 | if (ka->sa.sa_flags & SA_RESTORER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | restorer = ka->sa.sa_restorer; |
| 340 | |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 341 | /* Update SP now because the page fault handler refuses to extend |
| 342 | * the stack if the faulting address is too far below the current |
| 343 | * SP, which frame now certainly is. If there's an error, the original |
| 344 | * value is restored on the way out. |
| 345 | * When writing the sigcontext to the stack, we have to write the |
| 346 | * original value, so that's passed to copy_sc_to_user, which does |
| 347 | * the right thing with it. |
| 348 | */ |
| 349 | PT_REGS_SP(regs) = (unsigned long) frame; |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | err |= __put_user(restorer, &frame->pretcode); |
| 352 | err |= __put_user(sig, &frame->sig); |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 353 | err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | err |= __put_user(mask->sig[0], &frame->sc.oldmask); |
| 355 | if (_NSIG_WORDS > 1) |
| 356 | err |= __copy_to_user(&frame->extramask, &mask->sig[1], |
| 357 | sizeof(frame->extramask)); |
| 358 | |
| 359 | /* |
| 360 | * This is popl %eax ; movl $,%eax ; int $0x80 |
| 361 | * |
| 362 | * WE DO NOT USE IT ANY MORE! It's only left here for historical |
| 363 | * reasons and because gdb uses it as a signature to notice |
| 364 | * signal handler stack frames. |
| 365 | */ |
| 366 | err |= __put_user(0xb858, (short __user *)(frame->retcode+0)); |
| 367 | err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2)); |
| 368 | err |= __put_user(0x80cd, (short __user *)(frame->retcode+6)); |
| 369 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 370 | if (err) |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 371 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | PT_REGS_SP(regs) = (unsigned long) frame; |
| 374 | PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler; |
| 375 | PT_REGS_EAX(regs) = (unsigned long) sig; |
| 376 | PT_REGS_EDX(regs) = (unsigned long) 0; |
| 377 | PT_REGS_ECX(regs) = (unsigned long) 0; |
| 378 | |
| 379 | if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED)) |
| 380 | ptrace_notify(SIGTRAP); |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 381 | return 0; |
| 382 | |
| 383 | err: |
| 384 | PT_REGS_SP(regs) = save_sp; |
| 385 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | int setup_signal_stack_si(unsigned long stack_top, int sig, |
| 389 | struct k_sigaction *ka, struct pt_regs *regs, |
| 390 | siginfo_t *info, sigset_t *mask) |
| 391 | { |
| 392 | struct rt_sigframe __user *frame; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 393 | void __user *restorer; |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 394 | unsigned long save_sp = PT_REGS_SP(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | int err = 0; |
| 396 | |
| 397 | stack_top &= -8UL; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 398 | frame = (struct rt_sigframe __user *) stack_top - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 400 | return 1; |
| 401 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 402 | restorer = frame->retcode; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 403 | if (ka->sa.sa_flags & SA_RESTORER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | restorer = ka->sa.sa_restorer; |
| 405 | |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 406 | /* See comment above about why this is here */ |
| 407 | PT_REGS_SP(regs) = (unsigned long) frame; |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | err |= __put_user(restorer, &frame->pretcode); |
| 410 | err |= __put_user(sig, &frame->sig); |
| 411 | err |= __put_user(&frame->info, &frame->pinfo); |
| 412 | err |= __put_user(&frame->uc, &frame->puc); |
| 413 | err |= copy_siginfo_to_user(&frame->info, info); |
| 414 | err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask, |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 415 | save_sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * This is movl $,%eax ; int $0x80 |
| 419 | * |
| 420 | * WE DO NOT USE IT ANY MORE! It's only left here for historical |
| 421 | * reasons and because gdb uses it as a signature to notice |
| 422 | * signal handler stack frames. |
| 423 | */ |
| 424 | err |= __put_user(0xb8, (char __user *)(frame->retcode+0)); |
| 425 | err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1)); |
| 426 | err |= __put_user(0x80cd, (short __user *)(frame->retcode+5)); |
| 427 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 428 | if (err) |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 429 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler; |
| 432 | PT_REGS_EAX(regs) = (unsigned long) sig; |
| 433 | PT_REGS_EDX(regs) = (unsigned long) &frame->info; |
| 434 | PT_REGS_ECX(regs) = (unsigned long) &frame->uc; |
| 435 | |
| 436 | if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED)) |
| 437 | ptrace_notify(SIGTRAP); |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 438 | return 0; |
| 439 | |
| 440 | err: |
| 441 | PT_REGS_SP(regs) = save_sp; |
| 442 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | long sys_sigreturn(struct pt_regs regs) |
| 446 | { |
| 447 | unsigned long sp = PT_REGS_SP(¤t->thread.regs); |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 448 | struct sigframe __user *frame = (struct sigframe __user *)(sp - 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | sigset_t set; |
| 450 | struct sigcontext __user *sc = &frame->sc; |
| 451 | unsigned long __user *oldmask = &sc->oldmask; |
| 452 | unsigned long __user *extramask = frame->extramask; |
| 453 | int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long); |
| 454 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 455 | if (copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) || |
| 456 | copy_from_user(&set.sig[1], extramask, sig_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | goto segfault; |
| 458 | |
| 459 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 460 | |
| 461 | spin_lock_irq(¤t->sighand->siglock); |
| 462 | current->blocked = set; |
| 463 | recalc_sigpending(); |
| 464 | spin_unlock_irq(¤t->sighand->siglock); |
| 465 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 466 | if (copy_sc_from_user(¤t->thread.regs, sc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | goto segfault; |
| 468 | |
| 469 | /* Avoid ERESTART handling */ |
| 470 | PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1; |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 471 | return PT_REGS_SYSCALL_RET(¤t->thread.regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | segfault: |
| 474 | force_sig(SIGSEGV, current); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | long sys_rt_sigreturn(struct pt_regs regs) |
| 479 | { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 480 | unsigned long sp = PT_REGS_SP(¤t->thread.regs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 481 | struct rt_sigframe __user *frame = |
| 482 | (struct rt_sigframe __user *) (sp - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | sigset_t set; |
| 484 | struct ucontext __user *uc = &frame->uc; |
| 485 | int sig_size = _NSIG_WORDS * sizeof(unsigned long); |
| 486 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 487 | if (copy_from_user(&set, &uc->uc_sigmask, sig_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | goto segfault; |
| 489 | |
| 490 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 491 | |
| 492 | spin_lock_irq(¤t->sighand->siglock); |
| 493 | current->blocked = set; |
| 494 | recalc_sigpending(); |
| 495 | spin_unlock_irq(¤t->sighand->siglock); |
| 496 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 497 | if (copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | goto segfault; |
| 499 | |
| 500 | /* Avoid ERESTART handling */ |
| 501 | PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1; |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 502 | return PT_REGS_SYSCALL_RET(¤t->thread.regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | segfault: |
| 505 | force_sig(SIGSEGV, current); |
| 506 | return 0; |
| 507 | } |