cp1emu.c 29 KB

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