cp1emu.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * cp1emu.c: a MIPS coprocessor 1 (fpu) instruction emulator
  3. *
  4. * MIPS floating point support
  5. * Copyright (C) 1994-2000 Algorithmics Ltd.
  6. * http://www.algor.co.uk
  7. *
  8. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  9. * Copyright (C) 2000 MIPS Technologies, Inc.
  10. *
  11. * This program is free software; you can distribute it and/or modify it
  12. * under the terms of the GNU General Public License (Version 2) as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. * for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  23. *
  24. * A complete emulator for MIPS coprocessor 1 instructions. This is
  25. * required for #float(switch) or #float(trap), where it catches all
  26. * COP1 instructions via the "CoProcessor Unusable" exception.
  27. *
  28. * More surprisingly it is also required for #float(ieee), to help out
  29. * the hardware fpu at the boundaries of the IEEE-754 representation
  30. * (denormalised values, infinities, underflow, etc). It is made
  31. * quite nasty because emulation of some non-COP1 instructions is
  32. * required, e.g. in branch delay slots.
  33. *
  34. * Note if you know that you won't have an fpu, then you'll get much
  35. * better performance by compiling with -msoft-float!
  36. */
  37. #include <linux/sched.h>
  38. #include <asm/inst.h>
  39. #include <asm/bootinfo.h>
  40. #include <asm/processor.h>
  41. #include <asm/ptrace.h>
  42. #include <asm/signal.h>
  43. #include <asm/mipsregs.h>
  44. #include <asm/fpu_emulator.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/branch.h>
  47. #include "ieee754.h"
  48. #include "dsemul.h"
  49. /* Strap kernel emulator for full MIPS IV emulation */
  50. #ifdef __mips
  51. #undef __mips
  52. #endif
  53. #define __mips 4
  54. /* Function which emulates a floating point instruction. */
  55. static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
  56. mips_instruction);
  57. #if __mips >= 4 && __mips != 32
  58. static int fpux_emu(struct pt_regs *,
  59. struct mips_fpu_struct *, mips_instruction);
  60. #endif
  61. /* Further private data for which no space exists in mips_fpu_struct */
  62. struct mips_fpu_emulator_stats fpuemustats;
  63. /* Control registers */
  64. #define FPCREG_RID 0 /* $0 = revision id */
  65. #define FPCREG_CSR 31 /* $31 = csr */
  66. /* Convert Mips rounding mode (0..3) to IEEE library modes. */
  67. static const unsigned char ieee_rm[4] = {
  68. [FPU_CSR_RN] = IEEE754_RN,
  69. [FPU_CSR_RZ] = IEEE754_RZ,
  70. [FPU_CSR_RU] = IEEE754_RU,
  71. [FPU_CSR_RD] = IEEE754_RD,
  72. };
  73. /* Convert IEEE library modes to Mips rounding mode (0..3). */
  74. static const unsigned char mips_rm[4] = {
  75. [IEEE754_RN] = FPU_CSR_RN,
  76. [IEEE754_RZ] = FPU_CSR_RZ,
  77. [IEEE754_RD] = FPU_CSR_RD,
  78. [IEEE754_RU] = FPU_CSR_RU,
  79. };
  80. #if __mips >= 4
  81. /* convert condition code register number to csr bit */
  82. static const unsigned int fpucondbit[8] = {
  83. FPU_CSR_COND0,
  84. FPU_CSR_COND1,
  85. FPU_CSR_COND2,
  86. FPU_CSR_COND3,
  87. FPU_CSR_COND4,
  88. FPU_CSR_COND5,
  89. FPU_CSR_COND6,
  90. FPU_CSR_COND7
  91. };
  92. #endif
  93. /*
  94. * Redundant with logic already in kernel/branch.c,
  95. * embedded in compute_return_epc. At some point,
  96. * a single subroutine should be used across both
  97. * modules.
  98. */
  99. static int isBranchInstr(mips_instruction * i)
  100. {
  101. switch (MIPSInst_OPCODE(*i)) {
  102. case spec_op:
  103. switch (MIPSInst_FUNC(*i)) {
  104. case jalr_op:
  105. case jr_op:
  106. return 1;
  107. }
  108. break;
  109. case bcond_op:
  110. switch (MIPSInst_RT(*i)) {
  111. case bltz_op:
  112. case bgez_op:
  113. case bltzl_op:
  114. case bgezl_op:
  115. case bltzal_op:
  116. case bgezal_op:
  117. case bltzall_op:
  118. case bgezall_op:
  119. return 1;
  120. }
  121. break;
  122. case j_op:
  123. case jal_op:
  124. case jalx_op:
  125. case beq_op:
  126. case bne_op:
  127. case blez_op:
  128. case bgtz_op:
  129. case beql_op:
  130. case bnel_op:
  131. case blezl_op:
  132. case bgtzl_op:
  133. return 1;
  134. case cop0_op:
  135. case cop1_op:
  136. case cop2_op:
  137. case cop1x_op:
  138. if (MIPSInst_RS(*i) == bc_op)
  139. return 1;
  140. break;
  141. }
  142. return 0;
  143. }
  144. /*
  145. * In the Linux kernel, we support selection of FPR format on the
  146. * basis of the Status.FR bit. This does imply that, if a full 32
  147. * FPRs are desired, there needs to be a flip-flop that can be written
  148. * to one at that bit position. In any case, O32 MIPS ABI uses
  149. * only the even FPRs (Status.FR = 0).
  150. */
  151. #define CP0_STATUS_FR_SUPPORT
  152. #ifdef CP0_STATUS_FR_SUPPORT
  153. #define FR_BIT ST0_FR
  154. #else
  155. #define FR_BIT 0
  156. #endif
  157. #define SIFROMREG(si,x) ((si) = \
  158. (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
  159. (int)ctx->fpr[x] : \
  160. (int)(ctx->fpr[x & ~1] >> 32 ))
  161. #define SITOREG(si,x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] = \
  162. (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
  163. ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
  164. ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
  165. #define DIFROMREG(di,x) ((di) = \
  166. ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)])
  167. #define DITOREG(di,x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] \
  168. = (di))
  169. #define SPFROMREG(sp,x) SIFROMREG((sp).bits,x)
  170. #define SPTOREG(sp,x) SITOREG((sp).bits,x)
  171. #define DPFROMREG(dp,x) DIFROMREG((dp).bits,x)
  172. #define DPTOREG(dp,x) DITOREG((dp).bits,x)
  173. /*
  174. * Emulate the single floating point instruction pointed at by EPC.
  175. * Two instructions if the instruction is in a branch delay slot.
  176. */
  177. static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx)
  178. {
  179. mips_instruction ir;
  180. void * emulpc, *contpc;
  181. unsigned int cond;
  182. if (get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) {
  183. fpuemustats.errors++;
  184. return SIGBUS;
  185. }
  186. /* XXX NEC Vr54xx bug workaround */
  187. if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
  188. xcp->cp0_cause &= ~CAUSEF_BD;
  189. if (xcp->cp0_cause & CAUSEF_BD) {
  190. /*
  191. * The instruction to be emulated is in a branch delay slot
  192. * which means that we have to emulate the branch instruction
  193. * BEFORE we do the cop1 instruction.
  194. *
  195. * This branch could be a COP1 branch, but in that case we
  196. * would have had a trap for that instruction, and would not
  197. * come through this route.
  198. *
  199. * Linux MIPS branch emulator operates on context, updating the
  200. * cp0_epc.
  201. */
  202. emulpc = (void *) (xcp->cp0_epc + 4); /* Snapshot emulation target */
  203. if (__compute_return_epc(xcp)) {
  204. #ifdef CP1DBG
  205. printk("failed to emulate branch at %p\n",
  206. (void *) (xcp->cp0_epc));
  207. #endif
  208. return SIGILL;
  209. }
  210. if (get_user(ir, (mips_instruction __user *) emulpc)) {
  211. fpuemustats.errors++;
  212. return SIGBUS;
  213. }
  214. /* __compute_return_epc() will have updated cp0_epc */
  215. contpc = (void *) xcp->cp0_epc;
  216. /* In order not to confuse ptrace() et al, tweak context */
  217. xcp->cp0_epc = (unsigned long) emulpc - 4;
  218. } else {
  219. emulpc = (void *) xcp->cp0_epc;
  220. contpc = (void *) (xcp->cp0_epc + 4);
  221. }
  222. emul:
  223. fpuemustats.emulated++;
  224. switch (MIPSInst_OPCODE(ir)) {
  225. case ldc1_op:{
  226. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  227. MIPSInst_SIMM(ir));
  228. u64 val;
  229. fpuemustats.loads++;
  230. if (get_user(val, va)) {
  231. fpuemustats.errors++;
  232. return SIGBUS;
  233. }
  234. DITOREG(val, MIPSInst_RT(ir));
  235. break;
  236. }
  237. case sdc1_op:{
  238. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  239. MIPSInst_SIMM(ir));
  240. u64 val;
  241. fpuemustats.stores++;
  242. DIFROMREG(val, MIPSInst_RT(ir));
  243. if (put_user(val, va)) {
  244. fpuemustats.errors++;
  245. return SIGBUS;
  246. }
  247. break;
  248. }
  249. case lwc1_op:{
  250. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  251. MIPSInst_SIMM(ir));
  252. u32 val;
  253. fpuemustats.loads++;
  254. if (get_user(val, va)) {
  255. fpuemustats.errors++;
  256. return SIGBUS;
  257. }
  258. SITOREG(val, MIPSInst_RT(ir));
  259. break;
  260. }
  261. case swc1_op:{
  262. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  263. MIPSInst_SIMM(ir));
  264. u32 val;
  265. fpuemustats.stores++;
  266. SIFROMREG(val, MIPSInst_RT(ir));
  267. if (put_user(val, va)) {
  268. fpuemustats.errors++;
  269. return SIGBUS;
  270. }
  271. break;
  272. }
  273. case cop1_op:
  274. switch (MIPSInst_RS(ir)) {
  275. #if defined(__mips64)
  276. case dmfc_op:
  277. /* copregister fs -> gpr[rt] */
  278. if (MIPSInst_RT(ir) != 0) {
  279. DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  280. MIPSInst_RD(ir));
  281. }
  282. break;
  283. case dmtc_op:
  284. /* copregister fs <- rt */
  285. DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  286. break;
  287. #endif
  288. case mfc_op:
  289. /* copregister rd -> gpr[rt] */
  290. if (MIPSInst_RT(ir) != 0) {
  291. SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  292. MIPSInst_RD(ir));
  293. }
  294. break;
  295. case mtc_op:
  296. /* copregister rd <- rt */
  297. SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  298. break;
  299. case cfc_op:{
  300. /* cop control register rd -> gpr[rt] */
  301. u32 value;
  302. if (ir == CP1UNDEF) {
  303. return do_dsemulret(xcp);
  304. }
  305. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  306. value = ctx->fcr31;
  307. value = (value & ~0x3) | mips_rm[value & 0x3];
  308. #ifdef CSRTRACE
  309. printk("%p gpr[%d]<-csr=%08x\n",
  310. (void *) (xcp->cp0_epc),
  311. MIPSInst_RT(ir), value);
  312. #endif
  313. }
  314. else if (MIPSInst_RD(ir) == FPCREG_RID)
  315. value = 0;
  316. else
  317. value = 0;
  318. if (MIPSInst_RT(ir))
  319. xcp->regs[MIPSInst_RT(ir)] = value;
  320. break;
  321. }
  322. case ctc_op:{
  323. /* copregister rd <- rt */
  324. u32 value;
  325. if (MIPSInst_RT(ir) == 0)
  326. value = 0;
  327. else
  328. value = xcp->regs[MIPSInst_RT(ir)];
  329. /* we only have one writable control reg
  330. */
  331. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  332. #ifdef CSRTRACE
  333. printk("%p gpr[%d]->csr=%08x\n",
  334. (void *) (xcp->cp0_epc),
  335. MIPSInst_RT(ir), value);
  336. #endif
  337. value &= (FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
  338. ctx->fcr31 &= ~(FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
  339. /* convert to ieee library modes */
  340. ctx->fcr31 |= (value & ~0x3) | ieee_rm[value & 0x3];
  341. }
  342. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  343. return SIGFPE;
  344. }
  345. break;
  346. }
  347. case bc_op:{
  348. int likely = 0;
  349. if (xcp->cp0_cause & CAUSEF_BD)
  350. return SIGILL;
  351. #if __mips >= 4
  352. cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
  353. #else
  354. cond = ctx->fcr31 & FPU_CSR_COND;
  355. #endif
  356. switch (MIPSInst_RT(ir) & 3) {
  357. case bcfl_op:
  358. likely = 1;
  359. case bcf_op:
  360. cond = !cond;
  361. break;
  362. case bctl_op:
  363. likely = 1;
  364. case bct_op:
  365. break;
  366. default:
  367. /* thats an illegal instruction */
  368. return SIGILL;
  369. }
  370. xcp->cp0_cause |= CAUSEF_BD;
  371. if (cond) {
  372. /* branch taken: emulate dslot
  373. * instruction
  374. */
  375. xcp->cp0_epc += 4;
  376. contpc = (void *)
  377. (xcp->cp0_epc +
  378. (MIPSInst_SIMM(ir) << 2));
  379. if (get_user(ir,
  380. (mips_instruction __user *) xcp->cp0_epc)) {
  381. fpuemustats.errors++;
  382. return SIGBUS;
  383. }
  384. switch (MIPSInst_OPCODE(ir)) {
  385. case lwc1_op:
  386. case swc1_op:
  387. #if (__mips >= 2 || defined(__mips64))
  388. case ldc1_op:
  389. case sdc1_op:
  390. #endif
  391. case cop1_op:
  392. #if __mips >= 4 && __mips != 32
  393. case cop1x_op:
  394. #endif
  395. /* its one of ours */
  396. goto emul;
  397. #if __mips >= 4
  398. case spec_op:
  399. if (MIPSInst_FUNC(ir) == movc_op)
  400. goto emul;
  401. break;
  402. #endif
  403. }
  404. /*
  405. * Single step the non-cp1
  406. * instruction in the dslot
  407. */
  408. return mips_dsemul(xcp, ir, (unsigned long) contpc);
  409. }
  410. else {
  411. /* branch not taken */
  412. if (likely) {
  413. /*
  414. * branch likely nullifies
  415. * dslot if not taken
  416. */
  417. xcp->cp0_epc += 4;
  418. contpc += 4;
  419. /*
  420. * else continue & execute
  421. * dslot as normal insn
  422. */
  423. }
  424. }
  425. break;
  426. }
  427. default:
  428. if (!(MIPSInst_RS(ir) & 0x10))
  429. return SIGILL;
  430. {
  431. int sig;
  432. /* a real fpu computation instruction */
  433. if ((sig = fpu_emu(xcp, ctx, ir)))
  434. return sig;
  435. }
  436. }
  437. break;
  438. #if __mips >= 4 && __mips != 32
  439. case cop1x_op:{
  440. int sig;
  441. if ((sig = fpux_emu(xcp, ctx, ir)))
  442. return sig;
  443. break;
  444. }
  445. #endif
  446. #if __mips >= 4
  447. case spec_op:
  448. if (MIPSInst_FUNC(ir) != movc_op)
  449. return SIGILL;
  450. cond = fpucondbit[MIPSInst_RT(ir) >> 2];
  451. if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
  452. xcp->regs[MIPSInst_RD(ir)] =
  453. xcp->regs[MIPSInst_RS(ir)];
  454. break;
  455. #endif
  456. default:
  457. return SIGILL;
  458. }
  459. /* we did it !! */
  460. xcp->cp0_epc = (unsigned long) contpc;
  461. xcp->cp0_cause &= ~CAUSEF_BD;
  462. return 0;
  463. }
  464. /*
  465. * Conversion table from MIPS compare ops 48-63
  466. * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
  467. */
  468. static const unsigned char cmptab[8] = {
  469. 0, /* cmp_0 (sig) cmp_sf */
  470. IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
  471. IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
  472. IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
  473. IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
  474. IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
  475. IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
  476. IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
  477. };
  478. #if __mips >= 4 && __mips != 32
  479. /*
  480. * Additional MIPS4 instructions
  481. */
  482. #define DEF3OP(name, p, f1, f2, f3) \
  483. static ieee754##p fpemu_##p##_##name (ieee754##p r, ieee754##p s, \
  484. ieee754##p t) \
  485. { \
  486. struct _ieee754_csr ieee754_csr_save; \
  487. s = f1 (s, t); \
  488. ieee754_csr_save = ieee754_csr; \
  489. s = f2 (s, r); \
  490. ieee754_csr_save.cx |= ieee754_csr.cx; \
  491. ieee754_csr_save.sx |= ieee754_csr.sx; \
  492. s = f3 (s); \
  493. ieee754_csr.cx |= ieee754_csr_save.cx; \
  494. ieee754_csr.sx |= ieee754_csr_save.sx; \
  495. return s; \
  496. }
  497. static ieee754dp fpemu_dp_recip(ieee754dp d)
  498. {
  499. return ieee754dp_div(ieee754dp_one(0), d);
  500. }
  501. static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
  502. {
  503. return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
  504. }
  505. static ieee754sp fpemu_sp_recip(ieee754sp s)
  506. {
  507. return ieee754sp_div(ieee754sp_one(0), s);
  508. }
  509. static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
  510. {
  511. return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
  512. }
  513. DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add,);
  514. DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub,);
  515. DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
  516. DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
  517. DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add,);
  518. DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub,);
  519. DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
  520. DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
  521. static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  522. mips_instruction ir)
  523. {
  524. unsigned rcsr = 0; /* resulting csr */
  525. fpuemustats.cp1xops++;
  526. switch (MIPSInst_FMA_FFMT(ir)) {
  527. case s_fmt:{ /* 0 */
  528. ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
  529. ieee754sp fd, fr, fs, ft;
  530. u32 __user *va;
  531. u32 val;
  532. switch (MIPSInst_FUNC(ir)) {
  533. case lwxc1_op:
  534. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  535. xcp->regs[MIPSInst_FT(ir)]);
  536. fpuemustats.loads++;
  537. if (get_user(val, va)) {
  538. fpuemustats.errors++;
  539. return SIGBUS;
  540. }
  541. SITOREG(val, MIPSInst_FD(ir));
  542. break;
  543. case swxc1_op:
  544. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  545. xcp->regs[MIPSInst_FT(ir)]);
  546. fpuemustats.stores++;
  547. SIFROMREG(val, MIPSInst_FS(ir));
  548. if (put_user(val, va)) {
  549. fpuemustats.errors++;
  550. return SIGBUS;
  551. }
  552. break;
  553. case madd_s_op:
  554. handler = fpemu_sp_madd;
  555. goto scoptop;
  556. case msub_s_op:
  557. handler = fpemu_sp_msub;
  558. goto scoptop;
  559. case nmadd_s_op:
  560. handler = fpemu_sp_nmadd;
  561. goto scoptop;
  562. case nmsub_s_op:
  563. handler = fpemu_sp_nmsub;
  564. goto scoptop;
  565. scoptop:
  566. SPFROMREG(fr, MIPSInst_FR(ir));
  567. SPFROMREG(fs, MIPSInst_FS(ir));
  568. SPFROMREG(ft, MIPSInst_FT(ir));
  569. fd = (*handler) (fr, fs, ft);
  570. SPTOREG(fd, MIPSInst_FD(ir));
  571. copcsr:
  572. if (ieee754_cxtest(IEEE754_INEXACT))
  573. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  574. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  575. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  576. if (ieee754_cxtest(IEEE754_OVERFLOW))
  577. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  578. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  579. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  580. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  581. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  582. /*printk ("SIGFPE: fpu csr = %08x\n",
  583. ctx->fcr31); */
  584. return SIGFPE;
  585. }
  586. break;
  587. default:
  588. return SIGILL;
  589. }
  590. break;
  591. }
  592. case d_fmt:{ /* 1 */
  593. ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
  594. ieee754dp fd, fr, fs, ft;
  595. u64 __user *va;
  596. u64 val;
  597. switch (MIPSInst_FUNC(ir)) {
  598. case ldxc1_op:
  599. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  600. xcp->regs[MIPSInst_FT(ir)]);
  601. fpuemustats.loads++;
  602. if (get_user(val, va)) {
  603. fpuemustats.errors++;
  604. return SIGBUS;
  605. }
  606. DITOREG(val, MIPSInst_FD(ir));
  607. break;
  608. case sdxc1_op:
  609. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  610. xcp->regs[MIPSInst_FT(ir)]);
  611. fpuemustats.stores++;
  612. DIFROMREG(val, MIPSInst_FS(ir));
  613. if (put_user(val, va)) {
  614. fpuemustats.errors++;
  615. return SIGBUS;
  616. }
  617. break;
  618. case madd_d_op:
  619. handler = fpemu_dp_madd;
  620. goto dcoptop;
  621. case msub_d_op:
  622. handler = fpemu_dp_msub;
  623. goto dcoptop;
  624. case nmadd_d_op:
  625. handler = fpemu_dp_nmadd;
  626. goto dcoptop;
  627. case nmsub_d_op:
  628. handler = fpemu_dp_nmsub;
  629. goto dcoptop;
  630. dcoptop:
  631. DPFROMREG(fr, MIPSInst_FR(ir));
  632. DPFROMREG(fs, MIPSInst_FS(ir));
  633. DPFROMREG(ft, MIPSInst_FT(ir));
  634. fd = (*handler) (fr, fs, ft);
  635. DPTOREG(fd, MIPSInst_FD(ir));
  636. goto copcsr;
  637. default:
  638. return SIGILL;
  639. }
  640. break;
  641. }
  642. case 0x7: /* 7 */
  643. if (MIPSInst_FUNC(ir) != pfetch_op) {
  644. return SIGILL;
  645. }
  646. /* ignore prefx operation */
  647. break;
  648. default:
  649. return SIGILL;
  650. }
  651. return 0;
  652. }
  653. #endif
  654. /*
  655. * Emulate a single COP1 arithmetic instruction.
  656. */
  657. static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  658. mips_instruction ir)
  659. {
  660. int rfmt; /* resulting format */
  661. unsigned rcsr = 0; /* resulting csr */
  662. unsigned cond;
  663. union {
  664. ieee754dp d;
  665. ieee754sp s;
  666. int w;
  667. #ifdef __mips64
  668. s64 l;
  669. #endif
  670. } rv; /* resulting value */
  671. fpuemustats.cp1ops++;
  672. switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
  673. case s_fmt:{ /* 0 */
  674. union {
  675. ieee754sp(*b) (ieee754sp, ieee754sp);
  676. ieee754sp(*u) (ieee754sp);
  677. } handler;
  678. switch (MIPSInst_FUNC(ir)) {
  679. /* binary ops */
  680. case fadd_op:
  681. handler.b = ieee754sp_add;
  682. goto scopbop;
  683. case fsub_op:
  684. handler.b = ieee754sp_sub;
  685. goto scopbop;
  686. case fmul_op:
  687. handler.b = ieee754sp_mul;
  688. goto scopbop;
  689. case fdiv_op:
  690. handler.b = ieee754sp_div;
  691. goto scopbop;
  692. /* unary ops */
  693. #if __mips >= 2 || defined(__mips64)
  694. case fsqrt_op:
  695. handler.u = ieee754sp_sqrt;
  696. goto scopuop;
  697. #endif
  698. #if __mips >= 4 && __mips != 32
  699. case frsqrt_op:
  700. handler.u = fpemu_sp_rsqrt;
  701. goto scopuop;
  702. case frecip_op:
  703. handler.u = fpemu_sp_recip;
  704. goto scopuop;
  705. #endif
  706. #if __mips >= 4
  707. case fmovc_op:
  708. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  709. if (((ctx->fcr31 & cond) != 0) !=
  710. ((MIPSInst_FT(ir) & 1) != 0))
  711. return 0;
  712. SPFROMREG(rv.s, MIPSInst_FS(ir));
  713. break;
  714. case fmovz_op:
  715. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  716. return 0;
  717. SPFROMREG(rv.s, MIPSInst_FS(ir));
  718. break;
  719. case fmovn_op:
  720. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  721. return 0;
  722. SPFROMREG(rv.s, MIPSInst_FS(ir));
  723. break;
  724. #endif
  725. case fabs_op:
  726. handler.u = ieee754sp_abs;
  727. goto scopuop;
  728. case fneg_op:
  729. handler.u = ieee754sp_neg;
  730. goto scopuop;
  731. case fmov_op:
  732. /* an easy one */
  733. SPFROMREG(rv.s, MIPSInst_FS(ir));
  734. goto copcsr;
  735. /* binary op on handler */
  736. scopbop:
  737. {
  738. ieee754sp fs, ft;
  739. SPFROMREG(fs, MIPSInst_FS(ir));
  740. SPFROMREG(ft, MIPSInst_FT(ir));
  741. rv.s = (*handler.b) (fs, ft);
  742. goto copcsr;
  743. }
  744. scopuop:
  745. {
  746. ieee754sp fs;
  747. SPFROMREG(fs, MIPSInst_FS(ir));
  748. rv.s = (*handler.u) (fs);
  749. goto copcsr;
  750. }
  751. copcsr:
  752. if (ieee754_cxtest(IEEE754_INEXACT))
  753. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  754. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  755. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  756. if (ieee754_cxtest(IEEE754_OVERFLOW))
  757. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  758. if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
  759. rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
  760. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  761. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  762. break;
  763. /* unary conv ops */
  764. case fcvts_op:
  765. return SIGILL; /* not defined */
  766. case fcvtd_op:{
  767. ieee754sp fs;
  768. SPFROMREG(fs, MIPSInst_FS(ir));
  769. rv.d = ieee754dp_fsp(fs);
  770. rfmt = d_fmt;
  771. goto copcsr;
  772. }
  773. case fcvtw_op:{
  774. ieee754sp fs;
  775. SPFROMREG(fs, MIPSInst_FS(ir));
  776. rv.w = ieee754sp_tint(fs);
  777. rfmt = w_fmt;
  778. goto copcsr;
  779. }
  780. #if __mips >= 2 || defined(__mips64)
  781. case fround_op:
  782. case ftrunc_op:
  783. case fceil_op:
  784. case ffloor_op:{
  785. unsigned int oldrm = ieee754_csr.rm;
  786. ieee754sp fs;
  787. SPFROMREG(fs, MIPSInst_FS(ir));
  788. ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
  789. rv.w = ieee754sp_tint(fs);
  790. ieee754_csr.rm = oldrm;
  791. rfmt = w_fmt;
  792. goto copcsr;
  793. }
  794. #endif /* __mips >= 2 */
  795. #if defined(__mips64)
  796. case fcvtl_op:{
  797. ieee754sp fs;
  798. SPFROMREG(fs, MIPSInst_FS(ir));
  799. rv.l = ieee754sp_tlong(fs);
  800. rfmt = l_fmt;
  801. goto copcsr;
  802. }
  803. case froundl_op:
  804. case ftruncl_op:
  805. case fceill_op:
  806. case ffloorl_op:{
  807. unsigned int oldrm = ieee754_csr.rm;
  808. ieee754sp fs;
  809. SPFROMREG(fs, MIPSInst_FS(ir));
  810. ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
  811. rv.l = ieee754sp_tlong(fs);
  812. ieee754_csr.rm = oldrm;
  813. rfmt = l_fmt;
  814. goto copcsr;
  815. }
  816. #endif /* defined(__mips64) */
  817. default:
  818. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  819. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  820. ieee754sp fs, ft;
  821. SPFROMREG(fs, MIPSInst_FS(ir));
  822. SPFROMREG(ft, MIPSInst_FT(ir));
  823. rv.w = ieee754sp_cmp(fs, ft,
  824. cmptab[cmpop & 0x7], cmpop & 0x8);
  825. rfmt = -1;
  826. if ((cmpop & 0x8) && ieee754_cxtest
  827. (IEEE754_INVALID_OPERATION))
  828. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  829. else
  830. goto copcsr;
  831. }
  832. else {
  833. return SIGILL;
  834. }
  835. break;
  836. }
  837. break;
  838. }
  839. case d_fmt:{
  840. union {
  841. ieee754dp(*b) (ieee754dp, ieee754dp);
  842. ieee754dp(*u) (ieee754dp);
  843. } handler;
  844. switch (MIPSInst_FUNC(ir)) {
  845. /* binary ops */
  846. case fadd_op:
  847. handler.b = ieee754dp_add;
  848. goto dcopbop;
  849. case fsub_op:
  850. handler.b = ieee754dp_sub;
  851. goto dcopbop;
  852. case fmul_op:
  853. handler.b = ieee754dp_mul;
  854. goto dcopbop;
  855. case fdiv_op:
  856. handler.b = ieee754dp_div;
  857. goto dcopbop;
  858. /* unary ops */
  859. #if __mips >= 2 || defined(__mips64)
  860. case fsqrt_op:
  861. handler.u = ieee754dp_sqrt;
  862. goto dcopuop;
  863. #endif
  864. #if __mips >= 4 && __mips != 32
  865. case frsqrt_op:
  866. handler.u = fpemu_dp_rsqrt;
  867. goto dcopuop;
  868. case frecip_op:
  869. handler.u = fpemu_dp_recip;
  870. goto dcopuop;
  871. #endif
  872. #if __mips >= 4
  873. case fmovc_op:
  874. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  875. if (((ctx->fcr31 & cond) != 0) !=
  876. ((MIPSInst_FT(ir) & 1) != 0))
  877. return 0;
  878. DPFROMREG(rv.d, MIPSInst_FS(ir));
  879. break;
  880. case fmovz_op:
  881. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  882. return 0;
  883. DPFROMREG(rv.d, MIPSInst_FS(ir));
  884. break;
  885. case fmovn_op:
  886. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  887. return 0;
  888. DPFROMREG(rv.d, MIPSInst_FS(ir));
  889. break;
  890. #endif
  891. case fabs_op:
  892. handler.u = ieee754dp_abs;
  893. goto dcopuop;
  894. case fneg_op:
  895. handler.u = ieee754dp_neg;
  896. goto dcopuop;
  897. case fmov_op:
  898. /* an easy one */
  899. DPFROMREG(rv.d, MIPSInst_FS(ir));
  900. goto copcsr;
  901. /* binary op on handler */
  902. dcopbop:{
  903. ieee754dp fs, ft;
  904. DPFROMREG(fs, MIPSInst_FS(ir));
  905. DPFROMREG(ft, MIPSInst_FT(ir));
  906. rv.d = (*handler.b) (fs, ft);
  907. goto copcsr;
  908. }
  909. dcopuop:{
  910. ieee754dp fs;
  911. DPFROMREG(fs, MIPSInst_FS(ir));
  912. rv.d = (*handler.u) (fs);
  913. goto copcsr;
  914. }
  915. /* unary conv ops */
  916. case fcvts_op:{
  917. ieee754dp fs;
  918. DPFROMREG(fs, MIPSInst_FS(ir));
  919. rv.s = ieee754sp_fdp(fs);
  920. rfmt = s_fmt;
  921. goto copcsr;
  922. }
  923. case fcvtd_op:
  924. return SIGILL; /* not defined */
  925. case fcvtw_op:{
  926. ieee754dp fs;
  927. DPFROMREG(fs, MIPSInst_FS(ir));
  928. rv.w = ieee754dp_tint(fs); /* wrong */
  929. rfmt = w_fmt;
  930. goto copcsr;
  931. }
  932. #if __mips >= 2 || defined(__mips64)
  933. case fround_op:
  934. case ftrunc_op:
  935. case fceil_op:
  936. case ffloor_op:{
  937. unsigned int oldrm = ieee754_csr.rm;
  938. ieee754dp fs;
  939. DPFROMREG(fs, MIPSInst_FS(ir));
  940. ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
  941. rv.w = ieee754dp_tint(fs);
  942. ieee754_csr.rm = oldrm;
  943. rfmt = w_fmt;
  944. goto copcsr;
  945. }
  946. #endif
  947. #if defined(__mips64)
  948. case fcvtl_op:{
  949. ieee754dp fs;
  950. DPFROMREG(fs, MIPSInst_FS(ir));
  951. rv.l = ieee754dp_tlong(fs);
  952. rfmt = l_fmt;
  953. goto copcsr;
  954. }
  955. case froundl_op:
  956. case ftruncl_op:
  957. case fceill_op:
  958. case ffloorl_op:{
  959. unsigned int oldrm = ieee754_csr.rm;
  960. ieee754dp fs;
  961. DPFROMREG(fs, MIPSInst_FS(ir));
  962. ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
  963. rv.l = ieee754dp_tlong(fs);
  964. ieee754_csr.rm = oldrm;
  965. rfmt = l_fmt;
  966. goto copcsr;
  967. }
  968. #endif /* __mips >= 3 */
  969. default:
  970. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  971. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  972. ieee754dp fs, ft;
  973. DPFROMREG(fs, MIPSInst_FS(ir));
  974. DPFROMREG(ft, MIPSInst_FT(ir));
  975. rv.w = ieee754dp_cmp(fs, ft,
  976. cmptab[cmpop & 0x7], cmpop & 0x8);
  977. rfmt = -1;
  978. if ((cmpop & 0x8)
  979. &&
  980. ieee754_cxtest
  981. (IEEE754_INVALID_OPERATION))
  982. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  983. else
  984. goto copcsr;
  985. }
  986. else {
  987. return SIGILL;
  988. }
  989. break;
  990. }
  991. break;
  992. }
  993. case w_fmt:{
  994. ieee754sp fs;
  995. switch (MIPSInst_FUNC(ir)) {
  996. case fcvts_op:
  997. /* convert word to single precision real */
  998. SPFROMREG(fs, MIPSInst_FS(ir));
  999. rv.s = ieee754sp_fint(fs.bits);
  1000. rfmt = s_fmt;
  1001. goto copcsr;
  1002. case fcvtd_op:
  1003. /* convert word to double precision real */
  1004. SPFROMREG(fs, MIPSInst_FS(ir));
  1005. rv.d = ieee754dp_fint(fs.bits);
  1006. rfmt = d_fmt;
  1007. goto copcsr;
  1008. default:
  1009. return SIGILL;
  1010. }
  1011. break;
  1012. }
  1013. #if defined(__mips64)
  1014. case l_fmt:{
  1015. switch (MIPSInst_FUNC(ir)) {
  1016. case fcvts_op:
  1017. /* convert long to single precision real */
  1018. rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1019. rfmt = s_fmt;
  1020. goto copcsr;
  1021. case fcvtd_op:
  1022. /* convert long to double precision real */
  1023. rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1024. rfmt = d_fmt;
  1025. goto copcsr;
  1026. default:
  1027. return SIGILL;
  1028. }
  1029. break;
  1030. }
  1031. #endif
  1032. default:
  1033. return SIGILL;
  1034. }
  1035. /*
  1036. * Update the fpu CSR register for this operation.
  1037. * If an exception is required, generate a tidy SIGFPE exception,
  1038. * without updating the result register.
  1039. * Note: cause exception bits do not accumulate, they are rewritten
  1040. * for each op; only the flag/sticky bits accumulate.
  1041. */
  1042. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1043. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1044. /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
  1045. return SIGFPE;
  1046. }
  1047. /*
  1048. * Now we can safely write the result back to the register file.
  1049. */
  1050. switch (rfmt) {
  1051. case -1:{
  1052. #if __mips >= 4
  1053. cond = fpucondbit[MIPSInst_FD(ir) >> 2];
  1054. #else
  1055. cond = FPU_CSR_COND;
  1056. #endif
  1057. if (rv.w)
  1058. ctx->fcr31 |= cond;
  1059. else
  1060. ctx->fcr31 &= ~cond;
  1061. break;
  1062. }
  1063. case d_fmt:
  1064. DPTOREG(rv.d, MIPSInst_FD(ir));
  1065. break;
  1066. case s_fmt:
  1067. SPTOREG(rv.s, MIPSInst_FD(ir));
  1068. break;
  1069. case w_fmt:
  1070. SITOREG(rv.w, MIPSInst_FD(ir));
  1071. break;
  1072. #if defined(__mips64)
  1073. case l_fmt:
  1074. DITOREG(rv.l, MIPSInst_FD(ir));
  1075. break;
  1076. #endif
  1077. default:
  1078. return SIGILL;
  1079. }
  1080. return 0;
  1081. }
  1082. int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1083. int has_fpu)
  1084. {
  1085. unsigned long oldepc, prevepc;
  1086. mips_instruction insn;
  1087. int sig = 0;
  1088. oldepc = xcp->cp0_epc;
  1089. do {
  1090. prevepc = xcp->cp0_epc;
  1091. if (get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) {
  1092. fpuemustats.errors++;
  1093. return SIGBUS;
  1094. }
  1095. if (insn == 0)
  1096. xcp->cp0_epc += 4; /* skip nops */
  1097. else {
  1098. /*
  1099. * The 'ieee754_csr' is an alias of
  1100. * ctx->fcr31. No need to copy ctx->fcr31 to
  1101. * ieee754_csr. But ieee754_csr.rm is ieee
  1102. * library modes. (not mips rounding mode)
  1103. */
  1104. /* convert to ieee library modes */
  1105. ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
  1106. sig = cop1Emulate(xcp, ctx);
  1107. /* revert to mips rounding mode */
  1108. ieee754_csr.rm = mips_rm[ieee754_csr.rm];
  1109. }
  1110. if (has_fpu)
  1111. break;
  1112. if (sig)
  1113. break;
  1114. cond_resched();
  1115. } while (xcp->cp0_epc > prevepc);
  1116. /* SIGILL indicates a non-fpu instruction */
  1117. if (sig == SIGILL && xcp->cp0_epc != oldepc)
  1118. /* but if epc has advanced, then ignore it */
  1119. sig = 0;
  1120. return sig;
  1121. }