blob: bf45cdf1aaca340a7332cd54dd5ff43938521708 [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,
43 REGSET_TLS,
44};
Markus Metzgereee3af42008-01-30 13:31:09 +010045
46/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * does not yet catch signals sent when the child dies.
48 * in exit.c or in signal.c.
49 */
50
Chuck Ebbert9f155b92006-01-05 23:11:29 -050051/*
52 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050053 */
Roland McGrathe39c2892008-01-30 13:31:01 +010054#define FLAG_MASK_32 ((unsigned long) \
55 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
56 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
57 X86_EFLAGS_SF | X86_EFLAGS_TF | \
58 X86_EFLAGS_DF | X86_EFLAGS_OF | \
59 X86_EFLAGS_RF | X86_EFLAGS_AC))
60
Roland McGrath2047b082008-01-30 13:31:01 +010061/*
62 * Determines whether a value may be installed in a segment register.
63 */
64static inline bool invalid_selector(u16 value)
65{
66 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
67}
68
69#ifdef CONFIG_X86_32
70
Roland McGrathe39c2892008-01-30 13:31:01 +010071#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jaswinder Singh4fe702c2008-07-25 10:19:27 +053073static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010075 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Roland McGrath06ee1b62008-01-30 13:31:01 +010076 regno >>= 2;
Roland McGrath62a97d42008-01-30 13:30:52 +010077 if (regno > FS)
78 --regno;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010079 return &regs->bx + regno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Roland McGrath06ee1b62008-01-30 13:31:01 +010082static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Roland McGrath06ee1b62008-01-30 13:31:01 +010084 /*
85 * Returning the value truncates it to 16 bits.
86 */
87 unsigned int retval;
88 if (offset != offsetof(struct user_regs_struct, gs))
89 retval = *pt_regs_access(task_pt_regs(task), offset);
90 else {
91 retval = task->thread.gs;
92 if (task == current)
93 savesegment(gs, retval);
94 }
95 return retval;
96}
97
98static int set_segment_reg(struct task_struct *task,
99 unsigned long offset, u16 value)
100{
101 /*
102 * The value argument was already truncated to 16 bits.
103 */
Roland McGrath2047b082008-01-30 13:31:01 +0100104 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100105 return -EIO;
106
Roland McGrathc63855d2008-02-06 22:39:44 +0100107 /*
108 * For %cs and %ss we cannot permit a null selector.
109 * We can permit a bogus selector as long as it has USER_RPL.
110 * Null selectors are fine for other segment registers, but
111 * we will never get back to user mode with invalid %cs or %ss
112 * and will take the trap in iret instead. Much code relies
113 * on user_mode() to distinguish a user trap frame (which can
114 * safely use invalid selectors) from a kernel trap frame.
115 */
116 switch (offset) {
117 case offsetof(struct user_regs_struct, cs):
118 case offsetof(struct user_regs_struct, ss):
119 if (unlikely(value == 0))
120 return -EIO;
121
122 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100123 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100124 break;
125
126 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100127 task->thread.gs = value;
128 if (task == current)
Roland McGrath5fd4d162008-01-30 13:30:58 +0100129 /*
130 * The user-mode %gs is not affected by
131 * kernel entry, so we must update the CPU.
132 */
133 loadsegment(gs, 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
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100558#ifdef X86_BTS
559
Markus Metzgera95d67f2008-01-30 13:31:20 +0100560static int ptrace_bts_get_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100561{
562 if (!child->thread.ds_area_msr)
563 return -ENXIO;
564
Markus Metzgera95d67f2008-01-30 13:31:20 +0100565 return ds_get_bts_index((void *)child->thread.ds_area_msr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100566}
567
Markus Metzgereee3af42008-01-30 13:31:09 +0100568static int ptrace_bts_read_record(struct task_struct *child,
569 long index,
570 struct bts_struct __user *out)
571{
572 struct bts_struct ret;
573 int retval;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100574 int bts_end;
Markus Metzgere4811f22008-01-30 13:31:20 +0100575 int bts_index;
Markus Metzgereee3af42008-01-30 13:31:09 +0100576
577 if (!child->thread.ds_area_msr)
578 return -ENXIO;
579
Markus Metzgere4811f22008-01-30 13:31:20 +0100580 if (index < 0)
581 return -EINVAL;
582
Markus Metzgera95d67f2008-01-30 13:31:20 +0100583 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
584 if (bts_end <= index)
Markus Metzgere4811f22008-01-30 13:31:20 +0100585 return -EINVAL;
586
587 /* translate the ptrace bts index into the ds bts index */
588 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
589 bts_index -= (index + 1);
590 if (bts_index < 0)
Markus Metzgera95d67f2008-01-30 13:31:20 +0100591 bts_index += bts_end;
Markus Metzgere4811f22008-01-30 13:31:20 +0100592
Markus Metzgereee3af42008-01-30 13:31:09 +0100593 retval = ds_read_bts((void *)child->thread.ds_area_msr,
Markus Metzgere4811f22008-01-30 13:31:20 +0100594 bts_index, &ret);
Markus Metzgere6ae5d92008-01-30 13:32:03 +0100595 if (retval < 0)
Markus Metzgereee3af42008-01-30 13:31:09 +0100596 return retval;
597
598 if (copy_to_user(out, &ret, sizeof(ret)))
599 return -EFAULT;
600
601 return sizeof(ret);
602}
603
Markus Metzgera95d67f2008-01-30 13:31:20 +0100604static int ptrace_bts_clear(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100605{
Markus Metzgera95d67f2008-01-30 13:31:20 +0100606 if (!child->thread.ds_area_msr)
Markus Metzgereee3af42008-01-30 13:31:09 +0100607 return -ENXIO;
608
Markus Metzgera95d67f2008-01-30 13:31:20 +0100609 return ds_clear((void *)child->thread.ds_area_msr);
610}
611
612static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100613 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100614 struct bts_struct __user *out)
615{
616 int end, i;
617 void *ds = (void *)child->thread.ds_area_msr;
618
619 if (!ds)
620 return -ENXIO;
621
622 end = ds_get_bts_index(ds);
623 if (end <= 0)
624 return end;
625
Markus Metzgercba4b652008-01-30 13:32:03 +0100626 if (size < (end * sizeof(struct bts_struct)))
627 return -EIO;
628
Markus Metzgera95d67f2008-01-30 13:31:20 +0100629 for (i = 0; i < end; i++, out++) {
630 struct bts_struct ret;
631 int retval;
632
633 retval = ds_read_bts(ds, i, &ret);
634 if (retval < 0)
635 return retval;
636
637 if (copy_to_user(out, &ret, sizeof(ret)))
638 return -EFAULT;
639 }
640
641 ds_clear(ds);
642
Markus Metzgercba4b652008-01-30 13:32:03 +0100643 return end;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100644}
645
646static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100647 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100648 const struct ptrace_bts_config __user *ucfg)
649{
650 struct ptrace_bts_config cfg;
Markus Metzgerda35c372008-01-30 13:32:03 +0100651 int bts_size, ret = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100652 void *ds;
653
Markus Metzgercba4b652008-01-30 13:32:03 +0100654 if (cfg_size < sizeof(cfg))
655 return -EIO;
656
Markus Metzgera95d67f2008-01-30 13:31:20 +0100657 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
658 return -EFAULT;
659
Markus Metzgercba4b652008-01-30 13:32:03 +0100660 if ((int)cfg.size < 0)
661 return -EINVAL;
662
Markus Metzgera95d67f2008-01-30 13:31:20 +0100663 bts_size = 0;
664 ds = (void *)child->thread.ds_area_msr;
665 if (ds) {
666 bts_size = ds_get_bts_size(ds);
667 if (bts_size < 0)
668 return bts_size;
669 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100670 cfg.size = PAGE_ALIGN(cfg.size);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100671
672 if (bts_size != cfg.size) {
Markus Metzgerda35c372008-01-30 13:32:03 +0100673 ret = ptrace_bts_realloc(child, cfg.size,
674 cfg.flags & PTRACE_BTS_O_CUT_SIZE);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100675 if (ret < 0)
Markus Metzgerda35c372008-01-30 13:32:03 +0100676 goto errout;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100677
Markus Metzgera95d67f2008-01-30 13:31:20 +0100678 ds = (void *)child->thread.ds_area_msr;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100679 }
680
Markus Metzgerda35c372008-01-30 13:32:03 +0100681 if (cfg.flags & PTRACE_BTS_O_SIGNAL)
682 ret = ds_set_overflow(ds, DS_O_SIGNAL);
683 else
684 ret = ds_set_overflow(ds, DS_O_WRAP);
685 if (ret < 0)
686 goto errout;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100687
Markus Metzgerda35c372008-01-30 13:32:03 +0100688 if (cfg.flags & PTRACE_BTS_O_TRACE)
689 child->thread.debugctlmsr |= ds_debugctl_mask();
690 else
691 child->thread.debugctlmsr &= ~ds_debugctl_mask();
Markus Metzgereee3af42008-01-30 13:31:09 +0100692
Markus Metzgerda35c372008-01-30 13:32:03 +0100693 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgereee3af42008-01-30 13:31:09 +0100694 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
695 else
696 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
697
Markus Metzgercba4b652008-01-30 13:32:03 +0100698 ret = sizeof(cfg);
699
Markus Metzgerda35c372008-01-30 13:32:03 +0100700out:
701 if (child->thread.debugctlmsr)
702 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
703 else
704 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
705
706 return ret;
707
708errout:
709 child->thread.debugctlmsr &= ~ds_debugctl_mask();
710 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
711 goto out;
Markus Metzgereee3af42008-01-30 13:31:09 +0100712}
713
Markus Metzgera95d67f2008-01-30 13:31:20 +0100714static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100715 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100716 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100717{
Markus Metzgera95d67f2008-01-30 13:31:20 +0100718 void *ds = (void *)child->thread.ds_area_msr;
719 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100720
Markus Metzgercba4b652008-01-30 13:32:03 +0100721 if (cfg_size < sizeof(cfg))
722 return -EIO;
723
Markus Metzgera95d67f2008-01-30 13:31:20 +0100724 memset(&cfg, 0, sizeof(cfg));
Markus Metzgereee3af42008-01-30 13:31:09 +0100725
Markus Metzgera95d67f2008-01-30 13:31:20 +0100726 if (ds) {
727 cfg.size = ds_get_bts_size(ds);
Markus Metzgereee3af42008-01-30 13:31:09 +0100728
Markus Metzgera95d67f2008-01-30 13:31:20 +0100729 if (ds_get_overflow(ds) == DS_O_SIGNAL)
730 cfg.flags |= PTRACE_BTS_O_SIGNAL;
Markus Metzgereee3af42008-01-30 13:31:09 +0100731
Markus Metzgera95d67f2008-01-30 13:31:20 +0100732 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
733 child->thread.debugctlmsr & ds_debugctl_mask())
734 cfg.flags |= PTRACE_BTS_O_TRACE;
Markus Metzgereee3af42008-01-30 13:31:09 +0100735
Markus Metzgera95d67f2008-01-30 13:31:20 +0100736 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
737 cfg.flags |= PTRACE_BTS_O_SCHED;
Markus Metzgereee3af42008-01-30 13:31:09 +0100738 }
739
Markus Metzger87e84072008-01-30 13:32:54 +0100740 cfg.bts_size = sizeof(struct bts_struct);
741
Markus Metzgera95d67f2008-01-30 13:31:20 +0100742 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
743 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100744
Markus Metzgera95d67f2008-01-30 13:31:20 +0100745 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100746}
747
Andrew Mortond8d4f152008-03-04 15:05:39 -0800748
749static int ptrace_bts_write_record(struct task_struct *child,
750 const struct bts_struct *in)
751{
752 int retval;
753
754 if (!child->thread.ds_area_msr)
755 return -ENXIO;
756
757 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
758 if (retval)
759 return retval;
760
761 return sizeof(*in);
762}
763
764static int ptrace_bts_realloc(struct task_struct *child,
765 int size, int reduce_size)
766{
767 unsigned long rlim, vm;
768 int ret, old_size;
769
770 if (size < 0)
771 return -EINVAL;
772
773 old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
774 if (old_size < 0)
775 return old_size;
776
777 ret = ds_free((void **)&child->thread.ds_area_msr);
778 if (ret < 0)
779 goto out;
780
781 size >>= PAGE_SHIFT;
782 old_size >>= PAGE_SHIFT;
783
784 current->mm->total_vm -= old_size;
785 current->mm->locked_vm -= old_size;
786
787 if (size == 0)
788 goto out;
789
790 rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
791 vm = current->mm->total_vm + size;
792 if (rlim < vm) {
793 ret = -ENOMEM;
794
795 if (!reduce_size)
796 goto out;
797
798 size = rlim - current->mm->total_vm;
799 if (size <= 0)
800 goto out;
801 }
802
803 rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
804 vm = current->mm->locked_vm + size;
805 if (rlim < vm) {
806 ret = -ENOMEM;
807
808 if (!reduce_size)
809 goto out;
810
811 size = rlim - current->mm->locked_vm;
812 if (size <= 0)
813 goto out;
814 }
815
816 ret = ds_allocate((void **)&child->thread.ds_area_msr,
817 size << PAGE_SHIFT);
818 if (ret < 0)
819 goto out;
820
821 current->mm->total_vm += size;
822 current->mm->locked_vm += size;
823
824out:
825 if (child->thread.ds_area_msr)
826 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
827 else
828 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
829
830 return ret;
831}
832
Markus Metzgereee3af42008-01-30 13:31:09 +0100833void ptrace_bts_take_timestamp(struct task_struct *tsk,
834 enum bts_qualifier qualifier)
835{
836 struct bts_struct rec = {
837 .qualifier = qualifier,
Markus Metzgerda35c372008-01-30 13:32:03 +0100838 .variant.jiffies = jiffies_64
Markus Metzgereee3af42008-01-30 13:31:09 +0100839 };
840
Markus Metzgereee3af42008-01-30 13:31:09 +0100841 ptrace_bts_write_record(tsk, &rec);
842}
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100843#endif /* X86_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100844
Roland McGrathd9771e82008-01-30 13:30:52 +0100845/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * Called by kernel/ptrace.c when detaching..
847 *
848 * Make sure the single step bit is not set.
849 */
850void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100851{
Roland McGrath7f232342008-01-30 13:30:48 +0100852 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100853#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700854 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100855#endif
Markus Metzgereee3af42008-01-30 13:31:09 +0100856 if (child->thread.ds_area_msr) {
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100857#ifdef X86_BTS
Markus Metzgerda35c372008-01-30 13:32:03 +0100858 ptrace_bts_realloc(child, 0, 0);
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100859#endif
Markus Metzgerda35c372008-01-30 13:32:03 +0100860 child->thread.debugctlmsr &= ~ds_debugctl_mask();
861 if (!child->thread.debugctlmsr)
862 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
863 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
Markus Metzgereee3af42008-01-30 13:31:09 +0100864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Roland McGrath5a4646a2008-01-30 13:31:54 +0100867#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
868static const struct user_regset_view user_x86_32_view; /* Initialized below. */
869#endif
870
Christoph Hellwig481bed42005-11-07 00:59:47 -0800871long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100873 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned long __user *datap = (unsigned long __user *)data;
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* read the word at location addr in the USER area. */
878 case PTRACE_PEEKUSR: {
879 unsigned long tmp;
880
881 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100882 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
883 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 break;
885
886 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100887 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100889 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
890 addr <= offsetof(struct user, u_debugreg[7])) {
891 addr -= offsetof(struct user, u_debugreg[0]);
892 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 ret = put_user(tmp, datap);
895 break;
896 }
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
899 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100900 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
901 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903
Roland McGrathe9c86c72008-01-30 13:31:01 +0100904 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100906 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
907 addr <= offsetof(struct user, u_debugreg[7])) {
908 addr -= offsetof(struct user, u_debugreg[0]);
909 ret = ptrace_set_debugreg(child,
910 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100912 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Roland McGrath5a4646a2008-01-30 13:31:54 +0100914 case PTRACE_GETREGS: /* Get all gp regs from the child. */
915 return copy_regset_to_user(child,
916 task_user_regset_view(current),
917 REGSET_GENERAL,
918 0, sizeof(struct user_regs_struct),
919 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Roland McGrath5a4646a2008-01-30 13:31:54 +0100921 case PTRACE_SETREGS: /* Set all gp regs in the child. */
922 return copy_regset_from_user(child,
923 task_user_regset_view(current),
924 REGSET_GENERAL,
925 0, sizeof(struct user_regs_struct),
926 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Roland McGrath5a4646a2008-01-30 13:31:54 +0100928 case PTRACE_GETFPREGS: /* Get the child FPU state. */
929 return copy_regset_to_user(child,
930 task_user_regset_view(current),
931 REGSET_FP,
932 0, sizeof(struct user_i387_struct),
933 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Roland McGrath5a4646a2008-01-30 13:31:54 +0100935 case PTRACE_SETFPREGS: /* Set the child FPU state. */
936 return copy_regset_from_user(child,
937 task_user_regset_view(current),
938 REGSET_FP,
939 0, sizeof(struct user_i387_struct),
940 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Roland McGrathe9c86c72008-01-30 13:31:01 +0100942#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100943 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
944 return copy_regset_to_user(child, &user_x86_32_view,
945 REGSET_XFP,
946 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700947 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Roland McGrath5a4646a2008-01-30 13:31:54 +0100949 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
950 return copy_regset_from_user(child, &user_x86_32_view,
951 REGSET_XFP,
952 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700953 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Roland McGrathe9c86c72008-01-30 13:31:01 +0100956#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100958 if (addr < 0)
959 return -EIO;
960 ret = do_get_thread_area(child, addr,
961 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963
964 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100965 if (addr < 0)
966 return -EIO;
967 ret = do_set_thread_area(child, addr,
968 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100970#endif
971
972#ifdef CONFIG_X86_64
973 /* normal 64bit interface to access TLS data.
974 Works just like arch_prctl, except that the arguments
975 are reversed. */
976 case PTRACE_ARCH_PRCTL:
977 ret = do_arch_prctl(child, data, addr);
978 break;
979#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100981 /*
982 * These bits need more cooking - not enabled yet:
983 */
984#ifdef X86_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +0100985 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100986 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +0100987 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100988 break;
989
990 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100991 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +0100992 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100993 break;
994
995 case PTRACE_BTS_SIZE:
996 ret = ptrace_bts_get_size(child);
997 break;
998
999 case PTRACE_BTS_GET:
1000 ret = ptrace_bts_read_record
1001 (child, data, (struct bts_struct __user *) addr);
1002 break;
1003
1004 case PTRACE_BTS_CLEAR:
1005 ret = ptrace_bts_clear(child);
1006 break;
1007
1008 case PTRACE_BTS_DRAIN:
1009 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +01001010 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +01001011 break;
Ingo Molnarb4ef95d2008-02-26 09:40:27 +01001012#endif
Markus Metzgereee3af42008-01-30 13:31:09 +01001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 default:
1015 ret = ptrace_request(child, request, addr, data);
1016 break;
1017 }
Roland McGrathd9771e82008-01-30 13:30:52 +01001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return ret;
1020}
1021
Roland McGrathcb757c42008-01-30 13:31:01 +01001022#ifdef CONFIG_IA32_EMULATION
1023
Roland McGrath099cd6e2008-01-30 13:31:01 +01001024#include <linux/compat.h>
1025#include <linux/syscalls.h>
1026#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001027#include <asm/user32.h>
1028
1029#define R32(l,q) \
1030 case offsetof(struct user32, regs.l): \
1031 regs->q = value; break
1032
1033#define SEG32(rs) \
1034 case offsetof(struct user32, regs.rs): \
1035 return set_segment_reg(child, \
1036 offsetof(struct user_regs_struct, rs), \
1037 value); \
1038 break
1039
1040static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1041{
1042 struct pt_regs *regs = task_pt_regs(child);
1043
1044 switch (regno) {
1045
1046 SEG32(cs);
1047 SEG32(ds);
1048 SEG32(es);
1049 SEG32(fs);
1050 SEG32(gs);
1051 SEG32(ss);
1052
1053 R32(ebx, bx);
1054 R32(ecx, cx);
1055 R32(edx, dx);
1056 R32(edi, di);
1057 R32(esi, si);
1058 R32(ebp, bp);
1059 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001060 R32(eip, ip);
1061 R32(esp, sp);
1062
Roland McGrath40f09332008-02-28 19:57:07 -08001063 case offsetof(struct user32, regs.orig_eax):
1064 /*
1065 * Sign-extend the value so that orig_eax = -1
1066 * causes (long)orig_ax < 0 tests to fire correctly.
1067 */
1068 regs->orig_ax = (long) (s32) value;
1069 break;
1070
Roland McGrathcb757c42008-01-30 13:31:01 +01001071 case offsetof(struct user32, regs.eflags):
1072 return set_flags(child, value);
1073
1074 case offsetof(struct user32, u_debugreg[0]) ...
1075 offsetof(struct user32, u_debugreg[7]):
1076 regno -= offsetof(struct user32, u_debugreg[0]);
1077 return ptrace_set_debugreg(child, regno / 4, value);
1078
1079 default:
1080 if (regno > sizeof(struct user32) || (regno & 3))
1081 return -EIO;
1082
1083 /*
1084 * Other dummy fields in the virtual user structure
1085 * are ignored
1086 */
1087 break;
1088 }
1089 return 0;
1090}
1091
1092#undef R32
1093#undef SEG32
1094
1095#define R32(l,q) \
1096 case offsetof(struct user32, regs.l): \
1097 *val = regs->q; break
1098
1099#define SEG32(rs) \
1100 case offsetof(struct user32, regs.rs): \
1101 *val = get_segment_reg(child, \
1102 offsetof(struct user_regs_struct, rs)); \
1103 break
1104
1105static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1106{
1107 struct pt_regs *regs = task_pt_regs(child);
1108
1109 switch (regno) {
1110
1111 SEG32(ds);
1112 SEG32(es);
1113 SEG32(fs);
1114 SEG32(gs);
1115
1116 R32(cs, cs);
1117 R32(ss, ss);
1118 R32(ebx, bx);
1119 R32(ecx, cx);
1120 R32(edx, dx);
1121 R32(edi, di);
1122 R32(esi, si);
1123 R32(ebp, bp);
1124 R32(eax, ax);
1125 R32(orig_eax, orig_ax);
1126 R32(eip, ip);
1127 R32(esp, sp);
1128
1129 case offsetof(struct user32, regs.eflags):
1130 *val = get_flags(child);
1131 break;
1132
1133 case offsetof(struct user32, u_debugreg[0]) ...
1134 offsetof(struct user32, u_debugreg[7]):
1135 regno -= offsetof(struct user32, u_debugreg[0]);
1136 *val = ptrace_get_debugreg(child, regno / 4);
1137 break;
1138
1139 default:
1140 if (regno > sizeof(struct user32) || (regno & 3))
1141 return -EIO;
1142
1143 /*
1144 * Other dummy fields in the virtual user structure
1145 * are ignored
1146 */
1147 *val = 0;
1148 break;
1149 }
1150 return 0;
1151}
1152
1153#undef R32
1154#undef SEG32
1155
Roland McGrath91e7b702008-01-30 13:31:52 +01001156static int genregs32_get(struct task_struct *target,
1157 const struct user_regset *regset,
1158 unsigned int pos, unsigned int count,
1159 void *kbuf, void __user *ubuf)
1160{
1161 if (kbuf) {
1162 compat_ulong_t *k = kbuf;
1163 while (count > 0) {
1164 getreg32(target, pos, k++);
1165 count -= sizeof(*k);
1166 pos += sizeof(*k);
1167 }
1168 } else {
1169 compat_ulong_t __user *u = ubuf;
1170 while (count > 0) {
1171 compat_ulong_t word;
1172 getreg32(target, pos, &word);
1173 if (__put_user(word, u++))
1174 return -EFAULT;
1175 count -= sizeof(*u);
1176 pos += sizeof(*u);
1177 }
1178 }
1179
1180 return 0;
1181}
1182
1183static int genregs32_set(struct task_struct *target,
1184 const struct user_regset *regset,
1185 unsigned int pos, unsigned int count,
1186 const void *kbuf, const void __user *ubuf)
1187{
1188 int ret = 0;
1189 if (kbuf) {
1190 const compat_ulong_t *k = kbuf;
1191 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001192 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001193 count -= sizeof(*k);
1194 pos += sizeof(*k);
1195 }
1196 } else {
1197 const compat_ulong_t __user *u = ubuf;
1198 while (count > 0 && !ret) {
1199 compat_ulong_t word;
1200 ret = __get_user(word, u++);
1201 if (ret)
1202 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001203 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001204 count -= sizeof(*u);
1205 pos += sizeof(*u);
1206 }
1207 }
1208 return ret;
1209}
1210
Roland McGrath562b80b2008-04-22 12:21:25 -07001211long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1212 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001213{
Roland McGrath562b80b2008-04-22 12:21:25 -07001214 unsigned long addr = caddr;
1215 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001216 void __user *datap = compat_ptr(data);
1217 int ret;
1218 __u32 val;
1219
1220 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001221 case PTRACE_PEEKUSR:
1222 ret = getreg32(child, addr, &val);
1223 if (ret == 0)
1224 ret = put_user(val, (__u32 __user *)datap);
1225 break;
1226
1227 case PTRACE_POKEUSR:
1228 ret = putreg32(child, addr, data);
1229 break;
1230
Roland McGrath5a4646a2008-01-30 13:31:54 +01001231 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1232 return copy_regset_to_user(child, &user_x86_32_view,
1233 REGSET_GENERAL,
1234 0, sizeof(struct user_regs_struct32),
1235 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001236
Roland McGrath5a4646a2008-01-30 13:31:54 +01001237 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1238 return copy_regset_from_user(child, &user_x86_32_view,
1239 REGSET_GENERAL, 0,
1240 sizeof(struct user_regs_struct32),
1241 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001242
Roland McGrath5a4646a2008-01-30 13:31:54 +01001243 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1244 return copy_regset_to_user(child, &user_x86_32_view,
1245 REGSET_FP, 0,
1246 sizeof(struct user_i387_ia32_struct),
1247 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001248
Roland McGrath5a4646a2008-01-30 13:31:54 +01001249 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1250 return copy_regset_from_user(
1251 child, &user_x86_32_view, REGSET_FP,
1252 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001253
Roland McGrath5a4646a2008-01-30 13:31:54 +01001254 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1255 return copy_regset_to_user(child, &user_x86_32_view,
1256 REGSET_XFP, 0,
1257 sizeof(struct user32_fxsr_struct),
1258 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001259
Roland McGrath5a4646a2008-01-30 13:31:54 +01001260 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1261 return copy_regset_from_user(child, &user_x86_32_view,
1262 REGSET_XFP, 0,
1263 sizeof(struct user32_fxsr_struct),
1264 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001265
Roland McGrath562b80b2008-04-22 12:21:25 -07001266 case PTRACE_GET_THREAD_AREA:
1267 case PTRACE_SET_THREAD_AREA:
1268 return arch_ptrace(child, request, addr, data);
1269
Roland McGrath099cd6e2008-01-30 13:31:01 +01001270 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001271 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001272 }
1273
Roland McGrath099cd6e2008-01-30 13:31:01 +01001274 return ret;
1275}
1276
Roland McGrathcb757c42008-01-30 13:31:01 +01001277#endif /* CONFIG_IA32_EMULATION */
1278
Roland McGrath070459d2008-01-30 13:31:53 +01001279#ifdef CONFIG_X86_64
1280
1281static const struct user_regset x86_64_regsets[] = {
1282 [REGSET_GENERAL] = {
1283 .core_note_type = NT_PRSTATUS,
1284 .n = sizeof(struct user_regs_struct) / sizeof(long),
1285 .size = sizeof(long), .align = sizeof(long),
1286 .get = genregs_get, .set = genregs_set
1287 },
1288 [REGSET_FP] = {
1289 .core_note_type = NT_PRFPREG,
1290 .n = sizeof(struct user_i387_struct) / sizeof(long),
1291 .size = sizeof(long), .align = sizeof(long),
1292 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1293 },
1294};
1295
1296static const struct user_regset_view user_x86_64_view = {
1297 .name = "x86_64", .e_machine = EM_X86_64,
1298 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1299};
1300
1301#else /* CONFIG_X86_32 */
1302
1303#define user_regs_struct32 user_regs_struct
1304#define genregs32_get genregs_get
1305#define genregs32_set genregs_set
1306
Roland McGrath1f465f42008-05-09 15:43:44 -07001307#define user_i387_ia32_struct user_i387_struct
1308#define user32_fxsr_struct user_fxsr_struct
1309
Roland McGrath070459d2008-01-30 13:31:53 +01001310#endif /* CONFIG_X86_64 */
1311
1312#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1313static const struct user_regset x86_32_regsets[] = {
1314 [REGSET_GENERAL] = {
1315 .core_note_type = NT_PRSTATUS,
1316 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1317 .size = sizeof(u32), .align = sizeof(u32),
1318 .get = genregs32_get, .set = genregs32_set
1319 },
1320 [REGSET_FP] = {
1321 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001322 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001323 .size = sizeof(u32), .align = sizeof(u32),
1324 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1325 },
1326 [REGSET_XFP] = {
1327 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001328 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001329 .size = sizeof(u32), .align = sizeof(u32),
1330 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1331 },
1332 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001333 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001334 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1335 .size = sizeof(struct user_desc),
1336 .align = sizeof(struct user_desc),
1337 .active = regset_tls_active,
1338 .get = regset_tls_get, .set = regset_tls_set
1339 },
1340};
1341
1342static const struct user_regset_view user_x86_32_view = {
1343 .name = "i386", .e_machine = EM_386,
1344 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1345};
1346#endif
1347
1348const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1349{
1350#ifdef CONFIG_IA32_EMULATION
1351 if (test_tsk_thread_flag(task, TIF_IA32))
1352#endif
1353#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1354 return &user_x86_32_view;
1355#endif
1356#ifdef CONFIG_X86_64
1357 return &user_x86_64_view;
1358#endif
1359}
1360
Srinivasa Dsda654b72008-09-23 15:23:52 +05301361void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1362 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct siginfo info;
1365
1366 tsk->thread.trap_no = 1;
1367 tsk->thread.error_code = error_code;
1368
1369 memset(&info, 0, sizeof(info));
1370 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301371 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001373 /* User-mode ip? */
1374 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Simon Arlott27b46d72007-10-20 01:13:56 +02001376 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 force_sig_info(SIGTRAP, &info, tsk);
1378}
1379
Roland McGrath86976cd2008-01-30 13:31:01 +01001380
Roland McGrathd4d67152008-07-09 02:38:07 -07001381#ifdef CONFIG_X86_32
1382# define IS_IA32 1
1383#elif defined CONFIG_IA32_EMULATION
1384# define IS_IA32 test_thread_flag(TIF_IA32)
1385#else
1386# define IS_IA32 0
1387#endif
1388
1389/*
1390 * We must return the syscall number to actually look up in the table.
1391 * This can be -1L to skip running any syscall at all.
1392 */
1393asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001394{
Roland McGrathd4d67152008-07-09 02:38:07 -07001395 long ret = 0;
1396
Roland McGrath380fdd72008-07-09 02:39:29 -07001397 /*
1398 * If we stepped into a sysenter/syscall insn, it trapped in
1399 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1400 * If user-mode had set TF itself, then it's still clear from
1401 * do_debug() and we need to set it again to restore the user
1402 * state. If we entered on the slow path, TF was already set.
1403 */
1404 if (test_thread_flag(TIF_SINGLESTEP))
1405 regs->flags |= X86_EFLAGS_TF;
1406
Roland McGrath86976cd2008-01-30 13:31:01 +01001407 /* do the secure computing check first */
1408 secure_computing(regs->orig_ax);
1409
Roland McGrathd4d67152008-07-09 02:38:07 -07001410 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1411 ret = -1L;
1412
Roland McGratheeea3c32008-03-16 23:36:28 -07001413 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1414 tracehook_report_syscall_entry(regs))
1415 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001416
1417 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001418 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001419 audit_syscall_entry(AUDIT_ARCH_I386,
1420 regs->orig_ax,
1421 regs->bx, regs->cx,
1422 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001423#ifdef CONFIG_X86_64
1424 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001425 audit_syscall_entry(AUDIT_ARCH_X86_64,
1426 regs->orig_ax,
1427 regs->di, regs->si,
1428 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001429#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001430 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001431
1432 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001433}
1434
Roland McGrathd4d67152008-07-09 02:38:07 -07001435asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001436{
1437 if (unlikely(current->audit_context))
1438 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1439
Roland McGrathd4d67152008-07-09 02:38:07 -07001440 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001441 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001442
Roland McGrathd4d67152008-07-09 02:38:07 -07001443 /*
1444 * If TIF_SYSCALL_EMU is set, we only get here because of
1445 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1446 * We already reported this syscall instruction in
1447 * syscall_trace_enter(), so don't do any more now.
1448 */
1449 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1450 return;
1451
1452 /*
1453 * If we are single-stepping, synthesize a trap to follow the
1454 * system call instruction.
1455 */
1456 if (test_thread_flag(TIF_SINGLESTEP) &&
Roland McGratheeea3c32008-03-16 23:36:28 -07001457 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301458 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001459}