fpu.c revision 1.1 1 1.1 deraadt /*
2 1.1 deraadt * Copyright (c) 1992, 1993
3 1.1 deraadt * The Regents of the University of California. All rights reserved.
4 1.1 deraadt *
5 1.1 deraadt * This software was developed by the Computer Systems Engineering group
6 1.1 deraadt * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 1.1 deraadt * contributed to Berkeley.
8 1.1 deraadt *
9 1.1 deraadt * All advertising materials mentioning features or use of this software
10 1.1 deraadt * must display the following acknowledgement:
11 1.1 deraadt * This product includes software developed by the University of
12 1.1 deraadt * California, Lawrence Berkeley Laboratory.
13 1.1 deraadt *
14 1.1 deraadt * Redistribution and use in source and binary forms, with or without
15 1.1 deraadt * modification, are permitted provided that the following conditions
16 1.1 deraadt * are met:
17 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
18 1.1 deraadt * notice, this list of conditions and the following disclaimer.
19 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
21 1.1 deraadt * documentation and/or other materials provided with the distribution.
22 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
23 1.1 deraadt * must display the following acknowledgement:
24 1.1 deraadt * This product includes software developed by the University of
25 1.1 deraadt * California, Berkeley and its contributors.
26 1.1 deraadt * 4. Neither the name of the University nor the names of its contributors
27 1.1 deraadt * may be used to endorse or promote products derived from this software
28 1.1 deraadt * without specific prior written permission.
29 1.1 deraadt *
30 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 1.1 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.1 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 1.1 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.1 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.1 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.1 deraadt * SUCH DAMAGE.
41 1.1 deraadt *
42 1.1 deraadt * @(#)fpu.c 8.1 (Berkeley) 6/11/93
43 1.1 deraadt *
44 1.1 deraadt * from: Header: fpu.c,v 1.3 92/11/26 01:39:42 torek Exp
45 1.1 deraadt * $Id: fpu.c,v 1.1 1993/10/02 10:22:50 deraadt Exp $
46 1.1 deraadt */
47 1.1 deraadt
48 1.1 deraadt #include <sys/param.h>
49 1.1 deraadt #include <sys/proc.h>
50 1.1 deraadt #include <sys/signal.h>
51 1.1 deraadt #include <sys/systm.h>
52 1.1 deraadt #include <sys/syslog.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.1 deraadt
59 1.1 deraadt /*
60 1.1 deraadt * fpu_execute returns the following error numbers (0 = no error):
61 1.1 deraadt */
62 1.1 deraadt #define FPE 1 /* take a floating point exception */
63 1.1 deraadt #define NOTFPU 2 /* not an FPU instruction */
64 1.1 deraadt
65 1.1 deraadt /*
66 1.1 deraadt * Translate current exceptions into `first' exception. The
67 1.1 deraadt * bits go the wrong way for ffs() (0x10 is most important, etc).
68 1.1 deraadt * There are only 5, so do it the obvious way.
69 1.1 deraadt */
70 1.1 deraadt #define X1(x) x
71 1.1 deraadt #define X2(x) x,x
72 1.1 deraadt #define X4(x) x,x,x,x
73 1.1 deraadt #define X8(x) X4(x),X4(x)
74 1.1 deraadt #define X16(x) X8(x),X8(x)
75 1.1 deraadt
76 1.1 deraadt static char cx_to_trapx[] = {
77 1.1 deraadt X1(FSR_NX),
78 1.1 deraadt X2(FSR_DZ),
79 1.1 deraadt X4(FSR_UF),
80 1.1 deraadt X8(FSR_OF),
81 1.1 deraadt X16(FSR_NV)
82 1.1 deraadt };
83 1.1 deraadt static u_char fpu_codes[] = {
84 1.1 deraadt X1(FPE_FLTINEX_TRAP),
85 1.1 deraadt X2(FPE_FLTDIV_TRAP),
86 1.1 deraadt X4(FPE_FLTUND_TRAP),
87 1.1 deraadt X8(FPE_FLTOVF_TRAP),
88 1.1 deraadt X16(FPE_FLTOPERR_TRAP)
89 1.1 deraadt };
90 1.1 deraadt
91 1.1 deraadt /*
92 1.1 deraadt * The FPU gave us an exception. Clean up the mess. Note that the
93 1.1 deraadt * fp queue can only have FPops in it, never load/store FP registers
94 1.1 deraadt * nor FBfcc instructions. Experiments with `crashme' prove that
95 1.1 deraadt * unknown FPops do enter the queue, however.
96 1.1 deraadt */
97 1.1 deraadt fpu_cleanup(p, fs)
98 1.1 deraadt register struct proc *p;
99 1.1 deraadt register struct fpstate *fs;
100 1.1 deraadt {
101 1.1 deraadt register int i, fsr = fs->fs_fsr, error;
102 1.1 deraadt union instr instr;
103 1.1 deraadt struct fpemu fe;
104 1.1 deraadt
105 1.1 deraadt switch ((fsr >> FSR_FTT_SHIFT) & FSR_FTT_MASK) {
106 1.1 deraadt
107 1.1 deraadt case FSR_TT_NONE:
108 1.1 deraadt panic("fpu_cleanup 1"); /* ??? */
109 1.1 deraadt break;
110 1.1 deraadt
111 1.1 deraadt case FSR_TT_IEEE:
112 1.1 deraadt /* XXX missing trap address! */
113 1.1 deraadt if ((i = fsr & FSR_CX) == 0)
114 1.1 deraadt panic("fpu ieee trap, but no exception");
115 1.1 deraadt trapsignal(p, SIGFPE, fpu_codes[i - 1]);
116 1.1 deraadt break; /* XXX should return, but queue remains */
117 1.1 deraadt
118 1.1 deraadt case FSR_TT_UNFIN:
119 1.1 deraadt case FSR_TT_UNIMP:
120 1.1 deraadt if (fs->fs_qsize == 0)
121 1.1 deraadt panic("fpu_cleanup 2");
122 1.1 deraadt break;
123 1.1 deraadt
124 1.1 deraadt case FSR_TT_SEQ:
125 1.1 deraadt panic("fpu sequence error");
126 1.1 deraadt /* NOTREACHED */
127 1.1 deraadt
128 1.1 deraadt case FSR_TT_HWERR:
129 1.1 deraadt log(LOG_ERR, "fpu hardware error (%s[%d])\n",
130 1.1 deraadt p->p_comm, p->p_pid);
131 1.1 deraadt uprintf("%s[%d]: fpu hardware error\n", p->p_comm, p->p_pid);
132 1.1 deraadt trapsignal(p, SIGFPE, -1); /* ??? */
133 1.1 deraadt goto out;
134 1.1 deraadt
135 1.1 deraadt default:
136 1.1 deraadt printf("fsr=%x\n", fsr);
137 1.1 deraadt panic("fpu error");
138 1.1 deraadt }
139 1.1 deraadt
140 1.1 deraadt /* emulate the instructions left in the queue */
141 1.1 deraadt fe.fe_fpstate = fs;
142 1.1 deraadt for (i = 0; i < fs->fs_qsize; i++) {
143 1.1 deraadt instr.i_int = fs->fs_queue[i].fq_instr;
144 1.1 deraadt if (instr.i_any.i_op != IOP_reg ||
145 1.1 deraadt (instr.i_op3.i_op3 != IOP3_FPop1 &&
146 1.1 deraadt instr.i_op3.i_op3 != IOP3_FPop2))
147 1.1 deraadt panic("bogus fpu queue");
148 1.1 deraadt error = fpu_execute(&fe, instr);
149 1.1 deraadt switch (error) {
150 1.1 deraadt
151 1.1 deraadt case 0:
152 1.1 deraadt continue;
153 1.1 deraadt
154 1.1 deraadt case FPE:
155 1.1 deraadt trapsignal(p, SIGFPE,
156 1.1 deraadt fpu_codes[(fs->fs_fsr & FSR_CX) - 1]);
157 1.1 deraadt break;
158 1.1 deraadt
159 1.1 deraadt case NOTFPU:
160 1.1 deraadt trapsignal(p, SIGILL, 0); /* ??? code? */
161 1.1 deraadt break;
162 1.1 deraadt
163 1.1 deraadt default:
164 1.1 deraadt panic("fpu_cleanup 3");
165 1.1 deraadt /* NOTREACHED */
166 1.1 deraadt }
167 1.1 deraadt /* XXX should stop here, but queue remains */
168 1.1 deraadt }
169 1.1 deraadt out:
170 1.1 deraadt fs->fs_qsize = 0;
171 1.1 deraadt }
172 1.1 deraadt
173 1.1 deraadt #ifdef notyet
174 1.1 deraadt /*
175 1.1 deraadt * If we have no FPU at all (are there any machines like this out
176 1.1 deraadt * there!?) we have to emulate each instruction, and we need a pointer
177 1.1 deraadt * to the trapframe so that we can step over them and do FBfcc's.
178 1.1 deraadt * We know the `queue' is empty, though; we just want to emulate
179 1.1 deraadt * the instruction at tf->tf_pc.
180 1.1 deraadt */
181 1.1 deraadt fpu_emulate(p, tf, fs)
182 1.1 deraadt struct proc *p;
183 1.1 deraadt register struct trapframe *tf;
184 1.1 deraadt register struct fpstate *fs;
185 1.1 deraadt {
186 1.1 deraadt
187 1.1 deraadt do {
188 1.1 deraadt fetch instr from pc
189 1.1 deraadt decode
190 1.1 deraadt if (integer instr) {
191 1.1 deraadt /*
192 1.1 deraadt * We do this here, rather than earlier, to avoid
193 1.1 deraadt * losing even more badly than usual.
194 1.1 deraadt */
195 1.1 deraadt if (p->p_addr->u_pcb.pcb_uw) {
196 1.1 deraadt write_user_windows();
197 1.1 deraadt if (rwindow_save(p))
198 1.1 deraadt sigexit(p, SIGILL);
199 1.1 deraadt }
200 1.1 deraadt if (loadstore) {
201 1.1 deraadt do_it;
202 1.1 deraadt pc = npc, npc += 4
203 1.1 deraadt } else if (fbfcc) {
204 1.1 deraadt do_annul_stuff;
205 1.1 deraadt } else
206 1.1 deraadt return;
207 1.1 deraadt } else if (fpu instr) {
208 1.1 deraadt fe.fe_fsr = fs->fs_fsr &= ~FSR_CX;
209 1.1 deraadt error = fpu_execute(&fe, fs, instr);
210 1.1 deraadt switch (error) {
211 1.1 deraadt etc;
212 1.1 deraadt }
213 1.1 deraadt } else
214 1.1 deraadt return;
215 1.1 deraadt if (want to reschedule)
216 1.1 deraadt return;
217 1.1 deraadt } while (error == 0);
218 1.1 deraadt }
219 1.1 deraadt #endif
220 1.1 deraadt
221 1.1 deraadt /*
222 1.1 deraadt * Execute an FPU instruction (one that runs entirely in the FPU; not
223 1.1 deraadt * FBfcc or STF, for instance). On return, fe->fe_fs->fs_fsr will be
224 1.1 deraadt * modified to reflect the setting the hardware would have left.
225 1.1 deraadt *
226 1.1 deraadt * Note that we do not catch all illegal opcodes, so you can, for instance,
227 1.1 deraadt * multiply two integers this way.
228 1.1 deraadt */
229 1.1 deraadt int
230 1.1 deraadt fpu_execute(fe, instr)
231 1.1 deraadt register struct fpemu *fe;
232 1.1 deraadt union instr instr;
233 1.1 deraadt {
234 1.1 deraadt register struct fpn *fp;
235 1.1 deraadt register int opf, rs1, rs2, rd, type, mask, fsr, cx;
236 1.1 deraadt register struct fpstate *fs;
237 1.1 deraadt u_int space[4];
238 1.1 deraadt
239 1.1 deraadt /*
240 1.1 deraadt * `Decode' and execute instruction. Start with no exceptions.
241 1.1 deraadt * The type of any i_opf opcode is in the bottom two bits, so we
242 1.1 deraadt * squish them out here.
243 1.1 deraadt */
244 1.1 deraadt opf = instr.i_opf.i_opf;
245 1.1 deraadt type = opf & 3;
246 1.1 deraadt mask = "\0\0\1\3"[type];
247 1.1 deraadt rs1 = instr.i_opf.i_rs1 & ~mask;
248 1.1 deraadt rs2 = instr.i_opf.i_rs2 & ~mask;
249 1.1 deraadt rd = instr.i_opf.i_rd & ~mask;
250 1.1 deraadt #ifdef notdef
251 1.1 deraadt if ((rs1 | rs2 | rd) & mask)
252 1.1 deraadt return (BADREG);
253 1.1 deraadt #endif
254 1.1 deraadt fs = fe->fe_fpstate;
255 1.1 deraadt fe->fe_fsr = fs->fs_fsr & ~FSR_CX;
256 1.1 deraadt fe->fe_cx = 0;
257 1.1 deraadt switch (opf >>= 2) {
258 1.1 deraadt
259 1.1 deraadt default:
260 1.1 deraadt return (NOTFPU);
261 1.1 deraadt
262 1.1 deraadt case FMOV >> 2: /* these should all be pretty obvious */
263 1.1 deraadt rs1 = fs->fs_regs[rs2];
264 1.1 deraadt goto mov;
265 1.1 deraadt
266 1.1 deraadt case FNEG >> 2:
267 1.1 deraadt rs1 = fs->fs_regs[rs2] ^ (1 << 31);
268 1.1 deraadt goto mov;
269 1.1 deraadt
270 1.1 deraadt case FABS >> 2:
271 1.1 deraadt rs1 = fs->fs_regs[rs2] & ~(1 << 31);
272 1.1 deraadt mov:
273 1.1 deraadt fs->fs_regs[rd] = rs1;
274 1.1 deraadt fs->fs_fsr = fe->fe_fsr;
275 1.1 deraadt return (0); /* success */
276 1.1 deraadt
277 1.1 deraadt case FSQRT >> 2:
278 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs2);
279 1.1 deraadt fp = fpu_sqrt(fe);
280 1.1 deraadt break;
281 1.1 deraadt
282 1.1 deraadt case FADD >> 2:
283 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
284 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
285 1.1 deraadt fp = fpu_add(fe);
286 1.1 deraadt break;
287 1.1 deraadt
288 1.1 deraadt case FSUB >> 2:
289 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
290 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
291 1.1 deraadt fp = fpu_sub(fe);
292 1.1 deraadt break;
293 1.1 deraadt
294 1.1 deraadt case FMUL >> 2:
295 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
296 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
297 1.1 deraadt fp = fpu_mul(fe);
298 1.1 deraadt break;
299 1.1 deraadt
300 1.1 deraadt case FDIV >> 2:
301 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
302 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
303 1.1 deraadt fp = fpu_div(fe);
304 1.1 deraadt break;
305 1.1 deraadt
306 1.1 deraadt case FCMP >> 2:
307 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
308 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
309 1.1 deraadt fpu_compare(fe, 0);
310 1.1 deraadt goto cmpdone;
311 1.1 deraadt
312 1.1 deraadt case FCMPE >> 2:
313 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
314 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
315 1.1 deraadt fpu_compare(fe, 1);
316 1.1 deraadt cmpdone:
317 1.1 deraadt /*
318 1.1 deraadt * The only possible exception here is NV; catch it
319 1.1 deraadt * early and get out, as there is no result register.
320 1.1 deraadt */
321 1.1 deraadt cx = fe->fe_cx;
322 1.1 deraadt fsr = fe->fe_fsr | (cx << FSR_CX_SHIFT);
323 1.1 deraadt if (cx != 0) {
324 1.1 deraadt if (fsr & (FSR_NV << FSR_TEM_SHIFT)) {
325 1.1 deraadt fs->fs_fsr = (fsr & ~FSR_FTT) |
326 1.1 deraadt (FSR_TT_IEEE << FSR_FTT_SHIFT);
327 1.1 deraadt return (FPE);
328 1.1 deraadt }
329 1.1 deraadt fsr |= FSR_NV << FSR_AX_SHIFT;
330 1.1 deraadt }
331 1.1 deraadt fs->fs_fsr = fsr;
332 1.1 deraadt return (0);
333 1.1 deraadt
334 1.1 deraadt case FSMULD >> 2:
335 1.1 deraadt case FDMULX >> 2:
336 1.1 deraadt if (type == FTYPE_EXT)
337 1.1 deraadt return (NOTFPU);
338 1.1 deraadt fpu_explode(fe, &fe->fe_f1, type, rs1);
339 1.1 deraadt fpu_explode(fe, &fe->fe_f2, type, rs2);
340 1.1 deraadt type++; /* single to double, or double to quad */
341 1.1 deraadt fp = fpu_mul(fe);
342 1.1 deraadt break;
343 1.1 deraadt
344 1.1 deraadt case FTOS >> 2:
345 1.1 deraadt case FTOD >> 2:
346 1.1 deraadt case FTOX >> 2:
347 1.1 deraadt case FTOI >> 2:
348 1.1 deraadt fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
349 1.1 deraadt type = opf & 3; /* sneaky; depends on instruction encoding */
350 1.1 deraadt break;
351 1.1 deraadt }
352 1.1 deraadt
353 1.1 deraadt /*
354 1.1 deraadt * ALU operation is complete. Collapse the result and then check
355 1.1 deraadt * for exceptions. If we got any, and they are enabled, do not
356 1.1 deraadt * alter the destination register, just stop with an exception.
357 1.1 deraadt * Otherwise set new current exceptions and accrue.
358 1.1 deraadt */
359 1.1 deraadt fpu_implode(fe, fp, type, space);
360 1.1 deraadt cx = fe->fe_cx;
361 1.1 deraadt fsr = fe->fe_fsr;
362 1.1 deraadt if (cx != 0) {
363 1.1 deraadt mask = (fsr >> FSR_TEM_SHIFT) & FSR_TEM_MASK;
364 1.1 deraadt if (cx & mask) {
365 1.1 deraadt /* not accrued??? */
366 1.1 deraadt fs->fs_fsr = (fsr & ~FSR_FTT) |
367 1.1 deraadt (FSR_TT_IEEE << FSR_FTT_SHIFT) |
368 1.1 deraadt (cx_to_trapx[(cx & mask) - 1] << FSR_CX_SHIFT);
369 1.1 deraadt return (FPE);
370 1.1 deraadt }
371 1.1 deraadt fsr |= (cx << FSR_CX_SHIFT) | (cx << FSR_AX_SHIFT);
372 1.1 deraadt }
373 1.1 deraadt fs->fs_fsr = fsr;
374 1.1 deraadt fs->fs_regs[rd] = space[0];
375 1.1 deraadt if (type >= FTYPE_DBL) {
376 1.1 deraadt fs->fs_regs[rd + 1] = space[1];
377 1.1 deraadt if (type > FTYPE_DBL) {
378 1.1 deraadt fs->fs_regs[rd + 2] = space[2];
379 1.1 deraadt fs->fs_regs[rd + 3] = space[3];
380 1.1 deraadt }
381 1.1 deraadt }
382 1.1 deraadt return (0); /* success */
383 1.1 deraadt }
384