cp1emu.c 28 KB

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