/*1* arch/sparc/math-emu/math.c2*3* Copyright (C) 1998 Peter Maydell ([email protected])4* Copyright (C) 1997, 1999 Jakub Jelinek ([email protected])5* Copyright (C) 1999 David S. Miller ([email protected])6*7* This is a good place to start if you're trying to understand the8* emulation code, because it's pretty simple. What we do is9* essentially analyse the instruction to work out what the operation10* is and which registers are involved. We then execute the appropriate11* FXXXX function. [The floating point queue introduces a minor wrinkle;12* see below...]13* The fxxxxx.c files each emulate a single insn. They look relatively14* simple because the complexity is hidden away in an unholy tangle15* of preprocessor macros.16*17* The first layer of macros is single.h, double.h, quad.h. Generally18* these files define macros for working with floating point numbers19* of the three IEEE formats. FP_ADD_D(R,A,B) is for adding doubles,20* for instance. These macros are usually defined as calls to more21* generic macros (in this case _FP_ADD(D,2,R,X,Y) where the number22* of machine words required to store the given IEEE format is passed23* as a parameter. [double.h and co check the number of bits in a word24* and define FP_ADD_D & co appropriately].25* The generic macros are defined in op-common.h. This is where all26* the grotty stuff like handling NaNs is coded. To handle the possible27* word sizes macros in op-common.h use macros like _FP_FRAC_SLL_##wc()28* where wc is the 'number of machine words' parameter (here 2).29* These are defined in the third layer of macros: op-1.h, op-2.h30* and op-4.h. These handle operations on floating point numbers composed31* of 1,2 and 4 machine words respectively. [For example, on sparc6432* doubles are one machine word so macros in double.h eventually use33* constructs in op-1.h, but on sparc32 they use op-2.h definitions.]34* soft-fp.h is on the same level as op-common.h, and defines some35* macros which are independent of both word size and FP format.36* Finally, sfp-machine.h is the machine dependent part of the37* code: it defines the word size and what type a word is. It also38* defines how _FP_MUL_MEAT_t() maps to _FP_MUL_MEAT_n_* : op-n.h39* provide several possible flavours of multiply algorithm, most40* of which require that you supply some form of asm or C primitive to41* do the actual multiply. (such asm primitives should be defined42* in sfp-machine.h too). udivmodti4.c is the same sort of thing.43*44* There may be some errors here because I'm working from a45* SPARC architecture manual V9, and what I really want is V8...46* Also, the insns which can generate exceptions seem to be a47* greater subset of the FPops than for V9 (for example, FCMPED48* has to be emulated on V8). So I think I'm going to have49* to emulate them all just to be on the safe side...50*51* Emulation routines originate from soft-fp package, which is52* part of glibc and has appropriate copyrights in it (allegedly).53*54* NB: on sparc int == long == 4 bytes, long long == 8 bytes.55* Most bits of the kernel seem to go for long rather than int,56* so we follow that practice...57*/5859/* TODO:60* fpsave() saves the FP queue but fpload() doesn't reload it.61* Therefore when we context switch or change FPU ownership62* we have to check to see if the queue had anything in it and63* emulate it if it did. This is going to be a pain.64*/6566#include <linux/types.h>67#include <linux/sched.h>68#include <linux/mm.h>69#include <linux/perf_event.h>70#include <asm/uaccess.h>7172#include "sfp-util_32.h"73#include <math-emu/soft-fp.h>74#include <math-emu/single.h>75#include <math-emu/double.h>76#include <math-emu/quad.h>7778#define FLOATFUNC(x) extern int x(void *,void *,void *)7980/* The Vn labels indicate what version of the SPARC architecture gas thinks81* each insn is. This is from the binutils source :->82*/83/* quadword instructions */84#define FSQRTQ 0x02b /* v8 */85#define FADDQ 0x043 /* v8 */86#define FSUBQ 0x047 /* v8 */87#define FMULQ 0x04b /* v8 */88#define FDIVQ 0x04f /* v8 */89#define FDMULQ 0x06e /* v8 */90#define FQTOS 0x0c7 /* v8 */91#define FQTOD 0x0cb /* v8 */92#define FITOQ 0x0cc /* v8 */93#define FSTOQ 0x0cd /* v8 */94#define FDTOQ 0x0ce /* v8 */95#define FQTOI 0x0d3 /* v8 */96#define FCMPQ 0x053 /* v8 */97#define FCMPEQ 0x057 /* v8 */98/* single/double instructions (subnormal): should all work */99#define FSQRTS 0x029 /* v7 */100#define FSQRTD 0x02a /* v7 */101#define FADDS 0x041 /* v6 */102#define FADDD 0x042 /* v6 */103#define FSUBS 0x045 /* v6 */104#define FSUBD 0x046 /* v6 */105#define FMULS 0x049 /* v6 */106#define FMULD 0x04a /* v6 */107#define FDIVS 0x04d /* v6 */108#define FDIVD 0x04e /* v6 */109#define FSMULD 0x069 /* v6 */110#define FDTOS 0x0c6 /* v6 */111#define FSTOD 0x0c9 /* v6 */112#define FSTOI 0x0d1 /* v6 */113#define FDTOI 0x0d2 /* v6 */114#define FABSS 0x009 /* v6 */115#define FCMPS 0x051 /* v6 */116#define FCMPES 0x055 /* v6 */117#define FCMPD 0x052 /* v6 */118#define FCMPED 0x056 /* v6 */119#define FMOVS 0x001 /* v6 */120#define FNEGS 0x005 /* v6 */121#define FITOS 0x0c4 /* v6 */122#define FITOD 0x0c8 /* v6 */123124#define FSR_TEM_SHIFT 23UL125#define FSR_TEM_MASK (0x1fUL << FSR_TEM_SHIFT)126#define FSR_AEXC_SHIFT 5UL127#define FSR_AEXC_MASK (0x1fUL << FSR_AEXC_SHIFT)128#define FSR_CEXC_SHIFT 0UL129#define FSR_CEXC_MASK (0x1fUL << FSR_CEXC_SHIFT)130131static int do_one_mathemu(u32 insn, unsigned long *fsr, unsigned long *fregs);132133/* Unlike the Sparc64 version (which has a struct fpustate), we134* pass the taskstruct corresponding to the task which currently owns the135* FPU. This is partly because we don't have the fpustate struct and136* partly because the task owning the FPU isn't always current (as is137* the case for the Sparc64 port). This is probably SMP-related...138* This function returns 1 if all queued insns were emulated successfully.139* The test for unimplemented FPop in kernel mode has been moved into140* kernel/traps.c for simplicity.141*/142int do_mathemu(struct pt_regs *regs, struct task_struct *fpt)143{144/* regs->pc isn't necessarily the PC at which the offending insn is sitting.145* The FPU maintains a queue of FPops which cause traps.146* When it hits an instruction that requires that the trapped op succeeded147* (usually because it reads a reg. that the trapped op wrote) then it148* causes this exception. We need to emulate all the insns on the queue149* and then allow the op to proceed.150* This code should also handle the case where the trap was precise,151* in which case the queue length is zero and regs->pc points at the152* single FPop to be emulated. (this case is untested, though :->)153* You'll need this case if you want to be able to emulate all FPops154* because the FPU either doesn't exist or has been software-disabled.155* [The UltraSPARC makes FP a precise trap; this isn't as stupid as it156* might sound because the Ultra does funky things with a superscalar157* architecture.]158*/159160/* You wouldn't believe how often I typed 'ftp' when I meant 'fpt' :-> */161162int i;163int retcode = 0; /* assume all succeed */164unsigned long insn;165166perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, 0, regs, 0);167168#ifdef DEBUG_MATHEMU169printk("In do_mathemu()... pc is %08lx\n", regs->pc);170printk("fpqdepth is %ld\n", fpt->thread.fpqdepth);171for (i = 0; i < fpt->thread.fpqdepth; i++)172printk("%d: %08lx at %08lx\n", i, fpt->thread.fpqueue[i].insn,173(unsigned long)fpt->thread.fpqueue[i].insn_addr);174#endif175176if (fpt->thread.fpqdepth == 0) { /* no queue, guilty insn is at regs->pc */177#ifdef DEBUG_MATHEMU178printk("precise trap at %08lx\n", regs->pc);179#endif180if (!get_user(insn, (u32 __user *) regs->pc)) {181retcode = do_one_mathemu(insn, &fpt->thread.fsr, fpt->thread.float_regs);182if (retcode) {183/* in this case we need to fix up PC & nPC */184regs->pc = regs->npc;185regs->npc += 4;186}187}188return retcode;189}190191/* Normal case: need to empty the queue... */192for (i = 0; i < fpt->thread.fpqdepth; i++) {193retcode = do_one_mathemu(fpt->thread.fpqueue[i].insn, &(fpt->thread.fsr), fpt->thread.float_regs);194if (!retcode) /* insn failed, no point doing any more */195break;196}197/* Now empty the queue and clear the queue_not_empty flag */198if (retcode)199fpt->thread.fsr &= ~(0x3000 | FSR_CEXC_MASK);200else201fpt->thread.fsr &= ~0x3000;202fpt->thread.fpqdepth = 0;203204return retcode;205}206207/* All routines returning an exception to raise should detect208* such exceptions _before_ rounding to be consistent with209* the behavior of the hardware in the implemented cases210* (and thus with the recommendations in the V9 architecture211* manual).212*213* We return 0 if a SIGFPE should be sent, 1 otherwise.214*/215static inline int record_exception(unsigned long *pfsr, int eflag)216{217unsigned long fsr = *pfsr;218int would_trap;219220/* Determine if this exception would have generated a trap. */221would_trap = (fsr & ((long)eflag << FSR_TEM_SHIFT)) != 0UL;222223/* If trapping, we only want to signal one bit. */224if (would_trap != 0) {225eflag &= ((fsr & FSR_TEM_MASK) >> FSR_TEM_SHIFT);226if ((eflag & (eflag - 1)) != 0) {227if (eflag & FP_EX_INVALID)228eflag = FP_EX_INVALID;229else if (eflag & FP_EX_OVERFLOW)230eflag = FP_EX_OVERFLOW;231else if (eflag & FP_EX_UNDERFLOW)232eflag = FP_EX_UNDERFLOW;233else if (eflag & FP_EX_DIVZERO)234eflag = FP_EX_DIVZERO;235else if (eflag & FP_EX_INEXACT)236eflag = FP_EX_INEXACT;237}238}239240/* Set CEXC, here is the rule:241*242* In general all FPU ops will set one and only one243* bit in the CEXC field, this is always the case244* when the IEEE exception trap is enabled in TEM.245*/246fsr &= ~(FSR_CEXC_MASK);247fsr |= ((long)eflag << FSR_CEXC_SHIFT);248249/* Set the AEXC field, rule is:250*251* If a trap would not be generated, the252* CEXC just generated is OR'd into the253* existing value of AEXC.254*/255if (would_trap == 0)256fsr |= ((long)eflag << FSR_AEXC_SHIFT);257258/* If trapping, indicate fault trap type IEEE. */259if (would_trap != 0)260fsr |= (1UL << 14);261262*pfsr = fsr;263264return (would_trap ? 0 : 1);265}266267typedef union {268u32 s;269u64 d;270u64 q[2];271} *argp;272273static int do_one_mathemu(u32 insn, unsigned long *pfsr, unsigned long *fregs)274{275/* Emulate the given insn, updating fsr and fregs appropriately. */276int type = 0;277/* r is rd, b is rs2 and a is rs1. The *u arg tells278whether the argument should be packed/unpacked (0 - do not unpack/pack, 1 - unpack/pack)279non-u args tells the size of the argument (0 - no argument, 1 - single, 2 - double, 3 - quad */280#define TYPE(dummy, r, ru, b, bu, a, au) type = (au << 2) | (a << 0) | (bu << 5) | (b << 3) | (ru << 8) | (r << 6)281int freg;282argp rs1 = NULL, rs2 = NULL, rd = NULL;283FP_DECL_EX;284FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);285FP_DECL_D(DA); FP_DECL_D(DB); FP_DECL_D(DR);286FP_DECL_Q(QA); FP_DECL_Q(QB); FP_DECL_Q(QR);287int IR;288long fsr;289290#ifdef DEBUG_MATHEMU291printk("In do_mathemu(), emulating %08lx\n", insn);292#endif293294if ((insn & 0xc1f80000) == 0x81a00000) /* FPOP1 */ {295switch ((insn >> 5) & 0x1ff) {296case FSQRTQ: TYPE(3,3,1,3,1,0,0); break;297case FADDQ:298case FSUBQ:299case FMULQ:300case FDIVQ: TYPE(3,3,1,3,1,3,1); break;301case FDMULQ: TYPE(3,3,1,2,1,2,1); break;302case FQTOS: TYPE(3,1,1,3,1,0,0); break;303case FQTOD: TYPE(3,2,1,3,1,0,0); break;304case FITOQ: TYPE(3,3,1,1,0,0,0); break;305case FSTOQ: TYPE(3,3,1,1,1,0,0); break;306case FDTOQ: TYPE(3,3,1,2,1,0,0); break;307case FQTOI: TYPE(3,1,0,3,1,0,0); break;308case FSQRTS: TYPE(2,1,1,1,1,0,0); break;309case FSQRTD: TYPE(2,2,1,2,1,0,0); break;310case FADDD:311case FSUBD:312case FMULD:313case FDIVD: TYPE(2,2,1,2,1,2,1); break;314case FADDS:315case FSUBS:316case FMULS:317case FDIVS: TYPE(2,1,1,1,1,1,1); break;318case FSMULD: TYPE(2,2,1,1,1,1,1); break;319case FDTOS: TYPE(2,1,1,2,1,0,0); break;320case FSTOD: TYPE(2,2,1,1,1,0,0); break;321case FSTOI: TYPE(2,1,0,1,1,0,0); break;322case FDTOI: TYPE(2,1,0,2,1,0,0); break;323case FITOS: TYPE(2,1,1,1,0,0,0); break;324case FITOD: TYPE(2,2,1,1,0,0,0); break;325case FMOVS:326case FABSS:327case FNEGS: TYPE(2,1,0,1,0,0,0); break;328}329} else if ((insn & 0xc1f80000) == 0x81a80000) /* FPOP2 */ {330switch ((insn >> 5) & 0x1ff) {331case FCMPS: TYPE(3,0,0,1,1,1,1); break;332case FCMPES: TYPE(3,0,0,1,1,1,1); break;333case FCMPD: TYPE(3,0,0,2,1,2,1); break;334case FCMPED: TYPE(3,0,0,2,1,2,1); break;335case FCMPQ: TYPE(3,0,0,3,1,3,1); break;336case FCMPEQ: TYPE(3,0,0,3,1,3,1); break;337}338}339340if (!type) { /* oops, didn't recognise that FPop */341#ifdef DEBUG_MATHEMU342printk("attempt to emulate unrecognised FPop!\n");343#endif344return 0;345}346347/* Decode the registers to be used */348freg = (*pfsr >> 14) & 0xf;349350*pfsr &= ~0x1c000; /* clear the traptype bits */351352freg = ((insn >> 14) & 0x1f);353switch (type & 0x3) { /* is rs1 single, double or quad? */354case 3:355if (freg & 3) { /* quadwords must have bits 4&5 of the */356/* encoded reg. number set to zero. */357*pfsr |= (6 << 14);358return 0; /* simulate invalid_fp_register exception */359}360/* fall through */361case 2:362if (freg & 1) { /* doublewords must have bit 5 zeroed */363*pfsr |= (6 << 14);364return 0;365}366}367rs1 = (argp)&fregs[freg];368switch (type & 0x7) {369case 7: FP_UNPACK_QP (QA, rs1); break;370case 6: FP_UNPACK_DP (DA, rs1); break;371case 5: FP_UNPACK_SP (SA, rs1); break;372}373freg = (insn & 0x1f);374switch ((type >> 3) & 0x3) { /* same again for rs2 */375case 3:376if (freg & 3) { /* quadwords must have bits 4&5 of the */377/* encoded reg. number set to zero. */378*pfsr |= (6 << 14);379return 0; /* simulate invalid_fp_register exception */380}381/* fall through */382case 2:383if (freg & 1) { /* doublewords must have bit 5 zeroed */384*pfsr |= (6 << 14);385return 0;386}387}388rs2 = (argp)&fregs[freg];389switch ((type >> 3) & 0x7) {390case 7: FP_UNPACK_QP (QB, rs2); break;391case 6: FP_UNPACK_DP (DB, rs2); break;392case 5: FP_UNPACK_SP (SB, rs2); break;393}394freg = ((insn >> 25) & 0x1f);395switch ((type >> 6) & 0x3) { /* and finally rd. This one's a bit different */396case 0: /* dest is fcc. (this must be FCMPQ or FCMPEQ) */397if (freg) { /* V8 has only one set of condition codes, so */398/* anything but 0 in the rd field is an error */399*pfsr |= (6 << 14); /* (should probably flag as invalid opcode */400return 0; /* but SIGFPE will do :-> ) */401}402break;403case 3:404if (freg & 3) { /* quadwords must have bits 4&5 of the */405/* encoded reg. number set to zero. */406*pfsr |= (6 << 14);407return 0; /* simulate invalid_fp_register exception */408}409/* fall through */410case 2:411if (freg & 1) { /* doublewords must have bit 5 zeroed */412*pfsr |= (6 << 14);413return 0;414}415/* fall through */416case 1:417rd = (void *)&fregs[freg];418break;419}420#ifdef DEBUG_MATHEMU421printk("executing insn...\n");422#endif423/* do the Right Thing */424switch ((insn >> 5) & 0x1ff) {425/* + */426case FADDS: FP_ADD_S (SR, SA, SB); break;427case FADDD: FP_ADD_D (DR, DA, DB); break;428case FADDQ: FP_ADD_Q (QR, QA, QB); break;429/* - */430case FSUBS: FP_SUB_S (SR, SA, SB); break;431case FSUBD: FP_SUB_D (DR, DA, DB); break;432case FSUBQ: FP_SUB_Q (QR, QA, QB); break;433/* * */434case FMULS: FP_MUL_S (SR, SA, SB); break;435case FSMULD: FP_CONV (D, S, 2, 1, DA, SA);436FP_CONV (D, S, 2, 1, DB, SB);437case FMULD: FP_MUL_D (DR, DA, DB); break;438case FDMULQ: FP_CONV (Q, D, 4, 2, QA, DA);439FP_CONV (Q, D, 4, 2, QB, DB);440case FMULQ: FP_MUL_Q (QR, QA, QB); break;441/* / */442case FDIVS: FP_DIV_S (SR, SA, SB); break;443case FDIVD: FP_DIV_D (DR, DA, DB); break;444case FDIVQ: FP_DIV_Q (QR, QA, QB); break;445/* sqrt */446case FSQRTS: FP_SQRT_S (SR, SB); break;447case FSQRTD: FP_SQRT_D (DR, DB); break;448case FSQRTQ: FP_SQRT_Q (QR, QB); break;449/* mov */450case FMOVS: rd->s = rs2->s; break;451case FABSS: rd->s = rs2->s & 0x7fffffff; break;452case FNEGS: rd->s = rs2->s ^ 0x80000000; break;453/* float to int */454case FSTOI: FP_TO_INT_S (IR, SB, 32, 1); break;455case FDTOI: FP_TO_INT_D (IR, DB, 32, 1); break;456case FQTOI: FP_TO_INT_Q (IR, QB, 32, 1); break;457/* int to float */458case FITOS: IR = rs2->s; FP_FROM_INT_S (SR, IR, 32, int); break;459case FITOD: IR = rs2->s; FP_FROM_INT_D (DR, IR, 32, int); break;460case FITOQ: IR = rs2->s; FP_FROM_INT_Q (QR, IR, 32, int); break;461/* float to float */462case FSTOD: FP_CONV (D, S, 2, 1, DR, SB); break;463case FSTOQ: FP_CONV (Q, S, 4, 1, QR, SB); break;464case FDTOQ: FP_CONV (Q, D, 4, 2, QR, DB); break;465case FDTOS: FP_CONV (S, D, 1, 2, SR, DB); break;466case FQTOS: FP_CONV (S, Q, 1, 4, SR, QB); break;467case FQTOD: FP_CONV (D, Q, 2, 4, DR, QB); break;468/* comparison */469case FCMPS:470case FCMPES:471FP_CMP_S(IR, SB, SA, 3);472if (IR == 3 &&473(((insn >> 5) & 0x1ff) == FCMPES ||474FP_ISSIGNAN_S(SA) ||475FP_ISSIGNAN_S(SB)))476FP_SET_EXCEPTION (FP_EX_INVALID);477break;478case FCMPD:479case FCMPED:480FP_CMP_D(IR, DB, DA, 3);481if (IR == 3 &&482(((insn >> 5) & 0x1ff) == FCMPED ||483FP_ISSIGNAN_D(DA) ||484FP_ISSIGNAN_D(DB)))485FP_SET_EXCEPTION (FP_EX_INVALID);486break;487case FCMPQ:488case FCMPEQ:489FP_CMP_Q(IR, QB, QA, 3);490if (IR == 3 &&491(((insn >> 5) & 0x1ff) == FCMPEQ ||492FP_ISSIGNAN_Q(QA) ||493FP_ISSIGNAN_Q(QB)))494FP_SET_EXCEPTION (FP_EX_INVALID);495}496if (!FP_INHIBIT_RESULTS) {497switch ((type >> 6) & 0x7) {498case 0: fsr = *pfsr;499if (IR == -1) IR = 2;500/* fcc is always fcc0 */501fsr &= ~0xc00; fsr |= (IR << 10); break;502*pfsr = fsr;503break;504case 1: rd->s = IR; break;505case 5: FP_PACK_SP (rd, SR); break;506case 6: FP_PACK_DP (rd, DR); break;507case 7: FP_PACK_QP (rd, QR); break;508}509}510if (_fex == 0)511return 1; /* success! */512return record_exception(pfsr, _fex);513}514515516