blob: adbb24322d8f2beb496ff028c0749466fa590ebd [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>
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +010024#include <linux/ftrace.h>
Markus Metzgere2b371f2009-04-03 16:43:35 +020025#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/uaccess.h>
28#include <asm/pgtable.h>
29#include <asm/system.h>
30#include <asm/processor.h>
31#include <asm/i387.h>
32#include <asm/debugreg.h>
33#include <asm/ldt.h>
34#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010035#include <asm/prctl.h>
36#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010037#include <asm/ds.h>
38
Roland McGrath070459d2008-01-30 13:31:53 +010039#include "tls.h"
40
41enum x86_regset {
42 REGSET_GENERAL,
43 REGSET_FP,
44 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070045 REGSET_IOPERM64 = REGSET_XFP,
Roland McGrath070459d2008-01-30 13:31:53 +010046 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070047 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010048};
Markus Metzgereee3af42008-01-30 13:31:09 +010049
50/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * does not yet catch signals sent when the child dies.
52 * in exit.c or in signal.c.
53 */
54
Chuck Ebbert9f155b92006-01-05 23:11:29 -050055/*
56 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050057 */
Roland McGrathe39c2892008-01-30 13:31:01 +010058#define FLAG_MASK_32 ((unsigned long) \
59 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
60 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
61 X86_EFLAGS_SF | X86_EFLAGS_TF | \
62 X86_EFLAGS_DF | X86_EFLAGS_OF | \
63 X86_EFLAGS_RF | X86_EFLAGS_AC))
64
Roland McGrath2047b082008-01-30 13:31:01 +010065/*
66 * Determines whether a value may be installed in a segment register.
67 */
68static inline bool invalid_selector(u16 value)
69{
70 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
71}
72
73#ifdef CONFIG_X86_32
74
Roland McGrathe39c2892008-01-30 13:31:01 +010075#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jaswinder Singh4fe702c2008-07-25 10:19:27 +053077static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010079 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +090080 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Roland McGrath06ee1b62008-01-30 13:31:01 +010083static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Roland McGrath06ee1b62008-01-30 13:31:01 +010085 /*
86 * Returning the value truncates it to 16 bits.
87 */
88 unsigned int retval;
89 if (offset != offsetof(struct user_regs_struct, gs))
90 retval = *pt_regs_access(task_pt_regs(task), offset);
91 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +010092 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +090093 retval = get_user_gs(task_pt_regs(task));
94 else
95 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +010096 }
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 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900130 set_user_gs(task_pt_regs(task), value);
131 else
132 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
Roland McGrath2047b082008-01-30 13:31:01 +0100138static unsigned long debugreg_addr_limit(struct task_struct *task)
139{
140 return TASK_SIZE - 3;
141}
142
143#else /* CONFIG_X86_64 */
144
145#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
146
147static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
148{
149 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
150 return &regs->r15 + (offset / sizeof(regs->r15));
151}
152
153static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
154{
155 /*
156 * Returning the value truncates it to 16 bits.
157 */
158 unsigned int seg;
159
160 switch (offset) {
161 case offsetof(struct user_regs_struct, fs):
162 if (task == current) {
163 /* Older gas can't assemble movq %?s,%r?? */
164 asm("movl %%fs,%0" : "=r" (seg));
165 return seg;
166 }
167 return task->thread.fsindex;
168 case offsetof(struct user_regs_struct, gs):
169 if (task == current) {
170 asm("movl %%gs,%0" : "=r" (seg));
171 return seg;
172 }
173 return task->thread.gsindex;
174 case offsetof(struct user_regs_struct, ds):
175 if (task == current) {
176 asm("movl %%ds,%0" : "=r" (seg));
177 return seg;
178 }
179 return task->thread.ds;
180 case offsetof(struct user_regs_struct, es):
181 if (task == current) {
182 asm("movl %%es,%0" : "=r" (seg));
183 return seg;
184 }
185 return task->thread.es;
186
187 case offsetof(struct user_regs_struct, cs):
188 case offsetof(struct user_regs_struct, ss):
189 break;
190 }
191 return *pt_regs_access(task_pt_regs(task), offset);
192}
193
194static int set_segment_reg(struct task_struct *task,
195 unsigned long offset, u16 value)
196{
197 /*
198 * The value argument was already truncated to 16 bits.
199 */
200 if (invalid_selector(value))
201 return -EIO;
202
203 switch (offset) {
204 case offsetof(struct user_regs_struct,fs):
205 /*
206 * If this is setting fs as for normal 64-bit use but
207 * setting fs_base has implicitly changed it, leave it.
208 */
209 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
210 task->thread.fs != 0) ||
211 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
212 task->thread.fs == 0))
213 break;
214 task->thread.fsindex = value;
215 if (task == current)
216 loadsegment(fs, task->thread.fsindex);
217 break;
218 case offsetof(struct user_regs_struct,gs):
219 /*
220 * If this is setting gs as for normal 64-bit use but
221 * setting gs_base has implicitly changed it, leave it.
222 */
223 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
224 task->thread.gs != 0) ||
225 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
226 task->thread.gs == 0))
227 break;
228 task->thread.gsindex = value;
229 if (task == current)
230 load_gs_index(task->thread.gsindex);
231 break;
232 case offsetof(struct user_regs_struct,ds):
233 task->thread.ds = value;
234 if (task == current)
235 loadsegment(ds, task->thread.ds);
236 break;
237 case offsetof(struct user_regs_struct,es):
238 task->thread.es = value;
239 if (task == current)
240 loadsegment(es, task->thread.es);
241 break;
242
243 /*
244 * Can't actually change these in 64-bit mode.
245 */
246 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100247 if (unlikely(value == 0))
248 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100249#ifdef CONFIG_IA32_EMULATION
250 if (test_tsk_thread_flag(task, TIF_IA32))
251 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100252#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100253 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100254 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100255 if (unlikely(value == 0))
256 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100257#ifdef CONFIG_IA32_EMULATION
258 if (test_tsk_thread_flag(task, TIF_IA32))
259 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100260#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100261 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100262 }
263
264 return 0;
265}
266
267static unsigned long debugreg_addr_limit(struct task_struct *task)
268{
269#ifdef CONFIG_IA32_EMULATION
270 if (test_tsk_thread_flag(task, TIF_IA32))
271 return IA32_PAGE_OFFSET - 3;
272#endif
Ingo Molnard9517342009-02-20 23:32:28 +0100273 return TASK_SIZE_MAX - 7;
Roland McGrath2047b082008-01-30 13:31:01 +0100274}
275
276#endif /* CONFIG_X86_32 */
277
Roland McGrath06ee1b62008-01-30 13:31:01 +0100278static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100280 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Roland McGrath06ee1b62008-01-30 13:31:01 +0100282 /*
283 * If the debugger set TF, hide it from the readout.
284 */
285 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
286 retval &= ~X86_EFLAGS_TF;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return retval;
289}
290
Roland McGrath06ee1b62008-01-30 13:31:01 +0100291static int set_flags(struct task_struct *task, unsigned long value)
292{
293 struct pt_regs *regs = task_pt_regs(task);
294
295 /*
296 * If the user value contains TF, mark that
297 * it was not "us" (the debugger) that set it.
298 * If not, make sure it stays set if we had.
299 */
300 if (value & X86_EFLAGS_TF)
301 clear_tsk_thread_flag(task, TIF_FORCED_TF);
302 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
303 value |= X86_EFLAGS_TF;
304
305 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
306
307 return 0;
308}
309
310static int putreg(struct task_struct *child,
311 unsigned long offset, unsigned long value)
312{
313 switch (offset) {
314 case offsetof(struct user_regs_struct, cs):
315 case offsetof(struct user_regs_struct, ds):
316 case offsetof(struct user_regs_struct, es):
317 case offsetof(struct user_regs_struct, fs):
318 case offsetof(struct user_regs_struct, gs):
319 case offsetof(struct user_regs_struct, ss):
320 return set_segment_reg(child, offset, value);
321
322 case offsetof(struct user_regs_struct, flags):
323 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100324
325#ifdef CONFIG_X86_64
Roland McGrath84c6f602008-03-07 14:56:02 -0800326 /*
327 * Orig_ax is really just a flag with small positive and
328 * negative values, so make sure to always sign-extend it
329 * from 32 bits so that it works correctly regardless of
330 * whether we come from a 32-bit environment or not.
331 */
332 case offsetof(struct user_regs_struct, orig_ax):
333 value = (long) (s32) value;
334 break;
335
Roland McGrath2047b082008-01-30 13:31:01 +0100336 case offsetof(struct user_regs_struct,fs_base):
337 if (value >= TASK_SIZE_OF(child))
338 return -EIO;
339 /*
340 * When changing the segment base, use do_arch_prctl
341 * to set either thread.fs or thread.fsindex and the
342 * corresponding GDT slot.
343 */
344 if (child->thread.fs != value)
345 return do_arch_prctl(child, ARCH_SET_FS, value);
346 return 0;
347 case offsetof(struct user_regs_struct,gs_base):
348 /*
349 * Exactly the same here as the %fs handling above.
350 */
351 if (value >= TASK_SIZE_OF(child))
352 return -EIO;
353 if (child->thread.gs != value)
354 return do_arch_prctl(child, ARCH_SET_GS, value);
355 return 0;
356#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100357 }
358
359 *pt_regs_access(task_pt_regs(child), offset) = value;
360 return 0;
361}
362
363static unsigned long getreg(struct task_struct *task, unsigned long offset)
364{
365 switch (offset) {
366 case offsetof(struct user_regs_struct, cs):
367 case offsetof(struct user_regs_struct, ds):
368 case offsetof(struct user_regs_struct, es):
369 case offsetof(struct user_regs_struct, fs):
370 case offsetof(struct user_regs_struct, gs):
371 case offsetof(struct user_regs_struct, ss):
372 return get_segment_reg(task, offset);
373
374 case offsetof(struct user_regs_struct, flags):
375 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100376
377#ifdef CONFIG_X86_64
378 case offsetof(struct user_regs_struct, fs_base): {
379 /*
380 * do_arch_prctl may have used a GDT slot instead of
381 * the MSR. To userland, it appears the same either
382 * way, except the %fs segment selector might not be 0.
383 */
384 unsigned int seg = task->thread.fsindex;
385 if (task->thread.fs != 0)
386 return task->thread.fs;
387 if (task == current)
388 asm("movl %%fs,%0" : "=r" (seg));
389 if (seg != FS_TLS_SEL)
390 return 0;
391 return get_desc_base(&task->thread.tls_array[FS_TLS]);
392 }
393 case offsetof(struct user_regs_struct, gs_base): {
394 /*
395 * Exactly the same here as the %fs handling above.
396 */
397 unsigned int seg = task->thread.gsindex;
398 if (task->thread.gs != 0)
399 return task->thread.gs;
400 if (task == current)
401 asm("movl %%gs,%0" : "=r" (seg));
402 if (seg != GS_TLS_SEL)
403 return 0;
404 return get_desc_base(&task->thread.tls_array[GS_TLS]);
405 }
406#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100407 }
408
409 return *pt_regs_access(task_pt_regs(task), offset);
410}
411
Roland McGrath91e7b702008-01-30 13:31:52 +0100412static int genregs_get(struct task_struct *target,
413 const struct user_regset *regset,
414 unsigned int pos, unsigned int count,
415 void *kbuf, void __user *ubuf)
416{
417 if (kbuf) {
418 unsigned long *k = kbuf;
419 while (count > 0) {
420 *k++ = getreg(target, pos);
421 count -= sizeof(*k);
422 pos += sizeof(*k);
423 }
424 } else {
425 unsigned long __user *u = ubuf;
426 while (count > 0) {
427 if (__put_user(getreg(target, pos), u++))
428 return -EFAULT;
429 count -= sizeof(*u);
430 pos += sizeof(*u);
431 }
432 }
433
434 return 0;
435}
436
437static int genregs_set(struct task_struct *target,
438 const struct user_regset *regset,
439 unsigned int pos, unsigned int count,
440 const void *kbuf, const void __user *ubuf)
441{
442 int ret = 0;
443 if (kbuf) {
444 const unsigned long *k = kbuf;
445 while (count > 0 && !ret) {
446 ret = putreg(target, pos, *k++);
447 count -= sizeof(*k);
448 pos += sizeof(*k);
449 }
450 } else {
451 const unsigned long __user *u = ubuf;
452 while (count > 0 && !ret) {
453 unsigned long word;
454 ret = __get_user(word, u++);
455 if (ret)
456 break;
457 ret = putreg(target, pos, word);
458 count -= sizeof(*u);
459 pos += sizeof(*u);
460 }
461 }
462 return ret;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100466 * This function is trivial and will be inlined by the compiler.
467 * Having it separates the implementation details of debug
468 * registers from the interface details of ptrace.
469 */
470static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
471{
Roland McGrath0f534092008-01-30 13:30:59 +0100472 switch (n) {
473 case 0: return child->thread.debugreg0;
474 case 1: return child->thread.debugreg1;
475 case 2: return child->thread.debugreg2;
476 case 3: return child->thread.debugreg3;
477 case 6: return child->thread.debugreg6;
478 case 7: return child->thread.debugreg7;
479 }
480 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100481}
482
483static int ptrace_set_debugreg(struct task_struct *child,
484 int n, unsigned long data)
485{
Roland McGrath0f534092008-01-30 13:30:59 +0100486 int i;
487
Roland McGrathd9771e82008-01-30 13:30:52 +0100488 if (unlikely(n == 4 || n == 5))
489 return -EIO;
490
Roland McGrath2047b082008-01-30 13:31:01 +0100491 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100492 return -EIO;
493
Roland McGrath0f534092008-01-30 13:30:59 +0100494 switch (n) {
495 case 0: child->thread.debugreg0 = data; break;
496 case 1: child->thread.debugreg1 = data; break;
497 case 2: child->thread.debugreg2 = data; break;
498 case 3: child->thread.debugreg3 = data; break;
499
500 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100501 if ((data & ~0xffffffffUL) != 0)
502 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100503 child->thread.debugreg6 = data;
504 break;
505
506 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100507 /*
508 * Sanity-check data. Take one half-byte at once with
509 * check = (val >> (16 + 4*i)) & 0xf. It contains the
510 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
511 * 2 and 3 are LENi. Given a list of invalid values,
512 * we do mask |= 1 << invalid_value, so that
513 * (mask >> check) & 1 is a correct test for invalid
514 * values.
515 *
516 * R/Wi contains the type of the breakpoint /
517 * watchpoint, LENi contains the length of the watched
518 * data in the watchpoint case.
519 *
520 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100521 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100522 * - R/Wi == 0x10 (break on I/O reads or writes), so
523 * mask |= 0x4444.
524 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
525 * 0x1110.
526 *
527 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
528 *
529 * See the Intel Manual "System Programming Guide",
530 * 15.2.4
531 *
532 * Note that LENi == 0x10 is defined on x86_64 in long
533 * mode (i.e. even for 32-bit userspace software, but
534 * 64-bit kernel), so the x86_64 mask value is 0x5454.
535 * See the AMD manual no. 24593 (AMD64 System Programming)
536 */
Roland McGrath2047b082008-01-30 13:31:01 +0100537#ifdef CONFIG_X86_32
538#define DR7_MASK 0x5f54
539#else
540#define DR7_MASK 0x5554
541#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100542 data &= ~DR_CONTROL_RESERVED;
543 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100544 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100545 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100546 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100547 if (data)
548 set_tsk_thread_flag(child, TIF_DEBUG);
549 else
550 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100551 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100552 }
553
Roland McGrathd9771e82008-01-30 13:30:52 +0100554 return 0;
555}
556
Roland McGrath325af5f2008-08-08 15:58:39 -0700557/*
558 * These access the current or another (stopped) task's io permission
559 * bitmap for debugging or core dump.
560 */
561static int ioperm_active(struct task_struct *target,
562 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100563{
Roland McGrath325af5f2008-08-08 15:58:39 -0700564 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100565}
566
Roland McGrath325af5f2008-08-08 15:58:39 -0700567static int ioperm_get(struct task_struct *target,
568 const struct user_regset *regset,
569 unsigned int pos, unsigned int count,
570 void *kbuf, void __user *ubuf)
571{
572 if (!target->thread.io_bitmap_ptr)
573 return -ENXIO;
574
575 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
576 target->thread.io_bitmap_ptr,
577 0, IO_BITMAP_BYTES);
578}
579
Markus Metzger93fa7632008-04-08 11:01:58 +0200580#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200581/*
582 * A branch trace store context.
583 *
584 * Contexts may only be installed by ptrace_bts_config() and only for
585 * ptraced tasks.
586 *
587 * Contexts are destroyed when the tracee is detached from the tracer.
588 * The actual destruction work requires interrupts enabled, so the
589 * work is deferred and will be scheduled during __ptrace_unlink().
590 *
591 * Contexts hold an additional task_struct reference on the traced
592 * task, as well as a reference on the tracer's mm.
593 *
594 * Ptrace already holds a task_struct for the duration of ptrace operations,
595 * but since destruction is deferred, it may be executed after both
596 * tracer and tracee exited.
597 */
598struct bts_context {
599 /* The branch trace handle. */
600 struct bts_tracer *tracer;
601
602 /* The buffer used to store the branch trace and its size. */
603 void *buffer;
604 unsigned int size;
605
606 /* The mm that paid for the above buffer. */
607 struct mm_struct *mm;
608
609 /* The task this context belongs to. */
610 struct task_struct *task;
611
612 /* The signal to send on a bts buffer overflow. */
613 unsigned int bts_ovfl_signal;
614
615 /* The work struct to destroy a context. */
616 struct work_struct work;
617};
618
619static inline void alloc_bts_buffer(struct bts_context *context,
620 unsigned int size)
621{
622 void *buffer;
623
624 buffer = alloc_locked_buffer(size);
625 if (buffer) {
626 context->buffer = buffer;
627 context->size = size;
628 context->mm = get_task_mm(current);
629 }
630}
631
632static inline void free_bts_buffer(struct bts_context *context)
633{
634 if (!context->buffer)
635 return;
636
637 kfree(context->buffer);
638 context->buffer = NULL;
639
640 refund_locked_buffer_memory(context->mm, context->size);
641 context->size = 0;
642
643 mmput(context->mm);
644 context->mm = NULL;
645}
646
647static void free_bts_context_work(struct work_struct *w)
648{
649 struct bts_context *context;
650
651 context = container_of(w, struct bts_context, work);
652
653 ds_release_bts(context->tracer);
654 put_task_struct(context->task);
655 free_bts_buffer(context);
656 kfree(context);
657}
658
659static inline void free_bts_context(struct bts_context *context)
660{
661 INIT_WORK(&context->work, free_bts_context_work);
662 schedule_work(&context->work);
663}
664
665static inline struct bts_context *alloc_bts_context(struct task_struct *task)
666{
667 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
668 if (context) {
669 context->task = task;
670 task->bts = context;
671
672 get_task_struct(task);
673 }
674
675 return context;
676}
677
Markus Metzger93fa7632008-04-08 11:01:58 +0200678static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100679 struct bts_struct __user *out)
680{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200681 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100682 const struct bts_trace *trace;
683 struct bts_struct bts;
684 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200685 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100686
Markus Metzgere2b371f2009-04-03 16:43:35 +0200687 context = child->bts;
688 if (!context)
689 return -ESRCH;
690
691 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100692 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200693 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100694
695 at = trace->ds.top - ((index + 1) * trace->ds.size);
696 if ((void *)at < trace->ds.begin)
697 at += (trace->ds.n * trace->ds.size);
698
699 if (!trace->read)
700 return -EOPNOTSUPP;
701
Markus Metzgere2b371f2009-04-03 16:43:35 +0200702 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200703 if (error < 0)
704 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100705
Markus Metzgerc2724772008-12-11 13:49:59 +0100706 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100707 return -EFAULT;
708
Markus Metzgerc2724772008-12-11 13:49:59 +0100709 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100710}
711
Markus Metzgera95d67f2008-01-30 13:31:20 +0100712static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100713 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100714 struct bts_struct __user *out)
715{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200716 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100717 const struct bts_trace *trace;
718 const unsigned char *at;
719 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100720
Markus Metzgere2b371f2009-04-03 16:43:35 +0200721 context = child->bts;
722 if (!context)
723 return -ESRCH;
724
725 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100726 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200727 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100728
Markus Metzgerc2724772008-12-11 13:49:59 +0100729 if (!trace->read)
730 return -EOPNOTSUPP;
731
732 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100733 return -EIO;
734
Markus Metzgerc2724772008-12-11 13:49:59 +0100735 for (at = trace->ds.begin; (void *)at < trace->ds.top;
736 out++, drained++, at += trace->ds.size) {
737 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100738
Markus Metzgere2b371f2009-04-03 16:43:35 +0200739 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100740 if (error < 0)
741 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100742
Markus Metzgerc2724772008-12-11 13:49:59 +0100743 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100744 return -EFAULT;
745 }
746
Markus Metzgerc2724772008-12-11 13:49:59 +0100747 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
748
Markus Metzgere2b371f2009-04-03 16:43:35 +0200749 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200750 if (error < 0)
751 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100752
Markus Metzgerc2724772008-12-11 13:49:59 +0100753 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100754}
755
756static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100757 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100758 const struct ptrace_bts_config __user *ucfg)
759{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200760 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100761 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100762 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100763
Markus Metzgercba4b652008-01-30 13:32:03 +0100764 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100765 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100766
Markus Metzgera95d67f2008-01-30 13:31:20 +0100767 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100768 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100769
Markus Metzgere2b371f2009-04-03 16:43:35 +0200770 context = child->bts;
771 if (!context)
772 context = alloc_bts_context(child);
773 if (!context)
774 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100775
Markus Metzgerc2724772008-12-11 13:49:59 +0100776 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
777 if (!cfg.signal)
778 return -EINVAL;
779
Américo Wang5a8ac9d2009-03-13 15:56:58 +0800780 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200781 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +0100782 }
783
Markus Metzgere2b371f2009-04-03 16:43:35 +0200784 ds_release_bts(context->tracer);
785 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100786
Markus Metzgere2b371f2009-04-03 16:43:35 +0200787 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
788 free_bts_buffer(context);
789 if (!cfg.size)
790 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +0100791
Markus Metzgere2b371f2009-04-03 16:43:35 +0200792 alloc_bts_buffer(context, cfg.size);
793 if (!context->buffer)
794 return -ENOMEM;
Markus Metzgerc2724772008-12-11 13:49:59 +0100795 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100796
Markus Metzgerda35c372008-01-30 13:32:03 +0100797 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +0100798 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +0100799
Markus Metzgerda35c372008-01-30 13:32:03 +0100800 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +0100801 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +0100802
Markus Metzgerde79f542009-04-03 16:43:40 +0200803 context->tracer =
804 ds_request_bts_task(child, context->buffer, context->size,
805 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200806 if (unlikely(IS_ERR(context->tracer))) {
807 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +0100808
Markus Metzgere2b371f2009-04-03 16:43:35 +0200809 free_bts_buffer(context);
810 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100811 return error;
812 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100813
Markus Metzgerc2724772008-12-11 13:49:59 +0100814 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100815}
816
Markus Metzgera95d67f2008-01-30 13:31:20 +0100817static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100818 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100819 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100820{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200821 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100822 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100823 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100824
Markus Metzgere2b371f2009-04-03 16:43:35 +0200825 context = child->bts;
826 if (!context)
827 return -ESRCH;
828
Markus Metzgercba4b652008-01-30 13:32:03 +0100829 if (cfg_size < sizeof(cfg))
830 return -EIO;
831
Markus Metzgere2b371f2009-04-03 16:43:35 +0200832 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100833 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200834 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +0200835
Markus Metzgera95d67f2008-01-30 13:31:20 +0100836 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +0200837 cfg.size = trace->ds.end - trace->ds.begin;
838 cfg.signal = context->bts_ovfl_signal;
839 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +0100840
Markus Metzger93fa7632008-04-08 11:01:58 +0200841 if (cfg.signal)
842 cfg.flags |= PTRACE_BTS_O_SIGNAL;
843
Markus Metzgerc2724772008-12-11 13:49:59 +0100844 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +0200845 cfg.flags |= PTRACE_BTS_O_TRACE;
846
Markus Metzgerc2724772008-12-11 13:49:59 +0100847 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +0200848 cfg.flags |= PTRACE_BTS_O_SCHED;
849
Markus Metzgera95d67f2008-01-30 13:31:20 +0100850 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
851 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100852
Markus Metzgera95d67f2008-01-30 13:31:20 +0100853 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100854}
855
Markus Metzgerc2724772008-12-11 13:49:59 +0100856static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -0800857{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200858 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100859 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800860
Markus Metzgere2b371f2009-04-03 16:43:35 +0200861 context = child->bts;
862 if (!context)
863 return -ESRCH;
864
865 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100866 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200867 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800868
Markus Metzgerc2724772008-12-11 13:49:59 +0100869 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800870
Markus Metzgere2b371f2009-04-03 16:43:35 +0200871 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800872}
873
Markus Metzgerc2724772008-12-11 13:49:59 +0100874static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100875{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200876 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100877 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +0100878
Markus Metzgere2b371f2009-04-03 16:43:35 +0200879 context = child->bts;
880 if (!context)
881 return -ESRCH;
882
883 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100884 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200885 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +0200886
Markus Metzgerc2724772008-12-11 13:49:59 +0100887 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +0200888}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100889
Markus Metzgere2b371f2009-04-03 16:43:35 +0200890static inline void ptrace_bts_fork(struct task_struct *tsk)
Markus Metzgerbf53de92008-12-19 15:10:24 +0100891{
892 tsk->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +0100893}
894
Markus Metzgere2b371f2009-04-03 16:43:35 +0200895/*
896 * Called from __ptrace_unlink() after the child has been moved back
897 * to its original parent.
898 */
899static inline void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +0100900{
901 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +0200902 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +0100903 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +0100904 }
905}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100906#else
907static inline void ptrace_bts_fork(struct task_struct *tsk) {}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100908static inline void ptrace_bts_untrace(struct task_struct *child) {}
Markus Metzger93fa7632008-04-08 11:01:58 +0200909#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100910
Markus Metzgerbf53de92008-12-19 15:10:24 +0100911void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
912{
913 ptrace_bts_fork(child);
914}
915
916void x86_ptrace_untrace(struct task_struct *child)
917{
918 ptrace_bts_untrace(child);
919}
920
Roland McGrathd9771e82008-01-30 13:30:52 +0100921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Called by kernel/ptrace.c when detaching..
923 *
924 * Make sure the single step bit is not set.
925 */
926void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100927{
Roland McGrath7f232342008-01-30 13:30:48 +0100928 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100929#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700930 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100931#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Roland McGrath5a4646a2008-01-30 13:31:54 +0100934#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
935static const struct user_regset_view user_x86_32_view; /* Initialized below. */
936#endif
937
Christoph Hellwig481bed42005-11-07 00:59:47 -0800938long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100940 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 unsigned long __user *datap = (unsigned long __user *)data;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* read the word at location addr in the USER area. */
945 case PTRACE_PEEKUSR: {
946 unsigned long tmp;
947
948 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100949 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
950 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
952
953 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100954 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100956 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
957 addr <= offsetof(struct user, u_debugreg[7])) {
958 addr -= offsetof(struct user, u_debugreg[0]);
959 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 ret = put_user(tmp, datap);
962 break;
963 }
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
966 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100967 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
968 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
970
Roland McGrathe9c86c72008-01-30 13:31:01 +0100971 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100973 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
974 addr <= offsetof(struct user, u_debugreg[7])) {
975 addr -= offsetof(struct user, u_debugreg[0]);
976 ret = ptrace_set_debugreg(child,
977 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Roland McGrath5a4646a2008-01-30 13:31:54 +0100981 case PTRACE_GETREGS: /* Get all gp regs from the child. */
982 return copy_regset_to_user(child,
983 task_user_regset_view(current),
984 REGSET_GENERAL,
985 0, sizeof(struct user_regs_struct),
986 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Roland McGrath5a4646a2008-01-30 13:31:54 +0100988 case PTRACE_SETREGS: /* Set all gp regs in the child. */
989 return copy_regset_from_user(child,
990 task_user_regset_view(current),
991 REGSET_GENERAL,
992 0, sizeof(struct user_regs_struct),
993 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Roland McGrath5a4646a2008-01-30 13:31:54 +0100995 case PTRACE_GETFPREGS: /* Get the child FPU state. */
996 return copy_regset_to_user(child,
997 task_user_regset_view(current),
998 REGSET_FP,
999 0, sizeof(struct user_i387_struct),
1000 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Roland McGrath5a4646a2008-01-30 13:31:54 +01001002 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1003 return copy_regset_from_user(child,
1004 task_user_regset_view(current),
1005 REGSET_FP,
1006 0, sizeof(struct user_i387_struct),
1007 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Roland McGrathe9c86c72008-01-30 13:31:01 +01001009#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001010 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1011 return copy_regset_to_user(child, &user_x86_32_view,
1012 REGSET_XFP,
1013 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001014 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Roland McGrath5a4646a2008-01-30 13:31:54 +01001016 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1017 return copy_regset_from_user(child, &user_x86_32_view,
1018 REGSET_XFP,
1019 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001020 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Roland McGrathe9c86c72008-01-30 13:31:01 +01001023#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001025 if (addr < 0)
1026 return -EIO;
1027 ret = do_get_thread_area(child, addr,
1028 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
1030
1031 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001032 if (addr < 0)
1033 return -EIO;
1034 ret = do_set_thread_area(child, addr,
1035 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001037#endif
1038
1039#ifdef CONFIG_X86_64
1040 /* normal 64bit interface to access TLS data.
1041 Works just like arch_prctl, except that the arguments
1042 are reversed. */
1043 case PTRACE_ARCH_PRCTL:
1044 ret = do_arch_prctl(child, data, addr);
1045 break;
1046#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001048 /*
1049 * These bits need more cooking - not enabled yet:
1050 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001051#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001052 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001053 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001054 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001055 break;
1056
1057 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001058 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001059 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001060 break;
1061
Markus Metzgerc2724772008-12-11 13:49:59 +01001062 case PTRACE_BTS_SIZE:
1063 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001064 break;
1065
1066 case PTRACE_BTS_GET:
1067 ret = ptrace_bts_read_record
1068 (child, data, (struct bts_struct __user *) addr);
1069 break;
1070
1071 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001072 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001073 break;
1074
1075 case PTRACE_BTS_DRAIN:
1076 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001077 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001078 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001079#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 default:
1082 ret = ptrace_request(child, request, addr, data);
1083 break;
1084 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return ret;
1087}
1088
Roland McGrathcb757c42008-01-30 13:31:01 +01001089#ifdef CONFIG_IA32_EMULATION
1090
Roland McGrath099cd6e2008-01-30 13:31:01 +01001091#include <linux/compat.h>
1092#include <linux/syscalls.h>
1093#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001094#include <asm/user32.h>
1095
1096#define R32(l,q) \
1097 case offsetof(struct user32, regs.l): \
1098 regs->q = value; break
1099
1100#define SEG32(rs) \
1101 case offsetof(struct user32, regs.rs): \
1102 return set_segment_reg(child, \
1103 offsetof(struct user_regs_struct, rs), \
1104 value); \
1105 break
1106
1107static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1108{
1109 struct pt_regs *regs = task_pt_regs(child);
1110
1111 switch (regno) {
1112
1113 SEG32(cs);
1114 SEG32(ds);
1115 SEG32(es);
1116 SEG32(fs);
1117 SEG32(gs);
1118 SEG32(ss);
1119
1120 R32(ebx, bx);
1121 R32(ecx, cx);
1122 R32(edx, dx);
1123 R32(edi, di);
1124 R32(esi, si);
1125 R32(ebp, bp);
1126 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001127 R32(eip, ip);
1128 R32(esp, sp);
1129
Roland McGrath40f09332008-02-28 19:57:07 -08001130 case offsetof(struct user32, regs.orig_eax):
1131 /*
1132 * Sign-extend the value so that orig_eax = -1
1133 * causes (long)orig_ax < 0 tests to fire correctly.
1134 */
1135 regs->orig_ax = (long) (s32) value;
1136 break;
1137
Roland McGrathcb757c42008-01-30 13:31:01 +01001138 case offsetof(struct user32, regs.eflags):
1139 return set_flags(child, value);
1140
1141 case offsetof(struct user32, u_debugreg[0]) ...
1142 offsetof(struct user32, u_debugreg[7]):
1143 regno -= offsetof(struct user32, u_debugreg[0]);
1144 return ptrace_set_debugreg(child, regno / 4, value);
1145
1146 default:
1147 if (regno > sizeof(struct user32) || (regno & 3))
1148 return -EIO;
1149
1150 /*
1151 * Other dummy fields in the virtual user structure
1152 * are ignored
1153 */
1154 break;
1155 }
1156 return 0;
1157}
1158
1159#undef R32
1160#undef SEG32
1161
1162#define R32(l,q) \
1163 case offsetof(struct user32, regs.l): \
1164 *val = regs->q; break
1165
1166#define SEG32(rs) \
1167 case offsetof(struct user32, regs.rs): \
1168 *val = get_segment_reg(child, \
1169 offsetof(struct user_regs_struct, rs)); \
1170 break
1171
1172static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1173{
1174 struct pt_regs *regs = task_pt_regs(child);
1175
1176 switch (regno) {
1177
1178 SEG32(ds);
1179 SEG32(es);
1180 SEG32(fs);
1181 SEG32(gs);
1182
1183 R32(cs, cs);
1184 R32(ss, ss);
1185 R32(ebx, bx);
1186 R32(ecx, cx);
1187 R32(edx, dx);
1188 R32(edi, di);
1189 R32(esi, si);
1190 R32(ebp, bp);
1191 R32(eax, ax);
1192 R32(orig_eax, orig_ax);
1193 R32(eip, ip);
1194 R32(esp, sp);
1195
1196 case offsetof(struct user32, regs.eflags):
1197 *val = get_flags(child);
1198 break;
1199
1200 case offsetof(struct user32, u_debugreg[0]) ...
1201 offsetof(struct user32, u_debugreg[7]):
1202 regno -= offsetof(struct user32, u_debugreg[0]);
1203 *val = ptrace_get_debugreg(child, regno / 4);
1204 break;
1205
1206 default:
1207 if (regno > sizeof(struct user32) || (regno & 3))
1208 return -EIO;
1209
1210 /*
1211 * Other dummy fields in the virtual user structure
1212 * are ignored
1213 */
1214 *val = 0;
1215 break;
1216 }
1217 return 0;
1218}
1219
1220#undef R32
1221#undef SEG32
1222
Roland McGrath91e7b702008-01-30 13:31:52 +01001223static int genregs32_get(struct task_struct *target,
1224 const struct user_regset *regset,
1225 unsigned int pos, unsigned int count,
1226 void *kbuf, void __user *ubuf)
1227{
1228 if (kbuf) {
1229 compat_ulong_t *k = kbuf;
1230 while (count > 0) {
1231 getreg32(target, pos, k++);
1232 count -= sizeof(*k);
1233 pos += sizeof(*k);
1234 }
1235 } else {
1236 compat_ulong_t __user *u = ubuf;
1237 while (count > 0) {
1238 compat_ulong_t word;
1239 getreg32(target, pos, &word);
1240 if (__put_user(word, u++))
1241 return -EFAULT;
1242 count -= sizeof(*u);
1243 pos += sizeof(*u);
1244 }
1245 }
1246
1247 return 0;
1248}
1249
1250static int genregs32_set(struct task_struct *target,
1251 const struct user_regset *regset,
1252 unsigned int pos, unsigned int count,
1253 const void *kbuf, const void __user *ubuf)
1254{
1255 int ret = 0;
1256 if (kbuf) {
1257 const compat_ulong_t *k = kbuf;
1258 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001259 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001260 count -= sizeof(*k);
1261 pos += sizeof(*k);
1262 }
1263 } else {
1264 const compat_ulong_t __user *u = ubuf;
1265 while (count > 0 && !ret) {
1266 compat_ulong_t word;
1267 ret = __get_user(word, u++);
1268 if (ret)
1269 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001270 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001271 count -= sizeof(*u);
1272 pos += sizeof(*u);
1273 }
1274 }
1275 return ret;
1276}
1277
Roland McGrath562b80b2008-04-22 12:21:25 -07001278long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1279 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001280{
Roland McGrath562b80b2008-04-22 12:21:25 -07001281 unsigned long addr = caddr;
1282 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001283 void __user *datap = compat_ptr(data);
1284 int ret;
1285 __u32 val;
1286
1287 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001288 case PTRACE_PEEKUSR:
1289 ret = getreg32(child, addr, &val);
1290 if (ret == 0)
1291 ret = put_user(val, (__u32 __user *)datap);
1292 break;
1293
1294 case PTRACE_POKEUSR:
1295 ret = putreg32(child, addr, data);
1296 break;
1297
Roland McGrath5a4646a2008-01-30 13:31:54 +01001298 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1299 return copy_regset_to_user(child, &user_x86_32_view,
1300 REGSET_GENERAL,
1301 0, sizeof(struct user_regs_struct32),
1302 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001303
Roland McGrath5a4646a2008-01-30 13:31:54 +01001304 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1305 return copy_regset_from_user(child, &user_x86_32_view,
1306 REGSET_GENERAL, 0,
1307 sizeof(struct user_regs_struct32),
1308 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001309
Roland McGrath5a4646a2008-01-30 13:31:54 +01001310 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1311 return copy_regset_to_user(child, &user_x86_32_view,
1312 REGSET_FP, 0,
1313 sizeof(struct user_i387_ia32_struct),
1314 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001315
Roland McGrath5a4646a2008-01-30 13:31:54 +01001316 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1317 return copy_regset_from_user(
1318 child, &user_x86_32_view, REGSET_FP,
1319 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001320
Roland McGrath5a4646a2008-01-30 13:31:54 +01001321 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1322 return copy_regset_to_user(child, &user_x86_32_view,
1323 REGSET_XFP, 0,
1324 sizeof(struct user32_fxsr_struct),
1325 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001326
Roland McGrath5a4646a2008-01-30 13:31:54 +01001327 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1328 return copy_regset_from_user(child, &user_x86_32_view,
1329 REGSET_XFP, 0,
1330 sizeof(struct user32_fxsr_struct),
1331 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001332
Roland McGrath562b80b2008-04-22 12:21:25 -07001333 case PTRACE_GET_THREAD_AREA:
1334 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001335#ifdef CONFIG_X86_PTRACE_BTS
1336 case PTRACE_BTS_CONFIG:
1337 case PTRACE_BTS_STATUS:
1338 case PTRACE_BTS_SIZE:
1339 case PTRACE_BTS_GET:
1340 case PTRACE_BTS_CLEAR:
1341 case PTRACE_BTS_DRAIN:
1342#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001343 return arch_ptrace(child, request, addr, data);
1344
Roland McGrath099cd6e2008-01-30 13:31:01 +01001345 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001346 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001347 }
1348
Roland McGrath099cd6e2008-01-30 13:31:01 +01001349 return ret;
1350}
1351
Roland McGrathcb757c42008-01-30 13:31:01 +01001352#endif /* CONFIG_IA32_EMULATION */
1353
Roland McGrath070459d2008-01-30 13:31:53 +01001354#ifdef CONFIG_X86_64
1355
1356static const struct user_regset x86_64_regsets[] = {
1357 [REGSET_GENERAL] = {
1358 .core_note_type = NT_PRSTATUS,
1359 .n = sizeof(struct user_regs_struct) / sizeof(long),
1360 .size = sizeof(long), .align = sizeof(long),
1361 .get = genregs_get, .set = genregs_set
1362 },
1363 [REGSET_FP] = {
1364 .core_note_type = NT_PRFPREG,
1365 .n = sizeof(struct user_i387_struct) / sizeof(long),
1366 .size = sizeof(long), .align = sizeof(long),
1367 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1368 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001369 [REGSET_IOPERM64] = {
1370 .core_note_type = NT_386_IOPERM,
1371 .n = IO_BITMAP_LONGS,
1372 .size = sizeof(long), .align = sizeof(long),
1373 .active = ioperm_active, .get = ioperm_get
1374 },
Roland McGrath070459d2008-01-30 13:31:53 +01001375};
1376
1377static const struct user_regset_view user_x86_64_view = {
1378 .name = "x86_64", .e_machine = EM_X86_64,
1379 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1380};
1381
1382#else /* CONFIG_X86_32 */
1383
1384#define user_regs_struct32 user_regs_struct
1385#define genregs32_get genregs_get
1386#define genregs32_set genregs_set
1387
Roland McGrath1f465f42008-05-09 15:43:44 -07001388#define user_i387_ia32_struct user_i387_struct
1389#define user32_fxsr_struct user_fxsr_struct
1390
Roland McGrath070459d2008-01-30 13:31:53 +01001391#endif /* CONFIG_X86_64 */
1392
1393#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1394static const struct user_regset x86_32_regsets[] = {
1395 [REGSET_GENERAL] = {
1396 .core_note_type = NT_PRSTATUS,
1397 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1398 .size = sizeof(u32), .align = sizeof(u32),
1399 .get = genregs32_get, .set = genregs32_set
1400 },
1401 [REGSET_FP] = {
1402 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001403 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001404 .size = sizeof(u32), .align = sizeof(u32),
1405 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1406 },
1407 [REGSET_XFP] = {
1408 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001409 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001410 .size = sizeof(u32), .align = sizeof(u32),
1411 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1412 },
1413 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001414 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001415 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1416 .size = sizeof(struct user_desc),
1417 .align = sizeof(struct user_desc),
1418 .active = regset_tls_active,
1419 .get = regset_tls_get, .set = regset_tls_set
1420 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001421 [REGSET_IOPERM32] = {
1422 .core_note_type = NT_386_IOPERM,
1423 .n = IO_BITMAP_BYTES / sizeof(u32),
1424 .size = sizeof(u32), .align = sizeof(u32),
1425 .active = ioperm_active, .get = ioperm_get
1426 },
Roland McGrath070459d2008-01-30 13:31:53 +01001427};
1428
1429static const struct user_regset_view user_x86_32_view = {
1430 .name = "i386", .e_machine = EM_386,
1431 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1432};
1433#endif
1434
1435const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1436{
1437#ifdef CONFIG_IA32_EMULATION
1438 if (test_tsk_thread_flag(task, TIF_IA32))
1439#endif
1440#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1441 return &user_x86_32_view;
1442#endif
1443#ifdef CONFIG_X86_64
1444 return &user_x86_64_view;
1445#endif
1446}
1447
Srinivasa Dsda654b72008-09-23 15:23:52 +05301448void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1449 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct siginfo info;
1452
1453 tsk->thread.trap_no = 1;
1454 tsk->thread.error_code = error_code;
1455
1456 memset(&info, 0, sizeof(info));
1457 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301458 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001460 /* User-mode ip? */
1461 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Simon Arlott27b46d72007-10-20 01:13:56 +02001463 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 force_sig_info(SIGTRAP, &info, tsk);
1465}
1466
Roland McGrath86976cd2008-01-30 13:31:01 +01001467
Roland McGrathd4d67152008-07-09 02:38:07 -07001468#ifdef CONFIG_X86_32
1469# define IS_IA32 1
1470#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001471# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001472#else
1473# define IS_IA32 0
1474#endif
1475
1476/*
1477 * We must return the syscall number to actually look up in the table.
1478 * This can be -1L to skip running any syscall at all.
1479 */
1480asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001481{
Roland McGrathd4d67152008-07-09 02:38:07 -07001482 long ret = 0;
1483
Roland McGrath380fdd72008-07-09 02:39:29 -07001484 /*
1485 * If we stepped into a sysenter/syscall insn, it trapped in
1486 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1487 * If user-mode had set TF itself, then it's still clear from
1488 * do_debug() and we need to set it again to restore the user
1489 * state. If we entered on the slow path, TF was already set.
1490 */
1491 if (test_thread_flag(TIF_SINGLESTEP))
1492 regs->flags |= X86_EFLAGS_TF;
1493
Roland McGrath86976cd2008-01-30 13:31:01 +01001494 /* do the secure computing check first */
1495 secure_computing(regs->orig_ax);
1496
Roland McGrathd4d67152008-07-09 02:38:07 -07001497 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1498 ret = -1L;
1499
Roland McGratheeea3c32008-03-16 23:36:28 -07001500 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1501 tracehook_report_syscall_entry(regs))
1502 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001503
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001504 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1505 ftrace_syscall_enter(regs);
1506
Roland McGrath86976cd2008-01-30 13:31:01 +01001507 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001508 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001509 audit_syscall_entry(AUDIT_ARCH_I386,
1510 regs->orig_ax,
1511 regs->bx, regs->cx,
1512 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001513#ifdef CONFIG_X86_64
1514 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001515 audit_syscall_entry(AUDIT_ARCH_X86_64,
1516 regs->orig_ax,
1517 regs->di, regs->si,
1518 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001519#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001520 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001521
1522 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001523}
1524
Roland McGrathd4d67152008-07-09 02:38:07 -07001525asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001526{
1527 if (unlikely(current->audit_context))
1528 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1529
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001530 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1531 ftrace_syscall_exit(regs);
1532
Roland McGrathd4d67152008-07-09 02:38:07 -07001533 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001534 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001535
Roland McGrathd4d67152008-07-09 02:38:07 -07001536 /*
1537 * If TIF_SYSCALL_EMU is set, we only get here because of
1538 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1539 * We already reported this syscall instruction in
1540 * syscall_trace_enter(), so don't do any more now.
1541 */
1542 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1543 return;
1544
1545 /*
1546 * If we are single-stepping, synthesize a trap to follow the
1547 * system call instruction.
1548 */
1549 if (test_thread_flag(TIF_SINGLESTEP) &&
Oleg Nesterov43918f22009-04-02 16:58:00 -07001550 tracehook_consider_fatal_signal(current, SIGTRAP))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301551 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001552}