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