blob: 7ec39ab37a2d0c09cf99c78284d540300f0c9115 [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);
Tejun Heoccbeed32009-02-09 22:17:40 +090078 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Roland McGrath06ee1b62008-01-30 13:31:01 +010081static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Roland McGrath06ee1b62008-01-30 13:31:01 +010083 /*
84 * Returning the value truncates it to 16 bits.
85 */
86 unsigned int retval;
87 if (offset != offsetof(struct user_regs_struct, gs))
88 retval = *pt_regs_access(task_pt_regs(task), offset);
89 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +010090 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +090091 retval = get_user_gs(task_pt_regs(task));
92 else
93 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +010094 }
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 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900128 set_user_gs(task_pt_regs(task), value);
129 else
130 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
134}
135
Roland McGrath2047b082008-01-30 13:31:01 +0100136static unsigned long debugreg_addr_limit(struct task_struct *task)
137{
138 return TASK_SIZE - 3;
139}
140
141#else /* CONFIG_X86_64 */
142
143#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
144
145static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
146{
147 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
148 return &regs->r15 + (offset / sizeof(regs->r15));
149}
150
151static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
152{
153 /*
154 * Returning the value truncates it to 16 bits.
155 */
156 unsigned int seg;
157
158 switch (offset) {
159 case offsetof(struct user_regs_struct, fs):
160 if (task == current) {
161 /* Older gas can't assemble movq %?s,%r?? */
162 asm("movl %%fs,%0" : "=r" (seg));
163 return seg;
164 }
165 return task->thread.fsindex;
166 case offsetof(struct user_regs_struct, gs):
167 if (task == current) {
168 asm("movl %%gs,%0" : "=r" (seg));
169 return seg;
170 }
171 return task->thread.gsindex;
172 case offsetof(struct user_regs_struct, ds):
173 if (task == current) {
174 asm("movl %%ds,%0" : "=r" (seg));
175 return seg;
176 }
177 return task->thread.ds;
178 case offsetof(struct user_regs_struct, es):
179 if (task == current) {
180 asm("movl %%es,%0" : "=r" (seg));
181 return seg;
182 }
183 return task->thread.es;
184
185 case offsetof(struct user_regs_struct, cs):
186 case offsetof(struct user_regs_struct, ss):
187 break;
188 }
189 return *pt_regs_access(task_pt_regs(task), offset);
190}
191
192static int set_segment_reg(struct task_struct *task,
193 unsigned long offset, u16 value)
194{
195 /*
196 * The value argument was already truncated to 16 bits.
197 */
198 if (invalid_selector(value))
199 return -EIO;
200
201 switch (offset) {
202 case offsetof(struct user_regs_struct,fs):
203 /*
204 * If this is setting fs as for normal 64-bit use but
205 * setting fs_base has implicitly changed it, leave it.
206 */
207 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
208 task->thread.fs != 0) ||
209 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
210 task->thread.fs == 0))
211 break;
212 task->thread.fsindex = value;
213 if (task == current)
214 loadsegment(fs, task->thread.fsindex);
215 break;
216 case offsetof(struct user_regs_struct,gs):
217 /*
218 * If this is setting gs as for normal 64-bit use but
219 * setting gs_base has implicitly changed it, leave it.
220 */
221 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
222 task->thread.gs != 0) ||
223 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
224 task->thread.gs == 0))
225 break;
226 task->thread.gsindex = value;
227 if (task == current)
228 load_gs_index(task->thread.gsindex);
229 break;
230 case offsetof(struct user_regs_struct,ds):
231 task->thread.ds = value;
232 if (task == current)
233 loadsegment(ds, task->thread.ds);
234 break;
235 case offsetof(struct user_regs_struct,es):
236 task->thread.es = value;
237 if (task == current)
238 loadsegment(es, task->thread.es);
239 break;
240
241 /*
242 * Can't actually change these in 64-bit mode.
243 */
244 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100245 if (unlikely(value == 0))
246 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100247#ifdef CONFIG_IA32_EMULATION
248 if (test_tsk_thread_flag(task, TIF_IA32))
249 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100250#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100251 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100252 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100253 if (unlikely(value == 0))
254 return -EIO;
Roland McGrath2047b082008-01-30 13:31:01 +0100255#ifdef CONFIG_IA32_EMULATION
256 if (test_tsk_thread_flag(task, TIF_IA32))
257 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100258#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100259 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100260 }
261
262 return 0;
263}
264
265static unsigned long debugreg_addr_limit(struct task_struct *task)
266{
267#ifdef CONFIG_IA32_EMULATION
268 if (test_tsk_thread_flag(task, TIF_IA32))
269 return IA32_PAGE_OFFSET - 3;
270#endif
271 return TASK_SIZE64 - 7;
272}
273
274#endif /* CONFIG_X86_32 */
275
Roland McGrath06ee1b62008-01-30 13:31:01 +0100276static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100278 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Roland McGrath06ee1b62008-01-30 13:31:01 +0100280 /*
281 * If the debugger set TF, hide it from the readout.
282 */
283 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
284 retval &= ~X86_EFLAGS_TF;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return retval;
287}
288
Roland McGrath06ee1b62008-01-30 13:31:01 +0100289static int set_flags(struct task_struct *task, unsigned long value)
290{
291 struct pt_regs *regs = task_pt_regs(task);
292
293 /*
294 * If the user value contains TF, mark that
295 * it was not "us" (the debugger) that set it.
296 * If not, make sure it stays set if we had.
297 */
298 if (value & X86_EFLAGS_TF)
299 clear_tsk_thread_flag(task, TIF_FORCED_TF);
300 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
301 value |= X86_EFLAGS_TF;
302
303 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
304
305 return 0;
306}
307
308static int putreg(struct task_struct *child,
309 unsigned long offset, unsigned long value)
310{
311 switch (offset) {
312 case offsetof(struct user_regs_struct, cs):
313 case offsetof(struct user_regs_struct, ds):
314 case offsetof(struct user_regs_struct, es):
315 case offsetof(struct user_regs_struct, fs):
316 case offsetof(struct user_regs_struct, gs):
317 case offsetof(struct user_regs_struct, ss):
318 return set_segment_reg(child, offset, value);
319
320 case offsetof(struct user_regs_struct, flags):
321 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100322
323#ifdef CONFIG_X86_64
Roland McGrath84c6f602008-03-07 14:56:02 -0800324 /*
325 * Orig_ax is really just a flag with small positive and
326 * negative values, so make sure to always sign-extend it
327 * from 32 bits so that it works correctly regardless of
328 * whether we come from a 32-bit environment or not.
329 */
330 case offsetof(struct user_regs_struct, orig_ax):
331 value = (long) (s32) value;
332 break;
333
Roland McGrath2047b082008-01-30 13:31:01 +0100334 case offsetof(struct user_regs_struct,fs_base):
335 if (value >= TASK_SIZE_OF(child))
336 return -EIO;
337 /*
338 * When changing the segment base, use do_arch_prctl
339 * to set either thread.fs or thread.fsindex and the
340 * corresponding GDT slot.
341 */
342 if (child->thread.fs != value)
343 return do_arch_prctl(child, ARCH_SET_FS, value);
344 return 0;
345 case offsetof(struct user_regs_struct,gs_base):
346 /*
347 * Exactly the same here as the %fs handling above.
348 */
349 if (value >= TASK_SIZE_OF(child))
350 return -EIO;
351 if (child->thread.gs != value)
352 return do_arch_prctl(child, ARCH_SET_GS, value);
353 return 0;
354#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100355 }
356
357 *pt_regs_access(task_pt_regs(child), offset) = value;
358 return 0;
359}
360
361static unsigned long getreg(struct task_struct *task, unsigned long offset)
362{
363 switch (offset) {
364 case offsetof(struct user_regs_struct, cs):
365 case offsetof(struct user_regs_struct, ds):
366 case offsetof(struct user_regs_struct, es):
367 case offsetof(struct user_regs_struct, fs):
368 case offsetof(struct user_regs_struct, gs):
369 case offsetof(struct user_regs_struct, ss):
370 return get_segment_reg(task, offset);
371
372 case offsetof(struct user_regs_struct, flags):
373 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100374
375#ifdef CONFIG_X86_64
376 case offsetof(struct user_regs_struct, fs_base): {
377 /*
378 * do_arch_prctl may have used a GDT slot instead of
379 * the MSR. To userland, it appears the same either
380 * way, except the %fs segment selector might not be 0.
381 */
382 unsigned int seg = task->thread.fsindex;
383 if (task->thread.fs != 0)
384 return task->thread.fs;
385 if (task == current)
386 asm("movl %%fs,%0" : "=r" (seg));
387 if (seg != FS_TLS_SEL)
388 return 0;
389 return get_desc_base(&task->thread.tls_array[FS_TLS]);
390 }
391 case offsetof(struct user_regs_struct, gs_base): {
392 /*
393 * Exactly the same here as the %fs handling above.
394 */
395 unsigned int seg = task->thread.gsindex;
396 if (task->thread.gs != 0)
397 return task->thread.gs;
398 if (task == current)
399 asm("movl %%gs,%0" : "=r" (seg));
400 if (seg != GS_TLS_SEL)
401 return 0;
402 return get_desc_base(&task->thread.tls_array[GS_TLS]);
403 }
404#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100405 }
406
407 return *pt_regs_access(task_pt_regs(task), offset);
408}
409
Roland McGrath91e7b702008-01-30 13:31:52 +0100410static int genregs_get(struct task_struct *target,
411 const struct user_regset *regset,
412 unsigned int pos, unsigned int count,
413 void *kbuf, void __user *ubuf)
414{
415 if (kbuf) {
416 unsigned long *k = kbuf;
417 while (count > 0) {
418 *k++ = getreg(target, pos);
419 count -= sizeof(*k);
420 pos += sizeof(*k);
421 }
422 } else {
423 unsigned long __user *u = ubuf;
424 while (count > 0) {
425 if (__put_user(getreg(target, pos), u++))
426 return -EFAULT;
427 count -= sizeof(*u);
428 pos += sizeof(*u);
429 }
430 }
431
432 return 0;
433}
434
435static int genregs_set(struct task_struct *target,
436 const struct user_regset *regset,
437 unsigned int pos, unsigned int count,
438 const void *kbuf, const void __user *ubuf)
439{
440 int ret = 0;
441 if (kbuf) {
442 const unsigned long *k = kbuf;
443 while (count > 0 && !ret) {
444 ret = putreg(target, pos, *k++);
445 count -= sizeof(*k);
446 pos += sizeof(*k);
447 }
448 } else {
449 const unsigned long __user *u = ubuf;
450 while (count > 0 && !ret) {
451 unsigned long word;
452 ret = __get_user(word, u++);
453 if (ret)
454 break;
455 ret = putreg(target, pos, word);
456 count -= sizeof(*u);
457 pos += sizeof(*u);
458 }
459 }
460 return ret;
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100464 * This function is trivial and will be inlined by the compiler.
465 * Having it separates the implementation details of debug
466 * registers from the interface details of ptrace.
467 */
468static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
469{
Roland McGrath0f534092008-01-30 13:30:59 +0100470 switch (n) {
471 case 0: return child->thread.debugreg0;
472 case 1: return child->thread.debugreg1;
473 case 2: return child->thread.debugreg2;
474 case 3: return child->thread.debugreg3;
475 case 6: return child->thread.debugreg6;
476 case 7: return child->thread.debugreg7;
477 }
478 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100479}
480
481static int ptrace_set_debugreg(struct task_struct *child,
482 int n, unsigned long data)
483{
Roland McGrath0f534092008-01-30 13:30:59 +0100484 int i;
485
Roland McGrathd9771e82008-01-30 13:30:52 +0100486 if (unlikely(n == 4 || n == 5))
487 return -EIO;
488
Roland McGrath2047b082008-01-30 13:31:01 +0100489 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100490 return -EIO;
491
Roland McGrath0f534092008-01-30 13:30:59 +0100492 switch (n) {
493 case 0: child->thread.debugreg0 = data; break;
494 case 1: child->thread.debugreg1 = data; break;
495 case 2: child->thread.debugreg2 = data; break;
496 case 3: child->thread.debugreg3 = data; break;
497
498 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100499 if ((data & ~0xffffffffUL) != 0)
500 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100501 child->thread.debugreg6 = data;
502 break;
503
504 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100505 /*
506 * Sanity-check data. Take one half-byte at once with
507 * check = (val >> (16 + 4*i)) & 0xf. It contains the
508 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
509 * 2 and 3 are LENi. Given a list of invalid values,
510 * we do mask |= 1 << invalid_value, so that
511 * (mask >> check) & 1 is a correct test for invalid
512 * values.
513 *
514 * R/Wi contains the type of the breakpoint /
515 * watchpoint, LENi contains the length of the watched
516 * data in the watchpoint case.
517 *
518 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100519 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100520 * - R/Wi == 0x10 (break on I/O reads or writes), so
521 * mask |= 0x4444.
522 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
523 * 0x1110.
524 *
525 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
526 *
527 * See the Intel Manual "System Programming Guide",
528 * 15.2.4
529 *
530 * Note that LENi == 0x10 is defined on x86_64 in long
531 * mode (i.e. even for 32-bit userspace software, but
532 * 64-bit kernel), so the x86_64 mask value is 0x5454.
533 * See the AMD manual no. 24593 (AMD64 System Programming)
534 */
Roland McGrath2047b082008-01-30 13:31:01 +0100535#ifdef CONFIG_X86_32
536#define DR7_MASK 0x5f54
537#else
538#define DR7_MASK 0x5554
539#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100540 data &= ~DR_CONTROL_RESERVED;
541 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100542 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100543 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100544 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100545 if (data)
546 set_tsk_thread_flag(child, TIF_DEBUG);
547 else
548 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100549 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100550 }
551
Roland McGrathd9771e82008-01-30 13:30:52 +0100552 return 0;
553}
554
Roland McGrath325af5f2008-08-08 15:58:39 -0700555/*
556 * These access the current or another (stopped) task's io permission
557 * bitmap for debugging or core dump.
558 */
559static int ioperm_active(struct task_struct *target,
560 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100561{
Roland McGrath325af5f2008-08-08 15:58:39 -0700562 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100563}
564
Roland McGrath325af5f2008-08-08 15:58:39 -0700565static int ioperm_get(struct task_struct *target,
566 const struct user_regset *regset,
567 unsigned int pos, unsigned int count,
568 void *kbuf, void __user *ubuf)
569{
570 if (!target->thread.io_bitmap_ptr)
571 return -ENXIO;
572
573 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
574 target->thread.io_bitmap_ptr,
575 0, IO_BITMAP_BYTES);
576}
577
Markus Metzger93fa7632008-04-08 11:01:58 +0200578#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzger93fa7632008-04-08 11:01:58 +0200579static int ptrace_bts_read_record(struct task_struct *child, size_t index,
Markus Metzgereee3af42008-01-30 13:31:09 +0100580 struct bts_struct __user *out)
581{
Markus Metzgerc2724772008-12-11 13:49:59 +0100582 const struct bts_trace *trace;
583 struct bts_struct bts;
584 const unsigned char *at;
Markus Metzger93fa7632008-04-08 11:01:58 +0200585 int error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100586
Markus Metzgerc2724772008-12-11 13:49:59 +0100587 trace = ds_read_bts(child->bts);
588 if (!trace)
589 return -EPERM;
590
591 at = trace->ds.top - ((index + 1) * trace->ds.size);
592 if ((void *)at < trace->ds.begin)
593 at += (trace->ds.n * trace->ds.size);
594
595 if (!trace->read)
596 return -EOPNOTSUPP;
597
598 error = trace->read(child->bts, at, &bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200599 if (error < 0)
600 return error;
Markus Metzgereee3af42008-01-30 13:31:09 +0100601
Markus Metzgerc2724772008-12-11 13:49:59 +0100602 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgereee3af42008-01-30 13:31:09 +0100603 return -EFAULT;
604
Markus Metzgerc2724772008-12-11 13:49:59 +0100605 return sizeof(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100606}
607
Markus Metzgera95d67f2008-01-30 13:31:20 +0100608static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100609 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100610 struct bts_struct __user *out)
611{
Markus Metzgerc2724772008-12-11 13:49:59 +0100612 const struct bts_trace *trace;
613 const unsigned char *at;
614 int error, drained = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100615
Markus Metzgerc2724772008-12-11 13:49:59 +0100616 trace = ds_read_bts(child->bts);
617 if (!trace)
618 return -EPERM;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100619
Markus Metzgerc2724772008-12-11 13:49:59 +0100620 if (!trace->read)
621 return -EOPNOTSUPP;
622
623 if (size < (trace->ds.top - trace->ds.begin))
Markus Metzgercba4b652008-01-30 13:32:03 +0100624 return -EIO;
625
Markus Metzgerc2724772008-12-11 13:49:59 +0100626 for (at = trace->ds.begin; (void *)at < trace->ds.top;
627 out++, drained++, at += trace->ds.size) {
628 struct bts_struct bts;
629 int error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100630
Markus Metzgerc2724772008-12-11 13:49:59 +0100631 error = trace->read(child->bts, at, &bts);
632 if (error < 0)
633 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100634
Markus Metzgerc2724772008-12-11 13:49:59 +0100635 if (copy_to_user(out, &bts, sizeof(bts)))
Markus Metzgera95d67f2008-01-30 13:31:20 +0100636 return -EFAULT;
637 }
638
Markus Metzgerc2724772008-12-11 13:49:59 +0100639 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
640
641 error = ds_reset_bts(child->bts);
Markus Metzger93fa7632008-04-08 11:01:58 +0200642 if (error < 0)
643 return error;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100644
Markus Metzgerc2724772008-12-11 13:49:59 +0100645 return drained;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100646}
647
Markus Metzgerc5dee612008-12-19 15:17:02 +0100648static int ptrace_bts_allocate_buffer(struct task_struct *child, size_t size)
649{
650 child->bts_buffer = alloc_locked_buffer(size);
651 if (!child->bts_buffer)
652 return -ENOMEM;
653
654 child->bts_size = size;
655
656 return 0;
657}
658
659static void ptrace_bts_free_buffer(struct task_struct *child)
660{
661 free_locked_buffer(child->bts_buffer, child->bts_size);
662 child->bts_buffer = NULL;
663 child->bts_size = 0;
664}
665
Markus Metzgera95d67f2008-01-30 13:31:20 +0100666static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100667 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100668 const struct ptrace_bts_config __user *ucfg)
669{
670 struct ptrace_bts_config cfg;
Markus Metzgerc2724772008-12-11 13:49:59 +0100671 unsigned int flags = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100672
Markus Metzgercba4b652008-01-30 13:32:03 +0100673 if (cfg_size < sizeof(cfg))
Markus Metzgerc2724772008-12-11 13:49:59 +0100674 return -EIO;
Markus Metzgercba4b652008-01-30 13:32:03 +0100675
Markus Metzgera95d67f2008-01-30 13:31:20 +0100676 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
Markus Metzgerc2724772008-12-11 13:49:59 +0100677 return -EFAULT;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100678
Markus Metzgerc2724772008-12-11 13:49:59 +0100679 if (child->bts) {
680 ds_release_bts(child->bts);
681 child->bts = NULL;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100682 }
683
Markus Metzgerc2724772008-12-11 13:49:59 +0100684 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
685 if (!cfg.signal)
686 return -EINVAL;
687
688 return -EOPNOTSUPP;
689
690 child->thread.bts_ovfl_signal = cfg.signal;
691 }
692
693 if ((cfg.flags & PTRACE_BTS_O_ALLOC) &&
694 (cfg.size != child->bts_size)) {
Markus Metzgerc5dee612008-12-19 15:17:02 +0100695 int error;
Markus Metzgerc2724772008-12-11 13:49:59 +0100696
Markus Metzgerc5dee612008-12-19 15:17:02 +0100697 ptrace_bts_free_buffer(child);
698
699 error = ptrace_bts_allocate_buffer(child, cfg.size);
700 if (error < 0)
701 return error;
Markus Metzgerc2724772008-12-11 13:49:59 +0100702 }
Markus Metzgera95d67f2008-01-30 13:31:20 +0100703
Markus Metzgerda35c372008-01-30 13:32:03 +0100704 if (cfg.flags & PTRACE_BTS_O_TRACE)
Markus Metzgerc2724772008-12-11 13:49:59 +0100705 flags |= BTS_USER;
Markus Metzgereee3af42008-01-30 13:31:09 +0100706
Markus Metzgerda35c372008-01-30 13:32:03 +0100707 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgerc2724772008-12-11 13:49:59 +0100708 flags |= BTS_TIMESTAMPS;
Markus Metzgereee3af42008-01-30 13:31:09 +0100709
Markus Metzgerc2724772008-12-11 13:49:59 +0100710 child->bts = ds_request_bts(child, child->bts_buffer, child->bts_size,
711 /* ovfl = */ NULL, /* th = */ (size_t)-1,
712 flags);
713 if (IS_ERR(child->bts)) {
714 int error = PTR_ERR(child->bts);
Markus Metzgercba4b652008-01-30 13:32:03 +0100715
Markus Metzgerc5dee612008-12-19 15:17:02 +0100716 ptrace_bts_free_buffer(child);
Markus Metzgerc2724772008-12-11 13:49:59 +0100717 child->bts = NULL;
Markus Metzgerda35c372008-01-30 13:32:03 +0100718
Markus Metzgerc2724772008-12-11 13:49:59 +0100719 return error;
720 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100721
Markus Metzgerc2724772008-12-11 13:49:59 +0100722 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100723}
724
Markus Metzgera95d67f2008-01-30 13:31:20 +0100725static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100726 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100727 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100728{
Markus Metzgerc2724772008-12-11 13:49:59 +0100729 const struct bts_trace *trace;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100730 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100731
Markus Metzgercba4b652008-01-30 13:32:03 +0100732 if (cfg_size < sizeof(cfg))
733 return -EIO;
734
Markus Metzgerc2724772008-12-11 13:49:59 +0100735 trace = ds_read_bts(child->bts);
736 if (!trace)
737 return -EPERM;
Markus Metzger93fa7632008-04-08 11:01:58 +0200738
Markus Metzgera95d67f2008-01-30 13:31:20 +0100739 memset(&cfg, 0, sizeof(cfg));
Markus Metzgerc2724772008-12-11 13:49:59 +0100740 cfg.size = trace->ds.end - trace->ds.begin;
Markus Metzger93fa7632008-04-08 11:01:58 +0200741 cfg.signal = child->thread.bts_ovfl_signal;
Markus Metzger87e84072008-01-30 13:32:54 +0100742 cfg.bts_size = sizeof(struct bts_struct);
743
Markus Metzger93fa7632008-04-08 11:01:58 +0200744 if (cfg.signal)
745 cfg.flags |= PTRACE_BTS_O_SIGNAL;
746
Markus Metzgerc2724772008-12-11 13:49:59 +0100747 if (trace->ds.flags & BTS_USER)
Markus Metzger93fa7632008-04-08 11:01:58 +0200748 cfg.flags |= PTRACE_BTS_O_TRACE;
749
Markus Metzgerc2724772008-12-11 13:49:59 +0100750 if (trace->ds.flags & BTS_TIMESTAMPS)
Markus Metzger93fa7632008-04-08 11:01:58 +0200751 cfg.flags |= PTRACE_BTS_O_SCHED;
752
Markus Metzgera95d67f2008-01-30 13:31:20 +0100753 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
754 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100755
Markus Metzgera95d67f2008-01-30 13:31:20 +0100756 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100757}
758
Markus Metzgerc2724772008-12-11 13:49:59 +0100759static int ptrace_bts_clear(struct task_struct *child)
Andrew Mortond8d4f152008-03-04 15:05:39 -0800760{
Markus Metzgerc2724772008-12-11 13:49:59 +0100761 const struct bts_trace *trace;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800762
Markus Metzgerc2724772008-12-11 13:49:59 +0100763 trace = ds_read_bts(child->bts);
764 if (!trace)
765 return -EPERM;
Andrew Mortond8d4f152008-03-04 15:05:39 -0800766
Markus Metzgerc2724772008-12-11 13:49:59 +0100767 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800768
Markus Metzgerc2724772008-12-11 13:49:59 +0100769 return ds_reset_bts(child->bts);
Andrew Mortond8d4f152008-03-04 15:05:39 -0800770}
771
Markus Metzgerc2724772008-12-11 13:49:59 +0100772static int ptrace_bts_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100773{
Markus Metzgerc2724772008-12-11 13:49:59 +0100774 const struct bts_trace *trace;
Markus Metzgereee3af42008-01-30 13:31:09 +0100775
Markus Metzgerc2724772008-12-11 13:49:59 +0100776 trace = ds_read_bts(child->bts);
777 if (!trace)
778 return -EPERM;
Markus Metzger93fa7632008-04-08 11:01:58 +0200779
Markus Metzgerc2724772008-12-11 13:49:59 +0100780 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
Markus Metzger93fa7632008-04-08 11:01:58 +0200781}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100782
783static void ptrace_bts_fork(struct task_struct *tsk)
784{
785 tsk->bts = NULL;
786 tsk->bts_buffer = NULL;
787 tsk->bts_size = 0;
788 tsk->thread.bts_ovfl_signal = 0;
789}
790
791static void ptrace_bts_untrace(struct task_struct *child)
792{
793 if (unlikely(child->bts)) {
794 ds_release_bts(child->bts);
795 child->bts = NULL;
796
Markus Metzgerc5dee612008-12-19 15:17:02 +0100797 /* We cannot update total_vm and locked_vm since
798 child's mm is already gone. But we can reclaim the
799 memory. */
Markus Metzgerbf53de92008-12-19 15:10:24 +0100800 kfree(child->bts_buffer);
801 child->bts_buffer = NULL;
802 child->bts_size = 0;
803 }
804}
805
806static void ptrace_bts_detach(struct task_struct *child)
807{
Markus Metzgerc5dee612008-12-19 15:17:02 +0100808 if (unlikely(child->bts)) {
809 ds_release_bts(child->bts);
810 child->bts = NULL;
811
812 ptrace_bts_free_buffer(child);
813 }
Markus Metzgerbf53de92008-12-19 15:10:24 +0100814}
815#else
816static inline void ptrace_bts_fork(struct task_struct *tsk) {}
817static inline void ptrace_bts_detach(struct task_struct *child) {}
818static inline void ptrace_bts_untrace(struct task_struct *child) {}
Markus Metzger93fa7632008-04-08 11:01:58 +0200819#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100820
Markus Metzgerbf53de92008-12-19 15:10:24 +0100821void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
822{
823 ptrace_bts_fork(child);
824}
825
826void x86_ptrace_untrace(struct task_struct *child)
827{
828 ptrace_bts_untrace(child);
829}
830
Roland McGrathd9771e82008-01-30 13:30:52 +0100831/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * Called by kernel/ptrace.c when detaching..
833 *
834 * Make sure the single step bit is not set.
835 */
836void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100837{
Roland McGrath7f232342008-01-30 13:30:48 +0100838 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100839#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700840 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100841#endif
Markus Metzgerbf53de92008-12-19 15:10:24 +0100842 ptrace_bts_detach(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Roland McGrath5a4646a2008-01-30 13:31:54 +0100845#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
846static const struct user_regset_view user_x86_32_view; /* Initialized below. */
847#endif
848
Christoph Hellwig481bed42005-11-07 00:59:47 -0800849long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100851 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned long __user *datap = (unsigned long __user *)data;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* read the word at location addr in the USER area. */
856 case PTRACE_PEEKUSR: {
857 unsigned long tmp;
858
859 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100860 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
861 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863
864 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100865 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100867 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
868 addr <= offsetof(struct user, u_debugreg[7])) {
869 addr -= offsetof(struct user, u_debugreg[0]);
870 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 ret = put_user(tmp, datap);
873 break;
874 }
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
877 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100878 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
879 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
881
Roland McGrathe9c86c72008-01-30 13:31:01 +0100882 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100884 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
885 addr <= offsetof(struct user, u_debugreg[7])) {
886 addr -= offsetof(struct user, u_debugreg[0]);
887 ret = ptrace_set_debugreg(child,
888 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100890 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Roland McGrath5a4646a2008-01-30 13:31:54 +0100892 case PTRACE_GETREGS: /* Get all gp regs from the child. */
893 return copy_regset_to_user(child,
894 task_user_regset_view(current),
895 REGSET_GENERAL,
896 0, sizeof(struct user_regs_struct),
897 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Roland McGrath5a4646a2008-01-30 13:31:54 +0100899 case PTRACE_SETREGS: /* Set all gp regs in the child. */
900 return copy_regset_from_user(child,
901 task_user_regset_view(current),
902 REGSET_GENERAL,
903 0, sizeof(struct user_regs_struct),
904 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Roland McGrath5a4646a2008-01-30 13:31:54 +0100906 case PTRACE_GETFPREGS: /* Get the child FPU state. */
907 return copy_regset_to_user(child,
908 task_user_regset_view(current),
909 REGSET_FP,
910 0, sizeof(struct user_i387_struct),
911 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Roland McGrath5a4646a2008-01-30 13:31:54 +0100913 case PTRACE_SETFPREGS: /* Set the child FPU state. */
914 return copy_regset_from_user(child,
915 task_user_regset_view(current),
916 REGSET_FP,
917 0, sizeof(struct user_i387_struct),
918 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Roland McGrathe9c86c72008-01-30 13:31:01 +0100920#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100921 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
922 return copy_regset_to_user(child, &user_x86_32_view,
923 REGSET_XFP,
924 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700925 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Roland McGrath5a4646a2008-01-30 13:31:54 +0100927 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
928 return copy_regset_from_user(child, &user_x86_32_view,
929 REGSET_XFP,
930 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700931 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100932#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Roland McGrathe9c86c72008-01-30 13:31:01 +0100934#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100936 if (addr < 0)
937 return -EIO;
938 ret = do_get_thread_area(child, addr,
939 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
941
942 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100943 if (addr < 0)
944 return -EIO;
945 ret = do_set_thread_area(child, addr,
946 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100948#endif
949
950#ifdef CONFIG_X86_64
951 /* normal 64bit interface to access TLS data.
952 Works just like arch_prctl, except that the arguments
953 are reversed. */
954 case PTRACE_ARCH_PRCTL:
955 ret = do_arch_prctl(child, data, addr);
956 break;
957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Ingo Molnarb4ef95d2008-02-26 09:40:27 +0100959 /*
960 * These bits need more cooking - not enabled yet:
961 */
Markus Metzger93fa7632008-04-08 11:01:58 +0200962#ifdef CONFIG_X86_PTRACE_BTS
Markus Metzgereee3af42008-01-30 13:31:09 +0100963 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100964 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +0100965 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100966 break;
967
968 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100969 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +0100970 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100971 break;
972
Markus Metzgerc2724772008-12-11 13:49:59 +0100973 case PTRACE_BTS_SIZE:
974 ret = ptrace_bts_size(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100975 break;
976
977 case PTRACE_BTS_GET:
978 ret = ptrace_bts_read_record
979 (child, data, (struct bts_struct __user *) addr);
980 break;
981
982 case PTRACE_BTS_CLEAR:
Markus Metzgerc2724772008-12-11 13:49:59 +0100983 ret = ptrace_bts_clear(child);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100984 break;
985
986 case PTRACE_BTS_DRAIN:
987 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +0100988 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100989 break;
Markus Metzger93fa7632008-04-08 11:01:58 +0200990#endif /* CONFIG_X86_PTRACE_BTS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 default:
993 ret = ptrace_request(child, request, addr, data);
994 break;
995 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return ret;
998}
999
Roland McGrathcb757c42008-01-30 13:31:01 +01001000#ifdef CONFIG_IA32_EMULATION
1001
Roland McGrath099cd6e2008-01-30 13:31:01 +01001002#include <linux/compat.h>
1003#include <linux/syscalls.h>
1004#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +01001005#include <asm/user32.h>
1006
1007#define R32(l,q) \
1008 case offsetof(struct user32, regs.l): \
1009 regs->q = value; break
1010
1011#define SEG32(rs) \
1012 case offsetof(struct user32, regs.rs): \
1013 return set_segment_reg(child, \
1014 offsetof(struct user_regs_struct, rs), \
1015 value); \
1016 break
1017
1018static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1019{
1020 struct pt_regs *regs = task_pt_regs(child);
1021
1022 switch (regno) {
1023
1024 SEG32(cs);
1025 SEG32(ds);
1026 SEG32(es);
1027 SEG32(fs);
1028 SEG32(gs);
1029 SEG32(ss);
1030
1031 R32(ebx, bx);
1032 R32(ecx, cx);
1033 R32(edx, dx);
1034 R32(edi, di);
1035 R32(esi, si);
1036 R32(ebp, bp);
1037 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +01001038 R32(eip, ip);
1039 R32(esp, sp);
1040
Roland McGrath40f09332008-02-28 19:57:07 -08001041 case offsetof(struct user32, regs.orig_eax):
1042 /*
1043 * Sign-extend the value so that orig_eax = -1
1044 * causes (long)orig_ax < 0 tests to fire correctly.
1045 */
1046 regs->orig_ax = (long) (s32) value;
1047 break;
1048
Roland McGrathcb757c42008-01-30 13:31:01 +01001049 case offsetof(struct user32, regs.eflags):
1050 return set_flags(child, value);
1051
1052 case offsetof(struct user32, u_debugreg[0]) ...
1053 offsetof(struct user32, u_debugreg[7]):
1054 regno -= offsetof(struct user32, u_debugreg[0]);
1055 return ptrace_set_debugreg(child, regno / 4, value);
1056
1057 default:
1058 if (regno > sizeof(struct user32) || (regno & 3))
1059 return -EIO;
1060
1061 /*
1062 * Other dummy fields in the virtual user structure
1063 * are ignored
1064 */
1065 break;
1066 }
1067 return 0;
1068}
1069
1070#undef R32
1071#undef SEG32
1072
1073#define R32(l,q) \
1074 case offsetof(struct user32, regs.l): \
1075 *val = regs->q; break
1076
1077#define SEG32(rs) \
1078 case offsetof(struct user32, regs.rs): \
1079 *val = get_segment_reg(child, \
1080 offsetof(struct user_regs_struct, rs)); \
1081 break
1082
1083static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1084{
1085 struct pt_regs *regs = task_pt_regs(child);
1086
1087 switch (regno) {
1088
1089 SEG32(ds);
1090 SEG32(es);
1091 SEG32(fs);
1092 SEG32(gs);
1093
1094 R32(cs, cs);
1095 R32(ss, ss);
1096 R32(ebx, bx);
1097 R32(ecx, cx);
1098 R32(edx, dx);
1099 R32(edi, di);
1100 R32(esi, si);
1101 R32(ebp, bp);
1102 R32(eax, ax);
1103 R32(orig_eax, orig_ax);
1104 R32(eip, ip);
1105 R32(esp, sp);
1106
1107 case offsetof(struct user32, regs.eflags):
1108 *val = get_flags(child);
1109 break;
1110
1111 case offsetof(struct user32, u_debugreg[0]) ...
1112 offsetof(struct user32, u_debugreg[7]):
1113 regno -= offsetof(struct user32, u_debugreg[0]);
1114 *val = ptrace_get_debugreg(child, regno / 4);
1115 break;
1116
1117 default:
1118 if (regno > sizeof(struct user32) || (regno & 3))
1119 return -EIO;
1120
1121 /*
1122 * Other dummy fields in the virtual user structure
1123 * are ignored
1124 */
1125 *val = 0;
1126 break;
1127 }
1128 return 0;
1129}
1130
1131#undef R32
1132#undef SEG32
1133
Roland McGrath91e7b702008-01-30 13:31:52 +01001134static int genregs32_get(struct task_struct *target,
1135 const struct user_regset *regset,
1136 unsigned int pos, unsigned int count,
1137 void *kbuf, void __user *ubuf)
1138{
1139 if (kbuf) {
1140 compat_ulong_t *k = kbuf;
1141 while (count > 0) {
1142 getreg32(target, pos, k++);
1143 count -= sizeof(*k);
1144 pos += sizeof(*k);
1145 }
1146 } else {
1147 compat_ulong_t __user *u = ubuf;
1148 while (count > 0) {
1149 compat_ulong_t word;
1150 getreg32(target, pos, &word);
1151 if (__put_user(word, u++))
1152 return -EFAULT;
1153 count -= sizeof(*u);
1154 pos += sizeof(*u);
1155 }
1156 }
1157
1158 return 0;
1159}
1160
1161static int genregs32_set(struct task_struct *target,
1162 const struct user_regset *regset,
1163 unsigned int pos, unsigned int count,
1164 const void *kbuf, const void __user *ubuf)
1165{
1166 int ret = 0;
1167 if (kbuf) {
1168 const compat_ulong_t *k = kbuf;
1169 while (count > 0 && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001170 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001171 count -= sizeof(*k);
1172 pos += sizeof(*k);
1173 }
1174 } else {
1175 const compat_ulong_t __user *u = ubuf;
1176 while (count > 0 && !ret) {
1177 compat_ulong_t word;
1178 ret = __get_user(word, u++);
1179 if (ret)
1180 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001181 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001182 count -= sizeof(*u);
1183 pos += sizeof(*u);
1184 }
1185 }
1186 return ret;
1187}
1188
Roland McGrath562b80b2008-04-22 12:21:25 -07001189long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1190 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001191{
Roland McGrath562b80b2008-04-22 12:21:25 -07001192 unsigned long addr = caddr;
1193 unsigned long data = cdata;
Roland McGrath099cd6e2008-01-30 13:31:01 +01001194 void __user *datap = compat_ptr(data);
1195 int ret;
1196 __u32 val;
1197
1198 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001199 case PTRACE_PEEKUSR:
1200 ret = getreg32(child, addr, &val);
1201 if (ret == 0)
1202 ret = put_user(val, (__u32 __user *)datap);
1203 break;
1204
1205 case PTRACE_POKEUSR:
1206 ret = putreg32(child, addr, data);
1207 break;
1208
Roland McGrath5a4646a2008-01-30 13:31:54 +01001209 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1210 return copy_regset_to_user(child, &user_x86_32_view,
1211 REGSET_GENERAL,
1212 0, sizeof(struct user_regs_struct32),
1213 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001214
Roland McGrath5a4646a2008-01-30 13:31:54 +01001215 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1216 return copy_regset_from_user(child, &user_x86_32_view,
1217 REGSET_GENERAL, 0,
1218 sizeof(struct user_regs_struct32),
1219 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001220
Roland McGrath5a4646a2008-01-30 13:31:54 +01001221 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1222 return copy_regset_to_user(child, &user_x86_32_view,
1223 REGSET_FP, 0,
1224 sizeof(struct user_i387_ia32_struct),
1225 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001226
Roland McGrath5a4646a2008-01-30 13:31:54 +01001227 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1228 return copy_regset_from_user(
1229 child, &user_x86_32_view, REGSET_FP,
1230 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001231
Roland McGrath5a4646a2008-01-30 13:31:54 +01001232 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1233 return copy_regset_to_user(child, &user_x86_32_view,
1234 REGSET_XFP, 0,
1235 sizeof(struct user32_fxsr_struct),
1236 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001237
Roland McGrath5a4646a2008-01-30 13:31:54 +01001238 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1239 return copy_regset_from_user(child, &user_x86_32_view,
1240 REGSET_XFP, 0,
1241 sizeof(struct user32_fxsr_struct),
1242 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001243
Roland McGrath562b80b2008-04-22 12:21:25 -07001244 case PTRACE_GET_THREAD_AREA:
1245 case PTRACE_SET_THREAD_AREA:
Markus Metzgerc2724772008-12-11 13:49:59 +01001246#ifdef CONFIG_X86_PTRACE_BTS
1247 case PTRACE_BTS_CONFIG:
1248 case PTRACE_BTS_STATUS:
1249 case PTRACE_BTS_SIZE:
1250 case PTRACE_BTS_GET:
1251 case PTRACE_BTS_CLEAR:
1252 case PTRACE_BTS_DRAIN:
1253#endif /* CONFIG_X86_PTRACE_BTS */
Roland McGrath562b80b2008-04-22 12:21:25 -07001254 return arch_ptrace(child, request, addr, data);
1255
Roland McGrath099cd6e2008-01-30 13:31:01 +01001256 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001257 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001258 }
1259
Roland McGrath099cd6e2008-01-30 13:31:01 +01001260 return ret;
1261}
1262
Roland McGrathcb757c42008-01-30 13:31:01 +01001263#endif /* CONFIG_IA32_EMULATION */
1264
Roland McGrath070459d2008-01-30 13:31:53 +01001265#ifdef CONFIG_X86_64
1266
1267static const struct user_regset x86_64_regsets[] = {
1268 [REGSET_GENERAL] = {
1269 .core_note_type = NT_PRSTATUS,
1270 .n = sizeof(struct user_regs_struct) / sizeof(long),
1271 .size = sizeof(long), .align = sizeof(long),
1272 .get = genregs_get, .set = genregs_set
1273 },
1274 [REGSET_FP] = {
1275 .core_note_type = NT_PRFPREG,
1276 .n = sizeof(struct user_i387_struct) / sizeof(long),
1277 .size = sizeof(long), .align = sizeof(long),
1278 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1279 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001280 [REGSET_IOPERM64] = {
1281 .core_note_type = NT_386_IOPERM,
1282 .n = IO_BITMAP_LONGS,
1283 .size = sizeof(long), .align = sizeof(long),
1284 .active = ioperm_active, .get = ioperm_get
1285 },
Roland McGrath070459d2008-01-30 13:31:53 +01001286};
1287
1288static const struct user_regset_view user_x86_64_view = {
1289 .name = "x86_64", .e_machine = EM_X86_64,
1290 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1291};
1292
1293#else /* CONFIG_X86_32 */
1294
1295#define user_regs_struct32 user_regs_struct
1296#define genregs32_get genregs_get
1297#define genregs32_set genregs_set
1298
Roland McGrath1f465f42008-05-09 15:43:44 -07001299#define user_i387_ia32_struct user_i387_struct
1300#define user32_fxsr_struct user_fxsr_struct
1301
Roland McGrath070459d2008-01-30 13:31:53 +01001302#endif /* CONFIG_X86_64 */
1303
1304#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1305static const struct user_regset x86_32_regsets[] = {
1306 [REGSET_GENERAL] = {
1307 .core_note_type = NT_PRSTATUS,
1308 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1309 .size = sizeof(u32), .align = sizeof(u32),
1310 .get = genregs32_get, .set = genregs32_set
1311 },
1312 [REGSET_FP] = {
1313 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001314 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001315 .size = sizeof(u32), .align = sizeof(u32),
1316 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1317 },
1318 [REGSET_XFP] = {
1319 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001320 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001321 .size = sizeof(u32), .align = sizeof(u32),
1322 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1323 },
1324 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001325 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001326 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1327 .size = sizeof(struct user_desc),
1328 .align = sizeof(struct user_desc),
1329 .active = regset_tls_active,
1330 .get = regset_tls_get, .set = regset_tls_set
1331 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001332 [REGSET_IOPERM32] = {
1333 .core_note_type = NT_386_IOPERM,
1334 .n = IO_BITMAP_BYTES / sizeof(u32),
1335 .size = sizeof(u32), .align = sizeof(u32),
1336 .active = ioperm_active, .get = ioperm_get
1337 },
Roland McGrath070459d2008-01-30 13:31:53 +01001338};
1339
1340static const struct user_regset_view user_x86_32_view = {
1341 .name = "i386", .e_machine = EM_386,
1342 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1343};
1344#endif
1345
1346const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1347{
1348#ifdef CONFIG_IA32_EMULATION
1349 if (test_tsk_thread_flag(task, TIF_IA32))
1350#endif
1351#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1352 return &user_x86_32_view;
1353#endif
1354#ifdef CONFIG_X86_64
1355 return &user_x86_64_view;
1356#endif
1357}
1358
Srinivasa Dsda654b72008-09-23 15:23:52 +05301359void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1360 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct siginfo info;
1363
1364 tsk->thread.trap_no = 1;
1365 tsk->thread.error_code = error_code;
1366
1367 memset(&info, 0, sizeof(info));
1368 info.si_signo = SIGTRAP;
Srinivasa Dsda654b72008-09-23 15:23:52 +05301369 info.si_code = si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001371 /* User-mode ip? */
1372 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Simon Arlott27b46d72007-10-20 01:13:56 +02001374 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 force_sig_info(SIGTRAP, &info, tsk);
1376}
1377
Roland McGrath86976cd2008-01-30 13:31:01 +01001378
Roland McGrathd4d67152008-07-09 02:38:07 -07001379#ifdef CONFIG_X86_32
1380# define IS_IA32 1
1381#elif defined CONFIG_IA32_EMULATION
1382# define IS_IA32 test_thread_flag(TIF_IA32)
1383#else
1384# define IS_IA32 0
1385#endif
1386
1387/*
1388 * We must return the syscall number to actually look up in the table.
1389 * This can be -1L to skip running any syscall at all.
1390 */
1391asmregparm long syscall_trace_enter(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001392{
Roland McGrathd4d67152008-07-09 02:38:07 -07001393 long ret = 0;
1394
Roland McGrath380fdd72008-07-09 02:39:29 -07001395 /*
1396 * If we stepped into a sysenter/syscall insn, it trapped in
1397 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1398 * If user-mode had set TF itself, then it's still clear from
1399 * do_debug() and we need to set it again to restore the user
1400 * state. If we entered on the slow path, TF was already set.
1401 */
1402 if (test_thread_flag(TIF_SINGLESTEP))
1403 regs->flags |= X86_EFLAGS_TF;
1404
Roland McGrath86976cd2008-01-30 13:31:01 +01001405 /* do the secure computing check first */
1406 secure_computing(regs->orig_ax);
1407
Roland McGrathd4d67152008-07-09 02:38:07 -07001408 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1409 ret = -1L;
1410
Roland McGratheeea3c32008-03-16 23:36:28 -07001411 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1412 tracehook_report_syscall_entry(regs))
1413 ret = -1L;
Roland McGrath86976cd2008-01-30 13:31:01 +01001414
1415 if (unlikely(current->audit_context)) {
Roland McGrathd4d67152008-07-09 02:38:07 -07001416 if (IS_IA32)
Roland McGrath86976cd2008-01-30 13:31:01 +01001417 audit_syscall_entry(AUDIT_ARCH_I386,
1418 regs->orig_ax,
1419 regs->bx, regs->cx,
1420 regs->dx, regs->si);
Roland McGrathd4d67152008-07-09 02:38:07 -07001421#ifdef CONFIG_X86_64
1422 else
Roland McGrath86976cd2008-01-30 13:31:01 +01001423 audit_syscall_entry(AUDIT_ARCH_X86_64,
1424 regs->orig_ax,
1425 regs->di, regs->si,
1426 regs->dx, regs->r10);
Roland McGrathd4d67152008-07-09 02:38:07 -07001427#endif
Roland McGrath86976cd2008-01-30 13:31:01 +01001428 }
Roland McGrathd4d67152008-07-09 02:38:07 -07001429
1430 return ret ?: regs->orig_ax;
Roland McGrath86976cd2008-01-30 13:31:01 +01001431}
1432
Roland McGrathd4d67152008-07-09 02:38:07 -07001433asmregparm void syscall_trace_leave(struct pt_regs *regs)
Roland McGrath86976cd2008-01-30 13:31:01 +01001434{
1435 if (unlikely(current->audit_context))
1436 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1437
Roland McGrathd4d67152008-07-09 02:38:07 -07001438 if (test_thread_flag(TIF_SYSCALL_TRACE))
Roland McGratheeea3c32008-03-16 23:36:28 -07001439 tracehook_report_syscall_exit(regs, 0);
Roland McGrath86976cd2008-01-30 13:31:01 +01001440
Roland McGrathd4d67152008-07-09 02:38:07 -07001441 /*
1442 * If TIF_SYSCALL_EMU is set, we only get here because of
1443 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1444 * We already reported this syscall instruction in
1445 * syscall_trace_enter(), so don't do any more now.
1446 */
1447 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1448 return;
1449
1450 /*
1451 * If we are single-stepping, synthesize a trap to follow the
1452 * system call instruction.
1453 */
1454 if (test_thread_flag(TIF_SINGLESTEP) &&
Roland McGratheeea3c32008-03-16 23:36:28 -07001455 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
Srinivasa Dsda654b72008-09-23 15:23:52 +05301456 send_sigtrap(current, regs, 0, TRAP_BRKPT);
Roland McGrathd4d67152008-07-09 02:38:07 -07001457}