2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include <linux/config.h>
7 #include <linux/compiler.h>
8 #include "linux/sched.h"
10 #include "asm/ptrace.h"
11 #include "asm/uaccess.h"
12 #include "asm/unistd.h"
13 #include "sysdep/ptrace.h"
14 #include "sysdep/sigcontext.h"
15 #include "sysdep/sc.h"
17 void arch_switch(void)
19 update_debugregs(current->thread.arch.debugregs_seq);
22 int is_syscall(unsigned long addr)
27 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
29 printk("is_syscall : failed to read instruction from 0x%lx\n",
33 /* int 0x80 or sysenter */
34 return((instr == 0x80cd) || (instr == 0x340f));
37 /* determines which flags the user has access to. */
38 /* 1 = access 0 = no access */
39 #define FLAG_MASK 0x00044dd5
41 int putreg(struct task_struct *child, int regno, unsigned long value)
46 if (value && (value & 3) != 3)
48 PT_REGS_FS(&child->thread.regs) = value;
51 if (value && (value & 3) != 3)
53 PT_REGS_GS(&child->thread.regs) = value;
57 if (value && (value & 3) != 3)
69 value |= PT_REGS_EFLAGS(&child->thread.regs);
72 PT_REGS_SET(&child->thread.regs, regno, value);
76 int poke_user(struct task_struct *child, long addr, long data)
78 if ((addr & 3) || addr < 0)
81 if (addr < MAX_REG_OFFSET)
82 return putreg(child, addr, data);
84 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
85 (addr <= offsetof(struct user, u_debugreg[7]))){
86 addr -= offsetof(struct user, u_debugreg[0]);
88 if((addr == 4) || (addr == 5)) return -EIO;
89 child->thread.arch.debugregs[addr] = data;
95 unsigned long getreg(struct task_struct *child, int regno)
97 unsigned long retval = ~0UL;
110 retval &= PT_REG(&child->thread.regs, regno);
115 int peek_user(struct task_struct *child, long addr, long data)
117 /* read the word at location addr in the USER area. */
120 if ((addr & 3) || addr < 0)
123 tmp = 0; /* Default return condition */
124 if(addr < MAX_REG_OFFSET){
125 tmp = getreg(child, addr);
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 tmp = child->thread.arch.debugregs[addr];
133 return put_user(tmp, (unsigned long *) data);
136 struct i387_fxsave_struct {
147 long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
148 long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
153 * FPU tag word conversions.
156 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
158 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
160 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
162 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
163 /* and move the valid bits to the lower byte. */
164 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
165 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
166 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
170 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
172 struct _fpxreg *st = NULL;
173 unsigned long twd = (unsigned long) fxsave->twd;
175 unsigned long ret = 0xffff0000;
178 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
180 for ( i = 0 ; i < 8 ; i++ ) {
182 st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
184 switch ( st->exponent & 0x7fff ) {
186 tag = 2; /* Special */
189 if ( !st->significand[0] &&
190 !st->significand[1] &&
191 !st->significand[2] &&
192 !st->significand[3] ) {
195 tag = 2; /* Special */
199 if ( st->significand[3] & 0x8000 ) {
202 tag = 2; /* Special */
209 ret |= (tag << (2 * i));
216 * FXSR floating point environment conversions.
219 #ifdef CONFIG_MODE_TT
220 static inline int convert_fxsr_to_user_tt(struct _fpstate __user *buf,
221 struct pt_regs *regs)
223 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
224 unsigned long env[7];
225 struct _fpreg __user *to;
226 struct _fpxreg *from;
229 env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
230 env[1] = (unsigned long)fxsave->swd | 0xffff0000;
231 env[2] = twd_fxsr_to_i387(fxsave);
232 env[3] = fxsave->fip;
233 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
234 env[5] = fxsave->foo;
235 env[6] = fxsave->fos;
237 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
241 from = (struct _fpxreg *) &fxsave->st_space[0];
242 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
243 if ( __copy_to_user( to, from, sizeof(*to) ) )
250 static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
251 struct pt_regs *regs)
253 return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf, regs), 0));
256 #ifdef CONFIG_MODE_TT
257 static inline int convert_fxsr_from_user_tt(struct pt_regs *regs,
258 struct _fpstate __user *buf)
260 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
261 unsigned long env[7];
263 struct _fpreg __user *from;
266 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
269 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
270 fxsave->swd = (unsigned short)(env[1] & 0xffff);
271 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
272 fxsave->fip = env[3];
273 fxsave->fop = (unsigned short)((env[4] & 0xffff0000) >> 16);
274 fxsave->fcs = (env[4] & 0xffff);
275 fxsave->foo = env[5];
276 fxsave->fos = env[6];
278 to = (struct _fpxreg *) &fxsave->st_space[0];
280 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
281 if ( __copy_from_user( to, from, sizeof(*from) ) )
288 static inline int convert_fxsr_from_user(struct pt_regs *regs,
289 struct _fpstate __user *buf)
291 return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs, buf), 0));
294 int get_fpregs(unsigned long buf, struct task_struct *child)
298 err = convert_fxsr_to_user((struct _fpstate __user *) buf,
299 &child->thread.regs);
300 if(err) return(-EFAULT);
304 int set_fpregs(unsigned long buf, struct task_struct *child)
308 err = convert_fxsr_from_user(&child->thread.regs,
309 (struct _fpstate __user *) buf);
310 if(err) return(-EFAULT);
314 #ifdef CONFIG_MODE_TT
315 int get_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
317 struct pt_regs *regs = &tsk->thread.regs;
318 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
321 err = __copy_to_user((void __user *) buf, fxsave,
322 sizeof(struct user_fxsr_struct));
323 if(err) return -EFAULT;
328 int get_fpxregs(unsigned long buf, struct task_struct *tsk)
330 return(CHOOSE_MODE(get_fpxregs_tt(buf, tsk), 0));
333 #ifdef CONFIG_MODE_TT
334 int set_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
336 struct pt_regs *regs = &tsk->thread.regs;
337 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
340 err = __copy_from_user(fxsave, (void __user *) buf,
341 sizeof(struct user_fxsr_struct) );
342 if(err) return -EFAULT;
347 int set_fpxregs(unsigned long buf, struct task_struct *tsk)
349 return(CHOOSE_MODE(set_fpxregs_tt(buf, tsk), 0));
353 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
355 fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
356 (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
357 fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
358 fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
359 fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
360 fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
361 fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
363 memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
364 sizeof(fpu->st_space));
369 #ifdef CONFIG_MODE_TT
370 static inline void copy_fpu_fxsave_tt(struct pt_regs *regs,
371 struct user_i387_struct *buf)
373 struct i387_fxsave_struct *fpu = SC_FXSR_ENV(PT_REGS_SC(regs));
375 unsigned short *from;
378 memcpy( buf, fpu, 7 * sizeof(long) );
380 to = (unsigned short *) &buf->st_space[0];
381 from = (unsigned short *) &fpu->st_space[0];
382 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
383 memcpy( to, from, 5 * sizeof(unsigned short) );
388 static inline void copy_fpu_fxsave(struct pt_regs *regs,
389 struct user_i387_struct *buf)
391 (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs, buf), 0);
394 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
396 copy_fpu_fxsave(regs, (struct user_i387_struct *) fpu);
401 * Overrides for Emacs so that we follow Linus's tabbing style.
402 * Emacs will notice this stuff at the end of the file and automatically
403 * adjust the settings for this buffer only. This must remain at the end
405 * ---------------------------------------------------------------------------
407 * c-file-style: "linux"