blob: 18972a305890547701b19c0ecda0c3d705359aa2 [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>
16#include <linux/user.h>
17#include <linux/security.h>
18#include <linux/audit.h>
19#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/uaccess.h>
23#include <asm/pgtable.h>
24#include <asm/system.h>
25#include <asm/processor.h>
26#include <asm/i387.h>
27#include <asm/debugreg.h>
28#include <asm/ldt.h>
29#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010030#include <asm/prctl.h>
31#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010032#include <asm/ds.h>
33
34
35/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * does not yet catch signals sent when the child dies.
37 * in exit.c or in signal.c.
38 */
39
Chuck Ebbert9f155b92006-01-05 23:11:29 -050040/*
41 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050042 */
Roland McGrathe39c2892008-01-30 13:31:01 +010043#define FLAG_MASK_32 ((unsigned long) \
44 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
45 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
46 X86_EFLAGS_SF | X86_EFLAGS_TF | \
47 X86_EFLAGS_DF | X86_EFLAGS_OF | \
48 X86_EFLAGS_RF | X86_EFLAGS_AC))
49
Roland McGrath2047b082008-01-30 13:31:01 +010050/*
51 * Determines whether a value may be installed in a segment register.
52 */
53static inline bool invalid_selector(u16 value)
54{
55 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
56}
57
58#ifdef CONFIG_X86_32
59
Roland McGrathe39c2892008-01-30 13:31:01 +010060#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Roland McGrath62a97d42008-01-30 13:30:52 +010062static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010064 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Roland McGrath06ee1b62008-01-30 13:31:01 +010065 regno >>= 2;
Roland McGrath62a97d42008-01-30 13:30:52 +010066 if (regno > FS)
67 --regno;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010068 return &regs->bx + regno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Roland McGrath06ee1b62008-01-30 13:31:01 +010071static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Roland McGrath06ee1b62008-01-30 13:31:01 +010073 /*
74 * Returning the value truncates it to 16 bits.
75 */
76 unsigned int retval;
77 if (offset != offsetof(struct user_regs_struct, gs))
78 retval = *pt_regs_access(task_pt_regs(task), offset);
79 else {
80 retval = task->thread.gs;
81 if (task == current)
82 savesegment(gs, retval);
83 }
84 return retval;
85}
86
87static int set_segment_reg(struct task_struct *task,
88 unsigned long offset, u16 value)
89{
90 /*
91 * The value argument was already truncated to 16 bits.
92 */
Roland McGrath2047b082008-01-30 13:31:01 +010093 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +010094 return -EIO;
95
96 if (offset != offsetof(struct user_regs_struct, gs))
97 *pt_regs_access(task_pt_regs(task), offset) = value;
98 else {
99 task->thread.gs = value;
100 if (task == current)
Roland McGrath5fd4d162008-01-30 13:30:58 +0100101 /*
102 * The user-mode %gs is not affected by
103 * kernel entry, so we must update the CPU.
104 */
105 loadsegment(gs, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
Roland McGrath2047b082008-01-30 13:31:01 +0100111static unsigned long debugreg_addr_limit(struct task_struct *task)
112{
113 return TASK_SIZE - 3;
114}
115
116#else /* CONFIG_X86_64 */
117
118#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
119
120static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
121{
122 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
123 return &regs->r15 + (offset / sizeof(regs->r15));
124}
125
126static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
127{
128 /*
129 * Returning the value truncates it to 16 bits.
130 */
131 unsigned int seg;
132
133 switch (offset) {
134 case offsetof(struct user_regs_struct, fs):
135 if (task == current) {
136 /* Older gas can't assemble movq %?s,%r?? */
137 asm("movl %%fs,%0" : "=r" (seg));
138 return seg;
139 }
140 return task->thread.fsindex;
141 case offsetof(struct user_regs_struct, gs):
142 if (task == current) {
143 asm("movl %%gs,%0" : "=r" (seg));
144 return seg;
145 }
146 return task->thread.gsindex;
147 case offsetof(struct user_regs_struct, ds):
148 if (task == current) {
149 asm("movl %%ds,%0" : "=r" (seg));
150 return seg;
151 }
152 return task->thread.ds;
153 case offsetof(struct user_regs_struct, es):
154 if (task == current) {
155 asm("movl %%es,%0" : "=r" (seg));
156 return seg;
157 }
158 return task->thread.es;
159
160 case offsetof(struct user_regs_struct, cs):
161 case offsetof(struct user_regs_struct, ss):
162 break;
163 }
164 return *pt_regs_access(task_pt_regs(task), offset);
165}
166
167static int set_segment_reg(struct task_struct *task,
168 unsigned long offset, u16 value)
169{
170 /*
171 * The value argument was already truncated to 16 bits.
172 */
173 if (invalid_selector(value))
174 return -EIO;
175
176 switch (offset) {
177 case offsetof(struct user_regs_struct,fs):
178 /*
179 * If this is setting fs as for normal 64-bit use but
180 * setting fs_base has implicitly changed it, leave it.
181 */
182 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
183 task->thread.fs != 0) ||
184 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
185 task->thread.fs == 0))
186 break;
187 task->thread.fsindex = value;
188 if (task == current)
189 loadsegment(fs, task->thread.fsindex);
190 break;
191 case offsetof(struct user_regs_struct,gs):
192 /*
193 * If this is setting gs as for normal 64-bit use but
194 * setting gs_base has implicitly changed it, leave it.
195 */
196 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
197 task->thread.gs != 0) ||
198 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
199 task->thread.gs == 0))
200 break;
201 task->thread.gsindex = value;
202 if (task == current)
203 load_gs_index(task->thread.gsindex);
204 break;
205 case offsetof(struct user_regs_struct,ds):
206 task->thread.ds = value;
207 if (task == current)
208 loadsegment(ds, task->thread.ds);
209 break;
210 case offsetof(struct user_regs_struct,es):
211 task->thread.es = value;
212 if (task == current)
213 loadsegment(es, task->thread.es);
214 break;
215
216 /*
217 * Can't actually change these in 64-bit mode.
218 */
219 case offsetof(struct user_regs_struct,cs):
220#ifdef CONFIG_IA32_EMULATION
221 if (test_tsk_thread_flag(task, TIF_IA32))
222 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100223#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100224 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100225 case offsetof(struct user_regs_struct,ss):
226#ifdef CONFIG_IA32_EMULATION
227 if (test_tsk_thread_flag(task, TIF_IA32))
228 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100229#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100230 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100231 }
232
233 return 0;
234}
235
236static unsigned long debugreg_addr_limit(struct task_struct *task)
237{
238#ifdef CONFIG_IA32_EMULATION
239 if (test_tsk_thread_flag(task, TIF_IA32))
240 return IA32_PAGE_OFFSET - 3;
241#endif
242 return TASK_SIZE64 - 7;
243}
244
245#endif /* CONFIG_X86_32 */
246
Roland McGrath06ee1b62008-01-30 13:31:01 +0100247static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100249 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Roland McGrath06ee1b62008-01-30 13:31:01 +0100251 /*
252 * If the debugger set TF, hide it from the readout.
253 */
254 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
255 retval &= ~X86_EFLAGS_TF;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return retval;
258}
259
Roland McGrath06ee1b62008-01-30 13:31:01 +0100260static int set_flags(struct task_struct *task, unsigned long value)
261{
262 struct pt_regs *regs = task_pt_regs(task);
263
264 /*
265 * If the user value contains TF, mark that
266 * it was not "us" (the debugger) that set it.
267 * If not, make sure it stays set if we had.
268 */
269 if (value & X86_EFLAGS_TF)
270 clear_tsk_thread_flag(task, TIF_FORCED_TF);
271 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
272 value |= X86_EFLAGS_TF;
273
274 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
275
276 return 0;
277}
278
279static int putreg(struct task_struct *child,
280 unsigned long offset, unsigned long value)
281{
282 switch (offset) {
283 case offsetof(struct user_regs_struct, cs):
284 case offsetof(struct user_regs_struct, ds):
285 case offsetof(struct user_regs_struct, es):
286 case offsetof(struct user_regs_struct, fs):
287 case offsetof(struct user_regs_struct, gs):
288 case offsetof(struct user_regs_struct, ss):
289 return set_segment_reg(child, offset, value);
290
291 case offsetof(struct user_regs_struct, flags):
292 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100293
294#ifdef CONFIG_X86_64
295 case offsetof(struct user_regs_struct,fs_base):
296 if (value >= TASK_SIZE_OF(child))
297 return -EIO;
298 /*
299 * When changing the segment base, use do_arch_prctl
300 * to set either thread.fs or thread.fsindex and the
301 * corresponding GDT slot.
302 */
303 if (child->thread.fs != value)
304 return do_arch_prctl(child, ARCH_SET_FS, value);
305 return 0;
306 case offsetof(struct user_regs_struct,gs_base):
307 /*
308 * Exactly the same here as the %fs handling above.
309 */
310 if (value >= TASK_SIZE_OF(child))
311 return -EIO;
312 if (child->thread.gs != value)
313 return do_arch_prctl(child, ARCH_SET_GS, value);
314 return 0;
315#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100316 }
317
318 *pt_regs_access(task_pt_regs(child), offset) = value;
319 return 0;
320}
321
322static unsigned long getreg(struct task_struct *task, unsigned long offset)
323{
324 switch (offset) {
325 case offsetof(struct user_regs_struct, cs):
326 case offsetof(struct user_regs_struct, ds):
327 case offsetof(struct user_regs_struct, es):
328 case offsetof(struct user_regs_struct, fs):
329 case offsetof(struct user_regs_struct, gs):
330 case offsetof(struct user_regs_struct, ss):
331 return get_segment_reg(task, offset);
332
333 case offsetof(struct user_regs_struct, flags):
334 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100335
336#ifdef CONFIG_X86_64
337 case offsetof(struct user_regs_struct, fs_base): {
338 /*
339 * do_arch_prctl may have used a GDT slot instead of
340 * the MSR. To userland, it appears the same either
341 * way, except the %fs segment selector might not be 0.
342 */
343 unsigned int seg = task->thread.fsindex;
344 if (task->thread.fs != 0)
345 return task->thread.fs;
346 if (task == current)
347 asm("movl %%fs,%0" : "=r" (seg));
348 if (seg != FS_TLS_SEL)
349 return 0;
350 return get_desc_base(&task->thread.tls_array[FS_TLS]);
351 }
352 case offsetof(struct user_regs_struct, gs_base): {
353 /*
354 * Exactly the same here as the %fs handling above.
355 */
356 unsigned int seg = task->thread.gsindex;
357 if (task->thread.gs != 0)
358 return task->thread.gs;
359 if (task == current)
360 asm("movl %%gs,%0" : "=r" (seg));
361 if (seg != GS_TLS_SEL)
362 return 0;
363 return get_desc_base(&task->thread.tls_array[GS_TLS]);
364 }
365#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100366 }
367
368 return *pt_regs_access(task_pt_regs(task), offset);
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100372 * This function is trivial and will be inlined by the compiler.
373 * Having it separates the implementation details of debug
374 * registers from the interface details of ptrace.
375 */
376static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
377{
Roland McGrath0f534092008-01-30 13:30:59 +0100378 switch (n) {
379 case 0: return child->thread.debugreg0;
380 case 1: return child->thread.debugreg1;
381 case 2: return child->thread.debugreg2;
382 case 3: return child->thread.debugreg3;
383 case 6: return child->thread.debugreg6;
384 case 7: return child->thread.debugreg7;
385 }
386 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100387}
388
389static int ptrace_set_debugreg(struct task_struct *child,
390 int n, unsigned long data)
391{
Roland McGrath0f534092008-01-30 13:30:59 +0100392 int i;
393
Roland McGrathd9771e82008-01-30 13:30:52 +0100394 if (unlikely(n == 4 || n == 5))
395 return -EIO;
396
Roland McGrath2047b082008-01-30 13:31:01 +0100397 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100398 return -EIO;
399
Roland McGrath0f534092008-01-30 13:30:59 +0100400 switch (n) {
401 case 0: child->thread.debugreg0 = data; break;
402 case 1: child->thread.debugreg1 = data; break;
403 case 2: child->thread.debugreg2 = data; break;
404 case 3: child->thread.debugreg3 = data; break;
405
406 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100407 if ((data & ~0xffffffffUL) != 0)
408 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100409 child->thread.debugreg6 = data;
410 break;
411
412 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100413 /*
414 * Sanity-check data. Take one half-byte at once with
415 * check = (val >> (16 + 4*i)) & 0xf. It contains the
416 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
417 * 2 and 3 are LENi. Given a list of invalid values,
418 * we do mask |= 1 << invalid_value, so that
419 * (mask >> check) & 1 is a correct test for invalid
420 * values.
421 *
422 * R/Wi contains the type of the breakpoint /
423 * watchpoint, LENi contains the length of the watched
424 * data in the watchpoint case.
425 *
426 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100427 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100428 * - R/Wi == 0x10 (break on I/O reads or writes), so
429 * mask |= 0x4444.
430 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
431 * 0x1110.
432 *
433 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
434 *
435 * See the Intel Manual "System Programming Guide",
436 * 15.2.4
437 *
438 * Note that LENi == 0x10 is defined on x86_64 in long
439 * mode (i.e. even for 32-bit userspace software, but
440 * 64-bit kernel), so the x86_64 mask value is 0x5454.
441 * See the AMD manual no. 24593 (AMD64 System Programming)
442 */
Roland McGrath2047b082008-01-30 13:31:01 +0100443#ifdef CONFIG_X86_32
444#define DR7_MASK 0x5f54
445#else
446#define DR7_MASK 0x5554
447#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100448 data &= ~DR_CONTROL_RESERVED;
449 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100450 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100451 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100452 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100453 if (data)
454 set_tsk_thread_flag(child, TIF_DEBUG);
455 else
456 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100457 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100458 }
459
Roland McGrathd9771e82008-01-30 13:30:52 +0100460 return 0;
461}
462
Markus Metzgera95d67f2008-01-30 13:31:20 +0100463static int ptrace_bts_get_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100464{
465 if (!child->thread.ds_area_msr)
466 return -ENXIO;
467
Markus Metzgera95d67f2008-01-30 13:31:20 +0100468 return ds_get_bts_index((void *)child->thread.ds_area_msr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100469}
470
Markus Metzgereee3af42008-01-30 13:31:09 +0100471static int ptrace_bts_read_record(struct task_struct *child,
472 long index,
473 struct bts_struct __user *out)
474{
475 struct bts_struct ret;
476 int retval;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100477 int bts_end;
Markus Metzgere4811f22008-01-30 13:31:20 +0100478 int bts_index;
Markus Metzgereee3af42008-01-30 13:31:09 +0100479
480 if (!child->thread.ds_area_msr)
481 return -ENXIO;
482
Markus Metzgere4811f22008-01-30 13:31:20 +0100483 if (index < 0)
484 return -EINVAL;
485
Markus Metzgera95d67f2008-01-30 13:31:20 +0100486 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
487 if (bts_end <= index)
Markus Metzgere4811f22008-01-30 13:31:20 +0100488 return -EINVAL;
489
490 /* translate the ptrace bts index into the ds bts index */
491 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
492 bts_index -= (index + 1);
493 if (bts_index < 0)
Markus Metzgera95d67f2008-01-30 13:31:20 +0100494 bts_index += bts_end;
Markus Metzgere4811f22008-01-30 13:31:20 +0100495
Markus Metzgereee3af42008-01-30 13:31:09 +0100496 retval = ds_read_bts((void *)child->thread.ds_area_msr,
Markus Metzgere4811f22008-01-30 13:31:20 +0100497 bts_index, &ret);
Markus Metzgereee3af42008-01-30 13:31:09 +0100498 if (retval)
499 return retval;
500
501 if (copy_to_user(out, &ret, sizeof(ret)))
502 return -EFAULT;
503
504 return sizeof(ret);
505}
506
507static int ptrace_bts_write_record(struct task_struct *child,
508 const struct bts_struct *in)
509{
510 int retval;
511
512 if (!child->thread.ds_area_msr)
513 return -ENXIO;
514
515 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
516 if (retval)
517 return retval;
518
519 return sizeof(*in);
520}
521
Markus Metzgera95d67f2008-01-30 13:31:20 +0100522static int ptrace_bts_clear(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100523{
Markus Metzgera95d67f2008-01-30 13:31:20 +0100524 if (!child->thread.ds_area_msr)
Markus Metzgereee3af42008-01-30 13:31:09 +0100525 return -ENXIO;
526
Markus Metzgera95d67f2008-01-30 13:31:20 +0100527 return ds_clear((void *)child->thread.ds_area_msr);
528}
529
530static int ptrace_bts_drain(struct task_struct *child,
531 struct bts_struct __user *out)
532{
533 int end, i;
534 void *ds = (void *)child->thread.ds_area_msr;
535
536 if (!ds)
537 return -ENXIO;
538
539 end = ds_get_bts_index(ds);
540 if (end <= 0)
541 return end;
542
543 for (i = 0; i < end; i++, out++) {
544 struct bts_struct ret;
545 int retval;
546
547 retval = ds_read_bts(ds, i, &ret);
548 if (retval < 0)
549 return retval;
550
551 if (copy_to_user(out, &ret, sizeof(ret)))
552 return -EFAULT;
553 }
554
555 ds_clear(ds);
556
557 return i;
558}
559
560static int ptrace_bts_config(struct task_struct *child,
561 const struct ptrace_bts_config __user *ucfg)
562{
563 struct ptrace_bts_config cfg;
564 unsigned long debugctl_mask;
565 int bts_size, ret;
566 void *ds;
567
568 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
569 return -EFAULT;
570
571 bts_size = 0;
572 ds = (void *)child->thread.ds_area_msr;
573 if (ds) {
574 bts_size = ds_get_bts_size(ds);
575 if (bts_size < 0)
576 return bts_size;
577 }
578
579 if (bts_size != cfg.size) {
580 ret = ds_free((void **)&child->thread.ds_area_msr);
581 if (ret < 0)
582 return ret;
583
584 if (cfg.size > 0)
585 ret = ds_allocate((void **)&child->thread.ds_area_msr,
586 cfg.size);
587 ds = (void *)child->thread.ds_area_msr;
588 if (ds)
589 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
590 else
591 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
592
593 if (ret < 0)
594 return ret;
595
596 bts_size = ds_get_bts_size(ds);
597 if (bts_size <= 0)
598 return bts_size;
599 }
600
601 if (ds) {
602 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
603 ret = ds_set_overflow(ds, DS_O_SIGNAL);
604 } else {
605 ret = ds_set_overflow(ds, DS_O_WRAP);
606 }
607 if (ret < 0)
608 return ret;
609 }
610
611 debugctl_mask = ds_debugctl_mask();
612 if (ds && (cfg.flags & PTRACE_BTS_O_TRACE)) {
Markus Metzgereee3af42008-01-30 13:31:09 +0100613 child->thread.debugctlmsr |= debugctl_mask;
614 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
615 } else {
616 /* there is no way for us to check whether we 'own'
617 * the respective bits in the DEBUGCTL MSR, we're
618 * about to clear */
619 child->thread.debugctlmsr &= ~debugctl_mask;
620
621 if (!child->thread.debugctlmsr)
622 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
623 }
624
Markus Metzgera95d67f2008-01-30 13:31:20 +0100625 if (ds && (cfg.flags & PTRACE_BTS_O_SCHED))
Markus Metzgereee3af42008-01-30 13:31:09 +0100626 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
627 else
628 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
629
630 return 0;
631}
632
Markus Metzgera95d67f2008-01-30 13:31:20 +0100633static int ptrace_bts_status(struct task_struct *child,
634 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100635{
Markus Metzgera95d67f2008-01-30 13:31:20 +0100636 void *ds = (void *)child->thread.ds_area_msr;
637 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100638
Markus Metzgera95d67f2008-01-30 13:31:20 +0100639 memset(&cfg, 0, sizeof(cfg));
Markus Metzgereee3af42008-01-30 13:31:09 +0100640
Markus Metzgera95d67f2008-01-30 13:31:20 +0100641 if (ds) {
642 cfg.size = ds_get_bts_size(ds);
Markus Metzgereee3af42008-01-30 13:31:09 +0100643
Markus Metzgera95d67f2008-01-30 13:31:20 +0100644 if (ds_get_overflow(ds) == DS_O_SIGNAL)
645 cfg.flags |= PTRACE_BTS_O_SIGNAL;
Markus Metzgereee3af42008-01-30 13:31:09 +0100646
Markus Metzgera95d67f2008-01-30 13:31:20 +0100647 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
648 child->thread.debugctlmsr & ds_debugctl_mask())
649 cfg.flags |= PTRACE_BTS_O_TRACE;
Markus Metzgereee3af42008-01-30 13:31:09 +0100650
Markus Metzgera95d67f2008-01-30 13:31:20 +0100651 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
652 cfg.flags |= PTRACE_BTS_O_SCHED;
Markus Metzgereee3af42008-01-30 13:31:09 +0100653 }
654
Markus Metzgera95d67f2008-01-30 13:31:20 +0100655 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
656 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100657
Markus Metzgera95d67f2008-01-30 13:31:20 +0100658 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100659}
660
661void ptrace_bts_take_timestamp(struct task_struct *tsk,
662 enum bts_qualifier qualifier)
663{
664 struct bts_struct rec = {
665 .qualifier = qualifier,
Markus Metzger3c689042008-01-30 13:31:20 +0100666 .variant.jiffies = jiffies
Markus Metzgereee3af42008-01-30 13:31:09 +0100667 };
668
Markus Metzgereee3af42008-01-30 13:31:09 +0100669 ptrace_bts_write_record(tsk, &rec);
670}
671
Roland McGrathd9771e82008-01-30 13:30:52 +0100672/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * Called by kernel/ptrace.c when detaching..
674 *
675 * Make sure the single step bit is not set.
676 */
677void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100678{
Roland McGrath7f232342008-01-30 13:30:48 +0100679 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100680#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700681 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100682#endif
Markus Metzgereee3af42008-01-30 13:31:09 +0100683 ptrace_bts_config(child, /* options = */ 0);
684 if (child->thread.ds_area_msr) {
685 ds_free((void **)&child->thread.ds_area_msr);
686 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Christoph Hellwig481bed42005-11-07 00:59:47 -0800690long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int i, ret;
693 unsigned long __user *datap = (unsigned long __user *)data;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 switch (request) {
696 /* when I and D space are separate, these will need to be fixed. */
Roland McGrath9e714be2008-01-30 13:30:58 +0100697 case PTRACE_PEEKTEXT: /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700698 case PTRACE_PEEKDATA:
699 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* read the word at location addr in the USER area. */
703 case PTRACE_PEEKUSR: {
704 unsigned long tmp;
705
706 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100707 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
708 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710
711 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100712 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100714 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
715 addr <= offsetof(struct user, u_debugreg[7])) {
716 addr -= offsetof(struct user, u_debugreg[0]);
717 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 ret = put_user(tmp, datap);
720 break;
721 }
722
723 /* when I and D space are separate, this will have to be fixed. */
724 case PTRACE_POKETEXT: /* write the word at location addr. */
725 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700726 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
728
729 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
730 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100731 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
732 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734
Roland McGrathe9c86c72008-01-30 13:31:01 +0100735 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100737 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
738 addr <= offsetof(struct user, u_debugreg[7])) {
739 addr -= offsetof(struct user, u_debugreg[0]);
740 ret = ptrace_set_debugreg(child,
741 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100746 if (!access_ok(VERIFY_WRITE, datap, sizeof(struct user_regs_struct))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ret = -EIO;
748 break;
749 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100750 for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 __put_user(getreg(child, i), datap);
752 datap++;
753 }
754 ret = 0;
755 break;
756 }
757
758 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
759 unsigned long tmp;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100760 if (!access_ok(VERIFY_READ, datap, sizeof(struct user_regs_struct))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 ret = -EIO;
762 break;
763 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100764 for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 __get_user(tmp, datap);
766 putreg(child, i, tmp);
767 datap++;
768 }
769 ret = 0;
770 break;
771 }
772
773 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
774 if (!access_ok(VERIFY_WRITE, datap,
775 sizeof(struct user_i387_struct))) {
776 ret = -EIO;
777 break;
778 }
779 ret = 0;
780 if (!tsk_used_math(child))
781 init_fpu(child);
782 get_fpregs((struct user_i387_struct __user *)data, child);
783 break;
784 }
785
786 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
787 if (!access_ok(VERIFY_READ, datap,
788 sizeof(struct user_i387_struct))) {
789 ret = -EIO;
790 break;
791 }
792 set_stopped_child_used_math(child);
793 set_fpregs(child, (struct user_i387_struct __user *)data);
794 ret = 0;
795 break;
796 }
797
Roland McGrathe9c86c72008-01-30 13:31:01 +0100798#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
800 if (!access_ok(VERIFY_WRITE, datap,
801 sizeof(struct user_fxsr_struct))) {
802 ret = -EIO;
803 break;
804 }
805 if (!tsk_used_math(child))
806 init_fpu(child);
807 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
808 break;
809 }
810
811 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
812 if (!access_ok(VERIFY_READ, datap,
813 sizeof(struct user_fxsr_struct))) {
814 ret = -EIO;
815 break;
816 }
817 set_stopped_child_used_math(child);
818 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
819 break;
820 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Roland McGrathe9c86c72008-01-30 13:31:01 +0100823#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100825 if (addr < 0)
826 return -EIO;
827 ret = do_get_thread_area(child, addr,
828 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
830
831 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100832 if (addr < 0)
833 return -EIO;
834 ret = do_set_thread_area(child, addr,
835 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100837#endif
838
839#ifdef CONFIG_X86_64
840 /* normal 64bit interface to access TLS data.
841 Works just like arch_prctl, except that the arguments
842 are reversed. */
843 case PTRACE_ARCH_PRCTL:
844 ret = do_arch_prctl(child, data, addr);
845 break;
846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Markus Metzgereee3af42008-01-30 13:31:09 +0100848 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100849 ret = ptrace_bts_config
850 (child, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100851 break;
852
853 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100854 ret = ptrace_bts_status
855 (child, (struct ptrace_bts_config __user *)addr);
856 break;
857
858 case PTRACE_BTS_SIZE:
859 ret = ptrace_bts_get_size(child);
860 break;
861
862 case PTRACE_BTS_GET:
863 ret = ptrace_bts_read_record
864 (child, data, (struct bts_struct __user *) addr);
865 break;
866
867 case PTRACE_BTS_CLEAR:
868 ret = ptrace_bts_clear(child);
869 break;
870
871 case PTRACE_BTS_DRAIN:
872 ret = ptrace_bts_drain
873 (child, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100874 break;
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 default:
877 ret = ptrace_request(child, request, addr, data);
878 break;
879 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return ret;
882}
883
Roland McGrathcb757c42008-01-30 13:31:01 +0100884#ifdef CONFIG_IA32_EMULATION
885
Roland McGrath099cd6e2008-01-30 13:31:01 +0100886#include <linux/compat.h>
887#include <linux/syscalls.h>
888#include <asm/ia32.h>
889#include <asm/fpu32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +0100890#include <asm/user32.h>
891
892#define R32(l,q) \
893 case offsetof(struct user32, regs.l): \
894 regs->q = value; break
895
896#define SEG32(rs) \
897 case offsetof(struct user32, regs.rs): \
898 return set_segment_reg(child, \
899 offsetof(struct user_regs_struct, rs), \
900 value); \
901 break
902
903static int putreg32(struct task_struct *child, unsigned regno, u32 value)
904{
905 struct pt_regs *regs = task_pt_regs(child);
906
907 switch (regno) {
908
909 SEG32(cs);
910 SEG32(ds);
911 SEG32(es);
912 SEG32(fs);
913 SEG32(gs);
914 SEG32(ss);
915
916 R32(ebx, bx);
917 R32(ecx, cx);
918 R32(edx, dx);
919 R32(edi, di);
920 R32(esi, si);
921 R32(ebp, bp);
922 R32(eax, ax);
923 R32(orig_eax, orig_ax);
924 R32(eip, ip);
925 R32(esp, sp);
926
927 case offsetof(struct user32, regs.eflags):
928 return set_flags(child, value);
929
930 case offsetof(struct user32, u_debugreg[0]) ...
931 offsetof(struct user32, u_debugreg[7]):
932 regno -= offsetof(struct user32, u_debugreg[0]);
933 return ptrace_set_debugreg(child, regno / 4, value);
934
935 default:
936 if (regno > sizeof(struct user32) || (regno & 3))
937 return -EIO;
938
939 /*
940 * Other dummy fields in the virtual user structure
941 * are ignored
942 */
943 break;
944 }
945 return 0;
946}
947
948#undef R32
949#undef SEG32
950
951#define R32(l,q) \
952 case offsetof(struct user32, regs.l): \
953 *val = regs->q; break
954
955#define SEG32(rs) \
956 case offsetof(struct user32, regs.rs): \
957 *val = get_segment_reg(child, \
958 offsetof(struct user_regs_struct, rs)); \
959 break
960
961static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
962{
963 struct pt_regs *regs = task_pt_regs(child);
964
965 switch (regno) {
966
967 SEG32(ds);
968 SEG32(es);
969 SEG32(fs);
970 SEG32(gs);
971
972 R32(cs, cs);
973 R32(ss, ss);
974 R32(ebx, bx);
975 R32(ecx, cx);
976 R32(edx, dx);
977 R32(edi, di);
978 R32(esi, si);
979 R32(ebp, bp);
980 R32(eax, ax);
981 R32(orig_eax, orig_ax);
982 R32(eip, ip);
983 R32(esp, sp);
984
985 case offsetof(struct user32, regs.eflags):
986 *val = get_flags(child);
987 break;
988
989 case offsetof(struct user32, u_debugreg[0]) ...
990 offsetof(struct user32, u_debugreg[7]):
991 regno -= offsetof(struct user32, u_debugreg[0]);
992 *val = ptrace_get_debugreg(child, regno / 4);
993 break;
994
995 default:
996 if (regno > sizeof(struct user32) || (regno & 3))
997 return -EIO;
998
999 /*
1000 * Other dummy fields in the virtual user structure
1001 * are ignored
1002 */
1003 *val = 0;
1004 break;
1005 }
1006 return 0;
1007}
1008
1009#undef R32
1010#undef SEG32
1011
Roland McGrath099cd6e2008-01-30 13:31:01 +01001012static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
1013{
1014 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
1015 compat_siginfo_t __user *si32 = compat_ptr(data);
1016 siginfo_t ssi;
1017 int ret;
1018
1019 if (request == PTRACE_SETSIGINFO) {
1020 memset(&ssi, 0, sizeof(siginfo_t));
1021 ret = copy_siginfo_from_user32(&ssi, si32);
1022 if (ret)
1023 return ret;
1024 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
1025 return -EFAULT;
1026 }
1027 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
1028 if (ret)
1029 return ret;
1030 if (request == PTRACE_GETSIGINFO) {
1031 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
1032 return -EFAULT;
1033 ret = copy_siginfo_to_user32(si32, &ssi);
1034 }
1035 return ret;
1036}
1037
1038asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
1039{
1040 struct task_struct *child;
1041 struct pt_regs *childregs;
1042 void __user *datap = compat_ptr(data);
1043 int ret;
1044 __u32 val;
1045
1046 switch (request) {
1047 case PTRACE_TRACEME:
1048 case PTRACE_ATTACH:
1049 case PTRACE_KILL:
1050 case PTRACE_CONT:
1051 case PTRACE_SINGLESTEP:
1052 case PTRACE_SINGLEBLOCK:
1053 case PTRACE_DETACH:
1054 case PTRACE_SYSCALL:
1055 case PTRACE_OLDSETOPTIONS:
1056 case PTRACE_SETOPTIONS:
1057 case PTRACE_SET_THREAD_AREA:
1058 case PTRACE_GET_THREAD_AREA:
Markus Metzgereee3af42008-01-30 13:31:09 +01001059 case PTRACE_BTS_CONFIG:
1060 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001061 case PTRACE_BTS_SIZE:
1062 case PTRACE_BTS_GET:
1063 case PTRACE_BTS_CLEAR:
1064 case PTRACE_BTS_DRAIN:
Roland McGrath099cd6e2008-01-30 13:31:01 +01001065 return sys_ptrace(request, pid, addr, data);
1066
1067 default:
1068 return -EINVAL;
1069
1070 case PTRACE_PEEKTEXT:
1071 case PTRACE_PEEKDATA:
1072 case PTRACE_POKEDATA:
1073 case PTRACE_POKETEXT:
1074 case PTRACE_POKEUSR:
1075 case PTRACE_PEEKUSR:
1076 case PTRACE_GETREGS:
1077 case PTRACE_SETREGS:
1078 case PTRACE_SETFPREGS:
1079 case PTRACE_GETFPREGS:
1080 case PTRACE_SETFPXREGS:
1081 case PTRACE_GETFPXREGS:
1082 case PTRACE_GETEVENTMSG:
1083 break;
1084
1085 case PTRACE_SETSIGINFO:
1086 case PTRACE_GETSIGINFO:
1087 return ptrace32_siginfo(request, pid, addr, data);
1088 }
1089
1090 child = ptrace_get_task_struct(pid);
1091 if (IS_ERR(child))
1092 return PTR_ERR(child);
1093
1094 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1095 if (ret < 0)
1096 goto out;
1097
1098 childregs = task_pt_regs(child);
1099
1100 switch (request) {
1101 case PTRACE_PEEKDATA:
1102 case PTRACE_PEEKTEXT:
1103 ret = 0;
1104 if (access_process_vm(child, addr, &val, sizeof(u32), 0) !=
1105 sizeof(u32))
1106 ret = -EIO;
1107 else
1108 ret = put_user(val, (unsigned int __user *)datap);
1109 break;
1110
1111 case PTRACE_POKEDATA:
1112 case PTRACE_POKETEXT:
1113 ret = 0;
1114 if (access_process_vm(child, addr, &data, sizeof(u32), 1) !=
1115 sizeof(u32))
1116 ret = -EIO;
1117 break;
1118
1119 case PTRACE_PEEKUSR:
1120 ret = getreg32(child, addr, &val);
1121 if (ret == 0)
1122 ret = put_user(val, (__u32 __user *)datap);
1123 break;
1124
1125 case PTRACE_POKEUSR:
1126 ret = putreg32(child, addr, data);
1127 break;
1128
1129 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
1130 int i;
1131
1132 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
1133 ret = -EIO;
1134 break;
1135 }
1136 ret = 0;
1137 for (i = 0; i < sizeof(struct user_regs_struct32); i += sizeof(__u32)) {
1138 getreg32(child, i, &val);
1139 ret |= __put_user(val, (u32 __user *)datap);
1140 datap += sizeof(u32);
1141 }
1142 break;
1143 }
1144
1145 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
1146 unsigned long tmp;
1147 int i;
1148
1149 if (!access_ok(VERIFY_READ, datap, 16*4)) {
1150 ret = -EIO;
1151 break;
1152 }
1153 ret = 0;
1154 for (i = 0; i < sizeof(struct user_regs_struct32); i += sizeof(u32)) {
1155 ret |= __get_user(tmp, (u32 __user *)datap);
1156 putreg32(child, i, tmp);
1157 datap += sizeof(u32);
1158 }
1159 break;
1160 }
1161
1162 case PTRACE_GETFPREGS:
1163 ret = -EIO;
1164 if (!access_ok(VERIFY_READ, compat_ptr(data),
1165 sizeof(struct user_i387_struct)))
1166 break;
1167 save_i387_ia32(child, datap, childregs, 1);
1168 ret = 0;
1169 break;
1170
1171 case PTRACE_SETFPREGS:
1172 ret = -EIO;
1173 if (!access_ok(VERIFY_WRITE, datap,
1174 sizeof(struct user_i387_struct)))
1175 break;
1176 ret = 0;
1177 /* don't check EFAULT to be bug-to-bug compatible to i386 */
1178 restore_i387_ia32(child, datap, 1);
1179 break;
1180
1181 case PTRACE_GETFPXREGS: {
1182 struct user32_fxsr_struct __user *u = datap;
1183
1184 init_fpu(child);
1185 ret = -EIO;
1186 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
1187 break;
1188 ret = -EFAULT;
1189 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
1190 break;
1191 ret = __put_user(childregs->cs, &u->fcs);
1192 ret |= __put_user(child->thread.ds, &u->fos);
1193 break;
1194 }
1195 case PTRACE_SETFPXREGS: {
1196 struct user32_fxsr_struct __user *u = datap;
1197
1198 unlazy_fpu(child);
1199 ret = -EIO;
1200 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
1201 break;
1202 /*
1203 * no checking to be bug-to-bug compatible with i386.
1204 * but silence warning
1205 */
1206 if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
1207 ;
1208 set_stopped_child_used_math(child);
1209 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
1210 ret = 0;
1211 break;
1212 }
1213
1214 case PTRACE_GETEVENTMSG:
1215 ret = put_user(child->ptrace_message,
1216 (unsigned int __user *)compat_ptr(data));
1217 break;
1218
1219 default:
1220 BUG();
1221 }
1222
1223 out:
1224 put_task_struct(child);
1225 return ret;
1226}
1227
Roland McGrathcb757c42008-01-30 13:31:01 +01001228#endif /* CONFIG_IA32_EMULATION */
1229
Roland McGrath86976cd2008-01-30 13:31:01 +01001230#ifdef CONFIG_X86_32
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
1233{
1234 struct siginfo info;
1235
1236 tsk->thread.trap_no = 1;
1237 tsk->thread.error_code = error_code;
1238
1239 memset(&info, 0, sizeof(info));
1240 info.si_signo = SIGTRAP;
1241 info.si_code = TRAP_BRKPT;
1242
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001243 /* User-mode ip? */
1244 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Simon Arlott27b46d72007-10-20 01:13:56 +02001246 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 force_sig_info(SIGTRAP, &info, tsk);
1248}
1249
1250/* notification of system call entry/exit
1251 * - triggered by current->work.syscall_trace
1252 */
1253__attribute__((regparm(3)))
Laurent Viviered75e8d2005-09-03 15:57:18 -07001254int do_syscall_trace(struct pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001256 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
1257 /*
1258 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1259 * interception
1260 */
Bodo Stroesser1b38f002005-09-03 15:57:20 -07001261 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001262 int ret = 0;
Bodo Stroesser1b38f002005-09-03 15:57:20 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* do the secure computing check first */
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001265 if (!entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001266 secure_computing(regs->orig_ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Bodo Stroesserab1c23c2005-09-03 15:57:21 -07001268 if (unlikely(current->audit_context)) {
1269 if (entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001270 audit_syscall_exit(AUDITSC_RESULT(regs->ax),
1271 regs->ax);
Bodo Stroesserab1c23c2005-09-03 15:57:21 -07001272 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1273 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1274 * not used, entry.S will call us only on syscall exit, not
1275 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1276 * calling send_sigtrap() on syscall entry.
1277 *
1278 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1279 * is_singlestep is false, despite his name, so we will still do
1280 * the correct thing.
1281 */
1282 else if (is_singlestep)
1283 goto out;
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (!(current->ptrace & PT_PTRACED))
2fd6f582005-04-29 16:08:28 +01001287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Bodo Stroesser1b38f002005-09-03 15:57:20 -07001289 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1290 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1291 * here. We have to check this and return */
1292 if (is_sysemu && entryexit)
1293 return 0;
Laurent Viviered75e8d2005-09-03 15:57:18 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* Fake a debug trap */
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001296 if (is_singlestep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 send_sigtrap(current, regs, 0);
1298
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001299 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
2fd6f582005-04-29 16:08:28 +01001300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* the 0x80 provides a way for the tracing parent to distinguish
1303 between a syscall stop and SIGTRAP delivery */
Laurent Viviered75e8d2005-09-03 15:57:18 -07001304 /* Note that the debugger could change the result of test_thread_flag!*/
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001305 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /*
1308 * this isn't the same as continuing with a signal, but it will do
1309 * for normal use. strace only continues with a signal if the
1310 * stopping signal is not SIGTRAP. -brl
1311 */
1312 if (current->exit_code) {
1313 send_sig(current->exit_code, current, 1);
1314 current->exit_code = 0;
1315 }
Laurent Viviered75e8d2005-09-03 15:57:18 -07001316 ret = is_sysemu;
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001317out:
2fd6f582005-04-29 16:08:28 +01001318 if (unlikely(current->audit_context) && !entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001319 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
1320 regs->bx, regs->cx, regs->dx, regs->si);
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001321 if (ret == 0)
1322 return 0;
1323
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001324 regs->orig_ax = -1; /* force skip of syscall restarting */
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001325 if (unlikely(current->audit_context))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001326 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001327 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
Roland McGrath86976cd2008-01-30 13:31:01 +01001329
1330#else /* CONFIG_X86_64 */
1331
1332static void syscall_trace(struct pt_regs *regs)
1333{
1334
1335#if 0
1336 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1337 current->comm,
1338 regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
1339 current_thread_info()->flags, current->ptrace);
1340#endif
1341
1342 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1343 ? 0x80 : 0));
1344 /*
1345 * this isn't the same as continuing with a signal, but it will do
1346 * for normal use. strace only continues with a signal if the
1347 * stopping signal is not SIGTRAP. -brl
1348 */
1349 if (current->exit_code) {
1350 send_sig(current->exit_code, current, 1);
1351 current->exit_code = 0;
1352 }
1353}
1354
1355asmlinkage void syscall_trace_enter(struct pt_regs *regs)
1356{
1357 /* do the secure computing check first */
1358 secure_computing(regs->orig_ax);
1359
1360 if (test_thread_flag(TIF_SYSCALL_TRACE)
1361 && (current->ptrace & PT_PTRACED))
1362 syscall_trace(regs);
1363
1364 if (unlikely(current->audit_context)) {
1365 if (test_thread_flag(TIF_IA32)) {
1366 audit_syscall_entry(AUDIT_ARCH_I386,
1367 regs->orig_ax,
1368 regs->bx, regs->cx,
1369 regs->dx, regs->si);
1370 } else {
1371 audit_syscall_entry(AUDIT_ARCH_X86_64,
1372 regs->orig_ax,
1373 regs->di, regs->si,
1374 regs->dx, regs->r10);
1375 }
1376 }
1377}
1378
1379asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1380{
1381 if (unlikely(current->audit_context))
1382 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1383
1384 if ((test_thread_flag(TIF_SYSCALL_TRACE)
1385 || test_thread_flag(TIF_SINGLESTEP))
1386 && (current->ptrace & PT_PTRACED))
1387 syscall_trace(regs);
1388}
1389
1390#endif /* CONFIG_X86_32 */