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 | * x86-64 port 2000-2002 Andi Kleen |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/ptrace.h> |
| 15 | #include <linux/user.h> |
| 16 | #include <linux/security.h> |
| 17 | #include <linux/audit.h> |
| 18 | #include <linux/seccomp.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 19 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/uaccess.h> |
| 22 | #include <asm/pgtable.h> |
| 23 | #include <asm/system.h> |
| 24 | #include <asm/processor.h> |
| 25 | #include <asm/i387.h> |
| 26 | #include <asm/debugreg.h> |
| 27 | #include <asm/ldt.h> |
| 28 | #include <asm/desc.h> |
| 29 | #include <asm/proto.h> |
| 30 | #include <asm/ia32.h> |
| 31 | |
| 32 | /* |
| 33 | * does not yet catch signals sent when the child dies. |
| 34 | * in exit.c or in signal.c. |
| 35 | */ |
| 36 | |
Chuck Ebbert | 60923df | 2006-01-11 22:46:03 +0100 | [diff] [blame] | 37 | /* |
| 38 | * Determines which flags the user has access to [1 = access, 0 = no access]. |
| 39 | * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9). |
| 40 | * Also masks reserved bits (63-22, 15, 5, 3, 1). |
| 41 | */ |
| 42 | #define FLAG_MASK 0x54dd5UL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* set's the trap flag. */ |
| 45 | #define TRAP_FLAG 0x100UL |
| 46 | |
| 47 | /* |
| 48 | * eflags and offset of eflags on child stack.. |
| 49 | */ |
| 50 | #define EFLAGS offsetof(struct pt_regs, eflags) |
| 51 | #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs))) |
| 52 | |
| 53 | /* |
| 54 | * this routine will get a word off of the processes privileged stack. |
| 55 | * the offset is how far from the base addr as stored in the TSS. |
| 56 | * this routine assumes that all the privileged stacks are in our |
| 57 | * data space. |
| 58 | */ |
| 59 | static inline unsigned long get_stack_long(struct task_struct *task, int offset) |
| 60 | { |
| 61 | unsigned char *stack; |
| 62 | |
| 63 | stack = (unsigned char *)task->thread.rsp0; |
| 64 | stack += offset; |
| 65 | return (*((unsigned long *)stack)); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * this routine will put a word on the processes privileged stack. |
| 70 | * the offset is how far from the base addr as stored in the TSS. |
| 71 | * this routine assumes that all the privileged stacks are in our |
| 72 | * data space. |
| 73 | */ |
| 74 | static inline long put_stack_long(struct task_struct *task, int offset, |
| 75 | unsigned long data) |
| 76 | { |
| 77 | unsigned char * stack; |
| 78 | |
| 79 | stack = (unsigned char *) task->thread.rsp0; |
| 80 | stack += offset; |
| 81 | *(unsigned long *) stack = data; |
| 82 | return 0; |
| 83 | } |
| 84 | |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 85 | #define LDT_SEGMENT 4 |
| 86 | |
| 87 | unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs) |
| 88 | { |
| 89 | unsigned long addr, seg; |
| 90 | |
| 91 | addr = regs->rip; |
| 92 | seg = regs->cs & 0xffff; |
| 93 | |
| 94 | /* |
| 95 | * We'll assume that the code segments in the GDT |
| 96 | * are all zero-based. That is largely true: the |
| 97 | * TLS segments are used for data, and the PNPBIOS |
| 98 | * and APM bios ones we just ignore here. |
| 99 | */ |
| 100 | if (seg & LDT_SEGMENT) { |
| 101 | u32 *desc; |
| 102 | unsigned long base; |
| 103 | |
Roland McGrath | 29eb511 | 2007-07-16 01:03:16 -0700 | [diff] [blame] | 104 | seg &= ~7UL; |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 105 | |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 106 | mutex_lock(&child->mm->context.lock); |
Roland McGrath | 29eb511 | 2007-07-16 01:03:16 -0700 | [diff] [blame] | 107 | if (unlikely((seg >> 3) >= child->mm->context.size)) |
| 108 | addr = -1L; /* bogus selector, access would fault */ |
| 109 | else { |
| 110 | desc = child->mm->context.ldt + seg; |
| 111 | base = ((desc[0] >> 16) | |
| 112 | ((desc[1] & 0xff) << 16) | |
| 113 | (desc[1] & 0xff000000)); |
| 114 | |
| 115 | /* 16-bit code segment? */ |
| 116 | if (!((desc[1] >> 22) & 1)) |
| 117 | addr &= 0xffff; |
| 118 | addr += base; |
| 119 | } |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 120 | mutex_unlock(&child->mm->context.lock); |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 121 | } |
Roland McGrath | 29eb511 | 2007-07-16 01:03:16 -0700 | [diff] [blame] | 122 | |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 123 | return addr; |
| 124 | } |
| 125 | |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 126 | static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs) |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 127 | { |
| 128 | int i, copied; |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 129 | unsigned char opcode[15]; |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 130 | unsigned long addr = convert_rip_to_linear(child, regs); |
| 131 | |
| 132 | copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0); |
| 133 | for (i = 0; i < copied; i++) { |
| 134 | switch (opcode[i]) { |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 135 | /* popf and iret */ |
| 136 | case 0x9d: case 0xcf: |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 137 | return 1; |
| 138 | |
| 139 | /* CHECKME: 64 65 */ |
| 140 | |
| 141 | /* opcode and address size prefixes */ |
| 142 | case 0x66: case 0x67: |
| 143 | continue; |
| 144 | /* irrelevant prefixes (segment overrides and repeats) */ |
| 145 | case 0x26: case 0x2e: |
| 146 | case 0x36: case 0x3e: |
| 147 | case 0x64: case 0x65: |
Chuck Ebbert | d4d3585 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 148 | case 0xf2: case 0xf3: |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 149 | continue; |
| 150 | |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 151 | case 0x40 ... 0x4f: |
Chuck Ebbert | a752d71 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 152 | if (regs->cs != __USER_CS) |
| 153 | /* 32-bit mode: register increment */ |
| 154 | return 0; |
| 155 | /* 64-bit mode: REX prefix */ |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 156 | continue; |
| 157 | |
Chuck Ebbert | d4d3585 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 158 | /* CHECKME: f2, f3 */ |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * pushf: NOTE! We should probably not let |
| 162 | * the user see the TF bit being set. But |
| 163 | * it's more pain than it's worth to avoid |
| 164 | * it, and a debugger could emulate this |
| 165 | * all in user space if it _really_ cares. |
| 166 | */ |
| 167 | case 0x9c: |
| 168 | default: |
| 169 | return 0; |
| 170 | } |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 175 | static void set_singlestep(struct task_struct *child) |
| 176 | { |
Al Viro | bb04923 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 177 | struct pt_regs *regs = task_pt_regs(child); |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Always set TIF_SINGLESTEP - this guarantees that |
| 181 | * we single-step system calls etc.. This will also |
| 182 | * cause us to set TF when returning to user mode. |
| 183 | */ |
| 184 | set_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 185 | |
| 186 | /* |
| 187 | * If TF was already set, don't do anything else |
| 188 | */ |
| 189 | if (regs->eflags & TRAP_FLAG) |
| 190 | return; |
| 191 | |
| 192 | /* Set TF on the kernel stack.. */ |
| 193 | regs->eflags |= TRAP_FLAG; |
| 194 | |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 195 | /* |
| 196 | * ..but if TF is changed by the instruction we will trace, |
| 197 | * don't mark it as being "us" that set it, so that we |
| 198 | * won't clear it by hand later. |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 199 | */ |
Chuck Ebbert | 2ade292 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 200 | if (is_setting_trap_flag(child, regs)) |
Andi Kleen | e502cdd | 2005-04-16 15:24:58 -0700 | [diff] [blame] | 201 | return; |
| 202 | |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 203 | child->ptrace |= PT_DTRACE; |
| 204 | } |
| 205 | |
| 206 | static void clear_singlestep(struct task_struct *child) |
| 207 | { |
| 208 | /* Always clear TIF_SINGLESTEP... */ |
| 209 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 210 | |
| 211 | /* But touch TF only if it was set by us.. */ |
| 212 | if (child->ptrace & PT_DTRACE) { |
Al Viro | bb04923 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 213 | struct pt_regs *regs = task_pt_regs(child); |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 214 | regs->eflags &= ~TRAP_FLAG; |
| 215 | child->ptrace &= ~PT_DTRACE; |
| 216 | } |
| 217 | } |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* |
| 220 | * Called by kernel/ptrace.c when detaching.. |
| 221 | * |
| 222 | * Make sure the single step bit is not set. |
| 223 | */ |
| 224 | void ptrace_disable(struct task_struct *child) |
| 225 | { |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 226 | clear_singlestep(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int putreg(struct task_struct *child, |
| 230 | unsigned long regno, unsigned long value) |
| 231 | { |
| 232 | unsigned long tmp; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | switch (regno) { |
| 235 | case offsetof(struct user_regs_struct,fs): |
| 236 | if (value && (value & 3) != 3) |
| 237 | return -EIO; |
| 238 | child->thread.fsindex = value & 0xffff; |
| 239 | return 0; |
| 240 | case offsetof(struct user_regs_struct,gs): |
| 241 | if (value && (value & 3) != 3) |
| 242 | return -EIO; |
| 243 | child->thread.gsindex = value & 0xffff; |
| 244 | return 0; |
| 245 | case offsetof(struct user_regs_struct,ds): |
| 246 | if (value && (value & 3) != 3) |
| 247 | return -EIO; |
| 248 | child->thread.ds = value & 0xffff; |
| 249 | return 0; |
| 250 | case offsetof(struct user_regs_struct,es): |
| 251 | if (value && (value & 3) != 3) |
| 252 | return -EIO; |
| 253 | child->thread.es = value & 0xffff; |
| 254 | return 0; |
| 255 | case offsetof(struct user_regs_struct,ss): |
| 256 | if ((value & 3) != 3) |
| 257 | return -EIO; |
| 258 | value &= 0xffff; |
| 259 | return 0; |
| 260 | case offsetof(struct user_regs_struct,fs_base): |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 261 | if (value >= TASK_SIZE_OF(child)) |
Andi Kleen | f6b8d47 | 2005-05-16 21:53:30 -0700 | [diff] [blame] | 262 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | child->thread.fs = value; |
| 264 | return 0; |
| 265 | case offsetof(struct user_regs_struct,gs_base): |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 266 | if (value >= TASK_SIZE_OF(child)) |
Andi Kleen | f6b8d47 | 2005-05-16 21:53:30 -0700 | [diff] [blame] | 267 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | child->thread.gs = value; |
| 269 | return 0; |
| 270 | case offsetof(struct user_regs_struct, eflags): |
| 271 | value &= FLAG_MASK; |
| 272 | tmp = get_stack_long(child, EFL_OFFSET); |
| 273 | tmp &= ~FLAG_MASK; |
| 274 | value |= tmp; |
| 275 | break; |
| 276 | case offsetof(struct user_regs_struct,cs): |
| 277 | if ((value & 3) != 3) |
| 278 | return -EIO; |
| 279 | value &= 0xffff; |
| 280 | break; |
| 281 | } |
| 282 | put_stack_long(child, regno - sizeof(struct pt_regs), value); |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static unsigned long getreg(struct task_struct *child, unsigned long regno) |
| 287 | { |
| 288 | unsigned long val; |
| 289 | switch (regno) { |
| 290 | case offsetof(struct user_regs_struct, fs): |
| 291 | return child->thread.fsindex; |
| 292 | case offsetof(struct user_regs_struct, gs): |
| 293 | return child->thread.gsindex; |
| 294 | case offsetof(struct user_regs_struct, ds): |
| 295 | return child->thread.ds; |
| 296 | case offsetof(struct user_regs_struct, es): |
| 297 | return child->thread.es; |
| 298 | case offsetof(struct user_regs_struct, fs_base): |
| 299 | return child->thread.fs; |
| 300 | case offsetof(struct user_regs_struct, gs_base): |
| 301 | return child->thread.gs; |
| 302 | default: |
| 303 | regno = regno - sizeof(struct pt_regs); |
| 304 | val = get_stack_long(child, regno); |
| 305 | if (test_tsk_thread_flag(child, TIF_IA32)) |
| 306 | val &= 0xffffffff; |
| 307 | return val; |
| 308 | } |
| 309 | |
| 310 | } |
| 311 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 312 | 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] | 313 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | long i, ret; |
| 315 | unsigned ui; |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | switch (request) { |
| 318 | /* when I and D space are separate, these will need to be fixed. */ |
| 319 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 320 | case PTRACE_PEEKDATA: |
| 321 | ret = generic_ptrace_peekdata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | /* read the word at location addr in the USER area. */ |
| 325 | case PTRACE_PEEKUSR: { |
| 326 | unsigned long tmp; |
| 327 | |
| 328 | ret = -EIO; |
| 329 | if ((addr & 7) || |
| 330 | addr > sizeof(struct user) - 7) |
| 331 | break; |
| 332 | |
| 333 | switch (addr) { |
Andi Kleen | c4d1fcf | 2005-05-20 14:27:56 -0700 | [diff] [blame] | 334 | case 0 ... sizeof(struct user_regs_struct) - sizeof(long): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | tmp = getreg(child, addr); |
| 336 | break; |
| 337 | case offsetof(struct user, u_debugreg[0]): |
| 338 | tmp = child->thread.debugreg0; |
| 339 | break; |
| 340 | case offsetof(struct user, u_debugreg[1]): |
| 341 | tmp = child->thread.debugreg1; |
| 342 | break; |
| 343 | case offsetof(struct user, u_debugreg[2]): |
| 344 | tmp = child->thread.debugreg2; |
| 345 | break; |
| 346 | case offsetof(struct user, u_debugreg[3]): |
| 347 | tmp = child->thread.debugreg3; |
| 348 | break; |
| 349 | case offsetof(struct user, u_debugreg[6]): |
| 350 | tmp = child->thread.debugreg6; |
| 351 | break; |
| 352 | case offsetof(struct user, u_debugreg[7]): |
| 353 | tmp = child->thread.debugreg7; |
| 354 | break; |
| 355 | default: |
| 356 | tmp = 0; |
| 357 | break; |
| 358 | } |
| 359 | ret = put_user(tmp,(unsigned long __user *) data); |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | /* when I and D space are separate, this will have to be fixed. */ |
| 364 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 365 | case PTRACE_POKEDATA: |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 366 | ret = generic_ptrace_pokedata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | break; |
| 368 | |
| 369 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 370 | { |
| 371 | int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | ret = -EIO; |
| 373 | if ((addr & 7) || |
| 374 | addr > sizeof(struct user) - 7) |
| 375 | break; |
| 376 | |
| 377 | switch (addr) { |
Andi Kleen | c4d1fcf | 2005-05-20 14:27:56 -0700 | [diff] [blame] | 378 | case 0 ... sizeof(struct user_regs_struct) - sizeof(long): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | ret = putreg(child, addr, data); |
| 380 | break; |
| 381 | /* Disallows to set a breakpoint into the vsyscall */ |
| 382 | case offsetof(struct user, u_debugreg[0]): |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 383 | if (data >= TASK_SIZE_OF(child) - dsize) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | child->thread.debugreg0 = data; |
| 385 | ret = 0; |
| 386 | break; |
| 387 | case offsetof(struct user, u_debugreg[1]): |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 388 | if (data >= TASK_SIZE_OF(child) - dsize) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | child->thread.debugreg1 = data; |
| 390 | ret = 0; |
| 391 | break; |
| 392 | case offsetof(struct user, u_debugreg[2]): |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 393 | if (data >= TASK_SIZE_OF(child) - dsize) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | child->thread.debugreg2 = data; |
| 395 | ret = 0; |
| 396 | break; |
| 397 | case offsetof(struct user, u_debugreg[3]): |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 398 | if (data >= TASK_SIZE_OF(child) - dsize) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | child->thread.debugreg3 = data; |
| 400 | ret = 0; |
| 401 | break; |
| 402 | case offsetof(struct user, u_debugreg[6]): |
| 403 | if (data >> 32) |
| 404 | break; |
| 405 | child->thread.debugreg6 = data; |
| 406 | ret = 0; |
| 407 | break; |
| 408 | case offsetof(struct user, u_debugreg[7]): |
| 409 | /* See arch/i386/kernel/ptrace.c for an explanation of |
| 410 | * this awkward check.*/ |
Jan Beulich | 893efca | 2006-03-25 16:29:19 +0100 | [diff] [blame] | 411 | data &= ~DR_CONTROL_RESERVED; |
| 412 | for(i=0; i<4; i++) |
| 413 | if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | break; |
| 415 | if (i == 4) { |
Stephane Eranian | d3a4f48 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 416 | child->thread.debugreg7 = data; |
| 417 | if (data) |
| 418 | set_tsk_thread_flag(child, TIF_DEBUG); |
| 419 | else |
| 420 | clear_tsk_thread_flag(child, TIF_DEBUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | ret = 0; |
Stephane Eranian | d3a4f48 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | break; |
| 424 | } |
| 425 | break; |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 428 | case PTRACE_CONT: /* restart after signal. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 431 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | break; |
| 433 | if (request == PTRACE_SYSCALL) |
| 434 | set_tsk_thread_flag(child,TIF_SYSCALL_TRACE); |
| 435 | else |
| 436 | clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE); |
| 437 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 438 | child->exit_code = data; |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 439 | /* make sure the single step bit is not set. */ |
| 440 | clear_singlestep(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | wake_up_process(child); |
| 442 | ret = 0; |
| 443 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | #ifdef CONFIG_IA32_EMULATION |
| 446 | /* This makes only sense with 32bit programs. Allow a |
| 447 | 64bit debugger to fully examine them too. Better |
| 448 | don't use it against 64bit processes, use |
| 449 | PTRACE_ARCH_PRCTL instead. */ |
| 450 | case PTRACE_SET_THREAD_AREA: { |
| 451 | struct user_desc __user *p; |
| 452 | int old; |
| 453 | p = (struct user_desc __user *)data; |
| 454 | get_user(old, &p->entry_number); |
| 455 | put_user(addr, &p->entry_number); |
| 456 | ret = do_set_thread_area(&child->thread, p); |
| 457 | put_user(old, &p->entry_number); |
| 458 | break; |
| 459 | case PTRACE_GET_THREAD_AREA: |
| 460 | p = (struct user_desc __user *)data; |
| 461 | get_user(old, &p->entry_number); |
| 462 | put_user(addr, &p->entry_number); |
| 463 | ret = do_get_thread_area(&child->thread, p); |
| 464 | put_user(old, &p->entry_number); |
| 465 | break; |
| 466 | } |
| 467 | #endif |
| 468 | /* normal 64bit interface to access TLS data. |
| 469 | Works just like arch_prctl, except that the arguments |
| 470 | are reversed. */ |
| 471 | case PTRACE_ARCH_PRCTL: |
| 472 | ret = do_arch_prctl(child, data, addr); |
| 473 | break; |
| 474 | |
| 475 | /* |
| 476 | * make the child exit. Best I can do is send it a sigkill. |
| 477 | * perhaps it should be put in the status that it wants to |
| 478 | * exit. |
| 479 | */ |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 480 | case PTRACE_KILL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | ret = 0; |
| 482 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 483 | break; |
| 484 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 485 | child->exit_code = SIGKILL; |
| 486 | /* make sure the single step bit is not set. */ |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 487 | clear_singlestep(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | wake_up_process(child); |
| 489 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 491 | case PTRACE_SINGLESTEP: /* set the trap flag. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 493 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | break; |
| 495 | clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE); |
Andi Kleen | aa85b9a | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 496 | set_singlestep(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | child->exit_code = data; |
| 498 | /* give it a chance to run. */ |
| 499 | wake_up_process(child); |
| 500 | ret = 0; |
| 501 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
| 504 | if (!access_ok(VERIFY_WRITE, (unsigned __user *)data, |
| 505 | sizeof(struct user_regs_struct))) { |
| 506 | ret = -EIO; |
| 507 | break; |
| 508 | } |
| 509 | ret = 0; |
| 510 | for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) { |
| 511 | ret |= __put_user(getreg(child, ui),(unsigned long __user *) data); |
| 512 | data += sizeof(long); |
| 513 | } |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 518 | unsigned long tmp; |
| 519 | if (!access_ok(VERIFY_READ, (unsigned __user *)data, |
| 520 | sizeof(struct user_regs_struct))) { |
| 521 | ret = -EIO; |
| 522 | break; |
| 523 | } |
| 524 | ret = 0; |
| 525 | for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) { |
Andi Kleen | f49481b | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 526 | ret = __get_user(tmp, (unsigned long __user *) data); |
| 527 | if (ret) |
| 528 | break; |
| 529 | ret = putreg(child, ui, tmp); |
| 530 | if (ret) |
| 531 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | data += sizeof(long); |
| 533 | } |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */ |
| 538 | if (!access_ok(VERIFY_WRITE, (unsigned __user *)data, |
| 539 | sizeof(struct user_i387_struct))) { |
| 540 | ret = -EIO; |
| 541 | break; |
| 542 | } |
| 543 | ret = get_fpregs((struct user_i387_struct __user *)data, child); |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */ |
| 548 | if (!access_ok(VERIFY_READ, (unsigned __user *)data, |
| 549 | sizeof(struct user_i387_struct))) { |
| 550 | ret = -EIO; |
| 551 | break; |
| 552 | } |
| 553 | set_stopped_child_used_math(child); |
| 554 | ret = set_fpregs(child, (struct user_i387_struct __user *)data); |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | default: |
| 559 | ret = ptrace_request(child, request, addr, data); |
| 560 | break; |
| 561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | static void syscall_trace(struct pt_regs *regs) |
| 566 | { |
| 567 | |
| 568 | #if 0 |
| 569 | printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n", |
| 570 | current->comm, |
| 571 | regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0), |
| 572 | current_thread_info()->flags, current->ptrace); |
| 573 | #endif |
| 574 | |
| 575 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 576 | ? 0x80 : 0)); |
| 577 | /* |
| 578 | * this isn't the same as continuing with a signal, but it will do |
| 579 | * for normal use. strace only continues with a signal if the |
| 580 | * stopping signal is not SIGTRAP. -brl |
| 581 | */ |
| 582 | if (current->exit_code) { |
| 583 | send_sig(current->exit_code, current, 1); |
| 584 | current->exit_code = 0; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | asmlinkage void syscall_trace_enter(struct pt_regs *regs) |
| 589 | { |
| 590 | /* do the secure computing check first */ |
| 591 | secure_computing(regs->orig_rax); |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | if (test_thread_flag(TIF_SYSCALL_TRACE) |
| 594 | && (current->ptrace & PT_PTRACED)) |
| 595 | syscall_trace(regs); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 596 | |
David Woodhouse | 488f2ea | 2005-05-03 14:11:02 +0100 | [diff] [blame] | 597 | if (unlikely(current->audit_context)) { |
| 598 | if (test_thread_flag(TIF_IA32)) { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 599 | audit_syscall_entry(AUDIT_ARCH_I386, |
David Woodhouse | 488f2ea | 2005-05-03 14:11:02 +0100 | [diff] [blame] | 600 | regs->orig_rax, |
| 601 | regs->rbx, regs->rcx, |
| 602 | regs->rdx, regs->rsi); |
| 603 | } else { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 604 | audit_syscall_entry(AUDIT_ARCH_X86_64, |
David Woodhouse | 488f2ea | 2005-05-03 14:11:02 +0100 | [diff] [blame] | 605 | regs->orig_rax, |
| 606 | regs->rdi, regs->rsi, |
| 607 | regs->rdx, regs->r10); |
| 608 | } |
| 609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | asmlinkage void syscall_trace_leave(struct pt_regs *regs) |
| 613 | { |
| 614 | if (unlikely(current->audit_context)) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 615 | audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | if ((test_thread_flag(TIF_SYSCALL_TRACE) |
| 618 | || test_thread_flag(TIF_SINGLESTEP)) |
| 619 | && (current->ptrace & PT_PTRACED)) |
| 620 | syscall_trace(regs); |
| 621 | } |