Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | * |
| 5 | * Licensed under the GPL |
| 6 | */ |
| 7 | |
| 8 | #define __FRAME_OFFSETS |
Jeff Dike | ba9950c | 2005-05-20 13:59:07 -0700 | [diff] [blame] | 9 | #include <asm/ptrace.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/errno.h> |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 12 | #include <linux/mm.h> |
Jeff Dike | ba9950c | 2005-05-20 13:59:07 -0700 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
| 14 | #include <asm/elf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 16 | /* |
| 17 | * determines which flags the user has access to. |
| 18 | * 1 = access 0 = no access |
| 19 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #define FLAG_MASK 0x44dd5UL |
| 21 | |
| 22 | int putreg(struct task_struct *child, int regno, unsigned long value) |
| 23 | { |
| 24 | unsigned long tmp; |
| 25 | |
| 26 | #ifdef TIF_IA32 |
| 27 | /* Some code in the 64bit emulation may not be 64bit clean. |
| 28 | Don't take any chances. */ |
| 29 | if (test_tsk_thread_flag(child, TIF_IA32)) |
| 30 | value &= 0xffffffff; |
| 31 | #endif |
| 32 | switch (regno){ |
| 33 | case FS: |
| 34 | case GS: |
| 35 | case DS: |
| 36 | case ES: |
| 37 | case SS: |
| 38 | case CS: |
| 39 | if (value && (value & 3) != 3) |
| 40 | return -EIO; |
| 41 | value &= 0xffff; |
| 42 | break; |
| 43 | |
| 44 | case FS_BASE: |
| 45 | case GS_BASE: |
| 46 | if (!((value >> 48) == 0 || (value >> 48) == 0xffff)) |
| 47 | return -EIO; |
| 48 | break; |
| 49 | |
| 50 | case EFLAGS: |
| 51 | value &= FLAG_MASK; |
| 52 | tmp = PT_REGS_EFLAGS(&child->thread.regs) & ~FLAG_MASK; |
| 53 | value |= tmp; |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | PT_REGS_SET(&child->thread.regs, regno, value); |
| 58 | return 0; |
| 59 | } |
| 60 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 61 | int poke_user(struct task_struct *child, long addr, long data) |
| 62 | { |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 63 | if ((addr & 3) || addr < 0) |
| 64 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 65 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 66 | if (addr < MAX_REG_OFFSET) |
| 67 | return putreg(child, addr, data); |
| 68 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 69 | (addr <= offsetof(struct user, u_debugreg[7]))){ |
| 70 | addr -= offsetof(struct user, u_debugreg[0]); |
| 71 | addr = addr >> 2; |
| 72 | if ((addr == 4) || (addr == 5)) |
| 73 | return -EIO; |
| 74 | child->thread.arch.debugregs[addr] = data; |
| 75 | return 0; |
| 76 | } |
| 77 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | unsigned long getreg(struct task_struct *child, int regno) |
| 81 | { |
| 82 | unsigned long retval = ~0UL; |
| 83 | switch (regno) { |
| 84 | case FS: |
| 85 | case GS: |
| 86 | case DS: |
| 87 | case ES: |
| 88 | case SS: |
| 89 | case CS: |
| 90 | retval = 0xffff; |
| 91 | /* fall through */ |
| 92 | default: |
| 93 | retval &= PT_REG(&child->thread.regs, regno); |
| 94 | #ifdef TIF_IA32 |
| 95 | if (test_tsk_thread_flag(child, TIF_IA32)) |
| 96 | retval &= 0xffffffff; |
| 97 | #endif |
| 98 | } |
| 99 | return retval; |
| 100 | } |
| 101 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 102 | int peek_user(struct task_struct *child, long addr, long data) |
| 103 | { |
| 104 | /* read the word at location addr in the USER area. */ |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 105 | unsigned long tmp; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 106 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 107 | if ((addr & 3) || addr < 0) |
| 108 | return -EIO; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 109 | |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 110 | tmp = 0; /* Default return condition */ |
| 111 | if (addr < MAX_REG_OFFSET){ |
| 112 | tmp = getreg(child, addr); |
| 113 | } |
| 114 | else if ((addr >= offsetof(struct user, u_debugreg[0])) && |
| 115 | (addr <= offsetof(struct user, u_debugreg[7]))){ |
| 116 | addr -= offsetof(struct user, u_debugreg[0]); |
| 117 | addr = addr >> 2; |
| 118 | tmp = child->thread.arch.debugregs[addr]; |
| 119 | } |
| 120 | return put_user(tmp, (unsigned long *) data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 123 | /* XXX Mostly copied from sys-i386 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | int is_syscall(unsigned long addr) |
| 125 | { |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 126 | unsigned short instr; |
| 127 | int n; |
| 128 | |
| 129 | n = copy_from_user(&instr, (void __user *) addr, sizeof(instr)); |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 130 | if (n){ |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 131 | /* access_process_vm() grants access to vsyscall and stub, |
| 132 | * while copy_from_user doesn't. Maybe access_process_vm is |
| 133 | * slow, but that doesn't matter, since it will be called only |
| 134 | * in case of singlestepping, if copy_from_user failed. |
| 135 | */ |
| 136 | n = access_process_vm(current, addr, &instr, sizeof(instr), 0); |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 137 | if (n != sizeof(instr)) { |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 138 | printk("is_syscall : failed to read instruction from " |
| 139 | "0x%lx\n", addr); |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 140 | return 1; |
Bodo Stroesser | 81efcd3 | 2006-03-27 01:14:34 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | /* sysenter */ |
Jeff Dike | f0c4cad | 2007-10-16 01:27:18 -0700 | [diff] [blame^] | 144 | return instr == 0x050f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 147 | int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 149 | int err, n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 150 | long fpregs[HOST_FP_SIZE]; |
| 151 | |
| 152 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
| 153 | err = save_fp_registers(userspace_pid[cpu], fpregs); |
| 154 | if (err) |
| 155 | return err; |
| 156 | |
| 157 | n = copy_to_user((void *) buf, fpregs, sizeof(fpregs)); |
| 158 | if(n > 0) |
| 159 | return -EFAULT; |
| 160 | |
| 161 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 164 | int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 166 | int n, cpu = ((struct thread_info *) child->stack)->cpu; |
| 167 | long fpregs[HOST_FP_SIZE]; |
| 168 | |
| 169 | BUG_ON(sizeof(*buf) != sizeof(fpregs)); |
| 170 | n = copy_from_user(fpregs, (void *) buf, sizeof(fpregs)); |
| 171 | if (n > 0) |
| 172 | return -EFAULT; |
| 173 | |
| 174 | return restore_fp_registers(userspace_pid[cpu], fpregs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 177 | long subarch_ptrace(struct task_struct *child, long request, long addr, |
| 178 | long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 180 | int ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 182 | switch (request) { |
| 183 | case PTRACE_GETFPXREGS: /* Get the child FPU state. */ |
| 184 | ret = get_fpregs((struct user_i387_struct __user *) data, |
| 185 | child); |
| 186 | break; |
| 187 | case PTRACE_SETFPXREGS: /* Set the child FPU state. */ |
| 188 | ret = set_fpregs((struct user_i387_struct __user *) data, |
| 189 | child); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |