blob: 06ca07f6ad86243884c7852c13334d9da9604acf [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 {
93 retval = task->thread.gs;
94 if (task == current)
95 savesegment(gs, retval);
96 }
97 return retval;
98}
99
100static int set_segment_reg(struct task_struct *task,
101 unsigned long offset, u16 value)
102{
103 /*
104 * The value argument was already truncated to 16 bits.
105 */
Roland McGrath2047b082008-01-30 13:31:01 +0100106 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100107 return -EIO;
108
Roland McGrathc63855d2008-02-06 22:39:44 +0100109 /*
110 * For %cs and %ss we cannot permit a null selector.
111 * We can permit a bogus selector as long as it has USER_RPL.
112 * Null selectors are fine for other segment registers, but
113 * we will never get back to user mode with invalid %cs or %ss
114 * and will take the trap in iret instead. Much code relies
115 * on user_mode() to distinguish a user trap frame (which can
116 * safely use invalid selectors) from a kernel trap frame.
117 */
118 switch (offset) {
119 case offsetof(struct user_regs_struct, cs):
120 case offsetof(struct user_regs_struct, ss):
121 if (unlikely(value == 0))
122 return -EIO;
123
124 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100125 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100126 break;
127
128 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100129 task->thread.gs = value;
130 if (task == current)
Roland McGrath5fd4d162008-01-30 13:30:58 +0100131 /*
132 * The user-mode %gs is not affected by
133 * kernel entry, so we must update the CPU.
134 */
135 loadsegment(gs, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
Roland McGrath2047b082008-01-30 13:31:01 +0100141static unsigned long debugreg_addr_limit(struct task_struct *task)
142{
143 return TASK_SIZE - 3;
144}
145
146#else /* CONFIG_X86_64 */
147
148#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
149
150static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
151{
152 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
153 return &regs->r15 + (offset / sizeof(regs->r15));
154}
155
156static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
157{
158 /*
159 * Returning the value truncates it to 16 bits.
160 */
161 unsigned int seg;
162
163 switch (offset) {
164 case offsetof(struct user_regs_struct, fs):
165 if (task == current) {
166 /* Older gas can't assemble movq %?s,%r?? */
167 asm("movl %%fs,%0" : "=r" (seg));
168 return seg;
169 }
170 return task->thread.fsindex;
171 case offsetof(struct user_regs_struct, gs):
172 if (task == current) {
173 asm("movl %%gs,%0" : "=r" (seg));
174 return seg;
175 }
176 return task->thread.gsindex;
177 case offsetof(struct user_regs_struct, ds):
178 if (task == current) {
179 asm("movl %%ds,%0" : "=r" (seg));
180 return seg;
181 }
182 return task->thread.ds;
183 case offsetof(struct user_regs_struct, es):
184 if (task == current) {
185 asm("movl %%es,%0" : "=r" (seg));
186 return seg;
187 }
188 return task->thread.es;
189
190 case offsetof(struct user_regs_struct, cs):
191 case offsetof(struct user_regs_struct, ss):
192 break;
193 }
194 return *pt_regs_access(task_pt_regs(task), offset);
195}
196
197static int set_segment_reg(struct task_struct *task,
198 unsigned long offset, u16 value)
199{
200 /*
201 * The value argument was already truncated to 16 bits.
202 */
203 if (invalid_selector(value))
204 return -EIO;
205
206 switch (offset) {
207 case offsetof(struct user_regs_struct,fs):
208 /*
209 * If this is setting fs as for normal 64-bit use but
210 * setting fs_base has implicitly changed it, leave it.
211 */
212 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
213 task->thread.fs != 0) ||
214 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
215 task->thread.fs == 0))
216 break;
217 task->thread.fsindex = value;
218 if (task == current)
219 loadsegment(fs, task->thread.fsindex);
220 break;
221 case offsetof(struct user_regs_struct,gs):
222 /*
223 * If this is setting gs as for normal 64-bit use but
224 * setting gs_base has implicitly changed it, leave it.
225 */
226 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
227 task->thread.gs != 0) ||
228 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
229 task->thread.gs == 0))
230 break;
231 task->thread.gsindex = value;
232 if (task == current)
233 load_gs_index(task->thread.gsindex);
234 break;
235 case offsetof(struct user_regs_struct,ds):
236 task->thread.ds = value;
237 if (task == current)
238 loadsegment(ds, task->thread.ds);
239 break;
240 case offsetof(struct user_regs_struct,es):
241 task->thread.es = value;
242 if (task == current)
243 loadsegment(es, task->thread.es);
244 break;
245
246 /*
247 * Can't actually change these in 64-bit mode.
248 */
249 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100250 if (unlikely(value == 0))
251 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100252#ifdef CONFIG_IA32_EMULATION
253 if (test_tsk_thread_flag(task, TIF_IA32))
254 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100255#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100256 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100257 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100258 if (unlikely(value == 0))
259 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100260#ifdef CONFIG_IA32_EMULATION
261 if (test_tsk_thread_flag(task, TIF_IA32))
262 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100263#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100264 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100265 }
266
267 return 0;
268}
269
270static unsigned long debugreg_addr_limit(struct task_struct *task)
271{
272#ifdef CONFIG_IA32_EMULATION
273 if (test_tsk_thread_flag(task, TIF_IA32))
274 return IA32_PAGE_OFFSET - 3;
275#endif
276 return TASK_SIZE64 - 7;
277}
278
279#endif /* CONFIG_X86_32 */
280
Roland McGrath06ee1b62008-01-30 13:31:01 +0100281static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100283 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Roland McGrath06ee1b62008-01-30 13:31:01 +0100285 /*
286 * If the debugger set TF, hide it from the readout.
287 */
288 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
289 retval &= ~X86_EFLAGS_TF;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return retval;
292}
293
Roland McGrath06ee1b62008-01-30 13:31:01 +0100294static int set_flags(struct task_struct *task, unsigned long value)
295{
296 struct pt_regs *regs = task_pt_regs(task);
297
298 /*
299 * If the user value contains TF, mark that
300 * it was not "us" (the debugger) that set it.
301 * If not, make sure it stays set if we had.
302 */
303 if (value & X86_EFLAGS_TF)
304 clear_tsk_thread_flag(task, TIF_FORCED_TF);
305 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
306 value |= X86_EFLAGS_TF;
307
308 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
309
310 return 0;
311}
312
313static int putreg(struct task_struct *child,
314 unsigned long offset, unsigned long value)
315{
316 switch (offset) {
317 case offsetof(struct user_regs_struct, cs):
318 case offsetof(struct user_regs_struct, ds):
319 case offsetof(struct user_regs_struct, es):
320 case offsetof(struct user_regs_struct, fs):
321 case offsetof(struct user_regs_struct, gs):
322 case offsetof(struct user_regs_struct, ss):
323 return set_segment_reg(child, offset, value);
324
325 case offsetof(struct user_regs_struct, flags):
326 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100327
328#ifdef CONFIG_X86_64
Roland McGrath84c6f602008-03-07 14:56:02 -0800329 /*
330 * Orig_ax is really just a flag with small positive and
331 * negative values, so make sure to always sign-extend it
332 * from 32 bits so that it works correctly regardless of
333 * whether we come from a 32-bit environment or not.
334 */
335 case offsetof(struct user_regs_struct, orig_ax):
336 value = (long) (s32) value;
337 break;
338
Roland McGrath2047b082008-01-30 13:31:01 +0100339 case offsetof(struct user_regs_struct,fs_base):
340 if (value >= TASK_SIZE_OF(child))
341 return -EIO;
342 /*
343 * When changing the segment base, use do_arch_prctl
344 * to set either thread.fs or thread.fsindex and the
345 * corresponding GDT slot.
346 */
347 if (child->thread.fs != value)
348 return do_arch_prctl(child, ARCH_SET_FS, value);
349 return 0;
350 case offsetof(struct user_regs_struct,gs_base):
351 /*
352 * Exactly the same here as the %fs handling above.
353 */
354 if (value >= TASK_SIZE_OF(child))
355 return -EIO;
356 if (child->thread.gs != value)
357 return do_arch_prctl(child, ARCH_SET_GS, value);
358 return 0;
359#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100360 }
361
362 *pt_regs_access(task_pt_regs(child), offset) = value;
363 return 0;
364}
365
366static unsigned long getreg(struct task_struct *task, unsigned long offset)
367{
368 switch (offset) {
369 case offsetof(struct user_regs_struct, cs):
370 case offsetof(struct user_regs_struct, ds):
371 case offsetof(struct user_regs_struct, es):
372 case offsetof(struct user_regs_struct, fs):
373 case offsetof(struct user_regs_struct, gs):
374 case offsetof(struct user_regs_struct, ss):
375 return get_segment_reg(task, offset);
376
377 case offsetof(struct user_regs_struct, flags):
378 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100379
380#ifdef CONFIG_X86_64
381 case offsetof(struct user_regs_struct, fs_base): {
382 /*
383 * do_arch_prctl may have used a GDT slot instead of
384 * the MSR. To userland, it appears the same either
385 * way, except the %fs segment selector might not be 0.
386 */
387 unsigned int seg = task->thread.fsindex;
388 if (task->thread.fs != 0)
389 return task->thread.fs;
390 if (task == current)
391 asm("movl %%fs,%0" : "=r" (seg));
392 if (seg != FS_TLS_SEL)
393 return 0;
394 return get_desc_base(&task->thread.tls_array[FS_TLS]);
395 }
396 case offsetof(struct user_regs_struct, gs_base): {
397 /*
398 * Exactly the same here as the %fs handling above.
399 */
400 unsigned int seg = task->thread.gsindex;
401 if (task->thread.gs != 0)
402 return task->thread.gs;
403 if (task == current)
404 asm("movl %%gs,%0" : "=r" (seg));
405 if (seg != GS_TLS_SEL)
406 return 0;
407 return get_desc_base(&task->thread.tls_array[GS_TLS]);
408 }
409#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100410 }
411
412 return *pt_regs_access(task_pt_regs(task), offset);
413}
414
Roland McGrath91e7b702008-01-30 13:31:52 +0100415static int genregs_get(struct task_struct *target,
416 const struct user_regset *regset,
417 unsigned int pos, unsigned int count,
418 void *kbuf, void __user *ubuf)
419{
420 if (kbuf) {
421 unsigned long *k = kbuf;
422 while (count > 0) {
423 *k++ = getreg(target, pos);
424 count -= sizeof(*k);
425 pos += sizeof(*k);
426 }
427 } else {
428 unsigned long __user *u = ubuf;
429 while (count > 0) {
430 if (__put_user(getreg(target, pos), u++))
431 return -EFAULT;
432 count -= sizeof(*u);
433 pos += sizeof(*u);
434 }
435 }
436
437 return 0;
438}
439
440static int genregs_set(struct task_struct *target,
441 const struct user_regset *regset,
442 unsigned int pos, unsigned int count,
443 const void *kbuf, const void __user *ubuf)
444{
445 int ret = 0;
446 if (kbuf) {
447 const unsigned long *k = kbuf;
448 while (count > 0 && !ret) {
449 ret = putreg(target, pos, *k++);
450 count -= sizeof(*k);
451 pos += sizeof(*k);
452 }
453 } else {
454 const unsigned long __user *u = ubuf;
455 while (count > 0 && !ret) {
456 unsigned long word;
457 ret = __get_user(word, u++);
458 if (ret)
459 break;
460 ret = putreg(target, pos, word);
461 count -= sizeof(*u);
462 pos += sizeof(*u);
463 }
464 }
465 return ret;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100469 * This function is trivial and will be inlined by the compiler.
470 * Having it separates the implementation details of debug
471 * registers from the interface details of ptrace.
472 */
473static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
474{
Roland McGrath0f534092008-01-30 13:30:59 +0100475 switch (n) {
476 case 0: return child->thread.debugreg0;
477 case 1: return child->thread.debugreg1;
478 case 2: return child->thread.debugreg2;
479 case 3: return child->thread.debugreg3;
480 case 6: return child->thread.debugreg6;
481 case 7: return child->thread.debugreg7;
482 }
483 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100484}
485
486static int ptrace_set_debugreg(struct task_struct *child,
487 int n, unsigned long data)
488{
Roland McGrath0f534092008-01-30 13:30:59 +0100489 int i;
490
Roland McGrathd9771e82008-01-30 13:30:52 +0100491 if (unlikely(n == 4 || n == 5))
492 return -EIO;
493
Roland McGrath2047b082008-01-30 13:31:01 +0100494 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100495 return -EIO;
496
Roland McGrath0f534092008-01-30 13:30:59 +0100497 switch (n) {
498 case 0: child->thread.debugreg0 = data; break;
499 case 1: child->thread.debugreg1 = data; break;
500 case 2: child->thread.debugreg2 = data; break;
501 case 3: child->thread.debugreg3 = data; break;
502
503 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100504 if ((data & ~0xffffffffUL) != 0)
505 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100506 child->thread.debugreg6 = data;
507 break;
508
509 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100510 /*
511 * Sanity-check data. Take one half-byte at once with
512 * check = (val >> (16 + 4*i)) & 0xf. It contains the
513 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
514 * 2 and 3 are LENi. Given a list of invalid values,
515 * we do mask |= 1 << invalid_value, so that
516 * (mask >> check) & 1 is a correct test for invalid
517 * values.
518 *
519 * R/Wi contains the type of the breakpoint /
520 * watchpoint, LENi contains the length of the watched
521 * data in the watchpoint case.
522 *
523 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100524 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100525 * - R/Wi == 0x10 (break on I/O reads or writes), so
526 * mask |= 0x4444.
527 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
528 * 0x1110.
529 *
530 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
531 *
532 * See the Intel Manual "System Programming Guide",
533 * 15.2.4
534 *
535 * Note that LENi == 0x10 is defined on x86_64 in long
536 * mode (i.e. even for 32-bit userspace software, but
537 * 64-bit kernel), so the x86_64 mask value is 0x5454.
538 * See the AMD manual no. 24593 (AMD64 System Programming)
539 */
Roland McGrath2047b082008-01-30 13:31:01 +0100540#ifdef CONFIG_X86_32
541#define DR7_MASK 0x5f54
542#else
543#define DR7_MASK 0x5554
544#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100545 data &= ~DR_CONTROL_RESERVED;
546 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100547 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100548 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100549 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100550 if (data)
551 set_tsk_thread_flag(child, TIF_DEBUG);
552 else
553 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100554 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100555 }
556
Roland McGrathd9771e82008-01-30 13:30:52 +0100557 return 0;
558}
559
Roland McGrath325af5f2008-08-08 15:58:39 -0700560/*
561 * These access the current or another (stopped) task's io permission
562 * bitmap for debugging or core dump.
563 */
564static int ioperm_active(struct task_struct *target,
565 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100566{
Roland McGrath325af5f2008-08-08 15:58:39 -0700567 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100568}
569
Roland McGrath325af5f2008-08-08 15:58:39 -0700570static int ioperm_get(struct task_struct *target,
571 const struct user_regset *regset,
572 unsigned int pos, unsigned int count,
573 void *kbuf, void __user *ubuf)
574{
575 if (!target->thread.io_bitmap_ptr)
576 return -ENXIO;
577
578 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
579 target->thread.io_bitmap_ptr,
580 0, IO_BITMAP_BYTES);
581}
582
Markus Metzger93fa7632008-04-08 11:01:58 +0200583#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzger93fa7632008-04-08 11:01:58 +0200584static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100585 struct bts_struct __user *out)
586{
Markus Metzgerc2724772008-12-11 13:49:59 +0100587 const struct bts_trace *trace;
588 struct bts_struct bts;
589 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200590 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100591
Markus Metzgerc2724772008-12-11 13:49:59 +0100592 trace = ds_read_bts(child->bts);
593 if (!trace)
594 return -EPERM;
595
596 at = trace->ds.top - ((index + 1) * trace->ds.size);
597 if ((void *)at < trace->ds.begin)
598 at += (trace->ds.n * trace->ds.size);
599
600 if (!trace->read)
601 return -EOPNOTSUPP;
602
603 error = trace->read(child->bts, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200604 if (error < 0)
605 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100606
Markus Metzgerc2724772008-12-11 13:49:59 +0100607 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100608 return -EFAULT;
609
Markus Metzgerc2724772008-12-11 13:49:59 +0100610 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100611}
612
Markus Metzgera95d67f2008-01-30 13:31:20 +0100613static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100614 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100615 struct bts_struct __user *out)
616{
Markus Metzgerc2724772008-12-11 13:49:59 +0100617 const struct bts_trace *trace;
618 const unsigned char *at;
619 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100620
Markus Metzgerc2724772008-12-11 13:49:59 +0100621 trace = ds_read_bts(child->bts);
622 if (!trace)
623 return -EPERM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100624
Markus Metzgerc2724772008-12-11 13:49:59 +0100625 if (!trace->read)
626 return -EOPNOTSUPP;
627
628 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100629 return -EIO;
630
Markus Metzgerc2724772008-12-11 13:49:59 +0100631 for (at = trace->ds.begin; (void *)at < trace->ds.top;
632 out++, drained++, at += trace->ds.size) {
633 struct bts_struct bts;
634 int error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100635
Markus Metzgerc2724772008-12-11 13:49:59 +0100636 error = trace->read(child->bts, at, &bts);
637 if (error < 0)
638 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100639
Markus Metzgerc2724772008-12-11 13:49:59 +0100640 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100641 return -EFAULT;
642 }
643
Markus Metzgerc2724772008-12-11 13:49:59 +0100644 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
645
646 error = ds_reset_bts(child->bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200647 if (error < 0)
648 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100649
Markus Metzgerc2724772008-12-11 13:49:59 +0100650 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100651}
652
Markus Metzgerc5dee612008-12-19 15:17:02 +0100653static int ptrace_bts_allocate_buffer(struct task_struct *child, size_t size)
654{
655 child->bts_buffer = alloc_locked_buffer(size);
656 if (!child->bts_buffer)
657 return -ENOMEM;
658
659 child->bts_size = size;
660
661 return 0;
662}
663
664static void ptrace_bts_free_buffer(struct task_struct *child)
665{
666 free_locked_buffer(child->bts_buffer, child->bts_size);
667 child->bts_buffer = NULL;
668 child->bts_size = 0;
669}
670
Markus Metzgera95d67f2008-01-30 13:31:20 +0100671static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100672 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100673 const struct ptrace_bts_config __user *ucfg)
674{
675 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100676 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100677
Markus Metzgercba4b652008-01-30 13:32:03 +0100678 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100679 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100680
Markus Metzgera95d67f2008-01-30 13:31:20 +0100681 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100682 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100683
Markus Metzgerc2724772008-12-11 13:49:59 +0100684 if (child->bts) {
685 ds_release_bts(child->bts);
686 child->bts = NULL;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100687 }
688
Markus Metzgerc2724772008-12-11 13:49:59 +0100689 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
690 if (!cfg.signal)
691 return -EINVAL;
692
693 return -EOPNOTSUPP;
694
695 child->thread.bts_ovfl_signal = cfg.signal;
696 }
697
698 if ((cfg.flags & PTRACE_BTS_O_ALLOC) &&
699 (cfg.size != child->bts_size)) {
Markus Metzgerc5dee612008-12-19 15:17:02 +0100700 int error;
Markus Metzgerc2724772008-12-11 13:49:59 +0100701
Markus Metzgerc5dee612008-12-19 15:17:02 +0100702 ptrace_bts_free_buffer(child);
703
704 error = ptrace_bts_allocate_buffer(child, cfg.size);
705 if (error < 0)
706 return error;
Markus Metzgerc2724772008-12-11 13:49:59 +0100707 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100708
Markus Metzgerda35c372008-01-30 13:32:03 +0100709 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +0100710 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +0100711
Markus Metzgerda35c372008-01-30 13:32:03 +0100712 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +0100713 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +0100714
Markus Metzgerc2724772008-12-11 13:49:59 +0100715 child->bts = ds_request_bts(child, child->bts_buffer, child->bts_size,
716 /* ovfl = */ NULL, /* th = */ (size_t)-1,
717 flags);
718 if (IS_ERR(child->bts)) {
719 int error = PTR_ERR(child->bts);
Markus Metzgercba4b652008-01-30 13:32:03 +0100720
Markus Metzgerc5dee612008-12-19 15:17:02 +0100721 ptrace_bts_free_buffer(child);
Markus Metzgerc2724772008-12-11 13:49:59 +0100722 child->bts = NULL;
Markus Metzgerda35c372008-01-30 13:32:03 +0100723
Markus Metzgerc2724772008-12-11 13:49:59 +0100724 return error;
725 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100726
Markus Metzgerc2724772008-12-11 13:49:59 +0100727 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100728}
729
Markus Metzgera95d67f2008-01-30 13:31:20 +0100730static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100731 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100732 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100733{
Markus Metzgerc2724772008-12-11 13:49:59 +0100734 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100735 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100736
Markus Metzgercba4b652008-01-30 13:32:03 +0100737 if (cfg_size < sizeof(cfg))
738 return -EIO;
739
Markus Metzgerc2724772008-12-11 13:49:59 +0100740 trace = ds_read_bts(child->bts);
741 if (!trace)
742 return -EPERM;
Markus Metzger93fa7632008-04-08 11:01:58 +0200743
Markus Metzgera95d67f2008-01-30 13:31:20 +0100744 memset(&cfg, 0, sizeof(cfg));
Markus Metzgerc2724772008-12-11 13:49:59 +0100745 cfg.size = trace->ds.end - trace->ds.begin;
Markus Metzger93fa7632008-04-08 11:01:58 +0200746 cfg.signal = child->thread.bts_ovfl_signal;
Markus Metzger87e84072008-01-30 13:32:54 +0100747 cfg.bts_size = sizeof(struct bts_struct);
748
Markus Metzger93fa7632008-04-08 11:01:58 +0200749 if (cfg.signal)
750 cfg.flags |= PTRACE_BTS_O_SIGNAL;
751
Markus Metzgerc2724772008-12-11 13:49:59 +0100752 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +0200753 cfg.flags |= PTRACE_BTS_O_TRACE;
754
Markus Metzgerc2724772008-12-11 13:49:59 +0100755 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +0200756 cfg.flags |= PTRACE_BTS_O_SCHED;
757
Markus Metzgera95d67f2008-01-30 13:31:20 +0100758 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
759 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100760
Markus Metzgera95d67f2008-01-30 13:31:20 +0100761 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100762}
763
Markus Metzgerc2724772008-12-11 13:49:59 +0100764static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -0800765{
Markus Metzgerc2724772008-12-11 13:49:59 +0100766 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800767
Markus Metzgerc2724772008-12-11 13:49:59 +0100768 trace = ds_read_bts(child->bts);
769 if (!trace)
770 return -EPERM;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800771
Markus Metzgerc2724772008-12-11 13:49:59 +0100772 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800773
Markus Metzgerc2724772008-12-11 13:49:59 +0100774 return ds_reset_bts(child->bts);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800775}
776
Markus Metzgerc2724772008-12-11 13:49:59 +0100777static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100778{
Markus Metzgerc2724772008-12-11 13:49:59 +0100779 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +0100780
Markus Metzgerc2724772008-12-11 13:49:59 +0100781 trace = ds_read_bts(child->bts);
782 if (!trace)
783 return -EPERM;
Markus Metzger93fa7632008-04-08 11:01:58 +0200784
Markus Metzgerc2724772008-12-11 13:49:59 +0100785 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +0200786}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100787
788static void ptrace_bts_fork(struct task_struct *tsk)
789{
790 tsk->bts = NULL;
791 tsk->bts_buffer = NULL;
792 tsk->bts_size = 0;
793 tsk->thread.bts_ovfl_signal = 0;
794}
795
796static void ptrace_bts_untrace(struct task_struct *child)
797{
798 if (unlikely(child->bts)) {
799 ds_release_bts(child->bts);
800 child->bts = NULL;
801
Markus Metzgerc5dee612008-12-19 15:17:02 +0100802 /* We cannot update total_vm and locked_vm since
803 child's mm is already gone. But we can reclaim the
804 memory. */
Markus Metzgerbf53de92008-12-19 15:10:24 +0100805 kfree(child->bts_buffer);
806 child->bts_buffer = NULL;
807 child->bts_size = 0;
808 }
809}
810
811static void ptrace_bts_detach(struct task_struct *child)
812{
Markus Metzger9f339e72009-02-11 15:10:27 +0100813 /*
814 * Ptrace_detach() races with ptrace_untrace() in case
815 * the child dies and is reaped by another thread.
816 *
817 * We only do the memory accounting at this point and
818 * leave the buffer deallocation and the bts tracer
819 * release to ptrace_bts_untrace() which will be called
820 * later on with tasklist_lock held.
821 */
822 release_locked_buffer(child->bts_buffer, child->bts_size);
Markus Metzgerbf53de92008-12-19 15:10:24 +0100823}
824#else
825static inline void ptrace_bts_fork(struct task_struct *tsk) {}
826static inline void ptrace_bts_detach(struct task_struct *child) {}
827static inline void ptrace_bts_untrace(struct task_struct *child) {}
Markus Metzger93fa7632008-04-08 11:01:58 +0200828#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100829
Markus Metzgerbf53de92008-12-19 15:10:24 +0100830void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
831{
832 ptrace_bts_fork(child);
833}
834
835void x86_ptrace_untrace(struct task_struct *child)
836{
837 ptrace_bts_untrace(child);
838}
839
Roland McGrathd9771e82008-01-30 13:30:52 +0100840/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * Called by kernel/ptrace.c when detaching..
842 *
843 * Make sure the single step bit is not set.
844 */
845void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100846{
Roland McGrath7f232342008-01-30 13:30:48 +0100847 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100848#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700849 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100850#endif
Markus Metzgerbf53de92008-12-19 15:10:24 +0100851 ptrace_bts_detach(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Roland McGrath5a4646a2008-01-30 13:31:54 +0100854#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
855static const struct user_regset_view user_x86_32_view; /* Initialized below. */
856#endif
857
Christoph Hellwig481bed42005-11-07 00:59:47 -0800858long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100860 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned long __user *datap = (unsigned long __user *)data;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* read the word at location addr in the USER area. */
865 case PTRACE_PEEKUSR: {
866 unsigned long tmp;
867
868 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100869 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
870 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872
873 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100874 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100876 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
877 addr <= offsetof(struct user, u_debugreg[7])) {
878 addr -= offsetof(struct user, u_debugreg[0]);
879 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 ret = put_user(tmp, datap);
882 break;
883 }
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
886 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100887 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
888 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
890
Roland McGrathe9c86c72008-01-30 13:31:01 +0100891 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100893 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
894 addr <= offsetof(struct user, u_debugreg[7])) {
895 addr -= offsetof(struct user, u_debugreg[0]);
896 ret = ptrace_set_debugreg(child,
897 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Roland McGrath5a4646a2008-01-30 13:31:54 +0100901 case PTRACE_GETREGS: /* Get all gp regs from the child. */
902 return copy_regset_to_user(child,
903 task_user_regset_view(current),
904 REGSET_GENERAL,
905 0, sizeof(struct user_regs_struct),
906 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Roland McGrath5a4646a2008-01-30 13:31:54 +0100908 case PTRACE_SETREGS: /* Set all gp regs in the child. */
909 return copy_regset_from_user(child,
910 task_user_regset_view(current),
911 REGSET_GENERAL,
912 0, sizeof(struct user_regs_struct),
913 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Roland McGrath5a4646a2008-01-30 13:31:54 +0100915 case PTRACE_GETFPREGS: /* Get the child FPU state. */
916 return copy_regset_to_user(child,
917 task_user_regset_view(current),
918 REGSET_FP,
919 0, sizeof(struct user_i387_struct),
920 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Roland McGrath5a4646a2008-01-30 13:31:54 +0100922 case PTRACE_SETFPREGS: /* Set the child FPU state. */
923 return copy_regset_from_user(child,
924 task_user_regset_view(current),
925 REGSET_FP,
926 0, sizeof(struct user_i387_struct),
927 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Roland McGrathe9c86c72008-01-30 13:31:01 +0100929#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100930 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
931 return copy_regset_to_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Roland McGrath5a4646a2008-01-30 13:31:54 +0100936 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
937 return copy_regset_from_user(child, &user_x86_32_view,
938 REGSET_XFP,
939 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700940 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Roland McGrathe9c86c72008-01-30 13:31:01 +0100943#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100945 if (addr < 0)
946 return -EIO;
947 ret = do_get_thread_area(child, addr,
948 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950
951 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100952 if (addr < 0)
953 return -EIO;
954 ret = do_set_thread_area(child, addr,
955 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100957#endif
958
959#ifdef CONFIG_X86_64
960 /* normal 64bit interface to access TLS data.
961 Works just like arch_prctl, except that the arguments
962 are reversed. */
963 case PTRACE_ARCH_PRCTL:
964 ret = do_arch_prctl(child, data, addr);
965 break;
966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100968 /*
969 * These bits need more cooking - not enabled yet:
970 */
Markus Metzger93fa7632008-04-08 11:01:58 +0200971#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +0100972 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100973 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +0100974 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100975 break;
976
977 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100978 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +0100979 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100980 break;
981
Markus Metzgerc2724772008-12-11 13:49:59 +0100982 case PTRACE_BTS_SIZE:
983 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100984 break;
985
986 case PTRACE_BTS_GET:
987 ret = ptrace_bts_read_record
988 (child, data, (struct bts_struct __user *) addr);
989 break;
990
991 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +0100992 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100993 break;
994
995 case PTRACE_BTS_DRAIN:
996 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +0100997 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100998 break;
Markus Metzger93fa7632008-04-08 11:01:58 +0200999#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +01001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 default:
1002 ret = ptrace_request(child, request, addr, data);
1003 break;
1004 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return ret;
1007}
1008
Roland McGrathcb757c42008-01-30 13:31:01 +01001009#ifdef CONFIG_IA32_EMULATION
1010
Roland McGrath099cd6e2008-01-30 13:31:01 +01001011#include <linux/compat.h>
1012#include <linux/syscalls.h>
1013#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001014#include <asm/user32.h>
1015
1016#define R32(l,q) \
1017 case offsetof(struct user32, regs.l): \
1018 regs->q = value; break
1019
1020#define SEG32(rs) \
1021 case offsetof(struct user32, regs.rs): \
1022 return set_segment_reg(child, \
1023 offsetof(struct user_regs_struct, rs), \
1024 value); \
1025 break
1026
1027static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1028{
1029 struct pt_regs *regs = task_pt_regs(child);
1030
1031 switch (regno) {
1032
1033 SEG32(cs);
1034 SEG32(ds);
1035 SEG32(es);
1036 SEG32(fs);
1037 SEG32(gs);
1038 SEG32(ss);
1039
1040 R32(ebx, bx);
1041 R32(ecx, cx);
1042 R32(edx, dx);
1043 R32(edi, di);
1044 R32(esi, si);
1045 R32(ebp, bp);
1046 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001047 R32(eip, ip);
1048 R32(esp, sp);
1049
Roland McGrath40f09332008-02-28 19:57:07 -08001050 case offsetof(struct user32, regs.orig_eax):
1051 /*
1052 * Sign-extend the value so that orig_eax = -1
1053 * causes (long)orig_ax < 0 tests to fire correctly.
1054 */
1055 regs->orig_ax = (long) (s32) value;
1056 break;
1057
Roland McGrathcb757c42008-01-30 13:31:01 +01001058 case offsetof(struct user32, regs.eflags):
1059 return set_flags(child, value);
1060
1061 case offsetof(struct user32, u_debugreg[0]) ...
1062 offsetof(struct user32, u_debugreg[7]):
1063 regno -= offsetof(struct user32, u_debugreg[0]);
1064 return ptrace_set_debugreg(child, regno / 4, value);
1065
1066 default:
1067 if (regno > sizeof(struct user32) || (regno & 3))
1068 return -EIO;
1069
1070 /*
1071 * Other dummy fields in the virtual user structure
1072 * are ignored
1073 */
1074 break;
1075 }
1076 return 0;
1077}
1078
1079#undef R32
1080#undef SEG32
1081
1082#define R32(l,q) \
1083 case offsetof(struct user32, regs.l): \
1084 *val = regs->q; break
1085
1086#define SEG32(rs) \
1087 case offsetof(struct user32, regs.rs): \
1088 *val = get_segment_reg(child, \
1089 offsetof(struct user_regs_struct, rs)); \
1090 break
1091
1092static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1093{
1094 struct pt_regs *regs = task_pt_regs(child);
1095
1096 switch (regno) {
1097
1098 SEG32(ds);
1099 SEG32(es);
1100 SEG32(fs);
1101 SEG32(gs);
1102
1103 R32(cs, cs);
1104 R32(ss, ss);
1105 R32(ebx, bx);
1106 R32(ecx, cx);
1107 R32(edx, dx);
1108 R32(edi, di);
1109 R32(esi, si);
1110 R32(ebp, bp);
1111 R32(eax, ax);
1112 R32(orig_eax, orig_ax);
1113 R32(eip, ip);
1114 R32(esp, sp);
1115
1116 case offsetof(struct user32, regs.eflags):
1117 *val = get_flags(child);
1118 break;
1119
1120 case offsetof(struct user32, u_debugreg[0]) ...
1121 offsetof(struct user32, u_debugreg[7]):
1122 regno -= offsetof(struct user32, u_debugreg[0]);
1123 *val = ptrace_get_debugreg(child, regno / 4);
1124 break;
1125
1126 default:
1127 if (regno > sizeof(struct user32) || (regno & 3))
1128 return -EIO;
1129
1130 /*
1131 * Other dummy fields in the virtual user structure
1132 * are ignored
1133 */
1134 *val = 0;
1135 break;
1136 }
1137 return 0;
1138}
1139
1140#undef R32
1141#undef SEG32
1142
Roland McGrath91e7b702008-01-30 13:31:52 +01001143static int genregs32_get(struct task_struct *target,
1144 const struct user_regset *regset,
1145 unsigned int pos, unsigned int count,
1146 void *kbuf, void __user *ubuf)
1147{
1148 if (kbuf) {
1149 compat_ulong_t *k = kbuf;
1150 while (count > 0) {
1151 getreg32(target, pos, k++);
1152 count -= sizeof(*k);
1153 pos += sizeof(*k);
1154 }
1155 } else {
1156 compat_ulong_t __user *u = ubuf;
1157 while (count > 0) {
1158 compat_ulong_t word;
1159 getreg32(target, pos, &word);
1160 if (__put_user(word, u++))
1161 return -EFAULT;
1162 count -= sizeof(*u);
1163 pos += sizeof(*u);
1164 }
1165 }
1166
1167 return 0;
1168}
1169
1170static int genregs32_set(struct task_struct *target,
1171 const struct user_regset *regset,
1172 unsigned int pos, unsigned int count,
1173 const void *kbuf, const void __user *ubuf)
1174{
1175 int ret = 0;
1176 if (kbuf) {
1177 const compat_ulong_t *k = kbuf;
1178 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001179 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001180 count -= sizeof(*k);
1181 pos += sizeof(*k);
1182 }
1183 } else {
1184 const compat_ulong_t __user *u = ubuf;
1185 while (count > 0 && !ret) {
1186 compat_ulong_t word;
1187 ret = __get_user(word, u++);
1188 if (ret)
1189 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001190 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001191 count -= sizeof(*u);
1192 pos += sizeof(*u);
1193 }
1194 }
1195 return ret;
1196}
1197
Roland McGrath562b80b2008-04-22 12:21:25 -07001198long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1199 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001200{
Roland McGrath562b80b2008-04-22 12:21:25 -07001201 unsigned long addr = caddr;
1202 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001203 void __user *datap = compat_ptr(data);
1204 int ret;
1205 __u32 val;
1206
1207 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001208 case PTRACE_PEEKUSR:
1209 ret = getreg32(child, addr, &val);
1210 if (ret == 0)
1211 ret = put_user(val, (__u32 __user *)datap);
1212 break;
1213
1214 case PTRACE_POKEUSR:
1215 ret = putreg32(child, addr, data);
1216 break;
1217
Roland McGrath5a4646a2008-01-30 13:31:54 +01001218 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1219 return copy_regset_to_user(child, &user_x86_32_view,
1220 REGSET_GENERAL,
1221 0, 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_SETREGS: /* Set all gp regs in the child. */
1225 return copy_regset_from_user(child, &user_x86_32_view,
1226 REGSET_GENERAL, 0,
1227 sizeof(struct user_regs_struct32),
1228 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001229
Roland McGrath5a4646a2008-01-30 13:31:54 +01001230 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1231 return copy_regset_to_user(child, &user_x86_32_view,
1232 REGSET_FP, 0,
1233 sizeof(struct user_i387_ia32_struct),
1234 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001235
Roland McGrath5a4646a2008-01-30 13:31:54 +01001236 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1237 return copy_regset_from_user(
1238 child, &user_x86_32_view, REGSET_FP,
1239 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001240
Roland McGrath5a4646a2008-01-30 13:31:54 +01001241 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1242 return copy_regset_to_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 McGrath5a4646a2008-01-30 13:31:54 +01001247 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1248 return copy_regset_from_user(child, &user_x86_32_view,
1249 REGSET_XFP, 0,
1250 sizeof(struct user32_fxsr_struct),
1251 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001252
Roland McGrath562b80b2008-04-22 12:21:25 -07001253 case PTRACE_GET_THREAD_AREA:
1254 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001255#ifdef CONFIG_X86_PTRACE_BTS
1256 case PTRACE_BTS_CONFIG:
1257 case PTRACE_BTS_STATUS:
1258 case PTRACE_BTS_SIZE:
1259 case PTRACE_BTS_GET:
1260 case PTRACE_BTS_CLEAR:
1261 case PTRACE_BTS_DRAIN:
1262#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001263 return arch_ptrace(child, request, addr, data);
1264
Roland McGrath099cd6e2008-01-30 13:31:01 +01001265 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001266 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001267 }
1268
Roland McGrath099cd6e2008-01-30 13:31:01 +01001269 return ret;
1270}
1271
Roland McGrathcb757c42008-01-30 13:31:01 +01001272#endif /* CONFIG_IA32_EMULATION */
1273
Roland McGrath070459d2008-01-30 13:31:53 +01001274#ifdef CONFIG_X86_64
1275
1276static const struct user_regset x86_64_regsets[] = {
1277 [REGSET_GENERAL] = {
1278 .core_note_type = NT_PRSTATUS,
1279 .n = sizeof(struct user_regs_struct) / sizeof(long),
1280 .size = sizeof(long), .align = sizeof(long),
1281 .get = genregs_get, .set = genregs_set
1282 },
1283 [REGSET_FP] = {
1284 .core_note_type = NT_PRFPREG,
1285 .n = sizeof(struct user_i387_struct) / sizeof(long),
1286 .size = sizeof(long), .align = sizeof(long),
1287 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1288 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001289 [REGSET_IOPERM64] = {
1290 .core_note_type = NT_386_IOPERM,
1291 .n = IO_BITMAP_LONGS,
1292 .size = sizeof(long), .align = sizeof(long),
1293 .active = ioperm_active, .get = ioperm_get
1294 },
Roland McGrath070459d2008-01-30 13:31:53 +01001295};
1296
1297static const struct user_regset_view user_x86_64_view = {
1298 .name = "x86_64", .e_machine = EM_X86_64,
1299 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1300};
1301
1302#else /* CONFIG_X86_32 */
1303
1304#define user_regs_struct32 user_regs_struct
1305#define genregs32_get genregs_get
1306#define genregs32_set genregs_set
1307
Roland McGrath1f465f42008-05-09 15:43:44 -07001308#define user_i387_ia32_struct user_i387_struct
1309#define user32_fxsr_struct user_fxsr_struct
1310
Roland McGrath070459d2008-01-30 13:31:53 +01001311#endif /* CONFIG_X86_64 */
1312
1313#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1314static const struct user_regset x86_32_regsets[] = {
1315 [REGSET_GENERAL] = {
1316 .core_note_type = NT_PRSTATUS,
1317 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1318 .size = sizeof(u32), .align = sizeof(u32),
1319 .get = genregs32_get, .set = genregs32_set
1320 },
1321 [REGSET_FP] = {
1322 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001323 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001324 .size = sizeof(u32), .align = sizeof(u32),
1325 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1326 },
1327 [REGSET_XFP] = {
1328 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001329 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001330 .size = sizeof(u32), .align = sizeof(u32),
1331 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1332 },
1333 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001334 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001335 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1336 .size = sizeof(struct user_desc),
1337 .align = sizeof(struct user_desc),
1338 .active = regset_tls_active,
1339 .get = regset_tls_get, .set = regset_tls_set
1340 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001341 [REGSET_IOPERM32] = {
1342 .core_note_type = NT_386_IOPERM,
1343 .n = IO_BITMAP_BYTES / sizeof(u32),
1344 .size = sizeof(u32), .align = sizeof(u32),
1345 .active = ioperm_active, .get = ioperm_get
1346 },
Roland McGrath070459d2008-01-30 13:31:53 +01001347};
1348
1349static const struct user_regset_view user_x86_32_view = {
1350 .name = "i386", .e_machine = EM_386,
1351 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1352};
1353#endif
1354
1355const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1356{
1357#ifdef CONFIG_IA32_EMULATION
1358 if (test_tsk_thread_flag(task, TIF_IA32))
1359#endif
1360#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1361 return &user_x86_32_view;
1362#endif
1363#ifdef CONFIG_X86_64
1364 return &user_x86_64_view;
1365#endif
1366}
1367
Srinivasa Dsda654b72008-09-23 15:23:52 +05301368void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1369 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct siginfo info;
1372
1373 tsk->thread.trap_no = 1;
1374 tsk->thread.error_code = error_code;
1375
1376 memset(&info, 0, sizeof(info));
1377 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301378 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001380 /* User-mode ip? */
1381 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Simon Arlott27b46d72007-10-20 01:13:56 +02001383 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 force_sig_info(SIGTRAP, &info, tsk);
1385}
1386
Roland McGrath86976cd2008-01-30 13:31:01 +01001387
Roland McGrathd4d67152008-07-09 02:38:07 -07001388#ifdef CONFIG_X86_32
1389# define IS_IA32 1
1390#elif defined CONFIG_IA32_EMULATION
Roland McGrathccbe4952009-02-27 19:03:24 -08001391# define IS_IA32 is_compat_task()
Roland McGrathd4d67152008-07-09 02:38:07 -07001392#else
1393# define IS_IA32 0
1394#endif
1395
1396/*
1397 * We must return the syscall number to actually look up in the table.
1398 * This can be -1L to skip running any syscall at all.
1399 */
1400asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001401{
Roland McGrathd4d67152008-07-09 02:38:07 -07001402 long ret = 0;
1403
Roland McGrath380fdd72008-07-09 02:39:29 -07001404 /*
1405 * If we stepped into a sysenter/syscall insn, it trapped in
1406 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1407 * If user-mode had set TF itself, then it's still clear from
1408 * do_debug() and we need to set it again to restore the user
1409 * state. If we entered on the slow path, TF was already set.
1410 */
1411 if (test_thread_flag(TIF_SINGLESTEP))
1412 regs->flags |= X86_EFLAGS_TF;
1413
Roland McGrath86976cd2008-01-30 13:31:01 +01001414 /* do the secure computing check first */
1415 secure_computing(regs->orig_ax);
1416
Roland McGrathd4d67152008-07-09 02:38:07 -07001417 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1418 ret = -1L;
1419
Roland McGratheeea3c32008-03-16 23:36:28 -07001420 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1421 tracehook_report_syscall_entry(regs))
1422 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001423
1424 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001425 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001426 audit_syscall_entry(AUDIT_ARCH_I386,
1427 regs->orig_ax,
1428 regs->bx, regs->cx,
1429 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001430#ifdef CONFIG_X86_64
1431 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001432 audit_syscall_entry(AUDIT_ARCH_X86_64,
1433 regs->orig_ax,
1434 regs->di, regs->si,
1435 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001436#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001437 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001438
1439 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001440}
1441
Roland McGrathd4d67152008-07-09 02:38:07 -07001442asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001443{
1444 if (unlikely(current->audit_context))
1445 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1446
Roland McGrathd4d67152008-07-09 02:38:07 -07001447 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001448 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001449
Roland McGrathd4d67152008-07-09 02:38:07 -07001450 /*
1451 * If TIF_SYSCALL_EMU is set, we only get here because of
1452 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1453 * We already reported this syscall instruction in
1454 * syscall_trace_enter(), so don't do any more now.
1455 */
1456 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1457 return;
1458
1459 /*
1460 * If we are single-stepping, synthesize a trap to follow the
1461 * system call instruction.
1462 */
1463 if (test_thread_flag(TIF_SINGLESTEP) &&
Roland McGratheeea3c32008-03-16 23:36:28 -07001464 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301465 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001466}