cp1emu.c 31 KB

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