fpu_emu.c revision 1.8 1 1.8 lukem /* $NetBSD: fpu_emu.c,v 1.8 2003/07/15 02:54:43 lukem Exp $ */
2 1.1 simonb
3 1.1 simonb /*
4 1.1 simonb * Copyright 2001 Wasabi Systems, Inc.
5 1.1 simonb * All rights reserved.
6 1.1 simonb *
7 1.1 simonb * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 1.1 simonb *
9 1.1 simonb * Redistribution and use in source and binary forms, with or without
10 1.1 simonb * modification, are permitted provided that the following conditions
11 1.1 simonb * are met:
12 1.1 simonb * 1. Redistributions of source code must retain the above copyright
13 1.1 simonb * notice, this list of conditions and the following disclaimer.
14 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 simonb * notice, this list of conditions and the following disclaimer in the
16 1.1 simonb * documentation and/or other materials provided with the distribution.
17 1.1 simonb * 3. All advertising materials mentioning features or use of this software
18 1.1 simonb * must display the following acknowledgement:
19 1.1 simonb * This product includes software developed for the NetBSD Project by
20 1.1 simonb * Wasabi Systems, Inc.
21 1.1 simonb * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 simonb * or promote products derived from this software without specific prior
23 1.1 simonb * written permission.
24 1.1 simonb *
25 1.1 simonb * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 simonb * POSSIBILITY OF SUCH DAMAGE.
36 1.1 simonb */
37 1.1 simonb
38 1.1 simonb /*
39 1.1 simonb * Copyright (c) 1992, 1993
40 1.1 simonb * The Regents of the University of California. All rights reserved.
41 1.1 simonb *
42 1.1 simonb * This software was developed by the Computer Systems Engineering group
43 1.1 simonb * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
44 1.1 simonb * contributed to Berkeley.
45 1.1 simonb *
46 1.1 simonb * All advertising materials mentioning features or use of this software
47 1.1 simonb * must display the following acknowledgement:
48 1.1 simonb * This product includes software developed by the University of
49 1.1 simonb * California, Lawrence Berkeley Laboratory.
50 1.1 simonb *
51 1.1 simonb * Redistribution and use in source and binary forms, with or without
52 1.1 simonb * modification, are permitted provided that the following conditions
53 1.1 simonb * are met:
54 1.1 simonb * 1. Redistributions of source code must retain the above copyright
55 1.1 simonb * notice, this list of conditions and the following disclaimer.
56 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright
57 1.1 simonb * notice, this list of conditions and the following disclaimer in the
58 1.1 simonb * documentation and/or other materials provided with the distribution.
59 1.1 simonb * 3. All advertising materials mentioning features or use of this software
60 1.1 simonb * must display the following acknowledgement:
61 1.1 simonb * This product includes software developed by the University of
62 1.1 simonb * California, Berkeley and its contributors.
63 1.1 simonb * 4. Neither the name of the University nor the names of its contributors
64 1.1 simonb * may be used to endorse or promote products derived from this software
65 1.1 simonb * without specific prior written permission.
66 1.1 simonb *
67 1.1 simonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 1.1 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 1.1 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 1.1 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 1.1 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 1.1 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 1.1 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 1.1 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 1.1 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 1.1 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 1.1 simonb * SUCH DAMAGE.
78 1.1 simonb *
79 1.1 simonb * @(#)fpu.c 8.1 (Berkeley) 6/11/93
80 1.1 simonb */
81 1.8 lukem
82 1.8 lukem #include <sys/cdefs.h>
83 1.8 lukem __KERNEL_RCSID(0, "$NetBSD: fpu_emu.c,v 1.8 2003/07/15 02:54:43 lukem Exp $");
84 1.1 simonb
85 1.1 simonb #include "opt_ddb.h"
86 1.1 simonb
87 1.1 simonb #include <sys/param.h>
88 1.1 simonb #include <sys/proc.h>
89 1.1 simonb #include <sys/signal.h>
90 1.1 simonb #include <sys/systm.h>
91 1.1 simonb #include <sys/syslog.h>
92 1.1 simonb #include <sys/signalvar.h>
93 1.4 thorpej #include <sys/device.h> /* for evcnt */
94 1.1 simonb
95 1.1 simonb #include <powerpc/instr.h>
96 1.1 simonb #include <machine/reg.h>
97 1.1 simonb #include <machine/fpu.h>
98 1.1 simonb
99 1.1 simonb #include <powerpc/fpu/fpu_emu.h>
100 1.1 simonb #include <powerpc/fpu/fpu_extern.h>
101 1.1 simonb
102 1.4 thorpej #define FPU_EMU_EVCNT_DECL(name) \
103 1.4 thorpej static struct evcnt fpu_emu_ev_##name = \
104 1.4 thorpej EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "fpemu", #name); \
105 1.4 thorpej EVCNT_ATTACH_STATIC(fpu_emu_ev_##name)
106 1.4 thorpej
107 1.4 thorpej #define FPU_EMU_EVCNT_INCR(name) \
108 1.5 thorpej fpu_emu_ev_##name.ev_count++
109 1.4 thorpej
110 1.4 thorpej FPU_EMU_EVCNT_DECL(stfiwx);
111 1.4 thorpej FPU_EMU_EVCNT_DECL(fpstore);
112 1.4 thorpej FPU_EMU_EVCNT_DECL(fpload);
113 1.4 thorpej FPU_EMU_EVCNT_DECL(fcmpu);
114 1.4 thorpej FPU_EMU_EVCNT_DECL(frsp);
115 1.4 thorpej FPU_EMU_EVCNT_DECL(fctiw);
116 1.4 thorpej FPU_EMU_EVCNT_DECL(fcmpo);
117 1.4 thorpej FPU_EMU_EVCNT_DECL(mtfsb1);
118 1.4 thorpej FPU_EMU_EVCNT_DECL(fnegabs);
119 1.4 thorpej FPU_EMU_EVCNT_DECL(mcrfs);
120 1.4 thorpej FPU_EMU_EVCNT_DECL(mtfsb0);
121 1.4 thorpej FPU_EMU_EVCNT_DECL(fmr);
122 1.4 thorpej FPU_EMU_EVCNT_DECL(mtfsfi);
123 1.4 thorpej FPU_EMU_EVCNT_DECL(fnabs);
124 1.4 thorpej FPU_EMU_EVCNT_DECL(fabs);
125 1.4 thorpej FPU_EMU_EVCNT_DECL(mffs);
126 1.4 thorpej FPU_EMU_EVCNT_DECL(mtfsf);
127 1.4 thorpej FPU_EMU_EVCNT_DECL(fctid);
128 1.4 thorpej FPU_EMU_EVCNT_DECL(fcfid);
129 1.4 thorpej FPU_EMU_EVCNT_DECL(fdiv);
130 1.4 thorpej FPU_EMU_EVCNT_DECL(fsub);
131 1.4 thorpej FPU_EMU_EVCNT_DECL(fadd);
132 1.4 thorpej FPU_EMU_EVCNT_DECL(fsqrt);
133 1.4 thorpej FPU_EMU_EVCNT_DECL(fsel);
134 1.4 thorpej FPU_EMU_EVCNT_DECL(fpres);
135 1.4 thorpej FPU_EMU_EVCNT_DECL(fmul);
136 1.4 thorpej FPU_EMU_EVCNT_DECL(frsqrte);
137 1.4 thorpej FPU_EMU_EVCNT_DECL(fmulsub);
138 1.4 thorpej FPU_EMU_EVCNT_DECL(fmuladd);
139 1.4 thorpej FPU_EMU_EVCNT_DECL(fnmsub);
140 1.4 thorpej FPU_EMU_EVCNT_DECL(fnmadd);
141 1.1 simonb
142 1.1 simonb /* FPSR exception masks */
143 1.1 simonb #define FPSR_EX_MSK (FPSCR_VX|FPSCR_OX|FPSCR_UX|FPSCR_ZX| \
144 1.1 simonb FPSCR_XX|FPSCR_VXSNAN|FPSCR_VXISI|FPSCR_VXIDI| \
145 1.1 simonb FPSCR_VXZDZ|FPSCR_VXIMZ|FPSCR_VXVC|FPSCR_VXSOFT|\
146 1.1 simonb FPSCR_VXSQRT|FPSCR_VXCVI)
147 1.1 simonb #define FPSR_EX (FPSCR_VE|FPSCR_OE|FPSCR_UE|FPSCR_ZE|FPSCR_XE)
148 1.1 simonb #define FPSR_EXOP (FPSR_EX_MSK&(~FPSR_EX))
149 1.1 simonb
150 1.1 simonb
151 1.1 simonb int fpe_debug = 0;
152 1.1 simonb
153 1.1 simonb #ifdef DDB
154 1.1 simonb extern vaddr_t opc_disasm(vaddr_t loc, int opcode);
155 1.1 simonb #endif
156 1.1 simonb
157 1.1 simonb #ifdef DEBUG
158 1.1 simonb /*
159 1.1 simonb * Dump a `fpn' structure.
160 1.1 simonb */
161 1.1 simonb void
162 1.1 simonb fpu_dumpfpn(struct fpn *fp)
163 1.1 simonb {
164 1.1 simonb static char *class[] = {
165 1.1 simonb "SNAN", "QNAN", "ZERO", "NUM", "INF"
166 1.1 simonb };
167 1.1 simonb
168 1.1 simonb printf("%s %c.%x %x %x %xE%d", class[fp->fp_class + 2],
169 1.1 simonb fp->fp_sign ? '-' : ' ',
170 1.1 simonb fp->fp_mant[0], fp->fp_mant[1],
171 1.1 simonb fp->fp_mant[2], fp->fp_mant[3],
172 1.1 simonb fp->fp_exp);
173 1.1 simonb }
174 1.1 simonb #endif
175 1.1 simonb
176 1.1 simonb /*
177 1.1 simonb * fpu_execute returns the following error numbers (0 = no error):
178 1.1 simonb */
179 1.1 simonb #define FPE 1 /* take a floating point exception */
180 1.1 simonb #define NOTFPU 2 /* not an FPU instruction */
181 1.1 simonb #define FAULT 3
182 1.1 simonb
183 1.1 simonb
184 1.1 simonb /*
185 1.1 simonb * Emulate a floating-point instruction.
186 1.1 simonb * Return zero for success, else signal number.
187 1.1 simonb * (Typically: zero, SIGFPE, SIGILL, SIGSEGV)
188 1.1 simonb */
189 1.1 simonb int
190 1.1 simonb fpu_emulate(struct trapframe *frame, struct fpreg *fpf)
191 1.1 simonb {
192 1.1 simonb static union instr insn;
193 1.1 simonb static struct fpemu fe;
194 1.1 simonb static int lastill = 0;
195 1.1 simonb int sig;
196 1.1 simonb
197 1.1 simonb /* initialize insn.is_datasize to tell it is *not* initialized */
198 1.1 simonb fe.fe_fpstate = fpf;
199 1.1 simonb fe.fe_cx = 0;
200 1.1 simonb
201 1.1 simonb /* always set this (to avoid a warning) */
202 1.1 simonb
203 1.1 simonb if (copyin((void *) (frame->srr0), &insn.i_int, sizeof (insn.i_int))) {
204 1.1 simonb #ifdef DEBUG
205 1.1 simonb printf("fpu_emulate: fault reading opcode\n");
206 1.1 simonb #endif
207 1.1 simonb return SIGSEGV;
208 1.1 simonb }
209 1.1 simonb
210 1.1 simonb DPRINTF(FPE_EX, ("fpu_emulate: emulating insn %x at %p\n",
211 1.1 simonb insn.i_int, (void *)frame->srr0));
212 1.1 simonb
213 1.1 simonb
214 1.1 simonb if ((insn.i_any.i_opcd == OPC_TWI) ||
215 1.1 simonb ((insn.i_any.i_opcd == OPC_integer_31) &&
216 1.1 simonb (insn.i_x.i_xo == OPC31_TW))) {
217 1.1 simonb /* Check for the two trap insns. */
218 1.1 simonb DPRINTF(FPE_EX, ("fpu_emulate: SIGTRAP\n"));
219 1.1 simonb return (SIGTRAP);
220 1.1 simonb }
221 1.1 simonb sig = 0;
222 1.1 simonb switch (fpu_execute(frame, &fe, &insn)) {
223 1.1 simonb case 0:
224 1.1 simonb DPRINTF(FPE_EX, ("fpu_emulate: success\n"));
225 1.1 simonb frame->srr0 += 4;
226 1.1 simonb break;
227 1.1 simonb
228 1.1 simonb case FPE:
229 1.1 simonb DPRINTF(FPE_EX, ("fpu_emulate: SIGFPE\n"));
230 1.1 simonb sig = SIGFPE;
231 1.1 simonb break;
232 1.1 simonb
233 1.1 simonb case FAULT:
234 1.1 simonb DPRINTF(FPE_EX, ("fpu_emulate: SIGSEGV\n"));
235 1.1 simonb sig = SIGSEGV;
236 1.1 simonb break;
237 1.1 simonb
238 1.1 simonb case NOTFPU:
239 1.1 simonb default:
240 1.1 simonb DPRINTF(FPE_EX, ("fpu_emulate: SIGILL\n"));
241 1.2 simonb #ifdef DEBUG
242 1.1 simonb if (fpe_debug & FPE_EX) {
243 1.1 simonb printf("fpu_emulate: illegal insn %x at %p:",
244 1.1 simonb insn.i_int, (void *) (frame->srr0));
245 1.1 simonb opc_disasm((vaddr_t)(frame->srr0), insn.i_int);
246 1.1 simonb }
247 1.2 simonb #endif
248 1.1 simonb /*
249 1.1 simonb * XXXX retry an illegal insn once due to cache issues.
250 1.1 simonb */
251 1.1 simonb if (lastill == frame->srr0) {
252 1.1 simonb sig = SIGILL;
253 1.2 simonb #ifdef DEBUG
254 1.1 simonb if (fpe_debug & FPE_EX)
255 1.2 simonb Debugger();
256 1.2 simonb #endif
257 1.1 simonb }
258 1.1 simonb lastill = frame->srr0;
259 1.1 simonb break;
260 1.1 simonb }
261 1.1 simonb
262 1.1 simonb return (sig);
263 1.1 simonb }
264 1.1 simonb
265 1.1 simonb /*
266 1.1 simonb * Execute an FPU instruction (one that runs entirely in the FPU; not
267 1.1 simonb * FBfcc or STF, for instance). On return, fe->fe_fs->fs_fsr will be
268 1.1 simonb * modified to reflect the setting the hardware would have left.
269 1.1 simonb *
270 1.1 simonb * Note that we do not catch all illegal opcodes, so you can, for instance,
271 1.1 simonb * multiply two integers this way.
272 1.1 simonb */
273 1.1 simonb int
274 1.1 simonb fpu_execute(struct trapframe *tf, struct fpemu *fe, union instr *insn)
275 1.1 simonb {
276 1.1 simonb struct fpn *fp;
277 1.1 simonb union instr instr = *insn;
278 1.1 simonb int *a;
279 1.1 simonb vaddr_t addr;
280 1.1 simonb int ra, rb, rc, rt, type, mask, fsr, cx, bf, setcr, cond;
281 1.1 simonb struct fpreg *fs;
282 1.1 simonb
283 1.1 simonb /* Setup work. */
284 1.1 simonb fp = NULL;
285 1.1 simonb fs = fe->fe_fpstate;
286 1.1 simonb fe->fe_fpscr = ((int *)&fs->fpscr)[1];
287 1.1 simonb
288 1.1 simonb /*
289 1.1 simonb * On PowerPC all floating point values are stored in registers
290 1.1 simonb * as doubles, even when used for single precision operations.
291 1.1 simonb */
292 1.1 simonb type = FTYPE_DBL;
293 1.1 simonb cond = instr.i_any.i_rc;
294 1.1 simonb setcr = 0;
295 1.1 simonb
296 1.1 simonb #if defined(DDB) && defined(DEBUG)
297 1.1 simonb if (fpe_debug & FPE_EX) {
298 1.1 simonb vaddr_t loc = tf->srr0;
299 1.1 simonb
300 1.1 simonb printf("Trying to emulate: %p ", (void *)loc);
301 1.1 simonb opc_disasm(loc, instr.i_int);
302 1.1 simonb }
303 1.1 simonb #endif
304 1.1 simonb
305 1.1 simonb /*
306 1.1 simonb * `Decode' and execute instruction.
307 1.1 simonb */
308 1.1 simonb
309 1.1 simonb if ((instr.i_any.i_opcd >= OPC_LFS && instr.i_any.i_opcd <= OPC_STFDU) ||
310 1.1 simonb instr.i_any.i_opcd == OPC_integer_31) {
311 1.1 simonb /*
312 1.1 simonb * Handle load/store insns:
313 1.1 simonb *
314 1.1 simonb * Convert to/from single if needed, calculate addr,
315 1.1 simonb * and update index reg if needed.
316 1.1 simonb */
317 1.1 simonb double buf;
318 1.1 simonb size_t size = sizeof(float);
319 1.1 simonb int store, update;
320 1.1 simonb
321 1.1 simonb cond = 0; /* ld/st never set condition codes */
322 1.1 simonb
323 1.1 simonb
324 1.1 simonb if (instr.i_any.i_opcd == OPC_integer_31) {
325 1.1 simonb if (instr.i_x.i_xo == OPC31_STFIWX) {
326 1.4 thorpej FPU_EMU_EVCNT_INCR(stfiwx);
327 1.4 thorpej
328 1.1 simonb /* Store as integer */
329 1.1 simonb ra = instr.i_x.i_ra;
330 1.1 simonb rb = instr.i_x.i_rb;
331 1.7 thorpej DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n",
332 1.1 simonb ra, tf->fixreg[ra], rb, tf->fixreg[rb]));
333 1.1 simonb
334 1.1 simonb addr = tf->fixreg[rb];
335 1.1 simonb if (ra != 0)
336 1.1 simonb addr += tf->fixreg[ra];
337 1.1 simonb rt = instr.i_x.i_rt;
338 1.1 simonb a = (int *)&fs->fpreg[rt];
339 1.1 simonb DPRINTF(FPE_INSN,
340 1.1 simonb ("fpu_execute: Store INT %x at %p\n",
341 1.1 simonb a[1], (void *)addr));
342 1.1 simonb if (copyout(&a[1], (void *)addr, sizeof(int)))
343 1.1 simonb return (FAULT);
344 1.1 simonb return (0);
345 1.1 simonb }
346 1.1 simonb
347 1.1 simonb if ((instr.i_x.i_xo & OPC31_FPMASK) != OPC31_FPOP)
348 1.1 simonb /* Not an indexed FP load/store op */
349 1.1 simonb return (NOTFPU);
350 1.1 simonb
351 1.1 simonb store = (instr.i_x.i_xo & 0x80);
352 1.1 simonb if (instr.i_x.i_xo & 0x40)
353 1.1 simonb size = sizeof(double);
354 1.1 simonb else
355 1.1 simonb type = FTYPE_SNG;
356 1.1 simonb update = (instr.i_x.i_xo & 0x20);
357 1.1 simonb
358 1.1 simonb /* calculate EA of load/store */
359 1.1 simonb ra = instr.i_x.i_ra;
360 1.1 simonb rb = instr.i_x.i_rb;
361 1.7 thorpej DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n",
362 1.1 simonb ra, tf->fixreg[ra], rb, tf->fixreg[rb]));
363 1.1 simonb addr = tf->fixreg[rb];
364 1.1 simonb if (ra != 0)
365 1.1 simonb addr += tf->fixreg[ra];
366 1.1 simonb rt = instr.i_x.i_rt;
367 1.1 simonb } else {
368 1.1 simonb store = instr.i_d.i_opcd & 0x4;
369 1.1 simonb if (instr.i_d.i_opcd & 0x2)
370 1.1 simonb size = sizeof(double);
371 1.1 simonb else
372 1.1 simonb type = FTYPE_SNG;
373 1.1 simonb update = instr.i_d.i_opcd & 0x1;
374 1.1 simonb
375 1.1 simonb /* calculate EA of load/store */
376 1.1 simonb ra = instr.i_d.i_ra;
377 1.1 simonb addr = instr.i_d.i_d;
378 1.7 thorpej DPRINTF(FPE_INSN, ("reg %d has %lx displ %lx\n",
379 1.1 simonb ra, tf->fixreg[ra], addr));
380 1.1 simonb if (ra != 0)
381 1.1 simonb addr += tf->fixreg[ra];
382 1.1 simonb rt = instr.i_d.i_rt;
383 1.1 simonb }
384 1.1 simonb
385 1.1 simonb if (update && ra == 0)
386 1.1 simonb return (NOTFPU);
387 1.1 simonb
388 1.1 simonb if (store) {
389 1.1 simonb /* Store */
390 1.4 thorpej FPU_EMU_EVCNT_INCR(fpstore);
391 1.1 simonb if (type != FTYPE_DBL) {
392 1.1 simonb DPRINTF(FPE_INSN,
393 1.1 simonb ("fpu_execute: Store SNG at %p\n",
394 1.1 simonb (void *)addr));
395 1.1 simonb fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL, rt);
396 1.6 thorpej fpu_implode(fe, fp, type, (void *)&buf);
397 1.1 simonb if (copyout(&buf, (void *)addr, size))
398 1.1 simonb return (FAULT);
399 1.1 simonb } else {
400 1.1 simonb DPRINTF(FPE_INSN,
401 1.1 simonb ("fpu_execute: Store DBL at %p\n",
402 1.1 simonb (void *)addr));
403 1.1 simonb if (copyout(&fs->fpreg[rt], (void *)addr, size))
404 1.1 simonb return (FAULT);
405 1.1 simonb }
406 1.1 simonb } else {
407 1.1 simonb /* Load */
408 1.4 thorpej FPU_EMU_EVCNT_INCR(fpload);
409 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: Load from %p\n",
410 1.1 simonb (void *)addr));
411 1.1 simonb if (copyin((const void *)addr, &fs->fpreg[rt], size))
412 1.1 simonb return (FAULT);
413 1.1 simonb if (type != FTYPE_DBL) {
414 1.1 simonb fpu_explode(fe, fp = &fe->fe_f1, type, rt);
415 1.1 simonb fpu_implode(fe, fp, FTYPE_DBL,
416 1.1 simonb (u_int *)&fs->fpreg[rt]);
417 1.1 simonb }
418 1.1 simonb }
419 1.1 simonb if (update)
420 1.1 simonb tf->fixreg[ra] = addr;
421 1.1 simonb /* Complete. */
422 1.1 simonb return (0);
423 1.1 simonb #ifdef notyet
424 1.1 simonb } else if (instr.i_any.i_opcd == OPC_load_st_62) {
425 1.1 simonb /* These are 64-bit extenstions */
426 1.1 simonb return (NOTFPU);
427 1.1 simonb #endif
428 1.1 simonb } else if (instr.i_any.i_opcd == OPC_sp_fp_59 ||
429 1.1 simonb instr.i_any.i_opcd == OPC_dp_fp_63) {
430 1.1 simonb
431 1.1 simonb
432 1.1 simonb if (instr.i_any.i_opcd == OPC_dp_fp_63 &&
433 1.1 simonb !(instr.i_a.i_xo & OPC63M_MASK)) {
434 1.1 simonb /* Format X */
435 1.1 simonb rt = instr.i_x.i_rt;
436 1.1 simonb ra = instr.i_x.i_ra;
437 1.1 simonb rb = instr.i_x.i_rb;
438 1.1 simonb
439 1.1 simonb
440 1.1 simonb /* One of the special opcodes.... */
441 1.1 simonb switch (instr.i_x.i_xo) {
442 1.1 simonb case OPC63_FCMPU:
443 1.4 thorpej FPU_EMU_EVCNT_INCR(fcmpu);
444 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FCMPU\n"));
445 1.1 simonb rt >>= 2;
446 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
447 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
448 1.1 simonb fpu_compare(fe, 0);
449 1.1 simonb /* Make sure we do the condition regs. */
450 1.1 simonb cond = 0;
451 1.1 simonb /* N.B.: i_rs is already left shifted by two. */
452 1.1 simonb bf = instr.i_x.i_rs & 0xfc;
453 1.1 simonb setcr = 1;
454 1.1 simonb break;
455 1.1 simonb
456 1.1 simonb case OPC63_FRSP:
457 1.1 simonb /*
458 1.1 simonb * Convert to single:
459 1.1 simonb *
460 1.1 simonb * PowerPC uses this to round a double
461 1.1 simonb * precision value to single precision,
462 1.1 simonb * but values in registers are always
463 1.1 simonb * stored in double precision format.
464 1.1 simonb */
465 1.4 thorpej FPU_EMU_EVCNT_INCR(frsp);
466 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FRSP\n"));
467 1.1 simonb fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL, rb);
468 1.1 simonb fpu_implode(fe, fp, FTYPE_SNG,
469 1.1 simonb (u_int *)&fs->fpreg[rt]);
470 1.1 simonb fpu_explode(fe, fp = &fe->fe_f1, FTYPE_SNG, rt);
471 1.1 simonb type = FTYPE_DBL;
472 1.1 simonb break;
473 1.1 simonb case OPC63_FCTIW:
474 1.1 simonb case OPC63_FCTIWZ:
475 1.4 thorpej FPU_EMU_EVCNT_INCR(fctiw);
476 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FCTIW\n"));
477 1.1 simonb fpu_explode(fe, fp = &fe->fe_f1, type, rb);
478 1.1 simonb type = FTYPE_INT;
479 1.1 simonb break;
480 1.1 simonb case OPC63_FCMPO:
481 1.4 thorpej FPU_EMU_EVCNT_INCR(fcmpo);
482 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FCMPO\n"));
483 1.1 simonb rt >>= 2;
484 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
485 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
486 1.1 simonb fpu_compare(fe, 1);
487 1.1 simonb /* Make sure we do the condition regs. */
488 1.1 simonb cond = 0;
489 1.1 simonb /* N.B.: i_rs is already left shifted by two. */
490 1.1 simonb bf = instr.i_x.i_rs & 0xfc;
491 1.1 simonb setcr = 1;
492 1.1 simonb break;
493 1.1 simonb case OPC63_MTFSB1:
494 1.4 thorpej FPU_EMU_EVCNT_INCR(mtfsb1);
495 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: MTFSB1\n"));
496 1.1 simonb fe->fe_fpscr |=
497 1.1 simonb (~(FPSCR_VX|FPSR_EX) & (1<<(31-rt)));
498 1.1 simonb break;
499 1.1 simonb case OPC63_FNEG:
500 1.4 thorpej FPU_EMU_EVCNT_INCR(fnegabs);
501 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FNEGABS\n"));
502 1.3 wiz memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
503 1.1 simonb sizeof(double));
504 1.1 simonb a = (int *)&fs->fpreg[rt];
505 1.1 simonb *a ^= (1 << 31);
506 1.1 simonb break;
507 1.1 simonb case OPC63_MCRFS:
508 1.4 thorpej FPU_EMU_EVCNT_INCR(mcrfs);
509 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: MCRFS\n"));
510 1.1 simonb cond = 0;
511 1.1 simonb rt &= 0x1c;
512 1.1 simonb ra &= 0x1c;
513 1.1 simonb /* Extract the bits we want */
514 1.1 simonb mask = (fe->fe_fpscr >> (28 - ra)) & 0xf;
515 1.1 simonb /* Clear the bits we copied. */
516 1.1 simonb fe->fe_cx =
517 1.1 simonb (FPSR_EX_MSK | (0xf << (28 - ra)));
518 1.1 simonb fe->fe_fpscr &= fe->fe_cx;
519 1.1 simonb /* Now shove them in the right part of cr */
520 1.1 simonb tf->cr &= ~(0xf << (28 - rt));
521 1.1 simonb tf->cr |= (mask << (28 - rt));
522 1.1 simonb break;
523 1.1 simonb case OPC63_MTFSB0:
524 1.4 thorpej FPU_EMU_EVCNT_INCR(mtfsb0);
525 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: MTFSB0\n"));
526 1.1 simonb fe->fe_fpscr &=
527 1.1 simonb ((FPSCR_VX|FPSR_EX) & ~(1<<(31-rt)));
528 1.1 simonb break;
529 1.1 simonb case OPC63_FMR:
530 1.4 thorpej FPU_EMU_EVCNT_INCR(fmr);
531 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FMR\n"));
532 1.3 wiz memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
533 1.1 simonb sizeof(double));
534 1.1 simonb break;
535 1.1 simonb case OPC63_MTFSFI:
536 1.4 thorpej FPU_EMU_EVCNT_INCR(mtfsfi);
537 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: MTFSFI\n"));
538 1.1 simonb rb >>= 1;
539 1.1 simonb rt &= 0x1c; /* Already left-shifted 4 */
540 1.1 simonb fe->fe_cx = rb << (28 - rt);
541 1.1 simonb mask = 0xf<<(28 - rt);
542 1.1 simonb fe->fe_fpscr = (fe->fe_fpscr & ~mask) |
543 1.1 simonb fe->fe_cx;
544 1.1 simonb /* XXX weird stuff about OX, FX, FEX, and VX should be handled */
545 1.1 simonb break;
546 1.1 simonb case OPC63_FNABS:
547 1.4 thorpej FPU_EMU_EVCNT_INCR(fnabs);
548 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
549 1.3 wiz memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
550 1.1 simonb sizeof(double));
551 1.1 simonb a = (int *)&fs->fpreg[rt];
552 1.1 simonb *a |= (1 << 31);
553 1.1 simonb break;
554 1.1 simonb case OPC63_FABS:
555 1.4 thorpej FPU_EMU_EVCNT_INCR(fabs);
556 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
557 1.3 wiz memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
558 1.1 simonb sizeof(double));
559 1.1 simonb a = (int *)&fs->fpreg[rt];
560 1.1 simonb *a &= ~(1 << 31);
561 1.1 simonb break;
562 1.1 simonb case OPC63_MFFS:
563 1.4 thorpej FPU_EMU_EVCNT_INCR(mffs);
564 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: MFFS\n"));
565 1.3 wiz memcpy(&fs->fpreg[rt], &fs->fpscr,
566 1.1 simonb sizeof(fs->fpscr));
567 1.1 simonb break;
568 1.1 simonb case OPC63_MTFSF:
569 1.4 thorpej FPU_EMU_EVCNT_INCR(mtfsf);
570 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: MTFSF\n"));
571 1.1 simonb if ((rt = instr.i_xfl.i_flm) == -1)
572 1.1 simonb mask = -1;
573 1.1 simonb else {
574 1.1 simonb mask = 0;
575 1.1 simonb /* Convert 1 bit -> 4 bits */
576 1.1 simonb for (ra = 0; ra < 8; ra ++)
577 1.1 simonb if (rt & (1<<ra))
578 1.1 simonb mask |= (0xf<<(4*ra));
579 1.1 simonb }
580 1.1 simonb a = (int *)&fs->fpreg[rt];
581 1.1 simonb fe->fe_cx = mask & a[1];
582 1.1 simonb fe->fe_fpscr = (fe->fe_fpscr&~mask) |
583 1.1 simonb (fe->fe_cx);
584 1.1 simonb /* XXX weird stuff about OX, FX, FEX, and VX should be handled */
585 1.1 simonb break;
586 1.1 simonb case OPC63_FCTID:
587 1.1 simonb case OPC63_FCTIDZ:
588 1.4 thorpej FPU_EMU_EVCNT_INCR(fctid);
589 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FCTID\n"));
590 1.1 simonb fpu_explode(fe, fp = &fe->fe_f1, type, rb);
591 1.1 simonb type = FTYPE_LNG;
592 1.1 simonb break;
593 1.1 simonb case OPC63_FCFID:
594 1.4 thorpej FPU_EMU_EVCNT_INCR(fcfid);
595 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FCFID\n"));
596 1.1 simonb type = FTYPE_LNG;
597 1.1 simonb fpu_explode(fe, fp = &fe->fe_f1, type, rb);
598 1.1 simonb type = FTYPE_DBL;
599 1.1 simonb break;
600 1.1 simonb default:
601 1.1 simonb return (NOTFPU);
602 1.1 simonb break;
603 1.1 simonb }
604 1.1 simonb } else {
605 1.1 simonb /* Format A */
606 1.1 simonb rt = instr.i_a.i_frt;
607 1.1 simonb ra = instr.i_a.i_fra;
608 1.1 simonb rb = instr.i_a.i_frb;
609 1.1 simonb rc = instr.i_a.i_frc;
610 1.1 simonb
611 1.1 simonb type = FTYPE_SNG;
612 1.1 simonb if (instr.i_any.i_opcd & 0x4)
613 1.1 simonb type = FTYPE_DBL;
614 1.1 simonb switch ((unsigned int)instr.i_a.i_xo) {
615 1.1 simonb case OPC59_FDIVS:
616 1.4 thorpej FPU_EMU_EVCNT_INCR(fdiv);
617 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FDIV\n"));
618 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
619 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
620 1.1 simonb fp = fpu_div(fe);
621 1.1 simonb break;
622 1.1 simonb case OPC59_FSUBS:
623 1.4 thorpej FPU_EMU_EVCNT_INCR(fsub);
624 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FSUB\n"));
625 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
626 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
627 1.1 simonb fp = fpu_sub(fe);
628 1.1 simonb break;
629 1.1 simonb case OPC59_FADDS:
630 1.4 thorpej FPU_EMU_EVCNT_INCR(fadd);
631 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FADD\n"));
632 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
633 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
634 1.1 simonb fp = fpu_add(fe);
635 1.1 simonb break;
636 1.1 simonb case OPC59_FSQRTS:
637 1.4 thorpej FPU_EMU_EVCNT_INCR(fsqrt);
638 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FSQRT\n"));
639 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, rb);
640 1.1 simonb fp = fpu_sqrt(fe);
641 1.1 simonb break;
642 1.1 simonb case OPC63M_FSEL:
643 1.4 thorpej FPU_EMU_EVCNT_INCR(fsel);
644 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FSEL\n"));
645 1.1 simonb a = (int *)&fe->fe_fpstate->fpreg[ra];
646 1.1 simonb if ((*a & 0x80000000) && (*a & 0x7fffffff))
647 1.1 simonb /* fra < 0 */
648 1.1 simonb rc = rb;
649 1.1 simonb DPRINTF(FPE_INSN, ("f%d => f%d\n", rc, rt));
650 1.3 wiz memcpy(&fs->fpreg[rt], &fs->fpreg[rc],
651 1.1 simonb sizeof(double));
652 1.1 simonb break;
653 1.1 simonb case OPC59_FRES:
654 1.4 thorpej FPU_EMU_EVCNT_INCR(fpres);
655 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FPRES\n"));
656 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, rb);
657 1.1 simonb fp = fpu_sqrt(fe);
658 1.1 simonb /* now we've gotta overwrite the dest reg */
659 1.1 simonb *((int *)&fe->fe_fpstate->fpreg[rt]) = 1;
660 1.1 simonb fpu_explode(fe, &fe->fe_f1, FTYPE_INT, rt);
661 1.1 simonb fpu_div(fe);
662 1.1 simonb break;
663 1.1 simonb case OPC59_FMULS:
664 1.4 thorpej FPU_EMU_EVCNT_INCR(fmul);
665 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FMUL\n"));
666 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
667 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rc);
668 1.1 simonb fp = fpu_mul(fe);
669 1.1 simonb break;
670 1.1 simonb case OPC63M_FRSQRTE:
671 1.1 simonb /* Reciprocal sqrt() estimate */
672 1.4 thorpej FPU_EMU_EVCNT_INCR(frsqrte);
673 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FRSQRTE\n"));
674 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, rb);
675 1.1 simonb fe->fe_f2 = *fp;
676 1.1 simonb /* now we've gotta overwrite the dest reg */
677 1.1 simonb *((int *)&fe->fe_fpstate->fpreg[rt]) = 1;
678 1.1 simonb fpu_explode(fe, &fe->fe_f1, FTYPE_INT, rt);
679 1.1 simonb fpu_div(fe);
680 1.1 simonb break;
681 1.1 simonb case OPC59_FMSUBS:
682 1.4 thorpej FPU_EMU_EVCNT_INCR(fmulsub);
683 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FMULSUB\n"));
684 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
685 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rc);
686 1.1 simonb fp = fpu_mul(fe);
687 1.1 simonb fe->fe_f1 = *fp;
688 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
689 1.1 simonb fp = fpu_sub(fe);
690 1.1 simonb break;
691 1.1 simonb case OPC59_FMADDS:
692 1.4 thorpej FPU_EMU_EVCNT_INCR(fmuladd);
693 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FMULADD\n"));
694 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
695 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rc);
696 1.1 simonb fp = fpu_mul(fe);
697 1.1 simonb fe->fe_f1 = *fp;
698 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
699 1.1 simonb fp = fpu_add(fe);
700 1.1 simonb break;
701 1.1 simonb case OPC59_FNMSUBS:
702 1.4 thorpej FPU_EMU_EVCNT_INCR(fnmsub);
703 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FNMSUB\n"));
704 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
705 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rc);
706 1.1 simonb fp = fpu_mul(fe);
707 1.1 simonb fe->fe_f1 = *fp;
708 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
709 1.1 simonb fp = fpu_sub(fe);
710 1.1 simonb /* Negate */
711 1.1 simonb fp->fp_sign ^= 1;
712 1.1 simonb break;
713 1.1 simonb case OPC59_FNMADDS:
714 1.4 thorpej FPU_EMU_EVCNT_INCR(fnmadd);
715 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: FNMADD\n"));
716 1.1 simonb fpu_explode(fe, &fe->fe_f1, type, ra);
717 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rc);
718 1.1 simonb fp = fpu_mul(fe);
719 1.1 simonb fe->fe_f1 = *fp;
720 1.1 simonb fpu_explode(fe, &fe->fe_f2, type, rb);
721 1.1 simonb fp = fpu_add(fe);
722 1.1 simonb /* Negate */
723 1.1 simonb fp->fp_sign ^= 1;
724 1.1 simonb break;
725 1.1 simonb default:
726 1.1 simonb return (NOTFPU);
727 1.1 simonb break;
728 1.1 simonb }
729 1.1 simonb }
730 1.1 simonb } else {
731 1.1 simonb return (NOTFPU);
732 1.1 simonb }
733 1.1 simonb
734 1.1 simonb /*
735 1.1 simonb * ALU operation is complete. Collapse the result and then check
736 1.1 simonb * for exceptions. If we got any, and they are enabled, do not
737 1.1 simonb * alter the destination register, just stop with an exception.
738 1.1 simonb * Otherwise set new current exceptions and accrue.
739 1.1 simonb */
740 1.1 simonb if (fp)
741 1.1 simonb fpu_implode(fe, fp, type, (u_int *)&fs->fpreg[rt]);
742 1.1 simonb cx = fe->fe_cx;
743 1.1 simonb fsr = fe->fe_fpscr;
744 1.1 simonb if (cx != 0) {
745 1.1 simonb fsr &= ~FPSCR_FX;
746 1.1 simonb if ((cx^fsr)&FPSR_EX_MSK)
747 1.1 simonb fsr |= FPSCR_FX;
748 1.1 simonb mask = fsr & FPSR_EX;
749 1.1 simonb mask <<= (25-3);
750 1.1 simonb if (cx & mask)
751 1.1 simonb fsr |= FPSCR_FEX;
752 1.1 simonb if (cx & FPSCR_FPRF) {
753 1.1 simonb /* Need to replace CC */
754 1.1 simonb fsr &= ~FPSCR_FPRF;
755 1.1 simonb }
756 1.1 simonb if (cx & (FPSR_EXOP))
757 1.1 simonb fsr |= FPSCR_VX;
758 1.1 simonb fsr |= cx;
759 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: cx %x, fsr %x\n", cx, fsr));
760 1.1 simonb }
761 1.1 simonb
762 1.1 simonb if (cond) {
763 1.1 simonb cond = fsr & 0xf0000000;
764 1.1 simonb /* Isolate condition codes */
765 1.1 simonb cond >>= 28;
766 1.1 simonb /* Move fpu condition codes to cr[1] */
767 1.1 simonb tf->cr &= (0x0f000000);
768 1.1 simonb tf->cr |= (cond<<24);
769 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: cr[1] <= %x\n", cond));
770 1.1 simonb }
771 1.1 simonb
772 1.1 simonb if (setcr) {
773 1.1 simonb cond = fsr & FPSCR_FPCC;
774 1.1 simonb /* Isolate condition codes */
775 1.1 simonb cond <<= 16;
776 1.1 simonb /* Move fpu condition codes to cr[1] */
777 1.1 simonb tf->cr &= ~(0xf0000000>>bf);
778 1.1 simonb tf->cr |= (cond>>bf);
779 1.1 simonb DPRINTF(FPE_INSN, ("fpu_execute: cr[%d] (cr=%x) <= %x\n", bf/4, tf->cr, cond));
780 1.1 simonb }
781 1.1 simonb
782 1.1 simonb ((int *)&fs->fpscr)[1] = fsr;
783 1.1 simonb if (fsr & FPSCR_FEX)
784 1.1 simonb return(FPE);
785 1.1 simonb return (0); /* success */
786 1.1 simonb }
787