1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
6 * x86-64 port 2000-2002 Andi Kleen
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
18 #include <linux/seccomp.h>
19 #include <linux/signal.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
23 #include <asm/system.h>
24 #include <asm/processor.h>
25 #include <asm/prctl.h>
27 #include <asm/debugreg.h>
30 #include <asm/proto.h>
34 * does not yet catch signals sent when the child dies.
35 * in exit.c or in signal.c.
39 * Determines which flags the user has access to [1 = access, 0 = no access].
40 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
41 * Also masks reserved bits (63-22, 15, 5, 3, 1).
43 #define FLAG_MASK 0x54dd5UL
46 * Called by kernel/ptrace.c when detaching..
48 * Make sure the single step bit is not set.
50 void ptrace_disable(struct task_struct
*child
)
52 user_disable_single_step(child
);
55 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
57 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
58 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
61 static int putreg(struct task_struct
*child
,
62 unsigned long regno
, unsigned long value
)
64 struct pt_regs
*regs
= task_pt_regs(child
);
66 case offsetof(struct user_regs_struct
,fs
):
67 if (value
&& (value
& 3) != 3)
69 child
->thread
.fsindex
= value
& 0xffff;
71 case offsetof(struct user_regs_struct
,gs
):
72 if (value
&& (value
& 3) != 3)
74 child
->thread
.gsindex
= value
& 0xffff;
76 case offsetof(struct user_regs_struct
,ds
):
77 if (value
&& (value
& 3) != 3)
79 child
->thread
.ds
= value
& 0xffff;
81 case offsetof(struct user_regs_struct
,es
):
82 if (value
&& (value
& 3) != 3)
84 child
->thread
.es
= value
& 0xffff;
86 case offsetof(struct user_regs_struct
,ss
):
91 case offsetof(struct user_regs_struct
,fs_base
):
92 if (value
>= TASK_SIZE_OF(child
))
95 * When changing the segment base, use do_arch_prctl
96 * to set either thread.fs or thread.fsindex and the
97 * corresponding GDT slot.
99 if (child
->thread
.fs
!= value
)
100 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
102 case offsetof(struct user_regs_struct
,gs_base
):
104 * Exactly the same here as the %fs handling above.
106 if (value
>= TASK_SIZE_OF(child
))
108 if (child
->thread
.gs
!= value
)
109 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
111 case offsetof(struct user_regs_struct
,flags
):
114 * If the user value contains TF, mark that
115 * it was not "us" (the debugger) that set it.
116 * If not, make sure it stays set if we had.
118 if (value
& X86_EFLAGS_TF
)
119 clear_tsk_thread_flag(child
, TIF_FORCED_TF
);
120 else if (test_tsk_thread_flag(child
, TIF_FORCED_TF
))
121 value
|= X86_EFLAGS_TF
;
122 value
|= regs
->flags
& ~FLAG_MASK
;
124 case offsetof(struct user_regs_struct
,cs
):
125 if ((value
& 3) != 3)
130 *pt_regs_access(regs
, regno
) = value
;
134 static unsigned long getreg(struct task_struct
*child
, unsigned long regno
)
136 struct pt_regs
*regs
= task_pt_regs(child
);
139 case offsetof(struct user_regs_struct
, fs
):
140 return child
->thread
.fsindex
;
141 case offsetof(struct user_regs_struct
, gs
):
142 return child
->thread
.gsindex
;
143 case offsetof(struct user_regs_struct
, ds
):
144 return child
->thread
.ds
;
145 case offsetof(struct user_regs_struct
, es
):
146 return child
->thread
.es
;
147 case offsetof(struct user_regs_struct
, fs_base
):
149 * do_arch_prctl may have used a GDT slot instead of
150 * the MSR. To userland, it appears the same either
151 * way, except the %fs segment selector might not be 0.
153 if (child
->thread
.fs
!= 0)
154 return child
->thread
.fs
;
155 if (child
->thread
.fsindex
!= FS_TLS_SEL
)
157 return get_desc_base(&child
->thread
.tls_array
[FS_TLS
]);
158 case offsetof(struct user_regs_struct
, gs_base
):
160 * Exactly the same here as the %fs handling above.
162 if (child
->thread
.gs
!= 0)
163 return child
->thread
.gs
;
164 if (child
->thread
.gsindex
!= GS_TLS_SEL
)
166 return get_desc_base(&child
->thread
.tls_array
[GS_TLS
]);
167 case offsetof(struct user_regs_struct
, flags
):
169 * If the debugger set TF, hide it from the readout.
172 if (test_tsk_thread_flag(child
, TIF_IA32
))
174 if (test_tsk_thread_flag(child
, TIF_FORCED_TF
))
175 val
&= ~X86_EFLAGS_TF
;
178 val
= *pt_regs_access(regs
, regno
);
179 if (test_tsk_thread_flag(child
, TIF_IA32
))
186 unsigned long ptrace_get_debugreg(struct task_struct
*child
, int n
)
189 case 0: return child
->thread
.debugreg0
;
190 case 1: return child
->thread
.debugreg1
;
191 case 2: return child
->thread
.debugreg2
;
192 case 3: return child
->thread
.debugreg3
;
193 case 6: return child
->thread
.debugreg6
;
194 case 7: return child
->thread
.debugreg7
;
199 int ptrace_set_debugreg(struct task_struct
*child
, int n
, unsigned long data
)
204 int dsize
= test_tsk_thread_flag(child
, TIF_IA32
) ? 3 : 7;
205 if (unlikely(data
>= TASK_SIZE_OF(child
) - dsize
))
210 case 0: child
->thread
.debugreg0
= data
; break;
211 case 1: child
->thread
.debugreg1
= data
; break;
212 case 2: child
->thread
.debugreg2
= data
; break;
213 case 3: child
->thread
.debugreg3
= data
; break;
218 child
->thread
.debugreg6
= data
;
223 * See ptrace_32.c for an explanation of this awkward check.
225 data
&= ~DR_CONTROL_RESERVED
;
226 for (i
= 0; i
< 4; i
++)
227 if ((0x5554 >> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
229 child
->thread
.debugreg7
= data
;
231 set_tsk_thread_flag(child
, TIF_DEBUG
);
233 clear_tsk_thread_flag(child
, TIF_DEBUG
);
240 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
246 /* when I and D space are separate, these will need to be fixed. */
247 case PTRACE_PEEKTEXT
: /* read word at location addr. */
248 case PTRACE_PEEKDATA
:
249 ret
= generic_ptrace_peekdata(child
, addr
, data
);
252 /* read the word at location addr in the USER area. */
253 case PTRACE_PEEKUSR
: {
258 addr
> sizeof(struct user
) - 7)
262 if (addr
< sizeof(struct user_regs_struct
))
263 tmp
= getreg(child
, addr
);
264 else if (addr
>= offsetof(struct user
, u_debugreg
[0])) {
265 addr
-= offsetof(struct user
, u_debugreg
[0]);
266 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(long));
269 ret
= put_user(tmp
,(unsigned long __user
*) data
);
273 /* when I and D space are separate, this will have to be fixed. */
274 case PTRACE_POKETEXT
: /* write the word at location addr. */
275 case PTRACE_POKEDATA
:
276 ret
= generic_ptrace_pokedata(child
, addr
, data
);
279 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
282 addr
> sizeof(struct user
) - 7)
285 if (addr
< sizeof(struct user_regs_struct
))
286 ret
= putreg(child
, addr
, data
);
287 else if (addr
>= offsetof(struct user
, u_debugreg
[0])) {
288 addr
-= offsetof(struct user
, u_debugreg
[0]);
289 ret
= ptrace_set_debugreg(child
,
290 addr
/ sizeof(long), data
);
294 #ifdef CONFIG_IA32_EMULATION
295 /* This makes only sense with 32bit programs. Allow a
296 64bit debugger to fully examine them too. Better
297 don't use it against 64bit processes, use
298 PTRACE_ARCH_PRCTL instead. */
299 case PTRACE_GET_THREAD_AREA
:
302 ret
= do_get_thread_area(child
, addr
,
303 (struct user_desc __user
*) data
);
306 case PTRACE_SET_THREAD_AREA
:
309 ret
= do_set_thread_area(child
, addr
,
310 (struct user_desc __user
*) data
, 0);
313 /* normal 64bit interface to access TLS data.
314 Works just like arch_prctl, except that the arguments
316 case PTRACE_ARCH_PRCTL
:
317 ret
= do_arch_prctl(child
, data
, addr
);
320 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
321 if (!access_ok(VERIFY_WRITE
, (unsigned __user
*)data
,
322 sizeof(struct user_regs_struct
))) {
327 for (ui
= 0; ui
< sizeof(struct user_regs_struct
); ui
+= sizeof(long)) {
328 ret
|= __put_user(getreg(child
, ui
),(unsigned long __user
*) data
);
329 data
+= sizeof(long);
334 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
336 if (!access_ok(VERIFY_READ
, (unsigned __user
*)data
,
337 sizeof(struct user_regs_struct
))) {
342 for (ui
= 0; ui
< sizeof(struct user_regs_struct
); ui
+= sizeof(long)) {
343 ret
= __get_user(tmp
, (unsigned long __user
*) data
);
346 ret
= putreg(child
, ui
, tmp
);
349 data
+= sizeof(long);
354 case PTRACE_GETFPREGS
: { /* Get the child extended FPU state. */
355 if (!access_ok(VERIFY_WRITE
, (unsigned __user
*)data
,
356 sizeof(struct user_i387_struct
))) {
360 ret
= get_fpregs((struct user_i387_struct __user
*)data
, child
);
364 case PTRACE_SETFPREGS
: { /* Set the child extended FPU state. */
365 if (!access_ok(VERIFY_READ
, (unsigned __user
*)data
,
366 sizeof(struct user_i387_struct
))) {
370 set_stopped_child_used_math(child
);
371 ret
= set_fpregs(child
, (struct user_i387_struct __user
*)data
);
376 ret
= ptrace_request(child
, request
, addr
, data
);
382 static void syscall_trace(struct pt_regs
*regs
)
386 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
388 regs
->ip
, regs
->sp
, regs
->ax
, regs
->orig_ax
, __builtin_return_address(0),
389 current_thread_info()->flags
, current
->ptrace
);
392 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
395 * this isn't the same as continuing with a signal, but it will do
396 * for normal use. strace only continues with a signal if the
397 * stopping signal is not SIGTRAP. -brl
399 if (current
->exit_code
) {
400 send_sig(current
->exit_code
, current
, 1);
401 current
->exit_code
= 0;
405 asmlinkage
void syscall_trace_enter(struct pt_regs
*regs
)
407 /* do the secure computing check first */
408 secure_computing(regs
->orig_ax
);
410 if (test_thread_flag(TIF_SYSCALL_TRACE
)
411 && (current
->ptrace
& PT_PTRACED
))
414 if (unlikely(current
->audit_context
)) {
415 if (test_thread_flag(TIF_IA32
)) {
416 audit_syscall_entry(AUDIT_ARCH_I386
,
421 audit_syscall_entry(AUDIT_ARCH_X86_64
,
424 regs
->dx
, regs
->r10
);
429 asmlinkage
void syscall_trace_leave(struct pt_regs
*regs
)
431 if (unlikely(current
->audit_context
))
432 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
434 if ((test_thread_flag(TIF_SYSCALL_TRACE
)
435 || test_thread_flag(TIF_SINGLESTEP
))
436 && (current
->ptrace
& PT_PTRACED
))