blob: 9f94f8ec26e40f60f35e36e3151c6115b876ba73 [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
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530169static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100171 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900172 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Roland McGrath06ee1b62008-01-30 13:31:01 +0100175static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100177 /*
178 * Returning the value truncates it to 16 bits.
179 */
180 unsigned int retval;
181 if (offset != offsetof(struct user_regs_struct, gs))
182 retval = *pt_regs_access(task_pt_regs(task), offset);
183 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100184 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900185 retval = get_user_gs(task_pt_regs(task));
186 else
187 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100188 }
189 return retval;
190}
191
192static int set_segment_reg(struct task_struct *task,
193 unsigned long offset, u16 value)
194{
195 /*
196 * The value argument was already truncated to 16 bits.
197 */
Roland McGrath2047b082008-01-30 13:31:01 +0100198 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100199 return -EIO;
200
Roland McGrathc63855d2008-02-06 22:39:44 +0100201 /*
202 * For %cs and %ss we cannot permit a null selector.
203 * We can permit a bogus selector as long as it has USER_RPL.
204 * Null selectors are fine for other segment registers, but
205 * we will never get back to user mode with invalid %cs or %ss
206 * and will take the trap in iret instead. Much code relies
207 * on user_mode() to distinguish a user trap frame (which can
208 * safely use invalid selectors) from a kernel trap frame.
209 */
210 switch (offset) {
211 case offsetof(struct user_regs_struct, cs):
212 case offsetof(struct user_regs_struct, ss):
213 if (unlikely(value == 0))
214 return -EIO;
215
216 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100217 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100218 break;
219
220 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100221 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900222 set_user_gs(task_pt_regs(task), value);
223 else
224 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228}
229
Roland McGrath2047b082008-01-30 13:31:01 +0100230#else /* CONFIG_X86_64 */
231
232#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
233
234static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
235{
236 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
237 return &regs->r15 + (offset / sizeof(regs->r15));
238}
239
240static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
241{
242 /*
243 * Returning the value truncates it to 16 bits.
244 */
245 unsigned int seg;
246
247 switch (offset) {
248 case offsetof(struct user_regs_struct, fs):
249 if (task == current) {
250 /* Older gas can't assemble movq %?s,%r?? */
251 asm("movl %%fs,%0" : "=r" (seg));
252 return seg;
253 }
254 return task->thread.fsindex;
255 case offsetof(struct user_regs_struct, gs):
256 if (task == current) {
257 asm("movl %%gs,%0" : "=r" (seg));
258 return seg;
259 }
260 return task->thread.gsindex;
261 case offsetof(struct user_regs_struct, ds):
262 if (task == current) {
263 asm("movl %%ds,%0" : "=r" (seg));
264 return seg;
265 }
266 return task->thread.ds;
267 case offsetof(struct user_regs_struct, es):
268 if (task == current) {
269 asm("movl %%es,%0" : "=r" (seg));
270 return seg;
271 }
272 return task->thread.es;
273
274 case offsetof(struct user_regs_struct, cs):
275 case offsetof(struct user_regs_struct, ss):
276 break;
277 }
278 return *pt_regs_access(task_pt_regs(task), offset);
279}
280
281static int set_segment_reg(struct task_struct *task,
282 unsigned long offset, u16 value)
283{
284 /*
285 * The value argument was already truncated to 16 bits.
286 */
287 if (invalid_selector(value))
288 return -EIO;
289
290 switch (offset) {
291 case offsetof(struct user_regs_struct,fs):
292 /*
293 * If this is setting fs as for normal 64-bit use but
294 * setting fs_base has implicitly changed it, leave it.
295 */
296 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
297 task->thread.fs != 0) ||
298 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
299 task->thread.fs == 0))
300 break;
301 task->thread.fsindex = value;
302 if (task == current)
303 loadsegment(fs, task->thread.fsindex);
304 break;
305 case offsetof(struct user_regs_struct,gs):
306 /*
307 * If this is setting gs as for normal 64-bit use but
308 * setting gs_base has implicitly changed it, leave it.
309 */
310 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
311 task->thread.gs != 0) ||
312 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
313 task->thread.gs == 0))
314 break;
315 task->thread.gsindex = value;
316 if (task == current)
317 load_gs_index(task->thread.gsindex);
318 break;
319 case offsetof(struct user_regs_struct,ds):
320 task->thread.ds = value;
321 if (task == current)
322 loadsegment(ds, task->thread.ds);
323 break;
324 case offsetof(struct user_regs_struct,es):
325 task->thread.es = value;
326 if (task == current)
327 loadsegment(es, task->thread.es);
328 break;
329
330 /*
331 * Can't actually change these in 64-bit mode.
332 */
333 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100334 if (unlikely(value == 0))
335 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100336#ifdef CONFIG_IA32_EMULATION
337 if (test_tsk_thread_flag(task, TIF_IA32))
338 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100339#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100340 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100341 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100342 if (unlikely(value == 0))
343 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100344#ifdef CONFIG_IA32_EMULATION
345 if (test_tsk_thread_flag(task, TIF_IA32))
346 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100347#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100348 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100349 }
350
351 return 0;
352}
353
Roland McGrath2047b082008-01-30 13:31:01 +0100354#endif /* CONFIG_X86_32 */
355
Roland McGrath06ee1b62008-01-30 13:31:01 +0100356static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100358 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Roland McGrath06ee1b62008-01-30 13:31:01 +0100360 /*
361 * If the debugger set TF, hide it from the readout.
362 */
363 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
364 retval &= ~X86_EFLAGS_TF;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return retval;
367}
368
Roland McGrath06ee1b62008-01-30 13:31:01 +0100369static int set_flags(struct task_struct *task, unsigned long value)
370{
371 struct pt_regs *regs = task_pt_regs(task);
372
373 /*
374 * If the user value contains TF, mark that
375 * it was not "us" (the debugger) that set it.
376 * If not, make sure it stays set if we had.
377 */
378 if (value & X86_EFLAGS_TF)
379 clear_tsk_thread_flag(task, TIF_FORCED_TF);
380 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
381 value |= X86_EFLAGS_TF;
382
383 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
384
385 return 0;
386}
387
388static int putreg(struct task_struct *child,
389 unsigned long offset, unsigned long value)
390{
391 switch (offset) {
392 case offsetof(struct user_regs_struct, cs):
393 case offsetof(struct user_regs_struct, ds):
394 case offsetof(struct user_regs_struct, es):
395 case offsetof(struct user_regs_struct, fs):
396 case offsetof(struct user_regs_struct, gs):
397 case offsetof(struct user_regs_struct, ss):
398 return set_segment_reg(child, offset, value);
399
400 case offsetof(struct user_regs_struct, flags):
401 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100402
403#ifdef CONFIG_X86_64
404 case offsetof(struct user_regs_struct,fs_base):
405 if (value >= TASK_SIZE_OF(child))
406 return -EIO;
407 /*
408 * When changing the segment base, use do_arch_prctl
409 * to set either thread.fs or thread.fsindex and the
410 * corresponding GDT slot.
411 */
412 if (child->thread.fs != value)
413 return do_arch_prctl(child, ARCH_SET_FS, value);
414 return 0;
415 case offsetof(struct user_regs_struct,gs_base):
416 /*
417 * Exactly the same here as the %fs handling above.
418 */
419 if (value >= TASK_SIZE_OF(child))
420 return -EIO;
421 if (child->thread.gs != value)
422 return do_arch_prctl(child, ARCH_SET_GS, value);
423 return 0;
424#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100425 }
426
427 *pt_regs_access(task_pt_regs(child), offset) = value;
428 return 0;
429}
430
431static unsigned long getreg(struct task_struct *task, unsigned long offset)
432{
433 switch (offset) {
434 case offsetof(struct user_regs_struct, cs):
435 case offsetof(struct user_regs_struct, ds):
436 case offsetof(struct user_regs_struct, es):
437 case offsetof(struct user_regs_struct, fs):
438 case offsetof(struct user_regs_struct, gs):
439 case offsetof(struct user_regs_struct, ss):
440 return get_segment_reg(task, offset);
441
442 case offsetof(struct user_regs_struct, flags):
443 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100444
445#ifdef CONFIG_X86_64
446 case offsetof(struct user_regs_struct, fs_base): {
447 /*
448 * do_arch_prctl may have used a GDT slot instead of
449 * the MSR. To userland, it appears the same either
450 * way, except the %fs segment selector might not be 0.
451 */
452 unsigned int seg = task->thread.fsindex;
453 if (task->thread.fs != 0)
454 return task->thread.fs;
455 if (task == current)
456 asm("movl %%fs,%0" : "=r" (seg));
457 if (seg != FS_TLS_SEL)
458 return 0;
459 return get_desc_base(&task->thread.tls_array[FS_TLS]);
460 }
461 case offsetof(struct user_regs_struct, gs_base): {
462 /*
463 * Exactly the same here as the %fs handling above.
464 */
465 unsigned int seg = task->thread.gsindex;
466 if (task->thread.gs != 0)
467 return task->thread.gs;
468 if (task == current)
469 asm("movl %%gs,%0" : "=r" (seg));
470 if (seg != GS_TLS_SEL)
471 return 0;
472 return get_desc_base(&task->thread.tls_array[GS_TLS]);
473 }
474#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100475 }
476
477 return *pt_regs_access(task_pt_regs(task), offset);
478}
479
Roland McGrath91e7b702008-01-30 13:31:52 +0100480static int genregs_get(struct task_struct *target,
481 const struct user_regset *regset,
482 unsigned int pos, unsigned int count,
483 void *kbuf, void __user *ubuf)
484{
485 if (kbuf) {
486 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800487 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100488 *k++ = getreg(target, pos);
489 count -= sizeof(*k);
490 pos += sizeof(*k);
491 }
492 } else {
493 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800494 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100495 if (__put_user(getreg(target, pos), u++))
496 return -EFAULT;
497 count -= sizeof(*u);
498 pos += sizeof(*u);
499 }
500 }
501
502 return 0;
503}
504
505static int genregs_set(struct task_struct *target,
506 const struct user_regset *regset,
507 unsigned int pos, unsigned int count,
508 const void *kbuf, const void __user *ubuf)
509{
510 int ret = 0;
511 if (kbuf) {
512 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800513 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100514 ret = putreg(target, pos, *k++);
515 count -= sizeof(*k);
516 pos += sizeof(*k);
517 }
518 } else {
519 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800520 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100521 unsigned long word;
522 ret = __get_user(word, u++);
523 if (ret)
524 break;
525 ret = putreg(target, pos, word);
526 count -= sizeof(*u);
527 pos += sizeof(*u);
528 }
529 }
530 return ret;
531}
532
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200533static void ptrace_triggered(struct perf_event *bp,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100534 struct perf_sample_data *data,
535 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100536{
Roland McGrath0f534092008-01-30 13:30:59 +0100537 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200538 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100539
K.Prasad72f674d2009-06-01 23:45:48 +0530540 /*
541 * Store in the virtual DR6 register the fact that the breakpoint
542 * was hit so the thread's debugger will see it.
543 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200544 for (i = 0; i < HBP_NUM; i++) {
545 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530546 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100547 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100548
K.Prasad72f674d2009-06-01 23:45:48 +0530549 thread->debugreg6 |= (DR_TRAP0 << i);
550}
551
552/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200553 * Walk through every ptrace breakpoints for this thread and
554 * build the dr7 value on top of their attributes.
555 *
556 */
557static unsigned long ptrace_get_dr7(struct perf_event *bp[])
558{
559 int i;
560 int dr7 = 0;
561 struct arch_hw_breakpoint *info;
562
563 for (i = 0; i < HBP_NUM; i++) {
564 if (bp[i] && !bp[i]->attr.disabled) {
565 info = counter_arch_bp(bp[i]);
566 dr7 |= encode_dr7(i, info->len, info->type);
567 }
568 }
569
570 return dr7;
571}
572
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100573static int
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100574ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100575 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100576{
577 int err;
578 int gen_len, gen_type;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100579 struct perf_event_attr attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100580
581 /*
Adam Buchbinderc9404c92009-12-18 15:40:42 -0500582 * We should have at least an inactive breakpoint at this
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100583 * slot. It means the user is writing dr7 without having
584 * written the address register first
585 */
586 if (!bp)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100587 return -EINVAL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100588
589 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
590 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100591 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100592
593 attr = bp->attr;
594 attr.bp_len = gen_len;
595 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100596 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100597
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100598 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100599}
600
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200601/*
K.Prasad72f674d2009-06-01 23:45:48 +0530602 * Handle ptrace writes to debug register 7.
603 */
604static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
605{
606 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200607 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530608 int i, orig_ret = 0, rc = 0;
609 int enabled, second_pass = 0;
610 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200611 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530612
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200613 if (ptrace_get_breakpoints(tsk) < 0)
614 return -ESRCH;
615
K.Prasad72f674d2009-06-01 23:45:48 +0530616 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200617 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530618restore:
619 /*
620 * Loop through all the hardware breakpoints, making the
621 * appropriate changes to each.
622 */
623 for (i = 0; i < HBP_NUM; i++) {
624 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200625 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530626
627 if (!enabled) {
628 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200629 /*
630 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530631 * unless all register_user_hw_breakpoint()
632 * requests have succeeded. This prevents
633 * any window of opportunity for debug
634 * register grabbing by other users.
635 */
636 if (!second_pass)
637 continue;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100638
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100639 rc = ptrace_modify_breakpoint(bp, len, type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100640 tsk, 1);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100641 if (rc)
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100642 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530643 }
644 continue;
645 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200646
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100647 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
648 if (rc)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200649 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530650 }
651 /*
652 * Make a second pass to free the remaining unused breakpoints
653 * or to restore the original breakpoints if an error occurred.
654 */
655 if (!second_pass) {
656 second_pass = 1;
657 if (rc < 0) {
658 orig_ret = rc;
659 data = old_dr7;
660 }
661 goto restore;
662 }
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200663
664 ptrace_put_breakpoints(tsk);
665
K.Prasad72f674d2009-06-01 23:45:48 +0530666 return ((orig_ret < 0) ? orig_ret : rc);
667}
668
669/*
670 * Handle PTRACE_PEEKUSR calls for the debug register area.
671 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530672static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530673{
674 struct thread_struct *thread = &(tsk->thread);
675 unsigned long val = 0;
676
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200677 if (n < HBP_NUM) {
678 struct perf_event *bp;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200679
680 if (ptrace_get_breakpoints(tsk) < 0)
681 return -ESRCH;
682
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200683 bp = thread->ptrace_bps[n];
684 if (!bp)
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200685 val = 0;
686 else
687 val = bp->hw.info.address;
688
689 ptrace_put_breakpoints(tsk);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200690 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530691 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200692 } else if (n == 7) {
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100693 val = thread->ptrace_dr7;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200694 }
K.Prasad72f674d2009-06-01 23:45:48 +0530695 return val;
696}
697
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200698static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
699 unsigned long addr)
700{
701 struct perf_event *bp;
702 struct thread_struct *t = &tsk->thread;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100703 struct perf_event_attr attr;
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200704 int err = 0;
705
706 if (ptrace_get_breakpoints(tsk) < 0)
707 return -ESRCH;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200708
709 if (!t->ptrace_bps[nr]) {
Frederic Weisbecker73266fc2010-04-22 05:05:45 +0200710 ptrace_breakpoint_init(&attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200711 /*
712 * Put stub len and type to register (reserve) an inactive but
713 * correct bp
714 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100715 attr.bp_addr = addr;
716 attr.bp_len = HW_BREAKPOINT_LEN_1;
717 attr.bp_type = HW_BREAKPOINT_W;
718 attr.disabled = 1;
719
Avi Kivity4dc0da82011-06-29 18:42:35 +0300720 bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
721 NULL, tsk);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100722
723 /*
724 * CHECKME: the previous code returned -EIO if the addr wasn't
725 * a valid task virtual addr. The new one will return -EINVAL in
726 * this case.
727 * -EINVAL may be what we want for in-kernel breakpoints users,
728 * but -EIO looks better for ptrace, since we refuse a register
729 * writing for the user. And anyway this is the previous
730 * behaviour.
731 */
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200732 if (IS_ERR(bp)) {
733 err = PTR_ERR(bp);
734 goto put;
735 }
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100736
737 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200738 } else {
739 bp = t->ptrace_bps[nr];
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100740
741 attr = bp->attr;
742 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100743 err = modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200744 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200745
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200746put:
747 ptrace_put_breakpoints(tsk);
748 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
K.Prasad72f674d2009-06-01 23:45:48 +0530751/*
752 * Handle PTRACE_POKEUSR calls for the debug register area.
753 */
H Hartley Sweeten98b8b992011-11-15 14:48:58 -0800754static int ptrace_set_debugreg(struct task_struct *tsk, int n,
755 unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
K.Prasad72f674d2009-06-01 23:45:48 +0530757 struct thread_struct *thread = &(tsk->thread);
758 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
K.Prasad72f674d2009-06-01 23:45:48 +0530760 /* There are no DR4 or DR5 registers */
761 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -EIO;
763
K.Prasad72f674d2009-06-01 23:45:48 +0530764 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200765 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530766 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
K.Prasad72f674d2009-06-01 23:45:48 +0530768 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200769 rc = ptrace_set_breakpoint_addr(tsk, n, val);
770 if (rc)
771 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530772 }
773 /* All that's left is DR7 */
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100774 if (n == 7) {
K.Prasad72f674d2009-06-01 23:45:48 +0530775 rc = ptrace_write_dr7(tsk, val);
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100776 if (!rc)
777 thread->ptrace_dr7 = val;
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
K.Prasad72f674d2009-06-01 23:45:48 +0530780ret_path:
781 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Roland McGrath325af5f2008-08-08 15:58:39 -0700784/*
785 * These access the current or another (stopped) task's io permission
786 * bitmap for debugging or core dump.
787 */
788static int ioperm_active(struct task_struct *target,
789 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100790{
Roland McGrath325af5f2008-08-08 15:58:39 -0700791 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100792}
793
Roland McGrath325af5f2008-08-08 15:58:39 -0700794static int ioperm_get(struct task_struct *target,
795 const struct user_regset *regset,
796 unsigned int pos, unsigned int count,
797 void *kbuf, void __user *ubuf)
798{
799 if (!target->thread.io_bitmap_ptr)
800 return -ENXIO;
801
802 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
803 target->thread.io_bitmap_ptr,
804 0, IO_BITMAP_BYTES);
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
808 * Called by kernel/ptrace.c when detaching..
809 *
810 * Make sure the single step bit is not set.
811 */
812void ptrace_disable(struct task_struct *child)
813{
814 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100815#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Roland McGrath5a4646a2008-01-30 13:31:54 +0100820#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
821static const struct user_regset_view user_x86_32_view; /* Initialized below. */
822#endif
823
Namhyung Kim9b05a692010-10-27 15:33:47 -0700824long arch_ptrace(struct task_struct *child, long request,
825 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100827 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 unsigned long __user *datap = (unsigned long __user *)data;
829
830 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* read the word at location addr in the USER area. */
832 case PTRACE_PEEKUSR: {
833 unsigned long tmp;
834
835 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700836 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838
839 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100840 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100842 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
843 addr <= offsetof(struct user, u_debugreg[7])) {
844 addr -= offsetof(struct user, u_debugreg[0]);
845 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 ret = put_user(tmp, datap);
848 break;
849 }
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
852 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700853 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 break;
855
Roland McGrathe9c86c72008-01-30 13:31:01 +0100856 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100858 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
859 addr <= offsetof(struct user, u_debugreg[7])) {
860 addr -= offsetof(struct user, u_debugreg[0]);
861 ret = ptrace_set_debugreg(child,
862 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100864 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Roland McGrath5a4646a2008-01-30 13:31:54 +0100866 case PTRACE_GETREGS: /* Get all gp regs from the child. */
867 return copy_regset_to_user(child,
868 task_user_regset_view(current),
869 REGSET_GENERAL,
870 0, sizeof(struct user_regs_struct),
871 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Roland McGrath5a4646a2008-01-30 13:31:54 +0100873 case PTRACE_SETREGS: /* Set all gp regs in the child. */
874 return copy_regset_from_user(child,
875 task_user_regset_view(current),
876 REGSET_GENERAL,
877 0, sizeof(struct user_regs_struct),
878 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Roland McGrath5a4646a2008-01-30 13:31:54 +0100880 case PTRACE_GETFPREGS: /* Get the child FPU state. */
881 return copy_regset_to_user(child,
882 task_user_regset_view(current),
883 REGSET_FP,
884 0, sizeof(struct user_i387_struct),
885 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Roland McGrath5a4646a2008-01-30 13:31:54 +0100887 case PTRACE_SETFPREGS: /* Set the child FPU state. */
888 return copy_regset_from_user(child,
889 task_user_regset_view(current),
890 REGSET_FP,
891 0, sizeof(struct user_i387_struct),
892 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Roland McGrathe9c86c72008-01-30 13:31:01 +0100894#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100895 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
896 return copy_regset_to_user(child, &user_x86_32_view,
897 REGSET_XFP,
898 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700899 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Roland McGrath5a4646a2008-01-30 13:31:54 +0100901 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
902 return copy_regset_from_user(child, &user_x86_32_view,
903 REGSET_XFP,
904 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700905 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100906#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Roland McGrathe9c86c72008-01-30 13:31:01 +0100908#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 case PTRACE_GET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700910 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100911 return -EIO;
912 ret = do_get_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700913 (struct user_desc __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 break;
915
916 case PTRACE_SET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700917 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100918 return -EIO;
919 ret = do_set_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700920 (struct user_desc __user *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100922#endif
923
924#ifdef CONFIG_X86_64
925 /* normal 64bit interface to access TLS data.
926 Works just like arch_prctl, except that the arguments
927 are reversed. */
928 case PTRACE_ARCH_PRCTL:
929 ret = do_arch_prctl(child, data, addr);
930 break;
931#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 default:
934 ret = ptrace_request(child, request, addr, data);
935 break;
936 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return ret;
939}
940
Roland McGrathcb757c42008-01-30 13:31:01 +0100941#ifdef CONFIG_IA32_EMULATION
942
Roland McGrath099cd6e2008-01-30 13:31:01 +0100943#include <linux/compat.h>
944#include <linux/syscalls.h>
945#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +0100946#include <asm/user32.h>
947
948#define R32(l,q) \
949 case offsetof(struct user32, regs.l): \
950 regs->q = value; break
951
952#define SEG32(rs) \
953 case offsetof(struct user32, regs.rs): \
954 return set_segment_reg(child, \
955 offsetof(struct user_regs_struct, rs), \
956 value); \
957 break
958
959static int putreg32(struct task_struct *child, unsigned regno, u32 value)
960{
961 struct pt_regs *regs = task_pt_regs(child);
962
963 switch (regno) {
964
965 SEG32(cs);
966 SEG32(ds);
967 SEG32(es);
968 SEG32(fs);
969 SEG32(gs);
970 SEG32(ss);
971
972 R32(ebx, bx);
973 R32(ecx, cx);
974 R32(edx, dx);
975 R32(edi, di);
976 R32(esi, si);
977 R32(ebp, bp);
978 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +0100979 R32(eip, ip);
980 R32(esp, sp);
981
Roland McGrath40f09332008-02-28 19:57:07 -0800982 case offsetof(struct user32, regs.orig_eax):
983 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -0700984 * A 32-bit debugger setting orig_eax means to restore
985 * the state of the task restarting a 32-bit syscall.
986 * Make sure we interpret the -ERESTART* codes correctly
987 * in case the task is not actually still sitting at the
988 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -0800989 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -0700990 regs->orig_ax = value;
991 if (syscall_get_nr(child, regs) >= 0)
992 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -0800993 break;
994
Roland McGrathcb757c42008-01-30 13:31:01 +0100995 case offsetof(struct user32, regs.eflags):
996 return set_flags(child, value);
997
998 case offsetof(struct user32, u_debugreg[0]) ...
999 offsetof(struct user32, u_debugreg[7]):
1000 regno -= offsetof(struct user32, u_debugreg[0]);
1001 return ptrace_set_debugreg(child, regno / 4, value);
1002
1003 default:
1004 if (regno > sizeof(struct user32) || (regno & 3))
1005 return -EIO;
1006
1007 /*
1008 * Other dummy fields in the virtual user structure
1009 * are ignored
1010 */
1011 break;
1012 }
1013 return 0;
1014}
1015
1016#undef R32
1017#undef SEG32
1018
1019#define R32(l,q) \
1020 case offsetof(struct user32, regs.l): \
1021 *val = regs->q; break
1022
1023#define SEG32(rs) \
1024 case offsetof(struct user32, regs.rs): \
1025 *val = get_segment_reg(child, \
1026 offsetof(struct user_regs_struct, rs)); \
1027 break
1028
1029static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1030{
1031 struct pt_regs *regs = task_pt_regs(child);
1032
1033 switch (regno) {
1034
1035 SEG32(ds);
1036 SEG32(es);
1037 SEG32(fs);
1038 SEG32(gs);
1039
1040 R32(cs, cs);
1041 R32(ss, ss);
1042 R32(ebx, bx);
1043 R32(ecx, cx);
1044 R32(edx, dx);
1045 R32(edi, di);
1046 R32(esi, si);
1047 R32(ebp, bp);
1048 R32(eax, ax);
1049 R32(orig_eax, orig_ax);
1050 R32(eip, ip);
1051 R32(esp, sp);
1052
1053 case offsetof(struct user32, regs.eflags):
1054 *val = get_flags(child);
1055 break;
1056
1057 case offsetof(struct user32, u_debugreg[0]) ...
1058 offsetof(struct user32, u_debugreg[7]):
1059 regno -= offsetof(struct user32, u_debugreg[0]);
1060 *val = ptrace_get_debugreg(child, regno / 4);
1061 break;
1062
1063 default:
1064 if (regno > sizeof(struct user32) || (regno & 3))
1065 return -EIO;
1066
1067 /*
1068 * Other dummy fields in the virtual user structure
1069 * are ignored
1070 */
1071 *val = 0;
1072 break;
1073 }
1074 return 0;
1075}
1076
1077#undef R32
1078#undef SEG32
1079
Roland McGrath91e7b702008-01-30 13:31:52 +01001080static int genregs32_get(struct task_struct *target,
1081 const struct user_regset *regset,
1082 unsigned int pos, unsigned int count,
1083 void *kbuf, void __user *ubuf)
1084{
1085 if (kbuf) {
1086 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001087 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001088 getreg32(target, pos, k++);
1089 count -= sizeof(*k);
1090 pos += sizeof(*k);
1091 }
1092 } else {
1093 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001094 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001095 compat_ulong_t word;
1096 getreg32(target, pos, &word);
1097 if (__put_user(word, u++))
1098 return -EFAULT;
1099 count -= sizeof(*u);
1100 pos += sizeof(*u);
1101 }
1102 }
1103
1104 return 0;
1105}
1106
1107static int genregs32_set(struct task_struct *target,
1108 const struct user_regset *regset,
1109 unsigned int pos, unsigned int count,
1110 const void *kbuf, const void __user *ubuf)
1111{
1112 int ret = 0;
1113 if (kbuf) {
1114 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001115 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001116 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001117 count -= sizeof(*k);
1118 pos += sizeof(*k);
1119 }
1120 } else {
1121 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001122 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001123 compat_ulong_t word;
1124 ret = __get_user(word, u++);
1125 if (ret)
1126 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001127 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001128 count -= sizeof(*u);
1129 pos += sizeof(*u);
1130 }
1131 }
1132 return ret;
1133}
1134
H.J. Lu55283e22012-03-05 15:32:11 -08001135#ifdef CONFIG_X86_X32_ABI
1136static long x32_arch_ptrace(struct task_struct *child,
1137 compat_long_t request, compat_ulong_t caddr,
1138 compat_ulong_t cdata)
1139{
1140 unsigned long addr = caddr;
1141 unsigned long data = cdata;
1142 void __user *datap = compat_ptr(data);
1143 int ret;
1144
1145 switch (request) {
1146 /* Read 32bits at location addr in the USER area. Only allow
1147 to return the lower 32bits of segment and debug registers. */
1148 case PTRACE_PEEKUSR: {
1149 u32 tmp;
1150
1151 ret = -EIO;
1152 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1153 addr < offsetof(struct user_regs_struct, cs))
1154 break;
1155
1156 tmp = 0; /* Default return condition */
1157 if (addr < sizeof(struct user_regs_struct))
1158 tmp = getreg(child, addr);
1159 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1160 addr <= offsetof(struct user, u_debugreg[7])) {
1161 addr -= offsetof(struct user, u_debugreg[0]);
1162 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1163 }
1164 ret = put_user(tmp, (__u32 __user *)datap);
1165 break;
1166 }
1167
1168 /* Write the word at location addr in the USER area. Only allow
1169 to update segment and debug registers with the upper 32bits
1170 zero-extended. */
1171 case PTRACE_POKEUSR:
1172 ret = -EIO;
1173 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1174 addr < offsetof(struct user_regs_struct, cs))
1175 break;
1176
1177 if (addr < sizeof(struct user_regs_struct))
1178 ret = putreg(child, addr, data);
1179 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1180 addr <= offsetof(struct user, u_debugreg[7])) {
1181 addr -= offsetof(struct user, u_debugreg[0]);
1182 ret = ptrace_set_debugreg(child,
1183 addr / sizeof(data), data);
1184 }
1185 break;
1186
1187 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1188 return copy_regset_to_user(child,
1189 task_user_regset_view(current),
1190 REGSET_GENERAL,
1191 0, sizeof(struct user_regs_struct),
1192 datap);
1193
1194 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1195 return copy_regset_from_user(child,
1196 task_user_regset_view(current),
1197 REGSET_GENERAL,
1198 0, sizeof(struct user_regs_struct),
1199 datap);
1200
1201 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1202 return copy_regset_to_user(child,
1203 task_user_regset_view(current),
1204 REGSET_FP,
1205 0, sizeof(struct user_i387_struct),
1206 datap);
1207
1208 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1209 return copy_regset_from_user(child,
1210 task_user_regset_view(current),
1211 REGSET_FP,
1212 0, sizeof(struct user_i387_struct),
1213 datap);
1214
H.J. Lu55283e22012-03-05 15:32:11 -08001215 default:
1216 return compat_ptrace_request(child, request, addr, data);
1217 }
1218
1219 return ret;
1220}
1221#endif
1222
Roland McGrath562b80b2008-04-22 12:21:25 -07001223long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1224 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001225{
Roland McGrath562b80b2008-04-22 12:21:25 -07001226 unsigned long addr = caddr;
1227 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001228 void __user *datap = compat_ptr(data);
1229 int ret;
1230 __u32 val;
1231
H.J. Lu55283e22012-03-05 15:32:11 -08001232#ifdef CONFIG_X86_X32_ABI
1233 if (!is_ia32_task())
1234 return x32_arch_ptrace(child, request, caddr, cdata);
1235#endif
1236
Roland McGrath099cd6e2008-01-30 13:31:01 +01001237 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001238 case PTRACE_PEEKUSR:
1239 ret = getreg32(child, addr, &val);
1240 if (ret == 0)
1241 ret = put_user(val, (__u32 __user *)datap);
1242 break;
1243
1244 case PTRACE_POKEUSR:
1245 ret = putreg32(child, addr, data);
1246 break;
1247
Roland McGrath5a4646a2008-01-30 13:31:54 +01001248 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1249 return copy_regset_to_user(child, &user_x86_32_view,
1250 REGSET_GENERAL,
1251 0, sizeof(struct user_regs_struct32),
1252 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001253
Roland McGrath5a4646a2008-01-30 13:31:54 +01001254 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1255 return copy_regset_from_user(child, &user_x86_32_view,
1256 REGSET_GENERAL, 0,
1257 sizeof(struct user_regs_struct32),
1258 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001259
Roland McGrath5a4646a2008-01-30 13:31:54 +01001260 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1261 return copy_regset_to_user(child, &user_x86_32_view,
1262 REGSET_FP, 0,
1263 sizeof(struct user_i387_ia32_struct),
1264 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001265
Roland McGrath5a4646a2008-01-30 13:31:54 +01001266 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1267 return copy_regset_from_user(
1268 child, &user_x86_32_view, REGSET_FP,
1269 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001270
Roland McGrath5a4646a2008-01-30 13:31:54 +01001271 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1272 return copy_regset_to_user(child, &user_x86_32_view,
1273 REGSET_XFP, 0,
1274 sizeof(struct user32_fxsr_struct),
1275 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001276
Roland McGrath5a4646a2008-01-30 13:31:54 +01001277 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1278 return copy_regset_from_user(child, &user_x86_32_view,
1279 REGSET_XFP, 0,
1280 sizeof(struct user32_fxsr_struct),
1281 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001282
Roland McGrath562b80b2008-04-22 12:21:25 -07001283 case PTRACE_GET_THREAD_AREA:
1284 case PTRACE_SET_THREAD_AREA:
1285 return arch_ptrace(child, request, addr, data);
1286
Roland McGrath099cd6e2008-01-30 13:31:01 +01001287 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001288 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001289 }
1290
Roland McGrath099cd6e2008-01-30 13:31:01 +01001291 return ret;
1292}
1293
Roland McGrathcb757c42008-01-30 13:31:01 +01001294#endif /* CONFIG_IA32_EMULATION */
1295
Roland McGrath070459d2008-01-30 13:31:53 +01001296#ifdef CONFIG_X86_64
1297
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001298static struct user_regset x86_64_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001299 [REGSET_GENERAL] = {
1300 .core_note_type = NT_PRSTATUS,
1301 .n = sizeof(struct user_regs_struct) / sizeof(long),
1302 .size = sizeof(long), .align = sizeof(long),
1303 .get = genregs_get, .set = genregs_set
1304 },
1305 [REGSET_FP] = {
1306 .core_note_type = NT_PRFPREG,
1307 .n = sizeof(struct user_i387_struct) / sizeof(long),
1308 .size = sizeof(long), .align = sizeof(long),
1309 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1310 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001311 [REGSET_XSTATE] = {
1312 .core_note_type = NT_X86_XSTATE,
1313 .size = sizeof(u64), .align = sizeof(u64),
1314 .active = xstateregs_active, .get = xstateregs_get,
1315 .set = xstateregs_set
1316 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001317 [REGSET_IOPERM64] = {
1318 .core_note_type = NT_386_IOPERM,
1319 .n = IO_BITMAP_LONGS,
1320 .size = sizeof(long), .align = sizeof(long),
1321 .active = ioperm_active, .get = ioperm_get
1322 },
Roland McGrath070459d2008-01-30 13:31:53 +01001323};
1324
1325static const struct user_regset_view user_x86_64_view = {
1326 .name = "x86_64", .e_machine = EM_X86_64,
1327 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1328};
1329
1330#else /* CONFIG_X86_32 */
1331
1332#define user_regs_struct32 user_regs_struct
1333#define genregs32_get genregs_get
1334#define genregs32_set genregs_set
1335
Roland McGrath1f465f42008-05-09 15:43:44 -07001336#define user_i387_ia32_struct user_i387_struct
1337#define user32_fxsr_struct user_fxsr_struct
1338
Roland McGrath070459d2008-01-30 13:31:53 +01001339#endif /* CONFIG_X86_64 */
1340
1341#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001342static struct user_regset x86_32_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001343 [REGSET_GENERAL] = {
1344 .core_note_type = NT_PRSTATUS,
1345 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1346 .size = sizeof(u32), .align = sizeof(u32),
1347 .get = genregs32_get, .set = genregs32_set
1348 },
1349 [REGSET_FP] = {
1350 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001351 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001352 .size = sizeof(u32), .align = sizeof(u32),
1353 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1354 },
1355 [REGSET_XFP] = {
1356 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001357 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001358 .size = sizeof(u32), .align = sizeof(u32),
1359 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1360 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001361 [REGSET_XSTATE] = {
1362 .core_note_type = NT_X86_XSTATE,
1363 .size = sizeof(u64), .align = sizeof(u64),
1364 .active = xstateregs_active, .get = xstateregs_get,
1365 .set = xstateregs_set
1366 },
Roland McGrath070459d2008-01-30 13:31:53 +01001367 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001368 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001369 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1370 .size = sizeof(struct user_desc),
1371 .align = sizeof(struct user_desc),
1372 .active = regset_tls_active,
1373 .get = regset_tls_get, .set = regset_tls_set
1374 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001375 [REGSET_IOPERM32] = {
1376 .core_note_type = NT_386_IOPERM,
1377 .n = IO_BITMAP_BYTES / sizeof(u32),
1378 .size = sizeof(u32), .align = sizeof(u32),
1379 .active = ioperm_active, .get = ioperm_get
1380 },
Roland McGrath070459d2008-01-30 13:31:53 +01001381};
1382
1383static const struct user_regset_view user_x86_32_view = {
1384 .name = "i386", .e_machine = EM_386,
1385 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1386};
1387#endif
1388
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001389/*
1390 * This represents bytes 464..511 in the memory layout exported through
1391 * the REGSET_XSTATE interface.
1392 */
1393u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1394
1395void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1396{
1397#ifdef CONFIG_X86_64
1398 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1399#endif
1400#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1401 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1402#endif
1403 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1404}
1405
Roland McGrath070459d2008-01-30 13:31:53 +01001406const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1407{
1408#ifdef CONFIG_IA32_EMULATION
1409 if (test_tsk_thread_flag(task, TIF_IA32))
1410#endif
1411#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1412 return &user_x86_32_view;
1413#endif
1414#ifdef CONFIG_X86_64
1415 return &user_x86_64_view;
1416#endif
1417}
1418
Oleg Nesterov7f385512009-12-15 16:47:20 -08001419static void fill_sigtrap_info(struct task_struct *tsk,
1420 struct pt_regs *regs,
1421 int error_code, int si_code,
1422 struct siginfo *info)
1423{
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +05301424 tsk->thread.trap_nr = X86_TRAP_DB;
Oleg Nesterov7f385512009-12-15 16:47:20 -08001425 tsk->thread.error_code = error_code;
1426
1427 memset(info, 0, sizeof(*info));
1428 info->si_signo = SIGTRAP;
1429 info->si_code = si_code;
1430 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1431}
1432
1433void user_single_step_siginfo(struct task_struct *tsk,
1434 struct pt_regs *regs,
1435 struct siginfo *info)
1436{
1437 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1438}
1439
Srinivasa Dsda654b72008-09-23 15:23:52 +05301440void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1441 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct siginfo info;
1444
Oleg Nesterov7f385512009-12-15 16:47:20 -08001445 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
Simon Arlott27b46d72007-10-20 01:13:56 +02001446 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 force_sig_info(SIGTRAP, &info, tsk);
1448}
1449
Roland McGrath86976cd2008-01-30 13:31:01 +01001450
Roland McGrathd4d67152008-07-09 02:38:07 -07001451#ifdef CONFIG_X86_32
1452# define IS_IA32 1
1453#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001454# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001455#else
1456# define IS_IA32 0
1457#endif
1458
1459/*
1460 * We must return the syscall number to actually look up in the table.
1461 * This can be -1L to skip running any syscall at all.
1462 */
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001463long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001464{
Roland McGrathd4d67152008-07-09 02:38:07 -07001465 long ret = 0;
1466
Frederic Weisbeckerbf5a3c12012-07-11 20:26:34 +02001467 rcu_user_exit();
1468
Roland McGrath380fdd72008-07-09 02:39:29 -07001469 /*
1470 * If we stepped into a sysenter/syscall insn, it trapped in
1471 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1472 * If user-mode had set TF itself, then it's still clear from
1473 * do_debug() and we need to set it again to restore the user
1474 * state. If we entered on the slow path, TF was already set.
1475 */
1476 if (test_thread_flag(TIF_SINGLESTEP))
1477 regs->flags |= X86_EFLAGS_TF;
1478
Roland McGrath86976cd2008-01-30 13:31:01 +01001479 /* do the secure computing check first */
Will Drewryc6cfbeb2012-04-12 16:48:03 -05001480 if (secure_computing(regs->orig_ax)) {
1481 /* seccomp failures shouldn't expose any additional code. */
1482 ret = -1L;
1483 goto out;
1484 }
Roland McGrath86976cd2008-01-30 13:31:01 +01001485
Roland McGrathd4d67152008-07-09 02:38:07 -07001486 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1487 ret = -1L;
1488
Roland McGratheeea3c32008-03-16 23:36:28 -07001489 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1490 tracehook_report_syscall_entry(regs))
1491 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001492
Josh Stone66700002009-08-24 14:43:11 -07001493 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001494 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001495
Eric Parisb05d8442012-01-03 14:23:06 -05001496 if (IS_IA32)
1497 audit_syscall_entry(AUDIT_ARCH_I386,
1498 regs->orig_ax,
1499 regs->bx, regs->cx,
1500 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001501#ifdef CONFIG_X86_64
Eric Parisb05d8442012-01-03 14:23:06 -05001502 else
1503 audit_syscall_entry(AUDIT_ARCH_X86_64,
1504 regs->orig_ax,
1505 regs->di, regs->si,
1506 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001507#endif
Roland McGrathd4d67152008-07-09 02:38:07 -07001508
Will Drewryc6cfbeb2012-04-12 16:48:03 -05001509out:
Roland McGrathd4d67152008-07-09 02:38:07 -07001510 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001511}
1512
Richard Weinberger1b4ac2a2011-05-24 00:18:05 +02001513void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001514{
Oleg Nesterovd5196502009-12-15 16:47:21 -08001515 bool step;
1516
Eric Parisd7e75282012-01-03 14:23:06 -05001517 audit_syscall_exit(regs);
Roland McGrath86976cd2008-01-30 13:31:01 +01001518
Josh Stone66700002009-08-24 14:43:11 -07001519 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001520 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001521
Roland McGrathd4d67152008-07-09 02:38:07 -07001522 /*
1523 * If TIF_SYSCALL_EMU is set, we only get here because of
1524 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1525 * We already reported this syscall instruction in
Oleg Nesterovd5196502009-12-15 16:47:21 -08001526 * syscall_trace_enter().
Roland McGrathd4d67152008-07-09 02:38:07 -07001527 */
Oleg Nesterovd5196502009-12-15 16:47:21 -08001528 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1529 !test_thread_flag(TIF_SYSCALL_EMU);
1530 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1531 tracehook_report_syscall_exit(regs, step);
Frederic Weisbeckerbf5a3c12012-07-11 20:26:34 +02001532
1533 rcu_user_enter();
Roland McGrathd4d67152008-07-09 02:38:07 -07001534}