blob: e79610d95971fe333305f469aae83492bf65a6e1 [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>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020025#include <linux/perf_event.h>
26#include <linux/hw_breakpoint.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/uaccess.h>
29#include <asm/pgtable.h>
30#include <asm/system.h>
31#include <asm/processor.h>
32#include <asm/i387.h>
33#include <asm/debugreg.h>
34#include <asm/ldt.h>
35#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010036#include <asm/prctl.h>
37#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010038#include <asm/ds.h>
K.Prasad72f674d2009-06-01 23:45:48 +053039#include <asm/hw_breakpoint.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010040
Roland McGrath070459d2008-01-30 13:31:53 +010041#include "tls.h"
42
Josh Stone1c569f02009-08-24 14:43:14 -070043#define CREATE_TRACE_POINTS
44#include <trace/events/syscalls.h>
45
Roland McGrath070459d2008-01-30 13:31:53 +010046enum x86_regset {
47 REGSET_GENERAL,
48 REGSET_FP,
49 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070050 REGSET_IOPERM64 = REGSET_XFP,
Roland McGrath070459d2008-01-30 13:31:53 +010051 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070052 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010053};
Markus Metzgereee3af42008-01-30 13:31:09 +010054
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * does not yet catch signals sent when the child dies.
57 * in exit.c or in signal.c.
58 */
59
Chuck Ebbert9f155b92006-01-05 23:11:29 -050060/*
61 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050062 */
Roland McGrathe39c2892008-01-30 13:31:01 +010063#define FLAG_MASK_32 ((unsigned long) \
64 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
65 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
66 X86_EFLAGS_SF | X86_EFLAGS_TF | \
67 X86_EFLAGS_DF | X86_EFLAGS_OF | \
68 X86_EFLAGS_RF | X86_EFLAGS_AC))
69
Roland McGrath2047b082008-01-30 13:31:01 +010070/*
71 * Determines whether a value may be installed in a segment register.
72 */
73static inline bool invalid_selector(u16 value)
74{
75 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
76}
77
78#ifdef CONFIG_X86_32
79
Roland McGrathe39c2892008-01-30 13:31:01 +010080#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Jaswinder Singh4fe702c2008-07-25 10:19:27 +053082static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010084 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +090085 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Roland McGrath06ee1b62008-01-30 13:31:01 +010088static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Roland McGrath06ee1b62008-01-30 13:31:01 +010090 /*
91 * Returning the value truncates it to 16 bits.
92 */
93 unsigned int retval;
94 if (offset != offsetof(struct user_regs_struct, gs))
95 retval = *pt_regs_access(task_pt_regs(task), offset);
96 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +010097 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +090098 retval = get_user_gs(task_pt_regs(task));
99 else
100 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100101 }
102 return retval;
103}
104
105static int set_segment_reg(struct task_struct *task,
106 unsigned long offset, u16 value)
107{
108 /*
109 * The value argument was already truncated to 16 bits.
110 */
Roland McGrath2047b082008-01-30 13:31:01 +0100111 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100112 return -EIO;
113
Roland McGrathc63855d2008-02-06 22:39:44 +0100114 /*
115 * For %cs and %ss we cannot permit a null selector.
116 * We can permit a bogus selector as long as it has USER_RPL.
117 * Null selectors are fine for other segment registers, but
118 * we will never get back to user mode with invalid %cs or %ss
119 * and will take the trap in iret instead. Much code relies
120 * on user_mode() to distinguish a user trap frame (which can
121 * safely use invalid selectors) from a kernel trap frame.
122 */
123 switch (offset) {
124 case offsetof(struct user_regs_struct, cs):
125 case offsetof(struct user_regs_struct, ss):
126 if (unlikely(value == 0))
127 return -EIO;
128
129 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100130 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100131 break;
132
133 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100134 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900135 set_user_gs(task_pt_regs(task), value);
136 else
137 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return 0;
141}
142
Roland McGrath2047b082008-01-30 13:31:01 +0100143#else /* CONFIG_X86_64 */
144
145#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
146
147static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
148{
149 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
150 return &regs->r15 + (offset / sizeof(regs->r15));
151}
152
153static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
154{
155 /*
156 * Returning the value truncates it to 16 bits.
157 */
158 unsigned int seg;
159
160 switch (offset) {
161 case offsetof(struct user_regs_struct, fs):
162 if (task == current) {
163 /* Older gas can't assemble movq %?s,%r?? */
164 asm("movl %%fs,%0" : "=r" (seg));
165 return seg;
166 }
167 return task->thread.fsindex;
168 case offsetof(struct user_regs_struct, gs):
169 if (task == current) {
170 asm("movl %%gs,%0" : "=r" (seg));
171 return seg;
172 }
173 return task->thread.gsindex;
174 case offsetof(struct user_regs_struct, ds):
175 if (task == current) {
176 asm("movl %%ds,%0" : "=r" (seg));
177 return seg;
178 }
179 return task->thread.ds;
180 case offsetof(struct user_regs_struct, es):
181 if (task == current) {
182 asm("movl %%es,%0" : "=r" (seg));
183 return seg;
184 }
185 return task->thread.es;
186
187 case offsetof(struct user_regs_struct, cs):
188 case offsetof(struct user_regs_struct, ss):
189 break;
190 }
191 return *pt_regs_access(task_pt_regs(task), offset);
192}
193
194static int set_segment_reg(struct task_struct *task,
195 unsigned long offset, u16 value)
196{
197 /*
198 * The value argument was already truncated to 16 bits.
199 */
200 if (invalid_selector(value))
201 return -EIO;
202
203 switch (offset) {
204 case offsetof(struct user_regs_struct,fs):
205 /*
206 * If this is setting fs as for normal 64-bit use but
207 * setting fs_base has implicitly changed it, leave it.
208 */
209 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
210 task->thread.fs != 0) ||
211 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
212 task->thread.fs == 0))
213 break;
214 task->thread.fsindex = value;
215 if (task == current)
216 loadsegment(fs, task->thread.fsindex);
217 break;
218 case offsetof(struct user_regs_struct,gs):
219 /*
220 * If this is setting gs as for normal 64-bit use but
221 * setting gs_base has implicitly changed it, leave it.
222 */
223 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
224 task->thread.gs != 0) ||
225 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
226 task->thread.gs == 0))
227 break;
228 task->thread.gsindex = value;
229 if (task == current)
230 load_gs_index(task->thread.gsindex);
231 break;
232 case offsetof(struct user_regs_struct,ds):
233 task->thread.ds = value;
234 if (task == current)
235 loadsegment(ds, task->thread.ds);
236 break;
237 case offsetof(struct user_regs_struct,es):
238 task->thread.es = value;
239 if (task == current)
240 loadsegment(es, task->thread.es);
241 break;
242
243 /*
244 * Can't actually change these in 64-bit mode.
245 */
246 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100247 if (unlikely(value == 0))
248 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100249#ifdef CONFIG_IA32_EMULATION
250 if (test_tsk_thread_flag(task, TIF_IA32))
251 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100252#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100253 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100254 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100255 if (unlikely(value == 0))
256 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100257#ifdef CONFIG_IA32_EMULATION
258 if (test_tsk_thread_flag(task, TIF_IA32))
259 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100260#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100261 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100262 }
263
264 return 0;
265}
266
Roland McGrath2047b082008-01-30 13:31:01 +0100267#endif /* CONFIG_X86_32 */
268
Roland McGrath06ee1b62008-01-30 13:31:01 +0100269static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100271 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Roland McGrath06ee1b62008-01-30 13:31:01 +0100273 /*
274 * If the debugger set TF, hide it from the readout.
275 */
276 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
277 retval &= ~X86_EFLAGS_TF;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return retval;
280}
281
Roland McGrath06ee1b62008-01-30 13:31:01 +0100282static int set_flags(struct task_struct *task, unsigned long value)
283{
284 struct pt_regs *regs = task_pt_regs(task);
285
286 /*
287 * If the user value contains TF, mark that
288 * it was not "us" (the debugger) that set it.
289 * If not, make sure it stays set if we had.
290 */
291 if (value & X86_EFLAGS_TF)
292 clear_tsk_thread_flag(task, TIF_FORCED_TF);
293 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
294 value |= X86_EFLAGS_TF;
295
296 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
297
298 return 0;
299}
300
301static int putreg(struct task_struct *child,
302 unsigned long offset, unsigned long value)
303{
304 switch (offset) {
305 case offsetof(struct user_regs_struct, cs):
306 case offsetof(struct user_regs_struct, ds):
307 case offsetof(struct user_regs_struct, es):
308 case offsetof(struct user_regs_struct, fs):
309 case offsetof(struct user_regs_struct, gs):
310 case offsetof(struct user_regs_struct, ss):
311 return set_segment_reg(child, offset, value);
312
313 case offsetof(struct user_regs_struct, flags):
314 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100315
316#ifdef CONFIG_X86_64
317 case offsetof(struct user_regs_struct,fs_base):
318 if (value >= TASK_SIZE_OF(child))
319 return -EIO;
320 /*
321 * When changing the segment base, use do_arch_prctl
322 * to set either thread.fs or thread.fsindex and the
323 * corresponding GDT slot.
324 */
325 if (child->thread.fs != value)
326 return do_arch_prctl(child, ARCH_SET_FS, value);
327 return 0;
328 case offsetof(struct user_regs_struct,gs_base):
329 /*
330 * Exactly the same here as the %fs handling above.
331 */
332 if (value >= TASK_SIZE_OF(child))
333 return -EIO;
334 if (child->thread.gs != value)
335 return do_arch_prctl(child, ARCH_SET_GS, value);
336 return 0;
337#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100338 }
339
340 *pt_regs_access(task_pt_regs(child), offset) = value;
341 return 0;
342}
343
344static unsigned long getreg(struct task_struct *task, unsigned long offset)
345{
346 switch (offset) {
347 case offsetof(struct user_regs_struct, cs):
348 case offsetof(struct user_regs_struct, ds):
349 case offsetof(struct user_regs_struct, es):
350 case offsetof(struct user_regs_struct, fs):
351 case offsetof(struct user_regs_struct, gs):
352 case offsetof(struct user_regs_struct, ss):
353 return get_segment_reg(task, offset);
354
355 case offsetof(struct user_regs_struct, flags):
356 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100357
358#ifdef CONFIG_X86_64
359 case offsetof(struct user_regs_struct, fs_base): {
360 /*
361 * do_arch_prctl may have used a GDT slot instead of
362 * the MSR. To userland, it appears the same either
363 * way, except the %fs segment selector might not be 0.
364 */
365 unsigned int seg = task->thread.fsindex;
366 if (task->thread.fs != 0)
367 return task->thread.fs;
368 if (task == current)
369 asm("movl %%fs,%0" : "=r" (seg));
370 if (seg != FS_TLS_SEL)
371 return 0;
372 return get_desc_base(&task->thread.tls_array[FS_TLS]);
373 }
374 case offsetof(struct user_regs_struct, gs_base): {
375 /*
376 * Exactly the same here as the %fs handling above.
377 */
378 unsigned int seg = task->thread.gsindex;
379 if (task->thread.gs != 0)
380 return task->thread.gs;
381 if (task == current)
382 asm("movl %%gs,%0" : "=r" (seg));
383 if (seg != GS_TLS_SEL)
384 return 0;
385 return get_desc_base(&task->thread.tls_array[GS_TLS]);
386 }
387#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100388 }
389
390 return *pt_regs_access(task_pt_regs(task), offset);
391}
392
Roland McGrath91e7b702008-01-30 13:31:52 +0100393static int genregs_get(struct task_struct *target,
394 const struct user_regset *regset,
395 unsigned int pos, unsigned int count,
396 void *kbuf, void __user *ubuf)
397{
398 if (kbuf) {
399 unsigned long *k = kbuf;
400 while (count > 0) {
401 *k++ = getreg(target, pos);
402 count -= sizeof(*k);
403 pos += sizeof(*k);
404 }
405 } else {
406 unsigned long __user *u = ubuf;
407 while (count > 0) {
408 if (__put_user(getreg(target, pos), u++))
409 return -EFAULT;
410 count -= sizeof(*u);
411 pos += sizeof(*u);
412 }
413 }
414
415 return 0;
416}
417
418static int genregs_set(struct task_struct *target,
419 const struct user_regset *regset,
420 unsigned int pos, unsigned int count,
421 const void *kbuf, const void __user *ubuf)
422{
423 int ret = 0;
424 if (kbuf) {
425 const unsigned long *k = kbuf;
426 while (count > 0 && !ret) {
427 ret = putreg(target, pos, *k++);
428 count -= sizeof(*k);
429 pos += sizeof(*k);
430 }
431 } else {
432 const unsigned long __user *u = ubuf;
433 while (count > 0 && !ret) {
434 unsigned long word;
435 ret = __get_user(word, u++);
436 if (ret)
437 break;
438 ret = putreg(target, pos, word);
439 count -= sizeof(*u);
440 pos += sizeof(*u);
441 }
442 }
443 return ret;
444}
445
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200446static void ptrace_triggered(struct perf_event *bp, void *data)
Roland McGrathd9771e82008-01-30 13:30:52 +0100447{
Roland McGrath0f534092008-01-30 13:30:59 +0100448 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200449 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100450
K.Prasad72f674d2009-06-01 23:45:48 +0530451 /*
452 * Store in the virtual DR6 register the fact that the breakpoint
453 * was hit so the thread's debugger will see it.
454 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200455 for (i = 0; i < HBP_NUM; i++) {
456 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530457 break;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200458 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100459
K.Prasad72f674d2009-06-01 23:45:48 +0530460 thread->debugreg6 |= (DR_TRAP0 << i);
461}
462
463/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200464 * Walk through every ptrace breakpoints for this thread and
465 * build the dr7 value on top of their attributes.
466 *
467 */
468static unsigned long ptrace_get_dr7(struct perf_event *bp[])
469{
470 int i;
471 int dr7 = 0;
472 struct arch_hw_breakpoint *info;
473
474 for (i = 0; i < HBP_NUM; i++) {
475 if (bp[i] && !bp[i]->attr.disabled) {
476 info = counter_arch_bp(bp[i]);
477 dr7 |= encode_dr7(i, info->len, info->type);
478 }
479 }
480
481 return dr7;
482}
483
484/*
K.Prasad72f674d2009-06-01 23:45:48 +0530485 * Handle ptrace writes to debug register 7.
486 */
487static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
488{
489 struct thread_struct *thread = &(tsk->thread);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200490 unsigned long old_dr7;
K.Prasad72f674d2009-06-01 23:45:48 +0530491 int i, orig_ret = 0, rc = 0;
492 int enabled, second_pass = 0;
493 unsigned len, type;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200494 int gen_len, gen_type;
495 struct perf_event *bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530496
497 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200498 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
K.Prasad72f674d2009-06-01 23:45:48 +0530499restore:
500 /*
501 * Loop through all the hardware breakpoints, making the
502 * appropriate changes to each.
503 */
504 for (i = 0; i < HBP_NUM; i++) {
505 enabled = decode_dr7(data, i, &len, &type);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200506 bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530507
508 if (!enabled) {
509 if (bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200510 /*
511 * Don't unregister the breakpoints right-away,
K.Prasad72f674d2009-06-01 23:45:48 +0530512 * unless all register_user_hw_breakpoint()
513 * requests have succeeded. This prevents
514 * any window of opportunity for debug
515 * register grabbing by other users.
516 */
517 if (!second_pass)
518 continue;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200519 thread->ptrace_bps[i] = NULL;
520 unregister_hw_breakpoint(bp);
K.Prasad72f674d2009-06-01 23:45:48 +0530521 }
522 continue;
523 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200524
525 /*
526 * We shoud have at least an inactive breakpoint at this
527 * slot. It means the user is writing dr7 without having
528 * written the address register first
529 */
K.Prasad72f674d2009-06-01 23:45:48 +0530530 if (!bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200531 rc = -EINVAL;
532 break;
533 }
534
535 rc = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
K.Prasad72f674d2009-06-01 23:45:48 +0530536 if (rc)
537 break;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200538
539 /*
540 * This is a temporary thing as bp is unregistered/registered
541 * to simulate modification
542 */
543 bp = modify_user_hw_breakpoint(bp, bp->attr.bp_addr, gen_len,
544 gen_type, bp->callback,
545 tsk, true);
546 thread->ptrace_bps[i] = NULL;
547
548 if (!bp) { /* incorrect bp, or we have a bug in bp API */
549 rc = -EINVAL;
550 break;
551 }
552 if (IS_ERR(bp)) {
553 rc = PTR_ERR(bp);
554 bp = NULL;
555 break;
556 }
557 thread->ptrace_bps[i] = bp;
K.Prasad72f674d2009-06-01 23:45:48 +0530558 }
559 /*
560 * Make a second pass to free the remaining unused breakpoints
561 * or to restore the original breakpoints if an error occurred.
562 */
563 if (!second_pass) {
564 second_pass = 1;
565 if (rc < 0) {
566 orig_ret = rc;
567 data = old_dr7;
568 }
569 goto restore;
570 }
571 return ((orig_ret < 0) ? orig_ret : rc);
572}
573
574/*
575 * Handle PTRACE_PEEKUSR calls for the debug register area.
576 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530577static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530578{
579 struct thread_struct *thread = &(tsk->thread);
580 unsigned long val = 0;
581
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200582 if (n < HBP_NUM) {
583 struct perf_event *bp;
584 bp = thread->ptrace_bps[n];
585 if (!bp)
586 return 0;
587 val = bp->hw.info.address;
588 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530589 val = thread->debugreg6;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200590 } else if (n == 7) {
591 val = ptrace_get_dr7(thread->ptrace_bps);
592 }
K.Prasad72f674d2009-06-01 23:45:48 +0530593 return val;
594}
595
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200596static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
597 unsigned long addr)
598{
599 struct perf_event *bp;
600 struct thread_struct *t = &tsk->thread;
601
602 if (!t->ptrace_bps[nr]) {
603 /*
604 * Put stub len and type to register (reserve) an inactive but
605 * correct bp
606 */
607 bp = register_user_hw_breakpoint(addr, HW_BREAKPOINT_LEN_1,
608 HW_BREAKPOINT_W,
609 ptrace_triggered, tsk,
610 false);
611 } else {
612 bp = t->ptrace_bps[nr];
613 t->ptrace_bps[nr] = NULL;
614 bp = modify_user_hw_breakpoint(bp, addr, bp->attr.bp_len,
615 bp->attr.bp_type,
616 bp->callback,
617 tsk,
618 bp->attr.disabled);
619 }
620
621 if (!bp)
622 return -EIO;
623 /*
624 * CHECKME: the previous code returned -EIO if the addr wasn't a
625 * valid task virtual addr. The new one will return -EINVAL in this
626 * case.
627 * -EINVAL may be what we want for in-kernel breakpoints users, but
628 * -EIO looks better for ptrace, since we refuse a register writing
629 * for the user. And anyway this is the previous behaviour.
630 */
631 if (IS_ERR(bp))
632 return PTR_ERR(bp);
633
634 t->ptrace_bps[nr] = bp;
635
636 return 0;
637}
638
K.Prasad72f674d2009-06-01 23:45:48 +0530639/*
640 * Handle PTRACE_POKEUSR calls for the debug register area.
641 */
642int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
643{
644 struct thread_struct *thread = &(tsk->thread);
645 int rc = 0;
646
647 /* There are no DR4 or DR5 registers */
648 if (n == 4 || n == 5)
649 return -EIO;
650
651 if (n == 6) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200652 thread->debugreg6 = val;
K.Prasad72f674d2009-06-01 23:45:48 +0530653 goto ret_path;
654 }
655 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200656 rc = ptrace_set_breakpoint_addr(tsk, n, val);
657 if (rc)
658 return rc;
K.Prasad72f674d2009-06-01 23:45:48 +0530659 }
660 /* All that's left is DR7 */
661 if (n == 7)
662 rc = ptrace_write_dr7(tsk, val);
663
664ret_path:
665 return rc;
Roland McGrathd9771e82008-01-30 13:30:52 +0100666}
667
Roland McGrath325af5f2008-08-08 15:58:39 -0700668/*
669 * These access the current or another (stopped) task's io permission
670 * bitmap for debugging or core dump.
671 */
672static int ioperm_active(struct task_struct *target,
673 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100674{
Roland McGrath325af5f2008-08-08 15:58:39 -0700675 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100676}
677
Roland McGrath325af5f2008-08-08 15:58:39 -0700678static int ioperm_get(struct task_struct *target,
679 const struct user_regset *regset,
680 unsigned int pos, unsigned int count,
681 void *kbuf, void __user *ubuf)
682{
683 if (!target->thread.io_bitmap_ptr)
684 return -ENXIO;
685
686 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
687 target->thread.io_bitmap_ptr,
688 0, IO_BITMAP_BYTES);
689}
690
Markus Metzger93fa7632008-04-08 11:01:58 +0200691#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgere2b371f2009-04-03 16:43:35 +0200692/*
693 * A branch trace store context.
694 *
695 * Contexts may only be installed by ptrace_bts_config() and only for
696 * ptraced tasks.
697 *
698 * Contexts are destroyed when the tracee is detached from the tracer.
699 * The actual destruction work requires interrupts enabled, so the
700 * work is deferred and will be scheduled during __ptrace_unlink().
701 *
702 * Contexts hold an additional task_struct reference on the traced
703 * task, as well as a reference on the tracer's mm.
704 *
705 * Ptrace already holds a task_struct for the duration of ptrace operations,
706 * but since destruction is deferred, it may be executed after both
707 * tracer and tracee exited.
708 */
709struct bts_context {
710 /* The branch trace handle. */
711 struct bts_tracer *tracer;
712
713 /* The buffer used to store the branch trace and its size. */
714 void *buffer;
715 unsigned int size;
716
717 /* The mm that paid for the above buffer. */
718 struct mm_struct *mm;
719
720 /* The task this context belongs to. */
721 struct task_struct *task;
722
723 /* The signal to send on a bts buffer overflow. */
724 unsigned int bts_ovfl_signal;
725
726 /* The work struct to destroy a context. */
727 struct work_struct work;
728};
729
Markus Metzger1cb81b12009-04-24 09:51:43 +0200730static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200731{
Markus Metzger1cb81b12009-04-24 09:51:43 +0200732 void *buffer = NULL;
733 int err = -ENOMEM;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200734
Markus Metzger1cb81b12009-04-24 09:51:43 +0200735 err = account_locked_memory(current->mm, current->signal->rlim, size);
736 if (err < 0)
737 return err;
738
739 buffer = kzalloc(size, GFP_KERNEL);
740 if (!buffer)
741 goto out_refund;
742
743 context->buffer = buffer;
744 context->size = size;
745 context->mm = get_task_mm(current);
746
747 return 0;
748
749 out_refund:
750 refund_locked_memory(current->mm, size);
751 return err;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200752}
753
754static inline void free_bts_buffer(struct bts_context *context)
755{
756 if (!context->buffer)
757 return;
758
759 kfree(context->buffer);
760 context->buffer = NULL;
761
Markus Metzger1cb81b12009-04-24 09:51:43 +0200762 refund_locked_memory(context->mm, context->size);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200763 context->size = 0;
764
765 mmput(context->mm);
766 context->mm = NULL;
767}
768
769static void free_bts_context_work(struct work_struct *w)
770{
771 struct bts_context *context;
772
773 context = container_of(w, struct bts_context, work);
774
775 ds_release_bts(context->tracer);
776 put_task_struct(context->task);
777 free_bts_buffer(context);
778 kfree(context);
779}
780
781static inline void free_bts_context(struct bts_context *context)
782{
783 INIT_WORK(&context->work, free_bts_context_work);
784 schedule_work(&context->work);
785}
786
787static inline struct bts_context *alloc_bts_context(struct task_struct *task)
788{
789 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
790 if (context) {
791 context->task = task;
792 task->bts = context;
793
794 get_task_struct(task);
795 }
796
797 return context;
798}
799
Markus Metzger93fa7632008-04-08 11:01:58 +0200800static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100801 struct bts_struct __user *out)
802{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200803 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100804 const struct bts_trace *trace;
805 struct bts_struct bts;
806 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200807 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100808
Markus Metzgere2b371f2009-04-03 16:43:35 +0200809 context = child->bts;
810 if (!context)
811 return -ESRCH;
812
813 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100814 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200815 return -ESRCH;
Markus Metzgerc2724772008-12-11 13:49:59 +0100816
817 at = trace->ds.top - ((index + 1) * trace->ds.size);
818 if ((void *)at < trace->ds.begin)
819 at += (trace->ds.n * trace->ds.size);
820
821 if (!trace->read)
822 return -EOPNOTSUPP;
823
Markus Metzgere2b371f2009-04-03 16:43:35 +0200824 error = trace->read(context->tracer, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200825 if (error < 0)
826 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100827
Markus Metzgerc2724772008-12-11 13:49:59 +0100828 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100829 return -EFAULT;
830
Markus Metzgerc2724772008-12-11 13:49:59 +0100831 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100832}
833
Markus Metzgera95d67f2008-01-30 13:31:20 +0100834static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100835 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100836 struct bts_struct __user *out)
837{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200838 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100839 const struct bts_trace *trace;
840 const unsigned char *at;
841 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100842
Markus Metzgere2b371f2009-04-03 16:43:35 +0200843 context = child->bts;
844 if (!context)
845 return -ESRCH;
846
847 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100848 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200849 return -ESRCH;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100850
Markus Metzgerc2724772008-12-11 13:49:59 +0100851 if (!trace->read)
852 return -EOPNOTSUPP;
853
854 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100855 return -EIO;
856
Markus Metzgerc2724772008-12-11 13:49:59 +0100857 for (at = trace->ds.begin; (void *)at < trace->ds.top;
858 out++, drained++, at += trace->ds.size) {
859 struct bts_struct bts;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100860
Markus Metzgere2b371f2009-04-03 16:43:35 +0200861 error = trace->read(context->tracer, at, &bts);
Markus Metzgerc2724772008-12-11 13:49:59 +0100862 if (error < 0)
863 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100864
Markus Metzgerc2724772008-12-11 13:49:59 +0100865 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100866 return -EFAULT;
867 }
868
Markus Metzgerc2724772008-12-11 13:49:59 +0100869 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
870
Markus Metzgere2b371f2009-04-03 16:43:35 +0200871 error = ds_reset_bts(context->tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200872 if (error < 0)
873 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100874
Markus Metzgerc2724772008-12-11 13:49:59 +0100875 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100876}
877
878static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100879 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100880 const struct ptrace_bts_config __user *ucfg)
881{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200882 struct bts_context *context;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100883 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100884 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100885
Markus Metzgercba4b652008-01-30 13:32:03 +0100886 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100887 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100888
Markus Metzgera95d67f2008-01-30 13:31:20 +0100889 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100890 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100891
Markus Metzgere2b371f2009-04-03 16:43:35 +0200892 context = child->bts;
893 if (!context)
894 context = alloc_bts_context(child);
895 if (!context)
896 return -ENOMEM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100897
Markus Metzgerc2724772008-12-11 13:49:59 +0100898 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
899 if (!cfg.signal)
900 return -EINVAL;
901
Américo Wang5a8ac9d2009-03-13 15:56:58 +0800902 return -EOPNOTSUPP;
Markus Metzgere2b371f2009-04-03 16:43:35 +0200903 context->bts_ovfl_signal = cfg.signal;
Markus Metzgerc2724772008-12-11 13:49:59 +0100904 }
905
Markus Metzgere2b371f2009-04-03 16:43:35 +0200906 ds_release_bts(context->tracer);
907 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100908
Markus Metzgere2b371f2009-04-03 16:43:35 +0200909 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
Markus Metzger1cb81b12009-04-24 09:51:43 +0200910 int err;
911
Markus Metzgere2b371f2009-04-03 16:43:35 +0200912 free_bts_buffer(context);
913 if (!cfg.size)
914 return 0;
Markus Metzgerc5dee612008-12-19 15:17:02 +0100915
Markus Metzger1cb81b12009-04-24 09:51:43 +0200916 err = alloc_bts_buffer(context, cfg.size);
917 if (err < 0)
918 return err;
Markus Metzgerc2724772008-12-11 13:49:59 +0100919 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100920
Markus Metzgerda35c372008-01-30 13:32:03 +0100921 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +0100922 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +0100923
Markus Metzgerda35c372008-01-30 13:32:03 +0100924 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +0100925 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +0100926
Markus Metzgerde79f542009-04-03 16:43:40 +0200927 context->tracer =
928 ds_request_bts_task(child, context->buffer, context->size,
929 NULL, (size_t)-1, flags);
Markus Metzgere2b371f2009-04-03 16:43:35 +0200930 if (unlikely(IS_ERR(context->tracer))) {
931 int error = PTR_ERR(context->tracer);
Markus Metzgercba4b652008-01-30 13:32:03 +0100932
Markus Metzgere2b371f2009-04-03 16:43:35 +0200933 free_bts_buffer(context);
934 context->tracer = NULL;
Markus Metzgerc2724772008-12-11 13:49:59 +0100935 return error;
936 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100937
Markus Metzgerc2724772008-12-11 13:49:59 +0100938 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100939}
940
Markus Metzgera95d67f2008-01-30 13:31:20 +0100941static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100942 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100943 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100944{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200945 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100946 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100947 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100948
Markus Metzgere2b371f2009-04-03 16:43:35 +0200949 context = child->bts;
950 if (!context)
951 return -ESRCH;
952
Markus Metzgercba4b652008-01-30 13:32:03 +0100953 if (cfg_size < sizeof(cfg))
954 return -EIO;
955
Markus Metzgere2b371f2009-04-03 16:43:35 +0200956 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100957 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200958 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +0200959
Markus Metzgera95d67f2008-01-30 13:31:20 +0100960 memset(&cfg, 0, sizeof(cfg));
Markus Metzgere2b371f2009-04-03 16:43:35 +0200961 cfg.size = trace->ds.end - trace->ds.begin;
962 cfg.signal = context->bts_ovfl_signal;
963 cfg.bts_size = sizeof(struct bts_struct);
Markus Metzger87e84072008-01-30 13:32:54 +0100964
Markus Metzger93fa7632008-04-08 11:01:58 +0200965 if (cfg.signal)
966 cfg.flags |= PTRACE_BTS_O_SIGNAL;
967
Markus Metzgerc2724772008-12-11 13:49:59 +0100968 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +0200969 cfg.flags |= PTRACE_BTS_O_TRACE;
970
Markus Metzgerc2724772008-12-11 13:49:59 +0100971 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +0200972 cfg.flags |= PTRACE_BTS_O_SCHED;
973
Markus Metzgera95d67f2008-01-30 13:31:20 +0100974 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
975 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100976
Markus Metzgera95d67f2008-01-30 13:31:20 +0100977 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100978}
979
Markus Metzgerc2724772008-12-11 13:49:59 +0100980static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -0800981{
Markus Metzgere2b371f2009-04-03 16:43:35 +0200982 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100983 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800984
Markus Metzgere2b371f2009-04-03 16:43:35 +0200985 context = child->bts;
986 if (!context)
987 return -ESRCH;
988
989 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +0100990 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +0200991 return -ESRCH;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800992
Markus Metzgerc2724772008-12-11 13:49:59 +0100993 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800994
Markus Metzgere2b371f2009-04-03 16:43:35 +0200995 return ds_reset_bts(context->tracer);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800996}
997
Markus Metzgerc2724772008-12-11 13:49:59 +0100998static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100999{
Markus Metzgere2b371f2009-04-03 16:43:35 +02001000 struct bts_context *context;
Markus Metzgerc2724772008-12-11 13:49:59 +01001001 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +01001002
Markus Metzgere2b371f2009-04-03 16:43:35 +02001003 context = child->bts;
1004 if (!context)
1005 return -ESRCH;
1006
1007 trace = ds_read_bts(context->tracer);
Markus Metzgerc2724772008-12-11 13:49:59 +01001008 if (!trace)
Markus Metzgere2b371f2009-04-03 16:43:35 +02001009 return -ESRCH;
Markus Metzger93fa7632008-04-08 11:01:58 +02001010
Markus Metzgerc2724772008-12-11 13:49:59 +01001011 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +02001012}
Markus Metzgerbf53de92008-12-19 15:10:24 +01001013
Markus Metzgere2b371f2009-04-03 16:43:35 +02001014/*
1015 * Called from __ptrace_unlink() after the child has been moved back
1016 * to its original parent.
1017 */
Markus Metzger0f481402009-04-03 16:43:48 +02001018void ptrace_bts_untrace(struct task_struct *child)
Markus Metzgerbf53de92008-12-19 15:10:24 +01001019{
1020 if (unlikely(child->bts)) {
Markus Metzgere2b371f2009-04-03 16:43:35 +02001021 free_bts_context(child->bts);
Markus Metzgerbf53de92008-12-19 15:10:24 +01001022 child->bts = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +01001023 }
1024}
Markus Metzger93fa7632008-04-08 11:01:58 +02001025#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001026
Roland McGrathd9771e82008-01-30 13:30:52 +01001027/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * Called by kernel/ptrace.c when detaching..
1029 *
1030 * Make sure the single step bit is not set.
1031 */
1032void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +01001033{
Roland McGrath7f232342008-01-30 13:30:48 +01001034 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001035#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -07001036 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001037#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Roland McGrath5a4646a2008-01-30 13:31:54 +01001040#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1041static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1042#endif
1043
Christoph Hellwig481bed42005-11-07 00:59:47 -08001044long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Roland McGrath5a4646a2008-01-30 13:31:54 +01001046 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 unsigned long __user *datap = (unsigned long __user *)data;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* read the word at location addr in the USER area. */
1051 case PTRACE_PEEKUSR: {
1052 unsigned long tmp;
1053
1054 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001055 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1056 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058
1059 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +01001060 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001062 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1063 addr <= offsetof(struct user, u_debugreg[7])) {
1064 addr -= offsetof(struct user, u_debugreg[0]);
1065 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 ret = put_user(tmp, datap);
1068 break;
1069 }
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1072 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001073 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1074 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076
Roland McGrathe9c86c72008-01-30 13:31:01 +01001077 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +01001079 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1080 addr <= offsetof(struct user, u_debugreg[7])) {
1081 addr -= offsetof(struct user, u_debugreg[0]);
1082 ret = ptrace_set_debugreg(child,
1083 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Roland McGrathe9c86c72008-01-30 13:31:01 +01001085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Roland McGrath5a4646a2008-01-30 13:31:54 +01001087 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1088 return copy_regset_to_user(child,
1089 task_user_regset_view(current),
1090 REGSET_GENERAL,
1091 0, sizeof(struct user_regs_struct),
1092 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Roland McGrath5a4646a2008-01-30 13:31:54 +01001094 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1095 return copy_regset_from_user(child,
1096 task_user_regset_view(current),
1097 REGSET_GENERAL,
1098 0, sizeof(struct user_regs_struct),
1099 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Roland McGrath5a4646a2008-01-30 13:31:54 +01001101 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1102 return copy_regset_to_user(child,
1103 task_user_regset_view(current),
1104 REGSET_FP,
1105 0, sizeof(struct user_i387_struct),
1106 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Roland McGrath5a4646a2008-01-30 13:31:54 +01001108 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1109 return copy_regset_from_user(child,
1110 task_user_regset_view(current),
1111 REGSET_FP,
1112 0, sizeof(struct user_i387_struct),
1113 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Roland McGrathe9c86c72008-01-30 13:31:01 +01001115#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +01001116 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1117 return copy_regset_to_user(child, &user_x86_32_view,
1118 REGSET_XFP,
1119 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001120 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Roland McGrath5a4646a2008-01-30 13:31:54 +01001122 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1123 return copy_regset_from_user(child, &user_x86_32_view,
1124 REGSET_XFP,
1125 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -07001126 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Roland McGrathe9c86c72008-01-30 13:31:01 +01001129#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001131 if (addr < 0)
1132 return -EIO;
1133 ret = do_get_thread_area(child, addr,
1134 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136
1137 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +01001138 if (addr < 0)
1139 return -EIO;
1140 ret = do_set_thread_area(child, addr,
1141 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +01001143#endif
1144
1145#ifdef CONFIG_X86_64
1146 /* normal 64bit interface to access TLS data.
1147 Works just like arch_prctl, except that the arguments
1148 are reversed. */
1149 case PTRACE_ARCH_PRCTL:
1150 ret = do_arch_prctl(child, data, addr);
1151 break;
1152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001154 /*
1155 * These bits need more cooking - not enabled yet:
1156 */
Markus Metzger93fa7632008-04-08 11:01:58 +02001157#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +01001158 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001159 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +01001160 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001161 break;
1162
1163 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001164 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +01001165 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001166 break;
1167
Markus Metzgerc2724772008-12-11 13:49:59 +01001168 case PTRACE_BTS_SIZE:
1169 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001170 break;
1171
1172 case PTRACE_BTS_GET:
1173 ret = ptrace_bts_read_record
1174 (child, data, (struct bts_struct __user *) addr);
1175 break;
1176
1177 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +01001178 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +01001179 break;
1180
1181 case PTRACE_BTS_DRAIN:
1182 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001183 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001184 break;
Markus Metzger93fa7632008-04-08 11:01:58 +02001185#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 default:
1188 ret = ptrace_request(child, request, addr, data);
1189 break;
1190 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return ret;
1193}
1194
Roland McGrathcb757c42008-01-30 13:31:01 +01001195#ifdef CONFIG_IA32_EMULATION
1196
Roland McGrath099cd6e2008-01-30 13:31:01 +01001197#include <linux/compat.h>
1198#include <linux/syscalls.h>
1199#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001200#include <asm/user32.h>
1201
1202#define R32(l,q) \
1203 case offsetof(struct user32, regs.l): \
1204 regs->q = value; break
1205
1206#define SEG32(rs) \
1207 case offsetof(struct user32, regs.rs): \
1208 return set_segment_reg(child, \
1209 offsetof(struct user_regs_struct, rs), \
1210 value); \
1211 break
1212
1213static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1214{
1215 struct pt_regs *regs = task_pt_regs(child);
1216
1217 switch (regno) {
1218
1219 SEG32(cs);
1220 SEG32(ds);
1221 SEG32(es);
1222 SEG32(fs);
1223 SEG32(gs);
1224 SEG32(ss);
1225
1226 R32(ebx, bx);
1227 R32(ecx, cx);
1228 R32(edx, dx);
1229 R32(edi, di);
1230 R32(esi, si);
1231 R32(ebp, bp);
1232 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001233 R32(eip, ip);
1234 R32(esp, sp);
1235
Roland McGrath40f09332008-02-28 19:57:07 -08001236 case offsetof(struct user32, regs.orig_eax):
1237 /*
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001238 * A 32-bit debugger setting orig_eax means to restore
1239 * the state of the task restarting a 32-bit syscall.
1240 * Make sure we interpret the -ERESTART* codes correctly
1241 * in case the task is not actually still sitting at the
1242 * exit from a 32-bit syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -08001243 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -07001244 regs->orig_ax = value;
1245 if (syscall_get_nr(child, regs) >= 0)
1246 task_thread_info(child)->status |= TS_COMPAT;
Roland McGrath40f09332008-02-28 19:57:07 -08001247 break;
1248
Roland McGrathcb757c42008-01-30 13:31:01 +01001249 case offsetof(struct user32, regs.eflags):
1250 return set_flags(child, value);
1251
1252 case offsetof(struct user32, u_debugreg[0]) ...
1253 offsetof(struct user32, u_debugreg[7]):
1254 regno -= offsetof(struct user32, u_debugreg[0]);
1255 return ptrace_set_debugreg(child, regno / 4, value);
1256
1257 default:
1258 if (regno > sizeof(struct user32) || (regno & 3))
1259 return -EIO;
1260
1261 /*
1262 * Other dummy fields in the virtual user structure
1263 * are ignored
1264 */
1265 break;
1266 }
1267 return 0;
1268}
1269
1270#undef R32
1271#undef SEG32
1272
1273#define R32(l,q) \
1274 case offsetof(struct user32, regs.l): \
1275 *val = regs->q; break
1276
1277#define SEG32(rs) \
1278 case offsetof(struct user32, regs.rs): \
1279 *val = get_segment_reg(child, \
1280 offsetof(struct user_regs_struct, rs)); \
1281 break
1282
1283static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1284{
1285 struct pt_regs *regs = task_pt_regs(child);
1286
1287 switch (regno) {
1288
1289 SEG32(ds);
1290 SEG32(es);
1291 SEG32(fs);
1292 SEG32(gs);
1293
1294 R32(cs, cs);
1295 R32(ss, ss);
1296 R32(ebx, bx);
1297 R32(ecx, cx);
1298 R32(edx, dx);
1299 R32(edi, di);
1300 R32(esi, si);
1301 R32(ebp, bp);
1302 R32(eax, ax);
1303 R32(orig_eax, orig_ax);
1304 R32(eip, ip);
1305 R32(esp, sp);
1306
1307 case offsetof(struct user32, regs.eflags):
1308 *val = get_flags(child);
1309 break;
1310
1311 case offsetof(struct user32, u_debugreg[0]) ...
1312 offsetof(struct user32, u_debugreg[7]):
1313 regno -= offsetof(struct user32, u_debugreg[0]);
1314 *val = ptrace_get_debugreg(child, regno / 4);
1315 break;
1316
1317 default:
1318 if (regno > sizeof(struct user32) || (regno & 3))
1319 return -EIO;
1320
1321 /*
1322 * Other dummy fields in the virtual user structure
1323 * are ignored
1324 */
1325 *val = 0;
1326 break;
1327 }
1328 return 0;
1329}
1330
1331#undef R32
1332#undef SEG32
1333
Roland McGrath91e7b702008-01-30 13:31:52 +01001334static int genregs32_get(struct task_struct *target,
1335 const struct user_regset *regset,
1336 unsigned int pos, unsigned int count,
1337 void *kbuf, void __user *ubuf)
1338{
1339 if (kbuf) {
1340 compat_ulong_t *k = kbuf;
1341 while (count > 0) {
1342 getreg32(target, pos, k++);
1343 count -= sizeof(*k);
1344 pos += sizeof(*k);
1345 }
1346 } else {
1347 compat_ulong_t __user *u = ubuf;
1348 while (count > 0) {
1349 compat_ulong_t word;
1350 getreg32(target, pos, &word);
1351 if (__put_user(word, u++))
1352 return -EFAULT;
1353 count -= sizeof(*u);
1354 pos += sizeof(*u);
1355 }
1356 }
1357
1358 return 0;
1359}
1360
1361static int genregs32_set(struct task_struct *target,
1362 const struct user_regset *regset,
1363 unsigned int pos, unsigned int count,
1364 const void *kbuf, const void __user *ubuf)
1365{
1366 int ret = 0;
1367 if (kbuf) {
1368 const compat_ulong_t *k = kbuf;
1369 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001370 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001371 count -= sizeof(*k);
1372 pos += sizeof(*k);
1373 }
1374 } else {
1375 const compat_ulong_t __user *u = ubuf;
1376 while (count > 0 && !ret) {
1377 compat_ulong_t word;
1378 ret = __get_user(word, u++);
1379 if (ret)
1380 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001381 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001382 count -= sizeof(*u);
1383 pos += sizeof(*u);
1384 }
1385 }
1386 return ret;
1387}
1388
Roland McGrath562b80b2008-04-22 12:21:25 -07001389long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1390 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001391{
Roland McGrath562b80b2008-04-22 12:21:25 -07001392 unsigned long addr = caddr;
1393 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001394 void __user *datap = compat_ptr(data);
1395 int ret;
1396 __u32 val;
1397
1398 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001399 case PTRACE_PEEKUSR:
1400 ret = getreg32(child, addr, &val);
1401 if (ret == 0)
1402 ret = put_user(val, (__u32 __user *)datap);
1403 break;
1404
1405 case PTRACE_POKEUSR:
1406 ret = putreg32(child, addr, data);
1407 break;
1408
Roland McGrath5a4646a2008-01-30 13:31:54 +01001409 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1410 return copy_regset_to_user(child, &user_x86_32_view,
1411 REGSET_GENERAL,
1412 0, sizeof(struct user_regs_struct32),
1413 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001414
Roland McGrath5a4646a2008-01-30 13:31:54 +01001415 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1416 return copy_regset_from_user(child, &user_x86_32_view,
1417 REGSET_GENERAL, 0,
1418 sizeof(struct user_regs_struct32),
1419 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001420
Roland McGrath5a4646a2008-01-30 13:31:54 +01001421 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1422 return copy_regset_to_user(child, &user_x86_32_view,
1423 REGSET_FP, 0,
1424 sizeof(struct user_i387_ia32_struct),
1425 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001426
Roland McGrath5a4646a2008-01-30 13:31:54 +01001427 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1428 return copy_regset_from_user(
1429 child, &user_x86_32_view, REGSET_FP,
1430 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001431
Roland McGrath5a4646a2008-01-30 13:31:54 +01001432 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1433 return copy_regset_to_user(child, &user_x86_32_view,
1434 REGSET_XFP, 0,
1435 sizeof(struct user32_fxsr_struct),
1436 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001437
Roland McGrath5a4646a2008-01-30 13:31:54 +01001438 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1439 return copy_regset_from_user(child, &user_x86_32_view,
1440 REGSET_XFP, 0,
1441 sizeof(struct user32_fxsr_struct),
1442 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001443
Roland McGrath562b80b2008-04-22 12:21:25 -07001444 case PTRACE_GET_THREAD_AREA:
1445 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001446#ifdef CONFIG_X86_PTRACE_BTS
1447 case PTRACE_BTS_CONFIG:
1448 case PTRACE_BTS_STATUS:
1449 case PTRACE_BTS_SIZE:
1450 case PTRACE_BTS_GET:
1451 case PTRACE_BTS_CLEAR:
1452 case PTRACE_BTS_DRAIN:
1453#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001454 return arch_ptrace(child, request, addr, data);
1455
Roland McGrath099cd6e2008-01-30 13:31:01 +01001456 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001457 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001458 }
1459
Roland McGrath099cd6e2008-01-30 13:31:01 +01001460 return ret;
1461}
1462
Roland McGrathcb757c42008-01-30 13:31:01 +01001463#endif /* CONFIG_IA32_EMULATION */
1464
Roland McGrath070459d2008-01-30 13:31:53 +01001465#ifdef CONFIG_X86_64
1466
1467static const struct user_regset x86_64_regsets[] = {
1468 [REGSET_GENERAL] = {
1469 .core_note_type = NT_PRSTATUS,
1470 .n = sizeof(struct user_regs_struct) / sizeof(long),
1471 .size = sizeof(long), .align = sizeof(long),
1472 .get = genregs_get, .set = genregs_set
1473 },
1474 [REGSET_FP] = {
1475 .core_note_type = NT_PRFPREG,
1476 .n = sizeof(struct user_i387_struct) / sizeof(long),
1477 .size = sizeof(long), .align = sizeof(long),
1478 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1479 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001480 [REGSET_IOPERM64] = {
1481 .core_note_type = NT_386_IOPERM,
1482 .n = IO_BITMAP_LONGS,
1483 .size = sizeof(long), .align = sizeof(long),
1484 .active = ioperm_active, .get = ioperm_get
1485 },
Roland McGrath070459d2008-01-30 13:31:53 +01001486};
1487
1488static const struct user_regset_view user_x86_64_view = {
1489 .name = "x86_64", .e_machine = EM_X86_64,
1490 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1491};
1492
1493#else /* CONFIG_X86_32 */
1494
1495#define user_regs_struct32 user_regs_struct
1496#define genregs32_get genregs_get
1497#define genregs32_set genregs_set
1498
Roland McGrath1f465f42008-05-09 15:43:44 -07001499#define user_i387_ia32_struct user_i387_struct
1500#define user32_fxsr_struct user_fxsr_struct
1501
Roland McGrath070459d2008-01-30 13:31:53 +01001502#endif /* CONFIG_X86_64 */
1503
1504#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1505static const struct user_regset x86_32_regsets[] = {
1506 [REGSET_GENERAL] = {
1507 .core_note_type = NT_PRSTATUS,
1508 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1509 .size = sizeof(u32), .align = sizeof(u32),
1510 .get = genregs32_get, .set = genregs32_set
1511 },
1512 [REGSET_FP] = {
1513 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001514 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001515 .size = sizeof(u32), .align = sizeof(u32),
1516 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1517 },
1518 [REGSET_XFP] = {
1519 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001520 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001521 .size = sizeof(u32), .align = sizeof(u32),
1522 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1523 },
1524 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001525 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001526 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1527 .size = sizeof(struct user_desc),
1528 .align = sizeof(struct user_desc),
1529 .active = regset_tls_active,
1530 .get = regset_tls_get, .set = regset_tls_set
1531 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001532 [REGSET_IOPERM32] = {
1533 .core_note_type = NT_386_IOPERM,
1534 .n = IO_BITMAP_BYTES / sizeof(u32),
1535 .size = sizeof(u32), .align = sizeof(u32),
1536 .active = ioperm_active, .get = ioperm_get
1537 },
Roland McGrath070459d2008-01-30 13:31:53 +01001538};
1539
1540static const struct user_regset_view user_x86_32_view = {
1541 .name = "i386", .e_machine = EM_386,
1542 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1543};
1544#endif
1545
1546const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1547{
1548#ifdef CONFIG_IA32_EMULATION
1549 if (test_tsk_thread_flag(task, TIF_IA32))
1550#endif
1551#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1552 return &user_x86_32_view;
1553#endif
1554#ifdef CONFIG_X86_64
1555 return &user_x86_64_view;
1556#endif
1557}
1558
Srinivasa Dsda654b72008-09-23 15:23:52 +05301559void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1560 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct siginfo info;
1563
1564 tsk->thread.trap_no = 1;
1565 tsk->thread.error_code = error_code;
1566
1567 memset(&info, 0, sizeof(info));
1568 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301569 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001571 /* User-mode ip? */
1572 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Simon Arlott27b46d72007-10-20 01:13:56 +02001574 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 force_sig_info(SIGTRAP, &info, tsk);
1576}
1577
Roland McGrath86976cd2008-01-30 13:31:01 +01001578
Roland McGrathd4d67152008-07-09 02:38:07 -07001579#ifdef CONFIG_X86_32
1580# define IS_IA32 1
1581#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001582# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001583#else
1584# define IS_IA32 0
1585#endif
1586
1587/*
1588 * We must return the syscall number to actually look up in the table.
1589 * This can be -1L to skip running any syscall at all.
1590 */
1591asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001592{
Roland McGrathd4d67152008-07-09 02:38:07 -07001593 long ret = 0;
1594
Roland McGrath380fdd72008-07-09 02:39:29 -07001595 /*
1596 * If we stepped into a sysenter/syscall insn, it trapped in
1597 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1598 * If user-mode had set TF itself, then it's still clear from
1599 * do_debug() and we need to set it again to restore the user
1600 * state. If we entered on the slow path, TF was already set.
1601 */
1602 if (test_thread_flag(TIF_SINGLESTEP))
1603 regs->flags |= X86_EFLAGS_TF;
1604
Roland McGrath86976cd2008-01-30 13:31:01 +01001605 /* do the secure computing check first */
1606 secure_computing(regs->orig_ax);
1607
Roland McGrathd4d67152008-07-09 02:38:07 -07001608 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1609 ret = -1L;
1610
Roland McGratheeea3c32008-03-16 23:36:28 -07001611 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1612 tracehook_report_syscall_entry(regs))
1613 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001614
Josh Stone66700002009-08-24 14:43:11 -07001615 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001616 trace_sys_enter(regs, regs->orig_ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001617
Roland McGrath86976cd2008-01-30 13:31:01 +01001618 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001619 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001620 audit_syscall_entry(AUDIT_ARCH_I386,
1621 regs->orig_ax,
1622 regs->bx, regs->cx,
1623 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001624#ifdef CONFIG_X86_64
1625 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001626 audit_syscall_entry(AUDIT_ARCH_X86_64,
1627 regs->orig_ax,
1628 regs->di, regs->si,
1629 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001630#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001631 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001632
1633 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001634}
1635
Roland McGrathd4d67152008-07-09 02:38:07 -07001636asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001637{
1638 if (unlikely(current->audit_context))
1639 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1640
Josh Stone66700002009-08-24 14:43:11 -07001641 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -07001642 trace_sys_exit(regs, regs->ax);
Frederic Weisbecker1b3fa2c2009-03-07 05:53:00 +01001643
Roland McGrathd4d67152008-07-09 02:38:07 -07001644 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001645 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001646
Roland McGrathd4d67152008-07-09 02:38:07 -07001647 /*
1648 * If TIF_SYSCALL_EMU is set, we only get here because of
1649 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1650 * We already reported this syscall instruction in
1651 * syscall_trace_enter(), so don't do any more now.
1652 */
1653 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1654 return;
1655
1656 /*
1657 * If we are single-stepping, synthesize a trap to follow the
1658 * system call instruction.
1659 */
1660 if (test_thread_flag(TIF_SINGLESTEP) &&
Oleg Nesterov43918f22009-04-02 16:58:00 -07001661 tracehook_consider_fatal_signal(current, SIGTRAP))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301662 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001663}