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 |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 5 | * |
| 6 | * BTS tracing |
| 7 | * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/ptrace.h> |
Roland McGrath | 91e7b70 | 2008-01-30 13:31:52 +0100 | [diff] [blame] | 16 | #include <linux/regset.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/user.h> |
Roland McGrath | 070459d | 2008-01-30 13:31:53 +0100 | [diff] [blame] | 18 | #include <linux/elf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/security.h> |
| 20 | #include <linux/audit.h> |
| 21 | #include <linux/seccomp.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 22 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <asm/uaccess.h> |
| 25 | #include <asm/pgtable.h> |
| 26 | #include <asm/system.h> |
| 27 | #include <asm/processor.h> |
| 28 | #include <asm/i387.h> |
| 29 | #include <asm/debugreg.h> |
| 30 | #include <asm/ldt.h> |
| 31 | #include <asm/desc.h> |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 32 | #include <asm/prctl.h> |
| 33 | #include <asm/proto.h> |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 34 | #include <asm/ds.h> |
| 35 | |
Roland McGrath | 070459d | 2008-01-30 13:31:53 +0100 | [diff] [blame] | 36 | #include "tls.h" |
| 37 | |
| 38 | enum x86_regset { |
| 39 | REGSET_GENERAL, |
| 40 | REGSET_FP, |
| 41 | REGSET_XFP, |
| 42 | REGSET_TLS, |
| 43 | }; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 44 | |
| 45 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * does not yet catch signals sent when the child dies. |
| 47 | * in exit.c or in signal.c. |
| 48 | */ |
| 49 | |
Chuck Ebbert | 9f155b9 | 2006-01-05 23:11:29 -0500 | [diff] [blame] | 50 | /* |
| 51 | * Determines which flags the user has access to [1 = access, 0 = no access]. |
Chuck Ebbert | 9f155b9 | 2006-01-05 23:11:29 -0500 | [diff] [blame] | 52 | */ |
Roland McGrath | e39c289 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 53 | #define FLAG_MASK_32 ((unsigned long) \ |
| 54 | (X86_EFLAGS_CF | X86_EFLAGS_PF | \ |
| 55 | X86_EFLAGS_AF | X86_EFLAGS_ZF | \ |
| 56 | X86_EFLAGS_SF | X86_EFLAGS_TF | \ |
| 57 | X86_EFLAGS_DF | X86_EFLAGS_OF | \ |
| 58 | X86_EFLAGS_RF | X86_EFLAGS_AC)) |
| 59 | |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 60 | /* |
| 61 | * Determines whether a value may be installed in a segment register. |
| 62 | */ |
| 63 | static inline bool invalid_selector(u16 value) |
| 64 | { |
| 65 | return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL); |
| 66 | } |
| 67 | |
| 68 | #ifdef CONFIG_X86_32 |
| 69 | |
Roland McGrath | e39c289 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 70 | #define FLAG_MASK FLAG_MASK_32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 72 | static long *pt_regs_access(struct pt_regs *regs, unsigned long regno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 74 | BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0); |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 75 | regno >>= 2; |
Roland McGrath | 62a97d4 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 76 | if (regno > FS) |
| 77 | --regno; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 78 | return ®s->bx + regno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 81 | static u16 get_segment_reg(struct task_struct *task, unsigned long offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 83 | /* |
| 84 | * Returning the value truncates it to 16 bits. |
| 85 | */ |
| 86 | unsigned int retval; |
| 87 | if (offset != offsetof(struct user_regs_struct, gs)) |
| 88 | retval = *pt_regs_access(task_pt_regs(task), offset); |
| 89 | else { |
| 90 | retval = task->thread.gs; |
| 91 | if (task == current) |
| 92 | savesegment(gs, retval); |
| 93 | } |
| 94 | return retval; |
| 95 | } |
| 96 | |
| 97 | static int set_segment_reg(struct task_struct *task, |
| 98 | unsigned long offset, u16 value) |
| 99 | { |
| 100 | /* |
| 101 | * The value argument was already truncated to 16 bits. |
| 102 | */ |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 103 | if (invalid_selector(value)) |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 104 | return -EIO; |
| 105 | |
Roland McGrath | c63855d | 2008-02-06 22:39:44 +0100 | [diff] [blame] | 106 | /* |
| 107 | * For %cs and %ss we cannot permit a null selector. |
| 108 | * We can permit a bogus selector as long as it has USER_RPL. |
| 109 | * Null selectors are fine for other segment registers, but |
| 110 | * we will never get back to user mode with invalid %cs or %ss |
| 111 | * and will take the trap in iret instead. Much code relies |
| 112 | * on user_mode() to distinguish a user trap frame (which can |
| 113 | * safely use invalid selectors) from a kernel trap frame. |
| 114 | */ |
| 115 | switch (offset) { |
| 116 | case offsetof(struct user_regs_struct, cs): |
| 117 | case offsetof(struct user_regs_struct, ss): |
| 118 | if (unlikely(value == 0)) |
| 119 | return -EIO; |
| 120 | |
| 121 | default: |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 122 | *pt_regs_access(task_pt_regs(task), offset) = value; |
Roland McGrath | c63855d | 2008-02-06 22:39:44 +0100 | [diff] [blame] | 123 | break; |
| 124 | |
| 125 | case offsetof(struct user_regs_struct, gs): |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 126 | task->thread.gs = value; |
| 127 | if (task == current) |
Roland McGrath | 5fd4d16 | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 128 | /* |
| 129 | * The user-mode %gs is not affected by |
| 130 | * kernel entry, so we must update the CPU. |
| 131 | */ |
| 132 | loadsegment(gs, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 138 | static unsigned long debugreg_addr_limit(struct task_struct *task) |
| 139 | { |
| 140 | return TASK_SIZE - 3; |
| 141 | } |
| 142 | |
| 143 | #else /* CONFIG_X86_64 */ |
| 144 | |
| 145 | #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT) |
| 146 | |
| 147 | static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset) |
| 148 | { |
| 149 | BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0); |
| 150 | return ®s->r15 + (offset / sizeof(regs->r15)); |
| 151 | } |
| 152 | |
| 153 | static u16 get_segment_reg(struct task_struct *task, unsigned long offset) |
| 154 | { |
| 155 | /* |
| 156 | * Returning the value truncates it to 16 bits. |
| 157 | */ |
| 158 | unsigned int seg; |
| 159 | |
| 160 | switch (offset) { |
| 161 | case offsetof(struct user_regs_struct, fs): |
| 162 | if (task == current) { |
| 163 | /* Older gas can't assemble movq %?s,%r?? */ |
| 164 | asm("movl %%fs,%0" : "=r" (seg)); |
| 165 | return seg; |
| 166 | } |
| 167 | return task->thread.fsindex; |
| 168 | case offsetof(struct user_regs_struct, gs): |
| 169 | if (task == current) { |
| 170 | asm("movl %%gs,%0" : "=r" (seg)); |
| 171 | return seg; |
| 172 | } |
| 173 | return task->thread.gsindex; |
| 174 | case offsetof(struct user_regs_struct, ds): |
| 175 | if (task == current) { |
| 176 | asm("movl %%ds,%0" : "=r" (seg)); |
| 177 | return seg; |
| 178 | } |
| 179 | return task->thread.ds; |
| 180 | case offsetof(struct user_regs_struct, es): |
| 181 | if (task == current) { |
| 182 | asm("movl %%es,%0" : "=r" (seg)); |
| 183 | return seg; |
| 184 | } |
| 185 | return task->thread.es; |
| 186 | |
| 187 | case offsetof(struct user_regs_struct, cs): |
| 188 | case offsetof(struct user_regs_struct, ss): |
| 189 | break; |
| 190 | } |
| 191 | return *pt_regs_access(task_pt_regs(task), offset); |
| 192 | } |
| 193 | |
| 194 | static int set_segment_reg(struct task_struct *task, |
| 195 | unsigned long offset, u16 value) |
| 196 | { |
| 197 | /* |
| 198 | * The value argument was already truncated to 16 bits. |
| 199 | */ |
| 200 | if (invalid_selector(value)) |
| 201 | return -EIO; |
| 202 | |
| 203 | switch (offset) { |
| 204 | case offsetof(struct user_regs_struct,fs): |
| 205 | /* |
| 206 | * If this is setting fs as for normal 64-bit use but |
| 207 | * setting fs_base has implicitly changed it, leave it. |
| 208 | */ |
| 209 | if ((value == FS_TLS_SEL && task->thread.fsindex == 0 && |
| 210 | task->thread.fs != 0) || |
| 211 | (value == 0 && task->thread.fsindex == FS_TLS_SEL && |
| 212 | task->thread.fs == 0)) |
| 213 | break; |
| 214 | task->thread.fsindex = value; |
| 215 | if (task == current) |
| 216 | loadsegment(fs, task->thread.fsindex); |
| 217 | break; |
| 218 | case offsetof(struct user_regs_struct,gs): |
| 219 | /* |
| 220 | * If this is setting gs as for normal 64-bit use but |
| 221 | * setting gs_base has implicitly changed it, leave it. |
| 222 | */ |
| 223 | if ((value == GS_TLS_SEL && task->thread.gsindex == 0 && |
| 224 | task->thread.gs != 0) || |
| 225 | (value == 0 && task->thread.gsindex == GS_TLS_SEL && |
| 226 | task->thread.gs == 0)) |
| 227 | break; |
| 228 | task->thread.gsindex = value; |
| 229 | if (task == current) |
| 230 | load_gs_index(task->thread.gsindex); |
| 231 | break; |
| 232 | case offsetof(struct user_regs_struct,ds): |
| 233 | task->thread.ds = value; |
| 234 | if (task == current) |
| 235 | loadsegment(ds, task->thread.ds); |
| 236 | break; |
| 237 | case offsetof(struct user_regs_struct,es): |
| 238 | task->thread.es = value; |
| 239 | if (task == current) |
| 240 | loadsegment(es, task->thread.es); |
| 241 | break; |
| 242 | |
| 243 | /* |
| 244 | * Can't actually change these in 64-bit mode. |
| 245 | */ |
| 246 | case offsetof(struct user_regs_struct,cs): |
Roland McGrath | c63855d | 2008-02-06 22:39:44 +0100 | [diff] [blame] | 247 | if (unlikely(value == 0)) |
| 248 | return -EIO; |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 249 | #ifdef CONFIG_IA32_EMULATION |
| 250 | if (test_tsk_thread_flag(task, TIF_IA32)) |
| 251 | task_pt_regs(task)->cs = value; |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 252 | #endif |
Roland McGrath | cb757c4 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 253 | break; |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 254 | case offsetof(struct user_regs_struct,ss): |
Roland McGrath | c63855d | 2008-02-06 22:39:44 +0100 | [diff] [blame] | 255 | if (unlikely(value == 0)) |
| 256 | return -EIO; |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 257 | #ifdef CONFIG_IA32_EMULATION |
| 258 | if (test_tsk_thread_flag(task, TIF_IA32)) |
| 259 | task_pt_regs(task)->ss = value; |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 260 | #endif |
Roland McGrath | cb757c4 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 261 | break; |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static unsigned long debugreg_addr_limit(struct task_struct *task) |
| 268 | { |
| 269 | #ifdef CONFIG_IA32_EMULATION |
| 270 | if (test_tsk_thread_flag(task, TIF_IA32)) |
| 271 | return IA32_PAGE_OFFSET - 3; |
| 272 | #endif |
| 273 | return TASK_SIZE64 - 7; |
| 274 | } |
| 275 | |
| 276 | #endif /* CONFIG_X86_32 */ |
| 277 | |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 278 | static unsigned long get_flags(struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 280 | unsigned long retval = task_pt_regs(task)->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 282 | /* |
| 283 | * If the debugger set TF, hide it from the readout. |
| 284 | */ |
| 285 | if (test_tsk_thread_flag(task, TIF_FORCED_TF)) |
| 286 | retval &= ~X86_EFLAGS_TF; |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return retval; |
| 289 | } |
| 290 | |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 291 | static int set_flags(struct task_struct *task, unsigned long value) |
| 292 | { |
| 293 | struct pt_regs *regs = task_pt_regs(task); |
| 294 | |
| 295 | /* |
| 296 | * If the user value contains TF, mark that |
| 297 | * it was not "us" (the debugger) that set it. |
| 298 | * If not, make sure it stays set if we had. |
| 299 | */ |
| 300 | if (value & X86_EFLAGS_TF) |
| 301 | clear_tsk_thread_flag(task, TIF_FORCED_TF); |
| 302 | else if (test_tsk_thread_flag(task, TIF_FORCED_TF)) |
| 303 | value |= X86_EFLAGS_TF; |
| 304 | |
| 305 | regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int putreg(struct task_struct *child, |
| 311 | unsigned long offset, unsigned long value) |
| 312 | { |
| 313 | switch (offset) { |
| 314 | case offsetof(struct user_regs_struct, cs): |
| 315 | case offsetof(struct user_regs_struct, ds): |
| 316 | case offsetof(struct user_regs_struct, es): |
| 317 | case offsetof(struct user_regs_struct, fs): |
| 318 | case offsetof(struct user_regs_struct, gs): |
| 319 | case offsetof(struct user_regs_struct, ss): |
| 320 | return set_segment_reg(child, offset, value); |
| 321 | |
| 322 | case offsetof(struct user_regs_struct, flags): |
| 323 | return set_flags(child, value); |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 324 | |
| 325 | #ifdef CONFIG_X86_64 |
Roland McGrath | 84c6f60 | 2008-03-07 14:56:02 -0800 | [diff] [blame] | 326 | /* |
| 327 | * Orig_ax is really just a flag with small positive and |
| 328 | * negative values, so make sure to always sign-extend it |
| 329 | * from 32 bits so that it works correctly regardless of |
| 330 | * whether we come from a 32-bit environment or not. |
| 331 | */ |
| 332 | case offsetof(struct user_regs_struct, orig_ax): |
| 333 | value = (long) (s32) value; |
| 334 | break; |
| 335 | |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 336 | case offsetof(struct user_regs_struct,fs_base): |
| 337 | if (value >= TASK_SIZE_OF(child)) |
| 338 | return -EIO; |
| 339 | /* |
| 340 | * When changing the segment base, use do_arch_prctl |
| 341 | * to set either thread.fs or thread.fsindex and the |
| 342 | * corresponding GDT slot. |
| 343 | */ |
| 344 | if (child->thread.fs != value) |
| 345 | return do_arch_prctl(child, ARCH_SET_FS, value); |
| 346 | return 0; |
| 347 | case offsetof(struct user_regs_struct,gs_base): |
| 348 | /* |
| 349 | * Exactly the same here as the %fs handling above. |
| 350 | */ |
| 351 | if (value >= TASK_SIZE_OF(child)) |
| 352 | return -EIO; |
| 353 | if (child->thread.gs != value) |
| 354 | return do_arch_prctl(child, ARCH_SET_GS, value); |
| 355 | return 0; |
| 356 | #endif |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | *pt_regs_access(task_pt_regs(child), offset) = value; |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static unsigned long getreg(struct task_struct *task, unsigned long offset) |
| 364 | { |
| 365 | switch (offset) { |
| 366 | case offsetof(struct user_regs_struct, cs): |
| 367 | case offsetof(struct user_regs_struct, ds): |
| 368 | case offsetof(struct user_regs_struct, es): |
| 369 | case offsetof(struct user_regs_struct, fs): |
| 370 | case offsetof(struct user_regs_struct, gs): |
| 371 | case offsetof(struct user_regs_struct, ss): |
| 372 | return get_segment_reg(task, offset); |
| 373 | |
| 374 | case offsetof(struct user_regs_struct, flags): |
| 375 | return get_flags(task); |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 376 | |
| 377 | #ifdef CONFIG_X86_64 |
| 378 | case offsetof(struct user_regs_struct, fs_base): { |
| 379 | /* |
| 380 | * do_arch_prctl may have used a GDT slot instead of |
| 381 | * the MSR. To userland, it appears the same either |
| 382 | * way, except the %fs segment selector might not be 0. |
| 383 | */ |
| 384 | unsigned int seg = task->thread.fsindex; |
| 385 | if (task->thread.fs != 0) |
| 386 | return task->thread.fs; |
| 387 | if (task == current) |
| 388 | asm("movl %%fs,%0" : "=r" (seg)); |
| 389 | if (seg != FS_TLS_SEL) |
| 390 | return 0; |
| 391 | return get_desc_base(&task->thread.tls_array[FS_TLS]); |
| 392 | } |
| 393 | case offsetof(struct user_regs_struct, gs_base): { |
| 394 | /* |
| 395 | * Exactly the same here as the %fs handling above. |
| 396 | */ |
| 397 | unsigned int seg = task->thread.gsindex; |
| 398 | if (task->thread.gs != 0) |
| 399 | return task->thread.gs; |
| 400 | if (task == current) |
| 401 | asm("movl %%gs,%0" : "=r" (seg)); |
| 402 | if (seg != GS_TLS_SEL) |
| 403 | return 0; |
| 404 | return get_desc_base(&task->thread.tls_array[GS_TLS]); |
| 405 | } |
| 406 | #endif |
Roland McGrath | 06ee1b6 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return *pt_regs_access(task_pt_regs(task), offset); |
| 410 | } |
| 411 | |
Roland McGrath | 91e7b70 | 2008-01-30 13:31:52 +0100 | [diff] [blame] | 412 | static int genregs_get(struct task_struct *target, |
| 413 | const struct user_regset *regset, |
| 414 | unsigned int pos, unsigned int count, |
| 415 | void *kbuf, void __user *ubuf) |
| 416 | { |
| 417 | if (kbuf) { |
| 418 | unsigned long *k = kbuf; |
| 419 | while (count > 0) { |
| 420 | *k++ = getreg(target, pos); |
| 421 | count -= sizeof(*k); |
| 422 | pos += sizeof(*k); |
| 423 | } |
| 424 | } else { |
| 425 | unsigned long __user *u = ubuf; |
| 426 | while (count > 0) { |
| 427 | if (__put_user(getreg(target, pos), u++)) |
| 428 | return -EFAULT; |
| 429 | count -= sizeof(*u); |
| 430 | pos += sizeof(*u); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int genregs_set(struct task_struct *target, |
| 438 | const struct user_regset *regset, |
| 439 | unsigned int pos, unsigned int count, |
| 440 | const void *kbuf, const void __user *ubuf) |
| 441 | { |
| 442 | int ret = 0; |
| 443 | if (kbuf) { |
| 444 | const unsigned long *k = kbuf; |
| 445 | while (count > 0 && !ret) { |
| 446 | ret = putreg(target, pos, *k++); |
| 447 | count -= sizeof(*k); |
| 448 | pos += sizeof(*k); |
| 449 | } |
| 450 | } else { |
| 451 | const unsigned long __user *u = ubuf; |
| 452 | while (count > 0 && !ret) { |
| 453 | unsigned long word; |
| 454 | ret = __get_user(word, u++); |
| 455 | if (ret) |
| 456 | break; |
| 457 | ret = putreg(target, pos, word); |
| 458 | count -= sizeof(*u); |
| 459 | pos += sizeof(*u); |
| 460 | } |
| 461 | } |
| 462 | return ret; |
| 463 | } |
| 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | /* |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 466 | * This function is trivial and will be inlined by the compiler. |
| 467 | * Having it separates the implementation details of debug |
| 468 | * registers from the interface details of ptrace. |
| 469 | */ |
| 470 | static unsigned long ptrace_get_debugreg(struct task_struct *child, int n) |
| 471 | { |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 472 | switch (n) { |
| 473 | case 0: return child->thread.debugreg0; |
| 474 | case 1: return child->thread.debugreg1; |
| 475 | case 2: return child->thread.debugreg2; |
| 476 | case 3: return child->thread.debugreg3; |
| 477 | case 6: return child->thread.debugreg6; |
| 478 | case 7: return child->thread.debugreg7; |
| 479 | } |
| 480 | return 0; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int ptrace_set_debugreg(struct task_struct *child, |
| 484 | int n, unsigned long data) |
| 485 | { |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 486 | int i; |
| 487 | |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 488 | if (unlikely(n == 4 || n == 5)) |
| 489 | return -EIO; |
| 490 | |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 491 | if (n < 4 && unlikely(data >= debugreg_addr_limit(child))) |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 492 | return -EIO; |
| 493 | |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 494 | switch (n) { |
| 495 | case 0: child->thread.debugreg0 = data; break; |
| 496 | case 1: child->thread.debugreg1 = data; break; |
| 497 | case 2: child->thread.debugreg2 = data; break; |
| 498 | case 3: child->thread.debugreg3 = data; break; |
| 499 | |
| 500 | case 6: |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 501 | if ((data & ~0xffffffffUL) != 0) |
| 502 | return -EIO; |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 503 | child->thread.debugreg6 = data; |
| 504 | break; |
| 505 | |
| 506 | case 7: |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 507 | /* |
| 508 | * Sanity-check data. Take one half-byte at once with |
| 509 | * check = (val >> (16 + 4*i)) & 0xf. It contains the |
| 510 | * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits |
| 511 | * 2 and 3 are LENi. Given a list of invalid values, |
| 512 | * we do mask |= 1 << invalid_value, so that |
| 513 | * (mask >> check) & 1 is a correct test for invalid |
| 514 | * values. |
| 515 | * |
| 516 | * R/Wi contains the type of the breakpoint / |
| 517 | * watchpoint, LENi contains the length of the watched |
| 518 | * data in the watchpoint case. |
| 519 | * |
| 520 | * The invalid values are: |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 521 | * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit] |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 522 | * - R/Wi == 0x10 (break on I/O reads or writes), so |
| 523 | * mask |= 0x4444. |
| 524 | * - R/Wi == 0x00 && LENi != 0x00, so we have mask |= |
| 525 | * 0x1110. |
| 526 | * |
| 527 | * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54. |
| 528 | * |
| 529 | * See the Intel Manual "System Programming Guide", |
| 530 | * 15.2.4 |
| 531 | * |
| 532 | * Note that LENi == 0x10 is defined on x86_64 in long |
| 533 | * mode (i.e. even for 32-bit userspace software, but |
| 534 | * 64-bit kernel), so the x86_64 mask value is 0x5454. |
| 535 | * See the AMD manual no. 24593 (AMD64 System Programming) |
| 536 | */ |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 537 | #ifdef CONFIG_X86_32 |
| 538 | #define DR7_MASK 0x5f54 |
| 539 | #else |
| 540 | #define DR7_MASK 0x5554 |
| 541 | #endif |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 542 | data &= ~DR_CONTROL_RESERVED; |
| 543 | for (i = 0; i < 4; i++) |
Roland McGrath | 2047b08 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 544 | if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1) |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 545 | return -EIO; |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 546 | child->thread.debugreg7 = data; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 547 | if (data) |
| 548 | set_tsk_thread_flag(child, TIF_DEBUG); |
| 549 | else |
| 550 | clear_tsk_thread_flag(child, TIF_DEBUG); |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 551 | break; |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 557 | #ifdef CONFIG_X86_PTRACE_BTS |
| 558 | /* |
| 559 | * The configuration for a particular BTS hardware implementation. |
| 560 | */ |
| 561 | struct bts_configuration { |
| 562 | /* the size of a BTS record in bytes; at most BTS_MAX_RECORD_SIZE */ |
| 563 | unsigned char sizeof_bts; |
| 564 | /* the size of a field in the BTS record in bytes */ |
| 565 | unsigned char sizeof_field; |
| 566 | /* a bitmask to enable/disable BTS in DEBUGCTL MSR */ |
| 567 | unsigned long debugctl_mask; |
| 568 | }; |
| 569 | static struct bts_configuration bts_cfg; |
Ingo Molnar | b4ef95d | 2008-02-26 09:40:27 +0100 | [diff] [blame] | 570 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 571 | #define BTS_MAX_RECORD_SIZE (8 * 3) |
| 572 | |
| 573 | |
| 574 | /* |
| 575 | * Branch Trace Store (BTS) uses the following format. Different |
| 576 | * architectures vary in the size of those fields. |
| 577 | * - source linear address |
| 578 | * - destination linear address |
| 579 | * - flags |
| 580 | * |
| 581 | * Later architectures use 64bit pointers throughout, whereas earlier |
| 582 | * architectures use 32bit pointers in 32bit mode. |
| 583 | * |
| 584 | * We compute the base address for the first 8 fields based on: |
| 585 | * - the field size stored in the DS configuration |
| 586 | * - the relative field position |
| 587 | * |
| 588 | * In order to store additional information in the BTS buffer, we use |
| 589 | * a special source address to indicate that the record requires |
| 590 | * special interpretation. |
| 591 | * |
| 592 | * Netburst indicated via a bit in the flags field whether the branch |
| 593 | * was predicted; this is ignored. |
| 594 | */ |
| 595 | |
| 596 | enum bts_field { |
| 597 | bts_from = 0, |
| 598 | bts_to, |
| 599 | bts_flags, |
| 600 | |
| 601 | bts_escape = (unsigned long)-1, |
| 602 | bts_qual = bts_to, |
| 603 | bts_jiffies = bts_flags |
| 604 | }; |
| 605 | |
| 606 | static inline unsigned long bts_get(const char *base, enum bts_field field) |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 607 | { |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 608 | base += (bts_cfg.sizeof_field * field); |
| 609 | return *(unsigned long *)base; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 610 | } |
| 611 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 612 | static inline void bts_set(char *base, enum bts_field field, unsigned long val) |
| 613 | { |
| 614 | base += (bts_cfg.sizeof_field * field);; |
| 615 | (*(unsigned long *)base) = val; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Translate a BTS record from the raw format into the bts_struct format |
| 620 | * |
| 621 | * out (out): bts_struct interpretation |
| 622 | * raw: raw BTS record |
| 623 | */ |
| 624 | static void ptrace_bts_translate_record(struct bts_struct *out, const void *raw) |
| 625 | { |
| 626 | memset(out, 0, sizeof(*out)); |
| 627 | if (bts_get(raw, bts_from) == bts_escape) { |
| 628 | out->qualifier = bts_get(raw, bts_qual); |
| 629 | out->variant.jiffies = bts_get(raw, bts_jiffies); |
| 630 | } else { |
| 631 | out->qualifier = BTS_BRANCH; |
| 632 | out->variant.lbr.from_ip = bts_get(raw, bts_from); |
| 633 | out->variant.lbr.to_ip = bts_get(raw, bts_to); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | static int ptrace_bts_read_record(struct task_struct *child, size_t index, |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 638 | struct bts_struct __user *out) |
| 639 | { |
| 640 | struct bts_struct ret; |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 641 | const void *bts_record; |
| 642 | size_t bts_index, bts_end; |
| 643 | int error; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 644 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 645 | error = ds_get_bts_end(child, &bts_end); |
| 646 | if (error < 0) |
| 647 | return error; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 648 | |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 649 | if (bts_end <= index) |
Markus Metzger | e4811f2 | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 650 | return -EINVAL; |
| 651 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 652 | error = ds_get_bts_index(child, &bts_index); |
| 653 | if (error < 0) |
| 654 | return error; |
Markus Metzger | e4811f2 | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 655 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 656 | /* translate the ptrace bts index into the ds bts index */ |
| 657 | bts_index += bts_end - (index + 1); |
| 658 | if (bts_end <= bts_index) |
| 659 | bts_index -= bts_end; |
| 660 | |
| 661 | error = ds_access_bts(child, bts_index, &bts_record); |
| 662 | if (error < 0) |
| 663 | return error; |
| 664 | |
| 665 | ptrace_bts_translate_record(&ret, bts_record); |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 666 | |
| 667 | if (copy_to_user(out, &ret, sizeof(ret))) |
| 668 | return -EFAULT; |
| 669 | |
| 670 | return sizeof(ret); |
| 671 | } |
| 672 | |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 673 | static int ptrace_bts_drain(struct task_struct *child, |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 674 | long size, |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 675 | struct bts_struct __user *out) |
| 676 | { |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 677 | struct bts_struct ret; |
| 678 | const unsigned char *raw; |
| 679 | size_t end, i; |
| 680 | int error; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 681 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 682 | error = ds_get_bts_index(child, &end); |
| 683 | if (error < 0) |
| 684 | return error; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 685 | |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 686 | if (size < (end * sizeof(struct bts_struct))) |
| 687 | return -EIO; |
| 688 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 689 | error = ds_access_bts(child, 0, (const void **)&raw); |
| 690 | if (error < 0) |
| 691 | return error; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 692 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 693 | for (i = 0; i < end; i++, out++, raw += bts_cfg.sizeof_bts) { |
| 694 | ptrace_bts_translate_record(&ret, raw); |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 695 | |
| 696 | if (copy_to_user(out, &ret, sizeof(ret))) |
| 697 | return -EFAULT; |
| 698 | } |
| 699 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 700 | error = ds_clear_bts(child); |
| 701 | if (error < 0) |
| 702 | return error; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 703 | |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 704 | return end; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 707 | static void ptrace_bts_ovfl(struct task_struct *child) |
| 708 | { |
| 709 | send_sig(child->thread.bts_ovfl_signal, child, 0); |
| 710 | } |
| 711 | |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 712 | static int ptrace_bts_config(struct task_struct *child, |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 713 | long cfg_size, |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 714 | const struct ptrace_bts_config __user *ucfg) |
| 715 | { |
| 716 | struct ptrace_bts_config cfg; |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 717 | int error = 0; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 718 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 719 | error = -EOPNOTSUPP; |
| 720 | if (!bts_cfg.sizeof_bts) |
| 721 | goto errout; |
| 722 | |
| 723 | error = -EIO; |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 724 | if (cfg_size < sizeof(cfg)) |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 725 | goto errout; |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 726 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 727 | error = -EFAULT; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 728 | if (copy_from_user(&cfg, ucfg, sizeof(cfg))) |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 729 | goto errout; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 730 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 731 | error = -EINVAL; |
| 732 | if ((cfg.flags & PTRACE_BTS_O_SIGNAL) && |
| 733 | !(cfg.flags & PTRACE_BTS_O_ALLOC)) |
| 734 | goto errout; |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 735 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 736 | if (cfg.flags & PTRACE_BTS_O_ALLOC) { |
| 737 | ds_ovfl_callback_t ovfl = 0; |
| 738 | unsigned int sig = 0; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 739 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 740 | /* we ignore the error in case we were not tracing child */ |
| 741 | (void)ds_release_bts(child); |
| 742 | |
| 743 | if (cfg.flags & PTRACE_BTS_O_SIGNAL) { |
| 744 | if (!cfg.signal) |
| 745 | goto errout; |
| 746 | |
| 747 | sig = cfg.signal; |
| 748 | ovfl = ptrace_bts_ovfl; |
| 749 | } |
| 750 | |
| 751 | error = ds_request_bts(child, /* base = */ 0, cfg.size, ovfl); |
| 752 | if (error < 0) |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 753 | goto errout; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 754 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 755 | child->thread.bts_ovfl_signal = sig; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 758 | error = -EINVAL; |
| 759 | if (!child->thread.ds_ctx && cfg.flags) |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 760 | goto errout; |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 761 | |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 762 | if (cfg.flags & PTRACE_BTS_O_TRACE) |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 763 | child->thread.debugctlmsr |= bts_cfg.debugctl_mask; |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 764 | else |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 765 | child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 766 | |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 767 | if (cfg.flags & PTRACE_BTS_O_SCHED) |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 768 | set_tsk_thread_flag(child, TIF_BTS_TRACE_TS); |
| 769 | else |
| 770 | clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS); |
| 771 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 772 | error = sizeof(cfg); |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 773 | |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 774 | out: |
| 775 | if (child->thread.debugctlmsr) |
| 776 | set_tsk_thread_flag(child, TIF_DEBUGCTLMSR); |
| 777 | else |
| 778 | clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR); |
| 779 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 780 | return error; |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 781 | |
| 782 | errout: |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 783 | child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask; |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 784 | clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS); |
| 785 | goto out; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 788 | static int ptrace_bts_status(struct task_struct *child, |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 789 | long cfg_size, |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 790 | struct ptrace_bts_config __user *ucfg) |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 791 | { |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 792 | struct ptrace_bts_config cfg; |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 793 | size_t end; |
| 794 | const void *base, *max; |
| 795 | int error; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 796 | |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 797 | if (cfg_size < sizeof(cfg)) |
| 798 | return -EIO; |
| 799 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 800 | error = ds_get_bts_end(child, &end); |
| 801 | if (error < 0) |
| 802 | return error; |
| 803 | |
| 804 | error = ds_access_bts(child, /* index = */ 0, &base); |
| 805 | if (error < 0) |
| 806 | return error; |
| 807 | |
| 808 | error = ds_access_bts(child, /* index = */ end, &max); |
| 809 | if (error < 0) |
| 810 | return error; |
| 811 | |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 812 | memset(&cfg, 0, sizeof(cfg)); |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 813 | cfg.size = (max - base); |
| 814 | cfg.signal = child->thread.bts_ovfl_signal; |
Markus Metzger | 87e8407 | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 815 | cfg.bts_size = sizeof(struct bts_struct); |
| 816 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 817 | if (cfg.signal) |
| 818 | cfg.flags |= PTRACE_BTS_O_SIGNAL; |
| 819 | |
| 820 | if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) && |
| 821 | child->thread.debugctlmsr & bts_cfg.debugctl_mask) |
| 822 | cfg.flags |= PTRACE_BTS_O_TRACE; |
| 823 | |
| 824 | if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS)) |
| 825 | cfg.flags |= PTRACE_BTS_O_SCHED; |
| 826 | |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 827 | if (copy_to_user(ucfg, &cfg, sizeof(cfg))) |
| 828 | return -EFAULT; |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 829 | |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 830 | return sizeof(cfg); |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 831 | } |
| 832 | |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 833 | static int ptrace_bts_write_record(struct task_struct *child, |
| 834 | const struct bts_struct *in) |
| 835 | { |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 836 | unsigned char bts_record[BTS_MAX_RECORD_SIZE]; |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 837 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 838 | BUG_ON(BTS_MAX_RECORD_SIZE < bts_cfg.sizeof_bts); |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 839 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 840 | memset(bts_record, 0, bts_cfg.sizeof_bts); |
| 841 | switch (in->qualifier) { |
| 842 | case BTS_INVALID: |
| 843 | break; |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 844 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 845 | case BTS_BRANCH: |
| 846 | bts_set(bts_record, bts_from, in->variant.lbr.from_ip); |
| 847 | bts_set(bts_record, bts_to, in->variant.lbr.to_ip); |
| 848 | break; |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 849 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 850 | case BTS_TASK_ARRIVES: |
| 851 | case BTS_TASK_DEPARTS: |
| 852 | bts_set(bts_record, bts_from, bts_escape); |
| 853 | bts_set(bts_record, bts_qual, in->qualifier); |
| 854 | bts_set(bts_record, bts_jiffies, in->variant.jiffies); |
| 855 | break; |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 856 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 857 | default: |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 858 | return -EINVAL; |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 859 | } |
| 860 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 861 | /* The writing task will be the switched-to task on a context |
| 862 | * switch. It needs to write into the switched-from task's BTS |
| 863 | * buffer. */ |
| 864 | return ds_unchecked_write_bts(child, bts_record, bts_cfg.sizeof_bts); |
Andrew Morton | d8d4f15 | 2008-03-04 15:05:39 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 867 | void ptrace_bts_take_timestamp(struct task_struct *tsk, |
| 868 | enum bts_qualifier qualifier) |
| 869 | { |
| 870 | struct bts_struct rec = { |
| 871 | .qualifier = qualifier, |
Markus Metzger | da35c37 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 872 | .variant.jiffies = jiffies_64 |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 873 | }; |
| 874 | |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 875 | ptrace_bts_write_record(tsk, &rec); |
| 876 | } |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 877 | |
| 878 | static const struct bts_configuration bts_cfg_netburst = { |
| 879 | .sizeof_bts = sizeof(long) * 3, |
| 880 | .sizeof_field = sizeof(long), |
| 881 | .debugctl_mask = (1<<2)|(1<<3)|(1<<5) |
| 882 | }; |
| 883 | |
| 884 | static const struct bts_configuration bts_cfg_pentium_m = { |
| 885 | .sizeof_bts = sizeof(long) * 3, |
| 886 | .sizeof_field = sizeof(long), |
| 887 | .debugctl_mask = (1<<6)|(1<<7) |
| 888 | }; |
| 889 | |
| 890 | static const struct bts_configuration bts_cfg_core2 = { |
| 891 | .sizeof_bts = 8 * 3, |
| 892 | .sizeof_field = 8, |
| 893 | .debugctl_mask = (1<<6)|(1<<7)|(1<<9) |
| 894 | }; |
| 895 | |
| 896 | static inline void bts_configure(const struct bts_configuration *cfg) |
| 897 | { |
| 898 | bts_cfg = *cfg; |
| 899 | } |
| 900 | |
| 901 | void __cpuinit ptrace_bts_init_intel(struct cpuinfo_x86 *c) |
| 902 | { |
| 903 | switch (c->x86) { |
| 904 | case 0x6: |
| 905 | switch (c->x86_model) { |
| 906 | case 0xD: |
| 907 | case 0xE: /* Pentium M */ |
| 908 | bts_configure(&bts_cfg_pentium_m); |
| 909 | break; |
| 910 | case 0xF: /* Core2 */ |
| 911 | case 0x1C: /* Atom */ |
| 912 | bts_configure(&bts_cfg_core2); |
| 913 | break; |
| 914 | default: |
| 915 | /* sorry, don't know about them */ |
| 916 | break; |
| 917 | } |
| 918 | break; |
| 919 | case 0xF: |
| 920 | switch (c->x86_model) { |
| 921 | case 0x0: |
| 922 | case 0x1: |
| 923 | case 0x2: /* Netburst */ |
| 924 | bts_configure(&bts_cfg_netburst); |
| 925 | break; |
| 926 | default: |
| 927 | /* sorry, don't know about them */ |
| 928 | break; |
| 929 | } |
| 930 | break; |
| 931 | default: |
| 932 | /* sorry, don't know about them */ |
| 933 | break; |
| 934 | } |
| 935 | } |
| 936 | #endif /* CONFIG_X86_PTRACE_BTS */ |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 937 | |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 938 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | * Called by kernel/ptrace.c when detaching.. |
| 940 | * |
| 941 | * Make sure the single step bit is not set. |
| 942 | */ |
| 943 | void ptrace_disable(struct task_struct *child) |
Roland McGrath | 9e714be | 2008-01-30 13:30:58 +0100 | [diff] [blame] | 944 | { |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 945 | user_disable_single_step(child); |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 946 | #ifdef TIF_SYSCALL_EMU |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 947 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 948 | #endif |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 949 | #ifdef CONFIG_X86_PTRACE_BTS |
| 950 | (void)ds_release_bts(child); |
| 951 | |
| 952 | child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask; |
| 953 | if (!child->thread.debugctlmsr) |
| 954 | clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR); |
| 955 | |
| 956 | clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS); |
| 957 | #endif /* CONFIG_X86_PTRACE_BTS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 960 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
| 961 | static const struct user_regset_view user_x86_32_view; /* Initialized below. */ |
| 962 | #endif |
| 963 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 964 | 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] | 965 | { |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 966 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | unsigned long __user *datap = (unsigned long __user *)data; |
| 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | switch (request) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | /* read the word at location addr in the USER area. */ |
| 971 | case PTRACE_PEEKUSR: { |
| 972 | unsigned long tmp; |
| 973 | |
| 974 | ret = -EIO; |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 975 | if ((addr & (sizeof(data) - 1)) || addr < 0 || |
| 976 | addr >= sizeof(struct user)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | break; |
| 978 | |
| 979 | tmp = 0; /* Default return condition */ |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 980 | if (addr < sizeof(struct user_regs_struct)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | tmp = getreg(child, addr); |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 982 | else if (addr >= offsetof(struct user, u_debugreg[0]) && |
| 983 | addr <= offsetof(struct user, u_debugreg[7])) { |
| 984 | addr -= offsetof(struct user, u_debugreg[0]); |
| 985 | tmp = ptrace_get_debugreg(child, addr / sizeof(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
| 987 | ret = put_user(tmp, datap); |
| 988 | break; |
| 989 | } |
| 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
| 992 | ret = -EIO; |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 993 | if ((addr & (sizeof(data) - 1)) || addr < 0 || |
| 994 | addr >= sizeof(struct user)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | break; |
| 996 | |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 997 | if (addr < sizeof(struct user_regs_struct)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | ret = putreg(child, addr, data); |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 999 | else if (addr >= offsetof(struct user, u_debugreg[0]) && |
| 1000 | addr <= offsetof(struct user, u_debugreg[7])) { |
| 1001 | addr -= offsetof(struct user, u_debugreg[0]); |
| 1002 | ret = ptrace_set_debugreg(child, |
| 1003 | addr / sizeof(data), data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | } |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1005 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1007 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ |
| 1008 | return copy_regset_to_user(child, |
| 1009 | task_user_regset_view(current), |
| 1010 | REGSET_GENERAL, |
| 1011 | 0, sizeof(struct user_regs_struct), |
| 1012 | datap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1014 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ |
| 1015 | return copy_regset_from_user(child, |
| 1016 | task_user_regset_view(current), |
| 1017 | REGSET_GENERAL, |
| 1018 | 0, sizeof(struct user_regs_struct), |
| 1019 | datap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1021 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
| 1022 | return copy_regset_to_user(child, |
| 1023 | task_user_regset_view(current), |
| 1024 | REGSET_FP, |
| 1025 | 0, sizeof(struct user_i387_struct), |
| 1026 | datap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1028 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
| 1029 | return copy_regset_from_user(child, |
| 1030 | task_user_regset_view(current), |
| 1031 | REGSET_FP, |
| 1032 | 0, sizeof(struct user_i387_struct), |
| 1033 | datap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1035 | #ifdef CONFIG_X86_32 |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1036 | case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */ |
| 1037 | return copy_regset_to_user(child, &user_x86_32_view, |
| 1038 | REGSET_XFP, |
| 1039 | 0, sizeof(struct user_fxsr_struct), |
| 1040 | datap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1042 | case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */ |
| 1043 | return copy_regset_from_user(child, &user_x86_32_view, |
| 1044 | REGSET_XFP, |
| 1045 | 0, sizeof(struct user_fxsr_struct), |
| 1046 | datap); |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1047 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1049 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | case PTRACE_GET_THREAD_AREA: |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 1051 | if (addr < 0) |
| 1052 | return -EIO; |
| 1053 | ret = do_get_thread_area(child, addr, |
| 1054 | (struct user_desc __user *) data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | break; |
| 1056 | |
| 1057 | case PTRACE_SET_THREAD_AREA: |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 1058 | if (addr < 0) |
| 1059 | return -EIO; |
| 1060 | ret = do_set_thread_area(child, addr, |
| 1061 | (struct user_desc __user *) data, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | break; |
Roland McGrath | e9c86c7 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1063 | #endif |
| 1064 | |
| 1065 | #ifdef CONFIG_X86_64 |
| 1066 | /* normal 64bit interface to access TLS data. |
| 1067 | Works just like arch_prctl, except that the arguments |
| 1068 | are reversed. */ |
| 1069 | case PTRACE_ARCH_PRCTL: |
| 1070 | ret = do_arch_prctl(child, data, addr); |
| 1071 | break; |
| 1072 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Ingo Molnar | b4ef95d | 2008-02-26 09:40:27 +0100 | [diff] [blame] | 1074 | /* |
| 1075 | * These bits need more cooking - not enabled yet: |
| 1076 | */ |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 1077 | #ifdef CONFIG_X86_PTRACE_BTS |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 1078 | case PTRACE_BTS_CONFIG: |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 1079 | ret = ptrace_bts_config |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 1080 | (child, data, (struct ptrace_bts_config __user *)addr); |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 1081 | break; |
| 1082 | |
| 1083 | case PTRACE_BTS_STATUS: |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 1084 | ret = ptrace_bts_status |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 1085 | (child, data, (struct ptrace_bts_config __user *)addr); |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 1086 | break; |
| 1087 | |
| 1088 | case PTRACE_BTS_SIZE: |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 1089 | ret = ds_get_bts_index(child, /* pos = */ 0); |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 1090 | break; |
| 1091 | |
| 1092 | case PTRACE_BTS_GET: |
| 1093 | ret = ptrace_bts_read_record |
| 1094 | (child, data, (struct bts_struct __user *) addr); |
| 1095 | break; |
| 1096 | |
| 1097 | case PTRACE_BTS_CLEAR: |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 1098 | ret = ds_clear_bts(child); |
Markus Metzger | a95d67f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 1099 | break; |
| 1100 | |
| 1101 | case PTRACE_BTS_DRAIN: |
| 1102 | ret = ptrace_bts_drain |
Markus Metzger | cba4b65 | 2008-01-30 13:32:03 +0100 | [diff] [blame] | 1103 | (child, data, (struct bts_struct __user *) addr); |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 1104 | break; |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame^] | 1105 | #endif /* CONFIG_X86_PTRACE_BTS */ |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | default: |
| 1108 | ret = ptrace_request(child, request, addr, data); |
| 1109 | break; |
| 1110 | } |
Roland McGrath | d9771e8 | 2008-01-30 13:30:52 +0100 | [diff] [blame] | 1111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | return ret; |
| 1113 | } |
| 1114 | |
Roland McGrath | cb757c4 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1115 | #ifdef CONFIG_IA32_EMULATION |
| 1116 | |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1117 | #include <linux/compat.h> |
| 1118 | #include <linux/syscalls.h> |
| 1119 | #include <asm/ia32.h> |
Roland McGrath | cb757c4 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1120 | #include <asm/user32.h> |
| 1121 | |
| 1122 | #define R32(l,q) \ |
| 1123 | case offsetof(struct user32, regs.l): \ |
| 1124 | regs->q = value; break |
| 1125 | |
| 1126 | #define SEG32(rs) \ |
| 1127 | case offsetof(struct user32, regs.rs): \ |
| 1128 | return set_segment_reg(child, \ |
| 1129 | offsetof(struct user_regs_struct, rs), \ |
| 1130 | value); \ |
| 1131 | break |
| 1132 | |
| 1133 | static int putreg32(struct task_struct *child, unsigned regno, u32 value) |
| 1134 | { |
| 1135 | struct pt_regs *regs = task_pt_regs(child); |
| 1136 | |
| 1137 | switch (regno) { |
| 1138 | |
| 1139 | SEG32(cs); |
| 1140 | SEG32(ds); |
| 1141 | SEG32(es); |
| 1142 | SEG32(fs); |
| 1143 | SEG32(gs); |
| 1144 | SEG32(ss); |
| 1145 | |
| 1146 | R32(ebx, bx); |
| 1147 | R32(ecx, cx); |
| 1148 | R32(edx, dx); |
| 1149 | R32(edi, di); |
| 1150 | R32(esi, si); |
| 1151 | R32(ebp, bp); |
| 1152 | R32(eax, ax); |
Roland McGrath | cb757c4 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1153 | R32(eip, ip); |
| 1154 | R32(esp, sp); |
| 1155 | |
Roland McGrath | 40f0933 | 2008-02-28 19:57:07 -0800 | [diff] [blame] | 1156 | case offsetof(struct user32, regs.orig_eax): |
| 1157 | /* |
| 1158 | * Sign-extend the value so that orig_eax = -1 |
| 1159 | * causes (long)orig_ax < 0 tests to fire correctly. |
| 1160 | */ |
| 1161 | regs->orig_ax = (long) (s32) value; |
| 1162 | break; |
| 1163 | |
Roland McGrath | cb757c4 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1164 | case offsetof(struct user32, regs.eflags): |
| 1165 | return set_flags(child, value); |
| 1166 | |
| 1167 | case offsetof(struct user32, u_debugreg[0]) ... |
| 1168 | offsetof(struct user32, u_debugreg[7]): |
| 1169 | regno -= offsetof(struct user32, u_debugreg[0]); |
| 1170 | return ptrace_set_debugreg(child, regno / 4, value); |
| 1171 | |
| 1172 | default: |
| 1173 | if (regno > sizeof(struct user32) || (regno & 3)) |
| 1174 | return -EIO; |
| 1175 | |
| 1176 | /* |
| 1177 | * Other dummy fields in the virtual user structure |
| 1178 | * are ignored |
| 1179 | */ |
| 1180 | break; |
| 1181 | } |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | #undef R32 |
| 1186 | #undef SEG32 |
| 1187 | |
| 1188 | #define R32(l,q) \ |
| 1189 | case offsetof(struct user32, regs.l): \ |
| 1190 | *val = regs->q; break |
| 1191 | |
| 1192 | #define SEG32(rs) \ |
| 1193 | case offsetof(struct user32, regs.rs): \ |
| 1194 | *val = get_segment_reg(child, \ |
| 1195 | offsetof(struct user_regs_struct, rs)); \ |
| 1196 | break |
| 1197 | |
| 1198 | static int getreg32(struct task_struct *child, unsigned regno, u32 *val) |
| 1199 | { |
| 1200 | struct pt_regs *regs = task_pt_regs(child); |
| 1201 | |
| 1202 | switch (regno) { |
| 1203 | |
| 1204 | SEG32(ds); |
| 1205 | SEG32(es); |
| 1206 | SEG32(fs); |
| 1207 | SEG32(gs); |
| 1208 | |
| 1209 | R32(cs, cs); |
| 1210 | R32(ss, ss); |
| 1211 | R32(ebx, bx); |
| 1212 | R32(ecx, cx); |
| 1213 | R32(edx, dx); |
| 1214 | R32(edi, di); |
| 1215 | R32(esi, si); |
| 1216 | R32(ebp, bp); |
| 1217 | R32(eax, ax); |
| 1218 | R32(orig_eax, orig_ax); |
| 1219 | R32(eip, ip); |
| 1220 | R32(esp, sp); |
| 1221 | |
| 1222 | case offsetof(struct user32, regs.eflags): |
| 1223 | *val = get_flags(child); |
| 1224 | break; |
| 1225 | |
| 1226 | case offsetof(struct user32, u_debugreg[0]) ... |
| 1227 | offsetof(struct user32, u_debugreg[7]): |
| 1228 | regno -= offsetof(struct user32, u_debugreg[0]); |
| 1229 | *val = ptrace_get_debugreg(child, regno / 4); |
| 1230 | break; |
| 1231 | |
| 1232 | default: |
| 1233 | if (regno > sizeof(struct user32) || (regno & 3)) |
| 1234 | return -EIO; |
| 1235 | |
| 1236 | /* |
| 1237 | * Other dummy fields in the virtual user structure |
| 1238 | * are ignored |
| 1239 | */ |
| 1240 | *val = 0; |
| 1241 | break; |
| 1242 | } |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | #undef R32 |
| 1247 | #undef SEG32 |
| 1248 | |
Roland McGrath | 91e7b70 | 2008-01-30 13:31:52 +0100 | [diff] [blame] | 1249 | static int genregs32_get(struct task_struct *target, |
| 1250 | const struct user_regset *regset, |
| 1251 | unsigned int pos, unsigned int count, |
| 1252 | void *kbuf, void __user *ubuf) |
| 1253 | { |
| 1254 | if (kbuf) { |
| 1255 | compat_ulong_t *k = kbuf; |
| 1256 | while (count > 0) { |
| 1257 | getreg32(target, pos, k++); |
| 1258 | count -= sizeof(*k); |
| 1259 | pos += sizeof(*k); |
| 1260 | } |
| 1261 | } else { |
| 1262 | compat_ulong_t __user *u = ubuf; |
| 1263 | while (count > 0) { |
| 1264 | compat_ulong_t word; |
| 1265 | getreg32(target, pos, &word); |
| 1266 | if (__put_user(word, u++)) |
| 1267 | return -EFAULT; |
| 1268 | count -= sizeof(*u); |
| 1269 | pos += sizeof(*u); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | static int genregs32_set(struct task_struct *target, |
| 1277 | const struct user_regset *regset, |
| 1278 | unsigned int pos, unsigned int count, |
| 1279 | const void *kbuf, const void __user *ubuf) |
| 1280 | { |
| 1281 | int ret = 0; |
| 1282 | if (kbuf) { |
| 1283 | const compat_ulong_t *k = kbuf; |
| 1284 | while (count > 0 && !ret) { |
Roland McGrath | f9cb02b | 2008-02-21 20:37:24 -0800 | [diff] [blame] | 1285 | ret = putreg32(target, pos, *k++); |
Roland McGrath | 91e7b70 | 2008-01-30 13:31:52 +0100 | [diff] [blame] | 1286 | count -= sizeof(*k); |
| 1287 | pos += sizeof(*k); |
| 1288 | } |
| 1289 | } else { |
| 1290 | const compat_ulong_t __user *u = ubuf; |
| 1291 | while (count > 0 && !ret) { |
| 1292 | compat_ulong_t word; |
| 1293 | ret = __get_user(word, u++); |
| 1294 | if (ret) |
| 1295 | break; |
Roland McGrath | f9cb02b | 2008-02-21 20:37:24 -0800 | [diff] [blame] | 1296 | ret = putreg32(target, pos, word); |
Roland McGrath | 91e7b70 | 2008-01-30 13:31:52 +0100 | [diff] [blame] | 1297 | count -= sizeof(*u); |
| 1298 | pos += sizeof(*u); |
| 1299 | } |
| 1300 | } |
| 1301 | return ret; |
| 1302 | } |
| 1303 | |
Roland McGrath | 562b80b | 2008-04-22 12:21:25 -0700 | [diff] [blame] | 1304 | long compat_arch_ptrace(struct task_struct *child, compat_long_t request, |
| 1305 | compat_ulong_t caddr, compat_ulong_t cdata) |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1306 | { |
Roland McGrath | 562b80b | 2008-04-22 12:21:25 -0700 | [diff] [blame] | 1307 | unsigned long addr = caddr; |
| 1308 | unsigned long data = cdata; |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1309 | void __user *datap = compat_ptr(data); |
| 1310 | int ret; |
| 1311 | __u32 val; |
| 1312 | |
| 1313 | switch (request) { |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1314 | case PTRACE_PEEKUSR: |
| 1315 | ret = getreg32(child, addr, &val); |
| 1316 | if (ret == 0) |
| 1317 | ret = put_user(val, (__u32 __user *)datap); |
| 1318 | break; |
| 1319 | |
| 1320 | case PTRACE_POKEUSR: |
| 1321 | ret = putreg32(child, addr, data); |
| 1322 | break; |
| 1323 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1324 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ |
| 1325 | return copy_regset_to_user(child, &user_x86_32_view, |
| 1326 | REGSET_GENERAL, |
| 1327 | 0, sizeof(struct user_regs_struct32), |
| 1328 | datap); |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1329 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1330 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ |
| 1331 | return copy_regset_from_user(child, &user_x86_32_view, |
| 1332 | REGSET_GENERAL, 0, |
| 1333 | sizeof(struct user_regs_struct32), |
| 1334 | datap); |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1335 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1336 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
| 1337 | return copy_regset_to_user(child, &user_x86_32_view, |
| 1338 | REGSET_FP, 0, |
| 1339 | sizeof(struct user_i387_ia32_struct), |
| 1340 | datap); |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1341 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1342 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
| 1343 | return copy_regset_from_user( |
| 1344 | child, &user_x86_32_view, REGSET_FP, |
| 1345 | 0, sizeof(struct user_i387_ia32_struct), datap); |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1346 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1347 | case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */ |
| 1348 | return copy_regset_to_user(child, &user_x86_32_view, |
| 1349 | REGSET_XFP, 0, |
| 1350 | sizeof(struct user32_fxsr_struct), |
| 1351 | datap); |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1352 | |
Roland McGrath | 5a4646a | 2008-01-30 13:31:54 +0100 | [diff] [blame] | 1353 | case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */ |
| 1354 | return copy_regset_from_user(child, &user_x86_32_view, |
| 1355 | REGSET_XFP, 0, |
| 1356 | sizeof(struct user32_fxsr_struct), |
| 1357 | datap); |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1358 | |
Roland McGrath | 562b80b | 2008-04-22 12:21:25 -0700 | [diff] [blame] | 1359 | case PTRACE_GET_THREAD_AREA: |
| 1360 | case PTRACE_SET_THREAD_AREA: |
| 1361 | return arch_ptrace(child, request, addr, data); |
| 1362 | |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1363 | default: |
Roland McGrath | fdadd54 | 2008-01-30 13:31:56 +0100 | [diff] [blame] | 1364 | return compat_ptrace_request(child, request, addr, data); |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Roland McGrath | 099cd6e | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1367 | return ret; |
| 1368 | } |
| 1369 | |
Roland McGrath | cb757c4 | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1370 | #endif /* CONFIG_IA32_EMULATION */ |
| 1371 | |
Roland McGrath | 070459d | 2008-01-30 13:31:53 +0100 | [diff] [blame] | 1372 | #ifdef CONFIG_X86_64 |
| 1373 | |
| 1374 | static const struct user_regset x86_64_regsets[] = { |
| 1375 | [REGSET_GENERAL] = { |
| 1376 | .core_note_type = NT_PRSTATUS, |
| 1377 | .n = sizeof(struct user_regs_struct) / sizeof(long), |
| 1378 | .size = sizeof(long), .align = sizeof(long), |
| 1379 | .get = genregs_get, .set = genregs_set |
| 1380 | }, |
| 1381 | [REGSET_FP] = { |
| 1382 | .core_note_type = NT_PRFPREG, |
| 1383 | .n = sizeof(struct user_i387_struct) / sizeof(long), |
| 1384 | .size = sizeof(long), .align = sizeof(long), |
| 1385 | .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set |
| 1386 | }, |
| 1387 | }; |
| 1388 | |
| 1389 | static const struct user_regset_view user_x86_64_view = { |
| 1390 | .name = "x86_64", .e_machine = EM_X86_64, |
| 1391 | .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets) |
| 1392 | }; |
| 1393 | |
| 1394 | #else /* CONFIG_X86_32 */ |
| 1395 | |
| 1396 | #define user_regs_struct32 user_regs_struct |
| 1397 | #define genregs32_get genregs_get |
| 1398 | #define genregs32_set genregs_set |
| 1399 | |
| 1400 | #endif /* CONFIG_X86_64 */ |
| 1401 | |
| 1402 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
| 1403 | static const struct user_regset x86_32_regsets[] = { |
| 1404 | [REGSET_GENERAL] = { |
| 1405 | .core_note_type = NT_PRSTATUS, |
| 1406 | .n = sizeof(struct user_regs_struct32) / sizeof(u32), |
| 1407 | .size = sizeof(u32), .align = sizeof(u32), |
| 1408 | .get = genregs32_get, .set = genregs32_set |
| 1409 | }, |
| 1410 | [REGSET_FP] = { |
| 1411 | .core_note_type = NT_PRFPREG, |
| 1412 | .n = sizeof(struct user_i387_struct) / sizeof(u32), |
| 1413 | .size = sizeof(u32), .align = sizeof(u32), |
| 1414 | .active = fpregs_active, .get = fpregs_get, .set = fpregs_set |
| 1415 | }, |
| 1416 | [REGSET_XFP] = { |
| 1417 | .core_note_type = NT_PRXFPREG, |
| 1418 | .n = sizeof(struct user_i387_struct) / sizeof(u32), |
| 1419 | .size = sizeof(u32), .align = sizeof(u32), |
| 1420 | .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set |
| 1421 | }, |
| 1422 | [REGSET_TLS] = { |
Roland McGrath | bb61682 | 2008-01-30 13:31:56 +0100 | [diff] [blame] | 1423 | .core_note_type = NT_386_TLS, |
Roland McGrath | 070459d | 2008-01-30 13:31:53 +0100 | [diff] [blame] | 1424 | .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN, |
| 1425 | .size = sizeof(struct user_desc), |
| 1426 | .align = sizeof(struct user_desc), |
| 1427 | .active = regset_tls_active, |
| 1428 | .get = regset_tls_get, .set = regset_tls_set |
| 1429 | }, |
| 1430 | }; |
| 1431 | |
| 1432 | static const struct user_regset_view user_x86_32_view = { |
| 1433 | .name = "i386", .e_machine = EM_386, |
| 1434 | .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets) |
| 1435 | }; |
| 1436 | #endif |
| 1437 | |
| 1438 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
| 1439 | { |
| 1440 | #ifdef CONFIG_IA32_EMULATION |
| 1441 | if (test_tsk_thread_flag(task, TIF_IA32)) |
| 1442 | #endif |
| 1443 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
| 1444 | return &user_x86_32_view; |
| 1445 | #endif |
| 1446 | #ifdef CONFIG_X86_64 |
| 1447 | return &user_x86_64_view; |
| 1448 | #endif |
| 1449 | } |
| 1450 | |
Roland McGrath | 86976cd | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1451 | #ifdef CONFIG_X86_32 |
| 1452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code) |
| 1454 | { |
| 1455 | struct siginfo info; |
| 1456 | |
| 1457 | tsk->thread.trap_no = 1; |
| 1458 | tsk->thread.error_code = error_code; |
| 1459 | |
| 1460 | memset(&info, 0, sizeof(info)); |
| 1461 | info.si_signo = SIGTRAP; |
| 1462 | info.si_code = TRAP_BRKPT; |
| 1463 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1464 | /* User-mode ip? */ |
| 1465 | info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
Simon Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 1467 | /* Send us the fake SIGTRAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | force_sig_info(SIGTRAP, &info, tsk); |
| 1469 | } |
| 1470 | |
| 1471 | /* notification of system call entry/exit |
| 1472 | * - triggered by current->work.syscall_trace |
| 1473 | */ |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 1474 | int do_syscall_trace(struct pt_regs *regs, int entryexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 1476 | int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU); |
| 1477 | /* |
| 1478 | * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall |
| 1479 | * interception |
| 1480 | */ |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 1481 | int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP); |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 1482 | int ret = 0; |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | /* do the secure computing check first */ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 1485 | if (!entryexit) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1486 | secure_computing(regs->orig_ax); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 1488 | if (unlikely(current->audit_context)) { |
| 1489 | if (entryexit) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1490 | audit_syscall_exit(AUDITSC_RESULT(regs->ax), |
| 1491 | regs->ax); |
Bodo Stroesser | ab1c23c | 2005-09-03 15:57:21 -0700 | [diff] [blame] | 1492 | /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only |
| 1493 | * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is |
| 1494 | * not used, entry.S will call us only on syscall exit, not |
| 1495 | * entry; so when TIF_SYSCALL_AUDIT is used we must avoid |
| 1496 | * calling send_sigtrap() on syscall entry. |
| 1497 | * |
| 1498 | * Note that when PTRACE_SYSEMU_SINGLESTEP is used, |
| 1499 | * is_singlestep is false, despite his name, so we will still do |
| 1500 | * the correct thing. |
| 1501 | */ |
| 1502 | else if (is_singlestep) |
| 1503 | goto out; |
| 1504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
| 1506 | if (!(current->ptrace & PT_PTRACED)) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1507 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
Bodo Stroesser | 1b38f00 | 2005-09-03 15:57:20 -0700 | [diff] [blame] | 1509 | /* If a process stops on the 1st tracepoint with SYSCALL_TRACE |
| 1510 | * and then is resumed with SYSEMU_SINGLESTEP, it will come in |
| 1511 | * here. We have to check this and return */ |
| 1512 | if (is_sysemu && entryexit) |
| 1513 | return 0; |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 1514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | /* Fake a debug trap */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 1516 | if (is_singlestep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | send_sigtrap(current, regs, 0); |
| 1518 | |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 1519 | if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1520 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 1523 | between a syscall stop and SIGTRAP delivery */ |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 1524 | /* Note that the debugger could change the result of test_thread_flag!*/ |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 1525 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | |
| 1527 | /* |
| 1528 | * this isn't the same as continuing with a signal, but it will do |
| 1529 | * for normal use. strace only continues with a signal if the |
| 1530 | * stopping signal is not SIGTRAP. -brl |
| 1531 | */ |
| 1532 | if (current->exit_code) { |
| 1533 | send_sig(current->exit_code, current, 1); |
| 1534 | current->exit_code = 0; |
| 1535 | } |
Laurent Vivier | ed75e8d | 2005-09-03 15:57:18 -0700 | [diff] [blame] | 1536 | ret = is_sysemu; |
Andrea Arcangeli | 4c7fc72 | 2005-09-09 13:01:51 -0700 | [diff] [blame] | 1537 | out: |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1538 | if (unlikely(current->audit_context) && !entryexit) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1539 | audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax, |
| 1540 | regs->bx, regs->cx, regs->dx, regs->si); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 1541 | if (ret == 0) |
| 1542 | return 0; |
| 1543 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1544 | regs->orig_ax = -1; /* force skip of syscall restarting */ |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 1545 | if (unlikely(current->audit_context)) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1546 | audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax); |
Bodo Stroesser | c8c86ce | 2005-09-03 15:57:19 -0700 | [diff] [blame] | 1547 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
Roland McGrath | 86976cd | 2008-01-30 13:31:01 +0100 | [diff] [blame] | 1549 | |
| 1550 | #else /* CONFIG_X86_64 */ |
| 1551 | |
| 1552 | static void syscall_trace(struct pt_regs *regs) |
| 1553 | { |
| 1554 | |
| 1555 | #if 0 |
| 1556 | printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n", |
| 1557 | current->comm, |
| 1558 | regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0), |
| 1559 | current_thread_info()->flags, current->ptrace); |
| 1560 | #endif |
| 1561 | |
| 1562 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 1563 | ? 0x80 : 0)); |
| 1564 | /* |
| 1565 | * this isn't the same as continuing with a signal, but it will do |
| 1566 | * for normal use. strace only continues with a signal if the |
| 1567 | * stopping signal is not SIGTRAP. -brl |
| 1568 | */ |
| 1569 | if (current->exit_code) { |
| 1570 | send_sig(current->exit_code, current, 1); |
| 1571 | current->exit_code = 0; |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | asmlinkage void syscall_trace_enter(struct pt_regs *regs) |
| 1576 | { |
| 1577 | /* do the secure computing check first */ |
| 1578 | secure_computing(regs->orig_ax); |
| 1579 | |
| 1580 | if (test_thread_flag(TIF_SYSCALL_TRACE) |
| 1581 | && (current->ptrace & PT_PTRACED)) |
| 1582 | syscall_trace(regs); |
| 1583 | |
| 1584 | if (unlikely(current->audit_context)) { |
| 1585 | if (test_thread_flag(TIF_IA32)) { |
| 1586 | audit_syscall_entry(AUDIT_ARCH_I386, |
| 1587 | regs->orig_ax, |
| 1588 | regs->bx, regs->cx, |
| 1589 | regs->dx, regs->si); |
| 1590 | } else { |
| 1591 | audit_syscall_entry(AUDIT_ARCH_X86_64, |
| 1592 | regs->orig_ax, |
| 1593 | regs->di, regs->si, |
| 1594 | regs->dx, regs->r10); |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | asmlinkage void syscall_trace_leave(struct pt_regs *regs) |
| 1600 | { |
| 1601 | if (unlikely(current->audit_context)) |
| 1602 | audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax); |
| 1603 | |
| 1604 | if ((test_thread_flag(TIF_SYSCALL_TRACE) |
| 1605 | || test_thread_flag(TIF_SINGLESTEP)) |
| 1606 | && (current->ptrace & PT_PTRACED)) |
| 1607 | syscall_trace(regs); |
| 1608 | } |
| 1609 | |
| 1610 | #endif /* CONFIG_X86_32 */ |