2 * arch/sh/math-emu/math.c
4 * Copyright (C) 2006 Takashi YOSHII <takasi-y@ops.dti.ne.jp>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/sched.h>
14 #include <linux/signal.h>
15 #include <linux/perf_event.h>
17 #include <asm/system.h>
18 #include <asm/uaccess.h>
19 #include <asm/processor.h>
23 #include <math-emu/soft-fp.h>
24 #include <math-emu/single.h>
25 #include <math-emu/double.h>
27 #define FPUL (fregs->fpul)
28 #define FPSCR (fregs->fpscr)
29 #define FPSCR_RM (FPSCR&3)
30 #define FPSCR_DN ((FPSCR>>18)&1)
31 #define FPSCR_PR ((FPSCR>>19)&1)
32 #define FPSCR_SZ ((FPSCR>>20)&1)
33 #define FPSCR_FR ((FPSCR>>21)&1)
34 #define FPSCR_MASK 0x003fffffUL
36 #define BANK(n) (n^(FPSCR_FR?16:0))
37 #define FR ((unsigned long*)(fregs->fp_regs))
38 #define FR0 (FR[BANK(0)])
39 #define FRn (FR[BANK(n)])
40 #define FRm (FR[BANK(m)])
41 #define DR ((unsigned long long*)(fregs->fp_regs))
42 #define DRn (DR[BANK(n)/2])
43 #define DRm (DR[BANK(m)/2])
45 #define XREG(n) (n^16)
46 #define XFn (FR[BANK(XREG(n))])
47 #define XFm (FR[BANK(XREG(m))])
48 #define XDn (DR[BANK(XREG(n))/2])
49 #define XDm (DR[BANK(XREG(m))/2])
51 #define R0 (regs->regs[0])
52 #define Rn (regs->regs[n])
53 #define Rm (regs->regs[m])
55 #define WRITE(d,a) ({if(put_user(d, (typeof (d)*)a)) return -EFAULT;})
56 #define READ(d,a) ({if(get_user(d, (typeof (d)*)a)) return -EFAULT;})
58 #define PACK_S(r,f) FP_PACK_SP(&r,f)
59 #define UNPACK_S(f,r) FP_UNPACK_SP(f,&r)
61 {u32 t[2]; FP_PACK_DP(t,f); ((u32*)&r)[0]=t[1]; ((u32*)&r)[1]=t[0];}
62 #define UNPACK_D(f,r) \
63 {u32 t[2]; t[0]=((u32*)&r)[1]; t[1]=((u32*)&r)[0]; FP_UNPACK_DP(f,t);}
65 // 2 args instructions.
66 #define BOTH_PRmn(op,x) \
67 FP_DECL_EX; if(FPSCR_PR) op(D,x,DRm,DRn); else op(S,x,FRm,FRn);
69 #define CMP_X(SZ,R,M,N) do{ \
70 FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
71 UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
72 FP_CMP_##SZ(R, Fn, Fm, 2); }while(0)
73 #define EQ_X(SZ,R,M,N) do{ \
74 FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
75 UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
76 FP_CMP_EQ_##SZ(R, Fn, Fm); }while(0)
77 #define CMP(OP) ({ int r; BOTH_PRmn(OP##_X,r); r; })
80 fcmp_gt(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
91 fcmp_eq(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
93 if (CMP(CMP
/*EQ*/) == 0)
100 #define ARITH_X(SZ,OP,M,N) do{ \
101 FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); FP_DECL_##SZ(Fr); \
102 UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
103 FP_##OP##_##SZ(Fr, Fn, Fm); \
104 PACK_##SZ(N, Fr); }while(0)
107 fadd(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
109 BOTH_PRmn(ARITH_X
, ADD
);
114 fsub(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
116 BOTH_PRmn(ARITH_X
, SUB
);
121 fmul(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
123 BOTH_PRmn(ARITH_X
, MUL
);
128 fdiv(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
130 BOTH_PRmn(ARITH_X
, DIV
);
135 fmac(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
146 FP_MUL_S(Ft
, Fm
, F0
);
147 FP_ADD_S(Fr
, Fn
, Ft
);
152 // to process fmov's extension (odd n for DR access XD).
153 #define FMOV_EXT(x) if(x&1) x+=16-1
156 fmov_idx_reg(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
,
161 READ(FRn
, Rm
+ R0
+ 4);
172 fmov_mem_reg(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
,
188 fmov_inc_reg(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
,
206 fmov_reg_idx(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
,
211 WRITE(FRm
, Rn
+ R0
+ 4);
222 fmov_reg_mem(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
,
238 fmov_reg_dec(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
,
256 fmov_reg_reg(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
,
271 fnop_mn(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int m
, int n
)
276 // 1 arg instructions.
277 #define NOTYETn(i) static int i(struct sh_fpu_soft_struct *fregs, int n) \
278 { printk( #i " not yet done.\n"); return 0; }
286 #define EMU_FLOAT_X(SZ,N) do { \
288 FP_FROM_INT_##SZ(Fn, FPUL, 32, int); \
289 PACK_##SZ(N, Fn); }while(0)
290 static int ffloat(struct sh_fpu_soft_struct
*fregs
, int n
)
302 #define EMU_FTRC_X(SZ,N) do { \
304 UNPACK_##SZ(Fn, N); \
305 FP_TO_INT_##SZ(FPUL, Fn, 32, 1); }while(0)
306 static int ftrc(struct sh_fpu_soft_struct
*fregs
, int n
)
318 static int fcnvsd(struct sh_fpu_soft_struct
*fregs
, int n
)
324 FP_CONV(D
, S
, 2, 1, Fr
, Fn
);
329 static int fcnvds(struct sh_fpu_soft_struct
*fregs
, int n
)
335 FP_CONV(S
, D
, 1, 2, Fr
, Fn
);
340 static int fxchg(struct sh_fpu_soft_struct
*fregs
, int flag
)
346 static int fsts(struct sh_fpu_soft_struct
*fregs
, int n
)
352 static int flds(struct sh_fpu_soft_struct
*fregs
, int n
)
358 static int fneg(struct sh_fpu_soft_struct
*fregs
, int n
)
360 FRn
^= (1 << (_FP_W_TYPE_SIZE
- 1));
364 static int fabs(struct sh_fpu_soft_struct
*fregs
, int n
)
366 FRn
&= ~(1 << (_FP_W_TYPE_SIZE
- 1));
370 static int fld0(struct sh_fpu_soft_struct
*fregs
, int n
)
376 static int fld1(struct sh_fpu_soft_struct
*fregs
, int n
)
378 FRn
= (_FP_EXPBIAS_S
<< (_FP_FRACBITS_S
- 1));
382 static int fnop_n(struct sh_fpu_soft_struct
*fregs
, int n
)
387 /// Instruction decoders.
389 static int id_fxfd(struct sh_fpu_soft_struct
*, int);
390 static int id_fnxd(struct sh_fpu_soft_struct
*, struct pt_regs
*, int, int);
392 static int (*fnxd
[])(struct sh_fpu_soft_struct
*, int) = {
393 fsts
, flds
, ffloat
, ftrc
, fneg
, fabs
, fsqrt
, fsrra
,
394 fld0
, fld1
, fcnvsd
, fcnvds
, fnop_n
, fnop_n
, fipr
, id_fxfd
397 static int (*fnmx
[])(struct sh_fpu_soft_struct
*, struct pt_regs
*, int, int) = {
398 fadd
, fsub
, fmul
, fdiv
, fcmp_eq
, fcmp_gt
, fmov_idx_reg
, fmov_reg_idx
,
399 fmov_mem_reg
, fmov_inc_reg
, fmov_reg_mem
, fmov_reg_dec
,
400 fmov_reg_reg
, id_fnxd
, fmac
, fnop_mn
};
402 static int id_fxfd(struct sh_fpu_soft_struct
*fregs
, int x
)
404 const int flag
[] = { FPSCR_SZ
, FPSCR_PR
, FPSCR_FR
, 0 };
407 fxchg(fregs
, flag
[x
>> 2]);
419 id_fnxd(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, int x
, int n
)
421 return (fnxd
[x
])(fregs
, n
);
425 id_fnmx(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, u16 code
)
427 int n
= (code
>> 8) & 0xf, m
= (code
>> 4) & 0xf, x
= code
& 0xf;
428 return (fnmx
[x
])(fregs
, regs
, m
, n
);
432 id_sys(struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
, u16 code
)
434 int n
= ((code
>> 8) & 0xf);
435 unsigned long *reg
= (code
& 0x0010) ? &FPUL
: &FPSCR
;
437 switch (code
& 0xf0ff) {
463 static int fpu_emulate(u16 code
, struct sh_fpu_soft_struct
*fregs
, struct pt_regs
*regs
)
465 if ((code
& 0xf000) == 0xf000)
466 return id_fnmx(fregs
, regs
, code
);
468 return id_sys(fregs
, regs
, code
);
472 * denormal_to_double - Given denormalized float number,
475 * @fpu: Pointer to sh_fpu_soft structure
476 * @n: Index to FP register
478 static void denormal_to_double(struct sh_fpu_soft_struct
*fpu
, int n
)
480 unsigned long du
, dl
;
481 unsigned long x
= fpu
->fpul
;
482 int exp
= 1023 - 126;
484 if (x
!= 0 && (x
& 0x7f800000) == 0) {
485 du
= (x
& 0x80000000);
486 while ((x
& 0x00800000) == 0) {
491 du
|= (exp
<< 20) | (x
>> 3);
494 fpu
->fp_regs
[n
] = du
;
495 fpu
->fp_regs
[n
+1] = dl
;
500 * ieee_fpe_handler - Handle denormalized number exception
502 * @regs: Pointer to register structure
504 * Returns 1 when it's handled (should not cause exception).
506 static int ieee_fpe_handler(struct pt_regs
*regs
)
508 unsigned short insn
= *(unsigned short *)regs
->pc
;
509 unsigned short finsn
;
510 unsigned long nextpc
;
519 (nib
[0] == 0x4 && nib
[2] == 0x0 && nib
[3] == 0xb)) /* bsr & jsr */
520 regs
->pr
= regs
->pc
+ 4;
522 if (nib
[0] == 0xa || nib
[0] == 0xb) { /* bra & bsr */
523 nextpc
= regs
->pc
+ 4 + ((short) ((insn
& 0xfff) << 4) >> 3);
524 finsn
= *(unsigned short *) (regs
->pc
+ 2);
525 } else if (nib
[0] == 0x8 && nib
[1] == 0xd) { /* bt/s */
527 nextpc
= regs
->pc
+ 4 + ((char) (insn
& 0xff) << 1);
529 nextpc
= regs
->pc
+ 4;
530 finsn
= *(unsigned short *) (regs
->pc
+ 2);
531 } else if (nib
[0] == 0x8 && nib
[1] == 0xf) { /* bf/s */
533 nextpc
= regs
->pc
+ 4;
535 nextpc
= regs
->pc
+ 4 + ((char) (insn
& 0xff) << 1);
536 finsn
= *(unsigned short *) (regs
->pc
+ 2);
537 } else if (nib
[0] == 0x4 && nib
[3] == 0xb &&
538 (nib
[2] == 0x0 || nib
[2] == 0x2)) { /* jmp & jsr */
539 nextpc
= regs
->regs
[nib
[1]];
540 finsn
= *(unsigned short *) (regs
->pc
+ 2);
541 } else if (nib
[0] == 0x0 && nib
[3] == 0x3 &&
542 (nib
[2] == 0x0 || nib
[2] == 0x2)) { /* braf & bsrf */
543 nextpc
= regs
->pc
+ 4 + regs
->regs
[nib
[1]];
544 finsn
= *(unsigned short *) (regs
->pc
+ 2);
545 } else if (insn
== 0x000b) { /* rts */
547 finsn
= *(unsigned short *) (regs
->pc
+ 2);
549 nextpc
= regs
->pc
+ 2;
553 if ((finsn
& 0xf1ff) == 0xf0ad) { /* fcnvsd */
554 struct task_struct
*tsk
= current
;
556 if ((tsk
->thread
.xstate
->softfpu
.fpscr
& (1 << 17))) {
558 denormal_to_double (&tsk
->thread
.xstate
->softfpu
,
560 tsk
->thread
.xstate
->softfpu
.fpscr
&=
561 ~(FPSCR_CAUSE_MASK
| FPSCR_FLAG_MASK
);
562 task_thread_info(tsk
)->status
|= TS_USEDFPU
;
564 info
.si_signo
= SIGFPE
;
566 info
.si_code
= FPE_FLTINV
;
567 info
.si_addr
= (void __user
*)regs
->pc
;
568 force_sig_info(SIGFPE
, &info
, tsk
);
578 asmlinkage
void do_fpu_error(unsigned long r4
, unsigned long r5
,
579 unsigned long r6
, unsigned long r7
,
582 struct task_struct
*tsk
= current
;
585 if (ieee_fpe_handler (®s
))
589 info
.si_signo
= SIGFPE
;
591 info
.si_code
= FPE_FLTINV
;
592 info
.si_addr
= (void __user
*)regs
.pc
;
593 force_sig_info(SIGFPE
, &info
, tsk
);
597 * fpu_init - Initialize FPU registers
598 * @fpu: Pointer to software emulated FPU registers.
600 static void fpu_init(struct sh_fpu_soft_struct
*fpu
)
604 fpu
->fpscr
= FPSCR_INIT
;
607 for (i
= 0; i
< 16; i
++) {
614 * do_fpu_inst - Handle reserved instructions for FPU emulation
615 * @inst: instruction code.
616 * @regs: registers on stack.
618 int do_fpu_inst(unsigned short inst
, struct pt_regs
*regs
)
620 struct task_struct
*tsk
= current
;
621 struct sh_fpu_soft_struct
*fpu
= &(tsk
->thread
.xstate
->softfpu
);
623 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, 0, regs
, 0);
625 if (!(task_thread_info(tsk
)->status
& TS_USEDFPU
)) {
626 /* initialize once. */
628 task_thread_info(tsk
)->status
|= TS_USEDFPU
;
631 return fpu_emulate(inst
, fpu
, regs
);