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