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