2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include <linux/compiler.h>
7 #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_to_tt(struct task_struct
*from
, struct task_struct
*to
)
19 update_debugregs(to
->thread
.arch
.debugregs_seq
);
20 arch_switch_tls_tt(from
, to
);
23 void arch_switch_to_skas(struct task_struct
*from
, struct task_struct
*to
)
25 int err
= arch_switch_tls_skas(from
, to
);
30 printk(KERN_WARNING
"arch_switch_tls_skas failed, errno %d, not EINVAL\n", -err
);
32 printk(KERN_WARNING
"arch_switch_tls_skas failed, errno = EINVAL\n");
35 int is_syscall(unsigned long addr
)
40 n
= copy_from_user(&instr
, (void __user
*) addr
, sizeof(instr
));
42 /* access_process_vm() grants access to vsyscall and stub,
43 * while copy_from_user doesn't. Maybe access_process_vm is
44 * slow, but that doesn't matter, since it will be called only
45 * in case of singlestepping, if copy_from_user failed.
47 n
= access_process_vm(current
, addr
, &instr
, sizeof(instr
), 0);
48 if(n
!= sizeof(instr
)) {
49 printk("is_syscall : failed to read instruction from "
54 /* int 0x80 or sysenter */
55 return((instr
== 0x80cd) || (instr
== 0x340f));
58 /* determines which flags the user has access to. */
59 /* 1 = access 0 = no access */
60 #define FLAG_MASK 0x00044dd5
62 int putreg(struct task_struct
*child
, int regno
, unsigned long value
)
67 if (value
&& (value
& 3) != 3)
69 PT_REGS_FS(&child
->thread
.regs
) = value
;
72 if (value
&& (value
& 3) != 3)
74 PT_REGS_GS(&child
->thread
.regs
) = value
;
78 if (value
&& (value
& 3) != 3)
90 value
|= PT_REGS_EFLAGS(&child
->thread
.regs
);
93 PT_REGS_SET(&child
->thread
.regs
, regno
, value
);
97 int poke_user(struct task_struct
*child
, long addr
, long data
)
99 if ((addr
& 3) || addr
< 0)
102 if (addr
< MAX_REG_OFFSET
)
103 return putreg(child
, addr
, data
);
105 else if((addr
>= offsetof(struct user
, u_debugreg
[0])) &&
106 (addr
<= offsetof(struct user
, u_debugreg
[7]))){
107 addr
-= offsetof(struct user
, u_debugreg
[0]);
109 if((addr
== 4) || (addr
== 5)) return -EIO
;
110 child
->thread
.arch
.debugregs
[addr
] = data
;
116 unsigned long getreg(struct task_struct
*child
, int regno
)
118 unsigned long retval
= ~0UL;
131 retval
&= PT_REG(&child
->thread
.regs
, regno
);
136 int peek_user(struct task_struct
*child
, long addr
, long data
)
138 /* read the word at location addr in the USER area. */
141 if ((addr
& 3) || addr
< 0)
144 tmp
= 0; /* Default return condition */
145 if(addr
< MAX_REG_OFFSET
){
146 tmp
= getreg(child
, addr
);
148 else if((addr
>= offsetof(struct user
, u_debugreg
[0])) &&
149 (addr
<= offsetof(struct user
, u_debugreg
[7]))){
150 addr
-= offsetof(struct user
, u_debugreg
[0]);
152 tmp
= child
->thread
.arch
.debugregs
[addr
];
154 return put_user(tmp
, (unsigned long __user
*) data
);
157 struct i387_fxsave_struct
{
168 long st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
169 long xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
174 * FPU tag word conversions.
177 static inline unsigned short twd_i387_to_fxsr( unsigned short twd
)
179 unsigned int tmp
; /* to avoid 16 bit prefixes in the code */
181 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
183 tmp
= (tmp
| (tmp
>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
184 /* and move the valid bits to the lower byte. */
185 tmp
= (tmp
| (tmp
>> 1)) & 0x3333; /* 00VV00VV00VV00VV */
186 tmp
= (tmp
| (tmp
>> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
187 tmp
= (tmp
| (tmp
>> 4)) & 0x00ff; /* 00000000VVVVVVVV */
191 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct
*fxsave
)
193 struct _fpxreg
*st
= NULL
;
194 unsigned long twd
= (unsigned long) fxsave
->twd
;
196 unsigned long ret
= 0xffff0000;
199 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
201 for ( i
= 0 ; i
< 8 ; i
++ ) {
203 st
= (struct _fpxreg
*) FPREG_ADDR( fxsave
, i
);
205 switch ( st
->exponent
& 0x7fff ) {
207 tag
= 2; /* Special */
210 if ( !st
->significand
[0] &&
211 !st
->significand
[1] &&
212 !st
->significand
[2] &&
213 !st
->significand
[3] ) {
216 tag
= 2; /* Special */
220 if ( st
->significand
[3] & 0x8000 ) {
223 tag
= 2; /* Special */
230 ret
|= (tag
<< (2 * i
));
237 * FXSR floating point environment conversions.
240 #ifdef CONFIG_MODE_TT
241 static inline int convert_fxsr_to_user_tt(struct _fpstate __user
*buf
,
242 struct pt_regs
*regs
)
244 struct i387_fxsave_struct
*fxsave
= SC_FXSR_ENV(PT_REGS_SC(regs
));
245 unsigned long env
[7];
246 struct _fpreg __user
*to
;
247 struct _fpxreg
*from
;
250 env
[0] = (unsigned long)fxsave
->cwd
| 0xffff0000;
251 env
[1] = (unsigned long)fxsave
->swd
| 0xffff0000;
252 env
[2] = twd_fxsr_to_i387(fxsave
);
253 env
[3] = fxsave
->fip
;
254 env
[4] = fxsave
->fcs
| ((unsigned long)fxsave
->fop
<< 16);
255 env
[5] = fxsave
->foo
;
256 env
[6] = fxsave
->fos
;
258 if ( __copy_to_user( buf
, env
, 7 * sizeof(unsigned long) ) )
262 from
= (struct _fpxreg
*) &fxsave
->st_space
[0];
263 for ( i
= 0 ; i
< 8 ; i
++, to
++, from
++ ) {
264 if ( __copy_to_user( to
, from
, sizeof(*to
) ) )
271 static inline int convert_fxsr_to_user(struct _fpstate __user
*buf
,
272 struct pt_regs
*regs
)
274 return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf
, regs
), 0));
277 #ifdef CONFIG_MODE_TT
278 static inline int convert_fxsr_from_user_tt(struct pt_regs
*regs
,
279 struct _fpstate __user
*buf
)
281 struct i387_fxsave_struct
*fxsave
= SC_FXSR_ENV(PT_REGS_SC(regs
));
282 unsigned long env
[7];
284 struct _fpreg __user
*from
;
287 if ( __copy_from_user( env
, buf
, 7 * sizeof(long) ) )
290 fxsave
->cwd
= (unsigned short)(env
[0] & 0xffff);
291 fxsave
->swd
= (unsigned short)(env
[1] & 0xffff);
292 fxsave
->twd
= twd_i387_to_fxsr((unsigned short)(env
[2] & 0xffff));
293 fxsave
->fip
= env
[3];
294 fxsave
->fop
= (unsigned short)((env
[4] & 0xffff0000) >> 16);
295 fxsave
->fcs
= (env
[4] & 0xffff);
296 fxsave
->foo
= env
[5];
297 fxsave
->fos
= env
[6];
299 to
= (struct _fpxreg
*) &fxsave
->st_space
[0];
301 for ( i
= 0 ; i
< 8 ; i
++, to
++, from
++ ) {
302 if ( __copy_from_user( to
, from
, sizeof(*from
) ) )
309 static inline int convert_fxsr_from_user(struct pt_regs
*regs
,
310 struct _fpstate __user
*buf
)
312 return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs
, buf
), 0));
315 int get_fpregs(unsigned long buf
, struct task_struct
*child
)
319 err
= convert_fxsr_to_user((struct _fpstate __user
*) buf
,
320 &child
->thread
.regs
);
321 if(err
) return(-EFAULT
);
325 int set_fpregs(unsigned long buf
, struct task_struct
*child
)
329 err
= convert_fxsr_from_user(&child
->thread
.regs
,
330 (struct _fpstate __user
*) buf
);
331 if(err
) return(-EFAULT
);
335 #ifdef CONFIG_MODE_TT
336 int get_fpxregs_tt(unsigned long buf
, struct task_struct
*tsk
)
338 struct pt_regs
*regs
= &tsk
->thread
.regs
;
339 struct i387_fxsave_struct
*fxsave
= SC_FXSR_ENV(PT_REGS_SC(regs
));
342 err
= __copy_to_user((void __user
*) buf
, fxsave
,
343 sizeof(struct user_fxsr_struct
));
344 if(err
) return -EFAULT
;
349 int get_fpxregs(unsigned long buf
, struct task_struct
*tsk
)
351 return(CHOOSE_MODE(get_fpxregs_tt(buf
, tsk
), 0));
354 #ifdef CONFIG_MODE_TT
355 int set_fpxregs_tt(unsigned long buf
, struct task_struct
*tsk
)
357 struct pt_regs
*regs
= &tsk
->thread
.regs
;
358 struct i387_fxsave_struct
*fxsave
= SC_FXSR_ENV(PT_REGS_SC(regs
));
361 err
= __copy_from_user(fxsave
, (void __user
*) buf
,
362 sizeof(struct user_fxsr_struct
) );
363 if(err
) return -EFAULT
;
368 int set_fpxregs(unsigned long buf
, struct task_struct
*tsk
)
370 return(CHOOSE_MODE(set_fpxregs_tt(buf
, tsk
), 0));
374 int dump_fpu(struct pt_regs
*regs
, elf_fpregset_t
*fpu
)
376 fpu
->cwd
= (((SC_FP_CW(PT_REGS_SC(regs
)) & 0xffff) << 16) |
377 (SC_FP_SW(PT_REGS_SC(regs
)) & 0xffff));
378 fpu
->swd
= SC_FP_CSSEL(PT_REGS_SC(regs
)) & 0xffff;
379 fpu
->twd
= SC_FP_IPOFF(PT_REGS_SC(regs
));
380 fpu
->fip
= SC_FP_CSSEL(PT_REGS_SC(regs
)) & 0xffff;
381 fpu
->fcs
= SC_FP_DATAOFF(PT_REGS_SC(regs
));
382 fpu
->foo
= SC_FP_DATASEL(PT_REGS_SC(regs
));
384 memcpy(fpu
->st_space
, (void *) SC_FP_ST(PT_REGS_SC(regs
)),
385 sizeof(fpu
->st_space
));
390 #ifdef CONFIG_MODE_TT
391 static inline void copy_fpu_fxsave_tt(struct pt_regs
*regs
,
392 struct user_i387_struct
*buf
)
394 struct i387_fxsave_struct
*fpu
= SC_FXSR_ENV(PT_REGS_SC(regs
));
396 unsigned short *from
;
399 memcpy( buf
, fpu
, 7 * sizeof(long) );
401 to
= (unsigned short *) &buf
->st_space
[0];
402 from
= (unsigned short *) &fpu
->st_space
[0];
403 for ( i
= 0 ; i
< 8 ; i
++, to
+= 5, from
+= 8 ) {
404 memcpy( to
, from
, 5 * sizeof(unsigned short) );
409 static inline void copy_fpu_fxsave(struct pt_regs
*regs
,
410 struct user_i387_struct
*buf
)
412 (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs
, buf
), 0);
415 int dump_fpu(struct pt_regs
*regs
, elf_fpregset_t
*fpu
)
417 copy_fpu_fxsave(regs
, (struct user_i387_struct
*) fpu
);
422 * Overrides for Emacs so that we follow Linus's tabbing style.
423 * Emacs will notice this stuff at the end of the file and automatically
424 * adjust the settings for this buffer only. This must remain at the end
426 * ---------------------------------------------------------------------------
428 * c-file-style: "linux"