2 /* By Ross Biro 1/23/92 */
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
11 #include <linux/smp.h>
12 #include <linux/errno.h>
13 #include <linux/ptrace.h>
14 #include <linux/user.h>
15 #include <linux/security.h>
16 #include <linux/audit.h>
17 #include <linux/seccomp.h>
18 #include <linux/signal.h>
20 #include <asm/uaccess.h>
21 #include <asm/pgtable.h>
22 #include <asm/system.h>
23 #include <asm/processor.h>
25 #include <asm/debugreg.h>
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
35 * Determines which flags the user has access to [1 = access, 0 = no access].
36 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
37 * Also masks reserved bits (31-22, 15, 5, 3, 1).
39 #define FLAG_MASK 0x00050dd5
41 /* set's the trap flag. */
42 #define TRAP_FLAG 0x100
45 * Offset of eflags on child stack..
47 #define EFL_OFFSET offsetof(struct pt_regs, eflags)
49 static inline struct pt_regs
*get_child_regs(struct task_struct
*task
)
51 void *stack_top
= (void *)task
->thread
.esp0
;
52 return stack_top
- sizeof(struct pt_regs
);
56 * This routine will get a word off of the processes privileged stack.
57 * the offset is bytes into the pt_regs structure on the stack.
58 * This routine assumes that all the privileged stacks are in our
61 static inline int get_stack_long(struct task_struct
*task
, int offset
)
65 stack
= (unsigned char *)task
->thread
.esp0
- sizeof(struct pt_regs
);
67 return (*((int *)stack
));
71 * This routine will put a word on the processes privileged stack.
72 * the offset is bytes into the pt_regs structure on the stack.
73 * This routine assumes that all the privileged stacks are in our
76 static inline int put_stack_long(struct task_struct
*task
, int offset
,
79 unsigned char * stack
;
81 stack
= (unsigned char *)task
->thread
.esp0
- sizeof(struct pt_regs
);
83 *(unsigned long *) stack
= data
;
87 static int putreg(struct task_struct
*child
,
88 unsigned long regno
, unsigned long value
)
92 if (value
&& (value
& 3) != 3)
94 child
->thread
.gs
= value
;
99 if (value
&& (value
& 3) != 3)
105 if ((value
& 3) != 3)
111 value
|= get_stack_long(child
, EFL_OFFSET
) & ~FLAG_MASK
;
116 put_stack_long(child
, regno
, value
);
120 static unsigned long getreg(struct task_struct
*child
,
123 unsigned long retval
= ~0UL;
125 switch (regno
>> 2) {
127 retval
= child
->thread
.gs
;
139 retval
&= get_stack_long(child
, regno
);
144 #define LDT_SEGMENT 4
146 static unsigned long convert_eip_to_linear(struct task_struct
*child
, struct pt_regs
*regs
)
148 unsigned long addr
, seg
;
151 seg
= regs
->xcs
& 0xffff;
152 if (regs
->eflags
& VM_MASK
) {
153 addr
= (addr
& 0xffff) + (seg
<< 4);
158 * We'll assume that the code segments in the GDT
159 * are all zero-based. That is largely true: the
160 * TLS segments are used for data, and the PNPBIOS
161 * and APM bios ones we just ignore here.
163 if (seg
& LDT_SEGMENT
) {
167 down(&child
->mm
->context
.sem
);
168 desc
= child
->mm
->context
.ldt
+ (seg
& ~7);
169 base
= (desc
[0] >> 16) | ((desc
[1] & 0xff) << 16) | (desc
[1] & 0xff000000);
171 /* 16-bit code segment? */
172 if (!((desc
[1] >> 22) & 1))
175 up(&child
->mm
->context
.sem
);
180 static inline int is_setting_trap_flag(struct task_struct
*child
, struct pt_regs
*regs
)
183 unsigned char opcode
[15];
184 unsigned long addr
= convert_eip_to_linear(child
, regs
);
186 copied
= access_process_vm(child
, addr
, opcode
, sizeof(opcode
), 0);
187 for (i
= 0; i
< copied
; i
++) {
190 case 0x9d: case 0xcf:
192 /* opcode and address size prefixes */
193 case 0x66: case 0x67:
195 /* irrelevant prefixes (segment overrides and repeats) */
196 case 0x26: case 0x2e:
197 case 0x36: case 0x3e:
198 case 0x64: case 0x65:
199 case 0xf0: case 0xf2: case 0xf3:
203 * pushf: NOTE! We should probably not let
204 * the user see the TF bit being set. But
205 * it's more pain than it's worth to avoid
206 * it, and a debugger could emulate this
207 * all in user space if it _really_ cares.
217 static void set_singlestep(struct task_struct
*child
)
219 struct pt_regs
*regs
= get_child_regs(child
);
222 * Always set TIF_SINGLESTEP - this guarantees that
223 * we single-step system calls etc.. This will also
224 * cause us to set TF when returning to user mode.
226 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
229 * If TF was already set, don't do anything else
231 if (regs
->eflags
& TRAP_FLAG
)
234 /* Set TF on the kernel stack.. */
235 regs
->eflags
|= TRAP_FLAG
;
238 * ..but if TF is changed by the instruction we will trace,
239 * don't mark it as being "us" that set it, so that we
240 * won't clear it by hand later.
242 if (is_setting_trap_flag(child
, regs
))
245 child
->ptrace
|= PT_DTRACE
;
248 static void clear_singlestep(struct task_struct
*child
)
250 /* Always clear TIF_SINGLESTEP... */
251 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
253 /* But touch TF only if it was set by us.. */
254 if (child
->ptrace
& PT_DTRACE
) {
255 struct pt_regs
*regs
= get_child_regs(child
);
256 regs
->eflags
&= ~TRAP_FLAG
;
257 child
->ptrace
&= ~PT_DTRACE
;
262 * Called by kernel/ptrace.c when detaching..
264 * Make sure the single step bit is not set.
266 void ptrace_disable(struct task_struct
*child
)
268 clear_singlestep(child
);
269 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
270 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
274 * Perform get_thread_area on behalf of the traced child.
277 ptrace_get_thread_area(struct task_struct
*child
,
278 int idx
, struct user_desc __user
*user_desc
)
280 struct user_desc info
;
281 struct desc_struct
*desc
;
284 * Get the current Thread-Local Storage area:
287 #define GET_BASE(desc) ( \
288 (((desc)->a >> 16) & 0x0000ffff) | \
289 (((desc)->b << 16) & 0x00ff0000) | \
290 ( (desc)->b & 0xff000000) )
292 #define GET_LIMIT(desc) ( \
293 ((desc)->a & 0x0ffff) | \
294 ((desc)->b & 0xf0000) )
296 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
297 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
298 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
299 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
300 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
301 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
303 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
306 desc
= child
->thread
.tls_array
+ idx
- GDT_ENTRY_TLS_MIN
;
308 info
.entry_number
= idx
;
309 info
.base_addr
= GET_BASE(desc
);
310 info
.limit
= GET_LIMIT(desc
);
311 info
.seg_32bit
= GET_32BIT(desc
);
312 info
.contents
= GET_CONTENTS(desc
);
313 info
.read_exec_only
= !GET_WRITABLE(desc
);
314 info
.limit_in_pages
= GET_LIMIT_PAGES(desc
);
315 info
.seg_not_present
= !GET_PRESENT(desc
);
316 info
.useable
= GET_USEABLE(desc
);
318 if (copy_to_user(user_desc
, &info
, sizeof(info
)))
325 * Perform set_thread_area on behalf of the traced child.
328 ptrace_set_thread_area(struct task_struct
*child
,
329 int idx
, struct user_desc __user
*user_desc
)
331 struct user_desc info
;
332 struct desc_struct
*desc
;
334 if (copy_from_user(&info
, user_desc
, sizeof(info
)))
337 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
340 desc
= child
->thread
.tls_array
+ idx
- GDT_ENTRY_TLS_MIN
;
341 if (LDT_empty(&info
)) {
345 desc
->a
= LDT_entry_a(&info
);
346 desc
->b
= LDT_entry_b(&info
);
352 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
354 struct user
* dummy
= NULL
;
356 unsigned long __user
*datap
= (unsigned long __user
*)data
;
359 /* when I and D space are separate, these will need to be fixed. */
360 case PTRACE_PEEKTEXT
: /* read word at location addr. */
361 case PTRACE_PEEKDATA
:
362 ret
= generic_ptrace_peekdata(child
, addr
, data
);
365 /* read the word at location addr in the USER area. */
366 case PTRACE_PEEKUSR
: {
370 if ((addr
& 3) || addr
< 0 ||
371 addr
> sizeof(struct user
) - 3)
374 tmp
= 0; /* Default return condition */
375 if(addr
< FRAME_SIZE
*sizeof(long))
376 tmp
= getreg(child
, addr
);
377 if(addr
>= (long) &dummy
->u_debugreg
[0] &&
378 addr
<= (long) &dummy
->u_debugreg
[7]){
379 addr
-= (long) &dummy
->u_debugreg
[0];
381 tmp
= child
->thread
.debugreg
[addr
];
383 ret
= put_user(tmp
, datap
);
387 /* when I and D space are separate, this will have to be fixed. */
388 case PTRACE_POKETEXT
: /* write the word at location addr. */
389 case PTRACE_POKEDATA
:
391 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
396 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
398 if ((addr
& 3) || addr
< 0 ||
399 addr
> sizeof(struct user
) - 3)
402 if (addr
< FRAME_SIZE
*sizeof(long)) {
403 ret
= putreg(child
, addr
, data
);
406 /* We need to be very careful here. We implicitly
407 want to modify a portion of the task_struct, and we
408 have to be selective about what portions we allow someone
412 if(addr
>= (long) &dummy
->u_debugreg
[0] &&
413 addr
<= (long) &dummy
->u_debugreg
[7]){
415 if(addr
== (long) &dummy
->u_debugreg
[4]) break;
416 if(addr
== (long) &dummy
->u_debugreg
[5]) break;
417 if(addr
< (long) &dummy
->u_debugreg
[4] &&
418 ((unsigned long) data
) >= TASK_SIZE
-3) break;
420 /* Sanity-check data. Take one half-byte at once with
421 * check = (val >> (16 + 4*i)) & 0xf. It contains the
422 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
423 * 2 and 3 are LENi. Given a list of invalid values,
424 * we do mask |= 1 << invalid_value, so that
425 * (mask >> check) & 1 is a correct test for invalid
428 * R/Wi contains the type of the breakpoint /
429 * watchpoint, LENi contains the length of the watched
430 * data in the watchpoint case.
432 * The invalid values are:
433 * - LENi == 0x10 (undefined), so mask |= 0x0f00.
434 * - R/Wi == 0x10 (break on I/O reads or writes), so
436 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
439 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
441 * See the Intel Manual "System Programming Guide",
444 * Note that LENi == 0x10 is defined on x86_64 in long
445 * mode (i.e. even for 32-bit userspace software, but
446 * 64-bit kernel), so the x86_64 mask value is 0x5454.
447 * See the AMD manual no. 24593 (AMD64 System
450 if(addr
== (long) &dummy
->u_debugreg
[7]) {
451 data
&= ~DR_CONTROL_RESERVED
;
453 if ((0x5f54 >> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
456 set_tsk_thread_flag(child
, TIF_DEBUG
);
458 clear_tsk_thread_flag(child
, TIF_DEBUG
);
460 addr
-= (long) &dummy
->u_debugreg
;
462 child
->thread
.debugreg
[addr
] = data
;
467 case PTRACE_SYSEMU
: /* continue and stop at next syscall, which will not be executed */
468 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
469 case PTRACE_CONT
: /* restart after signal. */
471 if (!valid_signal(data
))
473 if (request
== PTRACE_SYSEMU
) {
474 set_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
475 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
476 } else if (request
== PTRACE_SYSCALL
) {
477 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
478 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
480 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
481 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
483 child
->exit_code
= data
;
484 /* make sure the single step bit is not set. */
485 clear_singlestep(child
);
486 wake_up_process(child
);
491 * make the child exit. Best I can do is send it a sigkill.
492 * perhaps it should be put in the status that it wants to
497 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
499 child
->exit_code
= SIGKILL
;
500 /* make sure the single step bit is not set. */
501 clear_singlestep(child
);
502 wake_up_process(child
);
505 case PTRACE_SYSEMU_SINGLESTEP
: /* Same as SYSEMU, but singlestep if not syscall */
506 case PTRACE_SINGLESTEP
: /* set the trap flag. */
508 if (!valid_signal(data
))
511 if (request
== PTRACE_SYSEMU_SINGLESTEP
)
512 set_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
514 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
516 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
517 set_singlestep(child
);
518 child
->exit_code
= data
;
519 /* give it a chance to run. */
520 wake_up_process(child
);
525 /* detach a process that was attached. */
526 ret
= ptrace_detach(child
, data
);
529 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
530 if (!access_ok(VERIFY_WRITE
, datap
, FRAME_SIZE
*sizeof(long))) {
534 for ( i
= 0; i
< FRAME_SIZE
*sizeof(long); i
+= sizeof(long) ) {
535 __put_user(getreg(child
, i
), datap
);
542 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
544 if (!access_ok(VERIFY_READ
, datap
, FRAME_SIZE
*sizeof(long))) {
548 for ( i
= 0; i
< FRAME_SIZE
*sizeof(long); i
+= sizeof(long) ) {
549 __get_user(tmp
, datap
);
550 putreg(child
, i
, tmp
);
557 case PTRACE_GETFPREGS
: { /* Get the child FPU state. */
558 if (!access_ok(VERIFY_WRITE
, datap
,
559 sizeof(struct user_i387_struct
))) {
564 if (!tsk_used_math(child
))
566 get_fpregs((struct user_i387_struct __user
*)data
, child
);
570 case PTRACE_SETFPREGS
: { /* Set the child FPU state. */
571 if (!access_ok(VERIFY_READ
, datap
,
572 sizeof(struct user_i387_struct
))) {
576 set_stopped_child_used_math(child
);
577 set_fpregs(child
, (struct user_i387_struct __user
*)data
);
582 case PTRACE_GETFPXREGS
: { /* Get the child extended FPU state. */
583 if (!access_ok(VERIFY_WRITE
, datap
,
584 sizeof(struct user_fxsr_struct
))) {
588 if (!tsk_used_math(child
))
590 ret
= get_fpxregs((struct user_fxsr_struct __user
*)data
, child
);
594 case PTRACE_SETFPXREGS
: { /* Set the child extended FPU state. */
595 if (!access_ok(VERIFY_READ
, datap
,
596 sizeof(struct user_fxsr_struct
))) {
600 set_stopped_child_used_math(child
);
601 ret
= set_fpxregs(child
, (struct user_fxsr_struct __user
*)data
);
605 case PTRACE_GET_THREAD_AREA
:
606 ret
= ptrace_get_thread_area(child
, addr
,
607 (struct user_desc __user
*) data
);
610 case PTRACE_SET_THREAD_AREA
:
611 ret
= ptrace_set_thread_area(child
, addr
,
612 (struct user_desc __user
*) data
);
616 ret
= ptrace_request(child
, request
, addr
, data
);
623 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
, int error_code
)
627 tsk
->thread
.trap_no
= 1;
628 tsk
->thread
.error_code
= error_code
;
630 memset(&info
, 0, sizeof(info
));
631 info
.si_signo
= SIGTRAP
;
632 info
.si_code
= TRAP_BRKPT
;
635 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->eip
: NULL
;
637 /* Send us the fakey SIGTRAP */
638 force_sig_info(SIGTRAP
, &info
, tsk
);
641 /* notification of system call entry/exit
642 * - triggered by current->work.syscall_trace
644 __attribute__((regparm(3)))
645 int do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
647 int is_sysemu
= test_thread_flag(TIF_SYSCALL_EMU
);
649 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
652 int is_singlestep
= !is_sysemu
&& test_thread_flag(TIF_SINGLESTEP
);
655 /* do the secure computing check first */
657 secure_computing(regs
->orig_eax
);
659 if (unlikely(current
->audit_context
)) {
661 audit_syscall_exit(AUDITSC_RESULT(regs
->eax
),
663 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
664 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
665 * not used, entry.S will call us only on syscall exit, not
666 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
667 * calling send_sigtrap() on syscall entry.
669 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
670 * is_singlestep is false, despite his name, so we will still do
673 else if (is_singlestep
)
677 if (!(current
->ptrace
& PT_PTRACED
))
680 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
681 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
682 * here. We have to check this and return */
683 if (is_sysemu
&& entryexit
)
686 /* Fake a debug trap */
688 send_sigtrap(current
, regs
, 0);
690 if (!test_thread_flag(TIF_SYSCALL_TRACE
) && !is_sysemu
)
693 /* the 0x80 provides a way for the tracing parent to distinguish
694 between a syscall stop and SIGTRAP delivery */
695 /* Note that the debugger could change the result of test_thread_flag!*/
696 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80:0));
699 * this isn't the same as continuing with a signal, but it will do
700 * for normal use. strace only continues with a signal if the
701 * stopping signal is not SIGTRAP. -brl
703 if (current
->exit_code
) {
704 send_sig(current
->exit_code
, current
, 1);
705 current
->exit_code
= 0;
709 if (unlikely(current
->audit_context
) && !entryexit
)
710 audit_syscall_entry(AUDIT_ARCH_I386
, regs
->orig_eax
,
711 regs
->ebx
, regs
->ecx
, regs
->edx
, regs
->esi
);
715 regs
->orig_eax
= -1; /* force skip of syscall restarting */
716 if (unlikely(current
->audit_context
))
717 audit_syscall_exit(AUDITSC_RESULT(regs
->eax
), regs
->eax
);