1
2
3
4
5
6
7
8
9
10
11#include <linux/config.h>
12#include <linux/sched.h>
13#include <linux/init.h>
14#include <asm/processor.h>
15#include <asm/i387.h>
16#include <asm/math_emu.h>
17#include <asm/sigcontext.h>
18#include <asm/user.h>
19#include <asm/ptrace.h>
20#include <asm/uaccess.h>
21
22#ifdef CONFIG_MATH_EMULATION
23#define HAVE_HWFP (boot_cpu_data.hard_math)
24#else
25#define HAVE_HWFP 1
26#endif
27
28static union i387_union empty_fpu_state;
29
30void __init boot_init_fpu(void)
31{
32 memset(&empty_fpu_state, 0, sizeof(union i387_union));
33
34 if (!cpu_has_fxsr) {
35 empty_fpu_state.fsave.cwd = 0xffff037f;
36 empty_fpu_state.fsave.swd = 0xffff0000;
37 empty_fpu_state.fsave.twd = 0xffffffff;
38 empty_fpu_state.fsave.fos = 0xffff0000;
39 } else {
40 empty_fpu_state.fxsave.cwd = 0x37f;
41 if (cpu_has_xmm)
42 empty_fpu_state.fxsave.mxcsr = 0x1f80;
43 }
44}
45
46void load_empty_fpu(struct task_struct * tsk)
47{
48 memcpy(&tsk->thread.i387, &empty_fpu_state, sizeof(union i387_union));
49}
50
51
52
53
54
55
56
57void init_fpu(void)
58{
59 if (cpu_has_fxsr)
60 asm volatile("fxrstor %0" : : "m" (empty_fpu_state.fxsave));
61 else
62 __asm__("fninit");
63 current->used_math = 1;
64}
65
66
67
68
69
70static inline void __save_init_fpu( struct task_struct *tsk )
71{
72 if ( cpu_has_fxsr ) {
73 asm volatile( "fxsave %0 ; fnclex"
74 : "=m" (tsk->thread.i387.fxsave) );
75 } else {
76 asm volatile( "fnsave %0 ; fwait"
77 : "=m" (tsk->thread.i387.fsave) );
78 }
79 tsk->flags &= ~PF_USEDFPU;
80}
81
82void save_init_fpu( struct task_struct *tsk )
83{
84 __save_init_fpu(tsk);
85 stts();
86}
87
88void kernel_fpu_begin(void)
89{
90 struct task_struct *tsk = current;
91
92 if (tsk->flags & PF_USEDFPU) {
93 __save_init_fpu(tsk);
94 return;
95 }
96 clts();
97}
98
99void restore_fpu( struct task_struct *tsk )
100{
101 if ( cpu_has_fxsr ) {
102 asm volatile( "fxrstor %0"
103 : : "m" (tsk->thread.i387.fxsave) );
104 } else {
105 asm volatile( "frstor %0"
106 : : "m" (tsk->thread.i387.fsave) );
107 }
108}
109
110
111
112
113
114static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
115{
116 unsigned int tmp;
117
118
119 tmp = ~twd;
120 tmp = (tmp | (tmp>>1)) & 0x5555;
121
122 tmp = (tmp | (tmp >> 1)) & 0x3333;
123 tmp = (tmp | (tmp >> 2)) & 0x0f0f;
124 tmp = (tmp | (tmp >> 4)) & 0x00ff;
125 return tmp;
126}
127
128static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
129{
130 struct _fpxreg *st = NULL;
131 unsigned long twd = (unsigned long) fxsave->twd;
132 unsigned long tag;
133 unsigned long ret = 0xffff0000;
134 int i;
135
136#define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
137
138 for ( i = 0 ; i < 8 ; i++ ) {
139 if ( twd & 0x1 ) {
140 st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
141
142 switch ( st->exponent & 0x7fff ) {
143 case 0x7fff:
144 tag = 2;
145 break;
146 case 0x0000:
147 if ( !st->significand[0] &&
148 !st->significand[1] &&
149 !st->significand[2] &&
150 !st->significand[3] ) {
151 tag = 1;
152 } else {
153 tag = 2;
154 }
155 break;
156 default:
157 if ( st->significand[3] & 0x8000 ) {
158 tag = 0;
159 } else {
160 tag = 2;
161 }
162 break;
163 }
164 } else {
165 tag = 3;
166 }
167 ret |= (tag << (2 * i));
168 twd = twd >> 1;
169 }
170 return ret;
171}
172
173
174
175
176
177unsigned short get_fpu_cwd( struct task_struct *tsk )
178{
179 if ( cpu_has_fxsr ) {
180 return tsk->thread.i387.fxsave.cwd;
181 } else {
182 return (unsigned short)tsk->thread.i387.fsave.cwd;
183 }
184}
185
186unsigned short get_fpu_swd( struct task_struct *tsk )
187{
188 if ( cpu_has_fxsr ) {
189 return tsk->thread.i387.fxsave.swd;
190 } else {
191 return (unsigned short)tsk->thread.i387.fsave.swd;
192 }
193}
194
195unsigned short get_fpu_twd( struct task_struct *tsk )
196{
197 if ( cpu_has_fxsr ) {
198 return tsk->thread.i387.fxsave.twd;
199 } else {
200 return (unsigned short)tsk->thread.i387.fsave.twd;
201 }
202}
203
204unsigned short get_fpu_mxcsr( struct task_struct *tsk )
205{
206 if ( cpu_has_xmm ) {
207 return tsk->thread.i387.fxsave.mxcsr;
208 } else {
209 return 0x1f80;
210 }
211}
212
213void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
214{
215 if ( cpu_has_fxsr ) {
216 tsk->thread.i387.fxsave.cwd = cwd;
217 } else {
218 tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000);
219 }
220}
221
222void set_fpu_swd( struct task_struct *tsk, unsigned short swd )
223{
224 if ( cpu_has_fxsr ) {
225 tsk->thread.i387.fxsave.swd = swd;
226 } else {
227 tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000);
228 }
229}
230
231void set_fpu_twd( struct task_struct *tsk, unsigned short twd )
232{
233 if ( cpu_has_fxsr ) {
234 tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
235 } else {
236 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000);
237 }
238}
239
240void set_fpu_mxcsr( struct task_struct *tsk, unsigned short mxcsr )
241{
242 if ( cpu_has_xmm ) {
243 tsk->thread.i387.fxsave.mxcsr = (mxcsr & 0xffbf);
244 }
245}
246
247
248
249
250
251static inline int convert_fxsr_to_user( struct _fpstate *buf,
252 struct i387_fxsave_struct *fxsave )
253{
254 unsigned long env[7];
255 struct _fpreg *to;
256 struct _fpxreg *from;
257 int i;
258
259 env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
260 env[1] = (unsigned long)fxsave->swd | 0xffff0000;
261 env[2] = twd_fxsr_to_i387(fxsave);
262 env[3] = fxsave->fip;
263 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
264 env[5] = fxsave->foo;
265 env[6] = fxsave->fos;
266
267 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
268 return 1;
269
270 to = &buf->_st[0];
271 from = (struct _fpxreg *) &fxsave->st_space[0];
272 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
273 if ( __copy_to_user( to, from, sizeof(*to) ) )
274 return 1;
275 }
276 return 0;
277}
278
279static inline int convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
280 struct _fpstate *buf )
281{
282 unsigned long env[7];
283 struct _fpxreg *to;
284 struct _fpreg *from;
285 int i;
286
287 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
288 return 1;
289
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];
298
299 to = (struct _fpxreg *) &fxsave->st_space[0];
300 from = &buf->_st[0];
301 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
302 if ( __copy_from_user( to, from, sizeof(*from) ) )
303 return 1;
304 }
305 return 0;
306}
307
308
309
310
311
312static inline int save_i387_fsave( struct _fpstate *buf )
313{
314 struct task_struct *tsk = current;
315
316 unlazy_fpu( tsk );
317 tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
318 if ( __copy_to_user( buf, &tsk->thread.i387.fsave,
319 sizeof(struct i387_fsave_struct) ) )
320 return -1;
321 return 1;
322}
323
324static inline int save_i387_fxsave( struct _fpstate *buf )
325{
326 struct task_struct *tsk = current;
327 int err = 0;
328
329 unlazy_fpu( tsk );
330
331 if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
332 return -1;
333
334 err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
335 err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
336 if ( err )
337 return -1;
338
339 if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
340 sizeof(struct i387_fxsave_struct) ) )
341 return -1;
342 return 1;
343}
344
345int save_i387( struct _fpstate *buf )
346{
347 if ( !current->used_math )
348 return 0;
349
350
351
352
353 current->used_math = 0;
354
355 if ( HAVE_HWFP ) {
356 if ( cpu_has_fxsr ) {
357 return save_i387_fxsave( buf );
358 } else {
359 return save_i387_fsave( buf );
360 }
361 } else {
362 return save_i387_soft( ¤t->thread.i387.soft, buf );
363 }
364}
365
366static inline int restore_i387_fsave( struct _fpstate *buf )
367{
368 struct task_struct *tsk = current;
369 clear_fpu( tsk );
370 return __copy_from_user( &tsk->thread.i387.fsave, buf,
371 sizeof(struct i387_fsave_struct) );
372}
373
374static inline int restore_i387_fxsave( struct _fpstate *buf )
375{
376 struct task_struct *tsk = current;
377 clear_fpu( tsk );
378 if ( __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
379 sizeof(struct i387_fxsave_struct) ) )
380 return 1;
381
382 tsk->thread.i387.fxsave.mxcsr &= 0xffbf;
383 return convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
384}
385
386int restore_i387( struct _fpstate *buf )
387{
388 int err;
389
390 if ( HAVE_HWFP ) {
391 if ( cpu_has_fxsr ) {
392 err = restore_i387_fxsave( buf );
393 } else {
394 err = restore_i387_fsave( buf );
395 }
396 } else {
397 err = restore_i387_soft( ¤t->thread.i387.soft, buf );
398 }
399 current->used_math = 1;
400 return err;
401}
402
403
404
405
406
407static inline int get_fpregs_fsave( struct user_i387_struct *buf,
408 struct task_struct *tsk )
409{
410 return __copy_to_user( buf, &tsk->thread.i387.fsave,
411 sizeof(struct user_i387_struct) );
412}
413
414static inline int get_fpregs_fxsave( struct user_i387_struct *buf,
415 struct task_struct *tsk )
416{
417 return convert_fxsr_to_user( (struct _fpstate *)buf,
418 &tsk->thread.i387.fxsave );
419}
420
421int get_fpregs( struct user_i387_struct *buf, struct task_struct *tsk )
422{
423 if ( HAVE_HWFP ) {
424 if ( cpu_has_fxsr ) {
425 return get_fpregs_fxsave( buf, tsk );
426 } else {
427 return get_fpregs_fsave( buf, tsk );
428 }
429 } else {
430 return save_i387_soft( &tsk->thread.i387.soft,
431 (struct _fpstate *)buf );
432 }
433}
434
435static inline int set_fpregs_fsave( struct task_struct *tsk,
436 struct user_i387_struct *buf )
437{
438 return __copy_from_user( &tsk->thread.i387.fsave, buf,
439 sizeof(struct user_i387_struct) );
440}
441
442static inline int set_fpregs_fxsave( struct task_struct *tsk,
443 struct user_i387_struct *buf )
444{
445 return convert_fxsr_from_user( &tsk->thread.i387.fxsave,
446 (struct _fpstate *)buf );
447}
448
449int set_fpregs( struct task_struct *tsk, struct user_i387_struct *buf )
450{
451 if ( HAVE_HWFP ) {
452 if ( cpu_has_fxsr ) {
453 return set_fpregs_fxsave( tsk, buf );
454 } else {
455 return set_fpregs_fsave( tsk, buf );
456 }
457 } else {
458 return restore_i387_soft( &tsk->thread.i387.soft,
459 (struct _fpstate *)buf );
460 }
461}
462
463int get_fpxregs( struct user_fxsr_struct *buf, struct task_struct *tsk )
464{
465 if ( cpu_has_fxsr ) {
466 if (__copy_to_user( (void *)buf, &tsk->thread.i387.fxsave,
467 sizeof(struct user_fxsr_struct) ))
468 return -EFAULT;
469 return 0;
470 } else {
471 return -EIO;
472 }
473}
474
475int set_fpxregs( struct task_struct *tsk, struct user_fxsr_struct *buf )
476{
477 if ( cpu_has_fxsr ) {
478 __copy_from_user( &tsk->thread.i387.fxsave, (void *)buf,
479 sizeof(struct user_fxsr_struct) );
480
481 tsk->thread.i387.fxsave.mxcsr &= 0xffbf;
482 return 0;
483 } else {
484 return -EIO;
485 }
486}
487
488
489
490
491
492static inline void copy_fpu_fsave( struct task_struct *tsk,
493 struct user_i387_struct *fpu )
494{
495 memcpy( fpu, &tsk->thread.i387.fsave,
496 sizeof(struct user_i387_struct) );
497}
498
499static inline void copy_fpu_fxsave( struct task_struct *tsk,
500 struct user_i387_struct *fpu )
501{
502 unsigned short *to;
503 unsigned short *from;
504 int i;
505
506 memcpy( fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long) );
507
508 to = (unsigned short *)&fpu->st_space[0];
509 from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
510 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
511 memcpy( to, from, 5 * sizeof(unsigned short) );
512 }
513}
514
515int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
516{
517 int fpvalid;
518 struct task_struct *tsk = current;
519
520 fpvalid = tsk->used_math;
521 if ( fpvalid ) {
522 unlazy_fpu( tsk );
523 if ( cpu_has_fxsr ) {
524 copy_fpu_fxsave( tsk, fpu );
525 } else {
526 copy_fpu_fsave( tsk, fpu );
527 }
528 }
529
530 return fpvalid;
531}
532
533int dump_extended_fpu( struct pt_regs *regs, struct user_fxsr_struct *fpu )
534{
535 int fpvalid;
536 struct task_struct *tsk = current;
537
538 fpvalid = tsk->used_math && cpu_has_fxsr;
539 if ( fpvalid ) {
540 unlazy_fpu( tsk );
541 memcpy( fpu, &tsk->thread.i387.fxsave,
542 sizeof(struct user_fxsr_struct) );
543 }
544
545 return fpvalid;
546}
547