Merge branches 'x86/apic', 'x86/cleanups', 'x86/cpufeature', 'x86/crashdump', 'x86...
[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>
1da177e4
LT
24
25#include <asm/uaccess.h>
26#include <asm/pgtable.h>
27#include <asm/system.h>
28#include <asm/processor.h>
29#include <asm/i387.h>
30#include <asm/debugreg.h>
31#include <asm/ldt.h>
32#include <asm/desc.h>
2047b08b
RM
33#include <asm/prctl.h>
34#include <asm/proto.h>
eee3af4a
MM
35#include <asm/ds.h>
36
070459d9
RM
37#include "tls.h"
38
39enum x86_regset {
40 REGSET_GENERAL,
41 REGSET_FP,
42 REGSET_XFP,
325af5fb 43 REGSET_IOPERM64 = REGSET_XFP,
070459d9 44 REGSET_TLS,
325af5fb 45 REGSET_IOPERM32,
070459d9 46};
eee3af4a 47
1da177e4
LT
48/*
49 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
51 */
52
9f155b98
CE
53/*
54 * Determines which flags the user has access to [1 = access, 0 = no access].
9f155b98 55 */
e39c2891
RM
56#define FLAG_MASK_32 ((unsigned long) \
57 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
58 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
59 X86_EFLAGS_SF | X86_EFLAGS_TF | \
60 X86_EFLAGS_DF | X86_EFLAGS_OF | \
61 X86_EFLAGS_RF | X86_EFLAGS_AC))
62
2047b08b
RM
63/*
64 * Determines whether a value may be installed in a segment register.
65 */
66static inline bool invalid_selector(u16 value)
67{
68 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
69}
70
71#ifdef CONFIG_X86_32
72
e39c2891 73#define FLAG_MASK FLAG_MASK_32
1da177e4 74
4fe702c7 75static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
1da177e4 76{
65ea5b03 77 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
06ee1b68 78 regno >>= 2;
62a97d44
RM
79 if (regno > FS)
80 --regno;
65ea5b03 81 return &regs->bx + regno;
1da177e4
LT
82}
83
06ee1b68 84static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
1da177e4 85{
06ee1b68
RM
86 /*
87 * Returning the value truncates it to 16 bits.
88 */
89 unsigned int retval;
90 if (offset != offsetof(struct user_regs_struct, gs))
91 retval = *pt_regs_access(task_pt_regs(task), offset);
92 else {
93 retval = task->thread.gs;
94 if (task == current)
95 savesegment(gs, retval);
96 }
97 return retval;
98}
99
100static int set_segment_reg(struct task_struct *task,
101 unsigned long offset, u16 value)
102{
103 /*
104 * The value argument was already truncated to 16 bits.
105 */
2047b08b 106 if (invalid_selector(value))
06ee1b68
RM
107 return -EIO;
108
c63855d0
RM
109 /*
110 * For %cs and %ss we cannot permit a null selector.
111 * We can permit a bogus selector as long as it has USER_RPL.
112 * Null selectors are fine for other segment registers, but
113 * we will never get back to user mode with invalid %cs or %ss
114 * and will take the trap in iret instead. Much code relies
115 * on user_mode() to distinguish a user trap frame (which can
116 * safely use invalid selectors) from a kernel trap frame.
117 */
118 switch (offset) {
119 case offsetof(struct user_regs_struct, cs):
120 case offsetof(struct user_regs_struct, ss):
121 if (unlikely(value == 0))
122 return -EIO;
123
124 default:
06ee1b68 125 *pt_regs_access(task_pt_regs(task), offset) = value;
c63855d0
RM
126 break;
127
128 case offsetof(struct user_regs_struct, gs):
06ee1b68
RM
129 task->thread.gs = value;
130 if (task == current)
5fd4d16b
RM
131 /*
132 * The user-mode %gs is not affected by
133 * kernel entry, so we must update the CPU.
134 */
135 loadsegment(gs, value);
1da177e4 136 }
06ee1b68 137
1da177e4
LT
138 return 0;
139}
140
2047b08b
RM
141static unsigned long debugreg_addr_limit(struct task_struct *task)
142{
143 return TASK_SIZE - 3;
144}
145
146#else /* CONFIG_X86_64 */
147
148#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
149
150static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
151{
152 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
153 return &regs->r15 + (offset / sizeof(regs->r15));
154}
155
156static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
157{
158 /*
159 * Returning the value truncates it to 16 bits.
160 */
161 unsigned int seg;
162
163 switch (offset) {
164 case offsetof(struct user_regs_struct, fs):
165 if (task == current) {
166 /* Older gas can't assemble movq %?s,%r?? */
167 asm("movl %%fs,%0" : "=r" (seg));
168 return seg;
169 }
170 return task->thread.fsindex;
171 case offsetof(struct user_regs_struct, gs):
172 if (task == current) {
173 asm("movl %%gs,%0" : "=r" (seg));
174 return seg;
175 }
176 return task->thread.gsindex;
177 case offsetof(struct user_regs_struct, ds):
178 if (task == current) {
179 asm("movl %%ds,%0" : "=r" (seg));
180 return seg;
181 }
182 return task->thread.ds;
183 case offsetof(struct user_regs_struct, es):
184 if (task == current) {
185 asm("movl %%es,%0" : "=r" (seg));
186 return seg;
187 }
188 return task->thread.es;
189
190 case offsetof(struct user_regs_struct, cs):
191 case offsetof(struct user_regs_struct, ss):
192 break;
193 }
194 return *pt_regs_access(task_pt_regs(task), offset);
195}
196
197static int set_segment_reg(struct task_struct *task,
198 unsigned long offset, u16 value)
199{
200 /*
201 * The value argument was already truncated to 16 bits.
202 */
203 if (invalid_selector(value))
204 return -EIO;
205
206 switch (offset) {
207 case offsetof(struct user_regs_struct,fs):
208 /*
209 * If this is setting fs as for normal 64-bit use but
210 * setting fs_base has implicitly changed it, leave it.
211 */
212 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
213 task->thread.fs != 0) ||
214 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
215 task->thread.fs == 0))
216 break;
217 task->thread.fsindex = value;
218 if (task == current)
219 loadsegment(fs, task->thread.fsindex);
220 break;
221 case offsetof(struct user_regs_struct,gs):
222 /*
223 * If this is setting gs as for normal 64-bit use but
224 * setting gs_base has implicitly changed it, leave it.
225 */
226 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
227 task->thread.gs != 0) ||
228 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
229 task->thread.gs == 0))
230 break;
231 task->thread.gsindex = value;
232 if (task == current)
233 load_gs_index(task->thread.gsindex);
234 break;
235 case offsetof(struct user_regs_struct,ds):
236 task->thread.ds = value;
237 if (task == current)
238 loadsegment(ds, task->thread.ds);
239 break;
240 case offsetof(struct user_regs_struct,es):
241 task->thread.es = value;
242 if (task == current)
243 loadsegment(es, task->thread.es);
244 break;
245
246 /*
247 * Can't actually change these in 64-bit mode.
248 */
249 case offsetof(struct user_regs_struct,cs):
c63855d0
RM
250 if (unlikely(value == 0))
251 return -EIO;
2047b08b
RM
252#ifdef CONFIG_IA32_EMULATION
253 if (test_tsk_thread_flag(task, TIF_IA32))
254 task_pt_regs(task)->cs = value;
2047b08b 255#endif
cb757c41 256 break;
2047b08b 257 case offsetof(struct user_regs_struct,ss):
c63855d0
RM
258 if (unlikely(value == 0))
259 return -EIO;
2047b08b
RM
260#ifdef CONFIG_IA32_EMULATION
261 if (test_tsk_thread_flag(task, TIF_IA32))
262 task_pt_regs(task)->ss = value;
2047b08b 263#endif
cb757c41 264 break;
2047b08b
RM
265 }
266
267 return 0;
268}
269
270static unsigned long debugreg_addr_limit(struct task_struct *task)
271{
272#ifdef CONFIG_IA32_EMULATION
273 if (test_tsk_thread_flag(task, TIF_IA32))
274 return IA32_PAGE_OFFSET - 3;
275#endif
276 return TASK_SIZE64 - 7;
277}
278
279#endif /* CONFIG_X86_32 */
280
06ee1b68 281static unsigned long get_flags(struct task_struct *task)
1da177e4 282{
06ee1b68
RM
283 unsigned long retval = task_pt_regs(task)->flags;
284
285 /*
286 * If the debugger set TF, hide it from the readout.
287 */
288 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
289 retval &= ~X86_EFLAGS_TF;
1da177e4 290
1da177e4
LT
291 return retval;
292}
293
06ee1b68
RM
294static int set_flags(struct task_struct *task, unsigned long value)
295{
296 struct pt_regs *regs = task_pt_regs(task);
297
298 /*
299 * If the user value contains TF, mark that
300 * it was not "us" (the debugger) that set it.
301 * If not, make sure it stays set if we had.
302 */
303 if (value & X86_EFLAGS_TF)
304 clear_tsk_thread_flag(task, TIF_FORCED_TF);
305 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
306 value |= X86_EFLAGS_TF;
307
308 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
309
310 return 0;
311}
312
313static int putreg(struct task_struct *child,
314 unsigned long offset, unsigned long value)
315{
316 switch (offset) {
317 case offsetof(struct user_regs_struct, cs):
318 case offsetof(struct user_regs_struct, ds):
319 case offsetof(struct user_regs_struct, es):
320 case offsetof(struct user_regs_struct, fs):
321 case offsetof(struct user_regs_struct, gs):
322 case offsetof(struct user_regs_struct, ss):
323 return set_segment_reg(child, offset, value);
324
325 case offsetof(struct user_regs_struct, flags):
326 return set_flags(child, value);
2047b08b
RM
327
328#ifdef CONFIG_X86_64
84c6f604
RM
329 /*
330 * Orig_ax is really just a flag with small positive and
331 * negative values, so make sure to always sign-extend it
332 * from 32 bits so that it works correctly regardless of
333 * whether we come from a 32-bit environment or not.
334 */
335 case offsetof(struct user_regs_struct, orig_ax):
336 value = (long) (s32) value;
337 break;
338
2047b08b
RM
339 case offsetof(struct user_regs_struct,fs_base):
340 if (value >= TASK_SIZE_OF(child))
341 return -EIO;
342 /*
343 * When changing the segment base, use do_arch_prctl
344 * to set either thread.fs or thread.fsindex and the
345 * corresponding GDT slot.
346 */
347 if (child->thread.fs != value)
348 return do_arch_prctl(child, ARCH_SET_FS, value);
349 return 0;
350 case offsetof(struct user_regs_struct,gs_base):
351 /*
352 * Exactly the same here as the %fs handling above.
353 */
354 if (value >= TASK_SIZE_OF(child))
355 return -EIO;
356 if (child->thread.gs != value)
357 return do_arch_prctl(child, ARCH_SET_GS, value);
358 return 0;
359#endif
06ee1b68
RM
360 }
361
362 *pt_regs_access(task_pt_regs(child), offset) = value;
363 return 0;
364}
365
366static unsigned long getreg(struct task_struct *task, unsigned long offset)
367{
368 switch (offset) {
369 case offsetof(struct user_regs_struct, cs):
370 case offsetof(struct user_regs_struct, ds):
371 case offsetof(struct user_regs_struct, es):
372 case offsetof(struct user_regs_struct, fs):
373 case offsetof(struct user_regs_struct, gs):
374 case offsetof(struct user_regs_struct, ss):
375 return get_segment_reg(task, offset);
376
377 case offsetof(struct user_regs_struct, flags):
378 return get_flags(task);
2047b08b
RM
379
380#ifdef CONFIG_X86_64
381 case offsetof(struct user_regs_struct, fs_base): {
382 /*
383 * do_arch_prctl may have used a GDT slot instead of
384 * the MSR. To userland, it appears the same either
385 * way, except the %fs segment selector might not be 0.
386 */
387 unsigned int seg = task->thread.fsindex;
388 if (task->thread.fs != 0)
389 return task->thread.fs;
390 if (task == current)
391 asm("movl %%fs,%0" : "=r" (seg));
392 if (seg != FS_TLS_SEL)
393 return 0;
394 return get_desc_base(&task->thread.tls_array[FS_TLS]);
395 }
396 case offsetof(struct user_regs_struct, gs_base): {
397 /*
398 * Exactly the same here as the %fs handling above.
399 */
400 unsigned int seg = task->thread.gsindex;
401 if (task->thread.gs != 0)
402 return task->thread.gs;
403 if (task == current)
404 asm("movl %%gs,%0" : "=r" (seg));
405 if (seg != GS_TLS_SEL)
406 return 0;
407 return get_desc_base(&task->thread.tls_array[GS_TLS]);
408 }
409#endif
06ee1b68
RM
410 }
411
412 return *pt_regs_access(task_pt_regs(task), offset);
413}
414
91e7b707
RM
415static int genregs_get(struct task_struct *target,
416 const struct user_regset *regset,
417 unsigned int pos, unsigned int count,
418 void *kbuf, void __user *ubuf)
419{
420 if (kbuf) {
421 unsigned long *k = kbuf;
422 while (count > 0) {
423 *k++ = getreg(target, pos);
424 count -= sizeof(*k);
425 pos += sizeof(*k);
426 }
427 } else {
428 unsigned long __user *u = ubuf;
429 while (count > 0) {
430 if (__put_user(getreg(target, pos), u++))
431 return -EFAULT;
432 count -= sizeof(*u);
433 pos += sizeof(*u);
434 }
435 }
436
437 return 0;
438}
439
440static int genregs_set(struct task_struct *target,
441 const struct user_regset *regset,
442 unsigned int pos, unsigned int count,
443 const void *kbuf, const void __user *ubuf)
444{
445 int ret = 0;
446 if (kbuf) {
447 const unsigned long *k = kbuf;
448 while (count > 0 && !ret) {
449 ret = putreg(target, pos, *k++);
450 count -= sizeof(*k);
451 pos += sizeof(*k);
452 }
453 } else {
454 const unsigned long __user *u = ubuf;
455 while (count > 0 && !ret) {
456 unsigned long word;
457 ret = __get_user(word, u++);
458 if (ret)
459 break;
460 ret = putreg(target, pos, word);
461 count -= sizeof(*u);
462 pos += sizeof(*u);
463 }
464 }
465 return ret;
466}
467
d9771e8c
RM
468/*
469 * This function is trivial and will be inlined by the compiler.
470 * Having it separates the implementation details of debug
471 * registers from the interface details of ptrace.
472 */
473static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
474{
0f534093
RM
475 switch (n) {
476 case 0: return child->thread.debugreg0;
477 case 1: return child->thread.debugreg1;
478 case 2: return child->thread.debugreg2;
479 case 3: return child->thread.debugreg3;
480 case 6: return child->thread.debugreg6;
481 case 7: return child->thread.debugreg7;
482 }
483 return 0;
d9771e8c
RM
484}
485
486static int ptrace_set_debugreg(struct task_struct *child,
487 int n, unsigned long data)
488{
0f534093
RM
489 int i;
490
d9771e8c
RM
491 if (unlikely(n == 4 || n == 5))
492 return -EIO;
493
2047b08b 494 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
d9771e8c
RM
495 return -EIO;
496
0f534093
RM
497 switch (n) {
498 case 0: child->thread.debugreg0 = data; break;
499 case 1: child->thread.debugreg1 = data; break;
500 case 2: child->thread.debugreg2 = data; break;
501 case 3: child->thread.debugreg3 = data; break;
502
503 case 6:
2047b08b
RM
504 if ((data & ~0xffffffffUL) != 0)
505 return -EIO;
0f534093
RM
506 child->thread.debugreg6 = data;
507 break;
508
509 case 7:
d9771e8c
RM
510 /*
511 * Sanity-check data. Take one half-byte at once with
512 * check = (val >> (16 + 4*i)) & 0xf. It contains the
513 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
514 * 2 and 3 are LENi. Given a list of invalid values,
515 * we do mask |= 1 << invalid_value, so that
516 * (mask >> check) & 1 is a correct test for invalid
517 * values.
518 *
519 * R/Wi contains the type of the breakpoint /
520 * watchpoint, LENi contains the length of the watched
521 * data in the watchpoint case.
522 *
523 * The invalid values are:
2047b08b 524 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
d9771e8c
RM
525 * - R/Wi == 0x10 (break on I/O reads or writes), so
526 * mask |= 0x4444.
527 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
528 * 0x1110.
529 *
530 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
531 *
532 * See the Intel Manual "System Programming Guide",
533 * 15.2.4
534 *
535 * Note that LENi == 0x10 is defined on x86_64 in long
536 * mode (i.e. even for 32-bit userspace software, but
537 * 64-bit kernel), so the x86_64 mask value is 0x5454.
538 * See the AMD manual no. 24593 (AMD64 System Programming)
539 */
2047b08b
RM
540#ifdef CONFIG_X86_32
541#define DR7_MASK 0x5f54
542#else
543#define DR7_MASK 0x5554
544#endif
d9771e8c
RM
545 data &= ~DR_CONTROL_RESERVED;
546 for (i = 0; i < 4; i++)
2047b08b 547 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
d9771e8c 548 return -EIO;
0f534093 549 child->thread.debugreg7 = data;
d9771e8c
RM
550 if (data)
551 set_tsk_thread_flag(child, TIF_DEBUG);
552 else
553 clear_tsk_thread_flag(child, TIF_DEBUG);
0f534093 554 break;
d9771e8c
RM
555 }
556
d9771e8c
RM
557 return 0;
558}
559
325af5fb
RM
560/*
561 * These access the current or another (stopped) task's io permission
562 * bitmap for debugging or core dump.
563 */
564static int ioperm_active(struct task_struct *target,
565 const struct user_regset *regset)
566{
567 return target->thread.io_bitmap_max / regset->size;
568}
b4ef95de 569
325af5fb
RM
570static int ioperm_get(struct task_struct *target,
571 const struct user_regset *regset,
572 unsigned int pos, unsigned int count,
573 void *kbuf, void __user *ubuf)
eee3af4a 574{
325af5fb 575 if (!target->thread.io_bitmap_ptr)
eee3af4a
MM
576 return -ENXIO;
577
325af5fb
RM
578 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
579 target->thread.io_bitmap_ptr,
580 0, IO_BITMAP_BYTES);
581}
582
93fa7636
MM
583#ifdef CONFIG_X86_PTRACE_BTS
584/*
585 * The configuration for a particular BTS hardware implementation.
586 */
587struct bts_configuration {
588 /* the size of a BTS record in bytes; at most BTS_MAX_RECORD_SIZE */
589 unsigned char sizeof_bts;
590 /* the size of a field in the BTS record in bytes */
591 unsigned char sizeof_field;
592 /* a bitmask to enable/disable BTS in DEBUGCTL MSR */
593 unsigned long debugctl_mask;
594};
595static struct bts_configuration bts_cfg;
596
597#define BTS_MAX_RECORD_SIZE (8 * 3)
598
599
600/*
601 * Branch Trace Store (BTS) uses the following format. Different
602 * architectures vary in the size of those fields.
603 * - source linear address
604 * - destination linear address
605 * - flags
606 *
607 * Later architectures use 64bit pointers throughout, whereas earlier
608 * architectures use 32bit pointers in 32bit mode.
609 *
610 * We compute the base address for the first 8 fields based on:
611 * - the field size stored in the DS configuration
612 * - the relative field position
613 *
614 * In order to store additional information in the BTS buffer, we use
615 * a special source address to indicate that the record requires
616 * special interpretation.
617 *
618 * Netburst indicated via a bit in the flags field whether the branch
619 * was predicted; this is ignored.
620 */
621
622enum bts_field {
623 bts_from = 0,
624 bts_to,
625 bts_flags,
626
627 bts_escape = (unsigned long)-1,
628 bts_qual = bts_to,
629 bts_jiffies = bts_flags
630};
b4ef95de 631
93fa7636 632static inline unsigned long bts_get(const char *base, enum bts_field field)
eee3af4a 633{
93fa7636
MM
634 base += (bts_cfg.sizeof_field * field);
635 return *(unsigned long *)base;
636}
637
638static inline void bts_set(char *base, enum bts_field field, unsigned long val)
639{
640 base += (bts_cfg.sizeof_field * field);;
641 (*(unsigned long *)base) = val;
642}
eee3af4a 643
93fa7636
MM
644/*
645 * Translate a BTS record from the raw format into the bts_struct format
646 *
647 * out (out): bts_struct interpretation
648 * raw: raw BTS record
649 */
650static void ptrace_bts_translate_record(struct bts_struct *out, const void *raw)
651{
652 memset(out, 0, sizeof(*out));
653 if (bts_get(raw, bts_from) == bts_escape) {
654 out->qualifier = bts_get(raw, bts_qual);
655 out->variant.jiffies = bts_get(raw, bts_jiffies);
656 } else {
657 out->qualifier = BTS_BRANCH;
658 out->variant.lbr.from_ip = bts_get(raw, bts_from);
659 out->variant.lbr.to_ip = bts_get(raw, bts_to);
660 }
eee3af4a
MM
661}
662
93fa7636 663static int ptrace_bts_read_record(struct task_struct *child, size_t index,
eee3af4a
MM
664 struct bts_struct __user *out)
665{
666 struct bts_struct ret;
93fa7636
MM
667 const void *bts_record;
668 size_t bts_index, bts_end;
669 int error;
eee3af4a 670
93fa7636
MM
671 error = ds_get_bts_end(child, &bts_end);
672 if (error < 0)
673 return error;
eee3af4a 674
a95d67f8 675 if (bts_end <= index)
e4811f25
MM
676 return -EINVAL;
677
93fa7636
MM
678 error = ds_get_bts_index(child, &bts_index);
679 if (error < 0)
680 return error;
681
e4811f25 682 /* translate the ptrace bts index into the ds bts index */
93fa7636
MM
683 bts_index += bts_end - (index + 1);
684 if (bts_end <= bts_index)
685 bts_index -= bts_end;
686
687 error = ds_access_bts(child, bts_index, &bts_record);
688 if (error < 0)
689 return error;
e4811f25 690
93fa7636 691 ptrace_bts_translate_record(&ret, bts_record);
eee3af4a
MM
692
693 if (copy_to_user(out, &ret, sizeof(ret)))
694 return -EFAULT;
695
696 return sizeof(ret);
697}
698
a95d67f8 699static int ptrace_bts_drain(struct task_struct *child,
cba4b65d 700 long size,
a95d67f8
MM
701 struct bts_struct __user *out)
702{
93fa7636
MM
703 struct bts_struct ret;
704 const unsigned char *raw;
705 size_t end, i;
706 int error;
eee3af4a 707
93fa7636
MM
708 error = ds_get_bts_index(child, &end);
709 if (error < 0)
710 return error;
a95d67f8 711
cba4b65d
MM
712 if (size < (end * sizeof(struct bts_struct)))
713 return -EIO;
714
93fa7636
MM
715 error = ds_access_bts(child, 0, (const void **)&raw);
716 if (error < 0)
717 return error;
a95d67f8 718
93fa7636
MM
719 for (i = 0; i < end; i++, out++, raw += bts_cfg.sizeof_bts) {
720 ptrace_bts_translate_record(&ret, raw);
a95d67f8
MM
721
722 if (copy_to_user(out, &ret, sizeof(ret)))
723 return -EFAULT;
724 }
725
93fa7636
MM
726 error = ds_clear_bts(child);
727 if (error < 0)
728 return error;
a95d67f8 729
cba4b65d 730 return end;
a95d67f8
MM
731}
732
93fa7636
MM
733static void ptrace_bts_ovfl(struct task_struct *child)
734{
735 send_sig(child->thread.bts_ovfl_signal, child, 0);
736}
737
a95d67f8 738static int ptrace_bts_config(struct task_struct *child,
cba4b65d 739 long cfg_size,
a95d67f8
MM
740 const struct ptrace_bts_config __user *ucfg)
741{
742 struct ptrace_bts_config cfg;
93fa7636
MM
743 int error = 0;
744
745 error = -EOPNOTSUPP;
746 if (!bts_cfg.sizeof_bts)
747 goto errout;
a95d67f8 748
93fa7636 749 error = -EIO;
cba4b65d 750 if (cfg_size < sizeof(cfg))
93fa7636 751 goto errout;
cba4b65d 752
93fa7636 753 error = -EFAULT;
a95d67f8 754 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
93fa7636 755 goto errout;
a95d67f8 756
93fa7636
MM
757 error = -EINVAL;
758 if ((cfg.flags & PTRACE_BTS_O_SIGNAL) &&
759 !(cfg.flags & PTRACE_BTS_O_ALLOC))
760 goto errout;
cba4b65d 761
93fa7636 762 if (cfg.flags & PTRACE_BTS_O_ALLOC) {
493cd912 763 ds_ovfl_callback_t ovfl = NULL;
93fa7636
MM
764 unsigned int sig = 0;
765
766 /* we ignore the error in case we were not tracing child */
767 (void)ds_release_bts(child);
768
769 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
770 if (!cfg.signal)
771 goto errout;
772
773 sig = cfg.signal;
774 ovfl = ptrace_bts_ovfl;
775 }
a95d67f8 776
493cd912 777 error = ds_request_bts(child, /* base = */ NULL, cfg.size, ovfl);
93fa7636 778 if (error < 0)
da35c371 779 goto errout;
a95d67f8 780
93fa7636 781 child->thread.bts_ovfl_signal = sig;
a95d67f8
MM
782 }
783
93fa7636
MM
784 error = -EINVAL;
785 if (!child->thread.ds_ctx && cfg.flags)
da35c371 786 goto errout;
eee3af4a 787
da35c371 788 if (cfg.flags & PTRACE_BTS_O_TRACE)
93fa7636 789 child->thread.debugctlmsr |= bts_cfg.debugctl_mask;
da35c371 790 else
93fa7636 791 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
eee3af4a 792
da35c371 793 if (cfg.flags & PTRACE_BTS_O_SCHED)
eee3af4a
MM
794 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
795 else
796 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
797
93fa7636 798 error = sizeof(cfg);
cba4b65d 799
da35c371
MM
800out:
801 if (child->thread.debugctlmsr)
802 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
803 else
804 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
805
93fa7636 806 return error;
da35c371
MM
807
808errout:
93fa7636 809 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
da35c371
MM
810 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
811 goto out;
eee3af4a
MM
812}
813
a95d67f8 814static int ptrace_bts_status(struct task_struct *child,
cba4b65d 815 long cfg_size,
a95d67f8 816 struct ptrace_bts_config __user *ucfg)
eee3af4a 817{
a95d67f8 818 struct ptrace_bts_config cfg;
93fa7636
MM
819 size_t end;
820 const void *base, *max;
821 int error;
eee3af4a 822
cba4b65d
MM
823 if (cfg_size < sizeof(cfg))
824 return -EIO;
825
93fa7636
MM
826 error = ds_get_bts_end(child, &end);
827 if (error < 0)
828 return error;
eee3af4a 829
93fa7636
MM
830 error = ds_access_bts(child, /* index = */ 0, &base);
831 if (error < 0)
832 return error;
eee3af4a 833
93fa7636
MM
834 error = ds_access_bts(child, /* index = */ end, &max);
835 if (error < 0)
836 return error;
eee3af4a 837
93fa7636
MM
838 memset(&cfg, 0, sizeof(cfg));
839 cfg.size = (max - base);
840 cfg.signal = child->thread.bts_ovfl_signal;
841 cfg.bts_size = sizeof(struct bts_struct);
eee3af4a 842
93fa7636
MM
843 if (cfg.signal)
844 cfg.flags |= PTRACE_BTS_O_SIGNAL;
eee3af4a 845
93fa7636
MM
846 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
847 child->thread.debugctlmsr & bts_cfg.debugctl_mask)
848 cfg.flags |= PTRACE_BTS_O_TRACE;
849
850 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
851 cfg.flags |= PTRACE_BTS_O_SCHED;
87e8407f 852
a95d67f8
MM
853 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
854 return -EFAULT;
eee3af4a 855
a95d67f8 856 return sizeof(cfg);
eee3af4a
MM
857}
858
d8d4f157
AM
859static int ptrace_bts_write_record(struct task_struct *child,
860 const struct bts_struct *in)
861{
93fa7636 862 unsigned char bts_record[BTS_MAX_RECORD_SIZE];
d8d4f157 863
93fa7636 864 BUG_ON(BTS_MAX_RECORD_SIZE < bts_cfg.sizeof_bts);
d8d4f157 865
93fa7636
MM
866 memset(bts_record, 0, bts_cfg.sizeof_bts);
867 switch (in->qualifier) {
868 case BTS_INVALID:
869 break;
d8d4f157 870
93fa7636
MM
871 case BTS_BRANCH:
872 bts_set(bts_record, bts_from, in->variant.lbr.from_ip);
873 bts_set(bts_record, bts_to, in->variant.lbr.to_ip);
874 break;
d8d4f157 875
93fa7636
MM
876 case BTS_TASK_ARRIVES:
877 case BTS_TASK_DEPARTS:
878 bts_set(bts_record, bts_from, bts_escape);
879 bts_set(bts_record, bts_qual, in->qualifier);
880 bts_set(bts_record, bts_jiffies, in->variant.jiffies);
881 break;
d8d4f157 882
93fa7636 883 default:
d8d4f157 884 return -EINVAL;
d8d4f157
AM
885 }
886
93fa7636
MM
887 /* The writing task will be the switched-to task on a context
888 * switch. It needs to write into the switched-from task's BTS
889 * buffer. */
890 return ds_unchecked_write_bts(child, bts_record, bts_cfg.sizeof_bts);
d8d4f157
AM
891}
892
eee3af4a
MM
893void ptrace_bts_take_timestamp(struct task_struct *tsk,
894 enum bts_qualifier qualifier)
895{
896 struct bts_struct rec = {
897 .qualifier = qualifier,
da35c371 898 .variant.jiffies = jiffies_64
eee3af4a
MM
899 };
900
eee3af4a
MM
901 ptrace_bts_write_record(tsk, &rec);
902}
93fa7636
MM
903
904static const struct bts_configuration bts_cfg_netburst = {
905 .sizeof_bts = sizeof(long) * 3,
906 .sizeof_field = sizeof(long),
907 .debugctl_mask = (1<<2)|(1<<3)|(1<<5)
908};
909
910static const struct bts_configuration bts_cfg_pentium_m = {
911 .sizeof_bts = sizeof(long) * 3,
912 .sizeof_field = sizeof(long),
913 .debugctl_mask = (1<<6)|(1<<7)
914};
915
916static const struct bts_configuration bts_cfg_core2 = {
917 .sizeof_bts = 8 * 3,
918 .sizeof_field = 8,
919 .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
920};
921
922static inline void bts_configure(const struct bts_configuration *cfg)
923{
924 bts_cfg = *cfg;
925}
926
927void __cpuinit ptrace_bts_init_intel(struct cpuinfo_x86 *c)
928{
929 switch (c->x86) {
930 case 0x6:
931 switch (c->x86_model) {
f4166c54
MM
932 case 0 ... 0xC:
933 /* sorry, don't know about them */
934 break;
93fa7636
MM
935 case 0xD:
936 case 0xE: /* Pentium M */
937 bts_configure(&bts_cfg_pentium_m);
938 break;
f4166c54 939 default: /* Core2, Atom, ... */
93fa7636
MM
940 bts_configure(&bts_cfg_core2);
941 break;
93fa7636
MM
942 }
943 break;
944 case 0xF:
945 switch (c->x86_model) {
946 case 0x0:
947 case 0x1:
948 case 0x2: /* Netburst */
949 bts_configure(&bts_cfg_netburst);
950 break;
951 default:
952 /* sorry, don't know about them */
953 break;
954 }
955 break;
956 default:
957 /* sorry, don't know about them */
958 break;
959 }
960}
961#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 962
1da177e4
LT
963/*
964 * Called by kernel/ptrace.c when detaching..
965 *
966 * Make sure the single step bit is not set.
967 */
968void ptrace_disable(struct task_struct *child)
9e714bed 969{
7f232343 970 user_disable_single_step(child);
e9c86c78 971#ifdef TIF_SYSCALL_EMU
ab1c23c2 972 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
e9c86c78 973#endif
93fa7636
MM
974#ifdef CONFIG_X86_PTRACE_BTS
975 (void)ds_release_bts(child);
976
977 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
978 if (!child->thread.debugctlmsr)
979 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
980
981 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
982#endif /* CONFIG_X86_PTRACE_BTS */
1da177e4
LT
983}
984
5a4646a4
RM
985#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
986static const struct user_regset_view user_x86_32_view; /* Initialized below. */
987#endif
988
481bed45 989long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1da177e4 990{
5a4646a4 991 int ret;
1da177e4
LT
992 unsigned long __user *datap = (unsigned long __user *)data;
993
1da177e4 994 switch (request) {
1da177e4
LT
995 /* read the word at location addr in the USER area. */
996 case PTRACE_PEEKUSR: {
997 unsigned long tmp;
998
999 ret = -EIO;
e9c86c78
RM
1000 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1001 addr >= sizeof(struct user))
1da177e4
LT
1002 break;
1003
1004 tmp = 0; /* Default return condition */
e9c86c78 1005 if (addr < sizeof(struct user_regs_struct))
1da177e4 1006 tmp = getreg(child, addr);
e9c86c78
RM
1007 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1008 addr <= offsetof(struct user, u_debugreg[7])) {
1009 addr -= offsetof(struct user, u_debugreg[0]);
1010 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1da177e4
LT
1011 }
1012 ret = put_user(tmp, datap);
1013 break;
1014 }
1015
1da177e4
LT
1016 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1017 ret = -EIO;
e9c86c78
RM
1018 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1019 addr >= sizeof(struct user))
1da177e4
LT
1020 break;
1021
e9c86c78 1022 if (addr < sizeof(struct user_regs_struct))
1da177e4 1023 ret = putreg(child, addr, data);
e9c86c78
RM
1024 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1025 addr <= offsetof(struct user, u_debugreg[7])) {
1026 addr -= offsetof(struct user, u_debugreg[0]);
1027 ret = ptrace_set_debugreg(child,
1028 addr / sizeof(data), data);
1da177e4 1029 }
e9c86c78 1030 break;
1da177e4 1031
5a4646a4
RM
1032 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1033 return copy_regset_to_user(child,
1034 task_user_regset_view(current),
1035 REGSET_GENERAL,
1036 0, sizeof(struct user_regs_struct),
1037 datap);
1038
1039 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1040 return copy_regset_from_user(child,
1041 task_user_regset_view(current),
1042 REGSET_GENERAL,
1043 0, sizeof(struct user_regs_struct),
1044 datap);
1045
1046 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1047 return copy_regset_to_user(child,
1048 task_user_regset_view(current),
1049 REGSET_FP,
1050 0, sizeof(struct user_i387_struct),
1051 datap);
1052
1053 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1054 return copy_regset_from_user(child,
1055 task_user_regset_view(current),
1056 REGSET_FP,
1057 0, sizeof(struct user_i387_struct),
1058 datap);
1da177e4 1059
e9c86c78 1060#ifdef CONFIG_X86_32
5a4646a4
RM
1061 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1062 return copy_regset_to_user(child, &user_x86_32_view,
1063 REGSET_XFP,
1064 0, sizeof(struct user_fxsr_struct),
45fdc3a7 1065 datap) ? -EIO : 0;
5a4646a4
RM
1066
1067 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1068 return copy_regset_from_user(child, &user_x86_32_view,
1069 REGSET_XFP,
1070 0, sizeof(struct user_fxsr_struct),
45fdc3a7 1071 datap) ? -EIO : 0;
e9c86c78 1072#endif
1da177e4 1073
e9c86c78 1074#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1da177e4 1075 case PTRACE_GET_THREAD_AREA:
efd1ca52
RM
1076 if (addr < 0)
1077 return -EIO;
1078 ret = do_get_thread_area(child, addr,
1079 (struct user_desc __user *) data);
1da177e4
LT
1080 break;
1081
1082 case PTRACE_SET_THREAD_AREA:
efd1ca52
RM
1083 if (addr < 0)
1084 return -EIO;
1085 ret = do_set_thread_area(child, addr,
1086 (struct user_desc __user *) data, 0);
1da177e4 1087 break;
e9c86c78
RM
1088#endif
1089
1090#ifdef CONFIG_X86_64
1091 /* normal 64bit interface to access TLS data.
1092 Works just like arch_prctl, except that the arguments
1093 are reversed. */
1094 case PTRACE_ARCH_PRCTL:
1095 ret = do_arch_prctl(child, data, addr);
1096 break;
1097#endif
1da177e4 1098
b4ef95de
IM
1099 /*
1100 * These bits need more cooking - not enabled yet:
1101 */
93fa7636 1102#ifdef CONFIG_X86_PTRACE_BTS
a95d67f8
MM
1103 case PTRACE_BTS_CONFIG:
1104 ret = ptrace_bts_config
cba4b65d 1105 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
1106 break;
1107
a95d67f8
MM
1108 case PTRACE_BTS_STATUS:
1109 ret = ptrace_bts_status
cba4b65d 1110 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
1111 break;
1112
a95d67f8 1113 case PTRACE_BTS_SIZE:
493cd912 1114 ret = ds_get_bts_index(child, /* pos = */ NULL);
eee3af4a
MM
1115 break;
1116
a95d67f8 1117 case PTRACE_BTS_GET:
eee3af4a 1118 ret = ptrace_bts_read_record
a95d67f8 1119 (child, data, (struct bts_struct __user *) addr);
eee3af4a
MM
1120 break;
1121
a95d67f8 1122 case PTRACE_BTS_CLEAR:
93fa7636 1123 ret = ds_clear_bts(child);
eee3af4a
MM
1124 break;
1125
a95d67f8
MM
1126 case PTRACE_BTS_DRAIN:
1127 ret = ptrace_bts_drain
cba4b65d 1128 (child, data, (struct bts_struct __user *) addr);
eee3af4a 1129 break;
93fa7636 1130#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 1131
1da177e4
LT
1132 default:
1133 ret = ptrace_request(child, request, addr, data);
1134 break;
1135 }
d9771e8c 1136
1da177e4
LT
1137 return ret;
1138}
1139
cb757c41
RM
1140#ifdef CONFIG_IA32_EMULATION
1141
099cd6e9
RM
1142#include <linux/compat.h>
1143#include <linux/syscalls.h>
1144#include <asm/ia32.h>
cb757c41
RM
1145#include <asm/user32.h>
1146
1147#define R32(l,q) \
1148 case offsetof(struct user32, regs.l): \
1149 regs->q = value; break
1150
1151#define SEG32(rs) \
1152 case offsetof(struct user32, regs.rs): \
1153 return set_segment_reg(child, \
1154 offsetof(struct user_regs_struct, rs), \
1155 value); \
1156 break
1157
1158static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1159{
1160 struct pt_regs *regs = task_pt_regs(child);
1161
1162 switch (regno) {
1163
1164 SEG32(cs);
1165 SEG32(ds);
1166 SEG32(es);
1167 SEG32(fs);
1168 SEG32(gs);
1169 SEG32(ss);
1170
1171 R32(ebx, bx);
1172 R32(ecx, cx);
1173 R32(edx, dx);
1174 R32(edi, di);
1175 R32(esi, si);
1176 R32(ebp, bp);
1177 R32(eax, ax);
cb757c41
RM
1178 R32(eip, ip);
1179 R32(esp, sp);
1180
40f0933d
RM
1181 case offsetof(struct user32, regs.orig_eax):
1182 /*
1183 * Sign-extend the value so that orig_eax = -1
1184 * causes (long)orig_ax < 0 tests to fire correctly.
1185 */
1186 regs->orig_ax = (long) (s32) value;
1187 break;
1188
cb757c41
RM
1189 case offsetof(struct user32, regs.eflags):
1190 return set_flags(child, value);
1191
1192 case offsetof(struct user32, u_debugreg[0]) ...
1193 offsetof(struct user32, u_debugreg[7]):
1194 regno -= offsetof(struct user32, u_debugreg[0]);
1195 return ptrace_set_debugreg(child, regno / 4, value);
1196
1197 default:
1198 if (regno > sizeof(struct user32) || (regno & 3))
1199 return -EIO;
1200
1201 /*
1202 * Other dummy fields in the virtual user structure
1203 * are ignored
1204 */
1205 break;
1206 }
1207 return 0;
1208}
1209
1210#undef R32
1211#undef SEG32
1212
1213#define R32(l,q) \
1214 case offsetof(struct user32, regs.l): \
1215 *val = regs->q; break
1216
1217#define SEG32(rs) \
1218 case offsetof(struct user32, regs.rs): \
1219 *val = get_segment_reg(child, \
1220 offsetof(struct user_regs_struct, rs)); \
1221 break
1222
1223static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1224{
1225 struct pt_regs *regs = task_pt_regs(child);
1226
1227 switch (regno) {
1228
1229 SEG32(ds);
1230 SEG32(es);
1231 SEG32(fs);
1232 SEG32(gs);
1233
1234 R32(cs, cs);
1235 R32(ss, ss);
1236 R32(ebx, bx);
1237 R32(ecx, cx);
1238 R32(edx, dx);
1239 R32(edi, di);
1240 R32(esi, si);
1241 R32(ebp, bp);
1242 R32(eax, ax);
1243 R32(orig_eax, orig_ax);
1244 R32(eip, ip);
1245 R32(esp, sp);
1246
1247 case offsetof(struct user32, regs.eflags):
1248 *val = get_flags(child);
1249 break;
1250
1251 case offsetof(struct user32, u_debugreg[0]) ...
1252 offsetof(struct user32, u_debugreg[7]):
1253 regno -= offsetof(struct user32, u_debugreg[0]);
1254 *val = ptrace_get_debugreg(child, regno / 4);
1255 break;
1256
1257 default:
1258 if (regno > sizeof(struct user32) || (regno & 3))
1259 return -EIO;
1260
1261 /*
1262 * Other dummy fields in the virtual user structure
1263 * are ignored
1264 */
1265 *val = 0;
1266 break;
1267 }
1268 return 0;
1269}
1270
1271#undef R32
1272#undef SEG32
1273
91e7b707
RM
1274static int genregs32_get(struct task_struct *target,
1275 const struct user_regset *regset,
1276 unsigned int pos, unsigned int count,
1277 void *kbuf, void __user *ubuf)
1278{
1279 if (kbuf) {
1280 compat_ulong_t *k = kbuf;
1281 while (count > 0) {
1282 getreg32(target, pos, k++);
1283 count -= sizeof(*k);
1284 pos += sizeof(*k);
1285 }
1286 } else {
1287 compat_ulong_t __user *u = ubuf;
1288 while (count > 0) {
1289 compat_ulong_t word;
1290 getreg32(target, pos, &word);
1291 if (__put_user(word, u++))
1292 return -EFAULT;
1293 count -= sizeof(*u);
1294 pos += sizeof(*u);
1295 }
1296 }
1297
1298 return 0;
1299}
1300
1301static int genregs32_set(struct task_struct *target,
1302 const struct user_regset *regset,
1303 unsigned int pos, unsigned int count,
1304 const void *kbuf, const void __user *ubuf)
1305{
1306 int ret = 0;
1307 if (kbuf) {
1308 const compat_ulong_t *k = kbuf;
1309 while (count > 0 && !ret) {
f9cb02b0 1310 ret = putreg32(target, pos, *k++);
91e7b707
RM
1311 count -= sizeof(*k);
1312 pos += sizeof(*k);
1313 }
1314 } else {
1315 const compat_ulong_t __user *u = ubuf;
1316 while (count > 0 && !ret) {
1317 compat_ulong_t word;
1318 ret = __get_user(word, u++);
1319 if (ret)
1320 break;
f9cb02b0 1321 ret = putreg32(target, pos, word);
91e7b707
RM
1322 count -= sizeof(*u);
1323 pos += sizeof(*u);
1324 }
1325 }
1326 return ret;
1327}
1328
562b80ba
RM
1329long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1330 compat_ulong_t caddr, compat_ulong_t cdata)
099cd6e9 1331{
562b80ba
RM
1332 unsigned long addr = caddr;
1333 unsigned long data = cdata;
099cd6e9
RM
1334 void __user *datap = compat_ptr(data);
1335 int ret;
1336 __u32 val;
1337
099cd6e9 1338 switch (request) {
099cd6e9
RM
1339 case PTRACE_PEEKUSR:
1340 ret = getreg32(child, addr, &val);
1341 if (ret == 0)
1342 ret = put_user(val, (__u32 __user *)datap);
1343 break;
1344
1345 case PTRACE_POKEUSR:
1346 ret = putreg32(child, addr, data);
1347 break;
1348
5a4646a4
RM
1349 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1350 return copy_regset_to_user(child, &user_x86_32_view,
1351 REGSET_GENERAL,
1352 0, sizeof(struct user_regs_struct32),
1353 datap);
1354
1355 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1356 return copy_regset_from_user(child, &user_x86_32_view,
1357 REGSET_GENERAL, 0,
1358 sizeof(struct user_regs_struct32),
1359 datap);
1360
1361 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1362 return copy_regset_to_user(child, &user_x86_32_view,
1363 REGSET_FP, 0,
1364 sizeof(struct user_i387_ia32_struct),
1365 datap);
1366
1367 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1368 return copy_regset_from_user(
1369 child, &user_x86_32_view, REGSET_FP,
1370 0, sizeof(struct user_i387_ia32_struct), datap);
1371
1372 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1373 return copy_regset_to_user(child, &user_x86_32_view,
1374 REGSET_XFP, 0,
1375 sizeof(struct user32_fxsr_struct),
1376 datap);
1377
1378 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1379 return copy_regset_from_user(child, &user_x86_32_view,
1380 REGSET_XFP, 0,
1381 sizeof(struct user32_fxsr_struct),
1382 datap);
099cd6e9 1383
562b80ba
RM
1384 case PTRACE_GET_THREAD_AREA:
1385 case PTRACE_SET_THREAD_AREA:
1386 return arch_ptrace(child, request, addr, data);
1387
099cd6e9 1388 default:
fdadd54d 1389 return compat_ptrace_request(child, request, addr, data);
099cd6e9
RM
1390 }
1391
099cd6e9
RM
1392 return ret;
1393}
1394
cb757c41
RM
1395#endif /* CONFIG_IA32_EMULATION */
1396
070459d9
RM
1397#ifdef CONFIG_X86_64
1398
1399static const struct user_regset x86_64_regsets[] = {
1400 [REGSET_GENERAL] = {
1401 .core_note_type = NT_PRSTATUS,
1402 .n = sizeof(struct user_regs_struct) / sizeof(long),
1403 .size = sizeof(long), .align = sizeof(long),
1404 .get = genregs_get, .set = genregs_set
1405 },
1406 [REGSET_FP] = {
1407 .core_note_type = NT_PRFPREG,
1408 .n = sizeof(struct user_i387_struct) / sizeof(long),
1409 .size = sizeof(long), .align = sizeof(long),
1410 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1411 },
325af5fb
RM
1412 [REGSET_IOPERM64] = {
1413 .core_note_type = NT_386_IOPERM,
1414 .n = IO_BITMAP_LONGS,
1415 .size = sizeof(long), .align = sizeof(long),
1416 .active = ioperm_active, .get = ioperm_get
1417 },
070459d9
RM
1418};
1419
1420static const struct user_regset_view user_x86_64_view = {
1421 .name = "x86_64", .e_machine = EM_X86_64,
1422 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1423};
1424
1425#else /* CONFIG_X86_32 */
1426
1427#define user_regs_struct32 user_regs_struct
1428#define genregs32_get genregs_get
1429#define genregs32_set genregs_set
1430
1f465f4e
RM
1431#define user_i387_ia32_struct user_i387_struct
1432#define user32_fxsr_struct user_fxsr_struct
1433
070459d9
RM
1434#endif /* CONFIG_X86_64 */
1435
1436#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1437static const struct user_regset x86_32_regsets[] = {
1438 [REGSET_GENERAL] = {
1439 .core_note_type = NT_PRSTATUS,
1440 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1441 .size = sizeof(u32), .align = sizeof(u32),
1442 .get = genregs32_get, .set = genregs32_set
1443 },
1444 [REGSET_FP] = {
1445 .core_note_type = NT_PRFPREG,
1f465f4e 1446 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
070459d9
RM
1447 .size = sizeof(u32), .align = sizeof(u32),
1448 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1449 },
1450 [REGSET_XFP] = {
1451 .core_note_type = NT_PRXFPREG,
1f465f4e 1452 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
070459d9
RM
1453 .size = sizeof(u32), .align = sizeof(u32),
1454 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1455 },
1456 [REGSET_TLS] = {
bb61682b 1457 .core_note_type = NT_386_TLS,
070459d9
RM
1458 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1459 .size = sizeof(struct user_desc),
1460 .align = sizeof(struct user_desc),
1461 .active = regset_tls_active,
1462 .get = regset_tls_get, .set = regset_tls_set
1463 },
325af5fb
RM
1464 [REGSET_IOPERM32] = {
1465 .core_note_type = NT_386_IOPERM,
1466 .n = IO_BITMAP_BYTES / sizeof(u32),
1467 .size = sizeof(u32), .align = sizeof(u32),
1468 .active = ioperm_active, .get = ioperm_get
1469 },
070459d9
RM
1470};
1471
1472static const struct user_regset_view user_x86_32_view = {
1473 .name = "i386", .e_machine = EM_386,
1474 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1475};
1476#endif
1477
1478const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1479{
1480#ifdef CONFIG_IA32_EMULATION
1481 if (test_tsk_thread_flag(task, TIF_IA32))
1482#endif
1483#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1484 return &user_x86_32_view;
1485#endif
1486#ifdef CONFIG_X86_64
1487 return &user_x86_64_view;
1488#endif
1489}
1490
da654b74
SD
1491void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1492 int error_code, int si_code)
1da177e4
LT
1493{
1494 struct siginfo info;
1495
1496 tsk->thread.trap_no = 1;
1497 tsk->thread.error_code = error_code;
1498
1499 memset(&info, 0, sizeof(info));
1500 info.si_signo = SIGTRAP;
da654b74 1501 info.si_code = si_code;
1da177e4 1502
65ea5b03
PA
1503 /* User-mode ip? */
1504 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1da177e4 1505
27b46d76 1506 /* Send us the fake SIGTRAP */
1da177e4
LT
1507 force_sig_info(SIGTRAP, &info, tsk);
1508}
1509
86976cd8 1510
d4d67150
RM
1511#ifdef CONFIG_X86_32
1512# define IS_IA32 1
1513#elif defined CONFIG_IA32_EMULATION
1514# define IS_IA32 test_thread_flag(TIF_IA32)
1515#else
1516# define IS_IA32 0
1517#endif
1518
1519/*
1520 * We must return the syscall number to actually look up in the table.
1521 * This can be -1L to skip running any syscall at all.
1522 */
1523asmregparm long syscall_trace_enter(struct pt_regs *regs)
86976cd8 1524{
d4d67150
RM
1525 long ret = 0;
1526
380fdd75
RM
1527 /*
1528 * If we stepped into a sysenter/syscall insn, it trapped in
1529 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1530 * If user-mode had set TF itself, then it's still clear from
1531 * do_debug() and we need to set it again to restore the user
1532 * state. If we entered on the slow path, TF was already set.
1533 */
1534 if (test_thread_flag(TIF_SINGLESTEP))
1535 regs->flags |= X86_EFLAGS_TF;
1536
86976cd8
RM
1537 /* do the secure computing check first */
1538 secure_computing(regs->orig_ax);
1539
d4d67150
RM
1540 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1541 ret = -1L;
1542
eeea3c3f
RM
1543 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1544 tracehook_report_syscall_entry(regs))
1545 ret = -1L;
86976cd8
RM
1546
1547 if (unlikely(current->audit_context)) {
d4d67150 1548 if (IS_IA32)
86976cd8
RM
1549 audit_syscall_entry(AUDIT_ARCH_I386,
1550 regs->orig_ax,
1551 regs->bx, regs->cx,
1552 regs->dx, regs->si);
d4d67150
RM
1553#ifdef CONFIG_X86_64
1554 else
86976cd8
RM
1555 audit_syscall_entry(AUDIT_ARCH_X86_64,
1556 regs->orig_ax,
1557 regs->di, regs->si,
1558 regs->dx, regs->r10);
d4d67150 1559#endif
86976cd8 1560 }
d4d67150
RM
1561
1562 return ret ?: regs->orig_ax;
86976cd8
RM
1563}
1564
d4d67150 1565asmregparm void syscall_trace_leave(struct pt_regs *regs)
86976cd8
RM
1566{
1567 if (unlikely(current->audit_context))
1568 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1569
d4d67150 1570 if (test_thread_flag(TIF_SYSCALL_TRACE))
eeea3c3f 1571 tracehook_report_syscall_exit(regs, 0);
86976cd8 1572
d4d67150
RM
1573 /*
1574 * If TIF_SYSCALL_EMU is set, we only get here because of
1575 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1576 * We already reported this syscall instruction in
1577 * syscall_trace_enter(), so don't do any more now.
1578 */
1579 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1580 return;
1581
1582 /*
1583 * If we are single-stepping, synthesize a trap to follow the
1584 * system call instruction.
1585 */
1586 if (test_thread_flag(TIF_SINGLESTEP) &&
eeea3c3f 1587 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
da654b74 1588 send_sigtrap(current, regs, 0, TRAP_BRKPT);
d4d67150 1589}