1 /* $NetBSD: fpu_emu.c,v 1.19 2016/12/28 10:52:30 rin Exp $ */ 2 3 /* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Copyright (c) 1992, 1993 40 * The Regents of the University of California. All rights reserved. 41 * 42 * This software was developed by the Computer Systems Engineering group 43 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 44 * contributed to Berkeley. 45 * 46 * All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Lawrence Berkeley Laboratory. 50 * 51 * Redistribution and use in source and binary forms, with or without 52 * modification, are permitted provided that the following conditions 53 * are met: 54 * 1. Redistributions of source code must retain the above copyright 55 * notice, this list of conditions and the following disclaimer. 56 * 2. Redistributions in binary form must reproduce the above copyright 57 * notice, this list of conditions and the following disclaimer in the 58 * documentation and/or other materials provided with the distribution. 59 * 3. Neither the name of the University nor the names of its contributors 60 * may be used to endorse or promote products derived from this software 61 * without specific prior written permission. 62 * 63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 73 * SUCH DAMAGE. 74 * 75 * @(#)fpu.c 8.1 (Berkeley) 6/11/93 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: fpu_emu.c,v 1.19 2016/12/28 10:52:30 rin Exp $"); 80 81 #include "opt_ddb.h" 82 83 #include <sys/param.h> 84 #include <sys/proc.h> 85 #include <sys/signal.h> 86 #include <sys/signalvar.h> 87 #include <sys/siginfo.h> 88 #include <sys/systm.h> 89 #include <sys/syslog.h> 90 #include <sys/evcnt.h> 91 92 #include <powerpc/instr.h> 93 #include <machine/reg.h> 94 #include <machine/fpu.h> 95 #include <machine/trap.h> 96 97 #include <powerpc/fpu/fpu_emu.h> 98 #include <powerpc/fpu/fpu_extern.h> 99 100 #define FPU_EMU_EVCNT_DECL(name) \ 101 static struct evcnt fpu_emu_ev_##name = \ 102 EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "fpemu", #name); \ 103 EVCNT_ATTACH_STATIC(fpu_emu_ev_##name) 104 105 #define FPU_EMU_EVCNT_INCR(name) \ 106 fpu_emu_ev_##name.ev_count++ 107 108 FPU_EMU_EVCNT_DECL(stfiwx); 109 FPU_EMU_EVCNT_DECL(fpstore); 110 FPU_EMU_EVCNT_DECL(fpload); 111 FPU_EMU_EVCNT_DECL(fcmpu); 112 FPU_EMU_EVCNT_DECL(frsp); 113 FPU_EMU_EVCNT_DECL(fctiw); 114 FPU_EMU_EVCNT_DECL(fcmpo); 115 FPU_EMU_EVCNT_DECL(mtfsb1); 116 FPU_EMU_EVCNT_DECL(fnegabs); 117 FPU_EMU_EVCNT_DECL(mcrfs); 118 FPU_EMU_EVCNT_DECL(mtfsb0); 119 FPU_EMU_EVCNT_DECL(fmr); 120 FPU_EMU_EVCNT_DECL(mtfsfi); 121 FPU_EMU_EVCNT_DECL(fnabs); 122 FPU_EMU_EVCNT_DECL(fabs); 123 FPU_EMU_EVCNT_DECL(mffs); 124 FPU_EMU_EVCNT_DECL(mtfsf); 125 FPU_EMU_EVCNT_DECL(fctid); 126 FPU_EMU_EVCNT_DECL(fcfid); 127 FPU_EMU_EVCNT_DECL(fdiv); 128 FPU_EMU_EVCNT_DECL(fsub); 129 FPU_EMU_EVCNT_DECL(fadd); 130 FPU_EMU_EVCNT_DECL(fsqrt); 131 FPU_EMU_EVCNT_DECL(fsel); 132 FPU_EMU_EVCNT_DECL(fpres); 133 FPU_EMU_EVCNT_DECL(fmul); 134 FPU_EMU_EVCNT_DECL(frsqrte); 135 FPU_EMU_EVCNT_DECL(fmulsub); 136 FPU_EMU_EVCNT_DECL(fmuladd); 137 FPU_EMU_EVCNT_DECL(fnmsub); 138 FPU_EMU_EVCNT_DECL(fnmadd); 139 140 /* FPSR exception masks */ 141 #define FPSR_EX_MSK (FPSCR_VX|FPSCR_OX|FPSCR_UX|FPSCR_ZX| \ 142 FPSCR_XX|FPSCR_VXSNAN|FPSCR_VXISI|FPSCR_VXIDI| \ 143 FPSCR_VXZDZ|FPSCR_VXIMZ|FPSCR_VXVC|FPSCR_VXSOFT|\ 144 FPSCR_VXSQRT|FPSCR_VXCVI) 145 #define FPSR_EX (FPSCR_VE|FPSCR_OE|FPSCR_UE|FPSCR_ZE|FPSCR_XE) 146 #define FPSR_EXOP (FPSR_EX_MSK&(~FPSR_EX)) 147 148 149 int fpe_debug = 0; 150 151 #ifdef DDB 152 extern vaddr_t opc_disasm(vaddr_t loc, int opcode); 153 #endif 154 155 #ifdef DEBUG 156 /* 157 * Dump a `fpn' structure. 158 */ 159 void 160 fpu_dumpfpn(struct fpn *fp) 161 { 162 static const char *class[] = { 163 "SNAN", "QNAN", "ZERO", "NUM", "INF" 164 }; 165 166 printf("%s %c.%x %x %x %xE%d", class[fp->fp_class + 2], 167 fp->fp_sign ? '-' : ' ', 168 fp->fp_mant[0], fp->fp_mant[1], 169 fp->fp_mant[2], fp->fp_mant[3], 170 fp->fp_exp); 171 } 172 #endif 173 174 /* 175 * fpu_execute returns the following error numbers (0 = no error): 176 */ 177 #define FPE 1 /* take a floating point exception */ 178 #define NOTFPU 2 /* not an FPU instruction */ 179 #define FAULT 3 180 181 182 /* 183 * Emulate a floating-point instruction. 184 * Return zero for success, else signal number. 185 * (Typically: zero, SIGFPE, SIGILL, SIGSEGV) 186 */ 187 bool 188 fpu_emulate(struct trapframe *tf, struct fpreg *fpf, ksiginfo_t *ksi) 189 { 190 union instr insn; 191 struct fpemu fe; 192 193 KSI_INIT_TRAP(ksi); 194 ksi->ksi_signo = 0; 195 ksi->ksi_addr = (void *)tf->tf_srr0; 196 197 /* initialize insn.is_datasize to tell it is *not* initialized */ 198 fe.fe_fpstate = fpf; 199 fe.fe_cx = 0; 200 201 /* always set this (to avoid a warning) */ 202 203 if (copyin((void *) (tf->tf_srr0), &insn.i_int, sizeof (insn.i_int))) { 204 #ifdef DEBUG 205 printf("fpu_emulate: fault reading opcode\n"); 206 #endif 207 ksi->ksi_signo = SIGSEGV; 208 ksi->ksi_trap = EXC_ISI; 209 ksi->ksi_code = SEGV_MAPERR; 210 ksi->ksi_addr = (void *)tf->tf_srr0; 211 return true; 212 } 213 214 DPRINTF(FPE_EX, ("fpu_emulate: emulating insn %x at %p\n", 215 insn.i_int, (void *)tf->tf_srr0)); 216 217 if ((insn.i_any.i_opcd == OPC_TWI) || 218 ((insn.i_any.i_opcd == OPC_integer_31) && 219 (insn.i_x.i_xo == OPC31_TW))) { 220 /* Check for the two trap insns. */ 221 DPRINTF(FPE_EX, ("fpu_emulate: SIGTRAP\n")); 222 ksi->ksi_signo = SIGTRAP; 223 ksi->ksi_trap = EXC_PGM; 224 ksi->ksi_code = TRAP_TRACE; 225 ksi->ksi_addr = (void *)tf->tf_srr0; 226 return true; 227 } 228 switch (fpu_execute(tf, &fe, &insn)) { 229 case 0: 230 DPRINTF(FPE_EX, ("fpu_emulate: success\n")); 231 tf->tf_srr0 += 4; 232 return true; 233 234 case FPE: 235 DPRINTF(FPE_EX, ("fpu_emulate: SIGFPE\n")); 236 ksi->ksi_signo = SIGFPE; 237 ksi->ksi_trap = EXC_PGM; 238 return true; 239 240 case FAULT: 241 DPRINTF(FPE_EX, ("fpu_emulate: SIGSEGV\n")); 242 ksi->ksi_signo = SIGSEGV; 243 ksi->ksi_trap = EXC_DSI; 244 ksi->ksi_code = SEGV_MAPERR; 245 ksi->ksi_addr = (void *)fe.fe_addr; 246 return true; 247 248 case NOTFPU: 249 default: 250 DPRINTF(FPE_EX, ("fpu_emulate: SIGILL\n")); 251 #if defined(DDB) && defined(DEBUG) 252 if (fpe_debug & FPE_EX) { 253 printf("fpu_emulate: illegal insn %x at %p:", 254 insn.i_int, (void *) (tf->tf_srr0)); 255 opc_disasm((vaddr_t)(tf->tf_srr0), insn.i_int); 256 } 257 #endif 258 #if defined(PPC_IBM4XX) && defined(DDB) && defined(DEBUG) 259 /* 260 * XXXX retry an illegal insn once due to cache issues. 261 */ 262 static int lastill = 0; 263 if (lastill == tf->tf_srr0) { 264 if (fpe_debug & FPE_EX) 265 Debugger(); 266 } 267 lastill = tf->tf_srr0; 268 #endif /* PPC_IBM4XX && DDB && DEBUG */ 269 return false; 270 } 271 } 272 273 /* 274 * Execute an FPU instruction (one that runs entirely in the FPU; not 275 * FBfcc or STF, for instance). On return, fe->fe_fs->fs_fsr will be 276 * modified to reflect the setting the hardware would have left. 277 * 278 * Note that we do not catch all illegal opcodes, so you can, for instance, 279 * multiply two integers this way. 280 */ 281 int 282 fpu_execute(struct trapframe *tf, struct fpemu *fe, union instr *insn) 283 { 284 struct fpn *fp; 285 union instr instr = *insn; 286 int *a; 287 vaddr_t addr; 288 int ra, rb, rc, rt, type, mask, fsr, cx, bf, setcr; 289 unsigned int cond; 290 struct fpreg *fs; 291 292 /* Setup work. */ 293 fp = NULL; 294 fs = fe->fe_fpstate; 295 fe->fe_fpscr = ((int *)&fs->fpscr)[1]; 296 297 /* 298 * On PowerPC all floating point values are stored in registers 299 * as doubles, even when used for single precision operations. 300 */ 301 type = FTYPE_DBL; 302 cond = instr.i_any.i_rc; 303 setcr = 0; 304 bf = 0; /* XXX gcc */ 305 306 #if defined(DDB) && defined(DEBUG) 307 if (fpe_debug & FPE_EX) { 308 vaddr_t loc = tf->tf_srr0; 309 310 printf("Trying to emulate: %p ", (void *)loc); 311 opc_disasm(loc, instr.i_int); 312 } 313 #endif 314 315 /* 316 * `Decode' and execute instruction. 317 */ 318 319 if ((instr.i_any.i_opcd >= OPC_LFS && instr.i_any.i_opcd <= OPC_STFDU) || 320 instr.i_any.i_opcd == OPC_integer_31) { 321 /* 322 * Handle load/store insns: 323 * 324 * Convert to/from single if needed, calculate addr, 325 * and update index reg if needed. 326 */ 327 uint64_t buf; 328 size_t size = sizeof(float); 329 int store, update; 330 331 cond = 0; /* ld/st never set condition codes */ 332 333 334 if (instr.i_any.i_opcd == OPC_integer_31) { 335 if (instr.i_x.i_xo == OPC31_STFIWX) { 336 FPU_EMU_EVCNT_INCR(stfiwx); 337 338 /* Store as integer */ 339 ra = instr.i_x.i_ra; 340 rb = instr.i_x.i_rb; 341 DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n", 342 ra, tf->tf_fixreg[ra], rb, tf->tf_fixreg[rb])); 343 344 addr = tf->tf_fixreg[rb]; 345 if (ra != 0) 346 addr += tf->tf_fixreg[ra]; 347 rt = instr.i_x.i_rt; 348 a = (int *)&fs->fpreg[rt]; 349 DPRINTF(FPE_INSN, 350 ("fpu_execute: Store INT %x at %p\n", 351 a[1], (void *)addr)); 352 if (copyout(&a[1], (void *)addr, sizeof(int))) { 353 fe->fe_addr = addr; 354 return (FAULT); 355 } 356 return (0); 357 } 358 359 if ((instr.i_x.i_xo & OPC31_FPMASK) != OPC31_FPOP) 360 /* Not an indexed FP load/store op */ 361 return (NOTFPU); 362 363 store = (instr.i_x.i_xo & 0x80); 364 if (instr.i_x.i_xo & 0x40) 365 size = sizeof(double); 366 else 367 type = FTYPE_SNG; 368 update = (instr.i_x.i_xo & 0x20); 369 370 /* calculate EA of load/store */ 371 ra = instr.i_x.i_ra; 372 rb = instr.i_x.i_rb; 373 DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n", 374 ra, tf->tf_fixreg[ra], rb, tf->tf_fixreg[rb])); 375 addr = tf->tf_fixreg[rb]; 376 if (ra != 0) 377 addr += tf->tf_fixreg[ra]; 378 rt = instr.i_x.i_rt; 379 } else { 380 store = instr.i_d.i_opcd & 0x4; 381 if (instr.i_d.i_opcd & 0x2) 382 size = sizeof(double); 383 else 384 type = FTYPE_SNG; 385 update = instr.i_d.i_opcd & 0x1; 386 387 /* calculate EA of load/store */ 388 ra = instr.i_d.i_ra; 389 addr = instr.i_d.i_d; 390 DPRINTF(FPE_INSN, ("reg %d has %lx displ %lx\n", 391 ra, tf->tf_fixreg[ra], addr)); 392 if (ra != 0) 393 addr += tf->tf_fixreg[ra]; 394 rt = instr.i_d.i_rt; 395 } 396 397 if (update && ra == 0) 398 return (NOTFPU); 399 400 if (store) { 401 /* Store */ 402 FPU_EMU_EVCNT_INCR(fpstore); 403 if (type != FTYPE_DBL) { 404 DPRINTF(FPE_INSN, 405 ("fpu_execute: Store SNG at %p\n", 406 (void *)addr)); 407 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL, rt); 408 fpu_implode(fe, fp, type, (void *)&buf); 409 if (copyout(&buf, (void *)addr, size)) { 410 fe->fe_addr = addr; 411 return (FAULT); 412 } 413 } else { 414 DPRINTF(FPE_INSN, 415 ("fpu_execute: Store DBL at %p\n", 416 (void *)addr)); 417 if (copyout(&fs->fpreg[rt], (void *)addr, size)) { 418 fe->fe_addr = addr; 419 return (FAULT); 420 } 421 } 422 } else { 423 /* Load */ 424 FPU_EMU_EVCNT_INCR(fpload); 425 DPRINTF(FPE_INSN, ("fpu_execute: Load from %p\n", 426 (void *)addr)); 427 if (copyin((const void *)addr, &fs->fpreg[rt], size)) { 428 fe->fe_addr = addr; 429 return (FAULT); 430 } 431 if (type != FTYPE_DBL) { 432 fpu_explode(fe, fp = &fe->fe_f1, type, rt); 433 fpu_implode(fe, fp, FTYPE_DBL, 434 (u_int *)&fs->fpreg[rt]); 435 } 436 } 437 if (update) 438 tf->tf_fixreg[ra] = addr; 439 /* Complete. */ 440 return (0); 441 #ifdef notyet 442 } else if (instr.i_any.i_opcd == OPC_load_st_62) { 443 /* These are 64-bit extenstions */ 444 return (NOTFPU); 445 #endif 446 } else if (instr.i_any.i_opcd == OPC_sp_fp_59 || 447 instr.i_any.i_opcd == OPC_dp_fp_63) { 448 449 450 if (instr.i_any.i_opcd == OPC_dp_fp_63 && 451 !(instr.i_a.i_xo & OPC63M_MASK)) { 452 /* Format X */ 453 rt = instr.i_x.i_rt; 454 ra = instr.i_x.i_ra; 455 rb = instr.i_x.i_rb; 456 457 458 /* One of the special opcodes.... */ 459 switch (instr.i_x.i_xo) { 460 case OPC63_FCMPU: 461 FPU_EMU_EVCNT_INCR(fcmpu); 462 DPRINTF(FPE_INSN, ("fpu_execute: FCMPU\n")); 463 rt >>= 2; 464 fpu_explode(fe, &fe->fe_f1, type, ra); 465 fpu_explode(fe, &fe->fe_f2, type, rb); 466 fpu_compare(fe, 0); 467 /* Make sure we do the condition regs. */ 468 cond = 0; 469 /* N.B.: i_rs is already left shifted by two. */ 470 bf = instr.i_x.i_rs & 0xfc; 471 setcr = 1; 472 break; 473 474 case OPC63_FRSP: 475 /* 476 * Convert to single: 477 * 478 * PowerPC uses this to round a double 479 * precision value to single precision, 480 * but values in registers are always 481 * stored in double precision format. 482 */ 483 FPU_EMU_EVCNT_INCR(frsp); 484 DPRINTF(FPE_INSN, ("fpu_execute: FRSP\n")); 485 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL, rb); 486 fpu_implode(fe, fp, FTYPE_SNG, 487 (u_int *)&fs->fpreg[rt]); 488 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_SNG, rt); 489 type = FTYPE_DBL; 490 break; 491 case OPC63_FCTIW: 492 case OPC63_FCTIWZ: 493 FPU_EMU_EVCNT_INCR(fctiw); 494 DPRINTF(FPE_INSN, ("fpu_execute: FCTIW\n")); 495 fpu_explode(fe, fp = &fe->fe_f1, type, rb); 496 type = FTYPE_INT; 497 break; 498 case OPC63_FCMPO: 499 FPU_EMU_EVCNT_INCR(fcmpo); 500 DPRINTF(FPE_INSN, ("fpu_execute: FCMPO\n")); 501 rt >>= 2; 502 fpu_explode(fe, &fe->fe_f1, type, ra); 503 fpu_explode(fe, &fe->fe_f2, type, rb); 504 fpu_compare(fe, 1); 505 /* Make sure we do the condition regs. */ 506 cond = 0; 507 /* N.B.: i_rs is already left shifted by two. */ 508 bf = instr.i_x.i_rs & 0xfc; 509 setcr = 1; 510 break; 511 case OPC63_MTFSB1: 512 FPU_EMU_EVCNT_INCR(mtfsb1); 513 DPRINTF(FPE_INSN, ("fpu_execute: MTFSB1\n")); 514 fe->fe_fpscr |= 515 (~(FPSCR_VX|FPSR_EX) & (1<<(31-rt))); 516 break; 517 case OPC63_FNEG: 518 FPU_EMU_EVCNT_INCR(fnegabs); 519 DPRINTF(FPE_INSN, ("fpu_execute: FNEGABS\n")); 520 memcpy(&fs->fpreg[rt], &fs->fpreg[rb], 521 sizeof(double)); 522 a = (int *)&fs->fpreg[rt]; 523 *a ^= (1 << 31); 524 break; 525 case OPC63_MCRFS: 526 FPU_EMU_EVCNT_INCR(mcrfs); 527 DPRINTF(FPE_INSN, ("fpu_execute: MCRFS\n")); 528 cond = 0; 529 rt &= 0x1c; 530 ra &= 0x1c; 531 /* Extract the bits we want */ 532 mask = (fe->fe_fpscr >> (28 - ra)) & 0xf; 533 /* Clear the bits we copied. */ 534 fe->fe_cx = 535 (FPSR_EX_MSK | (0xf << (28 - ra))); 536 fe->fe_fpscr &= fe->fe_cx; 537 /* Now shove them in the right part of cr */ 538 tf->tf_cr &= ~(0xf << (28 - rt)); 539 tf->tf_cr |= (mask << (28 - rt)); 540 break; 541 case OPC63_MTFSB0: 542 FPU_EMU_EVCNT_INCR(mtfsb0); 543 DPRINTF(FPE_INSN, ("fpu_execute: MTFSB0\n")); 544 fe->fe_fpscr &= 545 ((FPSCR_VX|FPSR_EX) & ~(1<<(31-rt))); 546 break; 547 case OPC63_FMR: 548 FPU_EMU_EVCNT_INCR(fmr); 549 DPRINTF(FPE_INSN, ("fpu_execute: FMR\n")); 550 memcpy(&fs->fpreg[rt], &fs->fpreg[rb], 551 sizeof(double)); 552 break; 553 case OPC63_MTFSFI: 554 FPU_EMU_EVCNT_INCR(mtfsfi); 555 DPRINTF(FPE_INSN, ("fpu_execute: MTFSFI\n")); 556 rb >>= 1; 557 rt &= 0x1c; /* Already left-shifted 4 */ 558 fe->fe_cx = rb << (28 - rt); 559 mask = 0xf<<(28 - rt); 560 fe->fe_fpscr = (fe->fe_fpscr & ~mask) | 561 fe->fe_cx; 562 /* XXX weird stuff about OX, FX, FEX, and VX should be handled */ 563 break; 564 case OPC63_FNABS: 565 FPU_EMU_EVCNT_INCR(fnabs); 566 DPRINTF(FPE_INSN, ("fpu_execute: FABS\n")); 567 memcpy(&fs->fpreg[rt], &fs->fpreg[rb], 568 sizeof(double)); 569 a = (int *)&fs->fpreg[rt]; 570 *a |= (1 << 31); 571 break; 572 case OPC63_FABS: 573 FPU_EMU_EVCNT_INCR(fabs); 574 DPRINTF(FPE_INSN, ("fpu_execute: FABS\n")); 575 memcpy(&fs->fpreg[rt], &fs->fpreg[rb], 576 sizeof(double)); 577 a = (int *)&fs->fpreg[rt]; 578 *a &= ~(1 << 31); 579 break; 580 case OPC63_MFFS: 581 FPU_EMU_EVCNT_INCR(mffs); 582 DPRINTF(FPE_INSN, ("fpu_execute: MFFS\n")); 583 memcpy(&fs->fpreg[rt], &fs->fpscr, 584 sizeof(fs->fpscr)); 585 break; 586 case OPC63_MTFSF: 587 FPU_EMU_EVCNT_INCR(mtfsf); 588 DPRINTF(FPE_INSN, ("fpu_execute: MTFSF\n")); 589 if ((rt = instr.i_xfl.i_flm) == -1) 590 mask = -1; 591 else { 592 mask = 0; 593 /* Convert 1 bit -> 4 bits */ 594 for (ra = 0; ra < 8; ra ++) 595 if (rt & (1<<ra)) 596 mask |= (0xf<<(4*ra)); 597 } 598 a = (int *)&fs->fpreg[rt]; 599 fe->fe_cx = mask & a[1]; 600 fe->fe_fpscr = (fe->fe_fpscr&~mask) | 601 (fe->fe_cx); 602 /* XXX weird stuff about OX, FX, FEX, and VX should be handled */ 603 break; 604 case OPC63_FCTID: 605 case OPC63_FCTIDZ: 606 FPU_EMU_EVCNT_INCR(fctid); 607 DPRINTF(FPE_INSN, ("fpu_execute: FCTID\n")); 608 fpu_explode(fe, fp = &fe->fe_f1, type, rb); 609 type = FTYPE_LNG; 610 break; 611 case OPC63_FCFID: 612 FPU_EMU_EVCNT_INCR(fcfid); 613 DPRINTF(FPE_INSN, ("fpu_execute: FCFID\n")); 614 type = FTYPE_LNG; 615 fpu_explode(fe, fp = &fe->fe_f1, type, rb); 616 type = FTYPE_DBL; 617 break; 618 default: 619 return (NOTFPU); 620 break; 621 } 622 } else { 623 /* Format A */ 624 rt = instr.i_a.i_frt; 625 ra = instr.i_a.i_fra; 626 rb = instr.i_a.i_frb; 627 rc = instr.i_a.i_frc; 628 629 /* 630 * All arithmetic operations work on registers, which 631 * are stored as doubles. 632 */ 633 type = FTYPE_DBL; 634 switch ((unsigned int)instr.i_a.i_xo) { 635 case OPC59_FDIVS: 636 FPU_EMU_EVCNT_INCR(fdiv); 637 DPRINTF(FPE_INSN, ("fpu_execute: FDIV\n")); 638 fpu_explode(fe, &fe->fe_f1, type, ra); 639 fpu_explode(fe, &fe->fe_f2, type, rb); 640 fp = fpu_div(fe); 641 break; 642 case OPC59_FSUBS: 643 FPU_EMU_EVCNT_INCR(fsub); 644 DPRINTF(FPE_INSN, ("fpu_execute: FSUB\n")); 645 fpu_explode(fe, &fe->fe_f1, type, ra); 646 fpu_explode(fe, &fe->fe_f2, type, rb); 647 fp = fpu_sub(fe); 648 break; 649 case OPC59_FADDS: 650 FPU_EMU_EVCNT_INCR(fadd); 651 DPRINTF(FPE_INSN, ("fpu_execute: FADD\n")); 652 fpu_explode(fe, &fe->fe_f1, type, ra); 653 fpu_explode(fe, &fe->fe_f2, type, rb); 654 fp = fpu_add(fe); 655 break; 656 case OPC59_FSQRTS: 657 FPU_EMU_EVCNT_INCR(fsqrt); 658 DPRINTF(FPE_INSN, ("fpu_execute: FSQRT\n")); 659 fpu_explode(fe, &fe->fe_f1, type, rb); 660 fp = fpu_sqrt(fe); 661 break; 662 case OPC63M_FSEL: 663 FPU_EMU_EVCNT_INCR(fsel); 664 DPRINTF(FPE_INSN, ("fpu_execute: FSEL\n")); 665 a = (int *)&fe->fe_fpstate->fpreg[ra]; 666 if ((*a & 0x80000000) && (*a & 0x7fffffff)) 667 /* fra < 0 */ 668 rc = rb; 669 DPRINTF(FPE_INSN, ("f%d => f%d\n", rc, rt)); 670 memcpy(&fs->fpreg[rt], &fs->fpreg[rc], 671 sizeof(double)); 672 break; 673 case OPC59_FRES: 674 FPU_EMU_EVCNT_INCR(fpres); 675 DPRINTF(FPE_INSN, ("fpu_execute: FPRES\n")); 676 fpu_explode(fe, &fe->fe_f1, type, rb); 677 fp = fpu_sqrt(fe); 678 /* now we've gotta overwrite the dest reg */ 679 *((int *)&fe->fe_fpstate->fpreg[rt]) = 1; 680 fpu_explode(fe, &fe->fe_f1, FTYPE_INT, rt); 681 fpu_div(fe); 682 break; 683 case OPC59_FMULS: 684 FPU_EMU_EVCNT_INCR(fmul); 685 DPRINTF(FPE_INSN, ("fpu_execute: FMUL\n")); 686 fpu_explode(fe, &fe->fe_f1, type, ra); 687 fpu_explode(fe, &fe->fe_f2, type, rc); 688 fp = fpu_mul(fe); 689 break; 690 case OPC63M_FRSQRTE: 691 /* Reciprocal sqrt() estimate */ 692 FPU_EMU_EVCNT_INCR(frsqrte); 693 DPRINTF(FPE_INSN, ("fpu_execute: FRSQRTE\n")); 694 fpu_explode(fe, &fe->fe_f1, type, rb); 695 fp = fpu_sqrt(fe); 696 fe->fe_f2 = *fp; 697 /* now we've gotta overwrite the dest reg */ 698 *((int *)&fe->fe_fpstate->fpreg[rt]) = 1; 699 fpu_explode(fe, &fe->fe_f1, FTYPE_INT, rt); 700 fpu_div(fe); 701 break; 702 case OPC59_FMSUBS: 703 FPU_EMU_EVCNT_INCR(fmulsub); 704 DPRINTF(FPE_INSN, ("fpu_execute: FMULSUB\n")); 705 fpu_explode(fe, &fe->fe_f1, type, ra); 706 fpu_explode(fe, &fe->fe_f2, type, rc); 707 fp = fpu_mul(fe); 708 fe->fe_f1 = *fp; 709 fpu_explode(fe, &fe->fe_f2, type, rb); 710 fp = fpu_sub(fe); 711 break; 712 case OPC59_FMADDS: 713 FPU_EMU_EVCNT_INCR(fmuladd); 714 DPRINTF(FPE_INSN, ("fpu_execute: FMULADD\n")); 715 fpu_explode(fe, &fe->fe_f1, type, ra); 716 fpu_explode(fe, &fe->fe_f2, type, rc); 717 fp = fpu_mul(fe); 718 fe->fe_f1 = *fp; 719 fpu_explode(fe, &fe->fe_f2, type, rb); 720 fp = fpu_add(fe); 721 break; 722 case OPC59_FNMSUBS: 723 FPU_EMU_EVCNT_INCR(fnmsub); 724 DPRINTF(FPE_INSN, ("fpu_execute: FNMSUB\n")); 725 fpu_explode(fe, &fe->fe_f1, type, ra); 726 fpu_explode(fe, &fe->fe_f2, type, rc); 727 fp = fpu_mul(fe); 728 fe->fe_f1 = *fp; 729 fpu_explode(fe, &fe->fe_f2, type, rb); 730 fp = fpu_sub(fe); 731 /* Negate */ 732 fp->fp_sign ^= 1; 733 break; 734 case OPC59_FNMADDS: 735 FPU_EMU_EVCNT_INCR(fnmadd); 736 DPRINTF(FPE_INSN, ("fpu_execute: FNMADD\n")); 737 fpu_explode(fe, &fe->fe_f1, type, ra); 738 fpu_explode(fe, &fe->fe_f2, type, rc); 739 fp = fpu_mul(fe); 740 fe->fe_f1 = *fp; 741 fpu_explode(fe, &fe->fe_f2, type, rb); 742 fp = fpu_add(fe); 743 /* Negate */ 744 fp->fp_sign ^= 1; 745 break; 746 default: 747 return (NOTFPU); 748 break; 749 } 750 751 /* If the instruction was single precision, round */ 752 if (!(instr.i_any.i_opcd & 0x4)) { 753 fpu_implode(fe, fp, FTYPE_SNG, 754 (u_int *)&fs->fpreg[rt]); 755 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_SNG, rt); 756 } 757 } 758 } else { 759 return (NOTFPU); 760 } 761 762 /* 763 * ALU operation is complete. Collapse the result and then check 764 * for exceptions. If we got any, and they are enabled, do not 765 * alter the destination register, just stop with an exception. 766 * Otherwise set new current exceptions and accrue. 767 */ 768 if (fp) 769 fpu_implode(fe, fp, type, (u_int *)&fs->fpreg[rt]); 770 cx = fe->fe_cx; 771 fsr = fe->fe_fpscr; 772 if (cx != 0) { 773 fsr &= ~FPSCR_FX; 774 if ((cx^fsr)&FPSR_EX_MSK) 775 fsr |= FPSCR_FX; 776 mask = fsr & FPSR_EX; 777 mask <<= (25-3); 778 if (cx & mask) 779 fsr |= FPSCR_FEX; 780 if (cx & FPSCR_FPRF) { 781 /* Need to replace CC */ 782 fsr &= ~FPSCR_FPRF; 783 } 784 if (cx & (FPSR_EXOP)) 785 fsr |= FPSCR_VX; 786 fsr |= cx; 787 DPRINTF(FPE_INSN, ("fpu_execute: cx %x, fsr %x\n", cx, fsr)); 788 } 789 790 if (cond) { 791 cond = fsr & 0xf0000000; 792 /* Isolate condition codes */ 793 cond >>= 28; 794 /* Move fpu condition codes to cr[1] */ 795 tf->tf_cr &= (0x0f000000); 796 tf->tf_cr |= (cond<<24); 797 DPRINTF(FPE_INSN, ("fpu_execute: cr[1] <= %x\n", cond)); 798 } 799 800 if (setcr) { 801 cond = fsr & FPSCR_FPCC; 802 /* Isolate condition codes */ 803 cond <<= 16; 804 /* Move fpu condition codes to cr[1] */ 805 tf->tf_cr &= ~(0xf0000000>>bf); 806 tf->tf_cr |= (cond>>bf); 807 DPRINTF(FPE_INSN, ("fpu_execute: cr[%d] (cr=%x) <= %x\n", bf/4, tf->tf_cr, cond)); 808 } 809 810 ((int *)&fs->fpscr)[1] = fsr; 811 if (fsr & FPSCR_FEX) 812 return(FPE); 813 return (0); /* success */ 814 } 815