1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
10 #include <linux/smp.h>
11 #include <linux/errno.h>
12 #include <linux/ptrace.h>
13 #include <linux/user.h>
14 #include <linux/security.h>
15 #include <linux/audit.h>
16 #include <linux/seccomp.h>
17 #include <linux/signal.h>
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/processor.h>
24 #include <asm/debugreg.h>
29 * does not yet catch signals sent when the child dies.
30 * in exit.c or in signal.c.
34 * Determines which flags the user has access to [1 = access, 0 = no access].
35 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
36 * Also masks reserved bits (31-22, 15, 5, 3, 1).
38 #define FLAG_MASK 0x00050dd5
40 /* set's the trap flag. */
41 #define TRAP_FLAG 0x100
44 * Offset of eflags on child stack..
46 #define EFL_OFFSET offsetof(struct pt_regs, eflags)
48 static inline struct pt_regs
*get_child_regs(struct task_struct
*task
)
50 void *stack_top
= (void *)task
->thread
.esp0
;
51 return stack_top
- sizeof(struct pt_regs
);
55 * This routine will get a word off of the processes privileged stack.
56 * the offset is bytes into the pt_regs structure on the stack.
57 * This routine assumes that all the privileged stacks are in our
60 static inline int get_stack_long(struct task_struct
*task
, int offset
)
64 stack
= (unsigned char *)task
->thread
.esp0
- sizeof(struct pt_regs
);
66 return (*((int *)stack
));
70 * This routine will put a word on the processes privileged stack.
71 * the offset is bytes into the pt_regs structure on the stack.
72 * This routine assumes that all the privileged stacks are in our
75 static inline int put_stack_long(struct task_struct
*task
, int offset
,
78 unsigned char * stack
;
80 stack
= (unsigned char *)task
->thread
.esp0
- sizeof(struct pt_regs
);
82 *(unsigned long *) stack
= data
;
86 static int putreg(struct task_struct
*child
,
87 unsigned long regno
, unsigned long value
)
91 if (value
&& (value
& 3) != 3)
93 child
->thread
.gs
= value
;
98 if (value
&& (value
& 3) != 3)
104 if ((value
& 3) != 3)
110 value
|= get_stack_long(child
, EFL_OFFSET
) & ~FLAG_MASK
;
115 put_stack_long(child
, regno
, value
);
119 static unsigned long getreg(struct task_struct
*child
,
122 unsigned long retval
= ~0UL;
124 switch (regno
>> 2) {
126 retval
= child
->thread
.gs
;
138 retval
&= get_stack_long(child
, regno
);
143 #define LDT_SEGMENT 4
145 static unsigned long convert_eip_to_linear(struct task_struct
*child
, struct pt_regs
*regs
)
147 unsigned long addr
, seg
;
150 seg
= regs
->xcs
& 0xffff;
151 if (regs
->eflags
& VM_MASK
) {
152 addr
= (addr
& 0xffff) + (seg
<< 4);
157 * We'll assume that the code segments in the GDT
158 * are all zero-based. That is largely true: the
159 * TLS segments are used for data, and the PNPBIOS
160 * and APM bios ones we just ignore here.
162 if (seg
& LDT_SEGMENT
) {
168 down(&child
->mm
->context
.sem
);
169 if (unlikely((seg
>> 3) >= child
->mm
->context
.size
))
170 addr
= -1L; /* bogus selector, access would fault */
172 desc
= child
->mm
->context
.ldt
+ seg
;
173 base
= ((desc
[0] >> 16) |
174 ((desc
[1] & 0xff) << 16) |
175 (desc
[1] & 0xff000000));
177 /* 16-bit code segment? */
178 if (!((desc
[1] >> 22) & 1))
182 up(&child
->mm
->context
.sem
);
187 static inline int is_setting_trap_flag(struct task_struct
*child
, struct pt_regs
*regs
)
190 unsigned char opcode
[15];
191 unsigned long addr
= convert_eip_to_linear(child
, regs
);
193 copied
= access_process_vm(child
, addr
, opcode
, sizeof(opcode
), 0);
194 for (i
= 0; i
< copied
; i
++) {
197 case 0x9d: case 0xcf:
199 /* opcode and address size prefixes */
200 case 0x66: case 0x67:
202 /* irrelevant prefixes (segment overrides and repeats) */
203 case 0x26: case 0x2e:
204 case 0x36: case 0x3e:
205 case 0x64: case 0x65:
206 case 0xf0: case 0xf2: case 0xf3:
210 * pushf: NOTE! We should probably not let
211 * the user see the TF bit being set. But
212 * it's more pain than it's worth to avoid
213 * it, and a debugger could emulate this
214 * all in user space if it _really_ cares.
224 static void set_singlestep(struct task_struct
*child
)
226 struct pt_regs
*regs
= get_child_regs(child
);
229 * Always set TIF_SINGLESTEP - this guarantees that
230 * we single-step system calls etc.. This will also
231 * cause us to set TF when returning to user mode.
233 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
236 * If TF was already set, don't do anything else
238 if (regs
->eflags
& TRAP_FLAG
)
241 /* Set TF on the kernel stack.. */
242 regs
->eflags
|= TRAP_FLAG
;
245 * ..but if TF is changed by the instruction we will trace,
246 * don't mark it as being "us" that set it, so that we
247 * won't clear it by hand later.
249 if (is_setting_trap_flag(child
, regs
))
252 child
->ptrace
|= PT_DTRACE
;
255 static void clear_singlestep(struct task_struct
*child
)
257 /* Always clear TIF_SINGLESTEP... */
258 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
260 /* But touch TF only if it was set by us.. */
261 if (child
->ptrace
& PT_DTRACE
) {
262 struct pt_regs
*regs
= get_child_regs(child
);
263 regs
->eflags
&= ~TRAP_FLAG
;
264 child
->ptrace
&= ~PT_DTRACE
;
269 * Called by kernel/ptrace.c when detaching..
271 * Make sure the single step bit is not set.
273 void ptrace_disable(struct task_struct
*child
)
275 clear_singlestep(child
);
276 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
280 * Perform get_thread_area on behalf of the traced child.
283 ptrace_get_thread_area(struct task_struct
*child
,
284 int idx
, struct user_desc __user
*user_desc
)
286 struct user_desc info
;
287 struct desc_struct
*desc
;
290 * Get the current Thread-Local Storage area:
293 #define GET_BASE(desc) ( \
294 (((desc)->a >> 16) & 0x0000ffff) | \
295 (((desc)->b << 16) & 0x00ff0000) | \
296 ( (desc)->b & 0xff000000) )
298 #define GET_LIMIT(desc) ( \
299 ((desc)->a & 0x0ffff) | \
300 ((desc)->b & 0xf0000) )
302 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
303 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
304 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
305 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
306 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
307 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
309 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
312 desc
= child
->thread
.tls_array
+ idx
- GDT_ENTRY_TLS_MIN
;
314 info
.entry_number
= idx
;
315 info
.base_addr
= GET_BASE(desc
);
316 info
.limit
= GET_LIMIT(desc
);
317 info
.seg_32bit
= GET_32BIT(desc
);
318 info
.contents
= GET_CONTENTS(desc
);
319 info
.read_exec_only
= !GET_WRITABLE(desc
);
320 info
.limit_in_pages
= GET_LIMIT_PAGES(desc
);
321 info
.seg_not_present
= !GET_PRESENT(desc
);
322 info
.useable
= GET_USEABLE(desc
);
324 if (copy_to_user(user_desc
, &info
, sizeof(info
)))
331 * Perform set_thread_area on behalf of the traced child.
334 ptrace_set_thread_area(struct task_struct
*child
,
335 int idx
, struct user_desc __user
*user_desc
)
337 struct user_desc info
;
338 struct desc_struct
*desc
;
340 if (copy_from_user(&info
, user_desc
, sizeof(info
)))
343 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
346 desc
= child
->thread
.tls_array
+ idx
- GDT_ENTRY_TLS_MIN
;
347 if (LDT_empty(&info
)) {
351 desc
->a
= LDT_entry_a(&info
);
352 desc
->b
= LDT_entry_b(&info
);
358 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
360 struct user
* dummy
= NULL
;
362 unsigned long __user
*datap
= (unsigned long __user
*)data
;
365 /* when I and D space are separate, these will need to be fixed. */
366 case PTRACE_PEEKTEXT
: /* read word at location addr. */
367 case PTRACE_PEEKDATA
:
368 ret
= generic_ptrace_peekdata(child
, addr
, data
);
371 /* read the word at location addr in the USER area. */
372 case PTRACE_PEEKUSR
: {
376 if ((addr
& 3) || addr
< 0 ||
377 addr
> sizeof(struct user
) - 3)
380 tmp
= 0; /* Default return condition */
381 if(addr
< FRAME_SIZE
*sizeof(long))
382 tmp
= getreg(child
, addr
);
383 if(addr
>= (long) &dummy
->u_debugreg
[0] &&
384 addr
<= (long) &dummy
->u_debugreg
[7]){
385 addr
-= (long) &dummy
->u_debugreg
[0];
387 tmp
= child
->thread
.debugreg
[addr
];
389 ret
= put_user(tmp
, datap
);
393 /* when I and D space are separate, this will have to be fixed. */
394 case PTRACE_POKETEXT
: /* write the word at location addr. */
395 case PTRACE_POKEDATA
:
396 ret
= generic_ptrace_pokedata(child
, addr
, data
);
399 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
401 if ((addr
& 3) || addr
< 0 ||
402 addr
> sizeof(struct user
) - 3)
405 if (addr
< FRAME_SIZE
*sizeof(long)) {
406 ret
= putreg(child
, addr
, data
);
409 /* We need to be very careful here. We implicitly
410 want to modify a portion of the task_struct, and we
411 have to be selective about what portions we allow someone
415 if(addr
>= (long) &dummy
->u_debugreg
[0] &&
416 addr
<= (long) &dummy
->u_debugreg
[7]){
418 if(addr
== (long) &dummy
->u_debugreg
[4]) break;
419 if(addr
== (long) &dummy
->u_debugreg
[5]) break;
420 if(addr
< (long) &dummy
->u_debugreg
[4] &&
421 ((unsigned long) data
) >= TASK_SIZE
-3) break;
423 /* Sanity-check data. Take one half-byte at once with
424 * check = (val >> (16 + 4*i)) & 0xf. It contains the
425 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
426 * 2 and 3 are LENi. Given a list of invalid values,
427 * we do mask |= 1 << invalid_value, so that
428 * (mask >> check) & 1 is a correct test for invalid
431 * R/Wi contains the type of the breakpoint /
432 * watchpoint, LENi contains the length of the watched
433 * data in the watchpoint case.
435 * The invalid values are:
436 * - LENi == 0x10 (undefined), so mask |= 0x0f00.
437 * - R/Wi == 0x10 (break on I/O reads or writes), so
439 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
442 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
444 * See the Intel Manual "System Programming Guide",
447 * Note that LENi == 0x10 is defined on x86_64 in long
448 * mode (i.e. even for 32-bit userspace software, but
449 * 64-bit kernel), so the x86_64 mask value is 0x5454.
450 * See the AMD manual no. 24593 (AMD64 System
453 if(addr
== (long) &dummy
->u_debugreg
[7]) {
454 data
&= ~DR_CONTROL_RESERVED
;
456 if ((0x5f54 >> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
459 set_tsk_thread_flag(child
, TIF_DEBUG
);
461 clear_tsk_thread_flag(child
, TIF_DEBUG
);
463 addr
-= (long) &dummy
->u_debugreg
;
465 child
->thread
.debugreg
[addr
] = data
;
470 case PTRACE_SYSEMU
: /* continue and stop at next syscall, which will not be executed */
471 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
472 case PTRACE_CONT
: /* restart after signal. */
474 if (!valid_signal(data
))
476 if (request
== PTRACE_SYSEMU
) {
477 set_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
478 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
479 } else if (request
== PTRACE_SYSCALL
) {
480 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
481 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
483 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
484 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
486 child
->exit_code
= data
;
487 /* make sure the single step bit is not set. */
488 clear_singlestep(child
);
489 wake_up_process(child
);
494 * make the child exit. Best I can do is send it a sigkill.
495 * perhaps it should be put in the status that it wants to
500 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
502 child
->exit_code
= SIGKILL
;
503 /* make sure the single step bit is not set. */
504 clear_singlestep(child
);
505 wake_up_process(child
);
508 case PTRACE_SYSEMU_SINGLESTEP
: /* Same as SYSEMU, but singlestep if not syscall */
509 case PTRACE_SINGLESTEP
: /* set the trap flag. */
511 if (!valid_signal(data
))
514 if (request
== PTRACE_SYSEMU_SINGLESTEP
)
515 set_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
517 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
519 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
520 set_singlestep(child
);
521 child
->exit_code
= data
;
522 /* give it a chance to run. */
523 wake_up_process(child
);
528 /* detach a process that was attached. */
529 ret
= ptrace_detach(child
, data
);
532 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
533 if (!access_ok(VERIFY_WRITE
, datap
, FRAME_SIZE
*sizeof(long))) {
537 for ( i
= 0; i
< FRAME_SIZE
*sizeof(long); i
+= sizeof(long) ) {
538 __put_user(getreg(child
, i
), datap
);
545 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
547 if (!access_ok(VERIFY_READ
, datap
, FRAME_SIZE
*sizeof(long))) {
551 for ( i
= 0; i
< FRAME_SIZE
*sizeof(long); i
+= sizeof(long) ) {
552 __get_user(tmp
, datap
);
553 putreg(child
, i
, tmp
);
560 case PTRACE_GETFPREGS
: { /* Get the child FPU state. */
561 if (!access_ok(VERIFY_WRITE
, datap
,
562 sizeof(struct user_i387_struct
))) {
567 if (!tsk_used_math(child
))
569 get_fpregs((struct user_i387_struct __user
*)data
, child
);
573 case PTRACE_SETFPREGS
: { /* Set the child FPU state. */
574 if (!access_ok(VERIFY_READ
, datap
,
575 sizeof(struct user_i387_struct
))) {
579 set_stopped_child_used_math(child
);
580 set_fpregs(child
, (struct user_i387_struct __user
*)data
);
585 case PTRACE_GETFPXREGS
: { /* Get the child extended FPU state. */
586 if (!access_ok(VERIFY_WRITE
, datap
,
587 sizeof(struct user_fxsr_struct
))) {
591 if (!tsk_used_math(child
))
593 ret
= get_fpxregs((struct user_fxsr_struct __user
*)data
, child
);
597 case PTRACE_SETFPXREGS
: { /* Set the child extended FPU state. */
598 if (!access_ok(VERIFY_READ
, datap
,
599 sizeof(struct user_fxsr_struct
))) {
603 set_stopped_child_used_math(child
);
604 ret
= set_fpxregs(child
, (struct user_fxsr_struct __user
*)data
);
608 case PTRACE_GET_THREAD_AREA
:
609 ret
= ptrace_get_thread_area(child
, addr
,
610 (struct user_desc __user
*) data
);
613 case PTRACE_SET_THREAD_AREA
:
614 ret
= ptrace_set_thread_area(child
, addr
,
615 (struct user_desc __user
*) data
);
619 ret
= ptrace_request(child
, request
, addr
, data
);
626 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
, int error_code
)
630 tsk
->thread
.trap_no
= 1;
631 tsk
->thread
.error_code
= error_code
;
633 memset(&info
, 0, sizeof(info
));
634 info
.si_signo
= SIGTRAP
;
635 info
.si_code
= TRAP_BRKPT
;
638 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->eip
: NULL
;
640 /* Send us the fakey SIGTRAP */
641 force_sig_info(SIGTRAP
, &info
, tsk
);
644 /* notification of system call entry/exit
645 * - triggered by current->work.syscall_trace
647 __attribute__((regparm(3)))
648 int do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
650 int is_sysemu
= test_thread_flag(TIF_SYSCALL_EMU
);
652 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
655 int is_singlestep
= !is_sysemu
&& test_thread_flag(TIF_SINGLESTEP
);
658 /* do the secure computing check first */
660 secure_computing(regs
->orig_eax
);
662 if (unlikely(current
->audit_context
)) {
664 audit_syscall_exit(AUDITSC_RESULT(regs
->eax
),
666 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
667 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
668 * not used, entry.S will call us only on syscall exit, not
669 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
670 * calling send_sigtrap() on syscall entry.
672 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
673 * is_singlestep is false, despite his name, so we will still do
676 else if (is_singlestep
)
680 if (!(current
->ptrace
& PT_PTRACED
))
683 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
684 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
685 * here. We have to check this and return */
686 if (is_sysemu
&& entryexit
)
689 /* Fake a debug trap */
691 send_sigtrap(current
, regs
, 0);
693 if (!test_thread_flag(TIF_SYSCALL_TRACE
) && !is_sysemu
)
696 /* the 0x80 provides a way for the tracing parent to distinguish
697 between a syscall stop and SIGTRAP delivery */
698 /* Note that the debugger could change the result of test_thread_flag!*/
699 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80:0));
702 * this isn't the same as continuing with a signal, but it will do
703 * for normal use. strace only continues with a signal if the
704 * stopping signal is not SIGTRAP. -brl
706 if (current
->exit_code
) {
707 send_sig(current
->exit_code
, current
, 1);
708 current
->exit_code
= 0;
712 if (unlikely(current
->audit_context
) && !entryexit
)
713 audit_syscall_entry(AUDIT_ARCH_I386
, regs
->orig_eax
,
714 regs
->ebx
, regs
->ecx
, regs
->edx
, regs
->esi
);
718 regs
->orig_eax
= -1; /* force skip of syscall restarting */
719 if (unlikely(current
->audit_context
))
720 audit_syscall_exit(AUDITSC_RESULT(regs
->eax
), regs
->eax
);