mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / x86 / kernel / ptrace.c
blobdc4d27000aa354aab8f172a06d3991600191a764
1 /* By Ross Biro 1/23/92 */
2 /*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
5 */
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/sched/task_stack.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/ptrace.h>
15 #include <linux/tracehook.h>
16 #include <linux/user.h>
17 #include <linux/elf.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/seccomp.h>
21 #include <linux/signal.h>
22 #include <linux/perf_event.h>
23 #include <linux/hw_breakpoint.h>
24 #include <linux/rcupdate.h>
25 #include <linux/export.h>
26 #include <linux/context_tracking.h>
27 #include <linux/nospec.h>
29 #include <linux/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/processor.h>
32 #include <asm/fpu/internal.h>
33 #include <asm/fpu/signal.h>
34 #include <asm/fpu/regset.h>
35 #include <asm/debugreg.h>
36 #include <asm/ldt.h>
37 #include <asm/desc.h>
38 #include <asm/prctl.h>
39 #include <asm/proto.h>
40 #include <asm/hw_breakpoint.h>
41 #include <asm/traps.h>
42 #include <asm/syscall.h>
43 #include <asm/mmu_context.h>
45 #include "tls.h"
47 enum x86_regset {
48 REGSET_GENERAL,
49 REGSET_FP,
50 REGSET_XFP,
51 REGSET_IOPERM64 = REGSET_XFP,
52 REGSET_XSTATE,
53 REGSET_TLS,
54 REGSET_IOPERM32,
57 struct pt_regs_offset {
58 const char *name;
59 int offset;
62 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
63 #define REG_OFFSET_END {.name = NULL, .offset = 0}
65 static const struct pt_regs_offset regoffset_table[] = {
66 #ifdef CONFIG_X86_64
67 REG_OFFSET_NAME(r15),
68 REG_OFFSET_NAME(r14),
69 REG_OFFSET_NAME(r13),
70 REG_OFFSET_NAME(r12),
71 REG_OFFSET_NAME(r11),
72 REG_OFFSET_NAME(r10),
73 REG_OFFSET_NAME(r9),
74 REG_OFFSET_NAME(r8),
75 #endif
76 REG_OFFSET_NAME(bx),
77 REG_OFFSET_NAME(cx),
78 REG_OFFSET_NAME(dx),
79 REG_OFFSET_NAME(si),
80 REG_OFFSET_NAME(di),
81 REG_OFFSET_NAME(bp),
82 REG_OFFSET_NAME(ax),
83 #ifdef CONFIG_X86_32
84 REG_OFFSET_NAME(ds),
85 REG_OFFSET_NAME(es),
86 REG_OFFSET_NAME(fs),
87 REG_OFFSET_NAME(gs),
88 #endif
89 REG_OFFSET_NAME(orig_ax),
90 REG_OFFSET_NAME(ip),
91 REG_OFFSET_NAME(cs),
92 REG_OFFSET_NAME(flags),
93 REG_OFFSET_NAME(sp),
94 REG_OFFSET_NAME(ss),
95 REG_OFFSET_END,
98 /**
99 * regs_query_register_offset() - query register offset from its name
100 * @name: the name of a register
102 * regs_query_register_offset() returns the offset of a register in struct
103 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
105 int regs_query_register_offset(const char *name)
107 const struct pt_regs_offset *roff;
108 for (roff = regoffset_table; roff->name != NULL; roff++)
109 if (!strcmp(roff->name, name))
110 return roff->offset;
111 return -EINVAL;
115 * regs_query_register_name() - query register name from its offset
116 * @offset: the offset of a register in struct pt_regs.
118 * regs_query_register_name() returns the name of a register from its
119 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
121 const char *regs_query_register_name(unsigned int offset)
123 const struct pt_regs_offset *roff;
124 for (roff = regoffset_table; roff->name != NULL; roff++)
125 if (roff->offset == offset)
126 return roff->name;
127 return NULL;
131 * does not yet catch signals sent when the child dies.
132 * in exit.c or in signal.c.
136 * Determines which flags the user has access to [1 = access, 0 = no access].
138 #define FLAG_MASK_32 ((unsigned long) \
139 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
140 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
141 X86_EFLAGS_SF | X86_EFLAGS_TF | \
142 X86_EFLAGS_DF | X86_EFLAGS_OF | \
143 X86_EFLAGS_RF | X86_EFLAGS_AC))
146 * Determines whether a value may be installed in a segment register.
148 static inline bool invalid_selector(u16 value)
150 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
153 #ifdef CONFIG_X86_32
155 #define FLAG_MASK FLAG_MASK_32
158 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
159 * when it traps. The previous stack will be directly underneath the saved
160 * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
162 * Now, if the stack is empty, '&regs->sp' is out of range. In this
163 * case we try to take the previous stack. To always return a non-null
164 * stack pointer we fall back to regs as stack if no previous stack
165 * exists.
167 * This is valid only for kernel mode traps.
169 unsigned long kernel_stack_pointer(struct pt_regs *regs)
171 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
172 unsigned long sp = (unsigned long)&regs->sp;
173 u32 *prev_esp;
175 if (context == (sp & ~(THREAD_SIZE - 1)))
176 return sp;
178 prev_esp = (u32 *)(context);
179 if (*prev_esp)
180 return (unsigned long)*prev_esp;
182 return (unsigned long)regs;
184 EXPORT_SYMBOL_GPL(kernel_stack_pointer);
186 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
188 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
189 return &regs->bx + (regno >> 2);
192 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
195 * Returning the value truncates it to 16 bits.
197 unsigned int retval;
198 if (offset != offsetof(struct user_regs_struct, gs))
199 retval = *pt_regs_access(task_pt_regs(task), offset);
200 else {
201 if (task == current)
202 retval = get_user_gs(task_pt_regs(task));
203 else
204 retval = task_user_gs(task);
206 return retval;
209 static int set_segment_reg(struct task_struct *task,
210 unsigned long offset, u16 value)
213 * The value argument was already truncated to 16 bits.
215 if (invalid_selector(value))
216 return -EIO;
219 * For %cs and %ss we cannot permit a null selector.
220 * We can permit a bogus selector as long as it has USER_RPL.
221 * Null selectors are fine for other segment registers, but
222 * we will never get back to user mode with invalid %cs or %ss
223 * and will take the trap in iret instead. Much code relies
224 * on user_mode() to distinguish a user trap frame (which can
225 * safely use invalid selectors) from a kernel trap frame.
227 switch (offset) {
228 case offsetof(struct user_regs_struct, cs):
229 case offsetof(struct user_regs_struct, ss):
230 if (unlikely(value == 0))
231 return -EIO;
233 default:
234 *pt_regs_access(task_pt_regs(task), offset) = value;
235 break;
237 case offsetof(struct user_regs_struct, gs):
238 if (task == current)
239 set_user_gs(task_pt_regs(task), value);
240 else
241 task_user_gs(task) = value;
244 return 0;
247 #else /* CONFIG_X86_64 */
249 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
251 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
253 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
254 return &regs->r15 + (offset / sizeof(regs->r15));
257 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
260 * Returning the value truncates it to 16 bits.
262 unsigned int seg;
264 switch (offset) {
265 case offsetof(struct user_regs_struct, fs):
266 if (task == current) {
267 /* Older gas can't assemble movq %?s,%r?? */
268 asm("movl %%fs,%0" : "=r" (seg));
269 return seg;
271 return task->thread.fsindex;
272 case offsetof(struct user_regs_struct, gs):
273 if (task == current) {
274 asm("movl %%gs,%0" : "=r" (seg));
275 return seg;
277 return task->thread.gsindex;
278 case offsetof(struct user_regs_struct, ds):
279 if (task == current) {
280 asm("movl %%ds,%0" : "=r" (seg));
281 return seg;
283 return task->thread.ds;
284 case offsetof(struct user_regs_struct, es):
285 if (task == current) {
286 asm("movl %%es,%0" : "=r" (seg));
287 return seg;
289 return task->thread.es;
291 case offsetof(struct user_regs_struct, cs):
292 case offsetof(struct user_regs_struct, ss):
293 break;
295 return *pt_regs_access(task_pt_regs(task), offset);
298 static int set_segment_reg(struct task_struct *task,
299 unsigned long offset, u16 value)
302 * The value argument was already truncated to 16 bits.
304 if (invalid_selector(value))
305 return -EIO;
307 switch (offset) {
308 case offsetof(struct user_regs_struct,fs):
309 task->thread.fsindex = value;
310 if (task == current)
311 loadsegment(fs, task->thread.fsindex);
312 break;
313 case offsetof(struct user_regs_struct,gs):
314 task->thread.gsindex = value;
315 if (task == current)
316 load_gs_index(task->thread.gsindex);
317 break;
318 case offsetof(struct user_regs_struct,ds):
319 task->thread.ds = value;
320 if (task == current)
321 loadsegment(ds, task->thread.ds);
322 break;
323 case offsetof(struct user_regs_struct,es):
324 task->thread.es = value;
325 if (task == current)
326 loadsegment(es, task->thread.es);
327 break;
330 * Can't actually change these in 64-bit mode.
332 case offsetof(struct user_regs_struct,cs):
333 if (unlikely(value == 0))
334 return -EIO;
335 task_pt_regs(task)->cs = value;
336 break;
337 case offsetof(struct user_regs_struct,ss):
338 if (unlikely(value == 0))
339 return -EIO;
340 task_pt_regs(task)->ss = value;
341 break;
344 return 0;
347 static unsigned long task_seg_base(struct task_struct *task,
348 unsigned short selector)
350 unsigned short idx = selector >> 3;
351 unsigned long base;
353 if (likely((selector & SEGMENT_TI_MASK) == 0)) {
354 if (unlikely(idx >= GDT_ENTRIES))
355 return 0;
358 * There are no user segments in the GDT with nonzero bases
359 * other than the TLS segments.
361 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
362 return 0;
364 idx -= GDT_ENTRY_TLS_MIN;
365 base = get_desc_base(&task->thread.tls_array[idx]);
366 } else {
367 #ifdef CONFIG_MODIFY_LDT_SYSCALL
368 struct ldt_struct *ldt;
371 * If performance here mattered, we could protect the LDT
372 * with RCU. This is a slow path, though, so we can just
373 * take the mutex.
375 mutex_lock(&task->mm->context.lock);
376 ldt = task->mm->context.ldt;
377 if (unlikely(!ldt || idx >= ldt->nr_entries))
378 base = 0;
379 else
380 base = get_desc_base(ldt->entries + idx);
381 mutex_unlock(&task->mm->context.lock);
382 #else
383 base = 0;
384 #endif
387 return base;
390 #endif /* CONFIG_X86_32 */
392 static unsigned long get_flags(struct task_struct *task)
394 unsigned long retval = task_pt_regs(task)->flags;
397 * If the debugger set TF, hide it from the readout.
399 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
400 retval &= ~X86_EFLAGS_TF;
402 return retval;
405 static int set_flags(struct task_struct *task, unsigned long value)
407 struct pt_regs *regs = task_pt_regs(task);
410 * If the user value contains TF, mark that
411 * it was not "us" (the debugger) that set it.
412 * If not, make sure it stays set if we had.
414 if (value & X86_EFLAGS_TF)
415 clear_tsk_thread_flag(task, TIF_FORCED_TF);
416 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
417 value |= X86_EFLAGS_TF;
419 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
421 return 0;
424 static int putreg(struct task_struct *child,
425 unsigned long offset, unsigned long value)
427 switch (offset) {
428 case offsetof(struct user_regs_struct, cs):
429 case offsetof(struct user_regs_struct, ds):
430 case offsetof(struct user_regs_struct, es):
431 case offsetof(struct user_regs_struct, fs):
432 case offsetof(struct user_regs_struct, gs):
433 case offsetof(struct user_regs_struct, ss):
434 return set_segment_reg(child, offset, value);
436 case offsetof(struct user_regs_struct, flags):
437 return set_flags(child, value);
439 #ifdef CONFIG_X86_64
440 case offsetof(struct user_regs_struct,fs_base):
441 if (value >= TASK_SIZE_MAX)
442 return -EIO;
444 * When changing the segment base, use do_arch_prctl_64
445 * to set either thread.fs or thread.fsindex and the
446 * corresponding GDT slot.
448 if (child->thread.fsbase != value)
449 return do_arch_prctl_64(child, ARCH_SET_FS, value);
450 return 0;
451 case offsetof(struct user_regs_struct,gs_base):
453 * Exactly the same here as the %fs handling above.
455 if (value >= TASK_SIZE_MAX)
456 return -EIO;
457 if (child->thread.gsbase != value)
458 return do_arch_prctl_64(child, ARCH_SET_GS, value);
459 return 0;
460 #endif
463 *pt_regs_access(task_pt_regs(child), offset) = value;
464 return 0;
467 static unsigned long getreg(struct task_struct *task, unsigned long offset)
469 switch (offset) {
470 case offsetof(struct user_regs_struct, cs):
471 case offsetof(struct user_regs_struct, ds):
472 case offsetof(struct user_regs_struct, es):
473 case offsetof(struct user_regs_struct, fs):
474 case offsetof(struct user_regs_struct, gs):
475 case offsetof(struct user_regs_struct, ss):
476 return get_segment_reg(task, offset);
478 case offsetof(struct user_regs_struct, flags):
479 return get_flags(task);
481 #ifdef CONFIG_X86_64
482 case offsetof(struct user_regs_struct, fs_base): {
483 if (task->thread.fsindex == 0)
484 return task->thread.fsbase;
485 else
486 return task_seg_base(task, task->thread.fsindex);
488 case offsetof(struct user_regs_struct, gs_base): {
489 if (task->thread.gsindex == 0)
490 return task->thread.gsbase;
491 else
492 return task_seg_base(task, task->thread.gsindex);
494 #endif
497 return *pt_regs_access(task_pt_regs(task), offset);
500 static int genregs_get(struct task_struct *target,
501 const struct user_regset *regset,
502 unsigned int pos, unsigned int count,
503 void *kbuf, void __user *ubuf)
505 if (kbuf) {
506 unsigned long *k = kbuf;
507 while (count >= sizeof(*k)) {
508 *k++ = getreg(target, pos);
509 count -= sizeof(*k);
510 pos += sizeof(*k);
512 } else {
513 unsigned long __user *u = ubuf;
514 while (count >= sizeof(*u)) {
515 if (__put_user(getreg(target, pos), u++))
516 return -EFAULT;
517 count -= sizeof(*u);
518 pos += sizeof(*u);
522 return 0;
525 static int genregs_set(struct task_struct *target,
526 const struct user_regset *regset,
527 unsigned int pos, unsigned int count,
528 const void *kbuf, const void __user *ubuf)
530 int ret = 0;
531 if (kbuf) {
532 const unsigned long *k = kbuf;
533 while (count >= sizeof(*k) && !ret) {
534 ret = putreg(target, pos, *k++);
535 count -= sizeof(*k);
536 pos += sizeof(*k);
538 } else {
539 const unsigned long __user *u = ubuf;
540 while (count >= sizeof(*u) && !ret) {
541 unsigned long word;
542 ret = __get_user(word, u++);
543 if (ret)
544 break;
545 ret = putreg(target, pos, word);
546 count -= sizeof(*u);
547 pos += sizeof(*u);
550 return ret;
553 static void ptrace_triggered(struct perf_event *bp,
554 struct perf_sample_data *data,
555 struct pt_regs *regs)
557 int i;
558 struct thread_struct *thread = &(current->thread);
561 * Store in the virtual DR6 register the fact that the breakpoint
562 * was hit so the thread's debugger will see it.
564 for (i = 0; i < HBP_NUM; i++) {
565 if (thread->ptrace_bps[i] == bp)
566 break;
569 thread->debugreg6 |= (DR_TRAP0 << i);
573 * Walk through every ptrace breakpoints for this thread and
574 * build the dr7 value on top of their attributes.
577 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
579 int i;
580 int dr7 = 0;
581 struct arch_hw_breakpoint *info;
583 for (i = 0; i < HBP_NUM; i++) {
584 if (bp[i] && !bp[i]->attr.disabled) {
585 info = counter_arch_bp(bp[i]);
586 dr7 |= encode_dr7(i, info->len, info->type);
590 return dr7;
593 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
594 int len, int type, bool disabled)
596 int err, bp_len, bp_type;
598 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
599 if (!err) {
600 attr->bp_len = bp_len;
601 attr->bp_type = bp_type;
602 attr->disabled = disabled;
605 return err;
608 static struct perf_event *
609 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
610 unsigned long addr, bool disabled)
612 struct perf_event_attr attr;
613 int err;
615 ptrace_breakpoint_init(&attr);
616 attr.bp_addr = addr;
618 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
619 if (err)
620 return ERR_PTR(err);
622 return register_user_hw_breakpoint(&attr, ptrace_triggered,
623 NULL, tsk);
626 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
627 int disabled)
629 struct perf_event_attr attr = bp->attr;
630 int err;
632 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
633 if (err)
634 return err;
636 return modify_user_hw_breakpoint(bp, &attr);
640 * Handle ptrace writes to debug register 7.
642 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
644 struct thread_struct *thread = &tsk->thread;
645 unsigned long old_dr7;
646 bool second_pass = false;
647 int i, rc, ret = 0;
649 data &= ~DR_CONTROL_RESERVED;
650 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
652 restore:
653 rc = 0;
654 for (i = 0; i < HBP_NUM; i++) {
655 unsigned len, type;
656 bool disabled = !decode_dr7(data, i, &len, &type);
657 struct perf_event *bp = thread->ptrace_bps[i];
659 if (!bp) {
660 if (disabled)
661 continue;
663 bp = ptrace_register_breakpoint(tsk,
664 len, type, 0, disabled);
665 if (IS_ERR(bp)) {
666 rc = PTR_ERR(bp);
667 break;
670 thread->ptrace_bps[i] = bp;
671 continue;
674 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
675 if (rc)
676 break;
679 /* Restore if the first pass failed, second_pass shouldn't fail. */
680 if (rc && !WARN_ON(second_pass)) {
681 ret = rc;
682 data = old_dr7;
683 second_pass = true;
684 goto restore;
687 return ret;
691 * Handle PTRACE_PEEKUSR calls for the debug register area.
693 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
695 struct thread_struct *thread = &tsk->thread;
696 unsigned long val = 0;
698 if (n < HBP_NUM) {
699 int index = array_index_nospec(n, HBP_NUM);
700 struct perf_event *bp = thread->ptrace_bps[index];
702 if (bp)
703 val = bp->hw.info.address;
704 } else if (n == 6) {
705 val = thread->debugreg6;
706 } else if (n == 7) {
707 val = thread->ptrace_dr7;
709 return val;
712 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
713 unsigned long addr)
715 struct thread_struct *t = &tsk->thread;
716 struct perf_event *bp = t->ptrace_bps[nr];
717 int err = 0;
719 if (!bp) {
721 * Put stub len and type to create an inactive but correct bp.
723 * CHECKME: the previous code returned -EIO if the addr wasn't
724 * a valid task virtual addr. The new one will return -EINVAL in
725 * this case.
726 * -EINVAL may be what we want for in-kernel breakpoints users,
727 * but -EIO looks better for ptrace, since we refuse a register
728 * writing for the user. And anyway this is the previous
729 * behaviour.
731 bp = ptrace_register_breakpoint(tsk,
732 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
733 addr, true);
734 if (IS_ERR(bp))
735 err = PTR_ERR(bp);
736 else
737 t->ptrace_bps[nr] = bp;
738 } else {
739 struct perf_event_attr attr = bp->attr;
741 attr.bp_addr = addr;
742 err = modify_user_hw_breakpoint(bp, &attr);
745 return err;
749 * Handle PTRACE_POKEUSR calls for the debug register area.
751 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
752 unsigned long val)
754 struct thread_struct *thread = &tsk->thread;
755 /* There are no DR4 or DR5 registers */
756 int rc = -EIO;
758 if (n < HBP_NUM) {
759 rc = ptrace_set_breakpoint_addr(tsk, n, val);
760 } else if (n == 6) {
761 thread->debugreg6 = val;
762 rc = 0;
763 } else if (n == 7) {
764 rc = ptrace_write_dr7(tsk, val);
765 if (!rc)
766 thread->ptrace_dr7 = val;
768 return rc;
772 * These access the current or another (stopped) task's io permission
773 * bitmap for debugging or core dump.
775 static int ioperm_active(struct task_struct *target,
776 const struct user_regset *regset)
778 return target->thread.io_bitmap_max / regset->size;
781 static int ioperm_get(struct task_struct *target,
782 const struct user_regset *regset,
783 unsigned int pos, unsigned int count,
784 void *kbuf, void __user *ubuf)
786 if (!target->thread.io_bitmap_ptr)
787 return -ENXIO;
789 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
790 target->thread.io_bitmap_ptr,
791 0, IO_BITMAP_BYTES);
795 * Called by kernel/ptrace.c when detaching..
797 * Make sure the single step bit is not set.
799 void ptrace_disable(struct task_struct *child)
801 user_disable_single_step(child);
802 #ifdef TIF_SYSCALL_EMU
803 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
804 #endif
807 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
808 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
809 #endif
811 long arch_ptrace(struct task_struct *child, long request,
812 unsigned long addr, unsigned long data)
814 int ret;
815 unsigned long __user *datap = (unsigned long __user *)data;
817 switch (request) {
818 /* read the word at location addr in the USER area. */
819 case PTRACE_PEEKUSR: {
820 unsigned long tmp;
822 ret = -EIO;
823 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
824 break;
826 tmp = 0; /* Default return condition */
827 if (addr < sizeof(struct user_regs_struct))
828 tmp = getreg(child, addr);
829 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
830 addr <= offsetof(struct user, u_debugreg[7])) {
831 addr -= offsetof(struct user, u_debugreg[0]);
832 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
834 ret = put_user(tmp, datap);
835 break;
838 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
839 ret = -EIO;
840 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
841 break;
843 if (addr < sizeof(struct user_regs_struct))
844 ret = putreg(child, addr, data);
845 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
846 addr <= offsetof(struct user, u_debugreg[7])) {
847 addr -= offsetof(struct user, u_debugreg[0]);
848 ret = ptrace_set_debugreg(child,
849 addr / sizeof(data), data);
851 break;
853 case PTRACE_GETREGS: /* Get all gp regs from the child. */
854 return copy_regset_to_user(child,
855 task_user_regset_view(current),
856 REGSET_GENERAL,
857 0, sizeof(struct user_regs_struct),
858 datap);
860 case PTRACE_SETREGS: /* Set all gp regs in the child. */
861 return copy_regset_from_user(child,
862 task_user_regset_view(current),
863 REGSET_GENERAL,
864 0, sizeof(struct user_regs_struct),
865 datap);
867 case PTRACE_GETFPREGS: /* Get the child FPU state. */
868 return copy_regset_to_user(child,
869 task_user_regset_view(current),
870 REGSET_FP,
871 0, sizeof(struct user_i387_struct),
872 datap);
874 case PTRACE_SETFPREGS: /* Set the child FPU state. */
875 return copy_regset_from_user(child,
876 task_user_regset_view(current),
877 REGSET_FP,
878 0, sizeof(struct user_i387_struct),
879 datap);
881 #ifdef CONFIG_X86_32
882 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
883 return copy_regset_to_user(child, &user_x86_32_view,
884 REGSET_XFP,
885 0, sizeof(struct user_fxsr_struct),
886 datap) ? -EIO : 0;
888 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
889 return copy_regset_from_user(child, &user_x86_32_view,
890 REGSET_XFP,
891 0, sizeof(struct user_fxsr_struct),
892 datap) ? -EIO : 0;
893 #endif
895 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
896 case PTRACE_GET_THREAD_AREA:
897 if ((int) addr < 0)
898 return -EIO;
899 ret = do_get_thread_area(child, addr,
900 (struct user_desc __user *)data);
901 break;
903 case PTRACE_SET_THREAD_AREA:
904 if ((int) addr < 0)
905 return -EIO;
906 ret = do_set_thread_area(child, addr,
907 (struct user_desc __user *)data, 0);
908 break;
909 #endif
911 #ifdef CONFIG_X86_64
912 /* normal 64bit interface to access TLS data.
913 Works just like arch_prctl, except that the arguments
914 are reversed. */
915 case PTRACE_ARCH_PRCTL:
916 ret = do_arch_prctl_64(child, data, addr);
917 break;
918 #endif
920 default:
921 ret = ptrace_request(child, request, addr, data);
922 break;
925 return ret;
928 #ifdef CONFIG_IA32_EMULATION
930 #include <linux/compat.h>
931 #include <linux/syscalls.h>
932 #include <asm/ia32.h>
933 #include <asm/user32.h>
935 #define R32(l,q) \
936 case offsetof(struct user32, regs.l): \
937 regs->q = value; break
939 #define SEG32(rs) \
940 case offsetof(struct user32, regs.rs): \
941 return set_segment_reg(child, \
942 offsetof(struct user_regs_struct, rs), \
943 value); \
944 break
946 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
948 struct pt_regs *regs = task_pt_regs(child);
950 switch (regno) {
952 SEG32(cs);
953 SEG32(ds);
954 SEG32(es);
955 SEG32(fs);
956 SEG32(gs);
957 SEG32(ss);
959 R32(ebx, bx);
960 R32(ecx, cx);
961 R32(edx, dx);
962 R32(edi, di);
963 R32(esi, si);
964 R32(ebp, bp);
965 R32(eax, ax);
966 R32(eip, ip);
967 R32(esp, sp);
969 case offsetof(struct user32, regs.orig_eax):
971 * Warning: bizarre corner case fixup here. A 32-bit
972 * debugger setting orig_eax to -1 wants to disable
973 * syscall restart. Make sure that the syscall
974 * restart code sign-extends orig_ax. Also make sure
975 * we interpret the -ERESTART* codes correctly if
976 * loaded into regs->ax in case the task is not
977 * actually still sitting at the exit from a 32-bit
978 * syscall with TS_COMPAT still set.
980 regs->orig_ax = value;
981 if (syscall_get_nr(child, regs) >= 0)
982 child->thread_info.status |= TS_I386_REGS_POKED;
983 break;
985 case offsetof(struct user32, regs.eflags):
986 return set_flags(child, value);
988 case offsetof(struct user32, u_debugreg[0]) ...
989 offsetof(struct user32, u_debugreg[7]):
990 regno -= offsetof(struct user32, u_debugreg[0]);
991 return ptrace_set_debugreg(child, regno / 4, value);
993 default:
994 if (regno > sizeof(struct user32) || (regno & 3))
995 return -EIO;
998 * Other dummy fields in the virtual user structure
999 * are ignored
1001 break;
1003 return 0;
1006 #undef R32
1007 #undef SEG32
1009 #define R32(l,q) \
1010 case offsetof(struct user32, regs.l): \
1011 *val = regs->q; break
1013 #define SEG32(rs) \
1014 case offsetof(struct user32, regs.rs): \
1015 *val = get_segment_reg(child, \
1016 offsetof(struct user_regs_struct, rs)); \
1017 break
1019 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1021 struct pt_regs *regs = task_pt_regs(child);
1023 switch (regno) {
1025 SEG32(ds);
1026 SEG32(es);
1027 SEG32(fs);
1028 SEG32(gs);
1030 R32(cs, cs);
1031 R32(ss, ss);
1032 R32(ebx, bx);
1033 R32(ecx, cx);
1034 R32(edx, dx);
1035 R32(edi, di);
1036 R32(esi, si);
1037 R32(ebp, bp);
1038 R32(eax, ax);
1039 R32(orig_eax, orig_ax);
1040 R32(eip, ip);
1041 R32(esp, sp);
1043 case offsetof(struct user32, regs.eflags):
1044 *val = get_flags(child);
1045 break;
1047 case offsetof(struct user32, u_debugreg[0]) ...
1048 offsetof(struct user32, u_debugreg[7]):
1049 regno -= offsetof(struct user32, u_debugreg[0]);
1050 *val = ptrace_get_debugreg(child, regno / 4);
1051 break;
1053 default:
1054 if (regno > sizeof(struct user32) || (regno & 3))
1055 return -EIO;
1058 * Other dummy fields in the virtual user structure
1059 * are ignored
1061 *val = 0;
1062 break;
1064 return 0;
1067 #undef R32
1068 #undef SEG32
1070 static int genregs32_get(struct task_struct *target,
1071 const struct user_regset *regset,
1072 unsigned int pos, unsigned int count,
1073 void *kbuf, void __user *ubuf)
1075 if (kbuf) {
1076 compat_ulong_t *k = kbuf;
1077 while (count >= sizeof(*k)) {
1078 getreg32(target, pos, k++);
1079 count -= sizeof(*k);
1080 pos += sizeof(*k);
1082 } else {
1083 compat_ulong_t __user *u = ubuf;
1084 while (count >= sizeof(*u)) {
1085 compat_ulong_t word;
1086 getreg32(target, pos, &word);
1087 if (__put_user(word, u++))
1088 return -EFAULT;
1089 count -= sizeof(*u);
1090 pos += sizeof(*u);
1094 return 0;
1097 static int genregs32_set(struct task_struct *target,
1098 const struct user_regset *regset,
1099 unsigned int pos, unsigned int count,
1100 const void *kbuf, const void __user *ubuf)
1102 int ret = 0;
1103 if (kbuf) {
1104 const compat_ulong_t *k = kbuf;
1105 while (count >= sizeof(*k) && !ret) {
1106 ret = putreg32(target, pos, *k++);
1107 count -= sizeof(*k);
1108 pos += sizeof(*k);
1110 } else {
1111 const compat_ulong_t __user *u = ubuf;
1112 while (count >= sizeof(*u) && !ret) {
1113 compat_ulong_t word;
1114 ret = __get_user(word, u++);
1115 if (ret)
1116 break;
1117 ret = putreg32(target, pos, word);
1118 count -= sizeof(*u);
1119 pos += sizeof(*u);
1122 return ret;
1125 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1126 compat_ulong_t caddr, compat_ulong_t cdata)
1128 unsigned long addr = caddr;
1129 unsigned long data = cdata;
1130 void __user *datap = compat_ptr(data);
1131 int ret;
1132 __u32 val;
1134 switch (request) {
1135 case PTRACE_PEEKUSR:
1136 ret = getreg32(child, addr, &val);
1137 if (ret == 0)
1138 ret = put_user(val, (__u32 __user *)datap);
1139 break;
1141 case PTRACE_POKEUSR:
1142 ret = putreg32(child, addr, data);
1143 break;
1145 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1146 return copy_regset_to_user(child, &user_x86_32_view,
1147 REGSET_GENERAL,
1148 0, sizeof(struct user_regs_struct32),
1149 datap);
1151 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1152 return copy_regset_from_user(child, &user_x86_32_view,
1153 REGSET_GENERAL, 0,
1154 sizeof(struct user_regs_struct32),
1155 datap);
1157 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1158 return copy_regset_to_user(child, &user_x86_32_view,
1159 REGSET_FP, 0,
1160 sizeof(struct user_i387_ia32_struct),
1161 datap);
1163 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1164 return copy_regset_from_user(
1165 child, &user_x86_32_view, REGSET_FP,
1166 0, sizeof(struct user_i387_ia32_struct), datap);
1168 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1169 return copy_regset_to_user(child, &user_x86_32_view,
1170 REGSET_XFP, 0,
1171 sizeof(struct user32_fxsr_struct),
1172 datap);
1174 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1175 return copy_regset_from_user(child, &user_x86_32_view,
1176 REGSET_XFP, 0,
1177 sizeof(struct user32_fxsr_struct),
1178 datap);
1180 case PTRACE_GET_THREAD_AREA:
1181 case PTRACE_SET_THREAD_AREA:
1182 return arch_ptrace(child, request, addr, data);
1184 default:
1185 return compat_ptrace_request(child, request, addr, data);
1188 return ret;
1190 #endif /* CONFIG_IA32_EMULATION */
1192 #ifdef CONFIG_X86_X32_ABI
1193 static long x32_arch_ptrace(struct task_struct *child,
1194 compat_long_t request, compat_ulong_t caddr,
1195 compat_ulong_t cdata)
1197 unsigned long addr = caddr;
1198 unsigned long data = cdata;
1199 void __user *datap = compat_ptr(data);
1200 int ret;
1202 switch (request) {
1203 /* Read 32bits at location addr in the USER area. Only allow
1204 to return the lower 32bits of segment and debug registers. */
1205 case PTRACE_PEEKUSR: {
1206 u32 tmp;
1208 ret = -EIO;
1209 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1210 addr < offsetof(struct user_regs_struct, cs))
1211 break;
1213 tmp = 0; /* Default return condition */
1214 if (addr < sizeof(struct user_regs_struct))
1215 tmp = getreg(child, addr);
1216 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1217 addr <= offsetof(struct user, u_debugreg[7])) {
1218 addr -= offsetof(struct user, u_debugreg[0]);
1219 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1221 ret = put_user(tmp, (__u32 __user *)datap);
1222 break;
1225 /* Write the word at location addr in the USER area. Only allow
1226 to update segment and debug registers with the upper 32bits
1227 zero-extended. */
1228 case PTRACE_POKEUSR:
1229 ret = -EIO;
1230 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1231 addr < offsetof(struct user_regs_struct, cs))
1232 break;
1234 if (addr < sizeof(struct user_regs_struct))
1235 ret = putreg(child, addr, data);
1236 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1237 addr <= offsetof(struct user, u_debugreg[7])) {
1238 addr -= offsetof(struct user, u_debugreg[0]);
1239 ret = ptrace_set_debugreg(child,
1240 addr / sizeof(data), data);
1242 break;
1244 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1245 return copy_regset_to_user(child,
1246 task_user_regset_view(current),
1247 REGSET_GENERAL,
1248 0, sizeof(struct user_regs_struct),
1249 datap);
1251 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1252 return copy_regset_from_user(child,
1253 task_user_regset_view(current),
1254 REGSET_GENERAL,
1255 0, sizeof(struct user_regs_struct),
1256 datap);
1258 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1259 return copy_regset_to_user(child,
1260 task_user_regset_view(current),
1261 REGSET_FP,
1262 0, sizeof(struct user_i387_struct),
1263 datap);
1265 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1266 return copy_regset_from_user(child,
1267 task_user_regset_view(current),
1268 REGSET_FP,
1269 0, sizeof(struct user_i387_struct),
1270 datap);
1272 default:
1273 return compat_ptrace_request(child, request, addr, data);
1276 return ret;
1278 #endif
1280 #ifdef CONFIG_COMPAT
1281 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1282 compat_ulong_t caddr, compat_ulong_t cdata)
1284 #ifdef CONFIG_X86_X32_ABI
1285 if (!in_ia32_syscall())
1286 return x32_arch_ptrace(child, request, caddr, cdata);
1287 #endif
1288 #ifdef CONFIG_IA32_EMULATION
1289 return ia32_arch_ptrace(child, request, caddr, cdata);
1290 #else
1291 return 0;
1292 #endif
1294 #endif /* CONFIG_COMPAT */
1296 #ifdef CONFIG_X86_64
1298 static struct user_regset x86_64_regsets[] __ro_after_init = {
1299 [REGSET_GENERAL] = {
1300 .core_note_type = NT_PRSTATUS,
1301 .n = sizeof(struct user_regs_struct) / sizeof(long),
1302 .size = sizeof(long), .align = sizeof(long),
1303 .get = genregs_get, .set = genregs_set
1305 [REGSET_FP] = {
1306 .core_note_type = NT_PRFPREG,
1307 .n = sizeof(struct user_i387_struct) / sizeof(long),
1308 .size = sizeof(long), .align = sizeof(long),
1309 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1311 [REGSET_XSTATE] = {
1312 .core_note_type = NT_X86_XSTATE,
1313 .size = sizeof(u64), .align = sizeof(u64),
1314 .active = xstateregs_active, .get = xstateregs_get,
1315 .set = xstateregs_set
1317 [REGSET_IOPERM64] = {
1318 .core_note_type = NT_386_IOPERM,
1319 .n = IO_BITMAP_LONGS,
1320 .size = sizeof(long), .align = sizeof(long),
1321 .active = ioperm_active, .get = ioperm_get
1325 static const struct user_regset_view user_x86_64_view = {
1326 .name = "x86_64", .e_machine = EM_X86_64,
1327 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1330 #else /* CONFIG_X86_32 */
1332 #define user_regs_struct32 user_regs_struct
1333 #define genregs32_get genregs_get
1334 #define genregs32_set genregs_set
1336 #endif /* CONFIG_X86_64 */
1338 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1339 static struct user_regset x86_32_regsets[] __ro_after_init = {
1340 [REGSET_GENERAL] = {
1341 .core_note_type = NT_PRSTATUS,
1342 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1343 .size = sizeof(u32), .align = sizeof(u32),
1344 .get = genregs32_get, .set = genregs32_set
1346 [REGSET_FP] = {
1347 .core_note_type = NT_PRFPREG,
1348 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1349 .size = sizeof(u32), .align = sizeof(u32),
1350 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1352 [REGSET_XFP] = {
1353 .core_note_type = NT_PRXFPREG,
1354 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1355 .size = sizeof(u32), .align = sizeof(u32),
1356 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1358 [REGSET_XSTATE] = {
1359 .core_note_type = NT_X86_XSTATE,
1360 .size = sizeof(u64), .align = sizeof(u64),
1361 .active = xstateregs_active, .get = xstateregs_get,
1362 .set = xstateregs_set
1364 [REGSET_TLS] = {
1365 .core_note_type = NT_386_TLS,
1366 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1367 .size = sizeof(struct user_desc),
1368 .align = sizeof(struct user_desc),
1369 .active = regset_tls_active,
1370 .get = regset_tls_get, .set = regset_tls_set
1372 [REGSET_IOPERM32] = {
1373 .core_note_type = NT_386_IOPERM,
1374 .n = IO_BITMAP_BYTES / sizeof(u32),
1375 .size = sizeof(u32), .align = sizeof(u32),
1376 .active = ioperm_active, .get = ioperm_get
1380 static const struct user_regset_view user_x86_32_view = {
1381 .name = "i386", .e_machine = EM_386,
1382 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1384 #endif
1387 * This represents bytes 464..511 in the memory layout exported through
1388 * the REGSET_XSTATE interface.
1390 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1392 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1394 #ifdef CONFIG_X86_64
1395 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1396 #endif
1397 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1398 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1399 #endif
1400 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1403 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1405 #ifdef CONFIG_IA32_EMULATION
1406 if (!user_64bit_mode(task_pt_regs(task)))
1407 #endif
1408 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1409 return &user_x86_32_view;
1410 #endif
1411 #ifdef CONFIG_X86_64
1412 return &user_x86_64_view;
1413 #endif
1416 static void fill_sigtrap_info(struct task_struct *tsk,
1417 struct pt_regs *regs,
1418 int error_code, int si_code,
1419 struct siginfo *info)
1421 tsk->thread.trap_nr = X86_TRAP_DB;
1422 tsk->thread.error_code = error_code;
1424 memset(info, 0, sizeof(*info));
1425 info->si_signo = SIGTRAP;
1426 info->si_code = si_code;
1427 info->si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
1430 void user_single_step_siginfo(struct task_struct *tsk,
1431 struct pt_regs *regs,
1432 struct siginfo *info)
1434 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1437 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1438 int error_code, int si_code)
1440 struct siginfo info;
1442 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
1443 /* Send us the fake SIGTRAP */
1444 force_sig_info(SIGTRAP, &info, tsk);