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