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