blob: 511ea0f16078dfbe39279eb1e80e5467aecb2092 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* By Ross Biro 1/23/92 */
2/*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
5 */
6
7#include <linux/kernel.h>
8#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +01009#include <linux/sched/task_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
11#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ptrace.h>
Roland McGratheeea3c32008-03-16 23:36:28 -070015#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/user.h>
Roland McGrath070459d2008-01-30 13:31:53 +010017#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/security.h>
19#include <linux/audit.h>
20#include <linux/seccomp.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070021#include <linux/signal.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020022#include <linux/perf_event.h>
23#include <linux/hw_breakpoint.h>
Frederic Weisbeckerbf5a3c12012-07-11 20:26:34 +020024#include <linux/rcupdate.h>
Paul Gortmaker19348e742013-02-14 15:14:02 -050025#include <linux/export.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010026#include <linux/context_tracking.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/processor.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020031#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020032#include <asm/fpu/signal.h>
Ingo Molnar59a36d12015-04-30 08:53:18 +020033#include <asm/fpu/regset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/debugreg.h>
35#include <asm/ldt.h>
36#include <asm/desc.h>
Roland McGrath2047b082008-01-30 13:31:01 +010037#include <asm/prctl.h>
38#include <asm/proto.h>
K.Prasad72f674d2009-06-01 23:45:48 +053039#include <asm/hw_breakpoint.h>
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +053040#include <asm/traps.h>
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070041#include <asm/syscall.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010042
Roland McGrath070459d2008-01-30 13:31:53 +010043#include "tls.h"
44
45enum x86_regset {
46 REGSET_GENERAL,
47 REGSET_FP,
48 REGSET_XFP,
Roland McGrath325af5f2008-08-08 15:58:39 -070049 REGSET_IOPERM64 = REGSET_XFP,
Suresh Siddha5b3efd52010-02-11 11:50:59 -080050 REGSET_XSTATE,
Roland McGrath070459d2008-01-30 13:31:53 +010051 REGSET_TLS,
Roland McGrath325af5f2008-08-08 15:58:39 -070052 REGSET_IOPERM32,
Roland McGrath070459d2008-01-30 13:31:53 +010053};
Markus Metzgereee3af42008-01-30 13:31:09 +010054
Masami Hiramatsub1cf5402009-08-13 16:34:44 -040055struct pt_regs_offset {
56 const char *name;
57 int offset;
58};
59
60#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61#define REG_OFFSET_END {.name = NULL, .offset = 0}
62
63static const struct pt_regs_offset regoffset_table[] = {
64#ifdef CONFIG_X86_64
65 REG_OFFSET_NAME(r15),
66 REG_OFFSET_NAME(r14),
67 REG_OFFSET_NAME(r13),
68 REG_OFFSET_NAME(r12),
69 REG_OFFSET_NAME(r11),
70 REG_OFFSET_NAME(r10),
71 REG_OFFSET_NAME(r9),
72 REG_OFFSET_NAME(r8),
73#endif
74 REG_OFFSET_NAME(bx),
75 REG_OFFSET_NAME(cx),
76 REG_OFFSET_NAME(dx),
77 REG_OFFSET_NAME(si),
78 REG_OFFSET_NAME(di),
79 REG_OFFSET_NAME(bp),
80 REG_OFFSET_NAME(ax),
81#ifdef CONFIG_X86_32
82 REG_OFFSET_NAME(ds),
83 REG_OFFSET_NAME(es),
84 REG_OFFSET_NAME(fs),
85 REG_OFFSET_NAME(gs),
86#endif
87 REG_OFFSET_NAME(orig_ax),
88 REG_OFFSET_NAME(ip),
89 REG_OFFSET_NAME(cs),
90 REG_OFFSET_NAME(flags),
91 REG_OFFSET_NAME(sp),
92 REG_OFFSET_NAME(ss),
93 REG_OFFSET_END,
94};
95
96/**
97 * regs_query_register_offset() - query register offset from its name
98 * @name: the name of a register
99 *
100 * regs_query_register_offset() returns the offset of a register in struct
101 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
102 */
103int regs_query_register_offset(const char *name)
104{
105 const struct pt_regs_offset *roff;
106 for (roff = regoffset_table; roff->name != NULL; roff++)
107 if (!strcmp(roff->name, name))
108 return roff->offset;
109 return -EINVAL;
110}
111
112/**
113 * regs_query_register_name() - query register name from its offset
114 * @offset: the offset of a register in struct pt_regs.
115 *
116 * regs_query_register_name() returns the name of a register from its
117 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
118 */
119const char *regs_query_register_name(unsigned int offset)
120{
121 const struct pt_regs_offset *roff;
122 for (roff = regoffset_table; roff->name != NULL; roff++)
123 if (roff->offset == offset)
124 return roff->name;
125 return NULL;
126}
127
Markus Metzgereee3af42008-01-30 13:31:09 +0100128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * does not yet catch signals sent when the child dies.
130 * in exit.c or in signal.c.
131 */
132
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500133/*
134 * Determines which flags the user has access to [1 = access, 0 = no access].
Chuck Ebbert9f155b92006-01-05 23:11:29 -0500135 */
Roland McGrathe39c2892008-01-30 13:31:01 +0100136#define FLAG_MASK_32 ((unsigned long) \
137 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
138 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
139 X86_EFLAGS_SF | X86_EFLAGS_TF | \
140 X86_EFLAGS_DF | X86_EFLAGS_OF | \
141 X86_EFLAGS_RF | X86_EFLAGS_AC))
142
Roland McGrath2047b082008-01-30 13:31:01 +0100143/*
144 * Determines whether a value may be installed in a segment register.
145 */
146static inline bool invalid_selector(u16 value)
147{
148 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
149}
150
151#ifdef CONFIG_X86_32
152
Roland McGrathe39c2892008-01-30 13:31:01 +0100153#define FLAG_MASK FLAG_MASK_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Robert Richter10226232012-09-03 20:54:48 +0200155/*
156 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
157 * when it traps. The previous stack will be directly underneath the saved
158 * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
159 *
160 * Now, if the stack is empty, '&regs->sp' is out of range. In this
161 * case we try to take the previous stack. To always return a non-null
162 * stack pointer we fall back to regs as stack if no previous stack
163 * exists.
164 *
165 * This is valid only for kernel mode traps.
166 */
167unsigned long kernel_stack_pointer(struct pt_regs *regs)
168{
169 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
170 unsigned long sp = (unsigned long)&regs->sp;
Steven Rostedt0788aa62014-02-06 09:41:30 -0500171 u32 *prev_esp;
Robert Richter10226232012-09-03 20:54:48 +0200172
173 if (context == (sp & ~(THREAD_SIZE - 1)))
174 return sp;
175
Steven Rostedt198d2082014-02-06 09:41:31 -0500176 prev_esp = (u32 *)(context);
Josh Poimboeuf72b4f6a2016-08-18 10:59:06 -0500177 if (*prev_esp)
178 return (unsigned long)*prev_esp;
Robert Richter10226232012-09-03 20:54:48 +0200179
180 return (unsigned long)regs;
181}
H. Peter Anvincb57a2b2012-11-20 22:21:02 -0800182EXPORT_SYMBOL_GPL(kernel_stack_pointer);
Robert Richter10226232012-09-03 20:54:48 +0200183
Jaswinder Singh4fe702c2008-07-25 10:19:27 +0530184static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100186 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
Tejun Heoccbeed32009-02-09 22:17:40 +0900187 return &regs->bx + (regno >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Roland McGrath06ee1b62008-01-30 13:31:01 +0100190static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100192 /*
193 * Returning the value truncates it to 16 bits.
194 */
195 unsigned int retval;
196 if (offset != offsetof(struct user_regs_struct, gs))
197 retval = *pt_regs_access(task_pt_regs(task), offset);
198 else {
Roland McGrath06ee1b62008-01-30 13:31:01 +0100199 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900200 retval = get_user_gs(task_pt_regs(task));
201 else
202 retval = task_user_gs(task);
Roland McGrath06ee1b62008-01-30 13:31:01 +0100203 }
204 return retval;
205}
206
207static int set_segment_reg(struct task_struct *task,
208 unsigned long offset, u16 value)
209{
210 /*
211 * The value argument was already truncated to 16 bits.
212 */
Roland McGrath2047b082008-01-30 13:31:01 +0100213 if (invalid_selector(value))
Roland McGrath06ee1b62008-01-30 13:31:01 +0100214 return -EIO;
215
Roland McGrathc63855d2008-02-06 22:39:44 +0100216 /*
217 * For %cs and %ss we cannot permit a null selector.
218 * We can permit a bogus selector as long as it has USER_RPL.
219 * Null selectors are fine for other segment registers, but
220 * we will never get back to user mode with invalid %cs or %ss
221 * and will take the trap in iret instead. Much code relies
222 * on user_mode() to distinguish a user trap frame (which can
223 * safely use invalid selectors) from a kernel trap frame.
224 */
225 switch (offset) {
226 case offsetof(struct user_regs_struct, cs):
227 case offsetof(struct user_regs_struct, ss):
228 if (unlikely(value == 0))
229 return -EIO;
230
231 default:
Roland McGrath06ee1b62008-01-30 13:31:01 +0100232 *pt_regs_access(task_pt_regs(task), offset) = value;
Roland McGrathc63855d2008-02-06 22:39:44 +0100233 break;
234
235 case offsetof(struct user_regs_struct, gs):
Roland McGrath06ee1b62008-01-30 13:31:01 +0100236 if (task == current)
Tejun Heod9a89a22009-02-09 22:17:40 +0900237 set_user_gs(task_pt_regs(task), value);
238 else
239 task_user_gs(task) = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Roland McGrath06ee1b62008-01-30 13:31:01 +0100241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
Roland McGrath2047b082008-01-30 13:31:01 +0100245#else /* CONFIG_X86_64 */
246
247#define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
248
249static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
250{
251 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
252 return &regs->r15 + (offset / sizeof(regs->r15));
253}
254
255static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
256{
257 /*
258 * Returning the value truncates it to 16 bits.
259 */
260 unsigned int seg;
261
262 switch (offset) {
263 case offsetof(struct user_regs_struct, fs):
264 if (task == current) {
265 /* Older gas can't assemble movq %?s,%r?? */
266 asm("movl %%fs,%0" : "=r" (seg));
267 return seg;
268 }
269 return task->thread.fsindex;
270 case offsetof(struct user_regs_struct, gs):
271 if (task == current) {
272 asm("movl %%gs,%0" : "=r" (seg));
273 return seg;
274 }
275 return task->thread.gsindex;
276 case offsetof(struct user_regs_struct, ds):
277 if (task == current) {
278 asm("movl %%ds,%0" : "=r" (seg));
279 return seg;
280 }
281 return task->thread.ds;
282 case offsetof(struct user_regs_struct, es):
283 if (task == current) {
284 asm("movl %%es,%0" : "=r" (seg));
285 return seg;
286 }
287 return task->thread.es;
288
289 case offsetof(struct user_regs_struct, cs):
290 case offsetof(struct user_regs_struct, ss):
291 break;
292 }
293 return *pt_regs_access(task_pt_regs(task), offset);
294}
295
296static int set_segment_reg(struct task_struct *task,
297 unsigned long offset, u16 value)
298{
299 /*
300 * The value argument was already truncated to 16 bits.
301 */
302 if (invalid_selector(value))
303 return -EIO;
304
305 switch (offset) {
306 case offsetof(struct user_regs_struct,fs):
Roland McGrath2047b082008-01-30 13:31:01 +0100307 task->thread.fsindex = value;
308 if (task == current)
309 loadsegment(fs, task->thread.fsindex);
310 break;
311 case offsetof(struct user_regs_struct,gs):
Roland McGrath2047b082008-01-30 13:31:01 +0100312 task->thread.gsindex = value;
313 if (task == current)
314 load_gs_index(task->thread.gsindex);
315 break;
316 case offsetof(struct user_regs_struct,ds):
317 task->thread.ds = value;
318 if (task == current)
319 loadsegment(ds, task->thread.ds);
320 break;
321 case offsetof(struct user_regs_struct,es):
322 task->thread.es = value;
323 if (task == current)
324 loadsegment(es, task->thread.es);
325 break;
326
327 /*
328 * Can't actually change these in 64-bit mode.
329 */
330 case offsetof(struct user_regs_struct,cs):
Roland McGrathc63855d2008-02-06 22:39:44 +0100331 if (unlikely(value == 0))
332 return -EIO;
Andy Lutomirski08571f12015-02-24 16:01:38 -0800333 task_pt_regs(task)->cs = value;
Roland McGrathcb757c42008-01-30 13:31:01 +0100334 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100335 case offsetof(struct user_regs_struct,ss):
Roland McGrathc63855d2008-02-06 22:39:44 +0100336 if (unlikely(value == 0))
337 return -EIO;
Andy Lutomirski08571f12015-02-24 16:01:38 -0800338 task_pt_regs(task)->ss = value;
Roland McGrathcb757c42008-01-30 13:31:01 +0100339 break;
Roland McGrath2047b082008-01-30 13:31:01 +0100340 }
341
342 return 0;
343}
344
Roland McGrath2047b082008-01-30 13:31:01 +0100345#endif /* CONFIG_X86_32 */
346
Roland McGrath06ee1b62008-01-30 13:31:01 +0100347static unsigned long get_flags(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Roland McGrath06ee1b62008-01-30 13:31:01 +0100349 unsigned long retval = task_pt_regs(task)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Roland McGrath06ee1b62008-01-30 13:31:01 +0100351 /*
352 * If the debugger set TF, hide it from the readout.
353 */
354 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
355 retval &= ~X86_EFLAGS_TF;
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return retval;
358}
359
Roland McGrath06ee1b62008-01-30 13:31:01 +0100360static int set_flags(struct task_struct *task, unsigned long value)
361{
362 struct pt_regs *regs = task_pt_regs(task);
363
364 /*
365 * If the user value contains TF, mark that
366 * it was not "us" (the debugger) that set it.
367 * If not, make sure it stays set if we had.
368 */
369 if (value & X86_EFLAGS_TF)
370 clear_tsk_thread_flag(task, TIF_FORCED_TF);
371 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
372 value |= X86_EFLAGS_TF;
373
374 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
375
376 return 0;
377}
378
379static int putreg(struct task_struct *child,
380 unsigned long offset, unsigned long value)
381{
382 switch (offset) {
383 case offsetof(struct user_regs_struct, cs):
384 case offsetof(struct user_regs_struct, ds):
385 case offsetof(struct user_regs_struct, es):
386 case offsetof(struct user_regs_struct, fs):
387 case offsetof(struct user_regs_struct, gs):
388 case offsetof(struct user_regs_struct, ss):
389 return set_segment_reg(child, offset, value);
390
391 case offsetof(struct user_regs_struct, flags):
392 return set_flags(child, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100393
394#ifdef CONFIG_X86_64
395 case offsetof(struct user_regs_struct,fs_base):
Andy Lutomirskid696ca02016-05-10 09:18:46 -0700396 if (value >= TASK_SIZE_MAX)
Roland McGrath2047b082008-01-30 13:31:01 +0100397 return -EIO;
398 /*
Kyle Huey17a6e1b2017-03-20 01:16:22 -0700399 * When changing the segment base, use do_arch_prctl_64
Roland McGrath2047b082008-01-30 13:31:01 +0100400 * to set either thread.fs or thread.fsindex and the
401 * corresponding GDT slot.
402 */
Andy Lutomirski296f7812016-04-26 12:23:29 -0700403 if (child->thread.fsbase != value)
Kyle Huey17a6e1b2017-03-20 01:16:22 -0700404 return do_arch_prctl_64(child, ARCH_SET_FS, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100405 return 0;
406 case offsetof(struct user_regs_struct,gs_base):
407 /*
408 * Exactly the same here as the %fs handling above.
409 */
Andy Lutomirskid696ca02016-05-10 09:18:46 -0700410 if (value >= TASK_SIZE_MAX)
Roland McGrath2047b082008-01-30 13:31:01 +0100411 return -EIO;
Andy Lutomirski296f7812016-04-26 12:23:29 -0700412 if (child->thread.gsbase != value)
Kyle Huey17a6e1b2017-03-20 01:16:22 -0700413 return do_arch_prctl_64(child, ARCH_SET_GS, value);
Roland McGrath2047b082008-01-30 13:31:01 +0100414 return 0;
415#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100416 }
417
418 *pt_regs_access(task_pt_regs(child), offset) = value;
419 return 0;
420}
421
422static unsigned long getreg(struct task_struct *task, unsigned long offset)
423{
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);
432
433 case offsetof(struct user_regs_struct, flags):
434 return get_flags(task);
Roland McGrath2047b082008-01-30 13:31:01 +0100435
436#ifdef CONFIG_X86_64
437 case offsetof(struct user_regs_struct, fs_base): {
438 /*
Andy Lutomirski731e33e2016-04-26 12:23:28 -0700439 * XXX: This will not behave as expected if called on
440 * current or if fsindex != 0.
Roland McGrath2047b082008-01-30 13:31:01 +0100441 */
Andy Lutomirski296f7812016-04-26 12:23:29 -0700442 return task->thread.fsbase;
Roland McGrath2047b082008-01-30 13:31:01 +0100443 }
444 case offsetof(struct user_regs_struct, gs_base): {
445 /*
Andy Lutomirski731e33e2016-04-26 12:23:28 -0700446 * XXX: This will not behave as expected if called on
447 * current or if fsindex != 0.
Roland McGrath2047b082008-01-30 13:31:01 +0100448 */
Andy Lutomirski296f7812016-04-26 12:23:29 -0700449 return task->thread.gsbase;
Roland McGrath2047b082008-01-30 13:31:01 +0100450 }
451#endif
Roland McGrath06ee1b62008-01-30 13:31:01 +0100452 }
453
454 return *pt_regs_access(task_pt_regs(task), offset);
455}
456
Roland McGrath91e7b702008-01-30 13:31:52 +0100457static int genregs_get(struct task_struct *target,
458 const struct user_regset *regset,
459 unsigned int pos, unsigned int count,
460 void *kbuf, void __user *ubuf)
461{
462 if (kbuf) {
463 unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800464 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100465 *k++ = getreg(target, pos);
466 count -= sizeof(*k);
467 pos += sizeof(*k);
468 }
469 } else {
470 unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800471 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100472 if (__put_user(getreg(target, pos), u++))
473 return -EFAULT;
474 count -= sizeof(*u);
475 pos += sizeof(*u);
476 }
477 }
478
479 return 0;
480}
481
482static int genregs_set(struct task_struct *target,
483 const struct user_regset *regset,
484 unsigned int pos, unsigned int count,
485 const void *kbuf, const void __user *ubuf)
486{
487 int ret = 0;
488 if (kbuf) {
489 const unsigned long *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800490 while (count >= sizeof(*k) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100491 ret = putreg(target, pos, *k++);
492 count -= sizeof(*k);
493 pos += sizeof(*k);
494 }
495 } else {
496 const unsigned long __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -0800497 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +0100498 unsigned long word;
499 ret = __get_user(word, u++);
500 if (ret)
501 break;
502 ret = putreg(target, pos, word);
503 count -= sizeof(*u);
504 pos += sizeof(*u);
505 }
506 }
507 return ret;
508}
509
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200510static void ptrace_triggered(struct perf_event *bp,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100511 struct perf_sample_data *data,
512 struct pt_regs *regs)
Roland McGrathd9771e82008-01-30 13:30:52 +0100513{
Roland McGrath0f534092008-01-30 13:30:59 +0100514 int i;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200515 struct thread_struct *thread = &(current->thread);
Roland McGrath0f534092008-01-30 13:30:59 +0100516
K.Prasad72f674d2009-06-01 23:45:48 +0530517 /*
518 * Store in the virtual DR6 register the fact that the breakpoint
519 * was hit so the thread's debugger will see it.
520 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200521 for (i = 0; i < HBP_NUM; i++) {
522 if (thread->ptrace_bps[i] == bp)
K.Prasad72f674d2009-06-01 23:45:48 +0530523 break;
Roland McGrathd9771e82008-01-30 13:30:52 +0100524 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100525
K.Prasad72f674d2009-06-01 23:45:48 +0530526 thread->debugreg6 |= (DR_TRAP0 << i);
527}
528
529/*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200530 * Walk through every ptrace breakpoints for this thread and
531 * build the dr7 value on top of their attributes.
532 *
533 */
534static unsigned long ptrace_get_dr7(struct perf_event *bp[])
535{
536 int i;
537 int dr7 = 0;
538 struct arch_hw_breakpoint *info;
539
540 for (i = 0; i < HBP_NUM; i++) {
541 if (bp[i] && !bp[i]->attr.disabled) {
542 info = counter_arch_bp(bp[i]);
543 dr7 |= encode_dr7(i, info->len, info->type);
544 }
545 }
546
547 return dr7;
548}
549
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700550static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
551 int len, int type, bool disabled)
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100552{
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700553 int err, bp_len, bp_type;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100554
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700555 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
556 if (!err) {
557 attr->bp_len = bp_len;
558 attr->bp_type = bp_type;
559 attr->disabled = disabled;
560 }
561
562 return err;
563}
564
565static struct perf_event *
566ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
567 unsigned long addr, bool disabled)
568{
569 struct perf_event_attr attr;
570 int err;
571
572 ptrace_breakpoint_init(&attr);
573 attr.bp_addr = addr;
574
575 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
576 if (err)
577 return ERR_PTR(err);
578
579 return register_user_hw_breakpoint(&attr, ptrace_triggered,
580 NULL, tsk);
581}
582
583static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
584 int disabled)
585{
586 struct perf_event_attr attr = bp->attr;
587 int err;
588
589 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100590 if (err)
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100591 return err;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100592
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100593 return modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100594}
595
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200596/*
K.Prasad72f674d2009-06-01 23:45:48 +0530597 * Handle ptrace writes to debug register 7.
598 */
599static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
600{
Oleg Nesterov29a55512013-07-08 16:00:58 -0700601 struct thread_struct *thread = &tsk->thread;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200602 unsigned long old_dr7;
Oleg Nesterov29a55512013-07-08 16:00:58 -0700603 bool second_pass = false;
604 int i, rc, ret = 0;
K.Prasad72f674d2009-06-01 23:45:48 +0530605
606 data &= ~DR_CONTROL_RESERVED;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200607 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
Oleg Nesterov29a55512013-07-08 16:00:58 -0700608
K.Prasad72f674d2009-06-01 23:45:48 +0530609restore:
Oleg Nesterov29a55512013-07-08 16:00:58 -0700610 rc = 0;
K.Prasad72f674d2009-06-01 23:45:48 +0530611 for (i = 0; i < HBP_NUM; i++) {
Oleg Nesterove6a7d602013-07-08 16:00:56 -0700612 unsigned len, type;
613 bool disabled = !decode_dr7(data, i, &len, &type);
614 struct perf_event *bp = thread->ptrace_bps[i];
K.Prasad72f674d2009-06-01 23:45:48 +0530615
Oleg Nesterov29a55512013-07-08 16:00:58 -0700616 if (!bp) {
617 if (disabled)
Oleg Nesterove6a7d602013-07-08 16:00:56 -0700618 continue;
Oleg Nesterovb87a95a2013-07-08 16:01:01 -0700619
620 bp = ptrace_register_breakpoint(tsk,
621 len, type, 0, disabled);
622 if (IS_ERR(bp)) {
623 rc = PTR_ERR(bp);
624 break;
625 }
626
627 thread->ptrace_bps[i] = bp;
628 continue;
K.Prasad72f674d2009-06-01 23:45:48 +0530629 }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200630
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700631 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100632 if (rc)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200633 break;
K.Prasad72f674d2009-06-01 23:45:48 +0530634 }
Oleg Nesterov29a55512013-07-08 16:00:58 -0700635
636 /* Restore if the first pass failed, second_pass shouldn't fail. */
637 if (rc && !WARN_ON(second_pass)) {
638 ret = rc;
639 data = old_dr7;
640 second_pass = true;
K.Prasad72f674d2009-06-01 23:45:48 +0530641 goto restore;
642 }
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200643
Oleg Nesterov29a55512013-07-08 16:00:58 -0700644 return ret;
K.Prasad72f674d2009-06-01 23:45:48 +0530645}
646
647/*
648 * Handle PTRACE_PEEKUSR calls for the debug register area.
649 */
Jaswinder Singh Rajput9d22b532009-07-01 19:52:30 +0530650static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
K.Prasad72f674d2009-06-01 23:45:48 +0530651{
Oleg Nesterov61e305c2013-07-08 16:01:03 -0700652 struct thread_struct *thread = &tsk->thread;
K.Prasad72f674d2009-06-01 23:45:48 +0530653 unsigned long val = 0;
654
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200655 if (n < HBP_NUM) {
Oleg Nesterov02be46f2013-07-08 16:00:47 -0700656 struct perf_event *bp = thread->ptrace_bps[n];
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200657
Oleg Nesterov02be46f2013-07-08 16:00:47 -0700658 if (bp)
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200659 val = bp->hw.info.address;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200660 } else if (n == 6) {
K.Prasad72f674d2009-06-01 23:45:48 +0530661 val = thread->debugreg6;
Oleg Nesterov61e305c2013-07-08 16:01:03 -0700662 } else if (n == 7) {
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100663 val = thread->ptrace_dr7;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200664 }
K.Prasad72f674d2009-06-01 23:45:48 +0530665 return val;
666}
667
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200668static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
669 unsigned long addr)
670{
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200671 struct thread_struct *t = &tsk->thread;
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700672 struct perf_event *bp = t->ptrace_bps[nr];
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200673 int err = 0;
674
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700675 if (!bp) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200676 /*
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700677 * Put stub len and type to create an inactive but correct bp.
678 *
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100679 * CHECKME: the previous code returned -EIO if the addr wasn't
680 * a valid task virtual addr. The new one will return -EINVAL in
681 * this case.
682 * -EINVAL may be what we want for in-kernel breakpoints users,
683 * but -EIO looks better for ptrace, since we refuse a register
684 * writing for the user. And anyway this is the previous
685 * behaviour.
686 */
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700687 bp = ptrace_register_breakpoint(tsk,
688 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
689 addr, true);
690 if (IS_ERR(bp))
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200691 err = PTR_ERR(bp);
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700692 else
693 t->ptrace_bps[nr] = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200694 } else {
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700695 struct perf_event_attr attr = bp->attr;
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100696
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100697 attr.bp_addr = addr;
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100698 err = modify_user_hw_breakpoint(bp, &attr);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200699 }
Oleg Nesterov9afe33a2013-07-08 16:00:59 -0700700
Frederic Weisbecker87dc6692011-04-08 17:29:36 +0200701 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
K.Prasad72f674d2009-06-01 23:45:48 +0530704/*
705 * Handle PTRACE_POKEUSR calls for the debug register area.
706 */
H Hartley Sweeten98b8b992011-11-15 14:48:58 -0800707static int ptrace_set_debugreg(struct task_struct *tsk, int n,
708 unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Oleg Nesterov61e305c2013-07-08 16:01:03 -0700710 struct thread_struct *thread = &tsk->thread;
K.Prasad72f674d2009-06-01 23:45:48 +0530711 /* There are no DR4 or DR5 registers */
Oleg Nesterov61e305c2013-07-08 16:01:03 -0700712 int rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
K.Prasad72f674d2009-06-01 23:45:48 +0530714 if (n < HBP_NUM) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200715 rc = ptrace_set_breakpoint_addr(tsk, n, val);
Oleg Nesterov61e305c2013-07-08 16:01:03 -0700716 } else if (n == 6) {
717 thread->debugreg6 = val;
718 rc = 0;
719 } else if (n == 7) {
K.Prasad72f674d2009-06-01 23:45:48 +0530720 rc = ptrace_write_dr7(tsk, val);
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100721 if (!rc)
722 thread->ptrace_dr7 = val;
723 }
K.Prasad72f674d2009-06-01 23:45:48 +0530724 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Roland McGrath325af5f2008-08-08 15:58:39 -0700727/*
728 * These access the current or another (stopped) task's io permission
729 * bitmap for debugging or core dump.
730 */
731static int ioperm_active(struct task_struct *target,
732 const struct user_regset *regset)
Markus Metzgereee3af42008-01-30 13:31:09 +0100733{
Roland McGrath325af5f2008-08-08 15:58:39 -0700734 return target->thread.io_bitmap_max / regset->size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100735}
736
Roland McGrath325af5f2008-08-08 15:58:39 -0700737static int ioperm_get(struct task_struct *target,
738 const struct user_regset *regset,
739 unsigned int pos, unsigned int count,
740 void *kbuf, void __user *ubuf)
741{
742 if (!target->thread.io_bitmap_ptr)
743 return -ENXIO;
744
745 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
746 target->thread.io_bitmap_ptr,
747 0, IO_BITMAP_BYTES);
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/*
751 * Called by kernel/ptrace.c when detaching..
752 *
753 * Make sure the single step bit is not set.
754 */
755void ptrace_disable(struct task_struct *child)
756{
757 user_disable_single_step(child);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100758#ifdef TIF_SYSCALL_EMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Roland McGrath5a4646a2008-01-30 13:31:54 +0100763#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
764static const struct user_regset_view user_x86_32_view; /* Initialized below. */
765#endif
766
Namhyung Kim9b05a692010-10-27 15:33:47 -0700767long arch_ptrace(struct task_struct *child, long request,
768 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Roland McGrath5a4646a2008-01-30 13:31:54 +0100770 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 unsigned long __user *datap = (unsigned long __user *)data;
772
773 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* read the word at location addr in the USER area. */
775 case PTRACE_PEEKUSR: {
776 unsigned long tmp;
777
778 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700779 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781
782 tmp = 0; /* Default return condition */
Roland McGrathe9c86c72008-01-30 13:31:01 +0100783 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 tmp = getreg(child, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100785 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
786 addr <= offsetof(struct user, u_debugreg[7])) {
787 addr -= offsetof(struct user, u_debugreg[0]);
788 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 ret = put_user(tmp, datap);
791 break;
792 }
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
795 ret = -EIO;
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700796 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 break;
798
Roland McGrathe9c86c72008-01-30 13:31:01 +0100799 if (addr < sizeof(struct user_regs_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ret = putreg(child, addr, data);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100801 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
802 addr <= offsetof(struct user, u_debugreg[7])) {
803 addr -= offsetof(struct user, u_debugreg[0]);
804 ret = ptrace_set_debugreg(child,
805 addr / sizeof(data), data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Roland McGrathe9c86c72008-01-30 13:31:01 +0100807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Roland McGrath5a4646a2008-01-30 13:31:54 +0100809 case PTRACE_GETREGS: /* Get all gp regs from the child. */
810 return copy_regset_to_user(child,
811 task_user_regset_view(current),
812 REGSET_GENERAL,
813 0, sizeof(struct user_regs_struct),
814 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Roland McGrath5a4646a2008-01-30 13:31:54 +0100816 case PTRACE_SETREGS: /* Set all gp regs in the child. */
817 return copy_regset_from_user(child,
818 task_user_regset_view(current),
819 REGSET_GENERAL,
820 0, sizeof(struct user_regs_struct),
821 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Roland McGrath5a4646a2008-01-30 13:31:54 +0100823 case PTRACE_GETFPREGS: /* Get the child FPU state. */
824 return copy_regset_to_user(child,
825 task_user_regset_view(current),
826 REGSET_FP,
827 0, sizeof(struct user_i387_struct),
828 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Roland McGrath5a4646a2008-01-30 13:31:54 +0100830 case PTRACE_SETFPREGS: /* Set the child FPU state. */
831 return copy_regset_from_user(child,
832 task_user_regset_view(current),
833 REGSET_FP,
834 0, sizeof(struct user_i387_struct),
835 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Roland McGrathe9c86c72008-01-30 13:31:01 +0100837#ifdef CONFIG_X86_32
Roland McGrath5a4646a2008-01-30 13:31:54 +0100838 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
839 return copy_regset_to_user(child, &user_x86_32_view,
840 REGSET_XFP,
841 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700842 datap) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Roland McGrath5a4646a2008-01-30 13:31:54 +0100844 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
845 return copy_regset_from_user(child, &user_x86_32_view,
846 REGSET_XFP,
847 0, sizeof(struct user_fxsr_struct),
Roland McGrath45fdc3a2008-06-30 14:02:41 -0700848 datap) ? -EIO : 0;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Roland McGrathe9c86c72008-01-30 13:31:01 +0100851#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 case PTRACE_GET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700853 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100854 return -EIO;
855 ret = do_get_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700856 (struct user_desc __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 break;
858
859 case PTRACE_SET_THREAD_AREA:
Namhyung Kim9b05a692010-10-27 15:33:47 -0700860 if ((int) addr < 0)
Roland McGrathefd1ca52008-01-30 13:30:46 +0100861 return -EIO;
862 ret = do_set_thread_area(child, addr,
Namhyung Kimeb5a3692010-10-27 15:33:48 -0700863 (struct user_desc __user *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
Roland McGrathe9c86c72008-01-30 13:31:01 +0100865#endif
866
867#ifdef CONFIG_X86_64
868 /* normal 64bit interface to access TLS data.
869 Works just like arch_prctl, except that the arguments
870 are reversed. */
871 case PTRACE_ARCH_PRCTL:
Kyle Huey17a6e1b2017-03-20 01:16:22 -0700872 ret = do_arch_prctl_64(child, data, addr);
Roland McGrathe9c86c72008-01-30 13:31:01 +0100873 break;
874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 default:
877 ret = ptrace_request(child, request, addr, data);
878 break;
879 }
Roland McGrathd9771e82008-01-30 13:30:52 +0100880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return ret;
882}
883
Roland McGrathcb757c42008-01-30 13:31:01 +0100884#ifdef CONFIG_IA32_EMULATION
885
Roland McGrath099cd6e2008-01-30 13:31:01 +0100886#include <linux/compat.h>
887#include <linux/syscalls.h>
888#include <asm/ia32.h>
Roland McGrathcb757c42008-01-30 13:31:01 +0100889#include <asm/user32.h>
890
891#define R32(l,q) \
892 case offsetof(struct user32, regs.l): \
893 regs->q = value; break
894
895#define SEG32(rs) \
896 case offsetof(struct user32, regs.rs): \
897 return set_segment_reg(child, \
898 offsetof(struct user_regs_struct, rs), \
899 value); \
900 break
901
902static int putreg32(struct task_struct *child, unsigned regno, u32 value)
903{
904 struct pt_regs *regs = task_pt_regs(child);
905
906 switch (regno) {
907
908 SEG32(cs);
909 SEG32(ds);
910 SEG32(es);
911 SEG32(fs);
912 SEG32(gs);
913 SEG32(ss);
914
915 R32(ebx, bx);
916 R32(ecx, cx);
917 R32(edx, dx);
918 R32(edi, di);
919 R32(esi, si);
920 R32(ebp, bp);
921 R32(eax, ax);
Roland McGrathcb757c42008-01-30 13:31:01 +0100922 R32(eip, ip);
923 R32(esp, sp);
924
Roland McGrath40f09332008-02-28 19:57:07 -0800925 case offsetof(struct user32, regs.orig_eax):
926 /*
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700927 * Warning: bizarre corner case fixup here. A 32-bit
928 * debugger setting orig_eax to -1 wants to disable
929 * syscall restart. Make sure that the syscall
930 * restart code sign-extends orig_ax. Also make sure
931 * we interpret the -ERESTART* codes correctly if
932 * loaded into regs->ax in case the task is not
933 * actually still sitting at the exit from a 32-bit
934 * syscall with TS_COMPAT still set.
Roland McGrath40f09332008-02-28 19:57:07 -0800935 */
Roland McGrath8cb3ed132009-09-22 20:12:07 -0700936 regs->orig_ax = value;
937 if (syscall_get_nr(child, regs) >= 0)
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -0800938 child->thread_info.status |= TS_I386_REGS_POKED;
Roland McGrath40f09332008-02-28 19:57:07 -0800939 break;
940
Roland McGrathcb757c42008-01-30 13:31:01 +0100941 case offsetof(struct user32, regs.eflags):
942 return set_flags(child, value);
943
944 case offsetof(struct user32, u_debugreg[0]) ...
945 offsetof(struct user32, u_debugreg[7]):
946 regno -= offsetof(struct user32, u_debugreg[0]);
947 return ptrace_set_debugreg(child, regno / 4, value);
948
949 default:
950 if (regno > sizeof(struct user32) || (regno & 3))
951 return -EIO;
952
953 /*
954 * Other dummy fields in the virtual user structure
955 * are ignored
956 */
957 break;
958 }
959 return 0;
960}
961
962#undef R32
963#undef SEG32
964
965#define R32(l,q) \
966 case offsetof(struct user32, regs.l): \
967 *val = regs->q; break
968
969#define SEG32(rs) \
970 case offsetof(struct user32, regs.rs): \
971 *val = get_segment_reg(child, \
972 offsetof(struct user_regs_struct, rs)); \
973 break
974
975static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
976{
977 struct pt_regs *regs = task_pt_regs(child);
978
979 switch (regno) {
980
981 SEG32(ds);
982 SEG32(es);
983 SEG32(fs);
984 SEG32(gs);
985
986 R32(cs, cs);
987 R32(ss, ss);
988 R32(ebx, bx);
989 R32(ecx, cx);
990 R32(edx, dx);
991 R32(edi, di);
992 R32(esi, si);
993 R32(ebp, bp);
994 R32(eax, ax);
995 R32(orig_eax, orig_ax);
996 R32(eip, ip);
997 R32(esp, sp);
998
999 case offsetof(struct user32, regs.eflags):
1000 *val = get_flags(child);
1001 break;
1002
1003 case offsetof(struct user32, u_debugreg[0]) ...
1004 offsetof(struct user32, u_debugreg[7]):
1005 regno -= offsetof(struct user32, u_debugreg[0]);
1006 *val = ptrace_get_debugreg(child, regno / 4);
1007 break;
1008
1009 default:
1010 if (regno > sizeof(struct user32) || (regno & 3))
1011 return -EIO;
1012
1013 /*
1014 * Other dummy fields in the virtual user structure
1015 * are ignored
1016 */
1017 *val = 0;
1018 break;
1019 }
1020 return 0;
1021}
1022
1023#undef R32
1024#undef SEG32
1025
Roland McGrath91e7b702008-01-30 13:31:52 +01001026static int genregs32_get(struct task_struct *target,
1027 const struct user_regset *regset,
1028 unsigned int pos, unsigned int count,
1029 void *kbuf, void __user *ubuf)
1030{
1031 if (kbuf) {
1032 compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001033 while (count >= sizeof(*k)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001034 getreg32(target, pos, k++);
1035 count -= sizeof(*k);
1036 pos += sizeof(*k);
1037 }
1038 } else {
1039 compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001040 while (count >= sizeof(*u)) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001041 compat_ulong_t word;
1042 getreg32(target, pos, &word);
1043 if (__put_user(word, u++))
1044 return -EFAULT;
1045 count -= sizeof(*u);
1046 pos += sizeof(*u);
1047 }
1048 }
1049
1050 return 0;
1051}
1052
1053static int genregs32_set(struct task_struct *target,
1054 const struct user_regset *regset,
1055 unsigned int pos, unsigned int count,
1056 const void *kbuf, const void __user *ubuf)
1057{
1058 int ret = 0;
1059 if (kbuf) {
1060 const compat_ulong_t *k = kbuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001061 while (count >= sizeof(*k) && !ret) {
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001062 ret = putreg32(target, pos, *k++);
Roland McGrath91e7b702008-01-30 13:31:52 +01001063 count -= sizeof(*k);
1064 pos += sizeof(*k);
1065 }
1066 } else {
1067 const compat_ulong_t __user *u = ubuf;
Linus Torvalds04a1e622009-12-17 07:04:56 -08001068 while (count >= sizeof(*u) && !ret) {
Roland McGrath91e7b702008-01-30 13:31:52 +01001069 compat_ulong_t word;
1070 ret = __get_user(word, u++);
1071 if (ret)
1072 break;
Roland McGrathf9cb02b2008-02-21 20:37:24 -08001073 ret = putreg32(target, pos, word);
Roland McGrath91e7b702008-01-30 13:31:52 +01001074 count -= sizeof(*u);
1075 pos += sizeof(*u);
1076 }
1077 }
1078 return ret;
1079}
1080
Brian Gerst601275c2015-06-22 07:55:14 -04001081static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1082 compat_ulong_t caddr, compat_ulong_t cdata)
1083{
1084 unsigned long addr = caddr;
1085 unsigned long data = cdata;
1086 void __user *datap = compat_ptr(data);
1087 int ret;
1088 __u32 val;
1089
1090 switch (request) {
1091 case PTRACE_PEEKUSR:
1092 ret = getreg32(child, addr, &val);
1093 if (ret == 0)
1094 ret = put_user(val, (__u32 __user *)datap);
1095 break;
1096
1097 case PTRACE_POKEUSR:
1098 ret = putreg32(child, addr, data);
1099 break;
1100
1101 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1102 return copy_regset_to_user(child, &user_x86_32_view,
1103 REGSET_GENERAL,
1104 0, sizeof(struct user_regs_struct32),
1105 datap);
1106
1107 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1108 return copy_regset_from_user(child, &user_x86_32_view,
1109 REGSET_GENERAL, 0,
1110 sizeof(struct user_regs_struct32),
1111 datap);
1112
1113 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1114 return copy_regset_to_user(child, &user_x86_32_view,
1115 REGSET_FP, 0,
1116 sizeof(struct user_i387_ia32_struct),
1117 datap);
1118
1119 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1120 return copy_regset_from_user(
1121 child, &user_x86_32_view, REGSET_FP,
1122 0, sizeof(struct user_i387_ia32_struct), datap);
1123
1124 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1125 return copy_regset_to_user(child, &user_x86_32_view,
1126 REGSET_XFP, 0,
1127 sizeof(struct user32_fxsr_struct),
1128 datap);
1129
1130 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1131 return copy_regset_from_user(child, &user_x86_32_view,
1132 REGSET_XFP, 0,
1133 sizeof(struct user32_fxsr_struct),
1134 datap);
1135
1136 case PTRACE_GET_THREAD_AREA:
1137 case PTRACE_SET_THREAD_AREA:
1138 return arch_ptrace(child, request, addr, data);
1139
1140 default:
1141 return compat_ptrace_request(child, request, addr, data);
1142 }
1143
1144 return ret;
1145}
1146#endif /* CONFIG_IA32_EMULATION */
1147
H.J. Lu55283e22012-03-05 15:32:11 -08001148#ifdef CONFIG_X86_X32_ABI
1149static long x32_arch_ptrace(struct task_struct *child,
1150 compat_long_t request, compat_ulong_t caddr,
1151 compat_ulong_t cdata)
1152{
1153 unsigned long addr = caddr;
1154 unsigned long data = cdata;
1155 void __user *datap = compat_ptr(data);
1156 int ret;
1157
1158 switch (request) {
1159 /* Read 32bits at location addr in the USER area. Only allow
1160 to return the lower 32bits of segment and debug registers. */
1161 case PTRACE_PEEKUSR: {
1162 u32 tmp;
1163
1164 ret = -EIO;
1165 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1166 addr < offsetof(struct user_regs_struct, cs))
1167 break;
1168
1169 tmp = 0; /* Default return condition */
1170 if (addr < sizeof(struct user_regs_struct))
1171 tmp = getreg(child, addr);
1172 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1173 addr <= offsetof(struct user, u_debugreg[7])) {
1174 addr -= offsetof(struct user, u_debugreg[0]);
1175 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1176 }
1177 ret = put_user(tmp, (__u32 __user *)datap);
1178 break;
1179 }
1180
1181 /* Write the word at location addr in the USER area. Only allow
1182 to update segment and debug registers with the upper 32bits
1183 zero-extended. */
1184 case PTRACE_POKEUSR:
1185 ret = -EIO;
1186 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1187 addr < offsetof(struct user_regs_struct, cs))
1188 break;
1189
1190 if (addr < sizeof(struct user_regs_struct))
1191 ret = putreg(child, addr, data);
1192 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1193 addr <= offsetof(struct user, u_debugreg[7])) {
1194 addr -= offsetof(struct user, u_debugreg[0]);
1195 ret = ptrace_set_debugreg(child,
1196 addr / sizeof(data), data);
1197 }
1198 break;
1199
1200 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1201 return copy_regset_to_user(child,
1202 task_user_regset_view(current),
1203 REGSET_GENERAL,
1204 0, sizeof(struct user_regs_struct),
1205 datap);
1206
1207 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1208 return copy_regset_from_user(child,
1209 task_user_regset_view(current),
1210 REGSET_GENERAL,
1211 0, sizeof(struct user_regs_struct),
1212 datap);
1213
1214 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1215 return copy_regset_to_user(child,
1216 task_user_regset_view(current),
1217 REGSET_FP,
1218 0, sizeof(struct user_i387_struct),
1219 datap);
1220
1221 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1222 return copy_regset_from_user(child,
1223 task_user_regset_view(current),
1224 REGSET_FP,
1225 0, sizeof(struct user_i387_struct),
1226 datap);
1227
H.J. Lu55283e22012-03-05 15:32:11 -08001228 default:
1229 return compat_ptrace_request(child, request, addr, data);
1230 }
1231
1232 return ret;
1233}
1234#endif
1235
Brian Gerst601275c2015-06-22 07:55:14 -04001236#ifdef CONFIG_COMPAT
Roland McGrath562b80b2008-04-22 12:21:25 -07001237long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1238 compat_ulong_t caddr, compat_ulong_t cdata)
Roland McGrath099cd6e2008-01-30 13:31:01 +01001239{
H.J. Lu55283e22012-03-05 15:32:11 -08001240#ifdef CONFIG_X86_X32_ABI
Dmitry Safonovabfb9492016-04-18 16:43:43 +03001241 if (!in_ia32_syscall())
H.J. Lu55283e22012-03-05 15:32:11 -08001242 return x32_arch_ptrace(child, request, caddr, cdata);
1243#endif
Brian Gerst601275c2015-06-22 07:55:14 -04001244#ifdef CONFIG_IA32_EMULATION
1245 return ia32_arch_ptrace(child, request, caddr, cdata);
1246#else
1247 return 0;
1248#endif
Roland McGrath099cd6e2008-01-30 13:31:01 +01001249}
Brian Gerst601275c2015-06-22 07:55:14 -04001250#endif /* CONFIG_COMPAT */
Roland McGrathcb757c42008-01-30 13:31:01 +01001251
Roland McGrath070459d2008-01-30 13:31:53 +01001252#ifdef CONFIG_X86_64
1253
Kees Cook404f6aa2016-08-08 16:29:06 -07001254static struct user_regset x86_64_regsets[] __ro_after_init = {
Roland McGrath070459d2008-01-30 13:31:53 +01001255 [REGSET_GENERAL] = {
1256 .core_note_type = NT_PRSTATUS,
1257 .n = sizeof(struct user_regs_struct) / sizeof(long),
1258 .size = sizeof(long), .align = sizeof(long),
1259 .get = genregs_get, .set = genregs_set
1260 },
1261 [REGSET_FP] = {
1262 .core_note_type = NT_PRFPREG,
1263 .n = sizeof(struct user_i387_struct) / sizeof(long),
1264 .size = sizeof(long), .align = sizeof(long),
Ingo Molnar678eaf62015-04-24 14:48:24 +02001265 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
Roland McGrath070459d2008-01-30 13:31:53 +01001266 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001267 [REGSET_XSTATE] = {
1268 .core_note_type = NT_X86_XSTATE,
1269 .size = sizeof(u64), .align = sizeof(u64),
1270 .active = xstateregs_active, .get = xstateregs_get,
1271 .set = xstateregs_set
1272 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001273 [REGSET_IOPERM64] = {
1274 .core_note_type = NT_386_IOPERM,
1275 .n = IO_BITMAP_LONGS,
1276 .size = sizeof(long), .align = sizeof(long),
1277 .active = ioperm_active, .get = ioperm_get
1278 },
Roland McGrath070459d2008-01-30 13:31:53 +01001279};
1280
1281static const struct user_regset_view user_x86_64_view = {
1282 .name = "x86_64", .e_machine = EM_X86_64,
1283 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1284};
1285
1286#else /* CONFIG_X86_32 */
1287
1288#define user_regs_struct32 user_regs_struct
1289#define genregs32_get genregs_get
1290#define genregs32_set genregs_set
1291
1292#endif /* CONFIG_X86_64 */
1293
1294#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Kees Cook404f6aa2016-08-08 16:29:06 -07001295static struct user_regset x86_32_regsets[] __ro_after_init = {
Roland McGrath070459d2008-01-30 13:31:53 +01001296 [REGSET_GENERAL] = {
1297 .core_note_type = NT_PRSTATUS,
1298 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1299 .size = sizeof(u32), .align = sizeof(u32),
1300 .get = genregs32_get, .set = genregs32_set
1301 },
1302 [REGSET_FP] = {
1303 .core_note_type = NT_PRFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001304 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001305 .size = sizeof(u32), .align = sizeof(u32),
Ingo Molnar678eaf62015-04-24 14:48:24 +02001306 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
Roland McGrath070459d2008-01-30 13:31:53 +01001307 },
1308 [REGSET_XFP] = {
1309 .core_note_type = NT_PRXFPREG,
Roland McGrath1f465f42008-05-09 15:43:44 -07001310 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
Roland McGrath070459d2008-01-30 13:31:53 +01001311 .size = sizeof(u32), .align = sizeof(u32),
Ingo Molnar678eaf62015-04-24 14:48:24 +02001312 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
Roland McGrath070459d2008-01-30 13:31:53 +01001313 },
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001314 [REGSET_XSTATE] = {
1315 .core_note_type = NT_X86_XSTATE,
1316 .size = sizeof(u64), .align = sizeof(u64),
1317 .active = xstateregs_active, .get = xstateregs_get,
1318 .set = xstateregs_set
1319 },
Roland McGrath070459d2008-01-30 13:31:53 +01001320 [REGSET_TLS] = {
Roland McGrathbb616822008-01-30 13:31:56 +01001321 .core_note_type = NT_386_TLS,
Roland McGrath070459d2008-01-30 13:31:53 +01001322 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1323 .size = sizeof(struct user_desc),
1324 .align = sizeof(struct user_desc),
1325 .active = regset_tls_active,
1326 .get = regset_tls_get, .set = regset_tls_set
1327 },
Roland McGrath325af5f2008-08-08 15:58:39 -07001328 [REGSET_IOPERM32] = {
1329 .core_note_type = NT_386_IOPERM,
1330 .n = IO_BITMAP_BYTES / sizeof(u32),
1331 .size = sizeof(u32), .align = sizeof(u32),
1332 .active = ioperm_active, .get = ioperm_get
1333 },
Roland McGrath070459d2008-01-30 13:31:53 +01001334};
1335
1336static const struct user_regset_view user_x86_32_view = {
1337 .name = "i386", .e_machine = EM_386,
1338 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1339};
1340#endif
1341
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001342/*
1343 * This represents bytes 464..511 in the memory layout exported through
1344 * the REGSET_XSTATE interface.
1345 */
1346u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1347
Kees Cook404f6aa2016-08-08 16:29:06 -07001348void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
Suresh Siddha5b3efd52010-02-11 11:50:59 -08001349{
1350#ifdef CONFIG_X86_64
1351 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1352#endif
1353#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1354 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1355#endif
1356 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1357}
1358
Roland McGrath070459d2008-01-30 13:31:53 +01001359const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1360{
1361#ifdef CONFIG_IA32_EMULATION
Dmitry Safonovcc873242016-09-05 16:33:07 +03001362 if (!user_64bit_mode(task_pt_regs(task)))
Roland McGrath070459d2008-01-30 13:31:53 +01001363#endif
1364#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1365 return &user_x86_32_view;
1366#endif
1367#ifdef CONFIG_X86_64
1368 return &user_x86_64_view;
1369#endif
1370}
1371
Srinivasa Dsda654b72008-09-23 15:23:52 +05301372void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1373 int error_code, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct siginfo info;
1376
Eric W. Biederman3eb0f512018-04-17 15:26:37 -05001377 clear_siginfo(&info);
Eric W. Biederman73f297a2018-04-16 14:22:17 -05001378 tsk->thread.trap_nr = X86_TRAP_DB;
1379 tsk->thread.error_code = error_code;
1380
1381 info.si_signo = SIGTRAP;
1382 info.si_code = si_code;
1383 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
1384
Simon Arlott27b46d72007-10-20 01:13:56 +02001385 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 force_sig_info(SIGTRAP, &info, tsk);
1387}
Eric W. Biedermanefc463a2018-04-16 14:18:26 -05001388
1389void user_single_step_report(struct pt_regs *regs)
1390{
1391 send_sigtrap(current, regs, 0, TRAP_BRKPT);
1392}