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