i387.h 3.5 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * include/asm-x86_64/i387.h
 *
 * Copyright (C) 1994 Linus Torvalds
 *
 * Pentium III FXSR, SSE support
 * General FPU state handling cleanups
 *	Gareth Hughes <gareth@valinux.com>, May 2000
9
 * x86-64 work by Andi Kleen 2002
10 11 12 13 14 15 16 17 18
 */

#ifndef __ASM_X86_64_I387_H
#define __ASM_X86_64_I387_H

#include <linux/sched.h>
#include <asm/processor.h>
#include <asm/sigcontext.h>
#include <asm/user.h>
Andi Kleen's avatar
Andi Kleen committed
19
#include <asm/thread_info.h>
Andi Kleen's avatar
Andi Kleen committed
20
#include <asm/uaccess.h>
21

22
extern void fpu_init(void);
23 24
extern unsigned int mxcsr_feature_mask;
extern void mxcsr_feature_mask_init(void);
Andi Kleen's avatar
Andi Kleen committed
25 26
extern void init_fpu(struct task_struct *child);
extern int save_i387(struct _fpstate *buf);
27

Andi Kleen's avatar
Andi Kleen committed
28 29 30 31 32
static inline int need_signal_i387(struct task_struct *me) 
{ 
	if (!me->used_math)
		return 0;
	me->used_math = 0; 
Andi Kleen's avatar
Andi Kleen committed
33
	if (me->thread_info->status & TS_USEDFPU)
Andi Kleen's avatar
Andi Kleen committed
34 35 36 37
		return 0;
	return 1;
} 

38 39 40 41
/*
 * FPU lazy state save handling...
 */

42
#define kernel_fpu_end() stts()
43

44
#define unlazy_fpu(tsk) do { \
Andi Kleen's avatar
Andi Kleen committed
45
	if ((tsk)->thread_info->status & TS_USEDFPU) \
46
		save_init_fpu(tsk); \
47 48
} while (0)

49
#define clear_fpu(tsk) do { \
Andi Kleen's avatar
Andi Kleen committed
50
	if ((tsk)->thread_info->status & TS_USEDFPU) {		\
51
		asm volatile("fwait");				\
Andi Kleen's avatar
Andi Kleen committed
52
		(tsk)->thread_info->status &= ~TS_USEDFPU;	\
53 54 55 56 57
		stts();						\
	}							\
} while (0)

/*
58
 * ptrace request handers...
59
 */
60 61 62 63
extern int get_fpregs(struct user_i387_struct *buf,
		      struct task_struct *tsk);
extern int set_fpregs(struct task_struct *tsk,
		      struct user_i387_struct *buf);
64 65

/*
66
 * i387 state interaction
67
 */
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#define get_fpu_mxcsr(t) ((t)->thread.i387.fxsave.mxcsr)
#define get_fpu_cwd(t) ((t)->thread.i387.fxsave.cwd)
#define get_fpu_fxsr_twd(t) ((t)->thread.i387.fxsave.twd)
#define get_fpu_swd(t) ((t)->thread.i387.fxsave.swd)
#define set_fpu_cwd(t,val) ((t)->thread.i387.fxsave.cwd = (val))
#define set_fpu_swd(t,val) ((t)->thread.i387.fxsave.swd = (val))
#define set_fpu_fxsr_twd(t,val) ((t)->thread.i387.fxsave.twd = (val))

static inline int restore_fpu_checking(struct i387_fxsave_struct *fx) 
{ 
	int err;
	asm volatile("1:  rex64 ; fxrstor (%[fx])\n\t"
		     "2:\n"
		     ".section .fixup,\"ax\"\n"
		     "3:  movl $-1,%[err]\n"
		     "    jmp  2b\n"
		     ".previous\n"
		     ".section __ex_table,\"a\"\n"
		     "   .align 8\n"
		     "   .quad  1b,3b\n"
		     ".previous"
		     : [err] "=r" (err)
		     : [fx] "r" (fx), "0" (0)); 
Andi Kleen's avatar
Andi Kleen committed
91 92
	if (unlikely(err))
		init_fpu(current);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	return err;
} 

static inline int save_i387_checking(struct i387_fxsave_struct *fx) 
{ 
	int err;
	asm volatile("1:  rex64 ; fxsave (%[fx])\n\t"
		     "2:\n"
		     ".section .fixup,\"ax\"\n"
		     "3:  movl $-1,%[err]\n"
		     "    jmp  2b\n"
		     ".previous\n"
		     ".section __ex_table,\"a\"\n"
		     "   .align 8\n"
		     "   .quad  1b,3b\n"
		     ".previous"
		     : [err] "=r" (err)
		     : [fx] "r" (fx), "0" (0)); 
Andi Kleen's avatar
Andi Kleen committed
111 112
	if (unlikely(err))
		__clear_user(fx, sizeof(struct i387_fxsave_struct));
113 114 115 116 117
	return err;
} 

static inline void kernel_fpu_begin(void)
{
Andi Kleen's avatar
Andi Kleen committed
118 119 120 121 122
	struct thread_info *me = current_thread_info();
	if (me->status & TS_USEDFPU) { 
		asm volatile("rex64 ; fxsave %0 ; fnclex"
			      : "=m" (me->task->thread.i387.fxsave));
		me->status &= ~TS_USEDFPU;
123 124 125 126 127 128 129 130 131
		return;
	}
	clts();
}

static inline void save_init_fpu( struct task_struct *tsk )
{
	asm volatile( "fxsave %0 ; fnclex"
		      : "=m" (tsk->thread.i387.fxsave));
Andi Kleen's avatar
Andi Kleen committed
132
	tsk->thread_info->status &= ~TS_USEDFPU;
133 134 135 136 137 138 139 140 141 142 143
	stts();
}

/* 
 * This restores directly out of user space. Exceptions are handled.
 */
static inline int restore_i387(struct _fpstate *buf)
{
	return restore_fpu_checking((struct i387_fxsave_struct *)buf);
}

144
#endif /* __ASM_X86_64_I387_H */