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