blob: a503b1fd04e51048c25fca9b675add81cac37304 [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
Markus Metzgereee3af42008-01-30 13:31:09 +0100144/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * does not yet catch signals sent when the child dies.
146 * in exit.c or in signal.c.
147 */
148
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500149/*
150 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500151 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100152#define FLAG_MASK_32 ((unsigned long) \
153 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
154 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
155 X86_EFLAGS_SF | X86_EFLAGS_TF | \
156 X86_EFLAGS_DF | X86_EFLAGS_OF | \
157 X86_EFLAGS_RF | X86_EFLAGS_AC))
158
Roland McGrath2047b082008-01-30 13:31:01 +0100159/*
160 * Determines whether a value may be installed in a segment register.
161 */
162static inline bool invalid_selector(u16 value)
163{
164 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
165}
166
167#ifdef CONFIG_X86_32
168
Roland McGrathe39c2892008-01-30 13:31:01 +0100169#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530171static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100173 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900174 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Roland McGrath06ee1b62008-01-30 13:31:01 +0100177static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100179 /*
180 * Returning the value truncates it to 16 bits.
181 */
182 unsigned int retval;
183 if (offset != offsetof(struct user_regs_struct, gs))
184 retval = *pt_regs_access(task_pt_regs(task), offset);
185 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100186 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900187 retval = get_user_gs(task_pt_regs(task));
188 else
189 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100190 }
191 return retval;
192}
193
194static int set_segment_reg(struct task_struct *task,
195 unsigned long offset, u16 value)
196{
197 /*
198 * The value argument was already truncated to 16 bits.
199 */
Roland McGrath2047b082008-01-30 13:31:01 +0100200 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100201 return -EIO;
202
Roland McGrathc63855d2008-02-06 22:39:44 +0100203 /*
204 * For %cs and %ss we cannot permit a null selector.
205 * We can permit a bogus selector as long as it has USER_RPL.
206 * Null selectors are fine for other segment registers, but
207 * we will never get back to user mode with invalid %cs or %ss
208 * and will take the trap in iret instead. Much code relies
209 * on user_mode() to distinguish a user trap frame (which can
210 * safely use invalid selectors) from a kernel trap frame.
211 */
212 switch (offset) {
213 case offsetof(struct user_regs_struct, cs):
214 case offsetof(struct user_regs_struct, ss):
215 if (unlikely(value == 0))
216 return -EIO;
217
218 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100219 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100220 break;
221
222 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100223 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900224 set_user_gs(task_pt_regs(task), value);
225 else
226 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
231
Roland McGrath2047b082008-01-30 13:31:01 +0100232#else /* CONFIG_X86_64 */
233
234#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
235
236static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
237{
238 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
239 return &regs->r15 + (offset / sizeof(regs->r15));
240}
241
242static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
243{
244 /*
245 * Returning the value truncates it to 16 bits.
246 */
247 unsigned int seg;
248
249 switch (offset) {
250 case offsetof(struct user_regs_struct, fs):
251 if (task == current) {
252 /* Older gas can't assemble movq %?s,%r?? */
253 asm("movl %%fs,%0" : "=r" (seg));
254 return seg;
255 }
256 return task->thread.fsindex;
257 case offsetof(struct user_regs_struct, gs):
258 if (task == current) {
259 asm("movl %%gs,%0" : "=r" (seg));
260 return seg;
261 }
262 return task->thread.gsindex;
263 case offsetof(struct user_regs_struct, ds):
264 if (task == current) {
265 asm("movl %%ds,%0" : "=r" (seg));
266 return seg;
267 }
268 return task->thread.ds;
269 case offsetof(struct user_regs_struct, es):
270 if (task == current) {
271 asm("movl %%es,%0" : "=r" (seg));
272 return seg;
273 }
274 return task->thread.es;
275
276 case offsetof(struct user_regs_struct, cs):
277 case offsetof(struct user_regs_struct, ss):
278 break;
279 }
280 return *pt_regs_access(task_pt_regs(task), offset);
281}
282
283static int set_segment_reg(struct task_struct *task,
284 unsigned long offset, u16 value)
285{
286 /*
287 * The value argument was already truncated to 16 bits.
288 */
289 if (invalid_selector(value))
290 return -EIO;
291
292 switch (offset) {
293 case offsetof(struct user_regs_struct,fs):
294 /*
295 * If this is setting fs as for normal 64-bit use but
296 * setting fs_base has implicitly changed it, leave it.
297 */
298 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
299 task->thread.fs != 0) ||
300 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
301 task->thread.fs == 0))
302 break;
303 task->thread.fsindex = value;
304 if (task == current)
305 loadsegment(fs, task->thread.fsindex);
306 break;
307 case offsetof(struct user_regs_struct,gs):
308 /*
309 * If this is setting gs as for normal 64-bit use but
310 * setting gs_base has implicitly changed it, leave it.
311 */
312 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
313 task->thread.gs != 0) ||
314 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
315 task->thread.gs == 0))
316 break;
317 task->thread.gsindex = value;
318 if (task == current)
319 load_gs_index(task->thread.gsindex);
320 break;
321 case offsetof(struct user_regs_struct,ds):
322 task->thread.ds = value;
323 if (task == current)
324 loadsegment(ds, task->thread.ds);
325 break;
326 case offsetof(struct user_regs_struct,es):
327 task->thread.es = value;
328 if (task == current)
329 loadsegment(es, task->thread.es);
330 break;
331
332 /*
333 * Can't actually change these in 64-bit mode.
334 */
335 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100336 if (unlikely(value == 0))
337 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100338#ifdef CONFIG_IA32_EMULATION
339 if (test_tsk_thread_flag(task, TIF_IA32))
340 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100341#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100342 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100343 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100344 if (unlikely(value == 0))
345 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100346#ifdef CONFIG_IA32_EMULATION
347 if (test_tsk_thread_flag(task, TIF_IA32))
348 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100349#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100350 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100351 }
352
353 return 0;
354}
355
Roland McGrath2047b082008-01-30 13:31:01 +0100356#endif /* CONFIG_X86_32 */
357
Roland McGrath06ee1b62008-01-30 13:31:01 +0100358static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100360 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Roland McGrath06ee1b62008-01-30 13:31:01 +0100362 /*
363 * If the debugger set TF, hide it from the readout.
364 */
365 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
366 retval &= ~X86_EFLAGS_TF;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return retval;
369}
370
Roland McGrath06ee1b62008-01-30 13:31:01 +0100371static int set_flags(struct task_struct *task, unsigned long value)
372{
373 struct pt_regs *regs = task_pt_regs(task);
374
375 /*
376 * If the user value contains TF, mark that
377 * it was not "us" (the debugger) that set it.
378 * If not, make sure it stays set if we had.
379 */
380 if (value & X86_EFLAGS_TF)
381 clear_tsk_thread_flag(task, TIF_FORCED_TF);
382 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
383 value |= X86_EFLAGS_TF;
384
385 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
386
387 return 0;
388}
389
390static int putreg(struct task_struct *child,
391 unsigned long offset, unsigned long value)
392{
393 switch (offset) {
394 case offsetof(struct user_regs_struct, cs):
395 case offsetof(struct user_regs_struct, ds):
396 case offsetof(struct user_regs_struct, es):
397 case offsetof(struct user_regs_struct, fs):
398 case offsetof(struct user_regs_struct, gs):
399 case offsetof(struct user_regs_struct, ss):
400 return set_segment_reg(child, offset, value);
401
402 case offsetof(struct user_regs_struct, flags):
403 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100404
405#ifdef CONFIG_X86_64
406 case offsetof(struct user_regs_struct,fs_base):
407 if (value >= TASK_SIZE_OF(child))
408 return -EIO;
409 /*
410 * When changing the segment base, use do_arch_prctl
411 * to set either thread.fs or thread.fsindex and the
412 * corresponding GDT slot.
413 */
414 if (child->thread.fs != value)
415 return do_arch_prctl(child, ARCH_SET_FS, value);
416 return 0;
417 case offsetof(struct user_regs_struct,gs_base):
418 /*
419 * Exactly the same here as the %fs handling above.
420 */
421 if (value >= TASK_SIZE_OF(child))
422 return -EIO;
423 if (child->thread.gs != value)
424 return do_arch_prctl(child, ARCH_SET_GS, value);
425 return 0;
426#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100427 }
428
429 *pt_regs_access(task_pt_regs(child), offset) = value;
430 return 0;
431}
432
433static unsigned long getreg(struct task_struct *task, unsigned long offset)
434{
435 switch (offset) {
436 case offsetof(struct user_regs_struct, cs):
437 case offsetof(struct user_regs_struct, ds):
438 case offsetof(struct user_regs_struct, es):
439 case offsetof(struct user_regs_struct, fs):
440 case offsetof(struct user_regs_struct, gs):
441 case offsetof(struct user_regs_struct, ss):
442 return get_segment_reg(task, offset);
443
444 case offsetof(struct user_regs_struct, flags):
445 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100446
447#ifdef CONFIG_X86_64
448 case offsetof(struct user_regs_struct, fs_base): {
449 /*
450 * do_arch_prctl may have used a GDT slot instead of
451 * the MSR. To userland, it appears the same either
452 * way, except the %fs segment selector might not be 0.
453 */
454 unsigned int seg = task->thread.fsindex;
455 if (task->thread.fs != 0)
456 return task->thread.fs;
457 if (task == current)
458 asm("movl %%fs,%0" : "=r" (seg));
459 if (seg != FS_TLS_SEL)
460 return 0;
461 return get_desc_base(&task->thread.tls_array[FS_TLS]);
462 }
463 case offsetof(struct user_regs_struct, gs_base): {
464 /*
465 * Exactly the same here as the %fs handling above.
466 */
467 unsigned int seg = task->thread.gsindex;
468 if (task->thread.gs != 0)
469 return task->thread.gs;
470 if (task == current)
471 asm("movl %%gs,%0" : "=r" (seg));
472 if (seg != GS_TLS_SEL)
473 return 0;
474 return get_desc_base(&task->thread.tls_array[GS_TLS]);
475 }
476#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100477 }
478
479 return *pt_regs_access(task_pt_regs(task), offset);
480}
481
Roland McGrath91e7b702008-01-30 13:31:52 +0100482static int genregs_get(struct task_struct *target,
483 const struct user_regset *regset,
484 unsigned int pos, unsigned int count,
485 void *kbuf, void __user *ubuf)
486{
487 if (kbuf) {
488 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800489 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100490 *k++ = getreg(target, pos);
491 count -= sizeof(*k);
492 pos += sizeof(*k);
493 }
494 } else {
495 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800496 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100497 if (__put_user(getreg(target, pos), u++))
498 return -EFAULT;
499 count -= sizeof(*u);
500 pos += sizeof(*u);
501 }
502 }
503
504 return 0;
505}
506
507static int genregs_set(struct task_struct *target,
508 const struct user_regset *regset,
509 unsigned int pos, unsigned int count,
510 const void *kbuf, const void __user *ubuf)
511{
512 int ret = 0;
513 if (kbuf) {
514 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800515 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100516 ret = putreg(target, pos, *k++);
517 count -= sizeof(*k);
518 pos += sizeof(*k);
519 }
520 } else {
521 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800522 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100523 unsigned long word;
524 ret = __get_user(word, u++);
525 if (ret)
526 break;
527 ret = putreg(target, pos, word);
528 count -= sizeof(*u);
529 pos += sizeof(*u);
530 }
531 }
532 return ret;
533}
534
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100535static void ptrace_triggered(struct perf_event *bp, int nmi,
536 struct perf_sample_data *data,
537 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100538{
Roland McGrath0f534092008-01-30 13:30:59 +0100539 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200540 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100541
K.Prasad72f674d2009-06-01 23:45:48 +0530542 /*
543 * Store in the virtual DR6 register the fact that the breakpoint
544 * was hit so the thread's debugger will see it.
545 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200546 for (i = 0; i < HBP_NUM; i++) {
547 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530548 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100549 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100550
K.Prasad72f674d2009-06-01 23:45:48 +0530551 thread->debugreg6 |= (DR_TRAP0 << i);
552}
553
554/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200555 * Walk through every ptrace breakpoints for this thread and
556 * build the dr7 value on top of their attributes.
557 *
558 */
559static unsigned long ptrace_get_dr7(struct perf_event *bp[])
560{
561 int i;
562 int dr7 = 0;
563 struct arch_hw_breakpoint *info;
564
565 for (i = 0; i < HBP_NUM; i++) {
566 if (bp[i] && !bp[i]->attr.disabled) {
567 info = counter_arch_bp(bp[i]);
568 dr7 |= encode_dr7(i, info->len, info->type);
569 }
570 }
571
572 return dr7;
573}
574
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100575static int
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100576ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100577 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100578{
579 int err;
580 int gen_len, gen_type;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100581 struct perf_event_attr attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100582
583 /*
Adam Buchbinderc9404c92009-12-18 15:40:42 -0500584 * We should have at least an inactive breakpoint at this
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100585 * slot. It means the user is writing dr7 without having
586 * written the address register first
587 */
588 if (!bp)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100589 return -EINVAL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100590
591 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
592 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100593 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100594
595 attr = bp->attr;
596 attr.bp_len = gen_len;
597 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100598 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100599
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100600 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100601}
602
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200603/*
K.Prasad72f674d2009-06-01 23:45:48 +0530604 * Handle ptrace writes to debug register 7.
605 */
606static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
607{
608 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200609 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530610 int i, orig_ret = 0, rc = 0;
611 int enabled, second_pass = 0;
612 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200613 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530614
615 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200616 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530617restore:
618 /*
619 * Loop through all the hardware breakpoints, making the
620 * appropriate changes to each.
621 */
622 for (i = 0; i < HBP_NUM; i++) {
623 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200624 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530625
626 if (!enabled) {
627 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200628 /*
629 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530630 * unless all register_user_hw_breakpoint()
631 * requests have succeeded. This prevents
632 * any window of opportunity for debug
633 * register grabbing by other users.
634 */
635 if (!second_pass)
636 continue;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100637
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100638 rc = ptrace_modify_breakpoint(bp, len, type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100639 tsk, 1);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100640 if (rc)
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100641 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530642 }
643 continue;
644 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200645
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100646 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
647 if (rc)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200648 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530649 }
650 /*
651 * Make a second pass to free the remaining unused breakpoints
652 * or to restore the original breakpoints if an error occurred.
653 */
654 if (!second_pass) {
655 second_pass = 1;
656 if (rc < 0) {
657 orig_ret = rc;
658 data = old_dr7;
659 }
660 goto restore;
661 }
662 return ((orig_ret < 0) ? orig_ret : rc);
663}
664
665/*
666 * Handle PTRACE_PEEKUSR calls for the debug register area.
667 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530668static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530669{
670 struct thread_struct *thread = &(tsk->thread);
671 unsigned long val = 0;
672
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200673 if (n < HBP_NUM) {
674 struct perf_event *bp;
675 bp = thread->ptrace_bps[n];
676 if (!bp)
677 return 0;
678 val = bp->hw.info.address;
679 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530680 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200681 } else if (n == 7) {
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100682 val = thread->ptrace_dr7;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200683 }
K.Prasad72f674d2009-06-01 23:45:48 +0530684 return val;
685}
686
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200687static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
688 unsigned long addr)
689{
690 struct perf_event *bp;
691 struct thread_struct *t = &tsk->thread;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100692 struct perf_event_attr attr;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200693
694 if (!t->ptrace_bps[nr]) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100695 hw_breakpoint_init(&attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200696 /*
697 * Put stub len and type to register (reserve) an inactive but
698 * correct bp
699 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100700 attr.bp_addr = addr;
701 attr.bp_len = HW_BREAKPOINT_LEN_1;
702 attr.bp_type = HW_BREAKPOINT_W;
703 attr.disabled = 1;
704
705 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100706
707 /*
708 * CHECKME: the previous code returned -EIO if the addr wasn't
709 * a valid task virtual addr. The new one will return -EINVAL in
710 * this case.
711 * -EINVAL may be what we want for in-kernel breakpoints users,
712 * but -EIO looks better for ptrace, since we refuse a register
713 * writing for the user. And anyway this is the previous
714 * behaviour.
715 */
716 if (IS_ERR(bp))
717 return PTR_ERR(bp);
718
719 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200720 } else {
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100721 int err;
722
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200723 bp = t->ptrace_bps[nr];
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100724
725 attr = bp->attr;
726 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100727 err = modify_user_hw_breakpoint(bp, &attr);
728 if (err)
729 return err;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200730 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200731
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734}
735
K.Prasad72f674d2009-06-01 23:45:48 +0530736/*
737 * Handle PTRACE_POKEUSR calls for the debug register area.
738 */
739int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
K.Prasad72f674d2009-06-01 23:45:48 +0530741 struct thread_struct *thread = &(tsk->thread);
742 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
K.Prasad72f674d2009-06-01 23:45:48 +0530744 /* There are no DR4 or DR5 registers */
745 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -EIO;
747
K.Prasad72f674d2009-06-01 23:45:48 +0530748 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200749 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530750 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
K.Prasad72f674d2009-06-01 23:45:48 +0530752 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200753 rc = ptrace_set_breakpoint_addr(tsk, n, val);
754 if (rc)
755 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530756 }
757 /* All that's left is DR7 */
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100758 if (n == 7) {
K.Prasad72f674d2009-06-01 23:45:48 +0530759 rc = ptrace_write_dr7(tsk, val);
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100760 if (!rc)
761 thread->ptrace_dr7 = val;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
K.Prasad72f674d2009-06-01 23:45:48 +0530764ret_path:
765 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Roland McGrath325af5f2008-08-08 15:58:39 -0700768/*
769 * These access the current or another (stopped) task's io permission
770 * bitmap for debugging or core dump.
771 */
772static int ioperm_active(struct task_struct *target,
773 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100774{
Roland McGrath325af5f2008-08-08 15:58:39 -0700775 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100776}
777
Roland McGrath325af5f2008-08-08 15:58:39 -0700778static int ioperm_get(struct task_struct *target,
779 const struct user_regset *regset,
780 unsigned int pos, unsigned int count,
781 void *kbuf, void __user *ubuf)
782{
783 if (!target->thread.io_bitmap_ptr)
784 return -ENXIO;
785
786 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
787 target->thread.io_bitmap_ptr,
788 0, IO_BITMAP_BYTES);
789}
790
Markus Metzger93fa7632008-04-08 11:01:58 +0200791#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200792/*
793 * A branch trace store context.
794 *
795 * Contexts may only be installed by ptrace_bts_config() and only for
796 * ptraced tasks.
797 *
798 * Contexts are destroyed when the tracee is detached from the tracer.
799 * The actual destruction work requires interrupts enabled, so the
800 * work is deferred and will be scheduled during __ptrace_unlink().
801 *
802 * Contexts hold an additional task_struct reference on the traced
803 * task, as well as a reference on the tracer's mm.
804 *
805 * Ptrace already holds a task_struct for the duration of ptrace operations,
806 * but since destruction is deferred, it may be executed after both
807 * tracer and tracee exited.
808 */
809struct bts_context {
810 /* The branch trace handle. */
811 struct bts_tracer *tracer;
812
813 /* The buffer used to store the branch trace and its size. */
814 void *buffer;
815 unsigned int size;
816
817 /* The mm that paid for the above buffer. */
818 struct mm_struct *mm;
819
820 /* The task this context belongs to. */
821 struct task_struct *task;
822
823 /* The signal to send on a bts buffer overflow. */
824 unsigned int bts_ovfl_signal;
825
826 /* The work struct to destroy a context. */
827 struct work_struct work;
828};
829
Markus Metzger1cb81b12009-04-24 09:51:43 +0200830static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200831{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200832 void *buffer = NULL;
833 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200834
Markus Metzger1cb81b12009-04-24 09:51:43 +0200835 err = account_locked_memory(current->mm, current->signal->rlim, size);
836 if (err < 0)
837 return err;
838
839 buffer = kzalloc(size, GFP_KERNEL);
840 if (!buffer)
841 goto out_refund;
842
843 context->buffer = buffer;
844 context->size = size;
845 context->mm = get_task_mm(current);
846
847 return 0;
848
849 out_refund:
850 refund_locked_memory(current->mm, size);
851 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200852}
853
854static inline void free_bts_buffer(struct bts_context *context)
855{
856 if (!context->buffer)
857 return;
858
859 kfree(context->buffer);
860 context->buffer = NULL;
861
Markus Metzger1cb81b12009-04-24 09:51:43 +0200862 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200863 context->size = 0;
864
865 mmput(context->mm);
866 context->mm = NULL;
867}
868
869static void free_bts_context_work(struct work_struct *w)
870{
871 struct bts_context *context;
872
873 context = container_of(w, struct bts_context, work);
874
875 ds_release_bts(context->tracer);
876 put_task_struct(context->task);
877 free_bts_buffer(context);
878 kfree(context);
879}
880
881static inline void free_bts_context(struct bts_context *context)
882{
883 INIT_WORK(&context->work, free_bts_context_work);
884 schedule_work(&context->work);
885}
886
887static inline struct bts_context *alloc_bts_context(struct task_struct *task)
888{
889 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
890 if (context) {
891 context->task = task;
892 task->bts = context;
893
894 get_task_struct(task);
895 }
896
897 return context;
898}
899
Markus Metzger93fa7632008-04-08 11:01:58 +0200900static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100901 struct bts_struct __user *out)
902{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200903 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100904 const struct bts_trace *trace;
905 struct bts_struct bts;
906 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200907 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100908
Markus Metzgere2b371f2009-04-03 16:43:35 +0200909 context = child->bts;
910 if (!context)
911 return -ESRCH;
912
913 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100914 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200915 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100916
917 at = trace->ds.top - ((index + 1) * trace->ds.size);
918 if ((void *)at < trace->ds.begin)
919 at += (trace->ds.n * trace->ds.size);
920
921 if (!trace->read)
922 return -EOPNOTSUPP;
923
Markus Metzgere2b371f2009-04-03 16:43:35 +0200924 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200925 if (error < 0)
926 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100927
Markus Metzgerc2724772008-12-11 13:49:59 +0100928 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100929 return -EFAULT;
930
Markus Metzgerc2724772008-12-11 13:49:59 +0100931 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100932}
933
Markus Metzgera95d67f2008-01-30 13:31:20 +0100934static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100935 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100936 struct bts_struct __user *out)
937{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200938 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100939 const struct bts_trace *trace;
940 const unsigned char *at;
941 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100942
Markus Metzgere2b371f2009-04-03 16:43:35 +0200943 context = child->bts;
944 if (!context)
945 return -ESRCH;
946
947 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100948 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200949 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100950
Markus Metzgerc2724772008-12-11 13:49:59 +0100951 if (!trace->read)
952 return -EOPNOTSUPP;
953
954 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100955 return -EIO;
956
Markus Metzgerc2724772008-12-11 13:49:59 +0100957 for (at = trace->ds.begin; (void *)at < trace->ds.top;
958 out++, drained++, at += trace->ds.size) {
959 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100960
Markus Metzgere2b371f2009-04-03 16:43:35 +0200961 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100962 if (error < 0)
963 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100964
Markus Metzgerc2724772008-12-11 13:49:59 +0100965 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100966 return -EFAULT;
967 }
968
Markus Metzgerc2724772008-12-11 13:49:59 +0100969 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
970
Markus Metzgere2b371f2009-04-03 16:43:35 +0200971 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200972 if (error < 0)
973 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100974
Markus Metzgerc2724772008-12-11 13:49:59 +0100975 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100976}
977
978static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100979 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100980 const struct ptrace_bts_config __user *ucfg)
981{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200982 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100983 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100984 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100985
Markus Metzgercba4b652008-01-30 13:32:03 +0100986 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100987 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100988
Markus Metzgera95d67f2008-01-30 13:31:20 +0100989 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100990 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100991
Markus Metzgere2b371f2009-04-03 16:43:35 +0200992 context = child->bts;
993 if (!context)
994 context = alloc_bts_context(child);
995 if (!context)
996 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100997
Markus Metzgerc2724772008-12-11 13:49:59 +0100998 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
999 if (!cfg.signal)
1000 return -EINVAL;
1001
Américo Wang5a8ac9d2009-03-13 15:56:58 +08001002 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +02001003 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +01001004 }
1005
Markus Metzgere2b371f2009-04-03 16:43:35 +02001006 ds_release_bts(context->tracer);
1007 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001008
Markus Metzgere2b371f2009-04-03 16:43:35 +02001009 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +02001010 int err;
1011
Markus Metzgere2b371f2009-04-03 16:43:35 +02001012 free_bts_buffer(context);
1013 if (!cfg.size)
1014 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +01001015
Markus Metzger1cb81b12009-04-24 09:51:43 +02001016 err = alloc_bts_buffer(context, cfg.size);
1017 if (err < 0)
1018 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +01001019 }
Markus Metzgera95d67f2008-01-30 13:31:20 +01001020
Markus Metzgerda35c372008-01-30 13:32:03 +01001021 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +01001022 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +01001023
Markus Metzgerda35c372008-01-30 13:32:03 +01001024 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +01001025 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +01001026
Markus Metzgerde79f542009-04-03 16:43:40 +02001027 context->tracer =
1028 ds_request_bts_task(child, context->buffer, context->size,
1029 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +02001030 if (unlikely(IS_ERR(context->tracer))) {
1031 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +01001032
Markus Metzgere2b371f2009-04-03 16:43:35 +02001033 free_bts_buffer(context);
1034 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001035 return error;
1036 }
Markus Metzgerda35c372008-01-30 13:32:03 +01001037
Markus Metzgerc2724772008-12-11 13:49:59 +01001038 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001039}
1040
Markus Metzgera95d67f2008-01-30 13:31:20 +01001041static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +01001042 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +01001043 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +01001044{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001045 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001046 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001047 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +01001048
Markus Metzgere2b371f2009-04-03 16:43:35 +02001049 context = child->bts;
1050 if (!context)
1051 return -ESRCH;
1052
Markus Metzgercba4b652008-01-30 13:32:03 +01001053 if (cfg_size < sizeof(cfg))
1054 return -EIO;
1055
Markus Metzgere2b371f2009-04-03 16:43:35 +02001056 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001057 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001058 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001059
Markus Metzgera95d67f2008-01-30 13:31:20 +01001060 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +02001061 cfg.size = trace->ds.end - trace->ds.begin;
1062 cfg.signal = context->bts_ovfl_signal;
1063 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +01001064
Markus Metzger93fa7632008-04-08 11:01:58 +02001065 if (cfg.signal)
1066 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1067
Markus Metzgerc2724772008-12-11 13:49:59 +01001068 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +02001069 cfg.flags |= PTRACE_BTS_O_TRACE;
1070
Markus Metzgerc2724772008-12-11 13:49:59 +01001071 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +02001072 cfg.flags |= PTRACE_BTS_O_SCHED;
1073
Markus Metzgera95d67f2008-01-30 13:31:20 +01001074 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1075 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +01001076
Markus Metzgera95d67f2008-01-30 13:31:20 +01001077 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001078}
1079
Markus Metzgerc2724772008-12-11 13:49:59 +01001080static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -08001081{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001082 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001083 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001084
Markus Metzgere2b371f2009-04-03 16:43:35 +02001085 context = child->bts;
1086 if (!context)
1087 return -ESRCH;
1088
1089 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001090 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001091 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001092
Markus Metzgerc2724772008-12-11 13:49:59 +01001093 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001094
Markus Metzgere2b371f2009-04-03 16:43:35 +02001095 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001096}
1097
Markus Metzgerc2724772008-12-11 13:49:59 +01001098static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +01001099{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001100 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001101 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +01001102
Markus Metzgere2b371f2009-04-03 16:43:35 +02001103 context = child->bts;
1104 if (!context)
1105 return -ESRCH;
1106
1107 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001108 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001109 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001110
Markus Metzgerc2724772008-12-11 13:49:59 +01001111 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +02001112}
Markus Metzgerbf53de92008-12-19 15:10:24 +01001113
Markus Metzgere2b371f2009-04-03 16:43:35 +02001114/*
1115 * Called from __ptrace_unlink() after the child has been moved back
1116 * to its original parent.
1117 */
Markus Metzger0f481402009-04-03 16:43:48 +02001118void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +01001119{
1120 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +02001121 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +01001122 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +01001123 }
1124}
Markus Metzger93fa7632008-04-08 11:01:58 +02001125#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/*
1128 * Called by kernel/ptrace.c when detaching..
1129 *
1130 * Make sure the single step bit is not set.
1131 */
1132void ptrace_disable(struct task_struct *child)
1133{
1134 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001135#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Roland McGrath5a4646a2008-01-30 13:31:54 +01001140#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1141static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1142#endif
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1145{
Roland McGrath5a4646a2008-01-30 13:31:54 +01001146 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 unsigned long __user *datap = (unsigned long __user *)data;
1148
1149 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* read the word at location addr in the USER area. */
1151 case PTRACE_PEEKUSR: {
1152 unsigned long tmp;
1153
1154 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001155 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1156 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158
1159 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001160 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001162 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1163 addr <= offsetof(struct user, u_debugreg[7])) {
1164 addr -= offsetof(struct user, u_debugreg[0]);
1165 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 ret = put_user(tmp, datap);
1168 break;
1169 }
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1172 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001173 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1174 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
1176
Roland McGrathe9c86c72008-01-30 13:31:01 +01001177 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001179 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1180 addr <= offsetof(struct user, u_debugreg[7])) {
1181 addr -= offsetof(struct user, u_debugreg[0]);
1182 ret = ptrace_set_debugreg(child,
1183 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Roland McGrath5a4646a2008-01-30 13:31:54 +01001187 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1188 return copy_regset_to_user(child,
1189 task_user_regset_view(current),
1190 REGSET_GENERAL,
1191 0, sizeof(struct user_regs_struct),
1192 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Roland McGrath5a4646a2008-01-30 13:31:54 +01001194 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1195 return copy_regset_from_user(child,
1196 task_user_regset_view(current),
1197 REGSET_GENERAL,
1198 0, sizeof(struct user_regs_struct),
1199 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Roland McGrath5a4646a2008-01-30 13:31:54 +01001201 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1202 return copy_regset_to_user(child,
1203 task_user_regset_view(current),
1204 REGSET_FP,
1205 0, sizeof(struct user_i387_struct),
1206 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Roland McGrath5a4646a2008-01-30 13:31:54 +01001208 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1209 return copy_regset_from_user(child,
1210 task_user_regset_view(current),
1211 REGSET_FP,
1212 0, sizeof(struct user_i387_struct),
1213 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Roland McGrathe9c86c72008-01-30 13:31:01 +01001215#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001216 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1217 return copy_regset_to_user(child, &user_x86_32_view,
1218 REGSET_XFP,
1219 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001220 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Roland McGrath5a4646a2008-01-30 13:31:54 +01001222 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1223 return copy_regset_from_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;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Roland McGrathe9c86c72008-01-30 13:31:01 +01001229#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001231 if (addr < 0)
1232 return -EIO;
1233 ret = do_get_thread_area(child, addr,
1234 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236
1237 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001238 if (addr < 0)
1239 return -EIO;
1240 ret = do_set_thread_area(child, addr,
1241 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001243#endif
1244
1245#ifdef CONFIG_X86_64
1246 /* normal 64bit interface to access TLS data.
1247 Works just like arch_prctl, except that the arguments
1248 are reversed. */
1249 case PTRACE_ARCH_PRCTL:
1250 ret = do_arch_prctl(child, data, addr);
1251 break;
1252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001254 /*
1255 * These bits need more cooking - not enabled yet:
1256 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001257#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001258 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001259 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001260 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001261 break;
1262
1263 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001264 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001265 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001266 break;
1267
Markus Metzgerc2724772008-12-11 13:49:59 +01001268 case PTRACE_BTS_SIZE:
1269 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001270 break;
1271
1272 case PTRACE_BTS_GET:
1273 ret = ptrace_bts_read_record
1274 (child, data, (struct bts_struct __user *) addr);
1275 break;
1276
1277 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001278 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001279 break;
1280
1281 case PTRACE_BTS_DRAIN:
1282 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001283 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001284 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001285#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 default:
1288 ret = ptrace_request(child, request, addr, data);
1289 break;
1290 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return ret;
1293}
1294
Roland McGrathcb757c42008-01-30 13:31:01 +01001295#ifdef CONFIG_IA32_EMULATION
1296
Roland McGrath099cd6e2008-01-30 13:31:01 +01001297#include <linux/compat.h>
1298#include <linux/syscalls.h>
1299#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001300#include <asm/user32.h>
1301
1302#define R32(l,q) \
1303 case offsetof(struct user32, regs.l): \
1304 regs->q = value; break
1305
1306#define SEG32(rs) \
1307 case offsetof(struct user32, regs.rs): \
1308 return set_segment_reg(child, \
1309 offsetof(struct user_regs_struct, rs), \
1310 value); \
1311 break
1312
1313static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1314{
1315 struct pt_regs *regs = task_pt_regs(child);
1316
1317 switch (regno) {
1318
1319 SEG32(cs);
1320 SEG32(ds);
1321 SEG32(es);
1322 SEG32(fs);
1323 SEG32(gs);
1324 SEG32(ss);
1325
1326 R32(ebx, bx);
1327 R32(ecx, cx);
1328 R32(edx, dx);
1329 R32(edi, di);
1330 R32(esi, si);
1331 R32(ebp, bp);
1332 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001333 R32(eip, ip);
1334 R32(esp, sp);
1335
Roland McGrath40f09332008-02-28 19:57:07 -08001336 case offsetof(struct user32, regs.orig_eax):
1337 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001338 * A 32-bit debugger setting orig_eax means to restore
1339 * the state of the task restarting a 32-bit syscall.
1340 * Make sure we interpret the -ERESTART* codes correctly
1341 * in case the task is not actually still sitting at the
1342 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001343 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001344 regs->orig_ax = value;
1345 if (syscall_get_nr(child, regs) >= 0)
1346 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001347 break;
1348
Roland McGrathcb757c42008-01-30 13:31:01 +01001349 case offsetof(struct user32, regs.eflags):
1350 return set_flags(child, value);
1351
1352 case offsetof(struct user32, u_debugreg[0]) ...
1353 offsetof(struct user32, u_debugreg[7]):
1354 regno -= offsetof(struct user32, u_debugreg[0]);
1355 return ptrace_set_debugreg(child, regno / 4, value);
1356
1357 default:
1358 if (regno > sizeof(struct user32) || (regno & 3))
1359 return -EIO;
1360
1361 /*
1362 * Other dummy fields in the virtual user structure
1363 * are ignored
1364 */
1365 break;
1366 }
1367 return 0;
1368}
1369
1370#undef R32
1371#undef SEG32
1372
1373#define R32(l,q) \
1374 case offsetof(struct user32, regs.l): \
1375 *val = regs->q; break
1376
1377#define SEG32(rs) \
1378 case offsetof(struct user32, regs.rs): \
1379 *val = get_segment_reg(child, \
1380 offsetof(struct user_regs_struct, rs)); \
1381 break
1382
1383static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1384{
1385 struct pt_regs *regs = task_pt_regs(child);
1386
1387 switch (regno) {
1388
1389 SEG32(ds);
1390 SEG32(es);
1391 SEG32(fs);
1392 SEG32(gs);
1393
1394 R32(cs, cs);
1395 R32(ss, ss);
1396 R32(ebx, bx);
1397 R32(ecx, cx);
1398 R32(edx, dx);
1399 R32(edi, di);
1400 R32(esi, si);
1401 R32(ebp, bp);
1402 R32(eax, ax);
1403 R32(orig_eax, orig_ax);
1404 R32(eip, ip);
1405 R32(esp, sp);
1406
1407 case offsetof(struct user32, regs.eflags):
1408 *val = get_flags(child);
1409 break;
1410
1411 case offsetof(struct user32, u_debugreg[0]) ...
1412 offsetof(struct user32, u_debugreg[7]):
1413 regno -= offsetof(struct user32, u_debugreg[0]);
1414 *val = ptrace_get_debugreg(child, regno / 4);
1415 break;
1416
1417 default:
1418 if (regno > sizeof(struct user32) || (regno & 3))
1419 return -EIO;
1420
1421 /*
1422 * Other dummy fields in the virtual user structure
1423 * are ignored
1424 */
1425 *val = 0;
1426 break;
1427 }
1428 return 0;
1429}
1430
1431#undef R32
1432#undef SEG32
1433
Roland McGrath91e7b702008-01-30 13:31:52 +01001434static int genregs32_get(struct task_struct *target,
1435 const struct user_regset *regset,
1436 unsigned int pos, unsigned int count,
1437 void *kbuf, void __user *ubuf)
1438{
1439 if (kbuf) {
1440 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001441 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001442 getreg32(target, pos, k++);
1443 count -= sizeof(*k);
1444 pos += sizeof(*k);
1445 }
1446 } else {
1447 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001448 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001449 compat_ulong_t word;
1450 getreg32(target, pos, &word);
1451 if (__put_user(word, u++))
1452 return -EFAULT;
1453 count -= sizeof(*u);
1454 pos += sizeof(*u);
1455 }
1456 }
1457
1458 return 0;
1459}
1460
1461static int genregs32_set(struct task_struct *target,
1462 const struct user_regset *regset,
1463 unsigned int pos, unsigned int count,
1464 const void *kbuf, const void __user *ubuf)
1465{
1466 int ret = 0;
1467 if (kbuf) {
1468 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001469 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001470 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001471 count -= sizeof(*k);
1472 pos += sizeof(*k);
1473 }
1474 } else {
1475 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001476 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001477 compat_ulong_t word;
1478 ret = __get_user(word, u++);
1479 if (ret)
1480 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001481 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001482 count -= sizeof(*u);
1483 pos += sizeof(*u);
1484 }
1485 }
1486 return ret;
1487}
1488
Roland McGrath562b80b2008-04-22 12:21:25 -07001489long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1490 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001491{
Roland McGrath562b80b2008-04-22 12:21:25 -07001492 unsigned long addr = caddr;
1493 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001494 void __user *datap = compat_ptr(data);
1495 int ret;
1496 __u32 val;
1497
1498 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001499 case PTRACE_PEEKUSR:
1500 ret = getreg32(child, addr, &val);
1501 if (ret == 0)
1502 ret = put_user(val, (__u32 __user *)datap);
1503 break;
1504
1505 case PTRACE_POKEUSR:
1506 ret = putreg32(child, addr, data);
1507 break;
1508
Roland McGrath5a4646a2008-01-30 13:31:54 +01001509 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1510 return copy_regset_to_user(child, &user_x86_32_view,
1511 REGSET_GENERAL,
1512 0, sizeof(struct user_regs_struct32),
1513 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001514
Roland McGrath5a4646a2008-01-30 13:31:54 +01001515 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1516 return copy_regset_from_user(child, &user_x86_32_view,
1517 REGSET_GENERAL, 0,
1518 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_GETFPREGS: /* Get the child FPU state. */
1522 return copy_regset_to_user(child, &user_x86_32_view,
1523 REGSET_FP, 0,
1524 sizeof(struct user_i387_ia32_struct),
1525 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001526
Roland McGrath5a4646a2008-01-30 13:31:54 +01001527 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1528 return copy_regset_from_user(
1529 child, &user_x86_32_view, REGSET_FP,
1530 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001531
Roland McGrath5a4646a2008-01-30 13:31:54 +01001532 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1533 return copy_regset_to_user(child, &user_x86_32_view,
1534 REGSET_XFP, 0,
1535 sizeof(struct user32_fxsr_struct),
1536 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001537
Roland McGrath5a4646a2008-01-30 13:31:54 +01001538 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1539 return copy_regset_from_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 McGrath562b80b2008-04-22 12:21:25 -07001544 case PTRACE_GET_THREAD_AREA:
1545 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001546#ifdef CONFIG_X86_PTRACE_BTS
1547 case PTRACE_BTS_CONFIG:
1548 case PTRACE_BTS_STATUS:
1549 case PTRACE_BTS_SIZE:
1550 case PTRACE_BTS_GET:
1551 case PTRACE_BTS_CLEAR:
1552 case PTRACE_BTS_DRAIN:
1553#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001554 return arch_ptrace(child, request, addr, data);
1555
Roland McGrath099cd6e2008-01-30 13:31:01 +01001556 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001557 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001558 }
1559
Roland McGrath099cd6e2008-01-30 13:31:01 +01001560 return ret;
1561}
1562
Roland McGrathcb757c42008-01-30 13:31:01 +01001563#endif /* CONFIG_IA32_EMULATION */
1564
Roland McGrath070459d2008-01-30 13:31:53 +01001565#ifdef CONFIG_X86_64
1566
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001567static struct user_regset x86_64_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001568 [REGSET_GENERAL] = {
1569 .core_note_type = NT_PRSTATUS,
1570 .n = sizeof(struct user_regs_struct) / sizeof(long),
1571 .size = sizeof(long), .align = sizeof(long),
1572 .get = genregs_get, .set = genregs_set
1573 },
1574 [REGSET_FP] = {
1575 .core_note_type = NT_PRFPREG,
1576 .n = sizeof(struct user_i387_struct) / sizeof(long),
1577 .size = sizeof(long), .align = sizeof(long),
1578 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1579 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001580 [REGSET_XSTATE] = {
1581 .core_note_type = NT_X86_XSTATE,
1582 .size = sizeof(u64), .align = sizeof(u64),
1583 .active = xstateregs_active, .get = xstateregs_get,
1584 .set = xstateregs_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
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001611static struct user_regset x86_32_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001612 [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 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001630 [REGSET_XSTATE] = {
1631 .core_note_type = NT_X86_XSTATE,
1632 .size = sizeof(u64), .align = sizeof(u64),
1633 .active = xstateregs_active, .get = xstateregs_get,
1634 .set = xstateregs_set
1635 },
Roland McGrath070459d2008-01-30 13:31:53 +01001636 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001637 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001638 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1639 .size = sizeof(struct user_desc),
1640 .align = sizeof(struct user_desc),
1641 .active = regset_tls_active,
1642 .get = regset_tls_get, .set = regset_tls_set
1643 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001644 [REGSET_IOPERM32] = {
1645 .core_note_type = NT_386_IOPERM,
1646 .n = IO_BITMAP_BYTES / sizeof(u32),
1647 .size = sizeof(u32), .align = sizeof(u32),
1648 .active = ioperm_active, .get = ioperm_get
1649 },
Roland McGrath070459d2008-01-30 13:31:53 +01001650};
1651
1652static const struct user_regset_view user_x86_32_view = {
1653 .name = "i386", .e_machine = EM_386,
1654 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1655};
1656#endif
1657
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001658/*
1659 * This represents bytes 464..511 in the memory layout exported through
1660 * the REGSET_XSTATE interface.
1661 */
1662u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1663
1664void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1665{
1666#ifdef CONFIG_X86_64
1667 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1668#endif
1669#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1670 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1671#endif
1672 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1673}
1674
Roland McGrath070459d2008-01-30 13:31:53 +01001675const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1676{
1677#ifdef CONFIG_IA32_EMULATION
1678 if (test_tsk_thread_flag(task, TIF_IA32))
1679#endif
1680#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1681 return &user_x86_32_view;
1682#endif
1683#ifdef CONFIG_X86_64
1684 return &user_x86_64_view;
1685#endif
1686}
1687
Oleg Nesterov7f385512009-12-15 16:47:20 -08001688static void fill_sigtrap_info(struct task_struct *tsk,
1689 struct pt_regs *regs,
1690 int error_code, int si_code,
1691 struct siginfo *info)
1692{
1693 tsk->thread.trap_no = 1;
1694 tsk->thread.error_code = error_code;
1695
1696 memset(info, 0, sizeof(*info));
1697 info->si_signo = SIGTRAP;
1698 info->si_code = si_code;
1699 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1700}
1701
1702void user_single_step_siginfo(struct task_struct *tsk,
1703 struct pt_regs *regs,
1704 struct siginfo *info)
1705{
1706 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1707}
1708
Srinivasa Dsda654b72008-09-23 15:23:52 +05301709void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1710 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct siginfo info;
1713
Oleg Nesterov7f385512009-12-15 16:47:20 -08001714 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
Simon Arlott27b46d72007-10-20 01:13:56 +02001715 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 force_sig_info(SIGTRAP, &info, tsk);
1717}
1718
Roland McGrath86976cd2008-01-30 13:31:01 +01001719
Roland McGrathd4d67152008-07-09 02:38:07 -07001720#ifdef CONFIG_X86_32
1721# define IS_IA32 1
1722#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001723# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001724#else
1725# define IS_IA32 0
1726#endif
1727
1728/*
1729 * We must return the syscall number to actually look up in the table.
1730 * This can be -1L to skip running any syscall at all.
1731 */
1732asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001733{
Roland McGrathd4d67152008-07-09 02:38:07 -07001734 long ret = 0;
1735
Roland McGrath380fdd72008-07-09 02:39:29 -07001736 /*
1737 * If we stepped into a sysenter/syscall insn, it trapped in
1738 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1739 * If user-mode had set TF itself, then it's still clear from
1740 * do_debug() and we need to set it again to restore the user
1741 * state. If we entered on the slow path, TF was already set.
1742 */
1743 if (test_thread_flag(TIF_SINGLESTEP))
1744 regs->flags |= X86_EFLAGS_TF;
1745
Roland McGrath86976cd2008-01-30 13:31:01 +01001746 /* do the secure computing check first */
1747 secure_computing(regs->orig_ax);
1748
Roland McGrathd4d67152008-07-09 02:38:07 -07001749 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1750 ret = -1L;
1751
Roland McGratheeea3c32008-03-16 23:36:28 -07001752 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1753 tracehook_report_syscall_entry(regs))
1754 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001755
Josh Stone66700002009-08-24 14:43:11 -07001756 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001757 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001758
Roland McGrath86976cd2008-01-30 13:31:01 +01001759 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001760 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001761 audit_syscall_entry(AUDIT_ARCH_I386,
1762 regs->orig_ax,
1763 regs->bx, regs->cx,
1764 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001765#ifdef CONFIG_X86_64
1766 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001767 audit_syscall_entry(AUDIT_ARCH_X86_64,
1768 regs->orig_ax,
1769 regs->di, regs->si,
1770 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001771#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001772 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001773
1774 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001775}
1776
Roland McGrathd4d67152008-07-09 02:38:07 -07001777asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001778{
Oleg Nesterovd5196502009-12-15 16:47:21 -08001779 bool step;
1780
Roland McGrath86976cd2008-01-30 13:31:01 +01001781 if (unlikely(current->audit_context))
1782 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1783
Josh Stone66700002009-08-24 14:43:11 -07001784 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001785 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001786
Roland McGrathd4d67152008-07-09 02:38:07 -07001787 /*
1788 * If TIF_SYSCALL_EMU is set, we only get here because of
1789 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1790 * We already reported this syscall instruction in
Oleg Nesterovd5196502009-12-15 16:47:21 -08001791 * syscall_trace_enter().
Roland McGrathd4d67152008-07-09 02:38:07 -07001792 */
Oleg Nesterovd5196502009-12-15 16:47:21 -08001793 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1794 !test_thread_flag(TIF_SYSCALL_EMU);
1795 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1796 tracehook_report_syscall_exit(regs, step);
Roland McGrathd4d67152008-07-09 02:38:07 -07001797}