Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* 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> | |
9 | #include <linux/mm.h> | |
10 | #include <linux/smp.h> | |
1da177e4 | 11 | #include <linux/errno.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
1da177e4 | 13 | #include <linux/ptrace.h> |
91e7b707 | 14 | #include <linux/regset.h> |
eeea3c3f | 15 | #include <linux/tracehook.h> |
1da177e4 | 16 | #include <linux/user.h> |
070459d9 | 17 | #include <linux/elf.h> |
1da177e4 LT |
18 | #include <linux/security.h> |
19 | #include <linux/audit.h> | |
20 | #include <linux/seccomp.h> | |
7ed20e1a | 21 | #include <linux/signal.h> |
24f1e32c FW |
22 | #include <linux/perf_event.h> |
23 | #include <linux/hw_breakpoint.h> | |
bf5a3c13 | 24 | #include <linux/rcupdate.h> |
1da177e4 LT |
25 | |
26 | #include <asm/uaccess.h> | |
27 | #include <asm/pgtable.h> | |
1da177e4 LT |
28 | #include <asm/processor.h> |
29 | #include <asm/i387.h> | |
1361b83a | 30 | #include <asm/fpu-internal.h> |
1da177e4 LT |
31 | #include <asm/debugreg.h> |
32 | #include <asm/ldt.h> | |
33 | #include <asm/desc.h> | |
2047b08b RM |
34 | #include <asm/prctl.h> |
35 | #include <asm/proto.h> | |
72f674d2 | 36 | #include <asm/hw_breakpoint.h> |
51e7dc70 | 37 | #include <asm/traps.h> |
eee3af4a | 38 | |
070459d9 RM |
39 | #include "tls.h" |
40 | ||
1c569f02 JS |
41 | #define CREATE_TRACE_POINTS |
42 | #include <trace/events/syscalls.h> | |
43 | ||
070459d9 RM |
44 | enum x86_regset { |
45 | REGSET_GENERAL, | |
46 | REGSET_FP, | |
47 | REGSET_XFP, | |
325af5fb | 48 | REGSET_IOPERM64 = REGSET_XFP, |
5b3efd50 | 49 | REGSET_XSTATE, |
070459d9 | 50 | REGSET_TLS, |
325af5fb | 51 | REGSET_IOPERM32, |
070459d9 | 52 | }; |
eee3af4a | 53 | |
b1cf540f MH |
54 | struct pt_regs_offset { |
55 | const char *name; | |
56 | int offset; | |
57 | }; | |
58 | ||
59 | #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)} | |
60 | #define REG_OFFSET_END {.name = NULL, .offset = 0} | |
61 | ||
62 | static const struct pt_regs_offset regoffset_table[] = { | |
63 | #ifdef CONFIG_X86_64 | |
64 | REG_OFFSET_NAME(r15), | |
65 | REG_OFFSET_NAME(r14), | |
66 | REG_OFFSET_NAME(r13), | |
67 | REG_OFFSET_NAME(r12), | |
68 | REG_OFFSET_NAME(r11), | |
69 | REG_OFFSET_NAME(r10), | |
70 | REG_OFFSET_NAME(r9), | |
71 | REG_OFFSET_NAME(r8), | |
72 | #endif | |
73 | REG_OFFSET_NAME(bx), | |
74 | REG_OFFSET_NAME(cx), | |
75 | REG_OFFSET_NAME(dx), | |
76 | REG_OFFSET_NAME(si), | |
77 | REG_OFFSET_NAME(di), | |
78 | REG_OFFSET_NAME(bp), | |
79 | REG_OFFSET_NAME(ax), | |
80 | #ifdef CONFIG_X86_32 | |
81 | REG_OFFSET_NAME(ds), | |
82 | REG_OFFSET_NAME(es), | |
83 | REG_OFFSET_NAME(fs), | |
84 | REG_OFFSET_NAME(gs), | |
85 | #endif | |
86 | REG_OFFSET_NAME(orig_ax), | |
87 | REG_OFFSET_NAME(ip), | |
88 | REG_OFFSET_NAME(cs), | |
89 | REG_OFFSET_NAME(flags), | |
90 | REG_OFFSET_NAME(sp), | |
91 | REG_OFFSET_NAME(ss), | |
92 | REG_OFFSET_END, | |
93 | }; | |
94 | ||
95 | /** | |
96 | * regs_query_register_offset() - query register offset from its name | |
97 | * @name: the name of a register | |
98 | * | |
99 | * regs_query_register_offset() returns the offset of a register in struct | |
100 | * pt_regs from its name. If the name is invalid, this returns -EINVAL; | |
101 | */ | |
102 | int regs_query_register_offset(const char *name) | |
103 | { | |
104 | const struct pt_regs_offset *roff; | |
105 | for (roff = regoffset_table; roff->name != NULL; roff++) | |
106 | if (!strcmp(roff->name, name)) | |
107 | return roff->offset; | |
108 | return -EINVAL; | |
109 | } | |
110 | ||
111 | /** | |
112 | * regs_query_register_name() - query register name from its offset | |
113 | * @offset: the offset of a register in struct pt_regs. | |
114 | * | |
115 | * regs_query_register_name() returns the name of a register from its | |
116 | * offset in struct pt_regs. If the @offset is invalid, this returns NULL; | |
117 | */ | |
118 | const char *regs_query_register_name(unsigned int offset) | |
119 | { | |
120 | const struct pt_regs_offset *roff; | |
121 | for (roff = regoffset_table; roff->name != NULL; roff++) | |
122 | if (roff->offset == offset) | |
123 | return roff->name; | |
124 | return NULL; | |
125 | } | |
126 | ||
127 | static const int arg_offs_table[] = { | |
128 | #ifdef CONFIG_X86_32 | |
129 | [0] = offsetof(struct pt_regs, ax), | |
130 | [1] = offsetof(struct pt_regs, dx), | |
131 | [2] = offsetof(struct pt_regs, cx) | |
132 | #else /* CONFIG_X86_64 */ | |
133 | [0] = offsetof(struct pt_regs, di), | |
134 | [1] = offsetof(struct pt_regs, si), | |
135 | [2] = offsetof(struct pt_regs, dx), | |
136 | [3] = offsetof(struct pt_regs, cx), | |
137 | [4] = offsetof(struct pt_regs, r8), | |
138 | [5] = offsetof(struct pt_regs, r9) | |
139 | #endif | |
140 | }; | |
141 | ||
1da177e4 LT |
142 | /* |
143 | * does not yet catch signals sent when the child dies. | |
144 | * in exit.c or in signal.c. | |
145 | */ | |
146 | ||
9f155b98 CE |
147 | /* |
148 | * Determines which flags the user has access to [1 = access, 0 = no access]. | |
9f155b98 | 149 | */ |
e39c2891 RM |
150 | #define FLAG_MASK_32 ((unsigned long) \ |
151 | (X86_EFLAGS_CF | X86_EFLAGS_PF | \ | |
152 | X86_EFLAGS_AF | X86_EFLAGS_ZF | \ | |
153 | X86_EFLAGS_SF | X86_EFLAGS_TF | \ | |
154 | X86_EFLAGS_DF | X86_EFLAGS_OF | \ | |
155 | X86_EFLAGS_RF | X86_EFLAGS_AC)) | |
156 | ||
2047b08b RM |
157 | /* |
158 | * Determines whether a value may be installed in a segment register. | |
159 | */ | |
160 | static inline bool invalid_selector(u16 value) | |
161 | { | |
162 | return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL); | |
163 | } | |
164 | ||
165 | #ifdef CONFIG_X86_32 | |
166 | ||
e39c2891 | 167 | #define FLAG_MASK FLAG_MASK_32 |
1da177e4 | 168 | |
10226238 RR |
169 | /* |
170 | * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode | |
171 | * when it traps. The previous stack will be directly underneath the saved | |
172 | * registers, and 'sp/ss' won't even have been saved. Thus the '®s->sp'. | |
173 | * | |
174 | * Now, if the stack is empty, '®s->sp' is out of range. In this | |
175 | * case we try to take the previous stack. To always return a non-null | |
176 | * stack pointer we fall back to regs as stack if no previous stack | |
177 | * exists. | |
178 | * | |
179 | * This is valid only for kernel mode traps. | |
180 | */ | |
181 | unsigned long kernel_stack_pointer(struct pt_regs *regs) | |
182 | { | |
183 | unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1); | |
184 | unsigned long sp = (unsigned long)®s->sp; | |
185 | struct thread_info *tinfo; | |
186 | ||
187 | if (context == (sp & ~(THREAD_SIZE - 1))) | |
188 | return sp; | |
189 | ||
190 | tinfo = (struct thread_info *)context; | |
191 | if (tinfo->previous_esp) | |
192 | return tinfo->previous_esp; | |
193 | ||
194 | return (unsigned long)regs; | |
195 | } | |
196 | ||
4fe702c7 | 197 | static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno) |
1da177e4 | 198 | { |
65ea5b03 | 199 | BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0); |
ccbeed3a | 200 | return ®s->bx + (regno >> 2); |
1da177e4 LT |
201 | } |
202 | ||
06ee1b68 | 203 | static u16 get_segment_reg(struct task_struct *task, unsigned long offset) |
1da177e4 | 204 | { |
06ee1b68 RM |
205 | /* |
206 | * Returning the value truncates it to 16 bits. | |
207 | */ | |
208 | unsigned int retval; | |
209 | if (offset != offsetof(struct user_regs_struct, gs)) | |
210 | retval = *pt_regs_access(task_pt_regs(task), offset); | |
211 | else { | |
06ee1b68 | 212 | if (task == current) |
d9a89a26 TH |
213 | retval = get_user_gs(task_pt_regs(task)); |
214 | else | |
215 | retval = task_user_gs(task); | |
06ee1b68 RM |
216 | } |
217 | return retval; | |
218 | } | |
219 | ||
220 | static int set_segment_reg(struct task_struct *task, | |
221 | unsigned long offset, u16 value) | |
222 | { | |
223 | /* | |
224 | * The value argument was already truncated to 16 bits. | |
225 | */ | |
2047b08b | 226 | if (invalid_selector(value)) |
06ee1b68 RM |
227 | return -EIO; |
228 | ||
c63855d0 RM |
229 | /* |
230 | * For %cs and %ss we cannot permit a null selector. | |
231 | * We can permit a bogus selector as long as it has USER_RPL. | |
232 | * Null selectors are fine for other segment registers, but | |
233 | * we will never get back to user mode with invalid %cs or %ss | |
234 | * and will take the trap in iret instead. Much code relies | |
235 | * on user_mode() to distinguish a user trap frame (which can | |
236 | * safely use invalid selectors) from a kernel trap frame. | |
237 | */ | |
238 | switch (offset) { | |
239 | case offsetof(struct user_regs_struct, cs): | |
240 | case offsetof(struct user_regs_struct, ss): | |
241 | if (unlikely(value == 0)) | |
242 | return -EIO; | |
243 | ||
244 | default: | |
06ee1b68 | 245 | *pt_regs_access(task_pt_regs(task), offset) = value; |
c63855d0 RM |
246 | break; |
247 | ||
248 | case offsetof(struct user_regs_struct, gs): | |
06ee1b68 | 249 | if (task == current) |
d9a89a26 TH |
250 | set_user_gs(task_pt_regs(task), value); |
251 | else | |
252 | task_user_gs(task) = value; | |
1da177e4 | 253 | } |
06ee1b68 | 254 | |
1da177e4 LT |
255 | return 0; |
256 | } | |
257 | ||
2047b08b RM |
258 | #else /* CONFIG_X86_64 */ |
259 | ||
260 | #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT) | |
261 | ||
262 | static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset) | |
263 | { | |
264 | BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0); | |
265 | return ®s->r15 + (offset / sizeof(regs->r15)); | |
266 | } | |
267 | ||
268 | static u16 get_segment_reg(struct task_struct *task, unsigned long offset) | |
269 | { | |
270 | /* | |
271 | * Returning the value truncates it to 16 bits. | |
272 | */ | |
273 | unsigned int seg; | |
274 | ||
275 | switch (offset) { | |
276 | case offsetof(struct user_regs_struct, fs): | |
277 | if (task == current) { | |
278 | /* Older gas can't assemble movq %?s,%r?? */ | |
279 | asm("movl %%fs,%0" : "=r" (seg)); | |
280 | return seg; | |
281 | } | |
282 | return task->thread.fsindex; | |
283 | case offsetof(struct user_regs_struct, gs): | |
284 | if (task == current) { | |
285 | asm("movl %%gs,%0" : "=r" (seg)); | |
286 | return seg; | |
287 | } | |
288 | return task->thread.gsindex; | |
289 | case offsetof(struct user_regs_struct, ds): | |
290 | if (task == current) { | |
291 | asm("movl %%ds,%0" : "=r" (seg)); | |
292 | return seg; | |
293 | } | |
294 | return task->thread.ds; | |
295 | case offsetof(struct user_regs_struct, es): | |
296 | if (task == current) { | |
297 | asm("movl %%es,%0" : "=r" (seg)); | |
298 | return seg; | |
299 | } | |
300 | return task->thread.es; | |
301 | ||
302 | case offsetof(struct user_regs_struct, cs): | |
303 | case offsetof(struct user_regs_struct, ss): | |
304 | break; | |
305 | } | |
306 | return *pt_regs_access(task_pt_regs(task), offset); | |
307 | } | |
308 | ||
309 | static int set_segment_reg(struct task_struct *task, | |
310 | unsigned long offset, u16 value) | |
311 | { | |
312 | /* | |
313 | * The value argument was already truncated to 16 bits. | |
314 | */ | |
315 | if (invalid_selector(value)) | |
316 | return -EIO; | |
317 | ||
318 | switch (offset) { | |
319 | case offsetof(struct user_regs_struct,fs): | |
320 | /* | |
321 | * If this is setting fs as for normal 64-bit use but | |
322 | * setting fs_base has implicitly changed it, leave it. | |
323 | */ | |
324 | if ((value == FS_TLS_SEL && task->thread.fsindex == 0 && | |
325 | task->thread.fs != 0) || | |
326 | (value == 0 && task->thread.fsindex == FS_TLS_SEL && | |
327 | task->thread.fs == 0)) | |
328 | break; | |
329 | task->thread.fsindex = value; | |
330 | if (task == current) | |
331 | loadsegment(fs, task->thread.fsindex); | |
332 | break; | |
333 | case offsetof(struct user_regs_struct,gs): | |
334 | /* | |
335 | * If this is setting gs as for normal 64-bit use but | |
336 | * setting gs_base has implicitly changed it, leave it. | |
337 | */ | |
338 | if ((value == GS_TLS_SEL && task->thread.gsindex == 0 && | |
339 | task->thread.gs != 0) || | |
340 | (value == 0 && task->thread.gsindex == GS_TLS_SEL && | |
341 | task->thread.gs == 0)) | |
342 | break; | |
343 | task->thread.gsindex = value; | |
344 | if (task == current) | |
345 | load_gs_index(task->thread.gsindex); | |
346 | break; | |
347 | case offsetof(struct user_regs_struct,ds): | |
348 | task->thread.ds = value; | |
349 | if (task == current) | |
350 | loadsegment(ds, task->thread.ds); | |
351 | break; | |
352 | case offsetof(struct user_regs_struct,es): | |
353 | task->thread.es = value; | |
354 | if (task == current) | |
355 | loadsegment(es, task->thread.es); | |
356 | break; | |
357 | ||
358 | /* | |
359 | * Can't actually change these in 64-bit mode. | |
360 | */ | |
361 | case offsetof(struct user_regs_struct,cs): | |
c63855d0 RM |
362 | if (unlikely(value == 0)) |
363 | return -EIO; | |
2047b08b RM |
364 | #ifdef CONFIG_IA32_EMULATION |
365 | if (test_tsk_thread_flag(task, TIF_IA32)) | |
366 | task_pt_regs(task)->cs = value; | |
2047b08b | 367 | #endif |
cb757c41 | 368 | break; |
2047b08b | 369 | case offsetof(struct user_regs_struct,ss): |
c63855d0 RM |
370 | if (unlikely(value == 0)) |
371 | return -EIO; | |
2047b08b RM |
372 | #ifdef CONFIG_IA32_EMULATION |
373 | if (test_tsk_thread_flag(task, TIF_IA32)) | |
374 | task_pt_regs(task)->ss = value; | |
2047b08b | 375 | #endif |
cb757c41 | 376 | break; |
2047b08b RM |
377 | } |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
2047b08b RM |
382 | #endif /* CONFIG_X86_32 */ |
383 | ||
06ee1b68 | 384 | static unsigned long get_flags(struct task_struct *task) |
1da177e4 | 385 | { |
06ee1b68 RM |
386 | unsigned long retval = task_pt_regs(task)->flags; |
387 | ||
388 | /* | |
389 | * If the debugger set TF, hide it from the readout. | |
390 | */ | |
391 | if (test_tsk_thread_flag(task, TIF_FORCED_TF)) | |
392 | retval &= ~X86_EFLAGS_TF; | |
1da177e4 | 393 | |
1da177e4 LT |
394 | return retval; |
395 | } | |
396 | ||
06ee1b68 RM |
397 | static int set_flags(struct task_struct *task, unsigned long value) |
398 | { | |
399 | struct pt_regs *regs = task_pt_regs(task); | |
400 | ||
401 | /* | |
402 | * If the user value contains TF, mark that | |
403 | * it was not "us" (the debugger) that set it. | |
404 | * If not, make sure it stays set if we had. | |
405 | */ | |
406 | if (value & X86_EFLAGS_TF) | |
407 | clear_tsk_thread_flag(task, TIF_FORCED_TF); | |
408 | else if (test_tsk_thread_flag(task, TIF_FORCED_TF)) | |
409 | value |= X86_EFLAGS_TF; | |
410 | ||
411 | regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK); | |
412 | ||
413 | return 0; | |
414 | } | |
415 | ||
416 | static int putreg(struct task_struct *child, | |
417 | unsigned long offset, unsigned long value) | |
418 | { | |
419 | switch (offset) { | |
420 | case offsetof(struct user_regs_struct, cs): | |
421 | case offsetof(struct user_regs_struct, ds): | |
422 | case offsetof(struct user_regs_struct, es): | |
423 | case offsetof(struct user_regs_struct, fs): | |
424 | case offsetof(struct user_regs_struct, gs): | |
425 | case offsetof(struct user_regs_struct, ss): | |
426 | return set_segment_reg(child, offset, value); | |
427 | ||
428 | case offsetof(struct user_regs_struct, flags): | |
429 | return set_flags(child, value); | |
2047b08b RM |
430 | |
431 | #ifdef CONFIG_X86_64 | |
432 | case offsetof(struct user_regs_struct,fs_base): | |
433 | if (value >= TASK_SIZE_OF(child)) | |
434 | return -EIO; | |
435 | /* | |
436 | * When changing the segment base, use do_arch_prctl | |
437 | * to set either thread.fs or thread.fsindex and the | |
438 | * corresponding GDT slot. | |
439 | */ | |
440 | if (child->thread.fs != value) | |
441 | return do_arch_prctl(child, ARCH_SET_FS, value); | |
442 | return 0; | |
443 | case offsetof(struct user_regs_struct,gs_base): | |
444 | /* | |
445 | * Exactly the same here as the %fs handling above. | |
446 | */ | |
447 | if (value >= TASK_SIZE_OF(child)) | |
448 | return -EIO; | |
449 | if (child->thread.gs != value) | |
450 | return do_arch_prctl(child, ARCH_SET_GS, value); | |
451 | return 0; | |
452 | #endif | |
06ee1b68 RM |
453 | } |
454 | ||
455 | *pt_regs_access(task_pt_regs(child), offset) = value; | |
456 | return 0; | |
457 | } | |
458 | ||
459 | static unsigned long getreg(struct task_struct *task, unsigned long offset) | |
460 | { | |
461 | switch (offset) { | |
462 | case offsetof(struct user_regs_struct, cs): | |
463 | case offsetof(struct user_regs_struct, ds): | |
464 | case offsetof(struct user_regs_struct, es): | |
465 | case offsetof(struct user_regs_struct, fs): | |
466 | case offsetof(struct user_regs_struct, gs): | |
467 | case offsetof(struct user_regs_struct, ss): | |
468 | return get_segment_reg(task, offset); | |
469 | ||
470 | case offsetof(struct user_regs_struct, flags): | |
471 | return get_flags(task); | |
2047b08b RM |
472 | |
473 | #ifdef CONFIG_X86_64 | |
474 | case offsetof(struct user_regs_struct, fs_base): { | |
475 | /* | |
476 | * do_arch_prctl may have used a GDT slot instead of | |
477 | * the MSR. To userland, it appears the same either | |
478 | * way, except the %fs segment selector might not be 0. | |
479 | */ | |
480 | unsigned int seg = task->thread.fsindex; | |
481 | if (task->thread.fs != 0) | |
482 | return task->thread.fs; | |
483 | if (task == current) | |
484 | asm("movl %%fs,%0" : "=r" (seg)); | |
485 | if (seg != FS_TLS_SEL) | |
486 | return 0; | |
487 | return get_desc_base(&task->thread.tls_array[FS_TLS]); | |
488 | } | |
489 | case offsetof(struct user_regs_struct, gs_base): { | |
490 | /* | |
491 | * Exactly the same here as the %fs handling above. | |
492 | */ | |
493 | unsigned int seg = task->thread.gsindex; | |
494 | if (task->thread.gs != 0) | |
495 | return task->thread.gs; | |
496 | if (task == current) | |
497 | asm("movl %%gs,%0" : "=r" (seg)); | |
498 | if (seg != GS_TLS_SEL) | |
499 | return 0; | |
500 | return get_desc_base(&task->thread.tls_array[GS_TLS]); | |
501 | } | |
502 | #endif | |
06ee1b68 RM |
503 | } |
504 | ||
505 | return *pt_regs_access(task_pt_regs(task), offset); | |
506 | } | |
507 | ||
91e7b707 RM |
508 | static int genregs_get(struct task_struct *target, |
509 | const struct user_regset *regset, | |
510 | unsigned int pos, unsigned int count, | |
511 | void *kbuf, void __user *ubuf) | |
512 | { | |
513 | if (kbuf) { | |
514 | unsigned long *k = kbuf; | |
04a1e62c | 515 | while (count >= sizeof(*k)) { |
91e7b707 RM |
516 | *k++ = getreg(target, pos); |
517 | count -= sizeof(*k); | |
518 | pos += sizeof(*k); | |
519 | } | |
520 | } else { | |
521 | unsigned long __user *u = ubuf; | |
04a1e62c | 522 | while (count >= sizeof(*u)) { |
91e7b707 RM |
523 | if (__put_user(getreg(target, pos), u++)) |
524 | return -EFAULT; | |
525 | count -= sizeof(*u); | |
526 | pos += sizeof(*u); | |
527 | } | |
528 | } | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
533 | static int genregs_set(struct task_struct *target, | |
534 | const struct user_regset *regset, | |
535 | unsigned int pos, unsigned int count, | |
536 | const void *kbuf, const void __user *ubuf) | |
537 | { | |
538 | int ret = 0; | |
539 | if (kbuf) { | |
540 | const unsigned long *k = kbuf; | |
04a1e62c | 541 | while (count >= sizeof(*k) && !ret) { |
91e7b707 RM |
542 | ret = putreg(target, pos, *k++); |
543 | count -= sizeof(*k); | |
544 | pos += sizeof(*k); | |
545 | } | |
546 | } else { | |
547 | const unsigned long __user *u = ubuf; | |
04a1e62c | 548 | while (count >= sizeof(*u) && !ret) { |
91e7b707 RM |
549 | unsigned long word; |
550 | ret = __get_user(word, u++); | |
551 | if (ret) | |
552 | break; | |
553 | ret = putreg(target, pos, word); | |
554 | count -= sizeof(*u); | |
555 | pos += sizeof(*u); | |
556 | } | |
557 | } | |
558 | return ret; | |
559 | } | |
560 | ||
a8b0ca17 | 561 | static void ptrace_triggered(struct perf_event *bp, |
b326e956 FW |
562 | struct perf_sample_data *data, |
563 | struct pt_regs *regs) | |
d9771e8c | 564 | { |
0f534093 | 565 | int i; |
24f1e32c | 566 | struct thread_struct *thread = &(current->thread); |
0f534093 | 567 | |
72f674d2 P |
568 | /* |
569 | * Store in the virtual DR6 register the fact that the breakpoint | |
570 | * was hit so the thread's debugger will see it. | |
571 | */ | |
24f1e32c FW |
572 | for (i = 0; i < HBP_NUM; i++) { |
573 | if (thread->ptrace_bps[i] == bp) | |
72f674d2 | 574 | break; |
24f1e32c | 575 | } |
d9771e8c | 576 | |
72f674d2 P |
577 | thread->debugreg6 |= (DR_TRAP0 << i); |
578 | } | |
d9771e8c | 579 | |
d9771e8c | 580 | /* |
24f1e32c FW |
581 | * Walk through every ptrace breakpoints for this thread and |
582 | * build the dr7 value on top of their attributes. | |
583 | * | |
d9771e8c | 584 | */ |
24f1e32c | 585 | static unsigned long ptrace_get_dr7(struct perf_event *bp[]) |
d9771e8c | 586 | { |
24f1e32c FW |
587 | int i; |
588 | int dr7 = 0; | |
589 | struct arch_hw_breakpoint *info; | |
590 | ||
591 | for (i = 0; i < HBP_NUM; i++) { | |
592 | if (bp[i] && !bp[i]->attr.disabled) { | |
593 | info = counter_arch_bp(bp[i]); | |
594 | dr7 |= encode_dr7(i, info->len, info->type); | |
595 | } | |
0f534093 | 596 | } |
24f1e32c FW |
597 | |
598 | return dr7; | |
d9771e8c RM |
599 | } |
600 | ||
44234adc | 601 | static int |
5fa10b28 | 602 | ptrace_modify_breakpoint(struct perf_event *bp, int len, int type, |
1cedae72 | 603 | struct task_struct *tsk, int disabled) |
5fa10b28 FW |
604 | { |
605 | int err; | |
606 | int gen_len, gen_type; | |
b326e956 | 607 | struct perf_event_attr attr; |
5fa10b28 FW |
608 | |
609 | /* | |
c9404c9c | 610 | * We should have at least an inactive breakpoint at this |
5fa10b28 FW |
611 | * slot. It means the user is writing dr7 without having |
612 | * written the address register first | |
613 | */ | |
614 | if (!bp) | |
44234adc | 615 | return -EINVAL; |
5fa10b28 FW |
616 | |
617 | err = arch_bp_generic_fields(len, type, &gen_len, &gen_type); | |
618 | if (err) | |
44234adc | 619 | return err; |
5fa10b28 FW |
620 | |
621 | attr = bp->attr; | |
622 | attr.bp_len = gen_len; | |
623 | attr.bp_type = gen_type; | |
1cedae72 | 624 | attr.disabled = disabled; |
5fa10b28 | 625 | |
2f0993e0 | 626 | return modify_user_hw_breakpoint(bp, &attr); |
5fa10b28 FW |
627 | } |
628 | ||
72f674d2 P |
629 | /* |
630 | * Handle ptrace writes to debug register 7. | |
631 | */ | |
632 | static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data) | |
d9771e8c | 633 | { |
72f674d2 | 634 | struct thread_struct *thread = &(tsk->thread); |
24f1e32c | 635 | unsigned long old_dr7; |
72f674d2 P |
636 | int i, orig_ret = 0, rc = 0; |
637 | int enabled, second_pass = 0; | |
638 | unsigned len, type; | |
24f1e32c | 639 | struct perf_event *bp; |
72f674d2 | 640 | |
87dc669b FW |
641 | if (ptrace_get_breakpoints(tsk) < 0) |
642 | return -ESRCH; | |
643 | ||
72f674d2 | 644 | data &= ~DR_CONTROL_RESERVED; |
24f1e32c | 645 | old_dr7 = ptrace_get_dr7(thread->ptrace_bps); |
72f674d2 P |
646 | restore: |
647 | /* | |
648 | * Loop through all the hardware breakpoints, making the | |
649 | * appropriate changes to each. | |
650 | */ | |
651 | for (i = 0; i < HBP_NUM; i++) { | |
652 | enabled = decode_dr7(data, i, &len, &type); | |
24f1e32c | 653 | bp = thread->ptrace_bps[i]; |
72f674d2 P |
654 | |
655 | if (!enabled) { | |
656 | if (bp) { | |
24f1e32c FW |
657 | /* |
658 | * Don't unregister the breakpoints right-away, | |
72f674d2 P |
659 | * unless all register_user_hw_breakpoint() |
660 | * requests have succeeded. This prevents | |
661 | * any window of opportunity for debug | |
662 | * register grabbing by other users. | |
663 | */ | |
664 | if (!second_pass) | |
665 | continue; | |
1cedae72 | 666 | |
44234adc | 667 | rc = ptrace_modify_breakpoint(bp, len, type, |
1cedae72 | 668 | tsk, 1); |
44234adc | 669 | if (rc) |
1cedae72 | 670 | break; |
72f674d2 P |
671 | } |
672 | continue; | |
673 | } | |
0f534093 | 674 | |
44234adc FW |
675 | rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0); |
676 | if (rc) | |
24f1e32c | 677 | break; |
72f674d2 P |
678 | } |
679 | /* | |
680 | * Make a second pass to free the remaining unused breakpoints | |
681 | * or to restore the original breakpoints if an error occurred. | |
682 | */ | |
683 | if (!second_pass) { | |
684 | second_pass = 1; | |
685 | if (rc < 0) { | |
686 | orig_ret = rc; | |
687 | data = old_dr7; | |
688 | } | |
689 | goto restore; | |
690 | } | |
87dc669b FW |
691 | |
692 | ptrace_put_breakpoints(tsk); | |
693 | ||
72f674d2 P |
694 | return ((orig_ret < 0) ? orig_ret : rc); |
695 | } | |
0f534093 | 696 | |
72f674d2 P |
697 | /* |
698 | * Handle PTRACE_PEEKUSR calls for the debug register area. | |
699 | */ | |
9d22b536 | 700 | static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n) |
72f674d2 P |
701 | { |
702 | struct thread_struct *thread = &(tsk->thread); | |
703 | unsigned long val = 0; | |
704 | ||
24f1e32c FW |
705 | if (n < HBP_NUM) { |
706 | struct perf_event *bp; | |
87dc669b FW |
707 | |
708 | if (ptrace_get_breakpoints(tsk) < 0) | |
709 | return -ESRCH; | |
710 | ||
24f1e32c FW |
711 | bp = thread->ptrace_bps[n]; |
712 | if (!bp) | |
87dc669b FW |
713 | val = 0; |
714 | else | |
715 | val = bp->hw.info.address; | |
716 | ||
717 | ptrace_put_breakpoints(tsk); | |
24f1e32c | 718 | } else if (n == 6) { |
72f674d2 | 719 | val = thread->debugreg6; |
24f1e32c | 720 | } else if (n == 7) { |
326264a0 | 721 | val = thread->ptrace_dr7; |
24f1e32c | 722 | } |
72f674d2 P |
723 | return val; |
724 | } | |
0f534093 | 725 | |
24f1e32c FW |
726 | static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr, |
727 | unsigned long addr) | |
728 | { | |
729 | struct perf_event *bp; | |
730 | struct thread_struct *t = &tsk->thread; | |
b326e956 | 731 | struct perf_event_attr attr; |
87dc669b FW |
732 | int err = 0; |
733 | ||
734 | if (ptrace_get_breakpoints(tsk) < 0) | |
735 | return -ESRCH; | |
24f1e32c FW |
736 | |
737 | if (!t->ptrace_bps[nr]) { | |
73266fc1 | 738 | ptrace_breakpoint_init(&attr); |
d9771e8c | 739 | /* |
24f1e32c FW |
740 | * Put stub len and type to register (reserve) an inactive but |
741 | * correct bp | |
d9771e8c | 742 | */ |
5fa10b28 FW |
743 | attr.bp_addr = addr; |
744 | attr.bp_len = HW_BREAKPOINT_LEN_1; | |
745 | attr.bp_type = HW_BREAKPOINT_W; | |
746 | attr.disabled = 1; | |
747 | ||
4dc0da86 AK |
748 | bp = register_user_hw_breakpoint(&attr, ptrace_triggered, |
749 | NULL, tsk); | |
44234adc FW |
750 | |
751 | /* | |
752 | * CHECKME: the previous code returned -EIO if the addr wasn't | |
753 | * a valid task virtual addr. The new one will return -EINVAL in | |
754 | * this case. | |
755 | * -EINVAL may be what we want for in-kernel breakpoints users, | |
756 | * but -EIO looks better for ptrace, since we refuse a register | |
757 | * writing for the user. And anyway this is the previous | |
758 | * behaviour. | |
759 | */ | |
87dc669b FW |
760 | if (IS_ERR(bp)) { |
761 | err = PTR_ERR(bp); | |
762 | goto put; | |
763 | } | |
44234adc FW |
764 | |
765 | t->ptrace_bps[nr] = bp; | |
24f1e32c FW |
766 | } else { |
767 | bp = t->ptrace_bps[nr]; | |
5fa10b28 FW |
768 | |
769 | attr = bp->attr; | |
770 | attr.bp_addr = addr; | |
44234adc | 771 | err = modify_user_hw_breakpoint(bp, &attr); |
d9771e8c | 772 | } |
24f1e32c | 773 | |
87dc669b FW |
774 | put: |
775 | ptrace_put_breakpoints(tsk); | |
776 | return err; | |
d9771e8c RM |
777 | } |
778 | ||
72f674d2 P |
779 | /* |
780 | * Handle PTRACE_POKEUSR calls for the debug register area. | |
781 | */ | |
98b8b99a HS |
782 | static int ptrace_set_debugreg(struct task_struct *tsk, int n, |
783 | unsigned long val) | |
72f674d2 P |
784 | { |
785 | struct thread_struct *thread = &(tsk->thread); | |
786 | int rc = 0; | |
787 | ||
788 | /* There are no DR4 or DR5 registers */ | |
789 | if (n == 4 || n == 5) | |
790 | return -EIO; | |
791 | ||
792 | if (n == 6) { | |
24f1e32c | 793 | thread->debugreg6 = val; |
72f674d2 | 794 | goto ret_path; |
d9771e8c | 795 | } |
72f674d2 | 796 | if (n < HBP_NUM) { |
24f1e32c FW |
797 | rc = ptrace_set_breakpoint_addr(tsk, n, val); |
798 | if (rc) | |
799 | return rc; | |
72f674d2 P |
800 | } |
801 | /* All that's left is DR7 */ | |
326264a0 | 802 | if (n == 7) { |
72f674d2 | 803 | rc = ptrace_write_dr7(tsk, val); |
326264a0 FW |
804 | if (!rc) |
805 | thread->ptrace_dr7 = val; | |
806 | } | |
d9771e8c | 807 | |
72f674d2 P |
808 | ret_path: |
809 | return rc; | |
d9771e8c RM |
810 | } |
811 | ||
325af5fb RM |
812 | /* |
813 | * These access the current or another (stopped) task's io permission | |
814 | * bitmap for debugging or core dump. | |
815 | */ | |
816 | static int ioperm_active(struct task_struct *target, | |
817 | const struct user_regset *regset) | |
818 | { | |
819 | return target->thread.io_bitmap_max / regset->size; | |
820 | } | |
b4ef95de | 821 | |
325af5fb RM |
822 | static int ioperm_get(struct task_struct *target, |
823 | const struct user_regset *regset, | |
824 | unsigned int pos, unsigned int count, | |
825 | void *kbuf, void __user *ubuf) | |
eee3af4a | 826 | { |
325af5fb | 827 | if (!target->thread.io_bitmap_ptr) |
eee3af4a MM |
828 | return -ENXIO; |
829 | ||
325af5fb RM |
830 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
831 | target->thread.io_bitmap_ptr, | |
832 | 0, IO_BITMAP_BYTES); | |
833 | } | |
834 | ||
1da177e4 LT |
835 | /* |
836 | * Called by kernel/ptrace.c when detaching.. | |
837 | * | |
838 | * Make sure the single step bit is not set. | |
839 | */ | |
840 | void ptrace_disable(struct task_struct *child) | |
9e714bed | 841 | { |
7f232343 | 842 | user_disable_single_step(child); |
e9c86c78 | 843 | #ifdef TIF_SYSCALL_EMU |
ab1c23c2 | 844 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
e9c86c78 | 845 | #endif |
1da177e4 LT |
846 | } |
847 | ||
5a4646a4 RM |
848 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
849 | static const struct user_regset_view user_x86_32_view; /* Initialized below. */ | |
850 | #endif | |
851 | ||
9b05a69e NK |
852 | long arch_ptrace(struct task_struct *child, long request, |
853 | unsigned long addr, unsigned long data) | |
1da177e4 | 854 | { |
5a4646a4 | 855 | int ret; |
1da177e4 LT |
856 | unsigned long __user *datap = (unsigned long __user *)data; |
857 | ||
1da177e4 | 858 | switch (request) { |
1da177e4 LT |
859 | /* read the word at location addr in the USER area. */ |
860 | case PTRACE_PEEKUSR: { | |
861 | unsigned long tmp; | |
862 | ||
863 | ret = -EIO; | |
eb5a3699 | 864 | if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user)) |
1da177e4 LT |
865 | break; |
866 | ||
867 | tmp = 0; /* Default return condition */ | |
e9c86c78 | 868 | if (addr < sizeof(struct user_regs_struct)) |
1da177e4 | 869 | tmp = getreg(child, addr); |
e9c86c78 RM |
870 | else if (addr >= offsetof(struct user, u_debugreg[0]) && |
871 | addr <= offsetof(struct user, u_debugreg[7])) { | |
872 | addr -= offsetof(struct user, u_debugreg[0]); | |
873 | tmp = ptrace_get_debugreg(child, addr / sizeof(data)); | |
1da177e4 LT |
874 | } |
875 | ret = put_user(tmp, datap); | |
876 | break; | |
877 | } | |
878 | ||
1da177e4 LT |
879 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
880 | ret = -EIO; | |
eb5a3699 | 881 | if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user)) |
1da177e4 LT |
882 | break; |
883 | ||
e9c86c78 | 884 | if (addr < sizeof(struct user_regs_struct)) |
1da177e4 | 885 | ret = putreg(child, addr, data); |
e9c86c78 RM |
886 | else if (addr >= offsetof(struct user, u_debugreg[0]) && |
887 | addr <= offsetof(struct user, u_debugreg[7])) { | |
888 | addr -= offsetof(struct user, u_debugreg[0]); | |
889 | ret = ptrace_set_debugreg(child, | |
890 | addr / sizeof(data), data); | |
1da177e4 | 891 | } |
e9c86c78 | 892 | break; |
1da177e4 | 893 | |
5a4646a4 RM |
894 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ |
895 | return copy_regset_to_user(child, | |
896 | task_user_regset_view(current), | |
897 | REGSET_GENERAL, | |
898 | 0, sizeof(struct user_regs_struct), | |
899 | datap); | |
900 | ||
901 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ | |
902 | return copy_regset_from_user(child, | |
903 | task_user_regset_view(current), | |
904 | REGSET_GENERAL, | |
905 | 0, sizeof(struct user_regs_struct), | |
906 | datap); | |
907 | ||
908 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ | |
909 | return copy_regset_to_user(child, | |
910 | task_user_regset_view(current), | |
911 | REGSET_FP, | |
912 | 0, sizeof(struct user_i387_struct), | |
913 | datap); | |
914 | ||
915 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ | |
916 | return copy_regset_from_user(child, | |
917 | task_user_regset_view(current), | |
918 | REGSET_FP, | |
919 | 0, sizeof(struct user_i387_struct), | |
920 | datap); | |
1da177e4 | 921 | |
e9c86c78 | 922 | #ifdef CONFIG_X86_32 |
5a4646a4 RM |
923 | case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */ |
924 | return copy_regset_to_user(child, &user_x86_32_view, | |
925 | REGSET_XFP, | |
926 | 0, sizeof(struct user_fxsr_struct), | |
45fdc3a7 | 927 | datap) ? -EIO : 0; |
5a4646a4 RM |
928 | |
929 | case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */ | |
930 | return copy_regset_from_user(child, &user_x86_32_view, | |
931 | REGSET_XFP, | |
932 | 0, sizeof(struct user_fxsr_struct), | |
45fdc3a7 | 933 | datap) ? -EIO : 0; |
e9c86c78 | 934 | #endif |
1da177e4 | 935 | |
e9c86c78 | 936 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
1da177e4 | 937 | case PTRACE_GET_THREAD_AREA: |
9b05a69e | 938 | if ((int) addr < 0) |
efd1ca52 RM |
939 | return -EIO; |
940 | ret = do_get_thread_area(child, addr, | |
eb5a3699 | 941 | (struct user_desc __user *)data); |
1da177e4 LT |
942 | break; |
943 | ||
944 | case PTRACE_SET_THREAD_AREA: | |
9b05a69e | 945 | if ((int) addr < 0) |
efd1ca52 RM |
946 | return -EIO; |
947 | ret = do_set_thread_area(child, addr, | |
eb5a3699 | 948 | (struct user_desc __user *)data, 0); |
1da177e4 | 949 | break; |
e9c86c78 RM |
950 | #endif |
951 | ||
952 | #ifdef CONFIG_X86_64 | |
953 | /* normal 64bit interface to access TLS data. | |
954 | Works just like arch_prctl, except that the arguments | |
955 | are reversed. */ | |
956 | case PTRACE_ARCH_PRCTL: | |
957 | ret = do_arch_prctl(child, data, addr); | |
958 | break; | |
959 | #endif | |
1da177e4 LT |
960 | |
961 | default: | |
962 | ret = ptrace_request(child, request, addr, data); | |
963 | break; | |
964 | } | |
d9771e8c | 965 | |
1da177e4 LT |
966 | return ret; |
967 | } | |
968 | ||
cb757c41 RM |
969 | #ifdef CONFIG_IA32_EMULATION |
970 | ||
099cd6e9 RM |
971 | #include <linux/compat.h> |
972 | #include <linux/syscalls.h> | |
973 | #include <asm/ia32.h> | |
cb757c41 RM |
974 | #include <asm/user32.h> |
975 | ||
976 | #define R32(l,q) \ | |
977 | case offsetof(struct user32, regs.l): \ | |
978 | regs->q = value; break | |
979 | ||
980 | #define SEG32(rs) \ | |
981 | case offsetof(struct user32, regs.rs): \ | |
982 | return set_segment_reg(child, \ | |
983 | offsetof(struct user_regs_struct, rs), \ | |
984 | value); \ | |
985 | break | |
986 | ||
987 | static int putreg32(struct task_struct *child, unsigned regno, u32 value) | |
988 | { | |
989 | struct pt_regs *regs = task_pt_regs(child); | |
990 | ||
991 | switch (regno) { | |
992 | ||
993 | SEG32(cs); | |
994 | SEG32(ds); | |
995 | SEG32(es); | |
996 | SEG32(fs); | |
997 | SEG32(gs); | |
998 | SEG32(ss); | |
999 | ||
1000 | R32(ebx, bx); | |
1001 | R32(ecx, cx); | |
1002 | R32(edx, dx); | |
1003 | R32(edi, di); | |
1004 | R32(esi, si); | |
1005 | R32(ebp, bp); | |
1006 | R32(eax, ax); | |
cb757c41 RM |
1007 | R32(eip, ip); |
1008 | R32(esp, sp); | |
1009 | ||
40f0933d RM |
1010 | case offsetof(struct user32, regs.orig_eax): |
1011 | /* | |
8cb3ed13 RM |
1012 | * A 32-bit debugger setting orig_eax means to restore |
1013 | * the state of the task restarting a 32-bit syscall. | |
1014 | * Make sure we interpret the -ERESTART* codes correctly | |
1015 | * in case the task is not actually still sitting at the | |
1016 | * exit from a 32-bit syscall with TS_COMPAT still set. | |
40f0933d | 1017 | */ |
8cb3ed13 RM |
1018 | regs->orig_ax = value; |
1019 | if (syscall_get_nr(child, regs) >= 0) | |
1020 | task_thread_info(child)->status |= TS_COMPAT; | |
40f0933d RM |
1021 | break; |
1022 | ||
cb757c41 RM |
1023 | case offsetof(struct user32, regs.eflags): |
1024 | return set_flags(child, value); | |
1025 | ||
1026 | case offsetof(struct user32, u_debugreg[0]) ... | |
1027 | offsetof(struct user32, u_debugreg[7]): | |
1028 | regno -= offsetof(struct user32, u_debugreg[0]); | |
1029 | return ptrace_set_debugreg(child, regno / 4, value); | |
1030 | ||
1031 | default: | |
1032 | if (regno > sizeof(struct user32) || (regno & 3)) | |
1033 | return -EIO; | |
1034 | ||
1035 | /* | |
1036 | * Other dummy fields in the virtual user structure | |
1037 | * are ignored | |
1038 | */ | |
1039 | break; | |
1040 | } | |
1041 | return 0; | |
1042 | } | |
1043 | ||
1044 | #undef R32 | |
1045 | #undef SEG32 | |
1046 | ||
1047 | #define R32(l,q) \ | |
1048 | case offsetof(struct user32, regs.l): \ | |
1049 | *val = regs->q; break | |
1050 | ||
1051 | #define SEG32(rs) \ | |
1052 | case offsetof(struct user32, regs.rs): \ | |
1053 | *val = get_segment_reg(child, \ | |
1054 | offsetof(struct user_regs_struct, rs)); \ | |
1055 | break | |
1056 | ||
1057 | static int getreg32(struct task_struct *child, unsigned regno, u32 *val) | |
1058 | { | |
1059 | struct pt_regs *regs = task_pt_regs(child); | |
1060 | ||
1061 | switch (regno) { | |
1062 | ||
1063 | SEG32(ds); | |
1064 | SEG32(es); | |
1065 | SEG32(fs); | |
1066 | SEG32(gs); | |
1067 | ||
1068 | R32(cs, cs); | |
1069 | R32(ss, ss); | |
1070 | R32(ebx, bx); | |
1071 | R32(ecx, cx); | |
1072 | R32(edx, dx); | |
1073 | R32(edi, di); | |
1074 | R32(esi, si); | |
1075 | R32(ebp, bp); | |
1076 | R32(eax, ax); | |
1077 | R32(orig_eax, orig_ax); | |
1078 | R32(eip, ip); | |
1079 | R32(esp, sp); | |
1080 | ||
1081 | case offsetof(struct user32, regs.eflags): | |
1082 | *val = get_flags(child); | |
1083 | break; | |
1084 | ||
1085 | case offsetof(struct user32, u_debugreg[0]) ... | |
1086 | offsetof(struct user32, u_debugreg[7]): | |
1087 | regno -= offsetof(struct user32, u_debugreg[0]); | |
1088 | *val = ptrace_get_debugreg(child, regno / 4); | |
1089 | break; | |
1090 | ||
1091 | default: | |
1092 | if (regno > sizeof(struct user32) || (regno & 3)) | |
1093 | return -EIO; | |
1094 | ||
1095 | /* | |
1096 | * Other dummy fields in the virtual user structure | |
1097 | * are ignored | |
1098 | */ | |
1099 | *val = 0; | |
1100 | break; | |
1101 | } | |
1102 | return 0; | |
1103 | } | |
1104 | ||
1105 | #undef R32 | |
1106 | #undef SEG32 | |
1107 | ||
91e7b707 RM |
1108 | static int genregs32_get(struct task_struct *target, |
1109 | const struct user_regset *regset, | |
1110 | unsigned int pos, unsigned int count, | |
1111 | void *kbuf, void __user *ubuf) | |
1112 | { | |
1113 | if (kbuf) { | |
1114 | compat_ulong_t *k = kbuf; | |
04a1e62c | 1115 | while (count >= sizeof(*k)) { |
91e7b707 RM |
1116 | getreg32(target, pos, k++); |
1117 | count -= sizeof(*k); | |
1118 | pos += sizeof(*k); | |
1119 | } | |
1120 | } else { | |
1121 | compat_ulong_t __user *u = ubuf; | |
04a1e62c | 1122 | while (count >= sizeof(*u)) { |
91e7b707 RM |
1123 | compat_ulong_t word; |
1124 | getreg32(target, pos, &word); | |
1125 | if (__put_user(word, u++)) | |
1126 | return -EFAULT; | |
1127 | count -= sizeof(*u); | |
1128 | pos += sizeof(*u); | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | ||
1135 | static int genregs32_set(struct task_struct *target, | |
1136 | const struct user_regset *regset, | |
1137 | unsigned int pos, unsigned int count, | |
1138 | const void *kbuf, const void __user *ubuf) | |
1139 | { | |
1140 | int ret = 0; | |
1141 | if (kbuf) { | |
1142 | const compat_ulong_t *k = kbuf; | |
04a1e62c | 1143 | while (count >= sizeof(*k) && !ret) { |
f9cb02b0 | 1144 | ret = putreg32(target, pos, *k++); |
91e7b707 RM |
1145 | count -= sizeof(*k); |
1146 | pos += sizeof(*k); | |
1147 | } | |
1148 | } else { | |
1149 | const compat_ulong_t __user *u = ubuf; | |
04a1e62c | 1150 | while (count >= sizeof(*u) && !ret) { |
91e7b707 RM |
1151 | compat_ulong_t word; |
1152 | ret = __get_user(word, u++); | |
1153 | if (ret) | |
1154 | break; | |
f9cb02b0 | 1155 | ret = putreg32(target, pos, word); |
91e7b707 RM |
1156 | count -= sizeof(*u); |
1157 | pos += sizeof(*u); | |
1158 | } | |
1159 | } | |
1160 | return ret; | |
1161 | } | |
1162 | ||
55283e25 L |
1163 | #ifdef CONFIG_X86_X32_ABI |
1164 | static long x32_arch_ptrace(struct task_struct *child, | |
1165 | compat_long_t request, compat_ulong_t caddr, | |
1166 | compat_ulong_t cdata) | |
1167 | { | |
1168 | unsigned long addr = caddr; | |
1169 | unsigned long data = cdata; | |
1170 | void __user *datap = compat_ptr(data); | |
1171 | int ret; | |
1172 | ||
1173 | switch (request) { | |
1174 | /* Read 32bits at location addr in the USER area. Only allow | |
1175 | to return the lower 32bits of segment and debug registers. */ | |
1176 | case PTRACE_PEEKUSR: { | |
1177 | u32 tmp; | |
1178 | ||
1179 | ret = -EIO; | |
1180 | if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) || | |
1181 | addr < offsetof(struct user_regs_struct, cs)) | |
1182 | break; | |
1183 | ||
1184 | tmp = 0; /* Default return condition */ | |
1185 | if (addr < sizeof(struct user_regs_struct)) | |
1186 | tmp = getreg(child, addr); | |
1187 | else if (addr >= offsetof(struct user, u_debugreg[0]) && | |
1188 | addr <= offsetof(struct user, u_debugreg[7])) { | |
1189 | addr -= offsetof(struct user, u_debugreg[0]); | |
1190 | tmp = ptrace_get_debugreg(child, addr / sizeof(data)); | |
1191 | } | |
1192 | ret = put_user(tmp, (__u32 __user *)datap); | |
1193 | break; | |
1194 | } | |
1195 | ||
1196 | /* Write the word at location addr in the USER area. Only allow | |
1197 | to update segment and debug registers with the upper 32bits | |
1198 | zero-extended. */ | |
1199 | case PTRACE_POKEUSR: | |
1200 | ret = -EIO; | |
1201 | if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) || | |
1202 | addr < offsetof(struct user_regs_struct, cs)) | |
1203 | break; | |
1204 | ||
1205 | if (addr < sizeof(struct user_regs_struct)) | |
1206 | ret = putreg(child, addr, data); | |
1207 | else if (addr >= offsetof(struct user, u_debugreg[0]) && | |
1208 | addr <= offsetof(struct user, u_debugreg[7])) { | |
1209 | addr -= offsetof(struct user, u_debugreg[0]); | |
1210 | ret = ptrace_set_debugreg(child, | |
1211 | addr / sizeof(data), data); | |
1212 | } | |
1213 | break; | |
1214 | ||
1215 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ | |
1216 | return copy_regset_to_user(child, | |
1217 | task_user_regset_view(current), | |
1218 | REGSET_GENERAL, | |
1219 | 0, sizeof(struct user_regs_struct), | |
1220 | datap); | |
1221 | ||
1222 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ | |
1223 | return copy_regset_from_user(child, | |
1224 | task_user_regset_view(current), | |
1225 | REGSET_GENERAL, | |
1226 | 0, sizeof(struct user_regs_struct), | |
1227 | datap); | |
1228 | ||
1229 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ | |
1230 | return copy_regset_to_user(child, | |
1231 | task_user_regset_view(current), | |
1232 | REGSET_FP, | |
1233 | 0, sizeof(struct user_i387_struct), | |
1234 | datap); | |
1235 | ||
1236 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ | |
1237 | return copy_regset_from_user(child, | |
1238 | task_user_regset_view(current), | |
1239 | REGSET_FP, | |
1240 | 0, sizeof(struct user_i387_struct), | |
1241 | datap); | |
1242 | ||
55283e25 L |
1243 | default: |
1244 | return compat_ptrace_request(child, request, addr, data); | |
1245 | } | |
1246 | ||
1247 | return ret; | |
1248 | } | |
1249 | #endif | |
1250 | ||
562b80ba RM |
1251 | long compat_arch_ptrace(struct task_struct *child, compat_long_t request, |
1252 | compat_ulong_t caddr, compat_ulong_t cdata) | |
099cd6e9 | 1253 | { |
562b80ba RM |
1254 | unsigned long addr = caddr; |
1255 | unsigned long data = cdata; | |
099cd6e9 RM |
1256 | void __user *datap = compat_ptr(data); |
1257 | int ret; | |
1258 | __u32 val; | |
1259 | ||
55283e25 L |
1260 | #ifdef CONFIG_X86_X32_ABI |
1261 | if (!is_ia32_task()) | |
1262 | return x32_arch_ptrace(child, request, caddr, cdata); | |
1263 | #endif | |
1264 | ||
099cd6e9 | 1265 | switch (request) { |
099cd6e9 RM |
1266 | case PTRACE_PEEKUSR: |
1267 | ret = getreg32(child, addr, &val); | |
1268 | if (ret == 0) | |
1269 | ret = put_user(val, (__u32 __user *)datap); | |
1270 | break; | |
1271 | ||
1272 | case PTRACE_POKEUSR: | |
1273 | ret = putreg32(child, addr, data); | |
1274 | break; | |
1275 | ||
5a4646a4 RM |
1276 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ |
1277 | return copy_regset_to_user(child, &user_x86_32_view, | |
1278 | REGSET_GENERAL, | |
1279 | 0, sizeof(struct user_regs_struct32), | |
1280 | datap); | |
1281 | ||
1282 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ | |
1283 | return copy_regset_from_user(child, &user_x86_32_view, | |
1284 | REGSET_GENERAL, 0, | |
1285 | sizeof(struct user_regs_struct32), | |
1286 | datap); | |
1287 | ||
1288 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ | |
1289 | return copy_regset_to_user(child, &user_x86_32_view, | |
1290 | REGSET_FP, 0, | |
1291 | sizeof(struct user_i387_ia32_struct), | |
1292 | datap); | |
1293 | ||
1294 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ | |
1295 | return copy_regset_from_user( | |
1296 | child, &user_x86_32_view, REGSET_FP, | |
1297 | 0, sizeof(struct user_i387_ia32_struct), datap); | |
1298 | ||
1299 | case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */ | |
1300 | return copy_regset_to_user(child, &user_x86_32_view, | |
1301 | REGSET_XFP, 0, | |
1302 | sizeof(struct user32_fxsr_struct), | |
1303 | datap); | |
1304 | ||
1305 | case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */ | |
1306 | return copy_regset_from_user(child, &user_x86_32_view, | |
1307 | REGSET_XFP, 0, | |
1308 | sizeof(struct user32_fxsr_struct), | |
1309 | datap); | |
099cd6e9 | 1310 | |
562b80ba RM |
1311 | case PTRACE_GET_THREAD_AREA: |
1312 | case PTRACE_SET_THREAD_AREA: | |
1313 | return arch_ptrace(child, request, addr, data); | |
1314 | ||
099cd6e9 | 1315 | default: |
fdadd54d | 1316 | return compat_ptrace_request(child, request, addr, data); |
099cd6e9 RM |
1317 | } |
1318 | ||
099cd6e9 RM |
1319 | return ret; |
1320 | } | |
1321 | ||
cb757c41 RM |
1322 | #endif /* CONFIG_IA32_EMULATION */ |
1323 | ||
070459d9 RM |
1324 | #ifdef CONFIG_X86_64 |
1325 | ||
5b3efd50 | 1326 | static struct user_regset x86_64_regsets[] __read_mostly = { |
070459d9 RM |
1327 | [REGSET_GENERAL] = { |
1328 | .core_note_type = NT_PRSTATUS, | |
1329 | .n = sizeof(struct user_regs_struct) / sizeof(long), | |
1330 | .size = sizeof(long), .align = sizeof(long), | |
1331 | .get = genregs_get, .set = genregs_set | |
1332 | }, | |
1333 | [REGSET_FP] = { | |
1334 | .core_note_type = NT_PRFPREG, | |
1335 | .n = sizeof(struct user_i387_struct) / sizeof(long), | |
1336 | .size = sizeof(long), .align = sizeof(long), | |
1337 | .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set | |
1338 | }, | |
5b3efd50 SS |
1339 | [REGSET_XSTATE] = { |
1340 | .core_note_type = NT_X86_XSTATE, | |
1341 | .size = sizeof(u64), .align = sizeof(u64), | |
1342 | .active = xstateregs_active, .get = xstateregs_get, | |
1343 | .set = xstateregs_set | |
1344 | }, | |
325af5fb RM |
1345 | [REGSET_IOPERM64] = { |
1346 | .core_note_type = NT_386_IOPERM, | |
1347 | .n = IO_BITMAP_LONGS, | |
1348 | .size = sizeof(long), .align = sizeof(long), | |
1349 | .active = ioperm_active, .get = ioperm_get | |
1350 | }, | |
070459d9 RM |
1351 | }; |
1352 | ||
1353 | static const struct user_regset_view user_x86_64_view = { | |
1354 | .name = "x86_64", .e_machine = EM_X86_64, | |
1355 | .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets) | |
1356 | }; | |
1357 | ||
1358 | #else /* CONFIG_X86_32 */ | |
1359 | ||
1360 | #define user_regs_struct32 user_regs_struct | |
1361 | #define genregs32_get genregs_get | |
1362 | #define genregs32_set genregs_set | |
1363 | ||
1364 | #endif /* CONFIG_X86_64 */ | |
1365 | ||
1366 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION | |
5b3efd50 | 1367 | static struct user_regset x86_32_regsets[] __read_mostly = { |
070459d9 RM |
1368 | [REGSET_GENERAL] = { |
1369 | .core_note_type = NT_PRSTATUS, | |
1370 | .n = sizeof(struct user_regs_struct32) / sizeof(u32), | |
1371 | .size = sizeof(u32), .align = sizeof(u32), | |
1372 | .get = genregs32_get, .set = genregs32_set | |
1373 | }, | |
1374 | [REGSET_FP] = { | |
1375 | .core_note_type = NT_PRFPREG, | |
1f465f4e | 1376 | .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32), |
070459d9 RM |
1377 | .size = sizeof(u32), .align = sizeof(u32), |
1378 | .active = fpregs_active, .get = fpregs_get, .set = fpregs_set | |
1379 | }, | |
1380 | [REGSET_XFP] = { | |
1381 | .core_note_type = NT_PRXFPREG, | |
1f465f4e | 1382 | .n = sizeof(struct user32_fxsr_struct) / sizeof(u32), |
070459d9 RM |
1383 | .size = sizeof(u32), .align = sizeof(u32), |
1384 | .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set | |
1385 | }, | |
5b3efd50 SS |
1386 | [REGSET_XSTATE] = { |
1387 | .core_note_type = NT_X86_XSTATE, | |
1388 | .size = sizeof(u64), .align = sizeof(u64), | |
1389 | .active = xstateregs_active, .get = xstateregs_get, | |
1390 | .set = xstateregs_set | |
1391 | }, | |
070459d9 | 1392 | [REGSET_TLS] = { |
bb61682b | 1393 | .core_note_type = NT_386_TLS, |
070459d9 RM |
1394 | .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN, |
1395 | .size = sizeof(struct user_desc), | |
1396 | .align = sizeof(struct user_desc), | |
1397 | .active = regset_tls_active, | |
1398 | .get = regset_tls_get, .set = regset_tls_set | |
1399 | }, | |
325af5fb RM |
1400 | [REGSET_IOPERM32] = { |
1401 | .core_note_type = NT_386_IOPERM, | |
1402 | .n = IO_BITMAP_BYTES / sizeof(u32), | |
1403 | .size = sizeof(u32), .align = sizeof(u32), | |
1404 | .active = ioperm_active, .get = ioperm_get | |
1405 | }, | |
070459d9 RM |
1406 | }; |
1407 | ||
1408 | static const struct user_regset_view user_x86_32_view = { | |
1409 | .name = "i386", .e_machine = EM_386, | |
1410 | .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets) | |
1411 | }; | |
1412 | #endif | |
1413 | ||
5b3efd50 SS |
1414 | /* |
1415 | * This represents bytes 464..511 in the memory layout exported through | |
1416 | * the REGSET_XSTATE interface. | |
1417 | */ | |
1418 | u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS]; | |
1419 | ||
1420 | void update_regset_xstate_info(unsigned int size, u64 xstate_mask) | |
1421 | { | |
1422 | #ifdef CONFIG_X86_64 | |
1423 | x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64); | |
1424 | #endif | |
1425 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION | |
1426 | x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64); | |
1427 | #endif | |
1428 | xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask; | |
1429 | } | |
1430 | ||
070459d9 RM |
1431 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
1432 | { | |
1433 | #ifdef CONFIG_IA32_EMULATION | |
1434 | if (test_tsk_thread_flag(task, TIF_IA32)) | |
1435 | #endif | |
1436 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION | |
1437 | return &user_x86_32_view; | |
1438 | #endif | |
1439 | #ifdef CONFIG_X86_64 | |
1440 | return &user_x86_64_view; | |
1441 | #endif | |
1442 | } | |
1443 | ||
7f38551f ON |
1444 | static void fill_sigtrap_info(struct task_struct *tsk, |
1445 | struct pt_regs *regs, | |
1446 | int error_code, int si_code, | |
1447 | struct siginfo *info) | |
1da177e4 | 1448 | { |
51e7dc70 | 1449 | tsk->thread.trap_nr = X86_TRAP_DB; |
1da177e4 LT |
1450 | tsk->thread.error_code = error_code; |
1451 | ||
7f38551f ON |
1452 | memset(info, 0, sizeof(*info)); |
1453 | info->si_signo = SIGTRAP; | |
1454 | info->si_code = si_code; | |
1455 | info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL; | |
1456 | } | |
1457 | ||
1458 | void user_single_step_siginfo(struct task_struct *tsk, | |
1459 | struct pt_regs *regs, | |
1460 | struct siginfo *info) | |
1461 | { | |
1462 | fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info); | |
1463 | } | |
1da177e4 | 1464 | |
7f38551f ON |
1465 | void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, |
1466 | int error_code, int si_code) | |
1467 | { | |
1468 | struct siginfo info; | |
1da177e4 | 1469 | |
7f38551f | 1470 | fill_sigtrap_info(tsk, regs, error_code, si_code, &info); |
27b46d76 | 1471 | /* Send us the fake SIGTRAP */ |
1da177e4 LT |
1472 | force_sig_info(SIGTRAP, &info, tsk); |
1473 | } | |
1474 | ||
86976cd8 | 1475 | |
d4d67150 RM |
1476 | #ifdef CONFIG_X86_32 |
1477 | # define IS_IA32 1 | |
1478 | #elif defined CONFIG_IA32_EMULATION | |
ccbe495c | 1479 | # define IS_IA32 is_compat_task() |
d4d67150 RM |
1480 | #else |
1481 | # define IS_IA32 0 | |
1482 | #endif | |
1483 | ||
1484 | /* | |
1485 | * We must return the syscall number to actually look up in the table. | |
1486 | * This can be -1L to skip running any syscall at all. | |
1487 | */ | |
1b4ac2a9 | 1488 | long syscall_trace_enter(struct pt_regs *regs) |
86976cd8 | 1489 | { |
d4d67150 RM |
1490 | long ret = 0; |
1491 | ||
bf5a3c13 FW |
1492 | rcu_user_exit(); |
1493 | ||
380fdd75 RM |
1494 | /* |
1495 | * If we stepped into a sysenter/syscall insn, it trapped in | |
1496 | * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP. | |
1497 | * If user-mode had set TF itself, then it's still clear from | |
1498 | * do_debug() and we need to set it again to restore the user | |
1499 | * state. If we entered on the slow path, TF was already set. | |
1500 | */ | |
1501 | if (test_thread_flag(TIF_SINGLESTEP)) | |
1502 | regs->flags |= X86_EFLAGS_TF; | |
1503 | ||
86976cd8 | 1504 | /* do the secure computing check first */ |
c6cfbeb4 WD |
1505 | if (secure_computing(regs->orig_ax)) { |
1506 | /* seccomp failures shouldn't expose any additional code. */ | |
1507 | ret = -1L; | |
1508 | goto out; | |
1509 | } | |
86976cd8 | 1510 | |
d4d67150 RM |
1511 | if (unlikely(test_thread_flag(TIF_SYSCALL_EMU))) |
1512 | ret = -1L; | |
1513 | ||
eeea3c3f RM |
1514 | if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) && |
1515 | tracehook_report_syscall_entry(regs)) | |
1516 | ret = -1L; | |
86976cd8 | 1517 | |
66700001 | 1518 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
1c569f02 | 1519 | trace_sys_enter(regs, regs->orig_ax); |
1b3fa2ce | 1520 | |
b05d8447 EP |
1521 | if (IS_IA32) |
1522 | audit_syscall_entry(AUDIT_ARCH_I386, | |
1523 | regs->orig_ax, | |
1524 | regs->bx, regs->cx, | |
1525 | regs->dx, regs->si); | |
d4d67150 | 1526 | #ifdef CONFIG_X86_64 |
b05d8447 EP |
1527 | else |
1528 | audit_syscall_entry(AUDIT_ARCH_X86_64, | |
1529 | regs->orig_ax, | |
1530 | regs->di, regs->si, | |
1531 | regs->dx, regs->r10); | |
d4d67150 | 1532 | #endif |
d4d67150 | 1533 | |
c6cfbeb4 | 1534 | out: |
d4d67150 | 1535 | return ret ?: regs->orig_ax; |
86976cd8 RM |
1536 | } |
1537 | ||
1b4ac2a9 | 1538 | void syscall_trace_leave(struct pt_regs *regs) |
86976cd8 | 1539 | { |
d5196503 ON |
1540 | bool step; |
1541 | ||
d7e7528b | 1542 | audit_syscall_exit(regs); |
86976cd8 | 1543 | |
66700001 | 1544 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
1c569f02 | 1545 | trace_sys_exit(regs, regs->ax); |
1b3fa2ce | 1546 | |
d4d67150 RM |
1547 | /* |
1548 | * If TIF_SYSCALL_EMU is set, we only get here because of | |
1549 | * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP). | |
1550 | * We already reported this syscall instruction in | |
d5196503 | 1551 | * syscall_trace_enter(). |
d4d67150 | 1552 | */ |
d5196503 ON |
1553 | step = unlikely(test_thread_flag(TIF_SINGLESTEP)) && |
1554 | !test_thread_flag(TIF_SYSCALL_EMU); | |
1555 | if (step || test_thread_flag(TIF_SYSCALL_TRACE)) | |
1556 | tracehook_report_syscall_exit(regs, step); | |
bf5a3c13 FW |
1557 | |
1558 | rcu_user_enter(); | |
d4d67150 | 1559 | } |