blob: cabdabce3cb2170e4af1d50ef46ce9a605405a90 [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>
Markus Metzgere2b371f2009-04-03 16:43:35 +020024#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/uaccess.h>
27#include <asm/pgtable.h>
28#include <asm/system.h>
29#include <asm/processor.h>
30#include <asm/i387.h>
31#include <asm/debugreg.h>
32#include <asm/ldt.h>
33#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010034#include <asm/prctl.h>
35#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010036#include <asm/ds.h>
K.Prasad72f674d2009-06-01 23:45:48 +053037#include <asm/hw_breakpoint.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010038
Frederic Weisbecker47788c52009-04-08 20:40:59 +020039#include <trace/syscall.h>
40
Roland McGrath070459d2008-01-30 13:31:53 +010041#include "tls.h"
42
43enum x86_regset {
44 REGSET_GENERAL,
45 REGSET_FP,
46 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070047 REGSET_IOPERM64 = REGSET_XFP,
Roland McGrath070459d2008-01-30 13:31:53 +010048 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070049 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010050};
Markus Metzgereee3af42008-01-30 13:31:09 +010051
52/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * does not yet catch signals sent when the child dies.
54 * in exit.c or in signal.c.
55 */
56
Chuck Ebbert9f155b92006-01-05 23:11:29 -050057/*
58 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050059 */
Roland McGrathe39c2892008-01-30 13:31:01 +010060#define FLAG_MASK_32 ((unsigned long) \
61 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
62 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
63 X86_EFLAGS_SF | X86_EFLAGS_TF | \
64 X86_EFLAGS_DF | X86_EFLAGS_OF | \
65 X86_EFLAGS_RF | X86_EFLAGS_AC))
66
Roland McGrath2047b082008-01-30 13:31:01 +010067/*
68 * Determines whether a value may be installed in a segment register.
69 */
70static inline bool invalid_selector(u16 value)
71{
72 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
73}
74
75#ifdef CONFIG_X86_32
76
Roland McGrathe39c2892008-01-30 13:31:01 +010077#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Jaswinder Singh4fe702c2008-07-25 10:19:27 +053079static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010081 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +090082 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Roland McGrath06ee1b62008-01-30 13:31:01 +010085static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Roland McGrath06ee1b62008-01-30 13:31:01 +010087 /*
88 * Returning the value truncates it to 16 bits.
89 */
90 unsigned int retval;
91 if (offset != offsetof(struct user_regs_struct, gs))
92 retval = *pt_regs_access(task_pt_regs(task), offset);
93 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +010094 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +090095 retval = get_user_gs(task_pt_regs(task));
96 else
97 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +010098 }
99 return retval;
100}
101
102static int set_segment_reg(struct task_struct *task,
103 unsigned long offset, u16 value)
104{
105 /*
106 * The value argument was already truncated to 16 bits.
107 */
Roland McGrath2047b082008-01-30 13:31:01 +0100108 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100109 return -EIO;
110
Roland McGrathc63855d2008-02-06 22:39:44 +0100111 /*
112 * For %cs and %ss we cannot permit a null selector.
113 * We can permit a bogus selector as long as it has USER_RPL.
114 * Null selectors are fine for other segment registers, but
115 * we will never get back to user mode with invalid %cs or %ss
116 * and will take the trap in iret instead. Much code relies
117 * on user_mode() to distinguish a user trap frame (which can
118 * safely use invalid selectors) from a kernel trap frame.
119 */
120 switch (offset) {
121 case offsetof(struct user_regs_struct, cs):
122 case offsetof(struct user_regs_struct, ss):
123 if (unlikely(value == 0))
124 return -EIO;
125
126 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100127 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100128 break;
129
130 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100131 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900132 set_user_gs(task_pt_regs(task), value);
133 else
134 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138}
139
Roland McGrath2047b082008-01-30 13:31:01 +0100140#else /* CONFIG_X86_64 */
141
142#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
143
144static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
145{
146 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
147 return &regs->r15 + (offset / sizeof(regs->r15));
148}
149
150static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
151{
152 /*
153 * Returning the value truncates it to 16 bits.
154 */
155 unsigned int seg;
156
157 switch (offset) {
158 case offsetof(struct user_regs_struct, fs):
159 if (task == current) {
160 /* Older gas can't assemble movq %?s,%r?? */
161 asm("movl %%fs,%0" : "=r" (seg));
162 return seg;
163 }
164 return task->thread.fsindex;
165 case offsetof(struct user_regs_struct, gs):
166 if (task == current) {
167 asm("movl %%gs,%0" : "=r" (seg));
168 return seg;
169 }
170 return task->thread.gsindex;
171 case offsetof(struct user_regs_struct, ds):
172 if (task == current) {
173 asm("movl %%ds,%0" : "=r" (seg));
174 return seg;
175 }
176 return task->thread.ds;
177 case offsetof(struct user_regs_struct, es):
178 if (task == current) {
179 asm("movl %%es,%0" : "=r" (seg));
180 return seg;
181 }
182 return task->thread.es;
183
184 case offsetof(struct user_regs_struct, cs):
185 case offsetof(struct user_regs_struct, ss):
186 break;
187 }
188 return *pt_regs_access(task_pt_regs(task), offset);
189}
190
191static int set_segment_reg(struct task_struct *task,
192 unsigned long offset, u16 value)
193{
194 /*
195 * The value argument was already truncated to 16 bits.
196 */
197 if (invalid_selector(value))
198 return -EIO;
199
200 switch (offset) {
201 case offsetof(struct user_regs_struct,fs):
202 /*
203 * If this is setting fs as for normal 64-bit use but
204 * setting fs_base has implicitly changed it, leave it.
205 */
206 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
207 task->thread.fs != 0) ||
208 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
209 task->thread.fs == 0))
210 break;
211 task->thread.fsindex = value;
212 if (task == current)
213 loadsegment(fs, task->thread.fsindex);
214 break;
215 case offsetof(struct user_regs_struct,gs):
216 /*
217 * If this is setting gs as for normal 64-bit use but
218 * setting gs_base has implicitly changed it, leave it.
219 */
220 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
221 task->thread.gs != 0) ||
222 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
223 task->thread.gs == 0))
224 break;
225 task->thread.gsindex = value;
226 if (task == current)
227 load_gs_index(task->thread.gsindex);
228 break;
229 case offsetof(struct user_regs_struct,ds):
230 task->thread.ds = value;
231 if (task == current)
232 loadsegment(ds, task->thread.ds);
233 break;
234 case offsetof(struct user_regs_struct,es):
235 task->thread.es = value;
236 if (task == current)
237 loadsegment(es, task->thread.es);
238 break;
239
240 /*
241 * Can't actually change these in 64-bit mode.
242 */
243 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100244 if (unlikely(value == 0))
245 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100246#ifdef CONFIG_IA32_EMULATION
247 if (test_tsk_thread_flag(task, TIF_IA32))
248 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100249#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100250 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100251 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100252 if (unlikely(value == 0))
253 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100254#ifdef CONFIG_IA32_EMULATION
255 if (test_tsk_thread_flag(task, TIF_IA32))
256 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100257#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100258 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100259 }
260
261 return 0;
262}
263
Roland McGrath2047b082008-01-30 13:31:01 +0100264#endif /* CONFIG_X86_32 */
265
Roland McGrath06ee1b62008-01-30 13:31:01 +0100266static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100268 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Roland McGrath06ee1b62008-01-30 13:31:01 +0100270 /*
271 * If the debugger set TF, hide it from the readout.
272 */
273 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
274 retval &= ~X86_EFLAGS_TF;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return retval;
277}
278
Roland McGrath06ee1b62008-01-30 13:31:01 +0100279static int set_flags(struct task_struct *task, unsigned long value)
280{
281 struct pt_regs *regs = task_pt_regs(task);
282
283 /*
284 * If the user value contains TF, mark that
285 * it was not "us" (the debugger) that set it.
286 * If not, make sure it stays set if we had.
287 */
288 if (value & X86_EFLAGS_TF)
289 clear_tsk_thread_flag(task, TIF_FORCED_TF);
290 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
291 value |= X86_EFLAGS_TF;
292
293 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
294
295 return 0;
296}
297
298static int putreg(struct task_struct *child,
299 unsigned long offset, unsigned long value)
300{
301 switch (offset) {
302 case offsetof(struct user_regs_struct, cs):
303 case offsetof(struct user_regs_struct, ds):
304 case offsetof(struct user_regs_struct, es):
305 case offsetof(struct user_regs_struct, fs):
306 case offsetof(struct user_regs_struct, gs):
307 case offsetof(struct user_regs_struct, ss):
308 return set_segment_reg(child, offset, value);
309
310 case offsetof(struct user_regs_struct, flags):
311 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100312
313#ifdef CONFIG_X86_64
Roland McGrath84c6f602008-03-07 14:56:02 -0800314 /*
315 * Orig_ax is really just a flag with small positive and
316 * negative values, so make sure to always sign-extend it
317 * from 32 bits so that it works correctly regardless of
318 * whether we come from a 32-bit environment or not.
319 */
320 case offsetof(struct user_regs_struct, orig_ax):
321 value = (long) (s32) value;
322 break;
323
Roland McGrath2047b082008-01-30 13:31:01 +0100324 case offsetof(struct user_regs_struct,fs_base):
325 if (value >= TASK_SIZE_OF(child))
326 return -EIO;
327 /*
328 * When changing the segment base, use do_arch_prctl
329 * to set either thread.fs or thread.fsindex and the
330 * corresponding GDT slot.
331 */
332 if (child->thread.fs != value)
333 return do_arch_prctl(child, ARCH_SET_FS, value);
334 return 0;
335 case offsetof(struct user_regs_struct,gs_base):
336 /*
337 * Exactly the same here as the %fs handling above.
338 */
339 if (value >= TASK_SIZE_OF(child))
340 return -EIO;
341 if (child->thread.gs != value)
342 return do_arch_prctl(child, ARCH_SET_GS, value);
343 return 0;
344#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100345 }
346
347 *pt_regs_access(task_pt_regs(child), offset) = value;
348 return 0;
349}
350
351static unsigned long getreg(struct task_struct *task, unsigned long offset)
352{
353 switch (offset) {
354 case offsetof(struct user_regs_struct, cs):
355 case offsetof(struct user_regs_struct, ds):
356 case offsetof(struct user_regs_struct, es):
357 case offsetof(struct user_regs_struct, fs):
358 case offsetof(struct user_regs_struct, gs):
359 case offsetof(struct user_regs_struct, ss):
360 return get_segment_reg(task, offset);
361
362 case offsetof(struct user_regs_struct, flags):
363 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100364
365#ifdef CONFIG_X86_64
366 case offsetof(struct user_regs_struct, fs_base): {
367 /*
368 * do_arch_prctl may have used a GDT slot instead of
369 * the MSR. To userland, it appears the same either
370 * way, except the %fs segment selector might not be 0.
371 */
372 unsigned int seg = task->thread.fsindex;
373 if (task->thread.fs != 0)
374 return task->thread.fs;
375 if (task == current)
376 asm("movl %%fs,%0" : "=r" (seg));
377 if (seg != FS_TLS_SEL)
378 return 0;
379 return get_desc_base(&task->thread.tls_array[FS_TLS]);
380 }
381 case offsetof(struct user_regs_struct, gs_base): {
382 /*
383 * Exactly the same here as the %fs handling above.
384 */
385 unsigned int seg = task->thread.gsindex;
386 if (task->thread.gs != 0)
387 return task->thread.gs;
388 if (task == current)
389 asm("movl %%gs,%0" : "=r" (seg));
390 if (seg != GS_TLS_SEL)
391 return 0;
392 return get_desc_base(&task->thread.tls_array[GS_TLS]);
393 }
394#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100395 }
396
397 return *pt_regs_access(task_pt_regs(task), offset);
398}
399
Roland McGrath91e7b702008-01-30 13:31:52 +0100400static int genregs_get(struct task_struct *target,
401 const struct user_regset *regset,
402 unsigned int pos, unsigned int count,
403 void *kbuf, void __user *ubuf)
404{
405 if (kbuf) {
406 unsigned long *k = kbuf;
407 while (count > 0) {
408 *k++ = getreg(target, pos);
409 count -= sizeof(*k);
410 pos += sizeof(*k);
411 }
412 } else {
413 unsigned long __user *u = ubuf;
414 while (count > 0) {
415 if (__put_user(getreg(target, pos), u++))
416 return -EFAULT;
417 count -= sizeof(*u);
418 pos += sizeof(*u);
419 }
420 }
421
422 return 0;
423}
424
425static int genregs_set(struct task_struct *target,
426 const struct user_regset *regset,
427 unsigned int pos, unsigned int count,
428 const void *kbuf, const void __user *ubuf)
429{
430 int ret = 0;
431 if (kbuf) {
432 const unsigned long *k = kbuf;
433 while (count > 0 && !ret) {
434 ret = putreg(target, pos, *k++);
435 count -= sizeof(*k);
436 pos += sizeof(*k);
437 }
438 } else {
439 const unsigned long __user *u = ubuf;
440 while (count > 0 && !ret) {
441 unsigned long word;
442 ret = __get_user(word, u++);
443 if (ret)
444 break;
445 ret = putreg(target, pos, word);
446 count -= sizeof(*u);
447 pos += sizeof(*u);
448 }
449 }
450 return ret;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
K.Prasad72f674d2009-06-01 23:45:48 +0530454 * Decode the length and type bits for a particular breakpoint as
455 * stored in debug register 7. Return the "enabled" status.
Roland McGrathd9771e82008-01-30 13:30:52 +0100456 */
K.Prasad72f674d2009-06-01 23:45:48 +0530457static int decode_dr7(unsigned long dr7, int bpnum, unsigned *len,
458 unsigned *type)
Roland McGrathd9771e82008-01-30 13:30:52 +0100459{
K.Prasad72f674d2009-06-01 23:45:48 +0530460 int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE);
461
462 *len = (bp_info & 0xc) | 0x40;
463 *type = (bp_info & 0x3) | 0x80;
464 return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3;
Roland McGrathd9771e82008-01-30 13:30:52 +0100465}
466
K.Prasad72f674d2009-06-01 23:45:48 +0530467static void ptrace_triggered(struct hw_breakpoint *bp, struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100468{
K.Prasad72f674d2009-06-01 23:45:48 +0530469 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100470 int i;
471
K.Prasad72f674d2009-06-01 23:45:48 +0530472 /*
473 * Store in the virtual DR6 register the fact that the breakpoint
474 * was hit so the thread's debugger will see it.
475 */
476 for (i = 0; i < hbp_kernel_pos; i++)
Roland McGrathd9771e82008-01-30 13:30:52 +0100477 /*
K.Prasad72f674d2009-06-01 23:45:48 +0530478 * We will check bp->info.address against the address stored in
479 * thread's hbp structure and not debugreg[i]. This is to ensure
480 * that the corresponding bit for 'i' in DR7 register is enabled
Roland McGrathd9771e82008-01-30 13:30:52 +0100481 */
K.Prasad72f674d2009-06-01 23:45:48 +0530482 if (bp->info.address == thread->hbp[i]->info.address)
483 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100484
K.Prasad72f674d2009-06-01 23:45:48 +0530485 thread->debugreg6 |= (DR_TRAP0 << i);
486}
487
488/*
489 * Handle ptrace writes to debug register 7.
490 */
491static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
492{
493 struct thread_struct *thread = &(tsk->thread);
494 unsigned long old_dr7 = thread->debugreg7;
495 int i, orig_ret = 0, rc = 0;
496 int enabled, second_pass = 0;
497 unsigned len, type;
498 struct hw_breakpoint *bp;
499
500 data &= ~DR_CONTROL_RESERVED;
501restore:
502 /*
503 * Loop through all the hardware breakpoints, making the
504 * appropriate changes to each.
505 */
506 for (i = 0; i < HBP_NUM; i++) {
507 enabled = decode_dr7(data, i, &len, &type);
508 bp = thread->hbp[i];
509
510 if (!enabled) {
511 if (bp) {
512 /* Don't unregister the breakpoints right-away,
513 * unless all register_user_hw_breakpoint()
514 * requests have succeeded. This prevents
515 * any window of opportunity for debug
516 * register grabbing by other users.
517 */
518 if (!second_pass)
519 continue;
520 unregister_user_hw_breakpoint(tsk, bp);
521 kfree(bp);
522 }
523 continue;
524 }
525 if (!bp) {
526 rc = -ENOMEM;
527 bp = kzalloc(sizeof(struct hw_breakpoint), GFP_KERNEL);
528 if (bp) {
529 bp->info.address = thread->debugreg[i];
530 bp->triggered = ptrace_triggered;
531 bp->info.len = len;
532 bp->info.type = type;
533 rc = register_user_hw_breakpoint(tsk, bp);
534 if (rc)
535 kfree(bp);
536 }
537 } else
538 rc = modify_user_hw_breakpoint(tsk, bp);
539 if (rc)
540 break;
541 }
542 /*
543 * Make a second pass to free the remaining unused breakpoints
544 * or to restore the original breakpoints if an error occurred.
545 */
546 if (!second_pass) {
547 second_pass = 1;
548 if (rc < 0) {
549 orig_ret = rc;
550 data = old_dr7;
551 }
552 goto restore;
553 }
554 return ((orig_ret < 0) ? orig_ret : rc);
555}
556
557/*
558 * Handle PTRACE_PEEKUSR calls for the debug register area.
559 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530560static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530561{
562 struct thread_struct *thread = &(tsk->thread);
563 unsigned long val = 0;
564
565 if (n < HBP_NUM)
566 val = thread->debugreg[n];
567 else if (n == 6)
568 val = thread->debugreg6;
569 else if (n == 7)
570 val = thread->debugreg7;
571 return val;
572}
573
574/*
575 * Handle PTRACE_POKEUSR calls for the debug register area.
576 */
577int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
578{
579 struct thread_struct *thread = &(tsk->thread);
580 int rc = 0;
581
582 /* There are no DR4 or DR5 registers */
583 if (n == 4 || n == 5)
584 return -EIO;
585
586 if (n == 6) {
587 tsk->thread.debugreg6 = val;
588 goto ret_path;
589 }
590 if (n < HBP_NUM) {
591 if (thread->hbp[n]) {
592 if (arch_check_va_in_userspace(val,
593 thread->hbp[n]->info.len) == 0) {
594 rc = -EIO;
595 goto ret_path;
596 }
597 thread->hbp[n]->info.address = val;
598 }
599 thread->debugreg[n] = val;
600 }
601 /* All that's left is DR7 */
602 if (n == 7)
603 rc = ptrace_write_dr7(tsk, val);
604
605ret_path:
606 return rc;
Roland McGrathd9771e82008-01-30 13:30:52 +0100607}
608
Roland McGrath325af5f2008-08-08 15:58:39 -0700609/*
610 * These access the current or another (stopped) task's io permission
611 * bitmap for debugging or core dump.
612 */
613static int ioperm_active(struct task_struct *target,
614 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100615{
Roland McGrath325af5f2008-08-08 15:58:39 -0700616 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100617}
618
Roland McGrath325af5f2008-08-08 15:58:39 -0700619static int ioperm_get(struct task_struct *target,
620 const struct user_regset *regset,
621 unsigned int pos, unsigned int count,
622 void *kbuf, void __user *ubuf)
623{
624 if (!target->thread.io_bitmap_ptr)
625 return -ENXIO;
626
627 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
628 target->thread.io_bitmap_ptr,
629 0, IO_BITMAP_BYTES);
630}
631
Markus Metzger93fa7632008-04-08 11:01:58 +0200632#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200633/*
634 * A branch trace store context.
635 *
636 * Contexts may only be installed by ptrace_bts_config() and only for
637 * ptraced tasks.
638 *
639 * Contexts are destroyed when the tracee is detached from the tracer.
640 * The actual destruction work requires interrupts enabled, so the
641 * work is deferred and will be scheduled during __ptrace_unlink().
642 *
643 * Contexts hold an additional task_struct reference on the traced
644 * task, as well as a reference on the tracer's mm.
645 *
646 * Ptrace already holds a task_struct for the duration of ptrace operations,
647 * but since destruction is deferred, it may be executed after both
648 * tracer and tracee exited.
649 */
650struct bts_context {
651 /* The branch trace handle. */
652 struct bts_tracer *tracer;
653
654 /* The buffer used to store the branch trace and its size. */
655 void *buffer;
656 unsigned int size;
657
658 /* The mm that paid for the above buffer. */
659 struct mm_struct *mm;
660
661 /* The task this context belongs to. */
662 struct task_struct *task;
663
664 /* The signal to send on a bts buffer overflow. */
665 unsigned int bts_ovfl_signal;
666
667 /* The work struct to destroy a context. */
668 struct work_struct work;
669};
670
Markus Metzger1cb81b12009-04-24 09:51:43 +0200671static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200672{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200673 void *buffer = NULL;
674 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200675
Markus Metzger1cb81b12009-04-24 09:51:43 +0200676 err = account_locked_memory(current->mm, current->signal->rlim, size);
677 if (err < 0)
678 return err;
679
680 buffer = kzalloc(size, GFP_KERNEL);
681 if (!buffer)
682 goto out_refund;
683
684 context->buffer = buffer;
685 context->size = size;
686 context->mm = get_task_mm(current);
687
688 return 0;
689
690 out_refund:
691 refund_locked_memory(current->mm, size);
692 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200693}
694
695static inline void free_bts_buffer(struct bts_context *context)
696{
697 if (!context->buffer)
698 return;
699
700 kfree(context->buffer);
701 context->buffer = NULL;
702
Markus Metzger1cb81b12009-04-24 09:51:43 +0200703 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200704 context->size = 0;
705
706 mmput(context->mm);
707 context->mm = NULL;
708}
709
710static void free_bts_context_work(struct work_struct *w)
711{
712 struct bts_context *context;
713
714 context = container_of(w, struct bts_context, work);
715
716 ds_release_bts(context->tracer);
717 put_task_struct(context->task);
718 free_bts_buffer(context);
719 kfree(context);
720}
721
722static inline void free_bts_context(struct bts_context *context)
723{
724 INIT_WORK(&context->work, free_bts_context_work);
725 schedule_work(&context->work);
726}
727
728static inline struct bts_context *alloc_bts_context(struct task_struct *task)
729{
730 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
731 if (context) {
732 context->task = task;
733 task->bts = context;
734
735 get_task_struct(task);
736 }
737
738 return context;
739}
740
Markus Metzger93fa7632008-04-08 11:01:58 +0200741static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100742 struct bts_struct __user *out)
743{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200744 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100745 const struct bts_trace *trace;
746 struct bts_struct bts;
747 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200748 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100749
Markus Metzgere2b371f2009-04-03 16:43:35 +0200750 context = child->bts;
751 if (!context)
752 return -ESRCH;
753
754 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100755 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200756 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100757
758 at = trace->ds.top - ((index + 1) * trace->ds.size);
759 if ((void *)at < trace->ds.begin)
760 at += (trace->ds.n * trace->ds.size);
761
762 if (!trace->read)
763 return -EOPNOTSUPP;
764
Markus Metzgere2b371f2009-04-03 16:43:35 +0200765 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200766 if (error < 0)
767 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100768
Markus Metzgerc2724772008-12-11 13:49:59 +0100769 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100770 return -EFAULT;
771
Markus Metzgerc2724772008-12-11 13:49:59 +0100772 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100773}
774
Markus Metzgera95d67f2008-01-30 13:31:20 +0100775static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100776 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100777 struct bts_struct __user *out)
778{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200779 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100780 const struct bts_trace *trace;
781 const unsigned char *at;
782 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100783
Markus Metzgere2b371f2009-04-03 16:43:35 +0200784 context = child->bts;
785 if (!context)
786 return -ESRCH;
787
788 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100789 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200790 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100791
Markus Metzgerc2724772008-12-11 13:49:59 +0100792 if (!trace->read)
793 return -EOPNOTSUPP;
794
795 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100796 return -EIO;
797
Markus Metzgerc2724772008-12-11 13:49:59 +0100798 for (at = trace->ds.begin; (void *)at < trace->ds.top;
799 out++, drained++, at += trace->ds.size) {
800 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100801
Markus Metzgere2b371f2009-04-03 16:43:35 +0200802 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100803 if (error < 0)
804 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100805
Markus Metzgerc2724772008-12-11 13:49:59 +0100806 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100807 return -EFAULT;
808 }
809
Markus Metzgerc2724772008-12-11 13:49:59 +0100810 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
811
Markus Metzgere2b371f2009-04-03 16:43:35 +0200812 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200813 if (error < 0)
814 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100815
Markus Metzgerc2724772008-12-11 13:49:59 +0100816 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100817}
818
819static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100820 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100821 const struct ptrace_bts_config __user *ucfg)
822{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200823 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100824 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100825 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100826
Markus Metzgercba4b652008-01-30 13:32:03 +0100827 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100828 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100829
Markus Metzgera95d67f2008-01-30 13:31:20 +0100830 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100831 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100832
Markus Metzgere2b371f2009-04-03 16:43:35 +0200833 context = child->bts;
834 if (!context)
835 context = alloc_bts_context(child);
836 if (!context)
837 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100838
Markus Metzgerc2724772008-12-11 13:49:59 +0100839 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
840 if (!cfg.signal)
841 return -EINVAL;
842
Américo Wang5a8ac9d2009-03-13 15:56:58 +0800843 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200844 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +0100845 }
846
Markus Metzgere2b371f2009-04-03 16:43:35 +0200847 ds_release_bts(context->tracer);
848 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100849
Markus Metzgere2b371f2009-04-03 16:43:35 +0200850 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +0200851 int err;
852
Markus Metzgere2b371f2009-04-03 16:43:35 +0200853 free_bts_buffer(context);
854 if (!cfg.size)
855 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +0100856
Markus Metzger1cb81b12009-04-24 09:51:43 +0200857 err = alloc_bts_buffer(context, cfg.size);
858 if (err < 0)
859 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +0100860 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100861
Markus Metzgerda35c372008-01-30 13:32:03 +0100862 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +0100863 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +0100864
Markus Metzgerda35c372008-01-30 13:32:03 +0100865 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +0100866 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +0100867
Markus Metzgerde79f542009-04-03 16:43:40 +0200868 context->tracer =
869 ds_request_bts_task(child, context->buffer, context->size,
870 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200871 if (unlikely(IS_ERR(context->tracer))) {
872 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +0100873
Markus Metzgere2b371f2009-04-03 16:43:35 +0200874 free_bts_buffer(context);
875 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100876 return error;
877 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100878
Markus Metzgerc2724772008-12-11 13:49:59 +0100879 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100880}
881
Markus Metzgera95d67f2008-01-30 13:31:20 +0100882static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100883 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100884 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100885{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200886 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100887 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100888 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100889
Markus Metzgere2b371f2009-04-03 16:43:35 +0200890 context = child->bts;
891 if (!context)
892 return -ESRCH;
893
Markus Metzgercba4b652008-01-30 13:32:03 +0100894 if (cfg_size < sizeof(cfg))
895 return -EIO;
896
Markus Metzgere2b371f2009-04-03 16:43:35 +0200897 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100898 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200899 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +0200900
Markus Metzgera95d67f2008-01-30 13:31:20 +0100901 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +0200902 cfg.size = trace->ds.end - trace->ds.begin;
903 cfg.signal = context->bts_ovfl_signal;
904 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +0100905
Markus Metzger93fa7632008-04-08 11:01:58 +0200906 if (cfg.signal)
907 cfg.flags |= PTRACE_BTS_O_SIGNAL;
908
Markus Metzgerc2724772008-12-11 13:49:59 +0100909 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +0200910 cfg.flags |= PTRACE_BTS_O_TRACE;
911
Markus Metzgerc2724772008-12-11 13:49:59 +0100912 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +0200913 cfg.flags |= PTRACE_BTS_O_SCHED;
914
Markus Metzgera95d67f2008-01-30 13:31:20 +0100915 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
916 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100917
Markus Metzgera95d67f2008-01-30 13:31:20 +0100918 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100919}
920
Markus Metzgerc2724772008-12-11 13:49:59 +0100921static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -0800922{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200923 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100924 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800925
Markus Metzgere2b371f2009-04-03 16:43:35 +0200926 context = child->bts;
927 if (!context)
928 return -ESRCH;
929
930 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100931 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200932 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800933
Markus Metzgerc2724772008-12-11 13:49:59 +0100934 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800935
Markus Metzgere2b371f2009-04-03 16:43:35 +0200936 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800937}
938
Markus Metzgerc2724772008-12-11 13:49:59 +0100939static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100940{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200941 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100942 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +0100943
Markus Metzgere2b371f2009-04-03 16:43:35 +0200944 context = child->bts;
945 if (!context)
946 return -ESRCH;
947
948 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100949 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200950 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +0200951
Markus Metzgerc2724772008-12-11 13:49:59 +0100952 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +0200953}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100954
Markus Metzgere2b371f2009-04-03 16:43:35 +0200955/*
956 * Called from __ptrace_unlink() after the child has been moved back
957 * to its original parent.
958 */
Markus Metzger0f481402009-04-03 16:43:48 +0200959void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +0100960{
961 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +0200962 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +0100963 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +0100964 }
965}
Markus Metzger93fa7632008-04-08 11:01:58 +0200966#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100967
Roland McGrathd9771e82008-01-30 13:30:52 +0100968/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * Called by kernel/ptrace.c when detaching..
970 *
971 * Make sure the single step bit is not set.
972 */
973void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100974{
Roland McGrath7f232342008-01-30 13:30:48 +0100975 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100976#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700977 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Roland McGrath5a4646a2008-01-30 13:31:54 +0100981#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
982static const struct user_regset_view user_x86_32_view; /* Initialized below. */
983#endif
984
Christoph Hellwig481bed42005-11-07 00:59:47 -0800985long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100987 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 unsigned long __user *datap = (unsigned long __user *)data;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* read the word at location addr in the USER area. */
992 case PTRACE_PEEKUSR: {
993 unsigned long tmp;
994
995 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100996 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
997 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 break;
999
1000 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001001 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001003 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1004 addr <= offsetof(struct user, u_debugreg[7])) {
1005 addr -= offsetof(struct user, u_debugreg[0]);
1006 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 ret = put_user(tmp, datap);
1009 break;
1010 }
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1013 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001014 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1015 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017
Roland McGrathe9c86c72008-01-30 13:31:01 +01001018 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001020 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1021 addr <= offsetof(struct user, u_debugreg[7])) {
1022 addr -= offsetof(struct user, u_debugreg[0]);
1023 ret = ptrace_set_debugreg(child,
1024 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Roland McGrath5a4646a2008-01-30 13:31:54 +01001028 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1029 return copy_regset_to_user(child,
1030 task_user_regset_view(current),
1031 REGSET_GENERAL,
1032 0, sizeof(struct user_regs_struct),
1033 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Roland McGrath5a4646a2008-01-30 13:31:54 +01001035 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1036 return copy_regset_from_user(child,
1037 task_user_regset_view(current),
1038 REGSET_GENERAL,
1039 0, sizeof(struct user_regs_struct),
1040 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Roland McGrath5a4646a2008-01-30 13:31:54 +01001042 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1043 return copy_regset_to_user(child,
1044 task_user_regset_view(current),
1045 REGSET_FP,
1046 0, sizeof(struct user_i387_struct),
1047 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Roland McGrath5a4646a2008-01-30 13:31:54 +01001049 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1050 return copy_regset_from_user(child,
1051 task_user_regset_view(current),
1052 REGSET_FP,
1053 0, sizeof(struct user_i387_struct),
1054 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Roland McGrathe9c86c72008-01-30 13:31:01 +01001056#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001057 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1058 return copy_regset_to_user(child, &user_x86_32_view,
1059 REGSET_XFP,
1060 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001061 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Roland McGrath5a4646a2008-01-30 13:31:54 +01001063 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1064 return copy_regset_from_user(child, &user_x86_32_view,
1065 REGSET_XFP,
1066 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001067 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Roland McGrathe9c86c72008-01-30 13:31:01 +01001070#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001072 if (addr < 0)
1073 return -EIO;
1074 ret = do_get_thread_area(child, addr,
1075 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 break;
1077
1078 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001079 if (addr < 0)
1080 return -EIO;
1081 ret = do_set_thread_area(child, addr,
1082 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001084#endif
1085
1086#ifdef CONFIG_X86_64
1087 /* normal 64bit interface to access TLS data.
1088 Works just like arch_prctl, except that the arguments
1089 are reversed. */
1090 case PTRACE_ARCH_PRCTL:
1091 ret = do_arch_prctl(child, data, addr);
1092 break;
1093#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001095 /*
1096 * These bits need more cooking - not enabled yet:
1097 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001098#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001099 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001100 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001101 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001102 break;
1103
1104 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001105 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001106 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001107 break;
1108
Markus Metzgerc2724772008-12-11 13:49:59 +01001109 case PTRACE_BTS_SIZE:
1110 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001111 break;
1112
1113 case PTRACE_BTS_GET:
1114 ret = ptrace_bts_read_record
1115 (child, data, (struct bts_struct __user *) addr);
1116 break;
1117
1118 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001119 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001120 break;
1121
1122 case PTRACE_BTS_DRAIN:
1123 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001124 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001125 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001126#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 default:
1129 ret = ptrace_request(child, request, addr, data);
1130 break;
1131 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return ret;
1134}
1135
Roland McGrathcb757c42008-01-30 13:31:01 +01001136#ifdef CONFIG_IA32_EMULATION
1137
Roland McGrath099cd6e2008-01-30 13:31:01 +01001138#include <linux/compat.h>
1139#include <linux/syscalls.h>
1140#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001141#include <asm/user32.h>
1142
1143#define R32(l,q) \
1144 case offsetof(struct user32, regs.l): \
1145 regs->q = value; break
1146
1147#define SEG32(rs) \
1148 case offsetof(struct user32, regs.rs): \
1149 return set_segment_reg(child, \
1150 offsetof(struct user_regs_struct, rs), \
1151 value); \
1152 break
1153
1154static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1155{
1156 struct pt_regs *regs = task_pt_regs(child);
1157
1158 switch (regno) {
1159
1160 SEG32(cs);
1161 SEG32(ds);
1162 SEG32(es);
1163 SEG32(fs);
1164 SEG32(gs);
1165 SEG32(ss);
1166
1167 R32(ebx, bx);
1168 R32(ecx, cx);
1169 R32(edx, dx);
1170 R32(edi, di);
1171 R32(esi, si);
1172 R32(ebp, bp);
1173 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001174 R32(eip, ip);
1175 R32(esp, sp);
1176
Roland McGrath40f09332008-02-28 19:57:07 -08001177 case offsetof(struct user32, regs.orig_eax):
1178 /*
1179 * Sign-extend the value so that orig_eax = -1
1180 * causes (long)orig_ax < 0 tests to fire correctly.
1181 */
1182 regs->orig_ax = (long) (s32) value;
1183 break;
1184
Roland McGrathcb757c42008-01-30 13:31:01 +01001185 case offsetof(struct user32, regs.eflags):
1186 return set_flags(child, value);
1187
1188 case offsetof(struct user32, u_debugreg[0]) ...
1189 offsetof(struct user32, u_debugreg[7]):
1190 regno -= offsetof(struct user32, u_debugreg[0]);
1191 return ptrace_set_debugreg(child, regno / 4, value);
1192
1193 default:
1194 if (regno > sizeof(struct user32) || (regno & 3))
1195 return -EIO;
1196
1197 /*
1198 * Other dummy fields in the virtual user structure
1199 * are ignored
1200 */
1201 break;
1202 }
1203 return 0;
1204}
1205
1206#undef R32
1207#undef SEG32
1208
1209#define R32(l,q) \
1210 case offsetof(struct user32, regs.l): \
1211 *val = regs->q; break
1212
1213#define SEG32(rs) \
1214 case offsetof(struct user32, regs.rs): \
1215 *val = get_segment_reg(child, \
1216 offsetof(struct user_regs_struct, rs)); \
1217 break
1218
1219static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1220{
1221 struct pt_regs *regs = task_pt_regs(child);
1222
1223 switch (regno) {
1224
1225 SEG32(ds);
1226 SEG32(es);
1227 SEG32(fs);
1228 SEG32(gs);
1229
1230 R32(cs, cs);
1231 R32(ss, ss);
1232 R32(ebx, bx);
1233 R32(ecx, cx);
1234 R32(edx, dx);
1235 R32(edi, di);
1236 R32(esi, si);
1237 R32(ebp, bp);
1238 R32(eax, ax);
1239 R32(orig_eax, orig_ax);
1240 R32(eip, ip);
1241 R32(esp, sp);
1242
1243 case offsetof(struct user32, regs.eflags):
1244 *val = get_flags(child);
1245 break;
1246
1247 case offsetof(struct user32, u_debugreg[0]) ...
1248 offsetof(struct user32, u_debugreg[7]):
1249 regno -= offsetof(struct user32, u_debugreg[0]);
1250 *val = ptrace_get_debugreg(child, regno / 4);
1251 break;
1252
1253 default:
1254 if (regno > sizeof(struct user32) || (regno & 3))
1255 return -EIO;
1256
1257 /*
1258 * Other dummy fields in the virtual user structure
1259 * are ignored
1260 */
1261 *val = 0;
1262 break;
1263 }
1264 return 0;
1265}
1266
1267#undef R32
1268#undef SEG32
1269
Roland McGrath91e7b702008-01-30 13:31:52 +01001270static int genregs32_get(struct task_struct *target,
1271 const struct user_regset *regset,
1272 unsigned int pos, unsigned int count,
1273 void *kbuf, void __user *ubuf)
1274{
1275 if (kbuf) {
1276 compat_ulong_t *k = kbuf;
1277 while (count > 0) {
1278 getreg32(target, pos, k++);
1279 count -= sizeof(*k);
1280 pos += sizeof(*k);
1281 }
1282 } else {
1283 compat_ulong_t __user *u = ubuf;
1284 while (count > 0) {
1285 compat_ulong_t word;
1286 getreg32(target, pos, &word);
1287 if (__put_user(word, u++))
1288 return -EFAULT;
1289 count -= sizeof(*u);
1290 pos += sizeof(*u);
1291 }
1292 }
1293
1294 return 0;
1295}
1296
1297static int genregs32_set(struct task_struct *target,
1298 const struct user_regset *regset,
1299 unsigned int pos, unsigned int count,
1300 const void *kbuf, const void __user *ubuf)
1301{
1302 int ret = 0;
1303 if (kbuf) {
1304 const compat_ulong_t *k = kbuf;
1305 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001306 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001307 count -= sizeof(*k);
1308 pos += sizeof(*k);
1309 }
1310 } else {
1311 const compat_ulong_t __user *u = ubuf;
1312 while (count > 0 && !ret) {
1313 compat_ulong_t word;
1314 ret = __get_user(word, u++);
1315 if (ret)
1316 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001317 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001318 count -= sizeof(*u);
1319 pos += sizeof(*u);
1320 }
1321 }
1322 return ret;
1323}
1324
Roland McGrath562b80b2008-04-22 12:21:25 -07001325long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1326 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001327{
Roland McGrath562b80b2008-04-22 12:21:25 -07001328 unsigned long addr = caddr;
1329 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001330 void __user *datap = compat_ptr(data);
1331 int ret;
1332 __u32 val;
1333
1334 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001335 case PTRACE_PEEKUSR:
1336 ret = getreg32(child, addr, &val);
1337 if (ret == 0)
1338 ret = put_user(val, (__u32 __user *)datap);
1339 break;
1340
1341 case PTRACE_POKEUSR:
1342 ret = putreg32(child, addr, data);
1343 break;
1344
Roland McGrath5a4646a2008-01-30 13:31:54 +01001345 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1346 return copy_regset_to_user(child, &user_x86_32_view,
1347 REGSET_GENERAL,
1348 0, sizeof(struct user_regs_struct32),
1349 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001350
Roland McGrath5a4646a2008-01-30 13:31:54 +01001351 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1352 return copy_regset_from_user(child, &user_x86_32_view,
1353 REGSET_GENERAL, 0,
1354 sizeof(struct user_regs_struct32),
1355 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001356
Roland McGrath5a4646a2008-01-30 13:31:54 +01001357 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1358 return copy_regset_to_user(child, &user_x86_32_view,
1359 REGSET_FP, 0,
1360 sizeof(struct user_i387_ia32_struct),
1361 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001362
Roland McGrath5a4646a2008-01-30 13:31:54 +01001363 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1364 return copy_regset_from_user(
1365 child, &user_x86_32_view, REGSET_FP,
1366 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001367
Roland McGrath5a4646a2008-01-30 13:31:54 +01001368 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1369 return copy_regset_to_user(child, &user_x86_32_view,
1370 REGSET_XFP, 0,
1371 sizeof(struct user32_fxsr_struct),
1372 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001373
Roland McGrath5a4646a2008-01-30 13:31:54 +01001374 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1375 return copy_regset_from_user(child, &user_x86_32_view,
1376 REGSET_XFP, 0,
1377 sizeof(struct user32_fxsr_struct),
1378 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001379
Roland McGrath562b80b2008-04-22 12:21:25 -07001380 case PTRACE_GET_THREAD_AREA:
1381 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001382#ifdef CONFIG_X86_PTRACE_BTS
1383 case PTRACE_BTS_CONFIG:
1384 case PTRACE_BTS_STATUS:
1385 case PTRACE_BTS_SIZE:
1386 case PTRACE_BTS_GET:
1387 case PTRACE_BTS_CLEAR:
1388 case PTRACE_BTS_DRAIN:
1389#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001390 return arch_ptrace(child, request, addr, data);
1391
Roland McGrath099cd6e2008-01-30 13:31:01 +01001392 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001393 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001394 }
1395
Roland McGrath099cd6e2008-01-30 13:31:01 +01001396 return ret;
1397}
1398
Roland McGrathcb757c42008-01-30 13:31:01 +01001399#endif /* CONFIG_IA32_EMULATION */
1400
Roland McGrath070459d2008-01-30 13:31:53 +01001401#ifdef CONFIG_X86_64
1402
1403static const struct user_regset x86_64_regsets[] = {
1404 [REGSET_GENERAL] = {
1405 .core_note_type = NT_PRSTATUS,
1406 .n = sizeof(struct user_regs_struct) / sizeof(long),
1407 .size = sizeof(long), .align = sizeof(long),
1408 .get = genregs_get, .set = genregs_set
1409 },
1410 [REGSET_FP] = {
1411 .core_note_type = NT_PRFPREG,
1412 .n = sizeof(struct user_i387_struct) / sizeof(long),
1413 .size = sizeof(long), .align = sizeof(long),
1414 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1415 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001416 [REGSET_IOPERM64] = {
1417 .core_note_type = NT_386_IOPERM,
1418 .n = IO_BITMAP_LONGS,
1419 .size = sizeof(long), .align = sizeof(long),
1420 .active = ioperm_active, .get = ioperm_get
1421 },
Roland McGrath070459d2008-01-30 13:31:53 +01001422};
1423
1424static const struct user_regset_view user_x86_64_view = {
1425 .name = "x86_64", .e_machine = EM_X86_64,
1426 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1427};
1428
1429#else /* CONFIG_X86_32 */
1430
1431#define user_regs_struct32 user_regs_struct
1432#define genregs32_get genregs_get
1433#define genregs32_set genregs_set
1434
Roland McGrath1f465f42008-05-09 15:43:44 -07001435#define user_i387_ia32_struct user_i387_struct
1436#define user32_fxsr_struct user_fxsr_struct
1437
Roland McGrath070459d2008-01-30 13:31:53 +01001438#endif /* CONFIG_X86_64 */
1439
1440#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1441static const struct user_regset x86_32_regsets[] = {
1442 [REGSET_GENERAL] = {
1443 .core_note_type = NT_PRSTATUS,
1444 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1445 .size = sizeof(u32), .align = sizeof(u32),
1446 .get = genregs32_get, .set = genregs32_set
1447 },
1448 [REGSET_FP] = {
1449 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001450 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001451 .size = sizeof(u32), .align = sizeof(u32),
1452 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1453 },
1454 [REGSET_XFP] = {
1455 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001456 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001457 .size = sizeof(u32), .align = sizeof(u32),
1458 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1459 },
1460 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001461 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001462 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1463 .size = sizeof(struct user_desc),
1464 .align = sizeof(struct user_desc),
1465 .active = regset_tls_active,
1466 .get = regset_tls_get, .set = regset_tls_set
1467 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001468 [REGSET_IOPERM32] = {
1469 .core_note_type = NT_386_IOPERM,
1470 .n = IO_BITMAP_BYTES / sizeof(u32),
1471 .size = sizeof(u32), .align = sizeof(u32),
1472 .active = ioperm_active, .get = ioperm_get
1473 },
Roland McGrath070459d2008-01-30 13:31:53 +01001474};
1475
1476static const struct user_regset_view user_x86_32_view = {
1477 .name = "i386", .e_machine = EM_386,
1478 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1479};
1480#endif
1481
1482const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1483{
1484#ifdef CONFIG_IA32_EMULATION
1485 if (test_tsk_thread_flag(task, TIF_IA32))
1486#endif
1487#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1488 return &user_x86_32_view;
1489#endif
1490#ifdef CONFIG_X86_64
1491 return &user_x86_64_view;
1492#endif
1493}
1494
Srinivasa Dsda654b72008-09-23 15:23:52 +05301495void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1496 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct siginfo info;
1499
1500 tsk->thread.trap_no = 1;
1501 tsk->thread.error_code = error_code;
1502
1503 memset(&info, 0, sizeof(info));
1504 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301505 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001507 /* User-mode ip? */
1508 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Simon Arlott27b46d72007-10-20 01:13:56 +02001510 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 force_sig_info(SIGTRAP, &info, tsk);
1512}
1513
Roland McGrath86976cd2008-01-30 13:31:01 +01001514
Roland McGrathd4d67152008-07-09 02:38:07 -07001515#ifdef CONFIG_X86_32
1516# define IS_IA32 1
1517#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001518# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001519#else
1520# define IS_IA32 0
1521#endif
1522
1523/*
1524 * We must return the syscall number to actually look up in the table.
1525 * This can be -1L to skip running any syscall at all.
1526 */
1527asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001528{
Roland McGrathd4d67152008-07-09 02:38:07 -07001529 long ret = 0;
1530
Roland McGrath380fdd72008-07-09 02:39:29 -07001531 /*
1532 * If we stepped into a sysenter/syscall insn, it trapped in
1533 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1534 * If user-mode had set TF itself, then it's still clear from
1535 * do_debug() and we need to set it again to restore the user
1536 * state. If we entered on the slow path, TF was already set.
1537 */
1538 if (test_thread_flag(TIF_SINGLESTEP))
1539 regs->flags |= X86_EFLAGS_TF;
1540
Roland McGrath86976cd2008-01-30 13:31:01 +01001541 /* do the secure computing check first */
1542 secure_computing(regs->orig_ax);
1543
Roland McGrathd4d67152008-07-09 02:38:07 -07001544 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1545 ret = -1L;
1546
Roland McGratheeea3c32008-03-16 23:36:28 -07001547 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1548 tracehook_report_syscall_entry(regs))
1549 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001550
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001551 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1552 ftrace_syscall_enter(regs);
1553
Roland McGrath86976cd2008-01-30 13:31:01 +01001554 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
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001577 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1578 ftrace_syscall_exit(regs);
1579
Roland McGrathd4d67152008-07-09 02:38:07 -07001580 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001581 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001582
Roland McGrathd4d67152008-07-09 02:38:07 -07001583 /*
1584 * If TIF_SYSCALL_EMU is set, we only get here because of
1585 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1586 * We already reported this syscall instruction in
1587 * syscall_trace_enter(), so don't do any more now.
1588 */
1589 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1590 return;
1591
1592 /*
1593 * If we are single-stepping, synthesize a trap to follow the
1594 * system call instruction.
1595 */
1596 if (test_thread_flag(TIF_SINGLESTEP) &&
Oleg Nesterov43918f22009-04-02 16:58:00 -07001597 tracehook_consider_fatal_signal(current, SIGTRAP))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301598 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001599}