blob: caffb6809452f0a2da3de34fecf240c64dcb757e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* By Ross Biro 1/23/92 */
2/*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
Markus Metzgereee3af42008-01-30 13:31:09 +01005 *
6 * BTS tracing
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/ptrace.h>
Roland McGrath91e7b702008-01-30 13:31:52 +010016#include <linux/regset.h>
Roland McGratheeea3c32008-03-16 23:36:28 -070017#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/user.h>
Roland McGrath070459d2008-01-30 13:31:53 +010019#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/security.h>
21#include <linux/audit.h>
22#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070023#include <linux/signal.h>
Markus Metzgere2b371f2009-04-03 16:43:35 +020024#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/uaccess.h>
27#include <asm/pgtable.h>
28#include <asm/system.h>
29#include <asm/processor.h>
30#include <asm/i387.h>
31#include <asm/debugreg.h>
32#include <asm/ldt.h>
33#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010034#include <asm/prctl.h>
35#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010036#include <asm/ds.h>
37
Roland McGrath070459d2008-01-30 13:31:53 +010038#include "tls.h"
39
Josh Stone1c569f02009-08-24 14:43:14 -070040#define CREATE_TRACE_POINTS
41#include <trace/events/syscalls.h>
42
Roland McGrath070459d2008-01-30 13:31:53 +010043enum x86_regset {
44 REGSET_GENERAL,
45 REGSET_FP,
46 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070047 REGSET_IOPERM64 = REGSET_XFP,
Roland McGrath070459d2008-01-30 13:31:53 +010048 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070049 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010050};
Markus Metzgereee3af42008-01-30 13:31:09 +010051
Masami Hiramatsub1cf5402009-08-13 16:34:44 -040052struct pt_regs_offset {
53 const char *name;
54 int offset;
55};
56
57#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
58#define REG_OFFSET_END {.name = NULL, .offset = 0}
59
60static const struct pt_regs_offset regoffset_table[] = {
61#ifdef CONFIG_X86_64
62 REG_OFFSET_NAME(r15),
63 REG_OFFSET_NAME(r14),
64 REG_OFFSET_NAME(r13),
65 REG_OFFSET_NAME(r12),
66 REG_OFFSET_NAME(r11),
67 REG_OFFSET_NAME(r10),
68 REG_OFFSET_NAME(r9),
69 REG_OFFSET_NAME(r8),
70#endif
71 REG_OFFSET_NAME(bx),
72 REG_OFFSET_NAME(cx),
73 REG_OFFSET_NAME(dx),
74 REG_OFFSET_NAME(si),
75 REG_OFFSET_NAME(di),
76 REG_OFFSET_NAME(bp),
77 REG_OFFSET_NAME(ax),
78#ifdef CONFIG_X86_32
79 REG_OFFSET_NAME(ds),
80 REG_OFFSET_NAME(es),
81 REG_OFFSET_NAME(fs),
82 REG_OFFSET_NAME(gs),
83#endif
84 REG_OFFSET_NAME(orig_ax),
85 REG_OFFSET_NAME(ip),
86 REG_OFFSET_NAME(cs),
87 REG_OFFSET_NAME(flags),
88 REG_OFFSET_NAME(sp),
89 REG_OFFSET_NAME(ss),
90 REG_OFFSET_END,
91};
92
93/**
94 * regs_query_register_offset() - query register offset from its name
95 * @name: the name of a register
96 *
97 * regs_query_register_offset() returns the offset of a register in struct
98 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
99 */
100int regs_query_register_offset(const char *name)
101{
102 const struct pt_regs_offset *roff;
103 for (roff = regoffset_table; roff->name != NULL; roff++)
104 if (!strcmp(roff->name, name))
105 return roff->offset;
106 return -EINVAL;
107}
108
109/**
110 * regs_query_register_name() - query register name from its offset
111 * @offset: the offset of a register in struct pt_regs.
112 *
113 * regs_query_register_name() returns the name of a register from its
114 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
115 */
116const char *regs_query_register_name(unsigned int offset)
117{
118 const struct pt_regs_offset *roff;
119 for (roff = regoffset_table; roff->name != NULL; roff++)
120 if (roff->offset == offset)
121 return roff->name;
122 return NULL;
123}
124
125static const int arg_offs_table[] = {
126#ifdef CONFIG_X86_32
127 [0] = offsetof(struct pt_regs, ax),
128 [1] = offsetof(struct pt_regs, dx),
129 [2] = offsetof(struct pt_regs, cx)
130#else /* CONFIG_X86_64 */
131 [0] = offsetof(struct pt_regs, di),
132 [1] = offsetof(struct pt_regs, si),
133 [2] = offsetof(struct pt_regs, dx),
134 [3] = offsetof(struct pt_regs, cx),
135 [4] = offsetof(struct pt_regs, r8),
136 [5] = offsetof(struct pt_regs, r9)
137#endif
138};
139
140/**
141 * regs_get_argument_nth() - get Nth argument at function call
142 * @regs: pt_regs which contains registers at function entry.
143 * @n: argument number.
144 *
145 * regs_get_argument_nth() returns @n th argument of a function call.
146 * Since usually the kernel stack will be changed right after function entry,
147 * you must use this at function entry. If the @n th entry is NOT in the
148 * kernel stack or pt_regs, this returns 0.
149 */
150unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
151{
152 if (n < ARRAY_SIZE(arg_offs_table))
Masami Hiramatsuad5cafc2009-09-10 19:53:06 -0400153 return *(unsigned long *)((char *)regs + arg_offs_table[n]);
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400154 else {
155 /*
156 * The typical case: arg n is on the stack.
157 * (Note: stack[0] = return address, so skip it)
158 */
159 n -= ARRAY_SIZE(arg_offs_table);
160 return regs_get_kernel_stack_nth(regs, 1 + n);
161 }
162}
163
Markus Metzgereee3af42008-01-30 13:31:09 +0100164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * does not yet catch signals sent when the child dies.
166 * in exit.c or in signal.c.
167 */
168
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500169/*
170 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500171 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100172#define FLAG_MASK_32 ((unsigned long) \
173 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
174 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
175 X86_EFLAGS_SF | X86_EFLAGS_TF | \
176 X86_EFLAGS_DF | X86_EFLAGS_OF | \
177 X86_EFLAGS_RF | X86_EFLAGS_AC))
178
Roland McGrath2047b082008-01-30 13:31:01 +0100179/*
180 * Determines whether a value may be installed in a segment register.
181 */
182static inline bool invalid_selector(u16 value)
183{
184 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
185}
186
187#ifdef CONFIG_X86_32
188
Roland McGrathe39c2892008-01-30 13:31:01 +0100189#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530191static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100193 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900194 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Roland McGrath06ee1b62008-01-30 13:31:01 +0100197static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100199 /*
200 * Returning the value truncates it to 16 bits.
201 */
202 unsigned int retval;
203 if (offset != offsetof(struct user_regs_struct, gs))
204 retval = *pt_regs_access(task_pt_regs(task), offset);
205 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100206 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900207 retval = get_user_gs(task_pt_regs(task));
208 else
209 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100210 }
211 return retval;
212}
213
214static int set_segment_reg(struct task_struct *task,
215 unsigned long offset, u16 value)
216{
217 /*
218 * The value argument was already truncated to 16 bits.
219 */
Roland McGrath2047b082008-01-30 13:31:01 +0100220 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100221 return -EIO;
222
Roland McGrathc63855d2008-02-06 22:39:44 +0100223 /*
224 * For %cs and %ss we cannot permit a null selector.
225 * We can permit a bogus selector as long as it has USER_RPL.
226 * Null selectors are fine for other segment registers, but
227 * we will never get back to user mode with invalid %cs or %ss
228 * and will take the trap in iret instead. Much code relies
229 * on user_mode() to distinguish a user trap frame (which can
230 * safely use invalid selectors) from a kernel trap frame.
231 */
232 switch (offset) {
233 case offsetof(struct user_regs_struct, cs):
234 case offsetof(struct user_regs_struct, ss):
235 if (unlikely(value == 0))
236 return -EIO;
237
238 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100239 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100240 break;
241
242 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100243 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900244 set_user_gs(task_pt_regs(task), value);
245 else
246 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250}
251
Roland McGrath2047b082008-01-30 13:31:01 +0100252static unsigned long debugreg_addr_limit(struct task_struct *task)
253{
254 return TASK_SIZE - 3;
255}
256
257#else /* CONFIG_X86_64 */
258
259#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
260
261static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
262{
263 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
264 return &regs->r15 + (offset / sizeof(regs->r15));
265}
266
267static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
268{
269 /*
270 * Returning the value truncates it to 16 bits.
271 */
272 unsigned int seg;
273
274 switch (offset) {
275 case offsetof(struct user_regs_struct, fs):
276 if (task == current) {
277 /* Older gas can't assemble movq %?s,%r?? */
278 asm("movl %%fs,%0" : "=r" (seg));
279 return seg;
280 }
281 return task->thread.fsindex;
282 case offsetof(struct user_regs_struct, gs):
283 if (task == current) {
284 asm("movl %%gs,%0" : "=r" (seg));
285 return seg;
286 }
287 return task->thread.gsindex;
288 case offsetof(struct user_regs_struct, ds):
289 if (task == current) {
290 asm("movl %%ds,%0" : "=r" (seg));
291 return seg;
292 }
293 return task->thread.ds;
294 case offsetof(struct user_regs_struct, es):
295 if (task == current) {
296 asm("movl %%es,%0" : "=r" (seg));
297 return seg;
298 }
299 return task->thread.es;
300
301 case offsetof(struct user_regs_struct, cs):
302 case offsetof(struct user_regs_struct, ss):
303 break;
304 }
305 return *pt_regs_access(task_pt_regs(task), offset);
306}
307
308static int set_segment_reg(struct task_struct *task,
309 unsigned long offset, u16 value)
310{
311 /*
312 * The value argument was already truncated to 16 bits.
313 */
314 if (invalid_selector(value))
315 return -EIO;
316
317 switch (offset) {
318 case offsetof(struct user_regs_struct,fs):
319 /*
320 * If this is setting fs as for normal 64-bit use but
321 * setting fs_base has implicitly changed it, leave it.
322 */
323 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
324 task->thread.fs != 0) ||
325 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
326 task->thread.fs == 0))
327 break;
328 task->thread.fsindex = value;
329 if (task == current)
330 loadsegment(fs, task->thread.fsindex);
331 break;
332 case offsetof(struct user_regs_struct,gs):
333 /*
334 * If this is setting gs as for normal 64-bit use but
335 * setting gs_base has implicitly changed it, leave it.
336 */
337 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
338 task->thread.gs != 0) ||
339 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
340 task->thread.gs == 0))
341 break;
342 task->thread.gsindex = value;
343 if (task == current)
344 load_gs_index(task->thread.gsindex);
345 break;
346 case offsetof(struct user_regs_struct,ds):
347 task->thread.ds = value;
348 if (task == current)
349 loadsegment(ds, task->thread.ds);
350 break;
351 case offsetof(struct user_regs_struct,es):
352 task->thread.es = value;
353 if (task == current)
354 loadsegment(es, task->thread.es);
355 break;
356
357 /*
358 * Can't actually change these in 64-bit mode.
359 */
360 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100361 if (unlikely(value == 0))
362 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100363#ifdef CONFIG_IA32_EMULATION
364 if (test_tsk_thread_flag(task, TIF_IA32))
365 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100366#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100367 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100368 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100369 if (unlikely(value == 0))
370 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100371#ifdef CONFIG_IA32_EMULATION
372 if (test_tsk_thread_flag(task, TIF_IA32))
373 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100374#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100375 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100376 }
377
378 return 0;
379}
380
381static unsigned long debugreg_addr_limit(struct task_struct *task)
382{
383#ifdef CONFIG_IA32_EMULATION
384 if (test_tsk_thread_flag(task, TIF_IA32))
385 return IA32_PAGE_OFFSET - 3;
386#endif
Ingo Molnard9517342009-02-20 23:32:28 +0100387 return TASK_SIZE_MAX - 7;
Roland McGrath2047b082008-01-30 13:31:01 +0100388}
389
390#endif /* CONFIG_X86_32 */
391
Roland McGrath06ee1b62008-01-30 13:31:01 +0100392static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100394 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Roland McGrath06ee1b62008-01-30 13:31:01 +0100396 /*
397 * If the debugger set TF, hide it from the readout.
398 */
399 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
400 retval &= ~X86_EFLAGS_TF;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return retval;
403}
404
Roland McGrath06ee1b62008-01-30 13:31:01 +0100405static int set_flags(struct task_struct *task, unsigned long value)
406{
407 struct pt_regs *regs = task_pt_regs(task);
408
409 /*
410 * If the user value contains TF, mark that
411 * it was not "us" (the debugger) that set it.
412 * If not, make sure it stays set if we had.
413 */
414 if (value & X86_EFLAGS_TF)
415 clear_tsk_thread_flag(task, TIF_FORCED_TF);
416 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
417 value |= X86_EFLAGS_TF;
418
419 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
420
421 return 0;
422}
423
424static int putreg(struct task_struct *child,
425 unsigned long offset, unsigned long value)
426{
427 switch (offset) {
428 case offsetof(struct user_regs_struct, cs):
429 case offsetof(struct user_regs_struct, ds):
430 case offsetof(struct user_regs_struct, es):
431 case offsetof(struct user_regs_struct, fs):
432 case offsetof(struct user_regs_struct, gs):
433 case offsetof(struct user_regs_struct, ss):
434 return set_segment_reg(child, offset, value);
435
436 case offsetof(struct user_regs_struct, flags):
437 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100438
439#ifdef CONFIG_X86_64
Roland McGrath84c6f602008-03-07 14:56:02 -0800440 /*
441 * Orig_ax is really just a flag with small positive and
442 * negative values, so make sure to always sign-extend it
443 * from 32 bits so that it works correctly regardless of
444 * whether we come from a 32-bit environment or not.
445 */
446 case offsetof(struct user_regs_struct, orig_ax):
447 value = (long) (s32) value;
448 break;
449
Roland McGrath2047b082008-01-30 13:31:01 +0100450 case offsetof(struct user_regs_struct,fs_base):
451 if (value >= TASK_SIZE_OF(child))
452 return -EIO;
453 /*
454 * When changing the segment base, use do_arch_prctl
455 * to set either thread.fs or thread.fsindex and the
456 * corresponding GDT slot.
457 */
458 if (child->thread.fs != value)
459 return do_arch_prctl(child, ARCH_SET_FS, value);
460 return 0;
461 case offsetof(struct user_regs_struct,gs_base):
462 /*
463 * Exactly the same here as the %fs handling above.
464 */
465 if (value >= TASK_SIZE_OF(child))
466 return -EIO;
467 if (child->thread.gs != value)
468 return do_arch_prctl(child, ARCH_SET_GS, value);
469 return 0;
470#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100471 }
472
473 *pt_regs_access(task_pt_regs(child), offset) = value;
474 return 0;
475}
476
477static unsigned long getreg(struct task_struct *task, unsigned long offset)
478{
479 switch (offset) {
480 case offsetof(struct user_regs_struct, cs):
481 case offsetof(struct user_regs_struct, ds):
482 case offsetof(struct user_regs_struct, es):
483 case offsetof(struct user_regs_struct, fs):
484 case offsetof(struct user_regs_struct, gs):
485 case offsetof(struct user_regs_struct, ss):
486 return get_segment_reg(task, offset);
487
488 case offsetof(struct user_regs_struct, flags):
489 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100490
491#ifdef CONFIG_X86_64
492 case offsetof(struct user_regs_struct, fs_base): {
493 /*
494 * do_arch_prctl may have used a GDT slot instead of
495 * the MSR. To userland, it appears the same either
496 * way, except the %fs segment selector might not be 0.
497 */
498 unsigned int seg = task->thread.fsindex;
499 if (task->thread.fs != 0)
500 return task->thread.fs;
501 if (task == current)
502 asm("movl %%fs,%0" : "=r" (seg));
503 if (seg != FS_TLS_SEL)
504 return 0;
505 return get_desc_base(&task->thread.tls_array[FS_TLS]);
506 }
507 case offsetof(struct user_regs_struct, gs_base): {
508 /*
509 * Exactly the same here as the %fs handling above.
510 */
511 unsigned int seg = task->thread.gsindex;
512 if (task->thread.gs != 0)
513 return task->thread.gs;
514 if (task == current)
515 asm("movl %%gs,%0" : "=r" (seg));
516 if (seg != GS_TLS_SEL)
517 return 0;
518 return get_desc_base(&task->thread.tls_array[GS_TLS]);
519 }
520#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100521 }
522
523 return *pt_regs_access(task_pt_regs(task), offset);
524}
525
Roland McGrath91e7b702008-01-30 13:31:52 +0100526static int genregs_get(struct task_struct *target,
527 const struct user_regset *regset,
528 unsigned int pos, unsigned int count,
529 void *kbuf, void __user *ubuf)
530{
531 if (kbuf) {
532 unsigned long *k = kbuf;
533 while (count > 0) {
534 *k++ = getreg(target, pos);
535 count -= sizeof(*k);
536 pos += sizeof(*k);
537 }
538 } else {
539 unsigned long __user *u = ubuf;
540 while (count > 0) {
541 if (__put_user(getreg(target, pos), u++))
542 return -EFAULT;
543 count -= sizeof(*u);
544 pos += sizeof(*u);
545 }
546 }
547
548 return 0;
549}
550
551static int genregs_set(struct task_struct *target,
552 const struct user_regset *regset,
553 unsigned int pos, unsigned int count,
554 const void *kbuf, const void __user *ubuf)
555{
556 int ret = 0;
557 if (kbuf) {
558 const unsigned long *k = kbuf;
559 while (count > 0 && !ret) {
560 ret = putreg(target, pos, *k++);
561 count -= sizeof(*k);
562 pos += sizeof(*k);
563 }
564 } else {
565 const unsigned long __user *u = ubuf;
566 while (count > 0 && !ret) {
567 unsigned long word;
568 ret = __get_user(word, u++);
569 if (ret)
570 break;
571 ret = putreg(target, pos, word);
572 count -= sizeof(*u);
573 pos += sizeof(*u);
574 }
575 }
576 return ret;
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100580 * This function is trivial and will be inlined by the compiler.
581 * Having it separates the implementation details of debug
582 * registers from the interface details of ptrace.
583 */
584static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
585{
Roland McGrath0f534092008-01-30 13:30:59 +0100586 switch (n) {
587 case 0: return child->thread.debugreg0;
588 case 1: return child->thread.debugreg1;
589 case 2: return child->thread.debugreg2;
590 case 3: return child->thread.debugreg3;
591 case 6: return child->thread.debugreg6;
592 case 7: return child->thread.debugreg7;
593 }
594 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100595}
596
597static int ptrace_set_debugreg(struct task_struct *child,
598 int n, unsigned long data)
599{
Roland McGrath0f534092008-01-30 13:30:59 +0100600 int i;
601
Roland McGrathd9771e82008-01-30 13:30:52 +0100602 if (unlikely(n == 4 || n == 5))
603 return -EIO;
604
Roland McGrath2047b082008-01-30 13:31:01 +0100605 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100606 return -EIO;
607
Roland McGrath0f534092008-01-30 13:30:59 +0100608 switch (n) {
609 case 0: child->thread.debugreg0 = data; break;
610 case 1: child->thread.debugreg1 = data; break;
611 case 2: child->thread.debugreg2 = data; break;
612 case 3: child->thread.debugreg3 = data; break;
613
614 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100615 if ((data & ~0xffffffffUL) != 0)
616 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100617 child->thread.debugreg6 = data;
618 break;
619
620 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100621 /*
622 * Sanity-check data. Take one half-byte at once with
623 * check = (val >> (16 + 4*i)) & 0xf. It contains the
624 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
625 * 2 and 3 are LENi. Given a list of invalid values,
626 * we do mask |= 1 << invalid_value, so that
627 * (mask >> check) & 1 is a correct test for invalid
628 * values.
629 *
630 * R/Wi contains the type of the breakpoint /
631 * watchpoint, LENi contains the length of the watched
632 * data in the watchpoint case.
633 *
634 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100635 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100636 * - R/Wi == 0x10 (break on I/O reads or writes), so
637 * mask |= 0x4444.
638 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
639 * 0x1110.
640 *
641 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
642 *
643 * See the Intel Manual "System Programming Guide",
644 * 15.2.4
645 *
646 * Note that LENi == 0x10 is defined on x86_64 in long
647 * mode (i.e. even for 32-bit userspace software, but
648 * 64-bit kernel), so the x86_64 mask value is 0x5454.
649 * See the AMD manual no. 24593 (AMD64 System Programming)
650 */
Roland McGrath2047b082008-01-30 13:31:01 +0100651#ifdef CONFIG_X86_32
652#define DR7_MASK 0x5f54
653#else
654#define DR7_MASK 0x5554
655#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100656 data &= ~DR_CONTROL_RESERVED;
657 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100658 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100659 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100660 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100661 if (data)
662 set_tsk_thread_flag(child, TIF_DEBUG);
663 else
664 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100665 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100666 }
667
Roland McGrathd9771e82008-01-30 13:30:52 +0100668 return 0;
669}
670
Roland McGrath325af5f2008-08-08 15:58:39 -0700671/*
672 * These access the current or another (stopped) task's io permission
673 * bitmap for debugging or core dump.
674 */
675static int ioperm_active(struct task_struct *target,
676 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100677{
Roland McGrath325af5f2008-08-08 15:58:39 -0700678 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100679}
680
Roland McGrath325af5f2008-08-08 15:58:39 -0700681static int ioperm_get(struct task_struct *target,
682 const struct user_regset *regset,
683 unsigned int pos, unsigned int count,
684 void *kbuf, void __user *ubuf)
685{
686 if (!target->thread.io_bitmap_ptr)
687 return -ENXIO;
688
689 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
690 target->thread.io_bitmap_ptr,
691 0, IO_BITMAP_BYTES);
692}
693
Markus Metzger93fa7632008-04-08 11:01:58 +0200694#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200695/*
696 * A branch trace store context.
697 *
698 * Contexts may only be installed by ptrace_bts_config() and only for
699 * ptraced tasks.
700 *
701 * Contexts are destroyed when the tracee is detached from the tracer.
702 * The actual destruction work requires interrupts enabled, so the
703 * work is deferred and will be scheduled during __ptrace_unlink().
704 *
705 * Contexts hold an additional task_struct reference on the traced
706 * task, as well as a reference on the tracer's mm.
707 *
708 * Ptrace already holds a task_struct for the duration of ptrace operations,
709 * but since destruction is deferred, it may be executed after both
710 * tracer and tracee exited.
711 */
712struct bts_context {
713 /* The branch trace handle. */
714 struct bts_tracer *tracer;
715
716 /* The buffer used to store the branch trace and its size. */
717 void *buffer;
718 unsigned int size;
719
720 /* The mm that paid for the above buffer. */
721 struct mm_struct *mm;
722
723 /* The task this context belongs to. */
724 struct task_struct *task;
725
726 /* The signal to send on a bts buffer overflow. */
727 unsigned int bts_ovfl_signal;
728
729 /* The work struct to destroy a context. */
730 struct work_struct work;
731};
732
Markus Metzger1cb81b12009-04-24 09:51:43 +0200733static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200734{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200735 void *buffer = NULL;
736 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200737
Markus Metzger1cb81b12009-04-24 09:51:43 +0200738 err = account_locked_memory(current->mm, current->signal->rlim, size);
739 if (err < 0)
740 return err;
741
742 buffer = kzalloc(size, GFP_KERNEL);
743 if (!buffer)
744 goto out_refund;
745
746 context->buffer = buffer;
747 context->size = size;
748 context->mm = get_task_mm(current);
749
750 return 0;
751
752 out_refund:
753 refund_locked_memory(current->mm, size);
754 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200755}
756
757static inline void free_bts_buffer(struct bts_context *context)
758{
759 if (!context->buffer)
760 return;
761
762 kfree(context->buffer);
763 context->buffer = NULL;
764
Markus Metzger1cb81b12009-04-24 09:51:43 +0200765 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200766 context->size = 0;
767
768 mmput(context->mm);
769 context->mm = NULL;
770}
771
772static void free_bts_context_work(struct work_struct *w)
773{
774 struct bts_context *context;
775
776 context = container_of(w, struct bts_context, work);
777
778 ds_release_bts(context->tracer);
779 put_task_struct(context->task);
780 free_bts_buffer(context);
781 kfree(context);
782}
783
784static inline void free_bts_context(struct bts_context *context)
785{
786 INIT_WORK(&context->work, free_bts_context_work);
787 schedule_work(&context->work);
788}
789
790static inline struct bts_context *alloc_bts_context(struct task_struct *task)
791{
792 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
793 if (context) {
794 context->task = task;
795 task->bts = context;
796
797 get_task_struct(task);
798 }
799
800 return context;
801}
802
Markus Metzger93fa7632008-04-08 11:01:58 +0200803static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100804 struct bts_struct __user *out)
805{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200806 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100807 const struct bts_trace *trace;
808 struct bts_struct bts;
809 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200810 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100811
Markus Metzgere2b371f2009-04-03 16:43:35 +0200812 context = child->bts;
813 if (!context)
814 return -ESRCH;
815
816 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100817 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200818 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100819
820 at = trace->ds.top - ((index + 1) * trace->ds.size);
821 if ((void *)at < trace->ds.begin)
822 at += (trace->ds.n * trace->ds.size);
823
824 if (!trace->read)
825 return -EOPNOTSUPP;
826
Markus Metzgere2b371f2009-04-03 16:43:35 +0200827 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200828 if (error < 0)
829 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100830
Markus Metzgerc2724772008-12-11 13:49:59 +0100831 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100832 return -EFAULT;
833
Markus Metzgerc2724772008-12-11 13:49:59 +0100834 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100835}
836
Markus Metzgera95d67f2008-01-30 13:31:20 +0100837static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100838 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100839 struct bts_struct __user *out)
840{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200841 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100842 const struct bts_trace *trace;
843 const unsigned char *at;
844 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100845
Markus Metzgere2b371f2009-04-03 16:43:35 +0200846 context = child->bts;
847 if (!context)
848 return -ESRCH;
849
850 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100851 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200852 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100853
Markus Metzgerc2724772008-12-11 13:49:59 +0100854 if (!trace->read)
855 return -EOPNOTSUPP;
856
857 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100858 return -EIO;
859
Markus Metzgerc2724772008-12-11 13:49:59 +0100860 for (at = trace->ds.begin; (void *)at < trace->ds.top;
861 out++, drained++, at += trace->ds.size) {
862 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100863
Markus Metzgere2b371f2009-04-03 16:43:35 +0200864 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100865 if (error < 0)
866 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100867
Markus Metzgerc2724772008-12-11 13:49:59 +0100868 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100869 return -EFAULT;
870 }
871
Markus Metzgerc2724772008-12-11 13:49:59 +0100872 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
873
Markus Metzgere2b371f2009-04-03 16:43:35 +0200874 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200875 if (error < 0)
876 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100877
Markus Metzgerc2724772008-12-11 13:49:59 +0100878 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100879}
880
881static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100882 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100883 const struct ptrace_bts_config __user *ucfg)
884{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200885 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100886 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100887 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100888
Markus Metzgercba4b652008-01-30 13:32:03 +0100889 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100890 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100891
Markus Metzgera95d67f2008-01-30 13:31:20 +0100892 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100893 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100894
Markus Metzgere2b371f2009-04-03 16:43:35 +0200895 context = child->bts;
896 if (!context)
897 context = alloc_bts_context(child);
898 if (!context)
899 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100900
Markus Metzgerc2724772008-12-11 13:49:59 +0100901 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
902 if (!cfg.signal)
903 return -EINVAL;
904
Américo Wang5a8ac9d2009-03-13 15:56:58 +0800905 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200906 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +0100907 }
908
Markus Metzgere2b371f2009-04-03 16:43:35 +0200909 ds_release_bts(context->tracer);
910 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100911
Markus Metzgere2b371f2009-04-03 16:43:35 +0200912 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +0200913 int err;
914
Markus Metzgere2b371f2009-04-03 16:43:35 +0200915 free_bts_buffer(context);
916 if (!cfg.size)
917 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +0100918
Markus Metzger1cb81b12009-04-24 09:51:43 +0200919 err = alloc_bts_buffer(context, cfg.size);
920 if (err < 0)
921 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +0100922 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100923
Markus Metzgerda35c372008-01-30 13:32:03 +0100924 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +0100925 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +0100926
Markus Metzgerda35c372008-01-30 13:32:03 +0100927 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +0100928 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +0100929
Markus Metzgerde79f542009-04-03 16:43:40 +0200930 context->tracer =
931 ds_request_bts_task(child, context->buffer, context->size,
932 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200933 if (unlikely(IS_ERR(context->tracer))) {
934 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +0100935
Markus Metzgere2b371f2009-04-03 16:43:35 +0200936 free_bts_buffer(context);
937 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100938 return error;
939 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100940
Markus Metzgerc2724772008-12-11 13:49:59 +0100941 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100942}
943
Markus Metzgera95d67f2008-01-30 13:31:20 +0100944static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100945 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100946 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100947{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200948 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100949 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100950 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100951
Markus Metzgere2b371f2009-04-03 16:43:35 +0200952 context = child->bts;
953 if (!context)
954 return -ESRCH;
955
Markus Metzgercba4b652008-01-30 13:32:03 +0100956 if (cfg_size < sizeof(cfg))
957 return -EIO;
958
Markus Metzgere2b371f2009-04-03 16:43:35 +0200959 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100960 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200961 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +0200962
Markus Metzgera95d67f2008-01-30 13:31:20 +0100963 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +0200964 cfg.size = trace->ds.end - trace->ds.begin;
965 cfg.signal = context->bts_ovfl_signal;
966 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +0100967
Markus Metzger93fa7632008-04-08 11:01:58 +0200968 if (cfg.signal)
969 cfg.flags |= PTRACE_BTS_O_SIGNAL;
970
Markus Metzgerc2724772008-12-11 13:49:59 +0100971 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +0200972 cfg.flags |= PTRACE_BTS_O_TRACE;
973
Markus Metzgerc2724772008-12-11 13:49:59 +0100974 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +0200975 cfg.flags |= PTRACE_BTS_O_SCHED;
976
Markus Metzgera95d67f2008-01-30 13:31:20 +0100977 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
978 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100979
Markus Metzgera95d67f2008-01-30 13:31:20 +0100980 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100981}
982
Markus Metzgerc2724772008-12-11 13:49:59 +0100983static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -0800984{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200985 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100986 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800987
Markus Metzgere2b371f2009-04-03 16:43:35 +0200988 context = child->bts;
989 if (!context)
990 return -ESRCH;
991
992 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100993 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200994 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800995
Markus Metzgerc2724772008-12-11 13:49:59 +0100996 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800997
Markus Metzgere2b371f2009-04-03 16:43:35 +0200998 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800999}
1000
Markus Metzgerc2724772008-12-11 13:49:59 +01001001static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +01001002{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001003 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001004 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +01001005
Markus Metzgere2b371f2009-04-03 16:43:35 +02001006 context = child->bts;
1007 if (!context)
1008 return -ESRCH;
1009
1010 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001011 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001012 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001013
Markus Metzgerc2724772008-12-11 13:49:59 +01001014 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +02001015}
Markus Metzgerbf53de92008-12-19 15:10:24 +01001016
Markus Metzgere2b371f2009-04-03 16:43:35 +02001017/*
1018 * Called from __ptrace_unlink() after the child has been moved back
1019 * to its original parent.
1020 */
Markus Metzger0f481402009-04-03 16:43:48 +02001021void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +01001022{
1023 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +02001024 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +01001025 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +01001026 }
1027}
Markus Metzger93fa7632008-04-08 11:01:58 +02001028#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001029
Roland McGrathd9771e82008-01-30 13:30:52 +01001030/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * Called by kernel/ptrace.c when detaching..
1032 *
1033 * Make sure the single step bit is not set.
1034 */
1035void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +01001036{
Roland McGrath7f232342008-01-30 13:30:48 +01001037 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001038#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -07001039 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Roland McGrath5a4646a2008-01-30 13:31:54 +01001043#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1044static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1045#endif
1046
Christoph Hellwig481bed42005-11-07 00:59:47 -08001047long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Roland McGrath5a4646a2008-01-30 13:31:54 +01001049 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 unsigned long __user *datap = (unsigned long __user *)data;
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* read the word at location addr in the USER area. */
1054 case PTRACE_PEEKUSR: {
1055 unsigned long tmp;
1056
1057 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001058 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1059 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
1061
1062 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001063 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001065 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1066 addr <= offsetof(struct user, u_debugreg[7])) {
1067 addr -= offsetof(struct user, u_debugreg[0]);
1068 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 ret = put_user(tmp, datap);
1071 break;
1072 }
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1075 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001076 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1077 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 break;
1079
Roland McGrathe9c86c72008-01-30 13:31:01 +01001080 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001082 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1083 addr <= offsetof(struct user, u_debugreg[7])) {
1084 addr -= offsetof(struct user, u_debugreg[0]);
1085 ret = ptrace_set_debugreg(child,
1086 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Roland McGrath5a4646a2008-01-30 13:31:54 +01001090 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1091 return copy_regset_to_user(child,
1092 task_user_regset_view(current),
1093 REGSET_GENERAL,
1094 0, sizeof(struct user_regs_struct),
1095 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Roland McGrath5a4646a2008-01-30 13:31:54 +01001097 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1098 return copy_regset_from_user(child,
1099 task_user_regset_view(current),
1100 REGSET_GENERAL,
1101 0, sizeof(struct user_regs_struct),
1102 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Roland McGrath5a4646a2008-01-30 13:31:54 +01001104 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1105 return copy_regset_to_user(child,
1106 task_user_regset_view(current),
1107 REGSET_FP,
1108 0, sizeof(struct user_i387_struct),
1109 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Roland McGrath5a4646a2008-01-30 13:31:54 +01001111 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1112 return copy_regset_from_user(child,
1113 task_user_regset_view(current),
1114 REGSET_FP,
1115 0, sizeof(struct user_i387_struct),
1116 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Roland McGrathe9c86c72008-01-30 13:31:01 +01001118#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001119 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1120 return copy_regset_to_user(child, &user_x86_32_view,
1121 REGSET_XFP,
1122 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001123 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Roland McGrath5a4646a2008-01-30 13:31:54 +01001125 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1126 return copy_regset_from_user(child, &user_x86_32_view,
1127 REGSET_XFP,
1128 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001129 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Roland McGrathe9c86c72008-01-30 13:31:01 +01001132#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001134 if (addr < 0)
1135 return -EIO;
1136 ret = do_get_thread_area(child, addr,
1137 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
1139
1140 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001141 if (addr < 0)
1142 return -EIO;
1143 ret = do_set_thread_area(child, addr,
1144 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001146#endif
1147
1148#ifdef CONFIG_X86_64
1149 /* normal 64bit interface to access TLS data.
1150 Works just like arch_prctl, except that the arguments
1151 are reversed. */
1152 case PTRACE_ARCH_PRCTL:
1153 ret = do_arch_prctl(child, data, addr);
1154 break;
1155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001157 /*
1158 * These bits need more cooking - not enabled yet:
1159 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001160#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001161 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001162 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001163 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001164 break;
1165
1166 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001167 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001168 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001169 break;
1170
Markus Metzgerc2724772008-12-11 13:49:59 +01001171 case PTRACE_BTS_SIZE:
1172 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001173 break;
1174
1175 case PTRACE_BTS_GET:
1176 ret = ptrace_bts_read_record
1177 (child, data, (struct bts_struct __user *) addr);
1178 break;
1179
1180 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001181 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001182 break;
1183
1184 case PTRACE_BTS_DRAIN:
1185 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001186 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001187 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001188#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 default:
1191 ret = ptrace_request(child, request, addr, data);
1192 break;
1193 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return ret;
1196}
1197
Roland McGrathcb757c42008-01-30 13:31:01 +01001198#ifdef CONFIG_IA32_EMULATION
1199
Roland McGrath099cd6e2008-01-30 13:31:01 +01001200#include <linux/compat.h>
1201#include <linux/syscalls.h>
1202#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001203#include <asm/user32.h>
1204
1205#define R32(l,q) \
1206 case offsetof(struct user32, regs.l): \
1207 regs->q = value; break
1208
1209#define SEG32(rs) \
1210 case offsetof(struct user32, regs.rs): \
1211 return set_segment_reg(child, \
1212 offsetof(struct user_regs_struct, rs), \
1213 value); \
1214 break
1215
1216static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1217{
1218 struct pt_regs *regs = task_pt_regs(child);
1219
1220 switch (regno) {
1221
1222 SEG32(cs);
1223 SEG32(ds);
1224 SEG32(es);
1225 SEG32(fs);
1226 SEG32(gs);
1227 SEG32(ss);
1228
1229 R32(ebx, bx);
1230 R32(ecx, cx);
1231 R32(edx, dx);
1232 R32(edi, di);
1233 R32(esi, si);
1234 R32(ebp, bp);
1235 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001236 R32(eip, ip);
1237 R32(esp, sp);
1238
Roland McGrath40f09332008-02-28 19:57:07 -08001239 case offsetof(struct user32, regs.orig_eax):
1240 /*
1241 * Sign-extend the value so that orig_eax = -1
1242 * causes (long)orig_ax < 0 tests to fire correctly.
1243 */
1244 regs->orig_ax = (long) (s32) value;
1245 break;
1246
Roland McGrathcb757c42008-01-30 13:31:01 +01001247 case offsetof(struct user32, regs.eflags):
1248 return set_flags(child, value);
1249
1250 case offsetof(struct user32, u_debugreg[0]) ...
1251 offsetof(struct user32, u_debugreg[7]):
1252 regno -= offsetof(struct user32, u_debugreg[0]);
1253 return ptrace_set_debugreg(child, regno / 4, value);
1254
1255 default:
1256 if (regno > sizeof(struct user32) || (regno & 3))
1257 return -EIO;
1258
1259 /*
1260 * Other dummy fields in the virtual user structure
1261 * are ignored
1262 */
1263 break;
1264 }
1265 return 0;
1266}
1267
1268#undef R32
1269#undef SEG32
1270
1271#define R32(l,q) \
1272 case offsetof(struct user32, regs.l): \
1273 *val = regs->q; break
1274
1275#define SEG32(rs) \
1276 case offsetof(struct user32, regs.rs): \
1277 *val = get_segment_reg(child, \
1278 offsetof(struct user_regs_struct, rs)); \
1279 break
1280
1281static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1282{
1283 struct pt_regs *regs = task_pt_regs(child);
1284
1285 switch (regno) {
1286
1287 SEG32(ds);
1288 SEG32(es);
1289 SEG32(fs);
1290 SEG32(gs);
1291
1292 R32(cs, cs);
1293 R32(ss, ss);
1294 R32(ebx, bx);
1295 R32(ecx, cx);
1296 R32(edx, dx);
1297 R32(edi, di);
1298 R32(esi, si);
1299 R32(ebp, bp);
1300 R32(eax, ax);
1301 R32(orig_eax, orig_ax);
1302 R32(eip, ip);
1303 R32(esp, sp);
1304
1305 case offsetof(struct user32, regs.eflags):
1306 *val = get_flags(child);
1307 break;
1308
1309 case offsetof(struct user32, u_debugreg[0]) ...
1310 offsetof(struct user32, u_debugreg[7]):
1311 regno -= offsetof(struct user32, u_debugreg[0]);
1312 *val = ptrace_get_debugreg(child, regno / 4);
1313 break;
1314
1315 default:
1316 if (regno > sizeof(struct user32) || (regno & 3))
1317 return -EIO;
1318
1319 /*
1320 * Other dummy fields in the virtual user structure
1321 * are ignored
1322 */
1323 *val = 0;
1324 break;
1325 }
1326 return 0;
1327}
1328
1329#undef R32
1330#undef SEG32
1331
Roland McGrath91e7b702008-01-30 13:31:52 +01001332static int genregs32_get(struct task_struct *target,
1333 const struct user_regset *regset,
1334 unsigned int pos, unsigned int count,
1335 void *kbuf, void __user *ubuf)
1336{
1337 if (kbuf) {
1338 compat_ulong_t *k = kbuf;
1339 while (count > 0) {
1340 getreg32(target, pos, k++);
1341 count -= sizeof(*k);
1342 pos += sizeof(*k);
1343 }
1344 } else {
1345 compat_ulong_t __user *u = ubuf;
1346 while (count > 0) {
1347 compat_ulong_t word;
1348 getreg32(target, pos, &word);
1349 if (__put_user(word, u++))
1350 return -EFAULT;
1351 count -= sizeof(*u);
1352 pos += sizeof(*u);
1353 }
1354 }
1355
1356 return 0;
1357}
1358
1359static int genregs32_set(struct task_struct *target,
1360 const struct user_regset *regset,
1361 unsigned int pos, unsigned int count,
1362 const void *kbuf, const void __user *ubuf)
1363{
1364 int ret = 0;
1365 if (kbuf) {
1366 const compat_ulong_t *k = kbuf;
1367 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001368 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001369 count -= sizeof(*k);
1370 pos += sizeof(*k);
1371 }
1372 } else {
1373 const compat_ulong_t __user *u = ubuf;
1374 while (count > 0 && !ret) {
1375 compat_ulong_t word;
1376 ret = __get_user(word, u++);
1377 if (ret)
1378 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001379 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001380 count -= sizeof(*u);
1381 pos += sizeof(*u);
1382 }
1383 }
1384 return ret;
1385}
1386
Roland McGrath562b80b2008-04-22 12:21:25 -07001387long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1388 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001389{
Roland McGrath562b80b2008-04-22 12:21:25 -07001390 unsigned long addr = caddr;
1391 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001392 void __user *datap = compat_ptr(data);
1393 int ret;
1394 __u32 val;
1395
1396 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001397 case PTRACE_PEEKUSR:
1398 ret = getreg32(child, addr, &val);
1399 if (ret == 0)
1400 ret = put_user(val, (__u32 __user *)datap);
1401 break;
1402
1403 case PTRACE_POKEUSR:
1404 ret = putreg32(child, addr, data);
1405 break;
1406
Roland McGrath5a4646a2008-01-30 13:31:54 +01001407 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1408 return copy_regset_to_user(child, &user_x86_32_view,
1409 REGSET_GENERAL,
1410 0, sizeof(struct user_regs_struct32),
1411 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001412
Roland McGrath5a4646a2008-01-30 13:31:54 +01001413 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1414 return copy_regset_from_user(child, &user_x86_32_view,
1415 REGSET_GENERAL, 0,
1416 sizeof(struct user_regs_struct32),
1417 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001418
Roland McGrath5a4646a2008-01-30 13:31:54 +01001419 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1420 return copy_regset_to_user(child, &user_x86_32_view,
1421 REGSET_FP, 0,
1422 sizeof(struct user_i387_ia32_struct),
1423 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001424
Roland McGrath5a4646a2008-01-30 13:31:54 +01001425 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1426 return copy_regset_from_user(
1427 child, &user_x86_32_view, REGSET_FP,
1428 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001429
Roland McGrath5a4646a2008-01-30 13:31:54 +01001430 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1431 return copy_regset_to_user(child, &user_x86_32_view,
1432 REGSET_XFP, 0,
1433 sizeof(struct user32_fxsr_struct),
1434 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001435
Roland McGrath5a4646a2008-01-30 13:31:54 +01001436 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1437 return copy_regset_from_user(child, &user_x86_32_view,
1438 REGSET_XFP, 0,
1439 sizeof(struct user32_fxsr_struct),
1440 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001441
Roland McGrath562b80b2008-04-22 12:21:25 -07001442 case PTRACE_GET_THREAD_AREA:
1443 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001444#ifdef CONFIG_X86_PTRACE_BTS
1445 case PTRACE_BTS_CONFIG:
1446 case PTRACE_BTS_STATUS:
1447 case PTRACE_BTS_SIZE:
1448 case PTRACE_BTS_GET:
1449 case PTRACE_BTS_CLEAR:
1450 case PTRACE_BTS_DRAIN:
1451#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001452 return arch_ptrace(child, request, addr, data);
1453
Roland McGrath099cd6e2008-01-30 13:31:01 +01001454 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001455 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001456 }
1457
Roland McGrath099cd6e2008-01-30 13:31:01 +01001458 return ret;
1459}
1460
Roland McGrathcb757c42008-01-30 13:31:01 +01001461#endif /* CONFIG_IA32_EMULATION */
1462
Roland McGrath070459d2008-01-30 13:31:53 +01001463#ifdef CONFIG_X86_64
1464
1465static const struct user_regset x86_64_regsets[] = {
1466 [REGSET_GENERAL] = {
1467 .core_note_type = NT_PRSTATUS,
1468 .n = sizeof(struct user_regs_struct) / sizeof(long),
1469 .size = sizeof(long), .align = sizeof(long),
1470 .get = genregs_get, .set = genregs_set
1471 },
1472 [REGSET_FP] = {
1473 .core_note_type = NT_PRFPREG,
1474 .n = sizeof(struct user_i387_struct) / sizeof(long),
1475 .size = sizeof(long), .align = sizeof(long),
1476 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1477 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001478 [REGSET_IOPERM64] = {
1479 .core_note_type = NT_386_IOPERM,
1480 .n = IO_BITMAP_LONGS,
1481 .size = sizeof(long), .align = sizeof(long),
1482 .active = ioperm_active, .get = ioperm_get
1483 },
Roland McGrath070459d2008-01-30 13:31:53 +01001484};
1485
1486static const struct user_regset_view user_x86_64_view = {
1487 .name = "x86_64", .e_machine = EM_X86_64,
1488 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1489};
1490
1491#else /* CONFIG_X86_32 */
1492
1493#define user_regs_struct32 user_regs_struct
1494#define genregs32_get genregs_get
1495#define genregs32_set genregs_set
1496
Roland McGrath1f465f42008-05-09 15:43:44 -07001497#define user_i387_ia32_struct user_i387_struct
1498#define user32_fxsr_struct user_fxsr_struct
1499
Roland McGrath070459d2008-01-30 13:31:53 +01001500#endif /* CONFIG_X86_64 */
1501
1502#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1503static const struct user_regset x86_32_regsets[] = {
1504 [REGSET_GENERAL] = {
1505 .core_note_type = NT_PRSTATUS,
1506 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1507 .size = sizeof(u32), .align = sizeof(u32),
1508 .get = genregs32_get, .set = genregs32_set
1509 },
1510 [REGSET_FP] = {
1511 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001512 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001513 .size = sizeof(u32), .align = sizeof(u32),
1514 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1515 },
1516 [REGSET_XFP] = {
1517 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001518 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001519 .size = sizeof(u32), .align = sizeof(u32),
1520 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1521 },
1522 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001523 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001524 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1525 .size = sizeof(struct user_desc),
1526 .align = sizeof(struct user_desc),
1527 .active = regset_tls_active,
1528 .get = regset_tls_get, .set = regset_tls_set
1529 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001530 [REGSET_IOPERM32] = {
1531 .core_note_type = NT_386_IOPERM,
1532 .n = IO_BITMAP_BYTES / sizeof(u32),
1533 .size = sizeof(u32), .align = sizeof(u32),
1534 .active = ioperm_active, .get = ioperm_get
1535 },
Roland McGrath070459d2008-01-30 13:31:53 +01001536};
1537
1538static const struct user_regset_view user_x86_32_view = {
1539 .name = "i386", .e_machine = EM_386,
1540 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1541};
1542#endif
1543
1544const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1545{
1546#ifdef CONFIG_IA32_EMULATION
1547 if (test_tsk_thread_flag(task, TIF_IA32))
1548#endif
1549#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1550 return &user_x86_32_view;
1551#endif
1552#ifdef CONFIG_X86_64
1553 return &user_x86_64_view;
1554#endif
1555}
1556
Srinivasa Dsda654b72008-09-23 15:23:52 +05301557void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1558 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 struct siginfo info;
1561
1562 tsk->thread.trap_no = 1;
1563 tsk->thread.error_code = error_code;
1564
1565 memset(&info, 0, sizeof(info));
1566 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301567 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001569 /* User-mode ip? */
1570 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Simon Arlott27b46d72007-10-20 01:13:56 +02001572 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 force_sig_info(SIGTRAP, &info, tsk);
1574}
1575
Roland McGrath86976cd2008-01-30 13:31:01 +01001576
Roland McGrathd4d67152008-07-09 02:38:07 -07001577#ifdef CONFIG_X86_32
1578# define IS_IA32 1
1579#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001580# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001581#else
1582# define IS_IA32 0
1583#endif
1584
1585/*
1586 * We must return the syscall number to actually look up in the table.
1587 * This can be -1L to skip running any syscall at all.
1588 */
1589asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001590{
Roland McGrathd4d67152008-07-09 02:38:07 -07001591 long ret = 0;
1592
Roland McGrath380fdd72008-07-09 02:39:29 -07001593 /*
1594 * If we stepped into a sysenter/syscall insn, it trapped in
1595 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1596 * If user-mode had set TF itself, then it's still clear from
1597 * do_debug() and we need to set it again to restore the user
1598 * state. If we entered on the slow path, TF was already set.
1599 */
1600 if (test_thread_flag(TIF_SINGLESTEP))
1601 regs->flags |= X86_EFLAGS_TF;
1602
Roland McGrath86976cd2008-01-30 13:31:01 +01001603 /* do the secure computing check first */
1604 secure_computing(regs->orig_ax);
1605
Roland McGrathd4d67152008-07-09 02:38:07 -07001606 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1607 ret = -1L;
1608
Roland McGratheeea3c32008-03-16 23:36:28 -07001609 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1610 tracehook_report_syscall_entry(regs))
1611 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001612
Josh Stone66700002009-08-24 14:43:11 -07001613 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001614 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001615
Roland McGrath86976cd2008-01-30 13:31:01 +01001616 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001617 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001618 audit_syscall_entry(AUDIT_ARCH_I386,
1619 regs->orig_ax,
1620 regs->bx, regs->cx,
1621 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001622#ifdef CONFIG_X86_64
1623 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001624 audit_syscall_entry(AUDIT_ARCH_X86_64,
1625 regs->orig_ax,
1626 regs->di, regs->si,
1627 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001628#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001629 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001630
1631 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001632}
1633
Roland McGrathd4d67152008-07-09 02:38:07 -07001634asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001635{
1636 if (unlikely(current->audit_context))
1637 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1638
Josh Stone66700002009-08-24 14:43:11 -07001639 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001640 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001641
Roland McGrathd4d67152008-07-09 02:38:07 -07001642 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001643 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001644
Roland McGrathd4d67152008-07-09 02:38:07 -07001645 /*
1646 * If TIF_SYSCALL_EMU is set, we only get here because of
1647 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1648 * We already reported this syscall instruction in
1649 * syscall_trace_enter(), so don't do any more now.
1650 */
1651 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1652 return;
1653
1654 /*
1655 * If we are single-stepping, synthesize a trap to follow the
1656 * system call instruction.
1657 */
1658 if (test_thread_flag(TIF_SINGLESTEP) &&
Oleg Nesterov43918f22009-04-02 16:58:00 -07001659 tracehook_consider_fatal_signal(current, SIGTRAP))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301660 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001661}