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