]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/kernel/ptrace.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp
[net-next-2.6.git] / arch / x86 / kernel / ptrace.c
CommitLineData
1da177e4
LT
1/* By Ross Biro 1/23/92 */
2/*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
eee3af4a
MM
5 *
6 * BTS tracing
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
1da177e4
LT
8 */
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
1da177e4
LT
14#include <linux/errno.h>
15#include <linux/ptrace.h>
91e7b707 16#include <linux/regset.h>
eeea3c3f 17#include <linux/tracehook.h>
1da177e4 18#include <linux/user.h>
070459d9 19#include <linux/elf.h>
1da177e4
LT
20#include <linux/security.h>
21#include <linux/audit.h>
22#include <linux/seccomp.h>
7ed20e1a 23#include <linux/signal.h>
e2b371f0 24#include <linux/workqueue.h>
24f1e32c
FW
25#include <linux/perf_event.h>
26#include <linux/hw_breakpoint.h>
1da177e4
LT
27
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>
2047b08b
RM
36#include <asm/prctl.h>
37#include <asm/proto.h>
eee3af4a 38#include <asm/ds.h>
72f674d2 39#include <asm/hw_breakpoint.h>
eee3af4a 40
070459d9
RM
41#include "tls.h"
42
1c569f02
JS
43#define CREATE_TRACE_POINTS
44#include <trace/events/syscalls.h>
45
070459d9
RM
46enum x86_regset {
47 REGSET_GENERAL,
48 REGSET_FP,
49 REGSET_XFP,
325af5fb 50 REGSET_IOPERM64 = REGSET_XFP,
070459d9 51 REGSET_TLS,
325af5fb 52 REGSET_IOPERM32,
070459d9 53};
eee3af4a 54
b1cf540f
MH
55struct pt_regs_offset {
56 const char *name;
57 int offset;
58};
59
60#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61#define REG_OFFSET_END {.name = NULL, .offset = 0}
62
63static const struct pt_regs_offset regoffset_table[] = {
64#ifdef CONFIG_X86_64
65 REG_OFFSET_NAME(r15),
66 REG_OFFSET_NAME(r14),
67 REG_OFFSET_NAME(r13),
68 REG_OFFSET_NAME(r12),
69 REG_OFFSET_NAME(r11),
70 REG_OFFSET_NAME(r10),
71 REG_OFFSET_NAME(r9),
72 REG_OFFSET_NAME(r8),
73#endif
74 REG_OFFSET_NAME(bx),
75 REG_OFFSET_NAME(cx),
76 REG_OFFSET_NAME(dx),
77 REG_OFFSET_NAME(si),
78 REG_OFFSET_NAME(di),
79 REG_OFFSET_NAME(bp),
80 REG_OFFSET_NAME(ax),
81#ifdef CONFIG_X86_32
82 REG_OFFSET_NAME(ds),
83 REG_OFFSET_NAME(es),
84 REG_OFFSET_NAME(fs),
85 REG_OFFSET_NAME(gs),
86#endif
87 REG_OFFSET_NAME(orig_ax),
88 REG_OFFSET_NAME(ip),
89 REG_OFFSET_NAME(cs),
90 REG_OFFSET_NAME(flags),
91 REG_OFFSET_NAME(sp),
92 REG_OFFSET_NAME(ss),
93 REG_OFFSET_END,
94};
95
96/**
97 * regs_query_register_offset() - query register offset from its name
98 * @name: the name of a register
99 *
100 * regs_query_register_offset() returns the offset of a register in struct
101 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
102 */
103int regs_query_register_offset(const char *name)
104{
105 const struct pt_regs_offset *roff;
106 for (roff = regoffset_table; roff->name != NULL; roff++)
107 if (!strcmp(roff->name, name))
108 return roff->offset;
109 return -EINVAL;
110}
111
112/**
113 * regs_query_register_name() - query register name from its offset
114 * @offset: the offset of a register in struct pt_regs.
115 *
116 * regs_query_register_name() returns the name of a register from its
117 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
118 */
119const char *regs_query_register_name(unsigned int offset)
120{
121 const struct pt_regs_offset *roff;
122 for (roff = regoffset_table; roff->name != NULL; roff++)
123 if (roff->offset == offset)
124 return roff->name;
125 return NULL;
126}
127
128static const int arg_offs_table[] = {
129#ifdef CONFIG_X86_32
130 [0] = offsetof(struct pt_regs, ax),
131 [1] = offsetof(struct pt_regs, dx),
132 [2] = offsetof(struct pt_regs, cx)
133#else /* CONFIG_X86_64 */
134 [0] = offsetof(struct pt_regs, di),
135 [1] = offsetof(struct pt_regs, si),
136 [2] = offsetof(struct pt_regs, dx),
137 [3] = offsetof(struct pt_regs, cx),
138 [4] = offsetof(struct pt_regs, r8),
139 [5] = offsetof(struct pt_regs, r9)
140#endif
141};
142
143/**
144 * regs_get_argument_nth() - get Nth argument at function call
145 * @regs: pt_regs which contains registers at function entry.
146 * @n: argument number.
147 *
148 * regs_get_argument_nth() returns @n th argument of a function call.
149 * Since usually the kernel stack will be changed right after function entry,
150 * you must use this at function entry. If the @n th entry is NOT in the
151 * kernel stack or pt_regs, this returns 0.
152 */
153unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
154{
155 if (n < ARRAY_SIZE(arg_offs_table))
ad5cafcd 156 return *(unsigned long *)((char *)regs + arg_offs_table[n]);
b1cf540f
MH
157 else {
158 /*
159 * The typical case: arg n is on the stack.
160 * (Note: stack[0] = return address, so skip it)
161 */
162 n -= ARRAY_SIZE(arg_offs_table);
163 return regs_get_kernel_stack_nth(regs, 1 + n);
164 }
165}
166
1da177e4
LT
167/*
168 * does not yet catch signals sent when the child dies.
169 * in exit.c or in signal.c.
170 */
171
9f155b98
CE
172/*
173 * Determines which flags the user has access to [1 = access, 0 = no access].
9f155b98 174 */
e39c2891
RM
175#define FLAG_MASK_32 ((unsigned long) \
176 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
177 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
178 X86_EFLAGS_SF | X86_EFLAGS_TF | \
179 X86_EFLAGS_DF | X86_EFLAGS_OF | \
180 X86_EFLAGS_RF | X86_EFLAGS_AC))
181
2047b08b
RM
182/*
183 * Determines whether a value may be installed in a segment register.
184 */
185static inline bool invalid_selector(u16 value)
186{
187 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
188}
189
190#ifdef CONFIG_X86_32
191
e39c2891 192#define FLAG_MASK FLAG_MASK_32
1da177e4 193
4fe702c7 194static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
1da177e4 195{
65ea5b03 196 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
ccbeed3a 197 return &regs->bx + (regno >> 2);
1da177e4
LT
198}
199
06ee1b68 200static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
1da177e4 201{
06ee1b68
RM
202 /*
203 * Returning the value truncates it to 16 bits.
204 */
205 unsigned int retval;
206 if (offset != offsetof(struct user_regs_struct, gs))
207 retval = *pt_regs_access(task_pt_regs(task), offset);
208 else {
06ee1b68 209 if (task == current)
d9a89a26
TH
210 retval = get_user_gs(task_pt_regs(task));
211 else
212 retval = task_user_gs(task);
06ee1b68
RM
213 }
214 return retval;
215}
216
217static int set_segment_reg(struct task_struct *task,
218 unsigned long offset, u16 value)
219{
220 /*
221 * The value argument was already truncated to 16 bits.
222 */
2047b08b 223 if (invalid_selector(value))
06ee1b68
RM
224 return -EIO;
225
c63855d0
RM
226 /*
227 * For %cs and %ss we cannot permit a null selector.
228 * We can permit a bogus selector as long as it has USER_RPL.
229 * Null selectors are fine for other segment registers, but
230 * we will never get back to user mode with invalid %cs or %ss
231 * and will take the trap in iret instead. Much code relies
232 * on user_mode() to distinguish a user trap frame (which can
233 * safely use invalid selectors) from a kernel trap frame.
234 */
235 switch (offset) {
236 case offsetof(struct user_regs_struct, cs):
237 case offsetof(struct user_regs_struct, ss):
238 if (unlikely(value == 0))
239 return -EIO;
240
241 default:
06ee1b68 242 *pt_regs_access(task_pt_regs(task), offset) = value;
c63855d0
RM
243 break;
244
245 case offsetof(struct user_regs_struct, gs):
06ee1b68 246 if (task == current)
d9a89a26
TH
247 set_user_gs(task_pt_regs(task), value);
248 else
249 task_user_gs(task) = value;
1da177e4 250 }
06ee1b68 251
1da177e4
LT
252 return 0;
253}
254
2047b08b
RM
255#else /* CONFIG_X86_64 */
256
257#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
258
259static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
260{
261 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
262 return &regs->r15 + (offset / sizeof(regs->r15));
263}
264
265static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
266{
267 /*
268 * Returning the value truncates it to 16 bits.
269 */
270 unsigned int seg;
271
272 switch (offset) {
273 case offsetof(struct user_regs_struct, fs):
274 if (task == current) {
275 /* Older gas can't assemble movq %?s,%r?? */
276 asm("movl %%fs,%0" : "=r" (seg));
277 return seg;
278 }
279 return task->thread.fsindex;
280 case offsetof(struct user_regs_struct, gs):
281 if (task == current) {
282 asm("movl %%gs,%0" : "=r" (seg));
283 return seg;
284 }
285 return task->thread.gsindex;
286 case offsetof(struct user_regs_struct, ds):
287 if (task == current) {
288 asm("movl %%ds,%0" : "=r" (seg));
289 return seg;
290 }
291 return task->thread.ds;
292 case offsetof(struct user_regs_struct, es):
293 if (task == current) {
294 asm("movl %%es,%0" : "=r" (seg));
295 return seg;
296 }
297 return task->thread.es;
298
299 case offsetof(struct user_regs_struct, cs):
300 case offsetof(struct user_regs_struct, ss):
301 break;
302 }
303 return *pt_regs_access(task_pt_regs(task), offset);
304}
305
306static int set_segment_reg(struct task_struct *task,
307 unsigned long offset, u16 value)
308{
309 /*
310 * The value argument was already truncated to 16 bits.
311 */
312 if (invalid_selector(value))
313 return -EIO;
314
315 switch (offset) {
316 case offsetof(struct user_regs_struct,fs):
317 /*
318 * If this is setting fs as for normal 64-bit use but
319 * setting fs_base has implicitly changed it, leave it.
320 */
321 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
322 task->thread.fs != 0) ||
323 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
324 task->thread.fs == 0))
325 break;
326 task->thread.fsindex = value;
327 if (task == current)
328 loadsegment(fs, task->thread.fsindex);
329 break;
330 case offsetof(struct user_regs_struct,gs):
331 /*
332 * If this is setting gs as for normal 64-bit use but
333 * setting gs_base has implicitly changed it, leave it.
334 */
335 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
336 task->thread.gs != 0) ||
337 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
338 task->thread.gs == 0))
339 break;
340 task->thread.gsindex = value;
341 if (task == current)
342 load_gs_index(task->thread.gsindex);
343 break;
344 case offsetof(struct user_regs_struct,ds):
345 task->thread.ds = value;
346 if (task == current)
347 loadsegment(ds, task->thread.ds);
348 break;
349 case offsetof(struct user_regs_struct,es):
350 task->thread.es = value;
351 if (task == current)
352 loadsegment(es, task->thread.es);
353 break;
354
355 /*
356 * Can't actually change these in 64-bit mode.
357 */
358 case offsetof(struct user_regs_struct,cs):
c63855d0
RM
359 if (unlikely(value == 0))
360 return -EIO;
2047b08b
RM
361#ifdef CONFIG_IA32_EMULATION
362 if (test_tsk_thread_flag(task, TIF_IA32))
363 task_pt_regs(task)->cs = value;
2047b08b 364#endif
cb757c41 365 break;
2047b08b 366 case offsetof(struct user_regs_struct,ss):
c63855d0
RM
367 if (unlikely(value == 0))
368 return -EIO;
2047b08b
RM
369#ifdef CONFIG_IA32_EMULATION
370 if (test_tsk_thread_flag(task, TIF_IA32))
371 task_pt_regs(task)->ss = value;
2047b08b 372#endif
cb757c41 373 break;
2047b08b
RM
374 }
375
376 return 0;
377}
378
2047b08b
RM
379#endif /* CONFIG_X86_32 */
380
06ee1b68 381static unsigned long get_flags(struct task_struct *task)
1da177e4 382{
06ee1b68
RM
383 unsigned long retval = task_pt_regs(task)->flags;
384
385 /*
386 * If the debugger set TF, hide it from the readout.
387 */
388 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
389 retval &= ~X86_EFLAGS_TF;
1da177e4 390
1da177e4
LT
391 return retval;
392}
393
06ee1b68
RM
394static int set_flags(struct task_struct *task, unsigned long value)
395{
396 struct pt_regs *regs = task_pt_regs(task);
397
398 /*
399 * If the user value contains TF, mark that
400 * it was not "us" (the debugger) that set it.
401 * If not, make sure it stays set if we had.
402 */
403 if (value & X86_EFLAGS_TF)
404 clear_tsk_thread_flag(task, TIF_FORCED_TF);
405 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
406 value |= X86_EFLAGS_TF;
407
408 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
409
410 return 0;
411}
412
413static int putreg(struct task_struct *child,
414 unsigned long offset, unsigned long value)
415{
416 switch (offset) {
417 case offsetof(struct user_regs_struct, cs):
418 case offsetof(struct user_regs_struct, ds):
419 case offsetof(struct user_regs_struct, es):
420 case offsetof(struct user_regs_struct, fs):
421 case offsetof(struct user_regs_struct, gs):
422 case offsetof(struct user_regs_struct, ss):
423 return set_segment_reg(child, offset, value);
424
425 case offsetof(struct user_regs_struct, flags):
426 return set_flags(child, value);
2047b08b
RM
427
428#ifdef CONFIG_X86_64
429 case offsetof(struct user_regs_struct,fs_base):
430 if (value >= TASK_SIZE_OF(child))
431 return -EIO;
432 /*
433 * When changing the segment base, use do_arch_prctl
434 * to set either thread.fs or thread.fsindex and the
435 * corresponding GDT slot.
436 */
437 if (child->thread.fs != value)
438 return do_arch_prctl(child, ARCH_SET_FS, value);
439 return 0;
440 case offsetof(struct user_regs_struct,gs_base):
441 /*
442 * Exactly the same here as the %fs handling above.
443 */
444 if (value >= TASK_SIZE_OF(child))
445 return -EIO;
446 if (child->thread.gs != value)
447 return do_arch_prctl(child, ARCH_SET_GS, value);
448 return 0;
449#endif
06ee1b68
RM
450 }
451
452 *pt_regs_access(task_pt_regs(child), offset) = value;
453 return 0;
454}
455
456static unsigned long getreg(struct task_struct *task, unsigned long offset)
457{
458 switch (offset) {
459 case offsetof(struct user_regs_struct, cs):
460 case offsetof(struct user_regs_struct, ds):
461 case offsetof(struct user_regs_struct, es):
462 case offsetof(struct user_regs_struct, fs):
463 case offsetof(struct user_regs_struct, gs):
464 case offsetof(struct user_regs_struct, ss):
465 return get_segment_reg(task, offset);
466
467 case offsetof(struct user_regs_struct, flags):
468 return get_flags(task);
2047b08b
RM
469
470#ifdef CONFIG_X86_64
471 case offsetof(struct user_regs_struct, fs_base): {
472 /*
473 * do_arch_prctl may have used a GDT slot instead of
474 * the MSR. To userland, it appears the same either
475 * way, except the %fs segment selector might not be 0.
476 */
477 unsigned int seg = task->thread.fsindex;
478 if (task->thread.fs != 0)
479 return task->thread.fs;
480 if (task == current)
481 asm("movl %%fs,%0" : "=r" (seg));
482 if (seg != FS_TLS_SEL)
483 return 0;
484 return get_desc_base(&task->thread.tls_array[FS_TLS]);
485 }
486 case offsetof(struct user_regs_struct, gs_base): {
487 /*
488 * Exactly the same here as the %fs handling above.
489 */
490 unsigned int seg = task->thread.gsindex;
491 if (task->thread.gs != 0)
492 return task->thread.gs;
493 if (task == current)
494 asm("movl %%gs,%0" : "=r" (seg));
495 if (seg != GS_TLS_SEL)
496 return 0;
497 return get_desc_base(&task->thread.tls_array[GS_TLS]);
498 }
499#endif
06ee1b68
RM
500 }
501
502 return *pt_regs_access(task_pt_regs(task), offset);
503}
504
91e7b707
RM
505static int genregs_get(struct task_struct *target,
506 const struct user_regset *regset,
507 unsigned int pos, unsigned int count,
508 void *kbuf, void __user *ubuf)
509{
510 if (kbuf) {
511 unsigned long *k = kbuf;
04a1e62c 512 while (count >= sizeof(*k)) {
91e7b707
RM
513 *k++ = getreg(target, pos);
514 count -= sizeof(*k);
515 pos += sizeof(*k);
516 }
517 } else {
518 unsigned long __user *u = ubuf;
04a1e62c 519 while (count >= sizeof(*u)) {
91e7b707
RM
520 if (__put_user(getreg(target, pos), u++))
521 return -EFAULT;
522 count -= sizeof(*u);
523 pos += sizeof(*u);
524 }
525 }
526
527 return 0;
528}
529
530static int genregs_set(struct task_struct *target,
531 const struct user_regset *regset,
532 unsigned int pos, unsigned int count,
533 const void *kbuf, const void __user *ubuf)
534{
535 int ret = 0;
536 if (kbuf) {
537 const unsigned long *k = kbuf;
04a1e62c 538 while (count >= sizeof(*k) && !ret) {
91e7b707
RM
539 ret = putreg(target, pos, *k++);
540 count -= sizeof(*k);
541 pos += sizeof(*k);
542 }
543 } else {
544 const unsigned long __user *u = ubuf;
04a1e62c 545 while (count >= sizeof(*u) && !ret) {
91e7b707
RM
546 unsigned long word;
547 ret = __get_user(word, u++);
548 if (ret)
549 break;
550 ret = putreg(target, pos, word);
551 count -= sizeof(*u);
552 pos += sizeof(*u);
553 }
554 }
555 return ret;
556}
557
b326e956
FW
558static void ptrace_triggered(struct perf_event *bp, int nmi,
559 struct perf_sample_data *data,
560 struct pt_regs *regs)
d9771e8c 561{
0f534093 562 int i;
24f1e32c 563 struct thread_struct *thread = &(current->thread);
0f534093 564
72f674d2
P
565 /*
566 * Store in the virtual DR6 register the fact that the breakpoint
567 * was hit so the thread's debugger will see it.
568 */
24f1e32c
FW
569 for (i = 0; i < HBP_NUM; i++) {
570 if (thread->ptrace_bps[i] == bp)
72f674d2 571 break;
24f1e32c 572 }
d9771e8c 573
72f674d2
P
574 thread->debugreg6 |= (DR_TRAP0 << i);
575}
d9771e8c 576
d9771e8c 577/*
24f1e32c
FW
578 * Walk through every ptrace breakpoints for this thread and
579 * build the dr7 value on top of their attributes.
580 *
d9771e8c 581 */
24f1e32c 582static unsigned long ptrace_get_dr7(struct perf_event *bp[])
d9771e8c 583{
24f1e32c
FW
584 int i;
585 int dr7 = 0;
586 struct arch_hw_breakpoint *info;
587
588 for (i = 0; i < HBP_NUM; i++) {
589 if (bp[i] && !bp[i]->attr.disabled) {
590 info = counter_arch_bp(bp[i]);
591 dr7 |= encode_dr7(i, info->len, info->type);
592 }
0f534093 593 }
24f1e32c
FW
594
595 return dr7;
d9771e8c
RM
596}
597
44234adc 598static int
5fa10b28 599ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
1cedae72 600 struct task_struct *tsk, int disabled)
5fa10b28
FW
601{
602 int err;
603 int gen_len, gen_type;
b326e956 604 struct perf_event_attr attr;
5fa10b28
FW
605
606 /*
607 * We shoud have at least an inactive breakpoint at this
608 * slot. It means the user is writing dr7 without having
609 * written the address register first
610 */
611 if (!bp)
44234adc 612 return -EINVAL;
5fa10b28
FW
613
614 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
615 if (err)
44234adc 616 return err;
5fa10b28
FW
617
618 attr = bp->attr;
619 attr.bp_len = gen_len;
620 attr.bp_type = gen_type;
1cedae72 621 attr.disabled = disabled;
5fa10b28 622
2f0993e0 623 return modify_user_hw_breakpoint(bp, &attr);
5fa10b28
FW
624}
625
72f674d2
P
626/*
627 * Handle ptrace writes to debug register 7.
628 */
629static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
d9771e8c 630{
72f674d2 631 struct thread_struct *thread = &(tsk->thread);
24f1e32c 632 unsigned long old_dr7;
72f674d2
P
633 int i, orig_ret = 0, rc = 0;
634 int enabled, second_pass = 0;
635 unsigned len, type;
24f1e32c 636 struct perf_event *bp;
72f674d2
P
637
638 data &= ~DR_CONTROL_RESERVED;
24f1e32c 639 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
72f674d2
P
640restore:
641 /*
642 * Loop through all the hardware breakpoints, making the
643 * appropriate changes to each.
644 */
645 for (i = 0; i < HBP_NUM; i++) {
646 enabled = decode_dr7(data, i, &len, &type);
24f1e32c 647 bp = thread->ptrace_bps[i];
72f674d2
P
648
649 if (!enabled) {
650 if (bp) {
24f1e32c
FW
651 /*
652 * Don't unregister the breakpoints right-away,
72f674d2
P
653 * unless all register_user_hw_breakpoint()
654 * requests have succeeded. This prevents
655 * any window of opportunity for debug
656 * register grabbing by other users.
657 */
658 if (!second_pass)
659 continue;
1cedae72 660
44234adc 661 rc = ptrace_modify_breakpoint(bp, len, type,
1cedae72 662 tsk, 1);
44234adc 663 if (rc)
1cedae72 664 break;
72f674d2
P
665 }
666 continue;
667 }
0f534093 668
44234adc
FW
669 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
670 if (rc)
24f1e32c 671 break;
72f674d2
P
672 }
673 /*
674 * Make a second pass to free the remaining unused breakpoints
675 * or to restore the original breakpoints if an error occurred.
676 */
677 if (!second_pass) {
678 second_pass = 1;
679 if (rc < 0) {
680 orig_ret = rc;
681 data = old_dr7;
682 }
683 goto restore;
684 }
685 return ((orig_ret < 0) ? orig_ret : rc);
686}
0f534093 687
72f674d2
P
688/*
689 * Handle PTRACE_PEEKUSR calls for the debug register area.
690 */
9d22b536 691static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
72f674d2
P
692{
693 struct thread_struct *thread = &(tsk->thread);
694 unsigned long val = 0;
695
24f1e32c
FW
696 if (n < HBP_NUM) {
697 struct perf_event *bp;
698 bp = thread->ptrace_bps[n];
699 if (!bp)
700 return 0;
701 val = bp->hw.info.address;
702 } else if (n == 6) {
72f674d2 703 val = thread->debugreg6;
24f1e32c
FW
704 } else if (n == 7) {
705 val = ptrace_get_dr7(thread->ptrace_bps);
706 }
72f674d2
P
707 return val;
708}
0f534093 709
24f1e32c
FW
710static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
711 unsigned long addr)
712{
713 struct perf_event *bp;
714 struct thread_struct *t = &tsk->thread;
b326e956 715 struct perf_event_attr attr;
24f1e32c
FW
716
717 if (!t->ptrace_bps[nr]) {
b326e956 718 hw_breakpoint_init(&attr);
d9771e8c 719 /*
24f1e32c
FW
720 * Put stub len and type to register (reserve) an inactive but
721 * correct bp
d9771e8c 722 */
5fa10b28
FW
723 attr.bp_addr = addr;
724 attr.bp_len = HW_BREAKPOINT_LEN_1;
725 attr.bp_type = HW_BREAKPOINT_W;
726 attr.disabled = 1;
727
728 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
44234adc
FW
729
730 /*
731 * CHECKME: the previous code returned -EIO if the addr wasn't
732 * a valid task virtual addr. The new one will return -EINVAL in
733 * this case.
734 * -EINVAL may be what we want for in-kernel breakpoints users,
735 * but -EIO looks better for ptrace, since we refuse a register
736 * writing for the user. And anyway this is the previous
737 * behaviour.
738 */
739 if (IS_ERR(bp))
740 return PTR_ERR(bp);
741
742 t->ptrace_bps[nr] = bp;
24f1e32c 743 } else {
44234adc
FW
744 int err;
745
24f1e32c 746 bp = t->ptrace_bps[nr];
5fa10b28
FW
747
748 attr = bp->attr;
749 attr.bp_addr = addr;
44234adc
FW
750 err = modify_user_hw_breakpoint(bp, &attr);
751 if (err)
752 return err;
d9771e8c 753 }
24f1e32c 754
24f1e32c 755
d9771e8c
RM
756 return 0;
757}
758
72f674d2
P
759/*
760 * Handle PTRACE_POKEUSR calls for the debug register area.
761 */
762int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
763{
764 struct thread_struct *thread = &(tsk->thread);
765 int rc = 0;
766
767 /* There are no DR4 or DR5 registers */
768 if (n == 4 || n == 5)
769 return -EIO;
770
771 if (n == 6) {
24f1e32c 772 thread->debugreg6 = val;
72f674d2 773 goto ret_path;
d9771e8c 774 }
72f674d2 775 if (n < HBP_NUM) {
24f1e32c
FW
776 rc = ptrace_set_breakpoint_addr(tsk, n, val);
777 if (rc)
778 return rc;
72f674d2
P
779 }
780 /* All that's left is DR7 */
781 if (n == 7)
782 rc = ptrace_write_dr7(tsk, val);
d9771e8c 783
72f674d2
P
784ret_path:
785 return rc;
d9771e8c
RM
786}
787
325af5fb
RM
788/*
789 * These access the current or another (stopped) task's io permission
790 * bitmap for debugging or core dump.
791 */
792static int ioperm_active(struct task_struct *target,
793 const struct user_regset *regset)
794{
795 return target->thread.io_bitmap_max / regset->size;
796}
b4ef95de 797
325af5fb
RM
798static int ioperm_get(struct task_struct *target,
799 const struct user_regset *regset,
800 unsigned int pos, unsigned int count,
801 void *kbuf, void __user *ubuf)
eee3af4a 802{
325af5fb 803 if (!target->thread.io_bitmap_ptr)
eee3af4a
MM
804 return -ENXIO;
805
325af5fb
RM
806 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
807 target->thread.io_bitmap_ptr,
808 0, IO_BITMAP_BYTES);
809}
810
93fa7636 811#ifdef CONFIG_X86_PTRACE_BTS
e2b371f0
MM
812/*
813 * A branch trace store context.
814 *
815 * Contexts may only be installed by ptrace_bts_config() and only for
816 * ptraced tasks.
817 *
818 * Contexts are destroyed when the tracee is detached from the tracer.
819 * The actual destruction work requires interrupts enabled, so the
820 * work is deferred and will be scheduled during __ptrace_unlink().
821 *
822 * Contexts hold an additional task_struct reference on the traced
823 * task, as well as a reference on the tracer's mm.
824 *
825 * Ptrace already holds a task_struct for the duration of ptrace operations,
826 * but since destruction is deferred, it may be executed after both
827 * tracer and tracee exited.
828 */
829struct bts_context {
830 /* The branch trace handle. */
831 struct bts_tracer *tracer;
832
833 /* The buffer used to store the branch trace and its size. */
834 void *buffer;
835 unsigned int size;
836
837 /* The mm that paid for the above buffer. */
838 struct mm_struct *mm;
839
840 /* The task this context belongs to. */
841 struct task_struct *task;
842
843 /* The signal to send on a bts buffer overflow. */
844 unsigned int bts_ovfl_signal;
845
846 /* The work struct to destroy a context. */
847 struct work_struct work;
848};
849
1cb81b14 850static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
e2b371f0 851{
1cb81b14
MM
852 void *buffer = NULL;
853 int err = -ENOMEM;
e2b371f0 854
1cb81b14
MM
855 err = account_locked_memory(current->mm, current->signal->rlim, size);
856 if (err < 0)
857 return err;
858
859 buffer = kzalloc(size, GFP_KERNEL);
860 if (!buffer)
861 goto out_refund;
862
863 context->buffer = buffer;
864 context->size = size;
865 context->mm = get_task_mm(current);
866
867 return 0;
868
869 out_refund:
870 refund_locked_memory(current->mm, size);
871 return err;
e2b371f0
MM
872}
873
874static inline void free_bts_buffer(struct bts_context *context)
875{
876 if (!context->buffer)
877 return;
878
879 kfree(context->buffer);
880 context->buffer = NULL;
881
1cb81b14 882 refund_locked_memory(context->mm, context->size);
e2b371f0
MM
883 context->size = 0;
884
885 mmput(context->mm);
886 context->mm = NULL;
887}
888
889static void free_bts_context_work(struct work_struct *w)
890{
891 struct bts_context *context;
892
893 context = container_of(w, struct bts_context, work);
894
895 ds_release_bts(context->tracer);
896 put_task_struct(context->task);
897 free_bts_buffer(context);
898 kfree(context);
899}
900
901static inline void free_bts_context(struct bts_context *context)
902{
903 INIT_WORK(&context->work, free_bts_context_work);
904 schedule_work(&context->work);
905}
906
907static inline struct bts_context *alloc_bts_context(struct task_struct *task)
908{
909 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
910 if (context) {
911 context->task = task;
912 task->bts = context;
913
914 get_task_struct(task);
915 }
916
917 return context;
918}
919
93fa7636 920static int ptrace_bts_read_record(struct task_struct *child, size_t index,
eee3af4a
MM
921 struct bts_struct __user *out)
922{
e2b371f0 923 struct bts_context *context;
c2724775
MM
924 const struct bts_trace *trace;
925 struct bts_struct bts;
926 const unsigned char *at;
93fa7636 927 int error;
eee3af4a 928
e2b371f0
MM
929 context = child->bts;
930 if (!context)
931 return -ESRCH;
932
933 trace = ds_read_bts(context->tracer);
c2724775 934 if (!trace)
e2b371f0 935 return -ESRCH;
e4811f25 936
c2724775
MM
937 at = trace->ds.top - ((index + 1) * trace->ds.size);
938 if ((void *)at < trace->ds.begin)
939 at += (trace->ds.n * trace->ds.size);
93fa7636 940
c2724775
MM
941 if (!trace->read)
942 return -EOPNOTSUPP;
93fa7636 943
e2b371f0 944 error = trace->read(context->tracer, at, &bts);
93fa7636
MM
945 if (error < 0)
946 return error;
e4811f25 947
c2724775 948 if (copy_to_user(out, &bts, sizeof(bts)))
eee3af4a
MM
949 return -EFAULT;
950
c2724775 951 return sizeof(bts);
eee3af4a
MM
952}
953
a95d67f8 954static int ptrace_bts_drain(struct task_struct *child,
cba4b65d 955 long size,
a95d67f8
MM
956 struct bts_struct __user *out)
957{
e2b371f0 958 struct bts_context *context;
c2724775
MM
959 const struct bts_trace *trace;
960 const unsigned char *at;
961 int error, drained = 0;
eee3af4a 962
e2b371f0
MM
963 context = child->bts;
964 if (!context)
965 return -ESRCH;
966
967 trace = ds_read_bts(context->tracer);
c2724775 968 if (!trace)
e2b371f0 969 return -ESRCH;
a95d67f8 970
c2724775
MM
971 if (!trace->read)
972 return -EOPNOTSUPP;
973
974 if (size < (trace->ds.top - trace->ds.begin))
cba4b65d
MM
975 return -EIO;
976
c2724775
MM
977 for (at = trace->ds.begin; (void *)at < trace->ds.top;
978 out++, drained++, at += trace->ds.size) {
979 struct bts_struct bts;
a95d67f8 980
e2b371f0 981 error = trace->read(context->tracer, at, &bts);
c2724775
MM
982 if (error < 0)
983 return error;
a95d67f8 984
c2724775 985 if (copy_to_user(out, &bts, sizeof(bts)))
a95d67f8
MM
986 return -EFAULT;
987 }
988
c2724775
MM
989 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
990
e2b371f0 991 error = ds_reset_bts(context->tracer);
93fa7636
MM
992 if (error < 0)
993 return error;
a95d67f8 994
c2724775 995 return drained;
a95d67f8
MM
996}
997
998static int ptrace_bts_config(struct task_struct *child,
cba4b65d 999 long cfg_size,
a95d67f8
MM
1000 const struct ptrace_bts_config __user *ucfg)
1001{
e2b371f0 1002 struct bts_context *context;
a95d67f8 1003 struct ptrace_bts_config cfg;
c2724775 1004 unsigned int flags = 0;
a95d67f8 1005
cba4b65d 1006 if (cfg_size < sizeof(cfg))
c2724775 1007 return -EIO;
cba4b65d 1008
a95d67f8 1009 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
c2724775 1010 return -EFAULT;
6abb11ae 1011
e2b371f0
MM
1012 context = child->bts;
1013 if (!context)
1014 context = alloc_bts_context(child);
1015 if (!context)
1016 return -ENOMEM;
93fa7636 1017
c2724775
MM
1018 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1019 if (!cfg.signal)
1020 return -EINVAL;
ca0002a1 1021
5a8ac9d2 1022 return -EOPNOTSUPP;
e2b371f0 1023 context->bts_ovfl_signal = cfg.signal;
c2724775 1024 }
6abb11ae 1025
e2b371f0
MM
1026 ds_release_bts(context->tracer);
1027 context->tracer = NULL;
6abb11ae 1028
e2b371f0 1029 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
1cb81b14
MM
1030 int err;
1031
e2b371f0
MM
1032 free_bts_buffer(context);
1033 if (!cfg.size)
1034 return 0;
c5dee617 1035
1cb81b14
MM
1036 err = alloc_bts_buffer(context, cfg.size);
1037 if (err < 0)
1038 return err;
a95d67f8
MM
1039 }
1040
da35c371 1041 if (cfg.flags & PTRACE_BTS_O_TRACE)
c2724775 1042 flags |= BTS_USER;
eee3af4a 1043
da35c371 1044 if (cfg.flags & PTRACE_BTS_O_SCHED)
c2724775 1045 flags |= BTS_TIMESTAMPS;
eee3af4a 1046
de79f54f
MM
1047 context->tracer =
1048 ds_request_bts_task(child, context->buffer, context->size,
1049 NULL, (size_t)-1, flags);
e2b371f0
MM
1050 if (unlikely(IS_ERR(context->tracer))) {
1051 int error = PTR_ERR(context->tracer);
da35c371 1052
e2b371f0
MM
1053 free_bts_buffer(context);
1054 context->tracer = NULL;
c2724775
MM
1055 return error;
1056 }
da35c371 1057
c2724775 1058 return sizeof(cfg);
eee3af4a
MM
1059}
1060
a95d67f8 1061static int ptrace_bts_status(struct task_struct *child,
cba4b65d 1062 long cfg_size,
a95d67f8 1063 struct ptrace_bts_config __user *ucfg)
eee3af4a 1064{
e2b371f0 1065 struct bts_context *context;
c2724775 1066 const struct bts_trace *trace;
a95d67f8 1067 struct ptrace_bts_config cfg;
eee3af4a 1068
e2b371f0
MM
1069 context = child->bts;
1070 if (!context)
1071 return -ESRCH;
1072
cba4b65d
MM
1073 if (cfg_size < sizeof(cfg))
1074 return -EIO;
1075
e2b371f0 1076 trace = ds_read_bts(context->tracer);
c2724775 1077 if (!trace)
e2b371f0 1078 return -ESRCH;
eee3af4a 1079
93fa7636 1080 memset(&cfg, 0, sizeof(cfg));
e2b371f0
MM
1081 cfg.size = trace->ds.end - trace->ds.begin;
1082 cfg.signal = context->bts_ovfl_signal;
1083 cfg.bts_size = sizeof(struct bts_struct);
eee3af4a 1084
93fa7636
MM
1085 if (cfg.signal)
1086 cfg.flags |= PTRACE_BTS_O_SIGNAL;
eee3af4a 1087
c2724775 1088 if (trace->ds.flags & BTS_USER)
93fa7636
MM
1089 cfg.flags |= PTRACE_BTS_O_TRACE;
1090
c2724775 1091 if (trace->ds.flags & BTS_TIMESTAMPS)
93fa7636 1092 cfg.flags |= PTRACE_BTS_O_SCHED;
87e8407f 1093
a95d67f8
MM
1094 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1095 return -EFAULT;
eee3af4a 1096
a95d67f8 1097 return sizeof(cfg);
eee3af4a
MM
1098}
1099
c2724775 1100static int ptrace_bts_clear(struct task_struct *child)
d8d4f157 1101{
e2b371f0 1102 struct bts_context *context;
c2724775 1103 const struct bts_trace *trace;
d8d4f157 1104
e2b371f0
MM
1105 context = child->bts;
1106 if (!context)
1107 return -ESRCH;
1108
1109 trace = ds_read_bts(context->tracer);
c2724775 1110 if (!trace)
e2b371f0 1111 return -ESRCH;
d8d4f157 1112
c2724775 1113 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
d8d4f157 1114
e2b371f0 1115 return ds_reset_bts(context->tracer);
d8d4f157
AM
1116}
1117
c2724775 1118static int ptrace_bts_size(struct task_struct *child)
eee3af4a 1119{
e2b371f0 1120 struct bts_context *context;
c2724775 1121 const struct bts_trace *trace;
93fa7636 1122
e2b371f0
MM
1123 context = child->bts;
1124 if (!context)
1125 return -ESRCH;
1126
1127 trace = ds_read_bts(context->tracer);
c2724775 1128 if (!trace)
e2b371f0 1129 return -ESRCH;
93fa7636 1130
c2724775 1131 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
93fa7636 1132}
bf53de90 1133
e2b371f0
MM
1134/*
1135 * Called from __ptrace_unlink() after the child has been moved back
1136 * to its original parent.
1137 */
0f481406 1138void ptrace_bts_untrace(struct task_struct *child)
bf53de90
MM
1139{
1140 if (unlikely(child->bts)) {
e2b371f0 1141 free_bts_context(child->bts);
bf53de90 1142 child->bts = NULL;
bf53de90
MM
1143 }
1144}
93fa7636 1145#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 1146
1da177e4
LT
1147/*
1148 * Called by kernel/ptrace.c when detaching..
1149 *
1150 * Make sure the single step bit is not set.
1151 */
1152void ptrace_disable(struct task_struct *child)
9e714bed 1153{
7f232343 1154 user_disable_single_step(child);
e9c86c78 1155#ifdef TIF_SYSCALL_EMU
ab1c23c2 1156 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
e9c86c78 1157#endif
1da177e4
LT
1158}
1159
5a4646a4
RM
1160#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1161static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1162#endif
1163
481bed45 1164long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1da177e4 1165{
5a4646a4 1166 int ret;
1da177e4
LT
1167 unsigned long __user *datap = (unsigned long __user *)data;
1168
1da177e4 1169 switch (request) {
1da177e4
LT
1170 /* read the word at location addr in the USER area. */
1171 case PTRACE_PEEKUSR: {
1172 unsigned long tmp;
1173
1174 ret = -EIO;
e9c86c78
RM
1175 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1176 addr >= sizeof(struct user))
1da177e4
LT
1177 break;
1178
1179 tmp = 0; /* Default return condition */
e9c86c78 1180 if (addr < sizeof(struct user_regs_struct))
1da177e4 1181 tmp = getreg(child, addr);
e9c86c78
RM
1182 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1183 addr <= offsetof(struct user, u_debugreg[7])) {
1184 addr -= offsetof(struct user, u_debugreg[0]);
1185 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1da177e4
LT
1186 }
1187 ret = put_user(tmp, datap);
1188 break;
1189 }
1190
1da177e4
LT
1191 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1192 ret = -EIO;
e9c86c78
RM
1193 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1194 addr >= sizeof(struct user))
1da177e4
LT
1195 break;
1196
e9c86c78 1197 if (addr < sizeof(struct user_regs_struct))
1da177e4 1198 ret = putreg(child, addr, data);
e9c86c78
RM
1199 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1200 addr <= offsetof(struct user, u_debugreg[7])) {
1201 addr -= offsetof(struct user, u_debugreg[0]);
1202 ret = ptrace_set_debugreg(child,
1203 addr / sizeof(data), data);
1da177e4 1204 }
e9c86c78 1205 break;
1da177e4 1206
5a4646a4
RM
1207 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1208 return copy_regset_to_user(child,
1209 task_user_regset_view(current),
1210 REGSET_GENERAL,
1211 0, sizeof(struct user_regs_struct),
1212 datap);
1213
1214 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1215 return copy_regset_from_user(child,
1216 task_user_regset_view(current),
1217 REGSET_GENERAL,
1218 0, sizeof(struct user_regs_struct),
1219 datap);
1220
1221 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1222 return copy_regset_to_user(child,
1223 task_user_regset_view(current),
1224 REGSET_FP,
1225 0, sizeof(struct user_i387_struct),
1226 datap);
1227
1228 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1229 return copy_regset_from_user(child,
1230 task_user_regset_view(current),
1231 REGSET_FP,
1232 0, sizeof(struct user_i387_struct),
1233 datap);
1da177e4 1234
e9c86c78 1235#ifdef CONFIG_X86_32
5a4646a4
RM
1236 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1237 return copy_regset_to_user(child, &user_x86_32_view,
1238 REGSET_XFP,
1239 0, sizeof(struct user_fxsr_struct),
45fdc3a7 1240 datap) ? -EIO : 0;
5a4646a4
RM
1241
1242 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1243 return copy_regset_from_user(child, &user_x86_32_view,
1244 REGSET_XFP,
1245 0, sizeof(struct user_fxsr_struct),
45fdc3a7 1246 datap) ? -EIO : 0;
e9c86c78 1247#endif
1da177e4 1248
e9c86c78 1249#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1da177e4 1250 case PTRACE_GET_THREAD_AREA:
efd1ca52
RM
1251 if (addr < 0)
1252 return -EIO;
1253 ret = do_get_thread_area(child, addr,
1254 (struct user_desc __user *) data);
1da177e4
LT
1255 break;
1256
1257 case PTRACE_SET_THREAD_AREA:
efd1ca52
RM
1258 if (addr < 0)
1259 return -EIO;
1260 ret = do_set_thread_area(child, addr,
1261 (struct user_desc __user *) data, 0);
1da177e4 1262 break;
e9c86c78
RM
1263#endif
1264
1265#ifdef CONFIG_X86_64
1266 /* normal 64bit interface to access TLS data.
1267 Works just like arch_prctl, except that the arguments
1268 are reversed. */
1269 case PTRACE_ARCH_PRCTL:
1270 ret = do_arch_prctl(child, data, addr);
1271 break;
1272#endif
1da177e4 1273
b4ef95de
IM
1274 /*
1275 * These bits need more cooking - not enabled yet:
1276 */
93fa7636 1277#ifdef CONFIG_X86_PTRACE_BTS
a95d67f8
MM
1278 case PTRACE_BTS_CONFIG:
1279 ret = ptrace_bts_config
cba4b65d 1280 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
1281 break;
1282
a95d67f8
MM
1283 case PTRACE_BTS_STATUS:
1284 ret = ptrace_bts_status
cba4b65d 1285 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
1286 break;
1287
c2724775
MM
1288 case PTRACE_BTS_SIZE:
1289 ret = ptrace_bts_size(child);
eee3af4a
MM
1290 break;
1291
a95d67f8 1292 case PTRACE_BTS_GET:
eee3af4a 1293 ret = ptrace_bts_read_record
a95d67f8 1294 (child, data, (struct bts_struct __user *) addr);
eee3af4a
MM
1295 break;
1296
a95d67f8 1297 case PTRACE_BTS_CLEAR:
c2724775 1298 ret = ptrace_bts_clear(child);
eee3af4a
MM
1299 break;
1300
a95d67f8
MM
1301 case PTRACE_BTS_DRAIN:
1302 ret = ptrace_bts_drain
cba4b65d 1303 (child, data, (struct bts_struct __user *) addr);
eee3af4a 1304 break;
93fa7636 1305#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 1306
1da177e4
LT
1307 default:
1308 ret = ptrace_request(child, request, addr, data);
1309 break;
1310 }
d9771e8c 1311
1da177e4
LT
1312 return ret;
1313}
1314
cb757c41
RM
1315#ifdef CONFIG_IA32_EMULATION
1316
099cd6e9
RM
1317#include <linux/compat.h>
1318#include <linux/syscalls.h>
1319#include <asm/ia32.h>
cb757c41
RM
1320#include <asm/user32.h>
1321
1322#define R32(l,q) \
1323 case offsetof(struct user32, regs.l): \
1324 regs->q = value; break
1325
1326#define SEG32(rs) \
1327 case offsetof(struct user32, regs.rs): \
1328 return set_segment_reg(child, \
1329 offsetof(struct user_regs_struct, rs), \
1330 value); \
1331 break
1332
1333static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1334{
1335 struct pt_regs *regs = task_pt_regs(child);
1336
1337 switch (regno) {
1338
1339 SEG32(cs);
1340 SEG32(ds);
1341 SEG32(es);
1342 SEG32(fs);
1343 SEG32(gs);
1344 SEG32(ss);
1345
1346 R32(ebx, bx);
1347 R32(ecx, cx);
1348 R32(edx, dx);
1349 R32(edi, di);
1350 R32(esi, si);
1351 R32(ebp, bp);
1352 R32(eax, ax);
cb757c41
RM
1353 R32(eip, ip);
1354 R32(esp, sp);
1355
40f0933d
RM
1356 case offsetof(struct user32, regs.orig_eax):
1357 /*
8cb3ed13
RM
1358 * A 32-bit debugger setting orig_eax means to restore
1359 * the state of the task restarting a 32-bit syscall.
1360 * Make sure we interpret the -ERESTART* codes correctly
1361 * in case the task is not actually still sitting at the
1362 * exit from a 32-bit syscall with TS_COMPAT still set.
40f0933d 1363 */
8cb3ed13
RM
1364 regs->orig_ax = value;
1365 if (syscall_get_nr(child, regs) >= 0)
1366 task_thread_info(child)->status |= TS_COMPAT;
40f0933d
RM
1367 break;
1368
cb757c41
RM
1369 case offsetof(struct user32, regs.eflags):
1370 return set_flags(child, value);
1371
1372 case offsetof(struct user32, u_debugreg[0]) ...
1373 offsetof(struct user32, u_debugreg[7]):
1374 regno -= offsetof(struct user32, u_debugreg[0]);
1375 return ptrace_set_debugreg(child, regno / 4, value);
1376
1377 default:
1378 if (regno > sizeof(struct user32) || (regno & 3))
1379 return -EIO;
1380
1381 /*
1382 * Other dummy fields in the virtual user structure
1383 * are ignored
1384 */
1385 break;
1386 }
1387 return 0;
1388}
1389
1390#undef R32
1391#undef SEG32
1392
1393#define R32(l,q) \
1394 case offsetof(struct user32, regs.l): \
1395 *val = regs->q; break
1396
1397#define SEG32(rs) \
1398 case offsetof(struct user32, regs.rs): \
1399 *val = get_segment_reg(child, \
1400 offsetof(struct user_regs_struct, rs)); \
1401 break
1402
1403static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1404{
1405 struct pt_regs *regs = task_pt_regs(child);
1406
1407 switch (regno) {
1408
1409 SEG32(ds);
1410 SEG32(es);
1411 SEG32(fs);
1412 SEG32(gs);
1413
1414 R32(cs, cs);
1415 R32(ss, ss);
1416 R32(ebx, bx);
1417 R32(ecx, cx);
1418 R32(edx, dx);
1419 R32(edi, di);
1420 R32(esi, si);
1421 R32(ebp, bp);
1422 R32(eax, ax);
1423 R32(orig_eax, orig_ax);
1424 R32(eip, ip);
1425 R32(esp, sp);
1426
1427 case offsetof(struct user32, regs.eflags):
1428 *val = get_flags(child);
1429 break;
1430
1431 case offsetof(struct user32, u_debugreg[0]) ...
1432 offsetof(struct user32, u_debugreg[7]):
1433 regno -= offsetof(struct user32, u_debugreg[0]);
1434 *val = ptrace_get_debugreg(child, regno / 4);
1435 break;
1436
1437 default:
1438 if (regno > sizeof(struct user32) || (regno & 3))
1439 return -EIO;
1440
1441 /*
1442 * Other dummy fields in the virtual user structure
1443 * are ignored
1444 */
1445 *val = 0;
1446 break;
1447 }
1448 return 0;
1449}
1450
1451#undef R32
1452#undef SEG32
1453
91e7b707
RM
1454static int genregs32_get(struct task_struct *target,
1455 const struct user_regset *regset,
1456 unsigned int pos, unsigned int count,
1457 void *kbuf, void __user *ubuf)
1458{
1459 if (kbuf) {
1460 compat_ulong_t *k = kbuf;
04a1e62c 1461 while (count >= sizeof(*k)) {
91e7b707
RM
1462 getreg32(target, pos, k++);
1463 count -= sizeof(*k);
1464 pos += sizeof(*k);
1465 }
1466 } else {
1467 compat_ulong_t __user *u = ubuf;
04a1e62c 1468 while (count >= sizeof(*u)) {
91e7b707
RM
1469 compat_ulong_t word;
1470 getreg32(target, pos, &word);
1471 if (__put_user(word, u++))
1472 return -EFAULT;
1473 count -= sizeof(*u);
1474 pos += sizeof(*u);
1475 }
1476 }
1477
1478 return 0;
1479}
1480
1481static int genregs32_set(struct task_struct *target,
1482 const struct user_regset *regset,
1483 unsigned int pos, unsigned int count,
1484 const void *kbuf, const void __user *ubuf)
1485{
1486 int ret = 0;
1487 if (kbuf) {
1488 const compat_ulong_t *k = kbuf;
04a1e62c 1489 while (count >= sizeof(*k) && !ret) {
f9cb02b0 1490 ret = putreg32(target, pos, *k++);
91e7b707
RM
1491 count -= sizeof(*k);
1492 pos += sizeof(*k);
1493 }
1494 } else {
1495 const compat_ulong_t __user *u = ubuf;
04a1e62c 1496 while (count >= sizeof(*u) && !ret) {
91e7b707
RM
1497 compat_ulong_t word;
1498 ret = __get_user(word, u++);
1499 if (ret)
1500 break;
f9cb02b0 1501 ret = putreg32(target, pos, word);
91e7b707
RM
1502 count -= sizeof(*u);
1503 pos += sizeof(*u);
1504 }
1505 }
1506 return ret;
1507}
1508
562b80ba
RM
1509long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1510 compat_ulong_t caddr, compat_ulong_t cdata)
099cd6e9 1511{
562b80ba
RM
1512 unsigned long addr = caddr;
1513 unsigned long data = cdata;
099cd6e9
RM
1514 void __user *datap = compat_ptr(data);
1515 int ret;
1516 __u32 val;
1517
099cd6e9 1518 switch (request) {
099cd6e9
RM
1519 case PTRACE_PEEKUSR:
1520 ret = getreg32(child, addr, &val);
1521 if (ret == 0)
1522 ret = put_user(val, (__u32 __user *)datap);
1523 break;
1524
1525 case PTRACE_POKEUSR:
1526 ret = putreg32(child, addr, data);
1527 break;
1528
5a4646a4
RM
1529 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1530 return copy_regset_to_user(child, &user_x86_32_view,
1531 REGSET_GENERAL,
1532 0, sizeof(struct user_regs_struct32),
1533 datap);
1534
1535 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1536 return copy_regset_from_user(child, &user_x86_32_view,
1537 REGSET_GENERAL, 0,
1538 sizeof(struct user_regs_struct32),
1539 datap);
1540
1541 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1542 return copy_regset_to_user(child, &user_x86_32_view,
1543 REGSET_FP, 0,
1544 sizeof(struct user_i387_ia32_struct),
1545 datap);
1546
1547 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1548 return copy_regset_from_user(
1549 child, &user_x86_32_view, REGSET_FP,
1550 0, sizeof(struct user_i387_ia32_struct), datap);
1551
1552 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1553 return copy_regset_to_user(child, &user_x86_32_view,
1554 REGSET_XFP, 0,
1555 sizeof(struct user32_fxsr_struct),
1556 datap);
1557
1558 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1559 return copy_regset_from_user(child, &user_x86_32_view,
1560 REGSET_XFP, 0,
1561 sizeof(struct user32_fxsr_struct),
1562 datap);
099cd6e9 1563
562b80ba
RM
1564 case PTRACE_GET_THREAD_AREA:
1565 case PTRACE_SET_THREAD_AREA:
c2724775
MM
1566#ifdef CONFIG_X86_PTRACE_BTS
1567 case PTRACE_BTS_CONFIG:
1568 case PTRACE_BTS_STATUS:
1569 case PTRACE_BTS_SIZE:
1570 case PTRACE_BTS_GET:
1571 case PTRACE_BTS_CLEAR:
1572 case PTRACE_BTS_DRAIN:
1573#endif /* CONFIG_X86_PTRACE_BTS */
562b80ba
RM
1574 return arch_ptrace(child, request, addr, data);
1575
099cd6e9 1576 default:
fdadd54d 1577 return compat_ptrace_request(child, request, addr, data);
099cd6e9
RM
1578 }
1579
099cd6e9
RM
1580 return ret;
1581}
1582
cb757c41
RM
1583#endif /* CONFIG_IA32_EMULATION */
1584
070459d9
RM
1585#ifdef CONFIG_X86_64
1586
1587static const struct user_regset x86_64_regsets[] = {
1588 [REGSET_GENERAL] = {
1589 .core_note_type = NT_PRSTATUS,
1590 .n = sizeof(struct user_regs_struct) / sizeof(long),
1591 .size = sizeof(long), .align = sizeof(long),
1592 .get = genregs_get, .set = genregs_set
1593 },
1594 [REGSET_FP] = {
1595 .core_note_type = NT_PRFPREG,
1596 .n = sizeof(struct user_i387_struct) / sizeof(long),
1597 .size = sizeof(long), .align = sizeof(long),
1598 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1599 },
325af5fb
RM
1600 [REGSET_IOPERM64] = {
1601 .core_note_type = NT_386_IOPERM,
1602 .n = IO_BITMAP_LONGS,
1603 .size = sizeof(long), .align = sizeof(long),
1604 .active = ioperm_active, .get = ioperm_get
1605 },
070459d9
RM
1606};
1607
1608static const struct user_regset_view user_x86_64_view = {
1609 .name = "x86_64", .e_machine = EM_X86_64,
1610 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1611};
1612
1613#else /* CONFIG_X86_32 */
1614
1615#define user_regs_struct32 user_regs_struct
1616#define genregs32_get genregs_get
1617#define genregs32_set genregs_set
1618
1f465f4e
RM
1619#define user_i387_ia32_struct user_i387_struct
1620#define user32_fxsr_struct user_fxsr_struct
1621
070459d9
RM
1622#endif /* CONFIG_X86_64 */
1623
1624#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1625static const struct user_regset x86_32_regsets[] = {
1626 [REGSET_GENERAL] = {
1627 .core_note_type = NT_PRSTATUS,
1628 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1629 .size = sizeof(u32), .align = sizeof(u32),
1630 .get = genregs32_get, .set = genregs32_set
1631 },
1632 [REGSET_FP] = {
1633 .core_note_type = NT_PRFPREG,
1f465f4e 1634 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
070459d9
RM
1635 .size = sizeof(u32), .align = sizeof(u32),
1636 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1637 },
1638 [REGSET_XFP] = {
1639 .core_note_type = NT_PRXFPREG,
1f465f4e 1640 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
070459d9
RM
1641 .size = sizeof(u32), .align = sizeof(u32),
1642 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1643 },
1644 [REGSET_TLS] = {
bb61682b 1645 .core_note_type = NT_386_TLS,
070459d9
RM
1646 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1647 .size = sizeof(struct user_desc),
1648 .align = sizeof(struct user_desc),
1649 .active = regset_tls_active,
1650 .get = regset_tls_get, .set = regset_tls_set
1651 },
325af5fb
RM
1652 [REGSET_IOPERM32] = {
1653 .core_note_type = NT_386_IOPERM,
1654 .n = IO_BITMAP_BYTES / sizeof(u32),
1655 .size = sizeof(u32), .align = sizeof(u32),
1656 .active = ioperm_active, .get = ioperm_get
1657 },
070459d9
RM
1658};
1659
1660static const struct user_regset_view user_x86_32_view = {
1661 .name = "i386", .e_machine = EM_386,
1662 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1663};
1664#endif
1665
1666const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1667{
1668#ifdef CONFIG_IA32_EMULATION
1669 if (test_tsk_thread_flag(task, TIF_IA32))
1670#endif
1671#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1672 return &user_x86_32_view;
1673#endif
1674#ifdef CONFIG_X86_64
1675 return &user_x86_64_view;
1676#endif
1677}
1678
7f38551f
ON
1679static void fill_sigtrap_info(struct task_struct *tsk,
1680 struct pt_regs *regs,
1681 int error_code, int si_code,
1682 struct siginfo *info)
1da177e4 1683{
1da177e4
LT
1684 tsk->thread.trap_no = 1;
1685 tsk->thread.error_code = error_code;
1686
7f38551f
ON
1687 memset(info, 0, sizeof(*info));
1688 info->si_signo = SIGTRAP;
1689 info->si_code = si_code;
1690 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1691}
1692
1693void user_single_step_siginfo(struct task_struct *tsk,
1694 struct pt_regs *regs,
1695 struct siginfo *info)
1696{
1697 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1698}
1da177e4 1699
7f38551f
ON
1700void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1701 int error_code, int si_code)
1702{
1703 struct siginfo info;
1da177e4 1704
7f38551f 1705 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
27b46d76 1706 /* Send us the fake SIGTRAP */
1da177e4
LT
1707 force_sig_info(SIGTRAP, &info, tsk);
1708}
1709
86976cd8 1710
d4d67150
RM
1711#ifdef CONFIG_X86_32
1712# define IS_IA32 1
1713#elif defined CONFIG_IA32_EMULATION
ccbe495c 1714# define IS_IA32 is_compat_task()
d4d67150
RM
1715#else
1716# define IS_IA32 0
1717#endif
1718
1719/*
1720 * We must return the syscall number to actually look up in the table.
1721 * This can be -1L to skip running any syscall at all.
1722 */
1723asmregparm long syscall_trace_enter(struct pt_regs *regs)
86976cd8 1724{
d4d67150
RM
1725 long ret = 0;
1726
380fdd75
RM
1727 /*
1728 * If we stepped into a sysenter/syscall insn, it trapped in
1729 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1730 * If user-mode had set TF itself, then it's still clear from
1731 * do_debug() and we need to set it again to restore the user
1732 * state. If we entered on the slow path, TF was already set.
1733 */
1734 if (test_thread_flag(TIF_SINGLESTEP))
1735 regs->flags |= X86_EFLAGS_TF;
1736
86976cd8
RM
1737 /* do the secure computing check first */
1738 secure_computing(regs->orig_ax);
1739
d4d67150
RM
1740 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1741 ret = -1L;
1742
eeea3c3f
RM
1743 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1744 tracehook_report_syscall_entry(regs))
1745 ret = -1L;
86976cd8 1746
66700001 1747 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1c569f02 1748 trace_sys_enter(regs, regs->orig_ax);
1b3fa2ce 1749
86976cd8 1750 if (unlikely(current->audit_context)) {
d4d67150 1751 if (IS_IA32)
86976cd8
RM
1752 audit_syscall_entry(AUDIT_ARCH_I386,
1753 regs->orig_ax,
1754 regs->bx, regs->cx,
1755 regs->dx, regs->si);
d4d67150
RM
1756#ifdef CONFIG_X86_64
1757 else
86976cd8
RM
1758 audit_syscall_entry(AUDIT_ARCH_X86_64,
1759 regs->orig_ax,
1760 regs->di, regs->si,
1761 regs->dx, regs->r10);
d4d67150 1762#endif
86976cd8 1763 }
d4d67150
RM
1764
1765 return ret ?: regs->orig_ax;
86976cd8
RM
1766}
1767
d4d67150 1768asmregparm void syscall_trace_leave(struct pt_regs *regs)
86976cd8 1769{
d5196503
ON
1770 bool step;
1771
86976cd8
RM
1772 if (unlikely(current->audit_context))
1773 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1774
66700001 1775 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1c569f02 1776 trace_sys_exit(regs, regs->ax);
1b3fa2ce 1777
d4d67150
RM
1778 /*
1779 * If TIF_SYSCALL_EMU is set, we only get here because of
1780 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1781 * We already reported this syscall instruction in
d5196503 1782 * syscall_trace_enter().
d4d67150 1783 */
d5196503
ON
1784 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1785 !test_thread_flag(TIF_SYSCALL_EMU);
1786 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1787 tracehook_report_syscall_exit(regs, step);
d4d67150 1788}