2 * Copyright (C) 1999 Eddie C. Dost (ecd@atecom.com)
5 #include <linux/types.h>
6 #include <linux/sched.h>
8 #include <asm/uaccess.h>
11 #include <asm/sfp-machine.h>
12 #include <math-emu/double.h>
14 #define FLOATFUNC(x) extern int x(void *, void *, void *, void *)
70 #define OP31 0x1f /* 31 */
71 #define LFS 0x30 /* 48 */
72 #define LFSU 0x31 /* 49 */
73 #define LFD 0x32 /* 50 */
74 #define LFDU 0x33 /* 51 */
75 #define STFS 0x34 /* 52 */
76 #define STFSU 0x35 /* 53 */
77 #define STFD 0x36 /* 54 */
78 #define STFDU 0x37 /* 55 */
79 #define OP59 0x3b /* 59 */
80 #define OP63 0x3f /* 63 */
84 #define LFSX 0x217 /* 535 */
85 #define LFSUX 0x237 /* 567 */
86 #define LFDX 0x257 /* 599 */
87 #define LFDUX 0x277 /* 631 */
88 #define STFSX 0x297 /* 663 */
89 #define STFSUX 0x2b7 /* 695 */
90 #define STFDX 0x2d7 /* 727 */
91 #define STFDUX 0x2f7 /* 759 */
92 #define STFIWX 0x3d7 /* 983 */
96 #define FDIVS 0x012 /* 18 */
97 #define FSUBS 0x014 /* 20 */
98 #define FADDS 0x015 /* 21 */
99 #define FSQRTS 0x016 /* 22 */
100 #define FRES 0x018 /* 24 */
101 #define FMULS 0x019 /* 25 */
102 #define FRSQRTES 0x01a /* 26 */
103 #define FMSUBS 0x01c /* 28 */
104 #define FMADDS 0x01d /* 29 */
105 #define FNMSUBS 0x01e /* 30 */
106 #define FNMADDS 0x01f /* 31 */
110 #define FDIV 0x012 /* 18 */
111 #define FSUB 0x014 /* 20 */
112 #define FADD 0x015 /* 21 */
113 #define FSQRT 0x016 /* 22 */
114 #define FSEL 0x017 /* 23 */
115 #define FRE 0x018 /* 24 */
116 #define FMUL 0x019 /* 25 */
117 #define FRSQRTE 0x01a /* 26 */
118 #define FMSUB 0x01c /* 28 */
119 #define FMADD 0x01d /* 29 */
120 #define FNMSUB 0x01e /* 30 */
121 #define FNMADD 0x01f /* 31 */
124 #define FCMPU 0x000 /* 0 */
125 #define FRSP 0x00c /* 12 */
126 #define FCTIW 0x00e /* 14 */
127 #define FCTIWZ 0x00f /* 15 */
128 #define FCMPO 0x020 /* 32 */
129 #define MTFSB1 0x026 /* 38 */
130 #define FNEG 0x028 /* 40 */
131 #define MCRFS 0x040 /* 64 */
132 #define MTFSB0 0x046 /* 70 */
133 #define FMR 0x048 /* 72 */
134 #define MTFSFI 0x086 /* 134 */
135 #define FNABS 0x088 /* 136 */
136 #define FABS 0x108 /* 264 */
137 #define MFFS 0x247 /* 583 */
138 #define MTFSF 0x2c7 /* 711 */
157 #ifdef CONFIG_MATH_EMULATION
159 record_exception(struct pt_regs *regs, int eflag)
167 if (eflag & EFLAG_OVERFLOW)
169 if (eflag & EFLAG_UNDERFLOW)
171 if (eflag & EFLAG_DIVZERO)
173 if (eflag & EFLAG_INEXACT)
175 if (eflag & EFLAG_INVALID)
177 if (eflag & EFLAG_VXSNAN)
178 fpscr |= FPSCR_VXSNAN;
179 if (eflag & EFLAG_VXISI)
180 fpscr |= FPSCR_VXISI;
181 if (eflag & EFLAG_VXIDI)
182 fpscr |= FPSCR_VXIDI;
183 if (eflag & EFLAG_VXZDZ)
184 fpscr |= FPSCR_VXZDZ;
185 if (eflag & EFLAG_VXIMZ)
186 fpscr |= FPSCR_VXIMZ;
187 if (eflag & EFLAG_VXVC)
189 if (eflag & EFLAG_VXSOFT)
190 fpscr |= FPSCR_VXSOFT;
191 if (eflag & EFLAG_VXSQRT)
192 fpscr |= FPSCR_VXSQRT;
193 if (eflag & EFLAG_VXCVI)
194 fpscr |= FPSCR_VXCVI;
197 // fpscr &= ~(FPSCR_VX);
198 if (fpscr & (FPSCR_VXSNAN | FPSCR_VXISI | FPSCR_VXIDI |
199 FPSCR_VXZDZ | FPSCR_VXIMZ | FPSCR_VXVC |
200 FPSCR_VXSOFT | FPSCR_VXSQRT | FPSCR_VXCVI))
203 fpscr &= ~(FPSCR_FEX);
204 if (((fpscr & FPSCR_VX) && (fpscr & FPSCR_VE)) ||
205 ((fpscr & FPSCR_OX) && (fpscr & FPSCR_OE)) ||
206 ((fpscr & FPSCR_UX) && (fpscr & FPSCR_UE)) ||
207 ((fpscr & FPSCR_ZX) && (fpscr & FPSCR_ZE)) ||
208 ((fpscr & FPSCR_XX) && (fpscr & FPSCR_XE)))
213 return (fpscr & FPSCR_FEX) ? 1 : 0;
215 #endif /* CONFIG_MATH_EMULATION */
218 do_mathemu(struct pt_regs *regs)
220 void *op0 = 0, *op1 = 0, *op2 = 0, *op3 = 0;
221 unsigned long pc = regs->nip;
225 #ifdef CONFIG_MATH_EMULATION
226 int (*func)(void *, void *, void *, void *);
231 if (get_user(insn, (u32 *)pc))
234 #ifndef CONFIG_MATH_EMULATION
235 switch (insn >> 26) {
237 idx = (insn >> 16) & 0x1f;
238 sdisp = (insn & 0xffff);
239 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
240 op1 = (void *)((idx ? regs->gpr[idx] : 0) + sdisp);
241 lfd(op0, op1, op2, op3);
244 idx = (insn >> 16) & 0x1f;
245 sdisp = (insn & 0xffff);
246 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
247 op1 = (void *)((idx ? regs->gpr[idx] : 0) + sdisp);
248 lfd(op0, op1, op2, op3);
249 regs->gpr[idx] = (unsigned long)op1;
252 idx = (insn >> 16) & 0x1f;
253 sdisp = (insn & 0xffff);
254 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
255 op1 = (void *)((idx ? regs->gpr[idx] : 0) + sdisp);
256 stfd(op0, op1, op2, op3);
259 idx = (insn >> 16) & 0x1f;
260 sdisp = (insn & 0xffff);
261 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
262 op1 = (void *)((idx ? regs->gpr[idx] : 0) + sdisp);
263 stfd(op0, op1, op2, op3);
264 regs->gpr[idx] = (unsigned long)op1;
267 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
268 op1 = (void *)¤t->thread.TS_FPR((insn >> 11) & 0x1f);
269 fmr(op0, op1, op2, op3);
274 #else /* CONFIG_MATH_EMULATION */
275 switch (insn >> 26) {
276 case LFS: func = lfs; type = D; break;
277 case LFSU: func = lfs; type = DU; break;
278 case LFD: func = lfd; type = D; break;
279 case LFDU: func = lfd; type = DU; break;
280 case STFS: func = stfs; type = D; break;
281 case STFSU: func = stfs; type = DU; break;
282 case STFD: func = stfd; type = D; break;
283 case STFDU: func = stfd; type = DU; break;
286 switch ((insn >> 1) & 0x3ff) {
287 case LFSX: func = lfs; type = XE; break;
288 case LFSUX: func = lfs; type = XEU; break;
289 case LFDX: func = lfd; type = XE; break;
290 case LFDUX: func = lfd; type = XEU; break;
291 case STFSX: func = stfs; type = XE; break;
292 case STFSUX: func = stfs; type = XEU; break;
293 case STFDX: func = stfd; type = XE; break;
294 case STFDUX: func = stfd; type = XEU; break;
295 case STFIWX: func = stfiwx; type = XE; break;
302 switch ((insn >> 1) & 0x1f) {
303 case FDIVS: func = fdivs; type = AB; break;
304 case FSUBS: func = fsubs; type = AB; break;
305 case FADDS: func = fadds; type = AB; break;
306 case FSQRTS: func = fsqrts; type = XB; break;
307 case FRES: func = fres; type = XB; break;
308 case FMULS: func = fmuls; type = AC; break;
309 case FRSQRTES: func = frsqrtes;type = XB; break;
310 case FMSUBS: func = fmsubs; type = ABC; break;
311 case FMADDS: func = fmadds; type = ABC; break;
312 case FNMSUBS: func = fnmsubs; type = ABC; break;
313 case FNMADDS: func = fnmadds; type = ABC; break;
321 switch ((insn >> 1) & 0x1f) {
322 case FDIV: func = fdiv; type = AB; break;
323 case FSUB: func = fsub; type = AB; break;
324 case FADD: func = fadd; type = AB; break;
325 case FSQRT: func = fsqrt; type = XB; break;
326 case FRE: func = fre; type = XB; break;
327 case FSEL: func = fsel; type = ABC; break;
328 case FMUL: func = fmul; type = AC; break;
329 case FRSQRTE: func = frsqrte; type = XB; break;
330 case FMSUB: func = fmsub; type = ABC; break;
331 case FMADD: func = fmadd; type = ABC; break;
332 case FNMSUB: func = fnmsub; type = ABC; break;
333 case FNMADD: func = fnmadd; type = ABC; break;
340 switch ((insn >> 1) & 0x3ff) {
341 case FCMPU: func = fcmpu; type = XCR; break;
342 case FRSP: func = frsp; type = XB; break;
343 case FCTIW: func = fctiw; type = XB; break;
344 case FCTIWZ: func = fctiwz; type = XB; break;
345 case FCMPO: func = fcmpo; type = XCR; break;
346 case MTFSB1: func = mtfsb1; type = XCRB; break;
347 case FNEG: func = fneg; type = XB; break;
348 case MCRFS: func = mcrfs; type = XCRL; break;
349 case MTFSB0: func = mtfsb0; type = XCRB; break;
350 case FMR: func = fmr; type = XB; break;
351 case MTFSFI: func = mtfsfi; type = XCRI; break;
352 case FNABS: func = fnabs; type = XB; break;
353 case FABS: func = fabs; type = XB; break;
354 case MFFS: func = mffs; type = X; break;
355 case MTFSF: func = mtfsf; type = XFLB; break;
367 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
368 op1 = (void *)¤t->thread.TS_FPR((insn >> 16) & 0x1f);
369 op2 = (void *)¤t->thread.TS_FPR((insn >> 11) & 0x1f);
373 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
374 op1 = (void *)¤t->thread.TS_FPR((insn >> 16) & 0x1f);
375 op2 = (void *)¤t->thread.TS_FPR((insn >> 6) & 0x1f);
379 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
380 op1 = (void *)¤t->thread.TS_FPR((insn >> 16) & 0x1f);
381 op2 = (void *)¤t->thread.TS_FPR((insn >> 11) & 0x1f);
382 op3 = (void *)¤t->thread.TS_FPR((insn >> 6) & 0x1f);
386 idx = (insn >> 16) & 0x1f;
387 sdisp = (insn & 0xffff);
388 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
389 op1 = (void *)((idx ? regs->gpr[idx] : 0) + sdisp);
393 idx = (insn >> 16) & 0x1f;
397 sdisp = (insn & 0xffff);
398 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
399 op1 = (void *)(regs->gpr[idx] + sdisp);
403 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
407 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
408 op1 = (void *)¤t->thread.TS_FPR((insn >> 16) & 0x1f);
412 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
413 op1 = (void *)¤t->thread.TS_FPR((insn >> 11) & 0x1f);
417 idx = (insn >> 16) & 0x1f;
418 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
420 if (((insn >> 1) & 0x3ff) == STFIWX)
421 op1 = (void *)(regs->gpr[(insn >> 11) & 0x1f]);
425 op1 = (void *)(regs->gpr[idx] + regs->gpr[(insn >> 11) & 0x1f]);
431 idx = (insn >> 16) & 0x1f;
432 op0 = (void *)¤t->thread.TS_FPR((insn >> 21) & 0x1f);
433 op1 = (void *)((idx ? regs->gpr[idx] : 0)
434 + regs->gpr[(insn >> 11) & 0x1f]);
438 op0 = (void *)®s->ccr;
439 op1 = (void *)((insn >> 23) & 0x7);
440 op2 = (void *)¤t->thread.TS_FPR((insn >> 16) & 0x1f);
441 op3 = (void *)¤t->thread.TS_FPR((insn >> 11) & 0x1f);
445 op0 = (void *)®s->ccr;
446 op1 = (void *)((insn >> 23) & 0x7);
447 op2 = (void *)((insn >> 18) & 0x7);
451 op0 = (void *)((insn >> 21) & 0x1f);
455 op0 = (void *)((insn >> 23) & 0x7);
456 op1 = (void *)((insn >> 12) & 0xf);
460 op0 = (void *)((insn >> 17) & 0xff);
461 op1 = (void *)¤t->thread.TS_FPR((insn >> 11) & 0x1f);
468 eflag = func(op0, op1, op2, op3);
471 regs->ccr &= ~(0x0f000000);
472 regs->ccr |= (__FPU_FPSCR >> 4) & 0x0f000000;
475 trap = record_exception(regs, eflag);
482 regs->gpr[idx] = (unsigned long)op1;
488 #endif /* CONFIG_MATH_EMULATION */