fpu.c revision 1.16 1 1.16 pk /* $NetBSD: fpu.c,v 1.16 2003/01/06 18:32:33 pk Exp $ */
2 1.2 deraadt
3 1.1 deraadt /*
4 1.1 deraadt * Copyright (c) 1992, 1993
5 1.1 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 deraadt *
7 1.1 deraadt * This software was developed by the Computer Systems Engineering group
8 1.1 deraadt * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 deraadt * contributed to Berkeley.
10 1.1 deraadt *
11 1.1 deraadt * All advertising materials mentioning features or use of this software
12 1.1 deraadt * must display the following acknowledgement:
13 1.1 deraadt * This product includes software developed by the University of
14 1.1 deraadt * California, Lawrence Berkeley Laboratory.
15 1.1 deraadt *
16 1.1 deraadt * Redistribution and use in source and binary forms, with or without
17 1.1 deraadt * modification, are permitted provided that the following conditions
18 1.1 deraadt * are met:
19 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
20 1.1 deraadt * notice, this list of conditions and the following disclaimer.
21 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
23 1.1 deraadt * documentation and/or other materials provided with the distribution.
24 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
25 1.1 deraadt * must display the following acknowledgement:
26 1.1 deraadt * This product includes software developed by the University of
27 1.1 deraadt * California, Berkeley and its contributors.
28 1.1 deraadt * 4. Neither the name of the University nor the names of its contributors
29 1.1 deraadt * may be used to endorse or promote products derived from this software
30 1.1 deraadt * without specific prior written permission.
31 1.1 deraadt *
32 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 deraadt * SUCH DAMAGE.
43 1.1 deraadt *
44 1.1 deraadt * @(#)fpu.c 8.1 (Berkeley) 6/11/93
45 1.1 deraadt */
46 1.1 deraadt
47 1.1 deraadt #include <sys/param.h>
48 1.1 deraadt #include <sys/proc.h>
49 1.1 deraadt #include <sys/signal.h>
50 1.1 deraadt #include <sys/systm.h>
51 1.1 deraadt #include <sys/syslog.h>
52 1.3 christos #include <sys/signalvar.h>
53 1.1 deraadt
54 1.1 deraadt #include <machine/instr.h>
55 1.1 deraadt #include <machine/reg.h>
56 1.1 deraadt
57 1.1 deraadt #include <sparc/fpu/fpu_emu.h>
58 1.3 christos #include <sparc/fpu/fpu_extern.h>
59 1.1 deraadt
60 1.8 eeh int fpe_debug = 0;
61 1.8 eeh
62 1.8 eeh #ifdef DEBUG
63 1.8 eeh /*
64 1.8 eeh * Dump a `fpn' structure.
65 1.8 eeh */
66 1.8 eeh void
67 1.8 eeh fpu_dumpfpn(struct fpn *fp)
68 1.8 eeh {
69 1.8 eeh static char *class[] = {
70 1.8 eeh "SNAN", "QNAN", "ZERO", "NUM", "INF"
71 1.8 eeh };
72 1.8 eeh
73 1.8 eeh printf("%s %c.%x %x %x %xE%d", class[fp->fp_class + 2],
74 1.8 eeh fp->fp_sign ? '-' : ' ',
75 1.8 eeh fp->fp_mant[0], fp->fp_mant[1],
76 1.8 eeh fp->fp_mant[2], fp->fp_mant[3],
77 1.8 eeh fp->fp_exp);
78 1.8 eeh }
79 1.8 eeh #endif
80 1.8 eeh
81 1.1 deraadt /*
82 1.1 deraadt * fpu_execute returns the following error numbers (0 = no error):
83 1.1 deraadt */
84 1.1 deraadt #define FPE 1 /* take a floating point exception */
85 1.1 deraadt #define NOTFPU 2 /* not an FPU instruction */
86 1.1 deraadt
87 1.1 deraadt /*
88 1.1 deraadt * Translate current exceptions into `first' exception. The
89 1.1 deraadt * bits go the wrong way for ffs() (0x10 is most important, etc).
90 1.1 deraadt * There are only 5, so do it the obvious way.
91 1.1 deraadt */
92 1.1 deraadt #define X1(x) x
93 1.1 deraadt #define X2(x) x,x
94 1.1 deraadt #define X4(x) x,x,x,x
95 1.1 deraadt #define X8(x) X4(x),X4(x)
96 1.1 deraadt #define X16(x) X8(x),X8(x)
97 1.1 deraadt
98 1.1 deraadt static char cx_to_trapx[] = {
99 1.1 deraadt X1(FSR_NX),
100 1.1 deraadt X2(FSR_DZ),
101 1.1 deraadt X4(FSR_UF),
102 1.1 deraadt X8(FSR_OF),
103 1.1 deraadt X16(FSR_NV)
104 1.1 deraadt };
105 1.1 deraadt static u_char fpu_codes[] = {
106 1.1 deraadt X1(FPE_FLTINEX_TRAP),
107 1.1 deraadt X2(FPE_FLTDIV_TRAP),
108 1.1 deraadt X4(FPE_FLTUND_TRAP),
109 1.1 deraadt X8(FPE_FLTOVF_TRAP),
110 1.1 deraadt X16(FPE_FLTOPERR_TRAP)
111 1.1 deraadt };
112 1.1 deraadt
113 1.1 deraadt /*
114 1.1 deraadt * The FPU gave us an exception. Clean up the mess. Note that the
115 1.1 deraadt * fp queue can only have FPops in it, never load/store FP registers
116 1.1 deraadt * nor FBfcc instructions. Experiments with `crashme' prove that
117 1.1 deraadt * unknown FPops do enter the queue, however.
118 1.1 deraadt */
119 1.3 christos void
120 1.1 deraadt fpu_cleanup(p, fs)
121 1.1 deraadt register struct proc *p;
122 1.7 mrg #ifndef SUN4U
123 1.1 deraadt register struct fpstate *fs;
124 1.7 mrg #else /* SUN4U */
125 1.7 mrg register struct fpstate64 *fs;
126 1.7 mrg #endif /* SUN4U */
127 1.1 deraadt {
128 1.1 deraadt register int i, fsr = fs->fs_fsr, error;
129 1.1 deraadt union instr instr;
130 1.1 deraadt struct fpemu fe;
131 1.1 deraadt
132 1.1 deraadt switch ((fsr >> FSR_FTT_SHIFT) & FSR_FTT_MASK) {
133 1.1 deraadt
134 1.1 deraadt case FSR_TT_NONE:
135 1.7 mrg panic("fpu_cleanup: No fault"); /* ??? */
136 1.1 deraadt break;
137 1.1 deraadt
138 1.1 deraadt case FSR_TT_IEEE:
139 1.14 eeh DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_IEEE\n"));
140 1.1 deraadt /* XXX missing trap address! */
141 1.1 deraadt if ((i = fsr & FSR_CX) == 0)
142 1.1 deraadt panic("fpu ieee trap, but no exception");
143 1.16 pk KERNEL_PROC_LOCK(p);
144 1.1 deraadt trapsignal(p, SIGFPE, fpu_codes[i - 1]);
145 1.16 pk KERNEL_PROC_UNLOCK(p);
146 1.1 deraadt break; /* XXX should return, but queue remains */
147 1.1 deraadt
148 1.1 deraadt case FSR_TT_UNFIN:
149 1.14 eeh DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_UNFIN\n"));
150 1.7 mrg #ifdef SUN4U
151 1.7 mrg if (fs->fs_qsize == 0) {
152 1.7 mrg printf("fpu_cleanup: unfinished fpop");
153 1.7 mrg /* The book sez reexecute or emulate. */
154 1.7 mrg return;
155 1.7 mrg }
156 1.7 mrg break;
157 1.7 mrg
158 1.7 mrg #endif /* SUN4U */
159 1.1 deraadt case FSR_TT_UNIMP:
160 1.14 eeh DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_UNIMP\n"));
161 1.1 deraadt if (fs->fs_qsize == 0)
162 1.7 mrg panic("fpu_cleanup: unimplemented fpop");
163 1.1 deraadt break;
164 1.1 deraadt
165 1.1 deraadt case FSR_TT_SEQ:
166 1.1 deraadt panic("fpu sequence error");
167 1.1 deraadt /* NOTREACHED */
168 1.1 deraadt
169 1.1 deraadt case FSR_TT_HWERR:
170 1.14 eeh DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_HWERR\n"));
171 1.1 deraadt log(LOG_ERR, "fpu hardware error (%s[%d])\n",
172 1.1 deraadt p->p_comm, p->p_pid);
173 1.1 deraadt uprintf("%s[%d]: fpu hardware error\n", p->p_comm, p->p_pid);
174 1.16 pk KERNEL_PROC_LOCK(p);
175 1.1 deraadt trapsignal(p, SIGFPE, -1); /* ??? */
176 1.16 pk KERNEL_PROC_UNLOCK(p);
177 1.1 deraadt goto out;
178 1.1 deraadt
179 1.1 deraadt default:
180 1.6 fair printf("fsr=0x%x\n", fsr);
181 1.1 deraadt panic("fpu error");
182 1.1 deraadt }
183 1.1 deraadt
184 1.1 deraadt /* emulate the instructions left in the queue */
185 1.1 deraadt fe.fe_fpstate = fs;
186 1.1 deraadt for (i = 0; i < fs->fs_qsize; i++) {
187 1.1 deraadt instr.i_int = fs->fs_queue[i].fq_instr;
188 1.1 deraadt if (instr.i_any.i_op != IOP_reg ||
189 1.1 deraadt (instr.i_op3.i_op3 != IOP3_FPop1 &&
190 1.1 deraadt instr.i_op3.i_op3 != IOP3_FPop2))
191 1.1 deraadt panic("bogus fpu queue");
192 1.1 deraadt error = fpu_execute(&fe, instr);
193 1.16 pk if (error == 0)
194 1.1 deraadt continue;
195 1.1 deraadt
196 1.16 pk KERNEL_PROC_LOCK(p);
197 1.16 pk switch (error) {
198 1.1 deraadt case FPE:
199 1.1 deraadt trapsignal(p, SIGFPE,
200 1.1 deraadt fpu_codes[(fs->fs_fsr & FSR_CX) - 1]);
201 1.1 deraadt break;
202 1.1 deraadt
203 1.1 deraadt case NOTFPU:
204 1.7 mrg #ifdef SUN4U
205 1.7 mrg #ifdef DEBUG
206 1.11 mrg printf("fpu_cleanup: not an FPU error -- sending SIGILL\n");
207 1.7 mrg Debugger();
208 1.7 mrg #endif
209 1.7 mrg #endif /* SUN4U */
210 1.1 deraadt trapsignal(p, SIGILL, 0); /* ??? code? */
211 1.1 deraadt break;
212 1.1 deraadt
213 1.1 deraadt default:
214 1.1 deraadt panic("fpu_cleanup 3");
215 1.1 deraadt /* NOTREACHED */
216 1.1 deraadt }
217 1.16 pk KERNEL_PROC_UNLOCK(p);
218 1.1 deraadt /* XXX should stop here, but queue remains */
219 1.1 deraadt }
220 1.1 deraadt out:
221 1.1 deraadt fs->fs_qsize = 0;
222 1.1 deraadt }
223 1.1 deraadt
224 1.1 deraadt #ifdef notyet
225 1.1 deraadt /*
226 1.1 deraadt * If we have no FPU at all (are there any machines like this out
227 1.1 deraadt * there!?) we have to emulate each instruction, and we need a pointer
228 1.1 deraadt * to the trapframe so that we can step over them and do FBfcc's.
229 1.1 deraadt * We know the `queue' is empty, though; we just want to emulate
230 1.1 deraadt * the instruction at tf->tf_pc.
231 1.1 deraadt */
232 1.1 deraadt fpu_emulate(p, tf, fs)
233 1.1 deraadt struct proc *p;
234 1.1 deraadt register struct trapframe *tf;
235 1.7 mrg #ifndef SUN4U
236 1.1 deraadt register struct fpstate *fs;
237 1.7 mrg #else /* SUN4U */
238 1.7 mrg register struct fpstate64 *fs;
239 1.7 mrg #endif /* SUN4U */
240 1.1 deraadt {
241 1.1 deraadt
242 1.1 deraadt do {
243 1.1 deraadt fetch instr from pc
244 1.1 deraadt decode
245 1.1 deraadt if (integer instr) {
246 1.1 deraadt /*
247 1.1 deraadt * We do this here, rather than earlier, to avoid
248 1.1 deraadt * losing even more badly than usual.
249 1.1 deraadt */
250 1.1 deraadt if (p->p_addr->u_pcb.pcb_uw) {
251 1.1 deraadt write_user_windows();
252 1.1 deraadt if (rwindow_save(p))
253 1.1 deraadt sigexit(p, SIGILL);
254 1.1 deraadt }
255 1.1 deraadt if (loadstore) {
256 1.1 deraadt do_it;
257 1.1 deraadt pc = npc, npc += 4
258 1.1 deraadt } else if (fbfcc) {
259 1.1 deraadt do_annul_stuff;
260 1.1 deraadt } else
261 1.1 deraadt return;
262 1.1 deraadt } else if (fpu instr) {
263 1.1 deraadt fe.fe_fsr = fs->fs_fsr &= ~FSR_CX;
264 1.1 deraadt error = fpu_execute(&fe, fs, instr);
265 1.1 deraadt switch (error) {
266 1.1 deraadt etc;
267 1.1 deraadt }
268 1.1 deraadt } else
269 1.1 deraadt return;
270 1.1 deraadt if (want to reschedule)
271 1.1 deraadt return;
272 1.1 deraadt } while (error == 0);
273 1.1 deraadt }
274 1.1 deraadt #endif
275 1.1 deraadt
276 1.1 deraadt /*
277 1.1 deraadt * Execute an FPU instruction (one that runs entirely in the FPU; not
278 1.1 deraadt * FBfcc or STF, for instance). On return, fe->fe_fs->fs_fsr will be
279 1.1 deraadt * modified to reflect the setting the hardware would have left.
280 1.1 deraadt *
281 1.1 deraadt * Note that we do not catch all illegal opcodes, so you can, for instance,
282 1.1 deraadt * multiply two integers this way.
283 1.1 deraadt */
284 1.1 deraadt int
285 1.1 deraadt fpu_execute(fe, instr)
286 1.1 deraadt register struct fpemu *fe;
287 1.1 deraadt union instr instr;
288 1.1 deraadt {
289 1.1 deraadt register struct fpn *fp;
290 1.7 mrg #ifndef SUN4U
291 1.1 deraadt register int opf, rs1, rs2, rd, type, mask, fsr, cx;
292 1.1 deraadt register struct fpstate *fs;
293 1.7 mrg #else /* SUN4U */
294 1.7 mrg register int opf, rs1, rs2, rd, type, mask, fsr, cx, i, cond;
295 1.7 mrg register struct fpstate64 *fs;
296 1.7 mrg #endif /* SUN4U */
297 1.1 deraadt u_int space[4];
298 1.14 eeh
299 1.1 deraadt /*
300 1.1 deraadt * `Decode' and execute instruction. Start with no exceptions.
301 1.1 deraadt * The type of any i_opf opcode is in the bottom two bits, so we
302 1.1 deraadt * squish them out here.
303 1.1 deraadt */
304 1.1 deraadt opf = instr.i_opf.i_opf;
305 1.12 eeh /*
306 1.12 eeh * The low two bits of the opf field for floating point insns usually
307 1.12 eeh * correspond to the operation width:
308 1.12 eeh *
309 1.12 eeh * 0: Invalid
310 1.12 eeh * 1: Single precision float
311 1.12 eeh * 2: Double precision float
312 1.12 eeh * 3: Quad precision float
313 1.12 eeh *
314 1.12 eeh * The exceptions are the integer to float conversion instructions.
315 1.12 eeh *
316 1.12 eeh * For double and quad precision, the low bit if the rs or rd field
317 1.12 eeh * is actually the high bit of the register number.
318 1.12 eeh */
319 1.12 eeh
320 1.1 deraadt type = opf & 3;
321 1.12 eeh mask = 0x3 >> (3 - type);
322 1.12 eeh
323 1.12 eeh rs1 = instr.i_opf.i_rs1;
324 1.12 eeh rs1 = (rs1 & ~mask) | ((rs1 & mask & 0x1) << 5);
325 1.12 eeh rs2 = instr.i_opf.i_rs2;
326 1.12 eeh rs2 = (rs2 & ~mask) | ((rs2 & mask & 0x1) << 5);
327 1.12 eeh rd = instr.i_opf.i_rd;
328 1.12 eeh rd = (rd & ~mask) | ((rd & mask & 0x1) << 5);
329 1.12 eeh #ifdef DIAGNOSTIC
330 1.1 deraadt if ((rs1 | rs2 | rd) & mask)
331 1.13 eeh /* This may be an FPU insn but it is illegal. */
332 1.13 eeh return (NOTFPU);
333 1.1 deraadt #endif
334 1.1 deraadt fs = fe->fe_fpstate;
335 1.1 deraadt fe->fe_fsr = fs->fs_fsr & ~FSR_CX;
336 1.1 deraadt fe->fe_cx = 0;
337 1.7 mrg #ifdef SUN4U
338 1.7 mrg /*
339 1.7 mrg * Check to see if we're dealing with a fancy cmove and handle
340 1.12 eeh * it first.
341 1.7 mrg */
342 1.7 mrg if (instr.i_op3.i_op3 == IOP3_FPop2 && (opf&0xff0) != (FCMP&0xff0)) {
343 1.7 mrg switch (opf >>= 2) {
344 1.7 mrg case FMVFC0 >> 2:
345 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVFC0\n"));
346 1.7 mrg cond = (fs->fs_fsr>>FSR_FCC_SHIFT)&FSR_FCC_MASK;
347 1.7 mrg if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
348 1.7 mrg rs1 = fs->fs_regs[rs2];
349 1.7 mrg goto mov;
350 1.7 mrg case FMVFC1 >> 2:
351 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVFC1\n"));
352 1.7 mrg cond = (fs->fs_fsr>>FSR_FCC1_SHIFT)&FSR_FCC_MASK;
353 1.7 mrg if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
354 1.7 mrg rs1 = fs->fs_regs[rs2];
355 1.7 mrg goto mov;
356 1.7 mrg case FMVFC2 >> 2:
357 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVFC2\n"));
358 1.7 mrg cond = (fs->fs_fsr>>FSR_FCC2_SHIFT)&FSR_FCC_MASK;
359 1.7 mrg if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
360 1.7 mrg rs1 = fs->fs_regs[rs2];
361 1.7 mrg goto mov;
362 1.7 mrg case FMVFC3 >> 2:
363 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVFC3\n"));
364 1.7 mrg cond = (fs->fs_fsr>>FSR_FCC3_SHIFT)&FSR_FCC_MASK;
365 1.7 mrg if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
366 1.7 mrg rs1 = fs->fs_regs[rs2];
367 1.7 mrg goto mov;
368 1.7 mrg case FMVIC >> 2:
369 1.7 mrg /* Presume we're curproc */
370 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVIC\n"));
371 1.7 mrg cond = (curproc->p_md.md_tf->tf_tstate>>TSTATE_CCR_SHIFT)&PSR_ICC;
372 1.7 mrg if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
373 1.7 mrg rs1 = fs->fs_regs[rs2];
374 1.7 mrg goto mov;
375 1.7 mrg case FMVXC >> 2:
376 1.7 mrg /* Presume we're curproc */
377 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVXC\n"));
378 1.7 mrg cond = (curproc->p_md.md_tf->tf_tstate>>(TSTATE_CCR_SHIFT+XCC_SHIFT))&PSR_ICC;
379 1.7 mrg if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
380 1.7 mrg rs1 = fs->fs_regs[rs2];
381 1.7 mrg goto mov;
382 1.7 mrg case FMVRZ >> 2:
383 1.7 mrg /* Presume we're curproc */
384 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVRZ\n"));
385 1.7 mrg rs1 = instr.i_fmovr.i_rs1;
386 1.7 mrg if (rs1 != 0 && (int64_t)curproc->p_md.md_tf->tf_global[rs1] != 0)
387 1.7 mrg return (0); /* success */
388 1.7 mrg rs1 = fs->fs_regs[rs2];
389 1.7 mrg goto mov;
390 1.7 mrg case FMVRLEZ >> 2:
391 1.7 mrg /* Presume we're curproc */
392 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVRLEZ\n"));
393 1.7 mrg rs1 = instr.i_fmovr.i_rs1;
394 1.7 mrg if (rs1 != 0 && (int64_t)curproc->p_md.md_tf->tf_global[rs1] > 0)
395 1.7 mrg return (0); /* success */
396 1.7 mrg rs1 = fs->fs_regs[rs2];
397 1.7 mrg goto mov;
398 1.7 mrg case FMVRLZ >> 2:
399 1.7 mrg /* Presume we're curproc */
400 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVRLZ\n"));
401 1.7 mrg rs1 = instr.i_fmovr.i_rs1;
402 1.7 mrg if (rs1 == 0 || (int64_t)curproc->p_md.md_tf->tf_global[rs1] >= 0)
403 1.7 mrg return (0); /* success */
404 1.7 mrg rs1 = fs->fs_regs[rs2];
405 1.7 mrg goto mov;
406 1.7 mrg case FMVRNZ >> 2:
407 1.7 mrg /* Presume we're curproc */
408 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVRNZ\n"));
409 1.7 mrg rs1 = instr.i_fmovr.i_rs1;
410 1.7 mrg if (rs1 == 0 || (int64_t)curproc->p_md.md_tf->tf_global[rs1] == 0)
411 1.7 mrg return (0); /* success */
412 1.7 mrg rs1 = fs->fs_regs[rs2];
413 1.7 mrg goto mov;
414 1.7 mrg case FMVRGZ >> 2:
415 1.7 mrg /* Presume we're curproc */
416 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVRGZ\n"));
417 1.7 mrg rs1 = instr.i_fmovr.i_rs1;
418 1.7 mrg if (rs1 == 0 || (int64_t)curproc->p_md.md_tf->tf_global[rs1] <= 0)
419 1.7 mrg return (0); /* success */
420 1.7 mrg rs1 = fs->fs_regs[rs2];
421 1.7 mrg goto mov;
422 1.7 mrg case FMVRGEZ >> 2:
423 1.7 mrg /* Presume we're curproc */
424 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMVRGEZ\n"));
425 1.7 mrg rs1 = instr.i_fmovr.i_rs1;
426 1.7 mrg if (rs1 != 0 && (int64_t)curproc->p_md.md_tf->tf_global[rs1] < 0)
427 1.7 mrg return (0); /* success */
428 1.7 mrg rs1 = fs->fs_regs[rs2];
429 1.8 eeh goto mov;
430 1.7 mrg default:
431 1.8 eeh DPRINTF(FPE_INSN,
432 1.8 eeh ("fpu_execute: unknown v9 FP inst %x opf %x\n",
433 1.8 eeh instr.i_int, opf));
434 1.7 mrg return (NOTFPU);
435 1.7 mrg }
436 1.7 mrg }
437 1.7 mrg #endif /* SUN4U */
438 1.1 deraadt switch (opf >>= 2) {
439 1.1 deraadt
440 1.1 deraadt default:
441 1.8 eeh DPRINTF(FPE_INSN,
442 1.8 eeh ("fpu_execute: unknown basic FP inst %x opf %x\n",
443 1.8 eeh instr.i_int, opf));
444 1.1 deraadt return (NOTFPU);
445 1.1 deraadt
446 1.1 deraadt case FMOV >> 2: /* these should all be pretty obvious */
447 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMOV\n"));
448 1.1 deraadt rs1 = fs->fs_regs[rs2];
449 1.1 deraadt goto mov;
450 1.1 deraadt
451 1.1 deraadt case FNEG >> 2:
452 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FNEG\n"));
453 1.1 deraadt rs1 = fs->fs_regs[rs2] ^ (1 << 31);
454 1.1 deraadt goto mov;
455 1.1 deraadt
456 1.1 deraadt case FABS >> 2:
457 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
458 1.1 deraadt rs1 = fs->fs_regs[rs2] & ~(1 << 31);
459 1.1 deraadt mov:
460 1.7 mrg #ifndef SUN4U
461 1.1 deraadt fs->fs_regs[rd] = rs1;
462 1.7 mrg #else /* SUN4U */
463 1.12 eeh i = 1<<(type-1);
464 1.7 mrg fs->fs_regs[rd++] = rs1;
465 1.12 eeh while (--i > 0)
466 1.7 mrg fs->fs_regs[rd++] = fs->fs_regs[++rs2];
467 1.7 mrg #endif /* SUN4U */
468 1.1 deraadt fs->fs_fsr = fe->fe_fsr;
469 1.1 deraadt return (0); /* success */
470 1.1 deraadt
471 1.1 deraadt case FSQRT >> 2:
472 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FSQRT\n"));
473 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs2);
474 1.1 deraadt fp = fpu_sqrt(fe);
475 1.1 deraadt break;
476 1.1 deraadt
477 1.1 deraadt case FADD >> 2:
478 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FADD\n"));
479 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
480 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
481 1.1 deraadt fp = fpu_add(fe);
482 1.1 deraadt break;
483 1.1 deraadt
484 1.1 deraadt case FSUB >> 2:
485 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FSUB\n"));
486 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
487 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
488 1.1 deraadt fp = fpu_sub(fe);
489 1.1 deraadt break;
490 1.1 deraadt
491 1.1 deraadt case FMUL >> 2:
492 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FMUL\n"));
493 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
494 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
495 1.1 deraadt fp = fpu_mul(fe);
496 1.1 deraadt break;
497 1.1 deraadt
498 1.1 deraadt case FDIV >> 2:
499 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FDIV\n"));
500 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
501 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
502 1.1 deraadt fp = fpu_div(fe);
503 1.1 deraadt break;
504 1.1 deraadt
505 1.1 deraadt case FCMP >> 2:
506 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FCMP\n"));
507 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
508 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
509 1.1 deraadt fpu_compare(fe, 0);
510 1.1 deraadt goto cmpdone;
511 1.1 deraadt
512 1.1 deraadt case FCMPE >> 2:
513 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FCMPE\n"));
514 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
515 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
516 1.1 deraadt fpu_compare(fe, 1);
517 1.1 deraadt cmpdone:
518 1.1 deraadt /*
519 1.1 deraadt * The only possible exception here is NV; catch it
520 1.1 deraadt * early and get out, as there is no result register.
521 1.1 deraadt */
522 1.1 deraadt cx = fe->fe_cx;
523 1.1 deraadt fsr = fe->fe_fsr | (cx << FSR_CX_SHIFT);
524 1.1 deraadt if (cx != 0) {
525 1.1 deraadt if (fsr & (FSR_NV << FSR_TEM_SHIFT)) {
526 1.1 deraadt fs->fs_fsr = (fsr & ~FSR_FTT) |
527 1.1 deraadt (FSR_TT_IEEE << FSR_FTT_SHIFT);
528 1.1 deraadt return (FPE);
529 1.1 deraadt }
530 1.1 deraadt fsr |= FSR_NV << FSR_AX_SHIFT;
531 1.1 deraadt }
532 1.1 deraadt fs->fs_fsr = fsr;
533 1.1 deraadt return (0);
534 1.1 deraadt
535 1.1 deraadt case FSMULD >> 2:
536 1.1 deraadt case FDMULX >> 2:
537 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FSMULx\n"));
538 1.1 deraadt if (type == FTYPE_EXT)
539 1.1 deraadt return (NOTFPU);
540 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
541 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
542 1.1 deraadt type++; /* single to double, or double to quad */
543 1.1 deraadt fp = fpu_mul(fe);
544 1.1 deraadt break;
545 1.1 deraadt
546 1.7 mrg #ifdef SUN4U
547 1.7 mrg case FXTOS >> 2:
548 1.7 mrg case FXTOD >> 2:
549 1.7 mrg case FXTOQ >> 2:
550 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FXTOx\n"));
551 1.7 mrg type = FTYPE_LNG;
552 1.7 mrg fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
553 1.7 mrg type = opf & 3; /* sneaky; depends on instruction encoding */
554 1.7 mrg break;
555 1.7 mrg
556 1.7 mrg case FTOX >> 2:
557 1.14 eeh DPRINTF(FPE_INSN, ("fpu_execute: FTOX\n"));
558 1.7 mrg fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
559 1.7 mrg type = FTYPE_LNG;
560 1.14 eeh /* Recalculate destination register */
561 1.14 eeh rd = instr.i_opf.i_rd;
562 1.8 eeh break;
563 1.14 eeh
564 1.7 mrg #endif /* SUN4U */
565 1.14 eeh case FTOI >> 2:
566 1.14 eeh DPRINTF(FPE_INSN, ("fpu_execute: FTOI\n"));
567 1.14 eeh fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
568 1.14 eeh type = FTYPE_INT;
569 1.14 eeh /* Recalculate destination register */
570 1.14 eeh rd = instr.i_opf.i_rd;
571 1.14 eeh break;
572 1.7 mrg
573 1.1 deraadt case FTOS >> 2:
574 1.1 deraadt case FTOD >> 2:
575 1.7 mrg case FTOQ >> 2:
576 1.8 eeh DPRINTF(FPE_INSN, ("fpu_execute: FTOx\n"));
577 1.1 deraadt fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
578 1.14 eeh /* Recalculate rd with correct type info. */
579 1.1 deraadt type = opf & 3; /* sneaky; depends on instruction encoding */
580 1.14 eeh mask = 0x3 >> (3 - type);
581 1.14 eeh rd = instr.i_opf.i_rd;
582 1.14 eeh rd = (rd & ~mask) | ((rd & mask & 0x1) << 5);
583 1.1 deraadt break;
584 1.1 deraadt }
585 1.1 deraadt
586 1.1 deraadt /*
587 1.1 deraadt * ALU operation is complete. Collapse the result and then check
588 1.1 deraadt * for exceptions. If we got any, and they are enabled, do not
589 1.1 deraadt * alter the destination register, just stop with an exception.
590 1.1 deraadt * Otherwise set new current exceptions and accrue.
591 1.1 deraadt */
592 1.1 deraadt fpu_implode(fe, fp, type, space);
593 1.1 deraadt cx = fe->fe_cx;
594 1.1 deraadt fsr = fe->fe_fsr;
595 1.1 deraadt if (cx != 0) {
596 1.1 deraadt mask = (fsr >> FSR_TEM_SHIFT) & FSR_TEM_MASK;
597 1.1 deraadt if (cx & mask) {
598 1.1 deraadt /* not accrued??? */
599 1.1 deraadt fs->fs_fsr = (fsr & ~FSR_FTT) |
600 1.1 deraadt (FSR_TT_IEEE << FSR_FTT_SHIFT) |
601 1.1 deraadt (cx_to_trapx[(cx & mask) - 1] << FSR_CX_SHIFT);
602 1.1 deraadt return (FPE);
603 1.1 deraadt }
604 1.1 deraadt fsr |= (cx << FSR_CX_SHIFT) | (cx << FSR_AX_SHIFT);
605 1.1 deraadt }
606 1.1 deraadt fs->fs_fsr = fsr;
607 1.14 eeh DPRINTF(FPE_REG, ("-> %c%d\n", (type == FTYPE_LNG) ? 'x' :
608 1.14 eeh ((type == FTYPE_INT) ? 'i' :
609 1.14 eeh ((type == FTYPE_SNG) ? 's' :
610 1.14 eeh ((type == FTYPE_DBL) ? 'd' :
611 1.14 eeh ((type == FTYPE_EXT) ? 'q' : '?')))),
612 1.14 eeh rd));
613 1.1 deraadt fs->fs_regs[rd] = space[0];
614 1.10 pk if (type >= FTYPE_DBL || type == FTYPE_LNG) {
615 1.1 deraadt fs->fs_regs[rd + 1] = space[1];
616 1.1 deraadt if (type > FTYPE_DBL) {
617 1.1 deraadt fs->fs_regs[rd + 2] = space[2];
618 1.1 deraadt fs->fs_regs[rd + 3] = space[3];
619 1.1 deraadt }
620 1.1 deraadt }
621 1.1 deraadt return (0); /* success */
622 1.1 deraadt }
623