faults.cc revision 7720
15124Sgblack@eecs.umich.edu/*
27087Snate@binkert.org * Copyright (c) 2007 The Hewlett-Packard Development Company
37087Snate@binkert.org * All rights reserved.
47087Snate@binkert.org *
57087Snate@binkert.org * The license below extends only to copyright in the software and shall
67087Snate@binkert.org * not be construed as granting a license to any other intellectual
77087Snate@binkert.org * property including but not limited to intellectual property relating
87087Snate@binkert.org * to a hardware implementation of the functionality of the software
97087Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
107087Snate@binkert.org * terms below provided that you ensure that this notice is replicated
117087Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
127087Snate@binkert.org * modified or unmodified, in source code or in binary form.
137087Snate@binkert.org *
145124Sgblack@eecs.umich.edu * Copyright (c) 2003-2007 The Regents of The University of Michigan
155124Sgblack@eecs.umich.edu * All rights reserved.
165124Sgblack@eecs.umich.edu *
175124Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
185124Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
195124Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
205124Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
215124Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
225124Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
235124Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
245124Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
255124Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
265124Sgblack@eecs.umich.edu * this software without specific prior written permission.
275124Sgblack@eecs.umich.edu *
285124Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
295124Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
305124Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
315124Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
325124Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
335124Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
345124Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
355124Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
365124Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
375124Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
385124Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
395124Sgblack@eecs.umich.edu *
405124Sgblack@eecs.umich.edu * Authors: Gabe Black
415124Sgblack@eecs.umich.edu */
425124Sgblack@eecs.umich.edu
435681Sgblack@eecs.umich.edu#include "arch/x86/decoder.hh"
445124Sgblack@eecs.umich.edu#include "arch/x86/faults.hh"
455124Sgblack@eecs.umich.edu#include "base/trace.hh"
465124Sgblack@eecs.umich.edu#include "config/full_system.hh"
475124Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
485124Sgblack@eecs.umich.edu#if !FULL_SYSTEM
495124Sgblack@eecs.umich.edu#include "arch/x86/isa_traits.hh"
505124Sgblack@eecs.umich.edu#include "mem/page_table.hh"
515124Sgblack@eecs.umich.edu#include "sim/process.hh"
525237Sgblack@eecs.umich.edu#else
535237Sgblack@eecs.umich.edu#include "arch/x86/tlb.hh"
545124Sgblack@eecs.umich.edu#endif
555124Sgblack@eecs.umich.edu
565124Sgblack@eecs.umich.edunamespace X86ISA
575124Sgblack@eecs.umich.edu{
585124Sgblack@eecs.umich.edu#if FULL_SYSTEM
597678Sgblack@eecs.umich.edu    void X86FaultBase::invoke(ThreadContext * tc, StaticInstPtr inst)
605124Sgblack@eecs.umich.edu    {
617720Sgblack@eecs.umich.edu        PCState pcState = tc->pcState();
627720Sgblack@eecs.umich.edu        Addr pc = pcState.pc();
635909Sgblack@eecs.umich.edu        DPRINTF(Faults, "RIP %#x: vector %d: %s\n", pc, vector, describe());
645681Sgblack@eecs.umich.edu        using namespace X86ISAInst::RomLabels;
655681Sgblack@eecs.umich.edu        HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
665681Sgblack@eecs.umich.edu        MicroPC entry;
675681Sgblack@eecs.umich.edu        if (m5reg.mode == LongMode) {
685858Sgblack@eecs.umich.edu            if (isSoft()) {
695858Sgblack@eecs.umich.edu                entry = extern_label_longModeSoftInterrupt;
705858Sgblack@eecs.umich.edu            } else {
715858Sgblack@eecs.umich.edu                entry = extern_label_longModeInterrupt;
725858Sgblack@eecs.umich.edu            }
735681Sgblack@eecs.umich.edu        } else {
745681Sgblack@eecs.umich.edu            entry = extern_label_legacyModeInterrupt;
755681Sgblack@eecs.umich.edu        }
765681Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_MICRO(1), vector);
775909Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_MICRO(7), pc);
785857Sgblack@eecs.umich.edu        if (errorCode != (uint64_t)(-1)) {
795858Sgblack@eecs.umich.edu            if (m5reg.mode == LongMode) {
805858Sgblack@eecs.umich.edu                entry = extern_label_longModeInterruptWithError;
815858Sgblack@eecs.umich.edu            } else {
825858Sgblack@eecs.umich.edu                panic("Legacy mode interrupts with error codes "
835858Sgblack@eecs.umich.edu                        "aren't implementde.\n");
845858Sgblack@eecs.umich.edu            }
855858Sgblack@eecs.umich.edu            // Software interrupts shouldn't have error codes. If one does,
865858Sgblack@eecs.umich.edu            // there would need to be microcode to set it up.
875858Sgblack@eecs.umich.edu            assert(!isSoft());
885857Sgblack@eecs.umich.edu            tc->setIntReg(INTREG_MICRO(15), errorCode);
895857Sgblack@eecs.umich.edu        }
907720Sgblack@eecs.umich.edu        pcState.upc(romMicroPC(entry));
917720Sgblack@eecs.umich.edu        pcState.nupc(romMicroPC(entry) + 1);
927720Sgblack@eecs.umich.edu        tc->pcState(pcState);
935124Sgblack@eecs.umich.edu    }
945909Sgblack@eecs.umich.edu
955909Sgblack@eecs.umich.edu    std::string
965909Sgblack@eecs.umich.edu    X86FaultBase::describe() const
975909Sgblack@eecs.umich.edu    {
985909Sgblack@eecs.umich.edu        std::stringstream ss;
995909Sgblack@eecs.umich.edu        ccprintf(ss, "%s", mnemonic());
1005909Sgblack@eecs.umich.edu        if (errorCode != (uint64_t)(-1)) {
1015909Sgblack@eecs.umich.edu            ccprintf(ss, "(%#x)", errorCode);
1025909Sgblack@eecs.umich.edu        }
1035909Sgblack@eecs.umich.edu
1045909Sgblack@eecs.umich.edu        return ss.str();
1055909Sgblack@eecs.umich.edu    }
1065858Sgblack@eecs.umich.edu
1077678Sgblack@eecs.umich.edu    void X86Trap::invoke(ThreadContext * tc, StaticInstPtr inst)
1085858Sgblack@eecs.umich.edu    {
1095858Sgblack@eecs.umich.edu        X86FaultBase::invoke(tc);
1105858Sgblack@eecs.umich.edu        // This is the same as a fault, but it happens -after- the instruction.
1117720Sgblack@eecs.umich.edu        PCState pc = tc->pcState();
1127720Sgblack@eecs.umich.edu        pc.uEnd();
1135858Sgblack@eecs.umich.edu    }
1145858Sgblack@eecs.umich.edu
1157678Sgblack@eecs.umich.edu    void X86Abort::invoke(ThreadContext * tc, StaticInstPtr inst)
1165858Sgblack@eecs.umich.edu    {
1175858Sgblack@eecs.umich.edu        panic("Abort exception!");
1185858Sgblack@eecs.umich.edu    }
1195858Sgblack@eecs.umich.edu
1207678Sgblack@eecs.umich.edu    void PageFault::invoke(ThreadContext * tc, StaticInstPtr inst)
1215858Sgblack@eecs.umich.edu    {
1225858Sgblack@eecs.umich.edu        HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
1235858Sgblack@eecs.umich.edu        X86FaultBase::invoke(tc);
1245858Sgblack@eecs.umich.edu        /*
1255858Sgblack@eecs.umich.edu         * If something bad happens while trying to enter the page fault
1265858Sgblack@eecs.umich.edu         * handler, I'm pretty sure that's a double fault and then all bets are
1275858Sgblack@eecs.umich.edu         * off. That means it should be safe to update this state now.
1285858Sgblack@eecs.umich.edu         */
1295858Sgblack@eecs.umich.edu        if (m5reg.mode == LongMode) {
1305858Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CR2, addr);
1315858Sgblack@eecs.umich.edu        } else {
1325858Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CR2, (uint32_t)addr);
1335858Sgblack@eecs.umich.edu        }
1345858Sgblack@eecs.umich.edu    }
1355237Sgblack@eecs.umich.edu
1365909Sgblack@eecs.umich.edu    std::string
1375909Sgblack@eecs.umich.edu    PageFault::describe() const
1385909Sgblack@eecs.umich.edu    {
1395909Sgblack@eecs.umich.edu        std::stringstream ss;
1405909Sgblack@eecs.umich.edu        ccprintf(ss, "%s at %#x", X86FaultBase::describe(), addr);
1415909Sgblack@eecs.umich.edu        return ss.str();
1425909Sgblack@eecs.umich.edu    }
1435909Sgblack@eecs.umich.edu
1446048Sgblack@eecs.umich.edu    void
1457678Sgblack@eecs.umich.edu    InitInterrupt::invoke(ThreadContext *tc, StaticInstPtr inst)
1466048Sgblack@eecs.umich.edu    {
1476048Sgblack@eecs.umich.edu        DPRINTF(Faults, "Init interrupt.\n");
1486048Sgblack@eecs.umich.edu        // The otherwise unmodified integer registers should be set to 0.
1496048Sgblack@eecs.umich.edu        for (int index = 0; index < NUM_INTREGS; index++) {
1506048Sgblack@eecs.umich.edu            tc->setIntReg(index, 0);
1516048Sgblack@eecs.umich.edu        }
1526048Sgblack@eecs.umich.edu
1536048Sgblack@eecs.umich.edu        CR0 cr0 = tc->readMiscReg(MISCREG_CR0);
1546048Sgblack@eecs.umich.edu        CR0 newCR0 = 1 << 4;
1556048Sgblack@eecs.umich.edu        newCR0.cd = cr0.cd;
1566048Sgblack@eecs.umich.edu        newCR0.nw = cr0.nw;
1576048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR0, newCR0);
1586048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR2, 0);
1596048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR3, 0);
1606048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR4, 0);
1616048Sgblack@eecs.umich.edu
1626048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_RFLAGS, 0x0000000000000002ULL);
1636048Sgblack@eecs.umich.edu
1646048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_EFER, 0);
1656048Sgblack@eecs.umich.edu
1666048Sgblack@eecs.umich.edu        SegAttr dataAttr = 0;
1676222Sgblack@eecs.umich.edu        dataAttr.dpl = 0;
1686222Sgblack@eecs.umich.edu        dataAttr.unusable = 0;
1696222Sgblack@eecs.umich.edu        dataAttr.defaultSize = 0;
1706222Sgblack@eecs.umich.edu        dataAttr.longMode = 0;
1716222Sgblack@eecs.umich.edu        dataAttr.avl = 0;
1726222Sgblack@eecs.umich.edu        dataAttr.granularity = 0;
1736222Sgblack@eecs.umich.edu        dataAttr.present = 1;
1746222Sgblack@eecs.umich.edu        dataAttr.type = 3;
1756048Sgblack@eecs.umich.edu        dataAttr.writable = 1;
1766048Sgblack@eecs.umich.edu        dataAttr.readable = 1;
1776048Sgblack@eecs.umich.edu        dataAttr.expandDown = 0;
1786222Sgblack@eecs.umich.edu        dataAttr.system = 1;
1796048Sgblack@eecs.umich.edu
1806048Sgblack@eecs.umich.edu        for (int seg = 0; seg != NUM_SEGMENTREGS; seg++) {
1816048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_SEL(seg), 0);
1826048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_BASE(seg), 0);
1836048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_EFF_BASE(seg), 0);
1846048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_LIMIT(seg), 0xffff);
1856048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_ATTR(seg), dataAttr);
1866048Sgblack@eecs.umich.edu        }
1876048Sgblack@eecs.umich.edu
1886048Sgblack@eecs.umich.edu        SegAttr codeAttr = 0;
1896222Sgblack@eecs.umich.edu        codeAttr.dpl = 0;
1906222Sgblack@eecs.umich.edu        codeAttr.unusable = 0;
1916222Sgblack@eecs.umich.edu        codeAttr.defaultSize = 0;
1926222Sgblack@eecs.umich.edu        codeAttr.longMode = 0;
1936222Sgblack@eecs.umich.edu        codeAttr.avl = 0;
1946222Sgblack@eecs.umich.edu        codeAttr.granularity = 0;
1956222Sgblack@eecs.umich.edu        codeAttr.present = 1;
1966222Sgblack@eecs.umich.edu        codeAttr.type = 10;
1976048Sgblack@eecs.umich.edu        codeAttr.writable = 0;
1986048Sgblack@eecs.umich.edu        codeAttr.readable = 1;
1996048Sgblack@eecs.umich.edu        codeAttr.expandDown = 0;
2006222Sgblack@eecs.umich.edu        codeAttr.system = 1;
2016048Sgblack@eecs.umich.edu
2026048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS, 0xf000);
2036048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_BASE,
2046048Sgblack@eecs.umich.edu                0x00000000ffff0000ULL);
2056048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_EFF_BASE,
2066048Sgblack@eecs.umich.edu                0x00000000ffff0000ULL);
2076048Sgblack@eecs.umich.edu        // This has the base value pre-added.
2086048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_LIMIT, 0xffffffff);
2096048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_ATTR, codeAttr);
2106048Sgblack@eecs.umich.edu
2117720Sgblack@eecs.umich.edu        PCState pc(0x000000000000fff0ULL + tc->readMiscReg(MISCREG_CS_BASE));
2127720Sgblack@eecs.umich.edu        tc->pcState(pc);
2136048Sgblack@eecs.umich.edu
2146048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TSG_BASE, 0);
2156048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TSG_LIMIT, 0xffff);
2166048Sgblack@eecs.umich.edu
2176048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_IDTR_BASE, 0);
2186048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_IDTR_LIMIT, 0xffff);
2196048Sgblack@eecs.umich.edu
2206048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TSL, 0);
2216048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TSL_BASE, 0);
2226048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TSL_LIMIT, 0xffff);
2236048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TSL_ATTR, 0);
2246048Sgblack@eecs.umich.edu
2256048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TR, 0);
2266048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TR_BASE, 0);
2276048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TR_LIMIT, 0xffff);
2286048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_TR_ATTR, 0);
2296048Sgblack@eecs.umich.edu
2306048Sgblack@eecs.umich.edu        // This value should be the family/model/stepping of the processor.
2316048Sgblack@eecs.umich.edu        // (page 418). It should be consistent with the value from CPUID, but
2326048Sgblack@eecs.umich.edu        // the actual value probably doesn't matter much.
2336048Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_RDX, 0);
2346048Sgblack@eecs.umich.edu
2356048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_DR0, 0);
2366048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_DR1, 0);
2376048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_DR2, 0);
2386048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_DR3, 0);
2396048Sgblack@eecs.umich.edu
2406048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_DR6, 0x00000000ffff0ff0ULL);
2416048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_DR7, 0x0000000000000400ULL);
2426048Sgblack@eecs.umich.edu
2436140Sgblack@eecs.umich.edu        // Update the handy M5 Reg.
2446140Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_M5_REG, 0);
2456048Sgblack@eecs.umich.edu        MicroPC entry = X86ISAInst::RomLabels::extern_label_initIntHalt;
2467720Sgblack@eecs.umich.edu        pc.upc(romMicroPC(entry));
2477720Sgblack@eecs.umich.edu        pc.nupc(romMicroPC(entry) + 1);
2487720Sgblack@eecs.umich.edu        tc->pcState(pc);
2496048Sgblack@eecs.umich.edu    }
2506048Sgblack@eecs.umich.edu
2516049Sgblack@eecs.umich.edu    void
2527678Sgblack@eecs.umich.edu    StartupInterrupt::invoke(ThreadContext *tc, StaticInstPtr inst)
2536049Sgblack@eecs.umich.edu    {
2546049Sgblack@eecs.umich.edu        DPRINTF(Faults, "Startup interrupt with vector %#x.\n", vector);
2556049Sgblack@eecs.umich.edu        HandyM5Reg m5Reg = tc->readMiscReg(MISCREG_M5_REG);
2566049Sgblack@eecs.umich.edu        if (m5Reg.mode != LegacyMode || m5Reg.submode != RealMode) {
2576049Sgblack@eecs.umich.edu            panic("Startup IPI recived outside of real mode. "
2586140Sgblack@eecs.umich.edu                    "Don't know what to do. %d, %d", m5Reg.mode, m5Reg.submode);
2596049Sgblack@eecs.umich.edu        }
2606049Sgblack@eecs.umich.edu
2616049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS, vector << 8);
2626049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_BASE, vector << 12);
2636049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_EFF_BASE, vector << 12);
2646049Sgblack@eecs.umich.edu        // This has the base value pre-added.
2656049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_LIMIT, 0xffff);
2666049Sgblack@eecs.umich.edu
2677720Sgblack@eecs.umich.edu        tc->pcState(tc->readMiscReg(MISCREG_CS_BASE));
2686049Sgblack@eecs.umich.edu    }
2696049Sgblack@eecs.umich.edu
2707625Sgblack@eecs.umich.edu#else
2717625Sgblack@eecs.umich.edu
2727625Sgblack@eecs.umich.edu    void
2737681Sgblack@eecs.umich.edu    InvalidOpcode::invoke(ThreadContext * tc, StaticInstPtr inst)
2747681Sgblack@eecs.umich.edu    {
2757681Sgblack@eecs.umich.edu        panic("Unrecognized/invalid instruction executed:\n %s",
2767681Sgblack@eecs.umich.edu                inst->machInst);
2777681Sgblack@eecs.umich.edu    }
2787681Sgblack@eecs.umich.edu
2797681Sgblack@eecs.umich.edu    void
2807678Sgblack@eecs.umich.edu    PageFault::invoke(ThreadContext * tc, StaticInstPtr inst)
2817625Sgblack@eecs.umich.edu    {
2827625Sgblack@eecs.umich.edu        PageFaultErrorCode code = errorCode;
2837625Sgblack@eecs.umich.edu        const char *modeStr = "";
2847625Sgblack@eecs.umich.edu        if (code.fetch)
2857625Sgblack@eecs.umich.edu            modeStr = "execute";
2867625Sgblack@eecs.umich.edu        else if (code.write)
2877625Sgblack@eecs.umich.edu            modeStr = "write";
2887625Sgblack@eecs.umich.edu        else
2897625Sgblack@eecs.umich.edu            modeStr = "read";
2907625Sgblack@eecs.umich.edu        panic("Tried to %s unmapped address %#x.\n", modeStr, addr);
2917625Sgblack@eecs.umich.edu    }
2927625Sgblack@eecs.umich.edu
2935124Sgblack@eecs.umich.edu#endif
2945124Sgblack@eecs.umich.edu} // namespace X86ISA
2955124Sgblack@eecs.umich.edu
296