xref: /linux/arch/x86/kernel/ptrace.c (revision bcefe12eff5dca6fdfa94ed85e5bee66380d5cd9)
1 /* By Ross Biro 1/23/92 */
2 /*
3  * Pentium III FXSR, SSE support
4  *	Gareth Hughes <gareth@valinux.com>, May 2000
5  *
6  * BTS tracing
7  *	Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/regset.h>
17 #include <linux/tracehook.h>
18 #include <linux/user.h>
19 #include <linux/elf.h>
20 #include <linux/security.h>
21 #include <linux/audit.h>
22 #include <linux/seccomp.h>
23 #include <linux/signal.h>
24 #include <linux/workqueue.h>
25 #include <linux/perf_event.h>
26 #include <linux/hw_breakpoint.h>
27 
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/system.h>
31 #include <asm/processor.h>
32 #include <asm/i387.h>
33 #include <asm/debugreg.h>
34 #include <asm/ldt.h>
35 #include <asm/desc.h>
36 #include <asm/prctl.h>
37 #include <asm/proto.h>
38 #include <asm/ds.h>
39 #include <asm/hw_breakpoint.h>
40 
41 #include "tls.h"
42 
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/syscalls.h>
45 
46 enum x86_regset {
47 	REGSET_GENERAL,
48 	REGSET_FP,
49 	REGSET_XFP,
50 	REGSET_IOPERM64 = REGSET_XFP,
51 	REGSET_TLS,
52 	REGSET_IOPERM32,
53 };
54 
55 struct pt_regs_offset {
56 	const char *name;
57 	int offset;
58 };
59 
60 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61 #define REG_OFFSET_END {.name = NULL, .offset = 0}
62 
63 static const struct pt_regs_offset regoffset_table[] = {
64 #ifdef CONFIG_X86_64
65 	REG_OFFSET_NAME(r15),
66 	REG_OFFSET_NAME(r14),
67 	REG_OFFSET_NAME(r13),
68 	REG_OFFSET_NAME(r12),
69 	REG_OFFSET_NAME(r11),
70 	REG_OFFSET_NAME(r10),
71 	REG_OFFSET_NAME(r9),
72 	REG_OFFSET_NAME(r8),
73 #endif
74 	REG_OFFSET_NAME(bx),
75 	REG_OFFSET_NAME(cx),
76 	REG_OFFSET_NAME(dx),
77 	REG_OFFSET_NAME(si),
78 	REG_OFFSET_NAME(di),
79 	REG_OFFSET_NAME(bp),
80 	REG_OFFSET_NAME(ax),
81 #ifdef CONFIG_X86_32
82 	REG_OFFSET_NAME(ds),
83 	REG_OFFSET_NAME(es),
84 	REG_OFFSET_NAME(fs),
85 	REG_OFFSET_NAME(gs),
86 #endif
87 	REG_OFFSET_NAME(orig_ax),
88 	REG_OFFSET_NAME(ip),
89 	REG_OFFSET_NAME(cs),
90 	REG_OFFSET_NAME(flags),
91 	REG_OFFSET_NAME(sp),
92 	REG_OFFSET_NAME(ss),
93 	REG_OFFSET_END,
94 };
95 
96 /**
97  * regs_query_register_offset() - query register offset from its name
98  * @name:	the name of a register
99  *
100  * regs_query_register_offset() returns the offset of a register in struct
101  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
102  */
103 int regs_query_register_offset(const char *name)
104 {
105 	const struct pt_regs_offset *roff;
106 	for (roff = regoffset_table; roff->name != NULL; roff++)
107 		if (!strcmp(roff->name, name))
108 			return roff->offset;
109 	return -EINVAL;
110 }
111 
112 /**
113  * regs_query_register_name() - query register name from its offset
114  * @offset:	the offset of a register in struct pt_regs.
115  *
116  * regs_query_register_name() returns the name of a register from its
117  * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
118  */
119 const char *regs_query_register_name(unsigned int offset)
120 {
121 	const struct pt_regs_offset *roff;
122 	for (roff = regoffset_table; roff->name != NULL; roff++)
123 		if (roff->offset == offset)
124 			return roff->name;
125 	return NULL;
126 }
127 
128 static const int arg_offs_table[] = {
129 #ifdef CONFIG_X86_32
130 	[0] = offsetof(struct pt_regs, ax),
131 	[1] = offsetof(struct pt_regs, dx),
132 	[2] = offsetof(struct pt_regs, cx)
133 #else /* CONFIG_X86_64 */
134 	[0] = offsetof(struct pt_regs, di),
135 	[1] = offsetof(struct pt_regs, si),
136 	[2] = offsetof(struct pt_regs, dx),
137 	[3] = offsetof(struct pt_regs, cx),
138 	[4] = offsetof(struct pt_regs, r8),
139 	[5] = offsetof(struct pt_regs, r9)
140 #endif
141 };
142 
143 /**
144  * regs_get_argument_nth() - get Nth argument at function call
145  * @regs:	pt_regs which contains registers at function entry.
146  * @n:		argument number.
147  *
148  * regs_get_argument_nth() returns @n th argument of a function call.
149  * Since usually the kernel stack will be changed right after function entry,
150  * you must use this at function entry. If the @n th entry is NOT in the
151  * kernel stack or pt_regs, this returns 0.
152  */
153 unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
154 {
155 	if (n < ARRAY_SIZE(arg_offs_table))
156 		return *(unsigned long *)((char *)regs + arg_offs_table[n]);
157 	else {
158 		/*
159 		 * The typical case: arg n is on the stack.
160 		 * (Note: stack[0] = return address, so skip it)
161 		 */
162 		n -= ARRAY_SIZE(arg_offs_table);
163 		return regs_get_kernel_stack_nth(regs, 1 + n);
164 	}
165 }
166 
167 /*
168  * does not yet catch signals sent when the child dies.
169  * in exit.c or in signal.c.
170  */
171 
172 /*
173  * Determines which flags the user has access to [1 = access, 0 = no access].
174  */
175 #define FLAG_MASK_32		((unsigned long)			\
176 				 (X86_EFLAGS_CF | X86_EFLAGS_PF |	\
177 				  X86_EFLAGS_AF | X86_EFLAGS_ZF |	\
178 				  X86_EFLAGS_SF | X86_EFLAGS_TF |	\
179 				  X86_EFLAGS_DF | X86_EFLAGS_OF |	\
180 				  X86_EFLAGS_RF | X86_EFLAGS_AC))
181 
182 /*
183  * Determines whether a value may be installed in a segment register.
184  */
185 static inline bool invalid_selector(u16 value)
186 {
187 	return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
188 }
189 
190 #ifdef CONFIG_X86_32
191 
192 #define FLAG_MASK		FLAG_MASK_32
193 
194 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
195 {
196 	BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
197 	return &regs->bx + (regno >> 2);
198 }
199 
200 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
201 {
202 	/*
203 	 * Returning the value truncates it to 16 bits.
204 	 */
205 	unsigned int retval;
206 	if (offset != offsetof(struct user_regs_struct, gs))
207 		retval = *pt_regs_access(task_pt_regs(task), offset);
208 	else {
209 		if (task == current)
210 			retval = get_user_gs(task_pt_regs(task));
211 		else
212 			retval = task_user_gs(task);
213 	}
214 	return retval;
215 }
216 
217 static int set_segment_reg(struct task_struct *task,
218 			   unsigned long offset, u16 value)
219 {
220 	/*
221 	 * The value argument was already truncated to 16 bits.
222 	 */
223 	if (invalid_selector(value))
224 		return -EIO;
225 
226 	/*
227 	 * For %cs and %ss we cannot permit a null selector.
228 	 * We can permit a bogus selector as long as it has USER_RPL.
229 	 * Null selectors are fine for other segment registers, but
230 	 * we will never get back to user mode with invalid %cs or %ss
231 	 * and will take the trap in iret instead.  Much code relies
232 	 * on user_mode() to distinguish a user trap frame (which can
233 	 * safely use invalid selectors) from a kernel trap frame.
234 	 */
235 	switch (offset) {
236 	case offsetof(struct user_regs_struct, cs):
237 	case offsetof(struct user_regs_struct, ss):
238 		if (unlikely(value == 0))
239 			return -EIO;
240 
241 	default:
242 		*pt_regs_access(task_pt_regs(task), offset) = value;
243 		break;
244 
245 	case offsetof(struct user_regs_struct, gs):
246 		if (task == current)
247 			set_user_gs(task_pt_regs(task), value);
248 		else
249 			task_user_gs(task) = value;
250 	}
251 
252 	return 0;
253 }
254 
255 #else  /* CONFIG_X86_64 */
256 
257 #define FLAG_MASK		(FLAG_MASK_32 | X86_EFLAGS_NT)
258 
259 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
260 {
261 	BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
262 	return &regs->r15 + (offset / sizeof(regs->r15));
263 }
264 
265 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
266 {
267 	/*
268 	 * Returning the value truncates it to 16 bits.
269 	 */
270 	unsigned int seg;
271 
272 	switch (offset) {
273 	case offsetof(struct user_regs_struct, fs):
274 		if (task == current) {
275 			/* Older gas can't assemble movq %?s,%r?? */
276 			asm("movl %%fs,%0" : "=r" (seg));
277 			return seg;
278 		}
279 		return task->thread.fsindex;
280 	case offsetof(struct user_regs_struct, gs):
281 		if (task == current) {
282 			asm("movl %%gs,%0" : "=r" (seg));
283 			return seg;
284 		}
285 		return task->thread.gsindex;
286 	case offsetof(struct user_regs_struct, ds):
287 		if (task == current) {
288 			asm("movl %%ds,%0" : "=r" (seg));
289 			return seg;
290 		}
291 		return task->thread.ds;
292 	case offsetof(struct user_regs_struct, es):
293 		if (task == current) {
294 			asm("movl %%es,%0" : "=r" (seg));
295 			return seg;
296 		}
297 		return task->thread.es;
298 
299 	case offsetof(struct user_regs_struct, cs):
300 	case offsetof(struct user_regs_struct, ss):
301 		break;
302 	}
303 	return *pt_regs_access(task_pt_regs(task), offset);
304 }
305 
306 static int set_segment_reg(struct task_struct *task,
307 			   unsigned long offset, u16 value)
308 {
309 	/*
310 	 * The value argument was already truncated to 16 bits.
311 	 */
312 	if (invalid_selector(value))
313 		return -EIO;
314 
315 	switch (offset) {
316 	case offsetof(struct user_regs_struct,fs):
317 		/*
318 		 * If this is setting fs as for normal 64-bit use but
319 		 * setting fs_base has implicitly changed it, leave it.
320 		 */
321 		if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
322 		     task->thread.fs != 0) ||
323 		    (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
324 		     task->thread.fs == 0))
325 			break;
326 		task->thread.fsindex = value;
327 		if (task == current)
328 			loadsegment(fs, task->thread.fsindex);
329 		break;
330 	case offsetof(struct user_regs_struct,gs):
331 		/*
332 		 * If this is setting gs as for normal 64-bit use but
333 		 * setting gs_base has implicitly changed it, leave it.
334 		 */
335 		if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
336 		     task->thread.gs != 0) ||
337 		    (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
338 		     task->thread.gs == 0))
339 			break;
340 		task->thread.gsindex = value;
341 		if (task == current)
342 			load_gs_index(task->thread.gsindex);
343 		break;
344 	case offsetof(struct user_regs_struct,ds):
345 		task->thread.ds = value;
346 		if (task == current)
347 			loadsegment(ds, task->thread.ds);
348 		break;
349 	case offsetof(struct user_regs_struct,es):
350 		task->thread.es = value;
351 		if (task == current)
352 			loadsegment(es, task->thread.es);
353 		break;
354 
355 		/*
356 		 * Can't actually change these in 64-bit mode.
357 		 */
358 	case offsetof(struct user_regs_struct,cs):
359 		if (unlikely(value == 0))
360 			return -EIO;
361 #ifdef CONFIG_IA32_EMULATION
362 		if (test_tsk_thread_flag(task, TIF_IA32))
363 			task_pt_regs(task)->cs = value;
364 #endif
365 		break;
366 	case offsetof(struct user_regs_struct,ss):
367 		if (unlikely(value == 0))
368 			return -EIO;
369 #ifdef CONFIG_IA32_EMULATION
370 		if (test_tsk_thread_flag(task, TIF_IA32))
371 			task_pt_regs(task)->ss = value;
372 #endif
373 		break;
374 	}
375 
376 	return 0;
377 }
378 
379 #endif	/* CONFIG_X86_32 */
380 
381 static unsigned long get_flags(struct task_struct *task)
382 {
383 	unsigned long retval = task_pt_regs(task)->flags;
384 
385 	/*
386 	 * If the debugger set TF, hide it from the readout.
387 	 */
388 	if (test_tsk_thread_flag(task, TIF_FORCED_TF))
389 		retval &= ~X86_EFLAGS_TF;
390 
391 	return retval;
392 }
393 
394 static int set_flags(struct task_struct *task, unsigned long value)
395 {
396 	struct pt_regs *regs = task_pt_regs(task);
397 
398 	/*
399 	 * If the user value contains TF, mark that
400 	 * it was not "us" (the debugger) that set it.
401 	 * If not, make sure it stays set if we had.
402 	 */
403 	if (value & X86_EFLAGS_TF)
404 		clear_tsk_thread_flag(task, TIF_FORCED_TF);
405 	else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
406 		value |= X86_EFLAGS_TF;
407 
408 	regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
409 
410 	return 0;
411 }
412 
413 static int putreg(struct task_struct *child,
414 		  unsigned long offset, unsigned long value)
415 {
416 	switch (offset) {
417 	case offsetof(struct user_regs_struct, cs):
418 	case offsetof(struct user_regs_struct, ds):
419 	case offsetof(struct user_regs_struct, es):
420 	case offsetof(struct user_regs_struct, fs):
421 	case offsetof(struct user_regs_struct, gs):
422 	case offsetof(struct user_regs_struct, ss):
423 		return set_segment_reg(child, offset, value);
424 
425 	case offsetof(struct user_regs_struct, flags):
426 		return set_flags(child, value);
427 
428 #ifdef CONFIG_X86_64
429 	case offsetof(struct user_regs_struct,fs_base):
430 		if (value >= TASK_SIZE_OF(child))
431 			return -EIO;
432 		/*
433 		 * When changing the segment base, use do_arch_prctl
434 		 * to set either thread.fs or thread.fsindex and the
435 		 * corresponding GDT slot.
436 		 */
437 		if (child->thread.fs != value)
438 			return do_arch_prctl(child, ARCH_SET_FS, value);
439 		return 0;
440 	case offsetof(struct user_regs_struct,gs_base):
441 		/*
442 		 * Exactly the same here as the %fs handling above.
443 		 */
444 		if (value >= TASK_SIZE_OF(child))
445 			return -EIO;
446 		if (child->thread.gs != value)
447 			return do_arch_prctl(child, ARCH_SET_GS, value);
448 		return 0;
449 #endif
450 	}
451 
452 	*pt_regs_access(task_pt_regs(child), offset) = value;
453 	return 0;
454 }
455 
456 static unsigned long getreg(struct task_struct *task, unsigned long offset)
457 {
458 	switch (offset) {
459 	case offsetof(struct user_regs_struct, cs):
460 	case offsetof(struct user_regs_struct, ds):
461 	case offsetof(struct user_regs_struct, es):
462 	case offsetof(struct user_regs_struct, fs):
463 	case offsetof(struct user_regs_struct, gs):
464 	case offsetof(struct user_regs_struct, ss):
465 		return get_segment_reg(task, offset);
466 
467 	case offsetof(struct user_regs_struct, flags):
468 		return get_flags(task);
469 
470 #ifdef CONFIG_X86_64
471 	case offsetof(struct user_regs_struct, fs_base): {
472 		/*
473 		 * do_arch_prctl may have used a GDT slot instead of
474 		 * the MSR.  To userland, it appears the same either
475 		 * way, except the %fs segment selector might not be 0.
476 		 */
477 		unsigned int seg = task->thread.fsindex;
478 		if (task->thread.fs != 0)
479 			return task->thread.fs;
480 		if (task == current)
481 			asm("movl %%fs,%0" : "=r" (seg));
482 		if (seg != FS_TLS_SEL)
483 			return 0;
484 		return get_desc_base(&task->thread.tls_array[FS_TLS]);
485 	}
486 	case offsetof(struct user_regs_struct, gs_base): {
487 		/*
488 		 * Exactly the same here as the %fs handling above.
489 		 */
490 		unsigned int seg = task->thread.gsindex;
491 		if (task->thread.gs != 0)
492 			return task->thread.gs;
493 		if (task == current)
494 			asm("movl %%gs,%0" : "=r" (seg));
495 		if (seg != GS_TLS_SEL)
496 			return 0;
497 		return get_desc_base(&task->thread.tls_array[GS_TLS]);
498 	}
499 #endif
500 	}
501 
502 	return *pt_regs_access(task_pt_regs(task), offset);
503 }
504 
505 static int genregs_get(struct task_struct *target,
506 		       const struct user_regset *regset,
507 		       unsigned int pos, unsigned int count,
508 		       void *kbuf, void __user *ubuf)
509 {
510 	if (kbuf) {
511 		unsigned long *k = kbuf;
512 		while (count > 0) {
513 			*k++ = getreg(target, pos);
514 			count -= sizeof(*k);
515 			pos += sizeof(*k);
516 		}
517 	} else {
518 		unsigned long __user *u = ubuf;
519 		while (count > 0) {
520 			if (__put_user(getreg(target, pos), u++))
521 				return -EFAULT;
522 			count -= sizeof(*u);
523 			pos += sizeof(*u);
524 		}
525 	}
526 
527 	return 0;
528 }
529 
530 static int genregs_set(struct task_struct *target,
531 		       const struct user_regset *regset,
532 		       unsigned int pos, unsigned int count,
533 		       const void *kbuf, const void __user *ubuf)
534 {
535 	int ret = 0;
536 	if (kbuf) {
537 		const unsigned long *k = kbuf;
538 		while (count > 0 && !ret) {
539 			ret = putreg(target, pos, *k++);
540 			count -= sizeof(*k);
541 			pos += sizeof(*k);
542 		}
543 	} else {
544 		const unsigned long  __user *u = ubuf;
545 		while (count > 0 && !ret) {
546 			unsigned long word;
547 			ret = __get_user(word, u++);
548 			if (ret)
549 				break;
550 			ret = putreg(target, pos, word);
551 			count -= sizeof(*u);
552 			pos += sizeof(*u);
553 		}
554 	}
555 	return ret;
556 }
557 
558 static void ptrace_triggered(struct perf_event *bp, void *data)
559 {
560 	int i;
561 	struct thread_struct *thread = &(current->thread);
562 
563 	/*
564 	 * Store in the virtual DR6 register the fact that the breakpoint
565 	 * was hit so the thread's debugger will see it.
566 	 */
567 	for (i = 0; i < HBP_NUM; i++) {
568 		if (thread->ptrace_bps[i] == bp)
569 			break;
570 	}
571 
572 	thread->debugreg6 |= (DR_TRAP0 << i);
573 }
574 
575 /*
576  * Walk through every ptrace breakpoints for this thread and
577  * build the dr7 value on top of their attributes.
578  *
579  */
580 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
581 {
582 	int i;
583 	int dr7 = 0;
584 	struct arch_hw_breakpoint *info;
585 
586 	for (i = 0; i < HBP_NUM; i++) {
587 		if (bp[i] && !bp[i]->attr.disabled) {
588 			info = counter_arch_bp(bp[i]);
589 			dr7 |= encode_dr7(i, info->len, info->type);
590 		}
591 	}
592 
593 	return dr7;
594 }
595 
596 static struct perf_event *
597 ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
598 			 struct task_struct *tsk)
599 {
600 	int err;
601 	int gen_len, gen_type;
602 	DEFINE_BREAKPOINT_ATTR(attr);
603 
604 	/*
605 	 * We shoud have at least an inactive breakpoint at this
606 	 * slot. It means the user is writing dr7 without having
607 	 * written the address register first
608 	 */
609 	if (!bp)
610 		return ERR_PTR(-EINVAL);
611 
612 	err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
613 	if (err)
614 		return ERR_PTR(err);
615 
616 	attr = bp->attr;
617 	attr.bp_len = gen_len;
618 	attr.bp_type = gen_type;
619 	attr.disabled = 0;
620 
621 	return modify_user_hw_breakpoint(bp, &attr, bp->callback, tsk);
622 }
623 
624 /*
625  * Handle ptrace writes to debug register 7.
626  */
627 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
628 {
629 	struct thread_struct *thread = &(tsk->thread);
630 	unsigned long old_dr7;
631 	int i, orig_ret = 0, rc = 0;
632 	int enabled, second_pass = 0;
633 	unsigned len, type;
634 	struct perf_event *bp;
635 
636 	data &= ~DR_CONTROL_RESERVED;
637 	old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
638 restore:
639 	/*
640 	 * Loop through all the hardware breakpoints, making the
641 	 * appropriate changes to each.
642 	 */
643 	for (i = 0; i < HBP_NUM; i++) {
644 		enabled = decode_dr7(data, i, &len, &type);
645 		bp = thread->ptrace_bps[i];
646 
647 		if (!enabled) {
648 			if (bp) {
649 				/*
650 				 * Don't unregister the breakpoints right-away,
651 				 * unless all register_user_hw_breakpoint()
652 				 * requests have succeeded. This prevents
653 				 * any window of opportunity for debug
654 				 * register grabbing by other users.
655 				 */
656 				if (!second_pass)
657 					continue;
658 				thread->ptrace_bps[i] = NULL;
659 				unregister_hw_breakpoint(bp);
660 			}
661 			continue;
662 		}
663 
664 		bp = ptrace_modify_breakpoint(bp, len, type, tsk);
665 
666 		/* Incorrect bp, or we have a bug in bp API */
667 		if (IS_ERR(bp)) {
668 			rc = PTR_ERR(bp);
669 			thread->ptrace_bps[i] = NULL;
670 			break;
671 		}
672 		thread->ptrace_bps[i] = bp;
673 	}
674 	/*
675 	 * Make a second pass to free the remaining unused breakpoints
676 	 * or to restore the original breakpoints if an error occurred.
677 	 */
678 	if (!second_pass) {
679 		second_pass = 1;
680 		if (rc < 0) {
681 			orig_ret = rc;
682 			data = old_dr7;
683 		}
684 		goto restore;
685 	}
686 	return ((orig_ret < 0) ? orig_ret : rc);
687 }
688 
689 /*
690  * Handle PTRACE_PEEKUSR calls for the debug register area.
691  */
692 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
693 {
694 	struct thread_struct *thread = &(tsk->thread);
695 	unsigned long val = 0;
696 
697 	if (n < HBP_NUM) {
698 		struct perf_event *bp;
699 		bp = thread->ptrace_bps[n];
700 		if (!bp)
701 			return 0;
702 		val = bp->hw.info.address;
703 	} else if (n == 6) {
704 		val = thread->debugreg6;
705 	 } else if (n == 7) {
706 		val = ptrace_get_dr7(thread->ptrace_bps);
707 	}
708 	return val;
709 }
710 
711 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
712 				      unsigned long addr)
713 {
714 	struct perf_event *bp;
715 	struct thread_struct *t = &tsk->thread;
716 	DEFINE_BREAKPOINT_ATTR(attr);
717 
718 	if (!t->ptrace_bps[nr]) {
719 		/*
720 		 * Put stub len and type to register (reserve) an inactive but
721 		 * correct bp
722 		 */
723 		attr.bp_addr = addr;
724 		attr.bp_len = HW_BREAKPOINT_LEN_1;
725 		attr.bp_type = HW_BREAKPOINT_W;
726 		attr.disabled = 1;
727 
728 		bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
729 	} else {
730 		bp = t->ptrace_bps[nr];
731 		t->ptrace_bps[nr] = NULL;
732 
733 		attr = bp->attr;
734 		attr.bp_addr = addr;
735 		bp = modify_user_hw_breakpoint(bp, &attr, bp->callback, tsk);
736 	}
737 	/*
738 	 * CHECKME: the previous code returned -EIO if the addr wasn't a
739 	 * valid task virtual addr. The new one will return -EINVAL in this
740 	 * case.
741 	 * -EINVAL may be what we want for in-kernel breakpoints users, but
742 	 * -EIO looks better for ptrace, since we refuse a register writing
743 	 * for the user. And anyway this is the previous behaviour.
744 	 */
745 	if (IS_ERR(bp))
746 		return PTR_ERR(bp);
747 
748 	t->ptrace_bps[nr] = bp;
749 
750 	return 0;
751 }
752 
753 /*
754  * Handle PTRACE_POKEUSR calls for the debug register area.
755  */
756 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
757 {
758 	struct thread_struct *thread = &(tsk->thread);
759 	int rc = 0;
760 
761 	/* There are no DR4 or DR5 registers */
762 	if (n == 4 || n == 5)
763 		return -EIO;
764 
765 	if (n == 6) {
766 		thread->debugreg6 = val;
767 		goto ret_path;
768 	}
769 	if (n < HBP_NUM) {
770 		rc = ptrace_set_breakpoint_addr(tsk, n, val);
771 		if (rc)
772 			return rc;
773 	}
774 	/* All that's left is DR7 */
775 	if (n == 7)
776 		rc = ptrace_write_dr7(tsk, val);
777 
778 ret_path:
779 	return rc;
780 }
781 
782 /*
783  * These access the current or another (stopped) task's io permission
784  * bitmap for debugging or core dump.
785  */
786 static int ioperm_active(struct task_struct *target,
787 			 const struct user_regset *regset)
788 {
789 	return target->thread.io_bitmap_max / regset->size;
790 }
791 
792 static int ioperm_get(struct task_struct *target,
793 		      const struct user_regset *regset,
794 		      unsigned int pos, unsigned int count,
795 		      void *kbuf, void __user *ubuf)
796 {
797 	if (!target->thread.io_bitmap_ptr)
798 		return -ENXIO;
799 
800 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
801 				   target->thread.io_bitmap_ptr,
802 				   0, IO_BITMAP_BYTES);
803 }
804 
805 #ifdef CONFIG_X86_PTRACE_BTS
806 /*
807  * A branch trace store context.
808  *
809  * Contexts may only be installed by ptrace_bts_config() and only for
810  * ptraced tasks.
811  *
812  * Contexts are destroyed when the tracee is detached from the tracer.
813  * The actual destruction work requires interrupts enabled, so the
814  * work is deferred and will be scheduled during __ptrace_unlink().
815  *
816  * Contexts hold an additional task_struct reference on the traced
817  * task, as well as a reference on the tracer's mm.
818  *
819  * Ptrace already holds a task_struct for the duration of ptrace operations,
820  * but since destruction is deferred, it may be executed after both
821  * tracer and tracee exited.
822  */
823 struct bts_context {
824 	/* The branch trace handle. */
825 	struct bts_tracer	*tracer;
826 
827 	/* The buffer used to store the branch trace and its size. */
828 	void			*buffer;
829 	unsigned int		size;
830 
831 	/* The mm that paid for the above buffer. */
832 	struct mm_struct	*mm;
833 
834 	/* The task this context belongs to. */
835 	struct task_struct	*task;
836 
837 	/* The signal to send on a bts buffer overflow. */
838 	unsigned int		bts_ovfl_signal;
839 
840 	/* The work struct to destroy a context. */
841 	struct work_struct	work;
842 };
843 
844 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
845 {
846 	void *buffer = NULL;
847 	int err = -ENOMEM;
848 
849 	err = account_locked_memory(current->mm, current->signal->rlim, size);
850 	if (err < 0)
851 		return err;
852 
853 	buffer = kzalloc(size, GFP_KERNEL);
854 	if (!buffer)
855 		goto out_refund;
856 
857 	context->buffer = buffer;
858 	context->size = size;
859 	context->mm = get_task_mm(current);
860 
861 	return 0;
862 
863  out_refund:
864 	refund_locked_memory(current->mm, size);
865 	return err;
866 }
867 
868 static inline void free_bts_buffer(struct bts_context *context)
869 {
870 	if (!context->buffer)
871 		return;
872 
873 	kfree(context->buffer);
874 	context->buffer = NULL;
875 
876 	refund_locked_memory(context->mm, context->size);
877 	context->size = 0;
878 
879 	mmput(context->mm);
880 	context->mm = NULL;
881 }
882 
883 static void free_bts_context_work(struct work_struct *w)
884 {
885 	struct bts_context *context;
886 
887 	context = container_of(w, struct bts_context, work);
888 
889 	ds_release_bts(context->tracer);
890 	put_task_struct(context->task);
891 	free_bts_buffer(context);
892 	kfree(context);
893 }
894 
895 static inline void free_bts_context(struct bts_context *context)
896 {
897 	INIT_WORK(&context->work, free_bts_context_work);
898 	schedule_work(&context->work);
899 }
900 
901 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
902 {
903 	struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
904 	if (context) {
905 		context->task = task;
906 		task->bts = context;
907 
908 		get_task_struct(task);
909 	}
910 
911 	return context;
912 }
913 
914 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
915 				  struct bts_struct __user *out)
916 {
917 	struct bts_context *context;
918 	const struct bts_trace *trace;
919 	struct bts_struct bts;
920 	const unsigned char *at;
921 	int error;
922 
923 	context = child->bts;
924 	if (!context)
925 		return -ESRCH;
926 
927 	trace = ds_read_bts(context->tracer);
928 	if (!trace)
929 		return -ESRCH;
930 
931 	at = trace->ds.top - ((index + 1) * trace->ds.size);
932 	if ((void *)at < trace->ds.begin)
933 		at += (trace->ds.n * trace->ds.size);
934 
935 	if (!trace->read)
936 		return -EOPNOTSUPP;
937 
938 	error = trace->read(context->tracer, at, &bts);
939 	if (error < 0)
940 		return error;
941 
942 	if (copy_to_user(out, &bts, sizeof(bts)))
943 		return -EFAULT;
944 
945 	return sizeof(bts);
946 }
947 
948 static int ptrace_bts_drain(struct task_struct *child,
949 			    long size,
950 			    struct bts_struct __user *out)
951 {
952 	struct bts_context *context;
953 	const struct bts_trace *trace;
954 	const unsigned char *at;
955 	int error, drained = 0;
956 
957 	context = child->bts;
958 	if (!context)
959 		return -ESRCH;
960 
961 	trace = ds_read_bts(context->tracer);
962 	if (!trace)
963 		return -ESRCH;
964 
965 	if (!trace->read)
966 		return -EOPNOTSUPP;
967 
968 	if (size < (trace->ds.top - trace->ds.begin))
969 		return -EIO;
970 
971 	for (at = trace->ds.begin; (void *)at < trace->ds.top;
972 	     out++, drained++, at += trace->ds.size) {
973 		struct bts_struct bts;
974 
975 		error = trace->read(context->tracer, at, &bts);
976 		if (error < 0)
977 			return error;
978 
979 		if (copy_to_user(out, &bts, sizeof(bts)))
980 			return -EFAULT;
981 	}
982 
983 	memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
984 
985 	error = ds_reset_bts(context->tracer);
986 	if (error < 0)
987 		return error;
988 
989 	return drained;
990 }
991 
992 static int ptrace_bts_config(struct task_struct *child,
993 			     long cfg_size,
994 			     const struct ptrace_bts_config __user *ucfg)
995 {
996 	struct bts_context *context;
997 	struct ptrace_bts_config cfg;
998 	unsigned int flags = 0;
999 
1000 	if (cfg_size < sizeof(cfg))
1001 		return -EIO;
1002 
1003 	if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
1004 		return -EFAULT;
1005 
1006 	context = child->bts;
1007 	if (!context)
1008 		context = alloc_bts_context(child);
1009 	if (!context)
1010 		return -ENOMEM;
1011 
1012 	if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1013 		if (!cfg.signal)
1014 			return -EINVAL;
1015 
1016 		return -EOPNOTSUPP;
1017 		context->bts_ovfl_signal = cfg.signal;
1018 	}
1019 
1020 	ds_release_bts(context->tracer);
1021 	context->tracer = NULL;
1022 
1023 	if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
1024 		int err;
1025 
1026 		free_bts_buffer(context);
1027 		if (!cfg.size)
1028 			return 0;
1029 
1030 		err = alloc_bts_buffer(context, cfg.size);
1031 		if (err < 0)
1032 			return err;
1033 	}
1034 
1035 	if (cfg.flags & PTRACE_BTS_O_TRACE)
1036 		flags |= BTS_USER;
1037 
1038 	if (cfg.flags & PTRACE_BTS_O_SCHED)
1039 		flags |= BTS_TIMESTAMPS;
1040 
1041 	context->tracer =
1042 		ds_request_bts_task(child, context->buffer, context->size,
1043 				    NULL, (size_t)-1, flags);
1044 	if (unlikely(IS_ERR(context->tracer))) {
1045 		int error = PTR_ERR(context->tracer);
1046 
1047 		free_bts_buffer(context);
1048 		context->tracer = NULL;
1049 		return error;
1050 	}
1051 
1052 	return sizeof(cfg);
1053 }
1054 
1055 static int ptrace_bts_status(struct task_struct *child,
1056 			     long cfg_size,
1057 			     struct ptrace_bts_config __user *ucfg)
1058 {
1059 	struct bts_context *context;
1060 	const struct bts_trace *trace;
1061 	struct ptrace_bts_config cfg;
1062 
1063 	context = child->bts;
1064 	if (!context)
1065 		return -ESRCH;
1066 
1067 	if (cfg_size < sizeof(cfg))
1068 		return -EIO;
1069 
1070 	trace = ds_read_bts(context->tracer);
1071 	if (!trace)
1072 		return -ESRCH;
1073 
1074 	memset(&cfg, 0, sizeof(cfg));
1075 	cfg.size	= trace->ds.end - trace->ds.begin;
1076 	cfg.signal	= context->bts_ovfl_signal;
1077 	cfg.bts_size	= sizeof(struct bts_struct);
1078 
1079 	if (cfg.signal)
1080 		cfg.flags |= PTRACE_BTS_O_SIGNAL;
1081 
1082 	if (trace->ds.flags & BTS_USER)
1083 		cfg.flags |= PTRACE_BTS_O_TRACE;
1084 
1085 	if (trace->ds.flags & BTS_TIMESTAMPS)
1086 		cfg.flags |= PTRACE_BTS_O_SCHED;
1087 
1088 	if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1089 		return -EFAULT;
1090 
1091 	return sizeof(cfg);
1092 }
1093 
1094 static int ptrace_bts_clear(struct task_struct *child)
1095 {
1096 	struct bts_context *context;
1097 	const struct bts_trace *trace;
1098 
1099 	context = child->bts;
1100 	if (!context)
1101 		return -ESRCH;
1102 
1103 	trace = ds_read_bts(context->tracer);
1104 	if (!trace)
1105 		return -ESRCH;
1106 
1107 	memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
1108 
1109 	return ds_reset_bts(context->tracer);
1110 }
1111 
1112 static int ptrace_bts_size(struct task_struct *child)
1113 {
1114 	struct bts_context *context;
1115 	const struct bts_trace *trace;
1116 
1117 	context = child->bts;
1118 	if (!context)
1119 		return -ESRCH;
1120 
1121 	trace = ds_read_bts(context->tracer);
1122 	if (!trace)
1123 		return -ESRCH;
1124 
1125 	return (trace->ds.top - trace->ds.begin) / trace->ds.size;
1126 }
1127 
1128 /*
1129  * Called from __ptrace_unlink() after the child has been moved back
1130  * to its original parent.
1131  */
1132 void ptrace_bts_untrace(struct task_struct *child)
1133 {
1134 	if (unlikely(child->bts)) {
1135 		free_bts_context(child->bts);
1136 		child->bts = NULL;
1137 	}
1138 }
1139 #endif /* CONFIG_X86_PTRACE_BTS */
1140 
1141 /*
1142  * Called by kernel/ptrace.c when detaching..
1143  *
1144  * Make sure the single step bit is not set.
1145  */
1146 void ptrace_disable(struct task_struct *child)
1147 {
1148 	user_disable_single_step(child);
1149 #ifdef TIF_SYSCALL_EMU
1150 	clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
1151 #endif
1152 }
1153 
1154 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1155 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1156 #endif
1157 
1158 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1159 {
1160 	int ret;
1161 	unsigned long __user *datap = (unsigned long __user *)data;
1162 
1163 	switch (request) {
1164 	/* read the word at location addr in the USER area. */
1165 	case PTRACE_PEEKUSR: {
1166 		unsigned long tmp;
1167 
1168 		ret = -EIO;
1169 		if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1170 		    addr >= sizeof(struct user))
1171 			break;
1172 
1173 		tmp = 0;  /* Default return condition */
1174 		if (addr < sizeof(struct user_regs_struct))
1175 			tmp = getreg(child, addr);
1176 		else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1177 			 addr <= offsetof(struct user, u_debugreg[7])) {
1178 			addr -= offsetof(struct user, u_debugreg[0]);
1179 			tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1180 		}
1181 		ret = put_user(tmp, datap);
1182 		break;
1183 	}
1184 
1185 	case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1186 		ret = -EIO;
1187 		if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1188 		    addr >= sizeof(struct user))
1189 			break;
1190 
1191 		if (addr < sizeof(struct user_regs_struct))
1192 			ret = putreg(child, addr, data);
1193 		else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1194 			 addr <= offsetof(struct user, u_debugreg[7])) {
1195 			addr -= offsetof(struct user, u_debugreg[0]);
1196 			ret = ptrace_set_debugreg(child,
1197 						  addr / sizeof(data), data);
1198 		}
1199 		break;
1200 
1201 	case PTRACE_GETREGS:	/* Get all gp regs from the child. */
1202 		return copy_regset_to_user(child,
1203 					   task_user_regset_view(current),
1204 					   REGSET_GENERAL,
1205 					   0, sizeof(struct user_regs_struct),
1206 					   datap);
1207 
1208 	case PTRACE_SETREGS:	/* Set all gp regs in the child. */
1209 		return copy_regset_from_user(child,
1210 					     task_user_regset_view(current),
1211 					     REGSET_GENERAL,
1212 					     0, sizeof(struct user_regs_struct),
1213 					     datap);
1214 
1215 	case PTRACE_GETFPREGS:	/* Get the child FPU state. */
1216 		return copy_regset_to_user(child,
1217 					   task_user_regset_view(current),
1218 					   REGSET_FP,
1219 					   0, sizeof(struct user_i387_struct),
1220 					   datap);
1221 
1222 	case PTRACE_SETFPREGS:	/* Set the child FPU state. */
1223 		return copy_regset_from_user(child,
1224 					     task_user_regset_view(current),
1225 					     REGSET_FP,
1226 					     0, sizeof(struct user_i387_struct),
1227 					     datap);
1228 
1229 #ifdef CONFIG_X86_32
1230 	case PTRACE_GETFPXREGS:	/* Get the child extended FPU state. */
1231 		return copy_regset_to_user(child, &user_x86_32_view,
1232 					   REGSET_XFP,
1233 					   0, sizeof(struct user_fxsr_struct),
1234 					   datap) ? -EIO : 0;
1235 
1236 	case PTRACE_SETFPXREGS:	/* Set the child extended FPU state. */
1237 		return copy_regset_from_user(child, &user_x86_32_view,
1238 					     REGSET_XFP,
1239 					     0, sizeof(struct user_fxsr_struct),
1240 					     datap) ? -EIO : 0;
1241 #endif
1242 
1243 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1244 	case PTRACE_GET_THREAD_AREA:
1245 		if (addr < 0)
1246 			return -EIO;
1247 		ret = do_get_thread_area(child, addr,
1248 					 (struct user_desc __user *) data);
1249 		break;
1250 
1251 	case PTRACE_SET_THREAD_AREA:
1252 		if (addr < 0)
1253 			return -EIO;
1254 		ret = do_set_thread_area(child, addr,
1255 					 (struct user_desc __user *) data, 0);
1256 		break;
1257 #endif
1258 
1259 #ifdef CONFIG_X86_64
1260 		/* normal 64bit interface to access TLS data.
1261 		   Works just like arch_prctl, except that the arguments
1262 		   are reversed. */
1263 	case PTRACE_ARCH_PRCTL:
1264 		ret = do_arch_prctl(child, data, addr);
1265 		break;
1266 #endif
1267 
1268 	/*
1269 	 * These bits need more cooking - not enabled yet:
1270 	 */
1271 #ifdef CONFIG_X86_PTRACE_BTS
1272 	case PTRACE_BTS_CONFIG:
1273 		ret = ptrace_bts_config
1274 			(child, data, (struct ptrace_bts_config __user *)addr);
1275 		break;
1276 
1277 	case PTRACE_BTS_STATUS:
1278 		ret = ptrace_bts_status
1279 			(child, data, (struct ptrace_bts_config __user *)addr);
1280 		break;
1281 
1282 	case PTRACE_BTS_SIZE:
1283 		ret = ptrace_bts_size(child);
1284 		break;
1285 
1286 	case PTRACE_BTS_GET:
1287 		ret = ptrace_bts_read_record
1288 			(child, data, (struct bts_struct __user *) addr);
1289 		break;
1290 
1291 	case PTRACE_BTS_CLEAR:
1292 		ret = ptrace_bts_clear(child);
1293 		break;
1294 
1295 	case PTRACE_BTS_DRAIN:
1296 		ret = ptrace_bts_drain
1297 			(child, data, (struct bts_struct __user *) addr);
1298 		break;
1299 #endif /* CONFIG_X86_PTRACE_BTS */
1300 
1301 	default:
1302 		ret = ptrace_request(child, request, addr, data);
1303 		break;
1304 	}
1305 
1306 	return ret;
1307 }
1308 
1309 #ifdef CONFIG_IA32_EMULATION
1310 
1311 #include <linux/compat.h>
1312 #include <linux/syscalls.h>
1313 #include <asm/ia32.h>
1314 #include <asm/user32.h>
1315 
1316 #define R32(l,q)							\
1317 	case offsetof(struct user32, regs.l):				\
1318 		regs->q = value; break
1319 
1320 #define SEG32(rs)							\
1321 	case offsetof(struct user32, regs.rs):				\
1322 		return set_segment_reg(child,				\
1323 				       offsetof(struct user_regs_struct, rs), \
1324 				       value);				\
1325 		break
1326 
1327 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1328 {
1329 	struct pt_regs *regs = task_pt_regs(child);
1330 
1331 	switch (regno) {
1332 
1333 	SEG32(cs);
1334 	SEG32(ds);
1335 	SEG32(es);
1336 	SEG32(fs);
1337 	SEG32(gs);
1338 	SEG32(ss);
1339 
1340 	R32(ebx, bx);
1341 	R32(ecx, cx);
1342 	R32(edx, dx);
1343 	R32(edi, di);
1344 	R32(esi, si);
1345 	R32(ebp, bp);
1346 	R32(eax, ax);
1347 	R32(eip, ip);
1348 	R32(esp, sp);
1349 
1350 	case offsetof(struct user32, regs.orig_eax):
1351 		/*
1352 		 * A 32-bit debugger setting orig_eax means to restore
1353 		 * the state of the task restarting a 32-bit syscall.
1354 		 * Make sure we interpret the -ERESTART* codes correctly
1355 		 * in case the task is not actually still sitting at the
1356 		 * exit from a 32-bit syscall with TS_COMPAT still set.
1357 		 */
1358 		regs->orig_ax = value;
1359 		if (syscall_get_nr(child, regs) >= 0)
1360 			task_thread_info(child)->status |= TS_COMPAT;
1361 		break;
1362 
1363 	case offsetof(struct user32, regs.eflags):
1364 		return set_flags(child, value);
1365 
1366 	case offsetof(struct user32, u_debugreg[0]) ...
1367 		offsetof(struct user32, u_debugreg[7]):
1368 		regno -= offsetof(struct user32, u_debugreg[0]);
1369 		return ptrace_set_debugreg(child, regno / 4, value);
1370 
1371 	default:
1372 		if (regno > sizeof(struct user32) || (regno & 3))
1373 			return -EIO;
1374 
1375 		/*
1376 		 * Other dummy fields in the virtual user structure
1377 		 * are ignored
1378 		 */
1379 		break;
1380 	}
1381 	return 0;
1382 }
1383 
1384 #undef R32
1385 #undef SEG32
1386 
1387 #define R32(l,q)							\
1388 	case offsetof(struct user32, regs.l):				\
1389 		*val = regs->q; break
1390 
1391 #define SEG32(rs)							\
1392 	case offsetof(struct user32, regs.rs):				\
1393 		*val = get_segment_reg(child,				\
1394 				       offsetof(struct user_regs_struct, rs)); \
1395 		break
1396 
1397 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1398 {
1399 	struct pt_regs *regs = task_pt_regs(child);
1400 
1401 	switch (regno) {
1402 
1403 	SEG32(ds);
1404 	SEG32(es);
1405 	SEG32(fs);
1406 	SEG32(gs);
1407 
1408 	R32(cs, cs);
1409 	R32(ss, ss);
1410 	R32(ebx, bx);
1411 	R32(ecx, cx);
1412 	R32(edx, dx);
1413 	R32(edi, di);
1414 	R32(esi, si);
1415 	R32(ebp, bp);
1416 	R32(eax, ax);
1417 	R32(orig_eax, orig_ax);
1418 	R32(eip, ip);
1419 	R32(esp, sp);
1420 
1421 	case offsetof(struct user32, regs.eflags):
1422 		*val = get_flags(child);
1423 		break;
1424 
1425 	case offsetof(struct user32, u_debugreg[0]) ...
1426 		offsetof(struct user32, u_debugreg[7]):
1427 		regno -= offsetof(struct user32, u_debugreg[0]);
1428 		*val = ptrace_get_debugreg(child, regno / 4);
1429 		break;
1430 
1431 	default:
1432 		if (regno > sizeof(struct user32) || (regno & 3))
1433 			return -EIO;
1434 
1435 		/*
1436 		 * Other dummy fields in the virtual user structure
1437 		 * are ignored
1438 		 */
1439 		*val = 0;
1440 		break;
1441 	}
1442 	return 0;
1443 }
1444 
1445 #undef R32
1446 #undef SEG32
1447 
1448 static int genregs32_get(struct task_struct *target,
1449 			 const struct user_regset *regset,
1450 			 unsigned int pos, unsigned int count,
1451 			 void *kbuf, void __user *ubuf)
1452 {
1453 	if (kbuf) {
1454 		compat_ulong_t *k = kbuf;
1455 		while (count > 0) {
1456 			getreg32(target, pos, k++);
1457 			count -= sizeof(*k);
1458 			pos += sizeof(*k);
1459 		}
1460 	} else {
1461 		compat_ulong_t __user *u = ubuf;
1462 		while (count > 0) {
1463 			compat_ulong_t word;
1464 			getreg32(target, pos, &word);
1465 			if (__put_user(word, u++))
1466 				return -EFAULT;
1467 			count -= sizeof(*u);
1468 			pos += sizeof(*u);
1469 		}
1470 	}
1471 
1472 	return 0;
1473 }
1474 
1475 static int genregs32_set(struct task_struct *target,
1476 			 const struct user_regset *regset,
1477 			 unsigned int pos, unsigned int count,
1478 			 const void *kbuf, const void __user *ubuf)
1479 {
1480 	int ret = 0;
1481 	if (kbuf) {
1482 		const compat_ulong_t *k = kbuf;
1483 		while (count > 0 && !ret) {
1484 			ret = putreg32(target, pos, *k++);
1485 			count -= sizeof(*k);
1486 			pos += sizeof(*k);
1487 		}
1488 	} else {
1489 		const compat_ulong_t __user *u = ubuf;
1490 		while (count > 0 && !ret) {
1491 			compat_ulong_t word;
1492 			ret = __get_user(word, u++);
1493 			if (ret)
1494 				break;
1495 			ret = putreg32(target, pos, word);
1496 			count -= sizeof(*u);
1497 			pos += sizeof(*u);
1498 		}
1499 	}
1500 	return ret;
1501 }
1502 
1503 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1504 			compat_ulong_t caddr, compat_ulong_t cdata)
1505 {
1506 	unsigned long addr = caddr;
1507 	unsigned long data = cdata;
1508 	void __user *datap = compat_ptr(data);
1509 	int ret;
1510 	__u32 val;
1511 
1512 	switch (request) {
1513 	case PTRACE_PEEKUSR:
1514 		ret = getreg32(child, addr, &val);
1515 		if (ret == 0)
1516 			ret = put_user(val, (__u32 __user *)datap);
1517 		break;
1518 
1519 	case PTRACE_POKEUSR:
1520 		ret = putreg32(child, addr, data);
1521 		break;
1522 
1523 	case PTRACE_GETREGS:	/* Get all gp regs from the child. */
1524 		return copy_regset_to_user(child, &user_x86_32_view,
1525 					   REGSET_GENERAL,
1526 					   0, sizeof(struct user_regs_struct32),
1527 					   datap);
1528 
1529 	case PTRACE_SETREGS:	/* Set all gp regs in the child. */
1530 		return copy_regset_from_user(child, &user_x86_32_view,
1531 					     REGSET_GENERAL, 0,
1532 					     sizeof(struct user_regs_struct32),
1533 					     datap);
1534 
1535 	case PTRACE_GETFPREGS:	/* Get the child FPU state. */
1536 		return copy_regset_to_user(child, &user_x86_32_view,
1537 					   REGSET_FP, 0,
1538 					   sizeof(struct user_i387_ia32_struct),
1539 					   datap);
1540 
1541 	case PTRACE_SETFPREGS:	/* Set the child FPU state. */
1542 		return copy_regset_from_user(
1543 			child, &user_x86_32_view, REGSET_FP,
1544 			0, sizeof(struct user_i387_ia32_struct), datap);
1545 
1546 	case PTRACE_GETFPXREGS:	/* Get the child extended FPU state. */
1547 		return copy_regset_to_user(child, &user_x86_32_view,
1548 					   REGSET_XFP, 0,
1549 					   sizeof(struct user32_fxsr_struct),
1550 					   datap);
1551 
1552 	case PTRACE_SETFPXREGS:	/* Set the child extended FPU state. */
1553 		return copy_regset_from_user(child, &user_x86_32_view,
1554 					     REGSET_XFP, 0,
1555 					     sizeof(struct user32_fxsr_struct),
1556 					     datap);
1557 
1558 	case PTRACE_GET_THREAD_AREA:
1559 	case PTRACE_SET_THREAD_AREA:
1560 #ifdef CONFIG_X86_PTRACE_BTS
1561 	case PTRACE_BTS_CONFIG:
1562 	case PTRACE_BTS_STATUS:
1563 	case PTRACE_BTS_SIZE:
1564 	case PTRACE_BTS_GET:
1565 	case PTRACE_BTS_CLEAR:
1566 	case PTRACE_BTS_DRAIN:
1567 #endif /* CONFIG_X86_PTRACE_BTS */
1568 		return arch_ptrace(child, request, addr, data);
1569 
1570 	default:
1571 		return compat_ptrace_request(child, request, addr, data);
1572 	}
1573 
1574 	return ret;
1575 }
1576 
1577 #endif	/* CONFIG_IA32_EMULATION */
1578 
1579 #ifdef CONFIG_X86_64
1580 
1581 static const struct user_regset x86_64_regsets[] = {
1582 	[REGSET_GENERAL] = {
1583 		.core_note_type = NT_PRSTATUS,
1584 		.n = sizeof(struct user_regs_struct) / sizeof(long),
1585 		.size = sizeof(long), .align = sizeof(long),
1586 		.get = genregs_get, .set = genregs_set
1587 	},
1588 	[REGSET_FP] = {
1589 		.core_note_type = NT_PRFPREG,
1590 		.n = sizeof(struct user_i387_struct) / sizeof(long),
1591 		.size = sizeof(long), .align = sizeof(long),
1592 		.active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1593 	},
1594 	[REGSET_IOPERM64] = {
1595 		.core_note_type = NT_386_IOPERM,
1596 		.n = IO_BITMAP_LONGS,
1597 		.size = sizeof(long), .align = sizeof(long),
1598 		.active = ioperm_active, .get = ioperm_get
1599 	},
1600 };
1601 
1602 static const struct user_regset_view user_x86_64_view = {
1603 	.name = "x86_64", .e_machine = EM_X86_64,
1604 	.regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1605 };
1606 
1607 #else  /* CONFIG_X86_32 */
1608 
1609 #define user_regs_struct32	user_regs_struct
1610 #define genregs32_get		genregs_get
1611 #define genregs32_set		genregs_set
1612 
1613 #define user_i387_ia32_struct	user_i387_struct
1614 #define user32_fxsr_struct	user_fxsr_struct
1615 
1616 #endif	/* CONFIG_X86_64 */
1617 
1618 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1619 static const struct user_regset x86_32_regsets[] = {
1620 	[REGSET_GENERAL] = {
1621 		.core_note_type = NT_PRSTATUS,
1622 		.n = sizeof(struct user_regs_struct32) / sizeof(u32),
1623 		.size = sizeof(u32), .align = sizeof(u32),
1624 		.get = genregs32_get, .set = genregs32_set
1625 	},
1626 	[REGSET_FP] = {
1627 		.core_note_type = NT_PRFPREG,
1628 		.n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1629 		.size = sizeof(u32), .align = sizeof(u32),
1630 		.active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1631 	},
1632 	[REGSET_XFP] = {
1633 		.core_note_type = NT_PRXFPREG,
1634 		.n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1635 		.size = sizeof(u32), .align = sizeof(u32),
1636 		.active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1637 	},
1638 	[REGSET_TLS] = {
1639 		.core_note_type = NT_386_TLS,
1640 		.n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1641 		.size = sizeof(struct user_desc),
1642 		.align = sizeof(struct user_desc),
1643 		.active = regset_tls_active,
1644 		.get = regset_tls_get, .set = regset_tls_set
1645 	},
1646 	[REGSET_IOPERM32] = {
1647 		.core_note_type = NT_386_IOPERM,
1648 		.n = IO_BITMAP_BYTES / sizeof(u32),
1649 		.size = sizeof(u32), .align = sizeof(u32),
1650 		.active = ioperm_active, .get = ioperm_get
1651 	},
1652 };
1653 
1654 static const struct user_regset_view user_x86_32_view = {
1655 	.name = "i386", .e_machine = EM_386,
1656 	.regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1657 };
1658 #endif
1659 
1660 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1661 {
1662 #ifdef CONFIG_IA32_EMULATION
1663 	if (test_tsk_thread_flag(task, TIF_IA32))
1664 #endif
1665 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1666 		return &user_x86_32_view;
1667 #endif
1668 #ifdef CONFIG_X86_64
1669 	return &user_x86_64_view;
1670 #endif
1671 }
1672 
1673 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1674 					 int error_code, int si_code)
1675 {
1676 	struct siginfo info;
1677 
1678 	tsk->thread.trap_no = 1;
1679 	tsk->thread.error_code = error_code;
1680 
1681 	memset(&info, 0, sizeof(info));
1682 	info.si_signo = SIGTRAP;
1683 	info.si_code = si_code;
1684 
1685 	/* User-mode ip? */
1686 	info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1687 
1688 	/* Send us the fake SIGTRAP */
1689 	force_sig_info(SIGTRAP, &info, tsk);
1690 }
1691 
1692 
1693 #ifdef CONFIG_X86_32
1694 # define IS_IA32	1
1695 #elif defined CONFIG_IA32_EMULATION
1696 # define IS_IA32	is_compat_task()
1697 #else
1698 # define IS_IA32	0
1699 #endif
1700 
1701 /*
1702  * We must return the syscall number to actually look up in the table.
1703  * This can be -1L to skip running any syscall at all.
1704  */
1705 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1706 {
1707 	long ret = 0;
1708 
1709 	/*
1710 	 * If we stepped into a sysenter/syscall insn, it trapped in
1711 	 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1712 	 * If user-mode had set TF itself, then it's still clear from
1713 	 * do_debug() and we need to set it again to restore the user
1714 	 * state.  If we entered on the slow path, TF was already set.
1715 	 */
1716 	if (test_thread_flag(TIF_SINGLESTEP))
1717 		regs->flags |= X86_EFLAGS_TF;
1718 
1719 	/* do the secure computing check first */
1720 	secure_computing(regs->orig_ax);
1721 
1722 	if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1723 		ret = -1L;
1724 
1725 	if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1726 	    tracehook_report_syscall_entry(regs))
1727 		ret = -1L;
1728 
1729 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1730 		trace_sys_enter(regs, regs->orig_ax);
1731 
1732 	if (unlikely(current->audit_context)) {
1733 		if (IS_IA32)
1734 			audit_syscall_entry(AUDIT_ARCH_I386,
1735 					    regs->orig_ax,
1736 					    regs->bx, regs->cx,
1737 					    regs->dx, regs->si);
1738 #ifdef CONFIG_X86_64
1739 		else
1740 			audit_syscall_entry(AUDIT_ARCH_X86_64,
1741 					    regs->orig_ax,
1742 					    regs->di, regs->si,
1743 					    regs->dx, regs->r10);
1744 #endif
1745 	}
1746 
1747 	return ret ?: regs->orig_ax;
1748 }
1749 
1750 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1751 {
1752 	if (unlikely(current->audit_context))
1753 		audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1754 
1755 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1756 		trace_sys_exit(regs, regs->ax);
1757 
1758 	if (test_thread_flag(TIF_SYSCALL_TRACE))
1759 		tracehook_report_syscall_exit(regs, 0);
1760 
1761 	/*
1762 	 * If TIF_SYSCALL_EMU is set, we only get here because of
1763 	 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1764 	 * We already reported this syscall instruction in
1765 	 * syscall_trace_enter(), so don't do any more now.
1766 	 */
1767 	if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1768 		return;
1769 
1770 	/*
1771 	 * If we are single-stepping, synthesize a trap to follow the
1772 	 * system call instruction.
1773 	 */
1774 	if (test_thread_flag(TIF_SINGLESTEP) &&
1775 	    tracehook_consider_fatal_signal(current, SIGTRAP))
1776 		send_sigtrap(current, regs, 0, TRAP_BRKPT);
1777 }
1778