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/smp_lock.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
17 #include <asm/uaccess.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
20 #include <asm/processor.h>
22 #include <asm/debugreg.h>
25 * does not yet catch signals sent when the child dies.
26 * in exit.c or in signal.c.
29 /* determines which flags the user has access to. */
30 /* 1 = access 0 = no access */
31 #define FLAG_MASK 0x00044dd5
33 /* set's the trap flag. */
34 #define TRAP_FLAG 0x100
37 * Offset of eflags on child stack..
39 #define EFL_OFFSET ((EFL-2)*4-sizeof(struct pt_regs))
42 * this routine will get a word off of the processes privileged stack.
43 * the offset is how far from the base addr as stored in the TSS.
44 * this routine assumes that all the privileged stacks are in our
47 static inline int get_stack_long(struct task_struct *task, int offset)
51 stack = (unsigned char *)task->thread.esp0;
53 return (*((int *)stack));
57 * this routine will put a word on the processes privileged stack.
58 * the offset is how far from the base addr as stored in the TSS.
59 * this routine assumes that all the privileged stacks are in our
62 static inline int put_stack_long(struct task_struct *task, int offset,
65 unsigned char * stack;
67 stack = (unsigned char *) task->thread.esp0;
69 *(unsigned long *) stack = data;
73 static int putreg(struct task_struct *child,
74 unsigned long regno, unsigned long value)
78 if (value && (value & 3) != 3)
80 child->thread.fs = value;
83 if (value && (value & 3) != 3)
85 child->thread.gs = value;
89 if (value && (value & 3) != 3)
101 value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
106 put_stack_long(child, regno - sizeof(struct pt_regs), value);
110 static unsigned long getreg(struct task_struct *child,
113 unsigned long retval = ~0UL;
115 switch (regno >> 2) {
117 retval = child->thread.fs;
120 retval = child->thread.gs;
131 regno = regno - sizeof(struct pt_regs);
132 retval &= get_stack_long(child, regno);
138 * Called by kernel/ptrace.c when detaching..
140 * Make sure the single step bit is not set.
142 void ptrace_disable(struct task_struct *child)
146 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
147 put_stack_long(child, EFL_OFFSET, tmp);
150 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
152 struct task_struct *child;
153 struct user * dummy = NULL;
158 if (request == PTRACE_TRACEME) {
159 /* are we already being traced? */
160 if (current->ptrace & PT_PTRACED)
162 /* set the ptrace bit in the process flags. */
163 current->ptrace |= PT_PTRACED;
168 read_lock(&tasklist_lock);
169 child = find_task_by_pid(pid);
171 get_task_struct(child);
172 read_unlock(&tasklist_lock);
177 if (pid == 1) /* you may not mess with init */
180 if (request == PTRACE_ATTACH) {
181 ret = ptrace_attach(child);
185 ret = ptrace_check_attach(child, request == PTRACE_KILL);
190 /* when I and D space are separate, these will need to be fixed. */
191 case PTRACE_PEEKTEXT: /* read word at location addr. */
192 case PTRACE_PEEKDATA: {
196 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
198 if (copied != sizeof(tmp))
200 ret = put_user(tmp,(unsigned long *) data);
204 /* read the word at location addr in the USER area. */
205 case PTRACE_PEEKUSR: {
209 if ((addr & 3) || addr < 0 ||
210 addr > sizeof(struct user) - 3)
213 tmp = 0; /* Default return condition */
214 if(addr < FRAME_SIZE*sizeof(long))
215 tmp = getreg(child, addr);
216 if(addr >= (long) &dummy->u_debugreg[0] &&
217 addr <= (long) &dummy->u_debugreg[7]){
218 addr -= (long) &dummy->u_debugreg[0];
220 tmp = child->thread.debugreg[addr];
222 ret = put_user(tmp,(unsigned long *) data);
226 /* when I and D space are separate, this will have to be fixed. */
227 case PTRACE_POKETEXT: /* write the word at location addr. */
228 case PTRACE_POKEDATA:
230 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
235 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
237 if ((addr & 3) || addr < 0 ||
238 addr > sizeof(struct user) - 3)
241 if (addr < FRAME_SIZE*sizeof(long)) {
242 ret = putreg(child, addr, data);
245 /* We need to be very careful here. We implicitly
246 want to modify a portion of the task_struct, and we
247 have to be selective about what portions we allow someone
251 if(addr >= (long) &dummy->u_debugreg[0] &&
252 addr <= (long) &dummy->u_debugreg[7]){
254 if(addr == (long) &dummy->u_debugreg[4]) break;
255 if(addr == (long) &dummy->u_debugreg[5]) break;
256 if(addr < (long) &dummy->u_debugreg[4] &&
257 ((unsigned long) data) >= TASK_SIZE-3) break;
259 if(addr == (long) &dummy->u_debugreg[7]) {
260 data &= ~DR_CONTROL_RESERVED;
262 if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
266 addr -= (long) &dummy->u_debugreg;
268 child->thread.debugreg[addr] = data;
273 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
274 case PTRACE_CONT: { /* restart after signal. */
278 if ((unsigned long) data > _NSIG)
280 if (request == PTRACE_SYSCALL)
281 child->ptrace |= PT_TRACESYS;
283 child->ptrace &= ~PT_TRACESYS;
284 child->exit_code = data;
285 /* make sure the single step bit is not set. */
286 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
287 put_stack_long(child, EFL_OFFSET,tmp);
288 wake_up_process(child);
294 * make the child exit. Best I can do is send it a sigkill.
295 * perhaps it should be put in the status that it wants to
302 if (child->state == TASK_ZOMBIE) /* already dead */
304 child->exit_code = SIGKILL;
305 /* make sure the single step bit is not set. */
306 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
307 put_stack_long(child, EFL_OFFSET, tmp);
308 wake_up_process(child);
312 case PTRACE_SINGLESTEP: { /* set the trap flag. */
316 if ((unsigned long) data > _NSIG)
318 child->ptrace &= ~PT_TRACESYS;
319 if ((child->ptrace & PT_DTRACE) == 0) {
320 /* Spurious delayed TF traps may occur */
321 child->ptrace |= PT_DTRACE;
323 tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
324 put_stack_long(child, EFL_OFFSET, tmp);
325 child->exit_code = data;
326 /* give it a chance to run. */
327 wake_up_process(child);
333 /* detach a process that was attached. */
334 ret = ptrace_detach(child, data);
337 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
338 if (!access_ok(VERIFY_WRITE, (unsigned *)data, FRAME_SIZE*sizeof(long))) {
342 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
343 __put_user(getreg(child, i),(unsigned long *) data);
344 data += sizeof(long);
350 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
352 if (!access_ok(VERIFY_READ, (unsigned *)data, FRAME_SIZE*sizeof(long))) {
356 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
357 __get_user(tmp, (unsigned long *) data);
358 putreg(child, i, tmp);
359 data += sizeof(long);
365 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
366 if (!access_ok(VERIFY_WRITE, (unsigned *)data,
367 sizeof(struct user_i387_struct))) {
372 if ( !child->used_math )
373 load_empty_fpu(child);
374 get_fpregs((struct user_i387_struct *)data, child);
378 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
379 if (!access_ok(VERIFY_READ, (unsigned *)data,
380 sizeof(struct user_i387_struct))) {
384 child->used_math = 1;
385 set_fpregs(child, (struct user_i387_struct *)data);
390 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
391 if (!access_ok(VERIFY_WRITE, (unsigned *)data,
392 sizeof(struct user_fxsr_struct))) {
396 if ( !child->used_math )
397 load_empty_fpu(child);
398 ret = get_fpxregs((struct user_fxsr_struct *)data, child);
402 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
403 if (!access_ok(VERIFY_READ, (unsigned *)data,
404 sizeof(struct user_fxsr_struct))) {
408 child->used_math = 1;
409 ret = set_fpxregs(child, (struct user_fxsr_struct *)data);
413 case PTRACE_SETOPTIONS: {
414 if (data & PTRACE_O_TRACESYSGOOD)
415 child->ptrace |= PT_TRACESYSGOOD;
417 child->ptrace &= ~PT_TRACESYSGOOD;
427 free_task_struct(child);
433 asmlinkage void syscall_trace(void)
435 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS)) !=
436 (PT_PTRACED|PT_TRACESYS))
438 /* the 0x80 provides a way for the tracing parent to distinguish
439 between a syscall stop and SIGTRAP delivery */
440 current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
442 current->state = TASK_STOPPED;
443 notify_parent(current, SIGCHLD);
446 * this isn't the same as continuing with a signal, but it will do
447 * for normal use. strace only continues with a signal if the
448 * stopping signal is not SIGTRAP. -brl
450 if (current->exit_code) {
451 send_sig(current->exit_code, current, 1);
452 current->exit_code = 0;