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