utility.cc revision 7707
12686Sksewell@umich.edu/*
25254Sksewell@umich.edu * Copyright (c) 2007 MIPS Technologies, Inc.
35254Sksewell@umich.edu * All rights reserved.
42686Sksewell@umich.edu *
55254Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are
75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
145254Sksewell@umich.edu * this software without specific prior written permission.
152686Sksewell@umich.edu *
165254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272706Sksewell@umich.edu *
285254Sksewell@umich.edu * Authors: Korey Sewell
292686Sksewell@umich.edu */
302686Sksewell@umich.edu
317678Sgblack@eecs.umich.edu#include <cmath>
327678Sgblack@eecs.umich.edu
334661Sksewell@umich.edu#include "arch/mips/isa_traits.hh"
342686Sksewell@umich.edu#include "arch/mips/utility.hh"
354661Sksewell@umich.edu#include "config/full_system.hh"
364661Sksewell@umich.edu#include "cpu/thread_context.hh"
374661Sksewell@umich.edu#include "cpu/static_inst.hh"
384661Sksewell@umich.edu#include "sim/serialize.hh"
394661Sksewell@umich.edu#include "base/bitfield.hh"
402980Sgblack@eecs.umich.edu#include "base/misc.hh"
412686Sksewell@umich.edu
425222Sksewell@umich.edu#if FULL_SYSTEM
436379Sgblack@eecs.umich.edu#include "arch/mips/registers.hh"
445222Sksewell@umich.edu#include "arch/mips/vtophys.hh"
455222Sksewell@umich.edu#include "mem/vport.hh"
465222Sksewell@umich.edu#endif
475222Sksewell@umich.edu
485222Sksewell@umich.edu
492686Sksewell@umich.eduusing namespace MipsISA;
504661Sksewell@umich.eduusing namespace std;
512686Sksewell@umich.edu
525222Sksewell@umich.edunamespace MipsISA {
535222Sksewell@umich.edu
542686Sksewell@umich.eduuint64_t
557707Sgblack@eecs.umich.edugetArgument(ThreadContext *tc, int &number, uint16_t size, bool fp)
565222Sksewell@umich.edu{
575222Sksewell@umich.edu#if FULL_SYSTEM
586379Sgblack@eecs.umich.edu    if (number < 4) {
595222Sksewell@umich.edu        if (fp)
606379Sgblack@eecs.umich.edu            return tc->readFloatRegBits(FirstArgumentReg + number);
615222Sksewell@umich.edu        else
626379Sgblack@eecs.umich.edu            return tc->readIntReg(FirstArgumentReg + number);
635222Sksewell@umich.edu    } else {
645222Sksewell@umich.edu        Addr sp = tc->readIntReg(StackPointerReg);
655498Ssaidi@eecs.umich.edu        VirtualPort *vp = tc->getVirtPort();
665222Sksewell@umich.edu        uint64_t arg = vp->read<uint64_t>(sp +
676379Sgblack@eecs.umich.edu                (number - 4) * sizeof(uint64_t));
685222Sksewell@umich.edu        return arg;
695222Sksewell@umich.edu    }
705222Sksewell@umich.edu#else
715222Sksewell@umich.edu    panic("getArgument() is Full system only\n");
725222Sksewell@umich.edu    M5_DUMMY_RETURN
735222Sksewell@umich.edu#endif
745222Sksewell@umich.edu}
755222Sksewell@umich.edu
765222Sksewell@umich.eduuint64_t
775222Sksewell@umich.edufpConvert(ConvertType cvt_type, double fp_val)
782686Sksewell@umich.edu{
792686Sksewell@umich.edu
802686Sksewell@umich.edu    switch (cvt_type)
812686Sksewell@umich.edu    {
822686Sksewell@umich.edu      case SINGLE_TO_DOUBLE:
832686Sksewell@umich.edu        {
842686Sksewell@umich.edu            double sdouble_val = fp_val;
852686Sksewell@umich.edu            void  *sdouble_ptr = &sdouble_val;
862686Sksewell@umich.edu            uint64_t sdp_bits  = *(uint64_t *) sdouble_ptr;
872686Sksewell@umich.edu            return sdp_bits;
882686Sksewell@umich.edu        }
892686Sksewell@umich.edu
902686Sksewell@umich.edu      case SINGLE_TO_WORD:
912686Sksewell@umich.edu        {
922686Sksewell@umich.edu            int32_t sword_val  = (int32_t) fp_val;
932686Sksewell@umich.edu            void  *sword_ptr   = &sword_val;
942686Sksewell@umich.edu            uint64_t sword_bits= *(uint32_t *) sword_ptr;
952686Sksewell@umich.edu            return sword_bits;
962686Sksewell@umich.edu        }
972686Sksewell@umich.edu
982686Sksewell@umich.edu      case WORD_TO_SINGLE:
992686Sksewell@umich.edu        {
1002686Sksewell@umich.edu            float wfloat_val   = fp_val;
1012686Sksewell@umich.edu            void  *wfloat_ptr  = &wfloat_val;
1022686Sksewell@umich.edu            uint64_t wfloat_bits = *(uint32_t *) wfloat_ptr;
1032686Sksewell@umich.edu            return wfloat_bits;
1042686Sksewell@umich.edu        }
1052686Sksewell@umich.edu
1062686Sksewell@umich.edu      case WORD_TO_DOUBLE:
1072686Sksewell@umich.edu        {
1082686Sksewell@umich.edu            double wdouble_val = fp_val;
1092686Sksewell@umich.edu            void  *wdouble_ptr = &wdouble_val;
1102686Sksewell@umich.edu            uint64_t wdp_bits  = *(uint64_t *) wdouble_ptr;
1112686Sksewell@umich.edu            return wdp_bits;
1122686Sksewell@umich.edu        }
1132686Sksewell@umich.edu
1142686Sksewell@umich.edu      default:
1152686Sksewell@umich.edu        panic("Invalid Floating Point Conversion Type (%d). See \"types.hh\" for List of Conversions\n",cvt_type);
1162686Sksewell@umich.edu        return 0;
1172686Sksewell@umich.edu    }
1182686Sksewell@umich.edu}
1192686Sksewell@umich.edu
1202686Sksewell@umich.edudouble
1215222Sksewell@umich.eduroundFP(double val, int digits)
1222686Sksewell@umich.edu{
1232686Sksewell@umich.edu    double digit_offset = pow(10.0,digits);
1242686Sksewell@umich.edu    val = val * digit_offset;
1252686Sksewell@umich.edu    val = val + 0.5;
1262686Sksewell@umich.edu    val = floor(val);
1272686Sksewell@umich.edu    val = val / digit_offset;
1282686Sksewell@umich.edu    return val;
1292686Sksewell@umich.edu}
1302686Sksewell@umich.edu
1312686Sksewell@umich.edudouble
1325222Sksewell@umich.edutruncFP(double val)
1332686Sksewell@umich.edu{
1342686Sksewell@umich.edu    int trunc_val = (int) val;
1352686Sksewell@umich.edu    return (double) trunc_val;
1362686Sksewell@umich.edu}
1372686Sksewell@umich.edu
1382686Sksewell@umich.edubool
1395222Sksewell@umich.edugetCondCode(uint32_t fcsr, int cc_idx)
1402686Sksewell@umich.edu{
1412686Sksewell@umich.edu    int shift = (cc_idx == 0) ? 23 : cc_idx + 24;
1422686Sksewell@umich.edu    bool cc_val = (fcsr >> shift) & 0x00000001;
1432686Sksewell@umich.edu    return cc_val;
1442686Sksewell@umich.edu}
1452686Sksewell@umich.edu
1462686Sksewell@umich.eduuint32_t
1475222Sksewell@umich.edugenCCVector(uint32_t fcsr, int cc_num, uint32_t cc_val)
1482686Sksewell@umich.edu{
1492686Sksewell@umich.edu    int cc_idx = (cc_num == 0) ? 23 : cc_num + 24;
1502686Sksewell@umich.edu
1515570Snate@binkert.org    fcsr = bits(fcsr, 31, cc_idx + 1) << (cc_idx + 1) |
1522686Sksewell@umich.edu           cc_val << cc_idx |
1532686Sksewell@umich.edu           bits(fcsr, cc_idx - 1, 0);
1542686Sksewell@umich.edu
1552686Sksewell@umich.edu    return fcsr;
1562686Sksewell@umich.edu}
1572686Sksewell@umich.edu
1582686Sksewell@umich.eduuint32_t
1595222Sksewell@umich.edugenInvalidVector(uint32_t fcsr_bits)
1602686Sksewell@umich.edu{
1612686Sksewell@umich.edu    //Set FCSR invalid in "flag" field
1622686Sksewell@umich.edu    int invalid_offset = Invalid + Flag_Field;
1632686Sksewell@umich.edu    fcsr_bits = fcsr_bits | (1 << invalid_offset);
1642686Sksewell@umich.edu
1652686Sksewell@umich.edu    //Set FCSR invalid in "cause" flag
1662686Sksewell@umich.edu    int cause_offset = Invalid + Cause_Field;
1672686Sksewell@umich.edu    fcsr_bits = fcsr_bits | (1 << cause_offset);
1682686Sksewell@umich.edu
1692686Sksewell@umich.edu    return fcsr_bits;
1702686Sksewell@umich.edu}
1712686Sksewell@umich.edu
1722686Sksewell@umich.edubool
1735222Sksewell@umich.eduisNan(void *val_ptr, int size)
1742686Sksewell@umich.edu{
1752686Sksewell@umich.edu    switch (size)
1762686Sksewell@umich.edu    {
1772686Sksewell@umich.edu      case 32:
1782686Sksewell@umich.edu        {
1792686Sksewell@umich.edu            uint32_t val_bits = *(uint32_t *) val_ptr;
1802686Sksewell@umich.edu            return (bits(val_bits, 30, 23) == 0xFF);
1812686Sksewell@umich.edu        }
1822686Sksewell@umich.edu
1832686Sksewell@umich.edu      case 64:
1842686Sksewell@umich.edu        {
1852686Sksewell@umich.edu            uint64_t val_bits = *(uint64_t *) val_ptr;
1862686Sksewell@umich.edu            return (bits(val_bits, 62, 52) == 0x7FF);
1872686Sksewell@umich.edu        }
1882686Sksewell@umich.edu
1892686Sksewell@umich.edu      default:
1902686Sksewell@umich.edu        panic("Type unsupported. Size mismatch\n");
1912686Sksewell@umich.edu    }
1922686Sksewell@umich.edu}
1932686Sksewell@umich.edu
1942686Sksewell@umich.edu
1952686Sksewell@umich.edubool
1965222Sksewell@umich.eduisQnan(void *val_ptr, int size)
1972686Sksewell@umich.edu{
1982686Sksewell@umich.edu    switch (size)
1992686Sksewell@umich.edu    {
2002686Sksewell@umich.edu      case 32:
2012686Sksewell@umich.edu        {
2022686Sksewell@umich.edu            uint32_t val_bits = *(uint32_t *) val_ptr;
2032686Sksewell@umich.edu            return (bits(val_bits, 30, 22) == 0x1FE);
2042686Sksewell@umich.edu        }
2052686Sksewell@umich.edu
2062686Sksewell@umich.edu      case 64:
2072686Sksewell@umich.edu        {
2082686Sksewell@umich.edu            uint64_t val_bits = *(uint64_t *) val_ptr;
2092686Sksewell@umich.edu            return (bits(val_bits, 62, 51) == 0xFFE);
2102686Sksewell@umich.edu        }
2112686Sksewell@umich.edu
2122686Sksewell@umich.edu      default:
2132686Sksewell@umich.edu        panic("Type unsupported. Size mismatch\n");
2142686Sksewell@umich.edu    }
2152686Sksewell@umich.edu}
2162686Sksewell@umich.edu
2172686Sksewell@umich.edubool
2185222Sksewell@umich.eduisSnan(void *val_ptr, int size)
2192686Sksewell@umich.edu{
2202686Sksewell@umich.edu    switch (size)
2212686Sksewell@umich.edu    {
2222686Sksewell@umich.edu      case 32:
2232686Sksewell@umich.edu        {
2242686Sksewell@umich.edu            uint32_t val_bits = *(uint32_t *) val_ptr;
2252686Sksewell@umich.edu            return (bits(val_bits, 30, 22) == 0x1FF);
2262686Sksewell@umich.edu        }
2272686Sksewell@umich.edu
2282686Sksewell@umich.edu      case 64:
2292686Sksewell@umich.edu        {
2302686Sksewell@umich.edu            uint64_t val_bits = *(uint64_t *) val_ptr;
2312686Sksewell@umich.edu            return (bits(val_bits, 62, 51) == 0xFFF);
2322686Sksewell@umich.edu        }
2332686Sksewell@umich.edu
2342686Sksewell@umich.edu      default:
2352686Sksewell@umich.edu        panic("Type unsupported. Size mismatch\n");
2362686Sksewell@umich.edu    }
2372686Sksewell@umich.edu}
2384661Sksewell@umich.edu
2395222Sksewell@umich.edutemplate <class CPU>
2405222Sksewell@umich.eduvoid
2415222Sksewell@umich.eduzeroRegisters(CPU *cpu)
2425222Sksewell@umich.edu{
2435222Sksewell@umich.edu    // Insure ISA semantics
2445222Sksewell@umich.edu    // (no longer very clean due to the change in setIntReg() in the
2455222Sksewell@umich.edu    // cpu model.  Consider changing later.)
2465222Sksewell@umich.edu    cpu->thread->setIntReg(ZeroReg, 0);
2475222Sksewell@umich.edu    cpu->thread->setFloatReg(ZeroReg, 0.0);
2485222Sksewell@umich.edu}
2495222Sksewell@umich.edu
2505222Sksewell@umich.eduvoid
2515222Sksewell@umich.edustartupCPU(ThreadContext *tc, int cpuId)
2525222Sksewell@umich.edu{
2535715Shsul@eecs.umich.edu    tc->activate(0/*tc->threadId()*/);
2545222Sksewell@umich.edu}
2555222Sksewell@umich.edu
2566329Sgblack@eecs.umich.eduvoid
2576329Sgblack@eecs.umich.educopyRegs(ThreadContext *src, ThreadContext *dest)
2586329Sgblack@eecs.umich.edu{
2596329Sgblack@eecs.umich.edu    panic("Copy Regs Not Implemented Yet\n");
2606329Sgblack@eecs.umich.edu}
2616329Sgblack@eecs.umich.edu
2626329Sgblack@eecs.umich.eduvoid
2636329Sgblack@eecs.umich.educopyMiscRegs(ThreadContext *src, ThreadContext *dest)
2646329Sgblack@eecs.umich.edu{
2656329Sgblack@eecs.umich.edu    panic("Copy Misc. Regs Not Implemented Yet\n");
2666329Sgblack@eecs.umich.edu}
2677693SAli.Saidi@ARM.comvoid
2687693SAli.Saidi@ARM.comskipFunction(ThreadContext *tc)
2697693SAli.Saidi@ARM.com{
2707693SAli.Saidi@ARM.com    Addr newpc = tc->readIntReg(ReturnAddressReg);
2717693SAli.Saidi@ARM.com    tc->setPC(newpc);
2727693SAli.Saidi@ARM.com    tc->setNextPC(tc->readPC() + sizeof(TheISA::MachInst));
2737693SAli.Saidi@ARM.com    tc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst));
2747693SAli.Saidi@ARM.com}
2757693SAli.Saidi@ARM.com
2766329Sgblack@eecs.umich.edu
2775222Sksewell@umich.edu} // namespace MipsISA
278