2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
7 #include <linux/sched.h>
8 #include <linux/uaccess.h>
9 #include <asm/ptrace-abi.h>
10 #include <registers.h>
13 extern int arch_switch_tls(struct task_struct *to);
15 void arch_switch_to(struct task_struct *to)
17 int err = arch_switch_tls(to);
22 printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
23 "not EINVAL\n", -err);
25 printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
28 int is_syscall(unsigned long addr)
33 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
35 /* access_process_vm() grants access to vsyscall and stub,
36 * while copy_from_user doesn't. Maybe access_process_vm is
37 * slow, but that doesn't matter, since it will be called only
38 * in case of singlestepping, if copy_from_user failed.
40 n = access_process_vm(current, addr, &instr, sizeof(instr),
42 if (n != sizeof(instr)) {
43 printk(KERN_ERR "is_syscall : failed to read "
44 "instruction from 0x%lx\n", addr);
48 /* int 0x80 or sysenter */
49 return (instr == 0x80cd) || (instr == 0x340f);
52 /* determines which flags the user has access to. */
53 /* 1 = access 0 = no access */
54 #define FLAG_MASK 0x00044dd5
56 static const int reg_offsets[] = {
73 [ORIG_EAX] = HOST_ORIG_AX,
76 int putreg(struct task_struct *child, int regno, unsigned long value)
91 /* Update the syscall number. */
92 UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
95 if (value && (value & 3) != 3)
99 if (value && (value & 3) != 3)
104 if (value && (value & 3) != 3)
110 if ((value & 3) != 3)
116 child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
119 panic("Bad register in putreg() : %d\n", regno);
121 child->thread.regs.regs.gp[reg_offsets[regno]] = value;
125 int poke_user(struct task_struct *child, long addr, long data)
127 if ((addr & 3) || addr < 0)
130 if (addr < MAX_REG_OFFSET)
131 return putreg(child, addr, data);
132 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
133 (addr <= offsetof(struct user, u_debugreg[7]))) {
134 addr -= offsetof(struct user, u_debugreg[0]);
136 if ((addr == 4) || (addr == 5))
138 child->thread.arch.debugregs[addr] = data;
144 unsigned long getreg(struct task_struct *child, int regno)
146 unsigned long mask = ~0UL;
171 panic("Bad register in getreg() : %d\n", regno);
173 return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
176 /* read the word at location addr in the USER area. */
177 int peek_user(struct task_struct *child, long addr, long data)
181 if ((addr & 3) || addr < 0)
184 tmp = 0; /* Default return condition */
185 if (addr < MAX_REG_OFFSET) {
186 tmp = getreg(child, addr);
188 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
189 (addr <= offsetof(struct user, u_debugreg[7]))) {
190 addr -= offsetof(struct user, u_debugreg[0]);
192 tmp = child->thread.arch.debugregs[addr];
194 return put_user(tmp, (unsigned long __user *) data);
197 static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
199 int err, n, cpu = task_cpu(child);
200 struct user_i387_struct fpregs;
202 err = save_i387_registers(userspace_pid[cpu],
203 (unsigned long *) &fpregs);
207 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
214 static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
216 int n, cpu = task_cpu(child);
217 struct user_i387_struct fpregs;
219 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
223 return restore_i387_registers(userspace_pid[cpu],
224 (unsigned long *) &fpregs);
227 static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
229 int err, n, cpu = task_cpu(child);
230 struct user_fxsr_struct fpregs;
232 err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
236 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
243 static int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
245 int n, cpu = task_cpu(child);
246 struct user_fxsr_struct fpregs;
248 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
252 return restore_fpx_registers(userspace_pid[cpu],
253 (unsigned long *) &fpregs);
256 long subarch_ptrace(struct task_struct *child, long request,
257 unsigned long addr, unsigned long data)
260 void __user *datap = (void __user *) data;
262 case PTRACE_GETFPREGS: /* Get the child FPU state. */
263 ret = get_fpregs(datap, child);
265 case PTRACE_SETFPREGS: /* Set the child FPU state. */
266 ret = set_fpregs(datap, child);
268 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
269 ret = get_fpxregs(datap, child);
271 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
272 ret = set_fpxregs(datap, child);