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