cp1emu.c 28 KB

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