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