faults.cc revision 7681
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    {
615909Sgblack@eecs.umich.edu        Addr pc = tc->readPC();
625909Sgblack@eecs.umich.edu        DPRINTF(Faults, "RIP %#x: vector %d: %s\n", pc, vector, describe());
635681Sgblack@eecs.umich.edu        using namespace X86ISAInst::RomLabels;
645681Sgblack@eecs.umich.edu        HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
655681Sgblack@eecs.umich.edu        MicroPC entry;
665681Sgblack@eecs.umich.edu        if (m5reg.mode == LongMode) {
675858Sgblack@eecs.umich.edu            if (isSoft()) {
685858Sgblack@eecs.umich.edu                entry = extern_label_longModeSoftInterrupt;
695858Sgblack@eecs.umich.edu            } else {
705858Sgblack@eecs.umich.edu                entry = extern_label_longModeInterrupt;
715858Sgblack@eecs.umich.edu            }
725681Sgblack@eecs.umich.edu        } else {
735681Sgblack@eecs.umich.edu            entry = extern_label_legacyModeInterrupt;
745681Sgblack@eecs.umich.edu        }
755681Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_MICRO(1), vector);
765909Sgblack@eecs.umich.edu        tc->setIntReg(INTREG_MICRO(7), pc);
775857Sgblack@eecs.umich.edu        if (errorCode != (uint64_t)(-1)) {
785858Sgblack@eecs.umich.edu            if (m5reg.mode == LongMode) {
795858Sgblack@eecs.umich.edu                entry = extern_label_longModeInterruptWithError;
805858Sgblack@eecs.umich.edu            } else {
815858Sgblack@eecs.umich.edu                panic("Legacy mode interrupts with error codes "
825858Sgblack@eecs.umich.edu                        "aren't implementde.\n");
835858Sgblack@eecs.umich.edu            }
845858Sgblack@eecs.umich.edu            // Software interrupts shouldn't have error codes. If one does,
855858Sgblack@eecs.umich.edu            // there would need to be microcode to set it up.
865858Sgblack@eecs.umich.edu            assert(!isSoft());
875857Sgblack@eecs.umich.edu            tc->setIntReg(INTREG_MICRO(15), errorCode);
885857Sgblack@eecs.umich.edu        }
895681Sgblack@eecs.umich.edu        tc->setMicroPC(romMicroPC(entry));
905681Sgblack@eecs.umich.edu        tc->setNextMicroPC(romMicroPC(entry) + 1);
915124Sgblack@eecs.umich.edu    }
925909Sgblack@eecs.umich.edu
935909Sgblack@eecs.umich.edu    std::string
945909Sgblack@eecs.umich.edu    X86FaultBase::describe() const
955909Sgblack@eecs.umich.edu    {
965909Sgblack@eecs.umich.edu        std::stringstream ss;
975909Sgblack@eecs.umich.edu        ccprintf(ss, "%s", mnemonic());
985909Sgblack@eecs.umich.edu        if (errorCode != (uint64_t)(-1)) {
995909Sgblack@eecs.umich.edu            ccprintf(ss, "(%#x)", errorCode);
1005909Sgblack@eecs.umich.edu        }
1015909Sgblack@eecs.umich.edu
1025909Sgblack@eecs.umich.edu        return ss.str();
1035909Sgblack@eecs.umich.edu    }
1045858Sgblack@eecs.umich.edu
1057678Sgblack@eecs.umich.edu    void X86Trap::invoke(ThreadContext * tc, StaticInstPtr inst)
1065858Sgblack@eecs.umich.edu    {
1075858Sgblack@eecs.umich.edu        X86FaultBase::invoke(tc);
1085858Sgblack@eecs.umich.edu        // This is the same as a fault, but it happens -after- the instruction.
1095858Sgblack@eecs.umich.edu        tc->setPC(tc->readNextPC());
1105858Sgblack@eecs.umich.edu        tc->setNextPC(tc->readNextNPC());
1115858Sgblack@eecs.umich.edu        tc->setNextNPC(tc->readNextNPC() + sizeof(MachInst));
1125858Sgblack@eecs.umich.edu    }
1135858Sgblack@eecs.umich.edu
1147678Sgblack@eecs.umich.edu    void X86Abort::invoke(ThreadContext * tc, StaticInstPtr inst)
1155858Sgblack@eecs.umich.edu    {
1165858Sgblack@eecs.umich.edu        panic("Abort exception!");
1175858Sgblack@eecs.umich.edu    }
1185858Sgblack@eecs.umich.edu
1197678Sgblack@eecs.umich.edu    void PageFault::invoke(ThreadContext * tc, StaticInstPtr inst)
1205858Sgblack@eecs.umich.edu    {
1215858Sgblack@eecs.umich.edu        HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
1225858Sgblack@eecs.umich.edu        X86FaultBase::invoke(tc);
1235858Sgblack@eecs.umich.edu        /*
1245858Sgblack@eecs.umich.edu         * If something bad happens while trying to enter the page fault
1255858Sgblack@eecs.umich.edu         * handler, I'm pretty sure that's a double fault and then all bets are
1265858Sgblack@eecs.umich.edu         * off. That means it should be safe to update this state now.
1275858Sgblack@eecs.umich.edu         */
1285858Sgblack@eecs.umich.edu        if (m5reg.mode == LongMode) {
1295858Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CR2, addr);
1305858Sgblack@eecs.umich.edu        } else {
1315858Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CR2, (uint32_t)addr);
1325858Sgblack@eecs.umich.edu        }
1335858Sgblack@eecs.umich.edu    }
1345237Sgblack@eecs.umich.edu
1355909Sgblack@eecs.umich.edu    std::string
1365909Sgblack@eecs.umich.edu    PageFault::describe() const
1375909Sgblack@eecs.umich.edu    {
1385909Sgblack@eecs.umich.edu        std::stringstream ss;
1395909Sgblack@eecs.umich.edu        ccprintf(ss, "%s at %#x", X86FaultBase::describe(), addr);
1405909Sgblack@eecs.umich.edu        return ss.str();
1415909Sgblack@eecs.umich.edu    }
1425909Sgblack@eecs.umich.edu
1436048Sgblack@eecs.umich.edu    void
1447678Sgblack@eecs.umich.edu    InitInterrupt::invoke(ThreadContext *tc, StaticInstPtr inst)
1456048Sgblack@eecs.umich.edu    {
1466048Sgblack@eecs.umich.edu        DPRINTF(Faults, "Init interrupt.\n");
1476048Sgblack@eecs.umich.edu        // The otherwise unmodified integer registers should be set to 0.
1486048Sgblack@eecs.umich.edu        for (int index = 0; index < NUM_INTREGS; index++) {
1496048Sgblack@eecs.umich.edu            tc->setIntReg(index, 0);
1506048Sgblack@eecs.umich.edu        }
1516048Sgblack@eecs.umich.edu
1526048Sgblack@eecs.umich.edu        CR0 cr0 = tc->readMiscReg(MISCREG_CR0);
1536048Sgblack@eecs.umich.edu        CR0 newCR0 = 1 << 4;
1546048Sgblack@eecs.umich.edu        newCR0.cd = cr0.cd;
1556048Sgblack@eecs.umich.edu        newCR0.nw = cr0.nw;
1566048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR0, newCR0);
1576048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR2, 0);
1586048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR3, 0);
1596048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CR4, 0);
1606048Sgblack@eecs.umich.edu
1616048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_RFLAGS, 0x0000000000000002ULL);
1626048Sgblack@eecs.umich.edu
1636048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_EFER, 0);
1646048Sgblack@eecs.umich.edu
1656048Sgblack@eecs.umich.edu        SegAttr dataAttr = 0;
1666222Sgblack@eecs.umich.edu        dataAttr.dpl = 0;
1676222Sgblack@eecs.umich.edu        dataAttr.unusable = 0;
1686222Sgblack@eecs.umich.edu        dataAttr.defaultSize = 0;
1696222Sgblack@eecs.umich.edu        dataAttr.longMode = 0;
1706222Sgblack@eecs.umich.edu        dataAttr.avl = 0;
1716222Sgblack@eecs.umich.edu        dataAttr.granularity = 0;
1726222Sgblack@eecs.umich.edu        dataAttr.present = 1;
1736222Sgblack@eecs.umich.edu        dataAttr.type = 3;
1746048Sgblack@eecs.umich.edu        dataAttr.writable = 1;
1756048Sgblack@eecs.umich.edu        dataAttr.readable = 1;
1766048Sgblack@eecs.umich.edu        dataAttr.expandDown = 0;
1776222Sgblack@eecs.umich.edu        dataAttr.system = 1;
1786048Sgblack@eecs.umich.edu
1796048Sgblack@eecs.umich.edu        for (int seg = 0; seg != NUM_SEGMENTREGS; seg++) {
1806048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_SEL(seg), 0);
1816048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_BASE(seg), 0);
1826048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_EFF_BASE(seg), 0);
1836048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_LIMIT(seg), 0xffff);
1846048Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_SEG_ATTR(seg), dataAttr);
1856048Sgblack@eecs.umich.edu        }
1866048Sgblack@eecs.umich.edu
1876048Sgblack@eecs.umich.edu        SegAttr codeAttr = 0;
1886222Sgblack@eecs.umich.edu        codeAttr.dpl = 0;
1896222Sgblack@eecs.umich.edu        codeAttr.unusable = 0;
1906222Sgblack@eecs.umich.edu        codeAttr.defaultSize = 0;
1916222Sgblack@eecs.umich.edu        codeAttr.longMode = 0;
1926222Sgblack@eecs.umich.edu        codeAttr.avl = 0;
1936222Sgblack@eecs.umich.edu        codeAttr.granularity = 0;
1946222Sgblack@eecs.umich.edu        codeAttr.present = 1;
1956222Sgblack@eecs.umich.edu        codeAttr.type = 10;
1966048Sgblack@eecs.umich.edu        codeAttr.writable = 0;
1976048Sgblack@eecs.umich.edu        codeAttr.readable = 1;
1986048Sgblack@eecs.umich.edu        codeAttr.expandDown = 0;
1996222Sgblack@eecs.umich.edu        codeAttr.system = 1;
2006048Sgblack@eecs.umich.edu
2016048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS, 0xf000);
2026048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_BASE,
2036048Sgblack@eecs.umich.edu                0x00000000ffff0000ULL);
2046048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_EFF_BASE,
2056048Sgblack@eecs.umich.edu                0x00000000ffff0000ULL);
2066048Sgblack@eecs.umich.edu        // This has the base value pre-added.
2076048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_LIMIT, 0xffffffff);
2086048Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_ATTR, codeAttr);
2096048Sgblack@eecs.umich.edu
2106048Sgblack@eecs.umich.edu        tc->setPC(0x000000000000fff0ULL +
2116048Sgblack@eecs.umich.edu                tc->readMiscReg(MISCREG_CS_BASE));
2126048Sgblack@eecs.umich.edu        tc->setNextPC(tc->readPC() + sizeof(MachInst));
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;
2466048Sgblack@eecs.umich.edu        tc->setMicroPC(romMicroPC(entry));
2476048Sgblack@eecs.umich.edu        tc->setNextMicroPC(romMicroPC(entry) + 1);
2486048Sgblack@eecs.umich.edu    }
2496048Sgblack@eecs.umich.edu
2506049Sgblack@eecs.umich.edu    void
2517678Sgblack@eecs.umich.edu    StartupInterrupt::invoke(ThreadContext *tc, StaticInstPtr inst)
2526049Sgblack@eecs.umich.edu    {
2536049Sgblack@eecs.umich.edu        DPRINTF(Faults, "Startup interrupt with vector %#x.\n", vector);
2546049Sgblack@eecs.umich.edu        HandyM5Reg m5Reg = tc->readMiscReg(MISCREG_M5_REG);
2556049Sgblack@eecs.umich.edu        if (m5Reg.mode != LegacyMode || m5Reg.submode != RealMode) {
2566049Sgblack@eecs.umich.edu            panic("Startup IPI recived outside of real mode. "
2576140Sgblack@eecs.umich.edu                    "Don't know what to do. %d, %d", m5Reg.mode, m5Reg.submode);
2586049Sgblack@eecs.umich.edu        }
2596049Sgblack@eecs.umich.edu
2606049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS, vector << 8);
2616049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_BASE, vector << 12);
2626049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_EFF_BASE, vector << 12);
2636049Sgblack@eecs.umich.edu        // This has the base value pre-added.
2646049Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CS_LIMIT, 0xffff);
2656049Sgblack@eecs.umich.edu
2666049Sgblack@eecs.umich.edu        tc->setPC(tc->readMiscReg(MISCREG_CS_BASE));
2676049Sgblack@eecs.umich.edu        tc->setNextPC(tc->readPC() + sizeof(MachInst));
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