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