blob: c4410fb4938fc33a62b38e0929fa5a132acf994e [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>
H. Peter Anvin4d064b92012-11-20 22:21:02 -080024#include <linux/module.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 Richter9d4f9622012-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}
H. Peter Anvin4d064b92012-11-20 22:21:02 -0800196EXPORT_SYMBOL_GPL(kernel_stack_pointer);
Robert Richter9d4f9622012-09-03 20:54:48 +0200197
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530198static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100200 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900201 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Roland McGrath06ee1b62008-01-30 13:31:01 +0100204static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100206 /*
207 * Returning the value truncates it to 16 bits.
208 */
209 unsigned int retval;
210 if (offset != offsetof(struct user_regs_struct, gs))
211 retval = *pt_regs_access(task_pt_regs(task), offset);
212 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100213 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900214 retval = get_user_gs(task_pt_regs(task));
215 else
216 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100217 }
218 return retval;
219}
220
221static int set_segment_reg(struct task_struct *task,
222 unsigned long offset, u16 value)
223{
224 /*
225 * The value argument was already truncated to 16 bits.
226 */
Roland McGrath2047b082008-01-30 13:31:01 +0100227 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100228 return -EIO;
229
Roland McGrathc63855d2008-02-06 22:39:44 +0100230 /*
231 * For %cs and %ss we cannot permit a null selector.
232 * We can permit a bogus selector as long as it has USER_RPL.
233 * Null selectors are fine for other segment registers, but
234 * we will never get back to user mode with invalid %cs or %ss
235 * and will take the trap in iret instead. Much code relies
236 * on user_mode() to distinguish a user trap frame (which can
237 * safely use invalid selectors) from a kernel trap frame.
238 */
239 switch (offset) {
240 case offsetof(struct user_regs_struct, cs):
241 case offsetof(struct user_regs_struct, ss):
242 if (unlikely(value == 0))
243 return -EIO;
244
245 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100246 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100247 break;
248
249 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100250 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900251 set_user_gs(task_pt_regs(task), value);
252 else
253 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257}
258
Roland McGrath2047b082008-01-30 13:31:01 +0100259#else /* CONFIG_X86_64 */
260
261#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
262
263static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
264{
265 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
266 return &regs->r15 + (offset / sizeof(regs->r15));
267}
268
269static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
270{
271 /*
272 * Returning the value truncates it to 16 bits.
273 */
274 unsigned int seg;
275
276 switch (offset) {
277 case offsetof(struct user_regs_struct, fs):
278 if (task == current) {
279 /* Older gas can't assemble movq %?s,%r?? */
280 asm("movl %%fs,%0" : "=r" (seg));
281 return seg;
282 }
283 return task->thread.fsindex;
284 case offsetof(struct user_regs_struct, gs):
285 if (task == current) {
286 asm("movl %%gs,%0" : "=r" (seg));
287 return seg;
288 }
289 return task->thread.gsindex;
290 case offsetof(struct user_regs_struct, ds):
291 if (task == current) {
292 asm("movl %%ds,%0" : "=r" (seg));
293 return seg;
294 }
295 return task->thread.ds;
296 case offsetof(struct user_regs_struct, es):
297 if (task == current) {
298 asm("movl %%es,%0" : "=r" (seg));
299 return seg;
300 }
301 return task->thread.es;
302
303 case offsetof(struct user_regs_struct, cs):
304 case offsetof(struct user_regs_struct, ss):
305 break;
306 }
307 return *pt_regs_access(task_pt_regs(task), offset);
308}
309
310static int set_segment_reg(struct task_struct *task,
311 unsigned long offset, u16 value)
312{
313 /*
314 * The value argument was already truncated to 16 bits.
315 */
316 if (invalid_selector(value))
317 return -EIO;
318
319 switch (offset) {
320 case offsetof(struct user_regs_struct,fs):
321 /*
322 * If this is setting fs as for normal 64-bit use but
323 * setting fs_base has implicitly changed it, leave it.
324 */
325 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
326 task->thread.fs != 0) ||
327 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
328 task->thread.fs == 0))
329 break;
330 task->thread.fsindex = value;
331 if (task == current)
332 loadsegment(fs, task->thread.fsindex);
333 break;
334 case offsetof(struct user_regs_struct,gs):
335 /*
336 * If this is setting gs as for normal 64-bit use but
337 * setting gs_base has implicitly changed it, leave it.
338 */
339 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
340 task->thread.gs != 0) ||
341 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
342 task->thread.gs == 0))
343 break;
344 task->thread.gsindex = value;
345 if (task == current)
346 load_gs_index(task->thread.gsindex);
347 break;
348 case offsetof(struct user_regs_struct,ds):
349 task->thread.ds = value;
350 if (task == current)
351 loadsegment(ds, task->thread.ds);
352 break;
353 case offsetof(struct user_regs_struct,es):
354 task->thread.es = value;
355 if (task == current)
356 loadsegment(es, task->thread.es);
357 break;
358
359 /*
360 * Can't actually change these in 64-bit mode.
361 */
362 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100363 if (unlikely(value == 0))
364 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100365#ifdef CONFIG_IA32_EMULATION
366 if (test_tsk_thread_flag(task, TIF_IA32))
367 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100368#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100369 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100370 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100371 if (unlikely(value == 0))
372 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100373#ifdef CONFIG_IA32_EMULATION
374 if (test_tsk_thread_flag(task, TIF_IA32))
375 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100376#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100377 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100378 }
379
380 return 0;
381}
382
Roland McGrath2047b082008-01-30 13:31:01 +0100383#endif /* CONFIG_X86_32 */
384
Roland McGrath06ee1b62008-01-30 13:31:01 +0100385static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100387 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Roland McGrath06ee1b62008-01-30 13:31:01 +0100389 /*
390 * If the debugger set TF, hide it from the readout.
391 */
392 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
393 retval &= ~X86_EFLAGS_TF;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return retval;
396}
397
Roland McGrath06ee1b62008-01-30 13:31:01 +0100398static int set_flags(struct task_struct *task, unsigned long value)
399{
400 struct pt_regs *regs = task_pt_regs(task);
401
402 /*
403 * If the user value contains TF, mark that
404 * it was not "us" (the debugger) that set it.
405 * If not, make sure it stays set if we had.
406 */
407 if (value & X86_EFLAGS_TF)
408 clear_tsk_thread_flag(task, TIF_FORCED_TF);
409 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
410 value |= X86_EFLAGS_TF;
411
412 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
413
414 return 0;
415}
416
417static int putreg(struct task_struct *child,
418 unsigned long offset, unsigned long value)
419{
420 switch (offset) {
421 case offsetof(struct user_regs_struct, cs):
422 case offsetof(struct user_regs_struct, ds):
423 case offsetof(struct user_regs_struct, es):
424 case offsetof(struct user_regs_struct, fs):
425 case offsetof(struct user_regs_struct, gs):
426 case offsetof(struct user_regs_struct, ss):
427 return set_segment_reg(child, offset, value);
428
429 case offsetof(struct user_regs_struct, flags):
430 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100431
432#ifdef CONFIG_X86_64
433 case offsetof(struct user_regs_struct,fs_base):
434 if (value >= TASK_SIZE_OF(child))
435 return -EIO;
436 /*
437 * When changing the segment base, use do_arch_prctl
438 * to set either thread.fs or thread.fsindex and the
439 * corresponding GDT slot.
440 */
441 if (child->thread.fs != value)
442 return do_arch_prctl(child, ARCH_SET_FS, value);
443 return 0;
444 case offsetof(struct user_regs_struct,gs_base):
445 /*
446 * Exactly the same here as the %fs handling above.
447 */
448 if (value >= TASK_SIZE_OF(child))
449 return -EIO;
450 if (child->thread.gs != value)
451 return do_arch_prctl(child, ARCH_SET_GS, value);
452 return 0;
453#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100454 }
455
456 *pt_regs_access(task_pt_regs(child), offset) = value;
457 return 0;
458}
459
460static unsigned long getreg(struct task_struct *task, unsigned long offset)
461{
462 switch (offset) {
463 case offsetof(struct user_regs_struct, cs):
464 case offsetof(struct user_regs_struct, ds):
465 case offsetof(struct user_regs_struct, es):
466 case offsetof(struct user_regs_struct, fs):
467 case offsetof(struct user_regs_struct, gs):
468 case offsetof(struct user_regs_struct, ss):
469 return get_segment_reg(task, offset);
470
471 case offsetof(struct user_regs_struct, flags):
472 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100473
474#ifdef CONFIG_X86_64
475 case offsetof(struct user_regs_struct, fs_base): {
476 /*
477 * do_arch_prctl may have used a GDT slot instead of
478 * the MSR. To userland, it appears the same either
479 * way, except the %fs segment selector might not be 0.
480 */
481 unsigned int seg = task->thread.fsindex;
482 if (task->thread.fs != 0)
483 return task->thread.fs;
484 if (task == current)
485 asm("movl %%fs,%0" : "=r" (seg));
486 if (seg != FS_TLS_SEL)
487 return 0;
488 return get_desc_base(&task->thread.tls_array[FS_TLS]);
489 }
490 case offsetof(struct user_regs_struct, gs_base): {
491 /*
492 * Exactly the same here as the %fs handling above.
493 */
494 unsigned int seg = task->thread.gsindex;
495 if (task->thread.gs != 0)
496 return task->thread.gs;
497 if (task == current)
498 asm("movl %%gs,%0" : "=r" (seg));
499 if (seg != GS_TLS_SEL)
500 return 0;
501 return get_desc_base(&task->thread.tls_array[GS_TLS]);
502 }
503#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100504 }
505
506 return *pt_regs_access(task_pt_regs(task), offset);
507}
508
Roland McGrath91e7b702008-01-30 13:31:52 +0100509static int genregs_get(struct task_struct *target,
510 const struct user_regset *regset,
511 unsigned int pos, unsigned int count,
512 void *kbuf, void __user *ubuf)
513{
514 if (kbuf) {
515 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800516 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100517 *k++ = getreg(target, pos);
518 count -= sizeof(*k);
519 pos += sizeof(*k);
520 }
521 } else {
522 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800523 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100524 if (__put_user(getreg(target, pos), u++))
525 return -EFAULT;
526 count -= sizeof(*u);
527 pos += sizeof(*u);
528 }
529 }
530
531 return 0;
532}
533
534static int genregs_set(struct task_struct *target,
535 const struct user_regset *regset,
536 unsigned int pos, unsigned int count,
537 const void *kbuf, const void __user *ubuf)
538{
539 int ret = 0;
540 if (kbuf) {
541 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800542 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100543 ret = putreg(target, pos, *k++);
544 count -= sizeof(*k);
545 pos += sizeof(*k);
546 }
547 } else {
548 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800549 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100550 unsigned long word;
551 ret = __get_user(word, u++);
552 if (ret)
553 break;
554 ret = putreg(target, pos, word);
555 count -= sizeof(*u);
556 pos += sizeof(*u);
557 }
558 }
559 return ret;
560}
561
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200562static void ptrace_triggered(struct perf_event *bp,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100563 struct perf_sample_data *data,
564 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100565{
Roland McGrath0f534092008-01-30 13:30:59 +0100566 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200567 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100568
K.Prasad72f674d2009-06-01 23:45:48 +0530569 /*
570 * Store in the virtual DR6 register the fact that the breakpoint
571 * was hit so the thread's debugger will see it.
572 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200573 for (i = 0; i < HBP_NUM; i++) {
574 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530575 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100576 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100577
K.Prasad72f674d2009-06-01 23:45:48 +0530578 thread->debugreg6 |= (DR_TRAP0 << i);
579}
580
581/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200582 * Walk through every ptrace breakpoints for this thread and
583 * build the dr7 value on top of their attributes.
584 *
585 */
586static unsigned long ptrace_get_dr7(struct perf_event *bp[])
587{
588 int i;
589 int dr7 = 0;
590 struct arch_hw_breakpoint *info;
591
592 for (i = 0; i < HBP_NUM; i++) {
593 if (bp[i] && !bp[i]->attr.disabled) {
594 info = counter_arch_bp(bp[i]);
595 dr7 |= encode_dr7(i, info->len, info->type);
596 }
597 }
598
599 return dr7;
600}
601
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100602static int
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100603ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100604 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100605{
606 int err;
607 int gen_len, gen_type;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100608 struct perf_event_attr attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100609
610 /*
Adam Buchbinderc9404c92009-12-18 15:40:42 -0500611 * We should have at least an inactive breakpoint at this
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100612 * slot. It means the user is writing dr7 without having
613 * written the address register first
614 */
615 if (!bp)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100616 return -EINVAL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100617
618 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
619 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100620 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100621
622 attr = bp->attr;
623 attr.bp_len = gen_len;
624 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100625 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100626
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100627 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100628}
629
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200630/*
K.Prasad72f674d2009-06-01 23:45:48 +0530631 * Handle ptrace writes to debug register 7.
632 */
633static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
634{
635 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200636 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530637 int i, orig_ret = 0, rc = 0;
638 int enabled, second_pass = 0;
639 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200640 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530641
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200642 if (ptrace_get_breakpoints(tsk) < 0)
643 return -ESRCH;
644
K.Prasad72f674d2009-06-01 23:45:48 +0530645 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200646 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530647restore:
648 /*
649 * Loop through all the hardware breakpoints, making the
650 * appropriate changes to each.
651 */
652 for (i = 0; i < HBP_NUM; i++) {
653 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200654 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530655
656 if (!enabled) {
657 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200658 /*
659 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530660 * unless all register_user_hw_breakpoint()
661 * requests have succeeded. This prevents
662 * any window of opportunity for debug
663 * register grabbing by other users.
664 */
665 if (!second_pass)
666 continue;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100667
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100668 rc = ptrace_modify_breakpoint(bp, len, type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100669 tsk, 1);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100670 if (rc)
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100671 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530672 }
673 continue;
674 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200675
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100676 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
677 if (rc)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200678 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530679 }
680 /*
681 * Make a second pass to free the remaining unused breakpoints
682 * or to restore the original breakpoints if an error occurred.
683 */
684 if (!second_pass) {
685 second_pass = 1;
686 if (rc < 0) {
687 orig_ret = rc;
688 data = old_dr7;
689 }
690 goto restore;
691 }
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200692
693 ptrace_put_breakpoints(tsk);
694
K.Prasad72f674d2009-06-01 23:45:48 +0530695 return ((orig_ret < 0) ? orig_ret : rc);
696}
697
698/*
699 * Handle PTRACE_PEEKUSR calls for the debug register area.
700 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530701static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530702{
703 struct thread_struct *thread = &(tsk->thread);
704 unsigned long val = 0;
705
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200706 if (n < HBP_NUM) {
707 struct perf_event *bp;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200708
709 if (ptrace_get_breakpoints(tsk) < 0)
710 return -ESRCH;
711
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200712 bp = thread->ptrace_bps[n];
713 if (!bp)
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200714 val = 0;
715 else
716 val = bp->hw.info.address;
717
718 ptrace_put_breakpoints(tsk);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200719 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530720 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200721 } else if (n == 7) {
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100722 val = thread->ptrace_dr7;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200723 }
K.Prasad72f674d2009-06-01 23:45:48 +0530724 return val;
725}
726
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200727static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
728 unsigned long addr)
729{
730 struct perf_event *bp;
731 struct thread_struct *t = &tsk->thread;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100732 struct perf_event_attr attr;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200733 int err = 0;
734
735 if (ptrace_get_breakpoints(tsk) < 0)
736 return -ESRCH;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200737
738 if (!t->ptrace_bps[nr]) {
Frederic Weisbecker73266fc2010-04-22 05:05:45 +0200739 ptrace_breakpoint_init(&attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200740 /*
741 * Put stub len and type to register (reserve) an inactive but
742 * correct bp
743 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100744 attr.bp_addr = addr;
745 attr.bp_len = HW_BREAKPOINT_LEN_1;
746 attr.bp_type = HW_BREAKPOINT_W;
747 attr.disabled = 1;
748
Avi Kivity4dc0da82011-06-29 18:42:35 +0300749 bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
750 NULL, tsk);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100751
752 /*
753 * CHECKME: the previous code returned -EIO if the addr wasn't
754 * a valid task virtual addr. The new one will return -EINVAL in
755 * this case.
756 * -EINVAL may be what we want for in-kernel breakpoints users,
757 * but -EIO looks better for ptrace, since we refuse a register
758 * writing for the user. And anyway this is the previous
759 * behaviour.
760 */
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200761 if (IS_ERR(bp)) {
762 err = PTR_ERR(bp);
763 goto put;
764 }
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100765
766 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200767 } else {
768 bp = t->ptrace_bps[nr];
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100769
770 attr = bp->attr;
771 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100772 err = modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200773 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200774
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200775put:
776 ptrace_put_breakpoints(tsk);
777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
K.Prasad72f674d2009-06-01 23:45:48 +0530780/*
781 * Handle PTRACE_POKEUSR calls for the debug register area.
782 */
H Hartley Sweeten98b8b992011-11-15 14:48:58 -0800783static int ptrace_set_debugreg(struct task_struct *tsk, int n,
784 unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
K.Prasad72f674d2009-06-01 23:45:48 +0530786 struct thread_struct *thread = &(tsk->thread);
787 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
K.Prasad72f674d2009-06-01 23:45:48 +0530789 /* There are no DR4 or DR5 registers */
790 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return -EIO;
792
K.Prasad72f674d2009-06-01 23:45:48 +0530793 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200794 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530795 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
K.Prasad72f674d2009-06-01 23:45:48 +0530797 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200798 rc = ptrace_set_breakpoint_addr(tsk, n, val);
799 if (rc)
800 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530801 }
802 /* All that's left is DR7 */
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100803 if (n == 7) {
K.Prasad72f674d2009-06-01 23:45:48 +0530804 rc = ptrace_write_dr7(tsk, val);
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100805 if (!rc)
806 thread->ptrace_dr7 = val;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
K.Prasad72f674d2009-06-01 23:45:48 +0530809ret_path:
810 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Roland McGrath325af5f2008-08-08 15:58:39 -0700813/*
814 * These access the current or another (stopped) task's io permission
815 * bitmap for debugging or core dump.
816 */
817static int ioperm_active(struct task_struct *target,
818 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100819{
Roland McGrath325af5f2008-08-08 15:58:39 -0700820 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100821}
822
Roland McGrath325af5f2008-08-08 15:58:39 -0700823static int ioperm_get(struct task_struct *target,
824 const struct user_regset *regset,
825 unsigned int pos, unsigned int count,
826 void *kbuf, void __user *ubuf)
827{
828 if (!target->thread.io_bitmap_ptr)
829 return -ENXIO;
830
831 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
832 target->thread.io_bitmap_ptr,
833 0, IO_BITMAP_BYTES);
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836/*
837 * Called by kernel/ptrace.c when detaching..
838 *
839 * Make sure the single step bit is not set.
840 */
841void ptrace_disable(struct task_struct *child)
842{
843 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100844#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Roland McGrath5a4646a2008-01-30 13:31:54 +0100849#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
850static const struct user_regset_view user_x86_32_view; /* Initialized below. */
851#endif
852
Namhyung Kim9b05a692010-10-27 15:33:47 -0700853long arch_ptrace(struct task_struct *child, long request,
854 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100856 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 unsigned long __user *datap = (unsigned long __user *)data;
858
859 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* read the word at location addr in the USER area. */
861 case PTRACE_PEEKUSR: {
862 unsigned long tmp;
863
864 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700865 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
867
868 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100869 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100871 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
872 addr <= offsetof(struct user, u_debugreg[7])) {
873 addr -= offsetof(struct user, u_debugreg[0]);
874 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 ret = put_user(tmp, datap);
877 break;
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
881 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700882 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884
Roland McGrathe9c86c72008-01-30 13:31:01 +0100885 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100887 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
888 addr <= offsetof(struct user, u_debugreg[7])) {
889 addr -= offsetof(struct user, u_debugreg[0]);
890 ret = ptrace_set_debugreg(child,
891 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Roland McGrath5a4646a2008-01-30 13:31:54 +0100895 case PTRACE_GETREGS: /* Get all gp regs from the child. */
896 return copy_regset_to_user(child,
897 task_user_regset_view(current),
898 REGSET_GENERAL,
899 0, sizeof(struct user_regs_struct),
900 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Roland McGrath5a4646a2008-01-30 13:31:54 +0100902 case PTRACE_SETREGS: /* Set all gp regs in the child. */
903 return copy_regset_from_user(child,
904 task_user_regset_view(current),
905 REGSET_GENERAL,
906 0, sizeof(struct user_regs_struct),
907 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Roland McGrath5a4646a2008-01-30 13:31:54 +0100909 case PTRACE_GETFPREGS: /* Get the child FPU state. */
910 return copy_regset_to_user(child,
911 task_user_regset_view(current),
912 REGSET_FP,
913 0, sizeof(struct user_i387_struct),
914 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Roland McGrath5a4646a2008-01-30 13:31:54 +0100916 case PTRACE_SETFPREGS: /* Set the child FPU state. */
917 return copy_regset_from_user(child,
918 task_user_regset_view(current),
919 REGSET_FP,
920 0, sizeof(struct user_i387_struct),
921 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Roland McGrathe9c86c72008-01-30 13:31:01 +0100923#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100924 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
925 return copy_regset_to_user(child, &user_x86_32_view,
926 REGSET_XFP,
927 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700928 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Roland McGrath5a4646a2008-01-30 13:31:54 +0100930 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
931 return copy_regset_from_user(child, &user_x86_32_view,
932 REGSET_XFP,
933 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700934 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Roland McGrathe9c86c72008-01-30 13:31:01 +0100937#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 case PTRACE_GET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700939 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100940 return -EIO;
941 ret = do_get_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700942 (struct user_desc __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944
945 case PTRACE_SET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700946 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100947 return -EIO;
948 ret = do_set_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700949 (struct user_desc __user *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100951#endif
952
953#ifdef CONFIG_X86_64
954 /* normal 64bit interface to access TLS data.
955 Works just like arch_prctl, except that the arguments
956 are reversed. */
957 case PTRACE_ARCH_PRCTL:
958 ret = do_arch_prctl(child, data, addr);
959 break;
960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 default:
963 ret = ptrace_request(child, request, addr, data);
964 break;
965 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return ret;
968}
969
Roland McGrathcb757c42008-01-30 13:31:01 +0100970#ifdef CONFIG_IA32_EMULATION
971
Roland McGrath099cd6e2008-01-30 13:31:01 +0100972#include <linux/compat.h>
973#include <linux/syscalls.h>
974#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +0100975#include <asm/user32.h>
976
977#define R32(l,q) \
978 case offsetof(struct user32, regs.l): \
979 regs->q = value; break
980
981#define SEG32(rs) \
982 case offsetof(struct user32, regs.rs): \
983 return set_segment_reg(child, \
984 offsetof(struct user_regs_struct, rs), \
985 value); \
986 break
987
988static int putreg32(struct task_struct *child, unsigned regno, u32 value)
989{
990 struct pt_regs *regs = task_pt_regs(child);
991
992 switch (regno) {
993
994 SEG32(cs);
995 SEG32(ds);
996 SEG32(es);
997 SEG32(fs);
998 SEG32(gs);
999 SEG32(ss);
1000
1001 R32(ebx, bx);
1002 R32(ecx, cx);
1003 R32(edx, dx);
1004 R32(edi, di);
1005 R32(esi, si);
1006 R32(ebp, bp);
1007 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001008 R32(eip, ip);
1009 R32(esp, sp);
1010
Roland McGrath40f09332008-02-28 19:57:07 -08001011 case offsetof(struct user32, regs.orig_eax):
1012 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001013 * A 32-bit debugger setting orig_eax means to restore
1014 * the state of the task restarting a 32-bit syscall.
1015 * Make sure we interpret the -ERESTART* codes correctly
1016 * in case the task is not actually still sitting at the
1017 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001018 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001019 regs->orig_ax = value;
1020 if (syscall_get_nr(child, regs) >= 0)
1021 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001022 break;
1023
Roland McGrathcb757c42008-01-30 13:31:01 +01001024 case offsetof(struct user32, regs.eflags):
1025 return set_flags(child, value);
1026
1027 case offsetof(struct user32, u_debugreg[0]) ...
1028 offsetof(struct user32, u_debugreg[7]):
1029 regno -= offsetof(struct user32, u_debugreg[0]);
1030 return ptrace_set_debugreg(child, regno / 4, value);
1031
1032 default:
1033 if (regno > sizeof(struct user32) || (regno & 3))
1034 return -EIO;
1035
1036 /*
1037 * Other dummy fields in the virtual user structure
1038 * are ignored
1039 */
1040 break;
1041 }
1042 return 0;
1043}
1044
1045#undef R32
1046#undef SEG32
1047
1048#define R32(l,q) \
1049 case offsetof(struct user32, regs.l): \
1050 *val = regs->q; break
1051
1052#define SEG32(rs) \
1053 case offsetof(struct user32, regs.rs): \
1054 *val = get_segment_reg(child, \
1055 offsetof(struct user_regs_struct, rs)); \
1056 break
1057
1058static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1059{
1060 struct pt_regs *regs = task_pt_regs(child);
1061
1062 switch (regno) {
1063
1064 SEG32(ds);
1065 SEG32(es);
1066 SEG32(fs);
1067 SEG32(gs);
1068
1069 R32(cs, cs);
1070 R32(ss, ss);
1071 R32(ebx, bx);
1072 R32(ecx, cx);
1073 R32(edx, dx);
1074 R32(edi, di);
1075 R32(esi, si);
1076 R32(ebp, bp);
1077 R32(eax, ax);
1078 R32(orig_eax, orig_ax);
1079 R32(eip, ip);
1080 R32(esp, sp);
1081
1082 case offsetof(struct user32, regs.eflags):
1083 *val = get_flags(child);
1084 break;
1085
1086 case offsetof(struct user32, u_debugreg[0]) ...
1087 offsetof(struct user32, u_debugreg[7]):
1088 regno -= offsetof(struct user32, u_debugreg[0]);
1089 *val = ptrace_get_debugreg(child, regno / 4);
1090 break;
1091
1092 default:
1093 if (regno > sizeof(struct user32) || (regno & 3))
1094 return -EIO;
1095
1096 /*
1097 * Other dummy fields in the virtual user structure
1098 * are ignored
1099 */
1100 *val = 0;
1101 break;
1102 }
1103 return 0;
1104}
1105
1106#undef R32
1107#undef SEG32
1108
Roland McGrath91e7b702008-01-30 13:31:52 +01001109static int genregs32_get(struct task_struct *target,
1110 const struct user_regset *regset,
1111 unsigned int pos, unsigned int count,
1112 void *kbuf, void __user *ubuf)
1113{
1114 if (kbuf) {
1115 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001116 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001117 getreg32(target, pos, k++);
1118 count -= sizeof(*k);
1119 pos += sizeof(*k);
1120 }
1121 } else {
1122 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001123 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001124 compat_ulong_t word;
1125 getreg32(target, pos, &word);
1126 if (__put_user(word, u++))
1127 return -EFAULT;
1128 count -= sizeof(*u);
1129 pos += sizeof(*u);
1130 }
1131 }
1132
1133 return 0;
1134}
1135
1136static int genregs32_set(struct task_struct *target,
1137 const struct user_regset *regset,
1138 unsigned int pos, unsigned int count,
1139 const void *kbuf, const void __user *ubuf)
1140{
1141 int ret = 0;
1142 if (kbuf) {
1143 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001144 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001145 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001146 count -= sizeof(*k);
1147 pos += sizeof(*k);
1148 }
1149 } else {
1150 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001151 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001152 compat_ulong_t word;
1153 ret = __get_user(word, u++);
1154 if (ret)
1155 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001156 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001157 count -= sizeof(*u);
1158 pos += sizeof(*u);
1159 }
1160 }
1161 return ret;
1162}
1163
H.J. Lu55283e22012-03-05 15:32:11 -08001164#ifdef CONFIG_X86_X32_ABI
1165static long x32_arch_ptrace(struct task_struct *child,
1166 compat_long_t request, compat_ulong_t caddr,
1167 compat_ulong_t cdata)
1168{
1169 unsigned long addr = caddr;
1170 unsigned long data = cdata;
1171 void __user *datap = compat_ptr(data);
1172 int ret;
1173
1174 switch (request) {
1175 /* Read 32bits at location addr in the USER area. Only allow
1176 to return the lower 32bits of segment and debug registers. */
1177 case PTRACE_PEEKUSR: {
1178 u32 tmp;
1179
1180 ret = -EIO;
1181 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1182 addr < offsetof(struct user_regs_struct, cs))
1183 break;
1184
1185 tmp = 0; /* Default return condition */
1186 if (addr < sizeof(struct user_regs_struct))
1187 tmp = getreg(child, addr);
1188 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1189 addr <= offsetof(struct user, u_debugreg[7])) {
1190 addr -= offsetof(struct user, u_debugreg[0]);
1191 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1192 }
1193 ret = put_user(tmp, (__u32 __user *)datap);
1194 break;
1195 }
1196
1197 /* Write the word at location addr in the USER area. Only allow
1198 to update segment and debug registers with the upper 32bits
1199 zero-extended. */
1200 case PTRACE_POKEUSR:
1201 ret = -EIO;
1202 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1203 addr < offsetof(struct user_regs_struct, cs))
1204 break;
1205
1206 if (addr < sizeof(struct user_regs_struct))
1207 ret = putreg(child, addr, data);
1208 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1209 addr <= offsetof(struct user, u_debugreg[7])) {
1210 addr -= offsetof(struct user, u_debugreg[0]);
1211 ret = ptrace_set_debugreg(child,
1212 addr / sizeof(data), data);
1213 }
1214 break;
1215
1216 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1217 return copy_regset_to_user(child,
1218 task_user_regset_view(current),
1219 REGSET_GENERAL,
1220 0, sizeof(struct user_regs_struct),
1221 datap);
1222
1223 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1224 return copy_regset_from_user(child,
1225 task_user_regset_view(current),
1226 REGSET_GENERAL,
1227 0, sizeof(struct user_regs_struct),
1228 datap);
1229
1230 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1231 return copy_regset_to_user(child,
1232 task_user_regset_view(current),
1233 REGSET_FP,
1234 0, sizeof(struct user_i387_struct),
1235 datap);
1236
1237 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1238 return copy_regset_from_user(child,
1239 task_user_regset_view(current),
1240 REGSET_FP,
1241 0, sizeof(struct user_i387_struct),
1242 datap);
1243
H.J. Lu55283e22012-03-05 15:32:11 -08001244 default:
1245 return compat_ptrace_request(child, request, addr, data);
1246 }
1247
1248 return ret;
1249}
1250#endif
1251
Roland McGrath562b80b2008-04-22 12:21:25 -07001252long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1253 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001254{
Roland McGrath562b80b2008-04-22 12:21:25 -07001255 unsigned long addr = caddr;
1256 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001257 void __user *datap = compat_ptr(data);
1258 int ret;
1259 __u32 val;
1260
H.J. Lu55283e22012-03-05 15:32:11 -08001261#ifdef CONFIG_X86_X32_ABI
1262 if (!is_ia32_task())
1263 return x32_arch_ptrace(child, request, caddr, cdata);
1264#endif
1265
Roland McGrath099cd6e2008-01-30 13:31:01 +01001266 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001267 case PTRACE_PEEKUSR:
1268 ret = getreg32(child, addr, &val);
1269 if (ret == 0)
1270 ret = put_user(val, (__u32 __user *)datap);
1271 break;
1272
1273 case PTRACE_POKEUSR:
1274 ret = putreg32(child, addr, data);
1275 break;
1276
Roland McGrath5a4646a2008-01-30 13:31:54 +01001277 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1278 return copy_regset_to_user(child, &user_x86_32_view,
1279 REGSET_GENERAL,
1280 0, sizeof(struct user_regs_struct32),
1281 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001282
Roland McGrath5a4646a2008-01-30 13:31:54 +01001283 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1284 return copy_regset_from_user(child, &user_x86_32_view,
1285 REGSET_GENERAL, 0,
1286 sizeof(struct user_regs_struct32),
1287 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001288
Roland McGrath5a4646a2008-01-30 13:31:54 +01001289 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1290 return copy_regset_to_user(child, &user_x86_32_view,
1291 REGSET_FP, 0,
1292 sizeof(struct user_i387_ia32_struct),
1293 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001294
Roland McGrath5a4646a2008-01-30 13:31:54 +01001295 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1296 return copy_regset_from_user(
1297 child, &user_x86_32_view, REGSET_FP,
1298 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001299
Roland McGrath5a4646a2008-01-30 13:31:54 +01001300 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1301 return copy_regset_to_user(child, &user_x86_32_view,
1302 REGSET_XFP, 0,
1303 sizeof(struct user32_fxsr_struct),
1304 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001305
Roland McGrath5a4646a2008-01-30 13:31:54 +01001306 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1307 return copy_regset_from_user(child, &user_x86_32_view,
1308 REGSET_XFP, 0,
1309 sizeof(struct user32_fxsr_struct),
1310 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001311
Roland McGrath562b80b2008-04-22 12:21:25 -07001312 case PTRACE_GET_THREAD_AREA:
1313 case PTRACE_SET_THREAD_AREA:
1314 return arch_ptrace(child, request, addr, data);
1315
Roland McGrath099cd6e2008-01-30 13:31:01 +01001316 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001317 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001318 }
1319
Roland McGrath099cd6e2008-01-30 13:31:01 +01001320 return ret;
1321}
1322
Roland McGrathcb757c42008-01-30 13:31:01 +01001323#endif /* CONFIG_IA32_EMULATION */
1324
Roland McGrath070459d2008-01-30 13:31:53 +01001325#ifdef CONFIG_X86_64
1326
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001327static struct user_regset x86_64_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001328 [REGSET_GENERAL] = {
1329 .core_note_type = NT_PRSTATUS,
1330 .n = sizeof(struct user_regs_struct) / sizeof(long),
1331 .size = sizeof(long), .align = sizeof(long),
1332 .get = genregs_get, .set = genregs_set
1333 },
1334 [REGSET_FP] = {
1335 .core_note_type = NT_PRFPREG,
1336 .n = sizeof(struct user_i387_struct) / sizeof(long),
1337 .size = sizeof(long), .align = sizeof(long),
1338 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1339 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001340 [REGSET_XSTATE] = {
1341 .core_note_type = NT_X86_XSTATE,
1342 .size = sizeof(u64), .align = sizeof(u64),
1343 .active = xstateregs_active, .get = xstateregs_get,
1344 .set = xstateregs_set
1345 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001346 [REGSET_IOPERM64] = {
1347 .core_note_type = NT_386_IOPERM,
1348 .n = IO_BITMAP_LONGS,
1349 .size = sizeof(long), .align = sizeof(long),
1350 .active = ioperm_active, .get = ioperm_get
1351 },
Roland McGrath070459d2008-01-30 13:31:53 +01001352};
1353
1354static const struct user_regset_view user_x86_64_view = {
1355 .name = "x86_64", .e_machine = EM_X86_64,
1356 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1357};
1358
1359#else /* CONFIG_X86_32 */
1360
1361#define user_regs_struct32 user_regs_struct
1362#define genregs32_get genregs_get
1363#define genregs32_set genregs_set
1364
Roland McGrath1f465f42008-05-09 15:43:44 -07001365#define user_i387_ia32_struct user_i387_struct
1366#define user32_fxsr_struct user_fxsr_struct
1367
Roland McGrath070459d2008-01-30 13:31:53 +01001368#endif /* CONFIG_X86_64 */
1369
1370#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001371static struct user_regset x86_32_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001372 [REGSET_GENERAL] = {
1373 .core_note_type = NT_PRSTATUS,
1374 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1375 .size = sizeof(u32), .align = sizeof(u32),
1376 .get = genregs32_get, .set = genregs32_set
1377 },
1378 [REGSET_FP] = {
1379 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001380 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001381 .size = sizeof(u32), .align = sizeof(u32),
1382 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1383 },
1384 [REGSET_XFP] = {
1385 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001386 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001387 .size = sizeof(u32), .align = sizeof(u32),
1388 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1389 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001390 [REGSET_XSTATE] = {
1391 .core_note_type = NT_X86_XSTATE,
1392 .size = sizeof(u64), .align = sizeof(u64),
1393 .active = xstateregs_active, .get = xstateregs_get,
1394 .set = xstateregs_set
1395 },
Roland McGrath070459d2008-01-30 13:31:53 +01001396 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001397 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001398 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1399 .size = sizeof(struct user_desc),
1400 .align = sizeof(struct user_desc),
1401 .active = regset_tls_active,
1402 .get = regset_tls_get, .set = regset_tls_set
1403 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001404 [REGSET_IOPERM32] = {
1405 .core_note_type = NT_386_IOPERM,
1406 .n = IO_BITMAP_BYTES / sizeof(u32),
1407 .size = sizeof(u32), .align = sizeof(u32),
1408 .active = ioperm_active, .get = ioperm_get
1409 },
Roland McGrath070459d2008-01-30 13:31:53 +01001410};
1411
1412static const struct user_regset_view user_x86_32_view = {
1413 .name = "i386", .e_machine = EM_386,
1414 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1415};
1416#endif
1417
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001418/*
1419 * This represents bytes 464..511 in the memory layout exported through
1420 * the REGSET_XSTATE interface.
1421 */
1422u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1423
1424void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1425{
1426#ifdef CONFIG_X86_64
1427 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1428#endif
1429#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1430 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1431#endif
1432 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1433}
1434
Roland McGrath070459d2008-01-30 13:31:53 +01001435const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1436{
1437#ifdef CONFIG_IA32_EMULATION
1438 if (test_tsk_thread_flag(task, TIF_IA32))
1439#endif
1440#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1441 return &user_x86_32_view;
1442#endif
1443#ifdef CONFIG_X86_64
1444 return &user_x86_64_view;
1445#endif
1446}
1447
Oleg Nesterov7f385512009-12-15 16:47:20 -08001448static void fill_sigtrap_info(struct task_struct *tsk,
1449 struct pt_regs *regs,
1450 int error_code, int si_code,
1451 struct siginfo *info)
1452{
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +05301453 tsk->thread.trap_nr = X86_TRAP_DB;
Oleg Nesterov7f385512009-12-15 16:47:20 -08001454 tsk->thread.error_code = error_code;
1455
1456 memset(info, 0, sizeof(*info));
1457 info->si_signo = SIGTRAP;
1458 info->si_code = si_code;
1459 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1460}
1461
1462void user_single_step_siginfo(struct task_struct *tsk,
1463 struct pt_regs *regs,
1464 struct siginfo *info)
1465{
1466 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1467}
1468
Srinivasa Dsda654b72008-09-23 15:23:52 +05301469void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1470 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct siginfo info;
1473
Oleg Nesterov7f385512009-12-15 16:47:20 -08001474 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
Simon Arlott27b46d72007-10-20 01:13:56 +02001475 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 force_sig_info(SIGTRAP, &info, tsk);
1477}
1478
Roland McGrath86976cd2008-01-30 13:31:01 +01001479
Roland McGrathd4d67152008-07-09 02:38:07 -07001480#ifdef CONFIG_X86_32
1481# define IS_IA32 1
1482#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001483# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001484#else
1485# define IS_IA32 0
1486#endif
1487
1488/*
1489 * We must return the syscall number to actually look up in the table.
1490 * This can be -1L to skip running any syscall at all.
1491 */
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001492long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001493{
Roland McGrathd4d67152008-07-09 02:38:07 -07001494 long ret = 0;
1495
Roland McGrath380fdd72008-07-09 02:39:29 -07001496 /*
1497 * If we stepped into a sysenter/syscall insn, it trapped in
1498 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1499 * If user-mode had set TF itself, then it's still clear from
1500 * do_debug() and we need to set it again to restore the user
1501 * state. If we entered on the slow path, TF was already set.
1502 */
1503 if (test_thread_flag(TIF_SINGLESTEP))
1504 regs->flags |= X86_EFLAGS_TF;
1505
Roland McGrath86976cd2008-01-30 13:31:01 +01001506 /* do the secure computing check first */
1507 secure_computing(regs->orig_ax);
1508
Roland McGrathd4d67152008-07-09 02:38:07 -07001509 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1510 ret = -1L;
1511
Roland McGratheeea3c32008-03-16 23:36:28 -07001512 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1513 tracehook_report_syscall_entry(regs))
1514 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001515
Josh Stone66700002009-08-24 14:43:11 -07001516 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001517 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001518
Eric Parisb05d8442012-01-03 14:23:06 -05001519 if (IS_IA32)
1520 audit_syscall_entry(AUDIT_ARCH_I386,
1521 regs->orig_ax,
1522 regs->bx, regs->cx,
1523 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001524#ifdef CONFIG_X86_64
Eric Parisb05d8442012-01-03 14:23:06 -05001525 else
1526 audit_syscall_entry(AUDIT_ARCH_X86_64,
1527 regs->orig_ax,
1528 regs->di, regs->si,
1529 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001530#endif
Roland McGrathd4d67152008-07-09 02:38:07 -07001531
1532 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001533}
1534
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001535void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001536{
Oleg Nesterovd5196502009-12-15 16:47:21 -08001537 bool step;
1538
Eric Parisd7e75282012-01-03 14:23:06 -05001539 audit_syscall_exit(regs);
Roland McGrath86976cd2008-01-30 13:31:01 +01001540
Josh Stone66700002009-08-24 14:43:11 -07001541 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001542 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001543
Roland McGrathd4d67152008-07-09 02:38:07 -07001544 /*
1545 * If TIF_SYSCALL_EMU is set, we only get here because of
1546 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1547 * We already reported this syscall instruction in
Oleg Nesterovd5196502009-12-15 16:47:21 -08001548 * syscall_trace_enter().
Roland McGrathd4d67152008-07-09 02:38:07 -07001549 */
Oleg Nesterovd5196502009-12-15 16:47:21 -08001550 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1551 !test_thread_flag(TIF_SYSCALL_EMU);
1552 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1553 tracehook_report_syscall_exit(regs, step);
Roland McGrathd4d67152008-07-09 02:38:07 -07001554}