Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ptrace.c */ |
| 2 | /* By Ross Biro 1/23/92 */ |
| 3 | /* |
| 4 | * Pentium III FXSR, SSE support |
| 5 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/ptrace.h> |
| 14 | #include <linux/user.h> |
| 15 | #include <linux/security.h> |
| 16 | #include <linux/audit.h> |
| 17 | #include <linux/seccomp.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 18 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/uaccess.h> |
| 21 | #include <asm/pgtable.h> |
| 22 | #include <asm/system.h> |
| 23 | #include <asm/processor.h> |
| 24 | #include <asm/i387.h> |
| 25 | #include <asm/debugreg.h> |
| 26 | #include <asm/ldt.h> |
| 27 | #include <asm/desc.h> |
| 28 | |
| 29 | /* |
| 30 | * does not yet catch signals sent when the child dies. |
| 31 | * in exit.c or in signal.c. |
| 32 | */ |
| 33 | |
Chuck Ebbert | 9f155b9 | 2006-01-05 23:11:29 -0500 | [diff] [blame] | 34 | /* |
| 35 | * 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] | 36 | * 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] | 37 | * Also masks reserved bits (31-22, 15, 5, 3, 1). |
| 38 | */ |
Chuck Ebbert | 3c36c6a | 2006-03-23 02:59:39 -0800 | [diff] [blame] | 39 | #define FLAG_MASK 0x00050dd5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* set's the trap flag. */ |
| 42 | #define TRAP_FLAG 0x100 |
| 43 | |
| 44 | /* |
| 45 | * Offset of eflags on child stack.. |
| 46 | */ |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 47 | #define EFL_OFFSET offsetof(struct pt_regs, eflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | static inline struct pt_regs *get_child_regs(struct task_struct *task) |
| 50 | { |
| 51 | void *stack_top = (void *)task->thread.esp0; |
| 52 | return stack_top - sizeof(struct pt_regs); |
| 53 | } |
| 54 | |
| 55 | /* |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 56 | * This routine will get a word off of the processes privileged stack. |
| 57 | * the offset is bytes into the pt_regs structure on the stack. |
| 58 | * This routine assumes that all the privileged stacks are in our |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * data space. |
| 60 | */ |
| 61 | static inline int get_stack_long(struct task_struct *task, int offset) |
| 62 | { |
| 63 | unsigned char *stack; |
| 64 | |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 65 | stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | stack += offset; |
| 67 | return (*((int *)stack)); |
| 68 | } |
| 69 | |
| 70 | /* |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 71 | * This routine will put a word on the processes privileged stack. |
| 72 | * the offset is bytes into the pt_regs structure on the stack. |
| 73 | * This routine assumes that all the privileged stacks are in our |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * data space. |
| 75 | */ |
| 76 | static inline int put_stack_long(struct task_struct *task, int offset, |
| 77 | unsigned long data) |
| 78 | { |
| 79 | unsigned char * stack; |
| 80 | |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 81 | stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | stack += offset; |
| 83 | *(unsigned long *) stack = data; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int putreg(struct task_struct *child, |
| 88 | unsigned long regno, unsigned long value) |
| 89 | { |
| 90 | switch (regno >> 2) { |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 91 | case GS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (value && (value & 3) != 3) |
| 93 | return -EIO; |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 94 | child->thread.gs = value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | case DS: |
| 97 | case ES: |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 98 | case FS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (value && (value & 3) != 3) |
| 100 | return -EIO; |
| 101 | value &= 0xffff; |
| 102 | break; |
| 103 | case SS: |
| 104 | case CS: |
| 105 | if ((value & 3) != 3) |
| 106 | return -EIO; |
| 107 | value &= 0xffff; |
| 108 | break; |
| 109 | case EFL: |
| 110 | value &= FLAG_MASK; |
| 111 | value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK; |
| 112 | break; |
| 113 | } |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 114 | if (regno > FS*4) |
Jeremy Fitzhardinge | 66e10a4 | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 115 | regno -= 1*4; |
Jeremy Fitzhardinge | 8701ea9 | 2006-12-22 01:11:21 -0800 | [diff] [blame] | 116 | put_stack_long(child, regno, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static unsigned long getreg(struct task_struct *child, |
| 121 | unsigned long regno) |
| 122 | { |
| 123 | unsigned long retval = ~0UL; |
| 124 | |
| 125 | switch (regno >> 2) { |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 126 | case GS: |
| 127 | retval = child->thread.gs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | case DS: |
| 130 | case ES: |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 131 | case FS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | case SS: |
| 133 | case CS: |
| 134 | retval = 0xffff; |
| 135 | /* fall through */ |
| 136 | default: |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 137 | if (regno > FS*4) |
Jeremy Fitzhardinge | 66e10a4 | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 138 | regno -= 1*4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | retval &= get_stack_long(child, regno); |
| 140 | } |
| 141 | return retval; |
| 142 | } |
| 143 | |
| 144 | #define LDT_SEGMENT 4 |
| 145 | |
| 146 | static unsigned long convert_eip_to_linear(struct task_struct *child, struct pt_regs *regs) |
| 147 | { |
| 148 | unsigned long addr, seg; |
| 149 | |
| 150 | addr = regs->eip; |
| 151 | seg = regs->xcs & 0xffff; |
| 152 | if (regs->eflags & VM_MASK) { |
| 153 | addr = (addr & 0xffff) + (seg << 4); |
| 154 | return addr; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * We'll assume that the code segments in the GDT |
| 159 | * are all zero-based. That is largely true: the |
| 160 | * TLS segments are used for data, and the PNPBIOS |
| 161 | * and APM bios ones we just ignore here. |
| 162 | */ |
| 163 | if (seg & LDT_SEGMENT) { |
| 164 | u32 *desc; |
| 165 | unsigned long base; |
| 166 | |
Roland McGrath | 29eb511 | 2007-07-16 01:03:16 -0700 | [diff] [blame^] | 167 | seg &= ~7UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Roland McGrath | 29eb511 | 2007-07-16 01:03:16 -0700 | [diff] [blame^] | 169 | down(&child->mm->context.sem); |
| 170 | if (unlikely((seg >> 3) >= child->mm->context.size)) |
| 171 | addr = -1L; /* bogus selector, access would fault */ |
| 172 | else { |
| 173 | desc = child->mm->context.ldt + seg; |
| 174 | base = ((desc[0] >> 16) | |
| 175 | ((desc[1] & 0xff) << 16) | |
| 176 | (desc[1] & 0xff000000)); |
| 177 | |
| 178 | /* 16-bit code segment? */ |
| 179 | if (!((desc[1] >> 22) & 1)) |
| 180 | addr &= 0xffff; |
| 181 | addr += base; |
| 182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | up(&child->mm->context.sem); |
| 184 | } |
| 185 | return addr; |
| 186 | } |
| 187 | |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 188 | static inline int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
| 190 | int i, copied; |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 191 | unsigned char opcode[15]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | unsigned long addr = convert_eip_to_linear(child, regs); |
| 193 | |
| 194 | copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0); |
| 195 | for (i = 0; i < copied; i++) { |
| 196 | switch (opcode[i]) { |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 197 | /* popf and iret */ |
| 198 | case 0x9d: case 0xcf: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return 1; |
| 200 | /* opcode and address size prefixes */ |
| 201 | case 0x66: case 0x67: |
| 202 | continue; |
| 203 | /* irrelevant prefixes (segment overrides and repeats) */ |
| 204 | case 0x26: case 0x2e: |
| 205 | case 0x36: case 0x3e: |
| 206 | case 0x64: case 0x65: |
| 207 | case 0xf0: case 0xf2: case 0xf3: |
| 208 | continue; |
| 209 | |
| 210 | /* |
| 211 | * pushf: NOTE! We should probably not let |
| 212 | * the user see the TF bit being set. But |
| 213 | * it's more pain than it's worth to avoid |
| 214 | * it, and a debugger could emulate this |
| 215 | * all in user space if it _really_ cares. |
| 216 | */ |
| 217 | case 0x9c: |
| 218 | default: |
| 219 | return 0; |
| 220 | } |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static void set_singlestep(struct task_struct *child) |
| 226 | { |
| 227 | struct pt_regs *regs = get_child_regs(child); |
| 228 | |
| 229 | /* |
| 230 | * Always set TIF_SINGLESTEP - this guarantees that |
| 231 | * we single-step system calls etc.. This will also |
| 232 | * cause us to set TF when returning to user mode. |
| 233 | */ |
| 234 | set_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 235 | |
| 236 | /* |
| 237 | * If TF was already set, don't do anything else |
| 238 | */ |
| 239 | if (regs->eflags & TRAP_FLAG) |
| 240 | return; |
| 241 | |
| 242 | /* Set TF on the kernel stack.. */ |
| 243 | regs->eflags |= TRAP_FLAG; |
| 244 | |
| 245 | /* |
| 246 | * ..but if TF is changed by the instruction we will trace, |
| 247 | * don't mark it as being "us" that set it, so that we |
| 248 | * won't clear it by hand later. |
| 249 | */ |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 250 | if (is_setting_trap_flag(child, regs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return; |
| 252 | |
| 253 | child->ptrace |= PT_DTRACE; |
| 254 | } |
| 255 | |
| 256 | static void clear_singlestep(struct task_struct *child) |
| 257 | { |
| 258 | /* Always clear TIF_SINGLESTEP... */ |
| 259 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 260 | |
| 261 | /* But touch TF only if it was set by us.. */ |
| 262 | if (child->ptrace & PT_DTRACE) { |
| 263 | struct pt_regs *regs = get_child_regs(child); |
| 264 | regs->eflags &= ~TRAP_FLAG; |
| 265 | child->ptrace &= ~PT_DTRACE; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Called by kernel/ptrace.c when detaching.. |
| 271 | * |
| 272 | * Make sure the single step bit is not set. |
| 273 | */ |
| 274 | void ptrace_disable(struct task_struct *child) |
| 275 | { |
| 276 | clear_singlestep(child); |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 277 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 278 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Perform get_thread_area on behalf of the traced child. |
| 283 | */ |
| 284 | static int |
| 285 | ptrace_get_thread_area(struct task_struct *child, |
| 286 | int idx, struct user_desc __user *user_desc) |
| 287 | { |
| 288 | struct user_desc info; |
| 289 | struct desc_struct *desc; |
| 290 | |
| 291 | /* |
| 292 | * Get the current Thread-Local Storage area: |
| 293 | */ |
| 294 | |
| 295 | #define GET_BASE(desc) ( \ |
| 296 | (((desc)->a >> 16) & 0x0000ffff) | \ |
| 297 | (((desc)->b << 16) & 0x00ff0000) | \ |
| 298 | ( (desc)->b & 0xff000000) ) |
| 299 | |
| 300 | #define GET_LIMIT(desc) ( \ |
| 301 | ((desc)->a & 0x0ffff) | \ |
| 302 | ((desc)->b & 0xf0000) ) |
| 303 | |
| 304 | #define GET_32BIT(desc) (((desc)->b >> 22) & 1) |
| 305 | #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3) |
| 306 | #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1) |
| 307 | #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1) |
| 308 | #define GET_PRESENT(desc) (((desc)->b >> 15) & 1) |
| 309 | #define GET_USEABLE(desc) (((desc)->b >> 20) & 1) |
| 310 | |
| 311 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 312 | return -EINVAL; |
| 313 | |
| 314 | desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN; |
| 315 | |
| 316 | info.entry_number = idx; |
| 317 | info.base_addr = GET_BASE(desc); |
| 318 | info.limit = GET_LIMIT(desc); |
| 319 | info.seg_32bit = GET_32BIT(desc); |
| 320 | info.contents = GET_CONTENTS(desc); |
| 321 | info.read_exec_only = !GET_WRITABLE(desc); |
| 322 | info.limit_in_pages = GET_LIMIT_PAGES(desc); |
| 323 | info.seg_not_present = !GET_PRESENT(desc); |
| 324 | info.useable = GET_USEABLE(desc); |
| 325 | |
| 326 | if (copy_to_user(user_desc, &info, sizeof(info))) |
| 327 | return -EFAULT; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Perform set_thread_area on behalf of the traced child. |
| 334 | */ |
| 335 | static int |
| 336 | ptrace_set_thread_area(struct task_struct *child, |
| 337 | int idx, struct user_desc __user *user_desc) |
| 338 | { |
| 339 | struct user_desc info; |
| 340 | struct desc_struct *desc; |
| 341 | |
| 342 | if (copy_from_user(&info, user_desc, sizeof(info))) |
| 343 | return -EFAULT; |
| 344 | |
| 345 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN; |
| 349 | if (LDT_empty(&info)) { |
| 350 | desc->a = 0; |
| 351 | desc->b = 0; |
| 352 | } else { |
| 353 | desc->a = LDT_entry_a(&info); |
| 354 | desc->b = LDT_entry_b(&info); |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 360 | 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] | 361 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | struct user * dummy = NULL; |
| 363 | int i, ret; |
| 364 | unsigned long __user *datap = (unsigned long __user *)data; |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | switch (request) { |
| 367 | /* when I and D space are separate, these will need to be fixed. */ |
| 368 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 369 | case PTRACE_PEEKDATA: |
| 370 | ret = generic_ptrace_peekdata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | /* read the word at location addr in the USER area. */ |
| 374 | case PTRACE_PEEKUSR: { |
| 375 | unsigned long tmp; |
| 376 | |
| 377 | ret = -EIO; |
| 378 | if ((addr & 3) || addr < 0 || |
| 379 | addr > sizeof(struct user) - 3) |
| 380 | break; |
| 381 | |
| 382 | tmp = 0; /* Default return condition */ |
| 383 | if(addr < FRAME_SIZE*sizeof(long)) |
| 384 | tmp = getreg(child, addr); |
| 385 | if(addr >= (long) &dummy->u_debugreg[0] && |
| 386 | addr <= (long) &dummy->u_debugreg[7]){ |
| 387 | addr -= (long) &dummy->u_debugreg[0]; |
| 388 | addr = addr >> 2; |
| 389 | tmp = child->thread.debugreg[addr]; |
| 390 | } |
| 391 | ret = put_user(tmp, datap); |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | /* when I and D space are separate, this will have to be fixed. */ |
| 396 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 397 | case PTRACE_POKEDATA: |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 398 | ret = generic_ptrace_pokedata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | break; |
| 400 | |
| 401 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
| 402 | ret = -EIO; |
| 403 | if ((addr & 3) || addr < 0 || |
| 404 | addr > sizeof(struct user) - 3) |
| 405 | break; |
| 406 | |
| 407 | if (addr < FRAME_SIZE*sizeof(long)) { |
| 408 | ret = putreg(child, addr, data); |
| 409 | break; |
| 410 | } |
| 411 | /* We need to be very careful here. We implicitly |
| 412 | want to modify a portion of the task_struct, and we |
| 413 | have to be selective about what portions we allow someone |
| 414 | to modify. */ |
| 415 | |
| 416 | ret = -EIO; |
| 417 | if(addr >= (long) &dummy->u_debugreg[0] && |
| 418 | addr <= (long) &dummy->u_debugreg[7]){ |
| 419 | |
| 420 | if(addr == (long) &dummy->u_debugreg[4]) break; |
| 421 | if(addr == (long) &dummy->u_debugreg[5]) break; |
| 422 | if(addr < (long) &dummy->u_debugreg[4] && |
| 423 | ((unsigned long) data) >= TASK_SIZE-3) break; |
| 424 | |
| 425 | /* Sanity-check data. Take one half-byte at once with |
| 426 | * check = (val >> (16 + 4*i)) & 0xf. It contains the |
| 427 | * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits |
| 428 | * 2 and 3 are LENi. Given a list of invalid values, |
| 429 | * we do mask |= 1 << invalid_value, so that |
| 430 | * (mask >> check) & 1 is a correct test for invalid |
| 431 | * values. |
| 432 | * |
| 433 | * R/Wi contains the type of the breakpoint / |
| 434 | * watchpoint, LENi contains the length of the watched |
| 435 | * data in the watchpoint case. |
| 436 | * |
| 437 | * The invalid values are: |
| 438 | * - LENi == 0x10 (undefined), so mask |= 0x0f00. |
| 439 | * - R/Wi == 0x10 (break on I/O reads or writes), so |
| 440 | * mask |= 0x4444. |
| 441 | * - R/Wi == 0x00 && LENi != 0x00, so we have mask |= |
| 442 | * 0x1110. |
| 443 | * |
| 444 | * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54. |
| 445 | * |
| 446 | * See the Intel Manual "System Programming Guide", |
| 447 | * 15.2.4 |
| 448 | * |
| 449 | * Note that LENi == 0x10 is defined on x86_64 in long |
| 450 | * mode (i.e. even for 32-bit userspace software, but |
| 451 | * 64-bit kernel), so the x86_64 mask value is 0x5454. |
| 452 | * See the AMD manual no. 24593 (AMD64 System |
| 453 | * Programming)*/ |
| 454 | |
| 455 | if(addr == (long) &dummy->u_debugreg[7]) { |
| 456 | data &= ~DR_CONTROL_RESERVED; |
| 457 | for(i=0; i<4; i++) |
| 458 | if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1) |
| 459 | goto out_tsk; |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 460 | if (data) |
| 461 | set_tsk_thread_flag(child, TIF_DEBUG); |
| 462 | else |
| 463 | clear_tsk_thread_flag(child, TIF_DEBUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | addr -= (long) &dummy->u_debugreg; |
| 466 | addr = addr >> 2; |
| 467 | child->thread.debugreg[addr] = data; |
| 468 | ret = 0; |
| 469 | } |
| 470 | break; |
| 471 | |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 472 | 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] | 473 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 474 | case PTRACE_CONT: /* restart after signal. */ |
| 475 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 476 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | break; |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 478 | if (request == PTRACE_SYSEMU) { |
| 479 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 480 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 481 | } else if (request == PTRACE_SYSCALL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 483 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 484 | } else { |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 485 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 487 | } |
| 488 | child->exit_code = data; |
| 489 | /* make sure the single step bit is not set. */ |
| 490 | clear_singlestep(child); |
| 491 | wake_up_process(child); |
| 492 | ret = 0; |
| 493 | break; |
| 494 | |
| 495 | /* |
| 496 | * make the child exit. Best I can do is send it a sigkill. |
| 497 | * perhaps it should be put in the status that it wants to |
| 498 | * exit. |
| 499 | */ |
| 500 | case PTRACE_KILL: |
| 501 | ret = 0; |
| 502 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 503 | break; |
| 504 | child->exit_code = SIGKILL; |
| 505 | /* make sure the single step bit is not set. */ |
| 506 | clear_singlestep(child); |
| 507 | wake_up_process(child); |
| 508 | break; |
| 509 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 510 | case PTRACE_SYSEMU_SINGLESTEP: /* Same as SYSEMU, but singlestep if not syscall */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | case PTRACE_SINGLESTEP: /* set the trap flag. */ |
| 512 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 513 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | break; |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 515 | |
| 516 | if (request == PTRACE_SYSEMU_SINGLESTEP) |
| 517 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 518 | else |
| 519 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 522 | set_singlestep(child); |
| 523 | child->exit_code = data; |
| 524 | /* give it a chance to run. */ |
| 525 | wake_up_process(child); |
| 526 | ret = 0; |
| 527 | break; |
| 528 | |
| 529 | case PTRACE_DETACH: |
| 530 | /* detach a process that was attached. */ |
| 531 | ret = ptrace_detach(child, data); |
| 532 | break; |
| 533 | |
| 534 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
| 535 | if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) { |
| 536 | ret = -EIO; |
| 537 | break; |
| 538 | } |
| 539 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { |
| 540 | __put_user(getreg(child, i), datap); |
| 541 | datap++; |
| 542 | } |
| 543 | ret = 0; |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 548 | unsigned long tmp; |
| 549 | if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) { |
| 550 | ret = -EIO; |
| 551 | break; |
| 552 | } |
| 553 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { |
| 554 | __get_user(tmp, datap); |
| 555 | putreg(child, i, tmp); |
| 556 | datap++; |
| 557 | } |
| 558 | ret = 0; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | case PTRACE_GETFPREGS: { /* Get the child FPU state. */ |
| 563 | if (!access_ok(VERIFY_WRITE, datap, |
| 564 | sizeof(struct user_i387_struct))) { |
| 565 | ret = -EIO; |
| 566 | break; |
| 567 | } |
| 568 | ret = 0; |
| 569 | if (!tsk_used_math(child)) |
| 570 | init_fpu(child); |
| 571 | get_fpregs((struct user_i387_struct __user *)data, child); |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | case PTRACE_SETFPREGS: { /* Set the child FPU state. */ |
| 576 | if (!access_ok(VERIFY_READ, datap, |
| 577 | sizeof(struct user_i387_struct))) { |
| 578 | ret = -EIO; |
| 579 | break; |
| 580 | } |
| 581 | set_stopped_child_used_math(child); |
| 582 | set_fpregs(child, (struct user_i387_struct __user *)data); |
| 583 | ret = 0; |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */ |
| 588 | if (!access_ok(VERIFY_WRITE, datap, |
| 589 | sizeof(struct user_fxsr_struct))) { |
| 590 | ret = -EIO; |
| 591 | break; |
| 592 | } |
| 593 | if (!tsk_used_math(child)) |
| 594 | init_fpu(child); |
| 595 | ret = get_fpxregs((struct user_fxsr_struct __user *)data, child); |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */ |
| 600 | if (!access_ok(VERIFY_READ, datap, |
| 601 | sizeof(struct user_fxsr_struct))) { |
| 602 | ret = -EIO; |
| 603 | break; |
| 604 | } |
| 605 | set_stopped_child_used_math(child); |
| 606 | ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data); |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | case PTRACE_GET_THREAD_AREA: |
| 611 | ret = ptrace_get_thread_area(child, addr, |
| 612 | (struct user_desc __user *) data); |
| 613 | break; |
| 614 | |
| 615 | case PTRACE_SET_THREAD_AREA: |
| 616 | ret = ptrace_set_thread_area(child, addr, |
| 617 | (struct user_desc __user *) data); |
| 618 | break; |
| 619 | |
| 620 | default: |
| 621 | ret = ptrace_request(child, request, addr, data); |
| 622 | break; |
| 623 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 624 | out_tsk: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code) |
| 629 | { |
| 630 | struct siginfo info; |
| 631 | |
| 632 | tsk->thread.trap_no = 1; |
| 633 | tsk->thread.error_code = error_code; |
| 634 | |
| 635 | memset(&info, 0, sizeof(info)); |
| 636 | info.si_signo = SIGTRAP; |
| 637 | info.si_code = TRAP_BRKPT; |
| 638 | |
| 639 | /* User-mode eip? */ |
Vincent Hanquez | fa1e1bd | 2005-06-23 00:08:44 -0700 | [diff] [blame] | 640 | info.si_addr = user_mode_vm(regs) ? (void __user *) regs->eip : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
| 642 | /* Send us the fakey SIGTRAP */ |
| 643 | force_sig_info(SIGTRAP, &info, tsk); |
| 644 | } |
| 645 | |
| 646 | /* notification of system call entry/exit |
| 647 | * - triggered by current->work.syscall_trace |
| 648 | */ |
| 649 | __attribute__((regparm(3))) |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 650 | int do_syscall_trace(struct pt_regs *regs, int entryexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 652 | int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU); |
| 653 | /* |
| 654 | * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall |
| 655 | * interception |
| 656 | */ |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 657 | int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP); |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 658 | int ret = 0; |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | /* do the secure computing check first */ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 661 | if (!entryexit) |
| 662 | secure_computing(regs->orig_eax); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 664 | if (unlikely(current->audit_context)) { |
| 665 | if (entryexit) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 666 | audit_syscall_exit(AUDITSC_RESULT(regs->eax), |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 667 | regs->eax); |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 668 | /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only |
| 669 | * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is |
| 670 | * not used, entry.S will call us only on syscall exit, not |
| 671 | * entry; so when TIF_SYSCALL_AUDIT is used we must avoid |
| 672 | * calling send_sigtrap() on syscall entry. |
| 673 | * |
| 674 | * Note that when PTRACE_SYSEMU_SINGLESTEP is used, |
| 675 | * is_singlestep is false, despite his name, so we will still do |
| 676 | * the correct thing. |
| 677 | */ |
| 678 | else if (is_singlestep) |
| 679 | goto out; |
| 680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
| 682 | if (!(current->ptrace & PT_PTRACED)) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 683 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 685 | /* If a process stops on the 1st tracepoint with SYSCALL_TRACE |
| 686 | * and then is resumed with SYSEMU_SINGLESTEP, it will come in |
| 687 | * here. We have to check this and return */ |
| 688 | if (is_sysemu && entryexit) |
| 689 | return 0; |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | /* Fake a debug trap */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 692 | if (is_singlestep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | send_sigtrap(current, regs, 0); |
| 694 | |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 695 | if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 696 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 699 | between a syscall stop and SIGTRAP delivery */ |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 700 | /* Note that the debugger could change the result of test_thread_flag!*/ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 701 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * this isn't the same as continuing with a signal, but it will do |
| 705 | * for normal use. strace only continues with a signal if the |
| 706 | * stopping signal is not SIGTRAP. -brl |
| 707 | */ |
| 708 | if (current->exit_code) { |
| 709 | send_sig(current->exit_code, current, 1); |
| 710 | current->exit_code = 0; |
| 711 | } |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 712 | ret = is_sysemu; |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 713 | out: |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 714 | if (unlikely(current->audit_context) && !entryexit) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 715 | audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_eax, |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 716 | regs->ebx, regs->ecx, regs->edx, regs->esi); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 717 | if (ret == 0) |
| 718 | return 0; |
| 719 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 720 | regs->orig_eax = -1; /* force skip of syscall restarting */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 721 | if (unlikely(current->audit_context)) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 722 | audit_syscall_exit(AUDITSC_RESULT(regs->eax), regs->eax); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 723 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |