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);
21 arch_switch_tls_tt(from, to);
24 void arch_switch_to_skas(struct task_struct *from, struct task_struct *to)
26 arch_switch_tls_skas(from, to);
29 int is_syscall(unsigned long addr)
34 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
36 /* access_process_vm() grants access to vsyscall and stub,
37 * while copy_from_user doesn't. Maybe access_process_vm is
38 * slow, but that doesn't matter, since it will be called only
39 * in case of singlestepping, if copy_from_user failed.
41 n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
42 if(n != sizeof(instr)) {
43 printk("is_syscall : failed to read instruction from "
48 /* int 0x80 or sysenter */
49 return((instr == 0x80cd) || (instr == 0x340f));
52 /* determines which flags the user has access to. */
53 /* 1 = access 0 = no access */
54 #define FLAG_MASK 0x00044dd5
56 int putreg(struct task_struct *child, int regno, unsigned long value)
61 if (value && (value & 3) != 3)
63 PT_REGS_FS(&child->thread.regs) = value;
66 if (value && (value & 3) != 3)
68 PT_REGS_GS(&child->thread.regs) = value;
72 if (value && (value & 3) != 3)
84 value |= PT_REGS_EFLAGS(&child->thread.regs);
87 PT_REGS_SET(&child->thread.regs, regno, value);
91 int poke_user(struct task_struct *child, long addr, long data)
93 if ((addr & 3) || addr < 0)
96 if (addr < MAX_REG_OFFSET)
97 return putreg(child, addr, data);
99 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
100 (addr <= offsetof(struct user, u_debugreg[7]))){
101 addr -= offsetof(struct user, u_debugreg[0]);
103 if((addr == 4) || (addr == 5)) return -EIO;
104 child->thread.arch.debugregs[addr] = data;
110 unsigned long getreg(struct task_struct *child, int regno)
112 unsigned long retval = ~0UL;
125 retval &= PT_REG(&child->thread.regs, regno);
130 int peek_user(struct task_struct *child, long addr, long data)
132 /* read the word at location addr in the USER area. */
135 if ((addr & 3) || addr < 0)
138 tmp = 0; /* Default return condition */
139 if(addr < MAX_REG_OFFSET){
140 tmp = getreg(child, addr);
142 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
143 (addr <= offsetof(struct user, u_debugreg[7]))){
144 addr -= offsetof(struct user, u_debugreg[0]);
146 tmp = child->thread.arch.debugregs[addr];
148 return put_user(tmp, (unsigned long __user *) data);
151 struct i387_fxsave_struct {
162 long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
163 long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
168 * FPU tag word conversions.
171 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
173 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
175 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
177 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
178 /* and move the valid bits to the lower byte. */
179 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
180 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
181 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
185 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
187 struct _fpxreg *st = NULL;
188 unsigned long twd = (unsigned long) fxsave->twd;
190 unsigned long ret = 0xffff0000;
193 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
195 for ( i = 0 ; i < 8 ; i++ ) {
197 st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
199 switch ( st->exponent & 0x7fff ) {
201 tag = 2; /* Special */
204 if ( !st->significand[0] &&
205 !st->significand[1] &&
206 !st->significand[2] &&
207 !st->significand[3] ) {
210 tag = 2; /* Special */
214 if ( st->significand[3] & 0x8000 ) {
217 tag = 2; /* Special */
224 ret |= (tag << (2 * i));
231 * FXSR floating point environment conversions.
234 #ifdef CONFIG_MODE_TT
235 static inline int convert_fxsr_to_user_tt(struct _fpstate __user *buf,
236 struct pt_regs *regs)
238 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
239 unsigned long env[7];
240 struct _fpreg __user *to;
241 struct _fpxreg *from;
244 env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
245 env[1] = (unsigned long)fxsave->swd | 0xffff0000;
246 env[2] = twd_fxsr_to_i387(fxsave);
247 env[3] = fxsave->fip;
248 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
249 env[5] = fxsave->foo;
250 env[6] = fxsave->fos;
252 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
256 from = (struct _fpxreg *) &fxsave->st_space[0];
257 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
258 if ( __copy_to_user( to, from, sizeof(*to) ) )
265 static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
266 struct pt_regs *regs)
268 return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf, regs), 0));
271 #ifdef CONFIG_MODE_TT
272 static inline int convert_fxsr_from_user_tt(struct pt_regs *regs,
273 struct _fpstate __user *buf)
275 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
276 unsigned long env[7];
278 struct _fpreg __user *from;
281 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
284 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
285 fxsave->swd = (unsigned short)(env[1] & 0xffff);
286 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
287 fxsave->fip = env[3];
288 fxsave->fop = (unsigned short)((env[4] & 0xffff0000) >> 16);
289 fxsave->fcs = (env[4] & 0xffff);
290 fxsave->foo = env[5];
291 fxsave->fos = env[6];
293 to = (struct _fpxreg *) &fxsave->st_space[0];
295 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
296 if ( __copy_from_user( to, from, sizeof(*from) ) )
303 static inline int convert_fxsr_from_user(struct pt_regs *regs,
304 struct _fpstate __user *buf)
306 return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs, buf), 0));
309 int get_fpregs(unsigned long buf, struct task_struct *child)
313 err = convert_fxsr_to_user((struct _fpstate __user *) buf,
314 &child->thread.regs);
315 if(err) return(-EFAULT);
319 int set_fpregs(unsigned long buf, struct task_struct *child)
323 err = convert_fxsr_from_user(&child->thread.regs,
324 (struct _fpstate __user *) buf);
325 if(err) return(-EFAULT);
329 #ifdef CONFIG_MODE_TT
330 int get_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
332 struct pt_regs *regs = &tsk->thread.regs;
333 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
336 err = __copy_to_user((void __user *) buf, fxsave,
337 sizeof(struct user_fxsr_struct));
338 if(err) return -EFAULT;
343 int get_fpxregs(unsigned long buf, struct task_struct *tsk)
345 return(CHOOSE_MODE(get_fpxregs_tt(buf, tsk), 0));
348 #ifdef CONFIG_MODE_TT
349 int set_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
351 struct pt_regs *regs = &tsk->thread.regs;
352 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
355 err = __copy_from_user(fxsave, (void __user *) buf,
356 sizeof(struct user_fxsr_struct) );
357 if(err) return -EFAULT;
362 int set_fpxregs(unsigned long buf, struct task_struct *tsk)
364 return(CHOOSE_MODE(set_fpxregs_tt(buf, tsk), 0));
368 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
370 fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
371 (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
372 fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
373 fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
374 fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
375 fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
376 fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
378 memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
379 sizeof(fpu->st_space));
384 #ifdef CONFIG_MODE_TT
385 static inline void copy_fpu_fxsave_tt(struct pt_regs *regs,
386 struct user_i387_struct *buf)
388 struct i387_fxsave_struct *fpu = SC_FXSR_ENV(PT_REGS_SC(regs));
390 unsigned short *from;
393 memcpy( buf, fpu, 7 * sizeof(long) );
395 to = (unsigned short *) &buf->st_space[0];
396 from = (unsigned short *) &fpu->st_space[0];
397 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
398 memcpy( to, from, 5 * sizeof(unsigned short) );
403 static inline void copy_fpu_fxsave(struct pt_regs *regs,
404 struct user_i387_struct *buf)
406 (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs, buf), 0);
409 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
411 copy_fpu_fxsave(regs, (struct user_i387_struct *) fpu);
416 * Overrides for Emacs so that we follow Linus's tabbing style.
417 * Emacs will notice this stuff at the end of the file and automatically
418 * adjust the settings for this buffer only. This must remain at the end
420 * ---------------------------------------------------------------------------
422 * c-file-style: "linux"