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