ptrace: move clearing of TIF_SYSCALL_EMU flag to core
[linux-2.6-microblaze.git] / arch / x86 / kernel / ptrace.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* By Ross Biro 1/23/92 */
3 /*
4  * Pentium III FXSR, SSE support
5  *      Gareth Hughes <gareth@valinux.com>, May 2000
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/ptrace.h>
16 #include <linux/tracehook.h>
17 #include <linux/user.h>
18 #include <linux/elf.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/seccomp.h>
22 #include <linux/signal.h>
23 #include <linux/perf_event.h>
24 #include <linux/hw_breakpoint.h>
25 #include <linux/rcupdate.h>
26 #include <linux/export.h>
27 #include <linux/context_tracking.h>
28
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/fsgsbase.h>
44
45 #include "tls.h"
46
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,
55 };
56
57 struct pt_regs_offset {
58         const char *name;
59         int offset;
60 };
61
62 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
63 #define REG_OFFSET_END {.name = NULL, .offset = 0}
64
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,
96 };
97
98 /**
99  * regs_query_register_offset() - query register offset from its name
100  * @name:       the name of a register
101  *
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;
104  */
105 int regs_query_register_offset(const char *name)
106 {
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;
112 }
113
114 /**
115  * regs_query_register_name() - query register name from its offset
116  * @offset:     the offset of a register in struct pt_regs.
117  *
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;
120  */
121 const char *regs_query_register_name(unsigned int offset)
122 {
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;
128 }
129
130 /*
131  * does not yet catch signals sent when the child dies.
132  * in exit.c or in signal.c.
133  */
134
135 /*
136  * Determines which flags the user has access to [1 = access, 0 = no access].
137  */
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))
144
145 /*
146  * Determines whether a value may be installed in a segment register.
147  */
148 static inline bool invalid_selector(u16 value)
149 {
150         return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
151 }
152
153 #ifdef CONFIG_X86_32
154
155 #define FLAG_MASK               FLAG_MASK_32
156
157 /*
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'.
161  *
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.
166  *
167  * This is valid only for kernel mode traps.
168  */
169 unsigned long kernel_stack_pointer(struct pt_regs *regs)
170 {
171         unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
172         unsigned long sp = (unsigned long)&regs->sp;
173         u32 *prev_esp;
174
175         if (context == (sp & ~(THREAD_SIZE - 1)))
176                 return sp;
177
178         prev_esp = (u32 *)(context);
179         if (*prev_esp)
180                 return (unsigned long)*prev_esp;
181
182         return (unsigned long)regs;
183 }
184 EXPORT_SYMBOL_GPL(kernel_stack_pointer);
185
186 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
187 {
188         BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
189         return &regs->bx + (regno >> 2);
190 }
191
192 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
193 {
194         /*
195          * Returning the value truncates it to 16 bits.
196          */
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);
205         }
206         return retval;
207 }
208
209 static int set_segment_reg(struct task_struct *task,
210                            unsigned long offset, u16 value)
211 {
212         /*
213          * The value argument was already truncated to 16 bits.
214          */
215         if (invalid_selector(value))
216                 return -EIO;
217
218         /*
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.
226          */
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;
232
233         default:
234                 *pt_regs_access(task_pt_regs(task), offset) = value;
235                 break;
236
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;
242         }
243
244         return 0;
245 }
246
247 #else  /* CONFIG_X86_64 */
248
249 #define FLAG_MASK               (FLAG_MASK_32 | X86_EFLAGS_NT)
250
251 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
252 {
253         BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
254         return &regs->r15 + (offset / sizeof(regs->r15));
255 }
256
257 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
258 {
259         /*
260          * Returning the value truncates it to 16 bits.
261          */
262         unsigned int seg;
263
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;
270                 }
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;
276                 }
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;
282                 }
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;
288                 }
289                 return task->thread.es;
290
291         case offsetof(struct user_regs_struct, cs):
292         case offsetof(struct user_regs_struct, ss):
293                 break;
294         }
295         return *pt_regs_access(task_pt_regs(task), offset);
296 }
297
298 static int set_segment_reg(struct task_struct *task,
299                            unsigned long offset, u16 value)
300 {
301         /*
302          * The value argument was already truncated to 16 bits.
303          */
304         if (invalid_selector(value))
305                 return -EIO;
306
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;
328
329                 /*
330                  * Can't actually change these in 64-bit mode.
331                  */
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;
342         }
343
344         return 0;
345 }
346
347 #endif  /* CONFIG_X86_32 */
348
349 static unsigned long get_flags(struct task_struct *task)
350 {
351         unsigned long retval = task_pt_regs(task)->flags;
352
353         /*
354          * If the debugger set TF, hide it from the readout.
355          */
356         if (test_tsk_thread_flag(task, TIF_FORCED_TF))
357                 retval &= ~X86_EFLAGS_TF;
358
359         return retval;
360 }
361
362 static int set_flags(struct task_struct *task, unsigned long value)
363 {
364         struct pt_regs *regs = task_pt_regs(task);
365
366         /*
367          * If the user value contains TF, mark that
368          * it was not "us" (the debugger) that set it.
369          * If not, make sure it stays set if we had.
370          */
371         if (value & X86_EFLAGS_TF)
372                 clear_tsk_thread_flag(task, TIF_FORCED_TF);
373         else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
374                 value |= X86_EFLAGS_TF;
375
376         regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
377
378         return 0;
379 }
380
381 static int putreg(struct task_struct *child,
382                   unsigned long offset, unsigned long value)
383 {
384         switch (offset) {
385         case offsetof(struct user_regs_struct, cs):
386         case offsetof(struct user_regs_struct, ds):
387         case offsetof(struct user_regs_struct, es):
388         case offsetof(struct user_regs_struct, fs):
389         case offsetof(struct user_regs_struct, gs):
390         case offsetof(struct user_regs_struct, ss):
391                 return set_segment_reg(child, offset, value);
392
393         case offsetof(struct user_regs_struct, flags):
394                 return set_flags(child, value);
395
396 #ifdef CONFIG_X86_64
397         case offsetof(struct user_regs_struct,fs_base):
398                 if (value >= TASK_SIZE_MAX)
399                         return -EIO;
400                 /*
401                  * When changing the FS base, use do_arch_prctl_64()
402                  * to set the index to zero and to set the base
403                  * as requested.
404                  */
405                 if (child->thread.fsbase != value)
406                         return do_arch_prctl_64(child, ARCH_SET_FS, value);
407                 return 0;
408         case offsetof(struct user_regs_struct,gs_base):
409                 /*
410                  * Exactly the same here as the %fs handling above.
411                  */
412                 if (value >= TASK_SIZE_MAX)
413                         return -EIO;
414                 if (child->thread.gsbase != value)
415                         return do_arch_prctl_64(child, ARCH_SET_GS, value);
416                 return 0;
417 #endif
418         }
419
420         *pt_regs_access(task_pt_regs(child), offset) = value;
421         return 0;
422 }
423
424 static unsigned long getreg(struct task_struct *task, unsigned long offset)
425 {
426         switch (offset) {
427         case offsetof(struct user_regs_struct, cs):
428         case offsetof(struct user_regs_struct, ds):
429         case offsetof(struct user_regs_struct, es):
430         case offsetof(struct user_regs_struct, fs):
431         case offsetof(struct user_regs_struct, gs):
432         case offsetof(struct user_regs_struct, ss):
433                 return get_segment_reg(task, offset);
434
435         case offsetof(struct user_regs_struct, flags):
436                 return get_flags(task);
437
438 #ifdef CONFIG_X86_64
439         case offsetof(struct user_regs_struct, fs_base):
440                 return x86_fsbase_read_task(task);
441         case offsetof(struct user_regs_struct, gs_base):
442                 return x86_gsbase_read_task(task);
443 #endif
444         }
445
446         return *pt_regs_access(task_pt_regs(task), offset);
447 }
448
449 static int genregs_get(struct task_struct *target,
450                        const struct user_regset *regset,
451                        unsigned int pos, unsigned int count,
452                        void *kbuf, void __user *ubuf)
453 {
454         if (kbuf) {
455                 unsigned long *k = kbuf;
456                 while (count >= sizeof(*k)) {
457                         *k++ = getreg(target, pos);
458                         count -= sizeof(*k);
459                         pos += sizeof(*k);
460                 }
461         } else {
462                 unsigned long __user *u = ubuf;
463                 while (count >= sizeof(*u)) {
464                         if (__put_user(getreg(target, pos), u++))
465                                 return -EFAULT;
466                         count -= sizeof(*u);
467                         pos += sizeof(*u);
468                 }
469         }
470
471         return 0;
472 }
473
474 static int genregs_set(struct task_struct *target,
475                        const struct user_regset *regset,
476                        unsigned int pos, unsigned int count,
477                        const void *kbuf, const void __user *ubuf)
478 {
479         int ret = 0;
480         if (kbuf) {
481                 const unsigned long *k = kbuf;
482                 while (count >= sizeof(*k) && !ret) {
483                         ret = putreg(target, pos, *k++);
484                         count -= sizeof(*k);
485                         pos += sizeof(*k);
486                 }
487         } else {
488                 const unsigned long  __user *u = ubuf;
489                 while (count >= sizeof(*u) && !ret) {
490                         unsigned long word;
491                         ret = __get_user(word, u++);
492                         if (ret)
493                                 break;
494                         ret = putreg(target, pos, word);
495                         count -= sizeof(*u);
496                         pos += sizeof(*u);
497                 }
498         }
499         return ret;
500 }
501
502 static void ptrace_triggered(struct perf_event *bp,
503                              struct perf_sample_data *data,
504                              struct pt_regs *regs)
505 {
506         int i;
507         struct thread_struct *thread = &(current->thread);
508
509         /*
510          * Store in the virtual DR6 register the fact that the breakpoint
511          * was hit so the thread's debugger will see it.
512          */
513         for (i = 0; i < HBP_NUM; i++) {
514                 if (thread->ptrace_bps[i] == bp)
515                         break;
516         }
517
518         thread->debugreg6 |= (DR_TRAP0 << i);
519 }
520
521 /*
522  * Walk through every ptrace breakpoints for this thread and
523  * build the dr7 value on top of their attributes.
524  *
525  */
526 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
527 {
528         int i;
529         int dr7 = 0;
530         struct arch_hw_breakpoint *info;
531
532         for (i = 0; i < HBP_NUM; i++) {
533                 if (bp[i] && !bp[i]->attr.disabled) {
534                         info = counter_arch_bp(bp[i]);
535                         dr7 |= encode_dr7(i, info->len, info->type);
536                 }
537         }
538
539         return dr7;
540 }
541
542 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
543                                         int len, int type, bool disabled)
544 {
545         int err, bp_len, bp_type;
546
547         err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
548         if (!err) {
549                 attr->bp_len = bp_len;
550                 attr->bp_type = bp_type;
551                 attr->disabled = disabled;
552         }
553
554         return err;
555 }
556
557 static struct perf_event *
558 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
559                                 unsigned long addr, bool disabled)
560 {
561         struct perf_event_attr attr;
562         int err;
563
564         ptrace_breakpoint_init(&attr);
565         attr.bp_addr = addr;
566
567         err = ptrace_fill_bp_fields(&attr, len, type, disabled);
568         if (err)
569                 return ERR_PTR(err);
570
571         return register_user_hw_breakpoint(&attr, ptrace_triggered,
572                                                  NULL, tsk);
573 }
574
575 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
576                                         int disabled)
577 {
578         struct perf_event_attr attr = bp->attr;
579         int err;
580
581         err = ptrace_fill_bp_fields(&attr, len, type, disabled);
582         if (err)
583                 return err;
584
585         return modify_user_hw_breakpoint(bp, &attr);
586 }
587
588 /*
589  * Handle ptrace writes to debug register 7.
590  */
591 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
592 {
593         struct thread_struct *thread = &tsk->thread;
594         unsigned long old_dr7;
595         bool second_pass = false;
596         int i, rc, ret = 0;
597
598         data &= ~DR_CONTROL_RESERVED;
599         old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
600
601 restore:
602         rc = 0;
603         for (i = 0; i < HBP_NUM; i++) {
604                 unsigned len, type;
605                 bool disabled = !decode_dr7(data, i, &len, &type);
606                 struct perf_event *bp = thread->ptrace_bps[i];
607
608                 if (!bp) {
609                         if (disabled)
610                                 continue;
611
612                         bp = ptrace_register_breakpoint(tsk,
613                                         len, type, 0, disabled);
614                         if (IS_ERR(bp)) {
615                                 rc = PTR_ERR(bp);
616                                 break;
617                         }
618
619                         thread->ptrace_bps[i] = bp;
620                         continue;
621                 }
622
623                 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
624                 if (rc)
625                         break;
626         }
627
628         /* Restore if the first pass failed, second_pass shouldn't fail. */
629         if (rc && !WARN_ON(second_pass)) {
630                 ret = rc;
631                 data = old_dr7;
632                 second_pass = true;
633                 goto restore;
634         }
635
636         return ret;
637 }
638
639 /*
640  * Handle PTRACE_PEEKUSR calls for the debug register area.
641  */
642 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
643 {
644         struct thread_struct *thread = &tsk->thread;
645         unsigned long val = 0;
646
647         if (n < HBP_NUM) {
648                 struct perf_event *bp = thread->ptrace_bps[n];
649
650                 if (bp)
651                         val = bp->hw.info.address;
652         } else if (n == 6) {
653                 val = thread->debugreg6;
654         } else if (n == 7) {
655                 val = thread->ptrace_dr7;
656         }
657         return val;
658 }
659
660 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
661                                       unsigned long addr)
662 {
663         struct thread_struct *t = &tsk->thread;
664         struct perf_event *bp = t->ptrace_bps[nr];
665         int err = 0;
666
667         if (!bp) {
668                 /*
669                  * Put stub len and type to create an inactive but correct bp.
670                  *
671                  * CHECKME: the previous code returned -EIO if the addr wasn't
672                  * a valid task virtual addr. The new one will return -EINVAL in
673                  *  this case.
674                  * -EINVAL may be what we want for in-kernel breakpoints users,
675                  * but -EIO looks better for ptrace, since we refuse a register
676                  * writing for the user. And anyway this is the previous
677                  * behaviour.
678                  */
679                 bp = ptrace_register_breakpoint(tsk,
680                                 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
681                                 addr, true);
682                 if (IS_ERR(bp))
683                         err = PTR_ERR(bp);
684                 else
685                         t->ptrace_bps[nr] = bp;
686         } else {
687                 struct perf_event_attr attr = bp->attr;
688
689                 attr.bp_addr = addr;
690                 err = modify_user_hw_breakpoint(bp, &attr);
691         }
692
693         return err;
694 }
695
696 /*
697  * Handle PTRACE_POKEUSR calls for the debug register area.
698  */
699 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
700                                unsigned long val)
701 {
702         struct thread_struct *thread = &tsk->thread;
703         /* There are no DR4 or DR5 registers */
704         int rc = -EIO;
705
706         if (n < HBP_NUM) {
707                 rc = ptrace_set_breakpoint_addr(tsk, n, val);
708         } else if (n == 6) {
709                 thread->debugreg6 = val;
710                 rc = 0;
711         } else if (n == 7) {
712                 rc = ptrace_write_dr7(tsk, val);
713                 if (!rc)
714                         thread->ptrace_dr7 = val;
715         }
716         return rc;
717 }
718
719 /*
720  * These access the current or another (stopped) task's io permission
721  * bitmap for debugging or core dump.
722  */
723 static int ioperm_active(struct task_struct *target,
724                          const struct user_regset *regset)
725 {
726         return target->thread.io_bitmap_max / regset->size;
727 }
728
729 static int ioperm_get(struct task_struct *target,
730                       const struct user_regset *regset,
731                       unsigned int pos, unsigned int count,
732                       void *kbuf, void __user *ubuf)
733 {
734         if (!target->thread.io_bitmap_ptr)
735                 return -ENXIO;
736
737         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
738                                    target->thread.io_bitmap_ptr,
739                                    0, IO_BITMAP_BYTES);
740 }
741
742 /*
743  * Called by kernel/ptrace.c when detaching..
744  *
745  * Make sure the single step bit is not set.
746  */
747 void ptrace_disable(struct task_struct *child)
748 {
749         user_disable_single_step(child);
750 }
751
752 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
753 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
754 #endif
755
756 long arch_ptrace(struct task_struct *child, long request,
757                  unsigned long addr, unsigned long data)
758 {
759         int ret;
760         unsigned long __user *datap = (unsigned long __user *)data;
761
762         switch (request) {
763         /* read the word at location addr in the USER area. */
764         case PTRACE_PEEKUSR: {
765                 unsigned long tmp;
766
767                 ret = -EIO;
768                 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
769                         break;
770
771                 tmp = 0;  /* Default return condition */
772                 if (addr < sizeof(struct user_regs_struct))
773                         tmp = getreg(child, addr);
774                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
775                          addr <= offsetof(struct user, u_debugreg[7])) {
776                         addr -= offsetof(struct user, u_debugreg[0]);
777                         tmp = ptrace_get_debugreg(child, addr / sizeof(data));
778                 }
779                 ret = put_user(tmp, datap);
780                 break;
781         }
782
783         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
784                 ret = -EIO;
785                 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
786                         break;
787
788                 if (addr < sizeof(struct user_regs_struct))
789                         ret = putreg(child, addr, data);
790                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
791                          addr <= offsetof(struct user, u_debugreg[7])) {
792                         addr -= offsetof(struct user, u_debugreg[0]);
793                         ret = ptrace_set_debugreg(child,
794                                                   addr / sizeof(data), data);
795                 }
796                 break;
797
798         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
799                 return copy_regset_to_user(child,
800                                            task_user_regset_view(current),
801                                            REGSET_GENERAL,
802                                            0, sizeof(struct user_regs_struct),
803                                            datap);
804
805         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
806                 return copy_regset_from_user(child,
807                                              task_user_regset_view(current),
808                                              REGSET_GENERAL,
809                                              0, sizeof(struct user_regs_struct),
810                                              datap);
811
812         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
813                 return copy_regset_to_user(child,
814                                            task_user_regset_view(current),
815                                            REGSET_FP,
816                                            0, sizeof(struct user_i387_struct),
817                                            datap);
818
819         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
820                 return copy_regset_from_user(child,
821                                              task_user_regset_view(current),
822                                              REGSET_FP,
823                                              0, sizeof(struct user_i387_struct),
824                                              datap);
825
826 #ifdef CONFIG_X86_32
827         case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
828                 return copy_regset_to_user(child, &user_x86_32_view,
829                                            REGSET_XFP,
830                                            0, sizeof(struct user_fxsr_struct),
831                                            datap) ? -EIO : 0;
832
833         case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
834                 return copy_regset_from_user(child, &user_x86_32_view,
835                                              REGSET_XFP,
836                                              0, sizeof(struct user_fxsr_struct),
837                                              datap) ? -EIO : 0;
838 #endif
839
840 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
841         case PTRACE_GET_THREAD_AREA:
842                 if ((int) addr < 0)
843                         return -EIO;
844                 ret = do_get_thread_area(child, addr,
845                                         (struct user_desc __user *)data);
846                 break;
847
848         case PTRACE_SET_THREAD_AREA:
849                 if ((int) addr < 0)
850                         return -EIO;
851                 ret = do_set_thread_area(child, addr,
852                                         (struct user_desc __user *)data, 0);
853                 break;
854 #endif
855
856 #ifdef CONFIG_X86_64
857                 /* normal 64bit interface to access TLS data.
858                    Works just like arch_prctl, except that the arguments
859                    are reversed. */
860         case PTRACE_ARCH_PRCTL:
861                 ret = do_arch_prctl_64(child, data, addr);
862                 break;
863 #endif
864
865         default:
866                 ret = ptrace_request(child, request, addr, data);
867                 break;
868         }
869
870         return ret;
871 }
872
873 #ifdef CONFIG_IA32_EMULATION
874
875 #include <linux/compat.h>
876 #include <linux/syscalls.h>
877 #include <asm/ia32.h>
878 #include <asm/user32.h>
879
880 #define R32(l,q)                                                        \
881         case offsetof(struct user32, regs.l):                           \
882                 regs->q = value; break
883
884 #define SEG32(rs)                                                       \
885         case offsetof(struct user32, regs.rs):                          \
886                 return set_segment_reg(child,                           \
887                                        offsetof(struct user_regs_struct, rs), \
888                                        value);                          \
889                 break
890
891 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
892 {
893         struct pt_regs *regs = task_pt_regs(child);
894
895         switch (regno) {
896
897         SEG32(cs);
898         SEG32(ds);
899         SEG32(es);
900         SEG32(fs);
901         SEG32(gs);
902         SEG32(ss);
903
904         R32(ebx, bx);
905         R32(ecx, cx);
906         R32(edx, dx);
907         R32(edi, di);
908         R32(esi, si);
909         R32(ebp, bp);
910         R32(eax, ax);
911         R32(eip, ip);
912         R32(esp, sp);
913
914         case offsetof(struct user32, regs.orig_eax):
915                 /*
916                  * Warning: bizarre corner case fixup here.  A 32-bit
917                  * debugger setting orig_eax to -1 wants to disable
918                  * syscall restart.  Make sure that the syscall
919                  * restart code sign-extends orig_ax.  Also make sure
920                  * we interpret the -ERESTART* codes correctly if
921                  * loaded into regs->ax in case the task is not
922                  * actually still sitting at the exit from a 32-bit
923                  * syscall with TS_COMPAT still set.
924                  */
925                 regs->orig_ax = value;
926                 if (syscall_get_nr(child, regs) >= 0)
927                         child->thread_info.status |= TS_I386_REGS_POKED;
928                 break;
929
930         case offsetof(struct user32, regs.eflags):
931                 return set_flags(child, value);
932
933         case offsetof(struct user32, u_debugreg[0]) ...
934                 offsetof(struct user32, u_debugreg[7]):
935                 regno -= offsetof(struct user32, u_debugreg[0]);
936                 return ptrace_set_debugreg(child, regno / 4, value);
937
938         default:
939                 if (regno > sizeof(struct user32) || (regno & 3))
940                         return -EIO;
941
942                 /*
943                  * Other dummy fields in the virtual user structure
944                  * are ignored
945                  */
946                 break;
947         }
948         return 0;
949 }
950
951 #undef R32
952 #undef SEG32
953
954 #define R32(l,q)                                                        \
955         case offsetof(struct user32, regs.l):                           \
956                 *val = regs->q; break
957
958 #define SEG32(rs)                                                       \
959         case offsetof(struct user32, regs.rs):                          \
960                 *val = get_segment_reg(child,                           \
961                                        offsetof(struct user_regs_struct, rs)); \
962                 break
963
964 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
965 {
966         struct pt_regs *regs = task_pt_regs(child);
967
968         switch (regno) {
969
970         SEG32(ds);
971         SEG32(es);
972         SEG32(fs);
973         SEG32(gs);
974
975         R32(cs, cs);
976         R32(ss, ss);
977         R32(ebx, bx);
978         R32(ecx, cx);
979         R32(edx, dx);
980         R32(edi, di);
981         R32(esi, si);
982         R32(ebp, bp);
983         R32(eax, ax);
984         R32(orig_eax, orig_ax);
985         R32(eip, ip);
986         R32(esp, sp);
987
988         case offsetof(struct user32, regs.eflags):
989                 *val = get_flags(child);
990                 break;
991
992         case offsetof(struct user32, u_debugreg[0]) ...
993                 offsetof(struct user32, u_debugreg[7]):
994                 regno -= offsetof(struct user32, u_debugreg[0]);
995                 *val = ptrace_get_debugreg(child, regno / 4);
996                 break;
997
998         default:
999                 if (regno > sizeof(struct user32) || (regno & 3))
1000                         return -EIO;
1001
1002                 /*
1003                  * Other dummy fields in the virtual user structure
1004                  * are ignored
1005                  */
1006                 *val = 0;
1007                 break;
1008         }
1009         return 0;
1010 }
1011
1012 #undef R32
1013 #undef SEG32
1014
1015 static int genregs32_get(struct task_struct *target,
1016                          const struct user_regset *regset,
1017                          unsigned int pos, unsigned int count,
1018                          void *kbuf, void __user *ubuf)
1019 {
1020         if (kbuf) {
1021                 compat_ulong_t *k = kbuf;
1022                 while (count >= sizeof(*k)) {
1023                         getreg32(target, pos, k++);
1024                         count -= sizeof(*k);
1025                         pos += sizeof(*k);
1026                 }
1027         } else {
1028                 compat_ulong_t __user *u = ubuf;
1029                 while (count >= sizeof(*u)) {
1030                         compat_ulong_t word;
1031                         getreg32(target, pos, &word);
1032                         if (__put_user(word, u++))
1033                                 return -EFAULT;
1034                         count -= sizeof(*u);
1035                         pos += sizeof(*u);
1036                 }
1037         }
1038
1039         return 0;
1040 }
1041
1042 static int genregs32_set(struct task_struct *target,
1043                          const struct user_regset *regset,
1044                          unsigned int pos, unsigned int count,
1045                          const void *kbuf, const void __user *ubuf)
1046 {
1047         int ret = 0;
1048         if (kbuf) {
1049                 const compat_ulong_t *k = kbuf;
1050                 while (count >= sizeof(*k) && !ret) {
1051                         ret = putreg32(target, pos, *k++);
1052                         count -= sizeof(*k);
1053                         pos += sizeof(*k);
1054                 }
1055         } else {
1056                 const compat_ulong_t __user *u = ubuf;
1057                 while (count >= sizeof(*u) && !ret) {
1058                         compat_ulong_t word;
1059                         ret = __get_user(word, u++);
1060                         if (ret)
1061                                 break;
1062                         ret = putreg32(target, pos, word);
1063                         count -= sizeof(*u);
1064                         pos += sizeof(*u);
1065                 }
1066         }
1067         return ret;
1068 }
1069
1070 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1071                              compat_ulong_t caddr, compat_ulong_t cdata)
1072 {
1073         unsigned long addr = caddr;
1074         unsigned long data = cdata;
1075         void __user *datap = compat_ptr(data);
1076         int ret;
1077         __u32 val;
1078
1079         switch (request) {
1080         case PTRACE_PEEKUSR:
1081                 ret = getreg32(child, addr, &val);
1082                 if (ret == 0)
1083                         ret = put_user(val, (__u32 __user *)datap);
1084                 break;
1085
1086         case PTRACE_POKEUSR:
1087                 ret = putreg32(child, addr, data);
1088                 break;
1089
1090         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
1091                 return copy_regset_to_user(child, &user_x86_32_view,
1092                                            REGSET_GENERAL,
1093                                            0, sizeof(struct user_regs_struct32),
1094                                            datap);
1095
1096         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
1097                 return copy_regset_from_user(child, &user_x86_32_view,
1098                                              REGSET_GENERAL, 0,
1099                                              sizeof(struct user_regs_struct32),
1100                                              datap);
1101
1102         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
1103                 return copy_regset_to_user(child, &user_x86_32_view,
1104                                            REGSET_FP, 0,
1105                                            sizeof(struct user_i387_ia32_struct),
1106                                            datap);
1107
1108         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
1109                 return copy_regset_from_user(
1110                         child, &user_x86_32_view, REGSET_FP,
1111                         0, sizeof(struct user_i387_ia32_struct), datap);
1112
1113         case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1114                 return copy_regset_to_user(child, &user_x86_32_view,
1115                                            REGSET_XFP, 0,
1116                                            sizeof(struct user32_fxsr_struct),
1117                                            datap);
1118
1119         case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1120                 return copy_regset_from_user(child, &user_x86_32_view,
1121                                              REGSET_XFP, 0,
1122                                              sizeof(struct user32_fxsr_struct),
1123                                              datap);
1124
1125         case PTRACE_GET_THREAD_AREA:
1126         case PTRACE_SET_THREAD_AREA:
1127                 return arch_ptrace(child, request, addr, data);
1128
1129         default:
1130                 return compat_ptrace_request(child, request, addr, data);
1131         }
1132
1133         return ret;
1134 }
1135 #endif /* CONFIG_IA32_EMULATION */
1136
1137 #ifdef CONFIG_X86_X32_ABI
1138 static long x32_arch_ptrace(struct task_struct *child,
1139                             compat_long_t request, compat_ulong_t caddr,
1140                             compat_ulong_t cdata)
1141 {
1142         unsigned long addr = caddr;
1143         unsigned long data = cdata;
1144         void __user *datap = compat_ptr(data);
1145         int ret;
1146
1147         switch (request) {
1148         /* Read 32bits at location addr in the USER area.  Only allow
1149            to return the lower 32bits of segment and debug registers.  */
1150         case PTRACE_PEEKUSR: {
1151                 u32 tmp;
1152
1153                 ret = -EIO;
1154                 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1155                     addr < offsetof(struct user_regs_struct, cs))
1156                         break;
1157
1158                 tmp = 0;  /* Default return condition */
1159                 if (addr < sizeof(struct user_regs_struct))
1160                         tmp = getreg(child, addr);
1161                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1162                          addr <= offsetof(struct user, u_debugreg[7])) {
1163                         addr -= offsetof(struct user, u_debugreg[0]);
1164                         tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1165                 }
1166                 ret = put_user(tmp, (__u32 __user *)datap);
1167                 break;
1168         }
1169
1170         /* Write the word at location addr in the USER area.  Only allow
1171            to update segment and debug registers with the upper 32bits
1172            zero-extended. */
1173         case PTRACE_POKEUSR:
1174                 ret = -EIO;
1175                 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1176                     addr < offsetof(struct user_regs_struct, cs))
1177                         break;
1178
1179                 if (addr < sizeof(struct user_regs_struct))
1180                         ret = putreg(child, addr, data);
1181                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1182                          addr <= offsetof(struct user, u_debugreg[7])) {
1183                         addr -= offsetof(struct user, u_debugreg[0]);
1184                         ret = ptrace_set_debugreg(child,
1185                                                   addr / sizeof(data), data);
1186                 }
1187                 break;
1188
1189         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
1190                 return copy_regset_to_user(child,
1191                                            task_user_regset_view(current),
1192                                            REGSET_GENERAL,
1193                                            0, sizeof(struct user_regs_struct),
1194                                            datap);
1195
1196         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
1197                 return copy_regset_from_user(child,
1198                                              task_user_regset_view(current),
1199                                              REGSET_GENERAL,
1200                                              0, sizeof(struct user_regs_struct),
1201                                              datap);
1202
1203         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
1204                 return copy_regset_to_user(child,
1205                                            task_user_regset_view(current),
1206                                            REGSET_FP,
1207                                            0, sizeof(struct user_i387_struct),
1208                                            datap);
1209
1210         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
1211                 return copy_regset_from_user(child,
1212                                              task_user_regset_view(current),
1213                                              REGSET_FP,
1214                                              0, sizeof(struct user_i387_struct),
1215                                              datap);
1216
1217         default:
1218                 return compat_ptrace_request(child, request, addr, data);
1219         }
1220
1221         return ret;
1222 }
1223 #endif
1224
1225 #ifdef CONFIG_COMPAT
1226 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1227                         compat_ulong_t caddr, compat_ulong_t cdata)
1228 {
1229 #ifdef CONFIG_X86_X32_ABI
1230         if (!in_ia32_syscall())
1231                 return x32_arch_ptrace(child, request, caddr, cdata);
1232 #endif
1233 #ifdef CONFIG_IA32_EMULATION
1234         return ia32_arch_ptrace(child, request, caddr, cdata);
1235 #else
1236         return 0;
1237 #endif
1238 }
1239 #endif  /* CONFIG_COMPAT */
1240
1241 #ifdef CONFIG_X86_64
1242
1243 static struct user_regset x86_64_regsets[] __ro_after_init = {
1244         [REGSET_GENERAL] = {
1245                 .core_note_type = NT_PRSTATUS,
1246                 .n = sizeof(struct user_regs_struct) / sizeof(long),
1247                 .size = sizeof(long), .align = sizeof(long),
1248                 .get = genregs_get, .set = genregs_set
1249         },
1250         [REGSET_FP] = {
1251                 .core_note_type = NT_PRFPREG,
1252                 .n = sizeof(struct user_i387_struct) / sizeof(long),
1253                 .size = sizeof(long), .align = sizeof(long),
1254                 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1255         },
1256         [REGSET_XSTATE] = {
1257                 .core_note_type = NT_X86_XSTATE,
1258                 .size = sizeof(u64), .align = sizeof(u64),
1259                 .active = xstateregs_active, .get = xstateregs_get,
1260                 .set = xstateregs_set
1261         },
1262         [REGSET_IOPERM64] = {
1263                 .core_note_type = NT_386_IOPERM,
1264                 .n = IO_BITMAP_LONGS,
1265                 .size = sizeof(long), .align = sizeof(long),
1266                 .active = ioperm_active, .get = ioperm_get
1267         },
1268 };
1269
1270 static const struct user_regset_view user_x86_64_view = {
1271         .name = "x86_64", .e_machine = EM_X86_64,
1272         .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1273 };
1274
1275 #else  /* CONFIG_X86_32 */
1276
1277 #define user_regs_struct32      user_regs_struct
1278 #define genregs32_get           genregs_get
1279 #define genregs32_set           genregs_set
1280
1281 #endif  /* CONFIG_X86_64 */
1282
1283 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1284 static struct user_regset x86_32_regsets[] __ro_after_init = {
1285         [REGSET_GENERAL] = {
1286                 .core_note_type = NT_PRSTATUS,
1287                 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1288                 .size = sizeof(u32), .align = sizeof(u32),
1289                 .get = genregs32_get, .set = genregs32_set
1290         },
1291         [REGSET_FP] = {
1292                 .core_note_type = NT_PRFPREG,
1293                 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1294                 .size = sizeof(u32), .align = sizeof(u32),
1295                 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1296         },
1297         [REGSET_XFP] = {
1298                 .core_note_type = NT_PRXFPREG,
1299                 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1300                 .size = sizeof(u32), .align = sizeof(u32),
1301                 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1302         },
1303         [REGSET_XSTATE] = {
1304                 .core_note_type = NT_X86_XSTATE,
1305                 .size = sizeof(u64), .align = sizeof(u64),
1306                 .active = xstateregs_active, .get = xstateregs_get,
1307                 .set = xstateregs_set
1308         },
1309         [REGSET_TLS] = {
1310                 .core_note_type = NT_386_TLS,
1311                 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1312                 .size = sizeof(struct user_desc),
1313                 .align = sizeof(struct user_desc),
1314                 .active = regset_tls_active,
1315                 .get = regset_tls_get, .set = regset_tls_set
1316         },
1317         [REGSET_IOPERM32] = {
1318                 .core_note_type = NT_386_IOPERM,
1319                 .n = IO_BITMAP_BYTES / sizeof(u32),
1320                 .size = sizeof(u32), .align = sizeof(u32),
1321                 .active = ioperm_active, .get = ioperm_get
1322         },
1323 };
1324
1325 static const struct user_regset_view user_x86_32_view = {
1326         .name = "i386", .e_machine = EM_386,
1327         .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1328 };
1329 #endif
1330
1331 /*
1332  * This represents bytes 464..511 in the memory layout exported through
1333  * the REGSET_XSTATE interface.
1334  */
1335 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1336
1337 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1338 {
1339 #ifdef CONFIG_X86_64
1340         x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1341 #endif
1342 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1343         x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1344 #endif
1345         xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1346 }
1347
1348 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1349 {
1350 #ifdef CONFIG_IA32_EMULATION
1351         if (!user_64bit_mode(task_pt_regs(task)))
1352 #endif
1353 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1354                 return &user_x86_32_view;
1355 #endif
1356 #ifdef CONFIG_X86_64
1357         return &user_x86_64_view;
1358 #endif
1359 }
1360
1361 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1362                                          int error_code, int si_code)
1363 {
1364         tsk->thread.trap_nr = X86_TRAP_DB;
1365         tsk->thread.error_code = error_code;
1366
1367         /* Send us the fake SIGTRAP */
1368         force_sig_fault(SIGTRAP, si_code,
1369                         user_mode(regs) ? (void __user *)regs->ip : NULL, tsk);
1370 }
1371
1372 void user_single_step_report(struct pt_regs *regs)
1373 {
1374         send_sigtrap(current, regs, 0, TRAP_BRKPT);
1375 }