blob: d3e115297fe226be429270711f74343dd5314a1e [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
5 */
6
7#include <linux/kernel.h>
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/ptrace.h>
Roland McGrath91e7b702008-01-30 13:31:52 +010014#include <linux/regset.h>
Roland McGratheeea3c32008-03-16 23:36:28 -070015#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/user.h>
Roland McGrath070459d2008-01-30 13:31:53 +010017#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/security.h>
19#include <linux/audit.h>
20#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070021#include <linux/signal.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020022#include <linux/perf_event.h>
23#include <linux/hw_breakpoint.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/processor.h>
28#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080029#include <asm/fpu-internal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/debugreg.h>
31#include <asm/ldt.h>
32#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010033#include <asm/prctl.h>
34#include <asm/proto.h>
K.Prasad72f674d2009-06-01 23:45:48 +053035#include <asm/hw_breakpoint.h>
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +053036#include <asm/traps.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010037
Roland McGrath070459d2008-01-30 13:31:53 +010038#include "tls.h"
39
Josh Stone1c569f02009-08-24 14:43:14 -070040#define CREATE_TRACE_POINTS
41#include <trace/events/syscalls.h>
42
Roland McGrath070459d2008-01-30 13:31:53 +010043enum x86_regset {
44 REGSET_GENERAL,
45 REGSET_FP,
46 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070047 REGSET_IOPERM64 = REGSET_XFP,
Suresh Siddha5b3efd52010-02-11 11:50:59 -080048 REGSET_XSTATE,
Roland McGrath070459d2008-01-30 13:31:53 +010049 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070050 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010051};
Markus Metzgereee3af42008-01-30 13:31:09 +010052
Masami Hiramatsub1cf5402009-08-13 16:34:44 -040053struct pt_regs_offset {
54 const char *name;
55 int offset;
56};
57
58#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
59#define REG_OFFSET_END {.name = NULL, .offset = 0}
60
61static const struct pt_regs_offset regoffset_table[] = {
62#ifdef CONFIG_X86_64
63 REG_OFFSET_NAME(r15),
64 REG_OFFSET_NAME(r14),
65 REG_OFFSET_NAME(r13),
66 REG_OFFSET_NAME(r12),
67 REG_OFFSET_NAME(r11),
68 REG_OFFSET_NAME(r10),
69 REG_OFFSET_NAME(r9),
70 REG_OFFSET_NAME(r8),
71#endif
72 REG_OFFSET_NAME(bx),
73 REG_OFFSET_NAME(cx),
74 REG_OFFSET_NAME(dx),
75 REG_OFFSET_NAME(si),
76 REG_OFFSET_NAME(di),
77 REG_OFFSET_NAME(bp),
78 REG_OFFSET_NAME(ax),
79#ifdef CONFIG_X86_32
80 REG_OFFSET_NAME(ds),
81 REG_OFFSET_NAME(es),
82 REG_OFFSET_NAME(fs),
83 REG_OFFSET_NAME(gs),
84#endif
85 REG_OFFSET_NAME(orig_ax),
86 REG_OFFSET_NAME(ip),
87 REG_OFFSET_NAME(cs),
88 REG_OFFSET_NAME(flags),
89 REG_OFFSET_NAME(sp),
90 REG_OFFSET_NAME(ss),
91 REG_OFFSET_END,
92};
93
94/**
95 * regs_query_register_offset() - query register offset from its name
96 * @name: the name of a register
97 *
98 * regs_query_register_offset() returns the offset of a register in struct
99 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
100 */
101int regs_query_register_offset(const char *name)
102{
103 const struct pt_regs_offset *roff;
104 for (roff = regoffset_table; roff->name != NULL; roff++)
105 if (!strcmp(roff->name, name))
106 return roff->offset;
107 return -EINVAL;
108}
109
110/**
111 * regs_query_register_name() - query register name from its offset
112 * @offset: the offset of a register in struct pt_regs.
113 *
114 * regs_query_register_name() returns the name of a register from its
115 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
116 */
117const char *regs_query_register_name(unsigned int offset)
118{
119 const struct pt_regs_offset *roff;
120 for (roff = regoffset_table; roff->name != NULL; roff++)
121 if (roff->offset == offset)
122 return roff->name;
123 return NULL;
124}
125
126static const int arg_offs_table[] = {
127#ifdef CONFIG_X86_32
128 [0] = offsetof(struct pt_regs, ax),
129 [1] = offsetof(struct pt_regs, dx),
130 [2] = offsetof(struct pt_regs, cx)
131#else /* CONFIG_X86_64 */
132 [0] = offsetof(struct pt_regs, di),
133 [1] = offsetof(struct pt_regs, si),
134 [2] = offsetof(struct pt_regs, dx),
135 [3] = offsetof(struct pt_regs, cx),
136 [4] = offsetof(struct pt_regs, r8),
137 [5] = offsetof(struct pt_regs, r9)
138#endif
139};
140
Markus Metzgereee3af42008-01-30 13:31:09 +0100141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * does not yet catch signals sent when the child dies.
143 * in exit.c or in signal.c.
144 */
145
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500146/*
147 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500148 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100149#define FLAG_MASK_32 ((unsigned long) \
150 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
151 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
152 X86_EFLAGS_SF | X86_EFLAGS_TF | \
153 X86_EFLAGS_DF | X86_EFLAGS_OF | \
154 X86_EFLAGS_RF | X86_EFLAGS_AC))
155
Roland McGrath2047b082008-01-30 13:31:01 +0100156/*
157 * Determines whether a value may be installed in a segment register.
158 */
159static inline bool invalid_selector(u16 value)
160{
161 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
162}
163
164#ifdef CONFIG_X86_32
165
Roland McGrathe39c2892008-01-30 13:31:01 +0100166#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Robert Richter9d4f9622012-09-03 20:54:48 +0200168/*
169 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
170 * when it traps. The previous stack will be directly underneath the saved
171 * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
172 *
173 * Now, if the stack is empty, '&regs->sp' is out of range. In this
174 * case we try to take the previous stack. To always return a non-null
175 * stack pointer we fall back to regs as stack if no previous stack
176 * exists.
177 *
178 * This is valid only for kernel mode traps.
179 */
180unsigned long kernel_stack_pointer(struct pt_regs *regs)
181{
182 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
183 unsigned long sp = (unsigned long)&regs->sp;
184 struct thread_info *tinfo;
185
186 if (context == (sp & ~(THREAD_SIZE - 1)))
187 return sp;
188
189 tinfo = (struct thread_info *)context;
190 if (tinfo->previous_esp)
191 return tinfo->previous_esp;
192
193 return (unsigned long)regs;
194}
195
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530196static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100198 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900199 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Roland McGrath06ee1b62008-01-30 13:31:01 +0100202static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100204 /*
205 * Returning the value truncates it to 16 bits.
206 */
207 unsigned int retval;
208 if (offset != offsetof(struct user_regs_struct, gs))
209 retval = *pt_regs_access(task_pt_regs(task), offset);
210 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100211 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900212 retval = get_user_gs(task_pt_regs(task));
213 else
214 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100215 }
216 return retval;
217}
218
219static int set_segment_reg(struct task_struct *task,
220 unsigned long offset, u16 value)
221{
222 /*
223 * The value argument was already truncated to 16 bits.
224 */
Roland McGrath2047b082008-01-30 13:31:01 +0100225 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100226 return -EIO;
227
Roland McGrathc63855d2008-02-06 22:39:44 +0100228 /*
229 * For %cs and %ss we cannot permit a null selector.
230 * We can permit a bogus selector as long as it has USER_RPL.
231 * Null selectors are fine for other segment registers, but
232 * we will never get back to user mode with invalid %cs or %ss
233 * and will take the trap in iret instead. Much code relies
234 * on user_mode() to distinguish a user trap frame (which can
235 * safely use invalid selectors) from a kernel trap frame.
236 */
237 switch (offset) {
238 case offsetof(struct user_regs_struct, cs):
239 case offsetof(struct user_regs_struct, ss):
240 if (unlikely(value == 0))
241 return -EIO;
242
243 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100244 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100245 break;
246
247 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100248 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900249 set_user_gs(task_pt_regs(task), value);
250 else
251 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255}
256
Roland McGrath2047b082008-01-30 13:31:01 +0100257#else /* CONFIG_X86_64 */
258
259#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
260
261static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
262{
263 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
264 return &regs->r15 + (offset / sizeof(regs->r15));
265}
266
267static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
268{
269 /*
270 * Returning the value truncates it to 16 bits.
271 */
272 unsigned int seg;
273
274 switch (offset) {
275 case offsetof(struct user_regs_struct, fs):
276 if (task == current) {
277 /* Older gas can't assemble movq %?s,%r?? */
278 asm("movl %%fs,%0" : "=r" (seg));
279 return seg;
280 }
281 return task->thread.fsindex;
282 case offsetof(struct user_regs_struct, gs):
283 if (task == current) {
284 asm("movl %%gs,%0" : "=r" (seg));
285 return seg;
286 }
287 return task->thread.gsindex;
288 case offsetof(struct user_regs_struct, ds):
289 if (task == current) {
290 asm("movl %%ds,%0" : "=r" (seg));
291 return seg;
292 }
293 return task->thread.ds;
294 case offsetof(struct user_regs_struct, es):
295 if (task == current) {
296 asm("movl %%es,%0" : "=r" (seg));
297 return seg;
298 }
299 return task->thread.es;
300
301 case offsetof(struct user_regs_struct, cs):
302 case offsetof(struct user_regs_struct, ss):
303 break;
304 }
305 return *pt_regs_access(task_pt_regs(task), offset);
306}
307
308static int set_segment_reg(struct task_struct *task,
309 unsigned long offset, u16 value)
310{
311 /*
312 * The value argument was already truncated to 16 bits.
313 */
314 if (invalid_selector(value))
315 return -EIO;
316
317 switch (offset) {
318 case offsetof(struct user_regs_struct,fs):
319 /*
320 * If this is setting fs as for normal 64-bit use but
321 * setting fs_base has implicitly changed it, leave it.
322 */
323 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
324 task->thread.fs != 0) ||
325 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
326 task->thread.fs == 0))
327 break;
328 task->thread.fsindex = value;
329 if (task == current)
330 loadsegment(fs, task->thread.fsindex);
331 break;
332 case offsetof(struct user_regs_struct,gs):
333 /*
334 * If this is setting gs as for normal 64-bit use but
335 * setting gs_base has implicitly changed it, leave it.
336 */
337 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
338 task->thread.gs != 0) ||
339 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
340 task->thread.gs == 0))
341 break;
342 task->thread.gsindex = value;
343 if (task == current)
344 load_gs_index(task->thread.gsindex);
345 break;
346 case offsetof(struct user_regs_struct,ds):
347 task->thread.ds = value;
348 if (task == current)
349 loadsegment(ds, task->thread.ds);
350 break;
351 case offsetof(struct user_regs_struct,es):
352 task->thread.es = value;
353 if (task == current)
354 loadsegment(es, task->thread.es);
355 break;
356
357 /*
358 * Can't actually change these in 64-bit mode.
359 */
360 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100361 if (unlikely(value == 0))
362 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100363#ifdef CONFIG_IA32_EMULATION
364 if (test_tsk_thread_flag(task, TIF_IA32))
365 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100366#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100367 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100368 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100369 if (unlikely(value == 0))
370 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100371#ifdef CONFIG_IA32_EMULATION
372 if (test_tsk_thread_flag(task, TIF_IA32))
373 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100374#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100375 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100376 }
377
378 return 0;
379}
380
Roland McGrath2047b082008-01-30 13:31:01 +0100381#endif /* CONFIG_X86_32 */
382
Roland McGrath06ee1b62008-01-30 13:31:01 +0100383static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100385 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Roland McGrath06ee1b62008-01-30 13:31:01 +0100387 /*
388 * If the debugger set TF, hide it from the readout.
389 */
390 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
391 retval &= ~X86_EFLAGS_TF;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return retval;
394}
395
Roland McGrath06ee1b62008-01-30 13:31:01 +0100396static int set_flags(struct task_struct *task, unsigned long value)
397{
398 struct pt_regs *regs = task_pt_regs(task);
399
400 /*
401 * If the user value contains TF, mark that
402 * it was not "us" (the debugger) that set it.
403 * If not, make sure it stays set if we had.
404 */
405 if (value & X86_EFLAGS_TF)
406 clear_tsk_thread_flag(task, TIF_FORCED_TF);
407 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
408 value |= X86_EFLAGS_TF;
409
410 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
411
412 return 0;
413}
414
415static int putreg(struct task_struct *child,
416 unsigned long offset, unsigned long value)
417{
418 switch (offset) {
419 case offsetof(struct user_regs_struct, cs):
420 case offsetof(struct user_regs_struct, ds):
421 case offsetof(struct user_regs_struct, es):
422 case offsetof(struct user_regs_struct, fs):
423 case offsetof(struct user_regs_struct, gs):
424 case offsetof(struct user_regs_struct, ss):
425 return set_segment_reg(child, offset, value);
426
427 case offsetof(struct user_regs_struct, flags):
428 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100429
430#ifdef CONFIG_X86_64
431 case offsetof(struct user_regs_struct,fs_base):
432 if (value >= TASK_SIZE_OF(child))
433 return -EIO;
434 /*
435 * When changing the segment base, use do_arch_prctl
436 * to set either thread.fs or thread.fsindex and the
437 * corresponding GDT slot.
438 */
439 if (child->thread.fs != value)
440 return do_arch_prctl(child, ARCH_SET_FS, value);
441 return 0;
442 case offsetof(struct user_regs_struct,gs_base):
443 /*
444 * Exactly the same here as the %fs handling above.
445 */
446 if (value >= TASK_SIZE_OF(child))
447 return -EIO;
448 if (child->thread.gs != value)
449 return do_arch_prctl(child, ARCH_SET_GS, value);
450 return 0;
451#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100452 }
453
454 *pt_regs_access(task_pt_regs(child), offset) = value;
455 return 0;
456}
457
458static unsigned long getreg(struct task_struct *task, unsigned long offset)
459{
460 switch (offset) {
461 case offsetof(struct user_regs_struct, cs):
462 case offsetof(struct user_regs_struct, ds):
463 case offsetof(struct user_regs_struct, es):
464 case offsetof(struct user_regs_struct, fs):
465 case offsetof(struct user_regs_struct, gs):
466 case offsetof(struct user_regs_struct, ss):
467 return get_segment_reg(task, offset);
468
469 case offsetof(struct user_regs_struct, flags):
470 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100471
472#ifdef CONFIG_X86_64
473 case offsetof(struct user_regs_struct, fs_base): {
474 /*
475 * do_arch_prctl may have used a GDT slot instead of
476 * the MSR. To userland, it appears the same either
477 * way, except the %fs segment selector might not be 0.
478 */
479 unsigned int seg = task->thread.fsindex;
480 if (task->thread.fs != 0)
481 return task->thread.fs;
482 if (task == current)
483 asm("movl %%fs,%0" : "=r" (seg));
484 if (seg != FS_TLS_SEL)
485 return 0;
486 return get_desc_base(&task->thread.tls_array[FS_TLS]);
487 }
488 case offsetof(struct user_regs_struct, gs_base): {
489 /*
490 * Exactly the same here as the %fs handling above.
491 */
492 unsigned int seg = task->thread.gsindex;
493 if (task->thread.gs != 0)
494 return task->thread.gs;
495 if (task == current)
496 asm("movl %%gs,%0" : "=r" (seg));
497 if (seg != GS_TLS_SEL)
498 return 0;
499 return get_desc_base(&task->thread.tls_array[GS_TLS]);
500 }
501#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100502 }
503
504 return *pt_regs_access(task_pt_regs(task), offset);
505}
506
Roland McGrath91e7b702008-01-30 13:31:52 +0100507static int genregs_get(struct task_struct *target,
508 const struct user_regset *regset,
509 unsigned int pos, unsigned int count,
510 void *kbuf, void __user *ubuf)
511{
512 if (kbuf) {
513 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800514 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100515 *k++ = getreg(target, pos);
516 count -= sizeof(*k);
517 pos += sizeof(*k);
518 }
519 } else {
520 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800521 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100522 if (__put_user(getreg(target, pos), u++))
523 return -EFAULT;
524 count -= sizeof(*u);
525 pos += sizeof(*u);
526 }
527 }
528
529 return 0;
530}
531
532static int genregs_set(struct task_struct *target,
533 const struct user_regset *regset,
534 unsigned int pos, unsigned int count,
535 const void *kbuf, const void __user *ubuf)
536{
537 int ret = 0;
538 if (kbuf) {
539 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800540 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100541 ret = putreg(target, pos, *k++);
542 count -= sizeof(*k);
543 pos += sizeof(*k);
544 }
545 } else {
546 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800547 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100548 unsigned long word;
549 ret = __get_user(word, u++);
550 if (ret)
551 break;
552 ret = putreg(target, pos, word);
553 count -= sizeof(*u);
554 pos += sizeof(*u);
555 }
556 }
557 return ret;
558}
559
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200560static void ptrace_triggered(struct perf_event *bp,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100561 struct perf_sample_data *data,
562 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100563{
Roland McGrath0f534092008-01-30 13:30:59 +0100564 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200565 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100566
K.Prasad72f674d2009-06-01 23:45:48 +0530567 /*
568 * Store in the virtual DR6 register the fact that the breakpoint
569 * was hit so the thread's debugger will see it.
570 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200571 for (i = 0; i < HBP_NUM; i++) {
572 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530573 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100574 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100575
K.Prasad72f674d2009-06-01 23:45:48 +0530576 thread->debugreg6 |= (DR_TRAP0 << i);
577}
578
579/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200580 * Walk through every ptrace breakpoints for this thread and
581 * build the dr7 value on top of their attributes.
582 *
583 */
584static unsigned long ptrace_get_dr7(struct perf_event *bp[])
585{
586 int i;
587 int dr7 = 0;
588 struct arch_hw_breakpoint *info;
589
590 for (i = 0; i < HBP_NUM; i++) {
591 if (bp[i] && !bp[i]->attr.disabled) {
592 info = counter_arch_bp(bp[i]);
593 dr7 |= encode_dr7(i, info->len, info->type);
594 }
595 }
596
597 return dr7;
598}
599
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100600static int
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100601ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100602 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100603{
604 int err;
605 int gen_len, gen_type;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100606 struct perf_event_attr attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100607
608 /*
Adam Buchbinderc9404c92009-12-18 15:40:42 -0500609 * We should have at least an inactive breakpoint at this
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100610 * slot. It means the user is writing dr7 without having
611 * written the address register first
612 */
613 if (!bp)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100614 return -EINVAL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100615
616 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
617 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100618 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100619
620 attr = bp->attr;
621 attr.bp_len = gen_len;
622 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100623 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100624
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100625 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100626}
627
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200628/*
K.Prasad72f674d2009-06-01 23:45:48 +0530629 * Handle ptrace writes to debug register 7.
630 */
631static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
632{
633 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200634 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530635 int i, orig_ret = 0, rc = 0;
636 int enabled, second_pass = 0;
637 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200638 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530639
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200640 if (ptrace_get_breakpoints(tsk) < 0)
641 return -ESRCH;
642
K.Prasad72f674d2009-06-01 23:45:48 +0530643 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200644 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530645restore:
646 /*
647 * Loop through all the hardware breakpoints, making the
648 * appropriate changes to each.
649 */
650 for (i = 0; i < HBP_NUM; i++) {
651 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200652 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530653
654 if (!enabled) {
655 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200656 /*
657 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530658 * unless all register_user_hw_breakpoint()
659 * requests have succeeded. This prevents
660 * any window of opportunity for debug
661 * register grabbing by other users.
662 */
663 if (!second_pass)
664 continue;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100665
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100666 rc = ptrace_modify_breakpoint(bp, len, type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100667 tsk, 1);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100668 if (rc)
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100669 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530670 }
671 continue;
672 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200673
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100674 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
675 if (rc)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200676 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530677 }
678 /*
679 * Make a second pass to free the remaining unused breakpoints
680 * or to restore the original breakpoints if an error occurred.
681 */
682 if (!second_pass) {
683 second_pass = 1;
684 if (rc < 0) {
685 orig_ret = rc;
686 data = old_dr7;
687 }
688 goto restore;
689 }
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200690
691 ptrace_put_breakpoints(tsk);
692
K.Prasad72f674d2009-06-01 23:45:48 +0530693 return ((orig_ret < 0) ? orig_ret : rc);
694}
695
696/*
697 * Handle PTRACE_PEEKUSR calls for the debug register area.
698 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530699static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530700{
701 struct thread_struct *thread = &(tsk->thread);
702 unsigned long val = 0;
703
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200704 if (n < HBP_NUM) {
705 struct perf_event *bp;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200706
707 if (ptrace_get_breakpoints(tsk) < 0)
708 return -ESRCH;
709
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200710 bp = thread->ptrace_bps[n];
711 if (!bp)
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200712 val = 0;
713 else
714 val = bp->hw.info.address;
715
716 ptrace_put_breakpoints(tsk);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200717 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530718 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200719 } else if (n == 7) {
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100720 val = thread->ptrace_dr7;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200721 }
K.Prasad72f674d2009-06-01 23:45:48 +0530722 return val;
723}
724
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200725static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
726 unsigned long addr)
727{
728 struct perf_event *bp;
729 struct thread_struct *t = &tsk->thread;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100730 struct perf_event_attr attr;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200731 int err = 0;
732
733 if (ptrace_get_breakpoints(tsk) < 0)
734 return -ESRCH;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200735
736 if (!t->ptrace_bps[nr]) {
Frederic Weisbecker73266fc2010-04-22 05:05:45 +0200737 ptrace_breakpoint_init(&attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200738 /*
739 * Put stub len and type to register (reserve) an inactive but
740 * correct bp
741 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100742 attr.bp_addr = addr;
743 attr.bp_len = HW_BREAKPOINT_LEN_1;
744 attr.bp_type = HW_BREAKPOINT_W;
745 attr.disabled = 1;
746
Avi Kivity4dc0da82011-06-29 18:42:35 +0300747 bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
748 NULL, tsk);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100749
750 /*
751 * CHECKME: the previous code returned -EIO if the addr wasn't
752 * a valid task virtual addr. The new one will return -EINVAL in
753 * this case.
754 * -EINVAL may be what we want for in-kernel breakpoints users,
755 * but -EIO looks better for ptrace, since we refuse a register
756 * writing for the user. And anyway this is the previous
757 * behaviour.
758 */
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200759 if (IS_ERR(bp)) {
760 err = PTR_ERR(bp);
761 goto put;
762 }
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100763
764 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200765 } else {
766 bp = t->ptrace_bps[nr];
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100767
768 attr = bp->attr;
769 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100770 err = modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200771 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200772
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200773put:
774 ptrace_put_breakpoints(tsk);
775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
K.Prasad72f674d2009-06-01 23:45:48 +0530778/*
779 * Handle PTRACE_POKEUSR calls for the debug register area.
780 */
H Hartley Sweeten98b8b992011-11-15 14:48:58 -0800781static int ptrace_set_debugreg(struct task_struct *tsk, int n,
782 unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
K.Prasad72f674d2009-06-01 23:45:48 +0530784 struct thread_struct *thread = &(tsk->thread);
785 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
K.Prasad72f674d2009-06-01 23:45:48 +0530787 /* There are no DR4 or DR5 registers */
788 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EIO;
790
K.Prasad72f674d2009-06-01 23:45:48 +0530791 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200792 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530793 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
K.Prasad72f674d2009-06-01 23:45:48 +0530795 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200796 rc = ptrace_set_breakpoint_addr(tsk, n, val);
797 if (rc)
798 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530799 }
800 /* All that's left is DR7 */
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100801 if (n == 7) {
K.Prasad72f674d2009-06-01 23:45:48 +0530802 rc = ptrace_write_dr7(tsk, val);
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100803 if (!rc)
804 thread->ptrace_dr7 = val;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
K.Prasad72f674d2009-06-01 23:45:48 +0530807ret_path:
808 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Roland McGrath325af5f2008-08-08 15:58:39 -0700811/*
812 * These access the current or another (stopped) task's io permission
813 * bitmap for debugging or core dump.
814 */
815static int ioperm_active(struct task_struct *target,
816 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100817{
Roland McGrath325af5f2008-08-08 15:58:39 -0700818 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100819}
820
Roland McGrath325af5f2008-08-08 15:58:39 -0700821static int ioperm_get(struct task_struct *target,
822 const struct user_regset *regset,
823 unsigned int pos, unsigned int count,
824 void *kbuf, void __user *ubuf)
825{
826 if (!target->thread.io_bitmap_ptr)
827 return -ENXIO;
828
829 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
830 target->thread.io_bitmap_ptr,
831 0, IO_BITMAP_BYTES);
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * Called by kernel/ptrace.c when detaching..
836 *
837 * Make sure the single step bit is not set.
838 */
839void ptrace_disable(struct task_struct *child)
840{
841 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100842#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100844#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Roland McGrath5a4646a2008-01-30 13:31:54 +0100847#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
848static const struct user_regset_view user_x86_32_view; /* Initialized below. */
849#endif
850
Namhyung Kim9b05a692010-10-27 15:33:47 -0700851long arch_ptrace(struct task_struct *child, long request,
852 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100854 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned long __user *datap = (unsigned long __user *)data;
856
857 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* read the word at location addr in the USER area. */
859 case PTRACE_PEEKUSR: {
860 unsigned long tmp;
861
862 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700863 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865
866 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100867 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100869 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
870 addr <= offsetof(struct user, u_debugreg[7])) {
871 addr -= offsetof(struct user, u_debugreg[0]);
872 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 ret = put_user(tmp, datap);
875 break;
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
879 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700880 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882
Roland McGrathe9c86c72008-01-30 13:31:01 +0100883 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100885 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
886 addr <= offsetof(struct user, u_debugreg[7])) {
887 addr -= offsetof(struct user, u_debugreg[0]);
888 ret = ptrace_set_debugreg(child,
889 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Roland McGrath5a4646a2008-01-30 13:31:54 +0100893 case PTRACE_GETREGS: /* Get all gp regs from the child. */
894 return copy_regset_to_user(child,
895 task_user_regset_view(current),
896 REGSET_GENERAL,
897 0, sizeof(struct user_regs_struct),
898 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Roland McGrath5a4646a2008-01-30 13:31:54 +0100900 case PTRACE_SETREGS: /* Set all gp regs in the child. */
901 return copy_regset_from_user(child,
902 task_user_regset_view(current),
903 REGSET_GENERAL,
904 0, sizeof(struct user_regs_struct),
905 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Roland McGrath5a4646a2008-01-30 13:31:54 +0100907 case PTRACE_GETFPREGS: /* Get the child FPU state. */
908 return copy_regset_to_user(child,
909 task_user_regset_view(current),
910 REGSET_FP,
911 0, sizeof(struct user_i387_struct),
912 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Roland McGrath5a4646a2008-01-30 13:31:54 +0100914 case PTRACE_SETFPREGS: /* Set the child FPU state. */
915 return copy_regset_from_user(child,
916 task_user_regset_view(current),
917 REGSET_FP,
918 0, sizeof(struct user_i387_struct),
919 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Roland McGrathe9c86c72008-01-30 13:31:01 +0100921#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100922 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
923 return copy_regset_to_user(child, &user_x86_32_view,
924 REGSET_XFP,
925 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700926 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Roland McGrath5a4646a2008-01-30 13:31:54 +0100928 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
929 return copy_regset_from_user(child, &user_x86_32_view,
930 REGSET_XFP,
931 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700932 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100933#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Roland McGrathe9c86c72008-01-30 13:31:01 +0100935#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 case PTRACE_GET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700937 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100938 return -EIO;
939 ret = do_get_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700940 (struct user_desc __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942
943 case PTRACE_SET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700944 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100945 return -EIO;
946 ret = do_set_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700947 (struct user_desc __user *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100949#endif
950
951#ifdef CONFIG_X86_64
952 /* normal 64bit interface to access TLS data.
953 Works just like arch_prctl, except that the arguments
954 are reversed. */
955 case PTRACE_ARCH_PRCTL:
956 ret = do_arch_prctl(child, data, addr);
957 break;
958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 default:
961 ret = ptrace_request(child, request, addr, data);
962 break;
963 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return ret;
966}
967
Roland McGrathcb757c42008-01-30 13:31:01 +0100968#ifdef CONFIG_IA32_EMULATION
969
Roland McGrath099cd6e2008-01-30 13:31:01 +0100970#include <linux/compat.h>
971#include <linux/syscalls.h>
972#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +0100973#include <asm/user32.h>
974
975#define R32(l,q) \
976 case offsetof(struct user32, regs.l): \
977 regs->q = value; break
978
979#define SEG32(rs) \
980 case offsetof(struct user32, regs.rs): \
981 return set_segment_reg(child, \
982 offsetof(struct user_regs_struct, rs), \
983 value); \
984 break
985
986static int putreg32(struct task_struct *child, unsigned regno, u32 value)
987{
988 struct pt_regs *regs = task_pt_regs(child);
989
990 switch (regno) {
991
992 SEG32(cs);
993 SEG32(ds);
994 SEG32(es);
995 SEG32(fs);
996 SEG32(gs);
997 SEG32(ss);
998
999 R32(ebx, bx);
1000 R32(ecx, cx);
1001 R32(edx, dx);
1002 R32(edi, di);
1003 R32(esi, si);
1004 R32(ebp, bp);
1005 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001006 R32(eip, ip);
1007 R32(esp, sp);
1008
Roland McGrath40f09332008-02-28 19:57:07 -08001009 case offsetof(struct user32, regs.orig_eax):
1010 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001011 * A 32-bit debugger setting orig_eax means to restore
1012 * the state of the task restarting a 32-bit syscall.
1013 * Make sure we interpret the -ERESTART* codes correctly
1014 * in case the task is not actually still sitting at the
1015 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001016 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001017 regs->orig_ax = value;
1018 if (syscall_get_nr(child, regs) >= 0)
1019 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001020 break;
1021
Roland McGrathcb757c42008-01-30 13:31:01 +01001022 case offsetof(struct user32, regs.eflags):
1023 return set_flags(child, value);
1024
1025 case offsetof(struct user32, u_debugreg[0]) ...
1026 offsetof(struct user32, u_debugreg[7]):
1027 regno -= offsetof(struct user32, u_debugreg[0]);
1028 return ptrace_set_debugreg(child, regno / 4, value);
1029
1030 default:
1031 if (regno > sizeof(struct user32) || (regno & 3))
1032 return -EIO;
1033
1034 /*
1035 * Other dummy fields in the virtual user structure
1036 * are ignored
1037 */
1038 break;
1039 }
1040 return 0;
1041}
1042
1043#undef R32
1044#undef SEG32
1045
1046#define R32(l,q) \
1047 case offsetof(struct user32, regs.l): \
1048 *val = regs->q; break
1049
1050#define SEG32(rs) \
1051 case offsetof(struct user32, regs.rs): \
1052 *val = get_segment_reg(child, \
1053 offsetof(struct user_regs_struct, rs)); \
1054 break
1055
1056static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1057{
1058 struct pt_regs *regs = task_pt_regs(child);
1059
1060 switch (regno) {
1061
1062 SEG32(ds);
1063 SEG32(es);
1064 SEG32(fs);
1065 SEG32(gs);
1066
1067 R32(cs, cs);
1068 R32(ss, ss);
1069 R32(ebx, bx);
1070 R32(ecx, cx);
1071 R32(edx, dx);
1072 R32(edi, di);
1073 R32(esi, si);
1074 R32(ebp, bp);
1075 R32(eax, ax);
1076 R32(orig_eax, orig_ax);
1077 R32(eip, ip);
1078 R32(esp, sp);
1079
1080 case offsetof(struct user32, regs.eflags):
1081 *val = get_flags(child);
1082 break;
1083
1084 case offsetof(struct user32, u_debugreg[0]) ...
1085 offsetof(struct user32, u_debugreg[7]):
1086 regno -= offsetof(struct user32, u_debugreg[0]);
1087 *val = ptrace_get_debugreg(child, regno / 4);
1088 break;
1089
1090 default:
1091 if (regno > sizeof(struct user32) || (regno & 3))
1092 return -EIO;
1093
1094 /*
1095 * Other dummy fields in the virtual user structure
1096 * are ignored
1097 */
1098 *val = 0;
1099 break;
1100 }
1101 return 0;
1102}
1103
1104#undef R32
1105#undef SEG32
1106
Roland McGrath91e7b702008-01-30 13:31:52 +01001107static int genregs32_get(struct task_struct *target,
1108 const struct user_regset *regset,
1109 unsigned int pos, unsigned int count,
1110 void *kbuf, void __user *ubuf)
1111{
1112 if (kbuf) {
1113 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001114 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001115 getreg32(target, pos, k++);
1116 count -= sizeof(*k);
1117 pos += sizeof(*k);
1118 }
1119 } else {
1120 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001121 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001122 compat_ulong_t word;
1123 getreg32(target, pos, &word);
1124 if (__put_user(word, u++))
1125 return -EFAULT;
1126 count -= sizeof(*u);
1127 pos += sizeof(*u);
1128 }
1129 }
1130
1131 return 0;
1132}
1133
1134static int genregs32_set(struct task_struct *target,
1135 const struct user_regset *regset,
1136 unsigned int pos, unsigned int count,
1137 const void *kbuf, const void __user *ubuf)
1138{
1139 int ret = 0;
1140 if (kbuf) {
1141 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001142 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001143 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001144 count -= sizeof(*k);
1145 pos += sizeof(*k);
1146 }
1147 } else {
1148 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001149 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001150 compat_ulong_t word;
1151 ret = __get_user(word, u++);
1152 if (ret)
1153 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001154 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001155 count -= sizeof(*u);
1156 pos += sizeof(*u);
1157 }
1158 }
1159 return ret;
1160}
1161
H.J. Lu55283e22012-03-05 15:32:11 -08001162#ifdef CONFIG_X86_X32_ABI
1163static long x32_arch_ptrace(struct task_struct *child,
1164 compat_long_t request, compat_ulong_t caddr,
1165 compat_ulong_t cdata)
1166{
1167 unsigned long addr = caddr;
1168 unsigned long data = cdata;
1169 void __user *datap = compat_ptr(data);
1170 int ret;
1171
1172 switch (request) {
1173 /* Read 32bits at location addr in the USER area. Only allow
1174 to return the lower 32bits of segment and debug registers. */
1175 case PTRACE_PEEKUSR: {
1176 u32 tmp;
1177
1178 ret = -EIO;
1179 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1180 addr < offsetof(struct user_regs_struct, cs))
1181 break;
1182
1183 tmp = 0; /* Default return condition */
1184 if (addr < sizeof(struct user_regs_struct))
1185 tmp = getreg(child, addr);
1186 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1187 addr <= offsetof(struct user, u_debugreg[7])) {
1188 addr -= offsetof(struct user, u_debugreg[0]);
1189 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1190 }
1191 ret = put_user(tmp, (__u32 __user *)datap);
1192 break;
1193 }
1194
1195 /* Write the word at location addr in the USER area. Only allow
1196 to update segment and debug registers with the upper 32bits
1197 zero-extended. */
1198 case PTRACE_POKEUSR:
1199 ret = -EIO;
1200 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1201 addr < offsetof(struct user_regs_struct, cs))
1202 break;
1203
1204 if (addr < sizeof(struct user_regs_struct))
1205 ret = putreg(child, addr, data);
1206 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1207 addr <= offsetof(struct user, u_debugreg[7])) {
1208 addr -= offsetof(struct user, u_debugreg[0]);
1209 ret = ptrace_set_debugreg(child,
1210 addr / sizeof(data), data);
1211 }
1212 break;
1213
1214 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1215 return copy_regset_to_user(child,
1216 task_user_regset_view(current),
1217 REGSET_GENERAL,
1218 0, sizeof(struct user_regs_struct),
1219 datap);
1220
1221 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1222 return copy_regset_from_user(child,
1223 task_user_regset_view(current),
1224 REGSET_GENERAL,
1225 0, sizeof(struct user_regs_struct),
1226 datap);
1227
1228 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1229 return copy_regset_to_user(child,
1230 task_user_regset_view(current),
1231 REGSET_FP,
1232 0, sizeof(struct user_i387_struct),
1233 datap);
1234
1235 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1236 return copy_regset_from_user(child,
1237 task_user_regset_view(current),
1238 REGSET_FP,
1239 0, sizeof(struct user_i387_struct),
1240 datap);
1241
H.J. Lu55283e22012-03-05 15:32:11 -08001242 default:
1243 return compat_ptrace_request(child, request, addr, data);
1244 }
1245
1246 return ret;
1247}
1248#endif
1249
Roland McGrath562b80b2008-04-22 12:21:25 -07001250long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1251 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001252{
Roland McGrath562b80b2008-04-22 12:21:25 -07001253 unsigned long addr = caddr;
1254 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001255 void __user *datap = compat_ptr(data);
1256 int ret;
1257 __u32 val;
1258
H.J. Lu55283e22012-03-05 15:32:11 -08001259#ifdef CONFIG_X86_X32_ABI
1260 if (!is_ia32_task())
1261 return x32_arch_ptrace(child, request, caddr, cdata);
1262#endif
1263
Roland McGrath099cd6e2008-01-30 13:31:01 +01001264 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001265 case PTRACE_PEEKUSR:
1266 ret = getreg32(child, addr, &val);
1267 if (ret == 0)
1268 ret = put_user(val, (__u32 __user *)datap);
1269 break;
1270
1271 case PTRACE_POKEUSR:
1272 ret = putreg32(child, addr, data);
1273 break;
1274
Roland McGrath5a4646a2008-01-30 13:31:54 +01001275 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1276 return copy_regset_to_user(child, &user_x86_32_view,
1277 REGSET_GENERAL,
1278 0, sizeof(struct user_regs_struct32),
1279 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001280
Roland McGrath5a4646a2008-01-30 13:31:54 +01001281 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1282 return copy_regset_from_user(child, &user_x86_32_view,
1283 REGSET_GENERAL, 0,
1284 sizeof(struct user_regs_struct32),
1285 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001286
Roland McGrath5a4646a2008-01-30 13:31:54 +01001287 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1288 return copy_regset_to_user(child, &user_x86_32_view,
1289 REGSET_FP, 0,
1290 sizeof(struct user_i387_ia32_struct),
1291 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001292
Roland McGrath5a4646a2008-01-30 13:31:54 +01001293 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1294 return copy_regset_from_user(
1295 child, &user_x86_32_view, REGSET_FP,
1296 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001297
Roland McGrath5a4646a2008-01-30 13:31:54 +01001298 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1299 return copy_regset_to_user(child, &user_x86_32_view,
1300 REGSET_XFP, 0,
1301 sizeof(struct user32_fxsr_struct),
1302 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001303
Roland McGrath5a4646a2008-01-30 13:31:54 +01001304 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1305 return copy_regset_from_user(child, &user_x86_32_view,
1306 REGSET_XFP, 0,
1307 sizeof(struct user32_fxsr_struct),
1308 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001309
Roland McGrath562b80b2008-04-22 12:21:25 -07001310 case PTRACE_GET_THREAD_AREA:
1311 case PTRACE_SET_THREAD_AREA:
1312 return arch_ptrace(child, request, addr, data);
1313
Roland McGrath099cd6e2008-01-30 13:31:01 +01001314 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001315 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001316 }
1317
Roland McGrath099cd6e2008-01-30 13:31:01 +01001318 return ret;
1319}
1320
Roland McGrathcb757c42008-01-30 13:31:01 +01001321#endif /* CONFIG_IA32_EMULATION */
1322
Roland McGrath070459d2008-01-30 13:31:53 +01001323#ifdef CONFIG_X86_64
1324
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001325static struct user_regset x86_64_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001326 [REGSET_GENERAL] = {
1327 .core_note_type = NT_PRSTATUS,
1328 .n = sizeof(struct user_regs_struct) / sizeof(long),
1329 .size = sizeof(long), .align = sizeof(long),
1330 .get = genregs_get, .set = genregs_set
1331 },
1332 [REGSET_FP] = {
1333 .core_note_type = NT_PRFPREG,
1334 .n = sizeof(struct user_i387_struct) / sizeof(long),
1335 .size = sizeof(long), .align = sizeof(long),
1336 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1337 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001338 [REGSET_XSTATE] = {
1339 .core_note_type = NT_X86_XSTATE,
1340 .size = sizeof(u64), .align = sizeof(u64),
1341 .active = xstateregs_active, .get = xstateregs_get,
1342 .set = xstateregs_set
1343 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001344 [REGSET_IOPERM64] = {
1345 .core_note_type = NT_386_IOPERM,
1346 .n = IO_BITMAP_LONGS,
1347 .size = sizeof(long), .align = sizeof(long),
1348 .active = ioperm_active, .get = ioperm_get
1349 },
Roland McGrath070459d2008-01-30 13:31:53 +01001350};
1351
1352static const struct user_regset_view user_x86_64_view = {
1353 .name = "x86_64", .e_machine = EM_X86_64,
1354 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1355};
1356
1357#else /* CONFIG_X86_32 */
1358
1359#define user_regs_struct32 user_regs_struct
1360#define genregs32_get genregs_get
1361#define genregs32_set genregs_set
1362
Roland McGrath1f465f42008-05-09 15:43:44 -07001363#define user_i387_ia32_struct user_i387_struct
1364#define user32_fxsr_struct user_fxsr_struct
1365
Roland McGrath070459d2008-01-30 13:31:53 +01001366#endif /* CONFIG_X86_64 */
1367
1368#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001369static struct user_regset x86_32_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001370 [REGSET_GENERAL] = {
1371 .core_note_type = NT_PRSTATUS,
1372 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1373 .size = sizeof(u32), .align = sizeof(u32),
1374 .get = genregs32_get, .set = genregs32_set
1375 },
1376 [REGSET_FP] = {
1377 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001378 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001379 .size = sizeof(u32), .align = sizeof(u32),
1380 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1381 },
1382 [REGSET_XFP] = {
1383 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001384 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001385 .size = sizeof(u32), .align = sizeof(u32),
1386 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1387 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001388 [REGSET_XSTATE] = {
1389 .core_note_type = NT_X86_XSTATE,
1390 .size = sizeof(u64), .align = sizeof(u64),
1391 .active = xstateregs_active, .get = xstateregs_get,
1392 .set = xstateregs_set
1393 },
Roland McGrath070459d2008-01-30 13:31:53 +01001394 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001395 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001396 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1397 .size = sizeof(struct user_desc),
1398 .align = sizeof(struct user_desc),
1399 .active = regset_tls_active,
1400 .get = regset_tls_get, .set = regset_tls_set
1401 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001402 [REGSET_IOPERM32] = {
1403 .core_note_type = NT_386_IOPERM,
1404 .n = IO_BITMAP_BYTES / sizeof(u32),
1405 .size = sizeof(u32), .align = sizeof(u32),
1406 .active = ioperm_active, .get = ioperm_get
1407 },
Roland McGrath070459d2008-01-30 13:31:53 +01001408};
1409
1410static const struct user_regset_view user_x86_32_view = {
1411 .name = "i386", .e_machine = EM_386,
1412 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1413};
1414#endif
1415
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001416/*
1417 * This represents bytes 464..511 in the memory layout exported through
1418 * the REGSET_XSTATE interface.
1419 */
1420u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1421
1422void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1423{
1424#ifdef CONFIG_X86_64
1425 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1426#endif
1427#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1428 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1429#endif
1430 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1431}
1432
Roland McGrath070459d2008-01-30 13:31:53 +01001433const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1434{
1435#ifdef CONFIG_IA32_EMULATION
1436 if (test_tsk_thread_flag(task, TIF_IA32))
1437#endif
1438#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1439 return &user_x86_32_view;
1440#endif
1441#ifdef CONFIG_X86_64
1442 return &user_x86_64_view;
1443#endif
1444}
1445
Oleg Nesterov7f385512009-12-15 16:47:20 -08001446static void fill_sigtrap_info(struct task_struct *tsk,
1447 struct pt_regs *regs,
1448 int error_code, int si_code,
1449 struct siginfo *info)
1450{
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +05301451 tsk->thread.trap_nr = X86_TRAP_DB;
Oleg Nesterov7f385512009-12-15 16:47:20 -08001452 tsk->thread.error_code = error_code;
1453
1454 memset(info, 0, sizeof(*info));
1455 info->si_signo = SIGTRAP;
1456 info->si_code = si_code;
1457 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1458}
1459
1460void user_single_step_siginfo(struct task_struct *tsk,
1461 struct pt_regs *regs,
1462 struct siginfo *info)
1463{
1464 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1465}
1466
Srinivasa Dsda654b72008-09-23 15:23:52 +05301467void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1468 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 struct siginfo info;
1471
Oleg Nesterov7f385512009-12-15 16:47:20 -08001472 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
Simon Arlott27b46d72007-10-20 01:13:56 +02001473 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 force_sig_info(SIGTRAP, &info, tsk);
1475}
1476
Roland McGrath86976cd2008-01-30 13:31:01 +01001477
Roland McGrathd4d67152008-07-09 02:38:07 -07001478#ifdef CONFIG_X86_32
1479# define IS_IA32 1
1480#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001481# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001482#else
1483# define IS_IA32 0
1484#endif
1485
1486/*
1487 * We must return the syscall number to actually look up in the table.
1488 * This can be -1L to skip running any syscall at all.
1489 */
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001490long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001491{
Roland McGrathd4d67152008-07-09 02:38:07 -07001492 long ret = 0;
1493
Roland McGrath380fdd72008-07-09 02:39:29 -07001494 /*
1495 * If we stepped into a sysenter/syscall insn, it trapped in
1496 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1497 * If user-mode had set TF itself, then it's still clear from
1498 * do_debug() and we need to set it again to restore the user
1499 * state. If we entered on the slow path, TF was already set.
1500 */
1501 if (test_thread_flag(TIF_SINGLESTEP))
1502 regs->flags |= X86_EFLAGS_TF;
1503
Roland McGrath86976cd2008-01-30 13:31:01 +01001504 /* do the secure computing check first */
1505 secure_computing(regs->orig_ax);
1506
Roland McGrathd4d67152008-07-09 02:38:07 -07001507 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1508 ret = -1L;
1509
Roland McGratheeea3c32008-03-16 23:36:28 -07001510 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1511 tracehook_report_syscall_entry(regs))
1512 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001513
Josh Stone66700002009-08-24 14:43:11 -07001514 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001515 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001516
Eric Parisb05d8442012-01-03 14:23:06 -05001517 if (IS_IA32)
1518 audit_syscall_entry(AUDIT_ARCH_I386,
1519 regs->orig_ax,
1520 regs->bx, regs->cx,
1521 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001522#ifdef CONFIG_X86_64
Eric Parisb05d8442012-01-03 14:23:06 -05001523 else
1524 audit_syscall_entry(AUDIT_ARCH_X86_64,
1525 regs->orig_ax,
1526 regs->di, regs->si,
1527 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001528#endif
Roland McGrathd4d67152008-07-09 02:38:07 -07001529
1530 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001531}
1532
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001533void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001534{
Oleg Nesterovd5196502009-12-15 16:47:21 -08001535 bool step;
1536
Eric Parisd7e75282012-01-03 14:23:06 -05001537 audit_syscall_exit(regs);
Roland McGrath86976cd2008-01-30 13:31:01 +01001538
Josh Stone66700002009-08-24 14:43:11 -07001539 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001540 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001541
Roland McGrathd4d67152008-07-09 02:38:07 -07001542 /*
1543 * If TIF_SYSCALL_EMU is set, we only get here because of
1544 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1545 * We already reported this syscall instruction in
Oleg Nesterovd5196502009-12-15 16:47:21 -08001546 * syscall_trace_enter().
Roland McGrathd4d67152008-07-09 02:38:07 -07001547 */
Oleg Nesterovd5196502009-12-15 16:47:21 -08001548 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1549 !test_thread_flag(TIF_SYSCALL_EMU);
1550 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1551 tracehook_report_syscall_exit(regs, step);
Roland McGrathd4d67152008-07-09 02:38:07 -07001552}