cp1emu.c 29 KB

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