fpu.c revision 1.25 1 /* $NetBSD: fpu.c,v 1.25 2018/11/14 10:58:04 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * FPU handling for NetBSD/hppa.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.25 2018/11/14 10:58:04 skrll Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/signalvar.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <machine/cpufunc.h>
47 #include <machine/frame.h>
48 #include <machine/reg.h>
49 #include <machine/pcb.h>
50 #include <machine/pmap.h>
51
52 #include <hppa/hppa/machdep.h>
53
54 #include "../spmath/float.h"
55 #include "../spmath/fpudispatch.h"
56
57 /* Some macros representing opcodes. */
58 #define OPCODE_NOP 0x08000240
59 #define OPCODE_COPR_0_0 0x30000000
60
61 /* Some macros representing fields in load/store opcodes. */
62 #define OPCODE_CMPLT_S 0x00002000
63 #define OPCODE_CMPLT_M 0x00000020
64 #define OPCODE_CMPLT_SM (OPCODE_CMPLT_S | OPCODE_CMPLT_M)
65 #define OPCODE_CMPLT_MB OPCODE_CMPLT_M
66 #define OPCODE_CMPLT_MA (OPCODE_CMPLT_S | OPCODE_CMPLT_M)
67 #define OPCODE_CMPLT (OPCODE_CMPLT_S | OPCODE_CMPLT_M)
68 #define OPCODE_DOUBLE 0x08000000
69 #define OPCODE_STORE 0x00000200
70 #define OPCODE_INDEXED 0x00001000
71
72 /* This is nonzero iff we're using a hardware FPU. */
73 int fpu_present;
74
75 /* If we have any FPU, this is its version. */
76 u_int fpu_version;
77
78 /* The number of times we have had to switch the FPU context. */
79 u_int fpu_csw;
80
81 /* In locore.S, this swaps states in and out of the FPU. */
82 void hppa_fpu_swapout(struct pcb *);
83 void hppa_fpu_swap(struct fpreg *, struct fpreg *);
84
85 static int hppa_fpu_ls(struct trapframe *, struct lwp *);
86
87 /*
88 * Given a trapframe and a general register number, the
89 * FRAME_REG macro returns a pointer to that general
90 * register. The _frame_reg_positions array is a lookup
91 * table, since the general registers aren't in order
92 * in a trapframe.
93 *
94 * NB: this more or less assumes that all members of
95 * struct trapframe are u_ints.
96 */
97 #define FRAME_REG(f, reg, r0) \
98 ((reg) == 0 ? (&r0) : ((&(f)->tf_t1) + _frame_reg_positions[reg]))
99 #define _FRAME_POSITION(f) \
100 ((&((struct trapframe *) 0)->f) - (&((struct trapframe *) 0)->tf_t1))
101 const int _frame_reg_positions[32] = {
102 -1, /* r0 */
103 _FRAME_POSITION(tf_r1),
104 _FRAME_POSITION(tf_rp), /* r2 */
105 _FRAME_POSITION(tf_r3),
106 _FRAME_POSITION(tf_r4),
107 _FRAME_POSITION(tf_r5),
108 _FRAME_POSITION(tf_r6),
109 _FRAME_POSITION(tf_r7),
110 _FRAME_POSITION(tf_r8),
111 _FRAME_POSITION(tf_r9),
112 _FRAME_POSITION(tf_r10),
113 _FRAME_POSITION(tf_r11),
114 _FRAME_POSITION(tf_r12),
115 _FRAME_POSITION(tf_r13),
116 _FRAME_POSITION(tf_r14),
117 _FRAME_POSITION(tf_r15),
118 _FRAME_POSITION(tf_r16),
119 _FRAME_POSITION(tf_r17),
120 _FRAME_POSITION(tf_r18),
121 _FRAME_POSITION(tf_t4), /* r19 */
122 _FRAME_POSITION(tf_t3), /* r20 */
123 _FRAME_POSITION(tf_t2), /* r21 */
124 _FRAME_POSITION(tf_t1), /* r22 */
125 _FRAME_POSITION(tf_arg3), /* r23 */
126 _FRAME_POSITION(tf_arg2), /* r24 */
127 _FRAME_POSITION(tf_arg1), /* r25 */
128 _FRAME_POSITION(tf_arg0), /* r26 */
129 _FRAME_POSITION(tf_dp), /* r27 */
130 _FRAME_POSITION(tf_ret0), /* r28 */
131 _FRAME_POSITION(tf_ret1), /* r29 */
132 _FRAME_POSITION(tf_sp), /* r30 */
133 _FRAME_POSITION(tf_r31),
134 };
135
136 /*
137 * Bootstraps the FPU.
138 */
139 void
140 hppa_fpu_bootstrap(u_int ccr_enable)
141 {
142 uint32_t junk[2];
143 uint32_t vers[2];
144
145 /* See if we have a present and functioning hardware FPU. */
146 fpu_present = (ccr_enable & HPPA_FPUS) == HPPA_FPUS;
147 if (!fpu_present) {
148 fpu_csw = 0;
149 curcpu()->ci_fpu_state = 0;
150
151 return;
152 }
153
154 KASSERT(fpu_present);
155 /* Initialize the FPU and get its version. */
156
157 /*
158 * We track what process has the FPU,
159 * and how many times we have to swap
160 * in and out.
161 */
162
163 /*
164 * The PA-RISC 1.1 Architecture manual is
165 * pretty clear that the copr,0,0 must be
166 * wrapped in double word stores of fr0,
167 * otherwise its operation is undefined.
168 */
169 __asm volatile(
170 " ldo %0, %%r22 \n"
171 " fstds %%fr0, 0(%%r22) \n"
172 " ldo %1, %%r22 \n"
173 " copr,0,0 \n"
174 " fstds %%fr0, 0(%%r22) \n"
175 : "=m" (junk), "=m" (vers) : : "r22");
176
177 /*
178 * Now mark that no process has the FPU,
179 * and disable it, so the first time it
180 * gets used the process' state gets
181 * swapped in.
182 */
183 fpu_csw = 0;
184 curcpu()->ci_fpu_state = 0;
185 mtctl(ccr_enable & (CCR_MASK ^ HPPA_FPUS), CR_CCR);
186
187 fpu_version = vers[0];
188 }
189
190 /*
191 * If the given LWP has its state in the FPU,
192 * flush that state out into the LWP's PCB.
193 */
194 void
195 hppa_fpu_flush(struct lwp *l)
196 {
197 struct trapframe *tf = l->l_md.md_regs;
198 struct pcb *pcb = lwp_getpcb(l);
199 struct cpu_info *ci = curcpu();
200
201 KASSERT(fpu_present);
202
203 /*
204 * If this process' state is currently in hardware, swap it out.
205 */
206
207 if (ci->ci_fpu_state == 0 ||
208 ci->ci_fpu_state != tf->tf_cr30) {
209 return;
210 }
211
212 hppa_fpu_swapout(pcb);
213 ci->ci_fpu_state = 0;
214 }
215
216 /*
217 * This emulates a coprocessor load/store instruction.
218 */
219 static int
220 hppa_fpu_ls(struct trapframe *frame, struct lwp *l)
221 {
222 struct pcb *pcb = lwp_getpcb(l);
223 u_int inst, inst_b, inst_x, inst_s, inst_t;
224 int log2size;
225 u_int *base;
226 u_int offset, index, im5;
227 void *fpreg;
228 u_int r0 = 0;
229 int error;
230
231 /*
232 * Get the instruction that we're emulating,
233 * and break it down. Using HP bit notation,
234 * b is a five-bit field starting at bit 10,
235 * x is a five-bit field starting at bit 15,
236 * s is a two-bit field starting at bit 17,
237 * and t is a five-bit field starting at bit 31.
238 */
239 inst = frame->tf_iir;
240 __asm volatile(
241 " extru %4, 10, 5, %1 \n"
242 " extru %4, 15, 5, %2 \n"
243 " extru %4, 17, 2, %3 \n"
244 " extru %4, 31, 5, %4 \n"
245 : "=r" (inst_b), "=r" (inst_x), "=r" (inst_s), "=r" (inst_t)
246 : "r" (inst));
247
248 /*
249 * The space must be the user's space, else we
250 * segfault.
251 */
252 if (inst_s != pcb->pcb_space)
253 return EFAULT;
254
255 /* See whether or not this is a doubleword load/store. */
256 log2size = (inst & OPCODE_DOUBLE) ? 3 : 2;
257
258 /* Get the floating point register. */
259 fpreg = ((char *)pcb->pcb_fpregs) + (inst_t << log2size);
260
261 /* Get the base register. */
262 base = FRAME_REG(frame, inst_b, r0);
263
264 /* Dispatch on whether or not this is an indexed load/store. */
265 if (inst & OPCODE_INDEXED) {
266
267 /* Get the index register value. */
268 index = *FRAME_REG(frame, inst_x, r0);
269
270 /* Dispatch on the completer. */
271 switch (inst & OPCODE_CMPLT) {
272 case OPCODE_CMPLT_S:
273 offset = *base + (index << log2size);
274 break;
275 case OPCODE_CMPLT_M:
276 offset = *base;
277 *base = *base + index;
278 break;
279 case OPCODE_CMPLT_SM:
280 offset = *base;
281 *base = *base + (index << log2size);
282 break;
283 default:
284 offset = *base + index;
285 break;
286 }
287 } else {
288
289 /* Do a low_sign_ext(x, 5). */
290 im5 = inst_x >> 1;
291 if (inst_x & 1)
292 im5 |= 0xfffffff0;
293
294 /* Dispatch on the completer. */
295 switch (inst & OPCODE_CMPLT) {
296 case OPCODE_CMPLT_MB:
297 offset = *base + im5;
298 *base = *base + im5;
299 break;
300 case OPCODE_CMPLT_MA:
301 offset = *base;
302 *base = *base + im5;
303 break;
304 default:
305 offset = *base + im5;
306 break;
307 }
308 }
309
310 /*
311 * The offset we calculated must be the same as the
312 * offset in the IOR.
313 */
314 KASSERT(offset == frame->tf_ior);
315
316 /* Perform the load or store. */
317 error = (inst & OPCODE_STORE) ?
318 copyout(fpreg, (void *) offset, 1 << log2size) :
319 copyin((const void *) offset, fpreg, 1 << log2size);
320 return error;
321 }
322
323 /*
324 * This is called to emulate an instruction.
325 */
326 void
327 hppa_fpu_emulate(struct trapframe *frame, struct lwp *l, u_int inst)
328 {
329 struct pcb *pcb = lwp_getpcb(l);
330 u_int opcode, class, sub;
331 u_int *fpregs;
332 int exception;
333 ksiginfo_t ksi;
334
335 /*
336 * If the process' state is in any hardware FPU,
337 * flush it out - we need to operate on it.
338 */
339 hppa_fpu_flush(l);
340
341 /*
342 * Get the instruction that we're emulating,
343 * and break it down. Using HP bit notation,
344 * the class is a two-bit field starting at
345 * bit 22, the opcode is a 6-bit field starting
346 * at bit 5, and sub for a class 1 instruction
347 * is a two bit field starting at bit 16, else
348 * it is a three bit field starting at bit 18.
349 */
350 #if 0
351 __asm volatile(
352 " extru %3, 22, 2, %1 \n"
353 " extru %3, 5, 6, %0 \n"
354 " extru %3, 18, 3, %2 \n"
355 " comib,<> 1, %1, 0 \n"
356 " extru %3, 16, 2, %2 \n"
357 : "=r" (opcode), "=r" (class), "=r" (sub)
358 : "r" (inst));
359 #else
360 opcode = (inst >> (31 - 5)) & 0x3f;
361 class = (inst >> (31 - 22)) & 0x3;
362 if (class == 1) {
363 sub = (inst >> (31 - 16)) & 3;
364 } else {
365 sub = (inst >> (31 - 18)) & 7;
366 }
367 #endif
368
369 /* Get this LWP's FPU registers. */
370 fpregs = (u_int *)pcb->pcb_fpregs;
371
372 /* Dispatch on the opcode. */
373 switch (opcode) {
374 case 0x09:
375 case 0x0b:
376 if (hppa_fpu_ls(frame, l) != 0) {
377 KSI_INIT_TRAP(&ksi);
378 ksi.ksi_signo = SIGSEGV;
379 ksi.ksi_code = SEGV_MAPERR;
380 ksi.ksi_trap = T_DTLBMISS;
381 ksi.ksi_addr = (void *)frame->tf_iioq_head;
382 trapsignal(l, &ksi);
383 }
384 return;
385 case 0x0c:
386 exception = decode_0c(inst, class, sub, fpregs);
387 break;
388 case 0x0e:
389 exception = decode_0e(inst, class, sub, fpregs);
390 break;
391 case 0x06:
392 exception = decode_06(inst, fpregs);
393 break;
394 case 0x26:
395 exception = decode_26(inst, fpregs);
396 break;
397 default:
398 exception = UNIMPLEMENTEDEXCEPTION;
399 break;
400 }
401
402 if (exception) {
403 KSI_INIT_TRAP(&ksi);
404 if (exception & UNIMPLEMENTEDEXCEPTION) {
405 ksi.ksi_signo = SIGILL;
406 ksi.ksi_code = ILL_COPROC;
407 } else {
408 ksi.ksi_signo = SIGFPE;
409 if (exception & INVALIDEXCEPTION) {
410 ksi.ksi_code = FPE_FLTINV;
411 } else if (exception & DIVISIONBYZEROEXCEPTION) {
412 ksi.ksi_code = FPE_FLTDIV;
413 } else if (exception & OVERFLOWEXCEPTION) {
414 ksi.ksi_code = FPE_FLTOVF;
415 } else if (exception & UNDERFLOWEXCEPTION) {
416 ksi.ksi_code = FPE_FLTUND;
417 } else if (exception & INEXACTEXCEPTION) {
418 ksi.ksi_code = FPE_FLTRES;
419 }
420 }
421 ksi.ksi_trap = T_EMULATION;
422 ksi.ksi_addr = (void *)frame->tf_iioq_head;
423 trapsignal(l, &ksi);
424 }
425 }
426