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 | |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 40 | static long *pt_regs_access(struct pt_regs *regs, unsigned long regno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 42 | BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0); |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 43 | if (regno > FS) |
| 44 | --regno; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 45 | return ®s->bx + regno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static int putreg(struct task_struct *child, |
| 49 | unsigned long regno, unsigned long value) |
| 50 | { |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 51 | struct pt_regs *regs = task_pt_regs(child); |
| 52 | regno >>= 2; |
| 53 | switch (regno) { |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 54 | case GS: |
| 55 | if (value && (value & 3) != 3) |
| 56 | return -EIO; |
| 57 | child->thread.gs = value; |
Roland McGrath | 5fd4d16 | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 58 | if (child == current) |
| 59 | /* |
| 60 | * The user-mode %gs is not affected by |
| 61 | * kernel entry, so we must update the CPU. |
| 62 | */ |
| 63 | loadsegment(gs, value); |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 64 | return 0; |
| 65 | case DS: |
| 66 | case ES: |
| 67 | case FS: |
| 68 | if (value && (value & 3) != 3) |
| 69 | return -EIO; |
| 70 | value &= 0xffff; |
| 71 | break; |
| 72 | case SS: |
| 73 | case CS: |
| 74 | if ((value & 3) != 3) |
| 75 | return -EIO; |
| 76 | value &= 0xffff; |
| 77 | break; |
| 78 | case EFL: |
| 79 | value &= FLAG_MASK; |
| 80 | /* |
| 81 | * If the user value contains TF, mark that |
| 82 | * it was not "us" (the debugger) that set it. |
| 83 | * If not, make sure it stays set if we had. |
| 84 | */ |
| 85 | if (value & X86_EFLAGS_TF) |
| 86 | clear_tsk_thread_flag(child, TIF_FORCED_TF); |
| 87 | else if (test_tsk_thread_flag(child, TIF_FORCED_TF)) |
| 88 | value |= X86_EFLAGS_TF; |
| 89 | value |= regs->flags & ~FLAG_MASK; |
| 90 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 92 | *pt_regs_access(regs, regno) = value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 96 | static unsigned long getreg(struct task_struct *child, unsigned long regno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 98 | struct pt_regs *regs = task_pt_regs(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | unsigned long retval = ~0UL; |
| 100 | |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 101 | regno >>= 2; |
| 102 | switch (regno) { |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 103 | case EFL: |
| 104 | /* |
| 105 | * If the debugger set TF, hide it from the readout. |
| 106 | */ |
| 107 | retval = regs->flags; |
| 108 | if (test_tsk_thread_flag(child, TIF_FORCED_TF)) |
| 109 | retval &= ~X86_EFLAGS_TF; |
| 110 | break; |
| 111 | case GS: |
| 112 | retval = child->thread.gs; |
Roland McGrath | 5fd4d16 | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 113 | if (child == current) |
| 114 | savesegment(gs, retval); |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 115 | break; |
| 116 | case DS: |
| 117 | case ES: |
| 118 | case FS: |
| 119 | case SS: |
| 120 | case CS: |
| 121 | retval = 0xffff; |
| 122 | /* fall through */ |
| 123 | default: |
| 124 | retval &= *pt_regs_access(regs, regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | return retval; |
| 127 | } |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | /* |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 130 | * This function is trivial and will be inlined by the compiler. |
| 131 | * Having it separates the implementation details of debug |
| 132 | * registers from the interface details of ptrace. |
| 133 | */ |
| 134 | static unsigned long ptrace_get_debugreg(struct task_struct *child, int n) |
| 135 | { |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame^] | 136 | switch (n) { |
| 137 | case 0: return child->thread.debugreg0; |
| 138 | case 1: return child->thread.debugreg1; |
| 139 | case 2: return child->thread.debugreg2; |
| 140 | case 3: return child->thread.debugreg3; |
| 141 | case 6: return child->thread.debugreg6; |
| 142 | case 7: return child->thread.debugreg7; |
| 143 | } |
| 144 | return 0; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static int ptrace_set_debugreg(struct task_struct *child, |
| 148 | int n, unsigned long data) |
| 149 | { |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame^] | 150 | int i; |
| 151 | |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 152 | if (unlikely(n == 4 || n == 5)) |
| 153 | return -EIO; |
| 154 | |
| 155 | if (n < 4 && unlikely(data >= TASK_SIZE - 3)) |
| 156 | return -EIO; |
| 157 | |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame^] | 158 | switch (n) { |
| 159 | case 0: child->thread.debugreg0 = data; break; |
| 160 | case 1: child->thread.debugreg1 = data; break; |
| 161 | case 2: child->thread.debugreg2 = data; break; |
| 162 | case 3: child->thread.debugreg3 = data; break; |
| 163 | |
| 164 | case 6: |
| 165 | child->thread.debugreg6 = data; |
| 166 | break; |
| 167 | |
| 168 | case 7: |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 169 | /* |
| 170 | * Sanity-check data. Take one half-byte at once with |
| 171 | * check = (val >> (16 + 4*i)) & 0xf. It contains the |
| 172 | * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits |
| 173 | * 2 and 3 are LENi. Given a list of invalid values, |
| 174 | * we do mask |= 1 << invalid_value, so that |
| 175 | * (mask >> check) & 1 is a correct test for invalid |
| 176 | * values. |
| 177 | * |
| 178 | * R/Wi contains the type of the breakpoint / |
| 179 | * watchpoint, LENi contains the length of the watched |
| 180 | * data in the watchpoint case. |
| 181 | * |
| 182 | * The invalid values are: |
| 183 | * - LENi == 0x10 (undefined), so mask |= 0x0f00. |
| 184 | * - R/Wi == 0x10 (break on I/O reads or writes), so |
| 185 | * mask |= 0x4444. |
| 186 | * - R/Wi == 0x00 && LENi != 0x00, so we have mask |= |
| 187 | * 0x1110. |
| 188 | * |
| 189 | * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54. |
| 190 | * |
| 191 | * See the Intel Manual "System Programming Guide", |
| 192 | * 15.2.4 |
| 193 | * |
| 194 | * Note that LENi == 0x10 is defined on x86_64 in long |
| 195 | * mode (i.e. even for 32-bit userspace software, but |
| 196 | * 64-bit kernel), so the x86_64 mask value is 0x5454. |
| 197 | * See the AMD manual no. 24593 (AMD64 System Programming) |
| 198 | */ |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 199 | data &= ~DR_CONTROL_RESERVED; |
| 200 | for (i = 0; i < 4; i++) |
| 201 | if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1) |
| 202 | return -EIO; |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame^] | 203 | child->thread.debugreg7 = data; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 204 | if (data) |
| 205 | set_tsk_thread_flag(child, TIF_DEBUG); |
| 206 | else |
| 207 | clear_tsk_thread_flag(child, TIF_DEBUG); |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame^] | 208 | break; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | * Called by kernel/ptrace.c when detaching.. |
| 216 | * |
| 217 | * Make sure the single step bit is not set. |
| 218 | */ |
| 219 | void ptrace_disable(struct task_struct *child) |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 220 | { |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 221 | user_disable_single_step(child); |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 222 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 225 | 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] | 226 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | struct user * dummy = NULL; |
| 228 | int i, ret; |
| 229 | unsigned long __user *datap = (unsigned long __user *)data; |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | switch (request) { |
| 232 | /* when I and D space are separate, these will need to be fixed. */ |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 233 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 234 | case PTRACE_PEEKDATA: |
| 235 | ret = generic_ptrace_peekdata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | /* read the word at location addr in the USER area. */ |
| 239 | case PTRACE_PEEKUSR: { |
| 240 | unsigned long tmp; |
| 241 | |
| 242 | ret = -EIO; |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 243 | if ((addr & 3) || addr < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | addr > sizeof(struct user) - 3) |
| 245 | break; |
| 246 | |
| 247 | tmp = 0; /* Default return condition */ |
| 248 | if(addr < FRAME_SIZE*sizeof(long)) |
| 249 | tmp = getreg(child, addr); |
| 250 | if(addr >= (long) &dummy->u_debugreg[0] && |
| 251 | addr <= (long) &dummy->u_debugreg[7]){ |
| 252 | addr -= (long) &dummy->u_debugreg[0]; |
| 253 | addr = addr >> 2; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 254 | tmp = ptrace_get_debugreg(child, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | ret = put_user(tmp, datap); |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | /* when I and D space are separate, this will have to be fixed. */ |
| 261 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 262 | case PTRACE_POKEDATA: |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 263 | ret = generic_ptrace_pokedata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | break; |
| 265 | |
| 266 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
| 267 | ret = -EIO; |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 268 | if ((addr & 3) || addr < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | addr > sizeof(struct user) - 3) |
| 270 | break; |
| 271 | |
| 272 | if (addr < FRAME_SIZE*sizeof(long)) { |
| 273 | ret = putreg(child, addr, data); |
| 274 | break; |
| 275 | } |
| 276 | /* We need to be very careful here. We implicitly |
| 277 | want to modify a portion of the task_struct, and we |
| 278 | have to be selective about what portions we allow someone |
| 279 | to modify. */ |
| 280 | |
| 281 | ret = -EIO; |
| 282 | if(addr >= (long) &dummy->u_debugreg[0] && |
| 283 | addr <= (long) &dummy->u_debugreg[7]){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | addr -= (long) &dummy->u_debugreg; |
| 285 | addr = addr >> 2; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 286 | ret = ptrace_set_debugreg(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | break; |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
| 291 | if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) { |
| 292 | ret = -EIO; |
| 293 | break; |
| 294 | } |
| 295 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { |
| 296 | __put_user(getreg(child, i), datap); |
| 297 | datap++; |
| 298 | } |
| 299 | ret = 0; |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 304 | unsigned long tmp; |
| 305 | if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) { |
| 306 | ret = -EIO; |
| 307 | break; |
| 308 | } |
| 309 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { |
| 310 | __get_user(tmp, datap); |
| 311 | putreg(child, i, tmp); |
| 312 | datap++; |
| 313 | } |
| 314 | ret = 0; |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | case PTRACE_GETFPREGS: { /* Get the child FPU state. */ |
| 319 | if (!access_ok(VERIFY_WRITE, datap, |
| 320 | sizeof(struct user_i387_struct))) { |
| 321 | ret = -EIO; |
| 322 | break; |
| 323 | } |
| 324 | ret = 0; |
| 325 | if (!tsk_used_math(child)) |
| 326 | init_fpu(child); |
| 327 | get_fpregs((struct user_i387_struct __user *)data, child); |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | case PTRACE_SETFPREGS: { /* Set the child FPU state. */ |
| 332 | if (!access_ok(VERIFY_READ, datap, |
| 333 | sizeof(struct user_i387_struct))) { |
| 334 | ret = -EIO; |
| 335 | break; |
| 336 | } |
| 337 | set_stopped_child_used_math(child); |
| 338 | set_fpregs(child, (struct user_i387_struct __user *)data); |
| 339 | ret = 0; |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */ |
| 344 | if (!access_ok(VERIFY_WRITE, datap, |
| 345 | sizeof(struct user_fxsr_struct))) { |
| 346 | ret = -EIO; |
| 347 | break; |
| 348 | } |
| 349 | if (!tsk_used_math(child)) |
| 350 | init_fpu(child); |
| 351 | ret = get_fpxregs((struct user_fxsr_struct __user *)data, child); |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */ |
| 356 | if (!access_ok(VERIFY_READ, datap, |
| 357 | sizeof(struct user_fxsr_struct))) { |
| 358 | ret = -EIO; |
| 359 | break; |
| 360 | } |
| 361 | set_stopped_child_used_math(child); |
| 362 | ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data); |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | case PTRACE_GET_THREAD_AREA: |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 367 | if (addr < 0) |
| 368 | return -EIO; |
| 369 | ret = do_get_thread_area(child, addr, |
| 370 | (struct user_desc __user *) data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | break; |
| 372 | |
| 373 | case PTRACE_SET_THREAD_AREA: |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 374 | if (addr < 0) |
| 375 | return -EIO; |
| 376 | ret = do_set_thread_area(child, addr, |
| 377 | (struct user_desc __user *) data, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | default: |
| 381 | ret = ptrace_request(child, request, addr, data); |
| 382 | break; |
| 383 | } |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code) |
| 389 | { |
| 390 | struct siginfo info; |
| 391 | |
| 392 | tsk->thread.trap_no = 1; |
| 393 | tsk->thread.error_code = error_code; |
| 394 | |
| 395 | memset(&info, 0, sizeof(info)); |
| 396 | info.si_signo = SIGTRAP; |
| 397 | info.si_code = TRAP_BRKPT; |
| 398 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 399 | /* User-mode ip? */ |
| 400 | info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Simon Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 402 | /* Send us the fake SIGTRAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | force_sig_info(SIGTRAP, &info, tsk); |
| 404 | } |
| 405 | |
| 406 | /* notification of system call entry/exit |
| 407 | * - triggered by current->work.syscall_trace |
| 408 | */ |
| 409 | __attribute__((regparm(3))) |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 410 | int do_syscall_trace(struct pt_regs *regs, int entryexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 412 | int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU); |
| 413 | /* |
| 414 | * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall |
| 415 | * interception |
| 416 | */ |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 417 | int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP); |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 418 | int ret = 0; |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* do the secure computing check first */ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 421 | if (!entryexit) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 422 | secure_computing(regs->orig_ax); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 424 | if (unlikely(current->audit_context)) { |
| 425 | if (entryexit) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 426 | audit_syscall_exit(AUDITSC_RESULT(regs->ax), |
| 427 | regs->ax); |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 428 | /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only |
| 429 | * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is |
| 430 | * not used, entry.S will call us only on syscall exit, not |
| 431 | * entry; so when TIF_SYSCALL_AUDIT is used we must avoid |
| 432 | * calling send_sigtrap() on syscall entry. |
| 433 | * |
| 434 | * Note that when PTRACE_SYSEMU_SINGLESTEP is used, |
| 435 | * is_singlestep is false, despite his name, so we will still do |
| 436 | * the correct thing. |
| 437 | */ |
| 438 | else if (is_singlestep) |
| 439 | goto out; |
| 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | if (!(current->ptrace & PT_PTRACED)) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 443 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 445 | /* If a process stops on the 1st tracepoint with SYSCALL_TRACE |
| 446 | * and then is resumed with SYSEMU_SINGLESTEP, it will come in |
| 447 | * here. We have to check this and return */ |
| 448 | if (is_sysemu && entryexit) |
| 449 | return 0; |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /* Fake a debug trap */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 452 | if (is_singlestep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | send_sigtrap(current, regs, 0); |
| 454 | |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 455 | if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 456 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 459 | between a syscall stop and SIGTRAP delivery */ |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 460 | /* Note that the debugger could change the result of test_thread_flag!*/ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 461 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * this isn't the same as continuing with a signal, but it will do |
| 465 | * for normal use. strace only continues with a signal if the |
| 466 | * stopping signal is not SIGTRAP. -brl |
| 467 | */ |
| 468 | if (current->exit_code) { |
| 469 | send_sig(current->exit_code, current, 1); |
| 470 | current->exit_code = 0; |
| 471 | } |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 472 | ret = is_sysemu; |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 473 | out: |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 474 | if (unlikely(current->audit_context) && !entryexit) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 475 | audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax, |
| 476 | regs->bx, regs->cx, regs->dx, regs->si); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 477 | if (ret == 0) |
| 478 | return 0; |
| 479 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 480 | regs->orig_ax = -1; /* force skip of syscall restarting */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 481 | if (unlikely(current->audit_context)) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 482 | audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 483 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |