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