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