]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/kernel/ptrace.c
x86, ptrace: change BTS GET ptrace interface
[net-next-2.6.git] / arch / x86 / kernel / ptrace.c
CommitLineData
1da177e4
LT
1/* By Ross Biro 1/23/92 */
2/*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
eee3af4a
MM
5 *
6 * BTS tracing
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
1da177e4
LT
8 */
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
1da177e4
LT
14#include <linux/errno.h>
15#include <linux/ptrace.h>
16#include <linux/user.h>
17#include <linux/security.h>
18#include <linux/audit.h>
19#include <linux/seccomp.h>
7ed20e1a 20#include <linux/signal.h>
1da177e4
LT
21
22#include <asm/uaccess.h>
23#include <asm/pgtable.h>
24#include <asm/system.h>
25#include <asm/processor.h>
26#include <asm/i387.h>
27#include <asm/debugreg.h>
28#include <asm/ldt.h>
29#include <asm/desc.h>
2047b08b
RM
30#include <asm/prctl.h>
31#include <asm/proto.h>
eee3af4a
MM
32#include <asm/ds.h>
33
34
35/*
36 * The maximal size of a BTS buffer per traced task in number of BTS
37 * records.
38 */
39#define PTRACE_BTS_BUFFER_MAX 4000
1da177e4
LT
40
41/*
42 * does not yet catch signals sent when the child dies.
43 * in exit.c or in signal.c.
44 */
45
9f155b98
CE
46/*
47 * Determines which flags the user has access to [1 = access, 0 = no access].
9f155b98 48 */
e39c2891
RM
49#define FLAG_MASK_32 ((unsigned long) \
50 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
51 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
52 X86_EFLAGS_SF | X86_EFLAGS_TF | \
53 X86_EFLAGS_DF | X86_EFLAGS_OF | \
54 X86_EFLAGS_RF | X86_EFLAGS_AC))
55
2047b08b
RM
56/*
57 * Determines whether a value may be installed in a segment register.
58 */
59static inline bool invalid_selector(u16 value)
60{
61 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
62}
63
64#ifdef CONFIG_X86_32
65
e39c2891 66#define FLAG_MASK FLAG_MASK_32
1da177e4 67
62a97d44 68static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
1da177e4 69{
65ea5b03 70 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
06ee1b68 71 regno >>= 2;
62a97d44
RM
72 if (regno > FS)
73 --regno;
65ea5b03 74 return &regs->bx + regno;
1da177e4
LT
75}
76
06ee1b68 77static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
1da177e4 78{
06ee1b68
RM
79 /*
80 * Returning the value truncates it to 16 bits.
81 */
82 unsigned int retval;
83 if (offset != offsetof(struct user_regs_struct, gs))
84 retval = *pt_regs_access(task_pt_regs(task), offset);
85 else {
86 retval = task->thread.gs;
87 if (task == current)
88 savesegment(gs, retval);
89 }
90 return retval;
91}
92
93static int set_segment_reg(struct task_struct *task,
94 unsigned long offset, u16 value)
95{
96 /*
97 * The value argument was already truncated to 16 bits.
98 */
2047b08b 99 if (invalid_selector(value))
06ee1b68
RM
100 return -EIO;
101
102 if (offset != offsetof(struct user_regs_struct, gs))
103 *pt_regs_access(task_pt_regs(task), offset) = value;
104 else {
105 task->thread.gs = value;
106 if (task == current)
5fd4d16b
RM
107 /*
108 * The user-mode %gs is not affected by
109 * kernel entry, so we must update the CPU.
110 */
111 loadsegment(gs, value);
1da177e4 112 }
06ee1b68 113
1da177e4
LT
114 return 0;
115}
116
2047b08b
RM
117static unsigned long debugreg_addr_limit(struct task_struct *task)
118{
119 return TASK_SIZE - 3;
120}
121
122#else /* CONFIG_X86_64 */
123
124#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
125
126static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
127{
128 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
129 return &regs->r15 + (offset / sizeof(regs->r15));
130}
131
132static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
133{
134 /*
135 * Returning the value truncates it to 16 bits.
136 */
137 unsigned int seg;
138
139 switch (offset) {
140 case offsetof(struct user_regs_struct, fs):
141 if (task == current) {
142 /* Older gas can't assemble movq %?s,%r?? */
143 asm("movl %%fs,%0" : "=r" (seg));
144 return seg;
145 }
146 return task->thread.fsindex;
147 case offsetof(struct user_regs_struct, gs):
148 if (task == current) {
149 asm("movl %%gs,%0" : "=r" (seg));
150 return seg;
151 }
152 return task->thread.gsindex;
153 case offsetof(struct user_regs_struct, ds):
154 if (task == current) {
155 asm("movl %%ds,%0" : "=r" (seg));
156 return seg;
157 }
158 return task->thread.ds;
159 case offsetof(struct user_regs_struct, es):
160 if (task == current) {
161 asm("movl %%es,%0" : "=r" (seg));
162 return seg;
163 }
164 return task->thread.es;
165
166 case offsetof(struct user_regs_struct, cs):
167 case offsetof(struct user_regs_struct, ss):
168 break;
169 }
170 return *pt_regs_access(task_pt_regs(task), offset);
171}
172
173static int set_segment_reg(struct task_struct *task,
174 unsigned long offset, u16 value)
175{
176 /*
177 * The value argument was already truncated to 16 bits.
178 */
179 if (invalid_selector(value))
180 return -EIO;
181
182 switch (offset) {
183 case offsetof(struct user_regs_struct,fs):
184 /*
185 * If this is setting fs as for normal 64-bit use but
186 * setting fs_base has implicitly changed it, leave it.
187 */
188 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
189 task->thread.fs != 0) ||
190 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
191 task->thread.fs == 0))
192 break;
193 task->thread.fsindex = value;
194 if (task == current)
195 loadsegment(fs, task->thread.fsindex);
196 break;
197 case offsetof(struct user_regs_struct,gs):
198 /*
199 * If this is setting gs as for normal 64-bit use but
200 * setting gs_base has implicitly changed it, leave it.
201 */
202 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
203 task->thread.gs != 0) ||
204 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
205 task->thread.gs == 0))
206 break;
207 task->thread.gsindex = value;
208 if (task == current)
209 load_gs_index(task->thread.gsindex);
210 break;
211 case offsetof(struct user_regs_struct,ds):
212 task->thread.ds = value;
213 if (task == current)
214 loadsegment(ds, task->thread.ds);
215 break;
216 case offsetof(struct user_regs_struct,es):
217 task->thread.es = value;
218 if (task == current)
219 loadsegment(es, task->thread.es);
220 break;
221
222 /*
223 * Can't actually change these in 64-bit mode.
224 */
225 case offsetof(struct user_regs_struct,cs):
226#ifdef CONFIG_IA32_EMULATION
227 if (test_tsk_thread_flag(task, TIF_IA32))
228 task_pt_regs(task)->cs = value;
2047b08b 229#endif
cb757c41 230 break;
2047b08b
RM
231 case offsetof(struct user_regs_struct,ss):
232#ifdef CONFIG_IA32_EMULATION
233 if (test_tsk_thread_flag(task, TIF_IA32))
234 task_pt_regs(task)->ss = value;
2047b08b 235#endif
cb757c41 236 break;
2047b08b
RM
237 }
238
239 return 0;
240}
241
242static unsigned long debugreg_addr_limit(struct task_struct *task)
243{
244#ifdef CONFIG_IA32_EMULATION
245 if (test_tsk_thread_flag(task, TIF_IA32))
246 return IA32_PAGE_OFFSET - 3;
247#endif
248 return TASK_SIZE64 - 7;
249}
250
251#endif /* CONFIG_X86_32 */
252
06ee1b68 253static unsigned long get_flags(struct task_struct *task)
1da177e4 254{
06ee1b68
RM
255 unsigned long retval = task_pt_regs(task)->flags;
256
257 /*
258 * If the debugger set TF, hide it from the readout.
259 */
260 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
261 retval &= ~X86_EFLAGS_TF;
1da177e4 262
1da177e4
LT
263 return retval;
264}
265
06ee1b68
RM
266static int set_flags(struct task_struct *task, unsigned long value)
267{
268 struct pt_regs *regs = task_pt_regs(task);
269
270 /*
271 * If the user value contains TF, mark that
272 * it was not "us" (the debugger) that set it.
273 * If not, make sure it stays set if we had.
274 */
275 if (value & X86_EFLAGS_TF)
276 clear_tsk_thread_flag(task, TIF_FORCED_TF);
277 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
278 value |= X86_EFLAGS_TF;
279
280 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
281
282 return 0;
283}
284
285static int putreg(struct task_struct *child,
286 unsigned long offset, unsigned long value)
287{
288 switch (offset) {
289 case offsetof(struct user_regs_struct, cs):
290 case offsetof(struct user_regs_struct, ds):
291 case offsetof(struct user_regs_struct, es):
292 case offsetof(struct user_regs_struct, fs):
293 case offsetof(struct user_regs_struct, gs):
294 case offsetof(struct user_regs_struct, ss):
295 return set_segment_reg(child, offset, value);
296
297 case offsetof(struct user_regs_struct, flags):
298 return set_flags(child, value);
2047b08b
RM
299
300#ifdef CONFIG_X86_64
301 case offsetof(struct user_regs_struct,fs_base):
302 if (value >= TASK_SIZE_OF(child))
303 return -EIO;
304 /*
305 * When changing the segment base, use do_arch_prctl
306 * to set either thread.fs or thread.fsindex and the
307 * corresponding GDT slot.
308 */
309 if (child->thread.fs != value)
310 return do_arch_prctl(child, ARCH_SET_FS, value);
311 return 0;
312 case offsetof(struct user_regs_struct,gs_base):
313 /*
314 * Exactly the same here as the %fs handling above.
315 */
316 if (value >= TASK_SIZE_OF(child))
317 return -EIO;
318 if (child->thread.gs != value)
319 return do_arch_prctl(child, ARCH_SET_GS, value);
320 return 0;
321#endif
06ee1b68
RM
322 }
323
324 *pt_regs_access(task_pt_regs(child), offset) = value;
325 return 0;
326}
327
328static unsigned long getreg(struct task_struct *task, unsigned long offset)
329{
330 switch (offset) {
331 case offsetof(struct user_regs_struct, cs):
332 case offsetof(struct user_regs_struct, ds):
333 case offsetof(struct user_regs_struct, es):
334 case offsetof(struct user_regs_struct, fs):
335 case offsetof(struct user_regs_struct, gs):
336 case offsetof(struct user_regs_struct, ss):
337 return get_segment_reg(task, offset);
338
339 case offsetof(struct user_regs_struct, flags):
340 return get_flags(task);
2047b08b
RM
341
342#ifdef CONFIG_X86_64
343 case offsetof(struct user_regs_struct, fs_base): {
344 /*
345 * do_arch_prctl may have used a GDT slot instead of
346 * the MSR. To userland, it appears the same either
347 * way, except the %fs segment selector might not be 0.
348 */
349 unsigned int seg = task->thread.fsindex;
350 if (task->thread.fs != 0)
351 return task->thread.fs;
352 if (task == current)
353 asm("movl %%fs,%0" : "=r" (seg));
354 if (seg != FS_TLS_SEL)
355 return 0;
356 return get_desc_base(&task->thread.tls_array[FS_TLS]);
357 }
358 case offsetof(struct user_regs_struct, gs_base): {
359 /*
360 * Exactly the same here as the %fs handling above.
361 */
362 unsigned int seg = task->thread.gsindex;
363 if (task->thread.gs != 0)
364 return task->thread.gs;
365 if (task == current)
366 asm("movl %%gs,%0" : "=r" (seg));
367 if (seg != GS_TLS_SEL)
368 return 0;
369 return get_desc_base(&task->thread.tls_array[GS_TLS]);
370 }
371#endif
06ee1b68
RM
372 }
373
374 return *pt_regs_access(task_pt_regs(task), offset);
375}
376
d9771e8c
RM
377/*
378 * This function is trivial and will be inlined by the compiler.
379 * Having it separates the implementation details of debug
380 * registers from the interface details of ptrace.
381 */
382static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
383{
0f534093
RM
384 switch (n) {
385 case 0: return child->thread.debugreg0;
386 case 1: return child->thread.debugreg1;
387 case 2: return child->thread.debugreg2;
388 case 3: return child->thread.debugreg3;
389 case 6: return child->thread.debugreg6;
390 case 7: return child->thread.debugreg7;
391 }
392 return 0;
d9771e8c
RM
393}
394
395static int ptrace_set_debugreg(struct task_struct *child,
396 int n, unsigned long data)
397{
0f534093
RM
398 int i;
399
d9771e8c
RM
400 if (unlikely(n == 4 || n == 5))
401 return -EIO;
402
2047b08b 403 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
d9771e8c
RM
404 return -EIO;
405
0f534093
RM
406 switch (n) {
407 case 0: child->thread.debugreg0 = data; break;
408 case 1: child->thread.debugreg1 = data; break;
409 case 2: child->thread.debugreg2 = data; break;
410 case 3: child->thread.debugreg3 = data; break;
411
412 case 6:
2047b08b
RM
413 if ((data & ~0xffffffffUL) != 0)
414 return -EIO;
0f534093
RM
415 child->thread.debugreg6 = data;
416 break;
417
418 case 7:
d9771e8c
RM
419 /*
420 * Sanity-check data. Take one half-byte at once with
421 * check = (val >> (16 + 4*i)) & 0xf. It contains the
422 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
423 * 2 and 3 are LENi. Given a list of invalid values,
424 * we do mask |= 1 << invalid_value, so that
425 * (mask >> check) & 1 is a correct test for invalid
426 * values.
427 *
428 * R/Wi contains the type of the breakpoint /
429 * watchpoint, LENi contains the length of the watched
430 * data in the watchpoint case.
431 *
432 * The invalid values are:
2047b08b 433 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
d9771e8c
RM
434 * - R/Wi == 0x10 (break on I/O reads or writes), so
435 * mask |= 0x4444.
436 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
437 * 0x1110.
438 *
439 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
440 *
441 * See the Intel Manual "System Programming Guide",
442 * 15.2.4
443 *
444 * Note that LENi == 0x10 is defined on x86_64 in long
445 * mode (i.e. even for 32-bit userspace software, but
446 * 64-bit kernel), so the x86_64 mask value is 0x5454.
447 * See the AMD manual no. 24593 (AMD64 System Programming)
448 */
2047b08b
RM
449#ifdef CONFIG_X86_32
450#define DR7_MASK 0x5f54
451#else
452#define DR7_MASK 0x5554
453#endif
d9771e8c
RM
454 data &= ~DR_CONTROL_RESERVED;
455 for (i = 0; i < 4; i++)
2047b08b 456 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
d9771e8c 457 return -EIO;
0f534093 458 child->thread.debugreg7 = data;
d9771e8c
RM
459 if (data)
460 set_tsk_thread_flag(child, TIF_DEBUG);
461 else
462 clear_tsk_thread_flag(child, TIF_DEBUG);
0f534093 463 break;
d9771e8c
RM
464 }
465
d9771e8c
RM
466 return 0;
467}
468
eee3af4a
MM
469static int ptrace_bts_max_buffer_size(void)
470{
471 return PTRACE_BTS_BUFFER_MAX;
472}
473
474static int ptrace_bts_get_buffer_size(struct task_struct *child)
475{
476 if (!child->thread.ds_area_msr)
477 return -ENXIO;
478
479 return ds_get_bts_size((void *)child->thread.ds_area_msr);
480}
481
eee3af4a
MM
482static int ptrace_bts_read_record(struct task_struct *child,
483 long index,
484 struct bts_struct __user *out)
485{
486 struct bts_struct ret;
487 int retval;
e4811f25
MM
488 int bts_size;
489 int bts_index;
eee3af4a
MM
490
491 if (!child->thread.ds_area_msr)
492 return -ENXIO;
493
e4811f25
MM
494 if (index < 0)
495 return -EINVAL;
496
497 bts_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
498 if (bts_size <= index)
499 return -EINVAL;
500
501 /* translate the ptrace bts index into the ds bts index */
502 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
503 bts_index -= (index + 1);
504 if (bts_index < 0)
505 bts_index += bts_size;
506
eee3af4a 507 retval = ds_read_bts((void *)child->thread.ds_area_msr,
e4811f25 508 bts_index, &ret);
eee3af4a
MM
509 if (retval)
510 return retval;
511
512 if (copy_to_user(out, &ret, sizeof(ret)))
513 return -EFAULT;
514
515 return sizeof(ret);
516}
517
518static int ptrace_bts_write_record(struct task_struct *child,
519 const struct bts_struct *in)
520{
521 int retval;
522
523 if (!child->thread.ds_area_msr)
524 return -ENXIO;
525
526 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
527 if (retval)
528 return retval;
529
530 return sizeof(*in);
531}
532
533static int ptrace_bts_config(struct task_struct *child,
534 unsigned long options)
535{
536 unsigned long debugctl_mask = ds_debugctl_mask();
537 int retval;
538
539 retval = ptrace_bts_get_buffer_size(child);
540 if (retval < 0)
541 return retval;
542 if (retval == 0)
543 return -ENXIO;
544
545 if (options & PTRACE_BTS_O_TRACE_TASK) {
546 child->thread.debugctlmsr |= debugctl_mask;
547 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
548 } else {
549 /* there is no way for us to check whether we 'own'
550 * the respective bits in the DEBUGCTL MSR, we're
551 * about to clear */
552 child->thread.debugctlmsr &= ~debugctl_mask;
553
554 if (!child->thread.debugctlmsr)
555 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
556 }
557
558 if (options & PTRACE_BTS_O_TIMESTAMPS)
559 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
560 else
561 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
562
563 return 0;
564}
565
566static int ptrace_bts_status(struct task_struct *child)
567{
568 unsigned long debugctl_mask = ds_debugctl_mask();
569 int retval, status = 0;
570
571 retval = ptrace_bts_get_buffer_size(child);
572 if (retval < 0)
573 return retval;
574 if (retval == 0)
575 return -ENXIO;
576
577 if (ptrace_bts_get_buffer_size(child) <= 0)
578 return -ENXIO;
579
580 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
581 child->thread.debugctlmsr & debugctl_mask)
582 status |= PTRACE_BTS_O_TRACE_TASK;
583 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
584 status |= PTRACE_BTS_O_TIMESTAMPS;
585
586 return status;
587}
588
589static int ptrace_bts_allocate_bts(struct task_struct *child,
590 int size_in_records)
591{
592 int retval = 0;
593 void *ds;
594
595 if (size_in_records < 0)
596 return -EINVAL;
597
598 if (size_in_records > ptrace_bts_max_buffer_size())
599 return -EINVAL;
600
601 if (size_in_records == 0) {
602 ptrace_bts_config(child, /* options = */ 0);
603 } else {
604 retval = ds_allocate(&ds, size_in_records);
605 if (retval)
606 return retval;
607 }
608
609 if (child->thread.ds_area_msr)
610 ds_free((void **)&child->thread.ds_area_msr);
611
612 child->thread.ds_area_msr = (unsigned long)ds;
613 if (child->thread.ds_area_msr)
614 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
615 else
616 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
617
618 return retval;
619}
620
621void ptrace_bts_take_timestamp(struct task_struct *tsk,
622 enum bts_qualifier qualifier)
623{
624 struct bts_struct rec = {
625 .qualifier = qualifier,
3c68904f 626 .variant.jiffies = jiffies
eee3af4a
MM
627 };
628
629 if (ptrace_bts_get_buffer_size(tsk) <= 0)
630 return;
631
632 ptrace_bts_write_record(tsk, &rec);
633}
634
1da177e4
LT
635/*
636 * Called by kernel/ptrace.c when detaching..
637 *
638 * Make sure the single step bit is not set.
639 */
640void ptrace_disable(struct task_struct *child)
9e714bed 641{
7f232343 642 user_disable_single_step(child);
e9c86c78 643#ifdef TIF_SYSCALL_EMU
ab1c23c2 644 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
e9c86c78 645#endif
eee3af4a
MM
646 ptrace_bts_config(child, /* options = */ 0);
647 if (child->thread.ds_area_msr) {
648 ds_free((void **)&child->thread.ds_area_msr);
649 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
650 }
1da177e4
LT
651}
652
481bed45 653long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1da177e4 654{
1da177e4
LT
655 int i, ret;
656 unsigned long __user *datap = (unsigned long __user *)data;
657
1da177e4
LT
658 switch (request) {
659 /* when I and D space are separate, these will need to be fixed. */
9e714bed 660 case PTRACE_PEEKTEXT: /* read word at location addr. */
76647323
AD
661 case PTRACE_PEEKDATA:
662 ret = generic_ptrace_peekdata(child, addr, data);
1da177e4 663 break;
1da177e4
LT
664
665 /* read the word at location addr in the USER area. */
666 case PTRACE_PEEKUSR: {
667 unsigned long tmp;
668
669 ret = -EIO;
e9c86c78
RM
670 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
671 addr >= sizeof(struct user))
1da177e4
LT
672 break;
673
674 tmp = 0; /* Default return condition */
e9c86c78 675 if (addr < sizeof(struct user_regs_struct))
1da177e4 676 tmp = getreg(child, addr);
e9c86c78
RM
677 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
678 addr <= offsetof(struct user, u_debugreg[7])) {
679 addr -= offsetof(struct user, u_debugreg[0]);
680 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1da177e4
LT
681 }
682 ret = put_user(tmp, datap);
683 break;
684 }
685
686 /* when I and D space are separate, this will have to be fixed. */
687 case PTRACE_POKETEXT: /* write the word at location addr. */
688 case PTRACE_POKEDATA:
f284ce72 689 ret = generic_ptrace_pokedata(child, addr, data);
1da177e4
LT
690 break;
691
692 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
693 ret = -EIO;
e9c86c78
RM
694 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
695 addr >= sizeof(struct user))
1da177e4
LT
696 break;
697
e9c86c78 698 if (addr < sizeof(struct user_regs_struct))
1da177e4 699 ret = putreg(child, addr, data);
e9c86c78
RM
700 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
701 addr <= offsetof(struct user, u_debugreg[7])) {
702 addr -= offsetof(struct user, u_debugreg[0]);
703 ret = ptrace_set_debugreg(child,
704 addr / sizeof(data), data);
1da177e4 705 }
e9c86c78 706 break;
1da177e4 707
1da177e4 708 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
e9c86c78 709 if (!access_ok(VERIFY_WRITE, datap, sizeof(struct user_regs_struct))) {
1da177e4
LT
710 ret = -EIO;
711 break;
712 }
e9c86c78 713 for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
1da177e4
LT
714 __put_user(getreg(child, i), datap);
715 datap++;
716 }
717 ret = 0;
718 break;
719 }
720
721 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
722 unsigned long tmp;
e9c86c78 723 if (!access_ok(VERIFY_READ, datap, sizeof(struct user_regs_struct))) {
1da177e4
LT
724 ret = -EIO;
725 break;
726 }
e9c86c78 727 for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
1da177e4
LT
728 __get_user(tmp, datap);
729 putreg(child, i, tmp);
730 datap++;
731 }
732 ret = 0;
733 break;
734 }
735
736 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
737 if (!access_ok(VERIFY_WRITE, datap,
738 sizeof(struct user_i387_struct))) {
739 ret = -EIO;
740 break;
741 }
742 ret = 0;
743 if (!tsk_used_math(child))
744 init_fpu(child);
745 get_fpregs((struct user_i387_struct __user *)data, child);
746 break;
747 }
748
749 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
750 if (!access_ok(VERIFY_READ, datap,
751 sizeof(struct user_i387_struct))) {
752 ret = -EIO;
753 break;
754 }
755 set_stopped_child_used_math(child);
756 set_fpregs(child, (struct user_i387_struct __user *)data);
757 ret = 0;
758 break;
759 }
760
e9c86c78 761#ifdef CONFIG_X86_32
1da177e4
LT
762 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
763 if (!access_ok(VERIFY_WRITE, datap,
764 sizeof(struct user_fxsr_struct))) {
765 ret = -EIO;
766 break;
767 }
768 if (!tsk_used_math(child))
769 init_fpu(child);
770 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
771 break;
772 }
773
774 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
775 if (!access_ok(VERIFY_READ, datap,
776 sizeof(struct user_fxsr_struct))) {
777 ret = -EIO;
778 break;
779 }
780 set_stopped_child_used_math(child);
781 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
782 break;
783 }
e9c86c78 784#endif
1da177e4 785
e9c86c78 786#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1da177e4 787 case PTRACE_GET_THREAD_AREA:
efd1ca52
RM
788 if (addr < 0)
789 return -EIO;
790 ret = do_get_thread_area(child, addr,
791 (struct user_desc __user *) data);
1da177e4
LT
792 break;
793
794 case PTRACE_SET_THREAD_AREA:
efd1ca52
RM
795 if (addr < 0)
796 return -EIO;
797 ret = do_set_thread_area(child, addr,
798 (struct user_desc __user *) data, 0);
1da177e4 799 break;
e9c86c78
RM
800#endif
801
802#ifdef CONFIG_X86_64
803 /* normal 64bit interface to access TLS data.
804 Works just like arch_prctl, except that the arguments
805 are reversed. */
806 case PTRACE_ARCH_PRCTL:
807 ret = do_arch_prctl(child, data, addr);
808 break;
809#endif
1da177e4 810
eee3af4a
MM
811 case PTRACE_BTS_MAX_BUFFER_SIZE:
812 ret = ptrace_bts_max_buffer_size();
813 break;
814
815 case PTRACE_BTS_ALLOCATE_BUFFER:
816 ret = ptrace_bts_allocate_bts(child, data);
817 break;
818
819 case PTRACE_BTS_GET_BUFFER_SIZE:
820 ret = ptrace_bts_get_buffer_size(child);
821 break;
822
eee3af4a
MM
823 case PTRACE_BTS_READ_RECORD:
824 ret = ptrace_bts_read_record
825 (child, data,
826 (struct bts_struct __user *) addr);
827 break;
828
829 case PTRACE_BTS_CONFIG:
830 ret = ptrace_bts_config(child, data);
831 break;
832
833 case PTRACE_BTS_STATUS:
834 ret = ptrace_bts_status(child);
835 break;
836
1da177e4
LT
837 default:
838 ret = ptrace_request(child, request, addr, data);
839 break;
840 }
d9771e8c 841
1da177e4
LT
842 return ret;
843}
844
cb757c41
RM
845#ifdef CONFIG_IA32_EMULATION
846
099cd6e9
RM
847#include <linux/compat.h>
848#include <linux/syscalls.h>
849#include <asm/ia32.h>
850#include <asm/fpu32.h>
cb757c41
RM
851#include <asm/user32.h>
852
853#define R32(l,q) \
854 case offsetof(struct user32, regs.l): \
855 regs->q = value; break
856
857#define SEG32(rs) \
858 case offsetof(struct user32, regs.rs): \
859 return set_segment_reg(child, \
860 offsetof(struct user_regs_struct, rs), \
861 value); \
862 break
863
864static int putreg32(struct task_struct *child, unsigned regno, u32 value)
865{
866 struct pt_regs *regs = task_pt_regs(child);
867
868 switch (regno) {
869
870 SEG32(cs);
871 SEG32(ds);
872 SEG32(es);
873 SEG32(fs);
874 SEG32(gs);
875 SEG32(ss);
876
877 R32(ebx, bx);
878 R32(ecx, cx);
879 R32(edx, dx);
880 R32(edi, di);
881 R32(esi, si);
882 R32(ebp, bp);
883 R32(eax, ax);
884 R32(orig_eax, orig_ax);
885 R32(eip, ip);
886 R32(esp, sp);
887
888 case offsetof(struct user32, regs.eflags):
889 return set_flags(child, value);
890
891 case offsetof(struct user32, u_debugreg[0]) ...
892 offsetof(struct user32, u_debugreg[7]):
893 regno -= offsetof(struct user32, u_debugreg[0]);
894 return ptrace_set_debugreg(child, regno / 4, value);
895
896 default:
897 if (regno > sizeof(struct user32) || (regno & 3))
898 return -EIO;
899
900 /*
901 * Other dummy fields in the virtual user structure
902 * are ignored
903 */
904 break;
905 }
906 return 0;
907}
908
909#undef R32
910#undef SEG32
911
912#define R32(l,q) \
913 case offsetof(struct user32, regs.l): \
914 *val = regs->q; break
915
916#define SEG32(rs) \
917 case offsetof(struct user32, regs.rs): \
918 *val = get_segment_reg(child, \
919 offsetof(struct user_regs_struct, rs)); \
920 break
921
922static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
923{
924 struct pt_regs *regs = task_pt_regs(child);
925
926 switch (regno) {
927
928 SEG32(ds);
929 SEG32(es);
930 SEG32(fs);
931 SEG32(gs);
932
933 R32(cs, cs);
934 R32(ss, ss);
935 R32(ebx, bx);
936 R32(ecx, cx);
937 R32(edx, dx);
938 R32(edi, di);
939 R32(esi, si);
940 R32(ebp, bp);
941 R32(eax, ax);
942 R32(orig_eax, orig_ax);
943 R32(eip, ip);
944 R32(esp, sp);
945
946 case offsetof(struct user32, regs.eflags):
947 *val = get_flags(child);
948 break;
949
950 case offsetof(struct user32, u_debugreg[0]) ...
951 offsetof(struct user32, u_debugreg[7]):
952 regno -= offsetof(struct user32, u_debugreg[0]);
953 *val = ptrace_get_debugreg(child, regno / 4);
954 break;
955
956 default:
957 if (regno > sizeof(struct user32) || (regno & 3))
958 return -EIO;
959
960 /*
961 * Other dummy fields in the virtual user structure
962 * are ignored
963 */
964 *val = 0;
965 break;
966 }
967 return 0;
968}
969
970#undef R32
971#undef SEG32
972
099cd6e9
RM
973static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
974{
975 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
976 compat_siginfo_t __user *si32 = compat_ptr(data);
977 siginfo_t ssi;
978 int ret;
979
980 if (request == PTRACE_SETSIGINFO) {
981 memset(&ssi, 0, sizeof(siginfo_t));
982 ret = copy_siginfo_from_user32(&ssi, si32);
983 if (ret)
984 return ret;
985 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
986 return -EFAULT;
987 }
988 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
989 if (ret)
990 return ret;
991 if (request == PTRACE_GETSIGINFO) {
992 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
993 return -EFAULT;
994 ret = copy_siginfo_to_user32(si32, &ssi);
995 }
996 return ret;
997}
998
999asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
1000{
1001 struct task_struct *child;
1002 struct pt_regs *childregs;
1003 void __user *datap = compat_ptr(data);
1004 int ret;
1005 __u32 val;
1006
1007 switch (request) {
1008 case PTRACE_TRACEME:
1009 case PTRACE_ATTACH:
1010 case PTRACE_KILL:
1011 case PTRACE_CONT:
1012 case PTRACE_SINGLESTEP:
1013 case PTRACE_SINGLEBLOCK:
1014 case PTRACE_DETACH:
1015 case PTRACE_SYSCALL:
1016 case PTRACE_OLDSETOPTIONS:
1017 case PTRACE_SETOPTIONS:
1018 case PTRACE_SET_THREAD_AREA:
1019 case PTRACE_GET_THREAD_AREA:
eee3af4a
MM
1020 case PTRACE_BTS_MAX_BUFFER_SIZE:
1021 case PTRACE_BTS_ALLOCATE_BUFFER:
1022 case PTRACE_BTS_GET_BUFFER_SIZE:
eee3af4a
MM
1023 case PTRACE_BTS_READ_RECORD:
1024 case PTRACE_BTS_CONFIG:
1025 case PTRACE_BTS_STATUS:
099cd6e9
RM
1026 return sys_ptrace(request, pid, addr, data);
1027
1028 default:
1029 return -EINVAL;
1030
1031 case PTRACE_PEEKTEXT:
1032 case PTRACE_PEEKDATA:
1033 case PTRACE_POKEDATA:
1034 case PTRACE_POKETEXT:
1035 case PTRACE_POKEUSR:
1036 case PTRACE_PEEKUSR:
1037 case PTRACE_GETREGS:
1038 case PTRACE_SETREGS:
1039 case PTRACE_SETFPREGS:
1040 case PTRACE_GETFPREGS:
1041 case PTRACE_SETFPXREGS:
1042 case PTRACE_GETFPXREGS:
1043 case PTRACE_GETEVENTMSG:
1044 break;
1045
1046 case PTRACE_SETSIGINFO:
1047 case PTRACE_GETSIGINFO:
1048 return ptrace32_siginfo(request, pid, addr, data);
1049 }
1050
1051 child = ptrace_get_task_struct(pid);
1052 if (IS_ERR(child))
1053 return PTR_ERR(child);
1054
1055 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1056 if (ret < 0)
1057 goto out;
1058
1059 childregs = task_pt_regs(child);
1060
1061 switch (request) {
1062 case PTRACE_PEEKDATA:
1063 case PTRACE_PEEKTEXT:
1064 ret = 0;
1065 if (access_process_vm(child, addr, &val, sizeof(u32), 0) !=
1066 sizeof(u32))
1067 ret = -EIO;
1068 else
1069 ret = put_user(val, (unsigned int __user *)datap);
1070 break;
1071
1072 case PTRACE_POKEDATA:
1073 case PTRACE_POKETEXT:
1074 ret = 0;
1075 if (access_process_vm(child, addr, &data, sizeof(u32), 1) !=
1076 sizeof(u32))
1077 ret = -EIO;
1078 break;
1079
1080 case PTRACE_PEEKUSR:
1081 ret = getreg32(child, addr, &val);
1082 if (ret == 0)
1083 ret = put_user(val, (__u32 __user *)datap);
1084 break;
1085
1086 case PTRACE_POKEUSR:
1087 ret = putreg32(child, addr, data);
1088 break;
1089
1090 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
1091 int i;
1092
1093 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
1094 ret = -EIO;
1095 break;
1096 }
1097 ret = 0;
1098 for (i = 0; i < sizeof(struct user_regs_struct32); i += sizeof(__u32)) {
1099 getreg32(child, i, &val);
1100 ret |= __put_user(val, (u32 __user *)datap);
1101 datap += sizeof(u32);
1102 }
1103 break;
1104 }
1105
1106 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
1107 unsigned long tmp;
1108 int i;
1109
1110 if (!access_ok(VERIFY_READ, datap, 16*4)) {
1111 ret = -EIO;
1112 break;
1113 }
1114 ret = 0;
1115 for (i = 0; i < sizeof(struct user_regs_struct32); i += sizeof(u32)) {
1116 ret |= __get_user(tmp, (u32 __user *)datap);
1117 putreg32(child, i, tmp);
1118 datap += sizeof(u32);
1119 }
1120 break;
1121 }
1122
1123 case PTRACE_GETFPREGS:
1124 ret = -EIO;
1125 if (!access_ok(VERIFY_READ, compat_ptr(data),
1126 sizeof(struct user_i387_struct)))
1127 break;
1128 save_i387_ia32(child, datap, childregs, 1);
1129 ret = 0;
1130 break;
1131
1132 case PTRACE_SETFPREGS:
1133 ret = -EIO;
1134 if (!access_ok(VERIFY_WRITE, datap,
1135 sizeof(struct user_i387_struct)))
1136 break;
1137 ret = 0;
1138 /* don't check EFAULT to be bug-to-bug compatible to i386 */
1139 restore_i387_ia32(child, datap, 1);
1140 break;
1141
1142 case PTRACE_GETFPXREGS: {
1143 struct user32_fxsr_struct __user *u = datap;
1144
1145 init_fpu(child);
1146 ret = -EIO;
1147 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
1148 break;
1149 ret = -EFAULT;
1150 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
1151 break;
1152 ret = __put_user(childregs->cs, &u->fcs);
1153 ret |= __put_user(child->thread.ds, &u->fos);
1154 break;
1155 }
1156 case PTRACE_SETFPXREGS: {
1157 struct user32_fxsr_struct __user *u = datap;
1158
1159 unlazy_fpu(child);
1160 ret = -EIO;
1161 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
1162 break;
1163 /*
1164 * no checking to be bug-to-bug compatible with i386.
1165 * but silence warning
1166 */
1167 if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
1168 ;
1169 set_stopped_child_used_math(child);
1170 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
1171 ret = 0;
1172 break;
1173 }
1174
1175 case PTRACE_GETEVENTMSG:
1176 ret = put_user(child->ptrace_message,
1177 (unsigned int __user *)compat_ptr(data));
1178 break;
1179
1180 default:
1181 BUG();
1182 }
1183
1184 out:
1185 put_task_struct(child);
1186 return ret;
1187}
1188
cb757c41
RM
1189#endif /* CONFIG_IA32_EMULATION */
1190
86976cd8
RM
1191#ifdef CONFIG_X86_32
1192
1da177e4
LT
1193void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
1194{
1195 struct siginfo info;
1196
1197 tsk->thread.trap_no = 1;
1198 tsk->thread.error_code = error_code;
1199
1200 memset(&info, 0, sizeof(info));
1201 info.si_signo = SIGTRAP;
1202 info.si_code = TRAP_BRKPT;
1203
65ea5b03
PA
1204 /* User-mode ip? */
1205 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1da177e4 1206
27b46d76 1207 /* Send us the fake SIGTRAP */
1da177e4
LT
1208 force_sig_info(SIGTRAP, &info, tsk);
1209}
1210
1211/* notification of system call entry/exit
1212 * - triggered by current->work.syscall_trace
1213 */
1214__attribute__((regparm(3)))
ed75e8d5 1215int do_syscall_trace(struct pt_regs *regs, int entryexit)
1da177e4 1216{
4c7fc722
AA
1217 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
1218 /*
1219 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1220 * interception
1221 */
1b38f006 1222 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
4c7fc722 1223 int ret = 0;
1b38f006 1224
1da177e4 1225 /* do the secure computing check first */
4c7fc722 1226 if (!entryexit)
65ea5b03 1227 secure_computing(regs->orig_ax);
1da177e4 1228
ab1c23c2
BS
1229 if (unlikely(current->audit_context)) {
1230 if (entryexit)
65ea5b03
PA
1231 audit_syscall_exit(AUDITSC_RESULT(regs->ax),
1232 regs->ax);
ab1c23c2
BS
1233 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1234 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1235 * not used, entry.S will call us only on syscall exit, not
1236 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1237 * calling send_sigtrap() on syscall entry.
1238 *
1239 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1240 * is_singlestep is false, despite his name, so we will still do
1241 * the correct thing.
1242 */
1243 else if (is_singlestep)
1244 goto out;
1245 }
1da177e4
LT
1246
1247 if (!(current->ptrace & PT_PTRACED))
2fd6f58b 1248 goto out;
1da177e4 1249
1b38f006
BS
1250 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1251 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1252 * here. We have to check this and return */
1253 if (is_sysemu && entryexit)
1254 return 0;
ed75e8d5 1255
1da177e4 1256 /* Fake a debug trap */
c8c86cec 1257 if (is_singlestep)
1da177e4
LT
1258 send_sigtrap(current, regs, 0);
1259
c8c86cec 1260 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
2fd6f58b 1261 goto out;
1da177e4
LT
1262
1263 /* the 0x80 provides a way for the tracing parent to distinguish
1264 between a syscall stop and SIGTRAP delivery */
ed75e8d5 1265 /* Note that the debugger could change the result of test_thread_flag!*/
4c7fc722 1266 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
1da177e4
LT
1267
1268 /*
1269 * this isn't the same as continuing with a signal, but it will do
1270 * for normal use. strace only continues with a signal if the
1271 * stopping signal is not SIGTRAP. -brl
1272 */
1273 if (current->exit_code) {
1274 send_sig(current->exit_code, current, 1);
1275 current->exit_code = 0;
1276 }
ed75e8d5 1277 ret = is_sysemu;
4c7fc722 1278out:
2fd6f58b 1279 if (unlikely(current->audit_context) && !entryexit)
65ea5b03
PA
1280 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
1281 regs->bx, regs->cx, regs->dx, regs->si);
c8c86cec
BS
1282 if (ret == 0)
1283 return 0;
1284
65ea5b03 1285 regs->orig_ax = -1; /* force skip of syscall restarting */
c8c86cec 1286 if (unlikely(current->audit_context))
65ea5b03 1287 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
c8c86cec 1288 return 1;
1da177e4 1289}
86976cd8
RM
1290
1291#else /* CONFIG_X86_64 */
1292
1293static void syscall_trace(struct pt_regs *regs)
1294{
1295
1296#if 0
1297 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1298 current->comm,
1299 regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
1300 current_thread_info()->flags, current->ptrace);
1301#endif
1302
1303 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1304 ? 0x80 : 0));
1305 /*
1306 * this isn't the same as continuing with a signal, but it will do
1307 * for normal use. strace only continues with a signal if the
1308 * stopping signal is not SIGTRAP. -brl
1309 */
1310 if (current->exit_code) {
1311 send_sig(current->exit_code, current, 1);
1312 current->exit_code = 0;
1313 }
1314}
1315
1316asmlinkage void syscall_trace_enter(struct pt_regs *regs)
1317{
1318 /* do the secure computing check first */
1319 secure_computing(regs->orig_ax);
1320
1321 if (test_thread_flag(TIF_SYSCALL_TRACE)
1322 && (current->ptrace & PT_PTRACED))
1323 syscall_trace(regs);
1324
1325 if (unlikely(current->audit_context)) {
1326 if (test_thread_flag(TIF_IA32)) {
1327 audit_syscall_entry(AUDIT_ARCH_I386,
1328 regs->orig_ax,
1329 regs->bx, regs->cx,
1330 regs->dx, regs->si);
1331 } else {
1332 audit_syscall_entry(AUDIT_ARCH_X86_64,
1333 regs->orig_ax,
1334 regs->di, regs->si,
1335 regs->dx, regs->r10);
1336 }
1337 }
1338}
1339
1340asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1341{
1342 if (unlikely(current->audit_context))
1343 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1344
1345 if ((test_thread_flag(TIF_SYSCALL_TRACE)
1346 || test_thread_flag(TIF_SINGLESTEP))
1347 && (current->ptrace & PT_PTRACED))
1348 syscall_trace(regs);
1349}
1350
1351#endif /* CONFIG_X86_32 */