blob: 04d182a7cfdbd6e3c88c41658f6d0a91878e6366 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* By Ross Biro 1/23/92 */
2/*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
Markus Metzgereee3af42008-01-30 13:31:09 +01005 *
6 * BTS tracing
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/ptrace.h>
Roland McGrath91e7b702008-01-30 13:31:52 +010016#include <linux/regset.h>
Roland McGratheeea3c32008-03-16 23:36:28 -070017#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/user.h>
Roland McGrath070459d2008-01-30 13:31:53 +010019#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/security.h>
21#include <linux/audit.h>
22#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070023#include <linux/signal.h>
Markus Metzgere2b371f2009-04-03 16:43:35 +020024#include <linux/workqueue.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020025#include <linux/perf_event.h>
26#include <linux/hw_breakpoint.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/uaccess.h>
29#include <asm/pgtable.h>
30#include <asm/system.h>
31#include <asm/processor.h>
32#include <asm/i387.h>
33#include <asm/debugreg.h>
34#include <asm/ldt.h>
35#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010036#include <asm/prctl.h>
37#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010038#include <asm/ds.h>
K.Prasad72f674d2009-06-01 23:45:48 +053039#include <asm/hw_breakpoint.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010040
Roland McGrath070459d2008-01-30 13:31:53 +010041#include "tls.h"
42
Josh Stone1c569f02009-08-24 14:43:14 -070043#define CREATE_TRACE_POINTS
44#include <trace/events/syscalls.h>
45
Roland McGrath070459d2008-01-30 13:31:53 +010046enum x86_regset {
47 REGSET_GENERAL,
48 REGSET_FP,
49 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070050 REGSET_IOPERM64 = REGSET_XFP,
Roland McGrath070459d2008-01-30 13:31:53 +010051 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070052 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010053};
Markus Metzgereee3af42008-01-30 13:31:09 +010054
Masami Hiramatsub1cf5402009-08-13 16:34:44 -040055struct pt_regs_offset {
56 const char *name;
57 int offset;
58};
59
60#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61#define REG_OFFSET_END {.name = NULL, .offset = 0}
62
63static const struct pt_regs_offset regoffset_table[] = {
64#ifdef CONFIG_X86_64
65 REG_OFFSET_NAME(r15),
66 REG_OFFSET_NAME(r14),
67 REG_OFFSET_NAME(r13),
68 REG_OFFSET_NAME(r12),
69 REG_OFFSET_NAME(r11),
70 REG_OFFSET_NAME(r10),
71 REG_OFFSET_NAME(r9),
72 REG_OFFSET_NAME(r8),
73#endif
74 REG_OFFSET_NAME(bx),
75 REG_OFFSET_NAME(cx),
76 REG_OFFSET_NAME(dx),
77 REG_OFFSET_NAME(si),
78 REG_OFFSET_NAME(di),
79 REG_OFFSET_NAME(bp),
80 REG_OFFSET_NAME(ax),
81#ifdef CONFIG_X86_32
82 REG_OFFSET_NAME(ds),
83 REG_OFFSET_NAME(es),
84 REG_OFFSET_NAME(fs),
85 REG_OFFSET_NAME(gs),
86#endif
87 REG_OFFSET_NAME(orig_ax),
88 REG_OFFSET_NAME(ip),
89 REG_OFFSET_NAME(cs),
90 REG_OFFSET_NAME(flags),
91 REG_OFFSET_NAME(sp),
92 REG_OFFSET_NAME(ss),
93 REG_OFFSET_END,
94};
95
96/**
97 * regs_query_register_offset() - query register offset from its name
98 * @name: the name of a register
99 *
100 * regs_query_register_offset() returns the offset of a register in struct
101 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
102 */
103int regs_query_register_offset(const char *name)
104{
105 const struct pt_regs_offset *roff;
106 for (roff = regoffset_table; roff->name != NULL; roff++)
107 if (!strcmp(roff->name, name))
108 return roff->offset;
109 return -EINVAL;
110}
111
112/**
113 * regs_query_register_name() - query register name from its offset
114 * @offset: the offset of a register in struct pt_regs.
115 *
116 * regs_query_register_name() returns the name of a register from its
117 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
118 */
119const char *regs_query_register_name(unsigned int offset)
120{
121 const struct pt_regs_offset *roff;
122 for (roff = regoffset_table; roff->name != NULL; roff++)
123 if (roff->offset == offset)
124 return roff->name;
125 return NULL;
126}
127
128static const int arg_offs_table[] = {
129#ifdef CONFIG_X86_32
130 [0] = offsetof(struct pt_regs, ax),
131 [1] = offsetof(struct pt_regs, dx),
132 [2] = offsetof(struct pt_regs, cx)
133#else /* CONFIG_X86_64 */
134 [0] = offsetof(struct pt_regs, di),
135 [1] = offsetof(struct pt_regs, si),
136 [2] = offsetof(struct pt_regs, dx),
137 [3] = offsetof(struct pt_regs, cx),
138 [4] = offsetof(struct pt_regs, r8),
139 [5] = offsetof(struct pt_regs, r9)
140#endif
141};
142
143/**
144 * regs_get_argument_nth() - get Nth argument at function call
145 * @regs: pt_regs which contains registers at function entry.
146 * @n: argument number.
147 *
148 * regs_get_argument_nth() returns @n th argument of a function call.
149 * Since usually the kernel stack will be changed right after function entry,
150 * you must use this at function entry. If the @n th entry is NOT in the
151 * kernel stack or pt_regs, this returns 0.
152 */
153unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
154{
155 if (n < ARRAY_SIZE(arg_offs_table))
Masami Hiramatsuad5cafc2009-09-10 19:53:06 -0400156 return *(unsigned long *)((char *)regs + arg_offs_table[n]);
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400157 else {
158 /*
159 * The typical case: arg n is on the stack.
160 * (Note: stack[0] = return address, so skip it)
161 */
162 n -= ARRAY_SIZE(arg_offs_table);
163 return regs_get_kernel_stack_nth(regs, 1 + n);
164 }
165}
166
Markus Metzgereee3af42008-01-30 13:31:09 +0100167/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * does not yet catch signals sent when the child dies.
169 * in exit.c or in signal.c.
170 */
171
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500172/*
173 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500174 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100175#define FLAG_MASK_32 ((unsigned long) \
176 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
177 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
178 X86_EFLAGS_SF | X86_EFLAGS_TF | \
179 X86_EFLAGS_DF | X86_EFLAGS_OF | \
180 X86_EFLAGS_RF | X86_EFLAGS_AC))
181
Roland McGrath2047b082008-01-30 13:31:01 +0100182/*
183 * Determines whether a value may be installed in a segment register.
184 */
185static inline bool invalid_selector(u16 value)
186{
187 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
188}
189
190#ifdef CONFIG_X86_32
191
Roland McGrathe39c2892008-01-30 13:31:01 +0100192#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530194static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100196 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900197 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Roland McGrath06ee1b62008-01-30 13:31:01 +0100200static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100202 /*
203 * Returning the value truncates it to 16 bits.
204 */
205 unsigned int retval;
206 if (offset != offsetof(struct user_regs_struct, gs))
207 retval = *pt_regs_access(task_pt_regs(task), offset);
208 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100209 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900210 retval = get_user_gs(task_pt_regs(task));
211 else
212 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100213 }
214 return retval;
215}
216
217static int set_segment_reg(struct task_struct *task,
218 unsigned long offset, u16 value)
219{
220 /*
221 * The value argument was already truncated to 16 bits.
222 */
Roland McGrath2047b082008-01-30 13:31:01 +0100223 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100224 return -EIO;
225
Roland McGrathc63855d2008-02-06 22:39:44 +0100226 /*
227 * For %cs and %ss we cannot permit a null selector.
228 * We can permit a bogus selector as long as it has USER_RPL.
229 * Null selectors are fine for other segment registers, but
230 * we will never get back to user mode with invalid %cs or %ss
231 * and will take the trap in iret instead. Much code relies
232 * on user_mode() to distinguish a user trap frame (which can
233 * safely use invalid selectors) from a kernel trap frame.
234 */
235 switch (offset) {
236 case offsetof(struct user_regs_struct, cs):
237 case offsetof(struct user_regs_struct, ss):
238 if (unlikely(value == 0))
239 return -EIO;
240
241 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100242 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100243 break;
244
245 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100246 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900247 set_user_gs(task_pt_regs(task), value);
248 else
249 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
Roland McGrath2047b082008-01-30 13:31:01 +0100255#else /* CONFIG_X86_64 */
256
257#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
258
259static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
260{
261 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
262 return &regs->r15 + (offset / sizeof(regs->r15));
263}
264
265static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
266{
267 /*
268 * Returning the value truncates it to 16 bits.
269 */
270 unsigned int seg;
271
272 switch (offset) {
273 case offsetof(struct user_regs_struct, fs):
274 if (task == current) {
275 /* Older gas can't assemble movq %?s,%r?? */
276 asm("movl %%fs,%0" : "=r" (seg));
277 return seg;
278 }
279 return task->thread.fsindex;
280 case offsetof(struct user_regs_struct, gs):
281 if (task == current) {
282 asm("movl %%gs,%0" : "=r" (seg));
283 return seg;
284 }
285 return task->thread.gsindex;
286 case offsetof(struct user_regs_struct, ds):
287 if (task == current) {
288 asm("movl %%ds,%0" : "=r" (seg));
289 return seg;
290 }
291 return task->thread.ds;
292 case offsetof(struct user_regs_struct, es):
293 if (task == current) {
294 asm("movl %%es,%0" : "=r" (seg));
295 return seg;
296 }
297 return task->thread.es;
298
299 case offsetof(struct user_regs_struct, cs):
300 case offsetof(struct user_regs_struct, ss):
301 break;
302 }
303 return *pt_regs_access(task_pt_regs(task), offset);
304}
305
306static int set_segment_reg(struct task_struct *task,
307 unsigned long offset, u16 value)
308{
309 /*
310 * The value argument was already truncated to 16 bits.
311 */
312 if (invalid_selector(value))
313 return -EIO;
314
315 switch (offset) {
316 case offsetof(struct user_regs_struct,fs):
317 /*
318 * If this is setting fs as for normal 64-bit use but
319 * setting fs_base has implicitly changed it, leave it.
320 */
321 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
322 task->thread.fs != 0) ||
323 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
324 task->thread.fs == 0))
325 break;
326 task->thread.fsindex = value;
327 if (task == current)
328 loadsegment(fs, task->thread.fsindex);
329 break;
330 case offsetof(struct user_regs_struct,gs):
331 /*
332 * If this is setting gs as for normal 64-bit use but
333 * setting gs_base has implicitly changed it, leave it.
334 */
335 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
336 task->thread.gs != 0) ||
337 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
338 task->thread.gs == 0))
339 break;
340 task->thread.gsindex = value;
341 if (task == current)
342 load_gs_index(task->thread.gsindex);
343 break;
344 case offsetof(struct user_regs_struct,ds):
345 task->thread.ds = value;
346 if (task == current)
347 loadsegment(ds, task->thread.ds);
348 break;
349 case offsetof(struct user_regs_struct,es):
350 task->thread.es = value;
351 if (task == current)
352 loadsegment(es, task->thread.es);
353 break;
354
355 /*
356 * Can't actually change these in 64-bit mode.
357 */
358 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100359 if (unlikely(value == 0))
360 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100361#ifdef CONFIG_IA32_EMULATION
362 if (test_tsk_thread_flag(task, TIF_IA32))
363 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100364#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100365 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100366 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100367 if (unlikely(value == 0))
368 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100369#ifdef CONFIG_IA32_EMULATION
370 if (test_tsk_thread_flag(task, TIF_IA32))
371 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100372#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100373 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100374 }
375
376 return 0;
377}
378
Roland McGrath2047b082008-01-30 13:31:01 +0100379#endif /* CONFIG_X86_32 */
380
Roland McGrath06ee1b62008-01-30 13:31:01 +0100381static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100383 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Roland McGrath06ee1b62008-01-30 13:31:01 +0100385 /*
386 * If the debugger set TF, hide it from the readout.
387 */
388 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
389 retval &= ~X86_EFLAGS_TF;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return retval;
392}
393
Roland McGrath06ee1b62008-01-30 13:31:01 +0100394static int set_flags(struct task_struct *task, unsigned long value)
395{
396 struct pt_regs *regs = task_pt_regs(task);
397
398 /*
399 * If the user value contains TF, mark that
400 * it was not "us" (the debugger) that set it.
401 * If not, make sure it stays set if we had.
402 */
403 if (value & X86_EFLAGS_TF)
404 clear_tsk_thread_flag(task, TIF_FORCED_TF);
405 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
406 value |= X86_EFLAGS_TF;
407
408 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
409
410 return 0;
411}
412
413static int putreg(struct task_struct *child,
414 unsigned long offset, unsigned long value)
415{
416 switch (offset) {
417 case offsetof(struct user_regs_struct, cs):
418 case offsetof(struct user_regs_struct, ds):
419 case offsetof(struct user_regs_struct, es):
420 case offsetof(struct user_regs_struct, fs):
421 case offsetof(struct user_regs_struct, gs):
422 case offsetof(struct user_regs_struct, ss):
423 return set_segment_reg(child, offset, value);
424
425 case offsetof(struct user_regs_struct, flags):
426 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100427
428#ifdef CONFIG_X86_64
429 case offsetof(struct user_regs_struct,fs_base):
430 if (value >= TASK_SIZE_OF(child))
431 return -EIO;
432 /*
433 * When changing the segment base, use do_arch_prctl
434 * to set either thread.fs or thread.fsindex and the
435 * corresponding GDT slot.
436 */
437 if (child->thread.fs != value)
438 return do_arch_prctl(child, ARCH_SET_FS, value);
439 return 0;
440 case offsetof(struct user_regs_struct,gs_base):
441 /*
442 * Exactly the same here as the %fs handling above.
443 */
444 if (value >= TASK_SIZE_OF(child))
445 return -EIO;
446 if (child->thread.gs != value)
447 return do_arch_prctl(child, ARCH_SET_GS, value);
448 return 0;
449#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100450 }
451
452 *pt_regs_access(task_pt_regs(child), offset) = value;
453 return 0;
454}
455
456static unsigned long getreg(struct task_struct *task, unsigned long offset)
457{
458 switch (offset) {
459 case offsetof(struct user_regs_struct, cs):
460 case offsetof(struct user_regs_struct, ds):
461 case offsetof(struct user_regs_struct, es):
462 case offsetof(struct user_regs_struct, fs):
463 case offsetof(struct user_regs_struct, gs):
464 case offsetof(struct user_regs_struct, ss):
465 return get_segment_reg(task, offset);
466
467 case offsetof(struct user_regs_struct, flags):
468 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100469
470#ifdef CONFIG_X86_64
471 case offsetof(struct user_regs_struct, fs_base): {
472 /*
473 * do_arch_prctl may have used a GDT slot instead of
474 * the MSR. To userland, it appears the same either
475 * way, except the %fs segment selector might not be 0.
476 */
477 unsigned int seg = task->thread.fsindex;
478 if (task->thread.fs != 0)
479 return task->thread.fs;
480 if (task == current)
481 asm("movl %%fs,%0" : "=r" (seg));
482 if (seg != FS_TLS_SEL)
483 return 0;
484 return get_desc_base(&task->thread.tls_array[FS_TLS]);
485 }
486 case offsetof(struct user_regs_struct, gs_base): {
487 /*
488 * Exactly the same here as the %fs handling above.
489 */
490 unsigned int seg = task->thread.gsindex;
491 if (task->thread.gs != 0)
492 return task->thread.gs;
493 if (task == current)
494 asm("movl %%gs,%0" : "=r" (seg));
495 if (seg != GS_TLS_SEL)
496 return 0;
497 return get_desc_base(&task->thread.tls_array[GS_TLS]);
498 }
499#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100500 }
501
502 return *pt_regs_access(task_pt_regs(task), offset);
503}
504
Roland McGrath91e7b702008-01-30 13:31:52 +0100505static int genregs_get(struct task_struct *target,
506 const struct user_regset *regset,
507 unsigned int pos, unsigned int count,
508 void *kbuf, void __user *ubuf)
509{
510 if (kbuf) {
511 unsigned long *k = kbuf;
512 while (count > 0) {
513 *k++ = getreg(target, pos);
514 count -= sizeof(*k);
515 pos += sizeof(*k);
516 }
517 } else {
518 unsigned long __user *u = ubuf;
519 while (count > 0) {
520 if (__put_user(getreg(target, pos), u++))
521 return -EFAULT;
522 count -= sizeof(*u);
523 pos += sizeof(*u);
524 }
525 }
526
527 return 0;
528}
529
530static int genregs_set(struct task_struct *target,
531 const struct user_regset *regset,
532 unsigned int pos, unsigned int count,
533 const void *kbuf, const void __user *ubuf)
534{
535 int ret = 0;
536 if (kbuf) {
537 const unsigned long *k = kbuf;
538 while (count > 0 && !ret) {
539 ret = putreg(target, pos, *k++);
540 count -= sizeof(*k);
541 pos += sizeof(*k);
542 }
543 } else {
544 const unsigned long __user *u = ubuf;
545 while (count > 0 && !ret) {
546 unsigned long word;
547 ret = __get_user(word, u++);
548 if (ret)
549 break;
550 ret = putreg(target, pos, word);
551 count -= sizeof(*u);
552 pos += sizeof(*u);
553 }
554 }
555 return ret;
556}
557
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200558static void ptrace_triggered(struct perf_event *bp, void *data)
Roland McGrathd9771e82008-01-30 13:30:52 +0100559{
Roland McGrath0f534092008-01-30 13:30:59 +0100560 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200561 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100562
K.Prasad72f674d2009-06-01 23:45:48 +0530563 /*
564 * Store in the virtual DR6 register the fact that the breakpoint
565 * was hit so the thread's debugger will see it.
566 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200567 for (i = 0; i < HBP_NUM; i++) {
568 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530569 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100570 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100571
K.Prasad72f674d2009-06-01 23:45:48 +0530572 thread->debugreg6 |= (DR_TRAP0 << i);
573}
574
575/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200576 * Walk through every ptrace breakpoints for this thread and
577 * build the dr7 value on top of their attributes.
578 *
579 */
580static unsigned long ptrace_get_dr7(struct perf_event *bp[])
581{
582 int i;
583 int dr7 = 0;
584 struct arch_hw_breakpoint *info;
585
586 for (i = 0; i < HBP_NUM; i++) {
587 if (bp[i] && !bp[i]->attr.disabled) {
588 info = counter_arch_bp(bp[i]);
589 dr7 |= encode_dr7(i, info->len, info->type);
590 }
591 }
592
593 return dr7;
594}
595
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100596static struct perf_event *
597ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100598 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100599{
600 int err;
601 int gen_len, gen_type;
602 DEFINE_BREAKPOINT_ATTR(attr);
603
604 /*
605 * We shoud have at least an inactive breakpoint at this
606 * slot. It means the user is writing dr7 without having
607 * written the address register first
608 */
609 if (!bp)
610 return ERR_PTR(-EINVAL);
611
612 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
613 if (err)
614 return ERR_PTR(err);
615
616 attr = bp->attr;
617 attr.bp_len = gen_len;
618 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100619 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100620
621 return modify_user_hw_breakpoint(bp, &attr, bp->callback, tsk);
622}
623
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200624/*
K.Prasad72f674d2009-06-01 23:45:48 +0530625 * Handle ptrace writes to debug register 7.
626 */
627static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
628{
629 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200630 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530631 int i, orig_ret = 0, rc = 0;
632 int enabled, second_pass = 0;
633 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200634 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530635
636 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200637 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530638restore:
639 /*
640 * Loop through all the hardware breakpoints, making the
641 * appropriate changes to each.
642 */
643 for (i = 0; i < HBP_NUM; i++) {
644 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200645 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530646
647 if (!enabled) {
648 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200649 /*
650 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530651 * unless all register_user_hw_breakpoint()
652 * requests have succeeded. This prevents
653 * any window of opportunity for debug
654 * register grabbing by other users.
655 */
656 if (!second_pass)
657 continue;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100658
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200659 thread->ptrace_bps[i] = NULL;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100660 bp = ptrace_modify_breakpoint(bp, len, type,
661 tsk, 1);
662 if (IS_ERR(bp)) {
663 rc = PTR_ERR(bp);
664 thread->ptrace_bps[i] = NULL;
665 break;
666 }
667 thread->ptrace_bps[i] = bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530668 }
669 continue;
670 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200671
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100672 bp = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200673
Frederic Weisbecker605bfae2009-11-26 05:35:42 +0100674 /* Incorrect bp, or we have a bug in bp API */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200675 if (IS_ERR(bp)) {
676 rc = PTR_ERR(bp);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100677 thread->ptrace_bps[i] = NULL;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200678 break;
679 }
680 thread->ptrace_bps[i] = bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530681 }
682 /*
683 * Make a second pass to free the remaining unused breakpoints
684 * or to restore the original breakpoints if an error occurred.
685 */
686 if (!second_pass) {
687 second_pass = 1;
688 if (rc < 0) {
689 orig_ret = rc;
690 data = old_dr7;
691 }
692 goto restore;
693 }
694 return ((orig_ret < 0) ? orig_ret : rc);
695}
696
697/*
698 * Handle PTRACE_PEEKUSR calls for the debug register area.
699 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530700static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530701{
702 struct thread_struct *thread = &(tsk->thread);
703 unsigned long val = 0;
704
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200705 if (n < HBP_NUM) {
706 struct perf_event *bp;
707 bp = thread->ptrace_bps[n];
708 if (!bp)
709 return 0;
710 val = bp->hw.info.address;
711 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530712 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200713 } else if (n == 7) {
714 val = ptrace_get_dr7(thread->ptrace_bps);
715 }
K.Prasad72f674d2009-06-01 23:45:48 +0530716 return val;
717}
718
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200719static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
720 unsigned long addr)
721{
722 struct perf_event *bp;
723 struct thread_struct *t = &tsk->thread;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100724 DEFINE_BREAKPOINT_ATTR(attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200725
726 if (!t->ptrace_bps[nr]) {
727 /*
728 * Put stub len and type to register (reserve) an inactive but
729 * correct bp
730 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100731 attr.bp_addr = addr;
732 attr.bp_len = HW_BREAKPOINT_LEN_1;
733 attr.bp_type = HW_BREAKPOINT_W;
734 attr.disabled = 1;
735
736 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200737 } else {
738 bp = t->ptrace_bps[nr];
739 t->ptrace_bps[nr] = NULL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100740
741 attr = bp->attr;
742 attr.bp_addr = addr;
743 bp = modify_user_hw_breakpoint(bp, &attr, bp->callback, tsk);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200744 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200745 /*
746 * CHECKME: the previous code returned -EIO if the addr wasn't a
747 * valid task virtual addr. The new one will return -EINVAL in this
748 * case.
749 * -EINVAL may be what we want for in-kernel breakpoints users, but
750 * -EIO looks better for ptrace, since we refuse a register writing
751 * for the user. And anyway this is the previous behaviour.
752 */
753 if (IS_ERR(bp))
754 return PTR_ERR(bp);
755
756 t->ptrace_bps[nr] = bp;
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759}
760
K.Prasad72f674d2009-06-01 23:45:48 +0530761/*
762 * Handle PTRACE_POKEUSR calls for the debug register area.
763 */
764int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
K.Prasad72f674d2009-06-01 23:45:48 +0530766 struct thread_struct *thread = &(tsk->thread);
767 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
K.Prasad72f674d2009-06-01 23:45:48 +0530769 /* There are no DR4 or DR5 registers */
770 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -EIO;
772
K.Prasad72f674d2009-06-01 23:45:48 +0530773 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200774 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530775 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
K.Prasad72f674d2009-06-01 23:45:48 +0530777 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200778 rc = ptrace_set_breakpoint_addr(tsk, n, val);
779 if (rc)
780 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530781 }
782 /* All that's left is DR7 */
783 if (n == 7)
784 rc = ptrace_write_dr7(tsk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
K.Prasad72f674d2009-06-01 23:45:48 +0530786ret_path:
787 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Roland McGrath325af5f2008-08-08 15:58:39 -0700790/*
791 * These access the current or another (stopped) task's io permission
792 * bitmap for debugging or core dump.
793 */
794static int ioperm_active(struct task_struct *target,
795 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100796{
Roland McGrath325af5f2008-08-08 15:58:39 -0700797 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100798}
799
Roland McGrath325af5f2008-08-08 15:58:39 -0700800static int ioperm_get(struct task_struct *target,
801 const struct user_regset *regset,
802 unsigned int pos, unsigned int count,
803 void *kbuf, void __user *ubuf)
804{
805 if (!target->thread.io_bitmap_ptr)
806 return -ENXIO;
807
808 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
809 target->thread.io_bitmap_ptr,
810 0, IO_BITMAP_BYTES);
811}
812
Markus Metzger93fa7632008-04-08 11:01:58 +0200813#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200814/*
815 * A branch trace store context.
816 *
817 * Contexts may only be installed by ptrace_bts_config() and only for
818 * ptraced tasks.
819 *
820 * Contexts are destroyed when the tracee is detached from the tracer.
821 * The actual destruction work requires interrupts enabled, so the
822 * work is deferred and will be scheduled during __ptrace_unlink().
823 *
824 * Contexts hold an additional task_struct reference on the traced
825 * task, as well as a reference on the tracer's mm.
826 *
827 * Ptrace already holds a task_struct for the duration of ptrace operations,
828 * but since destruction is deferred, it may be executed after both
829 * tracer and tracee exited.
830 */
831struct bts_context {
832 /* The branch trace handle. */
833 struct bts_tracer *tracer;
834
835 /* The buffer used to store the branch trace and its size. */
836 void *buffer;
837 unsigned int size;
838
839 /* The mm that paid for the above buffer. */
840 struct mm_struct *mm;
841
842 /* The task this context belongs to. */
843 struct task_struct *task;
844
845 /* The signal to send on a bts buffer overflow. */
846 unsigned int bts_ovfl_signal;
847
848 /* The work struct to destroy a context. */
849 struct work_struct work;
850};
851
Markus Metzger1cb81b12009-04-24 09:51:43 +0200852static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200853{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200854 void *buffer = NULL;
855 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200856
Markus Metzger1cb81b12009-04-24 09:51:43 +0200857 err = account_locked_memory(current->mm, current->signal->rlim, size);
858 if (err < 0)
859 return err;
860
861 buffer = kzalloc(size, GFP_KERNEL);
862 if (!buffer)
863 goto out_refund;
864
865 context->buffer = buffer;
866 context->size = size;
867 context->mm = get_task_mm(current);
868
869 return 0;
870
871 out_refund:
872 refund_locked_memory(current->mm, size);
873 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200874}
875
876static inline void free_bts_buffer(struct bts_context *context)
877{
878 if (!context->buffer)
879 return;
880
881 kfree(context->buffer);
882 context->buffer = NULL;
883
Markus Metzger1cb81b12009-04-24 09:51:43 +0200884 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200885 context->size = 0;
886
887 mmput(context->mm);
888 context->mm = NULL;
889}
890
891static void free_bts_context_work(struct work_struct *w)
892{
893 struct bts_context *context;
894
895 context = container_of(w, struct bts_context, work);
896
897 ds_release_bts(context->tracer);
898 put_task_struct(context->task);
899 free_bts_buffer(context);
900 kfree(context);
901}
902
903static inline void free_bts_context(struct bts_context *context)
904{
905 INIT_WORK(&context->work, free_bts_context_work);
906 schedule_work(&context->work);
907}
908
909static inline struct bts_context *alloc_bts_context(struct task_struct *task)
910{
911 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
912 if (context) {
913 context->task = task;
914 task->bts = context;
915
916 get_task_struct(task);
917 }
918
919 return context;
920}
921
Markus Metzger93fa7632008-04-08 11:01:58 +0200922static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100923 struct bts_struct __user *out)
924{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200925 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100926 const struct bts_trace *trace;
927 struct bts_struct bts;
928 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200929 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100930
Markus Metzgere2b371f2009-04-03 16:43:35 +0200931 context = child->bts;
932 if (!context)
933 return -ESRCH;
934
935 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100936 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200937 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100938
939 at = trace->ds.top - ((index + 1) * trace->ds.size);
940 if ((void *)at < trace->ds.begin)
941 at += (trace->ds.n * trace->ds.size);
942
943 if (!trace->read)
944 return -EOPNOTSUPP;
945
Markus Metzgere2b371f2009-04-03 16:43:35 +0200946 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200947 if (error < 0)
948 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100949
Markus Metzgerc2724772008-12-11 13:49:59 +0100950 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100951 return -EFAULT;
952
Markus Metzgerc2724772008-12-11 13:49:59 +0100953 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100954}
955
Markus Metzgera95d67f2008-01-30 13:31:20 +0100956static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100957 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100958 struct bts_struct __user *out)
959{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200960 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100961 const struct bts_trace *trace;
962 const unsigned char *at;
963 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100964
Markus Metzgere2b371f2009-04-03 16:43:35 +0200965 context = child->bts;
966 if (!context)
967 return -ESRCH;
968
969 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100970 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200971 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100972
Markus Metzgerc2724772008-12-11 13:49:59 +0100973 if (!trace->read)
974 return -EOPNOTSUPP;
975
976 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100977 return -EIO;
978
Markus Metzgerc2724772008-12-11 13:49:59 +0100979 for (at = trace->ds.begin; (void *)at < trace->ds.top;
980 out++, drained++, at += trace->ds.size) {
981 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100982
Markus Metzgere2b371f2009-04-03 16:43:35 +0200983 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100984 if (error < 0)
985 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100986
Markus Metzgerc2724772008-12-11 13:49:59 +0100987 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100988 return -EFAULT;
989 }
990
Markus Metzgerc2724772008-12-11 13:49:59 +0100991 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
992
Markus Metzgere2b371f2009-04-03 16:43:35 +0200993 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200994 if (error < 0)
995 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100996
Markus Metzgerc2724772008-12-11 13:49:59 +0100997 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100998}
999
1000static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +01001001 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +01001002 const struct ptrace_bts_config __user *ucfg)
1003{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001004 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001005 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +01001006 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001007
Markus Metzgercba4b652008-01-30 13:32:03 +01001008 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +01001009 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +01001010
Markus Metzgera95d67f2008-01-30 13:31:20 +01001011 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +01001012 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001013
Markus Metzgere2b371f2009-04-03 16:43:35 +02001014 context = child->bts;
1015 if (!context)
1016 context = alloc_bts_context(child);
1017 if (!context)
1018 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001019
Markus Metzgerc2724772008-12-11 13:49:59 +01001020 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1021 if (!cfg.signal)
1022 return -EINVAL;
1023
Américo Wang5a8ac9d2009-03-13 15:56:58 +08001024 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +02001025 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +01001026 }
1027
Markus Metzgere2b371f2009-04-03 16:43:35 +02001028 ds_release_bts(context->tracer);
1029 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001030
Markus Metzgere2b371f2009-04-03 16:43:35 +02001031 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +02001032 int err;
1033
Markus Metzgere2b371f2009-04-03 16:43:35 +02001034 free_bts_buffer(context);
1035 if (!cfg.size)
1036 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +01001037
Markus Metzger1cb81b12009-04-24 09:51:43 +02001038 err = alloc_bts_buffer(context, cfg.size);
1039 if (err < 0)
1040 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +01001041 }
Markus Metzgera95d67f2008-01-30 13:31:20 +01001042
Markus Metzgerda35c372008-01-30 13:32:03 +01001043 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +01001044 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +01001045
Markus Metzgerda35c372008-01-30 13:32:03 +01001046 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +01001047 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +01001048
Markus Metzgerde79f542009-04-03 16:43:40 +02001049 context->tracer =
1050 ds_request_bts_task(child, context->buffer, context->size,
1051 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +02001052 if (unlikely(IS_ERR(context->tracer))) {
1053 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +01001054
Markus Metzgere2b371f2009-04-03 16:43:35 +02001055 free_bts_buffer(context);
1056 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001057 return error;
1058 }
Markus Metzgerda35c372008-01-30 13:32:03 +01001059
Markus Metzgerc2724772008-12-11 13:49:59 +01001060 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001061}
1062
Markus Metzgera95d67f2008-01-30 13:31:20 +01001063static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +01001064 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +01001065 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +01001066{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001067 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001068 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001069 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +01001070
Markus Metzgere2b371f2009-04-03 16:43:35 +02001071 context = child->bts;
1072 if (!context)
1073 return -ESRCH;
1074
Markus Metzgercba4b652008-01-30 13:32:03 +01001075 if (cfg_size < sizeof(cfg))
1076 return -EIO;
1077
Markus Metzgere2b371f2009-04-03 16:43:35 +02001078 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001079 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001080 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001081
Markus Metzgera95d67f2008-01-30 13:31:20 +01001082 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +02001083 cfg.size = trace->ds.end - trace->ds.begin;
1084 cfg.signal = context->bts_ovfl_signal;
1085 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +01001086
Markus Metzger93fa7632008-04-08 11:01:58 +02001087 if (cfg.signal)
1088 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1089
Markus Metzgerc2724772008-12-11 13:49:59 +01001090 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +02001091 cfg.flags |= PTRACE_BTS_O_TRACE;
1092
Markus Metzgerc2724772008-12-11 13:49:59 +01001093 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +02001094 cfg.flags |= PTRACE_BTS_O_SCHED;
1095
Markus Metzgera95d67f2008-01-30 13:31:20 +01001096 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1097 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +01001098
Markus Metzgera95d67f2008-01-30 13:31:20 +01001099 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001100}
1101
Markus Metzgerc2724772008-12-11 13:49:59 +01001102static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -08001103{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001104 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001105 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001106
Markus Metzgere2b371f2009-04-03 16:43:35 +02001107 context = child->bts;
1108 if (!context)
1109 return -ESRCH;
1110
1111 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001112 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001113 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001114
Markus Metzgerc2724772008-12-11 13:49:59 +01001115 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001116
Markus Metzgere2b371f2009-04-03 16:43:35 +02001117 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001118}
1119
Markus Metzgerc2724772008-12-11 13:49:59 +01001120static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +01001121{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001122 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001123 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +01001124
Markus Metzgere2b371f2009-04-03 16:43:35 +02001125 context = child->bts;
1126 if (!context)
1127 return -ESRCH;
1128
1129 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001130 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001131 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001132
Markus Metzgerc2724772008-12-11 13:49:59 +01001133 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +02001134}
Markus Metzgerbf53de92008-12-19 15:10:24 +01001135
Markus Metzgere2b371f2009-04-03 16:43:35 +02001136/*
1137 * Called from __ptrace_unlink() after the child has been moved back
1138 * to its original parent.
1139 */
Markus Metzger0f481402009-04-03 16:43:48 +02001140void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +01001141{
1142 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +02001143 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +01001144 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +01001145 }
1146}
Markus Metzger93fa7632008-04-08 11:01:58 +02001147#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149/*
1150 * Called by kernel/ptrace.c when detaching..
1151 *
1152 * Make sure the single step bit is not set.
1153 */
1154void ptrace_disable(struct task_struct *child)
1155{
1156 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001157#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Roland McGrath5a4646a2008-01-30 13:31:54 +01001162#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1163static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1164#endif
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1167{
Roland McGrath5a4646a2008-01-30 13:31:54 +01001168 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 unsigned long __user *datap = (unsigned long __user *)data;
1170
1171 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* read the word at location addr in the USER area. */
1173 case PTRACE_PEEKUSR: {
1174 unsigned long tmp;
1175
1176 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001177 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1178 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180
1181 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001182 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001184 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1185 addr <= offsetof(struct user, u_debugreg[7])) {
1186 addr -= offsetof(struct user, u_debugreg[0]);
1187 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 ret = put_user(tmp, datap);
1190 break;
1191 }
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1194 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001195 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1196 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198
Roland McGrathe9c86c72008-01-30 13:31:01 +01001199 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001201 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1202 addr <= offsetof(struct user, u_debugreg[7])) {
1203 addr -= offsetof(struct user, u_debugreg[0]);
1204 ret = ptrace_set_debugreg(child,
1205 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Roland McGrath5a4646a2008-01-30 13:31:54 +01001209 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1210 return copy_regset_to_user(child,
1211 task_user_regset_view(current),
1212 REGSET_GENERAL,
1213 0, sizeof(struct user_regs_struct),
1214 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Roland McGrath5a4646a2008-01-30 13:31:54 +01001216 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1217 return copy_regset_from_user(child,
1218 task_user_regset_view(current),
1219 REGSET_GENERAL,
1220 0, sizeof(struct user_regs_struct),
1221 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Roland McGrath5a4646a2008-01-30 13:31:54 +01001223 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1224 return copy_regset_to_user(child,
1225 task_user_regset_view(current),
1226 REGSET_FP,
1227 0, sizeof(struct user_i387_struct),
1228 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Roland McGrath5a4646a2008-01-30 13:31:54 +01001230 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1231 return copy_regset_from_user(child,
1232 task_user_regset_view(current),
1233 REGSET_FP,
1234 0, sizeof(struct user_i387_struct),
1235 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Roland McGrathe9c86c72008-01-30 13:31:01 +01001237#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001238 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1239 return copy_regset_to_user(child, &user_x86_32_view,
1240 REGSET_XFP,
1241 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001242 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Roland McGrath5a4646a2008-01-30 13:31:54 +01001244 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1245 return copy_regset_from_user(child, &user_x86_32_view,
1246 REGSET_XFP,
1247 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001248 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Roland McGrathe9c86c72008-01-30 13:31:01 +01001251#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001253 if (addr < 0)
1254 return -EIO;
1255 ret = do_get_thread_area(child, addr,
1256 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
1258
1259 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001260 if (addr < 0)
1261 return -EIO;
1262 ret = do_set_thread_area(child, addr,
1263 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001265#endif
1266
1267#ifdef CONFIG_X86_64
1268 /* normal 64bit interface to access TLS data.
1269 Works just like arch_prctl, except that the arguments
1270 are reversed. */
1271 case PTRACE_ARCH_PRCTL:
1272 ret = do_arch_prctl(child, data, addr);
1273 break;
1274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001276 /*
1277 * These bits need more cooking - not enabled yet:
1278 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001279#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001280 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001281 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001282 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001283 break;
1284
1285 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001286 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001287 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001288 break;
1289
Markus Metzgerc2724772008-12-11 13:49:59 +01001290 case PTRACE_BTS_SIZE:
1291 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001292 break;
1293
1294 case PTRACE_BTS_GET:
1295 ret = ptrace_bts_read_record
1296 (child, data, (struct bts_struct __user *) addr);
1297 break;
1298
1299 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001300 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001301 break;
1302
1303 case PTRACE_BTS_DRAIN:
1304 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001305 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001306 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001307#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 default:
1310 ret = ptrace_request(child, request, addr, data);
1311 break;
1312 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return ret;
1315}
1316
Roland McGrathcb757c42008-01-30 13:31:01 +01001317#ifdef CONFIG_IA32_EMULATION
1318
Roland McGrath099cd6e2008-01-30 13:31:01 +01001319#include <linux/compat.h>
1320#include <linux/syscalls.h>
1321#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001322#include <asm/user32.h>
1323
1324#define R32(l,q) \
1325 case offsetof(struct user32, regs.l): \
1326 regs->q = value; break
1327
1328#define SEG32(rs) \
1329 case offsetof(struct user32, regs.rs): \
1330 return set_segment_reg(child, \
1331 offsetof(struct user_regs_struct, rs), \
1332 value); \
1333 break
1334
1335static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1336{
1337 struct pt_regs *regs = task_pt_regs(child);
1338
1339 switch (regno) {
1340
1341 SEG32(cs);
1342 SEG32(ds);
1343 SEG32(es);
1344 SEG32(fs);
1345 SEG32(gs);
1346 SEG32(ss);
1347
1348 R32(ebx, bx);
1349 R32(ecx, cx);
1350 R32(edx, dx);
1351 R32(edi, di);
1352 R32(esi, si);
1353 R32(ebp, bp);
1354 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001355 R32(eip, ip);
1356 R32(esp, sp);
1357
Roland McGrath40f09332008-02-28 19:57:07 -08001358 case offsetof(struct user32, regs.orig_eax):
1359 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001360 * A 32-bit debugger setting orig_eax means to restore
1361 * the state of the task restarting a 32-bit syscall.
1362 * Make sure we interpret the -ERESTART* codes correctly
1363 * in case the task is not actually still sitting at the
1364 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001365 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001366 regs->orig_ax = value;
1367 if (syscall_get_nr(child, regs) >= 0)
1368 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001369 break;
1370
Roland McGrathcb757c42008-01-30 13:31:01 +01001371 case offsetof(struct user32, regs.eflags):
1372 return set_flags(child, value);
1373
1374 case offsetof(struct user32, u_debugreg[0]) ...
1375 offsetof(struct user32, u_debugreg[7]):
1376 regno -= offsetof(struct user32, u_debugreg[0]);
1377 return ptrace_set_debugreg(child, regno / 4, value);
1378
1379 default:
1380 if (regno > sizeof(struct user32) || (regno & 3))
1381 return -EIO;
1382
1383 /*
1384 * Other dummy fields in the virtual user structure
1385 * are ignored
1386 */
1387 break;
1388 }
1389 return 0;
1390}
1391
1392#undef R32
1393#undef SEG32
1394
1395#define R32(l,q) \
1396 case offsetof(struct user32, regs.l): \
1397 *val = regs->q; break
1398
1399#define SEG32(rs) \
1400 case offsetof(struct user32, regs.rs): \
1401 *val = get_segment_reg(child, \
1402 offsetof(struct user_regs_struct, rs)); \
1403 break
1404
1405static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1406{
1407 struct pt_regs *regs = task_pt_regs(child);
1408
1409 switch (regno) {
1410
1411 SEG32(ds);
1412 SEG32(es);
1413 SEG32(fs);
1414 SEG32(gs);
1415
1416 R32(cs, cs);
1417 R32(ss, ss);
1418 R32(ebx, bx);
1419 R32(ecx, cx);
1420 R32(edx, dx);
1421 R32(edi, di);
1422 R32(esi, si);
1423 R32(ebp, bp);
1424 R32(eax, ax);
1425 R32(orig_eax, orig_ax);
1426 R32(eip, ip);
1427 R32(esp, sp);
1428
1429 case offsetof(struct user32, regs.eflags):
1430 *val = get_flags(child);
1431 break;
1432
1433 case offsetof(struct user32, u_debugreg[0]) ...
1434 offsetof(struct user32, u_debugreg[7]):
1435 regno -= offsetof(struct user32, u_debugreg[0]);
1436 *val = ptrace_get_debugreg(child, regno / 4);
1437 break;
1438
1439 default:
1440 if (regno > sizeof(struct user32) || (regno & 3))
1441 return -EIO;
1442
1443 /*
1444 * Other dummy fields in the virtual user structure
1445 * are ignored
1446 */
1447 *val = 0;
1448 break;
1449 }
1450 return 0;
1451}
1452
1453#undef R32
1454#undef SEG32
1455
Roland McGrath91e7b702008-01-30 13:31:52 +01001456static int genregs32_get(struct task_struct *target,
1457 const struct user_regset *regset,
1458 unsigned int pos, unsigned int count,
1459 void *kbuf, void __user *ubuf)
1460{
1461 if (kbuf) {
1462 compat_ulong_t *k = kbuf;
1463 while (count > 0) {
1464 getreg32(target, pos, k++);
1465 count -= sizeof(*k);
1466 pos += sizeof(*k);
1467 }
1468 } else {
1469 compat_ulong_t __user *u = ubuf;
1470 while (count > 0) {
1471 compat_ulong_t word;
1472 getreg32(target, pos, &word);
1473 if (__put_user(word, u++))
1474 return -EFAULT;
1475 count -= sizeof(*u);
1476 pos += sizeof(*u);
1477 }
1478 }
1479
1480 return 0;
1481}
1482
1483static int genregs32_set(struct task_struct *target,
1484 const struct user_regset *regset,
1485 unsigned int pos, unsigned int count,
1486 const void *kbuf, const void __user *ubuf)
1487{
1488 int ret = 0;
1489 if (kbuf) {
1490 const compat_ulong_t *k = kbuf;
1491 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001492 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001493 count -= sizeof(*k);
1494 pos += sizeof(*k);
1495 }
1496 } else {
1497 const compat_ulong_t __user *u = ubuf;
1498 while (count > 0 && !ret) {
1499 compat_ulong_t word;
1500 ret = __get_user(word, u++);
1501 if (ret)
1502 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001503 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001504 count -= sizeof(*u);
1505 pos += sizeof(*u);
1506 }
1507 }
1508 return ret;
1509}
1510
Roland McGrath562b80b2008-04-22 12:21:25 -07001511long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1512 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001513{
Roland McGrath562b80b2008-04-22 12:21:25 -07001514 unsigned long addr = caddr;
1515 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001516 void __user *datap = compat_ptr(data);
1517 int ret;
1518 __u32 val;
1519
1520 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001521 case PTRACE_PEEKUSR:
1522 ret = getreg32(child, addr, &val);
1523 if (ret == 0)
1524 ret = put_user(val, (__u32 __user *)datap);
1525 break;
1526
1527 case PTRACE_POKEUSR:
1528 ret = putreg32(child, addr, data);
1529 break;
1530
Roland McGrath5a4646a2008-01-30 13:31:54 +01001531 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1532 return copy_regset_to_user(child, &user_x86_32_view,
1533 REGSET_GENERAL,
1534 0, sizeof(struct user_regs_struct32),
1535 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001536
Roland McGrath5a4646a2008-01-30 13:31:54 +01001537 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1538 return copy_regset_from_user(child, &user_x86_32_view,
1539 REGSET_GENERAL, 0,
1540 sizeof(struct user_regs_struct32),
1541 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001542
Roland McGrath5a4646a2008-01-30 13:31:54 +01001543 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1544 return copy_regset_to_user(child, &user_x86_32_view,
1545 REGSET_FP, 0,
1546 sizeof(struct user_i387_ia32_struct),
1547 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001548
Roland McGrath5a4646a2008-01-30 13:31:54 +01001549 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1550 return copy_regset_from_user(
1551 child, &user_x86_32_view, REGSET_FP,
1552 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001553
Roland McGrath5a4646a2008-01-30 13:31:54 +01001554 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1555 return copy_regset_to_user(child, &user_x86_32_view,
1556 REGSET_XFP, 0,
1557 sizeof(struct user32_fxsr_struct),
1558 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001559
Roland McGrath5a4646a2008-01-30 13:31:54 +01001560 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1561 return copy_regset_from_user(child, &user_x86_32_view,
1562 REGSET_XFP, 0,
1563 sizeof(struct user32_fxsr_struct),
1564 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001565
Roland McGrath562b80b2008-04-22 12:21:25 -07001566 case PTRACE_GET_THREAD_AREA:
1567 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001568#ifdef CONFIG_X86_PTRACE_BTS
1569 case PTRACE_BTS_CONFIG:
1570 case PTRACE_BTS_STATUS:
1571 case PTRACE_BTS_SIZE:
1572 case PTRACE_BTS_GET:
1573 case PTRACE_BTS_CLEAR:
1574 case PTRACE_BTS_DRAIN:
1575#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001576 return arch_ptrace(child, request, addr, data);
1577
Roland McGrath099cd6e2008-01-30 13:31:01 +01001578 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001579 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001580 }
1581
Roland McGrath099cd6e2008-01-30 13:31:01 +01001582 return ret;
1583}
1584
Roland McGrathcb757c42008-01-30 13:31:01 +01001585#endif /* CONFIG_IA32_EMULATION */
1586
Roland McGrath070459d2008-01-30 13:31:53 +01001587#ifdef CONFIG_X86_64
1588
1589static const struct user_regset x86_64_regsets[] = {
1590 [REGSET_GENERAL] = {
1591 .core_note_type = NT_PRSTATUS,
1592 .n = sizeof(struct user_regs_struct) / sizeof(long),
1593 .size = sizeof(long), .align = sizeof(long),
1594 .get = genregs_get, .set = genregs_set
1595 },
1596 [REGSET_FP] = {
1597 .core_note_type = NT_PRFPREG,
1598 .n = sizeof(struct user_i387_struct) / sizeof(long),
1599 .size = sizeof(long), .align = sizeof(long),
1600 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1601 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001602 [REGSET_IOPERM64] = {
1603 .core_note_type = NT_386_IOPERM,
1604 .n = IO_BITMAP_LONGS,
1605 .size = sizeof(long), .align = sizeof(long),
1606 .active = ioperm_active, .get = ioperm_get
1607 },
Roland McGrath070459d2008-01-30 13:31:53 +01001608};
1609
1610static const struct user_regset_view user_x86_64_view = {
1611 .name = "x86_64", .e_machine = EM_X86_64,
1612 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1613};
1614
1615#else /* CONFIG_X86_32 */
1616
1617#define user_regs_struct32 user_regs_struct
1618#define genregs32_get genregs_get
1619#define genregs32_set genregs_set
1620
Roland McGrath1f465f42008-05-09 15:43:44 -07001621#define user_i387_ia32_struct user_i387_struct
1622#define user32_fxsr_struct user_fxsr_struct
1623
Roland McGrath070459d2008-01-30 13:31:53 +01001624#endif /* CONFIG_X86_64 */
1625
1626#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1627static const struct user_regset x86_32_regsets[] = {
1628 [REGSET_GENERAL] = {
1629 .core_note_type = NT_PRSTATUS,
1630 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1631 .size = sizeof(u32), .align = sizeof(u32),
1632 .get = genregs32_get, .set = genregs32_set
1633 },
1634 [REGSET_FP] = {
1635 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001636 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001637 .size = sizeof(u32), .align = sizeof(u32),
1638 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1639 },
1640 [REGSET_XFP] = {
1641 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001642 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001643 .size = sizeof(u32), .align = sizeof(u32),
1644 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1645 },
1646 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001647 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001648 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1649 .size = sizeof(struct user_desc),
1650 .align = sizeof(struct user_desc),
1651 .active = regset_tls_active,
1652 .get = regset_tls_get, .set = regset_tls_set
1653 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001654 [REGSET_IOPERM32] = {
1655 .core_note_type = NT_386_IOPERM,
1656 .n = IO_BITMAP_BYTES / sizeof(u32),
1657 .size = sizeof(u32), .align = sizeof(u32),
1658 .active = ioperm_active, .get = ioperm_get
1659 },
Roland McGrath070459d2008-01-30 13:31:53 +01001660};
1661
1662static const struct user_regset_view user_x86_32_view = {
1663 .name = "i386", .e_machine = EM_386,
1664 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1665};
1666#endif
1667
1668const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1669{
1670#ifdef CONFIG_IA32_EMULATION
1671 if (test_tsk_thread_flag(task, TIF_IA32))
1672#endif
1673#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1674 return &user_x86_32_view;
1675#endif
1676#ifdef CONFIG_X86_64
1677 return &user_x86_64_view;
1678#endif
1679}
1680
Srinivasa Dsda654b72008-09-23 15:23:52 +05301681void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1682 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 struct siginfo info;
1685
1686 tsk->thread.trap_no = 1;
1687 tsk->thread.error_code = error_code;
1688
1689 memset(&info, 0, sizeof(info));
1690 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301691 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001693 /* User-mode ip? */
1694 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Simon Arlott27b46d72007-10-20 01:13:56 +02001696 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 force_sig_info(SIGTRAP, &info, tsk);
1698}
1699
Roland McGrath86976cd2008-01-30 13:31:01 +01001700
Roland McGrathd4d67152008-07-09 02:38:07 -07001701#ifdef CONFIG_X86_32
1702# define IS_IA32 1
1703#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001704# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001705#else
1706# define IS_IA32 0
1707#endif
1708
1709/*
1710 * We must return the syscall number to actually look up in the table.
1711 * This can be -1L to skip running any syscall at all.
1712 */
1713asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001714{
Roland McGrathd4d67152008-07-09 02:38:07 -07001715 long ret = 0;
1716
Roland McGrath380fdd72008-07-09 02:39:29 -07001717 /*
1718 * If we stepped into a sysenter/syscall insn, it trapped in
1719 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1720 * If user-mode had set TF itself, then it's still clear from
1721 * do_debug() and we need to set it again to restore the user
1722 * state. If we entered on the slow path, TF was already set.
1723 */
1724 if (test_thread_flag(TIF_SINGLESTEP))
1725 regs->flags |= X86_EFLAGS_TF;
1726
Roland McGrath86976cd2008-01-30 13:31:01 +01001727 /* do the secure computing check first */
1728 secure_computing(regs->orig_ax);
1729
Roland McGrathd4d67152008-07-09 02:38:07 -07001730 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1731 ret = -1L;
1732
Roland McGratheeea3c32008-03-16 23:36:28 -07001733 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1734 tracehook_report_syscall_entry(regs))
1735 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001736
Josh Stone66700002009-08-24 14:43:11 -07001737 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001738 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001739
Roland McGrath86976cd2008-01-30 13:31:01 +01001740 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001741 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001742 audit_syscall_entry(AUDIT_ARCH_I386,
1743 regs->orig_ax,
1744 regs->bx, regs->cx,
1745 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001746#ifdef CONFIG_X86_64
1747 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001748 audit_syscall_entry(AUDIT_ARCH_X86_64,
1749 regs->orig_ax,
1750 regs->di, regs->si,
1751 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001752#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001753 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001754
1755 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001756}
1757
Roland McGrathd4d67152008-07-09 02:38:07 -07001758asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001759{
1760 if (unlikely(current->audit_context))
1761 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1762
Josh Stone66700002009-08-24 14:43:11 -07001763 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001764 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001765
Roland McGrathd4d67152008-07-09 02:38:07 -07001766 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001767 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001768
Roland McGrathd4d67152008-07-09 02:38:07 -07001769 /*
1770 * If TIF_SYSCALL_EMU is set, we only get here because of
1771 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1772 * We already reported this syscall instruction in
1773 * syscall_trace_enter(), so don't do any more now.
1774 */
1775 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1776 return;
1777
1778 /*
1779 * If we are single-stepping, synthesize a trap to follow the
1780 * system call instruction.
1781 */
1782 if (test_thread_flag(TIF_SINGLESTEP) &&
Oleg Nesterov43918f22009-04-02 16:58:00 -07001783 tracehook_consider_fatal_signal(current, SIGTRAP))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301784 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001785}