fpu_emulate.c revision 1.47 1 /* $NetBSD: fpu_emulate.c,v 1.47 2024/12/28 12:15:27 isaki Exp $ */
2
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * some portion Copyright (c) 1995 Ken Nakata
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 * 4. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Gordon Ross
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * mc68881 emulator
36 * XXX - Just a start at it for now...
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: fpu_emulate.c,v 1.47 2024/12/28 12:15:27 isaki Exp $");
41
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/signal.h>
45 #include <sys/systm.h>
46 #include <machine/frame.h>
47
48 #if defined(DDB) && defined(DEBUG_FPE)
49 # include <m68k/db_machdep.h>
50 #endif
51
52 #include "fpu_emulate.h"
53
54 #define fpe_abort(tfp, ksi, signo, code) \
55 do { \
56 (ksi)->ksi_signo = (signo); \
57 (ksi)->ksi_code = (code); \
58 (ksi)->ksi_addr = (void *)(frame)->f_pc; \
59 return -1; \
60 } while (/* CONSTCOND */ 0)
61
62 static int fpu_emul_fmovmcr(struct fpemu *, struct instruction *);
63 static int fpu_emul_fmovm(struct fpemu *, struct instruction *);
64 static int fpu_emul_arith(struct fpemu *, struct instruction *);
65 static int fpu_emul_type1(struct fpemu *, struct instruction *);
66 static int fpu_emul_brcc(struct fpemu *, struct instruction *);
67 static int test_cc(struct fpemu *, int);
68
69 #ifdef DEBUG_FPE
70 #define DUMP_INSN(insn) \
71 printf("%s: insn={adv=%d,siz=%d,op=%04x,w1=%04x}\n", \
72 __func__, \
73 (insn)->is_advance, (insn)->is_datasize, \
74 (insn)->is_opcode, (insn)->is_word1)
75 #define DPRINTF(x) printf x
76 #else
77 #define DUMP_INSN(insn) do {} while (/* CONSTCOND */ 0)
78 #define DPRINTF(x) do {} while (/* CONSTCOND */ 0)
79 #endif
80
81 /*
82 * Emulate a floating-point instruction.
83 * Return zero for success, else signal number.
84 * (Typically: zero, SIGFPE, SIGILL, SIGSEGV)
85 */
86 int
87 fpu_emulate(struct frame *frame, struct fpframe *fpf, ksiginfo_t *ksi)
88 {
89 static struct instruction insn;
90 static struct fpemu fe;
91 int optype, sig;
92 unsigned short sval;
93
94 /* initialize insn.is_datasize to tell it is *not* initialized */
95 insn.is_datasize = -1;
96
97 fe.fe_frame = frame;
98 fe.fe_fpframe = fpf;
99 fe.fe_fpsr = fpf->fpf_fpsr;
100 fe.fe_fpcr = fpf->fpf_fpcr;
101
102 DPRINTF(("%s: ENTERING: FPSR=%08x, FPCR=%08x\n",
103 __func__, fe.fe_fpsr, fe.fe_fpcr));
104
105 /* always set this (to avoid a warning) */
106 insn.is_pc = frame->f_pc;
107 insn.is_nextpc = 0;
108 if (frame->f_format == 4) {
109 /*
110 * A format 4 is generated by the 68{EC,LC}040. The PC is
111 * already set to the instruction following the faulting
112 * instruction. We need to calculate that, anyway. The
113 * fslw is the PC of the faulted instruction, which is what
114 * we expect to be in f_pc.
115 *
116 * XXX - This is a hack; it assumes we at least know the
117 * sizes of all instructions we run across.
118 * XXX TODO: This may not be true, so we might want to save
119 * the PC in order to restore it later.
120 */
121 #if 0
122 insn.is_nextpc = frame->f_pc;
123 #endif
124 insn.is_pc = frame->f_fmt4.f_fslw;
125 frame->f_pc = insn.is_pc;
126 }
127
128 if (ufetch_short((void *)(insn.is_pc), &sval)) {
129 DPRINTF(("%s: fault reading opcode\n", __func__));
130 fpe_abort(frame, ksi, SIGSEGV, SEGV_ACCERR);
131 }
132
133 if ((sval & 0xf000) != 0xf000) {
134 DPRINTF(("%s: not coproc. insn.: opcode=0x%x\n",
135 __func__, sval));
136 fpe_abort(frame, ksi, SIGILL, ILL_ILLOPC);
137 }
138
139 if ((sval & 0x0E00) != 0x0200) {
140 DPRINTF(("%s: bad coproc. id: opcode=0x%x\n", __func__, sval));
141 fpe_abort(frame, ksi, SIGILL, ILL_ILLOPC);
142 }
143
144 insn.is_opcode = sval;
145 optype = (sval & 0x01C0);
146
147 if (ufetch_short((void *)(insn.is_pc + 2), &sval)) {
148 DPRINTF(("%s: fault reading word1\n", __func__));
149 fpe_abort(frame, ksi, SIGSEGV, SEGV_ACCERR);
150 }
151 insn.is_word1 = sval;
152 /* all FPU instructions are at least 4-byte long */
153 insn.is_advance = 4;
154
155 DUMP_INSN(&insn);
156
157 /*
158 * Which family (or type) of opcode is it?
159 * Tests ordered by likelihood (hopefully).
160 * Certainly, type 0 is the most common.
161 */
162 if (optype == 0x0000) {
163 /* type=0: generic */
164 if ((sval & 0x8000)) {
165 if ((sval & 0x4000)) {
166 DPRINTF(("%s: fmovm FPr\n", __func__));
167 sig = fpu_emul_fmovm(&fe, &insn);
168 } else {
169 DPRINTF(("%s: fmovm FPcr\n", __func__));
170 sig = fpu_emul_fmovmcr(&fe, &insn);
171 }
172 } else {
173 if ((sval & 0xe000) == 0x6000) {
174 /* fstore = fmove FPn,mem */
175 DPRINTF(("%s: fmove to mem\n", __func__));
176 sig = fpu_emul_fstore(&fe, &insn);
177 } else if ((sval & 0xfc00) == 0x5c00) {
178 /* fmovecr */
179 DPRINTF(("%s: fmovecr\n", __func__));
180 sig = fpu_emul_fmovecr(&fe, &insn);
181 } else if ((sval & 0xa07f) == 0x26) {
182 /* fscale */
183 DPRINTF(("%s: fscale\n", __func__));
184 sig = fpu_emul_fscale(&fe, &insn);
185 } else {
186 DPRINTF(("%s: other type0\n", __func__));
187 /* all other type0 insns are arithmetic */
188 sig = fpu_emul_arith(&fe, &insn);
189 }
190 if (sig == 0) {
191 DPRINTF(("%s: type 0 returned 0\n", __func__));
192 sig = fpu_upd_excp(&fe);
193 }
194 }
195 } else if (optype == 0x0080 || optype == 0x00C0) {
196 /* type=2 or 3: fbcc, short or long disp. */
197 DPRINTF(("%s: fbcc %s\n", __func__,
198 (optype & 0x40) ? "long" : "short"));
199 sig = fpu_emul_brcc(&fe, &insn);
200 } else if (optype == 0x0040) {
201 /* type=1: fdbcc, fscc, ftrapcc */
202 DPRINTF(("%s: type1\n", __func__));
203 sig = fpu_emul_type1(&fe, &insn);
204 /* real FTRAPcc raises T_TRAPVINST if the condition is met. */
205 if (sig == SIGFPE) {
206 ksi->ksi_trap = T_TRAPVINST;
207 }
208 } else {
209 /* type=4: fsave (privileged) */
210 /* type=5: frestore (privileged) */
211 /* type=6: reserved */
212 /* type=7: reserved */
213 DPRINTF(("%s: bad opcode type: opcode=0x%x\n", __func__,
214 insn.is_opcode));
215 sig = SIGILL;
216 }
217
218 DUMP_INSN(&insn);
219
220 /*
221 * XXX it is not clear to me, if we should progress the PC always,
222 * for SIGFPE || 0, or only for 0; however, without SIGFPE, we
223 * don't pass the signalling regression tests. -is
224 */
225 if ((sig == 0) || (sig == SIGFPE))
226 frame->f_pc += insn.is_advance;
227 #if defined(DDB) && defined(DEBUG_FPE)
228 else {
229 printf("%s: sig=%d, opcode=%x, word1=%x\n", __func__,
230 sig, insn.is_opcode, insn.is_word1);
231 kdb_trap(-1, (db_regs_t *)&frame);
232 }
233 #endif
234 #if 0 /* XXX something is wrong */
235 if (frame->f_format == 4) {
236 /* XXX Restore PC -- 68{EC,LC}040 only */
237 if (insn.is_nextpc)
238 frame->f_pc = insn.is_nextpc;
239 }
240 #endif
241
242 DPRINTF(("%s: EXITING: w/FPSR=%08x, FPCR=%08x\n", __func__,
243 fe.fe_fpsr, fe.fe_fpcr));
244
245 if (sig)
246 fpe_abort(frame, ksi, sig, 0);
247 return sig;
248 }
249
250 /* update accrued exception bits and see if there's an FP exception */
251 int
252 fpu_upd_excp(struct fpemu *fe)
253 {
254 uint32_t fpsr;
255 uint32_t fpcr;
256
257 fpsr = fe->fe_fpsr;
258 fpcr = fe->fe_fpcr;
259 /*
260 * update fpsr accrued exception bits; each insn doesn't have to
261 * update this
262 */
263 if (fpsr & (FPSR_BSUN | FPSR_SNAN | FPSR_OPERR)) {
264 fpsr |= FPSR_AIOP;
265 }
266 if (fpsr & FPSR_OVFL) {
267 fpsr |= FPSR_AOVFL;
268 }
269 if ((fpsr & FPSR_UNFL) && (fpsr & FPSR_INEX2)) {
270 fpsr |= FPSR_AUNFL;
271 }
272 if (fpsr & FPSR_DZ) {
273 fpsr |= FPSR_ADZ;
274 }
275 if (fpsr & (FPSR_INEX1 | FPSR_INEX2 | FPSR_OVFL)) {
276 fpsr |= FPSR_AINEX;
277 }
278
279 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
280
281 return (fpsr & fpcr & FPSR_EXCP) ? SIGFPE : 0;
282 }
283
284 /* update fpsr according to fp (= result of an fp op) */
285 uint32_t
286 fpu_upd_fpsr(struct fpemu *fe, struct fpn *fp)
287 {
288 uint32_t fpsr;
289
290 DPRINTF(("%s: previous fpsr=%08x\n", __func__, fe->fe_fpsr));
291 /* clear all condition code */
292 fpsr = fe->fe_fpsr & ~FPSR_CCB;
293
294 DPRINTF(("%s: result is a ", __func__));
295 if (fp->fp_sign) {
296 DPRINTF(("negative "));
297 fpsr |= FPSR_NEG;
298 } else {
299 DPRINTF(("positive "));
300 }
301
302 switch (fp->fp_class) {
303 case FPC_SNAN:
304 DPRINTF(("signaling NAN\n"));
305 fpsr |= (FPSR_NAN | FPSR_SNAN);
306 break;
307 case FPC_QNAN:
308 DPRINTF(("quiet NAN\n"));
309 fpsr |= FPSR_NAN;
310 break;
311 case FPC_ZERO:
312 DPRINTF(("Zero\n"));
313 fpsr |= FPSR_ZERO;
314 break;
315 case FPC_INF:
316 DPRINTF(("Inf\n"));
317 fpsr |= FPSR_INF;
318 break;
319 default:
320 DPRINTF(("Number\n"));
321 /* anything else is treated as if it is a number */
322 break;
323 }
324
325 fe->fe_fpsr = fe->fe_fpframe->fpf_fpsr = fpsr;
326
327 DPRINTF(("%s: new fpsr=%08x\n", __func__, fe->fe_fpframe->fpf_fpsr));
328
329 return fpsr;
330 }
331
332 static int
333 fpu_emul_fmovmcr(struct fpemu *fe, struct instruction *insn)
334 {
335 struct frame *frame = fe->fe_frame;
336 struct fpframe *fpf = fe->fe_fpframe;
337 int sig;
338 int reglist;
339 int regcount;
340 int fpu_to_mem;
341 uint32_t tmp[3];
342
343 /* move to/from control registers */
344 reglist = (insn->is_word1 & 0x1c00) >> 10;
345 /*
346 * If reglist is 0b000, treat it as FPIAR. This is not specification
347 * but the behavior described in the 6888x user's manual.
348 */
349 if (reglist == 0)
350 reglist = 1;
351
352 if (reglist == 7) {
353 regcount = 3;
354 } else if (reglist == 3 || reglist == 5 || reglist == 6) {
355 regcount = 2;
356 } else {
357 regcount = 1;
358 }
359 insn->is_datasize = regcount * 4;
360 insn->is_advance = 4;
361 sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
362 if (sig)
363 return sig;
364
365 /*
366 * For data register, only single register can be transferred.
367 * For addr register, only FPIAR can be transferred.
368 */
369 if ((insn->is_ea.ea_flags & EA_DIRECT)) {
370 if (insn->is_ea.ea_regnum < 8) {
371 if (regcount != 1) {
372 return SIGILL;
373 }
374 } else {
375 if (reglist != 1) {
376 return SIGILL;
377 }
378 }
379 }
380
381 /* Bit 13 selects direction (FPU to/from Mem) */
382 fpu_to_mem = insn->is_word1 & 0x2000;
383 if (fpu_to_mem) {
384 uint32_t *s = &tmp[0];
385
386 if ((reglist & 4)) {
387 *s++ = fpf->fpf_fpcr;
388 }
389 if ((reglist & 2)) {
390 *s++ = fpf->fpf_fpsr;
391 }
392 if ((reglist & 1)) {
393 *s++ = fpf->fpf_fpiar;
394 }
395
396 sig = fpu_store_ea(frame, insn, &insn->is_ea, (char *)tmp);
397 } else {
398 const uint32_t *d = &tmp[0];
399
400 sig = fpu_load_ea(frame, insn, &insn->is_ea, (char *)tmp);
401 if (sig)
402 return sig;
403
404 if ((reglist & 4)) {
405 fpf->fpf_fpcr = *d++;
406 fpf->fpf_fpcr &= 0x0000fff0;
407 }
408 if ((reglist & 2)) {
409 fpf->fpf_fpsr = *d++;
410 fpf->fpf_fpsr &= 0x0ffffff8;
411 }
412 if ((reglist & 1)) {
413 fpf->fpf_fpiar = *d++;
414 }
415 }
416 return sig;
417 }
418
419 /*
420 * type 0: fmovem
421 * Separated out of fpu_emul_type0 for efficiency.
422 * In this function, we know:
423 * (opcode & 0x01C0) == 0
424 * (word1 & 0x8000) == 0x8000
425 *
426 * No conversion or rounding is done by this instruction,
427 * and the FPSR is not affected.
428 */
429 static int
430 fpu_emul_fmovm(struct fpemu *fe, struct instruction *insn)
431 {
432 struct frame *frame = fe->fe_frame;
433 struct fpframe *fpf = fe->fe_fpframe;
434 int word1, sig;
435 int reglist, regmask, regnum;
436 int fpu_to_mem, order;
437 /* int w1_post_incr; */
438 int *fpregs;
439
440 insn->is_advance = 4;
441 insn->is_datasize = 12;
442 word1 = insn->is_word1;
443
444 /* Bit 13 selects direction (FPU to/from Mem) */
445 fpu_to_mem = word1 & 0x2000;
446
447 /*
448 * Bits 12,11 select register list mode:
449 * 0,0: Static reg list, pre-decr.
450 * 0,1: Dynamic reg list, pre-decr.
451 * 1,0: Static reg list, post-incr.
452 * 1,1: Dynamic reg list, post-incr
453 */
454 /* w1_post_incr = word1 & 0x1000; */
455 if (word1 & 0x0800) {
456 /* dynamic reg list */
457 reglist = frame->f_regs[(word1 & 0x70) >> 4];
458 } else {
459 reglist = word1;
460 }
461 reglist &= 0xFF;
462
463 /* Get effective address. (modreg=opcode&077) */
464 sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
465 if (sig)
466 return sig;
467
468 /* Get address of soft coprocessor regs. */
469 fpregs = &fpf->fpf_regs[0];
470
471 if (insn->is_ea.ea_flags & EA_PREDECR) {
472 regnum = 7;
473 order = -1;
474 } else {
475 regnum = 0;
476 order = 1;
477 }
478
479 regmask = 0x80;
480 while ((0 <= regnum) && (regnum < 8)) {
481 if (regmask & reglist) {
482 if (fpu_to_mem) {
483 sig = fpu_store_ea(frame, insn, &insn->is_ea,
484 (char *)&fpregs[regnum * 3]);
485 DPRINTF(("%s: FP%d (%08x,%08x,%08x) saved\n",
486 __func__, regnum,
487 fpregs[regnum * 3],
488 fpregs[regnum * 3 + 1],
489 fpregs[regnum * 3 + 2]));
490 } else { /* mem to fpu */
491 sig = fpu_load_ea(frame, insn, &insn->is_ea,
492 (char *)&fpregs[regnum * 3]);
493 DPRINTF(("%s: FP%d (%08x,%08x,%08x) loaded\n",
494 __func__, regnum,
495 fpregs[regnum * 3],
496 fpregs[regnum * 3 + 1],
497 fpregs[regnum * 3 + 2]));
498 }
499 if (sig)
500 break;
501 }
502 regnum += order;
503 regmask >>= 1;
504 }
505
506 return sig;
507 }
508
509 struct fpn *
510 fpu_cmp(struct fpemu *fe)
511 {
512 struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
513
514 /* take care of special cases */
515 if (x->fp_class < 0 || y->fp_class < 0) {
516 /* if either of two is a SNAN, result is SNAN */
517 x->fp_class =
518 (y->fp_class < x->fp_class) ? y->fp_class : x->fp_class;
519 } else if (x->fp_class == FPC_INF) {
520 if (y->fp_class == FPC_INF) {
521 /* both infinities */
522 if (x->fp_sign == y->fp_sign) {
523 /* return a signed zero */
524 x->fp_class = FPC_ZERO;
525 } else {
526 /* return a faked number w/x's sign */
527 x->fp_class = FPC_NUM;
528 x->fp_exp = 16383;
529 x->fp_mant[0] = FP_1;
530 }
531 } else {
532 /* y is a number */
533 /* return a forged number w/x's sign */
534 x->fp_class = FPC_NUM;
535 x->fp_exp = 16383;
536 x->fp_mant[0] = FP_1;
537 }
538 } else if (y->fp_class == FPC_INF) {
539 /* x is a Num but y is an Inf */
540 /* return a forged number w/y's sign inverted */
541 x->fp_class = FPC_NUM;
542 x->fp_sign = !y->fp_sign;
543 x->fp_exp = 16383;
544 x->fp_mant[0] = FP_1;
545 } else {
546 /*
547 * x and y are both numbers or zeros,
548 * or pair of a number and a zero
549 */
550 y->fp_sign = !y->fp_sign;
551 x = fpu_add(fe); /* (x - y) */
552 /*
553 * FCMP does not set Inf bit in CC, so return a forged number
554 * (value doesn't matter) if Inf is the result of fsub.
555 */
556 if (x->fp_class == FPC_INF) {
557 x->fp_class = FPC_NUM;
558 x->fp_exp = 16383;
559 x->fp_mant[0] = FP_1;
560 }
561 }
562 return x;
563 }
564
565 /*
566 * arithmetic operations
567 */
568 static int
569 fpu_emul_arith(struct fpemu *fe, struct instruction *insn)
570 {
571 struct frame *frame = fe->fe_frame;
572 uint32_t *fpregs = &(fe->fe_fpframe->fpf_regs[0]);
573 struct fpn *res;
574 int word1, sig = 0;
575 int regnum, format;
576 int discard_result = 0;
577 uint32_t buf[3];
578 #ifdef DEBUG_FPE
579 int flags;
580 char regname;
581 #endif
582
583 fe->fe_fpsr &= ~FPSR_EXCP;
584
585 DUMP_INSN(insn);
586
587 DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
588 fe->fe_fpsr, fe->fe_fpcr));
589
590 word1 = insn->is_word1;
591 format = (word1 >> 10) & 7;
592 regnum = (word1 >> 7) & 7;
593
594 /* fetch a source operand : may not be used */
595 DPRINTF(("%s: dst/src FP%d=%08x,%08x,%08x\n", __func__,
596 regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
597 fpregs[regnum * 3 + 2]));
598
599 fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, &fpregs[regnum * 3]);
600
601 DUMP_INSN(insn);
602
603 /* get the other operand which is always the source */
604 if ((word1 & 0x4000) == 0) {
605 DPRINTF(("%s: FP%d op FP%d => FP%d\n", __func__,
606 format, regnum, regnum));
607 DPRINTF(("%s: src opr FP%d=%08x,%08x,%08x\n", __func__,
608 format, fpregs[format * 3], fpregs[format * 3 + 1],
609 fpregs[format * 3 + 2]));
610 fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
611 } else {
612 /* the operand is in memory */
613 if (format == FTYPE_DBL) {
614 insn->is_datasize = 8;
615 } else if (format == FTYPE_SNG || format == FTYPE_LNG) {
616 insn->is_datasize = 4;
617 } else if (format == FTYPE_WRD) {
618 insn->is_datasize = 2;
619 } else if (format == FTYPE_BYT) {
620 insn->is_datasize = 1;
621 } else if (format == FTYPE_EXT) {
622 insn->is_datasize = 12;
623 } else {
624 /* invalid or unsupported operand format */
625 sig = SIGFPE;
626 return sig;
627 }
628
629 /* Get effective address. (modreg=opcode&077) */
630 sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
631 if (sig) {
632 DPRINTF(("%s: error in fpu_decode_ea\n", __func__));
633 return sig;
634 }
635
636 DUMP_INSN(insn);
637
638 #ifdef DEBUG_FPE
639 printf("%s: addr mode = ", __func__);
640 flags = insn->is_ea.ea_flags;
641 regname = (insn->is_ea.ea_regnum & 8) ? 'a' : 'd';
642
643 if (flags & EA_DIRECT) {
644 printf("%c%d\n", regname, insn->is_ea.ea_regnum & 7);
645 } else if (flags & EA_PC_REL) {
646 if (flags & EA_OFFSET) {
647 printf("pc@(%d)\n", insn->is_ea.ea_offset);
648 } else if (flags & EA_INDEXED) {
649 printf("pc@(...)\n");
650 }
651 } else if (flags & EA_PREDECR) {
652 printf("%c%d@-\n", regname, insn->is_ea.ea_regnum & 7);
653 } else if (flags & EA_POSTINCR) {
654 printf("%c%d@+\n", regname, insn->is_ea.ea_regnum & 7);
655 } else if (flags & EA_OFFSET) {
656 printf("%c%d@(%d)\n", regname,
657 insn->is_ea.ea_regnum & 7,
658 insn->is_ea.ea_offset);
659 } else if (flags & EA_INDEXED) {
660 printf("%c%d@(...)\n", regname,
661 insn->is_ea.ea_regnum & 7);
662 } else if (flags & EA_ABS) {
663 printf("0x%08x\n", insn->is_ea.ea_absaddr);
664 } else if (flags & EA_IMMED) {
665 printf("#0x%08x,%08x,%08x\n",
666 insn->is_ea.ea_immed[0],
667 insn->is_ea.ea_immed[1],
668 insn->is_ea.ea_immed[2]);
669 } else {
670 printf("%c%d@\n", regname, insn->is_ea.ea_regnum & 7);
671 }
672 #endif /* DEBUG_FPE */
673
674 fpu_load_ea(frame, insn, &insn->is_ea, (char*)buf);
675 if (format == FTYPE_WRD) {
676 /* sign-extend */
677 buf[0] &= 0xffff;
678 if (buf[0] & 0x8000)
679 buf[0] |= 0xffff0000;
680 format = FTYPE_LNG;
681 } else if (format == FTYPE_BYT) {
682 /* sign-extend */
683 buf[0] &= 0xff;
684 if (buf[0] & 0x80)
685 buf[0] |= 0xffffff00;
686 format = FTYPE_LNG;
687 }
688 DPRINTF(("%s: src = %08x %08x %08x, siz = %d\n", __func__,
689 buf[0], buf[1], buf[2], insn->is_datasize));
690 fpu_explode(fe, &fe->fe_f2, format, buf);
691 }
692
693 DUMP_INSN(insn);
694
695 /*
696 * An arithmetic instruction emulate function has a prototype of
697 * struct fpn *fpu_op(struct fpemu *);
698 *
699 * 1) If the instruction is monadic, then fpu_op() must use
700 * fe->fe_f2 as its operand, and return a pointer to the
701 * result.
702 *
703 * 2) If the instruction is diadic, then fpu_op() must use
704 * fe->fe_f1 and fe->fe_f2 as its two operands, and return a
705 * pointer to the result.
706 *
707 */
708 res = NULL;
709 switch (word1 & 0x7f) {
710 case 0x00: /* fmove */
711 res = &fe->fe_f2;
712 break;
713
714 case 0x01: /* fint */
715 res = fpu_int(fe);
716 break;
717
718 case 0x02: /* fsinh */
719 res = fpu_sinh(fe);
720 break;
721
722 case 0x03: /* fintrz */
723 res = fpu_intrz(fe);
724 break;
725
726 case 0x04: /* fsqrt */
727 res = fpu_sqrt(fe);
728 break;
729
730 case 0x06: /* flognp1 */
731 res = fpu_lognp1(fe);
732 break;
733
734 case 0x08: /* fetoxm1 */
735 res = fpu_etoxm1(fe);
736 break;
737
738 case 0x09: /* ftanh */
739 res = fpu_tanh(fe);
740 break;
741
742 case 0x0A: /* fatan */
743 res = fpu_atan(fe);
744 break;
745
746 case 0x0C: /* fasin */
747 res = fpu_asin(fe);
748 break;
749
750 case 0x0D: /* fatanh */
751 res = fpu_atanh(fe);
752 break;
753
754 case 0x0E: /* fsin */
755 res = fpu_sin(fe);
756 break;
757
758 case 0x0F: /* ftan */
759 res = fpu_tan(fe);
760 break;
761
762 case 0x10: /* fetox */
763 res = fpu_etox(fe);
764 break;
765
766 case 0x11: /* ftwotox */
767 res = fpu_twotox(fe);
768 break;
769
770 case 0x12: /* ftentox */
771 res = fpu_tentox(fe);
772 break;
773
774 case 0x14: /* flogn */
775 res = fpu_logn(fe);
776 break;
777
778 case 0x15: /* flog10 */
779 res = fpu_log10(fe);
780 break;
781
782 case 0x16: /* flog2 */
783 res = fpu_log2(fe);
784 break;
785
786 case 0x18: /* fabs */
787 fe->fe_f2.fp_sign = 0;
788 res = &fe->fe_f2;
789 break;
790
791 case 0x19: /* fcosh */
792 res = fpu_cosh(fe);
793 break;
794
795 case 0x1A: /* fneg */
796 fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign;
797 res = &fe->fe_f2;
798 break;
799
800 case 0x1C: /* facos */
801 res = fpu_acos(fe);
802 break;
803
804 case 0x1D: /* fcos */
805 res = fpu_cos(fe);
806 break;
807
808 case 0x1E: /* fgetexp */
809 res = fpu_getexp(fe);
810 break;
811
812 case 0x1F: /* fgetman */
813 res = fpu_getman(fe);
814 break;
815
816 case 0x20: /* fdiv */
817 case 0x24: /* fsgldiv: cheating - better than nothing */
818 res = fpu_div(fe);
819 break;
820
821 case 0x21: /* fmod */
822 res = fpu_mod(fe);
823 break;
824
825 case 0x28: /* fsub */
826 fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign; /* f2 = -f2 */
827 /* FALLTHROUGH */
828 case 0x22: /* fadd */
829 res = fpu_add(fe);
830 break;
831
832 case 0x23: /* fmul */
833 case 0x27: /* fsglmul: cheating - better than nothing */
834 res = fpu_mul(fe);
835 break;
836
837 case 0x25: /* frem */
838 res = fpu_rem(fe);
839 break;
840
841 case 0x26:
842 /* fscale is handled by a separate function */
843 break;
844
845 case 0x30:
846 case 0x31:
847 case 0x32:
848 case 0x33:
849 case 0x34:
850 case 0x35:
851 case 0x36:
852 case 0x37: /* fsincos */
853 res = fpu_sincos(fe, word1 & 7);
854 break;
855
856 case 0x38: /* fcmp */
857 res = fpu_cmp(fe);
858 discard_result = 1;
859 break;
860
861 case 0x3A: /* ftst */
862 res = &fe->fe_f2;
863 discard_result = 1;
864 break;
865
866 default: /* possibly 040/060 instructions */
867 DPRINTF(("%s: bad opcode=0x%x, word1=0x%x\n", __func__,
868 insn->is_opcode, insn->is_word1));
869 sig = SIGILL;
870 }
871
872 /* for sanity */
873 if (res == NULL)
874 sig = SIGILL;
875
876 if (sig == 0) {
877 if (!discard_result)
878 fpu_implode(fe, res, FTYPE_EXT, &fpregs[regnum * 3]);
879
880 /* update fpsr according to the result of operation */
881 fpu_upd_fpsr(fe, res);
882 #ifdef DEBUG_FPE
883 if (!discard_result) {
884 printf("%s: %08x,%08x,%08x stored in FP%d\n", __func__,
885 fpregs[regnum * 3],
886 fpregs[regnum * 3 + 1],
887 fpregs[regnum * 3 + 2],
888 regnum);
889 } else {
890 static const char *class_name[] =
891 { "SNAN", "QNAN", "ZERO", "NUM", "INF" };
892 printf("%s: result(%s,%c,%d,%08x,%08x,%08x) "
893 "discarded\n", __func__,
894 class_name[res->fp_class + 2],
895 res->fp_sign ? '-' : '+', res->fp_exp,
896 res->fp_mant[0], res->fp_mant[1],
897 res->fp_mant[2]);
898 }
899 #endif
900 } else {
901 DPRINTF(("%s: received signal %d\n", __func__, sig));
902 }
903
904 DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
905 fe->fe_fpsr, fe->fe_fpcr));
906
907 DUMP_INSN(insn);
908
909 return sig;
910 }
911
912 /*
913 * test condition code according to the predicate in the opcode.
914 * returns -1 when the predicate evaluates to true, 0 when false.
915 * signal numbers are returned when an error is detected.
916 */
917 static int
918 test_cc(struct fpemu *fe, int pred)
919 {
920 int result, sig_bsun;
921 int fpsr;
922
923 fpsr = fe->fe_fpsr;
924 DPRINTF(("%s: fpsr=0x%08x\n", __func__, fpsr));
925 pred &= 0x3f; /* lowest 6 bits */
926
927 DPRINTF(("%s: ", __func__));
928
929 if (pred >= 0x20) {
930 DPRINTF(("Illegal condition code\n"));
931 return SIGILL;
932 } else if (pred & 0x10) {
933 /* IEEE nonaware tests */
934 sig_bsun = 1;
935 pred &= 0x0f; /* lower 4 bits */
936 } else {
937 /* IEEE aware tests */
938 DPRINTF(("IEEE "));
939 sig_bsun = 0;
940 }
941
942 /*
943 * condition real 68882
944 * mnemonic in manual condition
945 * -------- ---------- ----------
946 * 0000 F 0 <- = ~NAN & 0 & ~Z | 0
947 * 0001 EQ Z <- = ~NAN & 0 | Z | 0
948 * 0010 OGT ~(NAN|Z|N) <- = ~NAN & ~N & ~Z | 0
949 * 0011 OGE Z|~(NAN|N) <- = ~NAN & ~N | Z | 0
950 * 0100 OLT N&~(NAN|Z) <- = ~NAN & N & ~Z | 0
951 * 0101 OLE Z|(N&~NAN) <- = ~NAN & N | Z | 0
952 * 0110 OGL ~(NAN|Z) <- = ~NAN & 1 & ~Z | 0
953 * 0111 OR ~NAN Z|~NAN = ~NAN & 1 | Z | 0
954 *
955 * 1000 UN NAN <- = 1 & 0 & ~Z | NAN
956 * 1001 UEQ NAN|Z <- = 1 & 0 | Z | NAN
957 * 1010 UGT NAN|~(N|Z) <- = 1 & ~N & ~Z | NAN
958 * 1011 UGE NAN|(Z|~N) <- = 1 & ~N | Z | NAN
959 * 1100 ULT NAN|(N&~Z) <- = 1 & N & ~Z | NAN
960 * 1101 ULE NAN|(Z|N) <- = 1 & N | Z | NAN
961 * 1110 NE ~Z NAN|(~Z) = 1 & 1 & ~Z | NAN
962 * 1111 T 1 <- = 1 & 1 | Z | NAN
963 */
964 if ((pred & 0x08) == 0) {
965 result = ((fpsr & FPSR_NAN) == 0);
966 } else {
967 result = 1;
968 }
969 switch (pred & 0x06) {
970 case 0x00: /* AND 0 */
971 result &= 0;
972 break;
973 case 0x02: /* AND ~N */
974 result &= ((fpsr & FPSR_NEG) == 0);
975 break;
976 case 0x04: /* AND N */
977 result &= ((fpsr & FPSR_NEG) != 0);
978 break;
979 case 0x06: /* AND 1 */
980 result &= 1;
981 break;
982 }
983 if ((pred & 0x01) == 0) {
984 result &= ((fpsr & FPSR_ZERO) == 0);
985 } else {
986 result |= ((fpsr & FPSR_ZERO) != 0);
987 }
988 if ((pred & 0x08) != 0) {
989 result |= ((fpsr & FPSR_NAN) != 0);
990 }
991
992 DPRINTF(("=> %s (%d)\n", result ? "true" : "false", result));
993 /* if it's an IEEE unaware test and NAN is set, BSUN is set */
994 if (sig_bsun && (fpsr & FPSR_NAN)) {
995 fpsr |= FPSR_BSUN;
996 }
997 /* if BSUN is set, IOP is set too */
998 if ((fpsr & FPSR_BSUN)) {
999 fpsr |= FPSR_AIOP;
1000 }
1001
1002 /* put fpsr back */
1003 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
1004
1005 return -result;
1006 }
1007
1008 /*
1009 * type 1: fdbcc, fscc, ftrapcc
1010 * In this function, we know:
1011 * (opcode & 0x01C0) == 0x0040
1012 * return SIGILL for an illegal instruction.
1013 * return SIGFPE if FTRAPcc's condition is met.
1014 */
1015 static int
1016 fpu_emul_type1(struct fpemu *fe, struct instruction *insn)
1017 {
1018 struct frame *frame = fe->fe_frame;
1019 int advance, sig, branch, displ;
1020 unsigned short sval;
1021
1022 branch = test_cc(fe, insn->is_word1);
1023 if (branch > 0)
1024 return branch;
1025 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
1026
1027 insn->is_advance = 4;
1028 sig = 0;
1029
1030 switch (insn->is_opcode & 070) {
1031 case 010: /* fdbcc */
1032 if (branch) {
1033 /* advance */
1034 insn->is_advance = 6;
1035 } else {
1036 /* decrement Dn and if (Dn != -1) branch */
1037 uint16_t count = frame->f_regs[insn->is_opcode & 7];
1038
1039 if (count-- != 0) {
1040 if (ufetch_short((void *)(insn->is_pc +
1041 insn->is_advance),
1042 &sval)) {
1043 DPRINTF(("%s: fault reading "
1044 "displacement\n", __func__));
1045 return SIGSEGV;
1046 }
1047 displ = sval;
1048 /* sign-extend the displacement */
1049 displ &= 0xffff;
1050 if (displ & 0x8000) {
1051 displ |= 0xffff0000;
1052 }
1053 insn->is_advance += displ;
1054 #if 0 /* XXX */
1055 insn->is_nextpc = insn->is_pc +
1056 insn->is_advance;
1057 #endif
1058 } else {
1059 insn->is_advance = 6;
1060 }
1061 /* write it back */
1062 frame->f_regs[insn->is_opcode & 7] &= 0xffff0000;
1063 frame->f_regs[insn->is_opcode & 7] |= (uint32_t)count;
1064 }
1065 break;
1066
1067 case 070: /* ftrapcc or fscc */
1068 advance = 4;
1069 if ((insn->is_opcode & 07) >= 2) {
1070 switch (insn->is_opcode & 07) {
1071 case 3: /* long opr */
1072 advance += 2;
1073 case 2: /* word opr */
1074 advance += 2;
1075 case 4: /* no opr */
1076 break;
1077 default:
1078 return SIGILL;
1079 break;
1080 }
1081 insn->is_advance = advance;
1082
1083 if (branch) {
1084 /* trap */
1085 sig = SIGFPE;
1086 }
1087 break;
1088 }
1089
1090 /* FALLTHROUGH */
1091 default: /* fscc */
1092 insn->is_advance = 4;
1093 insn->is_datasize = 1; /* always byte */
1094 sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
1095 if (sig) {
1096 break;
1097 }
1098 /* set result */
1099 sig = fpu_store_ea(frame, insn, &insn->is_ea, (char *)&branch);
1100 break;
1101 }
1102 return sig;
1103 }
1104
1105 /*
1106 * Type 2 or 3: fbcc (also fnop)
1107 * In this function, we know:
1108 * (opcode & 0x0180) == 0x0080
1109 */
1110 static int
1111 fpu_emul_brcc(struct fpemu *fe, struct instruction *insn)
1112 {
1113 int displ, word2;
1114 int sig;
1115 unsigned short sval;
1116
1117 /*
1118 * Get branch displacement.
1119 */
1120 insn->is_advance = 4;
1121 displ = insn->is_word1;
1122
1123 if (insn->is_opcode & 0x40) {
1124 if (ufetch_short((void *)(insn->is_pc + insn->is_advance),
1125 &sval)) {
1126 DPRINTF(("%s: fault reading word2\n", __func__));
1127 return SIGSEGV;
1128 }
1129 word2 = sval;
1130 displ <<= 16;
1131 displ |= word2;
1132 insn->is_advance += 2;
1133 } else {
1134 /* displacement is word sized */
1135 if (displ & 0x8000)
1136 displ |= 0xFFFF0000;
1137 }
1138
1139 /* XXX: If CC, insn->is_pc += displ */
1140 sig = test_cc(fe, insn->is_opcode);
1141 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
1142
1143 if (fe->fe_fpsr & fe->fe_fpcr & FPSR_EXCP) {
1144 return SIGFPE; /* caught an exception */
1145 }
1146 if (sig == -1) {
1147 /*
1148 * branch does take place; 2 is the offset to the 1st disp word
1149 */
1150 insn->is_advance = displ + 2;
1151 #if 0 /* XXX */
1152 insn->is_nextpc = insn->is_pc + insn->is_advance;
1153 #endif
1154 } else if (sig)
1155 return SIGILL; /* got a signal */
1156 DPRINTF(("%s: %s insn @ %x (%x+%x) (disp=%x)\n", __func__,
1157 (sig == -1) ? "BRANCH to" : "NEXT",
1158 insn->is_pc + insn->is_advance, insn->is_pc, insn->is_advance,
1159 displ));
1160 return 0;
1161 }
1162