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"
11 #include "asm/ptrace.h"
12 #include "asm/uaccess.h"
13 #include "asm/unistd.h"
14 #include "sysdep/ptrace.h"
15 #include "sysdep/sigcontext.h"
16 #include "sysdep/sc.h"
18 void arch_switch_to_tt(struct task_struct *from, struct task_struct *to)
20 update_debugregs(to->thread.arch.debugregs_seq);
23 void arch_switch_to_skas(struct task_struct *from, struct task_struct *to)
27 int is_syscall(unsigned long addr)
32 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
34 /* access_process_vm() grants access to vsyscall and stub,
35 * while copy_from_user doesn't. Maybe access_process_vm is
36 * slow, but that doesn't matter, since it will be called only
37 * in case of singlestepping, if copy_from_user failed.
39 n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
40 if(n != sizeof(instr)) {
41 printk("is_syscall : failed to read instruction from "
46 /* int 0x80 or sysenter */
47 return((instr == 0x80cd) || (instr == 0x340f));
50 /* determines which flags the user has access to. */
51 /* 1 = access 0 = no access */
52 #define FLAG_MASK 0x00044dd5
54 int putreg(struct task_struct *child, int regno, unsigned long value)
59 if (value && (value & 3) != 3)
61 PT_REGS_FS(&child->thread.regs) = value;
64 if (value && (value & 3) != 3)
66 PT_REGS_GS(&child->thread.regs) = value;
70 if (value && (value & 3) != 3)
82 value |= PT_REGS_EFLAGS(&child->thread.regs);
85 PT_REGS_SET(&child->thread.regs, regno, value);
89 int poke_user(struct task_struct *child, long addr, long data)
91 if ((addr & 3) || addr < 0)
94 if (addr < MAX_REG_OFFSET)
95 return putreg(child, addr, data);
97 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
98 (addr <= offsetof(struct user, u_debugreg[7]))){
99 addr -= offsetof(struct user, u_debugreg[0]);
101 if((addr == 4) || (addr == 5)) return -EIO;
102 child->thread.arch.debugregs[addr] = data;
108 unsigned long getreg(struct task_struct *child, int regno)
110 unsigned long retval = ~0UL;
123 retval &= PT_REG(&child->thread.regs, regno);
128 int peek_user(struct task_struct *child, long addr, long data)
130 /* read the word at location addr in the USER area. */
133 if ((addr & 3) || addr < 0)
136 tmp = 0; /* Default return condition */
137 if(addr < MAX_REG_OFFSET){
138 tmp = getreg(child, addr);
140 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
141 (addr <= offsetof(struct user, u_debugreg[7]))){
142 addr -= offsetof(struct user, u_debugreg[0]);
144 tmp = child->thread.arch.debugregs[addr];
146 return put_user(tmp, (unsigned long __user *) data);
149 struct i387_fxsave_struct {
160 long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
161 long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
166 * FPU tag word conversions.
169 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
171 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
173 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
175 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
176 /* and move the valid bits to the lower byte. */
177 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
178 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
179 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
183 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
185 struct _fpxreg *st = NULL;
186 unsigned long twd = (unsigned long) fxsave->twd;
188 unsigned long ret = 0xffff0000;
191 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
193 for ( i = 0 ; i < 8 ; i++ ) {
195 st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
197 switch ( st->exponent & 0x7fff ) {
199 tag = 2; /* Special */
202 if ( !st->significand[0] &&
203 !st->significand[1] &&
204 !st->significand[2] &&
205 !st->significand[3] ) {
208 tag = 2; /* Special */
212 if ( st->significand[3] & 0x8000 ) {
215 tag = 2; /* Special */
222 ret |= (tag << (2 * i));
229 * FXSR floating point environment conversions.
232 #ifdef CONFIG_MODE_TT
233 static inline int convert_fxsr_to_user_tt(struct _fpstate __user *buf,
234 struct pt_regs *regs)
236 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
237 unsigned long env[7];
238 struct _fpreg __user *to;
239 struct _fpxreg *from;
242 env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
243 env[1] = (unsigned long)fxsave->swd | 0xffff0000;
244 env[2] = twd_fxsr_to_i387(fxsave);
245 env[3] = fxsave->fip;
246 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
247 env[5] = fxsave->foo;
248 env[6] = fxsave->fos;
250 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
254 from = (struct _fpxreg *) &fxsave->st_space[0];
255 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
256 if ( __copy_to_user( to, from, sizeof(*to) ) )
263 static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
264 struct pt_regs *regs)
266 return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf, regs), 0));
269 #ifdef CONFIG_MODE_TT
270 static inline int convert_fxsr_from_user_tt(struct pt_regs *regs,
271 struct _fpstate __user *buf)
273 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
274 unsigned long env[7];
276 struct _fpreg __user *from;
279 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
282 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
283 fxsave->swd = (unsigned short)(env[1] & 0xffff);
284 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
285 fxsave->fip = env[3];
286 fxsave->fop = (unsigned short)((env[4] & 0xffff0000) >> 16);
287 fxsave->fcs = (env[4] & 0xffff);
288 fxsave->foo = env[5];
289 fxsave->fos = env[6];
291 to = (struct _fpxreg *) &fxsave->st_space[0];
293 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
294 if ( __copy_from_user( to, from, sizeof(*from) ) )
301 static inline int convert_fxsr_from_user(struct pt_regs *regs,
302 struct _fpstate __user *buf)
304 return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs, buf), 0));
307 int get_fpregs(unsigned long buf, struct task_struct *child)
311 err = convert_fxsr_to_user((struct _fpstate __user *) buf,
312 &child->thread.regs);
313 if(err) return(-EFAULT);
317 int set_fpregs(unsigned long buf, struct task_struct *child)
321 err = convert_fxsr_from_user(&child->thread.regs,
322 (struct _fpstate __user *) buf);
323 if(err) return(-EFAULT);
327 #ifdef CONFIG_MODE_TT
328 int get_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
330 struct pt_regs *regs = &tsk->thread.regs;
331 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
334 err = __copy_to_user((void __user *) buf, fxsave,
335 sizeof(struct user_fxsr_struct));
336 if(err) return -EFAULT;
341 int get_fpxregs(unsigned long buf, struct task_struct *tsk)
343 return(CHOOSE_MODE(get_fpxregs_tt(buf, tsk), 0));
346 #ifdef CONFIG_MODE_TT
347 int set_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
349 struct pt_regs *regs = &tsk->thread.regs;
350 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
353 err = __copy_from_user(fxsave, (void __user *) buf,
354 sizeof(struct user_fxsr_struct) );
355 if(err) return -EFAULT;
360 int set_fpxregs(unsigned long buf, struct task_struct *tsk)
362 return(CHOOSE_MODE(set_fpxregs_tt(buf, tsk), 0));
366 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
368 fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
369 (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
370 fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
371 fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
372 fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
373 fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
374 fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
376 memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
377 sizeof(fpu->st_space));
382 #ifdef CONFIG_MODE_TT
383 static inline void copy_fpu_fxsave_tt(struct pt_regs *regs,
384 struct user_i387_struct *buf)
386 struct i387_fxsave_struct *fpu = SC_FXSR_ENV(PT_REGS_SC(regs));
388 unsigned short *from;
391 memcpy( buf, fpu, 7 * sizeof(long) );
393 to = (unsigned short *) &buf->st_space[0];
394 from = (unsigned short *) &fpu->st_space[0];
395 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
396 memcpy( to, from, 5 * sizeof(unsigned short) );
401 static inline void copy_fpu_fxsave(struct pt_regs *regs,
402 struct user_i387_struct *buf)
404 (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs, buf), 0);
407 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
409 copy_fpu_fxsave(regs, (struct user_i387_struct *) fpu);
414 * Overrides for Emacs so that we follow Linus's tabbing style.
415 * Emacs will notice this stuff at the end of the file and automatically
416 * adjust the settings for this buffer only. This must remain at the end
418 * ---------------------------------------------------------------------------
420 * c-file-style: "linux"