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