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