x86, bts: add fork and exit handling
[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,
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 583#ifdef CONFIG_X86_PTRACE_BTS
93fa7636 584static int ptrace_bts_read_record(struct task_struct *child, size_t index,
eee3af4a
MM
585 struct bts_struct __user *out)
586{
c2724775
MM
587 const struct bts_trace *trace;
588 struct bts_struct bts;
589 const unsigned char *at;
93fa7636 590 int error;
eee3af4a 591
c2724775
MM
592 trace = ds_read_bts(child->bts);
593 if (!trace)
594 return -EPERM;
e4811f25 595
c2724775
MM
596 at = trace->ds.top - ((index + 1) * trace->ds.size);
597 if ((void *)at < trace->ds.begin)
598 at += (trace->ds.n * trace->ds.size);
93fa7636 599
c2724775
MM
600 if (!trace->read)
601 return -EOPNOTSUPP;
93fa7636 602
c2724775 603 error = trace->read(child->bts, at, &bts);
93fa7636
MM
604 if (error < 0)
605 return error;
e4811f25 606
c2724775 607 if (copy_to_user(out, &bts, sizeof(bts)))
eee3af4a
MM
608 return -EFAULT;
609
c2724775 610 return sizeof(bts);
eee3af4a
MM
611}
612
a95d67f8 613static int ptrace_bts_drain(struct task_struct *child,
cba4b65d 614 long size,
a95d67f8
MM
615 struct bts_struct __user *out)
616{
c2724775
MM
617 const struct bts_trace *trace;
618 const unsigned char *at;
619 int error, drained = 0;
eee3af4a 620
c2724775
MM
621 trace = ds_read_bts(child->bts);
622 if (!trace)
623 return -EPERM;
a95d67f8 624
c2724775
MM
625 if (!trace->read)
626 return -EOPNOTSUPP;
627
628 if (size < (trace->ds.top - trace->ds.begin))
cba4b65d
MM
629 return -EIO;
630
c2724775
MM
631 for (at = trace->ds.begin; (void *)at < trace->ds.top;
632 out++, drained++, at += trace->ds.size) {
633 struct bts_struct bts;
634 int error;
a95d67f8 635
c2724775
MM
636 error = trace->read(child->bts, at, &bts);
637 if (error < 0)
638 return error;
a95d67f8 639
c2724775 640 if (copy_to_user(out, &bts, sizeof(bts)))
a95d67f8
MM
641 return -EFAULT;
642 }
643
c2724775
MM
644 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
645
646 error = ds_reset_bts(child->bts);
93fa7636
MM
647 if (error < 0)
648 return error;
a95d67f8 649
c2724775 650 return drained;
a95d67f8
MM
651}
652
653static int ptrace_bts_config(struct task_struct *child,
cba4b65d 654 long cfg_size,
a95d67f8
MM
655 const struct ptrace_bts_config __user *ucfg)
656{
657 struct ptrace_bts_config cfg;
c2724775 658 unsigned int flags = 0;
a95d67f8 659
cba4b65d 660 if (cfg_size < sizeof(cfg))
c2724775 661 return -EIO;
cba4b65d 662
a95d67f8 663 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
c2724775 664 return -EFAULT;
6abb11ae 665
c2724775
MM
666 if (child->bts) {
667 ds_release_bts(child->bts);
668 child->bts = NULL;
669 }
93fa7636 670
c2724775
MM
671 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
672 if (!cfg.signal)
673 return -EINVAL;
ca0002a1 674
c2724775 675 return -EOPNOTSUPP;
a95d67f8 676
c2724775
MM
677 child->thread.bts_ovfl_signal = cfg.signal;
678 }
6abb11ae 679
c2724775
MM
680 if ((cfg.flags & PTRACE_BTS_O_ALLOC) &&
681 (cfg.size != child->bts_size)) {
682 kfree(child->bts_buffer);
6abb11ae 683
c2724775 684 child->bts_size = cfg.size;
6abb11ae 685 child->bts_buffer = kzalloc(cfg.size, GFP_KERNEL);
c2724775
MM
686 if (!child->bts_buffer) {
687 child->bts_size = 0;
688 return -ENOMEM;
ca0002a1 689 }
a95d67f8
MM
690 }
691
da35c371 692 if (cfg.flags & PTRACE_BTS_O_TRACE)
c2724775 693 flags |= BTS_USER;
eee3af4a 694
da35c371 695 if (cfg.flags & PTRACE_BTS_O_SCHED)
c2724775 696 flags |= BTS_TIMESTAMPS;
eee3af4a 697
c2724775
MM
698 child->bts = ds_request_bts(child, child->bts_buffer, child->bts_size,
699 /* ovfl = */ NULL, /* th = */ (size_t)-1,
700 flags);
701 if (IS_ERR(child->bts)) {
702 int error = PTR_ERR(child->bts);
cba4b65d 703
c2724775
MM
704 kfree(child->bts_buffer);
705 child->bts = NULL;
706 child->bts_buffer = NULL;
707 child->bts_size = 0;
da35c371 708
c2724775
MM
709 return error;
710 }
da35c371 711
c2724775 712 return sizeof(cfg);
eee3af4a
MM
713}
714
a95d67f8 715static int ptrace_bts_status(struct task_struct *child,
cba4b65d 716 long cfg_size,
a95d67f8 717 struct ptrace_bts_config __user *ucfg)
eee3af4a 718{
c2724775 719 const struct bts_trace *trace;
a95d67f8 720 struct ptrace_bts_config cfg;
eee3af4a 721
cba4b65d
MM
722 if (cfg_size < sizeof(cfg))
723 return -EIO;
724
c2724775
MM
725 trace = ds_read_bts(child->bts);
726 if (!trace)
727 return -EPERM;
eee3af4a 728
93fa7636 729 memset(&cfg, 0, sizeof(cfg));
c2724775 730 cfg.size = trace->ds.end - trace->ds.begin;
93fa7636
MM
731 cfg.signal = child->thread.bts_ovfl_signal;
732 cfg.bts_size = sizeof(struct bts_struct);
eee3af4a 733
93fa7636
MM
734 if (cfg.signal)
735 cfg.flags |= PTRACE_BTS_O_SIGNAL;
eee3af4a 736
c2724775 737 if (trace->ds.flags & BTS_USER)
93fa7636
MM
738 cfg.flags |= PTRACE_BTS_O_TRACE;
739
c2724775 740 if (trace->ds.flags & BTS_TIMESTAMPS)
93fa7636 741 cfg.flags |= PTRACE_BTS_O_SCHED;
87e8407f 742
a95d67f8
MM
743 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
744 return -EFAULT;
eee3af4a 745
a95d67f8 746 return sizeof(cfg);
eee3af4a
MM
747}
748
c2724775 749static int ptrace_bts_clear(struct task_struct *child)
d8d4f157 750{
c2724775 751 const struct bts_trace *trace;
d8d4f157 752
c2724775
MM
753 trace = ds_read_bts(child->bts);
754 if (!trace)
755 return -EPERM;
d8d4f157 756
c2724775 757 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
d8d4f157 758
c2724775 759 return ds_reset_bts(child->bts);
d8d4f157
AM
760}
761
c2724775 762static int ptrace_bts_size(struct task_struct *child)
eee3af4a 763{
c2724775 764 const struct bts_trace *trace;
93fa7636 765
c2724775
MM
766 trace = ds_read_bts(child->bts);
767 if (!trace)
768 return -EPERM;
93fa7636 769
c2724775 770 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
93fa7636 771}
bf53de90
MM
772
773static void ptrace_bts_fork(struct task_struct *tsk)
774{
775 tsk->bts = NULL;
776 tsk->bts_buffer = NULL;
777 tsk->bts_size = 0;
778 tsk->thread.bts_ovfl_signal = 0;
779}
780
781static void ptrace_bts_untrace(struct task_struct *child)
782{
783 if (unlikely(child->bts)) {
784 ds_release_bts(child->bts);
785 child->bts = NULL;
786
787 kfree(child->bts_buffer);
788 child->bts_buffer = NULL;
789 child->bts_size = 0;
790 }
791}
792
793static void ptrace_bts_detach(struct task_struct *child)
794{
795 ptrace_bts_untrace(child);
796}
797#else
798static inline void ptrace_bts_fork(struct task_struct *tsk) {}
799static inline void ptrace_bts_detach(struct task_struct *child) {}
800static inline void ptrace_bts_untrace(struct task_struct *child) {}
93fa7636 801#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 802
bf53de90
MM
803void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
804{
805 ptrace_bts_fork(child);
806}
807
808void x86_ptrace_untrace(struct task_struct *child)
809{
810 ptrace_bts_untrace(child);
811}
812
1da177e4
LT
813/*
814 * Called by kernel/ptrace.c when detaching..
815 *
816 * Make sure the single step bit is not set.
817 */
818void ptrace_disable(struct task_struct *child)
9e714bed 819{
7f232343 820 user_disable_single_step(child);
e9c86c78 821#ifdef TIF_SYSCALL_EMU
ab1c23c2 822 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
e9c86c78 823#endif
bf53de90 824 ptrace_bts_detach(child);
1da177e4
LT
825}
826
5a4646a4
RM
827#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
828static const struct user_regset_view user_x86_32_view; /* Initialized below. */
829#endif
830
481bed45 831long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1da177e4 832{
5a4646a4 833 int ret;
1da177e4
LT
834 unsigned long __user *datap = (unsigned long __user *)data;
835
1da177e4 836 switch (request) {
1da177e4
LT
837 /* read the word at location addr in the USER area. */
838 case PTRACE_PEEKUSR: {
839 unsigned long tmp;
840
841 ret = -EIO;
e9c86c78
RM
842 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
843 addr >= sizeof(struct user))
1da177e4
LT
844 break;
845
846 tmp = 0; /* Default return condition */
e9c86c78 847 if (addr < sizeof(struct user_regs_struct))
1da177e4 848 tmp = getreg(child, addr);
e9c86c78
RM
849 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
850 addr <= offsetof(struct user, u_debugreg[7])) {
851 addr -= offsetof(struct user, u_debugreg[0]);
852 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1da177e4
LT
853 }
854 ret = put_user(tmp, datap);
855 break;
856 }
857
1da177e4
LT
858 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
859 ret = -EIO;
e9c86c78
RM
860 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
861 addr >= sizeof(struct user))
1da177e4
LT
862 break;
863
e9c86c78 864 if (addr < sizeof(struct user_regs_struct))
1da177e4 865 ret = putreg(child, addr, data);
e9c86c78
RM
866 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
867 addr <= offsetof(struct user, u_debugreg[7])) {
868 addr -= offsetof(struct user, u_debugreg[0]);
869 ret = ptrace_set_debugreg(child,
870 addr / sizeof(data), data);
1da177e4 871 }
e9c86c78 872 break;
1da177e4 873
5a4646a4
RM
874 case PTRACE_GETREGS: /* Get all gp regs from the child. */
875 return copy_regset_to_user(child,
876 task_user_regset_view(current),
877 REGSET_GENERAL,
878 0, sizeof(struct user_regs_struct),
879 datap);
880
881 case PTRACE_SETREGS: /* Set all gp regs in the child. */
882 return copy_regset_from_user(child,
883 task_user_regset_view(current),
884 REGSET_GENERAL,
885 0, sizeof(struct user_regs_struct),
886 datap);
887
888 case PTRACE_GETFPREGS: /* Get the child FPU state. */
889 return copy_regset_to_user(child,
890 task_user_regset_view(current),
891 REGSET_FP,
892 0, sizeof(struct user_i387_struct),
893 datap);
894
895 case PTRACE_SETFPREGS: /* Set the child FPU state. */
896 return copy_regset_from_user(child,
897 task_user_regset_view(current),
898 REGSET_FP,
899 0, sizeof(struct user_i387_struct),
900 datap);
1da177e4 901
e9c86c78 902#ifdef CONFIG_X86_32
5a4646a4
RM
903 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
904 return copy_regset_to_user(child, &user_x86_32_view,
905 REGSET_XFP,
906 0, sizeof(struct user_fxsr_struct),
45fdc3a7 907 datap) ? -EIO : 0;
5a4646a4
RM
908
909 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
910 return copy_regset_from_user(child, &user_x86_32_view,
911 REGSET_XFP,
912 0, sizeof(struct user_fxsr_struct),
45fdc3a7 913 datap) ? -EIO : 0;
e9c86c78 914#endif
1da177e4 915
e9c86c78 916#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1da177e4 917 case PTRACE_GET_THREAD_AREA:
efd1ca52
RM
918 if (addr < 0)
919 return -EIO;
920 ret = do_get_thread_area(child, addr,
921 (struct user_desc __user *) data);
1da177e4
LT
922 break;
923
924 case PTRACE_SET_THREAD_AREA:
efd1ca52
RM
925 if (addr < 0)
926 return -EIO;
927 ret = do_set_thread_area(child, addr,
928 (struct user_desc __user *) data, 0);
1da177e4 929 break;
e9c86c78
RM
930#endif
931
932#ifdef CONFIG_X86_64
933 /* normal 64bit interface to access TLS data.
934 Works just like arch_prctl, except that the arguments
935 are reversed. */
936 case PTRACE_ARCH_PRCTL:
937 ret = do_arch_prctl(child, data, addr);
938 break;
939#endif
1da177e4 940
b4ef95de
IM
941 /*
942 * These bits need more cooking - not enabled yet:
943 */
93fa7636 944#ifdef CONFIG_X86_PTRACE_BTS
a95d67f8
MM
945 case PTRACE_BTS_CONFIG:
946 ret = ptrace_bts_config
cba4b65d 947 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
948 break;
949
a95d67f8
MM
950 case PTRACE_BTS_STATUS:
951 ret = ptrace_bts_status
cba4b65d 952 (child, data, (struct ptrace_bts_config __user *)addr);
eee3af4a
MM
953 break;
954
c2724775
MM
955 case PTRACE_BTS_SIZE:
956 ret = ptrace_bts_size(child);
eee3af4a
MM
957 break;
958
a95d67f8 959 case PTRACE_BTS_GET:
eee3af4a 960 ret = ptrace_bts_read_record
a95d67f8 961 (child, data, (struct bts_struct __user *) addr);
eee3af4a
MM
962 break;
963
a95d67f8 964 case PTRACE_BTS_CLEAR:
c2724775 965 ret = ptrace_bts_clear(child);
eee3af4a
MM
966 break;
967
a95d67f8
MM
968 case PTRACE_BTS_DRAIN:
969 ret = ptrace_bts_drain
cba4b65d 970 (child, data, (struct bts_struct __user *) addr);
eee3af4a 971 break;
93fa7636 972#endif /* CONFIG_X86_PTRACE_BTS */
eee3af4a 973
1da177e4
LT
974 default:
975 ret = ptrace_request(child, request, addr, data);
976 break;
977 }
d9771e8c 978
1da177e4
LT
979 return ret;
980}
981
cb757c41
RM
982#ifdef CONFIG_IA32_EMULATION
983
099cd6e9
RM
984#include <linux/compat.h>
985#include <linux/syscalls.h>
986#include <asm/ia32.h>
cb757c41
RM
987#include <asm/user32.h>
988
989#define R32(l,q) \
990 case offsetof(struct user32, regs.l): \
991 regs->q = value; break
992
993#define SEG32(rs) \
994 case offsetof(struct user32, regs.rs): \
995 return set_segment_reg(child, \
996 offsetof(struct user_regs_struct, rs), \
997 value); \
998 break
999
1000static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1001{
1002 struct pt_regs *regs = task_pt_regs(child);
1003
1004 switch (regno) {
1005
1006 SEG32(cs);
1007 SEG32(ds);
1008 SEG32(es);
1009 SEG32(fs);
1010 SEG32(gs);
1011 SEG32(ss);
1012
1013 R32(ebx, bx);
1014 R32(ecx, cx);
1015 R32(edx, dx);
1016 R32(edi, di);
1017 R32(esi, si);
1018 R32(ebp, bp);
1019 R32(eax, ax);
cb757c41
RM
1020 R32(eip, ip);
1021 R32(esp, sp);
1022
40f0933d
RM
1023 case offsetof(struct user32, regs.orig_eax):
1024 /*
1025 * Sign-extend the value so that orig_eax = -1
1026 * causes (long)orig_ax < 0 tests to fire correctly.
1027 */
1028 regs->orig_ax = (long) (s32) value;
1029 break;
1030
cb757c41
RM
1031 case offsetof(struct user32, regs.eflags):
1032 return set_flags(child, value);
1033
1034 case offsetof(struct user32, u_debugreg[0]) ...
1035 offsetof(struct user32, u_debugreg[7]):
1036 regno -= offsetof(struct user32, u_debugreg[0]);
1037 return ptrace_set_debugreg(child, regno / 4, value);
1038
1039 default:
1040 if (regno > sizeof(struct user32) || (regno & 3))
1041 return -EIO;
1042
1043 /*
1044 * Other dummy fields in the virtual user structure
1045 * are ignored
1046 */
1047 break;
1048 }
1049 return 0;
1050}
1051
1052#undef R32
1053#undef SEG32
1054
1055#define R32(l,q) \
1056 case offsetof(struct user32, regs.l): \
1057 *val = regs->q; break
1058
1059#define SEG32(rs) \
1060 case offsetof(struct user32, regs.rs): \
1061 *val = get_segment_reg(child, \
1062 offsetof(struct user_regs_struct, rs)); \
1063 break
1064
1065static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1066{
1067 struct pt_regs *regs = task_pt_regs(child);
1068
1069 switch (regno) {
1070
1071 SEG32(ds);
1072 SEG32(es);
1073 SEG32(fs);
1074 SEG32(gs);
1075
1076 R32(cs, cs);
1077 R32(ss, ss);
1078 R32(ebx, bx);
1079 R32(ecx, cx);
1080 R32(edx, dx);
1081 R32(edi, di);
1082 R32(esi, si);
1083 R32(ebp, bp);
1084 R32(eax, ax);
1085 R32(orig_eax, orig_ax);
1086 R32(eip, ip);
1087 R32(esp, sp);
1088
1089 case offsetof(struct user32, regs.eflags):
1090 *val = get_flags(child);
1091 break;
1092
1093 case offsetof(struct user32, u_debugreg[0]) ...
1094 offsetof(struct user32, u_debugreg[7]):
1095 regno -= offsetof(struct user32, u_debugreg[0]);
1096 *val = ptrace_get_debugreg(child, regno / 4);
1097 break;
1098
1099 default:
1100 if (regno > sizeof(struct user32) || (regno & 3))
1101 return -EIO;
1102
1103 /*
1104 * Other dummy fields in the virtual user structure
1105 * are ignored
1106 */
1107 *val = 0;
1108 break;
1109 }
1110 return 0;
1111}
1112
1113#undef R32
1114#undef SEG32
1115
91e7b707
RM
1116static int genregs32_get(struct task_struct *target,
1117 const struct user_regset *regset,
1118 unsigned int pos, unsigned int count,
1119 void *kbuf, void __user *ubuf)
1120{
1121 if (kbuf) {
1122 compat_ulong_t *k = kbuf;
1123 while (count > 0) {
1124 getreg32(target, pos, k++);
1125 count -= sizeof(*k);
1126 pos += sizeof(*k);
1127 }
1128 } else {
1129 compat_ulong_t __user *u = ubuf;
1130 while (count > 0) {
1131 compat_ulong_t word;
1132 getreg32(target, pos, &word);
1133 if (__put_user(word, u++))
1134 return -EFAULT;
1135 count -= sizeof(*u);
1136 pos += sizeof(*u);
1137 }
1138 }
1139
1140 return 0;
1141}
1142
1143static int genregs32_set(struct task_struct *target,
1144 const struct user_regset *regset,
1145 unsigned int pos, unsigned int count,
1146 const void *kbuf, const void __user *ubuf)
1147{
1148 int ret = 0;
1149 if (kbuf) {
1150 const compat_ulong_t *k = kbuf;
1151 while (count > 0 && !ret) {
f9cb02b0 1152 ret = putreg32(target, pos, *k++);
91e7b707
RM
1153 count -= sizeof(*k);
1154 pos += sizeof(*k);
1155 }
1156 } else {
1157 const compat_ulong_t __user *u = ubuf;
1158 while (count > 0 && !ret) {
1159 compat_ulong_t word;
1160 ret = __get_user(word, u++);
1161 if (ret)
1162 break;
f9cb02b0 1163 ret = putreg32(target, pos, word);
91e7b707
RM
1164 count -= sizeof(*u);
1165 pos += sizeof(*u);
1166 }
1167 }
1168 return ret;
1169}
1170
562b80ba
RM
1171long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1172 compat_ulong_t caddr, compat_ulong_t cdata)
099cd6e9 1173{
562b80ba
RM
1174 unsigned long addr = caddr;
1175 unsigned long data = cdata;
099cd6e9
RM
1176 void __user *datap = compat_ptr(data);
1177 int ret;
1178 __u32 val;
1179
099cd6e9 1180 switch (request) {
099cd6e9
RM
1181 case PTRACE_PEEKUSR:
1182 ret = getreg32(child, addr, &val);
1183 if (ret == 0)
1184 ret = put_user(val, (__u32 __user *)datap);
1185 break;
1186
1187 case PTRACE_POKEUSR:
1188 ret = putreg32(child, addr, data);
1189 break;
1190
5a4646a4
RM
1191 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1192 return copy_regset_to_user(child, &user_x86_32_view,
1193 REGSET_GENERAL,
1194 0, sizeof(struct user_regs_struct32),
1195 datap);
1196
1197 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1198 return copy_regset_from_user(child, &user_x86_32_view,
1199 REGSET_GENERAL, 0,
1200 sizeof(struct user_regs_struct32),
1201 datap);
1202
1203 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1204 return copy_regset_to_user(child, &user_x86_32_view,
1205 REGSET_FP, 0,
1206 sizeof(struct user_i387_ia32_struct),
1207 datap);
1208
1209 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1210 return copy_regset_from_user(
1211 child, &user_x86_32_view, REGSET_FP,
1212 0, sizeof(struct user_i387_ia32_struct), datap);
1213
1214 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1215 return copy_regset_to_user(child, &user_x86_32_view,
1216 REGSET_XFP, 0,
1217 sizeof(struct user32_fxsr_struct),
1218 datap);
1219
1220 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1221 return copy_regset_from_user(child, &user_x86_32_view,
1222 REGSET_XFP, 0,
1223 sizeof(struct user32_fxsr_struct),
1224 datap);
099cd6e9 1225
562b80ba
RM
1226 case PTRACE_GET_THREAD_AREA:
1227 case PTRACE_SET_THREAD_AREA:
c2724775
MM
1228#ifdef CONFIG_X86_PTRACE_BTS
1229 case PTRACE_BTS_CONFIG:
1230 case PTRACE_BTS_STATUS:
1231 case PTRACE_BTS_SIZE:
1232 case PTRACE_BTS_GET:
1233 case PTRACE_BTS_CLEAR:
1234 case PTRACE_BTS_DRAIN:
1235#endif /* CONFIG_X86_PTRACE_BTS */
562b80ba
RM
1236 return arch_ptrace(child, request, addr, data);
1237
099cd6e9 1238 default:
fdadd54d 1239 return compat_ptrace_request(child, request, addr, data);
099cd6e9
RM
1240 }
1241
099cd6e9
RM
1242 return ret;
1243}
1244
cb757c41
RM
1245#endif /* CONFIG_IA32_EMULATION */
1246
070459d9
RM
1247#ifdef CONFIG_X86_64
1248
1249static const struct user_regset x86_64_regsets[] = {
1250 [REGSET_GENERAL] = {
1251 .core_note_type = NT_PRSTATUS,
1252 .n = sizeof(struct user_regs_struct) / sizeof(long),
1253 .size = sizeof(long), .align = sizeof(long),
1254 .get = genregs_get, .set = genregs_set
1255 },
1256 [REGSET_FP] = {
1257 .core_note_type = NT_PRFPREG,
1258 .n = sizeof(struct user_i387_struct) / sizeof(long),
1259 .size = sizeof(long), .align = sizeof(long),
1260 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1261 },
325af5fb
RM
1262 [REGSET_IOPERM64] = {
1263 .core_note_type = NT_386_IOPERM,
1264 .n = IO_BITMAP_LONGS,
1265 .size = sizeof(long), .align = sizeof(long),
1266 .active = ioperm_active, .get = ioperm_get
1267 },
070459d9
RM
1268};
1269
1270static const struct user_regset_view user_x86_64_view = {
1271 .name = "x86_64", .e_machine = EM_X86_64,
1272 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1273};
1274
1275#else /* CONFIG_X86_32 */
1276
1277#define user_regs_struct32 user_regs_struct
1278#define genregs32_get genregs_get
1279#define genregs32_set genregs_set
1280
1f465f4e
RM
1281#define user_i387_ia32_struct user_i387_struct
1282#define user32_fxsr_struct user_fxsr_struct
1283
070459d9
RM
1284#endif /* CONFIG_X86_64 */
1285
1286#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1287static const struct user_regset x86_32_regsets[] = {
1288 [REGSET_GENERAL] = {
1289 .core_note_type = NT_PRSTATUS,
1290 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1291 .size = sizeof(u32), .align = sizeof(u32),
1292 .get = genregs32_get, .set = genregs32_set
1293 },
1294 [REGSET_FP] = {
1295 .core_note_type = NT_PRFPREG,
1f465f4e 1296 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
070459d9
RM
1297 .size = sizeof(u32), .align = sizeof(u32),
1298 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1299 },
1300 [REGSET_XFP] = {
1301 .core_note_type = NT_PRXFPREG,
1f465f4e 1302 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
070459d9
RM
1303 .size = sizeof(u32), .align = sizeof(u32),
1304 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1305 },
1306 [REGSET_TLS] = {
bb61682b 1307 .core_note_type = NT_386_TLS,
070459d9
RM
1308 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1309 .size = sizeof(struct user_desc),
1310 .align = sizeof(struct user_desc),
1311 .active = regset_tls_active,
1312 .get = regset_tls_get, .set = regset_tls_set
1313 },
325af5fb
RM
1314 [REGSET_IOPERM32] = {
1315 .core_note_type = NT_386_IOPERM,
1316 .n = IO_BITMAP_BYTES / sizeof(u32),
1317 .size = sizeof(u32), .align = sizeof(u32),
1318 .active = ioperm_active, .get = ioperm_get
1319 },
070459d9
RM
1320};
1321
1322static const struct user_regset_view user_x86_32_view = {
1323 .name = "i386", .e_machine = EM_386,
1324 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1325};
1326#endif
1327
1328const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1329{
1330#ifdef CONFIG_IA32_EMULATION
1331 if (test_tsk_thread_flag(task, TIF_IA32))
1332#endif
1333#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1334 return &user_x86_32_view;
1335#endif
1336#ifdef CONFIG_X86_64
1337 return &user_x86_64_view;
1338#endif
1339}
1340
da654b74
SD
1341void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1342 int error_code, int si_code)
1da177e4
LT
1343{
1344 struct siginfo info;
1345
1346 tsk->thread.trap_no = 1;
1347 tsk->thread.error_code = error_code;
1348
1349 memset(&info, 0, sizeof(info));
1350 info.si_signo = SIGTRAP;
da654b74 1351 info.si_code = si_code;
1da177e4 1352
65ea5b03
PA
1353 /* User-mode ip? */
1354 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1da177e4 1355
27b46d76 1356 /* Send us the fake SIGTRAP */
1da177e4
LT
1357 force_sig_info(SIGTRAP, &info, tsk);
1358}
1359
86976cd8 1360
d4d67150
RM
1361#ifdef CONFIG_X86_32
1362# define IS_IA32 1
1363#elif defined CONFIG_IA32_EMULATION
1364# define IS_IA32 test_thread_flag(TIF_IA32)
1365#else
1366# define IS_IA32 0
1367#endif
1368
1369/*
1370 * We must return the syscall number to actually look up in the table.
1371 * This can be -1L to skip running any syscall at all.
1372 */
1373asmregparm long syscall_trace_enter(struct pt_regs *regs)
86976cd8 1374{
d4d67150
RM
1375 long ret = 0;
1376
380fdd75
RM
1377 /*
1378 * If we stepped into a sysenter/syscall insn, it trapped in
1379 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1380 * If user-mode had set TF itself, then it's still clear from
1381 * do_debug() and we need to set it again to restore the user
1382 * state. If we entered on the slow path, TF was already set.
1383 */
1384 if (test_thread_flag(TIF_SINGLESTEP))
1385 regs->flags |= X86_EFLAGS_TF;
1386
86976cd8
RM
1387 /* do the secure computing check first */
1388 secure_computing(regs->orig_ax);
1389
d4d67150
RM
1390 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1391 ret = -1L;
1392
eeea3c3f
RM
1393 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1394 tracehook_report_syscall_entry(regs))
1395 ret = -1L;
86976cd8
RM
1396
1397 if (unlikely(current->audit_context)) {
d4d67150 1398 if (IS_IA32)
86976cd8
RM
1399 audit_syscall_entry(AUDIT_ARCH_I386,
1400 regs->orig_ax,
1401 regs->bx, regs->cx,
1402 regs->dx, regs->si);
d4d67150
RM
1403#ifdef CONFIG_X86_64
1404 else
86976cd8
RM
1405 audit_syscall_entry(AUDIT_ARCH_X86_64,
1406 regs->orig_ax,
1407 regs->di, regs->si,
1408 regs->dx, regs->r10);
d4d67150 1409#endif
86976cd8 1410 }
d4d67150
RM
1411
1412 return ret ?: regs->orig_ax;
86976cd8
RM
1413}
1414
d4d67150 1415asmregparm void syscall_trace_leave(struct pt_regs *regs)
86976cd8
RM
1416{
1417 if (unlikely(current->audit_context))
1418 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1419
d4d67150 1420 if (test_thread_flag(TIF_SYSCALL_TRACE))
eeea3c3f 1421 tracehook_report_syscall_exit(regs, 0);
86976cd8 1422
d4d67150
RM
1423 /*
1424 * If TIF_SYSCALL_EMU is set, we only get here because of
1425 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1426 * We already reported this syscall instruction in
1427 * syscall_trace_enter(), so don't do any more now.
1428 */
1429 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1430 return;
1431
1432 /*
1433 * If we are single-stepping, synthesize a trap to follow the
1434 * system call instruction.
1435 */
1436 if (test_thread_flag(TIF_SINGLESTEP) &&
eeea3c3f 1437 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
da654b74 1438 send_sigtrap(current, regs, 0, TRAP_BRKPT);
d4d67150 1439}
This page took 0.733242 seconds and 5 git commands to generate.