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