blob: 508b6b57d0c313812262ccb34fc4432d3a7806a8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26#include <asm/pgtable.h>
27#include <asm/system.h>
28#include <asm/processor.h>
29#include <asm/i387.h>
30#include <asm/debugreg.h>
31#include <asm/ldt.h>
32#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010033#include <asm/prctl.h>
34#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010035#include <asm/ds.h>
36
Roland McGrath070459d2008-01-30 13:31:53 +010037#include "tls.h"
38
39enum x86_regset {
40 REGSET_GENERAL,
41 REGSET_FP,
42 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070043 REGSET_IOPERM64 = REGSET_XFP,
Roland McGrath070459d2008-01-30 13:31:53 +010044 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070045 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010046};
Markus Metzgereee3af42008-01-30 13:31:09 +010047
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
51 */
52
Chuck Ebbert9f155b92006-01-05 23:11:29 -050053/*
54 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050055 */
Roland McGrathe39c2892008-01-30 13:31:01 +010056#define FLAG_MASK_32 ((unsigned long) \
57 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
58 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
59 X86_EFLAGS_SF | X86_EFLAGS_TF | \
60 X86_EFLAGS_DF | X86_EFLAGS_OF | \
61 X86_EFLAGS_RF | X86_EFLAGS_AC))
62
Roland McGrath2047b082008-01-30 13:31:01 +010063/*
64 * Determines whether a value may be installed in a segment register.
65 */
66static inline bool invalid_selector(u16 value)
67{
68 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
69}
70
71#ifdef CONFIG_X86_32
72
Roland McGrathe39c2892008-01-30 13:31:01 +010073#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jaswinder Singh4fe702c2008-07-25 10:19:27 +053075static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010077 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Roland McGrath06ee1b62008-01-30 13:31:01 +010078 regno >>= 2;
Roland McGrath62a97d42008-01-30 13:30:52 +010079 if (regno > FS)
80 --regno;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010081 return &regs->bx + regno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Roland McGrath06ee1b62008-01-30 13:31:01 +010084static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Roland McGrath06ee1b62008-01-30 13:31:01 +010086 /*
87 * Returning the value truncates it to 16 bits.
88 */
89 unsigned int retval;
90 if (offset != offsetof(struct user_regs_struct, gs))
91 retval = *pt_regs_access(task_pt_regs(task), offset);
92 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +010093 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +090094 retval = get_user_gs(task_pt_regs(task));
95 else
96 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +010097 }
98 return retval;
99}
100
101static int set_segment_reg(struct task_struct *task,
102 unsigned long offset, u16 value)
103{
104 /*
105 * The value argument was already truncated to 16 bits.
106 */
Roland McGrath2047b082008-01-30 13:31:01 +0100107 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100108 return -EIO;
109
Roland McGrathc63855d2008-02-06 22:39:44 +0100110 /*
111 * For %cs and %ss we cannot permit a null selector.
112 * We can permit a bogus selector as long as it has USER_RPL.
113 * Null selectors are fine for other segment registers, but
114 * we will never get back to user mode with invalid %cs or %ss
115 * and will take the trap in iret instead. Much code relies
116 * on user_mode() to distinguish a user trap frame (which can
117 * safely use invalid selectors) from a kernel trap frame.
118 */
119 switch (offset) {
120 case offsetof(struct user_regs_struct, cs):
121 case offsetof(struct user_regs_struct, ss):
122 if (unlikely(value == 0))
123 return -EIO;
124
125 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100126 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100127 break;
128
129 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100130 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900131 set_user_gs(task_pt_regs(task), value);
132 else
133 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
137}
138
Roland McGrath2047b082008-01-30 13:31:01 +0100139static unsigned long debugreg_addr_limit(struct task_struct *task)
140{
141 return TASK_SIZE - 3;
142}
143
144#else /* CONFIG_X86_64 */
145
146#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
147
148static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
149{
150 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
151 return &regs->r15 + (offset / sizeof(regs->r15));
152}
153
154static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
155{
156 /*
157 * Returning the value truncates it to 16 bits.
158 */
159 unsigned int seg;
160
161 switch (offset) {
162 case offsetof(struct user_regs_struct, fs):
163 if (task == current) {
164 /* Older gas can't assemble movq %?s,%r?? */
165 asm("movl %%fs,%0" : "=r" (seg));
166 return seg;
167 }
168 return task->thread.fsindex;
169 case offsetof(struct user_regs_struct, gs):
170 if (task == current) {
171 asm("movl %%gs,%0" : "=r" (seg));
172 return seg;
173 }
174 return task->thread.gsindex;
175 case offsetof(struct user_regs_struct, ds):
176 if (task == current) {
177 asm("movl %%ds,%0" : "=r" (seg));
178 return seg;
179 }
180 return task->thread.ds;
181 case offsetof(struct user_regs_struct, es):
182 if (task == current) {
183 asm("movl %%es,%0" : "=r" (seg));
184 return seg;
185 }
186 return task->thread.es;
187
188 case offsetof(struct user_regs_struct, cs):
189 case offsetof(struct user_regs_struct, ss):
190 break;
191 }
192 return *pt_regs_access(task_pt_regs(task), offset);
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 */
201 if (invalid_selector(value))
202 return -EIO;
203
204 switch (offset) {
205 case offsetof(struct user_regs_struct,fs):
206 /*
207 * If this is setting fs as for normal 64-bit use but
208 * setting fs_base has implicitly changed it, leave it.
209 */
210 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
211 task->thread.fs != 0) ||
212 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
213 task->thread.fs == 0))
214 break;
215 task->thread.fsindex = value;
216 if (task == current)
217 loadsegment(fs, task->thread.fsindex);
218 break;
219 case offsetof(struct user_regs_struct,gs):
220 /*
221 * If this is setting gs as for normal 64-bit use but
222 * setting gs_base has implicitly changed it, leave it.
223 */
224 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
225 task->thread.gs != 0) ||
226 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
227 task->thread.gs == 0))
228 break;
229 task->thread.gsindex = value;
230 if (task == current)
231 load_gs_index(task->thread.gsindex);
232 break;
233 case offsetof(struct user_regs_struct,ds):
234 task->thread.ds = value;
235 if (task == current)
236 loadsegment(ds, task->thread.ds);
237 break;
238 case offsetof(struct user_regs_struct,es):
239 task->thread.es = value;
240 if (task == current)
241 loadsegment(es, task->thread.es);
242 break;
243
244 /*
245 * Can't actually change these in 64-bit mode.
246 */
247 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100248 if (unlikely(value == 0))
249 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100250#ifdef CONFIG_IA32_EMULATION
251 if (test_tsk_thread_flag(task, TIF_IA32))
252 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100253#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100254 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100255 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100256 if (unlikely(value == 0))
257 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100258#ifdef CONFIG_IA32_EMULATION
259 if (test_tsk_thread_flag(task, TIF_IA32))
260 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100261#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100262 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100263 }
264
265 return 0;
266}
267
268static unsigned long debugreg_addr_limit(struct task_struct *task)
269{
270#ifdef CONFIG_IA32_EMULATION
271 if (test_tsk_thread_flag(task, TIF_IA32))
272 return IA32_PAGE_OFFSET - 3;
273#endif
274 return TASK_SIZE64 - 7;
275}
276
277#endif /* CONFIG_X86_32 */
278
Roland McGrath06ee1b62008-01-30 13:31:01 +0100279static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100281 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Roland McGrath06ee1b62008-01-30 13:31:01 +0100283 /*
284 * If the debugger set TF, hide it from the readout.
285 */
286 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
287 retval &= ~X86_EFLAGS_TF;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return retval;
290}
291
Roland McGrath06ee1b62008-01-30 13:31:01 +0100292static int set_flags(struct task_struct *task, unsigned long value)
293{
294 struct pt_regs *regs = task_pt_regs(task);
295
296 /*
297 * If the user value contains TF, mark that
298 * it was not "us" (the debugger) that set it.
299 * If not, make sure it stays set if we had.
300 */
301 if (value & X86_EFLAGS_TF)
302 clear_tsk_thread_flag(task, TIF_FORCED_TF);
303 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
304 value |= X86_EFLAGS_TF;
305
306 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
307
308 return 0;
309}
310
311static int putreg(struct task_struct *child,
312 unsigned long offset, unsigned long value)
313{
314 switch (offset) {
315 case offsetof(struct user_regs_struct, cs):
316 case offsetof(struct user_regs_struct, ds):
317 case offsetof(struct user_regs_struct, es):
318 case offsetof(struct user_regs_struct, fs):
319 case offsetof(struct user_regs_struct, gs):
320 case offsetof(struct user_regs_struct, ss):
321 return set_segment_reg(child, offset, value);
322
323 case offsetof(struct user_regs_struct, flags):
324 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100325
326#ifdef CONFIG_X86_64
Roland McGrath84c6f602008-03-07 14:56:02 -0800327 /*
328 * Orig_ax is really just a flag with small positive and
329 * negative values, so make sure to always sign-extend it
330 * from 32 bits so that it works correctly regardless of
331 * whether we come from a 32-bit environment or not.
332 */
333 case offsetof(struct user_regs_struct, orig_ax):
334 value = (long) (s32) value;
335 break;
336
Roland McGrath2047b082008-01-30 13:31:01 +0100337 case offsetof(struct user_regs_struct,fs_base):
338 if (value >= TASK_SIZE_OF(child))
339 return -EIO;
340 /*
341 * When changing the segment base, use do_arch_prctl
342 * to set either thread.fs or thread.fsindex and the
343 * corresponding GDT slot.
344 */
345 if (child->thread.fs != value)
346 return do_arch_prctl(child, ARCH_SET_FS, value);
347 return 0;
348 case offsetof(struct user_regs_struct,gs_base):
349 /*
350 * Exactly the same here as the %fs handling above.
351 */
352 if (value >= TASK_SIZE_OF(child))
353 return -EIO;
354 if (child->thread.gs != value)
355 return do_arch_prctl(child, ARCH_SET_GS, value);
356 return 0;
357#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100358 }
359
360 *pt_regs_access(task_pt_regs(child), offset) = value;
361 return 0;
362}
363
364static unsigned long getreg(struct task_struct *task, unsigned long offset)
365{
366 switch (offset) {
367 case offsetof(struct user_regs_struct, cs):
368 case offsetof(struct user_regs_struct, ds):
369 case offsetof(struct user_regs_struct, es):
370 case offsetof(struct user_regs_struct, fs):
371 case offsetof(struct user_regs_struct, gs):
372 case offsetof(struct user_regs_struct, ss):
373 return get_segment_reg(task, offset);
374
375 case offsetof(struct user_regs_struct, flags):
376 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100377
378#ifdef CONFIG_X86_64
379 case offsetof(struct user_regs_struct, fs_base): {
380 /*
381 * do_arch_prctl may have used a GDT slot instead of
382 * the MSR. To userland, it appears the same either
383 * way, except the %fs segment selector might not be 0.
384 */
385 unsigned int seg = task->thread.fsindex;
386 if (task->thread.fs != 0)
387 return task->thread.fs;
388 if (task == current)
389 asm("movl %%fs,%0" : "=r" (seg));
390 if (seg != FS_TLS_SEL)
391 return 0;
392 return get_desc_base(&task->thread.tls_array[FS_TLS]);
393 }
394 case offsetof(struct user_regs_struct, gs_base): {
395 /*
396 * Exactly the same here as the %fs handling above.
397 */
398 unsigned int seg = task->thread.gsindex;
399 if (task->thread.gs != 0)
400 return task->thread.gs;
401 if (task == current)
402 asm("movl %%gs,%0" : "=r" (seg));
403 if (seg != GS_TLS_SEL)
404 return 0;
405 return get_desc_base(&task->thread.tls_array[GS_TLS]);
406 }
407#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100408 }
409
410 return *pt_regs_access(task_pt_regs(task), offset);
411}
412
Roland McGrath91e7b702008-01-30 13:31:52 +0100413static int genregs_get(struct task_struct *target,
414 const struct user_regset *regset,
415 unsigned int pos, unsigned int count,
416 void *kbuf, void __user *ubuf)
417{
418 if (kbuf) {
419 unsigned long *k = kbuf;
420 while (count > 0) {
421 *k++ = getreg(target, pos);
422 count -= sizeof(*k);
423 pos += sizeof(*k);
424 }
425 } else {
426 unsigned long __user *u = ubuf;
427 while (count > 0) {
428 if (__put_user(getreg(target, pos), u++))
429 return -EFAULT;
430 count -= sizeof(*u);
431 pos += sizeof(*u);
432 }
433 }
434
435 return 0;
436}
437
438static int genregs_set(struct task_struct *target,
439 const struct user_regset *regset,
440 unsigned int pos, unsigned int count,
441 const void *kbuf, const void __user *ubuf)
442{
443 int ret = 0;
444 if (kbuf) {
445 const unsigned long *k = kbuf;
446 while (count > 0 && !ret) {
447 ret = putreg(target, pos, *k++);
448 count -= sizeof(*k);
449 pos += sizeof(*k);
450 }
451 } else {
452 const unsigned long __user *u = ubuf;
453 while (count > 0 && !ret) {
454 unsigned long word;
455 ret = __get_user(word, u++);
456 if (ret)
457 break;
458 ret = putreg(target, pos, word);
459 count -= sizeof(*u);
460 pos += sizeof(*u);
461 }
462 }
463 return ret;
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100467 * This function is trivial and will be inlined by the compiler.
468 * Having it separates the implementation details of debug
469 * registers from the interface details of ptrace.
470 */
471static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
472{
Roland McGrath0f534092008-01-30 13:30:59 +0100473 switch (n) {
474 case 0: return child->thread.debugreg0;
475 case 1: return child->thread.debugreg1;
476 case 2: return child->thread.debugreg2;
477 case 3: return child->thread.debugreg3;
478 case 6: return child->thread.debugreg6;
479 case 7: return child->thread.debugreg7;
480 }
481 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100482}
483
484static int ptrace_set_debugreg(struct task_struct *child,
485 int n, unsigned long data)
486{
Roland McGrath0f534092008-01-30 13:30:59 +0100487 int i;
488
Roland McGrathd9771e82008-01-30 13:30:52 +0100489 if (unlikely(n == 4 || n == 5))
490 return -EIO;
491
Roland McGrath2047b082008-01-30 13:31:01 +0100492 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100493 return -EIO;
494
Roland McGrath0f534092008-01-30 13:30:59 +0100495 switch (n) {
496 case 0: child->thread.debugreg0 = data; break;
497 case 1: child->thread.debugreg1 = data; break;
498 case 2: child->thread.debugreg2 = data; break;
499 case 3: child->thread.debugreg3 = data; break;
500
501 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100502 if ((data & ~0xffffffffUL) != 0)
503 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100504 child->thread.debugreg6 = data;
505 break;
506
507 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100508 /*
509 * Sanity-check data. Take one half-byte at once with
510 * check = (val >> (16 + 4*i)) & 0xf. It contains the
511 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
512 * 2 and 3 are LENi. Given a list of invalid values,
513 * we do mask |= 1 << invalid_value, so that
514 * (mask >> check) & 1 is a correct test for invalid
515 * values.
516 *
517 * R/Wi contains the type of the breakpoint /
518 * watchpoint, LENi contains the length of the watched
519 * data in the watchpoint case.
520 *
521 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100522 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100523 * - R/Wi == 0x10 (break on I/O reads or writes), so
524 * mask |= 0x4444.
525 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
526 * 0x1110.
527 *
528 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
529 *
530 * See the Intel Manual "System Programming Guide",
531 * 15.2.4
532 *
533 * Note that LENi == 0x10 is defined on x86_64 in long
534 * mode (i.e. even for 32-bit userspace software, but
535 * 64-bit kernel), so the x86_64 mask value is 0x5454.
536 * See the AMD manual no. 24593 (AMD64 System Programming)
537 */
Roland McGrath2047b082008-01-30 13:31:01 +0100538#ifdef CONFIG_X86_32
539#define DR7_MASK 0x5f54
540#else
541#define DR7_MASK 0x5554
542#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100543 data &= ~DR_CONTROL_RESERVED;
544 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100545 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100546 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100547 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100548 if (data)
549 set_tsk_thread_flag(child, TIF_DEBUG);
550 else
551 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100552 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100553 }
554
Roland McGrathd9771e82008-01-30 13:30:52 +0100555 return 0;
556}
557
Roland McGrath325af5f2008-08-08 15:58:39 -0700558/*
559 * These access the current or another (stopped) task's io permission
560 * bitmap for debugging or core dump.
561 */
562static int ioperm_active(struct task_struct *target,
563 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100564{
Roland McGrath325af5f2008-08-08 15:58:39 -0700565 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100566}
567
Roland McGrath325af5f2008-08-08 15:58:39 -0700568static int ioperm_get(struct task_struct *target,
569 const struct user_regset *regset,
570 unsigned int pos, unsigned int count,
571 void *kbuf, void __user *ubuf)
572{
573 if (!target->thread.io_bitmap_ptr)
574 return -ENXIO;
575
576 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
577 target->thread.io_bitmap_ptr,
578 0, IO_BITMAP_BYTES);
579}
580
Markus Metzger93fa7632008-04-08 11:01:58 +0200581#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzger93fa7632008-04-08 11:01:58 +0200582static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100583 struct bts_struct __user *out)
584{
Markus Metzgerc2724772008-12-11 13:49:59 +0100585 const struct bts_trace *trace;
586 struct bts_struct bts;
587 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200588 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100589
Markus Metzgerc2724772008-12-11 13:49:59 +0100590 trace = ds_read_bts(child->bts);
591 if (!trace)
592 return -EPERM;
593
594 at = trace->ds.top - ((index + 1) * trace->ds.size);
595 if ((void *)at < trace->ds.begin)
596 at += (trace->ds.n * trace->ds.size);
597
598 if (!trace->read)
599 return -EOPNOTSUPP;
600
601 error = trace->read(child->bts, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200602 if (error < 0)
603 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100604
Markus Metzgerc2724772008-12-11 13:49:59 +0100605 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100606 return -EFAULT;
607
Markus Metzgerc2724772008-12-11 13:49:59 +0100608 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100609}
610
Markus Metzgera95d67f2008-01-30 13:31:20 +0100611static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100612 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100613 struct bts_struct __user *out)
614{
Markus Metzgerc2724772008-12-11 13:49:59 +0100615 const struct bts_trace *trace;
616 const unsigned char *at;
617 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100618
Markus Metzgerc2724772008-12-11 13:49:59 +0100619 trace = ds_read_bts(child->bts);
620 if (!trace)
621 return -EPERM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100622
Markus Metzgerc2724772008-12-11 13:49:59 +0100623 if (!trace->read)
624 return -EOPNOTSUPP;
625
626 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100627 return -EIO;
628
Markus Metzgerc2724772008-12-11 13:49:59 +0100629 for (at = trace->ds.begin; (void *)at < trace->ds.top;
630 out++, drained++, at += trace->ds.size) {
631 struct bts_struct bts;
632 int error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100633
Markus Metzgerc2724772008-12-11 13:49:59 +0100634 error = trace->read(child->bts, at, &bts);
635 if (error < 0)
636 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100637
Markus Metzgerc2724772008-12-11 13:49:59 +0100638 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100639 return -EFAULT;
640 }
641
Markus Metzgerc2724772008-12-11 13:49:59 +0100642 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
643
644 error = ds_reset_bts(child->bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200645 if (error < 0)
646 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100647
Markus Metzgerc2724772008-12-11 13:49:59 +0100648 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100649}
650
Markus Metzgerc5dee612008-12-19 15:17:02 +0100651static int ptrace_bts_allocate_buffer(struct task_struct *child, size_t size)
652{
653 child->bts_buffer = alloc_locked_buffer(size);
654 if (!child->bts_buffer)
655 return -ENOMEM;
656
657 child->bts_size = size;
658
659 return 0;
660}
661
662static void ptrace_bts_free_buffer(struct task_struct *child)
663{
664 free_locked_buffer(child->bts_buffer, child->bts_size);
665 child->bts_buffer = NULL;
666 child->bts_size = 0;
667}
668
Markus Metzgera95d67f2008-01-30 13:31:20 +0100669static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100670 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100671 const struct ptrace_bts_config __user *ucfg)
672{
673 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100674 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100675
Markus Metzgercba4b652008-01-30 13:32:03 +0100676 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100677 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100678
Markus Metzgera95d67f2008-01-30 13:31:20 +0100679 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100680 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100681
Markus Metzgerc2724772008-12-11 13:49:59 +0100682 if (child->bts) {
683 ds_release_bts(child->bts);
684 child->bts = NULL;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100685 }
686
Markus Metzgerc2724772008-12-11 13:49:59 +0100687 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
688 if (!cfg.signal)
689 return -EINVAL;
690
691 return -EOPNOTSUPP;
692
693 child->thread.bts_ovfl_signal = cfg.signal;
694 }
695
696 if ((cfg.flags & PTRACE_BTS_O_ALLOC) &&
697 (cfg.size != child->bts_size)) {
Markus Metzgerc5dee612008-12-19 15:17:02 +0100698 int error;
Markus Metzgerc2724772008-12-11 13:49:59 +0100699
Markus Metzgerc5dee612008-12-19 15:17:02 +0100700 ptrace_bts_free_buffer(child);
701
702 error = ptrace_bts_allocate_buffer(child, cfg.size);
703 if (error < 0)
704 return error;
Markus Metzgerc2724772008-12-11 13:49:59 +0100705 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100706
Markus Metzgerda35c372008-01-30 13:32:03 +0100707 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +0100708 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +0100709
Markus Metzgerda35c372008-01-30 13:32:03 +0100710 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +0100711 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +0100712
Markus Metzgerc2724772008-12-11 13:49:59 +0100713 child->bts = ds_request_bts(child, child->bts_buffer, child->bts_size,
714 /* ovfl = */ NULL, /* th = */ (size_t)-1,
715 flags);
716 if (IS_ERR(child->bts)) {
717 int error = PTR_ERR(child->bts);
Markus Metzgercba4b652008-01-30 13:32:03 +0100718
Markus Metzgerc5dee612008-12-19 15:17:02 +0100719 ptrace_bts_free_buffer(child);
Markus Metzgerc2724772008-12-11 13:49:59 +0100720 child->bts = NULL;
Markus Metzgerda35c372008-01-30 13:32:03 +0100721
Markus Metzgerc2724772008-12-11 13:49:59 +0100722 return error;
723 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100724
Markus Metzgerc2724772008-12-11 13:49:59 +0100725 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100726}
727
Markus Metzgera95d67f2008-01-30 13:31:20 +0100728static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100729 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100730 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100731{
Markus Metzgerc2724772008-12-11 13:49:59 +0100732 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100733 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100734
Markus Metzgercba4b652008-01-30 13:32:03 +0100735 if (cfg_size < sizeof(cfg))
736 return -EIO;
737
Markus Metzgerc2724772008-12-11 13:49:59 +0100738 trace = ds_read_bts(child->bts);
739 if (!trace)
740 return -EPERM;
Markus Metzger93fa7632008-04-08 11:01:58 +0200741
Markus Metzgera95d67f2008-01-30 13:31:20 +0100742 memset(&cfg, 0, sizeof(cfg));
Markus Metzgerc2724772008-12-11 13:49:59 +0100743 cfg.size = trace->ds.end - trace->ds.begin;
Markus Metzger93fa7632008-04-08 11:01:58 +0200744 cfg.signal = child->thread.bts_ovfl_signal;
Markus Metzger87e84072008-01-30 13:32:54 +0100745 cfg.bts_size = sizeof(struct bts_struct);
746
Markus Metzger93fa7632008-04-08 11:01:58 +0200747 if (cfg.signal)
748 cfg.flags |= PTRACE_BTS_O_SIGNAL;
749
Markus Metzgerc2724772008-12-11 13:49:59 +0100750 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +0200751 cfg.flags |= PTRACE_BTS_O_TRACE;
752
Markus Metzgerc2724772008-12-11 13:49:59 +0100753 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +0200754 cfg.flags |= PTRACE_BTS_O_SCHED;
755
Markus Metzgera95d67f2008-01-30 13:31:20 +0100756 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
757 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100758
Markus Metzgera95d67f2008-01-30 13:31:20 +0100759 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100760}
761
Markus Metzgerc2724772008-12-11 13:49:59 +0100762static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -0800763{
Markus Metzgerc2724772008-12-11 13:49:59 +0100764 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800765
Markus Metzgerc2724772008-12-11 13:49:59 +0100766 trace = ds_read_bts(child->bts);
767 if (!trace)
768 return -EPERM;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800769
Markus Metzgerc2724772008-12-11 13:49:59 +0100770 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800771
Markus Metzgerc2724772008-12-11 13:49:59 +0100772 return ds_reset_bts(child->bts);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800773}
774
Markus Metzgerc2724772008-12-11 13:49:59 +0100775static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100776{
Markus Metzgerc2724772008-12-11 13:49:59 +0100777 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +0100778
Markus Metzgerc2724772008-12-11 13:49:59 +0100779 trace = ds_read_bts(child->bts);
780 if (!trace)
781 return -EPERM;
Markus Metzger93fa7632008-04-08 11:01:58 +0200782
Markus Metzgerc2724772008-12-11 13:49:59 +0100783 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +0200784}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100785
786static void ptrace_bts_fork(struct task_struct *tsk)
787{
788 tsk->bts = NULL;
789 tsk->bts_buffer = NULL;
790 tsk->bts_size = 0;
791 tsk->thread.bts_ovfl_signal = 0;
792}
793
794static void ptrace_bts_untrace(struct task_struct *child)
795{
796 if (unlikely(child->bts)) {
797 ds_release_bts(child->bts);
798 child->bts = NULL;
799
Markus Metzgerc5dee612008-12-19 15:17:02 +0100800 /* We cannot update total_vm and locked_vm since
801 child's mm is already gone. But we can reclaim the
802 memory. */
Markus Metzgerbf53de92008-12-19 15:10:24 +0100803 kfree(child->bts_buffer);
804 child->bts_buffer = NULL;
805 child->bts_size = 0;
806 }
807}
808
809static void ptrace_bts_detach(struct task_struct *child)
810{
Markus Metzgerc5dee612008-12-19 15:17:02 +0100811 if (unlikely(child->bts)) {
812 ds_release_bts(child->bts);
813 child->bts = NULL;
814
815 ptrace_bts_free_buffer(child);
816 }
Markus Metzgerbf53de92008-12-19 15:10:24 +0100817}
818#else
819static inline void ptrace_bts_fork(struct task_struct *tsk) {}
820static inline void ptrace_bts_detach(struct task_struct *child) {}
821static inline void ptrace_bts_untrace(struct task_struct *child) {}
Markus Metzger93fa7632008-04-08 11:01:58 +0200822#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100823
Markus Metzgerbf53de92008-12-19 15:10:24 +0100824void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
825{
826 ptrace_bts_fork(child);
827}
828
829void x86_ptrace_untrace(struct task_struct *child)
830{
831 ptrace_bts_untrace(child);
832}
833
Roland McGrathd9771e82008-01-30 13:30:52 +0100834/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * Called by kernel/ptrace.c when detaching..
836 *
837 * Make sure the single step bit is not set.
838 */
839void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100840{
Roland McGrath7f232342008-01-30 13:30:48 +0100841 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100842#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700843 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100844#endif
Markus Metzgerbf53de92008-12-19 15:10:24 +0100845 ptrace_bts_detach(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Roland McGrath5a4646a2008-01-30 13:31:54 +0100848#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
849static const struct user_regset_view user_x86_32_view; /* Initialized below. */
850#endif
851
Christoph Hellwig481bed42005-11-07 00:59:47 -0800852long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100854 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned long __user *datap = (unsigned long __user *)data;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* read the word at location addr in the USER area. */
859 case PTRACE_PEEKUSR: {
860 unsigned long tmp;
861
862 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100863 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
864 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866
867 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100868 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100870 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
871 addr <= offsetof(struct user, u_debugreg[7])) {
872 addr -= offsetof(struct user, u_debugreg[0]);
873 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 ret = put_user(tmp, datap);
876 break;
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
880 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100881 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
882 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884
Roland McGrathe9c86c72008-01-30 13:31:01 +0100885 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100887 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
888 addr <= offsetof(struct user, u_debugreg[7])) {
889 addr -= offsetof(struct user, u_debugreg[0]);
890 ret = ptrace_set_debugreg(child,
891 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Roland McGrath5a4646a2008-01-30 13:31:54 +0100895 case PTRACE_GETREGS: /* Get all gp regs from the child. */
896 return copy_regset_to_user(child,
897 task_user_regset_view(current),
898 REGSET_GENERAL,
899 0, sizeof(struct user_regs_struct),
900 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Roland McGrath5a4646a2008-01-30 13:31:54 +0100902 case PTRACE_SETREGS: /* Set all gp regs in the child. */
903 return copy_regset_from_user(child,
904 task_user_regset_view(current),
905 REGSET_GENERAL,
906 0, sizeof(struct user_regs_struct),
907 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Roland McGrath5a4646a2008-01-30 13:31:54 +0100909 case PTRACE_GETFPREGS: /* Get the child FPU state. */
910 return copy_regset_to_user(child,
911 task_user_regset_view(current),
912 REGSET_FP,
913 0, sizeof(struct user_i387_struct),
914 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Roland McGrath5a4646a2008-01-30 13:31:54 +0100916 case PTRACE_SETFPREGS: /* Set the child FPU state. */
917 return copy_regset_from_user(child,
918 task_user_regset_view(current),
919 REGSET_FP,
920 0, sizeof(struct user_i387_struct),
921 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Roland McGrathe9c86c72008-01-30 13:31:01 +0100923#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100924 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
925 return copy_regset_to_user(child, &user_x86_32_view,
926 REGSET_XFP,
927 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700928 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Roland McGrath5a4646a2008-01-30 13:31:54 +0100930 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
931 return copy_regset_from_user(child, &user_x86_32_view,
932 REGSET_XFP,
933 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700934 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Roland McGrathe9c86c72008-01-30 13:31:01 +0100937#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100939 if (addr < 0)
940 return -EIO;
941 ret = do_get_thread_area(child, addr,
942 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944
945 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100946 if (addr < 0)
947 return -EIO;
948 ret = do_set_thread_area(child, addr,
949 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100951#endif
952
953#ifdef CONFIG_X86_64
954 /* normal 64bit interface to access TLS data.
955 Works just like arch_prctl, except that the arguments
956 are reversed. */
957 case PTRACE_ARCH_PRCTL:
958 ret = do_arch_prctl(child, data, addr);
959 break;
960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100962 /*
963 * These bits need more cooking - not enabled yet:
964 */
Markus Metzger93fa7632008-04-08 11:01:58 +0200965#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +0100966 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100967 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +0100968 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100969 break;
970
971 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100972 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +0100973 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100974 break;
975
Markus Metzgerc2724772008-12-11 13:49:59 +0100976 case PTRACE_BTS_SIZE:
977 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100978 break;
979
980 case PTRACE_BTS_GET:
981 ret = ptrace_bts_read_record
982 (child, data, (struct bts_struct __user *) addr);
983 break;
984
985 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +0100986 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100987 break;
988
989 case PTRACE_BTS_DRAIN:
990 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +0100991 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100992 break;
Markus Metzger93fa7632008-04-08 11:01:58 +0200993#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 default:
996 ret = ptrace_request(child, request, addr, data);
997 break;
998 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return ret;
1001}
1002
Roland McGrathcb757c42008-01-30 13:31:01 +01001003#ifdef CONFIG_IA32_EMULATION
1004
Roland McGrath099cd6e2008-01-30 13:31:01 +01001005#include <linux/compat.h>
1006#include <linux/syscalls.h>
1007#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001008#include <asm/user32.h>
1009
1010#define R32(l,q) \
1011 case offsetof(struct user32, regs.l): \
1012 regs->q = value; break
1013
1014#define SEG32(rs) \
1015 case offsetof(struct user32, regs.rs): \
1016 return set_segment_reg(child, \
1017 offsetof(struct user_regs_struct, rs), \
1018 value); \
1019 break
1020
1021static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1022{
1023 struct pt_regs *regs = task_pt_regs(child);
1024
1025 switch (regno) {
1026
1027 SEG32(cs);
1028 SEG32(ds);
1029 SEG32(es);
1030 SEG32(fs);
1031 SEG32(gs);
1032 SEG32(ss);
1033
1034 R32(ebx, bx);
1035 R32(ecx, cx);
1036 R32(edx, dx);
1037 R32(edi, di);
1038 R32(esi, si);
1039 R32(ebp, bp);
1040 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001041 R32(eip, ip);
1042 R32(esp, sp);
1043
Roland McGrath40f09332008-02-28 19:57:07 -08001044 case offsetof(struct user32, regs.orig_eax):
1045 /*
1046 * Sign-extend the value so that orig_eax = -1
1047 * causes (long)orig_ax < 0 tests to fire correctly.
1048 */
1049 regs->orig_ax = (long) (s32) value;
1050 break;
1051
Roland McGrathcb757c42008-01-30 13:31:01 +01001052 case offsetof(struct user32, regs.eflags):
1053 return set_flags(child, value);
1054
1055 case offsetof(struct user32, u_debugreg[0]) ...
1056 offsetof(struct user32, u_debugreg[7]):
1057 regno -= offsetof(struct user32, u_debugreg[0]);
1058 return ptrace_set_debugreg(child, regno / 4, value);
1059
1060 default:
1061 if (regno > sizeof(struct user32) || (regno & 3))
1062 return -EIO;
1063
1064 /*
1065 * Other dummy fields in the virtual user structure
1066 * are ignored
1067 */
1068 break;
1069 }
1070 return 0;
1071}
1072
1073#undef R32
1074#undef SEG32
1075
1076#define R32(l,q) \
1077 case offsetof(struct user32, regs.l): \
1078 *val = regs->q; break
1079
1080#define SEG32(rs) \
1081 case offsetof(struct user32, regs.rs): \
1082 *val = get_segment_reg(child, \
1083 offsetof(struct user_regs_struct, rs)); \
1084 break
1085
1086static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1087{
1088 struct pt_regs *regs = task_pt_regs(child);
1089
1090 switch (regno) {
1091
1092 SEG32(ds);
1093 SEG32(es);
1094 SEG32(fs);
1095 SEG32(gs);
1096
1097 R32(cs, cs);
1098 R32(ss, ss);
1099 R32(ebx, bx);
1100 R32(ecx, cx);
1101 R32(edx, dx);
1102 R32(edi, di);
1103 R32(esi, si);
1104 R32(ebp, bp);
1105 R32(eax, ax);
1106 R32(orig_eax, orig_ax);
1107 R32(eip, ip);
1108 R32(esp, sp);
1109
1110 case offsetof(struct user32, regs.eflags):
1111 *val = get_flags(child);
1112 break;
1113
1114 case offsetof(struct user32, u_debugreg[0]) ...
1115 offsetof(struct user32, u_debugreg[7]):
1116 regno -= offsetof(struct user32, u_debugreg[0]);
1117 *val = ptrace_get_debugreg(child, regno / 4);
1118 break;
1119
1120 default:
1121 if (regno > sizeof(struct user32) || (regno & 3))
1122 return -EIO;
1123
1124 /*
1125 * Other dummy fields in the virtual user structure
1126 * are ignored
1127 */
1128 *val = 0;
1129 break;
1130 }
1131 return 0;
1132}
1133
1134#undef R32
1135#undef SEG32
1136
Roland McGrath91e7b702008-01-30 13:31:52 +01001137static int genregs32_get(struct task_struct *target,
1138 const struct user_regset *regset,
1139 unsigned int pos, unsigned int count,
1140 void *kbuf, void __user *ubuf)
1141{
1142 if (kbuf) {
1143 compat_ulong_t *k = kbuf;
1144 while (count > 0) {
1145 getreg32(target, pos, k++);
1146 count -= sizeof(*k);
1147 pos += sizeof(*k);
1148 }
1149 } else {
1150 compat_ulong_t __user *u = ubuf;
1151 while (count > 0) {
1152 compat_ulong_t word;
1153 getreg32(target, pos, &word);
1154 if (__put_user(word, u++))
1155 return -EFAULT;
1156 count -= sizeof(*u);
1157 pos += sizeof(*u);
1158 }
1159 }
1160
1161 return 0;
1162}
1163
1164static int genregs32_set(struct task_struct *target,
1165 const struct user_regset *regset,
1166 unsigned int pos, unsigned int count,
1167 const void *kbuf, const void __user *ubuf)
1168{
1169 int ret = 0;
1170 if (kbuf) {
1171 const compat_ulong_t *k = kbuf;
1172 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001173 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001174 count -= sizeof(*k);
1175 pos += sizeof(*k);
1176 }
1177 } else {
1178 const compat_ulong_t __user *u = ubuf;
1179 while (count > 0 && !ret) {
1180 compat_ulong_t word;
1181 ret = __get_user(word, u++);
1182 if (ret)
1183 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001184 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001185 count -= sizeof(*u);
1186 pos += sizeof(*u);
1187 }
1188 }
1189 return ret;
1190}
1191
Roland McGrath562b80b2008-04-22 12:21:25 -07001192long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1193 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001194{
Roland McGrath562b80b2008-04-22 12:21:25 -07001195 unsigned long addr = caddr;
1196 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001197 void __user *datap = compat_ptr(data);
1198 int ret;
1199 __u32 val;
1200
1201 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001202 case PTRACE_PEEKUSR:
1203 ret = getreg32(child, addr, &val);
1204 if (ret == 0)
1205 ret = put_user(val, (__u32 __user *)datap);
1206 break;
1207
1208 case PTRACE_POKEUSR:
1209 ret = putreg32(child, addr, data);
1210 break;
1211
Roland McGrath5a4646a2008-01-30 13:31:54 +01001212 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1213 return copy_regset_to_user(child, &user_x86_32_view,
1214 REGSET_GENERAL,
1215 0, sizeof(struct user_regs_struct32),
1216 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001217
Roland McGrath5a4646a2008-01-30 13:31:54 +01001218 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1219 return copy_regset_from_user(child, &user_x86_32_view,
1220 REGSET_GENERAL, 0,
1221 sizeof(struct user_regs_struct32),
1222 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001223
Roland McGrath5a4646a2008-01-30 13:31:54 +01001224 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1225 return copy_regset_to_user(child, &user_x86_32_view,
1226 REGSET_FP, 0,
1227 sizeof(struct user_i387_ia32_struct),
1228 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001229
Roland McGrath5a4646a2008-01-30 13:31:54 +01001230 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1231 return copy_regset_from_user(
1232 child, &user_x86_32_view, REGSET_FP,
1233 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001234
Roland McGrath5a4646a2008-01-30 13:31:54 +01001235 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1236 return copy_regset_to_user(child, &user_x86_32_view,
1237 REGSET_XFP, 0,
1238 sizeof(struct user32_fxsr_struct),
1239 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001240
Roland McGrath5a4646a2008-01-30 13:31:54 +01001241 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1242 return copy_regset_from_user(child, &user_x86_32_view,
1243 REGSET_XFP, 0,
1244 sizeof(struct user32_fxsr_struct),
1245 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001246
Roland McGrath562b80b2008-04-22 12:21:25 -07001247 case PTRACE_GET_THREAD_AREA:
1248 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001249#ifdef CONFIG_X86_PTRACE_BTS
1250 case PTRACE_BTS_CONFIG:
1251 case PTRACE_BTS_STATUS:
1252 case PTRACE_BTS_SIZE:
1253 case PTRACE_BTS_GET:
1254 case PTRACE_BTS_CLEAR:
1255 case PTRACE_BTS_DRAIN:
1256#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001257 return arch_ptrace(child, request, addr, data);
1258
Roland McGrath099cd6e2008-01-30 13:31:01 +01001259 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001260 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001261 }
1262
Roland McGrath099cd6e2008-01-30 13:31:01 +01001263 return ret;
1264}
1265
Roland McGrathcb757c42008-01-30 13:31:01 +01001266#endif /* CONFIG_IA32_EMULATION */
1267
Roland McGrath070459d2008-01-30 13:31:53 +01001268#ifdef CONFIG_X86_64
1269
1270static const struct user_regset x86_64_regsets[] = {
1271 [REGSET_GENERAL] = {
1272 .core_note_type = NT_PRSTATUS,
1273 .n = sizeof(struct user_regs_struct) / sizeof(long),
1274 .size = sizeof(long), .align = sizeof(long),
1275 .get = genregs_get, .set = genregs_set
1276 },
1277 [REGSET_FP] = {
1278 .core_note_type = NT_PRFPREG,
1279 .n = sizeof(struct user_i387_struct) / sizeof(long),
1280 .size = sizeof(long), .align = sizeof(long),
1281 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1282 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001283 [REGSET_IOPERM64] = {
1284 .core_note_type = NT_386_IOPERM,
1285 .n = IO_BITMAP_LONGS,
1286 .size = sizeof(long), .align = sizeof(long),
1287 .active = ioperm_active, .get = ioperm_get
1288 },
Roland McGrath070459d2008-01-30 13:31:53 +01001289};
1290
1291static const struct user_regset_view user_x86_64_view = {
1292 .name = "x86_64", .e_machine = EM_X86_64,
1293 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1294};
1295
1296#else /* CONFIG_X86_32 */
1297
1298#define user_regs_struct32 user_regs_struct
1299#define genregs32_get genregs_get
1300#define genregs32_set genregs_set
1301
Roland McGrath1f465f42008-05-09 15:43:44 -07001302#define user_i387_ia32_struct user_i387_struct
1303#define user32_fxsr_struct user_fxsr_struct
1304
Roland McGrath070459d2008-01-30 13:31:53 +01001305#endif /* CONFIG_X86_64 */
1306
1307#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1308static const struct user_regset x86_32_regsets[] = {
1309 [REGSET_GENERAL] = {
1310 .core_note_type = NT_PRSTATUS,
1311 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1312 .size = sizeof(u32), .align = sizeof(u32),
1313 .get = genregs32_get, .set = genregs32_set
1314 },
1315 [REGSET_FP] = {
1316 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001317 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001318 .size = sizeof(u32), .align = sizeof(u32),
1319 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1320 },
1321 [REGSET_XFP] = {
1322 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001323 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001324 .size = sizeof(u32), .align = sizeof(u32),
1325 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1326 },
1327 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001328 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001329 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1330 .size = sizeof(struct user_desc),
1331 .align = sizeof(struct user_desc),
1332 .active = regset_tls_active,
1333 .get = regset_tls_get, .set = regset_tls_set
1334 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001335 [REGSET_IOPERM32] = {
1336 .core_note_type = NT_386_IOPERM,
1337 .n = IO_BITMAP_BYTES / sizeof(u32),
1338 .size = sizeof(u32), .align = sizeof(u32),
1339 .active = ioperm_active, .get = ioperm_get
1340 },
Roland McGrath070459d2008-01-30 13:31:53 +01001341};
1342
1343static const struct user_regset_view user_x86_32_view = {
1344 .name = "i386", .e_machine = EM_386,
1345 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1346};
1347#endif
1348
1349const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1350{
1351#ifdef CONFIG_IA32_EMULATION
1352 if (test_tsk_thread_flag(task, TIF_IA32))
1353#endif
1354#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1355 return &user_x86_32_view;
1356#endif
1357#ifdef CONFIG_X86_64
1358 return &user_x86_64_view;
1359#endif
1360}
1361
Srinivasa Dsda654b72008-09-23 15:23:52 +05301362void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1363 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct siginfo info;
1366
1367 tsk->thread.trap_no = 1;
1368 tsk->thread.error_code = error_code;
1369
1370 memset(&info, 0, sizeof(info));
1371 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301372 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001374 /* User-mode ip? */
1375 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Simon Arlott27b46d72007-10-20 01:13:56 +02001377 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 force_sig_info(SIGTRAP, &info, tsk);
1379}
1380
Roland McGrath86976cd2008-01-30 13:31:01 +01001381
Roland McGrathd4d67152008-07-09 02:38:07 -07001382#ifdef CONFIG_X86_32
1383# define IS_IA32 1
1384#elif defined CONFIG_IA32_EMULATION
1385# define IS_IA32 test_thread_flag(TIF_IA32)
1386#else
1387# define IS_IA32 0
1388#endif
1389
1390/*
1391 * We must return the syscall number to actually look up in the table.
1392 * This can be -1L to skip running any syscall at all.
1393 */
1394asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001395{
Roland McGrathd4d67152008-07-09 02:38:07 -07001396 long ret = 0;
1397
Roland McGrath380fdd72008-07-09 02:39:29 -07001398 /*
1399 * If we stepped into a sysenter/syscall insn, it trapped in
1400 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1401 * If user-mode had set TF itself, then it's still clear from
1402 * do_debug() and we need to set it again to restore the user
1403 * state. If we entered on the slow path, TF was already set.
1404 */
1405 if (test_thread_flag(TIF_SINGLESTEP))
1406 regs->flags |= X86_EFLAGS_TF;
1407
Roland McGrath86976cd2008-01-30 13:31:01 +01001408 /* do the secure computing check first */
1409 secure_computing(regs->orig_ax);
1410
Roland McGrathd4d67152008-07-09 02:38:07 -07001411 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1412 ret = -1L;
1413
Roland McGratheeea3c32008-03-16 23:36:28 -07001414 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1415 tracehook_report_syscall_entry(regs))
1416 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001417
1418 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001419 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001420 audit_syscall_entry(AUDIT_ARCH_I386,
1421 regs->orig_ax,
1422 regs->bx, regs->cx,
1423 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001424#ifdef CONFIG_X86_64
1425 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001426 audit_syscall_entry(AUDIT_ARCH_X86_64,
1427 regs->orig_ax,
1428 regs->di, regs->si,
1429 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001430#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001431 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001432
1433 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001434}
1435
Roland McGrathd4d67152008-07-09 02:38:07 -07001436asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001437{
1438 if (unlikely(current->audit_context))
1439 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1440
Roland McGrathd4d67152008-07-09 02:38:07 -07001441 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001442 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001443
Roland McGrathd4d67152008-07-09 02:38:07 -07001444 /*
1445 * If TIF_SYSCALL_EMU is set, we only get here because of
1446 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1447 * We already reported this syscall instruction in
1448 * syscall_trace_enter(), so don't do any more now.
1449 */
1450 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1451 return;
1452
1453 /*
1454 * If we are single-stepping, synthesize a trap to follow the
1455 * system call instruction.
1456 */
1457 if (test_thread_flag(TIF_SINGLESTEP) &&
Roland McGratheeea3c32008-03-16 23:36:28 -07001458 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301459 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001460}