blob: e19a91db9b351d3df8c3729390afb35e807040b5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/user.h>
Roland McGrath070459d2008-01-30 13:31:53 +010018#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/security.h>
20#include <linux/audit.h>
21#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070022#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/uaccess.h>
25#include <asm/pgtable.h>
26#include <asm/system.h>
27#include <asm/processor.h>
28#include <asm/i387.h>
29#include <asm/debugreg.h>
30#include <asm/ldt.h>
31#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010032#include <asm/prctl.h>
33#include <asm/proto.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010034#include <asm/ds.h>
35
Roland McGrath070459d2008-01-30 13:31:53 +010036#include "tls.h"
37
38enum x86_regset {
39 REGSET_GENERAL,
40 REGSET_FP,
41 REGSET_XFP,
42 REGSET_TLS,
43};
Markus Metzgereee3af42008-01-30 13:31:09 +010044
45/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * does not yet catch signals sent when the child dies.
47 * in exit.c or in signal.c.
48 */
49
Chuck Ebbert9f155b92006-01-05 23:11:29 -050050/*
51 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -050052 */
Roland McGrathe39c2892008-01-30 13:31:01 +010053#define FLAG_MASK_32 ((unsigned long) \
54 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
55 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
56 X86_EFLAGS_SF | X86_EFLAGS_TF | \
57 X86_EFLAGS_DF | X86_EFLAGS_OF | \
58 X86_EFLAGS_RF | X86_EFLAGS_AC))
59
Roland McGrath2047b082008-01-30 13:31:01 +010060/*
61 * Determines whether a value may be installed in a segment register.
62 */
63static inline bool invalid_selector(u16 value)
64{
65 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
66}
67
68#ifdef CONFIG_X86_32
69
Roland McGrathe39c2892008-01-30 13:31:01 +010070#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Roland McGrath62a97d42008-01-30 13:30:52 +010072static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010074 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Roland McGrath06ee1b62008-01-30 13:31:01 +010075 regno >>= 2;
Roland McGrath62a97d42008-01-30 13:30:52 +010076 if (regno > FS)
77 --regno;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010078 return &regs->bx + regno;
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 {
90 retval = task->thread.gs;
91 if (task == current)
92 savesegment(gs, retval);
93 }
94 return retval;
95}
96
97static int set_segment_reg(struct task_struct *task,
98 unsigned long offset, u16 value)
99{
100 /*
101 * The value argument was already truncated to 16 bits.
102 */
Roland McGrath2047b082008-01-30 13:31:01 +0100103 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100104 return -EIO;
105
106 if (offset != offsetof(struct user_regs_struct, gs))
107 *pt_regs_access(task_pt_regs(task), offset) = value;
108 else {
109 task->thread.gs = value;
110 if (task == current)
Roland McGrath5fd4d162008-01-30 13:30:58 +0100111 /*
112 * The user-mode %gs is not affected by
113 * kernel entry, so we must update the CPU.
114 */
115 loadsegment(gs, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119}
120
Roland McGrath2047b082008-01-30 13:31:01 +0100121static unsigned long debugreg_addr_limit(struct task_struct *task)
122{
123 return TASK_SIZE - 3;
124}
125
126#else /* CONFIG_X86_64 */
127
128#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
129
130static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
131{
132 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
133 return &regs->r15 + (offset / sizeof(regs->r15));
134}
135
136static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
137{
138 /*
139 * Returning the value truncates it to 16 bits.
140 */
141 unsigned int seg;
142
143 switch (offset) {
144 case offsetof(struct user_regs_struct, fs):
145 if (task == current) {
146 /* Older gas can't assemble movq %?s,%r?? */
147 asm("movl %%fs,%0" : "=r" (seg));
148 return seg;
149 }
150 return task->thread.fsindex;
151 case offsetof(struct user_regs_struct, gs):
152 if (task == current) {
153 asm("movl %%gs,%0" : "=r" (seg));
154 return seg;
155 }
156 return task->thread.gsindex;
157 case offsetof(struct user_regs_struct, ds):
158 if (task == current) {
159 asm("movl %%ds,%0" : "=r" (seg));
160 return seg;
161 }
162 return task->thread.ds;
163 case offsetof(struct user_regs_struct, es):
164 if (task == current) {
165 asm("movl %%es,%0" : "=r" (seg));
166 return seg;
167 }
168 return task->thread.es;
169
170 case offsetof(struct user_regs_struct, cs):
171 case offsetof(struct user_regs_struct, ss):
172 break;
173 }
174 return *pt_regs_access(task_pt_regs(task), offset);
175}
176
177static int set_segment_reg(struct task_struct *task,
178 unsigned long offset, u16 value)
179{
180 /*
181 * The value argument was already truncated to 16 bits.
182 */
183 if (invalid_selector(value))
184 return -EIO;
185
186 switch (offset) {
187 case offsetof(struct user_regs_struct,fs):
188 /*
189 * If this is setting fs as for normal 64-bit use but
190 * setting fs_base has implicitly changed it, leave it.
191 */
192 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
193 task->thread.fs != 0) ||
194 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
195 task->thread.fs == 0))
196 break;
197 task->thread.fsindex = value;
198 if (task == current)
199 loadsegment(fs, task->thread.fsindex);
200 break;
201 case offsetof(struct user_regs_struct,gs):
202 /*
203 * If this is setting gs as for normal 64-bit use but
204 * setting gs_base has implicitly changed it, leave it.
205 */
206 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
207 task->thread.gs != 0) ||
208 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
209 task->thread.gs == 0))
210 break;
211 task->thread.gsindex = value;
212 if (task == current)
213 load_gs_index(task->thread.gsindex);
214 break;
215 case offsetof(struct user_regs_struct,ds):
216 task->thread.ds = value;
217 if (task == current)
218 loadsegment(ds, task->thread.ds);
219 break;
220 case offsetof(struct user_regs_struct,es):
221 task->thread.es = value;
222 if (task == current)
223 loadsegment(es, task->thread.es);
224 break;
225
226 /*
227 * Can't actually change these in 64-bit mode.
228 */
229 case offsetof(struct user_regs_struct,cs):
230#ifdef CONFIG_IA32_EMULATION
231 if (test_tsk_thread_flag(task, TIF_IA32))
232 task_pt_regs(task)->cs = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100233#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100234 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100235 case offsetof(struct user_regs_struct,ss):
236#ifdef CONFIG_IA32_EMULATION
237 if (test_tsk_thread_flag(task, TIF_IA32))
238 task_pt_regs(task)->ss = value;
Roland McGrath2047b082008-01-30 13:31:01 +0100239#endif
Roland McGrathcb757c42008-01-30 13:31:01 +0100240 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100241 }
242
243 return 0;
244}
245
246static unsigned long debugreg_addr_limit(struct task_struct *task)
247{
248#ifdef CONFIG_IA32_EMULATION
249 if (test_tsk_thread_flag(task, TIF_IA32))
250 return IA32_PAGE_OFFSET - 3;
251#endif
252 return TASK_SIZE64 - 7;
253}
254
255#endif /* CONFIG_X86_32 */
256
Roland McGrath06ee1b62008-01-30 13:31:01 +0100257static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100259 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Roland McGrath06ee1b62008-01-30 13:31:01 +0100261 /*
262 * If the debugger set TF, hide it from the readout.
263 */
264 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
265 retval &= ~X86_EFLAGS_TF;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return retval;
268}
269
Roland McGrath06ee1b62008-01-30 13:31:01 +0100270static int set_flags(struct task_struct *task, unsigned long value)
271{
272 struct pt_regs *regs = task_pt_regs(task);
273
274 /*
275 * If the user value contains TF, mark that
276 * it was not "us" (the debugger) that set it.
277 * If not, make sure it stays set if we had.
278 */
279 if (value & X86_EFLAGS_TF)
280 clear_tsk_thread_flag(task, TIF_FORCED_TF);
281 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
282 value |= X86_EFLAGS_TF;
283
284 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
285
286 return 0;
287}
288
289static int putreg(struct task_struct *child,
290 unsigned long offset, unsigned long value)
291{
292 switch (offset) {
293 case offsetof(struct user_regs_struct, cs):
294 case offsetof(struct user_regs_struct, ds):
295 case offsetof(struct user_regs_struct, es):
296 case offsetof(struct user_regs_struct, fs):
297 case offsetof(struct user_regs_struct, gs):
298 case offsetof(struct user_regs_struct, ss):
299 return set_segment_reg(child, offset, value);
300
301 case offsetof(struct user_regs_struct, flags):
302 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100303
304#ifdef CONFIG_X86_64
305 case offsetof(struct user_regs_struct,fs_base):
306 if (value >= TASK_SIZE_OF(child))
307 return -EIO;
308 /*
309 * When changing the segment base, use do_arch_prctl
310 * to set either thread.fs or thread.fsindex and the
311 * corresponding GDT slot.
312 */
313 if (child->thread.fs != value)
314 return do_arch_prctl(child, ARCH_SET_FS, value);
315 return 0;
316 case offsetof(struct user_regs_struct,gs_base):
317 /*
318 * Exactly the same here as the %fs handling above.
319 */
320 if (value >= TASK_SIZE_OF(child))
321 return -EIO;
322 if (child->thread.gs != value)
323 return do_arch_prctl(child, ARCH_SET_GS, value);
324 return 0;
325#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100326 }
327
328 *pt_regs_access(task_pt_regs(child), offset) = value;
329 return 0;
330}
331
332static unsigned long getreg(struct task_struct *task, unsigned long offset)
333{
334 switch (offset) {
335 case offsetof(struct user_regs_struct, cs):
336 case offsetof(struct user_regs_struct, ds):
337 case offsetof(struct user_regs_struct, es):
338 case offsetof(struct user_regs_struct, fs):
339 case offsetof(struct user_regs_struct, gs):
340 case offsetof(struct user_regs_struct, ss):
341 return get_segment_reg(task, offset);
342
343 case offsetof(struct user_regs_struct, flags):
344 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100345
346#ifdef CONFIG_X86_64
347 case offsetof(struct user_regs_struct, fs_base): {
348 /*
349 * do_arch_prctl may have used a GDT slot instead of
350 * the MSR. To userland, it appears the same either
351 * way, except the %fs segment selector might not be 0.
352 */
353 unsigned int seg = task->thread.fsindex;
354 if (task->thread.fs != 0)
355 return task->thread.fs;
356 if (task == current)
357 asm("movl %%fs,%0" : "=r" (seg));
358 if (seg != FS_TLS_SEL)
359 return 0;
360 return get_desc_base(&task->thread.tls_array[FS_TLS]);
361 }
362 case offsetof(struct user_regs_struct, gs_base): {
363 /*
364 * Exactly the same here as the %fs handling above.
365 */
366 unsigned int seg = task->thread.gsindex;
367 if (task->thread.gs != 0)
368 return task->thread.gs;
369 if (task == current)
370 asm("movl %%gs,%0" : "=r" (seg));
371 if (seg != GS_TLS_SEL)
372 return 0;
373 return get_desc_base(&task->thread.tls_array[GS_TLS]);
374 }
375#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100376 }
377
378 return *pt_regs_access(task_pt_regs(task), offset);
379}
380
Roland McGrath91e7b702008-01-30 13:31:52 +0100381static int genregs_get(struct task_struct *target,
382 const struct user_regset *regset,
383 unsigned int pos, unsigned int count,
384 void *kbuf, void __user *ubuf)
385{
386 if (kbuf) {
387 unsigned long *k = kbuf;
388 while (count > 0) {
389 *k++ = getreg(target, pos);
390 count -= sizeof(*k);
391 pos += sizeof(*k);
392 }
393 } else {
394 unsigned long __user *u = ubuf;
395 while (count > 0) {
396 if (__put_user(getreg(target, pos), u++))
397 return -EFAULT;
398 count -= sizeof(*u);
399 pos += sizeof(*u);
400 }
401 }
402
403 return 0;
404}
405
406static int genregs_set(struct task_struct *target,
407 const struct user_regset *regset,
408 unsigned int pos, unsigned int count,
409 const void *kbuf, const void __user *ubuf)
410{
411 int ret = 0;
412 if (kbuf) {
413 const unsigned long *k = kbuf;
414 while (count > 0 && !ret) {
415 ret = putreg(target, pos, *k++);
416 count -= sizeof(*k);
417 pos += sizeof(*k);
418 }
419 } else {
420 const unsigned long __user *u = ubuf;
421 while (count > 0 && !ret) {
422 unsigned long word;
423 ret = __get_user(word, u++);
424 if (ret)
425 break;
426 ret = putreg(target, pos, word);
427 count -= sizeof(*u);
428 pos += sizeof(*u);
429 }
430 }
431 return ret;
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
Roland McGrathd9771e82008-01-30 13:30:52 +0100435 * This function is trivial and will be inlined by the compiler.
436 * Having it separates the implementation details of debug
437 * registers from the interface details of ptrace.
438 */
439static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
440{
Roland McGrath0f534092008-01-30 13:30:59 +0100441 switch (n) {
442 case 0: return child->thread.debugreg0;
443 case 1: return child->thread.debugreg1;
444 case 2: return child->thread.debugreg2;
445 case 3: return child->thread.debugreg3;
446 case 6: return child->thread.debugreg6;
447 case 7: return child->thread.debugreg7;
448 }
449 return 0;
Roland McGrathd9771e82008-01-30 13:30:52 +0100450}
451
452static int ptrace_set_debugreg(struct task_struct *child,
453 int n, unsigned long data)
454{
Roland McGrath0f534092008-01-30 13:30:59 +0100455 int i;
456
Roland McGrathd9771e82008-01-30 13:30:52 +0100457 if (unlikely(n == 4 || n == 5))
458 return -EIO;
459
Roland McGrath2047b082008-01-30 13:31:01 +0100460 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
Roland McGrathd9771e82008-01-30 13:30:52 +0100461 return -EIO;
462
Roland McGrath0f534092008-01-30 13:30:59 +0100463 switch (n) {
464 case 0: child->thread.debugreg0 = data; break;
465 case 1: child->thread.debugreg1 = data; break;
466 case 2: child->thread.debugreg2 = data; break;
467 case 3: child->thread.debugreg3 = data; break;
468
469 case 6:
Roland McGrath2047b082008-01-30 13:31:01 +0100470 if ((data & ~0xffffffffUL) != 0)
471 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100472 child->thread.debugreg6 = data;
473 break;
474
475 case 7:
Roland McGrathd9771e82008-01-30 13:30:52 +0100476 /*
477 * Sanity-check data. Take one half-byte at once with
478 * check = (val >> (16 + 4*i)) & 0xf. It contains the
479 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
480 * 2 and 3 are LENi. Given a list of invalid values,
481 * we do mask |= 1 << invalid_value, so that
482 * (mask >> check) & 1 is a correct test for invalid
483 * values.
484 *
485 * R/Wi contains the type of the breakpoint /
486 * watchpoint, LENi contains the length of the watched
487 * data in the watchpoint case.
488 *
489 * The invalid values are:
Roland McGrath2047b082008-01-30 13:31:01 +0100490 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
Roland McGrathd9771e82008-01-30 13:30:52 +0100491 * - R/Wi == 0x10 (break on I/O reads or writes), so
492 * mask |= 0x4444.
493 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
494 * 0x1110.
495 *
496 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
497 *
498 * See the Intel Manual "System Programming Guide",
499 * 15.2.4
500 *
501 * Note that LENi == 0x10 is defined on x86_64 in long
502 * mode (i.e. even for 32-bit userspace software, but
503 * 64-bit kernel), so the x86_64 mask value is 0x5454.
504 * See the AMD manual no. 24593 (AMD64 System Programming)
505 */
Roland McGrath2047b082008-01-30 13:31:01 +0100506#ifdef CONFIG_X86_32
507#define DR7_MASK 0x5f54
508#else
509#define DR7_MASK 0x5554
510#endif
Roland McGrathd9771e82008-01-30 13:30:52 +0100511 data &= ~DR_CONTROL_RESERVED;
512 for (i = 0; i < 4; i++)
Roland McGrath2047b082008-01-30 13:31:01 +0100513 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
Roland McGrathd9771e82008-01-30 13:30:52 +0100514 return -EIO;
Roland McGrath0f534092008-01-30 13:30:59 +0100515 child->thread.debugreg7 = data;
Roland McGrathd9771e82008-01-30 13:30:52 +0100516 if (data)
517 set_tsk_thread_flag(child, TIF_DEBUG);
518 else
519 clear_tsk_thread_flag(child, TIF_DEBUG);
Roland McGrath0f534092008-01-30 13:30:59 +0100520 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100521 }
522
Roland McGrathd9771e82008-01-30 13:30:52 +0100523 return 0;
524}
525
Markus Metzgera95d67f2008-01-30 13:31:20 +0100526static int ptrace_bts_get_size(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100527{
528 if (!child->thread.ds_area_msr)
529 return -ENXIO;
530
Markus Metzgera95d67f2008-01-30 13:31:20 +0100531 return ds_get_bts_index((void *)child->thread.ds_area_msr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100532}
533
Markus Metzgereee3af42008-01-30 13:31:09 +0100534static int ptrace_bts_read_record(struct task_struct *child,
535 long index,
536 struct bts_struct __user *out)
537{
538 struct bts_struct ret;
539 int retval;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100540 int bts_end;
Markus Metzgere4811f22008-01-30 13:31:20 +0100541 int bts_index;
Markus Metzgereee3af42008-01-30 13:31:09 +0100542
543 if (!child->thread.ds_area_msr)
544 return -ENXIO;
545
Markus Metzgere4811f22008-01-30 13:31:20 +0100546 if (index < 0)
547 return -EINVAL;
548
Markus Metzgera95d67f2008-01-30 13:31:20 +0100549 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
550 if (bts_end <= index)
Markus Metzgere4811f22008-01-30 13:31:20 +0100551 return -EINVAL;
552
553 /* translate the ptrace bts index into the ds bts index */
554 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
555 bts_index -= (index + 1);
556 if (bts_index < 0)
Markus Metzgera95d67f2008-01-30 13:31:20 +0100557 bts_index += bts_end;
Markus Metzgere4811f22008-01-30 13:31:20 +0100558
Markus Metzgereee3af42008-01-30 13:31:09 +0100559 retval = ds_read_bts((void *)child->thread.ds_area_msr,
Markus Metzgere4811f22008-01-30 13:31:20 +0100560 bts_index, &ret);
Markus Metzgere6ae5d92008-01-30 13:32:03 +0100561 if (retval < 0)
Markus Metzgereee3af42008-01-30 13:31:09 +0100562 return retval;
563
564 if (copy_to_user(out, &ret, sizeof(ret)))
565 return -EFAULT;
566
567 return sizeof(ret);
568}
569
570static int ptrace_bts_write_record(struct task_struct *child,
571 const struct bts_struct *in)
572{
573 int retval;
574
575 if (!child->thread.ds_area_msr)
576 return -ENXIO;
577
578 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
579 if (retval)
580 return retval;
581
582 return sizeof(*in);
583}
584
Markus Metzgera95d67f2008-01-30 13:31:20 +0100585static int ptrace_bts_clear(struct task_struct *child)
Markus Metzgereee3af42008-01-30 13:31:09 +0100586{
Markus Metzgera95d67f2008-01-30 13:31:20 +0100587 if (!child->thread.ds_area_msr)
Markus Metzgereee3af42008-01-30 13:31:09 +0100588 return -ENXIO;
589
Markus Metzgera95d67f2008-01-30 13:31:20 +0100590 return ds_clear((void *)child->thread.ds_area_msr);
591}
592
593static int ptrace_bts_drain(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100594 long size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100595 struct bts_struct __user *out)
596{
597 int end, i;
598 void *ds = (void *)child->thread.ds_area_msr;
599
600 if (!ds)
601 return -ENXIO;
602
603 end = ds_get_bts_index(ds);
604 if (end <= 0)
605 return end;
606
Markus Metzgercba4b652008-01-30 13:32:03 +0100607 if (size < (end * sizeof(struct bts_struct)))
608 return -EIO;
609
Markus Metzgera95d67f2008-01-30 13:31:20 +0100610 for (i = 0; i < end; i++, out++) {
611 struct bts_struct ret;
612 int retval;
613
614 retval = ds_read_bts(ds, i, &ret);
615 if (retval < 0)
616 return retval;
617
618 if (copy_to_user(out, &ret, sizeof(ret)))
619 return -EFAULT;
620 }
621
622 ds_clear(ds);
623
Markus Metzgercba4b652008-01-30 13:32:03 +0100624 return end;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100625}
626
Markus Metzgerda35c372008-01-30 13:32:03 +0100627static int ptrace_bts_realloc(struct task_struct *child,
628 int size, int reduce_size)
629{
630 unsigned long rlim, vm;
631 int ret, old_size;
632
633 if (size < 0)
634 return -EINVAL;
635
636 old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
637 if (old_size < 0)
638 return old_size;
639
640 ret = ds_free((void **)&child->thread.ds_area_msr);
641 if (ret < 0)
642 goto out;
643
644 size >>= PAGE_SHIFT;
645 old_size >>= PAGE_SHIFT;
646
647 current->mm->total_vm -= old_size;
648 current->mm->locked_vm -= old_size;
649
650 if (size == 0)
651 goto out;
652
653 rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
654 vm = current->mm->total_vm + size;
655 if (rlim < vm) {
656 ret = -ENOMEM;
657
658 if (!reduce_size)
659 goto out;
660
661 size = rlim - current->mm->total_vm;
662 if (size <= 0)
663 goto out;
664 }
665
666 rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
667 vm = current->mm->locked_vm + size;
668 if (rlim < vm) {
669 ret = -ENOMEM;
670
671 if (!reduce_size)
672 goto out;
673
674 size = rlim - current->mm->locked_vm;
675 if (size <= 0)
676 goto out;
677 }
678
679 ret = ds_allocate((void **)&child->thread.ds_area_msr,
680 size << PAGE_SHIFT);
681 if (ret < 0)
682 goto out;
683
684 current->mm->total_vm += size;
685 current->mm->locked_vm += size;
686
687out:
688 if (child->thread.ds_area_msr)
689 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
690 else
691 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
692
693 return ret;
694}
695
Markus Metzgera95d67f2008-01-30 13:31:20 +0100696static int ptrace_bts_config(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100697 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100698 const struct ptrace_bts_config __user *ucfg)
699{
700 struct ptrace_bts_config cfg;
Markus Metzgerda35c372008-01-30 13:32:03 +0100701 int bts_size, ret = 0;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100702 void *ds;
703
Markus Metzgercba4b652008-01-30 13:32:03 +0100704 if (cfg_size < sizeof(cfg))
705 return -EIO;
706
Markus Metzgera95d67f2008-01-30 13:31:20 +0100707 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
708 return -EFAULT;
709
Markus Metzgercba4b652008-01-30 13:32:03 +0100710 if ((int)cfg.size < 0)
711 return -EINVAL;
712
Markus Metzgera95d67f2008-01-30 13:31:20 +0100713 bts_size = 0;
714 ds = (void *)child->thread.ds_area_msr;
715 if (ds) {
716 bts_size = ds_get_bts_size(ds);
717 if (bts_size < 0)
718 return bts_size;
719 }
Markus Metzgerda35c372008-01-30 13:32:03 +0100720 cfg.size = PAGE_ALIGN(cfg.size);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100721
722 if (bts_size != cfg.size) {
Markus Metzgerda35c372008-01-30 13:32:03 +0100723 ret = ptrace_bts_realloc(child, cfg.size,
724 cfg.flags & PTRACE_BTS_O_CUT_SIZE);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100725 if (ret < 0)
Markus Metzgerda35c372008-01-30 13:32:03 +0100726 goto errout;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100727
Markus Metzgera95d67f2008-01-30 13:31:20 +0100728 ds = (void *)child->thread.ds_area_msr;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100729 }
730
Markus Metzgerda35c372008-01-30 13:32:03 +0100731 if (cfg.flags & PTRACE_BTS_O_SIGNAL)
732 ret = ds_set_overflow(ds, DS_O_SIGNAL);
733 else
734 ret = ds_set_overflow(ds, DS_O_WRAP);
735 if (ret < 0)
736 goto errout;
Markus Metzgera95d67f2008-01-30 13:31:20 +0100737
Markus Metzgerda35c372008-01-30 13:32:03 +0100738 if (cfg.flags & PTRACE_BTS_O_TRACE)
739 child->thread.debugctlmsr |= ds_debugctl_mask();
740 else
741 child->thread.debugctlmsr &= ~ds_debugctl_mask();
Markus Metzgereee3af42008-01-30 13:31:09 +0100742
Markus Metzgerda35c372008-01-30 13:32:03 +0100743 if (cfg.flags & PTRACE_BTS_O_SCHED)
Markus Metzgereee3af42008-01-30 13:31:09 +0100744 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
745 else
746 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
747
Markus Metzgercba4b652008-01-30 13:32:03 +0100748 ret = sizeof(cfg);
749
Markus Metzgerda35c372008-01-30 13:32:03 +0100750out:
751 if (child->thread.debugctlmsr)
752 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
753 else
754 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
755
756 return ret;
757
758errout:
759 child->thread.debugctlmsr &= ~ds_debugctl_mask();
760 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
761 goto out;
Markus Metzgereee3af42008-01-30 13:31:09 +0100762}
763
Markus Metzgera95d67f2008-01-30 13:31:20 +0100764static int ptrace_bts_status(struct task_struct *child,
Markus Metzgercba4b652008-01-30 13:32:03 +0100765 long cfg_size,
Markus Metzgera95d67f2008-01-30 13:31:20 +0100766 struct ptrace_bts_config __user *ucfg)
Markus Metzgereee3af42008-01-30 13:31:09 +0100767{
Markus Metzgera95d67f2008-01-30 13:31:20 +0100768 void *ds = (void *)child->thread.ds_area_msr;
769 struct ptrace_bts_config cfg;
Markus Metzgereee3af42008-01-30 13:31:09 +0100770
Markus Metzgercba4b652008-01-30 13:32:03 +0100771 if (cfg_size < sizeof(cfg))
772 return -EIO;
773
Markus Metzgera95d67f2008-01-30 13:31:20 +0100774 memset(&cfg, 0, sizeof(cfg));
Markus Metzgereee3af42008-01-30 13:31:09 +0100775
Markus Metzgera95d67f2008-01-30 13:31:20 +0100776 if (ds) {
777 cfg.size = ds_get_bts_size(ds);
Markus Metzgereee3af42008-01-30 13:31:09 +0100778
Markus Metzgera95d67f2008-01-30 13:31:20 +0100779 if (ds_get_overflow(ds) == DS_O_SIGNAL)
780 cfg.flags |= PTRACE_BTS_O_SIGNAL;
Markus Metzgereee3af42008-01-30 13:31:09 +0100781
Markus Metzgera95d67f2008-01-30 13:31:20 +0100782 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
783 child->thread.debugctlmsr & ds_debugctl_mask())
784 cfg.flags |= PTRACE_BTS_O_TRACE;
Markus Metzgereee3af42008-01-30 13:31:09 +0100785
Markus Metzgera95d67f2008-01-30 13:31:20 +0100786 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
787 cfg.flags |= PTRACE_BTS_O_SCHED;
Markus Metzgereee3af42008-01-30 13:31:09 +0100788 }
789
Markus Metzgera95d67f2008-01-30 13:31:20 +0100790 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
791 return -EFAULT;
Markus Metzgereee3af42008-01-30 13:31:09 +0100792
Markus Metzgera95d67f2008-01-30 13:31:20 +0100793 return sizeof(cfg);
Markus Metzgereee3af42008-01-30 13:31:09 +0100794}
795
796void ptrace_bts_take_timestamp(struct task_struct *tsk,
797 enum bts_qualifier qualifier)
798{
799 struct bts_struct rec = {
800 .qualifier = qualifier,
Markus Metzgerda35c372008-01-30 13:32:03 +0100801 .variant.jiffies = jiffies_64
Markus Metzgereee3af42008-01-30 13:31:09 +0100802 };
803
Markus Metzgereee3af42008-01-30 13:31:09 +0100804 ptrace_bts_write_record(tsk, &rec);
805}
806
Roland McGrathd9771e82008-01-30 13:30:52 +0100807/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * Called by kernel/ptrace.c when detaching..
809 *
810 * Make sure the single step bit is not set.
811 */
812void ptrace_disable(struct task_struct *child)
Roland McGrath9e714be2008-01-30 13:30:58 +0100813{
Roland McGrath7f232342008-01-30 13:30:48 +0100814 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100815#ifdef TIF_SYSCALL_EMU
Bodo Stroesserab1c23c2005-09-03 15:57:21 -0700816 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100817#endif
Markus Metzgereee3af42008-01-30 13:31:09 +0100818 if (child->thread.ds_area_msr) {
Markus Metzgerda35c372008-01-30 13:32:03 +0100819 ptrace_bts_realloc(child, 0, 0);
820 child->thread.debugctlmsr &= ~ds_debugctl_mask();
821 if (!child->thread.debugctlmsr)
822 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
823 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
Markus Metzgereee3af42008-01-30 13:31:09 +0100824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Roland McGrath5a4646a2008-01-30 13:31:54 +0100827#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
828static const struct user_regset_view user_x86_32_view; /* Initialized below. */
829#endif
830
Christoph Hellwig481bed42005-11-07 00:59:47 -0800831long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100833 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 unsigned long __user *datap = (unsigned long __user *)data;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* read the word at location addr in the USER area. */
838 case PTRACE_PEEKUSR: {
839 unsigned long tmp;
840
841 ret = -EIO;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100842 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
843 addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 break;
845
846 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100847 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100849 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
850 addr <= offsetof(struct user, u_debugreg[7])) {
851 addr -= offsetof(struct user, u_debugreg[0]);
852 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854 ret = put_user(tmp, datap);
855 break;
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
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
Roland McGrathe9c86c72008-01-30 13:31:01 +0100864 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100866 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
867 addr <= offsetof(struct user, u_debugreg[7])) {
868 addr -= offsetof(struct user, u_debugreg[0]);
869 ret = ptrace_set_debugreg(child,
870 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Roland McGrath5a4646a2008-01-30 13:31:54 +0100874 case PTRACE_GETREGS: /* Get all gp regs from the child. */
875 return copy_regset_to_user(child,
876 task_user_regset_view(current),
877 REGSET_GENERAL,
878 0, sizeof(struct user_regs_struct),
879 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Roland McGrath5a4646a2008-01-30 13:31:54 +0100881 case PTRACE_SETREGS: /* Set all gp regs in the child. */
882 return copy_regset_from_user(child,
883 task_user_regset_view(current),
884 REGSET_GENERAL,
885 0, sizeof(struct user_regs_struct),
886 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Roland McGrath5a4646a2008-01-30 13:31:54 +0100888 case PTRACE_GETFPREGS: /* Get the child FPU state. */
889 return copy_regset_to_user(child,
890 task_user_regset_view(current),
891 REGSET_FP,
892 0, sizeof(struct user_i387_struct),
893 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Roland McGrath5a4646a2008-01-30 13:31:54 +0100895 case PTRACE_SETFPREGS: /* Set the child FPU state. */
896 return copy_regset_from_user(child,
897 task_user_regset_view(current),
898 REGSET_FP,
899 0, sizeof(struct user_i387_struct),
900 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Roland McGrathe9c86c72008-01-30 13:31:01 +0100902#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100903 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
904 return copy_regset_to_user(child, &user_x86_32_view,
905 REGSET_XFP,
906 0, sizeof(struct user_fxsr_struct),
907 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Roland McGrath5a4646a2008-01-30 13:31:54 +0100909 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
910 return copy_regset_from_user(child, &user_x86_32_view,
911 REGSET_XFP,
912 0, sizeof(struct user_fxsr_struct),
913 datap);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Roland McGrathe9c86c72008-01-30 13:31:01 +0100916#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 case PTRACE_GET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100918 if (addr < 0)
919 return -EIO;
920 ret = do_get_thread_area(child, addr,
921 (struct user_desc __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 break;
923
924 case PTRACE_SET_THREAD_AREA:
Roland McGrathefd1ca52008-01-30 13:30:46 +0100925 if (addr < 0)
926 return -EIO;
927 ret = do_set_thread_area(child, addr,
928 (struct user_desc __user *) data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100930#endif
931
932#ifdef CONFIG_X86_64
933 /* normal 64bit interface to access TLS data.
934 Works just like arch_prctl, except that the arguments
935 are reversed. */
936 case PTRACE_ARCH_PRCTL:
937 ret = do_arch_prctl(child, data, addr);
938 break;
939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Markus Metzgereee3af42008-01-30 13:31:09 +0100941 case PTRACE_BTS_CONFIG:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100942 ret = ptrace_bts_config
Markus Metzgercba4b652008-01-30 13:32:03 +0100943 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100944 break;
945
946 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +0100947 ret = ptrace_bts_status
Markus Metzgercba4b652008-01-30 13:32:03 +0100948 (child, data, (struct ptrace_bts_config __user *)addr);
Markus Metzgera95d67f2008-01-30 13:31:20 +0100949 break;
950
951 case PTRACE_BTS_SIZE:
952 ret = ptrace_bts_get_size(child);
953 break;
954
955 case PTRACE_BTS_GET:
956 ret = ptrace_bts_read_record
957 (child, data, (struct bts_struct __user *) addr);
958 break;
959
960 case PTRACE_BTS_CLEAR:
961 ret = ptrace_bts_clear(child);
962 break;
963
964 case PTRACE_BTS_DRAIN:
965 ret = ptrace_bts_drain
Markus Metzgercba4b652008-01-30 13:32:03 +0100966 (child, data, (struct bts_struct __user *) addr);
Markus Metzgereee3af42008-01-30 13:31:09 +0100967 break;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 default:
970 ret = ptrace_request(child, request, addr, data);
971 break;
972 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return ret;
975}
976
Roland McGrathcb757c42008-01-30 13:31:01 +0100977#ifdef CONFIG_IA32_EMULATION
978
Roland McGrath099cd6e2008-01-30 13:31:01 +0100979#include <linux/compat.h>
980#include <linux/syscalls.h>
981#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +0100982#include <asm/user32.h>
983
984#define R32(l,q) \
985 case offsetof(struct user32, regs.l): \
986 regs->q = value; break
987
988#define SEG32(rs) \
989 case offsetof(struct user32, regs.rs): \
990 return set_segment_reg(child, \
991 offsetof(struct user_regs_struct, rs), \
992 value); \
993 break
994
995static int putreg32(struct task_struct *child, unsigned regno, u32 value)
996{
997 struct pt_regs *regs = task_pt_regs(child);
998
999 switch (regno) {
1000
1001 SEG32(cs);
1002 SEG32(ds);
1003 SEG32(es);
1004 SEG32(fs);
1005 SEG32(gs);
1006 SEG32(ss);
1007
1008 R32(ebx, bx);
1009 R32(ecx, cx);
1010 R32(edx, dx);
1011 R32(edi, di);
1012 R32(esi, si);
1013 R32(ebp, bp);
1014 R32(eax, ax);
1015 R32(orig_eax, orig_ax);
1016 R32(eip, ip);
1017 R32(esp, sp);
1018
1019 case offsetof(struct user32, regs.eflags):
1020 return set_flags(child, value);
1021
1022 case offsetof(struct user32, u_debugreg[0]) ...
1023 offsetof(struct user32, u_debugreg[7]):
1024 regno -= offsetof(struct user32, u_debugreg[0]);
1025 return ptrace_set_debugreg(child, regno / 4, value);
1026
1027 default:
1028 if (regno > sizeof(struct user32) || (regno & 3))
1029 return -EIO;
1030
1031 /*
1032 * Other dummy fields in the virtual user structure
1033 * are ignored
1034 */
1035 break;
1036 }
1037 return 0;
1038}
1039
1040#undef R32
1041#undef SEG32
1042
1043#define R32(l,q) \
1044 case offsetof(struct user32, regs.l): \
1045 *val = regs->q; break
1046
1047#define SEG32(rs) \
1048 case offsetof(struct user32, regs.rs): \
1049 *val = get_segment_reg(child, \
1050 offsetof(struct user_regs_struct, rs)); \
1051 break
1052
1053static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1054{
1055 struct pt_regs *regs = task_pt_regs(child);
1056
1057 switch (regno) {
1058
1059 SEG32(ds);
1060 SEG32(es);
1061 SEG32(fs);
1062 SEG32(gs);
1063
1064 R32(cs, cs);
1065 R32(ss, ss);
1066 R32(ebx, bx);
1067 R32(ecx, cx);
1068 R32(edx, dx);
1069 R32(edi, di);
1070 R32(esi, si);
1071 R32(ebp, bp);
1072 R32(eax, ax);
1073 R32(orig_eax, orig_ax);
1074 R32(eip, ip);
1075 R32(esp, sp);
1076
1077 case offsetof(struct user32, regs.eflags):
1078 *val = get_flags(child);
1079 break;
1080
1081 case offsetof(struct user32, u_debugreg[0]) ...
1082 offsetof(struct user32, u_debugreg[7]):
1083 regno -= offsetof(struct user32, u_debugreg[0]);
1084 *val = ptrace_get_debugreg(child, regno / 4);
1085 break;
1086
1087 default:
1088 if (regno > sizeof(struct user32) || (regno & 3))
1089 return -EIO;
1090
1091 /*
1092 * Other dummy fields in the virtual user structure
1093 * are ignored
1094 */
1095 *val = 0;
1096 break;
1097 }
1098 return 0;
1099}
1100
1101#undef R32
1102#undef SEG32
1103
Roland McGrath91e7b702008-01-30 13:31:52 +01001104static int genregs32_get(struct task_struct *target,
1105 const struct user_regset *regset,
1106 unsigned int pos, unsigned int count,
1107 void *kbuf, void __user *ubuf)
1108{
1109 if (kbuf) {
1110 compat_ulong_t *k = kbuf;
1111 while (count > 0) {
1112 getreg32(target, pos, k++);
1113 count -= sizeof(*k);
1114 pos += sizeof(*k);
1115 }
1116 } else {
1117 compat_ulong_t __user *u = ubuf;
1118 while (count > 0) {
1119 compat_ulong_t word;
1120 getreg32(target, pos, &word);
1121 if (__put_user(word, u++))
1122 return -EFAULT;
1123 count -= sizeof(*u);
1124 pos += sizeof(*u);
1125 }
1126 }
1127
1128 return 0;
1129}
1130
1131static int genregs32_set(struct task_struct *target,
1132 const struct user_regset *regset,
1133 unsigned int pos, unsigned int count,
1134 const void *kbuf, const void __user *ubuf)
1135{
1136 int ret = 0;
1137 if (kbuf) {
1138 const compat_ulong_t *k = kbuf;
1139 while (count > 0 && !ret) {
1140 ret = putreg(target, pos, *k++);
1141 count -= sizeof(*k);
1142 pos += sizeof(*k);
1143 }
1144 } else {
1145 const compat_ulong_t __user *u = ubuf;
1146 while (count > 0 && !ret) {
1147 compat_ulong_t word;
1148 ret = __get_user(word, u++);
1149 if (ret)
1150 break;
1151 ret = putreg(target, pos, word);
1152 count -= sizeof(*u);
1153 pos += sizeof(*u);
1154 }
1155 }
1156 return ret;
1157}
1158
Roland McGrath099cd6e2008-01-30 13:31:01 +01001159static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
1160{
1161 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
1162 compat_siginfo_t __user *si32 = compat_ptr(data);
1163 siginfo_t ssi;
1164 int ret;
1165
1166 if (request == PTRACE_SETSIGINFO) {
1167 memset(&ssi, 0, sizeof(siginfo_t));
1168 ret = copy_siginfo_from_user32(&ssi, si32);
1169 if (ret)
1170 return ret;
1171 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
1172 return -EFAULT;
1173 }
1174 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
1175 if (ret)
1176 return ret;
1177 if (request == PTRACE_GETSIGINFO) {
1178 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
1179 return -EFAULT;
1180 ret = copy_siginfo_to_user32(si32, &ssi);
1181 }
1182 return ret;
1183}
1184
1185asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
1186{
1187 struct task_struct *child;
1188 struct pt_regs *childregs;
1189 void __user *datap = compat_ptr(data);
1190 int ret;
1191 __u32 val;
1192
1193 switch (request) {
1194 case PTRACE_TRACEME:
1195 case PTRACE_ATTACH:
1196 case PTRACE_KILL:
1197 case PTRACE_CONT:
1198 case PTRACE_SINGLESTEP:
1199 case PTRACE_SINGLEBLOCK:
1200 case PTRACE_DETACH:
1201 case PTRACE_SYSCALL:
1202 case PTRACE_OLDSETOPTIONS:
1203 case PTRACE_SETOPTIONS:
1204 case PTRACE_SET_THREAD_AREA:
1205 case PTRACE_GET_THREAD_AREA:
Markus Metzgereee3af42008-01-30 13:31:09 +01001206 case PTRACE_BTS_CONFIG:
1207 case PTRACE_BTS_STATUS:
Markus Metzgera95d67f2008-01-30 13:31:20 +01001208 case PTRACE_BTS_SIZE:
1209 case PTRACE_BTS_GET:
1210 case PTRACE_BTS_CLEAR:
1211 case PTRACE_BTS_DRAIN:
Roland McGrath099cd6e2008-01-30 13:31:01 +01001212 return sys_ptrace(request, pid, addr, data);
1213
1214 default:
1215 return -EINVAL;
1216
1217 case PTRACE_PEEKTEXT:
1218 case PTRACE_PEEKDATA:
1219 case PTRACE_POKEDATA:
1220 case PTRACE_POKETEXT:
1221 case PTRACE_POKEUSR:
1222 case PTRACE_PEEKUSR:
1223 case PTRACE_GETREGS:
1224 case PTRACE_SETREGS:
1225 case PTRACE_SETFPREGS:
1226 case PTRACE_GETFPREGS:
1227 case PTRACE_SETFPXREGS:
1228 case PTRACE_GETFPXREGS:
1229 case PTRACE_GETEVENTMSG:
1230 break;
1231
1232 case PTRACE_SETSIGINFO:
1233 case PTRACE_GETSIGINFO:
1234 return ptrace32_siginfo(request, pid, addr, data);
1235 }
1236
1237 child = ptrace_get_task_struct(pid);
1238 if (IS_ERR(child))
1239 return PTR_ERR(child);
1240
1241 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1242 if (ret < 0)
1243 goto out;
1244
1245 childregs = task_pt_regs(child);
1246
1247 switch (request) {
Roland McGrath099cd6e2008-01-30 13:31:01 +01001248 case PTRACE_PEEKUSR:
1249 ret = getreg32(child, addr, &val);
1250 if (ret == 0)
1251 ret = put_user(val, (__u32 __user *)datap);
1252 break;
1253
1254 case PTRACE_POKEUSR:
1255 ret = putreg32(child, addr, data);
1256 break;
1257
Roland McGrath5a4646a2008-01-30 13:31:54 +01001258 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1259 return copy_regset_to_user(child, &user_x86_32_view,
1260 REGSET_GENERAL,
1261 0, sizeof(struct user_regs_struct32),
1262 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001263
Roland McGrath5a4646a2008-01-30 13:31:54 +01001264 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1265 return copy_regset_from_user(child, &user_x86_32_view,
1266 REGSET_GENERAL, 0,
1267 sizeof(struct user_regs_struct32),
1268 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001269
Roland McGrath5a4646a2008-01-30 13:31:54 +01001270 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1271 return copy_regset_to_user(child, &user_x86_32_view,
1272 REGSET_FP, 0,
1273 sizeof(struct user_i387_ia32_struct),
1274 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001275
Roland McGrath5a4646a2008-01-30 13:31:54 +01001276 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1277 return copy_regset_from_user(
1278 child, &user_x86_32_view, REGSET_FP,
1279 0, sizeof(struct user_i387_ia32_struct), datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001280
Roland McGrath5a4646a2008-01-30 13:31:54 +01001281 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1282 return copy_regset_to_user(child, &user_x86_32_view,
1283 REGSET_XFP, 0,
1284 sizeof(struct user32_fxsr_struct),
1285 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001286
Roland McGrath5a4646a2008-01-30 13:31:54 +01001287 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1288 return copy_regset_from_user(child, &user_x86_32_view,
1289 REGSET_XFP, 0,
1290 sizeof(struct user32_fxsr_struct),
1291 datap);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001292
Roland McGrath099cd6e2008-01-30 13:31:01 +01001293 default:
Roland McGrathfdadd542008-01-30 13:31:56 +01001294 return compat_ptrace_request(child, request, addr, data);
Roland McGrath099cd6e2008-01-30 13:31:01 +01001295 }
1296
1297 out:
1298 put_task_struct(child);
1299 return ret;
1300}
1301
Roland McGrathcb757c42008-01-30 13:31:01 +01001302#endif /* CONFIG_IA32_EMULATION */
1303
Roland McGrath070459d2008-01-30 13:31:53 +01001304#ifdef CONFIG_X86_64
1305
1306static const struct user_regset x86_64_regsets[] = {
1307 [REGSET_GENERAL] = {
1308 .core_note_type = NT_PRSTATUS,
1309 .n = sizeof(struct user_regs_struct) / sizeof(long),
1310 .size = sizeof(long), .align = sizeof(long),
1311 .get = genregs_get, .set = genregs_set
1312 },
1313 [REGSET_FP] = {
1314 .core_note_type = NT_PRFPREG,
1315 .n = sizeof(struct user_i387_struct) / sizeof(long),
1316 .size = sizeof(long), .align = sizeof(long),
1317 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1318 },
1319};
1320
1321static const struct user_regset_view user_x86_64_view = {
1322 .name = "x86_64", .e_machine = EM_X86_64,
1323 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1324};
1325
1326#else /* CONFIG_X86_32 */
1327
1328#define user_regs_struct32 user_regs_struct
1329#define genregs32_get genregs_get
1330#define genregs32_set genregs_set
1331
1332#endif /* CONFIG_X86_64 */
1333
1334#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1335static const struct user_regset x86_32_regsets[] = {
1336 [REGSET_GENERAL] = {
1337 .core_note_type = NT_PRSTATUS,
1338 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1339 .size = sizeof(u32), .align = sizeof(u32),
1340 .get = genregs32_get, .set = genregs32_set
1341 },
1342 [REGSET_FP] = {
1343 .core_note_type = NT_PRFPREG,
1344 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1345 .size = sizeof(u32), .align = sizeof(u32),
1346 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1347 },
1348 [REGSET_XFP] = {
1349 .core_note_type = NT_PRXFPREG,
1350 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1351 .size = sizeof(u32), .align = sizeof(u32),
1352 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1353 },
1354 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001355 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001356 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1357 .size = sizeof(struct user_desc),
1358 .align = sizeof(struct user_desc),
1359 .active = regset_tls_active,
1360 .get = regset_tls_get, .set = regset_tls_set
1361 },
1362};
1363
1364static const struct user_regset_view user_x86_32_view = {
1365 .name = "i386", .e_machine = EM_386,
1366 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1367};
1368#endif
1369
1370const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1371{
1372#ifdef CONFIG_IA32_EMULATION
1373 if (test_tsk_thread_flag(task, TIF_IA32))
1374#endif
1375#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1376 return &user_x86_32_view;
1377#endif
1378#ifdef CONFIG_X86_64
1379 return &user_x86_64_view;
1380#endif
1381}
1382
Roland McGrath86976cd2008-01-30 13:31:01 +01001383#ifdef CONFIG_X86_32
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
1386{
1387 struct siginfo info;
1388
1389 tsk->thread.trap_no = 1;
1390 tsk->thread.error_code = error_code;
1391
1392 memset(&info, 0, sizeof(info));
1393 info.si_signo = SIGTRAP;
1394 info.si_code = TRAP_BRKPT;
1395
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001396 /* User-mode ip? */
1397 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Simon Arlott27b46d72007-10-20 01:13:56 +02001399 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 force_sig_info(SIGTRAP, &info, tsk);
1401}
1402
1403/* notification of system call entry/exit
1404 * - triggered by current->work.syscall_trace
1405 */
1406__attribute__((regparm(3)))
Laurent Viviered75e8d2005-09-03 15:57:18 -07001407int do_syscall_trace(struct pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001409 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
1410 /*
1411 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1412 * interception
1413 */
Bodo Stroesser1b38f002005-09-03 15:57:20 -07001414 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001415 int ret = 0;
Bodo Stroesser1b38f002005-09-03 15:57:20 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* do the secure computing check first */
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001418 if (!entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001419 secure_computing(regs->orig_ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Bodo Stroesserab1c23c2005-09-03 15:57:21 -07001421 if (unlikely(current->audit_context)) {
1422 if (entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001423 audit_syscall_exit(AUDITSC_RESULT(regs->ax),
1424 regs->ax);
Bodo Stroesserab1c23c2005-09-03 15:57:21 -07001425 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1426 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1427 * not used, entry.S will call us only on syscall exit, not
1428 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1429 * calling send_sigtrap() on syscall entry.
1430 *
1431 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1432 * is_singlestep is false, despite his name, so we will still do
1433 * the correct thing.
1434 */
1435 else if (is_singlestep)
1436 goto out;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 if (!(current->ptrace & PT_PTRACED))
2fd6f582005-04-29 16:08:28 +01001440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Bodo Stroesser1b38f002005-09-03 15:57:20 -07001442 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1443 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1444 * here. We have to check this and return */
1445 if (is_sysemu && entryexit)
1446 return 0;
Laurent Viviered75e8d2005-09-03 15:57:18 -07001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* Fake a debug trap */
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001449 if (is_singlestep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 send_sigtrap(current, regs, 0);
1451
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001452 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
2fd6f582005-04-29 16:08:28 +01001453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* the 0x80 provides a way for the tracing parent to distinguish
1456 between a syscall stop and SIGTRAP delivery */
Laurent Viviered75e8d2005-09-03 15:57:18 -07001457 /* Note that the debugger could change the result of test_thread_flag!*/
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001458 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /*
1461 * this isn't the same as continuing with a signal, but it will do
1462 * for normal use. strace only continues with a signal if the
1463 * stopping signal is not SIGTRAP. -brl
1464 */
1465 if (current->exit_code) {
1466 send_sig(current->exit_code, current, 1);
1467 current->exit_code = 0;
1468 }
Laurent Viviered75e8d2005-09-03 15:57:18 -07001469 ret = is_sysemu;
Andrea Arcangeli4c7fc722005-09-09 13:01:51 -07001470out:
2fd6f582005-04-29 16:08:28 +01001471 if (unlikely(current->audit_context) && !entryexit)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001472 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
1473 regs->bx, regs->cx, regs->dx, regs->si);
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001474 if (ret == 0)
1475 return 0;
1476
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001477 regs->orig_ax = -1; /* force skip of syscall restarting */
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001478 if (unlikely(current->audit_context))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001479 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
Bodo Stroesserc8c86ce2005-09-03 15:57:19 -07001480 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
Roland McGrath86976cd2008-01-30 13:31:01 +01001482
1483#else /* CONFIG_X86_64 */
1484
1485static void syscall_trace(struct pt_regs *regs)
1486{
1487
1488#if 0
1489 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1490 current->comm,
1491 regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
1492 current_thread_info()->flags, current->ptrace);
1493#endif
1494
1495 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1496 ? 0x80 : 0));
1497 /*
1498 * this isn't the same as continuing with a signal, but it will do
1499 * for normal use. strace only continues with a signal if the
1500 * stopping signal is not SIGTRAP. -brl
1501 */
1502 if (current->exit_code) {
1503 send_sig(current->exit_code, current, 1);
1504 current->exit_code = 0;
1505 }
1506}
1507
1508asmlinkage void syscall_trace_enter(struct pt_regs *regs)
1509{
1510 /* do the secure computing check first */
1511 secure_computing(regs->orig_ax);
1512
1513 if (test_thread_flag(TIF_SYSCALL_TRACE)
1514 && (current->ptrace & PT_PTRACED))
1515 syscall_trace(regs);
1516
1517 if (unlikely(current->audit_context)) {
1518 if (test_thread_flag(TIF_IA32)) {
1519 audit_syscall_entry(AUDIT_ARCH_I386,
1520 regs->orig_ax,
1521 regs->bx, regs->cx,
1522 regs->dx, regs->si);
1523 } else {
1524 audit_syscall_entry(AUDIT_ARCH_X86_64,
1525 regs->orig_ax,
1526 regs->di, regs->si,
1527 regs->dx, regs->r10);
1528 }
1529 }
1530}
1531
1532asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1533{
1534 if (unlikely(current->audit_context))
1535 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1536
1537 if ((test_thread_flag(TIF_SYSCALL_TRACE)
1538 || test_thread_flag(TIF_SINGLESTEP))
1539 && (current->ptrace & PT_PTRACED))
1540 syscall_trace(regs);
1541}
1542
1543#endif /* CONFIG_X86_32 */