hugetlb: introduce generic version of hugetlb_free_pgd_range
[linux/fpc-iii.git] / arch / x86 / kernel / ptrace.c
blobffae9b9740fdf3a53619f03c4261be6711d7e7a7
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>
28 #include <linux/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/processor.h>
31 #include <asm/fpu/internal.h>
32 #include <asm/fpu/signal.h>
33 #include <asm/fpu/regset.h>
34 #include <asm/debugreg.h>
35 #include <asm/ldt.h>
36 #include <asm/desc.h>
37 #include <asm/prctl.h>
38 #include <asm/proto.h>
39 #include <asm/hw_breakpoint.h>
40 #include <asm/traps.h>
41 #include <asm/syscall.h>
42 #include <asm/fsgsbase.h>
44 #include "tls.h"
46 enum x86_regset {
47 REGSET_GENERAL,
48 REGSET_FP,
49 REGSET_XFP,
50 REGSET_IOPERM64 = REGSET_XFP,
51 REGSET_XSTATE,
52 REGSET_TLS,
53 REGSET_IOPERM32,
56 struct pt_regs_offset {
57 const char *name;
58 int offset;
61 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
62 #define REG_OFFSET_END {.name = NULL, .offset = 0}
64 static const struct pt_regs_offset regoffset_table[] = {
65 #ifdef CONFIG_X86_64
66 REG_OFFSET_NAME(r15),
67 REG_OFFSET_NAME(r14),
68 REG_OFFSET_NAME(r13),
69 REG_OFFSET_NAME(r12),
70 REG_OFFSET_NAME(r11),
71 REG_OFFSET_NAME(r10),
72 REG_OFFSET_NAME(r9),
73 REG_OFFSET_NAME(r8),
74 #endif
75 REG_OFFSET_NAME(bx),
76 REG_OFFSET_NAME(cx),
77 REG_OFFSET_NAME(dx),
78 REG_OFFSET_NAME(si),
79 REG_OFFSET_NAME(di),
80 REG_OFFSET_NAME(bp),
81 REG_OFFSET_NAME(ax),
82 #ifdef CONFIG_X86_32
83 REG_OFFSET_NAME(ds),
84 REG_OFFSET_NAME(es),
85 REG_OFFSET_NAME(fs),
86 REG_OFFSET_NAME(gs),
87 #endif
88 REG_OFFSET_NAME(orig_ax),
89 REG_OFFSET_NAME(ip),
90 REG_OFFSET_NAME(cs),
91 REG_OFFSET_NAME(flags),
92 REG_OFFSET_NAME(sp),
93 REG_OFFSET_NAME(ss),
94 REG_OFFSET_END,
97 /**
98 * regs_query_register_offset() - query register offset from its name
99 * @name: the name of a register
101 * regs_query_register_offset() returns the offset of a register in struct
102 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
104 int regs_query_register_offset(const char *name)
106 const struct pt_regs_offset *roff;
107 for (roff = regoffset_table; roff->name != NULL; roff++)
108 if (!strcmp(roff->name, name))
109 return roff->offset;
110 return -EINVAL;
114 * regs_query_register_name() - query register name from its offset
115 * @offset: the offset of a register in struct pt_regs.
117 * regs_query_register_name() returns the name of a register from its
118 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
120 const char *regs_query_register_name(unsigned int offset)
122 const struct pt_regs_offset *roff;
123 for (roff = regoffset_table; roff->name != NULL; roff++)
124 if (roff->offset == offset)
125 return roff->name;
126 return NULL;
130 * does not yet catch signals sent when the child dies.
131 * in exit.c or in signal.c.
135 * Determines which flags the user has access to [1 = access, 0 = no access].
137 #define FLAG_MASK_32 ((unsigned long) \
138 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
139 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
140 X86_EFLAGS_SF | X86_EFLAGS_TF | \
141 X86_EFLAGS_DF | X86_EFLAGS_OF | \
142 X86_EFLAGS_RF | X86_EFLAGS_AC))
145 * Determines whether a value may be installed in a segment register.
147 static inline bool invalid_selector(u16 value)
149 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
152 #ifdef CONFIG_X86_32
154 #define FLAG_MASK FLAG_MASK_32
157 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
158 * when it traps. The previous stack will be directly underneath the saved
159 * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
161 * Now, if the stack is empty, '&regs->sp' is out of range. In this
162 * case we try to take the previous stack. To always return a non-null
163 * stack pointer we fall back to regs as stack if no previous stack
164 * exists.
166 * This is valid only for kernel mode traps.
168 unsigned long kernel_stack_pointer(struct pt_regs *regs)
170 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
171 unsigned long sp = (unsigned long)&regs->sp;
172 u32 *prev_esp;
174 if (context == (sp & ~(THREAD_SIZE - 1)))
175 return sp;
177 prev_esp = (u32 *)(context);
178 if (*prev_esp)
179 return (unsigned long)*prev_esp;
181 return (unsigned long)regs;
183 EXPORT_SYMBOL_GPL(kernel_stack_pointer);
185 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
187 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
188 return &regs->bx + (regno >> 2);
191 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
194 * Returning the value truncates it to 16 bits.
196 unsigned int retval;
197 if (offset != offsetof(struct user_regs_struct, gs))
198 retval = *pt_regs_access(task_pt_regs(task), offset);
199 else {
200 if (task == current)
201 retval = get_user_gs(task_pt_regs(task));
202 else
203 retval = task_user_gs(task);
205 return retval;
208 static int set_segment_reg(struct task_struct *task,
209 unsigned long offset, u16 value)
212 * The value argument was already truncated to 16 bits.
214 if (invalid_selector(value))
215 return -EIO;
218 * For %cs and %ss we cannot permit a null selector.
219 * We can permit a bogus selector as long as it has USER_RPL.
220 * Null selectors are fine for other segment registers, but
221 * we will never get back to user mode with invalid %cs or %ss
222 * and will take the trap in iret instead. Much code relies
223 * on user_mode() to distinguish a user trap frame (which can
224 * safely use invalid selectors) from a kernel trap frame.
226 switch (offset) {
227 case offsetof(struct user_regs_struct, cs):
228 case offsetof(struct user_regs_struct, ss):
229 if (unlikely(value == 0))
230 return -EIO;
232 default:
233 *pt_regs_access(task_pt_regs(task), offset) = value;
234 break;
236 case offsetof(struct user_regs_struct, gs):
237 if (task == current)
238 set_user_gs(task_pt_regs(task), value);
239 else
240 task_user_gs(task) = value;
243 return 0;
246 #else /* CONFIG_X86_64 */
248 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
250 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
252 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
253 return &regs->r15 + (offset / sizeof(regs->r15));
256 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
259 * Returning the value truncates it to 16 bits.
261 unsigned int seg;
263 switch (offset) {
264 case offsetof(struct user_regs_struct, fs):
265 if (task == current) {
266 /* Older gas can't assemble movq %?s,%r?? */
267 asm("movl %%fs,%0" : "=r" (seg));
268 return seg;
270 return task->thread.fsindex;
271 case offsetof(struct user_regs_struct, gs):
272 if (task == current) {
273 asm("movl %%gs,%0" : "=r" (seg));
274 return seg;
276 return task->thread.gsindex;
277 case offsetof(struct user_regs_struct, ds):
278 if (task == current) {
279 asm("movl %%ds,%0" : "=r" (seg));
280 return seg;
282 return task->thread.ds;
283 case offsetof(struct user_regs_struct, es):
284 if (task == current) {
285 asm("movl %%es,%0" : "=r" (seg));
286 return seg;
288 return task->thread.es;
290 case offsetof(struct user_regs_struct, cs):
291 case offsetof(struct user_regs_struct, ss):
292 break;
294 return *pt_regs_access(task_pt_regs(task), offset);
297 static int set_segment_reg(struct task_struct *task,
298 unsigned long offset, u16 value)
301 * The value argument was already truncated to 16 bits.
303 if (invalid_selector(value))
304 return -EIO;
306 switch (offset) {
307 case offsetof(struct user_regs_struct,fs):
308 task->thread.fsindex = value;
309 if (task == current)
310 loadsegment(fs, task->thread.fsindex);
311 break;
312 case offsetof(struct user_regs_struct,gs):
313 task->thread.gsindex = value;
314 if (task == current)
315 load_gs_index(task->thread.gsindex);
316 break;
317 case offsetof(struct user_regs_struct,ds):
318 task->thread.ds = value;
319 if (task == current)
320 loadsegment(ds, task->thread.ds);
321 break;
322 case offsetof(struct user_regs_struct,es):
323 task->thread.es = value;
324 if (task == current)
325 loadsegment(es, task->thread.es);
326 break;
329 * Can't actually change these in 64-bit mode.
331 case offsetof(struct user_regs_struct,cs):
332 if (unlikely(value == 0))
333 return -EIO;
334 task_pt_regs(task)->cs = value;
335 break;
336 case offsetof(struct user_regs_struct,ss):
337 if (unlikely(value == 0))
338 return -EIO;
339 task_pt_regs(task)->ss = value;
340 break;
343 return 0;
346 #endif /* CONFIG_X86_32 */
348 static unsigned long get_flags(struct task_struct *task)
350 unsigned long retval = task_pt_regs(task)->flags;
353 * If the debugger set TF, hide it from the readout.
355 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
356 retval &= ~X86_EFLAGS_TF;
358 return retval;
361 static int set_flags(struct task_struct *task, unsigned long value)
363 struct pt_regs *regs = task_pt_regs(task);
366 * If the user value contains TF, mark that
367 * it was not "us" (the debugger) that set it.
368 * If not, make sure it stays set if we had.
370 if (value & X86_EFLAGS_TF)
371 clear_tsk_thread_flag(task, TIF_FORCED_TF);
372 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
373 value |= X86_EFLAGS_TF;
375 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
377 return 0;
380 static int putreg(struct task_struct *child,
381 unsigned long offset, unsigned long value)
383 switch (offset) {
384 case offsetof(struct user_regs_struct, cs):
385 case offsetof(struct user_regs_struct, ds):
386 case offsetof(struct user_regs_struct, es):
387 case offsetof(struct user_regs_struct, fs):
388 case offsetof(struct user_regs_struct, gs):
389 case offsetof(struct user_regs_struct, ss):
390 return set_segment_reg(child, offset, value);
392 case offsetof(struct user_regs_struct, flags):
393 return set_flags(child, value);
395 #ifdef CONFIG_X86_64
396 case offsetof(struct user_regs_struct,fs_base):
397 if (value >= TASK_SIZE_MAX)
398 return -EIO;
400 * When changing the FS base, use the same
401 * mechanism as for do_arch_prctl_64().
403 if (child->thread.fsbase != value)
404 return x86_fsbase_write_task(child, value);
405 return 0;
406 case offsetof(struct user_regs_struct,gs_base):
408 * Exactly the same here as the %fs handling above.
410 if (value >= TASK_SIZE_MAX)
411 return -EIO;
412 if (child->thread.gsbase != value)
413 return x86_gsbase_write_task(child, value);
414 return 0;
415 #endif
418 *pt_regs_access(task_pt_regs(child), offset) = value;
419 return 0;
422 static unsigned long getreg(struct task_struct *task, unsigned long offset)
424 switch (offset) {
425 case offsetof(struct user_regs_struct, cs):
426 case offsetof(struct user_regs_struct, ds):
427 case offsetof(struct user_regs_struct, es):
428 case offsetof(struct user_regs_struct, fs):
429 case offsetof(struct user_regs_struct, gs):
430 case offsetof(struct user_regs_struct, ss):
431 return get_segment_reg(task, offset);
433 case offsetof(struct user_regs_struct, flags):
434 return get_flags(task);
436 #ifdef CONFIG_X86_64
437 case offsetof(struct user_regs_struct, fs_base):
438 return x86_fsbase_read_task(task);
439 case offsetof(struct user_regs_struct, gs_base):
440 return x86_gsbase_read_task(task);
441 #endif
444 return *pt_regs_access(task_pt_regs(task), offset);
447 static int genregs_get(struct task_struct *target,
448 const struct user_regset *regset,
449 unsigned int pos, unsigned int count,
450 void *kbuf, void __user *ubuf)
452 if (kbuf) {
453 unsigned long *k = kbuf;
454 while (count >= sizeof(*k)) {
455 *k++ = getreg(target, pos);
456 count -= sizeof(*k);
457 pos += sizeof(*k);
459 } else {
460 unsigned long __user *u = ubuf;
461 while (count >= sizeof(*u)) {
462 if (__put_user(getreg(target, pos), u++))
463 return -EFAULT;
464 count -= sizeof(*u);
465 pos += sizeof(*u);
469 return 0;
472 static int genregs_set(struct task_struct *target,
473 const struct user_regset *regset,
474 unsigned int pos, unsigned int count,
475 const void *kbuf, const void __user *ubuf)
477 int ret = 0;
478 if (kbuf) {
479 const unsigned long *k = kbuf;
480 while (count >= sizeof(*k) && !ret) {
481 ret = putreg(target, pos, *k++);
482 count -= sizeof(*k);
483 pos += sizeof(*k);
485 } else {
486 const unsigned long __user *u = ubuf;
487 while (count >= sizeof(*u) && !ret) {
488 unsigned long word;
489 ret = __get_user(word, u++);
490 if (ret)
491 break;
492 ret = putreg(target, pos, word);
493 count -= sizeof(*u);
494 pos += sizeof(*u);
497 return ret;
500 static void ptrace_triggered(struct perf_event *bp,
501 struct perf_sample_data *data,
502 struct pt_regs *regs)
504 int i;
505 struct thread_struct *thread = &(current->thread);
508 * Store in the virtual DR6 register the fact that the breakpoint
509 * was hit so the thread's debugger will see it.
511 for (i = 0; i < HBP_NUM; i++) {
512 if (thread->ptrace_bps[i] == bp)
513 break;
516 thread->debugreg6 |= (DR_TRAP0 << i);
520 * Walk through every ptrace breakpoints for this thread and
521 * build the dr7 value on top of their attributes.
524 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
526 int i;
527 int dr7 = 0;
528 struct arch_hw_breakpoint *info;
530 for (i = 0; i < HBP_NUM; i++) {
531 if (bp[i] && !bp[i]->attr.disabled) {
532 info = counter_arch_bp(bp[i]);
533 dr7 |= encode_dr7(i, info->len, info->type);
537 return dr7;
540 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
541 int len, int type, bool disabled)
543 int err, bp_len, bp_type;
545 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
546 if (!err) {
547 attr->bp_len = bp_len;
548 attr->bp_type = bp_type;
549 attr->disabled = disabled;
552 return err;
555 static struct perf_event *
556 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
557 unsigned long addr, bool disabled)
559 struct perf_event_attr attr;
560 int err;
562 ptrace_breakpoint_init(&attr);
563 attr.bp_addr = addr;
565 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
566 if (err)
567 return ERR_PTR(err);
569 return register_user_hw_breakpoint(&attr, ptrace_triggered,
570 NULL, tsk);
573 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
574 int disabled)
576 struct perf_event_attr attr = bp->attr;
577 int err;
579 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
580 if (err)
581 return err;
583 return modify_user_hw_breakpoint(bp, &attr);
587 * Handle ptrace writes to debug register 7.
589 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
591 struct thread_struct *thread = &tsk->thread;
592 unsigned long old_dr7;
593 bool second_pass = false;
594 int i, rc, ret = 0;
596 data &= ~DR_CONTROL_RESERVED;
597 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
599 restore:
600 rc = 0;
601 for (i = 0; i < HBP_NUM; i++) {
602 unsigned len, type;
603 bool disabled = !decode_dr7(data, i, &len, &type);
604 struct perf_event *bp = thread->ptrace_bps[i];
606 if (!bp) {
607 if (disabled)
608 continue;
610 bp = ptrace_register_breakpoint(tsk,
611 len, type, 0, disabled);
612 if (IS_ERR(bp)) {
613 rc = PTR_ERR(bp);
614 break;
617 thread->ptrace_bps[i] = bp;
618 continue;
621 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
622 if (rc)
623 break;
626 /* Restore if the first pass failed, second_pass shouldn't fail. */
627 if (rc && !WARN_ON(second_pass)) {
628 ret = rc;
629 data = old_dr7;
630 second_pass = true;
631 goto restore;
634 return ret;
638 * Handle PTRACE_PEEKUSR calls for the debug register area.
640 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
642 struct thread_struct *thread = &tsk->thread;
643 unsigned long val = 0;
645 if (n < HBP_NUM) {
646 struct perf_event *bp = thread->ptrace_bps[n];
648 if (bp)
649 val = bp->hw.info.address;
650 } else if (n == 6) {
651 val = thread->debugreg6;
652 } else if (n == 7) {
653 val = thread->ptrace_dr7;
655 return val;
658 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
659 unsigned long addr)
661 struct thread_struct *t = &tsk->thread;
662 struct perf_event *bp = t->ptrace_bps[nr];
663 int err = 0;
665 if (!bp) {
667 * Put stub len and type to create an inactive but correct bp.
669 * CHECKME: the previous code returned -EIO if the addr wasn't
670 * a valid task virtual addr. The new one will return -EINVAL in
671 * this case.
672 * -EINVAL may be what we want for in-kernel breakpoints users,
673 * but -EIO looks better for ptrace, since we refuse a register
674 * writing for the user. And anyway this is the previous
675 * behaviour.
677 bp = ptrace_register_breakpoint(tsk,
678 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
679 addr, true);
680 if (IS_ERR(bp))
681 err = PTR_ERR(bp);
682 else
683 t->ptrace_bps[nr] = bp;
684 } else {
685 struct perf_event_attr attr = bp->attr;
687 attr.bp_addr = addr;
688 err = modify_user_hw_breakpoint(bp, &attr);
691 return err;
695 * Handle PTRACE_POKEUSR calls for the debug register area.
697 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
698 unsigned long val)
700 struct thread_struct *thread = &tsk->thread;
701 /* There are no DR4 or DR5 registers */
702 int rc = -EIO;
704 if (n < HBP_NUM) {
705 rc = ptrace_set_breakpoint_addr(tsk, n, val);
706 } else if (n == 6) {
707 thread->debugreg6 = val;
708 rc = 0;
709 } else if (n == 7) {
710 rc = ptrace_write_dr7(tsk, val);
711 if (!rc)
712 thread->ptrace_dr7 = val;
714 return rc;
718 * These access the current or another (stopped) task's io permission
719 * bitmap for debugging or core dump.
721 static int ioperm_active(struct task_struct *target,
722 const struct user_regset *regset)
724 return target->thread.io_bitmap_max / regset->size;
727 static int ioperm_get(struct task_struct *target,
728 const struct user_regset *regset,
729 unsigned int pos, unsigned int count,
730 void *kbuf, void __user *ubuf)
732 if (!target->thread.io_bitmap_ptr)
733 return -ENXIO;
735 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
736 target->thread.io_bitmap_ptr,
737 0, IO_BITMAP_BYTES);
741 * Called by kernel/ptrace.c when detaching..
743 * Make sure the single step bit is not set.
745 void ptrace_disable(struct task_struct *child)
747 user_disable_single_step(child);
748 #ifdef TIF_SYSCALL_EMU
749 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
750 #endif
753 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
754 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
755 #endif
757 long arch_ptrace(struct task_struct *child, long request,
758 unsigned long addr, unsigned long data)
760 int ret;
761 unsigned long __user *datap = (unsigned long __user *)data;
763 switch (request) {
764 /* read the word at location addr in the USER area. */
765 case PTRACE_PEEKUSR: {
766 unsigned long tmp;
768 ret = -EIO;
769 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
770 break;
772 tmp = 0; /* Default return condition */
773 if (addr < sizeof(struct user_regs_struct))
774 tmp = getreg(child, addr);
775 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
776 addr <= offsetof(struct user, u_debugreg[7])) {
777 addr -= offsetof(struct user, u_debugreg[0]);
778 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
780 ret = put_user(tmp, datap);
781 break;
784 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
785 ret = -EIO;
786 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
787 break;
789 if (addr < sizeof(struct user_regs_struct))
790 ret = putreg(child, addr, data);
791 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
792 addr <= offsetof(struct user, u_debugreg[7])) {
793 addr -= offsetof(struct user, u_debugreg[0]);
794 ret = ptrace_set_debugreg(child,
795 addr / sizeof(data), data);
797 break;
799 case PTRACE_GETREGS: /* Get all gp regs from the child. */
800 return copy_regset_to_user(child,
801 task_user_regset_view(current),
802 REGSET_GENERAL,
803 0, sizeof(struct user_regs_struct),
804 datap);
806 case PTRACE_SETREGS: /* Set all gp regs in the child. */
807 return copy_regset_from_user(child,
808 task_user_regset_view(current),
809 REGSET_GENERAL,
810 0, sizeof(struct user_regs_struct),
811 datap);
813 case PTRACE_GETFPREGS: /* Get the child FPU state. */
814 return copy_regset_to_user(child,
815 task_user_regset_view(current),
816 REGSET_FP,
817 0, sizeof(struct user_i387_struct),
818 datap);
820 case PTRACE_SETFPREGS: /* Set the child FPU state. */
821 return copy_regset_from_user(child,
822 task_user_regset_view(current),
823 REGSET_FP,
824 0, sizeof(struct user_i387_struct),
825 datap);
827 #ifdef CONFIG_X86_32
828 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
829 return copy_regset_to_user(child, &user_x86_32_view,
830 REGSET_XFP,
831 0, sizeof(struct user_fxsr_struct),
832 datap) ? -EIO : 0;
834 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
835 return copy_regset_from_user(child, &user_x86_32_view,
836 REGSET_XFP,
837 0, sizeof(struct user_fxsr_struct),
838 datap) ? -EIO : 0;
839 #endif
841 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
842 case PTRACE_GET_THREAD_AREA:
843 if ((int) addr < 0)
844 return -EIO;
845 ret = do_get_thread_area(child, addr,
846 (struct user_desc __user *)data);
847 break;
849 case PTRACE_SET_THREAD_AREA:
850 if ((int) addr < 0)
851 return -EIO;
852 ret = do_set_thread_area(child, addr,
853 (struct user_desc __user *)data, 0);
854 break;
855 #endif
857 #ifdef CONFIG_X86_64
858 /* normal 64bit interface to access TLS data.
859 Works just like arch_prctl, except that the arguments
860 are reversed. */
861 case PTRACE_ARCH_PRCTL:
862 ret = do_arch_prctl_64(child, data, addr);
863 break;
864 #endif
866 default:
867 ret = ptrace_request(child, request, addr, data);
868 break;
871 return ret;
874 #ifdef CONFIG_IA32_EMULATION
876 #include <linux/compat.h>
877 #include <linux/syscalls.h>
878 #include <asm/ia32.h>
879 #include <asm/user32.h>
881 #define R32(l,q) \
882 case offsetof(struct user32, regs.l): \
883 regs->q = value; break
885 #define SEG32(rs) \
886 case offsetof(struct user32, regs.rs): \
887 return set_segment_reg(child, \
888 offsetof(struct user_regs_struct, rs), \
889 value); \
890 break
892 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
894 struct pt_regs *regs = task_pt_regs(child);
896 switch (regno) {
898 SEG32(cs);
899 SEG32(ds);
900 SEG32(es);
901 SEG32(fs);
902 SEG32(gs);
903 SEG32(ss);
905 R32(ebx, bx);
906 R32(ecx, cx);
907 R32(edx, dx);
908 R32(edi, di);
909 R32(esi, si);
910 R32(ebp, bp);
911 R32(eax, ax);
912 R32(eip, ip);
913 R32(esp, sp);
915 case offsetof(struct user32, regs.orig_eax):
917 * Warning: bizarre corner case fixup here. A 32-bit
918 * debugger setting orig_eax to -1 wants to disable
919 * syscall restart. Make sure that the syscall
920 * restart code sign-extends orig_ax. Also make sure
921 * we interpret the -ERESTART* codes correctly if
922 * loaded into regs->ax in case the task is not
923 * actually still sitting at the exit from a 32-bit
924 * syscall with TS_COMPAT still set.
926 regs->orig_ax = value;
927 if (syscall_get_nr(child, regs) >= 0)
928 child->thread_info.status |= TS_I386_REGS_POKED;
929 break;
931 case offsetof(struct user32, regs.eflags):
932 return set_flags(child, value);
934 case offsetof(struct user32, u_debugreg[0]) ...
935 offsetof(struct user32, u_debugreg[7]):
936 regno -= offsetof(struct user32, u_debugreg[0]);
937 return ptrace_set_debugreg(child, regno / 4, value);
939 default:
940 if (regno > sizeof(struct user32) || (regno & 3))
941 return -EIO;
944 * Other dummy fields in the virtual user structure
945 * are ignored
947 break;
949 return 0;
952 #undef R32
953 #undef SEG32
955 #define R32(l,q) \
956 case offsetof(struct user32, regs.l): \
957 *val = regs->q; break
959 #define SEG32(rs) \
960 case offsetof(struct user32, regs.rs): \
961 *val = get_segment_reg(child, \
962 offsetof(struct user_regs_struct, rs)); \
963 break
965 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
967 struct pt_regs *regs = task_pt_regs(child);
969 switch (regno) {
971 SEG32(ds);
972 SEG32(es);
973 SEG32(fs);
974 SEG32(gs);
976 R32(cs, cs);
977 R32(ss, ss);
978 R32(ebx, bx);
979 R32(ecx, cx);
980 R32(edx, dx);
981 R32(edi, di);
982 R32(esi, si);
983 R32(ebp, bp);
984 R32(eax, ax);
985 R32(orig_eax, orig_ax);
986 R32(eip, ip);
987 R32(esp, sp);
989 case offsetof(struct user32, regs.eflags):
990 *val = get_flags(child);
991 break;
993 case offsetof(struct user32, u_debugreg[0]) ...
994 offsetof(struct user32, u_debugreg[7]):
995 regno -= offsetof(struct user32, u_debugreg[0]);
996 *val = ptrace_get_debugreg(child, regno / 4);
997 break;
999 default:
1000 if (regno > sizeof(struct user32) || (regno & 3))
1001 return -EIO;
1004 * Other dummy fields in the virtual user structure
1005 * are ignored
1007 *val = 0;
1008 break;
1010 return 0;
1013 #undef R32
1014 #undef SEG32
1016 static int genregs32_get(struct task_struct *target,
1017 const struct user_regset *regset,
1018 unsigned int pos, unsigned int count,
1019 void *kbuf, void __user *ubuf)
1021 if (kbuf) {
1022 compat_ulong_t *k = kbuf;
1023 while (count >= sizeof(*k)) {
1024 getreg32(target, pos, k++);
1025 count -= sizeof(*k);
1026 pos += sizeof(*k);
1028 } else {
1029 compat_ulong_t __user *u = ubuf;
1030 while (count >= sizeof(*u)) {
1031 compat_ulong_t word;
1032 getreg32(target, pos, &word);
1033 if (__put_user(word, u++))
1034 return -EFAULT;
1035 count -= sizeof(*u);
1036 pos += sizeof(*u);
1040 return 0;
1043 static int genregs32_set(struct task_struct *target,
1044 const struct user_regset *regset,
1045 unsigned int pos, unsigned int count,
1046 const void *kbuf, const void __user *ubuf)
1048 int ret = 0;
1049 if (kbuf) {
1050 const compat_ulong_t *k = kbuf;
1051 while (count >= sizeof(*k) && !ret) {
1052 ret = putreg32(target, pos, *k++);
1053 count -= sizeof(*k);
1054 pos += sizeof(*k);
1056 } else {
1057 const compat_ulong_t __user *u = ubuf;
1058 while (count >= sizeof(*u) && !ret) {
1059 compat_ulong_t word;
1060 ret = __get_user(word, u++);
1061 if (ret)
1062 break;
1063 ret = putreg32(target, pos, word);
1064 count -= sizeof(*u);
1065 pos += sizeof(*u);
1068 return ret;
1071 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1072 compat_ulong_t caddr, compat_ulong_t cdata)
1074 unsigned long addr = caddr;
1075 unsigned long data = cdata;
1076 void __user *datap = compat_ptr(data);
1077 int ret;
1078 __u32 val;
1080 switch (request) {
1081 case PTRACE_PEEKUSR:
1082 ret = getreg32(child, addr, &val);
1083 if (ret == 0)
1084 ret = put_user(val, (__u32 __user *)datap);
1085 break;
1087 case PTRACE_POKEUSR:
1088 ret = putreg32(child, addr, data);
1089 break;
1091 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1092 return copy_regset_to_user(child, &user_x86_32_view,
1093 REGSET_GENERAL,
1094 0, sizeof(struct user_regs_struct32),
1095 datap);
1097 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1098 return copy_regset_from_user(child, &user_x86_32_view,
1099 REGSET_GENERAL, 0,
1100 sizeof(struct user_regs_struct32),
1101 datap);
1103 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1104 return copy_regset_to_user(child, &user_x86_32_view,
1105 REGSET_FP, 0,
1106 sizeof(struct user_i387_ia32_struct),
1107 datap);
1109 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1110 return copy_regset_from_user(
1111 child, &user_x86_32_view, REGSET_FP,
1112 0, sizeof(struct user_i387_ia32_struct), datap);
1114 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1115 return copy_regset_to_user(child, &user_x86_32_view,
1116 REGSET_XFP, 0,
1117 sizeof(struct user32_fxsr_struct),
1118 datap);
1120 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1121 return copy_regset_from_user(child, &user_x86_32_view,
1122 REGSET_XFP, 0,
1123 sizeof(struct user32_fxsr_struct),
1124 datap);
1126 case PTRACE_GET_THREAD_AREA:
1127 case PTRACE_SET_THREAD_AREA:
1128 return arch_ptrace(child, request, addr, data);
1130 default:
1131 return compat_ptrace_request(child, request, addr, data);
1134 return ret;
1136 #endif /* CONFIG_IA32_EMULATION */
1138 #ifdef CONFIG_X86_X32_ABI
1139 static long x32_arch_ptrace(struct task_struct *child,
1140 compat_long_t request, compat_ulong_t caddr,
1141 compat_ulong_t cdata)
1143 unsigned long addr = caddr;
1144 unsigned long data = cdata;
1145 void __user *datap = compat_ptr(data);
1146 int ret;
1148 switch (request) {
1149 /* Read 32bits at location addr in the USER area. Only allow
1150 to return the lower 32bits of segment and debug registers. */
1151 case PTRACE_PEEKUSR: {
1152 u32 tmp;
1154 ret = -EIO;
1155 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1156 addr < offsetof(struct user_regs_struct, cs))
1157 break;
1159 tmp = 0; /* Default return condition */
1160 if (addr < sizeof(struct user_regs_struct))
1161 tmp = getreg(child, addr);
1162 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1163 addr <= offsetof(struct user, u_debugreg[7])) {
1164 addr -= offsetof(struct user, u_debugreg[0]);
1165 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1167 ret = put_user(tmp, (__u32 __user *)datap);
1168 break;
1171 /* Write the word at location addr in the USER area. Only allow
1172 to update segment and debug registers with the upper 32bits
1173 zero-extended. */
1174 case PTRACE_POKEUSR:
1175 ret = -EIO;
1176 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1177 addr < offsetof(struct user_regs_struct, cs))
1178 break;
1180 if (addr < sizeof(struct user_regs_struct))
1181 ret = putreg(child, addr, data);
1182 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1183 addr <= offsetof(struct user, u_debugreg[7])) {
1184 addr -= offsetof(struct user, u_debugreg[0]);
1185 ret = ptrace_set_debugreg(child,
1186 addr / sizeof(data), data);
1188 break;
1190 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1191 return copy_regset_to_user(child,
1192 task_user_regset_view(current),
1193 REGSET_GENERAL,
1194 0, sizeof(struct user_regs_struct),
1195 datap);
1197 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1198 return copy_regset_from_user(child,
1199 task_user_regset_view(current),
1200 REGSET_GENERAL,
1201 0, sizeof(struct user_regs_struct),
1202 datap);
1204 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1205 return copy_regset_to_user(child,
1206 task_user_regset_view(current),
1207 REGSET_FP,
1208 0, sizeof(struct user_i387_struct),
1209 datap);
1211 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1212 return copy_regset_from_user(child,
1213 task_user_regset_view(current),
1214 REGSET_FP,
1215 0, sizeof(struct user_i387_struct),
1216 datap);
1218 default:
1219 return compat_ptrace_request(child, request, addr, data);
1222 return ret;
1224 #endif
1226 #ifdef CONFIG_COMPAT
1227 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1228 compat_ulong_t caddr, compat_ulong_t cdata)
1230 #ifdef CONFIG_X86_X32_ABI
1231 if (!in_ia32_syscall())
1232 return x32_arch_ptrace(child, request, caddr, cdata);
1233 #endif
1234 #ifdef CONFIG_IA32_EMULATION
1235 return ia32_arch_ptrace(child, request, caddr, cdata);
1236 #else
1237 return 0;
1238 #endif
1240 #endif /* CONFIG_COMPAT */
1242 #ifdef CONFIG_X86_64
1244 static struct user_regset x86_64_regsets[] __ro_after_init = {
1245 [REGSET_GENERAL] = {
1246 .core_note_type = NT_PRSTATUS,
1247 .n = sizeof(struct user_regs_struct) / sizeof(long),
1248 .size = sizeof(long), .align = sizeof(long),
1249 .get = genregs_get, .set = genregs_set
1251 [REGSET_FP] = {
1252 .core_note_type = NT_PRFPREG,
1253 .n = sizeof(struct user_i387_struct) / sizeof(long),
1254 .size = sizeof(long), .align = sizeof(long),
1255 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1257 [REGSET_XSTATE] = {
1258 .core_note_type = NT_X86_XSTATE,
1259 .size = sizeof(u64), .align = sizeof(u64),
1260 .active = xstateregs_active, .get = xstateregs_get,
1261 .set = xstateregs_set
1263 [REGSET_IOPERM64] = {
1264 .core_note_type = NT_386_IOPERM,
1265 .n = IO_BITMAP_LONGS,
1266 .size = sizeof(long), .align = sizeof(long),
1267 .active = ioperm_active, .get = ioperm_get
1271 static const struct user_regset_view user_x86_64_view = {
1272 .name = "x86_64", .e_machine = EM_X86_64,
1273 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1276 #else /* CONFIG_X86_32 */
1278 #define user_regs_struct32 user_regs_struct
1279 #define genregs32_get genregs_get
1280 #define genregs32_set genregs_set
1282 #endif /* CONFIG_X86_64 */
1284 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1285 static struct user_regset x86_32_regsets[] __ro_after_init = {
1286 [REGSET_GENERAL] = {
1287 .core_note_type = NT_PRSTATUS,
1288 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1289 .size = sizeof(u32), .align = sizeof(u32),
1290 .get = genregs32_get, .set = genregs32_set
1292 [REGSET_FP] = {
1293 .core_note_type = NT_PRFPREG,
1294 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1295 .size = sizeof(u32), .align = sizeof(u32),
1296 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1298 [REGSET_XFP] = {
1299 .core_note_type = NT_PRXFPREG,
1300 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1301 .size = sizeof(u32), .align = sizeof(u32),
1302 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1304 [REGSET_XSTATE] = {
1305 .core_note_type = NT_X86_XSTATE,
1306 .size = sizeof(u64), .align = sizeof(u64),
1307 .active = xstateregs_active, .get = xstateregs_get,
1308 .set = xstateregs_set
1310 [REGSET_TLS] = {
1311 .core_note_type = NT_386_TLS,
1312 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1313 .size = sizeof(struct user_desc),
1314 .align = sizeof(struct user_desc),
1315 .active = regset_tls_active,
1316 .get = regset_tls_get, .set = regset_tls_set
1318 [REGSET_IOPERM32] = {
1319 .core_note_type = NT_386_IOPERM,
1320 .n = IO_BITMAP_BYTES / sizeof(u32),
1321 .size = sizeof(u32), .align = sizeof(u32),
1322 .active = ioperm_active, .get = ioperm_get
1326 static const struct user_regset_view user_x86_32_view = {
1327 .name = "i386", .e_machine = EM_386,
1328 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1330 #endif
1333 * This represents bytes 464..511 in the memory layout exported through
1334 * the REGSET_XSTATE interface.
1336 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1338 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1340 #ifdef CONFIG_X86_64
1341 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1342 #endif
1343 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1344 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1345 #endif
1346 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1349 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1351 #ifdef CONFIG_IA32_EMULATION
1352 if (!user_64bit_mode(task_pt_regs(task)))
1353 #endif
1354 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1355 return &user_x86_32_view;
1356 #endif
1357 #ifdef CONFIG_X86_64
1358 return &user_x86_64_view;
1359 #endif
1362 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1363 int error_code, int si_code)
1365 tsk->thread.trap_nr = X86_TRAP_DB;
1366 tsk->thread.error_code = error_code;
1368 /* Send us the fake SIGTRAP */
1369 force_sig_fault(SIGTRAP, si_code,
1370 user_mode(regs) ? (void __user *)regs->ip : NULL, tsk);
1373 void user_single_step_report(struct pt_regs *regs)
1375 send_sigtrap(current, regs, 0, TRAP_BRKPT);