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