cp1emu.c 29 KB

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