blob: 75e0cd847bd6b20bf8d99a9de3edbdbd92c191b6 [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
596/*
K.Prasad72f674d2009-06-01 23:45:48 +0530597 * Handle ptrace writes to debug register 7.
598 */
599static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
600{
601 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200602 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530603 int i, orig_ret = 0, rc = 0;
604 int enabled, second_pass = 0;
605 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200606 int gen_len, gen_type;
607 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530608
609 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200610 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530611restore:
612 /*
613 * Loop through all the hardware breakpoints, making the
614 * appropriate changes to each.
615 */
616 for (i = 0; i < HBP_NUM; i++) {
617 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200618 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530619
620 if (!enabled) {
621 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200622 /*
623 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530624 * unless all register_user_hw_breakpoint()
625 * requests have succeeded. This prevents
626 * any window of opportunity for debug
627 * register grabbing by other users.
628 */
629 if (!second_pass)
630 continue;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200631 thread->ptrace_bps[i] = NULL;
632 unregister_hw_breakpoint(bp);
K.Prasad72f674d2009-06-01 23:45:48 +0530633 }
634 continue;
635 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200636
637 /*
638 * We shoud have at least an inactive breakpoint at this
639 * slot. It means the user is writing dr7 without having
640 * written the address register first
641 */
K.Prasad72f674d2009-06-01 23:45:48 +0530642 if (!bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200643 rc = -EINVAL;
644 break;
645 }
646
647 rc = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
K.Prasad72f674d2009-06-01 23:45:48 +0530648 if (rc)
649 break;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200650
651 /*
652 * This is a temporary thing as bp is unregistered/registered
653 * to simulate modification
654 */
655 bp = modify_user_hw_breakpoint(bp, bp->attr.bp_addr, gen_len,
656 gen_type, bp->callback,
657 tsk, true);
658 thread->ptrace_bps[i] = NULL;
659
Frederic Weisbecker605bfae2009-11-26 05:35:42 +0100660 /* Incorrect bp, or we have a bug in bp API */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200661 if (IS_ERR(bp)) {
662 rc = PTR_ERR(bp);
663 bp = NULL;
664 break;
665 }
666 thread->ptrace_bps[i] = bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530667 }
668 /*
669 * Make a second pass to free the remaining unused breakpoints
670 * or to restore the original breakpoints if an error occurred.
671 */
672 if (!second_pass) {
673 second_pass = 1;
674 if (rc < 0) {
675 orig_ret = rc;
676 data = old_dr7;
677 }
678 goto restore;
679 }
680 return ((orig_ret < 0) ? orig_ret : rc);
681}
682
683/*
684 * Handle PTRACE_PEEKUSR calls for the debug register area.
685 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530686static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530687{
688 struct thread_struct *thread = &(tsk->thread);
689 unsigned long val = 0;
690
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200691 if (n < HBP_NUM) {
692 struct perf_event *bp;
693 bp = thread->ptrace_bps[n];
694 if (!bp)
695 return 0;
696 val = bp->hw.info.address;
697 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530698 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200699 } else if (n == 7) {
700 val = ptrace_get_dr7(thread->ptrace_bps);
701 }
K.Prasad72f674d2009-06-01 23:45:48 +0530702 return val;
703}
704
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200705static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
706 unsigned long addr)
707{
708 struct perf_event *bp;
709 struct thread_struct *t = &tsk->thread;
710
711 if (!t->ptrace_bps[nr]) {
712 /*
713 * Put stub len and type to register (reserve) an inactive but
714 * correct bp
715 */
716 bp = register_user_hw_breakpoint(addr, HW_BREAKPOINT_LEN_1,
717 HW_BREAKPOINT_W,
718 ptrace_triggered, tsk,
719 false);
720 } else {
721 bp = t->ptrace_bps[nr];
722 t->ptrace_bps[nr] = NULL;
723 bp = modify_user_hw_breakpoint(bp, addr, bp->attr.bp_len,
724 bp->attr.bp_type,
725 bp->callback,
726 tsk,
727 bp->attr.disabled);
728 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200729 /*
730 * CHECKME: the previous code returned -EIO if the addr wasn't a
731 * valid task virtual addr. The new one will return -EINVAL in this
732 * case.
733 * -EINVAL may be what we want for in-kernel breakpoints users, but
734 * -EIO looks better for ptrace, since we refuse a register writing
735 * for the user. And anyway this is the previous behaviour.
736 */
737 if (IS_ERR(bp))
738 return PTR_ERR(bp);
739
740 t->ptrace_bps[nr] = bp;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return 0;
743}
744
K.Prasad72f674d2009-06-01 23:45:48 +0530745/*
746 * Handle PTRACE_POKEUSR calls for the debug register area.
747 */
748int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
K.Prasad72f674d2009-06-01 23:45:48 +0530750 struct thread_struct *thread = &(tsk->thread);
751 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
K.Prasad72f674d2009-06-01 23:45:48 +0530753 /* There are no DR4 or DR5 registers */
754 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EIO;
756
K.Prasad72f674d2009-06-01 23:45:48 +0530757 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200758 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530759 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
K.Prasad72f674d2009-06-01 23:45:48 +0530761 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200762 rc = ptrace_set_breakpoint_addr(tsk, n, val);
763 if (rc)
764 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530765 }
766 /* All that's left is DR7 */
767 if (n == 7)
768 rc = ptrace_write_dr7(tsk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
K.Prasad72f674d2009-06-01 23:45:48 +0530770ret_path:
771 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Roland McGrath325af5f2008-08-08 15:58:39 -0700774/*
775 * These access the current or another (stopped) task's io permission
776 * bitmap for debugging or core dump.
777 */
778static int ioperm_active(struct task_struct *target,
779 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100780{
Roland McGrath325af5f2008-08-08 15:58:39 -0700781 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100782}
783
Roland McGrath325af5f2008-08-08 15:58:39 -0700784static int ioperm_get(struct task_struct *target,
785 const struct user_regset *regset,
786 unsigned int pos, unsigned int count,
787 void *kbuf, void __user *ubuf)
788{
789 if (!target->thread.io_bitmap_ptr)
790 return -ENXIO;
791
792 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
793 target->thread.io_bitmap_ptr,
794 0, IO_BITMAP_BYTES);
795}
796
Markus Metzger93fa7632008-04-08 11:01:58 +0200797#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200798/*
799 * A branch trace store context.
800 *
801 * Contexts may only be installed by ptrace_bts_config() and only for
802 * ptraced tasks.
803 *
804 * Contexts are destroyed when the tracee is detached from the tracer.
805 * The actual destruction work requires interrupts enabled, so the
806 * work is deferred and will be scheduled during __ptrace_unlink().
807 *
808 * Contexts hold an additional task_struct reference on the traced
809 * task, as well as a reference on the tracer's mm.
810 *
811 * Ptrace already holds a task_struct for the duration of ptrace operations,
812 * but since destruction is deferred, it may be executed after both
813 * tracer and tracee exited.
814 */
815struct bts_context {
816 /* The branch trace handle. */
817 struct bts_tracer *tracer;
818
819 /* The buffer used to store the branch trace and its size. */
820 void *buffer;
821 unsigned int size;
822
823 /* The mm that paid for the above buffer. */
824 struct mm_struct *mm;
825
826 /* The task this context belongs to. */
827 struct task_struct *task;
828
829 /* The signal to send on a bts buffer overflow. */
830 unsigned int bts_ovfl_signal;
831
832 /* The work struct to destroy a context. */
833 struct work_struct work;
834};
835
Markus Metzger1cb81b12009-04-24 09:51:43 +0200836static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200837{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200838 void *buffer = NULL;
839 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200840
Markus Metzger1cb81b12009-04-24 09:51:43 +0200841 err = account_locked_memory(current->mm, current->signal->rlim, size);
842 if (err < 0)
843 return err;
844
845 buffer = kzalloc(size, GFP_KERNEL);
846 if (!buffer)
847 goto out_refund;
848
849 context->buffer = buffer;
850 context->size = size;
851 context->mm = get_task_mm(current);
852
853 return 0;
854
855 out_refund:
856 refund_locked_memory(current->mm, size);
857 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200858}
859
860static inline void free_bts_buffer(struct bts_context *context)
861{
862 if (!context->buffer)
863 return;
864
865 kfree(context->buffer);
866 context->buffer = NULL;
867
Markus Metzger1cb81b12009-04-24 09:51:43 +0200868 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200869 context->size = 0;
870
871 mmput(context->mm);
872 context->mm = NULL;
873}
874
875static void free_bts_context_work(struct work_struct *w)
876{
877 struct bts_context *context;
878
879 context = container_of(w, struct bts_context, work);
880
881 ds_release_bts(context->tracer);
882 put_task_struct(context->task);
883 free_bts_buffer(context);
884 kfree(context);
885}
886
887static inline void free_bts_context(struct bts_context *context)
888{
889 INIT_WORK(&context->work, free_bts_context_work);
890 schedule_work(&context->work);
891}
892
893static inline struct bts_context *alloc_bts_context(struct task_struct *task)
894{
895 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
896 if (context) {
897 context->task = task;
898 task->bts = context;
899
900 get_task_struct(task);
901 }
902
903 return context;
904}
905
Markus Metzger93fa7632008-04-08 11:01:58 +0200906static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100907 struct bts_struct __user *out)
908{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200909 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100910 const struct bts_trace *trace;
911 struct bts_struct bts;
912 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200913 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100914
Markus Metzgere2b371f2009-04-03 16:43:35 +0200915 context = child->bts;
916 if (!context)
917 return -ESRCH;
918
919 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100920 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200921 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100922
923 at = trace->ds.top - ((index + 1) * trace->ds.size);
924 if ((void *)at < trace->ds.begin)
925 at += (trace->ds.n * trace->ds.size);
926
927 if (!trace->read)
928 return -EOPNOTSUPP;
929
Markus Metzgere2b371f2009-04-03 16:43:35 +0200930 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200931 if (error < 0)
932 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100933
Markus Metzgerc2724772008-12-11 13:49:59 +0100934 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100935 return -EFAULT;
936
Markus Metzgerc2724772008-12-11 13:49:59 +0100937 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100938}
939
Markus Metzgera95d67f2008-01-30 13:31:20 +0100940static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100941 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100942 struct bts_struct __user *out)
943{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200944 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100945 const struct bts_trace *trace;
946 const unsigned char *at;
947 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100948
Markus Metzgere2b371f2009-04-03 16:43:35 +0200949 context = child->bts;
950 if (!context)
951 return -ESRCH;
952
953 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100954 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200955 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100956
Markus Metzgerc2724772008-12-11 13:49:59 +0100957 if (!trace->read)
958 return -EOPNOTSUPP;
959
960 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100961 return -EIO;
962
Markus Metzgerc2724772008-12-11 13:49:59 +0100963 for (at = trace->ds.begin; (void *)at < trace->ds.top;
964 out++, drained++, at += trace->ds.size) {
965 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100966
Markus Metzgere2b371f2009-04-03 16:43:35 +0200967 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100968 if (error < 0)
969 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100970
Markus Metzgerc2724772008-12-11 13:49:59 +0100971 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100972 return -EFAULT;
973 }
974
Markus Metzgerc2724772008-12-11 13:49:59 +0100975 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
976
Markus Metzgere2b371f2009-04-03 16:43:35 +0200977 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200978 if (error < 0)
979 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100980
Markus Metzgerc2724772008-12-11 13:49:59 +0100981 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100982}
983
984static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100985 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100986 const struct ptrace_bts_config __user *ucfg)
987{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200988 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100989 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100990 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100991
Markus Metzgercba4b652008-01-30 13:32:03 +0100992 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100993 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100994
Markus Metzgera95d67f2008-01-30 13:31:20 +0100995 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100996 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100997
Markus Metzgere2b371f2009-04-03 16:43:35 +0200998 context = child->bts;
999 if (!context)
1000 context = alloc_bts_context(child);
1001 if (!context)
1002 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001003
Markus Metzgerc2724772008-12-11 13:49:59 +01001004 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1005 if (!cfg.signal)
1006 return -EINVAL;
1007
Américo Wang5a8ac9d2009-03-13 15:56:58 +08001008 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +02001009 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +01001010 }
1011
Markus Metzgere2b371f2009-04-03 16:43:35 +02001012 ds_release_bts(context->tracer);
1013 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001014
Markus Metzgere2b371f2009-04-03 16:43:35 +02001015 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +02001016 int err;
1017
Markus Metzgere2b371f2009-04-03 16:43:35 +02001018 free_bts_buffer(context);
1019 if (!cfg.size)
1020 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +01001021
Markus Metzger1cb81b12009-04-24 09:51:43 +02001022 err = alloc_bts_buffer(context, cfg.size);
1023 if (err < 0)
1024 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +01001025 }
Markus Metzgera95d67f2008-01-30 13:31:20 +01001026
Markus Metzgerda35c372008-01-30 13:32:03 +01001027 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +01001028 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +01001029
Markus Metzgerda35c372008-01-30 13:32:03 +01001030 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +01001031 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +01001032
Markus Metzgerde79f542009-04-03 16:43:40 +02001033 context->tracer =
1034 ds_request_bts_task(child, context->buffer, context->size,
1035 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +02001036 if (unlikely(IS_ERR(context->tracer))) {
1037 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +01001038
Markus Metzgere2b371f2009-04-03 16:43:35 +02001039 free_bts_buffer(context);
1040 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001041 return error;
1042 }
Markus Metzgerda35c372008-01-30 13:32:03 +01001043
Markus Metzgerc2724772008-12-11 13:49:59 +01001044 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001045}
1046
Markus Metzgera95d67f2008-01-30 13:31:20 +01001047static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +01001048 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +01001049 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +01001050{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001051 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001052 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001053 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +01001054
Markus Metzgere2b371f2009-04-03 16:43:35 +02001055 context = child->bts;
1056 if (!context)
1057 return -ESRCH;
1058
Markus Metzgercba4b652008-01-30 13:32:03 +01001059 if (cfg_size < sizeof(cfg))
1060 return -EIO;
1061
Markus Metzgere2b371f2009-04-03 16:43:35 +02001062 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001063 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001064 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001065
Markus Metzgera95d67f2008-01-30 13:31:20 +01001066 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +02001067 cfg.size = trace->ds.end - trace->ds.begin;
1068 cfg.signal = context->bts_ovfl_signal;
1069 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +01001070
Markus Metzger93fa7632008-04-08 11:01:58 +02001071 if (cfg.signal)
1072 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1073
Markus Metzgerc2724772008-12-11 13:49:59 +01001074 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +02001075 cfg.flags |= PTRACE_BTS_O_TRACE;
1076
Markus Metzgerc2724772008-12-11 13:49:59 +01001077 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +02001078 cfg.flags |= PTRACE_BTS_O_SCHED;
1079
Markus Metzgera95d67f2008-01-30 13:31:20 +01001080 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1081 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +01001082
Markus Metzgera95d67f2008-01-30 13:31:20 +01001083 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001084}
1085
Markus Metzgerc2724772008-12-11 13:49:59 +01001086static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -08001087{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001088 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001089 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001090
Markus Metzgere2b371f2009-04-03 16:43:35 +02001091 context = child->bts;
1092 if (!context)
1093 return -ESRCH;
1094
1095 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001096 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001097 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001098
Markus Metzgerc2724772008-12-11 13:49:59 +01001099 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001100
Markus Metzgere2b371f2009-04-03 16:43:35 +02001101 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001102}
1103
Markus Metzgerc2724772008-12-11 13:49:59 +01001104static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +01001105{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001106 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001107 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +01001108
Markus Metzgere2b371f2009-04-03 16:43:35 +02001109 context = child->bts;
1110 if (!context)
1111 return -ESRCH;
1112
1113 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001114 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001115 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001116
Markus Metzgerc2724772008-12-11 13:49:59 +01001117 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +02001118}
Markus Metzgerbf53de92008-12-19 15:10:24 +01001119
Markus Metzgere2b371f2009-04-03 16:43:35 +02001120/*
1121 * Called from __ptrace_unlink() after the child has been moved back
1122 * to its original parent.
1123 */
Markus Metzger0f481402009-04-03 16:43:48 +02001124void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +01001125{
1126 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +02001127 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +01001128 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +01001129 }
1130}
Markus Metzger93fa7632008-04-08 11:01:58 +02001131#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/*
1134 * Called by kernel/ptrace.c when detaching..
1135 *
1136 * Make sure the single step bit is not set.
1137 */
1138void ptrace_disable(struct task_struct *child)
1139{
1140 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001141#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Roland McGrath5a4646a2008-01-30 13:31:54 +01001146#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1147static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1148#endif
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1151{
Roland McGrath5a4646a2008-01-30 13:31:54 +01001152 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 unsigned long __user *datap = (unsigned long __user *)data;
1154
1155 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* read the word at location addr in the USER area. */
1157 case PTRACE_PEEKUSR: {
1158 unsigned long tmp;
1159
1160 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001161 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1162 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
1164
1165 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001166 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001168 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1169 addr <= offsetof(struct user, u_debugreg[7])) {
1170 addr -= offsetof(struct user, u_debugreg[0]);
1171 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 ret = put_user(tmp, datap);
1174 break;
1175 }
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1178 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001179 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1180 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182
Roland McGrathe9c86c72008-01-30 13:31:01 +01001183 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001185 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1186 addr <= offsetof(struct user, u_debugreg[7])) {
1187 addr -= offsetof(struct user, u_debugreg[0]);
1188 ret = ptrace_set_debugreg(child,
1189 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Roland McGrath5a4646a2008-01-30 13:31:54 +01001193 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1194 return copy_regset_to_user(child,
1195 task_user_regset_view(current),
1196 REGSET_GENERAL,
1197 0, sizeof(struct user_regs_struct),
1198 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Roland McGrath5a4646a2008-01-30 13:31:54 +01001200 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1201 return copy_regset_from_user(child,
1202 task_user_regset_view(current),
1203 REGSET_GENERAL,
1204 0, sizeof(struct user_regs_struct),
1205 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Roland McGrath5a4646a2008-01-30 13:31:54 +01001207 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1208 return copy_regset_to_user(child,
1209 task_user_regset_view(current),
1210 REGSET_FP,
1211 0, sizeof(struct user_i387_struct),
1212 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Roland McGrath5a4646a2008-01-30 13:31:54 +01001214 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1215 return copy_regset_from_user(child,
1216 task_user_regset_view(current),
1217 REGSET_FP,
1218 0, sizeof(struct user_i387_struct),
1219 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Roland McGrathe9c86c72008-01-30 13:31:01 +01001221#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001222 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1223 return copy_regset_to_user(child, &user_x86_32_view,
1224 REGSET_XFP,
1225 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001226 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Roland McGrath5a4646a2008-01-30 13:31:54 +01001228 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1229 return copy_regset_from_user(child, &user_x86_32_view,
1230 REGSET_XFP,
1231 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001232 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Roland McGrathe9c86c72008-01-30 13:31:01 +01001235#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001237 if (addr < 0)
1238 return -EIO;
1239 ret = do_get_thread_area(child, addr,
1240 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 break;
1242
1243 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001244 if (addr < 0)
1245 return -EIO;
1246 ret = do_set_thread_area(child, addr,
1247 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001249#endif
1250
1251#ifdef CONFIG_X86_64
1252 /* normal 64bit interface to access TLS data.
1253 Works just like arch_prctl, except that the arguments
1254 are reversed. */
1255 case PTRACE_ARCH_PRCTL:
1256 ret = do_arch_prctl(child, data, addr);
1257 break;
1258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001260 /*
1261 * These bits need more cooking - not enabled yet:
1262 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001263#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001264 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001265 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001266 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001267 break;
1268
1269 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001270 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001271 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001272 break;
1273
Markus Metzgerc2724772008-12-11 13:49:59 +01001274 case PTRACE_BTS_SIZE:
1275 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001276 break;
1277
1278 case PTRACE_BTS_GET:
1279 ret = ptrace_bts_read_record
1280 (child, data, (struct bts_struct __user *) addr);
1281 break;
1282
1283 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001284 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001285 break;
1286
1287 case PTRACE_BTS_DRAIN:
1288 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001289 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001290 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001291#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 default:
1294 ret = ptrace_request(child, request, addr, data);
1295 break;
1296 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return ret;
1299}
1300
Roland McGrathcb757c42008-01-30 13:31:01 +01001301#ifdef CONFIG_IA32_EMULATION
1302
Roland McGrath099cd6e2008-01-30 13:31:01 +01001303#include <linux/compat.h>
1304#include <linux/syscalls.h>
1305#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001306#include <asm/user32.h>
1307
1308#define R32(l,q) \
1309 case offsetof(struct user32, regs.l): \
1310 regs->q = value; break
1311
1312#define SEG32(rs) \
1313 case offsetof(struct user32, regs.rs): \
1314 return set_segment_reg(child, \
1315 offsetof(struct user_regs_struct, rs), \
1316 value); \
1317 break
1318
1319static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1320{
1321 struct pt_regs *regs = task_pt_regs(child);
1322
1323 switch (regno) {
1324
1325 SEG32(cs);
1326 SEG32(ds);
1327 SEG32(es);
1328 SEG32(fs);
1329 SEG32(gs);
1330 SEG32(ss);
1331
1332 R32(ebx, bx);
1333 R32(ecx, cx);
1334 R32(edx, dx);
1335 R32(edi, di);
1336 R32(esi, si);
1337 R32(ebp, bp);
1338 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001339 R32(eip, ip);
1340 R32(esp, sp);
1341
Roland McGrath40f09332008-02-28 19:57:07 -08001342 case offsetof(struct user32, regs.orig_eax):
1343 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001344 * A 32-bit debugger setting orig_eax means to restore
1345 * the state of the task restarting a 32-bit syscall.
1346 * Make sure we interpret the -ERESTART* codes correctly
1347 * in case the task is not actually still sitting at the
1348 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001349 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001350 regs->orig_ax = value;
1351 if (syscall_get_nr(child, regs) >= 0)
1352 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001353 break;
1354
Roland McGrathcb757c42008-01-30 13:31:01 +01001355 case offsetof(struct user32, regs.eflags):
1356 return set_flags(child, value);
1357
1358 case offsetof(struct user32, u_debugreg[0]) ...
1359 offsetof(struct user32, u_debugreg[7]):
1360 regno -= offsetof(struct user32, u_debugreg[0]);
1361 return ptrace_set_debugreg(child, regno / 4, value);
1362
1363 default:
1364 if (regno > sizeof(struct user32) || (regno & 3))
1365 return -EIO;
1366
1367 /*
1368 * Other dummy fields in the virtual user structure
1369 * are ignored
1370 */
1371 break;
1372 }
1373 return 0;
1374}
1375
1376#undef R32
1377#undef SEG32
1378
1379#define R32(l,q) \
1380 case offsetof(struct user32, regs.l): \
1381 *val = regs->q; break
1382
1383#define SEG32(rs) \
1384 case offsetof(struct user32, regs.rs): \
1385 *val = get_segment_reg(child, \
1386 offsetof(struct user_regs_struct, rs)); \
1387 break
1388
1389static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1390{
1391 struct pt_regs *regs = task_pt_regs(child);
1392
1393 switch (regno) {
1394
1395 SEG32(ds);
1396 SEG32(es);
1397 SEG32(fs);
1398 SEG32(gs);
1399
1400 R32(cs, cs);
1401 R32(ss, ss);
1402 R32(ebx, bx);
1403 R32(ecx, cx);
1404 R32(edx, dx);
1405 R32(edi, di);
1406 R32(esi, si);
1407 R32(ebp, bp);
1408 R32(eax, ax);
1409 R32(orig_eax, orig_ax);
1410 R32(eip, ip);
1411 R32(esp, sp);
1412
1413 case offsetof(struct user32, regs.eflags):
1414 *val = get_flags(child);
1415 break;
1416
1417 case offsetof(struct user32, u_debugreg[0]) ...
1418 offsetof(struct user32, u_debugreg[7]):
1419 regno -= offsetof(struct user32, u_debugreg[0]);
1420 *val = ptrace_get_debugreg(child, regno / 4);
1421 break;
1422
1423 default:
1424 if (regno > sizeof(struct user32) || (regno & 3))
1425 return -EIO;
1426
1427 /*
1428 * Other dummy fields in the virtual user structure
1429 * are ignored
1430 */
1431 *val = 0;
1432 break;
1433 }
1434 return 0;
1435}
1436
1437#undef R32
1438#undef SEG32
1439
Roland McGrath91e7b702008-01-30 13:31:52 +01001440static int genregs32_get(struct task_struct *target,
1441 const struct user_regset *regset,
1442 unsigned int pos, unsigned int count,
1443 void *kbuf, void __user *ubuf)
1444{
1445 if (kbuf) {
1446 compat_ulong_t *k = kbuf;
1447 while (count > 0) {
1448 getreg32(target, pos, k++);
1449 count -= sizeof(*k);
1450 pos += sizeof(*k);
1451 }
1452 } else {
1453 compat_ulong_t __user *u = ubuf;
1454 while (count > 0) {
1455 compat_ulong_t word;
1456 getreg32(target, pos, &word);
1457 if (__put_user(word, u++))
1458 return -EFAULT;
1459 count -= sizeof(*u);
1460 pos += sizeof(*u);
1461 }
1462 }
1463
1464 return 0;
1465}
1466
1467static int genregs32_set(struct task_struct *target,
1468 const struct user_regset *regset,
1469 unsigned int pos, unsigned int count,
1470 const void *kbuf, const void __user *ubuf)
1471{
1472 int ret = 0;
1473 if (kbuf) {
1474 const compat_ulong_t *k = kbuf;
1475 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001476 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001477 count -= sizeof(*k);
1478 pos += sizeof(*k);
1479 }
1480 } else {
1481 const compat_ulong_t __user *u = ubuf;
1482 while (count > 0 && !ret) {
1483 compat_ulong_t word;
1484 ret = __get_user(word, u++);
1485 if (ret)
1486 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001487 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001488 count -= sizeof(*u);
1489 pos += sizeof(*u);
1490 }
1491 }
1492 return ret;
1493}
1494
Roland McGrath562b80b2008-04-22 12:21:25 -07001495long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1496 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001497{
Roland McGrath562b80b2008-04-22 12:21:25 -07001498 unsigned long addr = caddr;
1499 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001500 void __user *datap = compat_ptr(data);
1501 int ret;
1502 __u32 val;
1503
1504 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001505 case PTRACE_PEEKUSR:
1506 ret = getreg32(child, addr, &val);
1507 if (ret == 0)
1508 ret = put_user(val, (__u32 __user *)datap);
1509 break;
1510
1511 case PTRACE_POKEUSR:
1512 ret = putreg32(child, addr, data);
1513 break;
1514
Roland McGrath5a4646a2008-01-30 13:31:54 +01001515 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1516 return copy_regset_to_user(child, &user_x86_32_view,
1517 REGSET_GENERAL,
1518 0, sizeof(struct user_regs_struct32),
1519 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001520
Roland McGrath5a4646a2008-01-30 13:31:54 +01001521 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1522 return copy_regset_from_user(child, &user_x86_32_view,
1523 REGSET_GENERAL, 0,
1524 sizeof(struct user_regs_struct32),
1525 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001526
Roland McGrath5a4646a2008-01-30 13:31:54 +01001527 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1528 return copy_regset_to_user(child, &user_x86_32_view,
1529 REGSET_FP, 0,
1530 sizeof(struct user_i387_ia32_struct),
1531 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001532
Roland McGrath5a4646a2008-01-30 13:31:54 +01001533 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1534 return copy_regset_from_user(
1535 child, &user_x86_32_view, REGSET_FP,
1536 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001537
Roland McGrath5a4646a2008-01-30 13:31:54 +01001538 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1539 return copy_regset_to_user(child, &user_x86_32_view,
1540 REGSET_XFP, 0,
1541 sizeof(struct user32_fxsr_struct),
1542 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001543
Roland McGrath5a4646a2008-01-30 13:31:54 +01001544 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1545 return copy_regset_from_user(child, &user_x86_32_view,
1546 REGSET_XFP, 0,
1547 sizeof(struct user32_fxsr_struct),
1548 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001549
Roland McGrath562b80b2008-04-22 12:21:25 -07001550 case PTRACE_GET_THREAD_AREA:
1551 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001552#ifdef CONFIG_X86_PTRACE_BTS
1553 case PTRACE_BTS_CONFIG:
1554 case PTRACE_BTS_STATUS:
1555 case PTRACE_BTS_SIZE:
1556 case PTRACE_BTS_GET:
1557 case PTRACE_BTS_CLEAR:
1558 case PTRACE_BTS_DRAIN:
1559#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001560 return arch_ptrace(child, request, addr, data);
1561
Roland McGrath099cd6e2008-01-30 13:31:01 +01001562 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001563 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001564 }
1565
Roland McGrath099cd6e2008-01-30 13:31:01 +01001566 return ret;
1567}
1568
Roland McGrathcb757c42008-01-30 13:31:01 +01001569#endif /* CONFIG_IA32_EMULATION */
1570
Roland McGrath070459d2008-01-30 13:31:53 +01001571#ifdef CONFIG_X86_64
1572
1573static const struct user_regset x86_64_regsets[] = {
1574 [REGSET_GENERAL] = {
1575 .core_note_type = NT_PRSTATUS,
1576 .n = sizeof(struct user_regs_struct) / sizeof(long),
1577 .size = sizeof(long), .align = sizeof(long),
1578 .get = genregs_get, .set = genregs_set
1579 },
1580 [REGSET_FP] = {
1581 .core_note_type = NT_PRFPREG,
1582 .n = sizeof(struct user_i387_struct) / sizeof(long),
1583 .size = sizeof(long), .align = sizeof(long),
1584 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1585 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001586 [REGSET_IOPERM64] = {
1587 .core_note_type = NT_386_IOPERM,
1588 .n = IO_BITMAP_LONGS,
1589 .size = sizeof(long), .align = sizeof(long),
1590 .active = ioperm_active, .get = ioperm_get
1591 },
Roland McGrath070459d2008-01-30 13:31:53 +01001592};
1593
1594static const struct user_regset_view user_x86_64_view = {
1595 .name = "x86_64", .e_machine = EM_X86_64,
1596 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1597};
1598
1599#else /* CONFIG_X86_32 */
1600
1601#define user_regs_struct32 user_regs_struct
1602#define genregs32_get genregs_get
1603#define genregs32_set genregs_set
1604
Roland McGrath1f465f42008-05-09 15:43:44 -07001605#define user_i387_ia32_struct user_i387_struct
1606#define user32_fxsr_struct user_fxsr_struct
1607
Roland McGrath070459d2008-01-30 13:31:53 +01001608#endif /* CONFIG_X86_64 */
1609
1610#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1611static const struct user_regset x86_32_regsets[] = {
1612 [REGSET_GENERAL] = {
1613 .core_note_type = NT_PRSTATUS,
1614 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1615 .size = sizeof(u32), .align = sizeof(u32),
1616 .get = genregs32_get, .set = genregs32_set
1617 },
1618 [REGSET_FP] = {
1619 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001620 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001621 .size = sizeof(u32), .align = sizeof(u32),
1622 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1623 },
1624 [REGSET_XFP] = {
1625 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001626 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001627 .size = sizeof(u32), .align = sizeof(u32),
1628 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1629 },
1630 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001631 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001632 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1633 .size = sizeof(struct user_desc),
1634 .align = sizeof(struct user_desc),
1635 .active = regset_tls_active,
1636 .get = regset_tls_get, .set = regset_tls_set
1637 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001638 [REGSET_IOPERM32] = {
1639 .core_note_type = NT_386_IOPERM,
1640 .n = IO_BITMAP_BYTES / sizeof(u32),
1641 .size = sizeof(u32), .align = sizeof(u32),
1642 .active = ioperm_active, .get = ioperm_get
1643 },
Roland McGrath070459d2008-01-30 13:31:53 +01001644};
1645
1646static const struct user_regset_view user_x86_32_view = {
1647 .name = "i386", .e_machine = EM_386,
1648 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1649};
1650#endif
1651
1652const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1653{
1654#ifdef CONFIG_IA32_EMULATION
1655 if (test_tsk_thread_flag(task, TIF_IA32))
1656#endif
1657#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1658 return &user_x86_32_view;
1659#endif
1660#ifdef CONFIG_X86_64
1661 return &user_x86_64_view;
1662#endif
1663}
1664
Srinivasa Dsda654b72008-09-23 15:23:52 +05301665void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1666 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 struct siginfo info;
1669
1670 tsk->thread.trap_no = 1;
1671 tsk->thread.error_code = error_code;
1672
1673 memset(&info, 0, sizeof(info));
1674 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301675 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001677 /* User-mode ip? */
1678 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Simon Arlott27b46d72007-10-20 01:13:56 +02001680 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 force_sig_info(SIGTRAP, &info, tsk);
1682}
1683
Roland McGrath86976cd2008-01-30 13:31:01 +01001684
Roland McGrathd4d67152008-07-09 02:38:07 -07001685#ifdef CONFIG_X86_32
1686# define IS_IA32 1
1687#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001688# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001689#else
1690# define IS_IA32 0
1691#endif
1692
1693/*
1694 * We must return the syscall number to actually look up in the table.
1695 * This can be -1L to skip running any syscall at all.
1696 */
1697asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001698{
Roland McGrathd4d67152008-07-09 02:38:07 -07001699 long ret = 0;
1700
Roland McGrath380fdd72008-07-09 02:39:29 -07001701 /*
1702 * If we stepped into a sysenter/syscall insn, it trapped in
1703 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1704 * If user-mode had set TF itself, then it's still clear from
1705 * do_debug() and we need to set it again to restore the user
1706 * state. If we entered on the slow path, TF was already set.
1707 */
1708 if (test_thread_flag(TIF_SINGLESTEP))
1709 regs->flags |= X86_EFLAGS_TF;
1710
Roland McGrath86976cd2008-01-30 13:31:01 +01001711 /* do the secure computing check first */
1712 secure_computing(regs->orig_ax);
1713
Roland McGrathd4d67152008-07-09 02:38:07 -07001714 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1715 ret = -1L;
1716
Roland McGratheeea3c32008-03-16 23:36:28 -07001717 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1718 tracehook_report_syscall_entry(regs))
1719 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001720
Josh Stone66700002009-08-24 14:43:11 -07001721 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001722 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001723
Roland McGrath86976cd2008-01-30 13:31:01 +01001724 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001725 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001726 audit_syscall_entry(AUDIT_ARCH_I386,
1727 regs->orig_ax,
1728 regs->bx, regs->cx,
1729 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001730#ifdef CONFIG_X86_64
1731 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001732 audit_syscall_entry(AUDIT_ARCH_X86_64,
1733 regs->orig_ax,
1734 regs->di, regs->si,
1735 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001736#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001737 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001738
1739 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001740}
1741
Roland McGrathd4d67152008-07-09 02:38:07 -07001742asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001743{
1744 if (unlikely(current->audit_context))
1745 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1746
Josh Stone66700002009-08-24 14:43:11 -07001747 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001748 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001749
Roland McGrathd4d67152008-07-09 02:38:07 -07001750 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001751 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001752
Roland McGrathd4d67152008-07-09 02:38:07 -07001753 /*
1754 * If TIF_SYSCALL_EMU is set, we only get here because of
1755 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1756 * We already reported this syscall instruction in
1757 * syscall_trace_enter(), so don't do any more now.
1758 */
1759 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1760 return;
1761
1762 /*
1763 * If we are single-stepping, synthesize a trap to follow the
1764 * system call instruction.
1765 */
1766 if (test_thread_flag(TIF_SINGLESTEP) &&
Oleg Nesterov43918f22009-04-02 16:58:00 -07001767 tracehook_consider_fatal_signal(current, SIGTRAP))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301768 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001769}