blob: 16433a59b396ada7c947fb8d76a5c8a19d4139e2 [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,
Suresh Siddha5b3efd52010-02-11 11:50:59 -080051 REGSET_XSTATE,
Roland McGrath070459d2008-01-30 13:31:53 +010052 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070053 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010054};
Markus Metzgereee3af42008-01-30 13:31:09 +010055
Masami Hiramatsub1cf5402009-08-13 16:34:44 -040056struct pt_regs_offset {
57 const char *name;
58 int offset;
59};
60
61#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
62#define REG_OFFSET_END {.name = NULL, .offset = 0}
63
64static const struct pt_regs_offset regoffset_table[] = {
65#ifdef CONFIG_X86_64
66 REG_OFFSET_NAME(r15),
67 REG_OFFSET_NAME(r14),
68 REG_OFFSET_NAME(r13),
69 REG_OFFSET_NAME(r12),
70 REG_OFFSET_NAME(r11),
71 REG_OFFSET_NAME(r10),
72 REG_OFFSET_NAME(r9),
73 REG_OFFSET_NAME(r8),
74#endif
75 REG_OFFSET_NAME(bx),
76 REG_OFFSET_NAME(cx),
77 REG_OFFSET_NAME(dx),
78 REG_OFFSET_NAME(si),
79 REG_OFFSET_NAME(di),
80 REG_OFFSET_NAME(bp),
81 REG_OFFSET_NAME(ax),
82#ifdef CONFIG_X86_32
83 REG_OFFSET_NAME(ds),
84 REG_OFFSET_NAME(es),
85 REG_OFFSET_NAME(fs),
86 REG_OFFSET_NAME(gs),
87#endif
88 REG_OFFSET_NAME(orig_ax),
89 REG_OFFSET_NAME(ip),
90 REG_OFFSET_NAME(cs),
91 REG_OFFSET_NAME(flags),
92 REG_OFFSET_NAME(sp),
93 REG_OFFSET_NAME(ss),
94 REG_OFFSET_END,
95};
96
97/**
98 * regs_query_register_offset() - query register offset from its name
99 * @name: the name of a register
100 *
101 * regs_query_register_offset() returns the offset of a register in struct
102 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
103 */
104int regs_query_register_offset(const char *name)
105{
106 const struct pt_regs_offset *roff;
107 for (roff = regoffset_table; roff->name != NULL; roff++)
108 if (!strcmp(roff->name, name))
109 return roff->offset;
110 return -EINVAL;
111}
112
113/**
114 * regs_query_register_name() - query register name from its offset
115 * @offset: the offset of a register in struct pt_regs.
116 *
117 * regs_query_register_name() returns the name of a register from its
118 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
119 */
120const char *regs_query_register_name(unsigned int offset)
121{
122 const struct pt_regs_offset *roff;
123 for (roff = regoffset_table; roff->name != NULL; roff++)
124 if (roff->offset == offset)
125 return roff->name;
126 return NULL;
127}
128
129static const int arg_offs_table[] = {
130#ifdef CONFIG_X86_32
131 [0] = offsetof(struct pt_regs, ax),
132 [1] = offsetof(struct pt_regs, dx),
133 [2] = offsetof(struct pt_regs, cx)
134#else /* CONFIG_X86_64 */
135 [0] = offsetof(struct pt_regs, di),
136 [1] = offsetof(struct pt_regs, si),
137 [2] = offsetof(struct pt_regs, dx),
138 [3] = offsetof(struct pt_regs, cx),
139 [4] = offsetof(struct pt_regs, r8),
140 [5] = offsetof(struct pt_regs, r9)
141#endif
142};
143
144/**
145 * regs_get_argument_nth() - get Nth argument at function call
146 * @regs: pt_regs which contains registers at function entry.
147 * @n: argument number.
148 *
149 * regs_get_argument_nth() returns @n th argument of a function call.
150 * Since usually the kernel stack will be changed right after function entry,
151 * you must use this at function entry. If the @n th entry is NOT in the
152 * kernel stack or pt_regs, this returns 0.
153 */
154unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
155{
156 if (n < ARRAY_SIZE(arg_offs_table))
Masami Hiramatsuad5cafc2009-09-10 19:53:06 -0400157 return *(unsigned long *)((char *)regs + arg_offs_table[n]);
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400158 else {
159 /*
160 * The typical case: arg n is on the stack.
161 * (Note: stack[0] = return address, so skip it)
162 */
163 n -= ARRAY_SIZE(arg_offs_table);
164 return regs_get_kernel_stack_nth(regs, 1 + n);
165 }
166}
167
Markus Metzgereee3af42008-01-30 13:31:09 +0100168/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * does not yet catch signals sent when the child dies.
170 * in exit.c or in signal.c.
171 */
172
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500173/*
174 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500175 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100176#define FLAG_MASK_32 ((unsigned long) \
177 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
178 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
179 X86_EFLAGS_SF | X86_EFLAGS_TF | \
180 X86_EFLAGS_DF | X86_EFLAGS_OF | \
181 X86_EFLAGS_RF | X86_EFLAGS_AC))
182
Roland McGrath2047b082008-01-30 13:31:01 +0100183/*
184 * Determines whether a value may be installed in a segment register.
185 */
186static inline bool invalid_selector(u16 value)
187{
188 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
189}
190
191#ifdef CONFIG_X86_32
192
Roland McGrathe39c2892008-01-30 13:31:01 +0100193#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530195static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100197 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900198 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Roland McGrath06ee1b62008-01-30 13:31:01 +0100201static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100203 /*
204 * Returning the value truncates it to 16 bits.
205 */
206 unsigned int retval;
207 if (offset != offsetof(struct user_regs_struct, gs))
208 retval = *pt_regs_access(task_pt_regs(task), offset);
209 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100210 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900211 retval = get_user_gs(task_pt_regs(task));
212 else
213 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100214 }
215 return retval;
216}
217
218static int set_segment_reg(struct task_struct *task,
219 unsigned long offset, u16 value)
220{
221 /*
222 * The value argument was already truncated to 16 bits.
223 */
Roland McGrath2047b082008-01-30 13:31:01 +0100224 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100225 return -EIO;
226
Roland McGrathc63855d2008-02-06 22:39:44 +0100227 /*
228 * For %cs and %ss we cannot permit a null selector.
229 * We can permit a bogus selector as long as it has USER_RPL.
230 * Null selectors are fine for other segment registers, but
231 * we will never get back to user mode with invalid %cs or %ss
232 * and will take the trap in iret instead. Much code relies
233 * on user_mode() to distinguish a user trap frame (which can
234 * safely use invalid selectors) from a kernel trap frame.
235 */
236 switch (offset) {
237 case offsetof(struct user_regs_struct, cs):
238 case offsetof(struct user_regs_struct, ss):
239 if (unlikely(value == 0))
240 return -EIO;
241
242 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100243 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100244 break;
245
246 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100247 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900248 set_user_gs(task_pt_regs(task), value);
249 else
250 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
Roland McGrath2047b082008-01-30 13:31:01 +0100256#else /* CONFIG_X86_64 */
257
258#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
259
260static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
261{
262 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
263 return &regs->r15 + (offset / sizeof(regs->r15));
264}
265
266static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
267{
268 /*
269 * Returning the value truncates it to 16 bits.
270 */
271 unsigned int seg;
272
273 switch (offset) {
274 case offsetof(struct user_regs_struct, fs):
275 if (task == current) {
276 /* Older gas can't assemble movq %?s,%r?? */
277 asm("movl %%fs,%0" : "=r" (seg));
278 return seg;
279 }
280 return task->thread.fsindex;
281 case offsetof(struct user_regs_struct, gs):
282 if (task == current) {
283 asm("movl %%gs,%0" : "=r" (seg));
284 return seg;
285 }
286 return task->thread.gsindex;
287 case offsetof(struct user_regs_struct, ds):
288 if (task == current) {
289 asm("movl %%ds,%0" : "=r" (seg));
290 return seg;
291 }
292 return task->thread.ds;
293 case offsetof(struct user_regs_struct, es):
294 if (task == current) {
295 asm("movl %%es,%0" : "=r" (seg));
296 return seg;
297 }
298 return task->thread.es;
299
300 case offsetof(struct user_regs_struct, cs):
301 case offsetof(struct user_regs_struct, ss):
302 break;
303 }
304 return *pt_regs_access(task_pt_regs(task), offset);
305}
306
307static int set_segment_reg(struct task_struct *task,
308 unsigned long offset, u16 value)
309{
310 /*
311 * The value argument was already truncated to 16 bits.
312 */
313 if (invalid_selector(value))
314 return -EIO;
315
316 switch (offset) {
317 case offsetof(struct user_regs_struct,fs):
318 /*
319 * If this is setting fs as for normal 64-bit use but
320 * setting fs_base has implicitly changed it, leave it.
321 */
322 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
323 task->thread.fs != 0) ||
324 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
325 task->thread.fs == 0))
326 break;
327 task->thread.fsindex = value;
328 if (task == current)
329 loadsegment(fs, task->thread.fsindex);
330 break;
331 case offsetof(struct user_regs_struct,gs):
332 /*
333 * If this is setting gs as for normal 64-bit use but
334 * setting gs_base has implicitly changed it, leave it.
335 */
336 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
337 task->thread.gs != 0) ||
338 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
339 task->thread.gs == 0))
340 break;
341 task->thread.gsindex = value;
342 if (task == current)
343 load_gs_index(task->thread.gsindex);
344 break;
345 case offsetof(struct user_regs_struct,ds):
346 task->thread.ds = value;
347 if (task == current)
348 loadsegment(ds, task->thread.ds);
349 break;
350 case offsetof(struct user_regs_struct,es):
351 task->thread.es = value;
352 if (task == current)
353 loadsegment(es, task->thread.es);
354 break;
355
356 /*
357 * Can't actually change these in 64-bit mode.
358 */
359 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100360 if (unlikely(value == 0))
361 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100362#ifdef CONFIG_IA32_EMULATION
363 if (test_tsk_thread_flag(task, TIF_IA32))
364 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100365#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100366 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100367 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100368 if (unlikely(value == 0))
369 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100370#ifdef CONFIG_IA32_EMULATION
371 if (test_tsk_thread_flag(task, TIF_IA32))
372 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100373#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100374 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100375 }
376
377 return 0;
378}
379
Roland McGrath2047b082008-01-30 13:31:01 +0100380#endif /* CONFIG_X86_32 */
381
Roland McGrath06ee1b62008-01-30 13:31:01 +0100382static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100384 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Roland McGrath06ee1b62008-01-30 13:31:01 +0100386 /*
387 * If the debugger set TF, hide it from the readout.
388 */
389 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
390 retval &= ~X86_EFLAGS_TF;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return retval;
393}
394
Roland McGrath06ee1b62008-01-30 13:31:01 +0100395static int set_flags(struct task_struct *task, unsigned long value)
396{
397 struct pt_regs *regs = task_pt_regs(task);
398
399 /*
400 * If the user value contains TF, mark that
401 * it was not "us" (the debugger) that set it.
402 * If not, make sure it stays set if we had.
403 */
404 if (value & X86_EFLAGS_TF)
405 clear_tsk_thread_flag(task, TIF_FORCED_TF);
406 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
407 value |= X86_EFLAGS_TF;
408
409 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
410
411 return 0;
412}
413
414static int putreg(struct task_struct *child,
415 unsigned long offset, unsigned long value)
416{
417 switch (offset) {
418 case offsetof(struct user_regs_struct, cs):
419 case offsetof(struct user_regs_struct, ds):
420 case offsetof(struct user_regs_struct, es):
421 case offsetof(struct user_regs_struct, fs):
422 case offsetof(struct user_regs_struct, gs):
423 case offsetof(struct user_regs_struct, ss):
424 return set_segment_reg(child, offset, value);
425
426 case offsetof(struct user_regs_struct, flags):
427 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100428
429#ifdef CONFIG_X86_64
430 case offsetof(struct user_regs_struct,fs_base):
431 if (value >= TASK_SIZE_OF(child))
432 return -EIO;
433 /*
434 * When changing the segment base, use do_arch_prctl
435 * to set either thread.fs or thread.fsindex and the
436 * corresponding GDT slot.
437 */
438 if (child->thread.fs != value)
439 return do_arch_prctl(child, ARCH_SET_FS, value);
440 return 0;
441 case offsetof(struct user_regs_struct,gs_base):
442 /*
443 * Exactly the same here as the %fs handling above.
444 */
445 if (value >= TASK_SIZE_OF(child))
446 return -EIO;
447 if (child->thread.gs != value)
448 return do_arch_prctl(child, ARCH_SET_GS, value);
449 return 0;
450#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100451 }
452
453 *pt_regs_access(task_pt_regs(child), offset) = value;
454 return 0;
455}
456
457static unsigned long getreg(struct task_struct *task, unsigned long offset)
458{
459 switch (offset) {
460 case offsetof(struct user_regs_struct, cs):
461 case offsetof(struct user_regs_struct, ds):
462 case offsetof(struct user_regs_struct, es):
463 case offsetof(struct user_regs_struct, fs):
464 case offsetof(struct user_regs_struct, gs):
465 case offsetof(struct user_regs_struct, ss):
466 return get_segment_reg(task, offset);
467
468 case offsetof(struct user_regs_struct, flags):
469 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100470
471#ifdef CONFIG_X86_64
472 case offsetof(struct user_regs_struct, fs_base): {
473 /*
474 * do_arch_prctl may have used a GDT slot instead of
475 * the MSR. To userland, it appears the same either
476 * way, except the %fs segment selector might not be 0.
477 */
478 unsigned int seg = task->thread.fsindex;
479 if (task->thread.fs != 0)
480 return task->thread.fs;
481 if (task == current)
482 asm("movl %%fs,%0" : "=r" (seg));
483 if (seg != FS_TLS_SEL)
484 return 0;
485 return get_desc_base(&task->thread.tls_array[FS_TLS]);
486 }
487 case offsetof(struct user_regs_struct, gs_base): {
488 /*
489 * Exactly the same here as the %fs handling above.
490 */
491 unsigned int seg = task->thread.gsindex;
492 if (task->thread.gs != 0)
493 return task->thread.gs;
494 if (task == current)
495 asm("movl %%gs,%0" : "=r" (seg));
496 if (seg != GS_TLS_SEL)
497 return 0;
498 return get_desc_base(&task->thread.tls_array[GS_TLS]);
499 }
500#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100501 }
502
503 return *pt_regs_access(task_pt_regs(task), offset);
504}
505
Roland McGrath91e7b702008-01-30 13:31:52 +0100506static int genregs_get(struct task_struct *target,
507 const struct user_regset *regset,
508 unsigned int pos, unsigned int count,
509 void *kbuf, void __user *ubuf)
510{
511 if (kbuf) {
512 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800513 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100514 *k++ = getreg(target, pos);
515 count -= sizeof(*k);
516 pos += sizeof(*k);
517 }
518 } else {
519 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800520 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100521 if (__put_user(getreg(target, pos), u++))
522 return -EFAULT;
523 count -= sizeof(*u);
524 pos += sizeof(*u);
525 }
526 }
527
528 return 0;
529}
530
531static int genregs_set(struct task_struct *target,
532 const struct user_regset *regset,
533 unsigned int pos, unsigned int count,
534 const void *kbuf, const void __user *ubuf)
535{
536 int ret = 0;
537 if (kbuf) {
538 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800539 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100540 ret = putreg(target, pos, *k++);
541 count -= sizeof(*k);
542 pos += sizeof(*k);
543 }
544 } else {
545 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800546 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100547 unsigned long word;
548 ret = __get_user(word, u++);
549 if (ret)
550 break;
551 ret = putreg(target, pos, word);
552 count -= sizeof(*u);
553 pos += sizeof(*u);
554 }
555 }
556 return ret;
557}
558
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100559static void ptrace_triggered(struct perf_event *bp, int nmi,
560 struct perf_sample_data *data,
561 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100562{
Roland McGrath0f534092008-01-30 13:30:59 +0100563 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200564 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100565
K.Prasad72f674d2009-06-01 23:45:48 +0530566 /*
567 * Store in the virtual DR6 register the fact that the breakpoint
568 * was hit so the thread's debugger will see it.
569 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200570 for (i = 0; i < HBP_NUM; i++) {
571 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530572 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100573 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100574
K.Prasad72f674d2009-06-01 23:45:48 +0530575 thread->debugreg6 |= (DR_TRAP0 << i);
576}
577
578/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200579 * Walk through every ptrace breakpoints for this thread and
580 * build the dr7 value on top of their attributes.
581 *
582 */
583static unsigned long ptrace_get_dr7(struct perf_event *bp[])
584{
585 int i;
586 int dr7 = 0;
587 struct arch_hw_breakpoint *info;
588
589 for (i = 0; i < HBP_NUM; i++) {
590 if (bp[i] && !bp[i]->attr.disabled) {
591 info = counter_arch_bp(bp[i]);
592 dr7 |= encode_dr7(i, info->len, info->type);
593 }
594 }
595
596 return dr7;
597}
598
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100599static int
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100600ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100601 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100602{
603 int err;
604 int gen_len, gen_type;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100605 struct perf_event_attr attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100606
607 /*
608 * We shoud have at least an inactive breakpoint at this
609 * slot. It means the user is writing dr7 without having
610 * written the address register first
611 */
612 if (!bp)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100613 return -EINVAL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100614
615 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
616 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100617 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100618
619 attr = bp->attr;
620 attr.bp_len = gen_len;
621 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100622 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100623
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100624 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100625}
626
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200627/*
K.Prasad72f674d2009-06-01 23:45:48 +0530628 * Handle ptrace writes to debug register 7.
629 */
630static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
631{
632 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200633 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530634 int i, orig_ret = 0, rc = 0;
635 int enabled, second_pass = 0;
636 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200637 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530638
639 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200640 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530641restore:
642 /*
643 * Loop through all the hardware breakpoints, making the
644 * appropriate changes to each.
645 */
646 for (i = 0; i < HBP_NUM; i++) {
647 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200648 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530649
650 if (!enabled) {
651 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200652 /*
653 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530654 * unless all register_user_hw_breakpoint()
655 * requests have succeeded. This prevents
656 * any window of opportunity for debug
657 * register grabbing by other users.
658 */
659 if (!second_pass)
660 continue;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100661
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100662 rc = ptrace_modify_breakpoint(bp, len, type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100663 tsk, 1);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100664 if (rc)
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100665 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530666 }
667 continue;
668 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200669
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100670 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
671 if (rc)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200672 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530673 }
674 /*
675 * Make a second pass to free the remaining unused breakpoints
676 * or to restore the original breakpoints if an error occurred.
677 */
678 if (!second_pass) {
679 second_pass = 1;
680 if (rc < 0) {
681 orig_ret = rc;
682 data = old_dr7;
683 }
684 goto restore;
685 }
686 return ((orig_ret < 0) ? orig_ret : rc);
687}
688
689/*
690 * Handle PTRACE_PEEKUSR calls for the debug register area.
691 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530692static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530693{
694 struct thread_struct *thread = &(tsk->thread);
695 unsigned long val = 0;
696
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200697 if (n < HBP_NUM) {
698 struct perf_event *bp;
699 bp = thread->ptrace_bps[n];
700 if (!bp)
701 return 0;
702 val = bp->hw.info.address;
703 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530704 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200705 } else if (n == 7) {
706 val = ptrace_get_dr7(thread->ptrace_bps);
707 }
K.Prasad72f674d2009-06-01 23:45:48 +0530708 return val;
709}
710
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200711static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
712 unsigned long addr)
713{
714 struct perf_event *bp;
715 struct thread_struct *t = &tsk->thread;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100716 struct perf_event_attr attr;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200717
718 if (!t->ptrace_bps[nr]) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100719 hw_breakpoint_init(&attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200720 /*
721 * Put stub len and type to register (reserve) an inactive but
722 * correct bp
723 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100724 attr.bp_addr = addr;
725 attr.bp_len = HW_BREAKPOINT_LEN_1;
726 attr.bp_type = HW_BREAKPOINT_W;
727 attr.disabled = 1;
728
729 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100730
731 /*
732 * CHECKME: the previous code returned -EIO if the addr wasn't
733 * a valid task virtual addr. The new one will return -EINVAL in
734 * this case.
735 * -EINVAL may be what we want for in-kernel breakpoints users,
736 * but -EIO looks better for ptrace, since we refuse a register
737 * writing for the user. And anyway this is the previous
738 * behaviour.
739 */
740 if (IS_ERR(bp))
741 return PTR_ERR(bp);
742
743 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200744 } else {
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100745 int err;
746
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200747 bp = t->ptrace_bps[nr];
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100748
749 attr = bp->attr;
750 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100751 err = modify_user_hw_breakpoint(bp, &attr);
752 if (err)
753 return err;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200754 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200755
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758}
759
K.Prasad72f674d2009-06-01 23:45:48 +0530760/*
761 * Handle PTRACE_POKEUSR calls for the debug register area.
762 */
763int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
K.Prasad72f674d2009-06-01 23:45:48 +0530765 struct thread_struct *thread = &(tsk->thread);
766 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
K.Prasad72f674d2009-06-01 23:45:48 +0530768 /* There are no DR4 or DR5 registers */
769 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -EIO;
771
K.Prasad72f674d2009-06-01 23:45:48 +0530772 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200773 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530774 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
K.Prasad72f674d2009-06-01 23:45:48 +0530776 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200777 rc = ptrace_set_breakpoint_addr(tsk, n, val);
778 if (rc)
779 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530780 }
781 /* All that's left is DR7 */
782 if (n == 7)
783 rc = ptrace_write_dr7(tsk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
K.Prasad72f674d2009-06-01 23:45:48 +0530785ret_path:
786 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Roland McGrath325af5f2008-08-08 15:58:39 -0700789/*
790 * These access the current or another (stopped) task's io permission
791 * bitmap for debugging or core dump.
792 */
793static int ioperm_active(struct task_struct *target,
794 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100795{
Roland McGrath325af5f2008-08-08 15:58:39 -0700796 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100797}
798
Roland McGrath325af5f2008-08-08 15:58:39 -0700799static int ioperm_get(struct task_struct *target,
800 const struct user_regset *regset,
801 unsigned int pos, unsigned int count,
802 void *kbuf, void __user *ubuf)
803{
804 if (!target->thread.io_bitmap_ptr)
805 return -ENXIO;
806
807 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
808 target->thread.io_bitmap_ptr,
809 0, IO_BITMAP_BYTES);
810}
811
Markus Metzger93fa7632008-04-08 11:01:58 +0200812#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200813/*
814 * A branch trace store context.
815 *
816 * Contexts may only be installed by ptrace_bts_config() and only for
817 * ptraced tasks.
818 *
819 * Contexts are destroyed when the tracee is detached from the tracer.
820 * The actual destruction work requires interrupts enabled, so the
821 * work is deferred and will be scheduled during __ptrace_unlink().
822 *
823 * Contexts hold an additional task_struct reference on the traced
824 * task, as well as a reference on the tracer's mm.
825 *
826 * Ptrace already holds a task_struct for the duration of ptrace operations,
827 * but since destruction is deferred, it may be executed after both
828 * tracer and tracee exited.
829 */
830struct bts_context {
831 /* The branch trace handle. */
832 struct bts_tracer *tracer;
833
834 /* The buffer used to store the branch trace and its size. */
835 void *buffer;
836 unsigned int size;
837
838 /* The mm that paid for the above buffer. */
839 struct mm_struct *mm;
840
841 /* The task this context belongs to. */
842 struct task_struct *task;
843
844 /* The signal to send on a bts buffer overflow. */
845 unsigned int bts_ovfl_signal;
846
847 /* The work struct to destroy a context. */
848 struct work_struct work;
849};
850
Markus Metzger1cb81b12009-04-24 09:51:43 +0200851static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200852{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200853 void *buffer = NULL;
854 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200855
Markus Metzger1cb81b12009-04-24 09:51:43 +0200856 err = account_locked_memory(current->mm, current->signal->rlim, size);
857 if (err < 0)
858 return err;
859
860 buffer = kzalloc(size, GFP_KERNEL);
861 if (!buffer)
862 goto out_refund;
863
864 context->buffer = buffer;
865 context->size = size;
866 context->mm = get_task_mm(current);
867
868 return 0;
869
870 out_refund:
871 refund_locked_memory(current->mm, size);
872 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200873}
874
875static inline void free_bts_buffer(struct bts_context *context)
876{
877 if (!context->buffer)
878 return;
879
880 kfree(context->buffer);
881 context->buffer = NULL;
882
Markus Metzger1cb81b12009-04-24 09:51:43 +0200883 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200884 context->size = 0;
885
886 mmput(context->mm);
887 context->mm = NULL;
888}
889
890static void free_bts_context_work(struct work_struct *w)
891{
892 struct bts_context *context;
893
894 context = container_of(w, struct bts_context, work);
895
896 ds_release_bts(context->tracer);
897 put_task_struct(context->task);
898 free_bts_buffer(context);
899 kfree(context);
900}
901
902static inline void free_bts_context(struct bts_context *context)
903{
904 INIT_WORK(&context->work, free_bts_context_work);
905 schedule_work(&context->work);
906}
907
908static inline struct bts_context *alloc_bts_context(struct task_struct *task)
909{
910 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
911 if (context) {
912 context->task = task;
913 task->bts = context;
914
915 get_task_struct(task);
916 }
917
918 return context;
919}
920
Markus Metzger93fa7632008-04-08 11:01:58 +0200921static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100922 struct bts_struct __user *out)
923{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200924 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100925 const struct bts_trace *trace;
926 struct bts_struct bts;
927 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200928 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100929
Markus Metzgere2b371f2009-04-03 16:43:35 +0200930 context = child->bts;
931 if (!context)
932 return -ESRCH;
933
934 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100935 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200936 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100937
938 at = trace->ds.top - ((index + 1) * trace->ds.size);
939 if ((void *)at < trace->ds.begin)
940 at += (trace->ds.n * trace->ds.size);
941
942 if (!trace->read)
943 return -EOPNOTSUPP;
944
Markus Metzgere2b371f2009-04-03 16:43:35 +0200945 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200946 if (error < 0)
947 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100948
Markus Metzgerc2724772008-12-11 13:49:59 +0100949 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100950 return -EFAULT;
951
Markus Metzgerc2724772008-12-11 13:49:59 +0100952 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100953}
954
Markus Metzgera95d67f2008-01-30 13:31:20 +0100955static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100956 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100957 struct bts_struct __user *out)
958{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200959 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100960 const struct bts_trace *trace;
961 const unsigned char *at;
962 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100963
Markus Metzgere2b371f2009-04-03 16:43:35 +0200964 context = child->bts;
965 if (!context)
966 return -ESRCH;
967
968 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100969 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200970 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100971
Markus Metzgerc2724772008-12-11 13:49:59 +0100972 if (!trace->read)
973 return -EOPNOTSUPP;
974
975 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100976 return -EIO;
977
Markus Metzgerc2724772008-12-11 13:49:59 +0100978 for (at = trace->ds.begin; (void *)at < trace->ds.top;
979 out++, drained++, at += trace->ds.size) {
980 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100981
Markus Metzgere2b371f2009-04-03 16:43:35 +0200982 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100983 if (error < 0)
984 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100985
Markus Metzgerc2724772008-12-11 13:49:59 +0100986 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100987 return -EFAULT;
988 }
989
Markus Metzgerc2724772008-12-11 13:49:59 +0100990 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
991
Markus Metzgere2b371f2009-04-03 16:43:35 +0200992 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200993 if (error < 0)
994 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100995
Markus Metzgerc2724772008-12-11 13:49:59 +0100996 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100997}
998
999static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +01001000 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +01001001 const struct ptrace_bts_config __user *ucfg)
1002{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001003 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001004 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +01001005 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001006
Markus Metzgercba4b652008-01-30 13:32:03 +01001007 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +01001008 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +01001009
Markus Metzgera95d67f2008-01-30 13:31:20 +01001010 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +01001011 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001012
Markus Metzgere2b371f2009-04-03 16:43:35 +02001013 context = child->bts;
1014 if (!context)
1015 context = alloc_bts_context(child);
1016 if (!context)
1017 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001018
Markus Metzgerc2724772008-12-11 13:49:59 +01001019 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1020 if (!cfg.signal)
1021 return -EINVAL;
1022
Américo Wang5a8ac9d2009-03-13 15:56:58 +08001023 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +02001024 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +01001025 }
1026
Markus Metzgere2b371f2009-04-03 16:43:35 +02001027 ds_release_bts(context->tracer);
1028 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001029
Markus Metzgere2b371f2009-04-03 16:43:35 +02001030 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +02001031 int err;
1032
Markus Metzgere2b371f2009-04-03 16:43:35 +02001033 free_bts_buffer(context);
1034 if (!cfg.size)
1035 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +01001036
Markus Metzger1cb81b12009-04-24 09:51:43 +02001037 err = alloc_bts_buffer(context, cfg.size);
1038 if (err < 0)
1039 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +01001040 }
Markus Metzgera95d67f2008-01-30 13:31:20 +01001041
Markus Metzgerda35c372008-01-30 13:32:03 +01001042 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +01001043 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +01001044
Markus Metzgerda35c372008-01-30 13:32:03 +01001045 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +01001046 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +01001047
Markus Metzgerde79f542009-04-03 16:43:40 +02001048 context->tracer =
1049 ds_request_bts_task(child, context->buffer, context->size,
1050 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +02001051 if (unlikely(IS_ERR(context->tracer))) {
1052 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +01001053
Markus Metzgere2b371f2009-04-03 16:43:35 +02001054 free_bts_buffer(context);
1055 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001056 return error;
1057 }
Markus Metzgerda35c372008-01-30 13:32:03 +01001058
Markus Metzgerc2724772008-12-11 13:49:59 +01001059 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001060}
1061
Markus Metzgera95d67f2008-01-30 13:31:20 +01001062static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +01001063 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +01001064 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +01001065{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001066 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001067 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001068 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +01001069
Markus Metzgere2b371f2009-04-03 16:43:35 +02001070 context = child->bts;
1071 if (!context)
1072 return -ESRCH;
1073
Markus Metzgercba4b652008-01-30 13:32:03 +01001074 if (cfg_size < sizeof(cfg))
1075 return -EIO;
1076
Markus Metzgere2b371f2009-04-03 16:43:35 +02001077 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001078 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001079 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001080
Markus Metzgera95d67f2008-01-30 13:31:20 +01001081 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +02001082 cfg.size = trace->ds.end - trace->ds.begin;
1083 cfg.signal = context->bts_ovfl_signal;
1084 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +01001085
Markus Metzger93fa7632008-04-08 11:01:58 +02001086 if (cfg.signal)
1087 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1088
Markus Metzgerc2724772008-12-11 13:49:59 +01001089 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +02001090 cfg.flags |= PTRACE_BTS_O_TRACE;
1091
Markus Metzgerc2724772008-12-11 13:49:59 +01001092 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +02001093 cfg.flags |= PTRACE_BTS_O_SCHED;
1094
Markus Metzgera95d67f2008-01-30 13:31:20 +01001095 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1096 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +01001097
Markus Metzgera95d67f2008-01-30 13:31:20 +01001098 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001099}
1100
Markus Metzgerc2724772008-12-11 13:49:59 +01001101static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -08001102{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001103 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001104 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001105
Markus Metzgere2b371f2009-04-03 16:43:35 +02001106 context = child->bts;
1107 if (!context)
1108 return -ESRCH;
1109
1110 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001111 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001112 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001113
Markus Metzgerc2724772008-12-11 13:49:59 +01001114 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001115
Markus Metzgere2b371f2009-04-03 16:43:35 +02001116 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001117}
1118
Markus Metzgerc2724772008-12-11 13:49:59 +01001119static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +01001120{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001121 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001122 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +01001123
Markus Metzgere2b371f2009-04-03 16:43:35 +02001124 context = child->bts;
1125 if (!context)
1126 return -ESRCH;
1127
1128 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001129 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001130 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001131
Markus Metzgerc2724772008-12-11 13:49:59 +01001132 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +02001133}
Markus Metzgerbf53de92008-12-19 15:10:24 +01001134
Markus Metzgere2b371f2009-04-03 16:43:35 +02001135/*
1136 * Called from __ptrace_unlink() after the child has been moved back
1137 * to its original parent.
1138 */
Markus Metzger0f481402009-04-03 16:43:48 +02001139void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +01001140{
1141 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +02001142 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +01001143 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +01001144 }
1145}
Markus Metzger93fa7632008-04-08 11:01:58 +02001146#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/*
1149 * Called by kernel/ptrace.c when detaching..
1150 *
1151 * Make sure the single step bit is not set.
1152 */
1153void ptrace_disable(struct task_struct *child)
1154{
1155 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001156#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Roland McGrath5a4646a2008-01-30 13:31:54 +01001161#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1162static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1163#endif
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1166{
Roland McGrath5a4646a2008-01-30 13:31:54 +01001167 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 unsigned long __user *datap = (unsigned long __user *)data;
1169
1170 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* read the word at location addr in the USER area. */
1172 case PTRACE_PEEKUSR: {
1173 unsigned long tmp;
1174
1175 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001176 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1177 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179
1180 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001181 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001183 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1184 addr <= offsetof(struct user, u_debugreg[7])) {
1185 addr -= offsetof(struct user, u_debugreg[0]);
1186 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 ret = put_user(tmp, datap);
1189 break;
1190 }
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1193 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001194 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1195 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197
Roland McGrathe9c86c72008-01-30 13:31:01 +01001198 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001200 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1201 addr <= offsetof(struct user, u_debugreg[7])) {
1202 addr -= offsetof(struct user, u_debugreg[0]);
1203 ret = ptrace_set_debugreg(child,
1204 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Roland McGrath5a4646a2008-01-30 13:31:54 +01001208 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1209 return copy_regset_to_user(child,
1210 task_user_regset_view(current),
1211 REGSET_GENERAL,
1212 0, sizeof(struct user_regs_struct),
1213 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Roland McGrath5a4646a2008-01-30 13:31:54 +01001215 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1216 return copy_regset_from_user(child,
1217 task_user_regset_view(current),
1218 REGSET_GENERAL,
1219 0, sizeof(struct user_regs_struct),
1220 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Roland McGrath5a4646a2008-01-30 13:31:54 +01001222 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1223 return copy_regset_to_user(child,
1224 task_user_regset_view(current),
1225 REGSET_FP,
1226 0, sizeof(struct user_i387_struct),
1227 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Roland McGrath5a4646a2008-01-30 13:31:54 +01001229 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1230 return copy_regset_from_user(child,
1231 task_user_regset_view(current),
1232 REGSET_FP,
1233 0, sizeof(struct user_i387_struct),
1234 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Roland McGrathe9c86c72008-01-30 13:31:01 +01001236#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001237 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1238 return copy_regset_to_user(child, &user_x86_32_view,
1239 REGSET_XFP,
1240 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001241 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Roland McGrath5a4646a2008-01-30 13:31:54 +01001243 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1244 return copy_regset_from_user(child, &user_x86_32_view,
1245 REGSET_XFP,
1246 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001247 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Roland McGrathe9c86c72008-01-30 13:31:01 +01001250#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001252 if (addr < 0)
1253 return -EIO;
1254 ret = do_get_thread_area(child, addr,
1255 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
1257
1258 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001259 if (addr < 0)
1260 return -EIO;
1261 ret = do_set_thread_area(child, addr,
1262 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001264#endif
1265
1266#ifdef CONFIG_X86_64
1267 /* normal 64bit interface to access TLS data.
1268 Works just like arch_prctl, except that the arguments
1269 are reversed. */
1270 case PTRACE_ARCH_PRCTL:
1271 ret = do_arch_prctl(child, data, addr);
1272 break;
1273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001275 /*
1276 * These bits need more cooking - not enabled yet:
1277 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001278#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001279 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001280 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001281 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001282 break;
1283
1284 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001285 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001286 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001287 break;
1288
Markus Metzgerc2724772008-12-11 13:49:59 +01001289 case PTRACE_BTS_SIZE:
1290 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001291 break;
1292
1293 case PTRACE_BTS_GET:
1294 ret = ptrace_bts_read_record
1295 (child, data, (struct bts_struct __user *) addr);
1296 break;
1297
1298 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001299 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001300 break;
1301
1302 case PTRACE_BTS_DRAIN:
1303 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001304 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001305 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001306#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 default:
1309 ret = ptrace_request(child, request, addr, data);
1310 break;
1311 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return ret;
1314}
1315
Roland McGrathcb757c42008-01-30 13:31:01 +01001316#ifdef CONFIG_IA32_EMULATION
1317
Roland McGrath099cd6e2008-01-30 13:31:01 +01001318#include <linux/compat.h>
1319#include <linux/syscalls.h>
1320#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001321#include <asm/user32.h>
1322
1323#define R32(l,q) \
1324 case offsetof(struct user32, regs.l): \
1325 regs->q = value; break
1326
1327#define SEG32(rs) \
1328 case offsetof(struct user32, regs.rs): \
1329 return set_segment_reg(child, \
1330 offsetof(struct user_regs_struct, rs), \
1331 value); \
1332 break
1333
1334static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1335{
1336 struct pt_regs *regs = task_pt_regs(child);
1337
1338 switch (regno) {
1339
1340 SEG32(cs);
1341 SEG32(ds);
1342 SEG32(es);
1343 SEG32(fs);
1344 SEG32(gs);
1345 SEG32(ss);
1346
1347 R32(ebx, bx);
1348 R32(ecx, cx);
1349 R32(edx, dx);
1350 R32(edi, di);
1351 R32(esi, si);
1352 R32(ebp, bp);
1353 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001354 R32(eip, ip);
1355 R32(esp, sp);
1356
Roland McGrath40f09332008-02-28 19:57:07 -08001357 case offsetof(struct user32, regs.orig_eax):
1358 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001359 * A 32-bit debugger setting orig_eax means to restore
1360 * the state of the task restarting a 32-bit syscall.
1361 * Make sure we interpret the -ERESTART* codes correctly
1362 * in case the task is not actually still sitting at the
1363 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001364 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001365 regs->orig_ax = value;
1366 if (syscall_get_nr(child, regs) >= 0)
1367 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001368 break;
1369
Roland McGrathcb757c42008-01-30 13:31:01 +01001370 case offsetof(struct user32, regs.eflags):
1371 return set_flags(child, value);
1372
1373 case offsetof(struct user32, u_debugreg[0]) ...
1374 offsetof(struct user32, u_debugreg[7]):
1375 regno -= offsetof(struct user32, u_debugreg[0]);
1376 return ptrace_set_debugreg(child, regno / 4, value);
1377
1378 default:
1379 if (regno > sizeof(struct user32) || (regno & 3))
1380 return -EIO;
1381
1382 /*
1383 * Other dummy fields in the virtual user structure
1384 * are ignored
1385 */
1386 break;
1387 }
1388 return 0;
1389}
1390
1391#undef R32
1392#undef SEG32
1393
1394#define R32(l,q) \
1395 case offsetof(struct user32, regs.l): \
1396 *val = regs->q; break
1397
1398#define SEG32(rs) \
1399 case offsetof(struct user32, regs.rs): \
1400 *val = get_segment_reg(child, \
1401 offsetof(struct user_regs_struct, rs)); \
1402 break
1403
1404static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1405{
1406 struct pt_regs *regs = task_pt_regs(child);
1407
1408 switch (regno) {
1409
1410 SEG32(ds);
1411 SEG32(es);
1412 SEG32(fs);
1413 SEG32(gs);
1414
1415 R32(cs, cs);
1416 R32(ss, ss);
1417 R32(ebx, bx);
1418 R32(ecx, cx);
1419 R32(edx, dx);
1420 R32(edi, di);
1421 R32(esi, si);
1422 R32(ebp, bp);
1423 R32(eax, ax);
1424 R32(orig_eax, orig_ax);
1425 R32(eip, ip);
1426 R32(esp, sp);
1427
1428 case offsetof(struct user32, regs.eflags):
1429 *val = get_flags(child);
1430 break;
1431
1432 case offsetof(struct user32, u_debugreg[0]) ...
1433 offsetof(struct user32, u_debugreg[7]):
1434 regno -= offsetof(struct user32, u_debugreg[0]);
1435 *val = ptrace_get_debugreg(child, regno / 4);
1436 break;
1437
1438 default:
1439 if (regno > sizeof(struct user32) || (regno & 3))
1440 return -EIO;
1441
1442 /*
1443 * Other dummy fields in the virtual user structure
1444 * are ignored
1445 */
1446 *val = 0;
1447 break;
1448 }
1449 return 0;
1450}
1451
1452#undef R32
1453#undef SEG32
1454
Roland McGrath91e7b702008-01-30 13:31:52 +01001455static int genregs32_get(struct task_struct *target,
1456 const struct user_regset *regset,
1457 unsigned int pos, unsigned int count,
1458 void *kbuf, void __user *ubuf)
1459{
1460 if (kbuf) {
1461 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001462 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001463 getreg32(target, pos, k++);
1464 count -= sizeof(*k);
1465 pos += sizeof(*k);
1466 }
1467 } else {
1468 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001469 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001470 compat_ulong_t word;
1471 getreg32(target, pos, &word);
1472 if (__put_user(word, u++))
1473 return -EFAULT;
1474 count -= sizeof(*u);
1475 pos += sizeof(*u);
1476 }
1477 }
1478
1479 return 0;
1480}
1481
1482static int genregs32_set(struct task_struct *target,
1483 const struct user_regset *regset,
1484 unsigned int pos, unsigned int count,
1485 const void *kbuf, const void __user *ubuf)
1486{
1487 int ret = 0;
1488 if (kbuf) {
1489 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001490 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001491 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001492 count -= sizeof(*k);
1493 pos += sizeof(*k);
1494 }
1495 } else {
1496 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001497 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001498 compat_ulong_t word;
1499 ret = __get_user(word, u++);
1500 if (ret)
1501 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001502 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001503 count -= sizeof(*u);
1504 pos += sizeof(*u);
1505 }
1506 }
1507 return ret;
1508}
1509
Roland McGrath562b80b2008-04-22 12:21:25 -07001510long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1511 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001512{
Roland McGrath562b80b2008-04-22 12:21:25 -07001513 unsigned long addr = caddr;
1514 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001515 void __user *datap = compat_ptr(data);
1516 int ret;
1517 __u32 val;
1518
1519 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001520 case PTRACE_PEEKUSR:
1521 ret = getreg32(child, addr, &val);
1522 if (ret == 0)
1523 ret = put_user(val, (__u32 __user *)datap);
1524 break;
1525
1526 case PTRACE_POKEUSR:
1527 ret = putreg32(child, addr, data);
1528 break;
1529
Roland McGrath5a4646a2008-01-30 13:31:54 +01001530 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1531 return copy_regset_to_user(child, &user_x86_32_view,
1532 REGSET_GENERAL,
1533 0, sizeof(struct user_regs_struct32),
1534 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001535
Roland McGrath5a4646a2008-01-30 13:31:54 +01001536 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1537 return copy_regset_from_user(child, &user_x86_32_view,
1538 REGSET_GENERAL, 0,
1539 sizeof(struct user_regs_struct32),
1540 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001541
Roland McGrath5a4646a2008-01-30 13:31:54 +01001542 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1543 return copy_regset_to_user(child, &user_x86_32_view,
1544 REGSET_FP, 0,
1545 sizeof(struct user_i387_ia32_struct),
1546 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001547
Roland McGrath5a4646a2008-01-30 13:31:54 +01001548 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1549 return copy_regset_from_user(
1550 child, &user_x86_32_view, REGSET_FP,
1551 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001552
Roland McGrath5a4646a2008-01-30 13:31:54 +01001553 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1554 return copy_regset_to_user(child, &user_x86_32_view,
1555 REGSET_XFP, 0,
1556 sizeof(struct user32_fxsr_struct),
1557 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001558
Roland McGrath5a4646a2008-01-30 13:31:54 +01001559 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1560 return copy_regset_from_user(child, &user_x86_32_view,
1561 REGSET_XFP, 0,
1562 sizeof(struct user32_fxsr_struct),
1563 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001564
Roland McGrath562b80b2008-04-22 12:21:25 -07001565 case PTRACE_GET_THREAD_AREA:
1566 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001567#ifdef CONFIG_X86_PTRACE_BTS
1568 case PTRACE_BTS_CONFIG:
1569 case PTRACE_BTS_STATUS:
1570 case PTRACE_BTS_SIZE:
1571 case PTRACE_BTS_GET:
1572 case PTRACE_BTS_CLEAR:
1573 case PTRACE_BTS_DRAIN:
1574#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001575 return arch_ptrace(child, request, addr, data);
1576
Roland McGrath099cd6e2008-01-30 13:31:01 +01001577 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001578 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001579 }
1580
Roland McGrath099cd6e2008-01-30 13:31:01 +01001581 return ret;
1582}
1583
Roland McGrathcb757c42008-01-30 13:31:01 +01001584#endif /* CONFIG_IA32_EMULATION */
1585
Roland McGrath070459d2008-01-30 13:31:53 +01001586#ifdef CONFIG_X86_64
1587
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001588static struct user_regset x86_64_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001589 [REGSET_GENERAL] = {
1590 .core_note_type = NT_PRSTATUS,
1591 .n = sizeof(struct user_regs_struct) / sizeof(long),
1592 .size = sizeof(long), .align = sizeof(long),
1593 .get = genregs_get, .set = genregs_set
1594 },
1595 [REGSET_FP] = {
1596 .core_note_type = NT_PRFPREG,
1597 .n = sizeof(struct user_i387_struct) / sizeof(long),
1598 .size = sizeof(long), .align = sizeof(long),
1599 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1600 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001601 [REGSET_XSTATE] = {
1602 .core_note_type = NT_X86_XSTATE,
1603 .size = sizeof(u64), .align = sizeof(u64),
1604 .active = xstateregs_active, .get = xstateregs_get,
1605 .set = xstateregs_set
1606 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001607 [REGSET_IOPERM64] = {
1608 .core_note_type = NT_386_IOPERM,
1609 .n = IO_BITMAP_LONGS,
1610 .size = sizeof(long), .align = sizeof(long),
1611 .active = ioperm_active, .get = ioperm_get
1612 },
Roland McGrath070459d2008-01-30 13:31:53 +01001613};
1614
1615static const struct user_regset_view user_x86_64_view = {
1616 .name = "x86_64", .e_machine = EM_X86_64,
1617 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1618};
1619
1620#else /* CONFIG_X86_32 */
1621
1622#define user_regs_struct32 user_regs_struct
1623#define genregs32_get genregs_get
1624#define genregs32_set genregs_set
1625
Roland McGrath1f465f42008-05-09 15:43:44 -07001626#define user_i387_ia32_struct user_i387_struct
1627#define user32_fxsr_struct user_fxsr_struct
1628
Roland McGrath070459d2008-01-30 13:31:53 +01001629#endif /* CONFIG_X86_64 */
1630
1631#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001632static struct user_regset x86_32_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001633 [REGSET_GENERAL] = {
1634 .core_note_type = NT_PRSTATUS,
1635 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1636 .size = sizeof(u32), .align = sizeof(u32),
1637 .get = genregs32_get, .set = genregs32_set
1638 },
1639 [REGSET_FP] = {
1640 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001641 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001642 .size = sizeof(u32), .align = sizeof(u32),
1643 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1644 },
1645 [REGSET_XFP] = {
1646 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001647 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001648 .size = sizeof(u32), .align = sizeof(u32),
1649 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1650 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001651 [REGSET_XSTATE] = {
1652 .core_note_type = NT_X86_XSTATE,
1653 .size = sizeof(u64), .align = sizeof(u64),
1654 .active = xstateregs_active, .get = xstateregs_get,
1655 .set = xstateregs_set
1656 },
Roland McGrath070459d2008-01-30 13:31:53 +01001657 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001658 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001659 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1660 .size = sizeof(struct user_desc),
1661 .align = sizeof(struct user_desc),
1662 .active = regset_tls_active,
1663 .get = regset_tls_get, .set = regset_tls_set
1664 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001665 [REGSET_IOPERM32] = {
1666 .core_note_type = NT_386_IOPERM,
1667 .n = IO_BITMAP_BYTES / sizeof(u32),
1668 .size = sizeof(u32), .align = sizeof(u32),
1669 .active = ioperm_active, .get = ioperm_get
1670 },
Roland McGrath070459d2008-01-30 13:31:53 +01001671};
1672
1673static const struct user_regset_view user_x86_32_view = {
1674 .name = "i386", .e_machine = EM_386,
1675 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1676};
1677#endif
1678
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001679/*
1680 * This represents bytes 464..511 in the memory layout exported through
1681 * the REGSET_XSTATE interface.
1682 */
1683u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1684
1685void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1686{
1687#ifdef CONFIG_X86_64
1688 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1689#endif
1690#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1691 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1692#endif
1693 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1694}
1695
Roland McGrath070459d2008-01-30 13:31:53 +01001696const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1697{
1698#ifdef CONFIG_IA32_EMULATION
1699 if (test_tsk_thread_flag(task, TIF_IA32))
1700#endif
1701#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1702 return &user_x86_32_view;
1703#endif
1704#ifdef CONFIG_X86_64
1705 return &user_x86_64_view;
1706#endif
1707}
1708
Oleg Nesterov7f385512009-12-15 16:47:20 -08001709static void fill_sigtrap_info(struct task_struct *tsk,
1710 struct pt_regs *regs,
1711 int error_code, int si_code,
1712 struct siginfo *info)
1713{
1714 tsk->thread.trap_no = 1;
1715 tsk->thread.error_code = error_code;
1716
1717 memset(info, 0, sizeof(*info));
1718 info->si_signo = SIGTRAP;
1719 info->si_code = si_code;
1720 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1721}
1722
1723void user_single_step_siginfo(struct task_struct *tsk,
1724 struct pt_regs *regs,
1725 struct siginfo *info)
1726{
1727 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1728}
1729
Srinivasa Dsda654b72008-09-23 15:23:52 +05301730void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1731 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 struct siginfo info;
1734
Oleg Nesterov7f385512009-12-15 16:47:20 -08001735 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
Simon Arlott27b46d72007-10-20 01:13:56 +02001736 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 force_sig_info(SIGTRAP, &info, tsk);
1738}
1739
Roland McGrath86976cd2008-01-30 13:31:01 +01001740
Roland McGrathd4d67152008-07-09 02:38:07 -07001741#ifdef CONFIG_X86_32
1742# define IS_IA32 1
1743#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001744# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001745#else
1746# define IS_IA32 0
1747#endif
1748
1749/*
1750 * We must return the syscall number to actually look up in the table.
1751 * This can be -1L to skip running any syscall at all.
1752 */
1753asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001754{
Roland McGrathd4d67152008-07-09 02:38:07 -07001755 long ret = 0;
1756
Roland McGrath380fdd72008-07-09 02:39:29 -07001757 /*
1758 * If we stepped into a sysenter/syscall insn, it trapped in
1759 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1760 * If user-mode had set TF itself, then it's still clear from
1761 * do_debug() and we need to set it again to restore the user
1762 * state. If we entered on the slow path, TF was already set.
1763 */
1764 if (test_thread_flag(TIF_SINGLESTEP))
1765 regs->flags |= X86_EFLAGS_TF;
1766
Roland McGrath86976cd2008-01-30 13:31:01 +01001767 /* do the secure computing check first */
1768 secure_computing(regs->orig_ax);
1769
Roland McGrathd4d67152008-07-09 02:38:07 -07001770 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1771 ret = -1L;
1772
Roland McGratheeea3c32008-03-16 23:36:28 -07001773 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1774 tracehook_report_syscall_entry(regs))
1775 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001776
Josh Stone66700002009-08-24 14:43:11 -07001777 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001778 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001779
Roland McGrath86976cd2008-01-30 13:31:01 +01001780 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001781 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001782 audit_syscall_entry(AUDIT_ARCH_I386,
1783 regs->orig_ax,
1784 regs->bx, regs->cx,
1785 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001786#ifdef CONFIG_X86_64
1787 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001788 audit_syscall_entry(AUDIT_ARCH_X86_64,
1789 regs->orig_ax,
1790 regs->di, regs->si,
1791 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001792#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001793 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001794
1795 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001796}
1797
Roland McGrathd4d67152008-07-09 02:38:07 -07001798asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001799{
Oleg Nesterovd5196502009-12-15 16:47:21 -08001800 bool step;
1801
Roland McGrath86976cd2008-01-30 13:31:01 +01001802 if (unlikely(current->audit_context))
1803 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1804
Josh Stone66700002009-08-24 14:43:11 -07001805 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001806 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001807
Roland McGrathd4d67152008-07-09 02:38:07 -07001808 /*
1809 * If TIF_SYSCALL_EMU is set, we only get here because of
1810 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1811 * We already reported this syscall instruction in
Oleg Nesterovd5196502009-12-15 16:47:21 -08001812 * syscall_trace_enter().
Roland McGrathd4d67152008-07-09 02:38:07 -07001813 */
Oleg Nesterovd5196502009-12-15 16:47:21 -08001814 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1815 !test_thread_flag(TIF_SYSCALL_EMU);
1816 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1817 tracehook_report_syscall_exit(regs, step);
Roland McGrathd4d67152008-07-09 02:38:07 -07001818}