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