blob: 76adf5b640ffc7e1344a1dbe94d2b6c02515971d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* By Ross Biro 1/23/92 */
2/*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
Markus Metzgereee3af42008-01-30 13:31:09 +01005 *
6 * BTS tracing
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/ptrace.h>
Roland McGrath91e7b702008-01-30 13:31:52 +010016#include <linux/regset.h>
Roland McGratheeea3c32008-03-16 23:36:28 -070017#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/user.h>
Roland McGrath070459d2008-01-30 13:31:53 +010019#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/security.h>
21#include <linux/audit.h>
22#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070023#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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>
Roland McGrath2047b082008-01-30 13:31:01 +010033#include <asm/prctl.h>
34#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010035#include <asm/ds.h>
36
Roland McGrath070459d2008-01-30 13:31:53 +010037#include "tls.h"
38
39enum x86_regset {
40 REGSET_GENERAL,
41 REGSET_FP,
42 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070043 REGSET_IOPERM64 = REGSET_XFP,
Roland McGrath070459d2008-01-30 13:31:53 +010044 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070045 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010046};
Markus Metzgereee3af42008-01-30 13:31:09 +010047
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
51 */
52
Chuck Ebbert9f155b92006-01-05 23:11:29 -050053/*
54 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050055 */
Roland McGrathe39c2892008-01-30 13:31:01 +010056#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
Roland McGrath2047b082008-01-30 13:31:01 +010063/*
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
Roland McGrathe39c2892008-01-30 13:31:01 +010073#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jaswinder Singh4fe702c2008-07-25 10:19:27 +053075static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010077 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Roland McGrath06ee1b62008-01-30 13:31:01 +010078 regno >>= 2;
Roland McGrath62a97d42008-01-30 13:30:52 +010079 if (regno > FS)
80 --regno;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010081 return &regs->bx + regno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Roland McGrath06ee1b62008-01-30 13:31:01 +010084static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Roland McGrath06ee1b62008-01-30 13:31:01 +010086 /*
87 * Returning the value truncates it to 16 bits.
88 */
89 unsigned int retval;
90 if (offset != offsetof(struct user_regs_struct, gs))
91 retval = *pt_regs_access(task_pt_regs(task), offset);
92 else {
93 retval = task->thread.gs;
94 if (task == current)
95 savesegment(gs, retval);
96 }
97 return retval;
98}
99
100static int set_segment_reg(struct task_struct *task,
101 unsigned long offset, u16 value)
102{
103 /*
104 * The value argument was already truncated to 16 bits.
105 */
Roland McGrath2047b082008-01-30 13:31:01 +0100106 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100107 return -EIO;
108
Roland McGrathc63855d2008-02-06 22:39:44 +0100109 /*
110 * For %cs and %ss we cannot permit a null selector.
111 * We can permit a bogus selector as long as it has USER_RPL.
112 * Null selectors are fine for other segment registers, but
113 * we will never get back to user mode with invalid %cs or %ss
114 * and will take the trap in iret instead. Much code relies
115 * on user_mode() to distinguish a user trap frame (which can
116 * safely use invalid selectors) from a kernel trap frame.
117 */
118 switch (offset) {
119 case offsetof(struct user_regs_struct, cs):
120 case offsetof(struct user_regs_struct, ss):
121 if (unlikely(value == 0))
122 return -EIO;
123
124 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100125 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100126 break;
127
128 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100129 task->thread.gs = value;
130 if (task == current)
Roland McGrath5fd4d162008-01-30 13:30:58 +0100131 /*
132 * The user-mode %gs is not affected by
133 * kernel entry, so we must update the CPU.
134 */
135 loadsegment(gs, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
Roland McGrath2047b082008-01-30 13:31:01 +0100141static unsigned long debugreg_addr_limit(struct task_struct *task)
142{
143 return TASK_SIZE - 3;
144}
145
146#else /* CONFIG_X86_64 */
147
148#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
149
150static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
151{
152 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
153 return &regs->r15 + (offset / sizeof(regs->r15));
154}
155
156static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
157{
158 /*
159 * Returning the value truncates it to 16 bits.
160 */
161 unsigned int seg;
162
163 switch (offset) {
164 case offsetof(struct user_regs_struct, fs):
165 if (task == current) {
166 /* Older gas can't assemble movq %?s,%r?? */
167 asm("movl %%fs,%0" : "=r" (seg));
168 return seg;
169 }
170 return task->thread.fsindex;
171 case offsetof(struct user_regs_struct, gs):
172 if (task == current) {
173 asm("movl %%gs,%0" : "=r" (seg));
174 return seg;
175 }
176 return task->thread.gsindex;
177 case offsetof(struct user_regs_struct, ds):
178 if (task == current) {
179 asm("movl %%ds,%0" : "=r" (seg));
180 return seg;
181 }
182 return task->thread.ds;
183 case offsetof(struct user_regs_struct, es):
184 if (task == current) {
185 asm("movl %%es,%0" : "=r" (seg));
186 return seg;
187 }
188 return task->thread.es;
189
190 case offsetof(struct user_regs_struct, cs):
191 case offsetof(struct user_regs_struct, ss):
192 break;
193 }
194 return *pt_regs_access(task_pt_regs(task), offset);
195}
196
197static int set_segment_reg(struct task_struct *task,
198 unsigned long offset, u16 value)
199{
200 /*
201 * The value argument was already truncated to 16 bits.
202 */
203 if (invalid_selector(value))
204 return -EIO;
205
206 switch (offset) {
207 case offsetof(struct user_regs_struct,fs):
208 /*
209 * If this is setting fs as for normal 64-bit use but
210 * setting fs_base has implicitly changed it, leave it.
211 */
212 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
213 task->thread.fs != 0) ||
214 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
215 task->thread.fs == 0))
216 break;
217 task->thread.fsindex = value;
218 if (task == current)
219 loadsegment(fs, task->thread.fsindex);
220 break;
221 case offsetof(struct user_regs_struct,gs):
222 /*
223 * If this is setting gs as for normal 64-bit use but
224 * setting gs_base has implicitly changed it, leave it.
225 */
226 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
227 task->thread.gs != 0) ||
228 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
229 task->thread.gs == 0))
230 break;
231 task->thread.gsindex = value;
232 if (task == current)
233 load_gs_index(task->thread.gsindex);
234 break;
235 case offsetof(struct user_regs_struct,ds):
236 task->thread.ds = value;
237 if (task == current)
238 loadsegment(ds, task->thread.ds);
239 break;
240 case offsetof(struct user_regs_struct,es):
241 task->thread.es = value;
242 if (task == current)
243 loadsegment(es, task->thread.es);
244 break;
245
246 /*
247 * Can't actually change these in 64-bit mode.
248 */
249 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100250 if (unlikely(value == 0))
251 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100252#ifdef CONFIG_IA32_EMULATION
253 if (test_tsk_thread_flag(task, TIF_IA32))
254 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100255#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100256 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100257 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100258 if (unlikely(value == 0))
259 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100260#ifdef CONFIG_IA32_EMULATION
261 if (test_tsk_thread_flag(task, TIF_IA32))
262 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100263#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100264 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100265 }
266
267 return 0;
268}
269
270static unsigned long debugreg_addr_limit(struct task_struct *task)
271{
272#ifdef CONFIG_IA32_EMULATION
273 if (test_tsk_thread_flag(task, TIF_IA32))
274 return IA32_PAGE_OFFSET - 3;
275#endif
276 return TASK_SIZE64 - 7;
277}
278
279#endif /* CONFIG_X86_32 */
280
Roland McGrath06ee1b62008-01-30 13:31:01 +0100281static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100283 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Roland McGrath06ee1b62008-01-30 13:31:01 +0100285 /*
286 * If the debugger set TF, hide it from the readout.
287 */
288 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
289 retval &= ~X86_EFLAGS_TF;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return retval;
292}
293
Roland McGrath06ee1b62008-01-30 13:31:01 +0100294static int set_flags(struct task_struct *task, unsigned long value)
295{
296 struct pt_regs *regs = task_pt_regs(task);
297
298 /*
299 * If the user value contains TF, mark that
300 * it was not "us" (the debugger) that set it.
301 * If not, make sure it stays set if we had.
302 */
303 if (value & X86_EFLAGS_TF)
304 clear_tsk_thread_flag(task, TIF_FORCED_TF);
305 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
306 value |= X86_EFLAGS_TF;
307
308 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
309
310 return 0;
311}
312
313static int putreg(struct task_struct *child,
314 unsigned long offset, unsigned long value)
315{
316 switch (offset) {
317 case offsetof(struct user_regs_struct, cs):
318 case offsetof(struct user_regs_struct, ds):
319 case offsetof(struct user_regs_struct, es):
320 case offsetof(struct user_regs_struct, fs):
321 case offsetof(struct user_regs_struct, gs):
322 case offsetof(struct user_regs_struct, ss):
323 return set_segment_reg(child, offset, value);
324
325 case offsetof(struct user_regs_struct, flags):
326 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100327
328#ifdef CONFIG_X86_64
Roland McGrath84c6f602008-03-07 14:56:02 -0800329 /*
330 * Orig_ax is really just a flag with small positive and
331 * negative values, so make sure to always sign-extend it
332 * from 32 bits so that it works correctly regardless of
333 * whether we come from a 32-bit environment or not.
334 */
335 case offsetof(struct user_regs_struct, orig_ax):
336 value = (long) (s32) value;
337 break;
338
Roland McGrath2047b082008-01-30 13:31:01 +0100339 case offsetof(struct user_regs_struct,fs_base):
340 if (value >= TASK_SIZE_OF(child))
341 return -EIO;
342 /*
343 * When changing the segment base, use do_arch_prctl
344 * to set either thread.fs or thread.fsindex and the
345 * corresponding GDT slot.
346 */
347 if (child->thread.fs != value)
348 return do_arch_prctl(child, ARCH_SET_FS, value);
349 return 0;
350 case offsetof(struct user_regs_struct,gs_base):
351 /*
352 * Exactly the same here as the %fs handling above.
353 */
354 if (value >= TASK_SIZE_OF(child))
355 return -EIO;
356 if (child->thread.gs != value)
357 return do_arch_prctl(child, ARCH_SET_GS, value);
358 return 0;
359#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100360 }
361
362 *pt_regs_access(task_pt_regs(child), offset) = value;
363 return 0;
364}
365
366static unsigned long getreg(struct task_struct *task, unsigned long offset)
367{
368 switch (offset) {
369 case offsetof(struct user_regs_struct, cs):
370 case offsetof(struct user_regs_struct, ds):
371 case offsetof(struct user_regs_struct, es):
372 case offsetof(struct user_regs_struct, fs):
373 case offsetof(struct user_regs_struct, gs):
374 case offsetof(struct user_regs_struct, ss):
375 return get_segment_reg(task, offset);
376
377 case offsetof(struct user_regs_struct, flags):
378 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100379
380#ifdef CONFIG_X86_64
381 case offsetof(struct user_regs_struct, fs_base): {
382 /*
383 * do_arch_prctl may have used a GDT slot instead of
384 * the MSR. To userland, it appears the same either
385 * way, except the %fs segment selector might not be 0.
386 */
387 unsigned int seg = task->thread.fsindex;
388 if (task->thread.fs != 0)
389 return task->thread.fs;
390 if (task == current)
391 asm("movl %%fs,%0" : "=r" (seg));
392 if (seg != FS_TLS_SEL)
393 return 0;
394 return get_desc_base(&task->thread.tls_array[FS_TLS]);
395 }
396 case offsetof(struct user_regs_struct, gs_base): {
397 /*
398 * Exactly the same here as the %fs handling above.
399 */
400 unsigned int seg = task->thread.gsindex;
401 if (task->thread.gs != 0)
402 return task->thread.gs;
403 if (task == current)
404 asm("movl %%gs,%0" : "=r" (seg));
405 if (seg != GS_TLS_SEL)
406 return 0;
407 return get_desc_base(&task->thread.tls_array[GS_TLS]);
408 }
409#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100410 }
411
412 return *pt_regs_access(task_pt_regs(task), offset);
413}
414
Roland McGrath91e7b702008-01-30 13:31:52 +0100415static int genregs_get(struct task_struct *target,
416 const struct user_regset *regset,
417 unsigned int pos, unsigned int count,
418 void *kbuf, void __user *ubuf)
419{
420 if (kbuf) {
421 unsigned long *k = kbuf;
422 while (count > 0) {
423 *k++ = getreg(target, pos);
424 count -= sizeof(*k);
425 pos += sizeof(*k);
426 }
427 } else {
428 unsigned long __user *u = ubuf;
429 while (count > 0) {
430 if (__put_user(getreg(target, pos), u++))
431 return -EFAULT;
432 count -= sizeof(*u);
433 pos += sizeof(*u);
434 }
435 }
436
437 return 0;
438}
439
440static int genregs_set(struct task_struct *target,
441 const struct user_regset *regset,
442 unsigned int pos, unsigned int count,
443 const void *kbuf, const void __user *ubuf)
444{
445 int ret = 0;
446 if (kbuf) {
447 const unsigned long *k = kbuf;
448 while (count > 0 && !ret) {
449 ret = putreg(target, pos, *k++);
450 count -= sizeof(*k);
451 pos += sizeof(*k);
452 }
453 } else {
454 const unsigned long __user *u = ubuf;
455 while (count > 0 && !ret) {
456 unsigned long word;
457 ret = __get_user(word, u++);
458 if (ret)
459 break;
460 ret = putreg(target, pos, word);
461 count -= sizeof(*u);
462 pos += sizeof(*u);
463 }
464 }
465 return ret;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100469 * This function is trivial and will be inlined by the compiler.
470 * Having it separates the implementation details of debug
471 * registers from the interface details of ptrace.
472 */
473static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
474{
Roland McGrath0f534092008-01-30 13:30:59 +0100475 switch (n) {
476 case 0: return child->thread.debugreg0;
477 case 1: return child->thread.debugreg1;
478 case 2: return child->thread.debugreg2;
479 case 3: return child->thread.debugreg3;
480 case 6: return child->thread.debugreg6;
481 case 7: return child->thread.debugreg7;
482 }
483 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100484}
485
486static int ptrace_set_debugreg(struct task_struct *child,
487 int n, unsigned long data)
488{
Roland McGrath0f534092008-01-30 13:30:59 +0100489 int i;
490
Roland McGrathd9771e82008-01-30 13:30:52 +0100491 if (unlikely(n == 4 || n == 5))
492 return -EIO;
493
Roland McGrath2047b082008-01-30 13:31:01 +0100494 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100495 return -EIO;
496
Roland McGrath0f534092008-01-30 13:30:59 +0100497 switch (n) {
498 case 0: child->thread.debugreg0 = data; break;
499 case 1: child->thread.debugreg1 = data; break;
500 case 2: child->thread.debugreg2 = data; break;
501 case 3: child->thread.debugreg3 = data; break;
502
503 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100504 if ((data & ~0xffffffffUL) != 0)
505 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100506 child->thread.debugreg6 = data;
507 break;
508
509 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100510 /*
511 * Sanity-check data. Take one half-byte at once with
512 * check = (val >> (16 + 4*i)) & 0xf. It contains the
513 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
514 * 2 and 3 are LENi. Given a list of invalid values,
515 * we do mask |= 1 << invalid_value, so that
516 * (mask >> check) & 1 is a correct test for invalid
517 * values.
518 *
519 * R/Wi contains the type of the breakpoint /
520 * watchpoint, LENi contains the length of the watched
521 * data in the watchpoint case.
522 *
523 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100524 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100525 * - R/Wi == 0x10 (break on I/O reads or writes), so
526 * mask |= 0x4444.
527 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
528 * 0x1110.
529 *
530 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
531 *
532 * See the Intel Manual "System Programming Guide",
533 * 15.2.4
534 *
535 * Note that LENi == 0x10 is defined on x86_64 in long
536 * mode (i.e. even for 32-bit userspace software, but
537 * 64-bit kernel), so the x86_64 mask value is 0x5454.
538 * See the AMD manual no. 24593 (AMD64 System Programming)
539 */
Roland McGrath2047b082008-01-30 13:31:01 +0100540#ifdef CONFIG_X86_32
541#define DR7_MASK 0x5f54
542#else
543#define DR7_MASK 0x5554
544#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100545 data &= ~DR_CONTROL_RESERVED;
546 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100547 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100548 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100549 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100550 if (data)
551 set_tsk_thread_flag(child, TIF_DEBUG);
552 else
553 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100554 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100555 }
556
Roland McGrathd9771e82008-01-30 13:30:52 +0100557 return 0;
558}
559
Roland McGrath325af5f2008-08-08 15:58:39 -0700560/*
561 * These access the current or another (stopped) task's io permission
562 * bitmap for debugging or core dump.
563 */
564static int ioperm_active(struct task_struct *target,
565 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100566{
Roland McGrath325af5f2008-08-08 15:58:39 -0700567 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100568}
569
Roland McGrath325af5f2008-08-08 15:58:39 -0700570static int ioperm_get(struct task_struct *target,
571 const struct user_regset *regset,
572 unsigned int pos, unsigned int count,
573 void *kbuf, void __user *ubuf)
574{
575 if (!target->thread.io_bitmap_ptr)
576 return -ENXIO;
577
578 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
579 target->thread.io_bitmap_ptr,
580 0, IO_BITMAP_BYTES);
581}
582
Markus Metzger93fa7632008-04-08 11:01:58 +0200583#ifdef CONFIG_X86_PTRACE_BTS
584/*
585 * The configuration for a particular BTS hardware implementation.
586 */
587struct bts_configuration {
588 /* the size of a BTS record in bytes; at most BTS_MAX_RECORD_SIZE */
589 unsigned char sizeof_bts;
590 /* the size of a field in the BTS record in bytes */
591 unsigned char sizeof_field;
592 /* a bitmask to enable/disable BTS in DEBUGCTL MSR */
593 unsigned long debugctl_mask;
594};
595static struct bts_configuration bts_cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100596
Markus Metzger93fa7632008-04-08 11:01:58 +0200597#define BTS_MAX_RECORD_SIZE (8 * 3)
598
599
600/*
601 * Branch Trace Store (BTS) uses the following format. Different
602 * architectures vary in the size of those fields.
603 * - source linear address
604 * - destination linear address
605 * - flags
606 *
607 * Later architectures use 64bit pointers throughout, whereas earlier
608 * architectures use 32bit pointers in 32bit mode.
609 *
610 * We compute the base address for the first 8 fields based on:
611 * - the field size stored in the DS configuration
612 * - the relative field position
613 *
614 * In order to store additional information in the BTS buffer, we use
615 * a special source address to indicate that the record requires
616 * special interpretation.
617 *
618 * Netburst indicated via a bit in the flags field whether the branch
619 * was predicted; this is ignored.
620 */
621
622enum bts_field {
623 bts_from = 0,
624 bts_to,
625 bts_flags,
626
627 bts_escape = (unsigned long)-1,
628 bts_qual = bts_to,
629 bts_jiffies = bts_flags
630};
631
632static inline unsigned long bts_get(const char *base, enum bts_field field)
Markus Metzgereee3af42008-01-30 13:31:09 +0100633{
Markus Metzger93fa7632008-04-08 11:01:58 +0200634 base += (bts_cfg.sizeof_field * field);
635 return *(unsigned long *)base;
Markus Metzgereee3af42008-01-30 13:31:09 +0100636}
637
Markus Metzger93fa7632008-04-08 11:01:58 +0200638static inline void bts_set(char *base, enum bts_field field, unsigned long val)
639{
640 base += (bts_cfg.sizeof_field * field);;
641 (*(unsigned long *)base) = val;
642}
643
644/*
645 * Translate a BTS record from the raw format into the bts_struct format
646 *
647 * out (out): bts_struct interpretation
648 * raw: raw BTS record
649 */
650static void ptrace_bts_translate_record(struct bts_struct *out, const void *raw)
651{
652 memset(out, 0, sizeof(*out));
653 if (bts_get(raw, bts_from) == bts_escape) {
654 out->qualifier = bts_get(raw, bts_qual);
655 out->variant.jiffies = bts_get(raw, bts_jiffies);
656 } else {
657 out->qualifier = BTS_BRANCH;
658 out->variant.lbr.from_ip = bts_get(raw, bts_from);
659 out->variant.lbr.to_ip = bts_get(raw, bts_to);
660 }
661}
662
663static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100664 struct bts_struct __user *out)
665{
666 struct bts_struct ret;
Markus Metzger93fa7632008-04-08 11:01:58 +0200667 const void *bts_record;
668 size_t bts_index, bts_end;
669 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100670
Markus Metzgerca0002a2008-11-25 09:01:25 +0100671 error = ds_get_bts_end(child->bts, &bts_end);
Markus Metzger93fa7632008-04-08 11:01:58 +0200672 if (error < 0)
673 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100674
Markus Metzgera95d67f2008-01-30 13:31:20 +0100675 if (bts_end <= index)
Markus Metzgere4811f22008-01-30 13:31:20 +0100676 return -EINVAL;
677
Markus Metzgerca0002a2008-11-25 09:01:25 +0100678 error = ds_get_bts_index(child->bts, &bts_index);
Markus Metzger93fa7632008-04-08 11:01:58 +0200679 if (error < 0)
680 return error;
Markus Metzgere4811f22008-01-30 13:31:20 +0100681
Markus Metzger93fa7632008-04-08 11:01:58 +0200682 /* translate the ptrace bts index into the ds bts index */
683 bts_index += bts_end - (index + 1);
684 if (bts_end <= bts_index)
685 bts_index -= bts_end;
686
Markus Metzgerca0002a2008-11-25 09:01:25 +0100687 error = ds_access_bts(child->bts, bts_index, &bts_record);
Markus Metzger93fa7632008-04-08 11:01:58 +0200688 if (error < 0)
689 return error;
690
691 ptrace_bts_translate_record(&ret, bts_record);
Markus Metzgereee3af42008-01-30 13:31:09 +0100692
693 if (copy_to_user(out, &ret, sizeof(ret)))
694 return -EFAULT;
695
696 return sizeof(ret);
697}
698
Markus Metzgera95d67f2008-01-30 13:31:20 +0100699static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100700 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100701 struct bts_struct __user *out)
702{
Markus Metzger93fa7632008-04-08 11:01:58 +0200703 struct bts_struct ret;
704 const unsigned char *raw;
705 size_t end, i;
706 int error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100707
Markus Metzgerca0002a2008-11-25 09:01:25 +0100708 error = ds_get_bts_index(child->bts, &end);
Markus Metzger93fa7632008-04-08 11:01:58 +0200709 if (error < 0)
710 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100711
Markus Metzgercba4b652008-01-30 13:32:03 +0100712 if (size < (end * sizeof(struct bts_struct)))
713 return -EIO;
714
Markus Metzgerca0002a2008-11-25 09:01:25 +0100715 error = ds_access_bts(child->bts, 0, (const void **)&raw);
Markus Metzger93fa7632008-04-08 11:01:58 +0200716 if (error < 0)
717 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100718
Markus Metzger93fa7632008-04-08 11:01:58 +0200719 for (i = 0; i < end; i++, out++, raw += bts_cfg.sizeof_bts) {
720 ptrace_bts_translate_record(&ret, raw);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100721
722 if (copy_to_user(out, &ret, sizeof(ret)))
723 return -EFAULT;
724 }
725
Markus Metzgerca0002a2008-11-25 09:01:25 +0100726 error = ds_clear_bts(child->bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200727 if (error < 0)
728 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100729
Markus Metzgercba4b652008-01-30 13:32:03 +0100730 return end;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100731}
732
733static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100734 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100735 const struct ptrace_bts_config __user *ucfg)
736{
737 struct ptrace_bts_config cfg;
Markus Metzger93fa7632008-04-08 11:01:58 +0200738 int error = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100739
Markus Metzger93fa7632008-04-08 11:01:58 +0200740 error = -EOPNOTSUPP;
741 if (!bts_cfg.sizeof_bts)
742 goto errout;
743
744 error = -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100745 if (cfg_size < sizeof(cfg))
Markus Metzger93fa7632008-04-08 11:01:58 +0200746 goto errout;
Markus Metzgercba4b652008-01-30 13:32:03 +0100747
Markus Metzger93fa7632008-04-08 11:01:58 +0200748 error = -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100749 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzger93fa7632008-04-08 11:01:58 +0200750 goto errout;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100751
Markus Metzger93fa7632008-04-08 11:01:58 +0200752 error = -EINVAL;
753 if ((cfg.flags & PTRACE_BTS_O_SIGNAL) &&
754 !(cfg.flags & PTRACE_BTS_O_ALLOC))
755 goto errout;
Markus Metzgercba4b652008-01-30 13:32:03 +0100756
Markus Metzger93fa7632008-04-08 11:01:58 +0200757 if (cfg.flags & PTRACE_BTS_O_ALLOC) {
Markus Metzgerca0002a2008-11-25 09:01:25 +0100758 bts_ovfl_callback_t ovfl = NULL;
Markus Metzger93fa7632008-04-08 11:01:58 +0200759 unsigned int sig = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100760
Markus Metzger93fa7632008-04-08 11:01:58 +0200761 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
762 if (!cfg.signal)
763 goto errout;
764
Markus Metzgerca0002a2008-11-25 09:01:25 +0100765 error = -EOPNOTSUPP;
766 goto errout;
767
Markus Metzger93fa7632008-04-08 11:01:58 +0200768 sig = cfg.signal;
Markus Metzger93fa7632008-04-08 11:01:58 +0200769 }
770
Markus Metzgerca0002a2008-11-25 09:01:25 +0100771 if (child->bts)
772 (void)ds_release_bts(child->bts);
773
774 child->bts = ds_request_bts(child, /* base = */ NULL, cfg.size,
775 ovfl, /* th = */ (size_t)-1);
776 if (IS_ERR(child->bts)) {
777 error = PTR_ERR(child->bts);
778 child->bts = NULL;
Markus Metzgerda35c372008-01-30 13:32:03 +0100779 goto errout;
Markus Metzgerca0002a2008-11-25 09:01:25 +0100780 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100781
Markus Metzger93fa7632008-04-08 11:01:58 +0200782 child->thread.bts_ovfl_signal = sig;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100783 }
784
Markus Metzger93fa7632008-04-08 11:01:58 +0200785 error = -EINVAL;
786 if (!child->thread.ds_ctx && cfg.flags)
Markus Metzgerda35c372008-01-30 13:32:03 +0100787 goto errout;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100788
Markus Metzgerda35c372008-01-30 13:32:03 +0100789 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzger93fa7632008-04-08 11:01:58 +0200790 child->thread.debugctlmsr |= bts_cfg.debugctl_mask;
Markus Metzgerda35c372008-01-30 13:32:03 +0100791 else
Markus Metzger93fa7632008-04-08 11:01:58 +0200792 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
Markus Metzgereee3af42008-01-30 13:31:09 +0100793
Markus Metzgerda35c372008-01-30 13:32:03 +0100794 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgereee3af42008-01-30 13:31:09 +0100795 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
796 else
797 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
798
Markus Metzger93fa7632008-04-08 11:01:58 +0200799 error = sizeof(cfg);
Markus Metzgercba4b652008-01-30 13:32:03 +0100800
Markus Metzgerda35c372008-01-30 13:32:03 +0100801out:
802 if (child->thread.debugctlmsr)
803 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
804 else
805 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
806
Markus Metzger93fa7632008-04-08 11:01:58 +0200807 return error;
Markus Metzgerda35c372008-01-30 13:32:03 +0100808
809errout:
Markus Metzger93fa7632008-04-08 11:01:58 +0200810 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
Markus Metzgerda35c372008-01-30 13:32:03 +0100811 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
812 goto out;
Markus Metzgereee3af42008-01-30 13:31:09 +0100813}
814
Markus Metzgera95d67f2008-01-30 13:31:20 +0100815static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100816 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100817 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100818{
Markus Metzgera95d67f2008-01-30 13:31:20 +0100819 struct ptrace_bts_config cfg;
Markus Metzger93fa7632008-04-08 11:01:58 +0200820 size_t end;
821 const void *base, *max;
822 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100823
Markus Metzgercba4b652008-01-30 13:32:03 +0100824 if (cfg_size < sizeof(cfg))
825 return -EIO;
826
Markus Metzgerca0002a2008-11-25 09:01:25 +0100827 error = ds_get_bts_end(child->bts, &end);
Markus Metzger93fa7632008-04-08 11:01:58 +0200828 if (error < 0)
829 return error;
830
Markus Metzgerca0002a2008-11-25 09:01:25 +0100831 error = ds_access_bts(child->bts, /* index = */ 0, &base);
Markus Metzger93fa7632008-04-08 11:01:58 +0200832 if (error < 0)
833 return error;
834
Markus Metzgerca0002a2008-11-25 09:01:25 +0100835 error = ds_access_bts(child->bts, /* index = */ end, &max);
Markus Metzger93fa7632008-04-08 11:01:58 +0200836 if (error < 0)
837 return error;
838
Markus Metzgera95d67f2008-01-30 13:31:20 +0100839 memset(&cfg, 0, sizeof(cfg));
Markus Metzger93fa7632008-04-08 11:01:58 +0200840 cfg.size = (max - base);
841 cfg.signal = child->thread.bts_ovfl_signal;
Markus Metzger87e84072008-01-30 13:32:54 +0100842 cfg.bts_size = sizeof(struct bts_struct);
843
Markus Metzger93fa7632008-04-08 11:01:58 +0200844 if (cfg.signal)
845 cfg.flags |= PTRACE_BTS_O_SIGNAL;
846
847 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
848 child->thread.debugctlmsr & bts_cfg.debugctl_mask)
849 cfg.flags |= PTRACE_BTS_O_TRACE;
850
851 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
852 cfg.flags |= PTRACE_BTS_O_SCHED;
853
Markus Metzgera95d67f2008-01-30 13:31:20 +0100854 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
855 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100856
Markus Metzgera95d67f2008-01-30 13:31:20 +0100857 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100858}
859
Andrew Mortond8d4f152008-03-04 15:05:39 -0800860static int ptrace_bts_write_record(struct task_struct *child,
861 const struct bts_struct *in)
862{
Markus Metzger93fa7632008-04-08 11:01:58 +0200863 unsigned char bts_record[BTS_MAX_RECORD_SIZE];
Andrew Mortond8d4f152008-03-04 15:05:39 -0800864
Markus Metzger93fa7632008-04-08 11:01:58 +0200865 BUG_ON(BTS_MAX_RECORD_SIZE < bts_cfg.sizeof_bts);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800866
Markus Metzger93fa7632008-04-08 11:01:58 +0200867 memset(bts_record, 0, bts_cfg.sizeof_bts);
868 switch (in->qualifier) {
869 case BTS_INVALID:
870 break;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800871
Markus Metzger93fa7632008-04-08 11:01:58 +0200872 case BTS_BRANCH:
873 bts_set(bts_record, bts_from, in->variant.lbr.from_ip);
874 bts_set(bts_record, bts_to, in->variant.lbr.to_ip);
875 break;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800876
Markus Metzger93fa7632008-04-08 11:01:58 +0200877 case BTS_TASK_ARRIVES:
878 case BTS_TASK_DEPARTS:
879 bts_set(bts_record, bts_from, bts_escape);
880 bts_set(bts_record, bts_qual, in->qualifier);
881 bts_set(bts_record, bts_jiffies, in->variant.jiffies);
882 break;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800883
Markus Metzger93fa7632008-04-08 11:01:58 +0200884 default:
Andrew Mortond8d4f152008-03-04 15:05:39 -0800885 return -EINVAL;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800886 }
887
Markus Metzgerca0002a2008-11-25 09:01:25 +0100888 return ds_write_bts(child->bts, bts_record, bts_cfg.sizeof_bts);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800889}
890
Markus Metzgereee3af42008-01-30 13:31:09 +0100891void ptrace_bts_take_timestamp(struct task_struct *tsk,
892 enum bts_qualifier qualifier)
893{
894 struct bts_struct rec = {
895 .qualifier = qualifier,
Markus Metzgerda35c372008-01-30 13:32:03 +0100896 .variant.jiffies = jiffies_64
Markus Metzgereee3af42008-01-30 13:31:09 +0100897 };
898
Markus Metzgereee3af42008-01-30 13:31:09 +0100899 ptrace_bts_write_record(tsk, &rec);
900}
Markus Metzger93fa7632008-04-08 11:01:58 +0200901
902static const struct bts_configuration bts_cfg_netburst = {
903 .sizeof_bts = sizeof(long) * 3,
904 .sizeof_field = sizeof(long),
905 .debugctl_mask = (1<<2)|(1<<3)|(1<<5)
906};
907
908static const struct bts_configuration bts_cfg_pentium_m = {
909 .sizeof_bts = sizeof(long) * 3,
910 .sizeof_field = sizeof(long),
911 .debugctl_mask = (1<<6)|(1<<7)
912};
913
914static const struct bts_configuration bts_cfg_core2 = {
915 .sizeof_bts = 8 * 3,
916 .sizeof_field = 8,
917 .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
918};
919
920static inline void bts_configure(const struct bts_configuration *cfg)
921{
922 bts_cfg = *cfg;
923}
924
925void __cpuinit ptrace_bts_init_intel(struct cpuinfo_x86 *c)
926{
927 switch (c->x86) {
928 case 0x6:
929 switch (c->x86_model) {
Markus Metzgerf4166c52008-11-09 14:29:21 +0100930 case 0 ... 0xC:
931 /* sorry, don't know about them */
932 break;
Markus Metzger93fa7632008-04-08 11:01:58 +0200933 case 0xD:
934 case 0xE: /* Pentium M */
935 bts_configure(&bts_cfg_pentium_m);
936 break;
Markus Metzgerf4166c52008-11-09 14:29:21 +0100937 default: /* Core2, Atom, ... */
Markus Metzger93fa7632008-04-08 11:01:58 +0200938 bts_configure(&bts_cfg_core2);
939 break;
Markus Metzger93fa7632008-04-08 11:01:58 +0200940 }
941 break;
942 case 0xF:
943 switch (c->x86_model) {
944 case 0x0:
945 case 0x1:
946 case 0x2: /* Netburst */
947 bts_configure(&bts_cfg_netburst);
948 break;
949 default:
950 /* sorry, don't know about them */
951 break;
952 }
953 break;
954 default:
955 /* sorry, don't know about them */
956 break;
957 }
958}
959#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100960
Roland McGrathd9771e82008-01-30 13:30:52 +0100961/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * Called by kernel/ptrace.c when detaching..
963 *
964 * Make sure the single step bit is not set.
965 */
966void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100967{
Roland McGrath7f232342008-01-30 13:30:48 +0100968 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100969#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700970 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100971#endif
Markus Metzger93fa7632008-04-08 11:01:58 +0200972#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgerca0002a2008-11-25 09:01:25 +0100973 if (child->bts) {
974 (void)ds_release_bts(child->bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200975
Markus Metzgerca0002a2008-11-25 09:01:25 +0100976 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
977 if (!child->thread.debugctlmsr)
978 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
Markus Metzger93fa7632008-04-08 11:01:58 +0200979
Markus Metzgerca0002a2008-11-25 09:01:25 +0100980 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
981 }
Markus Metzger93fa7632008-04-08 11:01:58 +0200982#endif /* CONFIG_X86_PTRACE_BTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Roland McGrath5a4646a2008-01-30 13:31:54 +0100985#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
986static const struct user_regset_view user_x86_32_view; /* Initialized below. */
987#endif
988
Christoph Hellwig481bed42005-11-07 00:59:47 -0800989long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100991 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 unsigned long __user *datap = (unsigned long __user *)data;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* read the word at location addr in the USER area. */
996 case PTRACE_PEEKUSR: {
997 unsigned long tmp;
998
999 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001000 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1001 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003
1004 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001005 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001007 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1008 addr <= offsetof(struct user, u_debugreg[7])) {
1009 addr -= offsetof(struct user, u_debugreg[0]);
1010 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 ret = put_user(tmp, datap);
1013 break;
1014 }
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1017 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001018 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1019 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021
Roland McGrathe9c86c72008-01-30 13:31:01 +01001022 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001024 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1025 addr <= offsetof(struct user, u_debugreg[7])) {
1026 addr -= offsetof(struct user, u_debugreg[0]);
1027 ret = ptrace_set_debugreg(child,
1028 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Roland McGrath5a4646a2008-01-30 13:31:54 +01001032 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1033 return copy_regset_to_user(child,
1034 task_user_regset_view(current),
1035 REGSET_GENERAL,
1036 0, sizeof(struct user_regs_struct),
1037 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Roland McGrath5a4646a2008-01-30 13:31:54 +01001039 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1040 return copy_regset_from_user(child,
1041 task_user_regset_view(current),
1042 REGSET_GENERAL,
1043 0, sizeof(struct user_regs_struct),
1044 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Roland McGrath5a4646a2008-01-30 13:31:54 +01001046 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1047 return copy_regset_to_user(child,
1048 task_user_regset_view(current),
1049 REGSET_FP,
1050 0, sizeof(struct user_i387_struct),
1051 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Roland McGrath5a4646a2008-01-30 13:31:54 +01001053 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1054 return copy_regset_from_user(child,
1055 task_user_regset_view(current),
1056 REGSET_FP,
1057 0, sizeof(struct user_i387_struct),
1058 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Roland McGrathe9c86c72008-01-30 13:31:01 +01001060#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001061 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1062 return copy_regset_to_user(child, &user_x86_32_view,
1063 REGSET_XFP,
1064 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001065 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Roland McGrath5a4646a2008-01-30 13:31:54 +01001067 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1068 return copy_regset_from_user(child, &user_x86_32_view,
1069 REGSET_XFP,
1070 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001071 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Roland McGrathe9c86c72008-01-30 13:31:01 +01001074#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001076 if (addr < 0)
1077 return -EIO;
1078 ret = do_get_thread_area(child, addr,
1079 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081
1082 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001083 if (addr < 0)
1084 return -EIO;
1085 ret = do_set_thread_area(child, addr,
1086 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001088#endif
1089
1090#ifdef CONFIG_X86_64
1091 /* normal 64bit interface to access TLS data.
1092 Works just like arch_prctl, except that the arguments
1093 are reversed. */
1094 case PTRACE_ARCH_PRCTL:
1095 ret = do_arch_prctl(child, data, addr);
1096 break;
1097#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001099 /*
1100 * These bits need more cooking - not enabled yet:
1101 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001102#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001103 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001104 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001105 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001106 break;
1107
1108 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001109 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001110 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001111 break;
1112
Markus Metzgerca0002a2008-11-25 09:01:25 +01001113 case PTRACE_BTS_SIZE: {
1114 size_t size;
1115
1116 ret = ds_get_bts_index(child->bts, &size);
1117 if (ret == 0) {
1118 BUG_ON(size != (int) size);
1119 ret = (int) size;
1120 }
Markus Metzgera95d67f2008-01-30 13:31:20 +01001121 break;
Markus Metzgerca0002a2008-11-25 09:01:25 +01001122 }
Markus Metzgera95d67f2008-01-30 13:31:20 +01001123
1124 case PTRACE_BTS_GET:
1125 ret = ptrace_bts_read_record
1126 (child, data, (struct bts_struct __user *) addr);
1127 break;
1128
1129 case PTRACE_BTS_CLEAR:
Markus Metzgerca0002a2008-11-25 09:01:25 +01001130 ret = ds_clear_bts(child->bts);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001131 break;
1132
1133 case PTRACE_BTS_DRAIN:
1134 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001135 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001136 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001137#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 default:
1140 ret = ptrace_request(child, request, addr, data);
1141 break;
1142 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return ret;
1145}
1146
Roland McGrathcb757c42008-01-30 13:31:01 +01001147#ifdef CONFIG_IA32_EMULATION
1148
Roland McGrath099cd6e2008-01-30 13:31:01 +01001149#include <linux/compat.h>
1150#include <linux/syscalls.h>
1151#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001152#include <asm/user32.h>
1153
1154#define R32(l,q) \
1155 case offsetof(struct user32, regs.l): \
1156 regs->q = value; break
1157
1158#define SEG32(rs) \
1159 case offsetof(struct user32, regs.rs): \
1160 return set_segment_reg(child, \
1161 offsetof(struct user_regs_struct, rs), \
1162 value); \
1163 break
1164
1165static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1166{
1167 struct pt_regs *regs = task_pt_regs(child);
1168
1169 switch (regno) {
1170
1171 SEG32(cs);
1172 SEG32(ds);
1173 SEG32(es);
1174 SEG32(fs);
1175 SEG32(gs);
1176 SEG32(ss);
1177
1178 R32(ebx, bx);
1179 R32(ecx, cx);
1180 R32(edx, dx);
1181 R32(edi, di);
1182 R32(esi, si);
1183 R32(ebp, bp);
1184 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001185 R32(eip, ip);
1186 R32(esp, sp);
1187
Roland McGrath40f09332008-02-28 19:57:07 -08001188 case offsetof(struct user32, regs.orig_eax):
1189 /*
1190 * Sign-extend the value so that orig_eax = -1
1191 * causes (long)orig_ax < 0 tests to fire correctly.
1192 */
1193 regs->orig_ax = (long) (s32) value;
1194 break;
1195
Roland McGrathcb757c42008-01-30 13:31:01 +01001196 case offsetof(struct user32, regs.eflags):
1197 return set_flags(child, value);
1198
1199 case offsetof(struct user32, u_debugreg[0]) ...
1200 offsetof(struct user32, u_debugreg[7]):
1201 regno -= offsetof(struct user32, u_debugreg[0]);
1202 return ptrace_set_debugreg(child, regno / 4, value);
1203
1204 default:
1205 if (regno > sizeof(struct user32) || (regno & 3))
1206 return -EIO;
1207
1208 /*
1209 * Other dummy fields in the virtual user structure
1210 * are ignored
1211 */
1212 break;
1213 }
1214 return 0;
1215}
1216
1217#undef R32
1218#undef SEG32
1219
1220#define R32(l,q) \
1221 case offsetof(struct user32, regs.l): \
1222 *val = regs->q; break
1223
1224#define SEG32(rs) \
1225 case offsetof(struct user32, regs.rs): \
1226 *val = get_segment_reg(child, \
1227 offsetof(struct user_regs_struct, rs)); \
1228 break
1229
1230static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1231{
1232 struct pt_regs *regs = task_pt_regs(child);
1233
1234 switch (regno) {
1235
1236 SEG32(ds);
1237 SEG32(es);
1238 SEG32(fs);
1239 SEG32(gs);
1240
1241 R32(cs, cs);
1242 R32(ss, ss);
1243 R32(ebx, bx);
1244 R32(ecx, cx);
1245 R32(edx, dx);
1246 R32(edi, di);
1247 R32(esi, si);
1248 R32(ebp, bp);
1249 R32(eax, ax);
1250 R32(orig_eax, orig_ax);
1251 R32(eip, ip);
1252 R32(esp, sp);
1253
1254 case offsetof(struct user32, regs.eflags):
1255 *val = get_flags(child);
1256 break;
1257
1258 case offsetof(struct user32, u_debugreg[0]) ...
1259 offsetof(struct user32, u_debugreg[7]):
1260 regno -= offsetof(struct user32, u_debugreg[0]);
1261 *val = ptrace_get_debugreg(child, regno / 4);
1262 break;
1263
1264 default:
1265 if (regno > sizeof(struct user32) || (regno & 3))
1266 return -EIO;
1267
1268 /*
1269 * Other dummy fields in the virtual user structure
1270 * are ignored
1271 */
1272 *val = 0;
1273 break;
1274 }
1275 return 0;
1276}
1277
1278#undef R32
1279#undef SEG32
1280
Roland McGrath91e7b702008-01-30 13:31:52 +01001281static int genregs32_get(struct task_struct *target,
1282 const struct user_regset *regset,
1283 unsigned int pos, unsigned int count,
1284 void *kbuf, void __user *ubuf)
1285{
1286 if (kbuf) {
1287 compat_ulong_t *k = kbuf;
1288 while (count > 0) {
1289 getreg32(target, pos, k++);
1290 count -= sizeof(*k);
1291 pos += sizeof(*k);
1292 }
1293 } else {
1294 compat_ulong_t __user *u = ubuf;
1295 while (count > 0) {
1296 compat_ulong_t word;
1297 getreg32(target, pos, &word);
1298 if (__put_user(word, u++))
1299 return -EFAULT;
1300 count -= sizeof(*u);
1301 pos += sizeof(*u);
1302 }
1303 }
1304
1305 return 0;
1306}
1307
1308static int genregs32_set(struct task_struct *target,
1309 const struct user_regset *regset,
1310 unsigned int pos, unsigned int count,
1311 const void *kbuf, const void __user *ubuf)
1312{
1313 int ret = 0;
1314 if (kbuf) {
1315 const compat_ulong_t *k = kbuf;
1316 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001317 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001318 count -= sizeof(*k);
1319 pos += sizeof(*k);
1320 }
1321 } else {
1322 const compat_ulong_t __user *u = ubuf;
1323 while (count > 0 && !ret) {
1324 compat_ulong_t word;
1325 ret = __get_user(word, u++);
1326 if (ret)
1327 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001328 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001329 count -= sizeof(*u);
1330 pos += sizeof(*u);
1331 }
1332 }
1333 return ret;
1334}
1335
Roland McGrath562b80b2008-04-22 12:21:25 -07001336long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1337 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001338{
Roland McGrath562b80b2008-04-22 12:21:25 -07001339 unsigned long addr = caddr;
1340 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001341 void __user *datap = compat_ptr(data);
1342 int ret;
1343 __u32 val;
1344
1345 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001346 case PTRACE_PEEKUSR:
1347 ret = getreg32(child, addr, &val);
1348 if (ret == 0)
1349 ret = put_user(val, (__u32 __user *)datap);
1350 break;
1351
1352 case PTRACE_POKEUSR:
1353 ret = putreg32(child, addr, data);
1354 break;
1355
Roland McGrath5a4646a2008-01-30 13:31:54 +01001356 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1357 return copy_regset_to_user(child, &user_x86_32_view,
1358 REGSET_GENERAL,
1359 0, sizeof(struct user_regs_struct32),
1360 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001361
Roland McGrath5a4646a2008-01-30 13:31:54 +01001362 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1363 return copy_regset_from_user(child, &user_x86_32_view,
1364 REGSET_GENERAL, 0,
1365 sizeof(struct user_regs_struct32),
1366 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001367
Roland McGrath5a4646a2008-01-30 13:31:54 +01001368 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1369 return copy_regset_to_user(child, &user_x86_32_view,
1370 REGSET_FP, 0,
1371 sizeof(struct user_i387_ia32_struct),
1372 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001373
Roland McGrath5a4646a2008-01-30 13:31:54 +01001374 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1375 return copy_regset_from_user(
1376 child, &user_x86_32_view, REGSET_FP,
1377 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001378
Roland McGrath5a4646a2008-01-30 13:31:54 +01001379 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1380 return copy_regset_to_user(child, &user_x86_32_view,
1381 REGSET_XFP, 0,
1382 sizeof(struct user32_fxsr_struct),
1383 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001384
Roland McGrath5a4646a2008-01-30 13:31:54 +01001385 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1386 return copy_regset_from_user(child, &user_x86_32_view,
1387 REGSET_XFP, 0,
1388 sizeof(struct user32_fxsr_struct),
1389 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001390
Roland McGrath562b80b2008-04-22 12:21:25 -07001391 case PTRACE_GET_THREAD_AREA:
1392 case PTRACE_SET_THREAD_AREA:
1393 return arch_ptrace(child, request, addr, data);
1394
Roland McGrath099cd6e2008-01-30 13:31:01 +01001395 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001396 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001397 }
1398
Roland McGrath099cd6e2008-01-30 13:31:01 +01001399 return ret;
1400}
1401
Roland McGrathcb757c42008-01-30 13:31:01 +01001402#endif /* CONFIG_IA32_EMULATION */
1403
Roland McGrath070459d2008-01-30 13:31:53 +01001404#ifdef CONFIG_X86_64
1405
1406static const struct user_regset x86_64_regsets[] = {
1407 [REGSET_GENERAL] = {
1408 .core_note_type = NT_PRSTATUS,
1409 .n = sizeof(struct user_regs_struct) / sizeof(long),
1410 .size = sizeof(long), .align = sizeof(long),
1411 .get = genregs_get, .set = genregs_set
1412 },
1413 [REGSET_FP] = {
1414 .core_note_type = NT_PRFPREG,
1415 .n = sizeof(struct user_i387_struct) / sizeof(long),
1416 .size = sizeof(long), .align = sizeof(long),
1417 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1418 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001419 [REGSET_IOPERM64] = {
1420 .core_note_type = NT_386_IOPERM,
1421 .n = IO_BITMAP_LONGS,
1422 .size = sizeof(long), .align = sizeof(long),
1423 .active = ioperm_active, .get = ioperm_get
1424 },
Roland McGrath070459d2008-01-30 13:31:53 +01001425};
1426
1427static const struct user_regset_view user_x86_64_view = {
1428 .name = "x86_64", .e_machine = EM_X86_64,
1429 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1430};
1431
1432#else /* CONFIG_X86_32 */
1433
1434#define user_regs_struct32 user_regs_struct
1435#define genregs32_get genregs_get
1436#define genregs32_set genregs_set
1437
Roland McGrath1f465f42008-05-09 15:43:44 -07001438#define user_i387_ia32_struct user_i387_struct
1439#define user32_fxsr_struct user_fxsr_struct
1440
Roland McGrath070459d2008-01-30 13:31:53 +01001441#endif /* CONFIG_X86_64 */
1442
1443#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1444static const struct user_regset x86_32_regsets[] = {
1445 [REGSET_GENERAL] = {
1446 .core_note_type = NT_PRSTATUS,
1447 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1448 .size = sizeof(u32), .align = sizeof(u32),
1449 .get = genregs32_get, .set = genregs32_set
1450 },
1451 [REGSET_FP] = {
1452 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001453 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001454 .size = sizeof(u32), .align = sizeof(u32),
1455 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1456 },
1457 [REGSET_XFP] = {
1458 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001459 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001460 .size = sizeof(u32), .align = sizeof(u32),
1461 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1462 },
1463 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001464 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001465 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1466 .size = sizeof(struct user_desc),
1467 .align = sizeof(struct user_desc),
1468 .active = regset_tls_active,
1469 .get = regset_tls_get, .set = regset_tls_set
1470 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001471 [REGSET_IOPERM32] = {
1472 .core_note_type = NT_386_IOPERM,
1473 .n = IO_BITMAP_BYTES / sizeof(u32),
1474 .size = sizeof(u32), .align = sizeof(u32),
1475 .active = ioperm_active, .get = ioperm_get
1476 },
Roland McGrath070459d2008-01-30 13:31:53 +01001477};
1478
1479static const struct user_regset_view user_x86_32_view = {
1480 .name = "i386", .e_machine = EM_386,
1481 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1482};
1483#endif
1484
1485const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1486{
1487#ifdef CONFIG_IA32_EMULATION
1488 if (test_tsk_thread_flag(task, TIF_IA32))
1489#endif
1490#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1491 return &user_x86_32_view;
1492#endif
1493#ifdef CONFIG_X86_64
1494 return &user_x86_64_view;
1495#endif
1496}
1497
Srinivasa Dsda654b72008-09-23 15:23:52 +05301498void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1499 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 struct siginfo info;
1502
1503 tsk->thread.trap_no = 1;
1504 tsk->thread.error_code = error_code;
1505
1506 memset(&info, 0, sizeof(info));
1507 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301508 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001510 /* User-mode ip? */
1511 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Simon Arlott27b46d72007-10-20 01:13:56 +02001513 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 force_sig_info(SIGTRAP, &info, tsk);
1515}
1516
Roland McGrath86976cd2008-01-30 13:31:01 +01001517
Roland McGrathd4d67152008-07-09 02:38:07 -07001518#ifdef CONFIG_X86_32
1519# define IS_IA32 1
1520#elif defined CONFIG_IA32_EMULATION
1521# define IS_IA32 test_thread_flag(TIF_IA32)
1522#else
1523# define IS_IA32 0
1524#endif
1525
1526/*
1527 * We must return the syscall number to actually look up in the table.
1528 * This can be -1L to skip running any syscall at all.
1529 */
1530asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001531{
Roland McGrathd4d67152008-07-09 02:38:07 -07001532 long ret = 0;
1533
Roland McGrath380fdd72008-07-09 02:39:29 -07001534 /*
1535 * If we stepped into a sysenter/syscall insn, it trapped in
1536 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1537 * If user-mode had set TF itself, then it's still clear from
1538 * do_debug() and we need to set it again to restore the user
1539 * state. If we entered on the slow path, TF was already set.
1540 */
1541 if (test_thread_flag(TIF_SINGLESTEP))
1542 regs->flags |= X86_EFLAGS_TF;
1543
Roland McGrath86976cd2008-01-30 13:31:01 +01001544 /* do the secure computing check first */
1545 secure_computing(regs->orig_ax);
1546
Roland McGrathd4d67152008-07-09 02:38:07 -07001547 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1548 ret = -1L;
1549
Roland McGratheeea3c32008-03-16 23:36:28 -07001550 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1551 tracehook_report_syscall_entry(regs))
1552 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001553
1554 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001555 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001556 audit_syscall_entry(AUDIT_ARCH_I386,
1557 regs->orig_ax,
1558 regs->bx, regs->cx,
1559 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001560#ifdef CONFIG_X86_64
1561 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001562 audit_syscall_entry(AUDIT_ARCH_X86_64,
1563 regs->orig_ax,
1564 regs->di, regs->si,
1565 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001566#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001567 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001568
1569 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001570}
1571
Roland McGrathd4d67152008-07-09 02:38:07 -07001572asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001573{
1574 if (unlikely(current->audit_context))
1575 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1576
Roland McGrathd4d67152008-07-09 02:38:07 -07001577 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001578 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001579
Roland McGrathd4d67152008-07-09 02:38:07 -07001580 /*
1581 * If TIF_SYSCALL_EMU is set, we only get here because of
1582 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1583 * We already reported this syscall instruction in
1584 * syscall_trace_enter(), so don't do any more now.
1585 */
1586 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1587 return;
1588
1589 /*
1590 * If we are single-stepping, synthesize a trap to follow the
1591 * system call instruction.
1592 */
1593 if (test_thread_flag(TIF_SINGLESTEP) &&
Roland McGratheeea3c32008-03-16 23:36:28 -07001594 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301595 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001596}