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 <asm/uaccess.h>
16 * determines which flags the user has access to.
17 * 1 = access 0 = no access
19 #define FLAG_MASK 0x44dd5UL
21 static const int reg_offsets[] =
25 [R10 >> 3] = HOST_R10,
26 [R11 >> 3] = HOST_R11,
27 [R12 >> 3] = HOST_R12,
28 [R13 >> 3] = HOST_R13,
29 [R14 >> 3] = HOST_R14,
30 [R15 >> 3] = HOST_R15,
42 [FS_BASE >> 3] = HOST_FS_BASE,
43 [GS_BASE >> 3] = HOST_GS_BASE,
48 [EFLAGS >> 3] = HOST_EFLAGS,
49 [ORIG_RAX >> 3] = HOST_ORIG_AX,
52 int putreg(struct task_struct *child, int regno, unsigned long value)
56 * Some code in the 64bit emulation may not be 64bit clean.
57 * Don't take any chances.
59 if (test_tsk_thread_flag(child, TIF_IA32))
89 if (value && (value & 3) != 3)
96 if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
102 child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
106 panic("Bad register in putreg(): %d\n", regno);
109 child->thread.regs.regs.gp[reg_offsets[regno >> 3]] = value;
113 int poke_user(struct task_struct *child, long addr, long data)
115 if ((addr & 3) || addr < 0)
118 if (addr < MAX_REG_OFFSET)
119 return putreg(child, addr, data);
120 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
121 (addr <= offsetof(struct user, u_debugreg[7]))) {
122 addr -= offsetof(struct user, u_debugreg[0]);
124 if ((addr == 4) || (addr == 5))
126 child->thread.arch.debugregs[addr] = data;
132 unsigned long getreg(struct task_struct *child, int regno)
134 unsigned long mask = ~0UL;
136 if (test_tsk_thread_flag(child, TIF_IA32))
171 panic("Bad register in getreg: %d\n", regno);
173 return mask & child->thread.regs.regs.gp[reg_offsets[regno >> 3]];
176 int peek_user(struct task_struct *child, long addr, long data)
178 /* read the word at location addr in the USER area. */
181 if ((addr & 3) || addr < 0)
184 tmp = 0; /* Default return condition */
185 if (addr < MAX_REG_OFFSET)
186 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 *) data);
196 /* XXX Mostly copied from sys-i386 */
197 int is_syscall(unsigned long addr)
199 unsigned short instr;
202 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
205 * access_process_vm() grants access to vsyscall and stub,
206 * while copy_from_user doesn't. Maybe access_process_vm is
207 * slow, but that doesn't matter, since it will be called only
208 * in case of singlestepping, if copy_from_user failed.
210 n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
211 if (n != sizeof(instr)) {
212 printk("is_syscall : failed to read instruction from "
218 return instr == 0x050f;
221 static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
223 int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
224 long fpregs[HOST_FP_SIZE];
226 BUG_ON(sizeof(*buf) != sizeof(fpregs));
227 err = save_fp_registers(userspace_pid[cpu], fpregs);
231 n = copy_to_user(buf, fpregs, sizeof(fpregs));
238 static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
240 int n, cpu = ((struct thread_info *) child->stack)->cpu;
241 long fpregs[HOST_FP_SIZE];
243 BUG_ON(sizeof(*buf) != sizeof(fpregs));
244 n = copy_from_user(fpregs, buf, sizeof(fpregs));
248 return restore_fp_registers(userspace_pid[cpu], fpregs);
251 long subarch_ptrace(struct task_struct *child, long request,
252 unsigned long addr, unsigned long data)
255 void __user *datap = (void __user *) data;
258 case PTRACE_GETFPREGS: /* Get the child FPU state. */
259 ret = get_fpregs(datap, child);
261 case PTRACE_SETFPREGS: /* Set the child FPU state. */
262 ret = set_fpregs(datap, child);
264 case PTRACE_ARCH_PRCTL:
265 /* XXX Calls ptrace on the host - needs some SMP thinking */
266 ret = arch_prctl(child, data, (void __user *) addr);