blob: 434936c732d67f94f083bbec7198051243feba66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * include/asm-i386/i387.h
3 *
4 * Copyright (C) 1994 Linus Torvalds
5 *
6 * Pentium III FXSR, SSE support
7 * General FPU state handling cleanups
8 * Gareth Hughes <gareth@valinux.com>, May 2000
9 */
10
11#ifndef __ASM_I386_I387_H
12#define __ASM_I386_I387_H
13
14#include <linux/sched.h>
15#include <linux/init.h>
Andi Kleen18bd0572006-04-20 02:36:45 +020016#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/processor.h>
18#include <asm/sigcontext.h>
19#include <asm/user.h>
20
21extern void mxcsr_feature_mask_init(void);
22extern void init_fpu(struct task_struct *);
Linus Torvalds8ed13832005-07-22 16:06:16 -040023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * FPU lazy state save handling...
26 */
Linus Torvalds8ed13832005-07-22 16:06:16 -040027
28/*
29 * The "nop" is needed to make the instructions the same
30 * length.
31 */
32#define restore_fpu(tsk) \
33 alternative_input( \
34 "nop ; frstor %1", \
35 "fxrstor %1", \
36 X86_FEATURE_FXSR, \
Linus Torvalds2847e342005-07-22 18:19:20 -040037 "m" ((tsk)->thread.i387.fxsave))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39extern void kernel_fpu_begin(void);
40#define kernel_fpu_end() do { stts(); preempt_enable(); } while(0)
41
Andi Kleen18bd0572006-04-20 02:36:45 +020042/* We need a safe address that is cheap to find and that is already
43 in L1 during context switch. The best choices are unfortunately
44 different for UP and SMP */
45#ifdef CONFIG_SMP
46#define safe_address (__per_cpu_offset[0])
47#else
48#define safe_address (kstat_cpu(0).cpustat.user)
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * These must be called with preempt disabled
53 */
54static inline void __save_init_fpu( struct task_struct *tsk )
55{
Andi Kleen18bd0572006-04-20 02:36:45 +020056 /* Use more nops than strictly needed in case the compiler
57 varies code */
Linus Torvalds2847e342005-07-22 18:19:20 -040058 alternative_input(
Andi Kleen18bd0572006-04-20 02:36:45 +020059 "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
60 "fxsave %[fx]\n"
Chuck Ebbert543f2a32006-04-29 14:07:49 -040061 "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
Linus Torvalds2847e342005-07-22 18:19:20 -040062 X86_FEATURE_FXSR,
Andi Kleen18bd0572006-04-20 02:36:45 +020063 [fx] "m" (tsk->thread.i387.fxsave),
64 [fsw] "m" (tsk->thread.i387.fxsave.swd) : "memory");
65 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
66 is pending. Clear the x87 state here by setting it to fixed
Chuck Ebbert543f2a32006-04-29 14:07:49 -040067 values. safe_address is a random variable that should be in L1 */
Andi Kleen18bd0572006-04-20 02:36:45 +020068 alternative_input(
69 GENERIC_NOP8 GENERIC_NOP2,
70 "emms\n\t" /* clear stack tags */
71 "fildl %[addr]", /* set F?P to defined value */
72 X86_FEATURE_FXSAVE_LEAK,
73 [addr] "m" (safe_address));
Al Viro06b425d2006-01-12 01:05:40 -080074 task_thread_info(tsk)->status &= ~TS_USEDFPU;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77#define __unlazy_fpu( tsk ) do { \
Al Viro06b425d2006-01-12 01:05:40 -080078 if (task_thread_info(tsk)->status & TS_USEDFPU) \
Chuck Ebbertacc20762006-12-07 02:14:01 +010079 save_init_fpu( tsk ); \
80 else \
81 tsk->fpu_counter = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070082} while (0)
83
84#define __clear_fpu( tsk ) \
85do { \
Al Viro06b425d2006-01-12 01:05:40 -080086 if (task_thread_info(tsk)->status & TS_USEDFPU) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 asm volatile("fnclex ; fwait"); \
Al Viro06b425d2006-01-12 01:05:40 -080088 task_thread_info(tsk)->status &= ~TS_USEDFPU; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 stts(); \
90 } \
91} while (0)
92
93
94/*
95 * These disable preemption on their own and are safe
96 */
97static inline void save_init_fpu( struct task_struct *tsk )
98{
99 preempt_disable();
100 __save_init_fpu(tsk);
101 stts();
102 preempt_enable();
103}
104
105#define unlazy_fpu( tsk ) do { \
106 preempt_disable(); \
107 __unlazy_fpu(tsk); \
108 preempt_enable(); \
109} while (0)
110
111#define clear_fpu( tsk ) do { \
112 preempt_disable(); \
113 __clear_fpu( tsk ); \
114 preempt_enable(); \
115} while (0)
116 \
117/*
118 * FPU state interaction...
119 */
120extern unsigned short get_fpu_cwd( struct task_struct *tsk );
121extern unsigned short get_fpu_swd( struct task_struct *tsk );
122extern unsigned short get_fpu_mxcsr( struct task_struct *tsk );
Chuck Ebbertacc20762006-12-07 02:14:01 +0100123extern asmlinkage void math_state_restore(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * Signal frame handlers...
127 */
128extern int save_i387( struct _fpstate __user *buf );
129extern int restore_i387( struct _fpstate __user *buf );
130
131/*
132 * ptrace request handers...
133 */
134extern int get_fpregs( struct user_i387_struct __user *buf,
135 struct task_struct *tsk );
136extern int set_fpregs( struct task_struct *tsk,
137 struct user_i387_struct __user *buf );
138
139extern int get_fpxregs( struct user_fxsr_struct __user *buf,
140 struct task_struct *tsk );
141extern int set_fpxregs( struct task_struct *tsk,
142 struct user_fxsr_struct __user *buf );
143
144/*
145 * FPU state for core dumps...
146 */
147extern int dump_fpu( struct pt_regs *regs,
148 struct user_i387_struct *fpu );
149
150#endif /* __ASM_I386_I387_H */