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