Home | History | Annotate | Line # | Download | only in fpe
fpu_fstore.c revision 1.16
      1 /*	$NetBSD: fpu_fstore.c,v 1.16 2025/01/03 05:54:07 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Ken Nakata
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: fpu_fstore.c,v 1.16 2025/01/03 05:54:07 isaki Exp $");
     30 
     31 #include <sys/types.h>
     32 #include <sys/signal.h>
     33 #include <sys/systm.h>
     34 #include <machine/frame.h>
     35 
     36 #include "fpu_emulate.h"
     37 
     38 /*
     39  * type 0: fmove mem/fpr->fpr
     40  * In this function, we know
     41  *	(opcode & 0x01c0) == 0
     42  *	(word1 & 0xe000) == 0x6000
     43  */
     44 int
     45 fpu_emul_fstore(struct fpemu *fe, struct instruction *insn)
     46 {
     47 	struct frame *frame = fe->fe_frame;
     48 	uint32_t *fpregs = fe->fe_fpframe->fpf_regs;
     49 	int word1, sig;
     50 	int regnum;
     51 	int format;
     52 	int modreg;
     53 	uint32_t buf[3];
     54 
     55 #if DEBUG_FPE
     56 	printf("  fpu_emul_fstore: frame at %p fpframe at %p\n",
     57 	    frame, fe->fe_fpframe);
     58 #endif
     59 
     60 	word1 = insn->is_word1;
     61 	format = (word1 >> 10) & 7;
     62 	regnum = (word1 >> 7) & 7;
     63 
     64 	insn->is_advance = 4;
     65 
     66 	if (format == FTYPE_DBL) {
     67 		insn->is_datasize = 8;
     68 	} else if (format == FTYPE_SNG || format == FTYPE_LNG) {
     69 		insn->is_datasize = 4;
     70 	} else if (format == FTYPE_WRD) {
     71 		insn->is_datasize = 2;
     72 		format = FTYPE_LNG;
     73 	} else if (format == FTYPE_BYT) {
     74 		insn->is_datasize = 1;
     75 		format = FTYPE_LNG;
     76 	} else if (format == FTYPE_EXT) {
     77 		insn->is_datasize = 12;
     78 	} else {
     79 		/* invalid or unsupported operand format */
     80 #if DEBUG_FPE
     81 		printf("  fpu_emul_fstore: invalid format %d\n", format);
     82 #endif
     83 		return SIGFPE;
     84 	}
     85 #if DEBUG_FPE
     86 	printf("  fpu_emul_fstore: format %d, size %d\n",
     87 	    format, insn->is_datasize);
     88 #endif
     89 
     90 	fe->fe_fpsr &= ~FPSR_EXCP;
     91 
     92 	/* Check an illegal mod/reg */
     93 	modreg = insn->is_opcode & 077;
     94 	if ((modreg >> 3) == 1/*An*/ || modreg >= 072/*PCrel and #imm*/) {
     95 #if DEBUG_FPE
     96 		printf("  fpu_emul_fstore: illegal modreg=0%o\n", modreg);
     97 #endif
     98 		return SIGILL;
     99 	}
    100 
    101 	/* Get effective address. */
    102 	sig = fpu_decode_ea(frame, insn, &insn->is_ea, modreg);
    103 	if (sig) {
    104 #if DEBUG_FPE
    105 		printf("  fpu_emul_fstore: failed in decode_ea sig=%d\n", sig);
    106 #endif
    107 		return sig;
    108 	}
    109 
    110 	if (insn->is_datasize > 4 && insn->is_ea.ea_flags == EA_DIRECT) {
    111 		/* trying to store dbl or ext into a data register */
    112 #ifdef DEBUG
    113 		printf("  fpu_fstore: attempted to store dbl/ext to reg\n");
    114 #endif
    115 		return SIGILL;
    116 	}
    117 
    118 #if DEBUG_FPE
    119 	printf("  fpu_emul_fstore: saving FP%d (%08x,%08x,%08x)\n",
    120 	    regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
    121 	    fpregs[regnum * 3 + 2]);
    122 #endif
    123 	fpu_explode(fe, &fe->fe_f3, FTYPE_EXT, &fpregs[regnum * 3]);
    124 #if DEBUG_FPE
    125 	{
    126 	static const char *class_name[] =
    127 	    { "SNAN", "QNAN", "ZERO", "NUM", "INF" };
    128 	printf("  fpu_emul_fstore: fpn (%s,%c,%d,%08x,%08x,%08x)\n",
    129 	    class_name[fe->fe_f3.fp_class + 2],
    130 	    fe->fe_f3.fp_sign ? '-' : '+', fe->fe_f3.fp_exp,
    131 	    fe->fe_f3.fp_mant[0], fe->fe_f3.fp_mant[1],
    132 	    fe->fe_f3.fp_mant[2]);
    133 	}
    134 #endif
    135 	fpu_implode(fe, &fe->fe_f3, format, buf);
    136 
    137 	fpu_store_ea(frame, insn, &insn->is_ea, (char *)buf);
    138 #if DEBUG_FPE
    139 	printf("  fpu_emul_fstore: %08x,%08x,%08x size %d\n",
    140 	    buf[0], buf[1], buf[2], insn->is_datasize);
    141 #endif
    142 
    143 	return 0;
    144 }
    145