utility.cc revision 10057
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
415647Sgblack@eecs.umich.edu#include "arch/x86/interrupts.hh"
428466Snilay@cs.wisc.edu#include "arch/x86/registers.hh"
438466Snilay@cs.wisc.edu#include "arch/x86/tlb.hh"
445086Sgblack@eecs.umich.edu#include "arch/x86/utility.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"
485234Sgblack@eecs.umich.edu#include "sim/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{
559887Sandreas@sandberg.pp.se    if (!FullSystem) {
569887Sandreas@sandberg.pp.se        panic("getArgument() only implemented for full system mode.\n");
579887Sandreas@sandberg.pp.se    } else if (fp) {
589887Sandreas@sandberg.pp.se        panic("getArgument(): Floating point arguments not implemented\n");
599887Sandreas@sandberg.pp.se    } else if (size != 8) {
609887Sandreas@sandberg.pp.se        panic("getArgument(): Can only handle 64-bit arguments.\n");
619887Sandreas@sandberg.pp.se    }
629887Sandreas@sandberg.pp.se
639887Sandreas@sandberg.pp.se    // The first 6 integer arguments are passed in registers, the rest
649887Sandreas@sandberg.pp.se    // are passed on the stack.
659887Sandreas@sandberg.pp.se    const int int_reg_map[] = {
669887Sandreas@sandberg.pp.se        INTREG_RDI, INTREG_RSI, INTREG_RDX,
679887Sandreas@sandberg.pp.se        INTREG_RCX, INTREG_R8, INTREG_R9
689887Sandreas@sandberg.pp.se    };
699887Sandreas@sandberg.pp.se    if (number < sizeof(int_reg_map) / sizeof(*int_reg_map)) {
709887Sandreas@sandberg.pp.se        return tc->readIntReg(int_reg_map[number]);
719887Sandreas@sandberg.pp.se    } else {
729887Sandreas@sandberg.pp.se        panic("getArgument(): Don't know how to handle stack arguments.\n");
739887Sandreas@sandberg.pp.se    }
745086Sgblack@eecs.umich.edu}
755135Sgblack@eecs.umich.edu
765135Sgblack@eecs.umich.eduvoid initCPU(ThreadContext *tc, int cpuId)
775135Sgblack@eecs.umich.edu{
786048Sgblack@eecs.umich.edu    // This function is essentially performing a reset. The actual INIT
796048Sgblack@eecs.umich.edu    // interrupt does a subset of this, so we'll piggyback on some of its
806048Sgblack@eecs.umich.edu    // functionality.
816048Sgblack@eecs.umich.edu    InitInterrupt init(0);
826048Sgblack@eecs.umich.edu    init.invoke(tc);
836048Sgblack@eecs.umich.edu
847720Sgblack@eecs.umich.edu    PCState pc = tc->pcState();
857720Sgblack@eecs.umich.edu    pc.upc(0);
867720Sgblack@eecs.umich.edu    pc.nupc(1);
877720Sgblack@eecs.umich.edu    tc->pcState(pc);
885135Sgblack@eecs.umich.edu
895135Sgblack@eecs.umich.edu    // These next two loops zero internal microcode and implicit registers.
905135Sgblack@eecs.umich.edu    // They aren't specified by the ISA but are used internally by M5's
915135Sgblack@eecs.umich.edu    // implementation.
925135Sgblack@eecs.umich.edu    for (int index = 0; index < NumMicroIntRegs; index++) {
935135Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_MICRO(index), 0);
945135Sgblack@eecs.umich.edu    }
955135Sgblack@eecs.umich.edu
965135Sgblack@eecs.umich.edu    for (int index = 0; index < NumImplicitIntRegs; index++) {
975135Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_IMPLICIT(index), 0);
985135Sgblack@eecs.umich.edu    }
995135Sgblack@eecs.umich.edu
1005135Sgblack@eecs.umich.edu    // Set integer register EAX to 0 to indicate that the optional BIST
1015135Sgblack@eecs.umich.edu    // passed. No BIST actually runs, but software may still check this
1025135Sgblack@eecs.umich.edu    // register for errors.
1035135Sgblack@eecs.umich.edu    tc->setIntReg(INTREG_RAX, 0);
1045135Sgblack@eecs.umich.edu
1055264Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CR0, 0x0000000060000010ULL);
1065135Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CR8, 0);
1075135Sgblack@eecs.umich.edu
1085135Sgblack@eecs.umich.edu    // TODO initialize x87, 64 bit, and 128 bit media state
1095135Sgblack@eecs.umich.edu
1105141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRRCAP, 0x0508);
1115141Sgblack@eecs.umich.edu    for (int i = 0; i < 8; i++) {
1125141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MTRR_PHYS_BASE(i), 0);
1135141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MTRR_PHYS_MASK(i), 0);
1145141Sgblack@eecs.umich.edu    }
1155141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_64K_00000, 0);
1165141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_16K_80000, 0);
1175141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_16K_A0000, 0);
1185141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_C0000, 0);
1195182Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_C8000, 0);
1205141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_D0000, 0);
1215141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_D8000, 0);
1225141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_E0000, 0);
1235141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_E8000, 0);
1245141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_F0000, 0);
1255141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MTRR_FIX_4K_F8000, 0);
1265135Sgblack@eecs.umich.edu
1275141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_DEF_TYPE, 0);
1285141Sgblack@eecs.umich.edu
1295141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MCG_CAP, 0x104);
1305141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MCG_STATUS, 0);
1315141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_MCG_CTL, 0);
1325141Sgblack@eecs.umich.edu
1335141Sgblack@eecs.umich.edu    for (int i = 0; i < 5; i++) {
1345141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_CTL(i), 0);
1355141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_STATUS(i), 0);
1365141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_ADDR(i), 0);
1375141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_MC_MISC(i), 0);
1385141Sgblack@eecs.umich.edu    }
1395135Sgblack@eecs.umich.edu
1405141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TSC, 0);
1415141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TSC_AUX, 0);
1425135Sgblack@eecs.umich.edu
1435141Sgblack@eecs.umich.edu    for (int i = 0; i < 4; i++) {
1445141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_PERF_EVT_SEL(i), 0);
1455141Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_PERF_EVT_CTR(i), 0);
1465141Sgblack@eecs.umich.edu    }
1475135Sgblack@eecs.umich.edu
1485141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_STAR, 0);
1495141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LSTAR, 0);
1505141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CSTAR, 0);
1515141Sgblack@eecs.umich.edu
1525141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SF_MASK, 0);
1535141Sgblack@eecs.umich.edu
1545141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_KERNEL_GS_BASE, 0);
1555141Sgblack@eecs.umich.edu
1565141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSENTER_CS, 0);
1575141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSENTER_ESP, 0);
1585141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSENTER_EIP, 0);
1595141Sgblack@eecs.umich.edu
1605264Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_PAT, 0x0007040600070406ULL);
1615141Sgblack@eecs.umich.edu
1625141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SYSCFG, 0x20601);
1635141Sgblack@eecs.umich.edu
1645141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_BASE0, 0);
1655141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_BASE1, 0);
1665141Sgblack@eecs.umich.edu
1675141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_MASK0, 0);
1685141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IORR_MASK1, 0);
1695141Sgblack@eecs.umich.edu
1705141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TOP_MEM, 0x4000000);
1715141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_TOP_MEM2, 0x0);
1725141Sgblack@eecs.umich.edu
1735141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_DEBUG_CTL_MSR, 0);
1745141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_BRANCH_FROM_IP, 0);
1755141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_BRANCH_TO_IP, 0);
1765141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_EXCEPTION_FROM_IP, 0);
1775141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_LAST_EXCEPTION_TO_IP, 0);
1785135Sgblack@eecs.umich.edu
1795135Sgblack@eecs.umich.edu    // Invalidate the caches (this should already be done for us)
1805135Sgblack@eecs.umich.edu
1815360Sgblack@eecs.umich.edu    LocalApicBase lApicBase = 0;
1825360Sgblack@eecs.umich.edu    lApicBase.base = 0xFEE00000 >> 12;
1835360Sgblack@eecs.umich.edu    lApicBase.enable = 1;
1845360Sgblack@eecs.umich.edu    lApicBase.bsp = (cpuId == 0);
1855360Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_APIC_BASE, lApicBase);
1865360Sgblack@eecs.umich.edu
1875647Sgblack@eecs.umich.edu    Interrupts * interrupts = dynamic_cast<Interrupts *>(
1885647Sgblack@eecs.umich.edu            tc->getCpuPtr()->getInterruptController());
1895647Sgblack@eecs.umich.edu    assert(interrupts);
1905360Sgblack@eecs.umich.edu
1915647Sgblack@eecs.umich.edu    interrupts->setRegNoEffect(APIC_ID, cpuId << 24);
1925647Sgblack@eecs.umich.edu
1935647Sgblack@eecs.umich.edu    interrupts->setRegNoEffect(APIC_VERSION, (5 << 16) | 0x14);
1949157Sandreas.hansson@arm.com
1955141Sgblack@eecs.umich.edu    // TODO Set the SMRAM base address (SMBASE) to 0x00030000
1965141Sgblack@eecs.umich.edu
1975141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_VM_CR, 0);
1985141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_IGNNE, 0);
1995141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_SMM_CTL, 0);
2005141Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_VM_HSAVE_PA, 0);
2015135Sgblack@eecs.umich.edu}
2025135Sgblack@eecs.umich.edu
2035135Sgblack@eecs.umich.eduvoid startupCPU(ThreadContext *tc, int cpuId)
2045135Sgblack@eecs.umich.edu{
2058768Sgblack@eecs.umich.edu    if (cpuId == 0 || !FullSystem) {
2069180Sandreas.hansson@arm.com        tc->activate(Cycles(0));
2075135Sgblack@eecs.umich.edu    } else {
2085135Sgblack@eecs.umich.edu        // This is an application processor (AP). It should be initialized to
2095135Sgblack@eecs.umich.edu        // look like only the BIOS POST has run on it and put then put it into
2105135Sgblack@eecs.umich.edu        // a halted state.
2119180Sandreas.hansson@arm.com        tc->suspend(Cycles(0));
2125135Sgblack@eecs.umich.edu    }
2135135Sgblack@eecs.umich.edu}
2145135Sgblack@eecs.umich.edu
2156329Sgblack@eecs.umich.eduvoid
2166329Sgblack@eecs.umich.educopyMiscRegs(ThreadContext *src, ThreadContext *dest)
2176329Sgblack@eecs.umich.edu{
2188466Snilay@cs.wisc.edu    // This function assumes no side effects other than TLB invalidation
2198466Snilay@cs.wisc.edu    // need to be considered while copying state. That will likely not be
2208466Snilay@cs.wisc.edu    // true in the future.
2216329Sgblack@eecs.umich.edu    for (int i = 0; i < NUM_MISCREGS; ++i) {
2226329Sgblack@eecs.umich.edu        if ( ( i != MISCREG_CR1 &&
2236329Sgblack@eecs.umich.edu             !(i > MISCREG_CR4 && i < MISCREG_CR8) &&
2246329Sgblack@eecs.umich.edu             !(i > MISCREG_CR8 && i <= MISCREG_CR15) ) == false) {
2256329Sgblack@eecs.umich.edu             continue;
2266329Sgblack@eecs.umich.edu        }
2276329Sgblack@eecs.umich.edu        dest->setMiscRegNoEffect(i, src->readMiscRegNoEffect(i));
2286329Sgblack@eecs.umich.edu    }
2298466Snilay@cs.wisc.edu
2309751Sandreas@sandberg.pp.se    // The TSC has to be updated with side-effects if the CPUs in a
2319751Sandreas@sandberg.pp.se    // CPU switch have different frequencies.
2329751Sandreas@sandberg.pp.se    dest->setMiscReg(MISCREG_TSC, src->readMiscReg(MISCREG_TSC));
2339751Sandreas@sandberg.pp.se
2349423SAndreas.Sandberg@arm.com    dest->getITBPtr()->flushAll();
2359423SAndreas.Sandberg@arm.com    dest->getDTBPtr()->flushAll();
2366329Sgblack@eecs.umich.edu}
2376329Sgblack@eecs.umich.edu
2386329Sgblack@eecs.umich.eduvoid
2396329Sgblack@eecs.umich.educopyRegs(ThreadContext *src, ThreadContext *dest)
2406329Sgblack@eecs.umich.edu{
2416329Sgblack@eecs.umich.edu    //copy int regs
2428466Snilay@cs.wisc.edu    for (int i = 0; i < NumIntRegs; ++i)
2438466Snilay@cs.wisc.edu         dest->setIntReg(i, src->readIntReg(i));
2446329Sgblack@eecs.umich.edu    //copy float regs
2458466Snilay@cs.wisc.edu    for (int i = 0; i < NumFloatRegs; ++i)
2468466Snilay@cs.wisc.edu         dest->setFloatRegBits(i, src->readFloatRegBits(i));
2479921Syasuko.eckert@amd.com    //copy condition-code regs
2489921Syasuko.eckert@amd.com    for (int i = 0; i < NumCCRegs; ++i)
2499921Syasuko.eckert@amd.com         dest->setCCReg(i, src->readCCReg(i));
2506329Sgblack@eecs.umich.edu    copyMiscRegs(src, dest);
2517720Sgblack@eecs.umich.edu    dest->pcState(src->pcState());
2526329Sgblack@eecs.umich.edu}
2536329Sgblack@eecs.umich.edu
2547693SAli.Saidi@ARM.comvoid
2557693SAli.Saidi@ARM.comskipFunction(ThreadContext *tc)
2567693SAli.Saidi@ARM.com{
2577693SAli.Saidi@ARM.com    panic("Not implemented for x86\n");
2587693SAli.Saidi@ARM.com}
2597693SAli.Saidi@ARM.com
2609759Sandreas@sandberg.pp.seuint64_t
2619759Sandreas@sandberg.pp.segetRFlags(ThreadContext *tc)
2629759Sandreas@sandberg.pp.se{
2639759Sandreas@sandberg.pp.se    const uint64_t ncc_flags(tc->readMiscRegNoEffect(MISCREG_RFLAGS));
26410057Snikos.nikoleris@gmail.com    const uint64_t cc_flags(tc->readCCReg(X86ISA::CCREG_ZAPS));
26510057Snikos.nikoleris@gmail.com    const uint64_t cfof_bits(tc->readCCReg(X86ISA::CCREG_CFOF));
26610057Snikos.nikoleris@gmail.com    const uint64_t df_bit(tc->readCCReg(X86ISA::CCREG_DF));
2679759Sandreas@sandberg.pp.se    // ecf (PSEUDO(3)) & ezf (PSEUDO(4)) are only visible to
2689759Sandreas@sandberg.pp.se    // microcode, so we can safely ignore them.
2699759Sandreas@sandberg.pp.se
2709759Sandreas@sandberg.pp.se    // Reconstruct the real rflags state, mask out internal flags, and
2719759Sandreas@sandberg.pp.se    // make sure reserved bits have the expected values.
2729759Sandreas@sandberg.pp.se    return ((ncc_flags | cc_flags | cfof_bits | df_bit) & 0x3F7FD5)
2739759Sandreas@sandberg.pp.se        | 0x2;
2749759Sandreas@sandberg.pp.se}
2759759Sandreas@sandberg.pp.se
2769759Sandreas@sandberg.pp.sevoid
2779759Sandreas@sandberg.pp.sesetRFlags(ThreadContext *tc, uint64_t val)
2789759Sandreas@sandberg.pp.se{
27910057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_ZAPS, val & ccFlagMask);
28010057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_CFOF, val & cfofMask);
28110057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_DF, val & DFBit);
2829759Sandreas@sandberg.pp.se
2839759Sandreas@sandberg.pp.se    // Internal microcode registers (ECF & EZF)
28410057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_ECF, 0);
28510057Snikos.nikoleris@gmail.com    tc->setCCReg(X86ISA::CCREG_EZF, 0);
2869759Sandreas@sandberg.pp.se
2879759Sandreas@sandberg.pp.se    // Update the RFLAGS misc reg with whatever didn't go into the
2889759Sandreas@sandberg.pp.se    // magic registers.
2899759Sandreas@sandberg.pp.se    tc->setMiscReg(MISCREG_RFLAGS, val & ~(ccFlagMask | cfofMask | DFBit));
2909759Sandreas@sandberg.pp.se}
2917693SAli.Saidi@ARM.com
2929880Sandreas@sandberg.pp.seuint8_t
2939880Sandreas@sandberg.pp.seconvX87TagsToXTags(uint16_t ftw)
2949880Sandreas@sandberg.pp.se{
2959880Sandreas@sandberg.pp.se    uint8_t ftwx(0);
2969880Sandreas@sandberg.pp.se    for (int i = 0; i < 8; ++i) {
2979880Sandreas@sandberg.pp.se        // Extract the tag for the current element on the FP stack
2989880Sandreas@sandberg.pp.se        const unsigned tag((ftw >> (2 * i)) & 0x3);
2999880Sandreas@sandberg.pp.se
3009880Sandreas@sandberg.pp.se        /*
3019880Sandreas@sandberg.pp.se         * Check the type of the current FP element. Valid values are:
3029880Sandreas@sandberg.pp.se         * 0 == Valid
3039880Sandreas@sandberg.pp.se         * 1 == Zero
3049880Sandreas@sandberg.pp.se         * 2 == Special (Nan, unsupported, infinity, denormal)
3059880Sandreas@sandberg.pp.se         * 3 == Empty
3069880Sandreas@sandberg.pp.se         */
3079880Sandreas@sandberg.pp.se        // The xsave version of the tag word only keeps track of
3089880Sandreas@sandberg.pp.se        // whether the element is empty or not. Set the corresponding
3099880Sandreas@sandberg.pp.se        // bit in the ftwx if it's not empty,
3109880Sandreas@sandberg.pp.se        if (tag != 0x3)
3119880Sandreas@sandberg.pp.se            ftwx |= 1 << i;
3129880Sandreas@sandberg.pp.se    }
3139880Sandreas@sandberg.pp.se
3149880Sandreas@sandberg.pp.se    return ftwx;
3159880Sandreas@sandberg.pp.se}
3169880Sandreas@sandberg.pp.se
3179880Sandreas@sandberg.pp.seuint16_t
3189880Sandreas@sandberg.pp.seconvX87XTagsToTags(uint8_t ftwx)
3199880Sandreas@sandberg.pp.se{
3209880Sandreas@sandberg.pp.se    uint16_t ftw(0);
3219880Sandreas@sandberg.pp.se    for (int i = 0; i < 8; ++i) {
3229880Sandreas@sandberg.pp.se        const unsigned xtag(((ftwx >> i) & 0x1));
3239880Sandreas@sandberg.pp.se
3249880Sandreas@sandberg.pp.se        // The xtag for an x87 stack position is 0 for empty stack positions.
3259880Sandreas@sandberg.pp.se        if (!xtag) {
3269880Sandreas@sandberg.pp.se            // Set the tag word to 3 (empty) for the current element.
3279880Sandreas@sandberg.pp.se            ftw |= 0x3 << (2 * i);
3289880Sandreas@sandberg.pp.se        } else {
3299880Sandreas@sandberg.pp.se            // TODO: We currently assume that non-empty elements are
3309880Sandreas@sandberg.pp.se            // valid (0x0), but we should ideally reconstruct the full
3319880Sandreas@sandberg.pp.se            // state (valid/zero/special).
3329880Sandreas@sandberg.pp.se        }
3339880Sandreas@sandberg.pp.se    }
3349880Sandreas@sandberg.pp.se
3359880Sandreas@sandberg.pp.se    return ftw;
3369880Sandreas@sandberg.pp.se}
3379880Sandreas@sandberg.pp.se
3389765Sandreas@sandberg.pp.seuint16_t
3399765Sandreas@sandberg.pp.segenX87Tags(uint16_t ftw, uint8_t top, int8_t spm)
3409765Sandreas@sandberg.pp.se{
3419765Sandreas@sandberg.pp.se    const uint8_t new_top((top + spm + 8) % 8);
3429765Sandreas@sandberg.pp.se
3439765Sandreas@sandberg.pp.se    if (spm > 0) {
3449765Sandreas@sandberg.pp.se        // Removing elements from the stack. Flag the elements as empty.
3459765Sandreas@sandberg.pp.se        for (int i = top; i != new_top; i = (i + 1 + 8) % 8)
3469765Sandreas@sandberg.pp.se            ftw |= 0x3 << (2 * i);
3479765Sandreas@sandberg.pp.se    } else if (spm < 0) {
3489765Sandreas@sandberg.pp.se        // Adding elements to the stack. Flag the new elements as
3499765Sandreas@sandberg.pp.se        // valid. We should ideally decode them and "do the right
3509765Sandreas@sandberg.pp.se        // thing".
3519765Sandreas@sandberg.pp.se        for (int i = new_top; i != top; i = (i + 1 + 8) % 8)
3529765Sandreas@sandberg.pp.se            ftw &= ~(0x3 << (2 * i));
3539765Sandreas@sandberg.pp.se    }
3549765Sandreas@sandberg.pp.se
3559765Sandreas@sandberg.pp.se    return ftw;
3569765Sandreas@sandberg.pp.se}
3579765Sandreas@sandberg.pp.se
3589889Sandreas@sandberg.pp.sedouble
3599889Sandreas@sandberg.pp.seloadFloat80(const void *_mem)
3609889Sandreas@sandberg.pp.se{
3619889Sandreas@sandberg.pp.se    const fp80_t *fp80((const fp80_t *)_mem);
3629889Sandreas@sandberg.pp.se
3639889Sandreas@sandberg.pp.se    return fp80_cvtd(*fp80);
3649889Sandreas@sandberg.pp.se}
3659889Sandreas@sandberg.pp.se
3669889Sandreas@sandberg.pp.sevoid
3679889Sandreas@sandberg.pp.sestoreFloat80(void *_mem, double value)
3689889Sandreas@sandberg.pp.se{
3699889Sandreas@sandberg.pp.se    fp80_t *fp80((fp80_t *)_mem);
3709889Sandreas@sandberg.pp.se
3719889Sandreas@sandberg.pp.se    *fp80 = fp80_cvfd(value);
3729889Sandreas@sandberg.pp.se}
3739889Sandreas@sandberg.pp.se
3747811Ssteve.reinhardt@amd.com} // namespace X86_ISA
375