2 * Save/restore floating point context for signal handlers.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
9 * Copyright (C) 2006 ST Microelectronics Ltd. (denorm support)
11 * FIXME! These routines have not been tested for big endian case.
13 #include <linux/sched.h>
14 #include <linux/signal.h>
17 #include <asm/processor.h>
18 #include <asm/system.h>
21 /* The PR (precision) bit in the FP Status Register must be clear when
22 * an frchg instruction is executed, otherwise the instruction is undefined.
23 * Executing frchg with PR set causes a trap on some SH4 implementations.
26 #define FPSCR_RCHG 0x00000000
27 extern unsigned long long float64_div(unsigned long long a
,
28 unsigned long long b
);
29 extern unsigned long int float32_div(unsigned long int a
, unsigned long int b
);
30 extern unsigned long long float64_mul(unsigned long long a
,
31 unsigned long long b
);
32 extern unsigned long int float32_mul(unsigned long int a
, unsigned long int b
);
33 extern unsigned long long float64_add(unsigned long long a
,
34 unsigned long long b
);
35 extern unsigned long int float32_add(unsigned long int a
, unsigned long int b
);
36 extern unsigned long long float64_sub(unsigned long long a
,
37 unsigned long long b
);
38 extern unsigned long int float32_sub(unsigned long int a
, unsigned long int b
);
39 extern unsigned long int float64_to_float32(unsigned long long a
);
40 static unsigned int fpu_exception_flags
;
43 * Save FPU registers onto task structure.
45 void save_fpu(struct task_struct
*tsk
)
50 asm volatile ("sts.l fpul, @-%0\n\t"
51 "sts.l fpscr, @-%0\n\t"
54 "fmov.s fr15, @-%0\n\t"
55 "fmov.s fr14, @-%0\n\t"
56 "fmov.s fr13, @-%0\n\t"
57 "fmov.s fr12, @-%0\n\t"
58 "fmov.s fr11, @-%0\n\t"
59 "fmov.s fr10, @-%0\n\t"
60 "fmov.s fr9, @-%0\n\t"
61 "fmov.s fr8, @-%0\n\t"
62 "fmov.s fr7, @-%0\n\t"
63 "fmov.s fr6, @-%0\n\t"
64 "fmov.s fr5, @-%0\n\t"
65 "fmov.s fr4, @-%0\n\t"
66 "fmov.s fr3, @-%0\n\t"
67 "fmov.s fr2, @-%0\n\t"
68 "fmov.s fr1, @-%0\n\t"
69 "fmov.s fr0, @-%0\n\t"
71 "fmov.s fr15, @-%0\n\t"
72 "fmov.s fr14, @-%0\n\t"
73 "fmov.s fr13, @-%0\n\t"
74 "fmov.s fr12, @-%0\n\t"
75 "fmov.s fr11, @-%0\n\t"
76 "fmov.s fr10, @-%0\n\t"
77 "fmov.s fr9, @-%0\n\t"
78 "fmov.s fr8, @-%0\n\t"
79 "fmov.s fr7, @-%0\n\t"
80 "fmov.s fr6, @-%0\n\t"
81 "fmov.s fr5, @-%0\n\t"
82 "fmov.s fr4, @-%0\n\t"
83 "fmov.s fr3, @-%0\n\t"
84 "fmov.s fr2, @-%0\n\t"
85 "fmov.s fr1, @-%0\n\t"
86 "fmov.s fr0, @-%0\n\t"
87 "lds %3, fpscr\n\t":"=r" (dummy
)
88 :"0"((char *)(&tsk
->thread
.fpu
.hard
.status
)),
89 "r"(FPSCR_RCHG
), "r"(FPSCR_INIT
)
95 static void restore_fpu(struct task_struct
*tsk
)
100 asm volatile ("lds %2, fpscr\n\t"
101 "fmov.s @%0+, fr0\n\t"
102 "fmov.s @%0+, fr1\n\t"
103 "fmov.s @%0+, fr2\n\t"
104 "fmov.s @%0+, fr3\n\t"
105 "fmov.s @%0+, fr4\n\t"
106 "fmov.s @%0+, fr5\n\t"
107 "fmov.s @%0+, fr6\n\t"
108 "fmov.s @%0+, fr7\n\t"
109 "fmov.s @%0+, fr8\n\t"
110 "fmov.s @%0+, fr9\n\t"
111 "fmov.s @%0+, fr10\n\t"
112 "fmov.s @%0+, fr11\n\t"
113 "fmov.s @%0+, fr12\n\t"
114 "fmov.s @%0+, fr13\n\t"
115 "fmov.s @%0+, fr14\n\t"
116 "fmov.s @%0+, fr15\n\t"
118 "fmov.s @%0+, fr0\n\t"
119 "fmov.s @%0+, fr1\n\t"
120 "fmov.s @%0+, fr2\n\t"
121 "fmov.s @%0+, fr3\n\t"
122 "fmov.s @%0+, fr4\n\t"
123 "fmov.s @%0+, fr5\n\t"
124 "fmov.s @%0+, fr6\n\t"
125 "fmov.s @%0+, fr7\n\t"
126 "fmov.s @%0+, fr8\n\t"
127 "fmov.s @%0+, fr9\n\t"
128 "fmov.s @%0+, fr10\n\t"
129 "fmov.s @%0+, fr11\n\t"
130 "fmov.s @%0+, fr12\n\t"
131 "fmov.s @%0+, fr13\n\t"
132 "fmov.s @%0+, fr14\n\t"
133 "fmov.s @%0+, fr15\n\t"
135 "lds.l @%0+, fpscr\n\t"
136 "lds.l @%0+, fpul\n\t"
138 :"0"(&tsk
->thread
.fpu
), "r"(FPSCR_RCHG
)
144 * Load the FPU with signalling NANS. This bit pattern we're using
145 * has the property that no matter wether considered as single or as
146 * double precision represents signaling NANS.
149 static void fpu_init(void)
152 asm volatile ( "lds %0, fpul\n\t"
164 "fsts fpul, fr10\n\t"
165 "fsts fpul, fr11\n\t"
166 "fsts fpul, fr12\n\t"
167 "fsts fpul, fr13\n\t"
168 "fsts fpul, fr14\n\t"
169 "fsts fpul, fr15\n\t"
181 "fsts fpul, fr10\n\t"
182 "fsts fpul, fr11\n\t"
183 "fsts fpul, fr12\n\t"
184 "fsts fpul, fr13\n\t"
185 "fsts fpul, fr14\n\t"
186 "fsts fpul, fr15\n\t"
190 :"r" (0), "r"(FPSCR_RCHG
), "r"(FPSCR_INIT
));
195 * denormal_to_double - Given denormalized float number,
198 * @fpu: Pointer to sh_fpu_hard structure
199 * @n: Index to FP register
201 static void denormal_to_double(struct sh_fpu_hard_struct
*fpu
, int n
)
203 unsigned long du
, dl
;
204 unsigned long x
= fpu
->fpul
;
205 int exp
= 1023 - 126;
207 if (x
!= 0 && (x
& 0x7f800000) == 0) {
208 du
= (x
& 0x80000000);
209 while ((x
& 0x00800000) == 0) {
214 du
|= (exp
<< 20) | (x
>> 3);
217 fpu
->fp_regs
[n
] = du
;
218 fpu
->fp_regs
[n
+ 1] = dl
;
223 * ieee_fpe_handler - Handle denormalized number exception
225 * @regs: Pointer to register structure
227 * Returns 1 when it's handled (should not cause exception).
229 static int ieee_fpe_handler(struct pt_regs
*regs
)
231 unsigned short insn
= *(unsigned short *)regs
->pc
;
232 unsigned short finsn
;
233 unsigned long nextpc
;
241 if (nib
[0] == 0xb || (nib
[0] == 0x4 && nib
[2] == 0x0 && nib
[3] == 0xb))
242 regs
->pr
= regs
->pc
+ 4; /* bsr & jsr */
244 if (nib
[0] == 0xa || nib
[0] == 0xb) {
246 nextpc
= regs
->pc
+ 4 + ((short)((insn
& 0xfff) << 4) >> 3);
247 finsn
= *(unsigned short *)(regs
->pc
+ 2);
248 } else if (nib
[0] == 0x8 && nib
[1] == 0xd) {
251 nextpc
= regs
->pc
+ 4 + ((char)(insn
& 0xff) << 1);
253 nextpc
= regs
->pc
+ 4;
254 finsn
= *(unsigned short *)(regs
->pc
+ 2);
255 } else if (nib
[0] == 0x8 && nib
[1] == 0xf) {
258 nextpc
= regs
->pc
+ 4;
260 nextpc
= regs
->pc
+ 4 + ((char)(insn
& 0xff) << 1);
261 finsn
= *(unsigned short *)(regs
->pc
+ 2);
262 } else if (nib
[0] == 0x4 && nib
[3] == 0xb &&
263 (nib
[2] == 0x0 || nib
[2] == 0x2)) {
265 nextpc
= regs
->regs
[nib
[1]];
266 finsn
= *(unsigned short *)(regs
->pc
+ 2);
267 } else if (nib
[0] == 0x0 && nib
[3] == 0x3 &&
268 (nib
[2] == 0x0 || nib
[2] == 0x2)) {
270 nextpc
= regs
->pc
+ 4 + regs
->regs
[nib
[1]];
271 finsn
= *(unsigned short *)(regs
->pc
+ 2);
272 } else if (insn
== 0x000b) {
275 finsn
= *(unsigned short *)(regs
->pc
+ 2);
277 nextpc
= regs
->pc
+ instruction_size(insn
);
281 if ((finsn
& 0xf1ff) == 0xf0ad) {
283 struct task_struct
*tsk
= current
;
285 if ((tsk
->thread
.fpu
.hard
.fpscr
& FPSCR_CAUSE_ERROR
))
287 denormal_to_double(&tsk
->thread
.fpu
.hard
,
294 } else if ((finsn
& 0xf00f) == 0xf002) {
296 struct task_struct
*tsk
= current
;
301 n
= (finsn
>> 8) & 0xf;
302 m
= (finsn
>> 4) & 0xf;
303 hx
= tsk
->thread
.fpu
.hard
.fp_regs
[n
];
304 hy
= tsk
->thread
.fpu
.hard
.fp_regs
[m
];
305 fpscr
= tsk
->thread
.fpu
.hard
.fpscr
;
306 prec
= fpscr
& FPSCR_DBL_PRECISION
;
308 if ((fpscr
& FPSCR_CAUSE_ERROR
)
309 && (prec
&& ((hx
& 0x7fffffff) < 0x00100000
310 || (hy
& 0x7fffffff) < 0x00100000))) {
313 /* FPU error because of denormal (doubles) */
314 llx
= ((long long)hx
<< 32)
315 | tsk
->thread
.fpu
.hard
.fp_regs
[n
+ 1];
316 lly
= ((long long)hy
<< 32)
317 | tsk
->thread
.fpu
.hard
.fp_regs
[m
+ 1];
318 llx
= float64_mul(llx
, lly
);
319 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = llx
>> 32;
320 tsk
->thread
.fpu
.hard
.fp_regs
[n
+ 1] = llx
& 0xffffffff;
321 } else if ((fpscr
& FPSCR_CAUSE_ERROR
)
322 && (!prec
&& ((hx
& 0x7fffffff) < 0x00800000
323 || (hy
& 0x7fffffff) < 0x00800000))) {
324 /* FPU error because of denormal (floats) */
325 hx
= float32_mul(hx
, hy
);
326 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = hx
;
332 } else if ((finsn
& 0xf00e) == 0xf000) {
334 struct task_struct
*tsk
= current
;
339 n
= (finsn
>> 8) & 0xf;
340 m
= (finsn
>> 4) & 0xf;
341 hx
= tsk
->thread
.fpu
.hard
.fp_regs
[n
];
342 hy
= tsk
->thread
.fpu
.hard
.fp_regs
[m
];
343 fpscr
= tsk
->thread
.fpu
.hard
.fpscr
;
344 prec
= fpscr
& FPSCR_DBL_PRECISION
;
346 if ((fpscr
& FPSCR_CAUSE_ERROR
)
347 && (prec
&& ((hx
& 0x7fffffff) < 0x00100000
348 || (hy
& 0x7fffffff) < 0x00100000))) {
351 /* FPU error because of denormal (doubles) */
352 llx
= ((long long)hx
<< 32)
353 | tsk
->thread
.fpu
.hard
.fp_regs
[n
+ 1];
354 lly
= ((long long)hy
<< 32)
355 | tsk
->thread
.fpu
.hard
.fp_regs
[m
+ 1];
356 if ((finsn
& 0xf00f) == 0xf000)
357 llx
= float64_add(llx
, lly
);
359 llx
= float64_sub(llx
, lly
);
360 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = llx
>> 32;
361 tsk
->thread
.fpu
.hard
.fp_regs
[n
+ 1] = llx
& 0xffffffff;
362 } else if ((fpscr
& FPSCR_CAUSE_ERROR
)
363 && (!prec
&& ((hx
& 0x7fffffff) < 0x00800000
364 || (hy
& 0x7fffffff) < 0x00800000))) {
365 /* FPU error because of denormal (floats) */
366 if ((finsn
& 0xf00f) == 0xf000)
367 hx
= float32_add(hx
, hy
);
369 hx
= float32_sub(hx
, hy
);
370 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = hx
;
376 } else if ((finsn
& 0xf003) == 0xf003) {
378 struct task_struct
*tsk
= current
;
383 n
= (finsn
>> 8) & 0xf;
384 m
= (finsn
>> 4) & 0xf;
385 hx
= tsk
->thread
.fpu
.hard
.fp_regs
[n
];
386 hy
= tsk
->thread
.fpu
.hard
.fp_regs
[m
];
387 fpscr
= tsk
->thread
.fpu
.hard
.fpscr
;
388 prec
= fpscr
& FPSCR_DBL_PRECISION
;
390 if ((fpscr
& FPSCR_CAUSE_ERROR
)
391 && (prec
&& ((hx
& 0x7fffffff) < 0x00100000
392 || (hy
& 0x7fffffff) < 0x00100000))) {
395 /* FPU error because of denormal (doubles) */
396 llx
= ((long long)hx
<< 32)
397 | tsk
->thread
.fpu
.hard
.fp_regs
[n
+ 1];
398 lly
= ((long long)hy
<< 32)
399 | tsk
->thread
.fpu
.hard
.fp_regs
[m
+ 1];
401 llx
= float64_div(llx
, lly
);
403 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = llx
>> 32;
404 tsk
->thread
.fpu
.hard
.fp_regs
[n
+ 1] = llx
& 0xffffffff;
405 } else if ((fpscr
& FPSCR_CAUSE_ERROR
)
406 && (!prec
&& ((hx
& 0x7fffffff) < 0x00800000
407 || (hy
& 0x7fffffff) < 0x00800000))) {
408 /* FPU error because of denormal (floats) */
409 hx
= float32_div(hx
, hy
);
410 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = hx
;
416 } else if ((finsn
& 0xf0bd) == 0xf0bd) {
417 /* fcnvds - double to single precision convert */
418 struct task_struct
*tsk
= current
;
422 m
= (finsn
>> 8) & 0x7;
423 hx
= tsk
->thread
.fpu
.hard
.fp_regs
[m
];
425 if ((tsk
->thread
.fpu
.hard
.fpscr
& FPSCR_CAUSE_ERROR
)
426 && ((hx
& 0x7fffffff) < 0x00100000)) {
427 /* subnormal double to float conversion */
430 llx
= ((long long)tsk
->thread
.fpu
.hard
.fp_regs
[m
] << 32)
431 | tsk
->thread
.fpu
.hard
.fp_regs
[m
+ 1];
433 tsk
->thread
.fpu
.hard
.fpul
= float64_to_float32(llx
);
444 void float_raise(unsigned int flags
)
446 fpu_exception_flags
|= flags
;
449 int float_rounding_mode(void)
451 struct task_struct
*tsk
= current
;
452 int roundingMode
= FPSCR_ROUNDING_MODE(tsk
->thread
.fpu
.hard
.fpscr
);
456 BUILD_TRAP_HANDLER(fpu_error
)
458 struct task_struct
*tsk
= current
;
461 __unlazy_fpu(tsk
, regs
);
462 fpu_exception_flags
= 0;
463 if (ieee_fpe_handler(regs
)) {
464 tsk
->thread
.fpu
.hard
.fpscr
&=
465 ~(FPSCR_CAUSE_MASK
| FPSCR_FLAG_MASK
);
466 tsk
->thread
.fpu
.hard
.fpscr
|= fpu_exception_flags
;
467 /* Set the FPSCR flag as well as cause bits - simply
468 * replicate the cause */
469 tsk
->thread
.fpu
.hard
.fpscr
|= (fpu_exception_flags
>> 10);
472 task_thread_info(tsk
)->status
|= TS_USEDFPU
;
473 if ((((tsk
->thread
.fpu
.hard
.fpscr
& FPSCR_ENABLE_MASK
) >> 7) &
474 (fpu_exception_flags
>> 2)) == 0) {
479 force_sig(SIGFPE
, tsk
);
482 void fpu_state_restore(struct pt_regs
*regs
)
484 struct task_struct
*tsk
= current
;
487 if (unlikely(!user_mode(regs
))) {
488 printk(KERN_ERR
"BUG: FPU is used in kernel mode.\n");
493 if (likely(used_math())) {
494 /* Using the FPU again. */
497 /* First time FPU user. */
501 task_thread_info(tsk
)->status
|= TS_USEDFPU
;
505 BUILD_TRAP_HANDLER(fpu_state_restore
)
509 fpu_state_restore(regs
);