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);
40 static unsigned int fpu_exception_flags;
43 * Save FPU registers onto task structure.
44 * Assume called with FPU enabled (SR.FD=0).
46 void save_fpu(struct task_struct *tsk, struct pt_regs *regs)
50 clear_tsk_thread_flag(tsk, TIF_USEDFPU);
52 asm volatile ("sts.l fpul, @-%0\n\t"
53 "sts.l fpscr, @-%0\n\t"
56 "fmov.s fr15, @-%0\n\t"
57 "fmov.s fr14, @-%0\n\t"
58 "fmov.s fr13, @-%0\n\t"
59 "fmov.s fr12, @-%0\n\t"
60 "fmov.s fr11, @-%0\n\t"
61 "fmov.s fr10, @-%0\n\t"
62 "fmov.s fr9, @-%0\n\t"
63 "fmov.s fr8, @-%0\n\t"
64 "fmov.s fr7, @-%0\n\t"
65 "fmov.s fr6, @-%0\n\t"
66 "fmov.s fr5, @-%0\n\t"
67 "fmov.s fr4, @-%0\n\t"
68 "fmov.s fr3, @-%0\n\t"
69 "fmov.s fr2, @-%0\n\t"
70 "fmov.s fr1, @-%0\n\t"
71 "fmov.s fr0, @-%0\n\t"
73 "fmov.s fr15, @-%0\n\t"
74 "fmov.s fr14, @-%0\n\t"
75 "fmov.s fr13, @-%0\n\t"
76 "fmov.s fr12, @-%0\n\t"
77 "fmov.s fr11, @-%0\n\t"
78 "fmov.s fr10, @-%0\n\t"
79 "fmov.s fr9, @-%0\n\t"
80 "fmov.s fr8, @-%0\n\t"
81 "fmov.s fr7, @-%0\n\t"
82 "fmov.s fr6, @-%0\n\t"
83 "fmov.s fr5, @-%0\n\t"
84 "fmov.s fr4, @-%0\n\t"
85 "fmov.s fr3, @-%0\n\t"
86 "fmov.s fr2, @-%0\n\t"
87 "fmov.s fr1, @-%0\n\t"
88 "fmov.s fr0, @-%0\n\t"
89 "lds %3, fpscr\n\t":"=r" (dummy)
90 :"0"((char *)(&tsk->thread.fpu.hard.status)),
91 "r"(FPSCR_RCHG), "r"(FPSCR_INIT)
98 static void restore_fpu(struct task_struct *tsk)
103 asm volatile ("lds %2, fpscr\n\t"
104 "fmov.s @%0+, fr0\n\t"
105 "fmov.s @%0+, fr1\n\t"
106 "fmov.s @%0+, fr2\n\t"
107 "fmov.s @%0+, fr3\n\t"
108 "fmov.s @%0+, fr4\n\t"
109 "fmov.s @%0+, fr5\n\t"
110 "fmov.s @%0+, fr6\n\t"
111 "fmov.s @%0+, fr7\n\t"
112 "fmov.s @%0+, fr8\n\t"
113 "fmov.s @%0+, fr9\n\t"
114 "fmov.s @%0+, fr10\n\t"
115 "fmov.s @%0+, fr11\n\t"
116 "fmov.s @%0+, fr12\n\t"
117 "fmov.s @%0+, fr13\n\t"
118 "fmov.s @%0+, fr14\n\t"
119 "fmov.s @%0+, fr15\n\t"
121 "fmov.s @%0+, fr0\n\t"
122 "fmov.s @%0+, fr1\n\t"
123 "fmov.s @%0+, fr2\n\t"
124 "fmov.s @%0+, fr3\n\t"
125 "fmov.s @%0+, fr4\n\t"
126 "fmov.s @%0+, fr5\n\t"
127 "fmov.s @%0+, fr6\n\t"
128 "fmov.s @%0+, fr7\n\t"
129 "fmov.s @%0+, fr8\n\t"
130 "fmov.s @%0+, fr9\n\t"
131 "fmov.s @%0+, fr10\n\t"
132 "fmov.s @%0+, fr11\n\t"
133 "fmov.s @%0+, fr12\n\t"
134 "fmov.s @%0+, fr13\n\t"
135 "fmov.s @%0+, fr14\n\t"
136 "fmov.s @%0+, fr15\n\t"
138 "lds.l @%0+, fpscr\n\t"
139 "lds.l @%0+, fpul\n\t"
141 :"0"(&tsk->thread.fpu), "r"(FPSCR_RCHG)
147 * Load the FPU with signalling NANS. This bit pattern we're using
148 * has the property that no matter wether considered as single or as
149 * double precision represents signaling NANS.
152 static void fpu_init(void)
155 asm volatile ( "lds %0, fpul\n\t"
167 "fsts fpul, fr10\n\t"
168 "fsts fpul, fr11\n\t"
169 "fsts fpul, fr12\n\t"
170 "fsts fpul, fr13\n\t"
171 "fsts fpul, fr14\n\t"
172 "fsts fpul, fr15\n\t"
184 "fsts fpul, fr10\n\t"
185 "fsts fpul, fr11\n\t"
186 "fsts fpul, fr12\n\t"
187 "fsts fpul, fr13\n\t"
188 "fsts fpul, fr14\n\t"
189 "fsts fpul, fr15\n\t"
193 :"r" (0), "r"(FPSCR_RCHG), "r"(FPSCR_INIT));
198 * denormal_to_double - Given denormalized float number,
201 * @fpu: Pointer to sh_fpu_hard structure
202 * @n: Index to FP register
204 static void denormal_to_double(struct sh_fpu_hard_struct *fpu, int n)
206 unsigned long du, dl;
207 unsigned long x = fpu->fpul;
208 int exp = 1023 - 126;
210 if (x != 0 && (x & 0x7f800000) == 0) {
211 du = (x & 0x80000000);
212 while ((x & 0x00800000) == 0) {
217 du |= (exp << 20) | (x >> 3);
220 fpu->fp_regs[n] = du;
221 fpu->fp_regs[n + 1] = dl;
226 * ieee_fpe_handler - Handle denormalized number exception
228 * @regs: Pointer to register structure
230 * Returns 1 when it's handled (should not cause exception).
232 static int ieee_fpe_handler(struct pt_regs *regs)
234 unsigned short insn = *(unsigned short *)regs->pc;
235 unsigned short finsn;
236 unsigned long nextpc;
244 if (nib[0] == 0xb || (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb))
245 regs->pr = regs->pc + 4; /* bsr & jsr */
247 if (nib[0] == 0xa || nib[0] == 0xb) {
249 nextpc = regs->pc + 4 + ((short)((insn & 0xfff) << 4) >> 3);
250 finsn = *(unsigned short *)(regs->pc + 2);
251 } else if (nib[0] == 0x8 && nib[1] == 0xd) {
254 nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
256 nextpc = regs->pc + 4;
257 finsn = *(unsigned short *)(regs->pc + 2);
258 } else if (nib[0] == 0x8 && nib[1] == 0xf) {
261 nextpc = regs->pc + 4;
263 nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
264 finsn = *(unsigned short *)(regs->pc + 2);
265 } else if (nib[0] == 0x4 && nib[3] == 0xb &&
266 (nib[2] == 0x0 || nib[2] == 0x2)) {
268 nextpc = regs->regs[nib[1]];
269 finsn = *(unsigned short *)(regs->pc + 2);
270 } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
271 (nib[2] == 0x0 || nib[2] == 0x2)) {
273 nextpc = regs->pc + 4 + regs->regs[nib[1]];
274 finsn = *(unsigned short *)(regs->pc + 2);
275 } else if (insn == 0x000b) {
278 finsn = *(unsigned short *)(regs->pc + 2);
280 nextpc = regs->pc + instruction_size(insn);
284 if ((finsn & 0xf1ff) == 0xf0ad) {
286 struct task_struct *tsk = current;
289 if ((tsk->thread.fpu.hard.fpscr & FPSCR_CAUSE_ERROR))
291 denormal_to_double(&tsk->thread.fpu.hard,
298 } else if ((finsn & 0xf00f) == 0xf002) {
300 struct task_struct *tsk = current;
305 n = (finsn >> 8) & 0xf;
306 m = (finsn >> 4) & 0xf;
307 hx = tsk->thread.fpu.hard.fp_regs[n];
308 hy = tsk->thread.fpu.hard.fp_regs[m];
309 fpscr = tsk->thread.fpu.hard.fpscr;
310 prec = fpscr & FPSCR_DBL_PRECISION;
312 if ((fpscr & FPSCR_CAUSE_ERROR)
313 && (prec && ((hx & 0x7fffffff) < 0x00100000
314 || (hy & 0x7fffffff) < 0x00100000))) {
317 /* FPU error because of denormal (doubles) */
318 llx = ((long long)hx << 32)
319 | tsk->thread.fpu.hard.fp_regs[n + 1];
320 lly = ((long long)hy << 32)
321 | tsk->thread.fpu.hard.fp_regs[m + 1];
322 llx = float64_mul(llx, lly);
323 tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
324 tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
325 } else if ((fpscr & FPSCR_CAUSE_ERROR)
326 && (!prec && ((hx & 0x7fffffff) < 0x00800000
327 || (hy & 0x7fffffff) < 0x00800000))) {
328 /* FPU error because of denormal (floats) */
329 hx = float32_mul(hx, hy);
330 tsk->thread.fpu.hard.fp_regs[n] = hx;
336 } else if ((finsn & 0xf00e) == 0xf000) {
338 struct task_struct *tsk = current;
343 n = (finsn >> 8) & 0xf;
344 m = (finsn >> 4) & 0xf;
345 hx = tsk->thread.fpu.hard.fp_regs[n];
346 hy = tsk->thread.fpu.hard.fp_regs[m];
347 fpscr = tsk->thread.fpu.hard.fpscr;
348 prec = fpscr & FPSCR_DBL_PRECISION;
350 if ((fpscr & FPSCR_CAUSE_ERROR)
351 && (prec && ((hx & 0x7fffffff) < 0x00100000
352 || (hy & 0x7fffffff) < 0x00100000))) {
355 /* FPU error because of denormal (doubles) */
356 llx = ((long long)hx << 32)
357 | tsk->thread.fpu.hard.fp_regs[n + 1];
358 lly = ((long long)hy << 32)
359 | tsk->thread.fpu.hard.fp_regs[m + 1];
360 if ((finsn & 0xf00f) == 0xf000)
361 llx = float64_add(llx, lly);
363 llx = float64_sub(llx, lly);
364 tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
365 tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
366 } else if ((fpscr & FPSCR_CAUSE_ERROR)
367 && (!prec && ((hx & 0x7fffffff) < 0x00800000
368 || (hy & 0x7fffffff) < 0x00800000))) {
369 /* FPU error because of denormal (floats) */
370 if ((finsn & 0xf00f) == 0xf000)
371 hx = float32_add(hx, hy);
373 hx = float32_sub(hx, hy);
374 tsk->thread.fpu.hard.fp_regs[n] = hx;
380 } else if ((finsn & 0xf003) == 0xf003) {
382 struct task_struct *tsk = current;
387 n = (finsn >> 8) & 0xf;
388 m = (finsn >> 4) & 0xf;
389 hx = tsk->thread.fpu.hard.fp_regs[n];
390 hy = tsk->thread.fpu.hard.fp_regs[m];
391 fpscr = tsk->thread.fpu.hard.fpscr;
392 prec = fpscr & FPSCR_DBL_PRECISION;
394 if ((fpscr & FPSCR_CAUSE_ERROR)
395 && (prec && ((hx & 0x7fffffff) < 0x00100000
396 || (hy & 0x7fffffff) < 0x00100000))) {
399 /* FPU error because of denormal (doubles) */
400 llx = ((long long)hx << 32)
401 | tsk->thread.fpu.hard.fp_regs[n + 1];
402 lly = ((long long)hy << 32)
403 | tsk->thread.fpu.hard.fp_regs[m + 1];
405 llx = float64_div(llx, lly);
407 tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
408 tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
409 } else if ((fpscr & FPSCR_CAUSE_ERROR)
410 && (!prec && ((hx & 0x7fffffff) < 0x00800000
411 || (hy & 0x7fffffff) < 0x00800000))) {
412 /* FPU error because of denormal (floats) */
413 hx = float32_div(hx, hy);
414 tsk->thread.fpu.hard.fp_regs[n] = hx;
425 void float_raise(unsigned int flags)
427 fpu_exception_flags |= flags;
430 int float_rounding_mode(void)
432 struct task_struct *tsk = current;
433 int roundingMode = FPSCR_ROUNDING_MODE(tsk->thread.fpu.hard.fpscr);
437 BUILD_TRAP_HANDLER(fpu_error)
439 struct task_struct *tsk = current;
443 fpu_exception_flags = 0;
444 if (ieee_fpe_handler(regs)) {
445 tsk->thread.fpu.hard.fpscr &=
446 ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
447 tsk->thread.fpu.hard.fpscr |= fpu_exception_flags;
448 /* Set the FPSCR flag as well as cause bits - simply
449 * replicate the cause */
450 tsk->thread.fpu.hard.fpscr |= (fpu_exception_flags >> 10);
453 set_tsk_thread_flag(tsk, TIF_USEDFPU);
454 if ((((tsk->thread.fpu.hard.fpscr & FPSCR_ENABLE_MASK) >> 7) &
455 (fpu_exception_flags >> 2)) == 0) {
460 force_sig(SIGFPE, tsk);
463 BUILD_TRAP_HANDLER(fpu_state_restore)
465 struct task_struct *tsk = current;
469 if (!user_mode(regs)) {
470 printk(KERN_ERR "BUG: FPU is used in kernel mode.\n");
475 /* Using the FPU again. */
478 /* First time FPU user. */
482 set_tsk_thread_flag(tsk, TIF_USEDFPU);