2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
7 #include <linux/sched.h>
8 #include <asm/uaccess.h>
9 #include <asm/ptrace-abi.h>
12 extern int arch_switch_tls(struct task_struct *to);
14 void arch_switch_to(struct task_struct *to)
16 int err = arch_switch_tls(to);
21 printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
22 "not EINVAL\n", -err);
24 printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
27 int is_syscall(unsigned long addr)
32 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
34 /* access_process_vm() grants access to vsyscall and stub,
35 * while copy_from_user doesn't. Maybe access_process_vm is
36 * slow, but that doesn't matter, since it will be called only
37 * in case of singlestepping, if copy_from_user failed.
39 n = access_process_vm(current, addr, &instr, sizeof(instr),
41 if (n != sizeof(instr)) {
42 printk(KERN_ERR "is_syscall : failed to read "
43 "instruction from 0x%lx\n", addr);
47 /* int 0x80 or sysenter */
48 return (instr == 0x80cd) || (instr == 0x340f);
51 /* determines which flags the user has access to. */
52 /* 1 = access 0 = no access */
53 #define FLAG_MASK 0x00044dd5
55 static const int reg_offsets[] = {
72 [ORIG_EAX] = HOST_ORIG_AX,
75 int putreg(struct task_struct *child, int regno, unsigned long value)
90 /* Update the syscall number. */
91 UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
94 if (value && (value & 3) != 3)
98 if (value && (value & 3) != 3)
103 if (value && (value & 3) != 3)
109 if ((value & 3) != 3)
115 child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
118 panic("Bad register in putreg() : %d\n", regno);
120 child->thread.regs.regs.gp[reg_offsets[regno]] = value;
124 int poke_user(struct task_struct *child, long addr, long data)
126 if ((addr & 3) || addr < 0)
129 if (addr < MAX_REG_OFFSET)
130 return putreg(child, addr, data);
131 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
132 (addr <= offsetof(struct user, u_debugreg[7]))) {
133 addr -= offsetof(struct user, u_debugreg[0]);
135 if ((addr == 4) || (addr == 5))
137 child->thread.arch.debugregs[addr] = data;
143 unsigned long getreg(struct task_struct *child, int regno)
145 unsigned long mask = ~0UL;
170 panic("Bad register in getreg() : %d\n", regno);
172 return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
175 /* read the word at location addr in the USER area. */
176 int peek_user(struct task_struct *child, long addr, long data)
180 if ((addr & 3) || addr < 0)
183 tmp = 0; /* Default return condition */
184 if (addr < MAX_REG_OFFSET) {
185 tmp = getreg(child, addr);
187 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
188 (addr <= offsetof(struct user, u_debugreg[7]))) {
189 addr -= offsetof(struct user, u_debugreg[0]);
191 tmp = child->thread.arch.debugregs[addr];
193 return put_user(tmp, (unsigned long __user *) data);
196 static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
198 int err, n, cpu = task_cpu(child);
199 struct user_i387_struct fpregs;
201 err = save_i387_registers(userspace_pid[cpu],
202 (unsigned long *) &fpregs);
206 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
213 static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
215 int n, cpu = task_cpu(child);
216 struct user_i387_struct fpregs;
218 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
222 return restore_i387_registers(userspace_pid[cpu],
223 (unsigned long *) &fpregs);
226 static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
228 int err, n, cpu = task_cpu(child);
229 struct user_fxsr_struct fpregs;
231 err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
235 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
242 static int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
244 int n, cpu = task_cpu(child);
245 struct user_fxsr_struct fpregs;
247 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
251 return restore_fpx_registers(userspace_pid[cpu],
252 (unsigned long *) &fpregs);
255 long subarch_ptrace(struct task_struct *child, long request,
256 unsigned long addr, unsigned long data)
259 void __user *datap = (void __user *) data;
261 case PTRACE_GETFPREGS: /* Get the child FPU state. */
262 ret = get_fpregs(datap, child);
264 case PTRACE_SETFPREGS: /* Set the child FPU state. */
265 ret = set_fpregs(datap, child);
267 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
268 ret = get_fpxregs(datap, child);
270 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
271 ret = set_fpxregs(datap, child);