cpu_impl.hh revision 8229
12315SN/A/*
22332SN/A * Copyright (c) 2006 The Regents of The University of Michigan
32315SN/A * All rights reserved.
42315SN/A *
52315SN/A * Redistribution and use in source and binary forms, with or without
62315SN/A * modification, are permitted provided that the following conditions are
72315SN/A * met: redistributions of source code must retain the above copyright
82315SN/A * notice, this list of conditions and the following disclaimer;
92315SN/A * redistributions in binary form must reproduce the above copyright
102315SN/A * notice, this list of conditions and the following disclaimer in the
112315SN/A * documentation and/or other materials provided with the distribution;
122315SN/A * neither the name of the copyright holders nor the names of its
132315SN/A * contributors may be used to endorse or promote products derived from
142315SN/A * this software without specific prior written permission.
152315SN/A *
162315SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172315SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182315SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192315SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202315SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212315SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222315SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232315SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242315SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252315SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262315SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272689SN/A *
282689SN/A * Authors: Kevin Lim
292315SN/A */
302315SN/A
312315SN/A#include <list>
322315SN/A#include <string>
332315SN/A
342315SN/A#include "base/refcnt.hh"
356658Snate@binkert.org#include "config/the_isa.hh"
368229Snate@binkert.org#include "cpu/checker/cpu.hh"
372315SN/A#include "cpu/base_dyn_inst.hh"
382683SN/A#include "cpu/simple_thread.hh"
398229Snate@binkert.org#include "cpu/static_inst.hh"
402680SN/A#include "cpu/thread_context.hh"
412315SN/A#include "sim/sim_object.hh"
422315SN/A#include "sim/stats.hh"
432315SN/A
442315SN/A#if FULL_SYSTEM
452315SN/A#include "arch/vtophys.hh"
462315SN/A#endif // FULL_SYSTEM
472315SN/A
482315SN/Ausing namespace std;
492315SN/A//The CheckerCPU does alpha only
502315SN/Ausing namespace AlphaISA;
512315SN/A
522315SN/Atemplate <class DynInstPtr>
532315SN/Avoid
542732SN/AChecker<DynInstPtr>::verify(DynInstPtr &completed_inst)
552315SN/A{
562315SN/A    DynInstPtr inst;
572315SN/A
582332SN/A    // Either check this instruction, or add it to a list of
592332SN/A    // instructions waiting to be checked.  Instructions must be
602332SN/A    // checked in program order, so if a store has committed yet not
612332SN/A    // completed, there may be some instructions that are waiting
622332SN/A    // behind it that have completed and must be checked.
632315SN/A    if (!instList.empty()) {
642315SN/A        if (youngestSN < completed_inst->seqNum) {
652315SN/A            DPRINTF(Checker, "Adding instruction [sn:%lli] PC:%#x to list.\n",
662315SN/A                    completed_inst->seqNum, completed_inst->readPC());
672315SN/A            instList.push_back(completed_inst);
682315SN/A            youngestSN = completed_inst->seqNum;
692315SN/A        }
702315SN/A
712315SN/A        if (!instList.front()->isCompleted()) {
722315SN/A            return;
732315SN/A        } else {
742315SN/A            inst = instList.front();
752315SN/A            instList.pop_front();
762315SN/A        }
772315SN/A    } else {
782315SN/A        if (!completed_inst->isCompleted()) {
792315SN/A            if (youngestSN < completed_inst->seqNum) {
802315SN/A                DPRINTF(Checker, "Adding instruction [sn:%lli] PC:%#x to list.\n",
812315SN/A                        completed_inst->seqNum, completed_inst->readPC());
822315SN/A                instList.push_back(completed_inst);
832315SN/A                youngestSN = completed_inst->seqNum;
842315SN/A            }
852315SN/A            return;
862315SN/A        } else {
872315SN/A            if (youngestSN < completed_inst->seqNum) {
882315SN/A                inst = completed_inst;
892315SN/A                youngestSN = completed_inst->seqNum;
902315SN/A            } else {
912315SN/A                return;
922315SN/A            }
932315SN/A        }
942315SN/A    }
952315SN/A
962354SN/A    unverifiedInst = inst;
972354SN/A
982332SN/A    // Try to check all instructions that are completed, ending if we
992332SN/A    // run out of instructions to check or if an instruction is not
1002332SN/A    // yet completed.
1012315SN/A    while (1) {
1022315SN/A        DPRINTF(Checker, "Processing instruction [sn:%lli] PC:%#x.\n",
1032315SN/A                inst->seqNum, inst->readPC());
1042315SN/A        unverifiedResult.integer = inst->readIntResult();
1052315SN/A        unverifiedReq = inst->req;
1062679SN/A        unverifiedMemData = inst->memData;
1072315SN/A        numCycles++;
1082315SN/A
1092315SN/A        Fault fault = NoFault;
1102315SN/A
1112315SN/A        // maintain $r0 semantics
1122683SN/A        thread->setIntReg(ZeroReg, 0);
1132315SN/A#ifdef TARGET_ALPHA
1142683SN/A        thread->setFloatRegDouble(ZeroReg, 0.0);
1152315SN/A#endif // TARGET_ALPHA
1162315SN/A
1172332SN/A        // Check if any recent PC changes match up with anything we
1182332SN/A        // expect to happen.  This is mostly to check if traps or
1192332SN/A        // PC-based events have occurred in both the checker and CPU.
1202315SN/A        if (changedPC) {
1212315SN/A            DPRINTF(Checker, "Changed PC recently to %#x\n",
1222683SN/A                    thread->readPC());
1232315SN/A            if (willChangePC) {
1242683SN/A                if (newPC == thread->readPC()) {
1252315SN/A                    DPRINTF(Checker, "Changed PC matches expected PC\n");
1262315SN/A                } else {
1272332SN/A                    warn("%lli: Changed PC does not match expected PC, "
1282332SN/A                         "changed: %#x, expected: %#x",
1297823Ssteve.reinhardt@amd.com                         curTick(), thread->readPC(), newPC);
1302732SN/A                    CheckerCPU::handleError();
1312315SN/A                }
1322315SN/A                willChangePC = false;
1332315SN/A            }
1342315SN/A            changedPC = false;
1352315SN/A        }
1362315SN/A        if (changedNextPC) {
1372315SN/A            DPRINTF(Checker, "Changed NextPC recently to %#x\n",
1382683SN/A                    thread->readNextPC());
1392315SN/A            changedNextPC = false;
1402315SN/A        }
1412315SN/A
1422332SN/A        // Try to fetch the instruction
1432332SN/A
1442332SN/A#if FULL_SYSTEM
1455543Ssaidi@eecs.umich.edu#define IFETCH_FLAGS(pc)        ((pc) & 1) ? PHYSICAL : 0
1462332SN/A#else
1475543Ssaidi@eecs.umich.edu#define IFETCH_FLAGS(pc)        0
1482332SN/A#endif
1492332SN/A
1502683SN/A        uint64_t fetch_PC = thread->readPC() & ~3;
1512679SN/A
1522332SN/A        // set up memory request for instruction fetch
1532679SN/A        memReq = new Request(inst->threadNumber, fetch_PC,
1542679SN/A                             sizeof(uint32_t),
1552683SN/A                             IFETCH_FLAGS(thread->readPC()),
1565714Shsul@eecs.umich.edu                             fetch_PC, thread->contextId(),
1575714Shsul@eecs.umich.edu                             inst->threadNumber);
1582315SN/A
1595891Sgblack@eecs.umich.edu        bool succeeded = itb->translateAtomic(memReq, thread);
1602315SN/A
1612315SN/A        if (!succeeded) {
1622323SN/A            if (inst->getFault() == NoFault) {
1632332SN/A                // In this case the instruction was not a dummy
1642332SN/A                // instruction carrying an ITB fault.  In the single
1652332SN/A                // threaded case the ITB should still be able to
1662332SN/A                // translate this instruction; in the SMT case it's
1672332SN/A                // possible that its ITB entry was kicked out.
1682332SN/A                warn("%lli: Instruction PC %#x was not found in the ITB!",
1697823Ssteve.reinhardt@amd.com                     curTick(), thread->readPC());
1702732SN/A                handleError(inst);
1712315SN/A
1722323SN/A                // go to the next instruction
1732683SN/A                thread->setPC(thread->readNextPC());
1742683SN/A                thread->setNextPC(thread->readNextPC() + sizeof(MachInst));
1752315SN/A
1762354SN/A                break;
1772323SN/A            } else {
1782332SN/A                // The instruction is carrying an ITB fault.  Handle
1792332SN/A                // the fault and see if our results match the CPU on
1802332SN/A                // the next tick().
1812323SN/A                fault = inst->getFault();
1822323SN/A            }
1832315SN/A        }
1842315SN/A
1852323SN/A        if (fault == NoFault) {
1863349Sbinkertn@umich.edu            PacketPtr pkt = new Packet(memReq, Packet::ReadReq,
1872679SN/A                                     Packet::Broadcast);
1882679SN/A
1892679SN/A            pkt->dataStatic(&machInst);
1902679SN/A
1912679SN/A            icachePort->sendFunctional(pkt);
1922679SN/A
1932679SN/A            delete pkt;
1942315SN/A
1952332SN/A            // keep an instruction count
1962323SN/A            numInst++;
1972315SN/A
1982323SN/A            // decode the instruction
1992323SN/A            machInst = gtoh(machInst);
2002323SN/A            // Checks that the instruction matches what we expected it to be.
2012323SN/A            // Checks both the machine instruction and the PC.
2022323SN/A            validateInst(inst);
2032315SN/A
2043484Sktlim@umich.edu#if THE_ISA == ALPHA_ISA
2053484Sktlim@umich.edu            curStaticInst = StaticInst::decode(makeExtMI(machInst,
2063484Sktlim@umich.edu                                                         thread->readPC()));
2073484Sktlim@umich.edu#elif THE_ISA == SPARC_ISA
2082332SN/A            curStaticInst = StaticInst::decode(makeExtMI(machInst,
2093120Sgblack@eecs.umich.edu                                                         thread->getTC()));
2103484Sktlim@umich.edu#endif
2112315SN/A
2122323SN/A            fault = inst->getFault();
2132323SN/A        }
2142315SN/A
2152679SN/A        // Discard fetch's memReq.
2162679SN/A        delete memReq;
2172679SN/A        memReq = NULL;
2182679SN/A
2192315SN/A        // Either the instruction was a fault and we should process the fault,
2202315SN/A        // or we should just go ahead execute the instruction.  This assumes
2212315SN/A        // that the instruction is properly marked as a fault.
2222315SN/A        if (fault == NoFault) {
2232315SN/A
2242683SN/A            thread->funcExeInst++;
2252315SN/A
2262354SN/A            if (!inst->isUnverifiable())
2272354SN/A                fault = curStaticInst->execute(this, NULL);
2282315SN/A
2292315SN/A            // Checks to make sure instrution results are correct.
2302315SN/A            validateExecution(inst);
2312315SN/A
2322315SN/A            if (curStaticInst->isLoad()) {
2332315SN/A                ++numLoad;
2342315SN/A            }
2352315SN/A        }
2362315SN/A
2372315SN/A        if (fault != NoFault) {
2382315SN/A#if FULL_SYSTEM
2397678Sgblack@eecs.umich.edu            fault->invoke(tc, curStaticInst);
2402315SN/A            willChangePC = true;
2412683SN/A            newPC = thread->readPC();
2422315SN/A            DPRINTF(Checker, "Fault, PC is now %#x\n", newPC);
2432838Sktlim@umich.edu#endif
2442315SN/A        } else {
2452315SN/A#if THE_ISA != MIPS_ISA
2462315SN/A            // go to the next instruction
2472683SN/A            thread->setPC(thread->readNextPC());
2482683SN/A            thread->setNextPC(thread->readNextPC() + sizeof(MachInst));
2492315SN/A#else
2502315SN/A            // go to the next instruction
2512683SN/A            thread->setPC(thread->readNextPC());
2522683SN/A            thread->setNextPC(thread->readNextNPC());
2532683SN/A            thread->setNextNPC(thread->readNextNPC() + sizeof(MachInst));
2542315SN/A#endif
2552315SN/A
2562315SN/A        }
2572315SN/A
2582315SN/A#if FULL_SYSTEM
2592332SN/A        // @todo: Determine if these should happen only if the
2602332SN/A        // instruction hasn't faulted.  In the SimpleCPU case this may
2612332SN/A        // not be true, but in the O3 or Ozone case this may be true.
2622315SN/A        Addr oldpc;
2632315SN/A        int count = 0;
2642315SN/A        do {
2652683SN/A            oldpc = thread->readPC();
2662690SN/A            system->pcEventQueue.service(tc);
2672315SN/A            count++;
2682683SN/A        } while (oldpc != thread->readPC());
2692315SN/A        if (count > 1) {
2702315SN/A            willChangePC = true;
2712683SN/A            newPC = thread->readPC();
2722315SN/A            DPRINTF(Checker, "PC Event, PC is now %#x\n", newPC);
2732315SN/A        }
2742315SN/A#endif
2752315SN/A
2762332SN/A        // @todo:  Optionally can check all registers. (Or just those
2772315SN/A        // that have been modified).
2782315SN/A        validateState();
2792315SN/A
2802679SN/A        if (memReq) {
2812679SN/A            delete memReq;
2822679SN/A            memReq = NULL;
2832679SN/A        }
2842679SN/A
2852332SN/A        // Continue verifying instructions if there's another completed
2862332SN/A        // instruction waiting to be verified.
2872315SN/A        if (instList.empty()) {
2882315SN/A            break;
2892315SN/A        } else if (instList.front()->isCompleted()) {
2902315SN/A            inst = instList.front();
2912315SN/A            instList.pop_front();
2922315SN/A        } else {
2932315SN/A            break;
2942315SN/A        }
2952315SN/A    }
2962354SN/A    unverifiedInst = NULL;
2972315SN/A}
2982315SN/A
2992315SN/Atemplate <class DynInstPtr>
3002315SN/Avoid
3012840Sktlim@umich.eduChecker<DynInstPtr>::switchOut()
3022315SN/A{
3032315SN/A    instList.clear();
3042315SN/A}
3052315SN/A
3062315SN/Atemplate <class DynInstPtr>
3072315SN/Avoid
3082315SN/AChecker<DynInstPtr>::takeOverFrom(BaseCPU *oldCPU)
3092315SN/A{
3102315SN/A}
3112315SN/A
3122315SN/Atemplate <class DynInstPtr>
3132315SN/Avoid
3142315SN/AChecker<DynInstPtr>::validateInst(DynInstPtr &inst)
3152315SN/A{
3162683SN/A    if (inst->readPC() != thread->readPC()) {
3172332SN/A        warn("%lli: PCs do not match! Inst: %#x, checker: %#x",
3187823Ssteve.reinhardt@amd.com             curTick(), inst->readPC(), thread->readPC());
3192315SN/A        if (changedPC) {
3202332SN/A            warn("%lli: Changed PCs recently, may not be an error",
3217823Ssteve.reinhardt@amd.com                 curTick());
3222315SN/A        } else {
3232732SN/A            handleError(inst);
3242315SN/A        }
3252315SN/A    }
3262315SN/A
3272332SN/A    MachInst mi = static_cast<MachInst>(inst->staticInst->machInst);
3282332SN/A
3292332SN/A    if (mi != machInst) {
3302332SN/A        warn("%lli: Binary instructions do not match! Inst: %#x, "
3312332SN/A             "checker: %#x",
3327823Ssteve.reinhardt@amd.com             curTick(), mi, machInst);
3332732SN/A        handleError(inst);
3342315SN/A    }
3352315SN/A}
3362315SN/A
3372315SN/Atemplate <class DynInstPtr>
3382315SN/Avoid
3392315SN/AChecker<DynInstPtr>::validateExecution(DynInstPtr &inst)
3402315SN/A{
3412732SN/A    bool result_mismatch = false;
3422315SN/A    if (inst->numDestRegs()) {
3432332SN/A        // @todo: Support more destination registers.
3442315SN/A        if (inst->isUnverifiable()) {
3452332SN/A            // Unverifiable instructions assume they were executed
3462332SN/A            // properly by the CPU. Grab the result from the
3472332SN/A            // instruction and write it to the register.
3482732SN/A            copyResult(inst);
3492315SN/A        } else if (result.integer != inst->readIntResult()) {
3502732SN/A            result_mismatch = true;
3512732SN/A        }
3522732SN/A    }
3532732SN/A
3542732SN/A    if (result_mismatch) {
3552732SN/A        warn("%lli: Instruction results do not match! (Values may not "
3562732SN/A             "actually be integers) Inst: %#x, checker: %#x",
3577823Ssteve.reinhardt@amd.com             curTick(), inst->readIntResult(), result.integer);
3582732SN/A
3592732SN/A        // It's useful to verify load values from memory, but in MP
3602732SN/A        // systems the value obtained at execute may be different than
3612732SN/A        // the value obtained at completion.  Similarly DMA can
3622732SN/A        // present the same problem on even UP systems.  Thus there is
3632732SN/A        // the option to only warn on loads having a result error.
3642732SN/A        if (inst->isLoad() && warnOnlyOnLoadError) {
3652732SN/A            copyResult(inst);
3662732SN/A        } else {
3672732SN/A            handleError(inst);
3682315SN/A        }
3692315SN/A    }
3702315SN/A
3712683SN/A    if (inst->readNextPC() != thread->readNextPC()) {
3722332SN/A        warn("%lli: Instruction next PCs do not match! Inst: %#x, "
3732332SN/A             "checker: %#x",
3747823Ssteve.reinhardt@amd.com             curTick(), inst->readNextPC(), thread->readNextPC());
3752732SN/A        handleError(inst);
3762315SN/A    }
3772315SN/A
3782315SN/A    // Checking side effect registers can be difficult if they are not
3792315SN/A    // checked simultaneously with the execution of the instruction.
3802315SN/A    // This is because other valid instructions may have modified
3812315SN/A    // these registers in the meantime, and their values are not
3822315SN/A    // stored within the DynInst.
3832315SN/A    while (!miscRegIdxs.empty()) {
3842315SN/A        int misc_reg_idx = miscRegIdxs.front();
3852315SN/A        miscRegIdxs.pop();
3862315SN/A
3874172Ssaidi@eecs.umich.edu        if (inst->tcBase()->readMiscRegNoEffect(misc_reg_idx) !=
3884172Ssaidi@eecs.umich.edu            thread->readMiscRegNoEffect(misc_reg_idx)) {
3892332SN/A            warn("%lli: Misc reg idx %i (side effect) does not match! "
3902332SN/A                 "Inst: %#x, checker: %#x",
3917823Ssteve.reinhardt@amd.com                 curTick(), misc_reg_idx,
3924172Ssaidi@eecs.umich.edu                 inst->tcBase()->readMiscRegNoEffect(misc_reg_idx),
3934172Ssaidi@eecs.umich.edu                 thread->readMiscRegNoEffect(misc_reg_idx));
3942732SN/A            handleError(inst);
3952315SN/A        }
3962315SN/A    }
3972315SN/A}
3982315SN/A
3992315SN/Atemplate <class DynInstPtr>
4002315SN/Avoid
4012315SN/AChecker<DynInstPtr>::validateState()
4022315SN/A{
4032354SN/A    if (updateThisCycle) {
4042354SN/A        warn("%lli: Instruction PC %#x results didn't match up, copying all "
4057823Ssteve.reinhardt@amd.com             "registers from main CPU", curTick(), unverifiedInst->readPC());
4062354SN/A        // Heavy-weight copying of all registers
4073126Sktlim@umich.edu        thread->copyArchRegs(unverifiedInst->tcBase());
4082356SN/A        // Also advance the PC.  Hopefully no PC-based events happened.
4092356SN/A#if THE_ISA != MIPS_ISA
4102356SN/A        // go to the next instruction
4113126Sktlim@umich.edu        thread->setPC(thread->readNextPC());
4123126Sktlim@umich.edu        thread->setNextPC(thread->readNextPC() + sizeof(MachInst));
4132356SN/A#else
4142356SN/A        // go to the next instruction
4153126Sktlim@umich.edu        thread->setPC(thread->readNextPC());
4163126Sktlim@umich.edu        thread->setNextPC(thread->readNextNPC());
4173126Sktlim@umich.edu        thread->setNextNPC(thread->readNextNPC() + sizeof(MachInst));
4182356SN/A#endif
4192354SN/A        updateThisCycle = false;
4203126Sktlim@umich.edu    }
4212315SN/A}
4222315SN/A
4232315SN/Atemplate <class DynInstPtr>
4242315SN/Avoid
4252732SN/AChecker<DynInstPtr>::copyResult(DynInstPtr &inst)
4262732SN/A{
4272732SN/A    RegIndex idx = inst->destRegIdx(0);
4282732SN/A    if (idx < TheISA::FP_Base_DepTag) {
4292732SN/A        thread->setIntReg(idx, inst->readIntResult());
4302732SN/A    } else if (idx < TheISA::Fpcr_DepTag) {
4312732SN/A        thread->setFloatRegBits(idx, inst->readIntResult());
4322732SN/A    } else {
4334172Ssaidi@eecs.umich.edu        thread->setMiscRegNoEffect(idx, inst->readIntResult());
4342732SN/A    }
4352732SN/A}
4362732SN/A
4372732SN/Atemplate <class DynInstPtr>
4382732SN/Avoid
4392732SN/AChecker<DynInstPtr>::dumpAndExit(DynInstPtr &inst)
4402732SN/A{
4412732SN/A    cprintf("Error detected, instruction information:\n");
4422732SN/A    cprintf("PC:%#x, nextPC:%#x\n[sn:%lli]\n[tid:%i]\n"
4432732SN/A            "Completed:%i\n",
4442732SN/A            inst->readPC(),
4452732SN/A            inst->readNextPC(),
4462732SN/A            inst->seqNum,
4472732SN/A            inst->threadNumber,
4482732SN/A            inst->isCompleted());
4492732SN/A    inst->dump();
4502732SN/A    CheckerCPU::dumpAndExit();
4512732SN/A}
4522732SN/A
4532732SN/Atemplate <class DynInstPtr>
4542732SN/Avoid
4552315SN/AChecker<DynInstPtr>::dumpInsts()
4562315SN/A{
4572315SN/A    int num = 0;
4582315SN/A
4592315SN/A    InstListIt inst_list_it = --(instList.end());
4602315SN/A
4612315SN/A    cprintf("Inst list size: %i\n", instList.size());
4622315SN/A
4632315SN/A    while (inst_list_it != instList.end())
4642315SN/A    {
4652315SN/A        cprintf("Instruction:%i\n",
4662315SN/A                num);
4672315SN/A
4682315SN/A        cprintf("PC:%#x\n[sn:%lli]\n[tid:%i]\n"
4692315SN/A                "Completed:%i\n",
4702315SN/A                (*inst_list_it)->readPC(),
4712315SN/A                (*inst_list_it)->seqNum,
4722315SN/A                (*inst_list_it)->threadNumber,
4732315SN/A                (*inst_list_it)->isCompleted());
4742315SN/A
4752315SN/A        cprintf("\n");
4762315SN/A
4772315SN/A        inst_list_it--;
4782315SN/A        ++num;
4792315SN/A    }
4802315SN/A
4812315SN/A}
482