blob: 2e9b55027b7e10e5a8ee31dc270ed7497439384c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/ptrace.h>
Roland McGrath91e7b702008-01-30 13:31:52 +010017#include <linux/regset.h>
Roland McGratheeea3c32008-03-16 23:36:28 -070018#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/user.h>
Roland McGrath070459d2008-01-30 13:31:53 +010020#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/security.h>
22#include <linux/audit.h>
23#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070024#include <linux/signal.h>
Markus Metzgere2b371f2009-04-03 16:43:35 +020025#include <linux/workqueue.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020026#include <linux/perf_event.h>
27#include <linux/hw_breakpoint.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/uaccess.h>
30#include <asm/pgtable.h>
31#include <asm/system.h>
32#include <asm/processor.h>
33#include <asm/i387.h>
34#include <asm/debugreg.h>
35#include <asm/ldt.h>
36#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010037#include <asm/prctl.h>
38#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010039#include <asm/ds.h>
K.Prasad72f674d2009-06-01 23:45:48 +053040#include <asm/hw_breakpoint.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010041
Roland McGrath070459d2008-01-30 13:31:53 +010042#include "tls.h"
43
Josh Stone1c569f02009-08-24 14:43:14 -070044#define CREATE_TRACE_POINTS
45#include <trace/events/syscalls.h>
46
Roland McGrath070459d2008-01-30 13:31:53 +010047enum x86_regset {
48 REGSET_GENERAL,
49 REGSET_FP,
50 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070051 REGSET_IOPERM64 = REGSET_XFP,
Suresh Siddha5b3efd52010-02-11 11:50:59 -080052 REGSET_XSTATE,
Roland McGrath070459d2008-01-30 13:31:53 +010053 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070054 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010055};
Markus Metzgereee3af42008-01-30 13:31:09 +010056
Masami Hiramatsub1cf5402009-08-13 16:34:44 -040057struct pt_regs_offset {
58 const char *name;
59 int offset;
60};
61
62#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
63#define REG_OFFSET_END {.name = NULL, .offset = 0}
64
65static const struct pt_regs_offset regoffset_table[] = {
66#ifdef CONFIG_X86_64
67 REG_OFFSET_NAME(r15),
68 REG_OFFSET_NAME(r14),
69 REG_OFFSET_NAME(r13),
70 REG_OFFSET_NAME(r12),
71 REG_OFFSET_NAME(r11),
72 REG_OFFSET_NAME(r10),
73 REG_OFFSET_NAME(r9),
74 REG_OFFSET_NAME(r8),
75#endif
76 REG_OFFSET_NAME(bx),
77 REG_OFFSET_NAME(cx),
78 REG_OFFSET_NAME(dx),
79 REG_OFFSET_NAME(si),
80 REG_OFFSET_NAME(di),
81 REG_OFFSET_NAME(bp),
82 REG_OFFSET_NAME(ax),
83#ifdef CONFIG_X86_32
84 REG_OFFSET_NAME(ds),
85 REG_OFFSET_NAME(es),
86 REG_OFFSET_NAME(fs),
87 REG_OFFSET_NAME(gs),
88#endif
89 REG_OFFSET_NAME(orig_ax),
90 REG_OFFSET_NAME(ip),
91 REG_OFFSET_NAME(cs),
92 REG_OFFSET_NAME(flags),
93 REG_OFFSET_NAME(sp),
94 REG_OFFSET_NAME(ss),
95 REG_OFFSET_END,
96};
97
98/**
99 * regs_query_register_offset() - query register offset from its name
100 * @name: the name of a register
101 *
102 * regs_query_register_offset() returns the offset of a register in struct
103 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
104 */
105int regs_query_register_offset(const char *name)
106{
107 const struct pt_regs_offset *roff;
108 for (roff = regoffset_table; roff->name != NULL; roff++)
109 if (!strcmp(roff->name, name))
110 return roff->offset;
111 return -EINVAL;
112}
113
114/**
115 * regs_query_register_name() - query register name from its offset
116 * @offset: the offset of a register in struct pt_regs.
117 *
118 * regs_query_register_name() returns the name of a register from its
119 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
120 */
121const char *regs_query_register_name(unsigned int offset)
122{
123 const struct pt_regs_offset *roff;
124 for (roff = regoffset_table; roff->name != NULL; roff++)
125 if (roff->offset == offset)
126 return roff->name;
127 return NULL;
128}
129
130static const int arg_offs_table[] = {
131#ifdef CONFIG_X86_32
132 [0] = offsetof(struct pt_regs, ax),
133 [1] = offsetof(struct pt_regs, dx),
134 [2] = offsetof(struct pt_regs, cx)
135#else /* CONFIG_X86_64 */
136 [0] = offsetof(struct pt_regs, di),
137 [1] = offsetof(struct pt_regs, si),
138 [2] = offsetof(struct pt_regs, dx),
139 [3] = offsetof(struct pt_regs, cx),
140 [4] = offsetof(struct pt_regs, r8),
141 [5] = offsetof(struct pt_regs, r9)
142#endif
143};
144
Markus Metzgereee3af42008-01-30 13:31:09 +0100145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * does not yet catch signals sent when the child dies.
147 * in exit.c or in signal.c.
148 */
149
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500150/*
151 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500152 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100153#define FLAG_MASK_32 ((unsigned long) \
154 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
155 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
156 X86_EFLAGS_SF | X86_EFLAGS_TF | \
157 X86_EFLAGS_DF | X86_EFLAGS_OF | \
158 X86_EFLAGS_RF | X86_EFLAGS_AC))
159
Roland McGrath2047b082008-01-30 13:31:01 +0100160/*
161 * Determines whether a value may be installed in a segment register.
162 */
163static inline bool invalid_selector(u16 value)
164{
165 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
166}
167
168#ifdef CONFIG_X86_32
169
Roland McGrathe39c2892008-01-30 13:31:01 +0100170#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530172static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100174 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900175 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Roland McGrath06ee1b62008-01-30 13:31:01 +0100178static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100180 /*
181 * Returning the value truncates it to 16 bits.
182 */
183 unsigned int retval;
184 if (offset != offsetof(struct user_regs_struct, gs))
185 retval = *pt_regs_access(task_pt_regs(task), offset);
186 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100187 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900188 retval = get_user_gs(task_pt_regs(task));
189 else
190 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100191 }
192 return retval;
193}
194
195static int set_segment_reg(struct task_struct *task,
196 unsigned long offset, u16 value)
197{
198 /*
199 * The value argument was already truncated to 16 bits.
200 */
Roland McGrath2047b082008-01-30 13:31:01 +0100201 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100202 return -EIO;
203
Roland McGrathc63855d2008-02-06 22:39:44 +0100204 /*
205 * For %cs and %ss we cannot permit a null selector.
206 * We can permit a bogus selector as long as it has USER_RPL.
207 * Null selectors are fine for other segment registers, but
208 * we will never get back to user mode with invalid %cs or %ss
209 * and will take the trap in iret instead. Much code relies
210 * on user_mode() to distinguish a user trap frame (which can
211 * safely use invalid selectors) from a kernel trap frame.
212 */
213 switch (offset) {
214 case offsetof(struct user_regs_struct, cs):
215 case offsetof(struct user_regs_struct, ss):
216 if (unlikely(value == 0))
217 return -EIO;
218
219 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100220 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100221 break;
222
223 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100224 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900225 set_user_gs(task_pt_regs(task), value);
226 else
227 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231}
232
Roland McGrath2047b082008-01-30 13:31:01 +0100233#else /* CONFIG_X86_64 */
234
235#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
236
237static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
238{
239 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
240 return &regs->r15 + (offset / sizeof(regs->r15));
241}
242
243static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
244{
245 /*
246 * Returning the value truncates it to 16 bits.
247 */
248 unsigned int seg;
249
250 switch (offset) {
251 case offsetof(struct user_regs_struct, fs):
252 if (task == current) {
253 /* Older gas can't assemble movq %?s,%r?? */
254 asm("movl %%fs,%0" : "=r" (seg));
255 return seg;
256 }
257 return task->thread.fsindex;
258 case offsetof(struct user_regs_struct, gs):
259 if (task == current) {
260 asm("movl %%gs,%0" : "=r" (seg));
261 return seg;
262 }
263 return task->thread.gsindex;
264 case offsetof(struct user_regs_struct, ds):
265 if (task == current) {
266 asm("movl %%ds,%0" : "=r" (seg));
267 return seg;
268 }
269 return task->thread.ds;
270 case offsetof(struct user_regs_struct, es):
271 if (task == current) {
272 asm("movl %%es,%0" : "=r" (seg));
273 return seg;
274 }
275 return task->thread.es;
276
277 case offsetof(struct user_regs_struct, cs):
278 case offsetof(struct user_regs_struct, ss):
279 break;
280 }
281 return *pt_regs_access(task_pt_regs(task), offset);
282}
283
284static int set_segment_reg(struct task_struct *task,
285 unsigned long offset, u16 value)
286{
287 /*
288 * The value argument was already truncated to 16 bits.
289 */
290 if (invalid_selector(value))
291 return -EIO;
292
293 switch (offset) {
294 case offsetof(struct user_regs_struct,fs):
295 /*
296 * If this is setting fs as for normal 64-bit use but
297 * setting fs_base has implicitly changed it, leave it.
298 */
299 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
300 task->thread.fs != 0) ||
301 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
302 task->thread.fs == 0))
303 break;
304 task->thread.fsindex = value;
305 if (task == current)
306 loadsegment(fs, task->thread.fsindex);
307 break;
308 case offsetof(struct user_regs_struct,gs):
309 /*
310 * If this is setting gs as for normal 64-bit use but
311 * setting gs_base has implicitly changed it, leave it.
312 */
313 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
314 task->thread.gs != 0) ||
315 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
316 task->thread.gs == 0))
317 break;
318 task->thread.gsindex = value;
319 if (task == current)
320 load_gs_index(task->thread.gsindex);
321 break;
322 case offsetof(struct user_regs_struct,ds):
323 task->thread.ds = value;
324 if (task == current)
325 loadsegment(ds, task->thread.ds);
326 break;
327 case offsetof(struct user_regs_struct,es):
328 task->thread.es = value;
329 if (task == current)
330 loadsegment(es, task->thread.es);
331 break;
332
333 /*
334 * Can't actually change these in 64-bit mode.
335 */
336 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100337 if (unlikely(value == 0))
338 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100339#ifdef CONFIG_IA32_EMULATION
340 if (test_tsk_thread_flag(task, TIF_IA32))
341 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100342#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100343 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100344 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100345 if (unlikely(value == 0))
346 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100347#ifdef CONFIG_IA32_EMULATION
348 if (test_tsk_thread_flag(task, TIF_IA32))
349 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100350#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100351 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100352 }
353
354 return 0;
355}
356
Roland McGrath2047b082008-01-30 13:31:01 +0100357#endif /* CONFIG_X86_32 */
358
Roland McGrath06ee1b62008-01-30 13:31:01 +0100359static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100361 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Roland McGrath06ee1b62008-01-30 13:31:01 +0100363 /*
364 * If the debugger set TF, hide it from the readout.
365 */
366 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
367 retval &= ~X86_EFLAGS_TF;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return retval;
370}
371
Roland McGrath06ee1b62008-01-30 13:31:01 +0100372static int set_flags(struct task_struct *task, unsigned long value)
373{
374 struct pt_regs *regs = task_pt_regs(task);
375
376 /*
377 * If the user value contains TF, mark that
378 * it was not "us" (the debugger) that set it.
379 * If not, make sure it stays set if we had.
380 */
381 if (value & X86_EFLAGS_TF)
382 clear_tsk_thread_flag(task, TIF_FORCED_TF);
383 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
384 value |= X86_EFLAGS_TF;
385
386 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
387
388 return 0;
389}
390
391static int putreg(struct task_struct *child,
392 unsigned long offset, unsigned long value)
393{
394 switch (offset) {
395 case offsetof(struct user_regs_struct, cs):
396 case offsetof(struct user_regs_struct, ds):
397 case offsetof(struct user_regs_struct, es):
398 case offsetof(struct user_regs_struct, fs):
399 case offsetof(struct user_regs_struct, gs):
400 case offsetof(struct user_regs_struct, ss):
401 return set_segment_reg(child, offset, value);
402
403 case offsetof(struct user_regs_struct, flags):
404 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100405
406#ifdef CONFIG_X86_64
407 case offsetof(struct user_regs_struct,fs_base):
408 if (value >= TASK_SIZE_OF(child))
409 return -EIO;
410 /*
411 * When changing the segment base, use do_arch_prctl
412 * to set either thread.fs or thread.fsindex and the
413 * corresponding GDT slot.
414 */
415 if (child->thread.fs != value)
416 return do_arch_prctl(child, ARCH_SET_FS, value);
417 return 0;
418 case offsetof(struct user_regs_struct,gs_base):
419 /*
420 * Exactly the same here as the %fs handling above.
421 */
422 if (value >= TASK_SIZE_OF(child))
423 return -EIO;
424 if (child->thread.gs != value)
425 return do_arch_prctl(child, ARCH_SET_GS, value);
426 return 0;
427#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100428 }
429
430 *pt_regs_access(task_pt_regs(child), offset) = value;
431 return 0;
432}
433
434static unsigned long getreg(struct task_struct *task, unsigned long offset)
435{
436 switch (offset) {
437 case offsetof(struct user_regs_struct, cs):
438 case offsetof(struct user_regs_struct, ds):
439 case offsetof(struct user_regs_struct, es):
440 case offsetof(struct user_regs_struct, fs):
441 case offsetof(struct user_regs_struct, gs):
442 case offsetof(struct user_regs_struct, ss):
443 return get_segment_reg(task, offset);
444
445 case offsetof(struct user_regs_struct, flags):
446 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100447
448#ifdef CONFIG_X86_64
449 case offsetof(struct user_regs_struct, fs_base): {
450 /*
451 * do_arch_prctl may have used a GDT slot instead of
452 * the MSR. To userland, it appears the same either
453 * way, except the %fs segment selector might not be 0.
454 */
455 unsigned int seg = task->thread.fsindex;
456 if (task->thread.fs != 0)
457 return task->thread.fs;
458 if (task == current)
459 asm("movl %%fs,%0" : "=r" (seg));
460 if (seg != FS_TLS_SEL)
461 return 0;
462 return get_desc_base(&task->thread.tls_array[FS_TLS]);
463 }
464 case offsetof(struct user_regs_struct, gs_base): {
465 /*
466 * Exactly the same here as the %fs handling above.
467 */
468 unsigned int seg = task->thread.gsindex;
469 if (task->thread.gs != 0)
470 return task->thread.gs;
471 if (task == current)
472 asm("movl %%gs,%0" : "=r" (seg));
473 if (seg != GS_TLS_SEL)
474 return 0;
475 return get_desc_base(&task->thread.tls_array[GS_TLS]);
476 }
477#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100478 }
479
480 return *pt_regs_access(task_pt_regs(task), offset);
481}
482
Roland McGrath91e7b702008-01-30 13:31:52 +0100483static int genregs_get(struct task_struct *target,
484 const struct user_regset *regset,
485 unsigned int pos, unsigned int count,
486 void *kbuf, void __user *ubuf)
487{
488 if (kbuf) {
489 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800490 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100491 *k++ = getreg(target, pos);
492 count -= sizeof(*k);
493 pos += sizeof(*k);
494 }
495 } else {
496 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800497 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100498 if (__put_user(getreg(target, pos), u++))
499 return -EFAULT;
500 count -= sizeof(*u);
501 pos += sizeof(*u);
502 }
503 }
504
505 return 0;
506}
507
508static int genregs_set(struct task_struct *target,
509 const struct user_regset *regset,
510 unsigned int pos, unsigned int count,
511 const void *kbuf, const void __user *ubuf)
512{
513 int ret = 0;
514 if (kbuf) {
515 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800516 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100517 ret = putreg(target, pos, *k++);
518 count -= sizeof(*k);
519 pos += sizeof(*k);
520 }
521 } else {
522 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800523 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100524 unsigned long word;
525 ret = __get_user(word, u++);
526 if (ret)
527 break;
528 ret = putreg(target, pos, word);
529 count -= sizeof(*u);
530 pos += sizeof(*u);
531 }
532 }
533 return ret;
534}
535
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100536static void ptrace_triggered(struct perf_event *bp, int nmi,
537 struct perf_sample_data *data,
538 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100539{
Roland McGrath0f534092008-01-30 13:30:59 +0100540 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200541 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100542
K.Prasad72f674d2009-06-01 23:45:48 +0530543 /*
544 * Store in the virtual DR6 register the fact that the breakpoint
545 * was hit so the thread's debugger will see it.
546 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200547 for (i = 0; i < HBP_NUM; i++) {
548 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530549 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100550 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100551
K.Prasad72f674d2009-06-01 23:45:48 +0530552 thread->debugreg6 |= (DR_TRAP0 << i);
553}
554
555/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200556 * Walk through every ptrace breakpoints for this thread and
557 * build the dr7 value on top of their attributes.
558 *
559 */
560static unsigned long ptrace_get_dr7(struct perf_event *bp[])
561{
562 int i;
563 int dr7 = 0;
564 struct arch_hw_breakpoint *info;
565
566 for (i = 0; i < HBP_NUM; i++) {
567 if (bp[i] && !bp[i]->attr.disabled) {
568 info = counter_arch_bp(bp[i]);
569 dr7 |= encode_dr7(i, info->len, info->type);
570 }
571 }
572
573 return dr7;
574}
575
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100576static int
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100577ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100578 struct task_struct *tsk, int disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100579{
580 int err;
581 int gen_len, gen_type;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100582 struct perf_event_attr attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100583
584 /*
Adam Buchbinderc9404c92009-12-18 15:40:42 -0500585 * We should have at least an inactive breakpoint at this
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100586 * slot. It means the user is writing dr7 without having
587 * written the address register first
588 */
589 if (!bp)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100590 return -EINVAL;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100591
592 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
593 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100594 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100595
596 attr = bp->attr;
597 attr.bp_len = gen_len;
598 attr.bp_type = gen_type;
Frederic Weisbecker1cedae72009-12-02 07:32:16 +0100599 attr.disabled = disabled;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100600
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100601 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100602}
603
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200604/*
K.Prasad72f674d2009-06-01 23:45:48 +0530605 * Handle ptrace writes to debug register 7.
606 */
607static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
608{
609 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200610 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530611 int i, orig_ret = 0, rc = 0;
612 int enabled, second_pass = 0;
613 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200614 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530615
616 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 }
663 return ((orig_ret < 0) ? orig_ret : rc);
664}
665
666/*
667 * Handle PTRACE_PEEKUSR calls for the debug register area.
668 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530669static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530670{
671 struct thread_struct *thread = &(tsk->thread);
672 unsigned long val = 0;
673
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200674 if (n < HBP_NUM) {
675 struct perf_event *bp;
676 bp = thread->ptrace_bps[n];
677 if (!bp)
678 return 0;
679 val = bp->hw.info.address;
680 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530681 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200682 } else if (n == 7) {
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100683 val = thread->ptrace_dr7;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200684 }
K.Prasad72f674d2009-06-01 23:45:48 +0530685 return val;
686}
687
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200688static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
689 unsigned long addr)
690{
691 struct perf_event *bp;
692 struct thread_struct *t = &tsk->thread;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100693 struct perf_event_attr attr;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200694
695 if (!t->ptrace_bps[nr]) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100696 hw_breakpoint_init(&attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200697 /*
698 * Put stub len and type to register (reserve) an inactive but
699 * correct bp
700 */
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100701 attr.bp_addr = addr;
702 attr.bp_len = HW_BREAKPOINT_LEN_1;
703 attr.bp_type = HW_BREAKPOINT_W;
704 attr.disabled = 1;
705
706 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100707
708 /*
709 * CHECKME: the previous code returned -EIO if the addr wasn't
710 * a valid task virtual addr. The new one will return -EINVAL in
711 * this case.
712 * -EINVAL may be what we want for in-kernel breakpoints users,
713 * but -EIO looks better for ptrace, since we refuse a register
714 * writing for the user. And anyway this is the previous
715 * behaviour.
716 */
717 if (IS_ERR(bp))
718 return PTR_ERR(bp);
719
720 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200721 } else {
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100722 int err;
723
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200724 bp = t->ptrace_bps[nr];
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100725
726 attr = bp->attr;
727 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100728 err = modify_user_hw_breakpoint(bp, &attr);
729 if (err)
730 return err;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200731 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200732
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735}
736
K.Prasad72f674d2009-06-01 23:45:48 +0530737/*
738 * Handle PTRACE_POKEUSR calls for the debug register area.
739 */
740int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
K.Prasad72f674d2009-06-01 23:45:48 +0530742 struct thread_struct *thread = &(tsk->thread);
743 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
K.Prasad72f674d2009-06-01 23:45:48 +0530745 /* There are no DR4 or DR5 registers */
746 if (n == 4 || n == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return -EIO;
748
K.Prasad72f674d2009-06-01 23:45:48 +0530749 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200750 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530751 goto ret_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
K.Prasad72f674d2009-06-01 23:45:48 +0530753 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200754 rc = ptrace_set_breakpoint_addr(tsk, n, val);
755 if (rc)
756 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530757 }
758 /* All that's left is DR7 */
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100759 if (n == 7) {
K.Prasad72f674d2009-06-01 23:45:48 +0530760 rc = ptrace_write_dr7(tsk, val);
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100761 if (!rc)
762 thread->ptrace_dr7 = val;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
K.Prasad72f674d2009-06-01 23:45:48 +0530765ret_path:
766 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Roland McGrath325af5f2008-08-08 15:58:39 -0700769/*
770 * These access the current or another (stopped) task's io permission
771 * bitmap for debugging or core dump.
772 */
773static int ioperm_active(struct task_struct *target,
774 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100775{
Roland McGrath325af5f2008-08-08 15:58:39 -0700776 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100777}
778
Roland McGrath325af5f2008-08-08 15:58:39 -0700779static int ioperm_get(struct task_struct *target,
780 const struct user_regset *regset,
781 unsigned int pos, unsigned int count,
782 void *kbuf, void __user *ubuf)
783{
784 if (!target->thread.io_bitmap_ptr)
785 return -ENXIO;
786
787 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
788 target->thread.io_bitmap_ptr,
789 0, IO_BITMAP_BYTES);
790}
791
Markus Metzger93fa7632008-04-08 11:01:58 +0200792#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200793/*
794 * A branch trace store context.
795 *
796 * Contexts may only be installed by ptrace_bts_config() and only for
797 * ptraced tasks.
798 *
799 * Contexts are destroyed when the tracee is detached from the tracer.
800 * The actual destruction work requires interrupts enabled, so the
801 * work is deferred and will be scheduled during __ptrace_unlink().
802 *
803 * Contexts hold an additional task_struct reference on the traced
804 * task, as well as a reference on the tracer's mm.
805 *
806 * Ptrace already holds a task_struct for the duration of ptrace operations,
807 * but since destruction is deferred, it may be executed after both
808 * tracer and tracee exited.
809 */
810struct bts_context {
811 /* The branch trace handle. */
812 struct bts_tracer *tracer;
813
814 /* The buffer used to store the branch trace and its size. */
815 void *buffer;
816 unsigned int size;
817
818 /* The mm that paid for the above buffer. */
819 struct mm_struct *mm;
820
821 /* The task this context belongs to. */
822 struct task_struct *task;
823
824 /* The signal to send on a bts buffer overflow. */
825 unsigned int bts_ovfl_signal;
826
827 /* The work struct to destroy a context. */
828 struct work_struct work;
829};
830
Markus Metzger1cb81b12009-04-24 09:51:43 +0200831static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200832{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200833 void *buffer = NULL;
834 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200835
Markus Metzger1cb81b12009-04-24 09:51:43 +0200836 err = account_locked_memory(current->mm, current->signal->rlim, size);
837 if (err < 0)
838 return err;
839
840 buffer = kzalloc(size, GFP_KERNEL);
841 if (!buffer)
842 goto out_refund;
843
844 context->buffer = buffer;
845 context->size = size;
846 context->mm = get_task_mm(current);
847
848 return 0;
849
850 out_refund:
851 refund_locked_memory(current->mm, size);
852 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200853}
854
855static inline void free_bts_buffer(struct bts_context *context)
856{
857 if (!context->buffer)
858 return;
859
860 kfree(context->buffer);
861 context->buffer = NULL;
862
Markus Metzger1cb81b12009-04-24 09:51:43 +0200863 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200864 context->size = 0;
865
866 mmput(context->mm);
867 context->mm = NULL;
868}
869
870static void free_bts_context_work(struct work_struct *w)
871{
872 struct bts_context *context;
873
874 context = container_of(w, struct bts_context, work);
875
876 ds_release_bts(context->tracer);
877 put_task_struct(context->task);
878 free_bts_buffer(context);
879 kfree(context);
880}
881
882static inline void free_bts_context(struct bts_context *context)
883{
884 INIT_WORK(&context->work, free_bts_context_work);
885 schedule_work(&context->work);
886}
887
888static inline struct bts_context *alloc_bts_context(struct task_struct *task)
889{
890 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
891 if (context) {
892 context->task = task;
893 task->bts = context;
894
895 get_task_struct(task);
896 }
897
898 return context;
899}
900
Markus Metzger93fa7632008-04-08 11:01:58 +0200901static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100902 struct bts_struct __user *out)
903{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200904 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100905 const struct bts_trace *trace;
906 struct bts_struct bts;
907 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200908 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100909
Markus Metzgere2b371f2009-04-03 16:43:35 +0200910 context = child->bts;
911 if (!context)
912 return -ESRCH;
913
914 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100915 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200916 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100917
918 at = trace->ds.top - ((index + 1) * trace->ds.size);
919 if ((void *)at < trace->ds.begin)
920 at += (trace->ds.n * trace->ds.size);
921
922 if (!trace->read)
923 return -EOPNOTSUPP;
924
Markus Metzgere2b371f2009-04-03 16:43:35 +0200925 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200926 if (error < 0)
927 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100928
Markus Metzgerc2724772008-12-11 13:49:59 +0100929 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100930 return -EFAULT;
931
Markus Metzgerc2724772008-12-11 13:49:59 +0100932 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100933}
934
Markus Metzgera95d67f2008-01-30 13:31:20 +0100935static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100936 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100937 struct bts_struct __user *out)
938{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200939 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100940 const struct bts_trace *trace;
941 const unsigned char *at;
942 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100943
Markus Metzgere2b371f2009-04-03 16:43:35 +0200944 context = child->bts;
945 if (!context)
946 return -ESRCH;
947
948 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100949 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200950 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100951
Markus Metzgerc2724772008-12-11 13:49:59 +0100952 if (!trace->read)
953 return -EOPNOTSUPP;
954
955 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100956 return -EIO;
957
Markus Metzgerc2724772008-12-11 13:49:59 +0100958 for (at = trace->ds.begin; (void *)at < trace->ds.top;
959 out++, drained++, at += trace->ds.size) {
960 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100961
Markus Metzgere2b371f2009-04-03 16:43:35 +0200962 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100963 if (error < 0)
964 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100965
Markus Metzgerc2724772008-12-11 13:49:59 +0100966 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100967 return -EFAULT;
968 }
969
Markus Metzgerc2724772008-12-11 13:49:59 +0100970 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
971
Markus Metzgere2b371f2009-04-03 16:43:35 +0200972 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200973 if (error < 0)
974 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100975
Markus Metzgerc2724772008-12-11 13:49:59 +0100976 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100977}
978
979static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100980 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100981 const struct ptrace_bts_config __user *ucfg)
982{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200983 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100984 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100985 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100986
Markus Metzgercba4b652008-01-30 13:32:03 +0100987 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100988 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100989
Markus Metzgera95d67f2008-01-30 13:31:20 +0100990 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100991 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100992
Markus Metzgere2b371f2009-04-03 16:43:35 +0200993 context = child->bts;
994 if (!context)
995 context = alloc_bts_context(child);
996 if (!context)
997 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100998
Markus Metzgerc2724772008-12-11 13:49:59 +0100999 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1000 if (!cfg.signal)
1001 return -EINVAL;
1002
Américo Wang5a8ac9d2009-03-13 15:56:58 +08001003 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +02001004 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +01001005 }
1006
Markus Metzgere2b371f2009-04-03 16:43:35 +02001007 ds_release_bts(context->tracer);
1008 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001009
Markus Metzgere2b371f2009-04-03 16:43:35 +02001010 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +02001011 int err;
1012
Markus Metzgere2b371f2009-04-03 16:43:35 +02001013 free_bts_buffer(context);
1014 if (!cfg.size)
1015 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +01001016
Markus Metzger1cb81b12009-04-24 09:51:43 +02001017 err = alloc_bts_buffer(context, cfg.size);
1018 if (err < 0)
1019 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +01001020 }
Markus Metzgera95d67f2008-01-30 13:31:20 +01001021
Markus Metzgerda35c372008-01-30 13:32:03 +01001022 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +01001023 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +01001024
Markus Metzgerda35c372008-01-30 13:32:03 +01001025 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +01001026 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +01001027
Markus Metzgerde79f542009-04-03 16:43:40 +02001028 context->tracer =
1029 ds_request_bts_task(child, context->buffer, context->size,
1030 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +02001031 if (unlikely(IS_ERR(context->tracer))) {
1032 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +01001033
Markus Metzgere2b371f2009-04-03 16:43:35 +02001034 free_bts_buffer(context);
1035 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +01001036 return error;
1037 }
Markus Metzgerda35c372008-01-30 13:32:03 +01001038
Markus Metzgerc2724772008-12-11 13:49:59 +01001039 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001040}
1041
Markus Metzgera95d67f2008-01-30 13:31:20 +01001042static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +01001043 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +01001044 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +01001045{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001046 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001047 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +01001048 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +01001049
Markus Metzgere2b371f2009-04-03 16:43:35 +02001050 context = child->bts;
1051 if (!context)
1052 return -ESRCH;
1053
Markus Metzgercba4b652008-01-30 13:32:03 +01001054 if (cfg_size < sizeof(cfg))
1055 return -EIO;
1056
Markus Metzgere2b371f2009-04-03 16:43:35 +02001057 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001058 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001059 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001060
Markus Metzgera95d67f2008-01-30 13:31:20 +01001061 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +02001062 cfg.size = trace->ds.end - trace->ds.begin;
1063 cfg.signal = context->bts_ovfl_signal;
1064 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +01001065
Markus Metzger93fa7632008-04-08 11:01:58 +02001066 if (cfg.signal)
1067 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1068
Markus Metzgerc2724772008-12-11 13:49:59 +01001069 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +02001070 cfg.flags |= PTRACE_BTS_O_TRACE;
1071
Markus Metzgerc2724772008-12-11 13:49:59 +01001072 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +02001073 cfg.flags |= PTRACE_BTS_O_SCHED;
1074
Markus Metzgera95d67f2008-01-30 13:31:20 +01001075 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1076 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +01001077
Markus Metzgera95d67f2008-01-30 13:31:20 +01001078 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +01001079}
1080
Markus Metzgerc2724772008-12-11 13:49:59 +01001081static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -08001082{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001083 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001084 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001085
Markus Metzgere2b371f2009-04-03 16:43:35 +02001086 context = child->bts;
1087 if (!context)
1088 return -ESRCH;
1089
1090 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001091 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001092 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -08001093
Markus Metzgerc2724772008-12-11 13:49:59 +01001094 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001095
Markus Metzgere2b371f2009-04-03 16:43:35 +02001096 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -08001097}
1098
Markus Metzgerc2724772008-12-11 13:49:59 +01001099static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +01001100{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001101 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001102 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +01001103
Markus Metzgere2b371f2009-04-03 16:43:35 +02001104 context = child->bts;
1105 if (!context)
1106 return -ESRCH;
1107
1108 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001109 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001110 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001111
Markus Metzgerc2724772008-12-11 13:49:59 +01001112 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +02001113}
Markus Metzgerbf53de92008-12-19 15:10:24 +01001114
Markus Metzgere2b371f2009-04-03 16:43:35 +02001115/*
1116 * Called from __ptrace_unlink() after the child has been moved back
1117 * to its original parent.
1118 */
Markus Metzger0f481402009-04-03 16:43:48 +02001119void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +01001120{
1121 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +02001122 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +01001123 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +01001124 }
1125}
Markus Metzger93fa7632008-04-08 11:01:58 +02001126#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * Called by kernel/ptrace.c when detaching..
1130 *
1131 * Make sure the single step bit is not set.
1132 */
1133void ptrace_disable(struct task_struct *child)
1134{
1135 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001136#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Roland McGrath5a4646a2008-01-30 13:31:54 +01001141#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1142static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1143#endif
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1146{
Roland McGrath5a4646a2008-01-30 13:31:54 +01001147 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 unsigned long __user *datap = (unsigned long __user *)data;
1149
1150 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* read the word at location addr in the USER area. */
1152 case PTRACE_PEEKUSR: {
1153 unsigned long tmp;
1154
1155 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001156 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1157 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159
1160 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001161 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001163 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1164 addr <= offsetof(struct user, u_debugreg[7])) {
1165 addr -= offsetof(struct user, u_debugreg[0]);
1166 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 ret = put_user(tmp, datap);
1169 break;
1170 }
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1173 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001174 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1175 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
1177
Roland McGrathe9c86c72008-01-30 13:31:01 +01001178 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001180 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1181 addr <= offsetof(struct user, u_debugreg[7])) {
1182 addr -= offsetof(struct user, u_debugreg[0]);
1183 ret = ptrace_set_debugreg(child,
1184 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Roland McGrath5a4646a2008-01-30 13:31:54 +01001188 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1189 return copy_regset_to_user(child,
1190 task_user_regset_view(current),
1191 REGSET_GENERAL,
1192 0, sizeof(struct user_regs_struct),
1193 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Roland McGrath5a4646a2008-01-30 13:31:54 +01001195 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1196 return copy_regset_from_user(child,
1197 task_user_regset_view(current),
1198 REGSET_GENERAL,
1199 0, sizeof(struct user_regs_struct),
1200 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Roland McGrath5a4646a2008-01-30 13:31:54 +01001202 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1203 return copy_regset_to_user(child,
1204 task_user_regset_view(current),
1205 REGSET_FP,
1206 0, sizeof(struct user_i387_struct),
1207 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Roland McGrath5a4646a2008-01-30 13:31:54 +01001209 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1210 return copy_regset_from_user(child,
1211 task_user_regset_view(current),
1212 REGSET_FP,
1213 0, sizeof(struct user_i387_struct),
1214 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Roland McGrathe9c86c72008-01-30 13:31:01 +01001216#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001217 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1218 return copy_regset_to_user(child, &user_x86_32_view,
1219 REGSET_XFP,
1220 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001221 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Roland McGrath5a4646a2008-01-30 13:31:54 +01001223 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1224 return copy_regset_from_user(child, &user_x86_32_view,
1225 REGSET_XFP,
1226 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001227 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001228#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Roland McGrathe9c86c72008-01-30 13:31:01 +01001230#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001232 if (addr < 0)
1233 return -EIO;
1234 ret = do_get_thread_area(child, addr,
1235 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237
1238 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001239 if (addr < 0)
1240 return -EIO;
1241 ret = do_set_thread_area(child, addr,
1242 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001244#endif
1245
1246#ifdef CONFIG_X86_64
1247 /* normal 64bit interface to access TLS data.
1248 Works just like arch_prctl, except that the arguments
1249 are reversed. */
1250 case PTRACE_ARCH_PRCTL:
1251 ret = do_arch_prctl(child, data, addr);
1252 break;
1253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001255 /*
1256 * These bits need more cooking - not enabled yet:
1257 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001258#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001259 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001260 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001261 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001262 break;
1263
1264 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001265 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001266 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001267 break;
1268
Markus Metzgerc2724772008-12-11 13:49:59 +01001269 case PTRACE_BTS_SIZE:
1270 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001271 break;
1272
1273 case PTRACE_BTS_GET:
1274 ret = ptrace_bts_read_record
1275 (child, data, (struct bts_struct __user *) addr);
1276 break;
1277
1278 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001279 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001280 break;
1281
1282 case PTRACE_BTS_DRAIN:
1283 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001284 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001285 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001286#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 default:
1289 ret = ptrace_request(child, request, addr, data);
1290 break;
1291 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return ret;
1294}
1295
Roland McGrathcb757c42008-01-30 13:31:01 +01001296#ifdef CONFIG_IA32_EMULATION
1297
Roland McGrath099cd6e2008-01-30 13:31:01 +01001298#include <linux/compat.h>
1299#include <linux/syscalls.h>
1300#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001301#include <asm/user32.h>
1302
1303#define R32(l,q) \
1304 case offsetof(struct user32, regs.l): \
1305 regs->q = value; break
1306
1307#define SEG32(rs) \
1308 case offsetof(struct user32, regs.rs): \
1309 return set_segment_reg(child, \
1310 offsetof(struct user_regs_struct, rs), \
1311 value); \
1312 break
1313
1314static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1315{
1316 struct pt_regs *regs = task_pt_regs(child);
1317
1318 switch (regno) {
1319
1320 SEG32(cs);
1321 SEG32(ds);
1322 SEG32(es);
1323 SEG32(fs);
1324 SEG32(gs);
1325 SEG32(ss);
1326
1327 R32(ebx, bx);
1328 R32(ecx, cx);
1329 R32(edx, dx);
1330 R32(edi, di);
1331 R32(esi, si);
1332 R32(ebp, bp);
1333 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001334 R32(eip, ip);
1335 R32(esp, sp);
1336
Roland McGrath40f09332008-02-28 19:57:07 -08001337 case offsetof(struct user32, regs.orig_eax):
1338 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001339 * A 32-bit debugger setting orig_eax means to restore
1340 * the state of the task restarting a 32-bit syscall.
1341 * Make sure we interpret the -ERESTART* codes correctly
1342 * in case the task is not actually still sitting at the
1343 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001344 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001345 regs->orig_ax = value;
1346 if (syscall_get_nr(child, regs) >= 0)
1347 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001348 break;
1349
Roland McGrathcb757c42008-01-30 13:31:01 +01001350 case offsetof(struct user32, regs.eflags):
1351 return set_flags(child, value);
1352
1353 case offsetof(struct user32, u_debugreg[0]) ...
1354 offsetof(struct user32, u_debugreg[7]):
1355 regno -= offsetof(struct user32, u_debugreg[0]);
1356 return ptrace_set_debugreg(child, regno / 4, value);
1357
1358 default:
1359 if (regno > sizeof(struct user32) || (regno & 3))
1360 return -EIO;
1361
1362 /*
1363 * Other dummy fields in the virtual user structure
1364 * are ignored
1365 */
1366 break;
1367 }
1368 return 0;
1369}
1370
1371#undef R32
1372#undef SEG32
1373
1374#define R32(l,q) \
1375 case offsetof(struct user32, regs.l): \
1376 *val = regs->q; break
1377
1378#define SEG32(rs) \
1379 case offsetof(struct user32, regs.rs): \
1380 *val = get_segment_reg(child, \
1381 offsetof(struct user_regs_struct, rs)); \
1382 break
1383
1384static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1385{
1386 struct pt_regs *regs = task_pt_regs(child);
1387
1388 switch (regno) {
1389
1390 SEG32(ds);
1391 SEG32(es);
1392 SEG32(fs);
1393 SEG32(gs);
1394
1395 R32(cs, cs);
1396 R32(ss, ss);
1397 R32(ebx, bx);
1398 R32(ecx, cx);
1399 R32(edx, dx);
1400 R32(edi, di);
1401 R32(esi, si);
1402 R32(ebp, bp);
1403 R32(eax, ax);
1404 R32(orig_eax, orig_ax);
1405 R32(eip, ip);
1406 R32(esp, sp);
1407
1408 case offsetof(struct user32, regs.eflags):
1409 *val = get_flags(child);
1410 break;
1411
1412 case offsetof(struct user32, u_debugreg[0]) ...
1413 offsetof(struct user32, u_debugreg[7]):
1414 regno -= offsetof(struct user32, u_debugreg[0]);
1415 *val = ptrace_get_debugreg(child, regno / 4);
1416 break;
1417
1418 default:
1419 if (regno > sizeof(struct user32) || (regno & 3))
1420 return -EIO;
1421
1422 /*
1423 * Other dummy fields in the virtual user structure
1424 * are ignored
1425 */
1426 *val = 0;
1427 break;
1428 }
1429 return 0;
1430}
1431
1432#undef R32
1433#undef SEG32
1434
Roland McGrath91e7b702008-01-30 13:31:52 +01001435static int genregs32_get(struct task_struct *target,
1436 const struct user_regset *regset,
1437 unsigned int pos, unsigned int count,
1438 void *kbuf, void __user *ubuf)
1439{
1440 if (kbuf) {
1441 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001442 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001443 getreg32(target, pos, k++);
1444 count -= sizeof(*k);
1445 pos += sizeof(*k);
1446 }
1447 } else {
1448 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001449 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001450 compat_ulong_t word;
1451 getreg32(target, pos, &word);
1452 if (__put_user(word, u++))
1453 return -EFAULT;
1454 count -= sizeof(*u);
1455 pos += sizeof(*u);
1456 }
1457 }
1458
1459 return 0;
1460}
1461
1462static int genregs32_set(struct task_struct *target,
1463 const struct user_regset *regset,
1464 unsigned int pos, unsigned int count,
1465 const void *kbuf, const void __user *ubuf)
1466{
1467 int ret = 0;
1468 if (kbuf) {
1469 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001470 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001471 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001472 count -= sizeof(*k);
1473 pos += sizeof(*k);
1474 }
1475 } else {
1476 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001477 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001478 compat_ulong_t word;
1479 ret = __get_user(word, u++);
1480 if (ret)
1481 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001482 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001483 count -= sizeof(*u);
1484 pos += sizeof(*u);
1485 }
1486 }
1487 return ret;
1488}
1489
Roland McGrath562b80b2008-04-22 12:21:25 -07001490long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1491 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001492{
Roland McGrath562b80b2008-04-22 12:21:25 -07001493 unsigned long addr = caddr;
1494 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001495 void __user *datap = compat_ptr(data);
1496 int ret;
1497 __u32 val;
1498
1499 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001500 case PTRACE_PEEKUSR:
1501 ret = getreg32(child, addr, &val);
1502 if (ret == 0)
1503 ret = put_user(val, (__u32 __user *)datap);
1504 break;
1505
1506 case PTRACE_POKEUSR:
1507 ret = putreg32(child, addr, data);
1508 break;
1509
Roland McGrath5a4646a2008-01-30 13:31:54 +01001510 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1511 return copy_regset_to_user(child, &user_x86_32_view,
1512 REGSET_GENERAL,
1513 0, sizeof(struct user_regs_struct32),
1514 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001515
Roland McGrath5a4646a2008-01-30 13:31:54 +01001516 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1517 return copy_regset_from_user(child, &user_x86_32_view,
1518 REGSET_GENERAL, 0,
1519 sizeof(struct user_regs_struct32),
1520 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001521
Roland McGrath5a4646a2008-01-30 13:31:54 +01001522 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1523 return copy_regset_to_user(child, &user_x86_32_view,
1524 REGSET_FP, 0,
1525 sizeof(struct user_i387_ia32_struct),
1526 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001527
Roland McGrath5a4646a2008-01-30 13:31:54 +01001528 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1529 return copy_regset_from_user(
1530 child, &user_x86_32_view, REGSET_FP,
1531 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001532
Roland McGrath5a4646a2008-01-30 13:31:54 +01001533 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1534 return copy_regset_to_user(child, &user_x86_32_view,
1535 REGSET_XFP, 0,
1536 sizeof(struct user32_fxsr_struct),
1537 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001538
Roland McGrath5a4646a2008-01-30 13:31:54 +01001539 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1540 return copy_regset_from_user(child, &user_x86_32_view,
1541 REGSET_XFP, 0,
1542 sizeof(struct user32_fxsr_struct),
1543 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001544
Roland McGrath562b80b2008-04-22 12:21:25 -07001545 case PTRACE_GET_THREAD_AREA:
1546 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001547#ifdef CONFIG_X86_PTRACE_BTS
1548 case PTRACE_BTS_CONFIG:
1549 case PTRACE_BTS_STATUS:
1550 case PTRACE_BTS_SIZE:
1551 case PTRACE_BTS_GET:
1552 case PTRACE_BTS_CLEAR:
1553 case PTRACE_BTS_DRAIN:
1554#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001555 return arch_ptrace(child, request, addr, data);
1556
Roland McGrath099cd6e2008-01-30 13:31:01 +01001557 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001558 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001559 }
1560
Roland McGrath099cd6e2008-01-30 13:31:01 +01001561 return ret;
1562}
1563
Roland McGrathcb757c42008-01-30 13:31:01 +01001564#endif /* CONFIG_IA32_EMULATION */
1565
Roland McGrath070459d2008-01-30 13:31:53 +01001566#ifdef CONFIG_X86_64
1567
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001568static struct user_regset x86_64_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001569 [REGSET_GENERAL] = {
1570 .core_note_type = NT_PRSTATUS,
1571 .n = sizeof(struct user_regs_struct) / sizeof(long),
1572 .size = sizeof(long), .align = sizeof(long),
1573 .get = genregs_get, .set = genregs_set
1574 },
1575 [REGSET_FP] = {
1576 .core_note_type = NT_PRFPREG,
1577 .n = sizeof(struct user_i387_struct) / sizeof(long),
1578 .size = sizeof(long), .align = sizeof(long),
1579 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1580 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001581 [REGSET_XSTATE] = {
1582 .core_note_type = NT_X86_XSTATE,
1583 .size = sizeof(u64), .align = sizeof(u64),
1584 .active = xstateregs_active, .get = xstateregs_get,
1585 .set = xstateregs_set
1586 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001587 [REGSET_IOPERM64] = {
1588 .core_note_type = NT_386_IOPERM,
1589 .n = IO_BITMAP_LONGS,
1590 .size = sizeof(long), .align = sizeof(long),
1591 .active = ioperm_active, .get = ioperm_get
1592 },
Roland McGrath070459d2008-01-30 13:31:53 +01001593};
1594
1595static const struct user_regset_view user_x86_64_view = {
1596 .name = "x86_64", .e_machine = EM_X86_64,
1597 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1598};
1599
1600#else /* CONFIG_X86_32 */
1601
1602#define user_regs_struct32 user_regs_struct
1603#define genregs32_get genregs_get
1604#define genregs32_set genregs_set
1605
Roland McGrath1f465f42008-05-09 15:43:44 -07001606#define user_i387_ia32_struct user_i387_struct
1607#define user32_fxsr_struct user_fxsr_struct
1608
Roland McGrath070459d2008-01-30 13:31:53 +01001609#endif /* CONFIG_X86_64 */
1610
1611#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001612static struct user_regset x86_32_regsets[] __read_mostly = {
Roland McGrath070459d2008-01-30 13:31:53 +01001613 [REGSET_GENERAL] = {
1614 .core_note_type = NT_PRSTATUS,
1615 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1616 .size = sizeof(u32), .align = sizeof(u32),
1617 .get = genregs32_get, .set = genregs32_set
1618 },
1619 [REGSET_FP] = {
1620 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001621 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001622 .size = sizeof(u32), .align = sizeof(u32),
1623 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1624 },
1625 [REGSET_XFP] = {
1626 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001627 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001628 .size = sizeof(u32), .align = sizeof(u32),
1629 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1630 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001631 [REGSET_XSTATE] = {
1632 .core_note_type = NT_X86_XSTATE,
1633 .size = sizeof(u64), .align = sizeof(u64),
1634 .active = xstateregs_active, .get = xstateregs_get,
1635 .set = xstateregs_set
1636 },
Roland McGrath070459d2008-01-30 13:31:53 +01001637 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001638 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001639 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1640 .size = sizeof(struct user_desc),
1641 .align = sizeof(struct user_desc),
1642 .active = regset_tls_active,
1643 .get = regset_tls_get, .set = regset_tls_set
1644 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001645 [REGSET_IOPERM32] = {
1646 .core_note_type = NT_386_IOPERM,
1647 .n = IO_BITMAP_BYTES / sizeof(u32),
1648 .size = sizeof(u32), .align = sizeof(u32),
1649 .active = ioperm_active, .get = ioperm_get
1650 },
Roland McGrath070459d2008-01-30 13:31:53 +01001651};
1652
1653static const struct user_regset_view user_x86_32_view = {
1654 .name = "i386", .e_machine = EM_386,
1655 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1656};
1657#endif
1658
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001659/*
1660 * This represents bytes 464..511 in the memory layout exported through
1661 * the REGSET_XSTATE interface.
1662 */
1663u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1664
1665void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1666{
1667#ifdef CONFIG_X86_64
1668 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1669#endif
1670#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1671 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1672#endif
1673 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1674}
1675
Roland McGrath070459d2008-01-30 13:31:53 +01001676const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1677{
1678#ifdef CONFIG_IA32_EMULATION
1679 if (test_tsk_thread_flag(task, TIF_IA32))
1680#endif
1681#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1682 return &user_x86_32_view;
1683#endif
1684#ifdef CONFIG_X86_64
1685 return &user_x86_64_view;
1686#endif
1687}
1688
Oleg Nesterov7f385512009-12-15 16:47:20 -08001689static void fill_sigtrap_info(struct task_struct *tsk,
1690 struct pt_regs *regs,
1691 int error_code, int si_code,
1692 struct siginfo *info)
1693{
1694 tsk->thread.trap_no = 1;
1695 tsk->thread.error_code = error_code;
1696
1697 memset(info, 0, sizeof(*info));
1698 info->si_signo = SIGTRAP;
1699 info->si_code = si_code;
1700 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1701}
1702
1703void user_single_step_siginfo(struct task_struct *tsk,
1704 struct pt_regs *regs,
1705 struct siginfo *info)
1706{
1707 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1708}
1709
Srinivasa Dsda654b72008-09-23 15:23:52 +05301710void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1711 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct siginfo info;
1714
Oleg Nesterov7f385512009-12-15 16:47:20 -08001715 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
Simon Arlott27b46d72007-10-20 01:13:56 +02001716 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 force_sig_info(SIGTRAP, &info, tsk);
1718}
1719
Roland McGrath86976cd2008-01-30 13:31:01 +01001720
Roland McGrathd4d67152008-07-09 02:38:07 -07001721#ifdef CONFIG_X86_32
1722# define IS_IA32 1
1723#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001724# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001725#else
1726# define IS_IA32 0
1727#endif
1728
1729/*
1730 * We must return the syscall number to actually look up in the table.
1731 * This can be -1L to skip running any syscall at all.
1732 */
1733asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001734{
Roland McGrathd4d67152008-07-09 02:38:07 -07001735 long ret = 0;
1736
Roland McGrath380fdd72008-07-09 02:39:29 -07001737 /*
1738 * If we stepped into a sysenter/syscall insn, it trapped in
1739 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1740 * If user-mode had set TF itself, then it's still clear from
1741 * do_debug() and we need to set it again to restore the user
1742 * state. If we entered on the slow path, TF was already set.
1743 */
1744 if (test_thread_flag(TIF_SINGLESTEP))
1745 regs->flags |= X86_EFLAGS_TF;
1746
Roland McGrath86976cd2008-01-30 13:31:01 +01001747 /* do the secure computing check first */
1748 secure_computing(regs->orig_ax);
1749
Roland McGrathd4d67152008-07-09 02:38:07 -07001750 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1751 ret = -1L;
1752
Roland McGratheeea3c32008-03-16 23:36:28 -07001753 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1754 tracehook_report_syscall_entry(regs))
1755 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001756
Josh Stone66700002009-08-24 14:43:11 -07001757 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001758 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001759
Roland McGrath86976cd2008-01-30 13:31:01 +01001760 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001761 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001762 audit_syscall_entry(AUDIT_ARCH_I386,
1763 regs->orig_ax,
1764 regs->bx, regs->cx,
1765 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001766#ifdef CONFIG_X86_64
1767 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001768 audit_syscall_entry(AUDIT_ARCH_X86_64,
1769 regs->orig_ax,
1770 regs->di, regs->si,
1771 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001772#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001773 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001774
1775 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001776}
1777
Roland McGrathd4d67152008-07-09 02:38:07 -07001778asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001779{
Oleg Nesterovd5196502009-12-15 16:47:21 -08001780 bool step;
1781
Roland McGrath86976cd2008-01-30 13:31:01 +01001782 if (unlikely(current->audit_context))
1783 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1784
Josh Stone66700002009-08-24 14:43:11 -07001785 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001786 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001787
Roland McGrathd4d67152008-07-09 02:38:07 -07001788 /*
1789 * If TIF_SYSCALL_EMU is set, we only get here because of
1790 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1791 * We already reported this syscall instruction in
Oleg Nesterovd5196502009-12-15 16:47:21 -08001792 * syscall_trace_enter().
Roland McGrathd4d67152008-07-09 02:38:07 -07001793 */
Oleg Nesterovd5196502009-12-15 16:47:21 -08001794 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1795 !test_thread_flag(TIF_SYSCALL_EMU);
1796 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1797 tracehook_report_syscall_exit(regs, step);
Roland McGrathd4d67152008-07-09 02:38:07 -07001798}