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>
11 extern int arch_switch_tls(struct task_struct
*to
);
13 void arch_switch_to(struct task_struct
*to
)
15 int err
= arch_switch_tls(to
);
20 printk(KERN_WARNING
"arch_switch_tls failed, errno %d, "
21 "not EINVAL\n", -err
);
23 printk(KERN_WARNING
"arch_switch_tls failed, errno = EINVAL\n");
26 int is_syscall(unsigned long addr
)
31 n
= copy_from_user(&instr
, (void __user
*) addr
, sizeof(instr
));
33 /* access_process_vm() grants access to vsyscall and stub,
34 * while copy_from_user doesn't. Maybe access_process_vm is
35 * slow, but that doesn't matter, since it will be called only
36 * in case of singlestepping, if copy_from_user failed.
38 n
= access_process_vm(current
, addr
, &instr
, sizeof(instr
), 0);
39 if (n
!= sizeof(instr
)) {
40 printk(KERN_ERR
"is_syscall : failed to read "
41 "instruction from 0x%lx\n", addr
);
45 /* int 0x80 or sysenter */
46 return (instr
== 0x80cd) || (instr
== 0x340f);
49 /* determines which flags the user has access to. */
50 /* 1 = access 0 = no access */
51 #define FLAG_MASK 0x00044dd5
53 static const int reg_offsets
[] = {
72 int putreg(struct task_struct
*child
, int regno
, unsigned long value
)
87 if (value
&& (value
& 3) != 3)
91 if (value
&& (value
& 3) != 3)
96 if (value
&& (value
& 3) != 3)
102 if ((value
& 3) != 3)
108 child
->thread
.regs
.regs
.gp
[HOST_EFLAGS
] |= value
;
111 child
->thread
.regs
.regs
.syscall
= value
;
114 panic("Bad register in putreg() : %d\n", regno
);
116 child
->thread
.regs
.regs
.gp
[reg_offsets
[regno
]] = value
;
120 int poke_user(struct task_struct
*child
, long addr
, long data
)
122 if ((addr
& 3) || addr
< 0)
125 if (addr
< MAX_REG_OFFSET
)
126 return putreg(child
, addr
, data
);
127 else if ((addr
>= offsetof(struct user
, u_debugreg
[0])) &&
128 (addr
<= offsetof(struct user
, u_debugreg
[7]))) {
129 addr
-= offsetof(struct user
, u_debugreg
[0]);
131 if ((addr
== 4) || (addr
== 5))
133 child
->thread
.arch
.debugregs
[addr
] = data
;
139 unsigned long getreg(struct task_struct
*child
, int regno
)
141 unsigned long mask
= ~0UL;
146 return child
->thread
.regs
.regs
.syscall
;
167 panic("Bad register in getreg() : %d\n", regno
);
169 return mask
& child
->thread
.regs
.regs
.gp
[reg_offsets
[regno
]];
172 /* read the word at location addr in the USER area. */
173 int peek_user(struct task_struct
*child
, long addr
, long data
)
177 if ((addr
& 3) || addr
< 0)
180 tmp
= 0; /* Default return condition */
181 if (addr
< MAX_REG_OFFSET
) {
182 tmp
= getreg(child
, addr
);
184 else if ((addr
>= offsetof(struct user
, u_debugreg
[0])) &&
185 (addr
<= offsetof(struct user
, u_debugreg
[7]))) {
186 addr
-= offsetof(struct user
, u_debugreg
[0]);
188 tmp
= child
->thread
.arch
.debugregs
[addr
];
190 return put_user(tmp
, (unsigned long __user
*) data
);
193 static int get_fpregs(struct user_i387_struct __user
*buf
, struct task_struct
*child
)
195 int err
, n
, cpu
= ((struct thread_info
*) child
->stack
)->cpu
;
196 struct user_i387_struct fpregs
;
198 err
= save_fp_registers(userspace_pid
[cpu
], (unsigned long *) &fpregs
);
202 n
= copy_to_user(buf
, &fpregs
, sizeof(fpregs
));
209 static int set_fpregs(struct user_i387_struct __user
*buf
, struct task_struct
*child
)
211 int n
, cpu
= ((struct thread_info
*) child
->stack
)->cpu
;
212 struct user_i387_struct fpregs
;
214 n
= copy_from_user(&fpregs
, buf
, sizeof(fpregs
));
218 return restore_fp_registers(userspace_pid
[cpu
],
219 (unsigned long *) &fpregs
);
222 static int get_fpxregs(struct user_fxsr_struct __user
*buf
, struct task_struct
*child
)
224 int err
, n
, cpu
= ((struct thread_info
*) child
->stack
)->cpu
;
225 struct user_fxsr_struct fpregs
;
227 err
= save_fpx_registers(userspace_pid
[cpu
], (unsigned long *) &fpregs
);
231 n
= copy_to_user(buf
, &fpregs
, sizeof(fpregs
));
238 static int set_fpxregs(struct user_fxsr_struct __user
*buf
, struct task_struct
*child
)
240 int n
, cpu
= ((struct thread_info
*) child
->stack
)->cpu
;
241 struct user_fxsr_struct fpregs
;
243 n
= copy_from_user(&fpregs
, buf
, sizeof(fpregs
));
247 return restore_fpx_registers(userspace_pid
[cpu
],
248 (unsigned long *) &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
;
257 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
258 ret
= get_fpregs(datap
, child
);
260 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
261 ret
= set_fpregs(datap
, child
);
263 case PTRACE_GETFPXREGS
: /* Get the child FPU state. */
264 ret
= get_fpxregs(datap
, child
);
266 case PTRACE_SETFPXREGS
: /* Set the child FPU state. */
267 ret
= set_fpxregs(datap
, child
);