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