hw-breakpoints: Improve in-kernel event creation error granularity
[deliverable/linux.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;
512 while (count > 0) {
513 *k++ = getreg(target, pos);
514 count -= sizeof(*k);
515 pos += sizeof(*k);
516 }
517 } else {
518 unsigned long __user *u = ubuf;
519 while (count > 0) {
520 if (__put_user(getreg(target, pos), u++))
521 return -EFAULT;
522 count -= sizeof(*u);
523 pos += sizeof(*u);
524 }
525 }
526
527 return 0;
528}
529
530static int genregs_set(struct task_struct *target,
531 const struct user_regset *regset,
532 unsigned int pos, unsigned int count,
533 const void *kbuf, const void __user *ubuf)
534{
535 int ret = 0;
536 if (kbuf) {
537 const unsigned long *k = kbuf;
538 while (count > 0 && !ret) {
539 ret = putreg(target, pos, *k++);
540 count -= sizeof(*k);
541 pos += sizeof(*k);
542 }
543 } else {
544 const unsigned long __user *u = ubuf;
545 while (count > 0 && !ret) {
546 unsigned long word;
547 ret = __get_user(word, u++);
548 if (ret)
549 break;
550 ret = putreg(target, pos, word);
551 count -= sizeof(*u);
552 pos += sizeof(*u);
553 }
554 }
555 return ret;
556}
557
24f1e32c 558static void ptrace_triggered(struct perf_event *bp, void *data)
d9771e8c 559{
0f534093 560 int i;
24f1e32c 561 struct thread_struct *thread = &(current->thread);
0f534093 562
72f674d2
P
563 /*
564 * Store in the virtual DR6 register the fact that the breakpoint
565 * was hit so the thread's debugger will see it.
566 */
24f1e32c
FW
567 for (i = 0; i < HBP_NUM; i++) {
568 if (thread->ptrace_bps[i] == bp)
72f674d2 569 break;
24f1e32c 570 }
d9771e8c 571
72f674d2
P
572 thread->debugreg6 |= (DR_TRAP0 << i);
573}
d9771e8c 574
d9771e8c 575/*
24f1e32c
FW
576 * Walk through every ptrace breakpoints for this thread and
577 * build the dr7 value on top of their attributes.
578 *
d9771e8c 579 */
24f1e32c 580static unsigned long ptrace_get_dr7(struct perf_event *bp[])
d9771e8c 581{
24f1e32c
FW
582 int i;
583 int dr7 = 0;
584 struct arch_hw_breakpoint *info;
585
586 for (i = 0; i < HBP_NUM; i++) {
587 if (bp[i] && !bp[i]->attr.disabled) {
588 info = counter_arch_bp(bp[i]);
589 dr7 |= encode_dr7(i, info->len, info->type);
590 }
0f534093 591 }
24f1e32c
FW
592
593 return dr7;
d9771e8c
RM
594}
595
72f674d2
P
596/*
597 * Handle ptrace writes to debug register 7.
598 */
599static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
d9771e8c 600{
72f674d2 601 struct thread_struct *thread = &(tsk->thread);
24f1e32c 602 unsigned long old_dr7;
72f674d2
P
603 int i, orig_ret = 0, rc = 0;
604 int enabled, second_pass = 0;
605 unsigned len, type;
24f1e32c
FW
606 int gen_len, gen_type;
607 struct perf_event *bp;
72f674d2
P
608
609 data &= ~DR_CONTROL_RESERVED;
24f1e32c 610 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
72f674d2
P
611restore:
612 /*
613 * Loop through all the hardware breakpoints, making the
614 * appropriate changes to each.
615 */
616 for (i = 0; i < HBP_NUM; i++) {
617 enabled = decode_dr7(data, i, &len, &type);
24f1e32c 618 bp = thread->ptrace_bps[i];
72f674d2
P
619
620 if (!enabled) {
621 if (bp) {
24f1e32c
FW
622 /*
623 * Don't unregister the breakpoints right-away,
72f674d2
P
624 * unless all register_user_hw_breakpoint()
625 * requests have succeeded. This prevents
626 * any window of opportunity for debug
627 * register grabbing by other users.
628 */
629 if (!second_pass)
630 continue;
24f1e32c
FW
631 thread->ptrace_bps[i] = NULL;
632 unregister_hw_breakpoint(bp);
72f674d2
P
633 }
634 continue;
635 }
0f534093 636
24f1e32c
FW
637 /*
638 * We shoud have at least an inactive breakpoint at this
639 * slot. It means the user is writing dr7 without having
640 * written the address register first
641 */
72f674d2 642 if (!bp) {
24f1e32c
FW
643 rc = -EINVAL;
644 break;
645 }
d9771e8c 646
24f1e32c 647 rc = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
72f674d2
P
648 if (rc)
649 break;
d9771e8c 650
24f1e32c
FW
651 /*
652 * This is a temporary thing as bp is unregistered/registered
653 * to simulate modification
654 */
655 bp = modify_user_hw_breakpoint(bp, bp->attr.bp_addr, gen_len,
656 gen_type, bp->callback,
657 tsk, true);
658 thread->ptrace_bps[i] = NULL;
0f534093 659
24f1e32c
FW
660 if (!bp) { /* incorrect bp, or we have a bug in bp API */
661 rc = -EINVAL;
662 break;
663 }
664 if (IS_ERR(bp)) {
665 rc = PTR_ERR(bp);
666 bp = NULL;
667 break;
668 }
669 thread->ptrace_bps[i] = bp;
72f674d2
P
670 }
671 /*
672 * Make a second pass to free the remaining unused breakpoints
673 * or to restore the original breakpoints if an error occurred.
674 */
675 if (!second_pass) {
676 second_pass = 1;
677 if (rc < 0) {
678 orig_ret = rc;
679 data = old_dr7;
680 }
681 goto restore;
682 }
683 return ((orig_ret < 0) ? orig_ret : rc);
684}
0f534093 685
72f674d2
P
686/*
687 * Handle PTRACE_PEEKUSR calls for the debug register area.
688 */
9d22b536 689static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
72f674d2
P
690{
691 struct thread_struct *thread = &(tsk->thread);
692 unsigned long val = 0;
693
24f1e32c
FW
694 if (n < HBP_NUM) {
695 struct perf_event *bp;
696 bp = thread->ptrace_bps[n];
697 if (!bp)
698 return 0;
699 val = bp->hw.info.address;
700 } else if (n == 6) {
72f674d2 701 val = thread->debugreg6;
24f1e32c
FW
702 } else if (n == 7) {
703 val = ptrace_get_dr7(thread->ptrace_bps);
704 }
72f674d2
P
705 return val;
706}
0f534093 707
24f1e32c
FW
708static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
709 unsigned long addr)
710{
711 struct perf_event *bp;
712 struct thread_struct *t = &tsk->thread;
713
714 if (!t->ptrace_bps[nr]) {
d9771e8c 715 /*
24f1e32c
FW
716 * Put stub len and type to register (reserve) an inactive but
717 * correct bp
d9771e8c 718 */
24f1e32c
FW
719 bp = register_user_hw_breakpoint(addr, HW_BREAKPOINT_LEN_1,
720 HW_BREAKPOINT_W,
721 ptrace_triggered, tsk,
722 false);
723 } else {
724 bp = t->ptrace_bps[nr];
725 t->ptrace_bps[nr] = NULL;
726 bp = modify_user_hw_breakpoint(bp, addr, bp->attr.bp_len,
727 bp->attr.bp_type,
728 bp->callback,
729 tsk,
730 bp->attr.disabled);
d9771e8c
RM
731 }
732
24f1e32c
FW
733 if (!bp)
734 return -EIO;
735 /*
736 * CHECKME: the previous code returned -EIO if the addr wasn't a
737 * valid task virtual addr. The new one will return -EINVAL in this
738 * case.
739 * -EINVAL may be what we want for in-kernel breakpoints users, but
740 * -EIO looks better for ptrace, since we refuse a register writing
741 * for the user. And anyway this is the previous behaviour.
742 */
743 if (IS_ERR(bp))
744 return PTR_ERR(bp);
745
746 t->ptrace_bps[nr] = bp;
747
d9771e8c
RM
748 return 0;
749}
750
72f674d2
P
751/*
752 * Handle PTRACE_POKEUSR calls for the debug register area.
753 */
754int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
755{
756 struct thread_struct *thread = &(tsk->thread);
757 int rc = 0;
758
759 /* There are no DR4 or DR5 registers */
760 if (n == 4 || n == 5)
761 return -EIO;
762
763 if (n == 6) {
24f1e32c 764 thread->debugreg6 = val;
72f674d2 765 goto ret_path;
d9771e8c 766 }
72f674d2 767 if (n < HBP_NUM) {
24f1e32c
FW
768 rc = ptrace_set_breakpoint_addr(tsk, n, val);
769 if (rc)
770 return rc;
72f674d2
P
771 }
772 /* All that's left is DR7 */
773 if (n == 7)
774 rc = ptrace_write_dr7(tsk, val);
d9771e8c 775
72f674d2
P
776ret_path:
777 return rc;
d9771e8c
RM
778}
779
325af5fb
RM
780/*
781 * These access the current or another (stopped) task's io permission
782 * bitmap for debugging or core dump.
783 */
784static int ioperm_active(struct task_struct *target,
785 const struct user_regset *regset)
786{
787 return target->thread.io_bitmap_max / regset->size;
788}
b4ef95de 789
325af5fb
RM
790static int ioperm_get(struct task_struct *target,
791 const struct user_regset *regset,
792 unsigned int pos, unsigned int count,
793 void *kbuf, void __user *ubuf)
eee3af4a 794{
325af5fb 795 if (!target->thread.io_bitmap_ptr)
eee3af4a
MM
796 return -ENXIO;
797
325af5fb
RM
798 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
799 target->thread.io_bitmap_ptr,
800 0, IO_BITMAP_BYTES);
801}
802
93fa7636 803#ifdef CONFIG_X86_PTRACE_BTS
e2b371f0
MM
804/*
805 * A branch trace store context.
806 *
807 * Contexts may only be installed by ptrace_bts_config() and only for
808 * ptraced tasks.
809 *
810 * Contexts are destroyed when the tracee is detached from the tracer.
811 * The actual destruction work requires interrupts enabled, so the
812 * work is deferred and will be scheduled during __ptrace_unlink().
813 *
814 * Contexts hold an additional task_struct reference on the traced
815 * task, as well as a reference on the tracer's mm.
816 *
817 * Ptrace already holds a task_struct for the duration of ptrace operations,
818 * but since destruction is deferred, it may be executed after both
819 * tracer and tracee exited.
820 */
821struct bts_context {
822 /* The branch trace handle. */
823 struct bts_tracer *tracer;
824
825 /* The buffer used to store the branch trace and its size. */
826 void *buffer;
827 unsigned int size;
828
829 /* The mm that paid for the above buffer. */
830 struct mm_struct *mm;
831
832 /* The task this context belongs to. */
833 struct task_struct *task;
834
835 /* The signal to send on a bts buffer overflow. */
836 unsigned int bts_ovfl_signal;
837
838 /* The work struct to destroy a context. */
839 struct work_struct work;
840};
841
1cb81b14 842static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
e2b371f0 843{
1cb81b14
MM
844 void *buffer = NULL;
845 int err = -ENOMEM;
e2b371f0 846
1cb81b14
MM
847 err = account_locked_memory(current->mm, current->signal->rlim, size);
848 if (err < 0)
849 return err;
850
851 buffer = kzalloc(size, GFP_KERNEL);
852 if (!buffer)
853 goto out_refund;
854
855 context->buffer = buffer;
856 context->size = size;
857 context->mm = get_task_mm(current);
858
859 return 0;
860
861 out_refund:
862 refund_locked_memory(current->mm, size);
863 return err;
e2b371f0
MM
864}
865
866static inline void free_bts_buffer(struct bts_context *context)
867{
868 if (!context->buffer)
869 return;
870
871 kfree(context->buffer);
872 context->buffer = NULL;
873
1cb81b14 874 refund_locked_memory(context->mm, context->size);
e2b371f0
MM
875 context->size = 0;
876
877 mmput(context->mm);
878 context->mm = NULL;
879}
880
881static void free_bts_context_work(struct work_struct *w)
882{
883 struct bts_context *context;
884
885 context = container_of(w, struct bts_context, work);
886
887 ds_release_bts(context->tracer);
888 put_task_struct(context->task);
889 free_bts_buffer(context);
890 kfree(context);
891}
892
893static inline void free_bts_context(struct bts_context *context)
894{
895 INIT_WORK(&context->work, free_bts_context_work);
896 schedule_work(&context->work);
897}
898
899static inline struct bts_context *alloc_bts_context(struct task_struct *task)
900{
901 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
902 if (context) {
903 context->task = task;
904 task->bts = context;
905
906 get_task_struct(task);
907 }
908
909 return context;
910}
911
93fa7636 912static int ptrace_bts_read_record(struct task_struct *child, size_t index,
eee3af4a
MM
913 struct bts_struct __user *out)
914{
e2b371f0 915 struct bts_context *context;
c2724775
MM
916 const struct bts_trace *trace;
917 struct bts_struct bts;
918 const unsigned char *at;
93fa7636 919 int error;
eee3af4a 920
e2b371f0
MM
921 context = child->bts;
922 if (!context)
923 return -ESRCH;
924
925 trace = ds_read_bts(context->tracer);
c2724775 926 if (!trace)
e2b371f0 927 return -ESRCH;
e4811f25 928
c2724775
MM
929 at = trace->ds.top - ((index + 1) * trace->ds.size);
930 if ((void *)at < trace->ds.begin)
931 at += (trace->ds.n * trace->ds.size);
93fa7636 932
c2724775
MM
933 if (!trace->read)
934 return -EOPNOTSUPP;
93fa7636 935
e2b371f0 936 error = trace->read(context->tracer, at, &bts);
93fa7636
MM
937 if (error < 0)
938 return error;
e4811f25 939
c2724775 940 if (copy_to_user(out, &bts, sizeof(bts)))
eee3af4a
MM
941 return -EFAULT;
942
c2724775 943 return sizeof(bts);
eee3af4a
MM
944}
945
a95d67f8 946static int ptrace_bts_drain(struct task_struct *child,
cba4b65d 947 long size,
a95d67f8
MM
948 struct bts_struct __user *out)
949{
e2b371f0 950 struct bts_context *context;
c2724775
MM
951 const struct bts_trace *trace;
952 const unsigned char *at;
953 int error, drained = 0;
eee3af4a 954
e2b371f0
MM
955 context = child->bts;
956 if (!context)
957 return -ESRCH;
958
959 trace = ds_read_bts(context->tracer);
c2724775 960 if (!trace)
e2b371f0 961 return -ESRCH;
a95d67f8 962
c2724775
MM
963 if (!trace->read)
964 return -EOPNOTSUPP;
965
966 if (size < (trace->ds.top - trace->ds.begin))
cba4b65d
MM
967 return -EIO;
968
c2724775
MM
969 for (at = trace->ds.begin; (void *)at < trace->ds.top;
970 out++, drained++, at += trace->ds.size) {
971 struct bts_struct bts;
a95d67f8 972
e2b371f0 973 error = trace->read(context->tracer, at, &bts);
c2724775
MM
974 if (error < 0)
975 return error;
a95d67f8 976
c2724775 977 if (copy_to_user(out, &bts, sizeof(bts)))
a95d67f8
MM
978 return -EFAULT;
979 }
980
c2724775
MM
981 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
982
e2b371f0 983 error = ds_reset_bts(context->tracer);
93fa7636
MM
984 if (error < 0)
985 return error;
a95d67f8 986
c2724775 987 return drained;
a95d67f8
MM
988}
989
990static int ptrace_bts_config(struct task_struct *child,
cba4b65d 991 long cfg_size,
a95d67f8
MM
992 const struct ptrace_bts_config __user *ucfg)
993{
e2b371f0 994 struct bts_context *context;
a95d67f8 995 struct ptrace_bts_config cfg;
c2724775 996 unsigned int flags = 0;
a95d67f8 997
cba4b65d 998 if (cfg_size < sizeof(cfg))
c2724775 999 return -EIO;
cba4b65d 1000
a95d67f8 1001 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
c2724775 1002 return -EFAULT;
6abb11ae 1003
e2b371f0
MM
1004 context = child->bts;
1005 if (!context)
1006 context = alloc_bts_context(child);
1007 if (!context)
1008 return -ENOMEM;
93fa7636 1009
c2724775
MM
1010 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1011 if (!cfg.signal)
1012 return -EINVAL;
ca0002a1 1013
5a8ac9d2 1014 return -EOPNOTSUPP;
e2b371f0 1015 context->bts_ovfl_signal = cfg.signal;
c2724775 1016 }
6abb11ae 1017
e2b371f0
MM
1018 ds_release_bts(context->tracer);
1019 context->tracer = NULL;
6abb11ae 1020
e2b371f0 1021 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
1cb81b14
MM
1022 int err;
1023
e2b371f0
MM
1024 free_bts_buffer(context);
1025 if (!cfg.size)
1026 return 0;
c5dee617 1027
1cb81b14
MM
1028 err = alloc_bts_buffer(context, cfg.size);
1029 if (err < 0)
1030 return err;
a95d67f8
MM
1031 }
1032
da35c371 1033 if (cfg.flags & PTRACE_BTS_O_TRACE)
c2724775 1034 flags |= BTS_USER;
eee3af4a 1035
da35c371 1036 if (cfg.flags & PTRACE_BTS_O_SCHED)
c2724775 1037 flags |= BTS_TIMESTAMPS;
eee3af4a 1038
de79f54f
MM
1039 context->tracer =
1040 ds_request_bts_task(child, context->buffer, context->size,
1041 NULL, (size_t)-1, flags);
e2b371f0
MM
1042 if (unlikely(IS_ERR(context->tracer))) {
1043 int error = PTR_ERR(context->tracer);
da35c371 1044
e2b371f0
MM
1045 free_bts_buffer(context);
1046 context->tracer = NULL;
c2724775
MM
1047 return error;
1048 }
da35c371 1049
c2724775 1050 return sizeof(cfg);
eee3af4a
MM
1051}
1052
a95d67f8 1053static int ptrace_bts_status(struct task_struct *child,
cba4b65d 1054 long cfg_size,
a95d67f8 1055 struct ptrace_bts_config __user *ucfg)
eee3af4a 1056{
e2b371f0 1057 struct bts_context *context;
c2724775 1058 const struct bts_trace *trace;
a95d67f8 1059 struct ptrace_bts_config cfg;
eee3af4a 1060
e2b371f0
MM
1061 context = child->bts;
1062 if (!context)
1063 return -ESRCH;
1064
cba4b65d
MM
1065 if (cfg_size < sizeof(cfg))
1066 return -EIO;
1067
e2b371f0 1068 trace = ds_read_bts(context->tracer);
c2724775 1069 if (!trace)
e2b371f0 1070 return -ESRCH;
eee3af4a 1071
93fa7636 1072 memset(&cfg, 0, sizeof(cfg));
e2b371f0
MM
1073 cfg.size = trace->ds.end - trace->ds.begin;
1074 cfg.signal = context->bts_ovfl_signal;
1075 cfg.bts_size = sizeof(struct bts_struct);
eee3af4a 1076
93fa7636
MM
1077 if (cfg.signal)
1078 cfg.flags |= PTRACE_BTS_O_SIGNAL;
eee3af4a 1079
c2724775 1080 if (trace->ds.flags & BTS_USER)
93fa7636
MM
1081 cfg.flags |= PTRACE_BTS_O_TRACE;
1082
c2724775 1083 if (trace->ds.flags & BTS_TIMESTAMPS)
93fa7636 1084 cfg.flags |= PTRACE_BTS_O_SCHED;
87e8407f 1085
a95d67f8
MM
1086 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1087 return -EFAULT;
eee3af4a 1088
a95d67f8 1089 return sizeof(cfg);
eee3af4a
MM
1090}
1091
c2724775 1092static int ptrace_bts_clear(struct task_struct *child)
d8d4f157 1093{
e2b371f0 1094 struct bts_context *context;
c2724775 1095 const struct bts_trace *trace;
d8d4f157 1096
e2b371f0
MM
1097 context = child->bts;
1098 if (!context)
1099 return -ESRCH;
1100
1101 trace = ds_read_bts(context->tracer);
c2724775 1102 if (!trace)
e2b371f0 1103 return -ESRCH;
d8d4f157 1104
c2724775 1105 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
d8d4f157 1106
e2b371f0 1107 return ds_reset_bts(context->tracer);
d8d4f157
AM
1108}
1109
c2724775 1110static int ptrace_bts_size(struct task_struct *child)
eee3af4a 1111{
e2b371f0 1112 struct bts_context *context;
c2724775 1113 const struct bts_trace *trace;
93fa7636 1114
e2b371f0
MM
1115 context = child->bts;
1116 if (!context)
1117 return -ESRCH;
1118
1119 trace = ds_read_bts(context->tracer);
c2724775 1120 if (!trace)
e2b371f0 1121 return -ESRCH;
93fa7636 1122
c2724775 1123 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
93fa7636 1124}
bf53de90 1125
e2b371f0
MM
1126/*
1127 * Called from __ptrace_unlink() after the child has been moved back
1128 * to its original parent.
1129 */
0f481406 1130void ptrace_bts_untrace(struct task_struct *child)
bf53de90
MM
1131{
1132 if (unlikely(child->bts)) {
e2b371f0 1133 free_bts_context(child->bts);
bf53de90 1134 child->bts = NULL;
bf53de90
MM
1135 }
1136}
93fa7636 1137#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 1138
1da177e4
LT
1139/*
1140 * Called by kernel/ptrace.c when detaching..
1141 *
1142 * Make sure the single step bit is not set.
1143 */
1144void ptrace_disable(struct task_struct *child)
9e714bed 1145{
7f232343 1146 user_disable_single_step(child);
e9c86c78 1147#ifdef TIF_SYSCALL_EMU
ab1c23c2 1148 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
e9c86c78 1149#endif
1da177e4
LT
1150}
1151
5a4646a4
RM
1152#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1153static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1154#endif
1155
481bed45 1156long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1da177e4 1157{
5a4646a4 1158 int ret;
1da177e4
LT
1159 unsigned long __user *datap = (unsigned long __user *)data;
1160
1da177e4 1161 switch (request) {
1da177e4
LT
1162 /* read the word at location addr in the USER area. */
1163 case PTRACE_PEEKUSR: {
1164 unsigned long tmp;
1165
1166 ret = -EIO;
e9c86c78
RM
1167 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1168 addr >= sizeof(struct user))
1da177e4
LT
1169 break;
1170
1171 tmp = 0; /* Default return condition */
e9c86c78 1172 if (addr < sizeof(struct user_regs_struct))
1da177e4 1173 tmp = getreg(child, addr);
e9c86c78
RM
1174 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1175 addr <= offsetof(struct user, u_debugreg[7])) {
1176 addr -= offsetof(struct user, u_debugreg[0]);
1177 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1da177e4
LT
1178 }
1179 ret = put_user(tmp, datap);
1180 break;
1181 }
1182
1da177e4
LT
1183 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1184 ret = -EIO;
e9c86c78
RM
1185 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1186 addr >= sizeof(struct user))
1da177e4
LT
1187 break;
1188
e9c86c78 1189 if (addr < sizeof(struct user_regs_struct))
1da177e4 1190 ret = putreg(child, addr, data);
e9c86c78
RM
1191 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1192 addr <= offsetof(struct user, u_debugreg[7])) {
1193 addr -= offsetof(struct user, u_debugreg[0]);
1194 ret = ptrace_set_debugreg(child,
1195 addr / sizeof(data), data);
1da177e4 1196 }
e9c86c78 1197 break;
1da177e4 1198
5a4646a4
RM
1199 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1200 return copy_regset_to_user(child,
1201 task_user_regset_view(current),
1202 REGSET_GENERAL,
1203 0, sizeof(struct user_regs_struct),
1204 datap);
1205
1206 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1207 return copy_regset_from_user(child,
1208 task_user_regset_view(current),
1209 REGSET_GENERAL,
1210 0, sizeof(struct user_regs_struct),
1211 datap);
1212
1213 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1214 return copy_regset_to_user(child,
1215 task_user_regset_view(current),
1216 REGSET_FP,
1217 0, sizeof(struct user_i387_struct),
1218 datap);
1219
1220 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1221 return copy_regset_from_user(child,
1222 task_user_regset_view(current),
1223 REGSET_FP,
1224 0, sizeof(struct user_i387_struct),
1225 datap);
1da177e4 1226
e9c86c78 1227#ifdef CONFIG_X86_32
5a4646a4
RM
1228 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1229 return copy_regset_to_user(child, &user_x86_32_view,
1230 REGSET_XFP,
1231 0, sizeof(struct user_fxsr_struct),
45fdc3a7 1232 datap) ? -EIO : 0;
5a4646a4
RM
1233
1234 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1235 return copy_regset_from_user(child, &user_x86_32_view,
1236 REGSET_XFP,
1237 0, sizeof(struct user_fxsr_struct),
45fdc3a7 1238 datap) ? -EIO : 0;
e9c86c78 1239#endif
1da177e4 1240
e9c86c78 1241#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1da177e4 1242 case PTRACE_GET_THREAD_AREA:
efd1ca52
RM
1243 if (addr < 0)
1244 return -EIO;
1245 ret = do_get_thread_area(child, addr,
1246 (struct user_desc __user *) data);
1da177e4
LT
1247 break;
1248
1249 case PTRACE_SET_THREAD_AREA:
efd1ca52
RM
1250 if (addr < 0)
1251 return -EIO;
1252 ret = do_set_thread_area(child, addr,
1253 (struct user_desc __user *) data, 0);
1da177e4 1254 break;
e9c86c78
RM
1255#endif
1256
1257#ifdef CONFIG_X86_64
1258 /* normal 64bit interface to access TLS data.
1259 Works just like arch_prctl, except that the arguments
1260 are reversed. */
1261 case PTRACE_ARCH_PRCTL:
1262 ret = do_arch_prctl(child, data, addr);
1263 break;
1264#endif
1da177e4 1265
b4ef95de
IM
1266 /*
1267 * These bits need more cooking - not enabled yet:
1268 */
93fa7636 1269#ifdef CONFIG_X86_PTRACE_BTS
a95d67f8
MM
1270 case PTRACE_BTS_CONFIG:
1271 ret = ptrace_bts_config
cba4b65d 1272 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
1273 break;
1274
a95d67f8
MM
1275 case PTRACE_BTS_STATUS:
1276 ret = ptrace_bts_status
cba4b65d 1277 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
1278 break;
1279
c2724775
MM
1280 case PTRACE_BTS_SIZE:
1281 ret = ptrace_bts_size(child);
eee3af4a
MM
1282 break;
1283
a95d67f8 1284 case PTRACE_BTS_GET:
eee3af4a 1285 ret = ptrace_bts_read_record
a95d67f8 1286 (child, data, (struct bts_struct __user *) addr);
eee3af4a
MM
1287 break;
1288
a95d67f8 1289 case PTRACE_BTS_CLEAR:
c2724775 1290 ret = ptrace_bts_clear(child);
eee3af4a
MM
1291 break;
1292
a95d67f8
MM
1293 case PTRACE_BTS_DRAIN:
1294 ret = ptrace_bts_drain
cba4b65d 1295 (child, data, (struct bts_struct __user *) addr);
eee3af4a 1296 break;
93fa7636 1297#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 1298
1da177e4
LT
1299 default:
1300 ret = ptrace_request(child, request, addr, data);
1301 break;
1302 }
d9771e8c 1303
1da177e4
LT
1304 return ret;
1305}
1306
cb757c41
RM
1307#ifdef CONFIG_IA32_EMULATION
1308
099cd6e9
RM
1309#include <linux/compat.h>
1310#include <linux/syscalls.h>
1311#include <asm/ia32.h>
cb757c41
RM
1312#include <asm/user32.h>
1313
1314#define R32(l,q) \
1315 case offsetof(struct user32, regs.l): \
1316 regs->q = value; break
1317
1318#define SEG32(rs) \
1319 case offsetof(struct user32, regs.rs): \
1320 return set_segment_reg(child, \
1321 offsetof(struct user_regs_struct, rs), \
1322 value); \
1323 break
1324
1325static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1326{
1327 struct pt_regs *regs = task_pt_regs(child);
1328
1329 switch (regno) {
1330
1331 SEG32(cs);
1332 SEG32(ds);
1333 SEG32(es);
1334 SEG32(fs);
1335 SEG32(gs);
1336 SEG32(ss);
1337
1338 R32(ebx, bx);
1339 R32(ecx, cx);
1340 R32(edx, dx);
1341 R32(edi, di);
1342 R32(esi, si);
1343 R32(ebp, bp);
1344 R32(eax, ax);
cb757c41
RM
1345 R32(eip, ip);
1346 R32(esp, sp);
1347
40f0933d
RM
1348 case offsetof(struct user32, regs.orig_eax):
1349 /*
8cb3ed13
RM
1350 * A 32-bit debugger setting orig_eax means to restore
1351 * the state of the task restarting a 32-bit syscall.
1352 * Make sure we interpret the -ERESTART* codes correctly
1353 * in case the task is not actually still sitting at the
1354 * exit from a 32-bit syscall with TS_COMPAT still set.
40f0933d 1355 */
8cb3ed13
RM
1356 regs->orig_ax = value;
1357 if (syscall_get_nr(child, regs) >= 0)
1358 task_thread_info(child)->status |= TS_COMPAT;
40f0933d
RM
1359 break;
1360
cb757c41
RM
1361 case offsetof(struct user32, regs.eflags):
1362 return set_flags(child, value);
1363
1364 case offsetof(struct user32, u_debugreg[0]) ...
1365 offsetof(struct user32, u_debugreg[7]):
1366 regno -= offsetof(struct user32, u_debugreg[0]);
1367 return ptrace_set_debugreg(child, regno / 4, value);
1368
1369 default:
1370 if (regno > sizeof(struct user32) || (regno & 3))
1371 return -EIO;
1372
1373 /*
1374 * Other dummy fields in the virtual user structure
1375 * are ignored
1376 */
1377 break;
1378 }
1379 return 0;
1380}
1381
1382#undef R32
1383#undef SEG32
1384
1385#define R32(l,q) \
1386 case offsetof(struct user32, regs.l): \
1387 *val = regs->q; break
1388
1389#define SEG32(rs) \
1390 case offsetof(struct user32, regs.rs): \
1391 *val = get_segment_reg(child, \
1392 offsetof(struct user_regs_struct, rs)); \
1393 break
1394
1395static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1396{
1397 struct pt_regs *regs = task_pt_regs(child);
1398
1399 switch (regno) {
1400
1401 SEG32(ds);
1402 SEG32(es);
1403 SEG32(fs);
1404 SEG32(gs);
1405
1406 R32(cs, cs);
1407 R32(ss, ss);
1408 R32(ebx, bx);
1409 R32(ecx, cx);
1410 R32(edx, dx);
1411 R32(edi, di);
1412 R32(esi, si);
1413 R32(ebp, bp);
1414 R32(eax, ax);
1415 R32(orig_eax, orig_ax);
1416 R32(eip, ip);
1417 R32(esp, sp);
1418
1419 case offsetof(struct user32, regs.eflags):
1420 *val = get_flags(child);
1421 break;
1422
1423 case offsetof(struct user32, u_debugreg[0]) ...
1424 offsetof(struct user32, u_debugreg[7]):
1425 regno -= offsetof(struct user32, u_debugreg[0]);
1426 *val = ptrace_get_debugreg(child, regno / 4);
1427 break;
1428
1429 default:
1430 if (regno > sizeof(struct user32) || (regno & 3))
1431 return -EIO;
1432
1433 /*
1434 * Other dummy fields in the virtual user structure
1435 * are ignored
1436 */
1437 *val = 0;
1438 break;
1439 }
1440 return 0;
1441}
1442
1443#undef R32
1444#undef SEG32
1445
91e7b707
RM
1446static int genregs32_get(struct task_struct *target,
1447 const struct user_regset *regset,
1448 unsigned int pos, unsigned int count,
1449 void *kbuf, void __user *ubuf)
1450{
1451 if (kbuf) {
1452 compat_ulong_t *k = kbuf;
1453 while (count > 0) {
1454 getreg32(target, pos, k++);
1455 count -= sizeof(*k);
1456 pos += sizeof(*k);
1457 }
1458 } else {
1459 compat_ulong_t __user *u = ubuf;
1460 while (count > 0) {
1461 compat_ulong_t word;
1462 getreg32(target, pos, &word);
1463 if (__put_user(word, u++))
1464 return -EFAULT;
1465 count -= sizeof(*u);
1466 pos += sizeof(*u);
1467 }
1468 }
1469
1470 return 0;
1471}
1472
1473static int genregs32_set(struct task_struct *target,
1474 const struct user_regset *regset,
1475 unsigned int pos, unsigned int count,
1476 const void *kbuf, const void __user *ubuf)
1477{
1478 int ret = 0;
1479 if (kbuf) {
1480 const compat_ulong_t *k = kbuf;
1481 while (count > 0 && !ret) {
f9cb02b0 1482 ret = putreg32(target, pos, *k++);
91e7b707
RM
1483 count -= sizeof(*k);
1484 pos += sizeof(*k);
1485 }
1486 } else {
1487 const compat_ulong_t __user *u = ubuf;
1488 while (count > 0 && !ret) {
1489 compat_ulong_t word;
1490 ret = __get_user(word, u++);
1491 if (ret)
1492 break;
f9cb02b0 1493 ret = putreg32(target, pos, word);
91e7b707
RM
1494 count -= sizeof(*u);
1495 pos += sizeof(*u);
1496 }
1497 }
1498 return ret;
1499}
1500
562b80ba
RM
1501long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1502 compat_ulong_t caddr, compat_ulong_t cdata)
099cd6e9 1503{
562b80ba
RM
1504 unsigned long addr = caddr;
1505 unsigned long data = cdata;
099cd6e9
RM
1506 void __user *datap = compat_ptr(data);
1507 int ret;
1508 __u32 val;
1509
099cd6e9 1510 switch (request) {
099cd6e9
RM
1511 case PTRACE_PEEKUSR:
1512 ret = getreg32(child, addr, &val);
1513 if (ret == 0)
1514 ret = put_user(val, (__u32 __user *)datap);
1515 break;
1516
1517 case PTRACE_POKEUSR:
1518 ret = putreg32(child, addr, data);
1519 break;
1520
5a4646a4
RM
1521 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1522 return copy_regset_to_user(child, &user_x86_32_view,
1523 REGSET_GENERAL,
1524 0, sizeof(struct user_regs_struct32),
1525 datap);
1526
1527 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1528 return copy_regset_from_user(child, &user_x86_32_view,
1529 REGSET_GENERAL, 0,
1530 sizeof(struct user_regs_struct32),
1531 datap);
1532
1533 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1534 return copy_regset_to_user(child, &user_x86_32_view,
1535 REGSET_FP, 0,
1536 sizeof(struct user_i387_ia32_struct),
1537 datap);
1538
1539 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1540 return copy_regset_from_user(
1541 child, &user_x86_32_view, REGSET_FP,
1542 0, sizeof(struct user_i387_ia32_struct), datap);
1543
1544 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1545 return copy_regset_to_user(child, &user_x86_32_view,
1546 REGSET_XFP, 0,
1547 sizeof(struct user32_fxsr_struct),
1548 datap);
1549
1550 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1551 return copy_regset_from_user(child, &user_x86_32_view,
1552 REGSET_XFP, 0,
1553 sizeof(struct user32_fxsr_struct),
1554 datap);
099cd6e9 1555
562b80ba
RM
1556 case PTRACE_GET_THREAD_AREA:
1557 case PTRACE_SET_THREAD_AREA:
c2724775
MM
1558#ifdef CONFIG_X86_PTRACE_BTS
1559 case PTRACE_BTS_CONFIG:
1560 case PTRACE_BTS_STATUS:
1561 case PTRACE_BTS_SIZE:
1562 case PTRACE_BTS_GET:
1563 case PTRACE_BTS_CLEAR:
1564 case PTRACE_BTS_DRAIN:
1565#endif /* CONFIG_X86_PTRACE_BTS */
562b80ba
RM
1566 return arch_ptrace(child, request, addr, data);
1567
099cd6e9 1568 default:
fdadd54d 1569 return compat_ptrace_request(child, request, addr, data);
099cd6e9
RM
1570 }
1571
099cd6e9
RM
1572 return ret;
1573}
1574
cb757c41
RM
1575#endif /* CONFIG_IA32_EMULATION */
1576
070459d9
RM
1577#ifdef CONFIG_X86_64
1578
1579static const struct user_regset x86_64_regsets[] = {
1580 [REGSET_GENERAL] = {
1581 .core_note_type = NT_PRSTATUS,
1582 .n = sizeof(struct user_regs_struct) / sizeof(long),
1583 .size = sizeof(long), .align = sizeof(long),
1584 .get = genregs_get, .set = genregs_set
1585 },
1586 [REGSET_FP] = {
1587 .core_note_type = NT_PRFPREG,
1588 .n = sizeof(struct user_i387_struct) / sizeof(long),
1589 .size = sizeof(long), .align = sizeof(long),
1590 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1591 },
325af5fb
RM
1592 [REGSET_IOPERM64] = {
1593 .core_note_type = NT_386_IOPERM,
1594 .n = IO_BITMAP_LONGS,
1595 .size = sizeof(long), .align = sizeof(long),
1596 .active = ioperm_active, .get = ioperm_get
1597 },
070459d9
RM
1598};
1599
1600static const struct user_regset_view user_x86_64_view = {
1601 .name = "x86_64", .e_machine = EM_X86_64,
1602 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1603};
1604
1605#else /* CONFIG_X86_32 */
1606
1607#define user_regs_struct32 user_regs_struct
1608#define genregs32_get genregs_get
1609#define genregs32_set genregs_set
1610
1f465f4e
RM
1611#define user_i387_ia32_struct user_i387_struct
1612#define user32_fxsr_struct user_fxsr_struct
1613
070459d9
RM
1614#endif /* CONFIG_X86_64 */
1615
1616#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1617static const struct user_regset x86_32_regsets[] = {
1618 [REGSET_GENERAL] = {
1619 .core_note_type = NT_PRSTATUS,
1620 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1621 .size = sizeof(u32), .align = sizeof(u32),
1622 .get = genregs32_get, .set = genregs32_set
1623 },
1624 [REGSET_FP] = {
1625 .core_note_type = NT_PRFPREG,
1f465f4e 1626 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
070459d9
RM
1627 .size = sizeof(u32), .align = sizeof(u32),
1628 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1629 },
1630 [REGSET_XFP] = {
1631 .core_note_type = NT_PRXFPREG,
1f465f4e 1632 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
070459d9
RM
1633 .size = sizeof(u32), .align = sizeof(u32),
1634 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1635 },
1636 [REGSET_TLS] = {
bb61682b 1637 .core_note_type = NT_386_TLS,
070459d9
RM
1638 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1639 .size = sizeof(struct user_desc),
1640 .align = sizeof(struct user_desc),
1641 .active = regset_tls_active,
1642 .get = regset_tls_get, .set = regset_tls_set
1643 },
325af5fb
RM
1644 [REGSET_IOPERM32] = {
1645 .core_note_type = NT_386_IOPERM,
1646 .n = IO_BITMAP_BYTES / sizeof(u32),
1647 .size = sizeof(u32), .align = sizeof(u32),
1648 .active = ioperm_active, .get = ioperm_get
1649 },
070459d9
RM
1650};
1651
1652static const struct user_regset_view user_x86_32_view = {
1653 .name = "i386", .e_machine = EM_386,
1654 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1655};
1656#endif
1657
1658const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1659{
1660#ifdef CONFIG_IA32_EMULATION
1661 if (test_tsk_thread_flag(task, TIF_IA32))
1662#endif
1663#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1664 return &user_x86_32_view;
1665#endif
1666#ifdef CONFIG_X86_64
1667 return &user_x86_64_view;
1668#endif
1669}
1670
da654b74
SD
1671void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1672 int error_code, int si_code)
1da177e4
LT
1673{
1674 struct siginfo info;
1675
1676 tsk->thread.trap_no = 1;
1677 tsk->thread.error_code = error_code;
1678
1679 memset(&info, 0, sizeof(info));
1680 info.si_signo = SIGTRAP;
da654b74 1681 info.si_code = si_code;
1da177e4 1682
65ea5b03
PA
1683 /* User-mode ip? */
1684 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1da177e4 1685
27b46d76 1686 /* Send us the fake SIGTRAP */
1da177e4
LT
1687 force_sig_info(SIGTRAP, &info, tsk);
1688}
1689
86976cd8 1690
d4d67150
RM
1691#ifdef CONFIG_X86_32
1692# define IS_IA32 1
1693#elif defined CONFIG_IA32_EMULATION
ccbe495c 1694# define IS_IA32 is_compat_task()
d4d67150
RM
1695#else
1696# define IS_IA32 0
1697#endif
1698
1699/*
1700 * We must return the syscall number to actually look up in the table.
1701 * This can be -1L to skip running any syscall at all.
1702 */
1703asmregparm long syscall_trace_enter(struct pt_regs *regs)
86976cd8 1704{
d4d67150
RM
1705 long ret = 0;
1706
380fdd75
RM
1707 /*
1708 * If we stepped into a sysenter/syscall insn, it trapped in
1709 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1710 * If user-mode had set TF itself, then it's still clear from
1711 * do_debug() and we need to set it again to restore the user
1712 * state. If we entered on the slow path, TF was already set.
1713 */
1714 if (test_thread_flag(TIF_SINGLESTEP))
1715 regs->flags |= X86_EFLAGS_TF;
1716
86976cd8
RM
1717 /* do the secure computing check first */
1718 secure_computing(regs->orig_ax);
1719
d4d67150
RM
1720 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1721 ret = -1L;
1722
eeea3c3f
RM
1723 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1724 tracehook_report_syscall_entry(regs))
1725 ret = -1L;
86976cd8 1726
66700001 1727 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1c569f02 1728 trace_sys_enter(regs, regs->orig_ax);
1b3fa2ce 1729
86976cd8 1730 if (unlikely(current->audit_context)) {
d4d67150 1731 if (IS_IA32)
86976cd8
RM
1732 audit_syscall_entry(AUDIT_ARCH_I386,
1733 regs->orig_ax,
1734 regs->bx, regs->cx,
1735 regs->dx, regs->si);
d4d67150
RM
1736#ifdef CONFIG_X86_64
1737 else
86976cd8
RM
1738 audit_syscall_entry(AUDIT_ARCH_X86_64,
1739 regs->orig_ax,
1740 regs->di, regs->si,
1741 regs->dx, regs->r10);
d4d67150 1742#endif
86976cd8 1743 }
d4d67150
RM
1744
1745 return ret ?: regs->orig_ax;
86976cd8
RM
1746}
1747
d4d67150 1748asmregparm void syscall_trace_leave(struct pt_regs *regs)
86976cd8
RM
1749{
1750 if (unlikely(current->audit_context))
1751 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1752
66700001 1753 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1c569f02 1754 trace_sys_exit(regs, regs->ax);
1b3fa2ce 1755
d4d67150 1756 if (test_thread_flag(TIF_SYSCALL_TRACE))
eeea3c3f 1757 tracehook_report_syscall_exit(regs, 0);
86976cd8 1758
d4d67150
RM
1759 /*
1760 * If TIF_SYSCALL_EMU is set, we only get here because of
1761 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1762 * We already reported this syscall instruction in
1763 * syscall_trace_enter(), so don't do any more now.
1764 */
1765 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1766 return;
1767
1768 /*
1769 * If we are single-stepping, synthesize a trap to follow the
1770 * system call instruction.
1771 */
1772 if (test_thread_flag(TIF_SINGLESTEP) &&
43918f2b 1773 tracehook_consider_fatal_signal(current, SIGTRAP))
da654b74 1774 send_sigtrap(current, regs, 0, TRAP_BRKPT);
d4d67150 1775}
This page took 0.622665 seconds and 5 git commands to generate.