Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | static int copy_sc_from_user_skas(struct pt_regs *regs, |
| 24 | struct sigcontext *from) |
| 25 | { |
| 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 | |
| 57 | int copy_sc_to_user_skas(struct sigcontext *to, struct _fpstate *to_fp, |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 58 | struct pt_regs *regs, unsigned long mask, |
| 59 | unsigned long sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Bodo Stroesser | c578455 | 2005-05-05 16:15:31 -0700 | [diff] [blame] | 61 | struct faultinfo * fi = ¤t->thread.arch.faultinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int err = 0; |
| 63 | |
| 64 | err |= __put_user(0, &to->gs); |
| 65 | err |= __put_user(0, &to->fs); |
| 66 | |
| 67 | #define PUTREG(regs, regno, sc, regname) \ |
| 68 | __put_user((regs)->regs.skas.regs[(regno) / sizeof(unsigned long)], \ |
| 69 | &(sc)->regname) |
| 70 | |
| 71 | err |= PUTREG(regs, RDI, to, rdi); |
| 72 | err |= PUTREG(regs, RSI, to, rsi); |
| 73 | err |= PUTREG(regs, RBP, to, rbp); |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 74 | /* Must use orignal RSP, which is passed in, rather than what's in |
| 75 | * the pt_regs, because that's already been updated to point at the |
| 76 | * signal frame. |
| 77 | */ |
| 78 | err |= __put_user(sp, &to->rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | err |= PUTREG(regs, RBX, to, rbx); |
| 80 | err |= PUTREG(regs, RDX, to, rdx); |
| 81 | err |= PUTREG(regs, RCX, to, rcx); |
| 82 | err |= PUTREG(regs, RAX, to, rax); |
| 83 | err |= PUTREG(regs, R8, to, r8); |
| 84 | err |= PUTREG(regs, R9, to, r9); |
| 85 | err |= PUTREG(regs, R10, to, r10); |
| 86 | err |= PUTREG(regs, R11, to, r11); |
| 87 | err |= PUTREG(regs, R12, to, r12); |
| 88 | err |= PUTREG(regs, R13, to, r13); |
| 89 | err |= PUTREG(regs, R14, to, r14); |
| 90 | err |= PUTREG(regs, R15, to, r15); |
| 91 | 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] | 92 | |
| 93 | err |= __put_user(fi->cr2, &to->cr2); |
| 94 | err |= __put_user(fi->error_code, &to->err); |
| 95 | err |= __put_user(fi->trap_no, &to->trapno); |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | err |= PUTREG(regs, RIP, to, rip); |
| 98 | err |= PUTREG(regs, EFLAGS, to, eflags); |
| 99 | #undef PUTREG |
| 100 | |
| 101 | err |= __put_user(mask, &to->oldmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | return(err); |
| 104 | } |
| 105 | |
| 106 | #endif |
| 107 | |
| 108 | #ifdef CONFIG_MODE_TT |
| 109 | int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext *from, |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 110 | int fpsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Al Viro | e54a5df | 2005-09-03 15:57:27 -0700 | [diff] [blame] | 112 | struct _fpstate *to_fp, *from_fp; |
| 113 | unsigned long sigs; |
| 114 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Al Viro | e54a5df | 2005-09-03 15:57:27 -0700 | [diff] [blame] | 116 | to_fp = to->fpstate; |
| 117 | sigs = to->oldmask; |
| 118 | err = copy_from_user(to, from, sizeof(*to)); |
| 119 | from_fp = to->fpstate; |
| 120 | to->fpstate = to_fp; |
| 121 | to->oldmask = sigs; |
| 122 | if(to_fp != NULL) |
| 123 | err |= copy_from_user(to_fp, from_fp, fpsize); |
| 124 | return(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int copy_sc_to_user_tt(struct sigcontext *to, struct _fpstate *fp, |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 128 | struct sigcontext *from, int fpsize, unsigned long sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Al Viro | e54a5df | 2005-09-03 15:57:27 -0700 | [diff] [blame] | 130 | struct _fpstate *to_fp, *from_fp; |
| 131 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Al Viro | e54a5df | 2005-09-03 15:57:27 -0700 | [diff] [blame] | 133 | to_fp = (fp ? fp : (struct _fpstate *) (to + 1)); |
| 134 | from_fp = from->fpstate; |
| 135 | err = copy_to_user(to, from, sizeof(*to)); |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 136 | /* The SP in the sigcontext is the updated one for the signal |
| 137 | * delivery. The sp passed in is the original, and this needs |
| 138 | * to be restored, so we stick it in separately. |
| 139 | */ |
| 140 | err |= copy_to_user(&SC_SP(to), sp, sizeof(sp)); |
| 141 | |
Al Viro | e54a5df | 2005-09-03 15:57:27 -0700 | [diff] [blame] | 142 | if(from_fp != NULL){ |
| 143 | err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate)); |
| 144 | err |= copy_to_user(to_fp, from_fp, fpsize); |
| 145 | } |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 146 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | #endif |
| 150 | |
| 151 | static int copy_sc_from_user(struct pt_regs *to, void __user *from) |
| 152 | { |
| 153 | int ret; |
| 154 | |
| 155 | ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from, |
| 156 | sizeof(struct _fpstate)), |
| 157 | copy_sc_from_user_skas(to, from)); |
| 158 | return(ret); |
| 159 | } |
| 160 | |
| 161 | static int copy_sc_to_user(struct sigcontext *to, struct _fpstate *fp, |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 162 | struct pt_regs *from, unsigned long mask, |
| 163 | unsigned long sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
| 165 | return(CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs), |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 166 | sizeof(*fp), sp), |
| 167 | copy_sc_to_user_skas(to, fp, from, mask, sp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | struct rt_sigframe |
| 171 | { |
| 172 | char *pretcode; |
| 173 | struct ucontext uc; |
| 174 | struct siginfo info; |
| 175 | }; |
| 176 | |
| 177 | #define round_down(m, n) (((m) / (n)) * (n)) |
| 178 | |
| 179 | int setup_signal_stack_si(unsigned long stack_top, int sig, |
| 180 | struct k_sigaction *ka, struct pt_regs * regs, |
| 181 | siginfo_t *info, sigset_t *set) |
| 182 | { |
| 183 | struct rt_sigframe __user *frame; |
| 184 | struct _fpstate __user *fp = NULL; |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 185 | unsigned long save_sp = PT_REGS_RSP(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | int err = 0; |
| 187 | struct task_struct *me = current; |
| 188 | |
| 189 | frame = (struct rt_sigframe __user *) |
| 190 | round_down(stack_top - sizeof(struct rt_sigframe), 16) - 8; |
Paolo 'Blaisorblade' Giarrusso | bcb01b8 | 2005-07-14 00:33:34 -0700 | [diff] [blame] | 191 | frame = (struct rt_sigframe *) ((unsigned long) frame - 128); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate))) |
| 194 | goto out; |
| 195 | |
| 196 | #if 0 /* XXX */ |
| 197 | if (save_i387(fp) < 0) |
| 198 | err |= -1; |
| 199 | #endif |
| 200 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 201 | goto out; |
| 202 | |
| 203 | if (ka->sa.sa_flags & SA_SIGINFO) { |
| 204 | err |= copy_siginfo_to_user(&frame->info, info); |
| 205 | if (err) |
| 206 | goto out; |
| 207 | } |
| 208 | |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 209 | /* Update SP now because the page fault handler refuses to extend |
| 210 | * the stack if the faulting address is too far below the current |
| 211 | * SP, which frame now certainly is. If there's an error, the original |
| 212 | * value is restored on the way out. |
| 213 | * When writing the sigcontext to the stack, we have to write the |
| 214 | * original value, so that's passed to copy_sc_to_user, which does |
| 215 | * the right thing with it. |
| 216 | */ |
| 217 | PT_REGS_RSP(regs) = (unsigned long) frame; |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* Create the ucontext. */ |
| 220 | err |= __put_user(0, &frame->uc.uc_flags); |
| 221 | err |= __put_user(0, &frame->uc.uc_link); |
| 222 | 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] | 223 | err |= __put_user(sas_ss_flags(save_sp), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | &frame->uc.uc_stack.ss_flags); |
| 225 | err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size); |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 226 | err |= copy_sc_to_user(&frame->uc.uc_mcontext, fp, regs, set->sig[0], |
| 227 | save_sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate); |
| 229 | if (sizeof(*set) == 16) { |
| 230 | __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]); |
| 231 | __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]); |
| 232 | } |
| 233 | else |
| 234 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, |
| 235 | sizeof(*set)); |
| 236 | |
| 237 | /* Set up to return from userspace. If provided, use a stub |
| 238 | already in userspace. */ |
| 239 | /* x86-64 should always use SA_RESTORER. */ |
| 240 | if (ka->sa.sa_flags & SA_RESTORER) |
| 241 | err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); |
| 242 | else |
| 243 | /* could use a vstub here */ |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 244 | goto restore_sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | if (err) |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 247 | goto restore_sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | /* Set up registers for signal handler */ |
| 250 | { |
| 251 | struct exec_domain *ed = current_thread_info()->exec_domain; |
| 252 | if (unlikely(ed && ed->signal_invmap && sig < 32)) |
| 253 | sig = ed->signal_invmap[sig]; |
| 254 | } |
| 255 | |
| 256 | PT_REGS_RDI(regs) = sig; |
| 257 | /* In case the signal handler was declared without prototypes */ |
| 258 | PT_REGS_RAX(regs) = 0; |
| 259 | |
| 260 | /* This also works for non SA_SIGINFO handlers because they expect the |
| 261 | next argument after the signal number on the stack. */ |
| 262 | PT_REGS_RSI(regs) = (unsigned long) &frame->info; |
| 263 | PT_REGS_RDX(regs) = (unsigned long) &frame->uc; |
| 264 | PT_REGS_RIP(regs) = (unsigned long) ka->sa.sa_handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | out: |
Jeff Dike | 98c1823 | 2006-03-27 01:14:38 -0800 | [diff] [blame] | 266 | return err; |
| 267 | |
| 268 | restore_sp: |
| 269 | PT_REGS_RSP(regs) = save_sp; |
| 270 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | long sys_rt_sigreturn(struct pt_regs *regs) |
| 274 | { |
| 275 | unsigned long sp = PT_REGS_SP(¤t->thread.regs); |
| 276 | struct rt_sigframe __user *frame = |
| 277 | (struct rt_sigframe __user *)(sp - 8); |
| 278 | struct ucontext __user *uc = &frame->uc; |
| 279 | sigset_t set; |
| 280 | |
| 281 | if(copy_from_user(&set, &uc->uc_sigmask, sizeof(set))) |
| 282 | goto segfault; |
| 283 | |
| 284 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 285 | |
| 286 | spin_lock_irq(¤t->sighand->siglock); |
| 287 | current->blocked = set; |
| 288 | recalc_sigpending(); |
| 289 | spin_unlock_irq(¤t->sighand->siglock); |
| 290 | |
| 291 | if(copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext)) |
| 292 | goto segfault; |
| 293 | |
| 294 | /* Avoid ERESTART handling */ |
| 295 | PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1; |
| 296 | return(PT_REGS_SYSCALL_RET(¤t->thread.regs)); |
| 297 | |
| 298 | segfault: |
| 299 | force_sig(SIGSEGV, current); |
| 300 | return 0; |
| 301 | } |
| 302 | /* |
| 303 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 304 | * Emacs will notice this stuff at the end of the file and automatically |
| 305 | * adjust the settings for this buffer only. This must remain at the end |
| 306 | * of the file. |
| 307 | * --------------------------------------------------------------------------- |
| 308 | * Local variables: |
| 309 | * c-file-style: "linux" |
| 310 | * End: |
| 311 | */ |