fpu_emulate.c revision 1.10 1 /* $NetBSD: fpu_emulate.c,v 1.10 1996/10/13 03:19:12 christos 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 int fusword __P((void *));
55
56 #if !defined(DL_DEFAULT)
57 # if defined(DEBUG_WITH_FPU)
58 # define DL_DEFAULT DL_ALL
59 # else
60 # define DL_DEFAULT 0
61 # endif
62 #endif
63
64 int fpu_debug_level;
65 #if DEBUG
66 static int global_debug_level = DL_DEFAULT;
67 #endif
68
69 #define DUMP_INSN(insn) \
70 if (fpu_debug_level & DL_DUMPINSN) { \
71 printf(" fpu_emulate: insn={adv=%d,siz=%d,op=%04x,w1=%04x}\n", \
72 (insn)->is_advance, (insn)->is_datasize, \
73 (insn)->is_opcode, (insn)->is_word1); \
74 }
75
76 #ifdef DEBUG_WITH_FPU
77 /* mock fpframe for FPE - it's never overwritten by the real fpframe */
78 struct fpframe mockfpf;
79 #endif
80
81 /*
82 * Emulate a floating-point instruction.
83 * Return zero for success, else signal number.
84 * (Typically: zero, SIGFPE, SIGILL, SIGSEGV)
85 */
86 int
87 fpu_emulate(frame, fpf)
88 struct frame *frame;
89 struct fpframe *fpf;
90 {
91 static struct instruction insn;
92 static struct fpemu fe;
93 u_int savedpc;
94 int word, optype, sig;
95
96 #ifdef DEBUG
97 /* initialize insn.is_datasize to tell it is *not* initialized */
98 insn.is_datasize = -1;
99 #endif
100 fe.fe_frame = frame;
101 #ifdef DEBUG_WITH_FPU
102 fe.fe_fpframe = &mockfpf;
103 fe.fe_fpsr = mockfpf.fpf_fpsr;
104 fe.fe_fpcr = mockfpf.fpf_fpcr;
105 #else
106 fe.fe_fpframe = fpf;
107 fe.fe_fpsr = fpf->fpf_fpsr;
108 fe.fe_fpcr = fpf->fpf_fpcr;
109 #endif
110
111 #ifdef DEBUG
112 if ((fpu_debug_level = (fe.fe_fpcr >> 16) & 0x0000ffff) == 0) {
113 /* set the default */
114 fpu_debug_level = global_debug_level;
115 }
116 #endif
117
118 if (fpu_debug_level & DL_VERBOSE) {
119 printf("ENTERING fpu_emulate: FPSR=%08x, FPCR=%08x\n",
120 fe.fe_fpsr, fe.fe_fpcr);
121 }
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 savedpc = frame->f_pc;
135 frame->f_pc = frame->f_fmt4.f_fslw;
136 }
137
138 word = fusword((void *) (frame->f_pc));
139 if (word < 0) {
140 #ifdef DEBUG
141 printf(" fpu_emulate: fault reading opcode\n");
142 #endif
143 return SIGSEGV;
144 }
145
146 if ((word & 0xf000) != 0xf000) {
147 #ifdef DEBUG
148 printf(" fpu_emulate: not coproc. insn.: opcode=0x%x\n", word);
149 #endif
150 return SIGILL;
151 }
152
153 if (
154 #ifdef DEBUG_WITH_FPU
155 (word & 0x0E00) != 0x0c00 /* accept fake ID == 6 */
156 #else
157 (word & 0x0E00) != 0x0200
158 #endif
159 ) {
160 #ifdef DEBUG
161 printf(" fpu_emulate: bad coproc. id: opcode=0x%x\n", word);
162 #endif
163 return SIGILL;
164 }
165
166 insn.is_opcode = word;
167 optype = (word & 0x01C0);
168
169 word = fusword((void *) (frame->f_pc + 2));
170 if (word < 0) {
171 #ifdef DEBUG
172 printf(" fpu_emulate: fault reading word1\n");
173 #endif
174 return SIGSEGV;
175 }
176 insn.is_word1 = word;
177 /* all FPU instructions are at least 4-byte long */
178 insn.is_advance = 4;
179
180 DUMP_INSN(&insn);
181
182 /*
183 * Which family (or type) of opcode is it?
184 * Tests ordered by likelihood (hopefully).
185 * Certainly, type 0 is the most common.
186 */
187 if (optype == 0x0000) {
188 /* type=0: generic */
189 if ((word & 0xc000) == 0xc000) {
190 if (fpu_debug_level & DL_INSN)
191 printf(" fpu_emulate: fmovm FPr\n");
192 sig = fpu_emul_fmovm(&fe, &insn);
193 } else if ((word & 0xc000) == 0x8000) {
194 if (fpu_debug_level & DL_INSN)
195 printf(" fpu_emulate: fmovm FPcr\n");
196 sig = fpu_emul_fmovmcr(&fe, &insn);
197 } else if ((word & 0xe000) == 0x6000) {
198 /* fstore = fmove FPn,mem */
199 if (fpu_debug_level & DL_INSN)
200 printf(" fpu_emulate: fmove to mem\n");
201 sig = fpu_emul_fstore(&fe, &insn);
202 } else if ((word & 0xfc00) == 0x5c00) {
203 /* fmovecr */
204 if (fpu_debug_level & DL_INSN)
205 printf(" fpu_emulate: fmovecr\n");
206 sig = fpu_emul_fmovecr(&fe, &insn);
207 } else if ((word & 0xa07f) == 0x26) {
208 /* fscale */
209 if (fpu_debug_level & DL_INSN)
210 printf(" fpu_emulate: fscale\n");
211 sig = fpu_emul_fscale(&fe, &insn);
212 } else {
213 if (fpu_debug_level & DL_INSN)
214 printf(" fpu_emulte: other type0\n");
215 /* all other type0 insns are arithmetic */
216 sig = fpu_emul_arith(&fe, &insn);
217 }
218 if (sig == 0) {
219 if (fpu_debug_level & DL_VERBOSE)
220 printf(" fpu_emulate: type 0 returned 0\n");
221 sig = fpu_upd_excp(&fe);
222 }
223 } else if (optype == 0x0080 || optype == 0x00C0) {
224 /* type=2 or 3: fbcc, short or long disp. */
225 if (fpu_debug_level & DL_INSN)
226 printf(" fpu_emulate: fbcc %s\n",
227 (optype & 0x40) ? "long" : "short");
228 sig = fpu_emul_brcc(&fe, &insn);
229 } else if (optype == 0x0040) {
230 /* type=1: fdbcc, fscc, ftrapcc */
231 if (fpu_debug_level & DL_INSN)
232 printf(" fpu_emulate: type1\n");
233 sig = fpu_emul_type1(&fe, &insn);
234 } else {
235 /* type=4: fsave (privileged) */
236 /* type=5: frestore (privileged) */
237 /* type=6: reserved */
238 /* type=7: reserved */
239 #ifdef DEBUG
240 printf(" fpu_emulate: bad opcode type: opcode=0x%x\n", insn.is_opcode);
241 #endif
242 sig = SIGILL;
243 }
244
245 DUMP_INSN(&insn);
246
247 if (sig == 0)
248 frame->f_pc += insn.is_advance;
249 #if defined(DDB) && defined(DEBUG)
250 else {
251 printf(" fpu_emulate: sig=%d, opcode=%x, word1=%x\n",
252 sig, insn.is_opcode, insn.is_word1);
253 kdb_trap(-1, frame);
254 }
255 #endif
256 if (frame->f_format == 4)
257 frame->f_pc = savedpc; /* XXX Restore PC -- 68{EC,LC}040 only */
258
259 if (fpu_debug_level & DL_VERBOSE)
260 printf("EXITING fpu_emulate: w/FPSR=%08x, FPCR=%08x\n",
261 fe.fe_fpsr, fe.fe_fpcr);
262
263 return (sig);
264 }
265
266 /* update accrued exception bits and see if there's an FP exception */
267 int
268 fpu_upd_excp(fe)
269 struct fpemu *fe;
270 {
271 u_int fpsr;
272 u_int fpcr;
273
274 fpsr = fe->fe_fpsr;
275 fpcr = fe->fe_fpcr;
276 /* update fpsr accrued exception bits; each insn doesn't have to
277 update this */
278 if (fpsr & (FPSR_BSUN | FPSR_SNAN | FPSR_OPERR)) {
279 fpsr |= FPSR_AIOP;
280 }
281 if (fpsr & FPSR_OVFL) {
282 fpsr |= FPSR_AOVFL;
283 }
284 if ((fpsr & FPSR_UNFL) && (fpsr & FPSR_INEX2)) {
285 fpsr |= FPSR_AUNFL;
286 }
287 if (fpsr & FPSR_DZ) {
288 fpsr |= FPSR_ADZ;
289 }
290 if (fpsr & (FPSR_INEX1 | FPSR_INEX2 | FPSR_OVFL)) {
291 fpsr |= FPSR_AINEX;
292 }
293
294 fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
295
296 return (fpsr & fpcr & FPSR_EXCP) ? SIGFPE : 0;
297 }
298
299 /* update fpsr according to fp (= result of an fp op) */
300 u_int
301 fpu_upd_fpsr(fe, fp)
302 struct fpemu *fe;
303 struct fpn *fp;
304 {
305 u_int fpsr;
306
307 if (fpu_debug_level & DL_RESULT)
308 printf(" fpu_upd_fpsr: previous fpsr=%08x\n", fe->fe_fpsr);
309
310 /* clear all condition code */
311 fpsr = fe->fe_fpsr & ~FPSR_CCB;
312
313 if (fpu_debug_level & DL_RESULT)
314 printf(" fpu_upd_fpsr: result is a ");
315
316 if (fp->fp_sign) {
317 if (fpu_debug_level & DL_RESULT)
318 printf("negative ");
319 fpsr |= FPSR_NEG;
320 } else {
321 if (fpu_debug_level & DL_RESULT)
322 printf("positive ");
323 }
324
325 switch (fp->fp_class) {
326 case FPC_SNAN:
327 if (fpu_debug_level & DL_RESULT)
328 printf("signaling NAN\n");
329 fpsr |= (FPSR_NAN | FPSR_SNAN);
330 break;
331 case FPC_QNAN:
332 if (fpu_debug_level & DL_RESULT)
333 printf("quiet NAN\n");
334 fpsr |= FPSR_NAN;
335 break;
336 case FPC_ZERO:
337 if (fpu_debug_level & DL_RESULT)
338 printf("Zero\n");
339 fpsr |= FPSR_ZERO;
340 break;
341 case FPC_INF:
342 if (fpu_debug_level & DL_RESULT)
343 printf("Inf\n");
344 fpsr |= FPSR_INF;
345 break;
346 default:
347 if (fpu_debug_level & DL_RESULT)
348 printf("Number\n");
349 /* anything else is treated as if it is a number */
350 break;
351 }
352
353 fe->fe_fpsr = fe->fe_fpframe->fpf_fpsr = fpsr;
354
355 if (fpu_debug_level & DL_RESULT)
356 printf(" fpu_upd_fpsr: new fpsr=%08x\n", fe->fe_fpframe->fpf_fpsr);
357
358 return fpsr;
359 }
360
361 static int
362 fpu_emul_fmovmcr(fe, insn)
363 struct fpemu *fe;
364 struct instruction *insn;
365 {
366 struct frame *frame = fe->fe_frame;
367 struct fpframe *fpf = fe->fe_fpframe;
368 int sig;
369 int reglist;
370 int fpu_to_mem;
371
372 /* move to/from control registers */
373 reglist = (insn->is_word1 & 0x1c00) >> 10;
374 /* Bit 13 selects direction (FPU to/from Mem) */
375 fpu_to_mem = insn->is_word1 & 0x2000;
376
377 insn->is_datasize = 4;
378 insn->is_advance = 4;
379 sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
380 if (sig) { return sig; }
381
382 if (reglist != 1 && reglist != 2 && reglist != 4 &&
383 (insn->is_ea0.ea_flags & EA_DIRECT)) {
384 /* attempted to copy more than one FPcr to CPU regs */
385 #ifdef DEBUG
386 printf(" fpu_emul_fmovmcr: tried to copy too many FPcr\n");
387 #endif
388 return SIGILL;
389 }
390
391 if (reglist & 4) {
392 /* fpcr */
393 if ((insn->is_ea0.ea_flags & EA_DIRECT) &&
394 insn->is_ea0.ea_regnum >= 8 /* address reg */) {
395 /* attempted to copy FPCR to An */
396 #ifdef DEBUG
397 printf(" fpu_emul_fmovmcr: tried to copy FPCR from/to A%d\n",
398 insn->is_ea0.ea_regnum & 7);
399 #endif
400 return SIGILL;
401 }
402 if (fpu_to_mem) {
403 sig = fpu_store_ea(frame, insn, &insn->is_ea0,
404 (char *)&fpf->fpf_fpcr);
405 } else {
406 sig = fpu_load_ea(frame, insn, &insn->is_ea0,
407 (char *)&fpf->fpf_fpcr);
408 }
409 }
410 if (sig) { return sig; }
411
412 if (reglist & 2) {
413 /* fpsr */
414 if ((insn->is_ea0.ea_flags & EA_DIRECT) &&
415 insn->is_ea0.ea_regnum >= 8 /* address reg */) {
416 /* attempted to copy FPSR to An */
417 #ifdef DEBUG
418 printf(" fpu_emul_fmovmcr: tried to copy FPSR from/to A%d\n",
419 insn->is_ea0.ea_regnum & 7);
420 #endif
421 return SIGILL;
422 }
423 if (fpu_to_mem) {
424 sig = fpu_store_ea(frame, insn, &insn->is_ea0,
425 (char *)&fpf->fpf_fpsr);
426 } else {
427 sig = fpu_load_ea(frame, insn, &insn->is_ea0,
428 (char *)&fpf->fpf_fpsr);
429 }
430 }
431 if (sig) { return sig; }
432
433 if (reglist & 1) {
434 /* fpiar - can be moved to/from An */
435 if (fpu_to_mem) {
436 sig = fpu_store_ea(frame, insn, &insn->is_ea0,
437 (char *)&fpf->fpf_fpiar);
438 } else {
439 sig = fpu_load_ea(frame, insn, &insn->is_ea0,
440 (char *)&fpf->fpf_fpiar);
441 }
442 }
443 return sig;
444 }
445
446 /*
447 * type 0: fmovem
448 * Separated out of fpu_emul_type0 for efficiency.
449 * In this function, we know:
450 * (opcode & 0x01C0) == 0
451 * (word1 & 0x8000) == 0x8000
452 *
453 * No conversion or rounding is done by this instruction,
454 * and the FPSR is not affected.
455 */
456 static int
457 fpu_emul_fmovm(fe, insn)
458 struct fpemu *fe;
459 struct instruction *insn;
460 {
461 struct frame *frame = fe->fe_frame;
462 struct fpframe *fpf = fe->fe_fpframe;
463 int word1, sig;
464 int reglist, regmask, regnum;
465 int fpu_to_mem, order;
466 int w1_post_incr;
467 int *fpregs;
468
469 insn->is_advance = 4;
470 insn->is_datasize = 12;
471 word1 = insn->is_word1;
472
473 /* Bit 13 selects direction (FPU to/from Mem) */
474 fpu_to_mem = word1 & 0x2000;
475
476 /*
477 * Bits 12,11 select register list mode:
478 * 0,0: Static reg list, pre-decr.
479 * 0,1: Dynamic reg list, pre-decr.
480 * 1,0: Static reg list, post-incr.
481 * 1,1: Dynamic reg list, post-incr
482 */
483 w1_post_incr = word1 & 0x1000;
484 if (word1 & 0x0800) {
485 /* dynamic reg list */
486 reglist = frame->f_regs[(word1 & 0x70) >> 4];
487 } else {
488 reglist = word1;
489 }
490 reglist &= 0xFF;
491
492 /* Get effective address. (modreg=opcode&077) */
493 sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
494 if (sig) { return sig; }
495
496 /* Get address of soft coprocessor regs. */
497 fpregs = &fpf->fpf_regs[0];
498
499 if (insn->is_ea0.ea_flags & EA_PREDECR) {
500 regnum = 7;
501 order = -1;
502 } else {
503 regnum = 0;
504 order = 1;
505 }
506
507 while ((0 <= regnum) && (regnum < 8)) {
508 if (w1_post_incr)
509 regmask = 0x80 >> regnum;
510 else
511 regmask = 1 << regnum;
512 if (regmask & reglist) {
513 if (fpu_to_mem) {
514 sig = fpu_store_ea(frame, insn, &insn->is_ea0,
515 (char*)&fpregs[regnum * 3]);
516 if (fpu_debug_level & DL_RESULT)
517 printf(" fpu_emul_fmovm: FP%d (%08x,%08x,%08x) saved\n",
518 regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
519 fpregs[regnum * 3 + 2]);
520 } else { /* mem to fpu */
521 sig = fpu_load_ea(frame, insn, &insn->is_ea0,
522 (char*)&fpregs[regnum * 3]);
523 if (fpu_debug_level & DL_RESULT)
524 printf(" fpu_emul_fmovm: FP%d (%08x,%08x,%08x) loaded\n",
525 regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
526 fpregs[regnum * 3 + 2]);
527 }
528 if (sig) { break; }
529 }
530 regnum += order;
531 }
532
533 return sig;
534 }
535
536 static struct fpn *
537 fpu_cmp(fe)
538 struct fpemu *fe;
539 {
540 struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
541
542 /* take care of special cases */
543 if (x->fp_class < 0 || y->fp_class < 0) {
544 /* if either of two is a SNAN, result is SNAN */
545 x->fp_class = (y->fp_class < x->fp_class) ? y->fp_class : x->fp_class;
546 } else if (x->fp_class == FPC_INF) {
547 if (y->fp_class == FPC_INF) {
548 /* both infinities */
549 if (x->fp_sign == y->fp_sign) {
550 x->fp_class = FPC_ZERO; /* return a signed zero */
551 } else {
552 x->fp_class = FPC_NUM; /* return a faked number w/x's sign */
553 x->fp_exp = 16383;
554 x->fp_mant[0] = FP_1;
555 }
556 } else {
557 /* y is a number */
558 x->fp_class = FPC_NUM; /* return a forged number w/x's sign */
559 x->fp_exp = 16383;
560 x->fp_mant[0] = FP_1;
561 }
562 } else if (y->fp_class == FPC_INF) {
563 /* x is a Num but y is an Inf */
564 /* return a forged number w/y's sign inverted */
565 x->fp_class = FPC_NUM;
566 x->fp_sign = !y->fp_sign;
567 x->fp_exp = 16383;
568 x->fp_mant[0] = FP_1;
569 } else {
570 /* x and y are both numbers or zeros, or pair of a number and a zero */
571 y->fp_sign = !y->fp_sign;
572 x = fpu_add(fe); /* (x - y) */
573 /*
574 * FCMP does not set Inf bit in CC, so return a forged number
575 * (value doesn't matter) if Inf is the result of fsub.
576 */
577 if (x->fp_class == FPC_INF) {
578 x->fp_class = FPC_NUM;
579 x->fp_exp = 16383;
580 x->fp_mant[0] = FP_1;
581 }
582 }
583 return x;
584 }
585
586 /*
587 * arithmetic oprations
588 */
589 static int
590 fpu_emul_arith(fe, insn)
591 struct fpemu *fe;
592 struct instruction *insn;
593 {
594 struct frame *frame = fe->fe_frame;
595 u_int *fpregs = &(fe->fe_fpframe->fpf_regs[0]);
596 struct fpn *res;
597 int word1, sig = 0;
598 int regnum, format;
599 int discard_result = 0;
600 u_int buf[3];
601 int flags;
602 char regname;
603
604 DUMP_INSN(insn);
605
606 if (fpu_debug_level & DL_ARITH) {
607 printf(" fpu_emul_arith: FPSR = %08x, FPCR = %08x\n",
608 fe->fe_fpsr, fe->fe_fpcr);
609 }
610
611 word1 = insn->is_word1;
612 format = (word1 >> 10) & 7;
613 regnum = (word1 >> 7) & 7;
614
615 /* fetch a source operand : may not be used */
616 if (fpu_debug_level & DL_ARITH) {
617 printf(" fpu_emul_arith: dst/src FP%d=%08x,%08x,%08x\n",
618 regnum, fpregs[regnum*3], fpregs[regnum*3+1],
619 fpregs[regnum*3+2]);
620 }
621 fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, &fpregs[regnum * 3]);
622
623 DUMP_INSN(insn);
624
625 /* get the other operand which is always the source */
626 if ((word1 & 0x4000) == 0) {
627 if (fpu_debug_level & DL_ARITH) {
628 printf(" fpu_emul_arith: FP%d op FP%d => FP%d\n",
629 format, regnum, regnum);
630 printf(" fpu_emul_arith: src opr FP%d=%08x,%08x,%08x\n",
631 format, fpregs[format*3], fpregs[format*3+1],
632 fpregs[format*3+2]);
633 }
634 fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
635 } else {
636 /* the operand is in memory */
637 if (format == FTYPE_DBL) {
638 insn->is_datasize = 8;
639 } else if (format == FTYPE_SNG || format == FTYPE_LNG) {
640 insn->is_datasize = 4;
641 } else if (format == FTYPE_WRD) {
642 insn->is_datasize = 2;
643 } else if (format == FTYPE_BYT) {
644 insn->is_datasize = 1;
645 } else if (format == FTYPE_EXT) {
646 insn->is_datasize = 12;
647 } else {
648 /* invalid or unsupported operand format */
649 sig = SIGFPE;
650 return sig;
651 }
652
653 /* Get effective address. (modreg=opcode&077) */
654 sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
655 if (sig) {
656 if (fpu_debug_level & DL_ARITH) {
657 printf(" fpu_emul_arith: error in fpu_decode_ea\n");
658 }
659 return sig;
660 }
661
662 DUMP_INSN(insn);
663
664 if (fpu_debug_level & DL_ARITH) {
665 printf(" fpu_emul_arith: addr mode = ");
666 flags = insn->is_ea0.ea_flags;
667 regname = (insn->is_ea0.ea_regnum & 8) ? 'a' : 'd';
668
669 if (flags & EA_DIRECT) {
670 printf("%c%d\n",
671 regname, insn->is_ea0.ea_regnum & 7);
672 } else if (flags & EA_PC_REL) {
673 if (flags & EA_OFFSET) {
674 printf("pc@(%d)\n", insn->is_ea0.ea_offset);
675 } else if (flags & EA_INDEXED) {
676 printf("pc@(...)\n");
677 }
678 } else if (flags & EA_PREDECR) {
679 printf("%c%d@-\n",
680 regname, insn->is_ea0.ea_regnum & 7);
681 } else if (flags & EA_POSTINCR) {
682 printf("%c%d@+\n", regname, insn->is_ea0.ea_regnum & 7);
683 } else if (flags & EA_OFFSET) {
684 printf("%c%d@(%d)\n", regname, insn->is_ea0.ea_regnum & 7,
685 insn->is_ea0.ea_offset);
686 } else if (flags & EA_INDEXED) {
687 printf("%c%d@(...)\n", regname, insn->is_ea0.ea_regnum & 7);
688 } else if (flags & EA_ABS) {
689 printf("0x%08x\n", insn->is_ea0.ea_absaddr);
690 } else if (flags & EA_IMMED) {
691
692 printf("#0x%08x,%08x,%08x\n", insn->is_ea0.ea_immed[0],
693 insn->is_ea0.ea_immed[1], insn->is_ea0.ea_immed[2]);
694 } else {
695 printf("%c%d@\n", regname, insn->is_ea0.ea_regnum & 7);
696 }
697 } /* if (fpu_debug_level & DL_ARITH) */
698
699 fpu_load_ea(frame, insn, &insn->is_ea0, (char*)buf);
700 if (format == FTYPE_WRD) {
701 /* sign-extend */
702 buf[0] &= 0xffff;
703 if (buf[0] & 0x8000) {
704 buf[0] |= 0xffff0000;
705 }
706 format = FTYPE_LNG;
707 } else if (format == FTYPE_BYT) {
708 /* sign-extend */
709 buf[0] &= 0xff;
710 if (buf[0] & 0x80) {
711 buf[0] |= 0xffffff00;
712 }
713 format = FTYPE_LNG;
714 }
715 if (fpu_debug_level & DL_ARITH) {
716 printf(" fpu_emul_arith: src = %08x %08x %08x, siz = %d\n",
717 buf[0], buf[1], buf[2], insn->is_datasize);
718 }
719 fpu_explode(fe, &fe->fe_f2, format, buf);
720 }
721
722 DUMP_INSN(insn);
723
724 /* An arithmetic instruction emulate function has a prototype of
725 * struct fpn *fpu_op(struct fpemu *);
726
727 * 1) If the instruction is monadic, then fpu_op() must use
728 * fe->fe_f2 as its operand, and return a pointer to the
729 * result.
730
731 * 2) If the instruction is diadic, then fpu_op() must use
732 * fe->fe_f1 and fe->fe_f2 as its two operands, and return a
733 * pointer to the result.
734
735 */
736 res = 0;
737 switch (word1 & 0x3f) {
738 case 0x00: /* fmove */
739 res = &fe->fe_f2;
740 break;
741
742 case 0x01: /* fint */
743 res = fpu_int(fe);
744 break;
745
746 case 0x02: /* fsinh */
747 res = fpu_sinh(fe);
748 break;
749
750 case 0x03: /* fintrz */
751 res = fpu_intrz(fe);
752 break;
753
754 case 0x04: /* fsqrt */
755 res = fpu_sqrt(fe);
756 break;
757
758 case 0x06: /* flognp1 */
759 res = fpu_lognp1(fe);
760 break;
761
762 case 0x08: /* fetoxm1 */
763 res = fpu_etoxm1(fe);
764 break;
765
766 case 0x09: /* ftanh */
767 res = fpu_tanh(fe);
768 break;
769
770 case 0x0A: /* fatan */
771 res = fpu_atan(fe);
772 break;
773
774 case 0x0C: /* fasin */
775 res = fpu_asin(fe);
776 break;
777
778 case 0x0D: /* fatanh */
779 res = fpu_atanh(fe);
780 break;
781
782 case 0x0E: /* fsin */
783 res = fpu_sin(fe);
784 break;
785
786 case 0x0F: /* ftan */
787 res = fpu_tan(fe);
788 break;
789
790 case 0x10: /* fetox */
791 res = fpu_etox(fe);
792 break;
793
794 case 0x11: /* ftwotox */
795 res = fpu_twotox(fe);
796 break;
797
798 case 0x12: /* ftentox */
799 res = fpu_tentox(fe);
800 break;
801
802 case 0x14: /* flogn */
803 res = fpu_logn(fe);
804 break;
805
806 case 0x15: /* flog10 */
807 res = fpu_log10(fe);
808 break;
809
810 case 0x16: /* flog2 */
811 res = fpu_log2(fe);
812 break;
813
814 case 0x18: /* fabs */
815 fe->fe_f2.fp_sign = 0;
816 res = &fe->fe_f2;
817 break;
818
819 case 0x19: /* fcosh */
820 res = fpu_cosh(fe);
821 break;
822
823 case 0x1A: /* fneg */
824 fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign;
825 res = &fe->fe_f2;
826 break;
827
828 case 0x1C: /* facos */
829 res = fpu_acos(fe);
830 break;
831
832 case 0x1D: /* fcos */
833 res = fpu_cos(fe);
834 break;
835
836 case 0x1E: /* fgetexp */
837 res = fpu_getexp(fe);
838 break;
839
840 case 0x1F: /* fgetman */
841 res = fpu_getman(fe);
842 break;
843
844 case 0x20: /* fdiv */
845 case 0x24: /* fsgldiv: cheating - better than nothing */
846 res = fpu_div(fe);
847 break;
848
849 case 0x21: /* fmod */
850 res = fpu_mod(fe);
851 break;
852
853 case 0x28: /* fsub */
854 fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign; /* f2 = -f2 */
855 case 0x22: /* fadd */
856 res = fpu_add(fe);
857 break;
858
859 case 0x23: /* fmul */
860 case 0x27: /* fsglmul: cheating - better than nothing */
861 res = fpu_mul(fe);
862 break;
863
864 case 0x25: /* frem */
865 res = fpu_rem(fe);
866 break;
867
868 case 0x26:
869 /* fscale is handled by a separate function */
870 break;
871
872 case 0x30:
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