spe.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*-
  2. * Copyright (C) 1996 Wolfgang Solfrank.
  3. * Copyright (C) 1996 TooLs GmbH.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by TooLs GmbH.
  17. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  26. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * $NetBSD: fpu.c,v 1.5 2001/07/22 11:29:46 wiz Exp $
  32. */
  33. #include <sys/cdefs.h>
  34. __FBSDID("$FreeBSD$");
  35. #include <sys/param.h>
  36. #include <sys/proc.h>
  37. #include <sys/systm.h>
  38. #include <sys/limits.h>
  39. #include <machine/altivec.h>
  40. #include <machine/fpu.h>
  41. #include <machine/ieeefp.h>
  42. #include <machine/pcb.h>
  43. #include <machine/psl.h>
  44. #include <powerpc/fpu/fpu_arith.h>
  45. #include <powerpc/fpu/fpu_emu.h>
  46. #include <powerpc/fpu/fpu_extern.h>
  47. void spe_handle_fpdata(struct trapframe *);
  48. void spe_handle_fpround(struct trapframe *);
  49. static int spe_emu_instr(uint32_t, struct fpemu *, struct fpn **, uint32_t *);
  50. static void
  51. save_vec_int(struct thread *td)
  52. {
  53. int msr;
  54. struct pcb *pcb;
  55. pcb = td->td_pcb;
  56. /*
  57. * Temporarily re-enable the vector unit during the save
  58. */
  59. msr = mfmsr();
  60. mtmsr(msr | PSL_VEC);
  61. /*
  62. * Save the vector registers and SPEFSCR to the PCB
  63. */
  64. #define EVSTDW(n) __asm ("evstdw %1,0(%0)" \
  65. :: "b"(pcb->pcb_vec.vr[n]), "n"(n));
  66. EVSTDW(0); EVSTDW(1); EVSTDW(2); EVSTDW(3);
  67. EVSTDW(4); EVSTDW(5); EVSTDW(6); EVSTDW(7);
  68. EVSTDW(8); EVSTDW(9); EVSTDW(10); EVSTDW(11);
  69. EVSTDW(12); EVSTDW(13); EVSTDW(14); EVSTDW(15);
  70. EVSTDW(16); EVSTDW(17); EVSTDW(18); EVSTDW(19);
  71. EVSTDW(20); EVSTDW(21); EVSTDW(22); EVSTDW(23);
  72. EVSTDW(24); EVSTDW(25); EVSTDW(26); EVSTDW(27);
  73. EVSTDW(28); EVSTDW(29); EVSTDW(30); EVSTDW(31);
  74. #undef EVSTDW
  75. __asm ( "evxor 0,0,0\n"
  76. "evmwumiaa 0,0,0\n"
  77. "evstdd 0,0(%0)" :: "b"(&pcb->pcb_vec.spare[0]));
  78. pcb->pcb_vec.vscr = mfspr(SPR_SPEFSCR);
  79. /*
  80. * Disable vector unit again
  81. */
  82. isync();
  83. mtmsr(msr);
  84. }
  85. void
  86. enable_vec(struct thread *td)
  87. {
  88. int msr;
  89. struct pcb *pcb;
  90. struct trapframe *tf;
  91. pcb = td->td_pcb;
  92. tf = trapframe(td);
  93. /*
  94. * Save the thread's SPE CPU number, and set the CPU's current
  95. * vector thread
  96. */
  97. td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
  98. PCPU_SET(vecthread, td);
  99. /*
  100. * Enable the vector unit for when the thread returns from the
  101. * exception. If this is the first time the unit has been used by
  102. * the thread, initialise the vector registers and VSCR to 0, and
  103. * set the flag to indicate that the vector unit is in use.
  104. */
  105. tf->srr1 |= PSL_VEC;
  106. if (!(pcb->pcb_flags & PCB_VEC)) {
  107. memset(&pcb->pcb_vec, 0, sizeof pcb->pcb_vec);
  108. pcb->pcb_flags |= PCB_VEC;
  109. pcb->pcb_vec.vscr = mfspr(SPR_SPEFSCR);
  110. }
  111. /*
  112. * Temporarily enable the vector unit so the registers
  113. * can be restored.
  114. */
  115. msr = mfmsr();
  116. mtmsr(msr | PSL_VEC);
  117. /* Restore SPEFSCR and ACC. Use %r0 as the scratch for ACC. */
  118. mtspr(SPR_SPEFSCR, pcb->pcb_vec.vscr);
  119. __asm __volatile("isync;evldd 0, 0(%0); evmra 0,0\n"
  120. :: "b"(&pcb->pcb_vec.spare[0]));
  121. /*
  122. * The lower half of each register will be restored on trap return. Use
  123. * %r0 as a scratch register, and restore it last.
  124. */
  125. #define EVLDW(n) __asm __volatile("evldw 0, 0(%0); evmergehilo "#n",0,"#n \
  126. :: "b"(&pcb->pcb_vec.vr[n]));
  127. EVLDW(1); EVLDW(2); EVLDW(3); EVLDW(4);
  128. EVLDW(5); EVLDW(6); EVLDW(7); EVLDW(8);
  129. EVLDW(9); EVLDW(10); EVLDW(11); EVLDW(12);
  130. EVLDW(13); EVLDW(14); EVLDW(15); EVLDW(16);
  131. EVLDW(17); EVLDW(18); EVLDW(19); EVLDW(20);
  132. EVLDW(21); EVLDW(22); EVLDW(23); EVLDW(24);
  133. EVLDW(25); EVLDW(26); EVLDW(27); EVLDW(28);
  134. EVLDW(29); EVLDW(30); EVLDW(31); EVLDW(0);
  135. #undef EVLDW
  136. isync();
  137. mtmsr(msr);
  138. }
  139. void
  140. save_vec(struct thread *td)
  141. {
  142. struct pcb *pcb;
  143. pcb = td->td_pcb;
  144. save_vec_int(td);
  145. /*
  146. * Clear the current vec thread and pcb's CPU id
  147. * XXX should this be left clear to allow lazy save/restore ?
  148. */
  149. pcb->pcb_veccpu = INT_MAX;
  150. PCPU_SET(vecthread, NULL);
  151. }
  152. /*
  153. * Save SPE state without dropping ownership. This will only save state if
  154. * the current vector-thread is `td'. This is used for taking core dumps, so
  155. * don't leak kernel information; overwrite the low words of each vector with
  156. * their real value, taken from the thread's trap frame, unconditionally.
  157. */
  158. void
  159. save_vec_nodrop(struct thread *td)
  160. {
  161. struct pcb *pcb;
  162. int i;
  163. if (td == PCPU_GET(vecthread))
  164. save_vec_int(td);
  165. pcb = td->td_pcb;
  166. for (i = 0; i < 32; i++) {
  167. pcb->pcb_vec.vr[i][1] =
  168. td->td_frame ? td->td_frame->fixreg[i] : 0;
  169. }
  170. }
  171. #define SPE_INST_MASK 0x31f
  172. #define EADD 0x200
  173. #define ESUB 0x201
  174. #define EABS 0x204
  175. #define ENABS 0x205
  176. #define ENEG 0x206
  177. #define EMUL 0x208
  178. #define EDIV 0x209
  179. #define ECMPGT 0x20c
  180. #define ECMPLT 0x20d
  181. #define ECMPEQ 0x20e
  182. #define ECFUI 0x210
  183. #define ECFSI 0x211
  184. #define ECTUI 0x214
  185. #define ECTSI 0x215
  186. #define ECTUF 0x216
  187. #define ECTSF 0x217
  188. #define ECTUIZ 0x218
  189. #define ECTSIZ 0x21a
  190. #define SPE 0x4
  191. #define SPFP 0x6
  192. #define DPFP 0x7
  193. #define SPE_OPC 4
  194. #define OPC_SHIFT 26
  195. #define EVFSADD 0x280
  196. #define EVFSSUB 0x281
  197. #define EVFSABS 0x284
  198. #define EVFSNABS 0x285
  199. #define EVFSNEG 0x286
  200. #define EVFSMUL 0x288
  201. #define EVFSDIV 0x289
  202. #define EVFSCMPGT 0x28c
  203. #define EVFSCMPLT 0x28d
  204. #define EVFSCMPEQ 0x28e
  205. #define EVFSCFUI 0x290
  206. #define EVFSCFSI 0x291
  207. #define EVFSCTUI 0x294
  208. #define EVFSCTSI 0x295
  209. #define EVFSCTUF 0x296
  210. #define EVFSCTSF 0x297
  211. #define EVFSCTUIZ 0x298
  212. #define EVFSCTSIZ 0x29a
  213. #define EFSADD 0x2c0
  214. #define EFSSUB 0x2c1
  215. #define EFSABS 0x2c4
  216. #define EFSNABS 0x2c5
  217. #define EFSNEG 0x2c6
  218. #define EFSMUL 0x2c8
  219. #define EFSDIV 0x2c9
  220. #define EFSCMPGT 0x2cc
  221. #define EFSCMPLT 0x2cd
  222. #define EFSCMPEQ 0x2ce
  223. #define EFSCFD 0x2cf
  224. #define EFSCFUI 0x2d0
  225. #define EFSCFSI 0x2d1
  226. #define EFSCTUI 0x2d4
  227. #define EFSCTSI 0x2d5
  228. #define EFSCTUF 0x2d6
  229. #define EFSCTSF 0x2d7
  230. #define EFSCTUIZ 0x2d8
  231. #define EFSCTSIZ 0x2da
  232. #define EFDADD 0x2e0
  233. #define EFDSUB 0x2e1
  234. #define EFDABS 0x2e4
  235. #define EFDNABS 0x2e5
  236. #define EFDNEG 0x2e6
  237. #define EFDMUL 0x2e8
  238. #define EFDDIV 0x2e9
  239. #define EFDCMPGT 0x2ec
  240. #define EFDCMPLT 0x2ed
  241. #define EFDCMPEQ 0x2ee
  242. #define EFDCFS 0x2ef
  243. #define EFDCFUI 0x2f0
  244. #define EFDCFSI 0x2f1
  245. #define EFDCTUI 0x2f4
  246. #define EFDCTSI 0x2f5
  247. #define EFDCTUF 0x2f6
  248. #define EFDCTSF 0x2f7
  249. #define EFDCTUIZ 0x2f8
  250. #define EFDCTSIZ 0x2fa
  251. enum {
  252. NONE,
  253. SINGLE,
  254. DOUBLE,
  255. VECTOR,
  256. };
  257. static uint32_t fpscr_to_spefscr(uint32_t fpscr)
  258. {
  259. uint32_t spefscr;
  260. spefscr = 0;
  261. if (fpscr & FPSCR_VX)
  262. spefscr |= SPEFSCR_FINV;
  263. if (fpscr & FPSCR_OX)
  264. spefscr |= SPEFSCR_FOVF;
  265. if (fpscr & FPSCR_UX)
  266. spefscr |= SPEFSCR_FUNF;
  267. if (fpscr & FPSCR_ZX)
  268. spefscr |= SPEFSCR_FDBZ;
  269. if (fpscr & FPSCR_XX)
  270. spefscr |= SPEFSCR_FX;
  271. return (spefscr);
  272. }
  273. /* Sign is 0 for unsigned, 1 for signed. */
  274. static int
  275. spe_to_int(struct fpemu *fpemu, struct fpn *fpn, uint32_t *val, int sign)
  276. {
  277. uint32_t res[2];
  278. res[0] = fpu_ftox(fpemu, fpn, res);
  279. if (res[0] != UINT_MAX && res[0] != 0)
  280. fpemu->fe_cx |= FPSCR_OX;
  281. else if (sign == 0 && res[0] != 0)
  282. fpemu->fe_cx |= FPSCR_UX;
  283. else
  284. *val = res[1];
  285. return (0);
  286. }
  287. /* Masked instruction */
  288. /*
  289. * For compare instructions, returns 1 if success, 0 if not. For all others,
  290. * returns -1, or -2 if no result needs recorded.
  291. */
  292. static int
  293. spe_emu_instr(uint32_t instr, struct fpemu *fpemu,
  294. struct fpn **result, uint32_t *iresult)
  295. {
  296. switch (instr & SPE_INST_MASK) {
  297. case EABS:
  298. case ENABS:
  299. case ENEG:
  300. /* Taken care of elsewhere. */
  301. break;
  302. case ECTUIZ:
  303. fpemu->fe_cx &= ~FPSCR_RN;
  304. fpemu->fe_cx |= FP_RZ;
  305. case ECTUI:
  306. spe_to_int(fpemu, &fpemu->fe_f2, iresult, 0);
  307. return (-1);
  308. case ECTSIZ:
  309. fpemu->fe_cx &= ~FPSCR_RN;
  310. fpemu->fe_cx |= FP_RZ;
  311. case ECTSI:
  312. spe_to_int(fpemu, &fpemu->fe_f2, iresult, 1);
  313. return (-1);
  314. case EADD:
  315. *result = fpu_add(fpemu);
  316. break;
  317. case ESUB:
  318. *result = fpu_sub(fpemu);
  319. break;
  320. case EMUL:
  321. *result = fpu_mul(fpemu);
  322. break;
  323. case EDIV:
  324. *result = fpu_div(fpemu);
  325. break;
  326. case ECMPGT:
  327. fpu_compare(fpemu, 0);
  328. if (fpemu->fe_cx & FPSCR_FG)
  329. return (1);
  330. return (0);
  331. case ECMPLT:
  332. fpu_compare(fpemu, 0);
  333. if (fpemu->fe_cx & FPSCR_FL)
  334. return (1);
  335. return (0);
  336. case ECMPEQ:
  337. fpu_compare(fpemu, 0);
  338. if (fpemu->fe_cx & FPSCR_FE)
  339. return (1);
  340. return (0);
  341. default:
  342. printf("Unknown instruction %x\n", instr);
  343. }
  344. return (-1);
  345. }
  346. static int
  347. spe_explode(struct fpemu *fe, struct fpn *fp, uint32_t type,
  348. uint32_t hi, uint32_t lo)
  349. {
  350. uint32_t s;
  351. fp->fp_sign = hi >> 31;
  352. fp->fp_sticky = 0;
  353. switch (type) {
  354. case SINGLE:
  355. s = fpu_stof(fp, hi);
  356. break;
  357. case DOUBLE:
  358. s = fpu_dtof(fp, hi, lo);
  359. break;
  360. }
  361. if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
  362. /*
  363. * Input is a signalling NaN. All operations that return
  364. * an input NaN operand put it through a ``NaN conversion'',
  365. * which basically just means ``turn on the quiet bit''.
  366. * We do this here so that all NaNs internally look quiet
  367. * (we can tell signalling ones by their class).
  368. */
  369. fp->fp_mant[0] |= FP_QUIETBIT;
  370. fe->fe_cx = FPSCR_VXSNAN; /* assert invalid operand */
  371. s = FPC_SNAN;
  372. }
  373. fp->fp_class = s;
  374. return (0);
  375. }
  376. /*
  377. * Save the high word of a 64-bit GPR for manipulation in the exception handler.
  378. */
  379. static uint32_t
  380. spe_save_reg_high(int reg)
  381. {
  382. uint32_t vec[2];
  383. #define EVSTDW(n) case n: __asm __volatile ("evstdw %1,0(%0)" \
  384. :: "b"(vec), "n"(n) : "memory"); break;
  385. switch (reg) {
  386. EVSTDW(0); EVSTDW(1); EVSTDW(2); EVSTDW(3);
  387. EVSTDW(4); EVSTDW(5); EVSTDW(6); EVSTDW(7);
  388. EVSTDW(8); EVSTDW(9); EVSTDW(10); EVSTDW(11);
  389. EVSTDW(12); EVSTDW(13); EVSTDW(14); EVSTDW(15);
  390. EVSTDW(16); EVSTDW(17); EVSTDW(18); EVSTDW(19);
  391. EVSTDW(20); EVSTDW(21); EVSTDW(22); EVSTDW(23);
  392. EVSTDW(24); EVSTDW(25); EVSTDW(26); EVSTDW(27);
  393. EVSTDW(28); EVSTDW(29); EVSTDW(30); EVSTDW(31);
  394. }
  395. #undef EVSTDW
  396. return (vec[0]);
  397. }
  398. /*
  399. * Load the given value into the high word of the requested register.
  400. */
  401. static void
  402. spe_load_reg_high(int reg, uint32_t val)
  403. {
  404. #define EVLDW(n) case n: __asm __volatile("evmergelo "#n",%0,"#n \
  405. :: "r"(val)); break;
  406. switch (reg) {
  407. EVLDW(1); EVLDW(2); EVLDW(3); EVLDW(4);
  408. EVLDW(5); EVLDW(6); EVLDW(7); EVLDW(8);
  409. EVLDW(9); EVLDW(10); EVLDW(11); EVLDW(12);
  410. EVLDW(13); EVLDW(14); EVLDW(15); EVLDW(16);
  411. EVLDW(17); EVLDW(18); EVLDW(19); EVLDW(20);
  412. EVLDW(21); EVLDW(22); EVLDW(23); EVLDW(24);
  413. EVLDW(25); EVLDW(26); EVLDW(27); EVLDW(28);
  414. EVLDW(29); EVLDW(30); EVLDW(31); EVLDW(0);
  415. }
  416. #undef EVLDW
  417. }
  418. void
  419. spe_handle_fpdata(struct trapframe *frame)
  420. {
  421. struct fpemu fpemu;
  422. struct fpn *result;
  423. uint32_t instr, instr_sec_op;
  424. uint32_t cr_shift, ra, rb, rd, src;
  425. uint32_t high, low, res, tmp; /* For vector operations. */
  426. uint32_t spefscr = 0;
  427. uint32_t ftod_res[2];
  428. int width; /* Single, Double, Vector, Integer */
  429. int err;
  430. uint32_t msr;
  431. err = fueword32((void *)frame->srr0, &instr);
  432. if (err != 0)
  433. return;
  434. /* Fault. */;
  435. if ((instr >> OPC_SHIFT) != SPE_OPC)
  436. return;
  437. msr = mfmsr();
  438. /*
  439. * 'cr' field is the upper 3 bits of rd. Magically, since a) rd is 5
  440. * bits, b) each 'cr' field is 4 bits, and c) Only the 'GT' bit is
  441. * modified for most compare operations, the full value of rd can be
  442. * used as a shift value.
  443. */
  444. rd = (instr >> 21) & 0x1f;
  445. ra = (instr >> 16) & 0x1f;
  446. rb = (instr >> 11) & 0x1f;
  447. src = (instr >> 5) & 0x7;
  448. cr_shift = 28 - (rd & 0x1f);
  449. instr_sec_op = (instr & 0x7ff);
  450. memset(&fpemu, 0, sizeof(fpemu));
  451. width = NONE;
  452. switch (src) {
  453. case SPE:
  454. mtmsr(msr | PSL_VEC);
  455. switch (instr_sec_op) {
  456. case EVFSABS:
  457. high = spe_save_reg_high(ra) & ~(1U << 31);
  458. frame->fixreg[rd] = frame->fixreg[ra] & ~(1U << 31);
  459. spe_load_reg_high(rd, high);
  460. break;
  461. case EVFSNABS:
  462. high = spe_save_reg_high(ra) | (1U << 31);
  463. frame->fixreg[rd] = frame->fixreg[ra] | (1U << 31);
  464. spe_load_reg_high(rd, high);
  465. break;
  466. case EVFSNEG:
  467. high = spe_save_reg_high(ra) ^ (1U << 31);
  468. frame->fixreg[rd] = frame->fixreg[ra] ^ (1U << 31);
  469. spe_load_reg_high(rd, high);
  470. break;
  471. default:
  472. /* High word */
  473. spe_explode(&fpemu, &fpemu.fe_f1, SINGLE,
  474. spe_save_reg_high(ra), 0);
  475. spe_explode(&fpemu, &fpemu.fe_f2, SINGLE,
  476. spe_save_reg_high(rb), 0);
  477. high = spe_emu_instr(instr_sec_op, &fpemu, &result,
  478. &tmp);
  479. if (high < 0)
  480. spe_load_reg_high(rd, tmp);
  481. spefscr = fpscr_to_spefscr(fpemu.fe_cx) << 16;
  482. /* Clear the fpemu to start over on the lower bits. */
  483. memset(&fpemu, 0, sizeof(fpemu));
  484. /* Now low word */
  485. spe_explode(&fpemu, &fpemu.fe_f1, SINGLE,
  486. frame->fixreg[ra], 0);
  487. spe_explode(&fpemu, &fpemu.fe_f2, SINGLE,
  488. frame->fixreg[rb], 0);
  489. spefscr |= fpscr_to_spefscr(fpemu.fe_cx);
  490. low = spe_emu_instr(instr_sec_op, &fpemu, &result,
  491. &frame->fixreg[rd]);
  492. if (instr_sec_op == EVFSCMPEQ ||
  493. instr_sec_op == EVFSCMPGT ||
  494. instr_sec_op == EVFSCMPLT) {
  495. res = (high << 3) | (low << 2) |
  496. ((high | low) << 1) | (high & low);
  497. width = NONE;
  498. } else
  499. width = VECTOR;
  500. break;
  501. }
  502. goto end;
  503. case SPFP:
  504. switch (instr_sec_op) {
  505. case EFSABS:
  506. frame->fixreg[rd] = frame->fixreg[ra] & ~(1U << 31);
  507. break;
  508. case EFSNABS:
  509. frame->fixreg[rd] = frame->fixreg[ra] | (1U << 31);
  510. break;
  511. case EFSNEG:
  512. frame->fixreg[rd] = frame->fixreg[ra] ^ (1U << 31);
  513. break;
  514. case EFSCFD:
  515. mtmsr(msr | PSL_VEC);
  516. spe_explode(&fpemu, &fpemu.fe_f3, DOUBLE,
  517. spe_save_reg_high(rb), frame->fixreg[rb]);
  518. result = &fpemu.fe_f3;
  519. width = SINGLE;
  520. break;
  521. default:
  522. spe_explode(&fpemu, &fpemu.fe_f1, SINGLE,
  523. frame->fixreg[ra], 0);
  524. spe_explode(&fpemu, &fpemu.fe_f2, SINGLE,
  525. frame->fixreg[rb], 0);
  526. width = SINGLE;
  527. }
  528. break;
  529. case DPFP:
  530. mtmsr(msr | PSL_VEC);
  531. switch (instr_sec_op) {
  532. case EFDABS:
  533. high = spe_save_reg_high(ra) & ~(1U << 31);
  534. frame->fixreg[rd] = frame->fixreg[ra];
  535. spe_load_reg_high(rd, high);
  536. break;
  537. case EFDNABS:
  538. high = spe_save_reg_high(ra) | (1U << 31);
  539. frame->fixreg[rd] = frame->fixreg[ra];
  540. spe_load_reg_high(rd, high);
  541. break;
  542. case EFDNEG:
  543. high = spe_save_reg_high(ra) ^ (1U << 31);
  544. frame->fixreg[rd] = frame->fixreg[ra];
  545. spe_load_reg_high(rd, high);
  546. break;
  547. case EFDCFS:
  548. spe_explode(&fpemu, &fpemu.fe_f3, SINGLE,
  549. frame->fixreg[rb], 0);
  550. result = &fpemu.fe_f3;
  551. width = DOUBLE;
  552. break;
  553. default:
  554. spe_explode(&fpemu, &fpemu.fe_f1, DOUBLE,
  555. spe_save_reg_high(ra), frame->fixreg[ra]);
  556. spe_explode(&fpemu, &fpemu.fe_f2, DOUBLE,
  557. spe_save_reg_high(rb), frame->fixreg[rb]);
  558. width = DOUBLE;
  559. }
  560. break;
  561. }
  562. switch (instr_sec_op) {
  563. case EFDCFS:
  564. case EFSCFD:
  565. /* Already handled. */
  566. break;
  567. default:
  568. res = spe_emu_instr(instr_sec_op, &fpemu, &result,
  569. &frame->fixreg[rd]);
  570. if (res != -1)
  571. res <<= 2;
  572. break;
  573. }
  574. switch (instr_sec_op & SPE_INST_MASK) {
  575. case ECMPEQ:
  576. case ECMPGT:
  577. case ECMPLT:
  578. frame->cr &= ~(0xf << cr_shift);
  579. frame->cr |= (res << cr_shift);
  580. break;
  581. case ECTUI:
  582. case ECTUIZ:
  583. case ECTSI:
  584. case ECTSIZ:
  585. break;
  586. default:
  587. switch (width) {
  588. case NONE:
  589. case VECTOR:
  590. break;
  591. case SINGLE:
  592. frame->fixreg[rd] = fpu_ftos(&fpemu, result);
  593. break;
  594. case DOUBLE:
  595. spe_load_reg_high(rd, fpu_ftod(&fpemu, result, ftod_res));
  596. frame->fixreg[rd] = ftod_res[1];
  597. break;
  598. default:
  599. panic("Unknown storage width %d", width);
  600. break;
  601. }
  602. }
  603. end:
  604. spefscr |= (mfspr(SPR_SPEFSCR) & ~SPEFSCR_FINVS);
  605. mtspr(SPR_SPEFSCR, spefscr);
  606. frame->srr0 += 4;
  607. mtmsr(msr);
  608. return;
  609. }
  610. void
  611. spe_handle_fpround(struct trapframe *frame)
  612. {
  613. /*
  614. * Punt fpround exceptions for now. This leaves the truncated result in
  615. * the register. We'll deal with overflow/underflow later.
  616. */
  617. return;
  618. }