fpu_emulate.c revision 1.16 1 /* $NetBSD: fpu_emulate.c,v 1.16 1997/07/19 22:28:48 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 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 fe->fe_fpsr &= ~FPSR_EXCP;
608
609 DUMP_INSN(insn);
610
611 if (fpu_debug_level & DL_ARITH) {
612 printf(" fpu_emul_arith: FPSR = %08x, FPCR = %08x\n",
613 fe->fe_fpsr, fe->fe_fpcr);
614 }
615
616 word1 = insn->is_word1;
617 format = (word1 >> 10) & 7;
618 regnum = (word1 >> 7) & 7;
619
620 /* fetch a source operand : may not be used */
621 if (fpu_debug_level & DL_ARITH) {
622 printf(" fpu_emul_arith: dst/src FP%d=%08x,%08x,%08x\n",
623 regnum, fpregs[regnum*3], fpregs[regnum*3+1],
624 fpregs[regnum*3+2]);
625 }
626 fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, &fpregs[regnum * 3]);
627
628 DUMP_INSN(insn);
629
630 /* get the other operand which is always the source */
631 if ((word1 & 0x4000) == 0) {
632 if (fpu_debug_level & DL_ARITH) {
633 printf(" fpu_emul_arith: FP%d op FP%d => FP%d\n",
634 format, regnum, regnum);
635 printf(" fpu_emul_arith: src opr FP%d=%08x,%08x,%08x\n",
636 format, fpregs[format*3], fpregs[format*3+1],
637 fpregs[format*3+2]);
638 }
639 fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
640 } else {
641 /* the operand is in memory */
642 if (format == FTYPE_DBL) {
643 insn->is_datasize = 8;
644 } else if (format == FTYPE_SNG || format == FTYPE_LNG) {
645 insn->is_datasize = 4;
646 } else if (format == FTYPE_WRD) {
647 insn->is_datasize = 2;
648 } else if (format == FTYPE_BYT) {
649 insn->is_datasize = 1;
650 } else if (format == FTYPE_EXT) {
651 insn->is_datasize = 12;
652 } else {
653 /* invalid or unsupported operand format */
654 sig = SIGFPE;
655 return sig;
656 }
657
658 /* Get effective address. (modreg=opcode&077) */
659 sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
660 if (sig) {
661 if (fpu_debug_level & DL_ARITH) {
662 printf(" fpu_emul_arith: error in fpu_decode_ea\n");
663 }
664 return sig;
665 }
666
667 DUMP_INSN(insn);
668
669 if (fpu_debug_level & DL_ARITH) {
670 printf(" fpu_emul_arith: addr mode = ");
671 flags = insn->is_ea0.ea_flags;
672 regname = (insn->is_ea0.ea_regnum & 8) ? 'a' : 'd';
673
674 if (flags & EA_DIRECT) {
675 printf("%c%d\n",
676 regname, insn->is_ea0.ea_regnum & 7);
677 } else if (flags & EA_PC_REL) {
678 if (flags & EA_OFFSET) {
679 printf("pc@(%d)\n", insn->is_ea0.ea_offset);
680 } else if (flags & EA_INDEXED) {
681 printf("pc@(...)\n");
682 }
683 } else if (flags & EA_PREDECR) {
684 printf("%c%d@-\n",
685 regname, insn->is_ea0.ea_regnum & 7);
686 } else if (flags & EA_POSTINCR) {
687 printf("%c%d@+\n", regname, insn->is_ea0.ea_regnum & 7);
688 } else if (flags & EA_OFFSET) {
689 printf("%c%d@(%d)\n", regname, insn->is_ea0.ea_regnum & 7,
690 insn->is_ea0.ea_offset);
691 } else if (flags & EA_INDEXED) {
692 printf("%c%d@(...)\n", regname, insn->is_ea0.ea_regnum & 7);
693 } else if (flags & EA_ABS) {
694 printf("0x%08x\n", insn->is_ea0.ea_absaddr);
695 } else if (flags & EA_IMMED) {
696
697 printf("#0x%08x,%08x,%08x\n", insn->is_ea0.ea_immed[0],
698 insn->is_ea0.ea_immed[1], insn->is_ea0.ea_immed[2]);
699 } else {
700 printf("%c%d@\n", regname, insn->is_ea0.ea_regnum & 7);
701 }
702 } /* if (fpu_debug_level & DL_ARITH) */
703
704 fpu_load_ea(frame, insn, &insn->is_ea0, (char*)buf);
705 if (format == FTYPE_WRD) {
706 /* sign-extend */
707 buf[0] &= 0xffff;
708 if (buf[0] & 0x8000) {
709 buf[0] |= 0xffff0000;
710 }
711 format = FTYPE_LNG;
712 } else if (format == FTYPE_BYT) {
713 /* sign-extend */
714 buf[0] &= 0xff;
715 if (buf[0] & 0x80) {
716 buf[0] |= 0xffffff00;
717 }
718 format = FTYPE_LNG;
719 }
720 if (fpu_debug_level & DL_ARITH) {
721 printf(" fpu_emul_arith: src = %08x %08x %08x, siz = %d\n",
722 buf[0], buf[1], buf[2], insn->is_datasize);
723 }
724 fpu_explode(fe, &fe->fe_f2, format, buf);
725 }
726
727 DUMP_INSN(insn);
728
729 /* An arithmetic instruction emulate function has a prototype of
730 * struct fpn *fpu_op(struct fpemu *);
731
732 * 1) If the instruction is monadic, then fpu_op() must use
733 * fe->fe_f2 as its operand, and return a pointer to the
734 * result.
735
736 * 2) If the instruction is diadic, then fpu_op() must use
737 * fe->fe_f1 and fe->fe_f2 as its two operands, and return a
738 * pointer to the result.
739
740 */
741 res = 0;
742 switch (word1 & 0x3f) {
743 case 0x00: /* fmove */
744 res = &fe->fe_f2;
745 break;
746
747 case 0x01: /* fint */
748 res = fpu_int(fe);
749 break;
750
751 case 0x02: /* fsinh */
752 res = fpu_sinh(fe);
753 break;
754
755 case 0x03: /* fintrz */
756 res = fpu_intrz(fe);
757 break;
758
759 case 0x04: /* fsqrt */
760 res = fpu_sqrt(fe);
761 break;
762
763 case 0x06: /* flognp1 */
764 res = fpu_lognp1(fe);
765 break;
766
767 case 0x08: /* fetoxm1 */
768 res = fpu_etoxm1(fe);
769 break;
770
771 case 0x09: /* ftanh */
772 res = fpu_tanh(fe);
773 break;
774
775 case 0x0A: /* fatan */
776 res = fpu_atan(fe);
777 break;
778
779 case 0x0C: /* fasin */
780 res = fpu_asin(fe);
781 break;
782
783 case 0x0D: /* fatanh */
784 res = fpu_atanh(fe);
785 break;
786
787 case 0x0E: /* fsin */
788 res = fpu_sin(fe);
789 break;
790
791 case 0x0F: /* ftan */
792 res = fpu_tan(fe);
793 break;
794
795 case 0x10: /* fetox */
796 res = fpu_etox(fe);
797 break;
798
799 case 0x11: /* ftwotox */
800 res = fpu_twotox(fe);
801 break;
802
803 case 0x12: /* ftentox */
804 res = fpu_tentox(fe);
805 break;
806
807 case 0x14: /* flogn */
808 res = fpu_logn(fe);
809 break;
810
811 case 0x15: /* flog10 */
812 res = fpu_log10(fe);
813 break;
814
815 case 0x16: /* flog2 */
816 res = fpu_log2(fe);
817 break;
818
819 case 0x18: /* fabs */
820 fe->fe_f2.fp_sign = 0;
821 res = &fe->fe_f2;
822 break;
823
824 case 0x19: /* fcosh */
825 res = fpu_cosh(fe);
826 break;
827
828 case 0x1A: /* fneg */
829 fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign;
830 res = &fe->fe_f2;
831 break;
832
833 case 0x1C: /* facos */
834 res = fpu_acos(fe);
835 break;
836
837 case 0x1D: /* fcos */
838 res = fpu_cos(fe);
839 break;
840
841 case 0x1E: /* fgetexp */
842 res = fpu_getexp(fe);
843 break;
844
845 case 0x1F: /* fgetman */
846 res = fpu_getman(fe);
847 break;
848
849 case 0x20: /* fdiv */
850 case 0x24: /* fsgldiv: cheating - better than nothing */
851 res = fpu_div(fe);
852 break;
853
854 case 0x21: /* fmod */
855 res = fpu_mod(fe);
856 break;
857
858 case 0x28: /* fsub */
859 fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign; /* f2 = -f2 */
860 case 0x22: /* fadd */
861 res = fpu_add(fe);
862 break;
863
864 case 0x23: /* fmul */
865 case 0x27: /* fsglmul: cheating - better than nothing */
866 res = fpu_mul(fe);
867 break;
868
869 case 0x25: /* frem */
870 res = fpu_rem(fe);
871 break;
872
873 case 0x26:
874 /* fscale is handled by a separate function */
875 break;
876
877 case 0x30:
878 case 0x31:
879 case 0x32:
880 case 0x33:
881 case 0x34:
882 case 0x35:
883 case 0x36:
884 case 0x37: /* fsincos */
885 res = fpu_sincos(fe, word1 & 7);
886 break;
887
888 case 0x38: /* fcmp */
889 res = fpu_cmp(fe);
890 discard_result = 1;
891 break;
892
893 case 0x3A: /* ftst */
894 res = &fe->fe_f2;
895 discard_result = 1;
896 break;
897
898 default:
899 #ifdef DEBUG
900 printf(" fpu_emul_arith: bad opcode=0x%x, word1=0x%x\n",
901 insn->is_opcode, insn->is_word1);
902 #endif
903 sig = SIGILL;
904 } /* switch (word1 & 0x3f) */
905
906 if (!discard_result && sig == 0) {
907 fpu_implode(fe, res, FTYPE_EXT, &fpregs[regnum * 3]);
908 if (fpu_debug_level & DL_ARITH) {
909 printf(" fpu_emul_arith: %08x,%08x,%08x stored in FP%d\n",
910 fpregs[regnum*3], fpregs[regnum*3+1],
911 fpregs[regnum*3+2], regnum);
912 }
913 } else if (sig == 0 && fpu_debug_level & DL_ARITH) {
914 static char *class_name[] = { "SNAN", "QNAN", "ZERO", "NUM", "INF" };
915 printf(" fpu_emul_arith: result(%s,%c,%d,%08x,%08x,%08x,%08x) discarded\n",
916 class_name[res->fp_class + 2],
917 res->fp_sign ? '-' : '+', res->fp_exp,
918 res->fp_mant[0], res->fp_mant[1],
919 res->fp_mant[2], res->fp_mant[3]);
920 } else if (fpu_debug_level & DL_ARITH) {
921 printf(" fpu_emul_arith: received signal %d\n", sig);
922 }
923
924 /* update fpsr according to the result of operation */
925 fpu_upd_fpsr(fe, res);
926
927 if (fpu_debug_level & DL_ARITH) {
928 printf(" fpu_emul_arith: FPSR = %08x, FPCR = %08x\n",
929 fe->fe_fpsr, fe->fe_fpcr);
930 }
931
932 DUMP_INSN(insn);
933
934 return sig;
935 }
936
937 /* test condition code according to the predicate in the opcode.
938 * returns -1 when the predicate evaluates to true, 0 when false.
939 * signal numbers are returned when an error is detected.
940 */
941 static int
942 test_cc(fe, pred)
943 struct fpemu *fe;
944 int pred;
945 {
946 int result, sig_bsun, invert;
947 int fpsr;
948
949 fpsr = fe->fe_fpsr;
950 invert = 0;
951 fpsr &= ~FPSR_EXCP; /* clear all exceptions */
952 if (fpu_debug_level & DL_TESTCC) {
953 printf(" test_cc: fpsr=0x%08x\n", fpsr);
954 }
955 pred &= 0x3f; /* lowest 6 bits */
956
957 if (fpu_debug_level & DL_TESTCC) {
958 printf(" test_cc: ");
959 }
960
961 if (pred >= 040) {
962 return SIGILL;
963 } else if (pred & 0x10) {
964 /* IEEE nonaware tests */
965 sig_bsun = 1;
966 pred &= 017; /* lower 4 bits */
967 } else {
968 /* IEEE aware tests */
969 if (fpu_debug_level & DL_TESTCC) {
970 printf("IEEE ");
971 }
972 sig_bsun = 0;
973 }
974
975 if (pred >= 010) {
976 if (fpu_debug_level & DL_TESTCC) {
977 printf("Not ");
978 }
979 /* predicate is "NOT ..." */
980 pred ^= 0xf; /* invert */
981 invert = -1;
982 }
983 switch (pred) {
984 case 0: /* (Signaling) False */
985 if (fpu_debug_level & DL_TESTCC) {
986 printf("False");
987 }
988 result = 0;
989 break;
990 case 1: /* (Signaling) Equal */
991 if (fpu_debug_level & DL_TESTCC) {
992 printf("Equal");
993 }
994 result = -((fpsr & FPSR_ZERO) == FPSR_ZERO);
995 break;
996 case 2: /* Greater Than */
997 if (fpu_debug_level & DL_TESTCC) {
998 printf("GT");
999 }
1000 result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == 0);
1001 break;
1002 case 3: /* Greater or Equal */
1003 if (fpu_debug_level & DL_TESTCC) {
1004 printf("GE");
1005 }
1006 result = -((fpsr & FPSR_ZERO) ||
1007 (fpsr & (FPSR_NAN|FPSR_NEG)) == 0);
1008 break;
1009 case 4: /* Less Than */
1010 if (fpu_debug_level & DL_TESTCC) {
1011 printf("LT");
1012 }
1013 result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == FPSR_NEG);
1014 break;
1015 case 5: /* Less or Equal */
1016 if (fpu_debug_level & DL_TESTCC) {
1017 printf("LE");
1018 }
1019 result = -((fpsr & FPSR_ZERO) ||
1020 ((fpsr & (FPSR_NAN|FPSR_NEG)) == FPSR_NEG));
1021 break;
1022 case 6: /* Greater or Less than */
1023 if (fpu_debug_level & DL_TESTCC) {
1024 printf("GLT");
1025 }
1026 result = -((fpsr & (FPSR_NAN|FPSR_ZERO)) == 0);
1027 break;
1028 case 7: /* Greater, Less or Equal */
1029 if (fpu_debug_level & DL_TESTCC) {
1030 printf("GLE");
1031 }
1032 result = -((fpsr & FPSR_NAN) == 0);
1033 break;
1034 default:
1035 /* invalid predicate */
1036 return SIGILL;
1037 }
1038 result ^= invert; /* if the predicate is "NOT ...", then
1039 invert the result */
1040 if (fpu_debug_level & DL_TESTCC) {
1041 printf(" => %s (%d)\n", result ? "true" : "false", result);
1042 }
1043 /* if it's an IEEE unaware test and NAN is set, BSUN is set */
1044 if (sig_bsun && (fpsr & FPSR_NAN)) {
1045 fpsr |= FPSR_BSUN;
1046 }
1047
1048 /* put fpsr back */
1049 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
1050
1051 return result;
1052 }
1053
1054 /*
1055 * type 1: fdbcc, fscc, ftrapcc
1056 * In this function, we know:
1057 * (opcode & 0x01C0) == 0x0040
1058 */
1059 static int
1060 fpu_emul_type1(fe, insn)
1061 struct fpemu *fe;
1062 struct instruction *insn;
1063 {
1064 struct frame *frame = fe->fe_frame;
1065 int advance, sig, branch, displ;
1066
1067 branch = test_cc(fe, insn->is_word1);
1068 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
1069
1070 insn->is_advance = 4;
1071 sig = 0;
1072
1073 switch (insn->is_opcode & 070) {
1074 case 010: /* fdbcc */
1075 if (branch == -1) {
1076 /* advance */
1077 insn->is_advance = 6;
1078 } else if (!branch) {
1079 /* decrement Dn and if (Dn != -1) branch */
1080 u_int16_t count = frame->f_regs[insn->is_opcode & 7];
1081
1082 if (count-- != 0) {
1083 displ = fusword((void *) (frame->f_pc + insn->is_advance));
1084 if (displ < 0) {
1085 #ifdef DEBUG
1086 printf(" fpu_emul_type1: fault reading displacement\n");
1087 #endif
1088 return SIGSEGV;
1089 }
1090 /* sign-extend the displacement */
1091 displ &= 0xffff;
1092 if (displ & 0x8000) {
1093 displ |= 0xffff0000;
1094 }
1095 insn->is_advance += displ;
1096 } else {
1097 insn->is_advance = 6;
1098 }
1099 /* write it back */
1100 frame->f_regs[insn->is_opcode & 7] &= 0xffff0000;
1101 frame->f_regs[insn->is_opcode & 7] |= (u_int32_t)count;
1102 } else { /* got a signal */
1103 sig = SIGFPE;
1104 }
1105 break;
1106
1107 case 070: /* ftrapcc or fscc */
1108 advance = 4;
1109 if ((insn->is_opcode & 07) >= 2) {
1110 switch (insn->is_opcode & 07) {
1111 case 3: /* long opr */
1112 advance += 2;
1113 case 2: /* word opr */
1114 advance += 2;
1115 case 4: /* no opr */
1116 break;
1117 default:
1118 return SIGILL;
1119 break;
1120 }
1121
1122 if (branch == 0) {
1123 /* no trap */
1124 insn->is_advance = advance;
1125 sig = 0;
1126 } else {
1127 /* trap */
1128 sig = SIGFPE;
1129 }
1130 break;
1131 } /* if ((insn->is_opcode & 7) < 2), fall through to FScc */
1132
1133 default: /* fscc */
1134 insn->is_advance = 4;
1135 insn->is_datasize = 1; /* always byte */
1136 sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
1137 if (sig) {
1138 break;
1139 }
1140 if (branch == -1 || branch == 0) {
1141 /* set result */
1142 sig = fpu_store_ea(frame, insn, &insn->is_ea0, (char *)&branch);
1143 } else {
1144 /* got an exception */
1145 sig = branch;
1146 }
1147 break;
1148 }
1149 return sig;
1150 }
1151
1152 /*
1153 * Type 2 or 3: fbcc (also fnop)
1154 * In this function, we know:
1155 * (opcode & 0x0180) == 0x0080
1156 */
1157 static int
1158 fpu_emul_brcc(fe, insn)
1159 struct fpemu *fe;
1160 struct instruction *insn;
1161 {
1162 struct frame *frame = fe->fe_frame;
1163 int displ, word2;
1164 int sig;
1165
1166 /*
1167 * Get branch displacement.
1168 */
1169 insn->is_advance = 4;
1170 displ = insn->is_word1;
1171
1172 if (insn->is_opcode & 0x40) {
1173 word2 = fusword((void *) (frame->f_pc + insn->is_advance));
1174 if (word2 < 0) {
1175 #ifdef DEBUG
1176 printf(" fpu_emul_brcc: fault reading word2\n");
1177 #endif
1178 return SIGSEGV;
1179 }
1180 displ <<= 16;
1181 displ |= word2;
1182 insn->is_advance += 2;
1183 } else /* displacement is word sized */
1184 if (displ & 0x8000)
1185 displ |= 0xFFFF0000;
1186
1187 /* XXX: If CC, frame->f_pc += displ */
1188 sig = test_cc(fe, insn->is_opcode);
1189 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
1190
1191 if (fe->fe_fpsr & fe->fe_fpcr & FPSR_EXCP) {
1192 return SIGFPE; /* caught an exception */
1193 }
1194 if (sig == -1) {
1195 /* branch does take place; 2 is the offset to the 1st disp word */
1196 insn->is_advance = displ + 2;
1197 } else if (sig) {
1198 return SIGILL; /* got a signal */
1199 }
1200 if (fpu_debug_level & DL_BRANCH) {
1201 printf(" fpu_emul_brcc: %s insn @ %x (%x+%x) (disp=%x)\n",
1202 (sig == -1) ? "BRANCH to" : "NEXT",
1203 frame->f_pc + insn->is_advance, frame->f_pc, insn->is_advance,
1204 displ);
1205 }
1206 return 0;
1207 }
1208