Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* By Ross Biro 1/23/92 */ |
| 2 | /* |
| 3 | * Pentium III FXSR, SSE support |
| 4 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/errno.h> |
| 12 | #include <linux/ptrace.h> |
| 13 | #include <linux/user.h> |
| 14 | #include <linux/security.h> |
| 15 | #include <linux/audit.h> |
| 16 | #include <linux/seccomp.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 17 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/uaccess.h> |
| 20 | #include <asm/pgtable.h> |
| 21 | #include <asm/system.h> |
| 22 | #include <asm/processor.h> |
| 23 | #include <asm/i387.h> |
| 24 | #include <asm/debugreg.h> |
| 25 | #include <asm/ldt.h> |
| 26 | #include <asm/desc.h> |
| 27 | |
| 28 | /* |
| 29 | * does not yet catch signals sent when the child dies. |
| 30 | * in exit.c or in signal.c. |
| 31 | */ |
| 32 | |
Chuck Ebbert | 9f155b9 | 2006-01-05 23:11:29 -0500 | [diff] [blame] | 33 | /* |
| 34 | * Determines which flags the user has access to [1 = access, 0 = no access]. |
Chuck Ebbert | 3c36c6a | 2006-03-23 02:59:39 -0800 | [diff] [blame] | 35 | * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9). |
Chuck Ebbert | 9f155b9 | 2006-01-05 23:11:29 -0500 | [diff] [blame] | 36 | * Also masks reserved bits (31-22, 15, 5, 3, 1). |
| 37 | */ |
Chuck Ebbert | 3c36c6a | 2006-03-23 02:59:39 -0800 | [diff] [blame] | 38 | #define FLAG_MASK 0x00050dd5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Offset of eflags on child stack.. |
| 42 | */ |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 43 | #define EFL_OFFSET offsetof(struct pt_regs, eflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | static inline struct pt_regs *get_child_regs(struct task_struct *task) |
| 46 | { |
| 47 | void *stack_top = (void *)task->thread.esp0; |
| 48 | return stack_top - sizeof(struct pt_regs); |
| 49 | } |
| 50 | |
| 51 | /* |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 52 | * This routine will get a word off of the processes privileged stack. |
| 53 | * the offset is bytes into the pt_regs structure on the stack. |
| 54 | * This routine assumes that all the privileged stacks are in our |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | * data space. |
| 56 | */ |
| 57 | static inline int get_stack_long(struct task_struct *task, int offset) |
| 58 | { |
| 59 | unsigned char *stack; |
| 60 | |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 61 | stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | stack += offset; |
| 63 | return (*((int *)stack)); |
| 64 | } |
| 65 | |
| 66 | /* |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 67 | * This routine will put a word on the processes privileged stack. |
| 68 | * the offset is bytes into the pt_regs structure on the stack. |
| 69 | * This routine assumes that all the privileged stacks are in our |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * data space. |
| 71 | */ |
| 72 | static inline int put_stack_long(struct task_struct *task, int offset, |
| 73 | unsigned long data) |
| 74 | { |
| 75 | unsigned char * stack; |
| 76 | |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 77 | stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | stack += offset; |
| 79 | *(unsigned long *) stack = data; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int putreg(struct task_struct *child, |
| 84 | unsigned long regno, unsigned long value) |
| 85 | { |
| 86 | switch (regno >> 2) { |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 87 | case GS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (value && (value & 3) != 3) |
| 89 | return -EIO; |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 90 | child->thread.gs = value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | case DS: |
| 93 | case ES: |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 94 | case FS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if (value && (value & 3) != 3) |
| 96 | return -EIO; |
| 97 | value &= 0xffff; |
| 98 | break; |
| 99 | case SS: |
| 100 | case CS: |
| 101 | if ((value & 3) != 3) |
| 102 | return -EIO; |
| 103 | value &= 0xffff; |
| 104 | break; |
| 105 | case EFL: |
| 106 | value &= FLAG_MASK; |
Roland McGrath | e1f2877 | 2008-01-30 13:30:50 +0100 | [diff] [blame^] | 107 | /* |
| 108 | * If the user value contains TF, mark that |
| 109 | * it was not "us" (the debugger) that set it. |
| 110 | * If not, make sure it stays set if we had. |
| 111 | */ |
| 112 | if (value & X86_EFLAGS_TF) |
| 113 | clear_tsk_thread_flag(child, TIF_FORCED_TF); |
| 114 | else if (test_tsk_thread_flag(child, TIF_FORCED_TF)) |
| 115 | value |= X86_EFLAGS_TF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK; |
| 117 | break; |
| 118 | } |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 119 | if (regno > FS*4) |
Jeremy Fitzhardinge | 66e10a4 | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 120 | regno -= 1*4; |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 121 | put_stack_long(child, regno, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static unsigned long getreg(struct task_struct *child, |
| 126 | unsigned long regno) |
| 127 | { |
| 128 | unsigned long retval = ~0UL; |
| 129 | |
| 130 | switch (regno >> 2) { |
Roland McGrath | e1f2877 | 2008-01-30 13:30:50 +0100 | [diff] [blame^] | 131 | case EFL: |
| 132 | /* |
| 133 | * If the debugger set TF, hide it from the readout. |
| 134 | */ |
| 135 | retval = get_stack_long(child, EFL_OFFSET); |
| 136 | if (test_tsk_thread_flag(child, TIF_FORCED_TF)) |
| 137 | retval &= ~X86_EFLAGS_TF; |
| 138 | break; |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 139 | case GS: |
| 140 | retval = child->thread.gs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | case DS: |
| 143 | case ES: |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 144 | case FS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | case SS: |
| 146 | case CS: |
| 147 | retval = 0xffff; |
| 148 | /* fall through */ |
| 149 | default: |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 150 | if (regno > FS*4) |
Jeremy Fitzhardinge | 66e10a4 | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 151 | regno -= 1*4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | retval &= get_stack_long(child, regno); |
| 153 | } |
| 154 | return retval; |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* |
| 158 | * Called by kernel/ptrace.c when detaching.. |
| 159 | * |
| 160 | * Make sure the single step bit is not set. |
| 161 | */ |
| 162 | void ptrace_disable(struct task_struct *child) |
| 163 | { |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 164 | user_disable_single_step(child); |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 165 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 168 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | struct user * dummy = NULL; |
| 171 | int i, ret; |
| 172 | unsigned long __user *datap = (unsigned long __user *)data; |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | switch (request) { |
| 175 | /* when I and D space are separate, these will need to be fixed. */ |
| 176 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 177 | case PTRACE_PEEKDATA: |
| 178 | ret = generic_ptrace_peekdata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | /* read the word at location addr in the USER area. */ |
| 182 | case PTRACE_PEEKUSR: { |
| 183 | unsigned long tmp; |
| 184 | |
| 185 | ret = -EIO; |
| 186 | if ((addr & 3) || addr < 0 || |
| 187 | addr > sizeof(struct user) - 3) |
| 188 | break; |
| 189 | |
| 190 | tmp = 0; /* Default return condition */ |
| 191 | if(addr < FRAME_SIZE*sizeof(long)) |
| 192 | tmp = getreg(child, addr); |
| 193 | if(addr >= (long) &dummy->u_debugreg[0] && |
| 194 | addr <= (long) &dummy->u_debugreg[7]){ |
| 195 | addr -= (long) &dummy->u_debugreg[0]; |
| 196 | addr = addr >> 2; |
| 197 | tmp = child->thread.debugreg[addr]; |
| 198 | } |
| 199 | ret = put_user(tmp, datap); |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | /* when I and D space are separate, this will have to be fixed. */ |
| 204 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 205 | case PTRACE_POKEDATA: |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 206 | ret = generic_ptrace_pokedata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
| 210 | ret = -EIO; |
| 211 | if ((addr & 3) || addr < 0 || |
| 212 | addr > sizeof(struct user) - 3) |
| 213 | break; |
| 214 | |
| 215 | if (addr < FRAME_SIZE*sizeof(long)) { |
| 216 | ret = putreg(child, addr, data); |
| 217 | break; |
| 218 | } |
| 219 | /* We need to be very careful here. We implicitly |
| 220 | want to modify a portion of the task_struct, and we |
| 221 | have to be selective about what portions we allow someone |
| 222 | to modify. */ |
| 223 | |
| 224 | ret = -EIO; |
| 225 | if(addr >= (long) &dummy->u_debugreg[0] && |
| 226 | addr <= (long) &dummy->u_debugreg[7]){ |
| 227 | |
| 228 | if(addr == (long) &dummy->u_debugreg[4]) break; |
| 229 | if(addr == (long) &dummy->u_debugreg[5]) break; |
| 230 | if(addr < (long) &dummy->u_debugreg[4] && |
| 231 | ((unsigned long) data) >= TASK_SIZE-3) break; |
| 232 | |
| 233 | /* Sanity-check data. Take one half-byte at once with |
| 234 | * check = (val >> (16 + 4*i)) & 0xf. It contains the |
| 235 | * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits |
| 236 | * 2 and 3 are LENi. Given a list of invalid values, |
| 237 | * we do mask |= 1 << invalid_value, so that |
| 238 | * (mask >> check) & 1 is a correct test for invalid |
| 239 | * values. |
| 240 | * |
| 241 | * R/Wi contains the type of the breakpoint / |
| 242 | * watchpoint, LENi contains the length of the watched |
| 243 | * data in the watchpoint case. |
| 244 | * |
| 245 | * The invalid values are: |
| 246 | * - LENi == 0x10 (undefined), so mask |= 0x0f00. |
| 247 | * - R/Wi == 0x10 (break on I/O reads or writes), so |
| 248 | * mask |= 0x4444. |
| 249 | * - R/Wi == 0x00 && LENi != 0x00, so we have mask |= |
| 250 | * 0x1110. |
| 251 | * |
| 252 | * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54. |
| 253 | * |
| 254 | * See the Intel Manual "System Programming Guide", |
| 255 | * 15.2.4 |
| 256 | * |
| 257 | * Note that LENi == 0x10 is defined on x86_64 in long |
| 258 | * mode (i.e. even for 32-bit userspace software, but |
| 259 | * 64-bit kernel), so the x86_64 mask value is 0x5454. |
| 260 | * See the AMD manual no. 24593 (AMD64 System |
| 261 | * Programming)*/ |
| 262 | |
| 263 | if(addr == (long) &dummy->u_debugreg[7]) { |
| 264 | data &= ~DR_CONTROL_RESERVED; |
| 265 | for(i=0; i<4; i++) |
| 266 | if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1) |
| 267 | goto out_tsk; |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 268 | if (data) |
| 269 | set_tsk_thread_flag(child, TIF_DEBUG); |
| 270 | else |
| 271 | clear_tsk_thread_flag(child, TIF_DEBUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | addr -= (long) &dummy->u_debugreg; |
| 274 | addr = addr >> 2; |
| 275 | child->thread.debugreg[addr] = data; |
| 276 | ret = 0; |
| 277 | } |
| 278 | break; |
| 279 | |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 280 | case PTRACE_SYSEMU: /* continue and stop at next syscall, which will not be executed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 282 | case PTRACE_CONT: /* restart after signal. */ |
| 283 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 284 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | break; |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 286 | if (request == PTRACE_SYSEMU) { |
| 287 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 288 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 289 | } else if (request == PTRACE_SYSCALL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 291 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 292 | } else { |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 293 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 295 | } |
| 296 | child->exit_code = data; |
| 297 | /* make sure the single step bit is not set. */ |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 298 | user_disable_single_step(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | wake_up_process(child); |
| 300 | ret = 0; |
| 301 | break; |
| 302 | |
| 303 | /* |
| 304 | * make the child exit. Best I can do is send it a sigkill. |
| 305 | * perhaps it should be put in the status that it wants to |
| 306 | * exit. |
| 307 | */ |
| 308 | case PTRACE_KILL: |
| 309 | ret = 0; |
| 310 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 311 | break; |
| 312 | child->exit_code = SIGKILL; |
| 313 | /* make sure the single step bit is not set. */ |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 314 | user_disable_single_step(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | wake_up_process(child); |
| 316 | break; |
| 317 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 318 | case PTRACE_SYSEMU_SINGLESTEP: /* Same as SYSEMU, but singlestep if not syscall */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | case PTRACE_SINGLESTEP: /* set the trap flag. */ |
| 320 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 321 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | break; |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 323 | |
| 324 | if (request == PTRACE_SYSEMU_SINGLESTEP) |
| 325 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 326 | else |
| 327 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 330 | user_enable_single_step(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | child->exit_code = data; |
| 332 | /* give it a chance to run. */ |
| 333 | wake_up_process(child); |
| 334 | ret = 0; |
| 335 | break; |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
| 338 | if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) { |
| 339 | ret = -EIO; |
| 340 | break; |
| 341 | } |
| 342 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { |
| 343 | __put_user(getreg(child, i), datap); |
| 344 | datap++; |
| 345 | } |
| 346 | ret = 0; |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 351 | unsigned long tmp; |
| 352 | if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) { |
| 353 | ret = -EIO; |
| 354 | break; |
| 355 | } |
| 356 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { |
| 357 | __get_user(tmp, datap); |
| 358 | putreg(child, i, tmp); |
| 359 | datap++; |
| 360 | } |
| 361 | ret = 0; |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | case PTRACE_GETFPREGS: { /* Get the child FPU state. */ |
| 366 | if (!access_ok(VERIFY_WRITE, datap, |
| 367 | sizeof(struct user_i387_struct))) { |
| 368 | ret = -EIO; |
| 369 | break; |
| 370 | } |
| 371 | ret = 0; |
| 372 | if (!tsk_used_math(child)) |
| 373 | init_fpu(child); |
| 374 | get_fpregs((struct user_i387_struct __user *)data, child); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | case PTRACE_SETFPREGS: { /* Set the child FPU state. */ |
| 379 | if (!access_ok(VERIFY_READ, datap, |
| 380 | sizeof(struct user_i387_struct))) { |
| 381 | ret = -EIO; |
| 382 | break; |
| 383 | } |
| 384 | set_stopped_child_used_math(child); |
| 385 | set_fpregs(child, (struct user_i387_struct __user *)data); |
| 386 | ret = 0; |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */ |
| 391 | if (!access_ok(VERIFY_WRITE, datap, |
| 392 | sizeof(struct user_fxsr_struct))) { |
| 393 | ret = -EIO; |
| 394 | break; |
| 395 | } |
| 396 | if (!tsk_used_math(child)) |
| 397 | init_fpu(child); |
| 398 | ret = get_fpxregs((struct user_fxsr_struct __user *)data, child); |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */ |
| 403 | if (!access_ok(VERIFY_READ, datap, |
| 404 | sizeof(struct user_fxsr_struct))) { |
| 405 | ret = -EIO; |
| 406 | break; |
| 407 | } |
| 408 | set_stopped_child_used_math(child); |
| 409 | ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data); |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | case PTRACE_GET_THREAD_AREA: |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 414 | if (addr < 0) |
| 415 | return -EIO; |
| 416 | ret = do_get_thread_area(child, addr, |
| 417 | (struct user_desc __user *) data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | break; |
| 419 | |
| 420 | case PTRACE_SET_THREAD_AREA: |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 421 | if (addr < 0) |
| 422 | return -EIO; |
| 423 | ret = do_set_thread_area(child, addr, |
| 424 | (struct user_desc __user *) data, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | break; |
| 426 | |
| 427 | default: |
| 428 | ret = ptrace_request(child, request, addr, data); |
| 429 | break; |
| 430 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 431 | out_tsk: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code) |
| 436 | { |
| 437 | struct siginfo info; |
| 438 | |
| 439 | tsk->thread.trap_no = 1; |
| 440 | tsk->thread.error_code = error_code; |
| 441 | |
| 442 | memset(&info, 0, sizeof(info)); |
| 443 | info.si_signo = SIGTRAP; |
| 444 | info.si_code = TRAP_BRKPT; |
| 445 | |
| 446 | /* User-mode eip? */ |
Vincent Hanquez | fa1e1bd | 2005-06-23 00:08:44 -0700 | [diff] [blame] | 447 | info.si_addr = user_mode_vm(regs) ? (void __user *) regs->eip : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Simon Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 449 | /* Send us the fake SIGTRAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | force_sig_info(SIGTRAP, &info, tsk); |
| 451 | } |
| 452 | |
| 453 | /* notification of system call entry/exit |
| 454 | * - triggered by current->work.syscall_trace |
| 455 | */ |
| 456 | __attribute__((regparm(3))) |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 457 | int do_syscall_trace(struct pt_regs *regs, int entryexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 459 | int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU); |
| 460 | /* |
| 461 | * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall |
| 462 | * interception |
| 463 | */ |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 464 | int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP); |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 465 | int ret = 0; |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* do the secure computing check first */ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 468 | if (!entryexit) |
| 469 | secure_computing(regs->orig_eax); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 471 | if (unlikely(current->audit_context)) { |
| 472 | if (entryexit) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 473 | audit_syscall_exit(AUDITSC_RESULT(regs->eax), |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 474 | regs->eax); |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 475 | /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only |
| 476 | * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is |
| 477 | * not used, entry.S will call us only on syscall exit, not |
| 478 | * entry; so when TIF_SYSCALL_AUDIT is used we must avoid |
| 479 | * calling send_sigtrap() on syscall entry. |
| 480 | * |
| 481 | * Note that when PTRACE_SYSEMU_SINGLESTEP is used, |
| 482 | * is_singlestep is false, despite his name, so we will still do |
| 483 | * the correct thing. |
| 484 | */ |
| 485 | else if (is_singlestep) |
| 486 | goto out; |
| 487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | if (!(current->ptrace & PT_PTRACED)) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 490 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 492 | /* If a process stops on the 1st tracepoint with SYSCALL_TRACE |
| 493 | * and then is resumed with SYSEMU_SINGLESTEP, it will come in |
| 494 | * here. We have to check this and return */ |
| 495 | if (is_sysemu && entryexit) |
| 496 | return 0; |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | /* Fake a debug trap */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 499 | if (is_singlestep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | send_sigtrap(current, regs, 0); |
| 501 | |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 502 | if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 503 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 506 | between a syscall stop and SIGTRAP delivery */ |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 507 | /* Note that the debugger could change the result of test_thread_flag!*/ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 508 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * this isn't the same as continuing with a signal, but it will do |
| 512 | * for normal use. strace only continues with a signal if the |
| 513 | * stopping signal is not SIGTRAP. -brl |
| 514 | */ |
| 515 | if (current->exit_code) { |
| 516 | send_sig(current->exit_code, current, 1); |
| 517 | current->exit_code = 0; |
| 518 | } |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 519 | ret = is_sysemu; |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 520 | out: |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 521 | if (unlikely(current->audit_context) && !entryexit) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 522 | audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_eax, |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 523 | regs->ebx, regs->ecx, regs->edx, regs->esi); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 524 | if (ret == 0) |
| 525 | return 0; |
| 526 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 527 | regs->orig_eax = -1; /* force skip of syscall restarting */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 528 | if (unlikely(current->audit_context)) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 529 | audit_syscall_exit(AUDITSC_RESULT(regs->eax), regs->eax); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 530 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |