2 * Copyright 2003 PathScale, Inc.
3 * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
5 * Licensed under the GPL
9 #include <linux/sched.h>
10 #include <linux/errno.h>
11 #define __FRAME_OFFSETS
12 #include <asm/ptrace.h>
13 #include <linux/uaccess.h>
14 #include <registers.h>
15 #include <asm/ptrace-abi.h>
18 * determines which flags the user has access to.
19 * 1 = access 0 = no access
21 #define FLAG_MASK 0x44dd5UL
23 static const int reg_offsets[] =
27 [R10 >> 3] = HOST_R10,
28 [R11 >> 3] = HOST_R11,
29 [R12 >> 3] = HOST_R12,
30 [R13 >> 3] = HOST_R13,
31 [R14 >> 3] = HOST_R14,
32 [R15 >> 3] = HOST_R15,
44 [FS_BASE >> 3] = HOST_FS_BASE,
45 [GS_BASE >> 3] = HOST_GS_BASE,
50 [EFLAGS >> 3] = HOST_EFLAGS,
51 [ORIG_RAX >> 3] = HOST_ORIG_AX,
54 int putreg(struct task_struct *child, int regno, unsigned long value)
77 /* Update the syscall number. */
78 UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
87 if (value && (value & 3) != 3)
94 if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
100 child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
104 panic("Bad register in putreg(): %d\n", regno);
107 child->thread.regs.regs.gp[reg_offsets[regno >> 3]] = value;
111 int poke_user(struct task_struct *child, long addr, long data)
113 if ((addr & 3) || addr < 0)
116 if (addr < MAX_REG_OFFSET)
117 return putreg(child, addr, data);
118 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
119 (addr <= offsetof(struct user, u_debugreg[7]))) {
120 addr -= offsetof(struct user, u_debugreg[0]);
122 if ((addr == 4) || (addr == 5))
124 child->thread.arch.debugregs[addr] = data;
130 unsigned long getreg(struct task_struct *child, int regno)
132 unsigned long mask = ~0UL;
166 panic("Bad register in getreg: %d\n", regno);
168 return mask & child->thread.regs.regs.gp[reg_offsets[regno >> 3]];
171 int peek_user(struct task_struct *child, long addr, long data)
173 /* read the word at location addr in the USER area. */
176 if ((addr & 3) || addr < 0)
179 tmp = 0; /* Default return condition */
180 if (addr < MAX_REG_OFFSET)
181 tmp = getreg(child, addr);
182 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
183 (addr <= offsetof(struct user, u_debugreg[7]))) {
184 addr -= offsetof(struct user, u_debugreg[0]);
186 tmp = child->thread.arch.debugregs[addr];
188 return put_user(tmp, (unsigned long *) data);
191 /* XXX Mostly copied from sys-i386 */
192 int is_syscall(unsigned long addr)
194 unsigned short instr;
197 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
200 * access_process_vm() grants access to vsyscall and stub,
201 * while copy_from_user doesn't. Maybe access_process_vm is
202 * slow, but that doesn't matter, since it will be called only
203 * in case of singlestepping, if copy_from_user failed.
205 n = access_process_vm(current, addr, &instr, sizeof(instr),
207 if (n != sizeof(instr)) {
208 printk("is_syscall : failed to read instruction from "
214 return instr == 0x050f;
217 static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
219 int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
220 struct user_i387_struct fpregs;
222 err = save_i387_registers(userspace_pid[cpu],
223 (unsigned long *) &fpregs);
227 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
234 static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
236 int n, cpu = ((struct thread_info *) child->stack)->cpu;
237 struct user_i387_struct fpregs;
239 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
243 return restore_i387_registers(userspace_pid[cpu],
244 (unsigned long *) &fpregs);
247 long subarch_ptrace(struct task_struct *child, long request,
248 unsigned long addr, unsigned long data)
251 void __user *datap = (void __user *) data;
254 case PTRACE_GETFPREGS: /* Get the child FPU state. */
255 ret = get_fpregs(datap, child);
257 case PTRACE_SETFPREGS: /* Set the child FPU state. */
258 ret = set_fpregs(datap, child);
260 case PTRACE_ARCH_PRCTL:
261 /* XXX Calls ptrace on the host - needs some SMP thinking */
262 ret = arch_prctl(child, data, (void __user *) addr);