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"
10 extern int arch_switch_tls(struct task_struct *from, struct task_struct *to);
12 void arch_switch_to(struct task_struct *from, struct task_struct *to)
14 int err = arch_switch_tls(from, to);
19 printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
20 "not EINVAL\n", -err);
22 printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
25 int is_syscall(unsigned long addr)
30 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
32 /* access_process_vm() grants access to vsyscall and stub,
33 * while copy_from_user doesn't. Maybe access_process_vm is
34 * slow, but that doesn't matter, since it will be called only
35 * in case of singlestepping, if copy_from_user failed.
37 n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
38 if (n != sizeof(instr)) {
39 printk(KERN_ERR "is_syscall : failed to read "
40 "instruction from 0x%lx\n", addr);
44 /* int 0x80 or sysenter */
45 return (instr == 0x80cd) || (instr == 0x340f);
48 /* determines which flags the user has access to. */
49 /* 1 = access 0 = no access */
50 #define FLAG_MASK 0x00044dd5
52 int putreg(struct task_struct *child, int regno, unsigned long value)
57 if (value && (value & 3) != 3)
59 PT_REGS_FS(&child->thread.regs) = value;
62 if (value && (value & 3) != 3)
64 PT_REGS_GS(&child->thread.regs) = value;
68 if (value && (value & 3) != 3)
80 value |= PT_REGS_EFLAGS(&child->thread.regs);
83 PT_REGS_SET(&child->thread.regs, regno, value);
87 int poke_user(struct task_struct *child, long addr, long data)
89 if ((addr & 3) || addr < 0)
92 if (addr < MAX_REG_OFFSET)
93 return putreg(child, addr, data);
94 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
95 (addr <= offsetof(struct user, u_debugreg[7]))) {
96 addr -= offsetof(struct user, u_debugreg[0]);
98 if ((addr == 4) || (addr == 5))
100 child->thread.arch.debugregs[addr] = data;
106 unsigned long getreg(struct task_struct *child, int regno)
108 unsigned long retval = ~0UL;
121 retval &= PT_REG(&child->thread.regs, regno);
126 /* read the word at location addr in the USER area. */
127 int peek_user(struct task_struct *child, long addr, long data)
131 if ((addr & 3) || addr < 0)
134 tmp = 0; /* Default return condition */
135 if (addr < MAX_REG_OFFSET) {
136 tmp = getreg(child, addr);
138 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
139 (addr <= offsetof(struct user, u_debugreg[7]))) {
140 addr -= offsetof(struct user, u_debugreg[0]);
142 tmp = child->thread.arch.debugregs[addr];
144 return put_user(tmp, (unsigned long __user *) data);
147 struct i387_fxsave_struct {
158 long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
159 long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
164 * FPU tag word conversions.
167 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
169 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
171 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
173 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
174 /* and move the valid bits to the lower byte. */
175 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
176 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
177 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
181 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
183 struct _fpxreg *st = NULL;
184 unsigned long twd = (unsigned long) fxsave->twd;
186 unsigned long ret = 0xffff0000;
189 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
191 for ( i = 0 ; i < 8 ; i++ ) {
193 st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
195 switch ( st->exponent & 0x7fff ) {
197 tag = 2; /* Special */
200 if ( !st->significand[0] &&
201 !st->significand[1] &&
202 !st->significand[2] &&
203 !st->significand[3] ) {
206 tag = 2; /* Special */
210 if ( st->significand[3] & 0x8000 ) {
213 tag = 2; /* Special */
220 ret |= (tag << (2 * i));
226 static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
227 struct pt_regs *regs)
232 static inline int convert_fxsr_from_user(struct pt_regs *regs,
233 struct _fpstate __user *buf)
238 int get_fpregs(unsigned long buf, struct task_struct *child)
242 err = convert_fxsr_to_user((struct _fpstate __user *) buf,
243 &child->thread.regs);
249 int set_fpregs(unsigned long buf, struct task_struct *child)
253 err = convert_fxsr_from_user(&child->thread.regs,
254 (struct _fpstate __user *) buf);
260 int get_fpxregs(unsigned long buf, struct task_struct *tsk)
265 int set_fpxregs(unsigned long buf, struct task_struct *tsk)
271 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
273 fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
274 (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
275 fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
276 fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
277 fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
278 fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
279 fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
281 memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
282 sizeof(fpu->st_space));
287 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )