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