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