utility.cc revision 12334
15086Sgblack@eecs.umich.edu/*
25086Sgblack@eecs.umich.edu * Copyright (c) 2007 The Hewlett-Packard Development Company
38466Snilay@cs.wisc.edu * Copyright (c) 2011 Advanced Micro Devices, Inc.
45086Sgblack@eecs.umich.edu * All rights reserved.
55086Sgblack@eecs.umich.edu *
67087Snate@binkert.org * The license below extends only to copyright in the software and shall
77087Snate@binkert.org * not be construed as granting a license to any other intellectual
87087Snate@binkert.org * property including but not limited to intellectual property relating
97087Snate@binkert.org * to a hardware implementation of the functionality of the software
107087Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
117087Snate@binkert.org * terms below provided that you ensure that this notice is replicated
127087Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
137087Snate@binkert.org * modified or unmodified, in source code or in binary form.
145086Sgblack@eecs.umich.edu *
157087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
167087Snate@binkert.org * modification, are permitted provided that the following conditions are
177087Snate@binkert.org * met: redistributions of source code must retain the above copyright
187087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
197087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
207087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
217087Snate@binkert.org * documentation and/or other materials provided with the distribution;
227087Snate@binkert.org * neither the name of the copyright holders nor the names of its
235086Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
247087Snate@binkert.org * this software without specific prior written permission.
255086Sgblack@eecs.umich.edu *
265086Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
275086Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
285086Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
295086Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
305086Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
315086Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
325086Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
335086Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
345086Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
355086Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
365086Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
375086Sgblack@eecs.umich.edu *
385086Sgblack@eecs.umich.edu * Authors: Gabe Black
395086Sgblack@eecs.umich.edu */
405086Sgblack@eecs.umich.edu
4111793Sbrandon.potter@amd.com#include "arch/x86/utility.hh"
4211793Sbrandon.potter@amd.com
435647Sgblack@eecs.umich.edu#include "arch/x86/interrupts.hh"
448466Snilay@cs.wisc.edu#include "arch/x86/registers.hh"
455135Sgblack@eecs.umich.edu#include "arch/x86/x86_traits.hh"
465647Sgblack@eecs.umich.edu#include "cpu/base.hh"
479889Sandreas@sandberg.pp.se#include "fputils/fp80.h"
4811800Sbrandon.potter@amd.com#include "sim/full_system.hh"
495086Sgblack@eecs.umich.edu
505086Sgblack@eecs.umich.edunamespace X86ISA {
515086Sgblack@eecs.umich.edu
527707Sgblack@eecs.umich.eduuint64_t
537707Sgblack@eecs.umich.edugetArgument(ThreadContext *tc, int &number, uint16_t size, bool fp)
547707Sgblack@eecs.umich.edu{
5510553Salexandru.dutu@amd.com    if (fp) {
569887Sandreas@sandberg.pp.se        panic("getArgument(): Floating point arguments not implemented\n");
579887Sandreas@sandberg.pp.se    } else if (size != 8) {
589887Sandreas@sandberg.pp.se        panic("getArgument(): Can only handle 64-bit arguments.\n");
599887Sandreas@sandberg.pp.se    }
609887Sandreas@sandberg.pp.se
619887Sandreas@sandberg.pp.se    // The first 6 integer arguments are passed in registers, the rest
629887Sandreas@sandberg.pp.se    // are passed on the stack.
639887Sandreas@sandberg.pp.se    const int int_reg_map[] = {
649887Sandreas@sandberg.pp.se        INTREG_RDI, INTREG_RSI, INTREG_RDX,
659887Sandreas@sandberg.pp.se        INTREG_RCX, INTREG_R8, INTREG_R9
669887Sandreas@sandberg.pp.se    };
679887Sandreas@sandberg.pp.se    if (number < sizeof(int_reg_map) / sizeof(*int_reg_map)) {
689887Sandreas@sandberg.pp.se        return tc->readIntReg(int_reg_map[number]);
699887Sandreas@sandberg.pp.se    } else {
709887Sandreas@sandberg.pp.se        panic("getArgument(): Don't know how to handle stack arguments.\n");
719887Sandreas@sandberg.pp.se    }
725086Sgblack@eecs.umich.edu}
735135Sgblack@eecs.umich.edu
745135Sgblack@eecs.umich.eduvoid initCPU(ThreadContext *tc, int cpuId)
755135Sgblack@eecs.umich.edu{
766048Sgblack@eecs.umich.edu    // This function is essentially performing a reset. The actual INIT
776048Sgblack@eecs.umich.edu    // interrupt does a subset of this, so we'll piggyback on some of its
786048Sgblack@eecs.umich.edu    // functionality.
796048Sgblack@eecs.umich.edu    InitInterrupt init(0);
806048Sgblack@eecs.umich.edu    init.invoke(tc);
816048Sgblack@eecs.umich.edu
827720Sgblack@eecs.umich.edu    PCState pc = tc->pcState();
837720Sgblack@eecs.umich.edu    pc.upc(0);
847720Sgblack@eecs.umich.edu    pc.nupc(1);
857720Sgblack@eecs.umich.edu    tc->pcState(pc);
865135Sgblack@eecs.umich.edu
875135Sgblack@eecs.umich.edu    // These next two loops zero internal microcode and implicit registers.
885135Sgblack@eecs.umich.edu    // They aren't specified by the ISA but are used internally by M5's
895135Sgblack@eecs.umich.edu    // implementation.
905135Sgblack@eecs.umich.edu    for (int index = 0; index < NumMicroIntRegs; index++) {
915135Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_MICRO(index), 0);
925135Sgblack@eecs.umich.edu    }
935135Sgblack@eecs.umich.edu
945135Sgblack@eecs.umich.edu    for (int index = 0; index < NumImplicitIntRegs; index++) {
955135Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_IMPLICIT(index), 0);
965135Sgblack@eecs.umich.edu    }
975135Sgblack@eecs.umich.edu
985135Sgblack@eecs.umich.edu    // Set integer register EAX to 0 to indicate that the optional BIST
995135Sgblack@eecs.umich.edu    // passed. No BIST actually runs, but software may still check this
1005135Sgblack@eecs.umich.edu    // register for errors.
1015135Sgblack@eecs.umich.edu    tc->setIntReg(INTREG_RAX, 0);
1025135Sgblack@eecs.umich.edu
1035264Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CR0, 0x0000000060000010ULL);
1045135Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CR8, 0);
1055135Sgblack@eecs.umich.edu
1065135Sgblack@eecs.umich.edu    // TODO initialize x87, 64 bit, and 128 bit media state
1075135Sgblack@eecs.umich.edu
1085141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRRCAP, 0x0508);
1095141Sgblack@eecs.umich.edu    for (int i = 0; i < 8; i++) {
1105141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MTRR_PHYS_BASE(i), 0);
1115141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MTRR_PHYS_MASK(i), 0);
1125141Sgblack@eecs.umich.edu    }
1135141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_64K_00000, 0);
1145141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_16K_80000, 0);
1155141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_16K_A0000, 0);
1165141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_C0000, 0);
1175182Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_C8000, 0);
1185141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_D0000, 0);
1195141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_D8000, 0);
1205141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_E0000, 0);
1215141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_E8000, 0);
1225141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_F0000, 0);
1235141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_F8000, 0);
1245135Sgblack@eecs.umich.edu
1255141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_DEF_TYPE, 0);
1265141Sgblack@eecs.umich.edu
1275141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MCG_CAP, 0x104);
1285141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MCG_STATUS, 0);
1295141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MCG_CTL, 0);
1305141Sgblack@eecs.umich.edu
1315141Sgblack@eecs.umich.edu    for (int i = 0; i < 5; i++) {
1325141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_CTL(i), 0);
1335141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_STATUS(i), 0);
1345141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_ADDR(i), 0);
1355141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_MISC(i), 0);
1365141Sgblack@eecs.umich.edu    }
1375135Sgblack@eecs.umich.edu
1385141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TSC, 0);
1395141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TSC_AUX, 0);
1405135Sgblack@eecs.umich.edu
1415141Sgblack@eecs.umich.edu    for (int i = 0; i < 4; i++) {
1425141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_PERF_EVT_SEL(i), 0);
1435141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_PERF_EVT_CTR(i), 0);
1445141Sgblack@eecs.umich.edu    }
1455135Sgblack@eecs.umich.edu
1465141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_STAR, 0);
1475141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LSTAR, 0);
1485141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CSTAR, 0);
1495141Sgblack@eecs.umich.edu
1505141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SF_MASK, 0);
1515141Sgblack@eecs.umich.edu
1525141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_KERNEL_GS_BASE, 0);
1535141Sgblack@eecs.umich.edu
1545141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSENTER_CS, 0);
1555141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSENTER_ESP, 0);
1565141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSENTER_EIP, 0);
1575141Sgblack@eecs.umich.edu
1585264Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_PAT, 0x0007040600070406ULL);
1595141Sgblack@eecs.umich.edu
1605141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSCFG, 0x20601);
1615141Sgblack@eecs.umich.edu
1625141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_BASE0, 0);
1635141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_BASE1, 0);
1645141Sgblack@eecs.umich.edu
1655141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_MASK0, 0);
1665141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_MASK1, 0);
1675141Sgblack@eecs.umich.edu
1685141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TOP_MEM, 0x4000000);
1695141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TOP_MEM2, 0x0);
1705141Sgblack@eecs.umich.edu
1715141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_DEBUG_CTL_MSR, 0);
1725141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_BRANCH_FROM_IP, 0);
1735141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_BRANCH_TO_IP, 0);
1745141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_EXCEPTION_FROM_IP, 0);
1755141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_EXCEPTION_TO_IP, 0);
1765135Sgblack@eecs.umich.edu
1775135Sgblack@eecs.umich.edu    // Invalidate the caches (this should already be done for us)
1785135Sgblack@eecs.umich.edu
1795360Sgblack@eecs.umich.edu    LocalApicBase lApicBase = 0;
1805360Sgblack@eecs.umich.edu    lApicBase.base = 0xFEE00000 >> 12;
1815360Sgblack@eecs.umich.edu    lApicBase.enable = 1;
1825360Sgblack@eecs.umich.edu    lApicBase.bsp = (cpuId == 0);
1835360Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_APIC_BASE, lApicBase);
1845360Sgblack@eecs.umich.edu
1855647Sgblack@eecs.umich.edu    Interrupts * interrupts = dynamic_cast<Interrupts *>(
18611150Smitch.hayenga@arm.com            tc->getCpuPtr()->getInterruptController(0));
1875647Sgblack@eecs.umich.edu    assert(interrupts);
1885360Sgblack@eecs.umich.edu
1895647Sgblack@eecs.umich.edu    interrupts->setRegNoEffect(APIC_ID, cpuId << 24);
1905647Sgblack@eecs.umich.edu
1915647Sgblack@eecs.umich.edu    interrupts->setRegNoEffect(APIC_VERSION, (5 << 16) | 0x14);
1929157Sandreas.hansson@arm.com
1935141Sgblack@eecs.umich.edu    // TODO Set the SMRAM base address (SMBASE) to 0x00030000
1945141Sgblack@eecs.umich.edu
1955141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_VM_CR, 0);
1965141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IGNNE, 0);
1975141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SMM_CTL, 0);
1985141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_VM_HSAVE_PA, 0);
1995135Sgblack@eecs.umich.edu}
2005135Sgblack@eecs.umich.edu
2015135Sgblack@eecs.umich.eduvoid startupCPU(ThreadContext *tc, int cpuId)
2025135Sgblack@eecs.umich.edu{
2038768Sgblack@eecs.umich.edu    if (cpuId == 0 || !FullSystem) {
20410407Smitch.hayenga@arm.com        tc->activate();
2055135Sgblack@eecs.umich.edu    } else {
2065135Sgblack@eecs.umich.edu        // This is an application processor (AP). It should be initialized to
2075135Sgblack@eecs.umich.edu        // look like only the BIOS POST has run on it and put then put it into
2085135Sgblack@eecs.umich.edu        // a halted state.
20910407Smitch.hayenga@arm.com        tc->suspend();
2105135Sgblack@eecs.umich.edu    }
2115135Sgblack@eecs.umich.edu}
2125135Sgblack@eecs.umich.edu
2136329Sgblack@eecs.umich.eduvoid
2146329Sgblack@eecs.umich.educopyMiscRegs(ThreadContext *src, ThreadContext *dest)
2156329Sgblack@eecs.umich.edu{
2168466Snilay@cs.wisc.edu    // This function assumes no side effects other than TLB invalidation
2178466Snilay@cs.wisc.edu    // need to be considered while copying state. That will likely not be
2188466Snilay@cs.wisc.edu    // true in the future.
2196329Sgblack@eecs.umich.edu    for (int i = 0; i < NUM_MISCREGS; ++i) {
22011324Ssteve.reinhardt@amd.com        if (!isValidMiscReg(i))
2216329Sgblack@eecs.umich.edu             continue;
22211324Ssteve.reinhardt@amd.com
2236329Sgblack@eecs.umich.edu        dest->setMiscRegNoEffect(i, src->readMiscRegNoEffect(i));
2246329Sgblack@eecs.umich.edu    }
2258466Snilay@cs.wisc.edu
2269751Sandreas@sandberg.pp.se    // The TSC has to be updated with side-effects if the CPUs in a
2279751Sandreas@sandberg.pp.se    // CPU switch have different frequencies.
2289751Sandreas@sandberg.pp.se    dest->setMiscReg(MISCREG_TSC, src->readMiscReg(MISCREG_TSC));
2299751Sandreas@sandberg.pp.se
2309423SAndreas.Sandberg@arm.com    dest->getITBPtr()->flushAll();
2319423SAndreas.Sandberg@arm.com    dest->getDTBPtr()->flushAll();
2326329Sgblack@eecs.umich.edu}
2336329Sgblack@eecs.umich.edu
2346329Sgblack@eecs.umich.eduvoid
2356329Sgblack@eecs.umich.educopyRegs(ThreadContext *src, ThreadContext *dest)
2366329Sgblack@eecs.umich.edu{
2376329Sgblack@eecs.umich.edu    //copy int regs
2388466Snilay@cs.wisc.edu    for (int i = 0; i < NumIntRegs; ++i)
23910058Sandreas@sandberg.pp.se         dest->setIntRegFlat(i, src->readIntRegFlat(i));
2406329Sgblack@eecs.umich.edu    //copy float regs
2418466Snilay@cs.wisc.edu    for (int i = 0; i < NumFloatRegs; ++i)
24210058Sandreas@sandberg.pp.se         dest->setFloatRegBitsFlat(i, src->readFloatRegBitsFlat(i));
2439921Syasuko.eckert@amd.com    //copy condition-code regs
2449921Syasuko.eckert@amd.com    for (int i = 0; i < NumCCRegs; ++i)
24510058Sandreas@sandberg.pp.se         dest->setCCRegFlat(i, src->readCCRegFlat(i));
2466329Sgblack@eecs.umich.edu    copyMiscRegs(src, dest);
2477720Sgblack@eecs.umich.edu    dest->pcState(src->pcState());
2486329Sgblack@eecs.umich.edu}
2496329Sgblack@eecs.umich.edu
2507693SAli.Saidi@ARM.comvoid
2517693SAli.Saidi@ARM.comskipFunction(ThreadContext *tc)
2527693SAli.Saidi@ARM.com{
2537693SAli.Saidi@ARM.com    panic("Not implemented for x86\n");
2547693SAli.Saidi@ARM.com}
2557693SAli.Saidi@ARM.com
2569759Sandreas@sandberg.pp.seuint64_t
2579759Sandreas@sandberg.pp.segetRFlags(ThreadContext *tc)
2589759Sandreas@sandberg.pp.se{
2599759Sandreas@sandberg.pp.se    const uint64_t ncc_flags(tc->readMiscRegNoEffect(MISCREG_RFLAGS));
26010057Snikos.nikoleris@gmail.com    const uint64_t cc_flags(tc->readCCReg(X86ISA::CCREG_ZAPS));
26110057Snikos.nikoleris@gmail.com    const uint64_t cfof_bits(tc->readCCReg(X86ISA::CCREG_CFOF));
26210057Snikos.nikoleris@gmail.com    const uint64_t df_bit(tc->readCCReg(X86ISA::CCREG_DF));
2639759Sandreas@sandberg.pp.se    // ecf (PSEUDO(3)) & ezf (PSEUDO(4)) are only visible to
2649759Sandreas@sandberg.pp.se    // microcode, so we can safely ignore them.
2659759Sandreas@sandberg.pp.se
2669759Sandreas@sandberg.pp.se    // Reconstruct the real rflags state, mask out internal flags, and
2679759Sandreas@sandberg.pp.se    // make sure reserved bits have the expected values.
2689759Sandreas@sandberg.pp.se    return ((ncc_flags | cc_flags | cfof_bits | df_bit) & 0x3F7FD5)
2699759Sandreas@sandberg.pp.se        | 0x2;
2709759Sandreas@sandberg.pp.se}
2719759Sandreas@sandberg.pp.se
2729759Sandreas@sandberg.pp.sevoid
2739759Sandreas@sandberg.pp.sesetRFlags(ThreadContext *tc, uint64_t val)
2749759Sandreas@sandberg.pp.se{
27510057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_ZAPS, val & ccFlagMask);
27610057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_CFOF, val & cfofMask);
27710057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_DF, val & DFBit);
2789759Sandreas@sandberg.pp.se
2799759Sandreas@sandberg.pp.se    // Internal microcode registers (ECF & EZF)
28010057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_ECF, 0);
28110057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_EZF, 0);
2829759Sandreas@sandberg.pp.se
2839759Sandreas@sandberg.pp.se    // Update the RFLAGS misc reg with whatever didn't go into the
2849759Sandreas@sandberg.pp.se    // magic registers.
2859759Sandreas@sandberg.pp.se    tc->setMiscReg(MISCREG_RFLAGS, val & ~(ccFlagMask | cfofMask | DFBit));
2869759Sandreas@sandberg.pp.se}
2877693SAli.Saidi@ARM.com
2889880Sandreas@sandberg.pp.seuint8_t
2899880Sandreas@sandberg.pp.seconvX87TagsToXTags(uint16_t ftw)
2909880Sandreas@sandberg.pp.se{
2919880Sandreas@sandberg.pp.se    uint8_t ftwx(0);
2929880Sandreas@sandberg.pp.se    for (int i = 0; i < 8; ++i) {
2939880Sandreas@sandberg.pp.se        // Extract the tag for the current element on the FP stack
2949880Sandreas@sandberg.pp.se        const unsigned tag((ftw >> (2 * i)) & 0x3);
2959880Sandreas@sandberg.pp.se
2969880Sandreas@sandberg.pp.se        /*
2979880Sandreas@sandberg.pp.se         * Check the type of the current FP element. Valid values are:
2989880Sandreas@sandberg.pp.se         * 0 == Valid
2999880Sandreas@sandberg.pp.se         * 1 == Zero
3009880Sandreas@sandberg.pp.se         * 2 == Special (Nan, unsupported, infinity, denormal)
3019880Sandreas@sandberg.pp.se         * 3 == Empty
3029880Sandreas@sandberg.pp.se         */
3039880Sandreas@sandberg.pp.se        // The xsave version of the tag word only keeps track of
3049880Sandreas@sandberg.pp.se        // whether the element is empty or not. Set the corresponding
3059880Sandreas@sandberg.pp.se        // bit in the ftwx if it's not empty,
3069880Sandreas@sandberg.pp.se        if (tag != 0x3)
3079880Sandreas@sandberg.pp.se            ftwx |= 1 << i;
3089880Sandreas@sandberg.pp.se    }
3099880Sandreas@sandberg.pp.se
3109880Sandreas@sandberg.pp.se    return ftwx;
3119880Sandreas@sandberg.pp.se}
3129880Sandreas@sandberg.pp.se
3139880Sandreas@sandberg.pp.seuint16_t
3149880Sandreas@sandberg.pp.seconvX87XTagsToTags(uint8_t ftwx)
3159880Sandreas@sandberg.pp.se{
3169880Sandreas@sandberg.pp.se    uint16_t ftw(0);
3179880Sandreas@sandberg.pp.se    for (int i = 0; i < 8; ++i) {
3189880Sandreas@sandberg.pp.se        const unsigned xtag(((ftwx >> i) & 0x1));
3199880Sandreas@sandberg.pp.se
3209880Sandreas@sandberg.pp.se        // The xtag for an x87 stack position is 0 for empty stack positions.
3219880Sandreas@sandberg.pp.se        if (!xtag) {
3229880Sandreas@sandberg.pp.se            // Set the tag word to 3 (empty) for the current element.
3239880Sandreas@sandberg.pp.se            ftw |= 0x3 << (2 * i);
3249880Sandreas@sandberg.pp.se        } else {
3259880Sandreas@sandberg.pp.se            // TODO: We currently assume that non-empty elements are
3269880Sandreas@sandberg.pp.se            // valid (0x0), but we should ideally reconstruct the full
3279880Sandreas@sandberg.pp.se            // state (valid/zero/special).
3289880Sandreas@sandberg.pp.se        }
3299880Sandreas@sandberg.pp.se    }
3309880Sandreas@sandberg.pp.se
3319880Sandreas@sandberg.pp.se    return ftw;
3329880Sandreas@sandberg.pp.se}
3339880Sandreas@sandberg.pp.se
3349765Sandreas@sandberg.pp.seuint16_t
3359765Sandreas@sandberg.pp.segenX87Tags(uint16_t ftw, uint8_t top, int8_t spm)
3369765Sandreas@sandberg.pp.se{
3379765Sandreas@sandberg.pp.se    const uint8_t new_top((top + spm + 8) % 8);
3389765Sandreas@sandberg.pp.se
3399765Sandreas@sandberg.pp.se    if (spm > 0) {
3409765Sandreas@sandberg.pp.se        // Removing elements from the stack. Flag the elements as empty.
3419765Sandreas@sandberg.pp.se        for (int i = top; i != new_top; i = (i + 1 + 8) % 8)
3429765Sandreas@sandberg.pp.se            ftw |= 0x3 << (2 * i);
3439765Sandreas@sandberg.pp.se    } else if (spm < 0) {
3449765Sandreas@sandberg.pp.se        // Adding elements to the stack. Flag the new elements as
3459765Sandreas@sandberg.pp.se        // valid. We should ideally decode them and "do the right
3469765Sandreas@sandberg.pp.se        // thing".
3479765Sandreas@sandberg.pp.se        for (int i = new_top; i != top; i = (i + 1 + 8) % 8)
3489765Sandreas@sandberg.pp.se            ftw &= ~(0x3 << (2 * i));
3499765Sandreas@sandberg.pp.se    }
3509765Sandreas@sandberg.pp.se
3519765Sandreas@sandberg.pp.se    return ftw;
3529765Sandreas@sandberg.pp.se}
3539765Sandreas@sandberg.pp.se
3549889Sandreas@sandberg.pp.sedouble
3559889Sandreas@sandberg.pp.seloadFloat80(const void *_mem)
3569889Sandreas@sandberg.pp.se{
35711709Santhony.gutierrez@amd.com    fp80_t fp80;
35811709Santhony.gutierrez@amd.com    memcpy(fp80.bits, _mem, 10);
3599889Sandreas@sandberg.pp.se
36011709Santhony.gutierrez@amd.com    return fp80_cvtd(fp80);
3619889Sandreas@sandberg.pp.se}
3629889Sandreas@sandberg.pp.se
3639889Sandreas@sandberg.pp.sevoid
3649889Sandreas@sandberg.pp.sestoreFloat80(void *_mem, double value)
3659889Sandreas@sandberg.pp.se{
36611709Santhony.gutierrez@amd.com    fp80_t fp80 = fp80_cvfd(value);
36711709Santhony.gutierrez@amd.com    memcpy(_mem, fp80.bits, 10);
3689889Sandreas@sandberg.pp.se}
3699889Sandreas@sandberg.pp.se
3707811Ssteve.reinhardt@amd.com} // namespace X86_ISA
371