blob: 2484e331a64d4ccfc9b92b243d17197b5120930c [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>
Frederic Weisbeckerbf5a3c12012-07-11 20:26:34 +020024#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/uaccess.h>
27#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/processor.h>
29#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080030#include <asm/fpu-internal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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>
K.Prasad72f674d2009-06-01 23:45:48 +053036#include <asm/hw_breakpoint.h>
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +053037#include <asm/traps.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010038
Roland McGrath070459d2008-01-30 13:31:53 +010039#include "tls.h"
40
Josh Stone1c569f02009-08-24 14:43:14 -070041#define CREATE_TRACE_POINTS
42#include <trace/events/syscalls.h>
43
Roland McGrath070459d2008-01-30 13:31:53 +010044enum x86_regset {
45 REGSET_GENERAL,
46 REGSET_FP,
47 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070048 REGSET_IOPERM64 = REGSET_XFP,
Suresh Siddha5b3efd52010-02-11 11:50:59 -080049 REGSET_XSTATE,
Roland McGrath070459d2008-01-30 13:31:53 +010050 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070051 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010052};
Markus Metzgereee3af42008-01-30 13:31:09 +010053
Masami Hiramatsub1cf5402009-08-13 16:34:44 -040054struct pt_regs_offset {
55 const char *name;
56 int offset;
57};
58
59#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
60#define REG_OFFSET_END {.name = NULL, .offset = 0}
61
62static const struct pt_regs_offset regoffset_table[] = {
63#ifdef CONFIG_X86_64
64 REG_OFFSET_NAME(r15),
65 REG_OFFSET_NAME(r14),
66 REG_OFFSET_NAME(r13),
67 REG_OFFSET_NAME(r12),
68 REG_OFFSET_NAME(r11),
69 REG_OFFSET_NAME(r10),
70 REG_OFFSET_NAME(r9),
71 REG_OFFSET_NAME(r8),
72#endif
73 REG_OFFSET_NAME(bx),
74 REG_OFFSET_NAME(cx),
75 REG_OFFSET_NAME(dx),
76 REG_OFFSET_NAME(si),
77 REG_OFFSET_NAME(di),
78 REG_OFFSET_NAME(bp),
79 REG_OFFSET_NAME(ax),
80#ifdef CONFIG_X86_32
81 REG_OFFSET_NAME(ds),
82 REG_OFFSET_NAME(es),
83 REG_OFFSET_NAME(fs),
84 REG_OFFSET_NAME(gs),
85#endif
86 REG_OFFSET_NAME(orig_ax),
87 REG_OFFSET_NAME(ip),
88 REG_OFFSET_NAME(cs),
89 REG_OFFSET_NAME(flags),
90 REG_OFFSET_NAME(sp),
91 REG_OFFSET_NAME(ss),
92 REG_OFFSET_END,
93};
94
95/**
96 * regs_query_register_offset() - query register offset from its name
97 * @name: the name of a register
98 *
99 * regs_query_register_offset() returns the offset of a register in struct
100 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
101 */
102int regs_query_register_offset(const char *name)
103{
104 const struct pt_regs_offset *roff;
105 for (roff = regoffset_table; roff->name != NULL; roff++)
106 if (!strcmp(roff->name, name))
107 return roff->offset;
108 return -EINVAL;
109}
110
111/**
112 * regs_query_register_name() - query register name from its offset
113 * @offset: the offset of a register in struct pt_regs.
114 *
115 * regs_query_register_name() returns the name of a register from its
116 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
117 */
118const char *regs_query_register_name(unsigned int offset)
119{
120 const struct pt_regs_offset *roff;
121 for (roff = regoffset_table; roff->name != NULL; roff++)
122 if (roff->offset == offset)
123 return roff->name;
124 return NULL;
125}
126
127static const int arg_offs_table[] = {
128#ifdef CONFIG_X86_32
129 [0] = offsetof(struct pt_regs, ax),
130 [1] = offsetof(struct pt_regs, dx),
131 [2] = offsetof(struct pt_regs, cx)
132#else /* CONFIG_X86_64 */
133 [0] = offsetof(struct pt_regs, di),
134 [1] = offsetof(struct pt_regs, si),
135 [2] = offsetof(struct pt_regs, dx),
136 [3] = offsetof(struct pt_regs, cx),
137 [4] = offsetof(struct pt_regs, r8),
138 [5] = offsetof(struct pt_regs, r9)
139#endif
140};
141
Markus Metzgereee3af42008-01-30 13:31:09 +0100142/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * does not yet catch signals sent when the child dies.
144 * in exit.c or in signal.c.
145 */
146
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500147/*
148 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500149 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100150#define FLAG_MASK_32 ((unsigned long) \
151 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
152 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
153 X86_EFLAGS_SF | X86_EFLAGS_TF | \
154 X86_EFLAGS_DF | X86_EFLAGS_OF | \
155 X86_EFLAGS_RF | X86_EFLAGS_AC))
156
Roland McGrath2047b082008-01-30 13:31:01 +0100157/*
158 * Determines whether a value may be installed in a segment register.
159 */
160static inline bool invalid_selector(u16 value)
161{
162 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
163}
164
165#ifdef CONFIG_X86_32
166
Roland McGrathe39c2892008-01-30 13:31:01 +0100167#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Robert Richter10226232012-09-03 20:54:48 +0200169/*
170 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
171 * when it traps. The previous stack will be directly underneath the saved
172 * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
173 *
174 * Now, if the stack is empty, '&regs->sp' is out of range. In this
175 * case we try to take the previous stack. To always return a non-null
176 * stack pointer we fall back to regs as stack if no previous stack
177 * exists.
178 *
179 * This is valid only for kernel mode traps.
180 */
181unsigned long kernel_stack_pointer(struct pt_regs *regs)
182{
183 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
184 unsigned long sp = (unsigned long)&regs->sp;
185 struct thread_info *tinfo;
186
187 if (context == (sp & ~(THREAD_SIZE - 1)))
188 return sp;
189
190 tinfo = (struct thread_info *)context;
191 if (tinfo->previous_esp)
192 return tinfo->previous_esp;
193
194 return (unsigned long)regs;
195}
196
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530197static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100199 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900200 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Roland McGrath06ee1b62008-01-30 13:31:01 +0100203static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100205 /*
206 * Returning the value truncates it to 16 bits.
207 */
208 unsigned int retval;
209 if (offset != offsetof(struct user_regs_struct, gs))
210 retval = *pt_regs_access(task_pt_regs(task), offset);
211 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100212 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900213 retval = get_user_gs(task_pt_regs(task));
214 else
215 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100216 }
217 return retval;
218}
219
220static int set_segment_reg(struct task_struct *task,
221 unsigned long offset, u16 value)
222{
223 /*
224 * The value argument was already truncated to 16 bits.
225 */
Roland McGrath2047b082008-01-30 13:31:01 +0100226 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100227 return -EIO;
228
Roland McGrathc63855d2008-02-06 22:39:44 +0100229 /*
230 * For %cs and %ss we cannot permit a null selector.
231 * We can permit a bogus selector as long as it has USER_RPL.
232 * Null selectors are fine for other segment registers, but
233 * we will never get back to user mode with invalid %cs or %ss
234 * and will take the trap in iret instead. Much code relies
235 * on user_mode() to distinguish a user trap frame (which can
236 * safely use invalid selectors) from a kernel trap frame.
237 */
238 switch (offset) {
239 case offsetof(struct user_regs_struct, cs):
240 case offsetof(struct user_regs_struct, ss):
241 if (unlikely(value == 0))
242 return -EIO;
243
244 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100245 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100246 break;
247
248 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100249 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900250 set_user_gs(task_pt_regs(task), value);
251 else
252 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256}
257
Roland McGrath2047b082008-01-30 13:31:01 +0100258#else /* CONFIG_X86_64 */
259
260#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
261
262static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
263{
264 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
265 return &regs->r15 + (offset / sizeof(regs->r15));
266}
267
268static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
269{
270 /*
271 * Returning the value truncates it to 16 bits.
272 */
273 unsigned int seg;
274
275 switch (offset) {
276 case offsetof(struct user_regs_struct, fs):
277 if (task == current) {
278 /* Older gas can't assemble movq %?s,%r?? */
279 asm("movl %%fs,%0" : "=r" (seg));
280 return seg;
281 }
282 return task->thread.fsindex;
283 case offsetof(struct user_regs_struct, gs):
284 if (task == current) {
285 asm("movl %%gs,%0" : "=r" (seg));
286 return seg;
287 }
288 return task->thread.gsindex;
289 case offsetof(struct user_regs_struct, ds):
290 if (task == current) {
291 asm("movl %%ds,%0" : "=r" (seg));
292 return seg;
293 }
294 return task->thread.ds;
295 case offsetof(struct user_regs_struct, es):
296 if (task == current) {
297 asm("movl %%es,%0" : "=r" (seg));
298 return seg;
299 }
300 return task->thread.es;
301
302 case offsetof(struct user_regs_struct, cs):
303 case offsetof(struct user_regs_struct, ss):
304 break;
305 }
306 return *pt_regs_access(task_pt_regs(task), offset);
307}
308
309static int set_segment_reg(struct task_struct *task,
310 unsigned long offset, u16 value)
311{
312 /*
313 * The value argument was already truncated to 16 bits.
314 */
315 if (invalid_selector(value))
316 return -EIO;
317
318 switch (offset) {
319 case offsetof(struct user_regs_struct,fs):
320 /*
321 * If this is setting fs as for normal 64-bit use but
322 * setting fs_base has implicitly changed it, leave it.
323 */
324 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
325 task->thread.fs != 0) ||
326 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
327 task->thread.fs == 0))
328 break;
329 task->thread.fsindex = value;
330 if (task == current)
331 loadsegment(fs, task->thread.fsindex);
332 break;
333 case offsetof(struct user_regs_struct,gs):
334 /*
335 * If this is setting gs as for normal 64-bit use but
336 * setting gs_base has implicitly changed it, leave it.
337 */
338 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
339 task->thread.gs != 0) ||
340 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
341 task->thread.gs == 0))
342 break;
343 task->thread.gsindex = value;
344 if (task == current)
345 load_gs_index(task->thread.gsindex);
346 break;
347 case offsetof(struct user_regs_struct,ds):
348 task->thread.ds = value;
349 if (task == current)
350 loadsegment(ds, task->thread.ds);
351 break;
352 case offsetof(struct user_regs_struct,es):
353 task->thread.es = value;
354 if (task == current)
355 loadsegment(es, task->thread.es);
356 break;
357
358 /*
359 * Can't actually change these in 64-bit mode.
360 */
361 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100362 if (unlikely(value == 0))
363 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100364#ifdef CONFIG_IA32_EMULATION
365 if (test_tsk_thread_flag(task, TIF_IA32))
366 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100367#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100368 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100369 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100370 if (unlikely(value == 0))
371 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100372#ifdef CONFIG_IA32_EMULATION
373 if (test_tsk_thread_flag(task, TIF_IA32))
374 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100375#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100376 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100377 }
378
379 return 0;
380}
381
Roland McGrath2047b082008-01-30 13:31:01 +0100382#endif /* CONFIG_X86_32 */
383
Roland McGrath06ee1b62008-01-30 13:31:01 +0100384static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100386 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Roland McGrath06ee1b62008-01-30 13:31:01 +0100388 /*
389 * If the debugger set TF, hide it from the readout.
390 */
391 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
392 retval &= ~X86_EFLAGS_TF;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return retval;
395}
396
Roland McGrath06ee1b62008-01-30 13:31:01 +0100397static int set_flags(struct task_struct *task, unsigned long value)
398{
399 struct pt_regs *regs = task_pt_regs(task);
400
401 /*
402 * If the user value contains TF, mark that
403 * it was not "us" (the debugger) that set it.
404 * If not, make sure it stays set if we had.
405 */
406 if (value & X86_EFLAGS_TF)
407 clear_tsk_thread_flag(task, TIF_FORCED_TF);
408 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
409 value |= X86_EFLAGS_TF;
410
411 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
412
413 return 0;
414}
415
416static int putreg(struct task_struct *child,
417 unsigned long offset, unsigned long value)
418{
419 switch (offset) {
420 case offsetof(struct user_regs_struct, cs):
421 case offsetof(struct user_regs_struct, ds):
422 case offsetof(struct user_regs_struct, es):
423 case offsetof(struct user_regs_struct, fs):
424 case offsetof(struct user_regs_struct, gs):
425 case offsetof(struct user_regs_struct, ss):
426 return set_segment_reg(child, offset, value);
427
428 case offsetof(struct user_regs_struct, flags):
429 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100430
431#ifdef CONFIG_X86_64
432 case offsetof(struct user_regs_struct,fs_base):
433 if (value >= TASK_SIZE_OF(child))
434 return -EIO;
435 /*
436 * When changing the segment base, use do_arch_prctl
437 * to set either thread.fs or thread.fsindex and the
438 * corresponding GDT slot.
439 */
440 if (child->thread.fs != value)
441 return do_arch_prctl(child, ARCH_SET_FS, value);
442 return 0;
443 case offsetof(struct user_regs_struct,gs_base):
444 /*
445 * Exactly the same here as the %fs handling above.
446 */
447 if (value >= TASK_SIZE_OF(child))
448 return -EIO;
449 if (child->thread.gs != value)
450 return do_arch_prctl(child, ARCH_SET_GS, value);
451 return 0;
452#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100453 }
454
455 *pt_regs_access(task_pt_regs(child), offset) = value;
456 return 0;
457}
458
459static unsigned long getreg(struct task_struct *task, unsigned long offset)
460{
461 switch (offset) {
462 case offsetof(struct user_regs_struct, cs):
463 case offsetof(struct user_regs_struct, ds):
464 case offsetof(struct user_regs_struct, es):
465 case offsetof(struct user_regs_struct, fs):
466 case offsetof(struct user_regs_struct, gs):
467 case offsetof(struct user_regs_struct, ss):
468 return get_segment_reg(task, offset);
469
470 case offsetof(struct user_regs_struct, flags):
471 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100472
473#ifdef CONFIG_X86_64
474 case offsetof(struct user_regs_struct, fs_base): {
475 /*
476 * do_arch_prctl may have used a GDT slot instead of
477 * the MSR. To userland, it appears the same either
478 * way, except the %fs segment selector might not be 0.
479 */
480 unsigned int seg = task->thread.fsindex;
481 if (task->thread.fs != 0)
482 return task->thread.fs;
483 if (task == current)
484 asm("movl %%fs,%0" : "=r" (seg));
485 if (seg != FS_TLS_SEL)
486 return 0;
487 return get_desc_base(&task->thread.tls_array[FS_TLS]);
488 }
489 case offsetof(struct user_regs_struct, gs_base): {
490 /*
491 * Exactly the same here as the %fs handling above.
492 */
493 unsigned int seg = task->thread.gsindex;
494 if (task->thread.gs != 0)
495 return task->thread.gs;
496 if (task == current)
497 asm("movl %%gs,%0" : "=r" (seg));
498 if (seg != GS_TLS_SEL)
499 return 0;
500 return get_desc_base(&task->thread.tls_array[GS_TLS]);
501 }
502#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100503 }
504
505 return *pt_regs_access(task_pt_regs(task), offset);
506}
507
Roland McGrath91e7b702008-01-30 13:31:52 +0100508static int genregs_get(struct task_struct *target,
509 const struct user_regset *regset,
510 unsigned int pos, unsigned int count,
511 void *kbuf, void __user *ubuf)
512{
513 if (kbuf) {
514 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800515 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100516 *k++ = getreg(target, pos);
517 count -= sizeof(*k);
518 pos += sizeof(*k);
519 }
520 } else {
521 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800522 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100523 if (__put_user(getreg(target, pos), u++))
524 return -EFAULT;
525 count -= sizeof(*u);
526 pos += sizeof(*u);
527 }
528 }
529
530 return 0;
531}
532
533static int genregs_set(struct task_struct *target,
534 const struct user_regset *regset,
535 unsigned int pos, unsigned int count,
536 const void *kbuf, const void __user *ubuf)
537{
538 int ret = 0;
539 if (kbuf) {
540 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800541 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100542 ret = putreg(target, pos, *k++);
543 count -= sizeof(*k);
544 pos += sizeof(*k);
545 }
546 } else {
547 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800548 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100549 unsigned long word;
550 ret = __get_user(word, u++);
551 if (ret)
552 break;
553 ret = putreg(target, pos, word);
554 count -= sizeof(*u);
555 pos += sizeof(*u);
556 }
557 }
558 return ret;
559}
560
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200561static void ptrace_triggered(struct perf_event *bp,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100562 struct perf_sample_data *data,
563 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100564{
Roland McGrath0f534092008-01-30 13:30:59 +0100565 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200566 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100567
K.Prasad72f674d2009-06-01 23:45:48 +0530568 /*
569 * Store in the virtual DR6 register the fact that the breakpoint
570 * was hit so the thread's debugger will see it.
571 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200572 for (i = 0; i < HBP_NUM; i++) {
573 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530574 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100575 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100576
K.Prasad72f674d2009-06-01 23:45:48 +0530577 thread->debugreg6 |= (DR_TRAP0 << i);
578}
579
580/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200581 * Walk through every ptrace breakpoints for this thread and
582 * build the dr7 value on top of their attributes.
583 *
584 */
585static unsigned long ptrace_get_dr7(struct perf_event *bp[])
586{
587 int i;
588 int dr7 = 0;
589 struct arch_hw_breakpoint *info;
590
591 for (i = 0; i < HBP_NUM; i++) {
592 if (bp[i] && !bp[i]->attr.disabled) {
593 info = counter_arch_bp(bp[i]);
594 dr7 |= encode_dr7(i, info->len, info->type);
595 }
596 }
597
598 return dr7;
599}
600
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100601static int
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100602ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100603 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100604{
605 int err;
606 int gen_len, gen_type;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100607 struct perf_event_attr attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100608
609 /*
Adam Buchbinderc9404c92009-12-18 15:40:42 -0500610 * We should have at least an inactive breakpoint at this
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100611 * slot. It means the user is writing dr7 without having
612 * written the address register first
613 */
614 if (!bp)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100615 return -EINVAL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100616
617 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
618 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100619 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100620
621 attr = bp->attr;
622 attr.bp_len = gen_len;
623 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100624 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100625
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100626 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100627}
628
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200629/*
K.Prasad72f674d2009-06-01 23:45:48 +0530630 * Handle ptrace writes to debug register 7.
631 */
632static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
633{
634 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200635 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530636 int i, orig_ret = 0, rc = 0;
637 int enabled, second_pass = 0;
638 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200639 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530640
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200641 if (ptrace_get_breakpoints(tsk) < 0)
642 return -ESRCH;
643
K.Prasad72f674d2009-06-01 23:45:48 +0530644 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200645 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530646restore:
647 /*
648 * Loop through all the hardware breakpoints, making the
649 * appropriate changes to each.
650 */
651 for (i = 0; i < HBP_NUM; i++) {
652 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200653 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530654
655 if (!enabled) {
656 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200657 /*
658 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530659 * unless all register_user_hw_breakpoint()
660 * requests have succeeded. This prevents
661 * any window of opportunity for debug
662 * register grabbing by other users.
663 */
664 if (!second_pass)
665 continue;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100666
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100667 rc = ptrace_modify_breakpoint(bp, len, type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100668 tsk, 1);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100669 if (rc)
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100670 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530671 }
672 continue;
673 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200674
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100675 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
676 if (rc)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200677 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530678 }
679 /*
680 * Make a second pass to free the remaining unused breakpoints
681 * or to restore the original breakpoints if an error occurred.
682 */
683 if (!second_pass) {
684 second_pass = 1;
685 if (rc < 0) {
686 orig_ret = rc;
687 data = old_dr7;
688 }
689 goto restore;
690 }
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200691
692 ptrace_put_breakpoints(tsk);
693
K.Prasad72f674d2009-06-01 23:45:48 +0530694 return ((orig_ret < 0) ? orig_ret : rc);
695}
696
697/*
698 * Handle PTRACE_PEEKUSR calls for the debug register area.
699 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530700static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530701{
702 struct thread_struct *thread = &(tsk->thread);
703 unsigned long val = 0;
704
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200705 if (n < HBP_NUM) {
706 struct perf_event *bp;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200707
708 if (ptrace_get_breakpoints(tsk) < 0)
709 return -ESRCH;
710
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200711 bp = thread->ptrace_bps[n];
712 if (!bp)
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200713 val = 0;
714 else
715 val = bp->hw.info.address;
716
717 ptrace_put_breakpoints(tsk);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200718 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530719 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200720 } else if (n == 7) {
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100721 val = thread->ptrace_dr7;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200722 }
K.Prasad72f674d2009-06-01 23:45:48 +0530723 return val;
724}
725
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200726static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
727 unsigned long addr)
728{
729 struct perf_event *bp;
730 struct thread_struct *t = &tsk->thread;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100731 struct perf_event_attr attr;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200732 int err = 0;
733
734 if (ptrace_get_breakpoints(tsk) < 0)
735 return -ESRCH;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200736
737 if (!t->ptrace_bps[nr]) {
Frederic Weisbecker73266fc2010-04-22 05:05:45 +0200738 ptrace_breakpoint_init(&attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200739 /*
740 * Put stub len and type to register (reserve) an inactive but
741 * correct bp
742 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100743 attr.bp_addr = addr;
744 attr.bp_len = HW_BREAKPOINT_LEN_1;
745 attr.bp_type = HW_BREAKPOINT_W;
746 attr.disabled = 1;
747
Avi Kivity4dc0da82011-06-29 18:42:35 +0300748 bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
749 NULL, tsk);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100750
751 /*
752 * CHECKME: the previous code returned -EIO if the addr wasn't
753 * a valid task virtual addr. The new one will return -EINVAL in
754 * this case.
755 * -EINVAL may be what we want for in-kernel breakpoints users,
756 * but -EIO looks better for ptrace, since we refuse a register
757 * writing for the user. And anyway this is the previous
758 * behaviour.
759 */
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200760 if (IS_ERR(bp)) {
761 err = PTR_ERR(bp);
762 goto put;
763 }
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100764
765 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200766 } else {
767 bp = t->ptrace_bps[nr];
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100768
769 attr = bp->attr;
770 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100771 err = modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200772 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200773
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200774put:
775 ptrace_put_breakpoints(tsk);
776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
K.Prasad72f674d2009-06-01 23:45:48 +0530779/*
780 * Handle PTRACE_POKEUSR calls for the debug register area.
781 */
H Hartley Sweeten98b8b992011-11-15 14:48:58 -0800782static int ptrace_set_debugreg(struct task_struct *tsk, int n,
783 unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
K.Prasad72f674d2009-06-01 23:45:48 +0530785 struct thread_struct *thread = &(tsk->thread);
786 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
K.Prasad72f674d2009-06-01 23:45:48 +0530788 /* There are no DR4 or DR5 registers */
789 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EIO;
791
K.Prasad72f674d2009-06-01 23:45:48 +0530792 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200793 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530794 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
K.Prasad72f674d2009-06-01 23:45:48 +0530796 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200797 rc = ptrace_set_breakpoint_addr(tsk, n, val);
798 if (rc)
799 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530800 }
801 /* All that's left is DR7 */
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100802 if (n == 7) {
K.Prasad72f674d2009-06-01 23:45:48 +0530803 rc = ptrace_write_dr7(tsk, val);
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100804 if (!rc)
805 thread->ptrace_dr7 = val;
806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
K.Prasad72f674d2009-06-01 23:45:48 +0530808ret_path:
809 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Roland McGrath325af5f2008-08-08 15:58:39 -0700812/*
813 * These access the current or another (stopped) task's io permission
814 * bitmap for debugging or core dump.
815 */
816static int ioperm_active(struct task_struct *target,
817 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100818{
Roland McGrath325af5f2008-08-08 15:58:39 -0700819 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100820}
821
Roland McGrath325af5f2008-08-08 15:58:39 -0700822static int ioperm_get(struct task_struct *target,
823 const struct user_regset *regset,
824 unsigned int pos, unsigned int count,
825 void *kbuf, void __user *ubuf)
826{
827 if (!target->thread.io_bitmap_ptr)
828 return -ENXIO;
829
830 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
831 target->thread.io_bitmap_ptr,
832 0, IO_BITMAP_BYTES);
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/*
836 * Called by kernel/ptrace.c when detaching..
837 *
838 * Make sure the single step bit is not set.
839 */
840void ptrace_disable(struct task_struct *child)
841{
842 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100843#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Roland McGrath5a4646a2008-01-30 13:31:54 +0100848#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
849static const struct user_regset_view user_x86_32_view; /* Initialized below. */
850#endif
851
Namhyung Kim9b05a692010-10-27 15:33:47 -0700852long arch_ptrace(struct task_struct *child, long request,
853 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100855 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 unsigned long __user *datap = (unsigned long __user *)data;
857
858 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /* read the word at location addr in the USER area. */
860 case PTRACE_PEEKUSR: {
861 unsigned long tmp;
862
863 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700864 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866
867 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100868 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100870 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
871 addr <= offsetof(struct user, u_debugreg[7])) {
872 addr -= offsetof(struct user, u_debugreg[0]);
873 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 ret = put_user(tmp, datap);
876 break;
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
880 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700881 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
883
Roland McGrathe9c86c72008-01-30 13:31:01 +0100884 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100886 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
887 addr <= offsetof(struct user, u_debugreg[7])) {
888 addr -= offsetof(struct user, u_debugreg[0]);
889 ret = ptrace_set_debugreg(child,
890 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Roland McGrath5a4646a2008-01-30 13:31:54 +0100894 case PTRACE_GETREGS: /* Get all gp regs from the child. */
895 return copy_regset_to_user(child,
896 task_user_regset_view(current),
897 REGSET_GENERAL,
898 0, sizeof(struct user_regs_struct),
899 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Roland McGrath5a4646a2008-01-30 13:31:54 +0100901 case PTRACE_SETREGS: /* Set all gp regs in the child. */
902 return copy_regset_from_user(child,
903 task_user_regset_view(current),
904 REGSET_GENERAL,
905 0, sizeof(struct user_regs_struct),
906 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Roland McGrath5a4646a2008-01-30 13:31:54 +0100908 case PTRACE_GETFPREGS: /* Get the child FPU state. */
909 return copy_regset_to_user(child,
910 task_user_regset_view(current),
911 REGSET_FP,
912 0, sizeof(struct user_i387_struct),
913 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Roland McGrath5a4646a2008-01-30 13:31:54 +0100915 case PTRACE_SETFPREGS: /* Set the child FPU state. */
916 return copy_regset_from_user(child,
917 task_user_regset_view(current),
918 REGSET_FP,
919 0, sizeof(struct user_i387_struct),
920 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Roland McGrathe9c86c72008-01-30 13:31:01 +0100922#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100923 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
924 return copy_regset_to_user(child, &user_x86_32_view,
925 REGSET_XFP,
926 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700927 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Roland McGrath5a4646a2008-01-30 13:31:54 +0100929 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
930 return copy_regset_from_user(child, &user_x86_32_view,
931 REGSET_XFP,
932 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700933 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100934#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Roland McGrathe9c86c72008-01-30 13:31:01 +0100936#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 case PTRACE_GET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700938 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100939 return -EIO;
940 ret = do_get_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700941 (struct user_desc __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 break;
943
944 case PTRACE_SET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700945 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100946 return -EIO;
947 ret = do_set_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700948 (struct user_desc __user *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100950#endif
951
952#ifdef CONFIG_X86_64
953 /* normal 64bit interface to access TLS data.
954 Works just like arch_prctl, except that the arguments
955 are reversed. */
956 case PTRACE_ARCH_PRCTL:
957 ret = do_arch_prctl(child, data, addr);
958 break;
959#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 default:
962 ret = ptrace_request(child, request, addr, data);
963 break;
964 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return ret;
967}
968
Roland McGrathcb757c42008-01-30 13:31:01 +0100969#ifdef CONFIG_IA32_EMULATION
970
Roland McGrath099cd6e2008-01-30 13:31:01 +0100971#include <linux/compat.h>
972#include <linux/syscalls.h>
973#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +0100974#include <asm/user32.h>
975
976#define R32(l,q) \
977 case offsetof(struct user32, regs.l): \
978 regs->q = value; break
979
980#define SEG32(rs) \
981 case offsetof(struct user32, regs.rs): \
982 return set_segment_reg(child, \
983 offsetof(struct user_regs_struct, rs), \
984 value); \
985 break
986
987static int putreg32(struct task_struct *child, unsigned regno, u32 value)
988{
989 struct pt_regs *regs = task_pt_regs(child);
990
991 switch (regno) {
992
993 SEG32(cs);
994 SEG32(ds);
995 SEG32(es);
996 SEG32(fs);
997 SEG32(gs);
998 SEG32(ss);
999
1000 R32(ebx, bx);
1001 R32(ecx, cx);
1002 R32(edx, dx);
1003 R32(edi, di);
1004 R32(esi, si);
1005 R32(ebp, bp);
1006 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001007 R32(eip, ip);
1008 R32(esp, sp);
1009
Roland McGrath40f09332008-02-28 19:57:07 -08001010 case offsetof(struct user32, regs.orig_eax):
1011 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001012 * A 32-bit debugger setting orig_eax means to restore
1013 * the state of the task restarting a 32-bit syscall.
1014 * Make sure we interpret the -ERESTART* codes correctly
1015 * in case the task is not actually still sitting at the
1016 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001017 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001018 regs->orig_ax = value;
1019 if (syscall_get_nr(child, regs) >= 0)
1020 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001021 break;
1022
Roland McGrathcb757c42008-01-30 13:31:01 +01001023 case offsetof(struct user32, regs.eflags):
1024 return set_flags(child, value);
1025
1026 case offsetof(struct user32, u_debugreg[0]) ...
1027 offsetof(struct user32, u_debugreg[7]):
1028 regno -= offsetof(struct user32, u_debugreg[0]);
1029 return ptrace_set_debugreg(child, regno / 4, value);
1030
1031 default:
1032 if (regno > sizeof(struct user32) || (regno & 3))
1033 return -EIO;
1034
1035 /*
1036 * Other dummy fields in the virtual user structure
1037 * are ignored
1038 */
1039 break;
1040 }
1041 return 0;
1042}
1043
1044#undef R32
1045#undef SEG32
1046
1047#define R32(l,q) \
1048 case offsetof(struct user32, regs.l): \
1049 *val = regs->q; break
1050
1051#define SEG32(rs) \
1052 case offsetof(struct user32, regs.rs): \
1053 *val = get_segment_reg(child, \
1054 offsetof(struct user_regs_struct, rs)); \
1055 break
1056
1057static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1058{
1059 struct pt_regs *regs = task_pt_regs(child);
1060
1061 switch (regno) {
1062
1063 SEG32(ds);
1064 SEG32(es);
1065 SEG32(fs);
1066 SEG32(gs);
1067
1068 R32(cs, cs);
1069 R32(ss, ss);
1070 R32(ebx, bx);
1071 R32(ecx, cx);
1072 R32(edx, dx);
1073 R32(edi, di);
1074 R32(esi, si);
1075 R32(ebp, bp);
1076 R32(eax, ax);
1077 R32(orig_eax, orig_ax);
1078 R32(eip, ip);
1079 R32(esp, sp);
1080
1081 case offsetof(struct user32, regs.eflags):
1082 *val = get_flags(child);
1083 break;
1084
1085 case offsetof(struct user32, u_debugreg[0]) ...
1086 offsetof(struct user32, u_debugreg[7]):
1087 regno -= offsetof(struct user32, u_debugreg[0]);
1088 *val = ptrace_get_debugreg(child, regno / 4);
1089 break;
1090
1091 default:
1092 if (regno > sizeof(struct user32) || (regno & 3))
1093 return -EIO;
1094
1095 /*
1096 * Other dummy fields in the virtual user structure
1097 * are ignored
1098 */
1099 *val = 0;
1100 break;
1101 }
1102 return 0;
1103}
1104
1105#undef R32
1106#undef SEG32
1107
Roland McGrath91e7b702008-01-30 13:31:52 +01001108static int genregs32_get(struct task_struct *target,
1109 const struct user_regset *regset,
1110 unsigned int pos, unsigned int count,
1111 void *kbuf, void __user *ubuf)
1112{
1113 if (kbuf) {
1114 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001115 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001116 getreg32(target, pos, k++);
1117 count -= sizeof(*k);
1118 pos += sizeof(*k);
1119 }
1120 } else {
1121 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001122 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001123 compat_ulong_t word;
1124 getreg32(target, pos, &word);
1125 if (__put_user(word, u++))
1126 return -EFAULT;
1127 count -= sizeof(*u);
1128 pos += sizeof(*u);
1129 }
1130 }
1131
1132 return 0;
1133}
1134
1135static int genregs32_set(struct task_struct *target,
1136 const struct user_regset *regset,
1137 unsigned int pos, unsigned int count,
1138 const void *kbuf, const void __user *ubuf)
1139{
1140 int ret = 0;
1141 if (kbuf) {
1142 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001143 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001144 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001145 count -= sizeof(*k);
1146 pos += sizeof(*k);
1147 }
1148 } else {
1149 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001150 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001151 compat_ulong_t word;
1152 ret = __get_user(word, u++);
1153 if (ret)
1154 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001155 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001156 count -= sizeof(*u);
1157 pos += sizeof(*u);
1158 }
1159 }
1160 return ret;
1161}
1162
H.J. Lu55283e22012-03-05 15:32:11 -08001163#ifdef CONFIG_X86_X32_ABI
1164static long x32_arch_ptrace(struct task_struct *child,
1165 compat_long_t request, compat_ulong_t caddr,
1166 compat_ulong_t cdata)
1167{
1168 unsigned long addr = caddr;
1169 unsigned long data = cdata;
1170 void __user *datap = compat_ptr(data);
1171 int ret;
1172
1173 switch (request) {
1174 /* Read 32bits at location addr in the USER area. Only allow
1175 to return the lower 32bits of segment and debug registers. */
1176 case PTRACE_PEEKUSR: {
1177 u32 tmp;
1178
1179 ret = -EIO;
1180 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1181 addr < offsetof(struct user_regs_struct, cs))
1182 break;
1183
1184 tmp = 0; /* Default return condition */
1185 if (addr < sizeof(struct user_regs_struct))
1186 tmp = getreg(child, addr);
1187 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1188 addr <= offsetof(struct user, u_debugreg[7])) {
1189 addr -= offsetof(struct user, u_debugreg[0]);
1190 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1191 }
1192 ret = put_user(tmp, (__u32 __user *)datap);
1193 break;
1194 }
1195
1196 /* Write the word at location addr in the USER area. Only allow
1197 to update segment and debug registers with the upper 32bits
1198 zero-extended. */
1199 case PTRACE_POKEUSR:
1200 ret = -EIO;
1201 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1202 addr < offsetof(struct user_regs_struct, cs))
1203 break;
1204
1205 if (addr < sizeof(struct user_regs_struct))
1206 ret = putreg(child, addr, data);
1207 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1208 addr <= offsetof(struct user, u_debugreg[7])) {
1209 addr -= offsetof(struct user, u_debugreg[0]);
1210 ret = ptrace_set_debugreg(child,
1211 addr / sizeof(data), data);
1212 }
1213 break;
1214
1215 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1216 return copy_regset_to_user(child,
1217 task_user_regset_view(current),
1218 REGSET_GENERAL,
1219 0, sizeof(struct user_regs_struct),
1220 datap);
1221
1222 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1223 return copy_regset_from_user(child,
1224 task_user_regset_view(current),
1225 REGSET_GENERAL,
1226 0, sizeof(struct user_regs_struct),
1227 datap);
1228
1229 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1230 return copy_regset_to_user(child,
1231 task_user_regset_view(current),
1232 REGSET_FP,
1233 0, sizeof(struct user_i387_struct),
1234 datap);
1235
1236 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1237 return copy_regset_from_user(child,
1238 task_user_regset_view(current),
1239 REGSET_FP,
1240 0, sizeof(struct user_i387_struct),
1241 datap);
1242
H.J. Lu55283e22012-03-05 15:32:11 -08001243 default:
1244 return compat_ptrace_request(child, request, addr, data);
1245 }
1246
1247 return ret;
1248}
1249#endif
1250
Roland McGrath562b80b2008-04-22 12:21:25 -07001251long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1252 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001253{
Roland McGrath562b80b2008-04-22 12:21:25 -07001254 unsigned long addr = caddr;
1255 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001256 void __user *datap = compat_ptr(data);
1257 int ret;
1258 __u32 val;
1259
H.J. Lu55283e22012-03-05 15:32:11 -08001260#ifdef CONFIG_X86_X32_ABI
1261 if (!is_ia32_task())
1262 return x32_arch_ptrace(child, request, caddr, cdata);
1263#endif
1264
Roland McGrath099cd6e2008-01-30 13:31:01 +01001265 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001266 case PTRACE_PEEKUSR:
1267 ret = getreg32(child, addr, &val);
1268 if (ret == 0)
1269 ret = put_user(val, (__u32 __user *)datap);
1270 break;
1271
1272 case PTRACE_POKEUSR:
1273 ret = putreg32(child, addr, data);
1274 break;
1275
Roland McGrath5a4646a2008-01-30 13:31:54 +01001276 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1277 return copy_regset_to_user(child, &user_x86_32_view,
1278 REGSET_GENERAL,
1279 0, sizeof(struct user_regs_struct32),
1280 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001281
Roland McGrath5a4646a2008-01-30 13:31:54 +01001282 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1283 return copy_regset_from_user(child, &user_x86_32_view,
1284 REGSET_GENERAL, 0,
1285 sizeof(struct user_regs_struct32),
1286 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001287
Roland McGrath5a4646a2008-01-30 13:31:54 +01001288 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1289 return copy_regset_to_user(child, &user_x86_32_view,
1290 REGSET_FP, 0,
1291 sizeof(struct user_i387_ia32_struct),
1292 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001293
Roland McGrath5a4646a2008-01-30 13:31:54 +01001294 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1295 return copy_regset_from_user(
1296 child, &user_x86_32_view, REGSET_FP,
1297 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001298
Roland McGrath5a4646a2008-01-30 13:31:54 +01001299 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1300 return copy_regset_to_user(child, &user_x86_32_view,
1301 REGSET_XFP, 0,
1302 sizeof(struct user32_fxsr_struct),
1303 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001304
Roland McGrath5a4646a2008-01-30 13:31:54 +01001305 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1306 return copy_regset_from_user(child, &user_x86_32_view,
1307 REGSET_XFP, 0,
1308 sizeof(struct user32_fxsr_struct),
1309 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001310
Roland McGrath562b80b2008-04-22 12:21:25 -07001311 case PTRACE_GET_THREAD_AREA:
1312 case PTRACE_SET_THREAD_AREA:
1313 return arch_ptrace(child, request, addr, data);
1314
Roland McGrath099cd6e2008-01-30 13:31:01 +01001315 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001316 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001317 }
1318
Roland McGrath099cd6e2008-01-30 13:31:01 +01001319 return ret;
1320}
1321
Roland McGrathcb757c42008-01-30 13:31:01 +01001322#endif /* CONFIG_IA32_EMULATION */
1323
Roland McGrath070459d2008-01-30 13:31:53 +01001324#ifdef CONFIG_X86_64
1325
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001326static struct user_regset x86_64_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001327 [REGSET_GENERAL] = {
1328 .core_note_type = NT_PRSTATUS,
1329 .n = sizeof(struct user_regs_struct) / sizeof(long),
1330 .size = sizeof(long), .align = sizeof(long),
1331 .get = genregs_get, .set = genregs_set
1332 },
1333 [REGSET_FP] = {
1334 .core_note_type = NT_PRFPREG,
1335 .n = sizeof(struct user_i387_struct) / sizeof(long),
1336 .size = sizeof(long), .align = sizeof(long),
1337 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1338 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001339 [REGSET_XSTATE] = {
1340 .core_note_type = NT_X86_XSTATE,
1341 .size = sizeof(u64), .align = sizeof(u64),
1342 .active = xstateregs_active, .get = xstateregs_get,
1343 .set = xstateregs_set
1344 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001345 [REGSET_IOPERM64] = {
1346 .core_note_type = NT_386_IOPERM,
1347 .n = IO_BITMAP_LONGS,
1348 .size = sizeof(long), .align = sizeof(long),
1349 .active = ioperm_active, .get = ioperm_get
1350 },
Roland McGrath070459d2008-01-30 13:31:53 +01001351};
1352
1353static const struct user_regset_view user_x86_64_view = {
1354 .name = "x86_64", .e_machine = EM_X86_64,
1355 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1356};
1357
1358#else /* CONFIG_X86_32 */
1359
1360#define user_regs_struct32 user_regs_struct
1361#define genregs32_get genregs_get
1362#define genregs32_set genregs_set
1363
1364#endif /* CONFIG_X86_64 */
1365
1366#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001367static struct user_regset x86_32_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001368 [REGSET_GENERAL] = {
1369 .core_note_type = NT_PRSTATUS,
1370 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1371 .size = sizeof(u32), .align = sizeof(u32),
1372 .get = genregs32_get, .set = genregs32_set
1373 },
1374 [REGSET_FP] = {
1375 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001376 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001377 .size = sizeof(u32), .align = sizeof(u32),
1378 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1379 },
1380 [REGSET_XFP] = {
1381 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001382 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001383 .size = sizeof(u32), .align = sizeof(u32),
1384 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1385 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001386 [REGSET_XSTATE] = {
1387 .core_note_type = NT_X86_XSTATE,
1388 .size = sizeof(u64), .align = sizeof(u64),
1389 .active = xstateregs_active, .get = xstateregs_get,
1390 .set = xstateregs_set
1391 },
Roland McGrath070459d2008-01-30 13:31:53 +01001392 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001393 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001394 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1395 .size = sizeof(struct user_desc),
1396 .align = sizeof(struct user_desc),
1397 .active = regset_tls_active,
1398 .get = regset_tls_get, .set = regset_tls_set
1399 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001400 [REGSET_IOPERM32] = {
1401 .core_note_type = NT_386_IOPERM,
1402 .n = IO_BITMAP_BYTES / sizeof(u32),
1403 .size = sizeof(u32), .align = sizeof(u32),
1404 .active = ioperm_active, .get = ioperm_get
1405 },
Roland McGrath070459d2008-01-30 13:31:53 +01001406};
1407
1408static const struct user_regset_view user_x86_32_view = {
1409 .name = "i386", .e_machine = EM_386,
1410 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1411};
1412#endif
1413
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001414/*
1415 * This represents bytes 464..511 in the memory layout exported through
1416 * the REGSET_XSTATE interface.
1417 */
1418u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1419
1420void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1421{
1422#ifdef CONFIG_X86_64
1423 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1424#endif
1425#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1426 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1427#endif
1428 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1429}
1430
Roland McGrath070459d2008-01-30 13:31:53 +01001431const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1432{
1433#ifdef CONFIG_IA32_EMULATION
1434 if (test_tsk_thread_flag(task, TIF_IA32))
1435#endif
1436#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1437 return &user_x86_32_view;
1438#endif
1439#ifdef CONFIG_X86_64
1440 return &user_x86_64_view;
1441#endif
1442}
1443
Oleg Nesterov7f385512009-12-15 16:47:20 -08001444static void fill_sigtrap_info(struct task_struct *tsk,
1445 struct pt_regs *regs,
1446 int error_code, int si_code,
1447 struct siginfo *info)
1448{
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +05301449 tsk->thread.trap_nr = X86_TRAP_DB;
Oleg Nesterov7f385512009-12-15 16:47:20 -08001450 tsk->thread.error_code = error_code;
1451
1452 memset(info, 0, sizeof(*info));
1453 info->si_signo = SIGTRAP;
1454 info->si_code = si_code;
1455 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1456}
1457
1458void user_single_step_siginfo(struct task_struct *tsk,
1459 struct pt_regs *regs,
1460 struct siginfo *info)
1461{
1462 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1463}
1464
Srinivasa Dsda654b72008-09-23 15:23:52 +05301465void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1466 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct siginfo info;
1469
Oleg Nesterov7f385512009-12-15 16:47:20 -08001470 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
Simon Arlott27b46d72007-10-20 01:13:56 +02001471 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 force_sig_info(SIGTRAP, &info, tsk);
1473}
1474
Roland McGrath86976cd2008-01-30 13:31:01 +01001475
Roland McGrathd4d67152008-07-09 02:38:07 -07001476#ifdef CONFIG_X86_32
1477# define IS_IA32 1
1478#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001479# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001480#else
1481# define IS_IA32 0
1482#endif
1483
1484/*
1485 * We must return the syscall number to actually look up in the table.
1486 * This can be -1L to skip running any syscall at all.
1487 */
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001488long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001489{
Roland McGrathd4d67152008-07-09 02:38:07 -07001490 long ret = 0;
1491
Frederic Weisbeckerbf5a3c12012-07-11 20:26:34 +02001492 rcu_user_exit();
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 */
Will Drewryc6cfbeb2012-04-12 16:48:03 -05001505 if (secure_computing(regs->orig_ax)) {
1506 /* seccomp failures shouldn't expose any additional code. */
1507 ret = -1L;
1508 goto out;
1509 }
Roland McGrath86976cd2008-01-30 13:31:01 +01001510
Roland McGrathd4d67152008-07-09 02:38:07 -07001511 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1512 ret = -1L;
1513
Roland McGratheeea3c32008-03-16 23:36:28 -07001514 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1515 tracehook_report_syscall_entry(regs))
1516 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001517
Josh Stone66700002009-08-24 14:43:11 -07001518 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001519 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001520
Eric Parisb05d8442012-01-03 14:23:06 -05001521 if (IS_IA32)
1522 audit_syscall_entry(AUDIT_ARCH_I386,
1523 regs->orig_ax,
1524 regs->bx, regs->cx,
1525 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001526#ifdef CONFIG_X86_64
Eric Parisb05d8442012-01-03 14:23:06 -05001527 else
1528 audit_syscall_entry(AUDIT_ARCH_X86_64,
1529 regs->orig_ax,
1530 regs->di, regs->si,
1531 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001532#endif
Roland McGrathd4d67152008-07-09 02:38:07 -07001533
Will Drewryc6cfbeb2012-04-12 16:48:03 -05001534out:
Roland McGrathd4d67152008-07-09 02:38:07 -07001535 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001536}
1537
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001538void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001539{
Oleg Nesterovd5196502009-12-15 16:47:21 -08001540 bool step;
1541
Eric Parisd7e75282012-01-03 14:23:06 -05001542 audit_syscall_exit(regs);
Roland McGrath86976cd2008-01-30 13:31:01 +01001543
Josh Stone66700002009-08-24 14:43:11 -07001544 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001545 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001546
Roland McGrathd4d67152008-07-09 02:38:07 -07001547 /*
1548 * If TIF_SYSCALL_EMU is set, we only get here because of
1549 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1550 * We already reported this syscall instruction in
Oleg Nesterovd5196502009-12-15 16:47:21 -08001551 * syscall_trace_enter().
Roland McGrathd4d67152008-07-09 02:38:07 -07001552 */
Oleg Nesterovd5196502009-12-15 16:47:21 -08001553 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1554 !test_thread_flag(TIF_SYSCALL_EMU);
1555 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1556 tracehook_report_syscall_exit(regs, step);
Frederic Weisbeckerbf5a3c12012-07-11 20:26:34 +02001557
1558 rcu_user_enter();
Roland McGrathd4d67152008-07-09 02:38:07 -07001559}