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