2 * Copyright (c) 2011 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include "arch/isa_traits.hh"
48 #include "arch/vtophys.hh"
49 #include "base/refcnt.hh"
50 #include "config/the_isa.hh"
51 #include "cpu/base_dyn_inst.hh"
52 #include "cpu/exetrace.hh"
53 #include "cpu/simple_thread.hh"
54 #include "cpu/static_inst.hh"
55 #include "cpu/thread_context.hh"
56 #include "cpu/checker/cpu.hh"
57 #include "debug/Checker.hh"
58 #include "sim/full_system.hh"
59 #include "sim/sim_object.hh"
60 #include "sim/stats.hh"
63 using namespace TheISA;
67 Checker<Impl>::advancePC(Fault fault)
69 if (fault != NoFault) {
70 curMacroStaticInst = StaticInst::nullStaticInstPtr;
71 fault->invoke(tc, curStaticInst);
72 thread->decoder.reset();
75 if (curStaticInst->isLastMicroop())
76 curMacroStaticInst = StaticInst::nullStaticInstPtr;
77 TheISA::PCState pcState = thread->pcState();
78 TheISA::advancePC(pcState, curStaticInst);
79 thread->pcState(pcState);
80 DPRINTF(Checker, "Advancing PC to %s.\n", thread->pcState());
84 //////////////////////////////////////////////////
88 Checker<Impl>::handlePendingInt()
90 DPRINTF(Checker, "IRQ detected at PC: %s with %d insts in buffer\n",
91 thread->pcState(), instList.size());
92 DynInstPtr boundaryInst = NULL;
93 if (!instList.empty()) {
94 // Set the instructions as completed and verify as much as possible.
96 typename std::list<DynInstPtr>::iterator itr;
98 for (itr = instList.begin(); itr != instList.end(); itr++) {
99 (*itr)->setCompleted();
102 inst = instList.front();
103 boundaryInst = instList.back();
104 verify(inst); // verify the instructions
107 if ((!boundaryInst && curMacroStaticInst &&
108 curStaticInst->isDelayedCommit() &&
109 !curStaticInst->isLastMicroop()) ||
110 (boundaryInst && boundaryInst->isDelayedCommit() &&
111 !boundaryInst->isLastMicroop())) {
112 panic("%lli: Trying to take an interrupt in middle of "
113 "a non-interuptable instruction!", curTick());
116 thread->decoder.reset();
117 curMacroStaticInst = StaticInst::nullStaticInstPtr;
120 template <class Impl>
122 Checker<Impl>::verify(DynInstPtr &completed_inst)
126 // Make sure serializing instructions are actually
127 // seen as serializing to commit. instList should be
128 // empty in these cases.
129 if ((completed_inst->isSerializing() ||
130 completed_inst->isSerializeBefore()) &&
132 (instList.front()->seqNum != completed_inst->seqNum) : 0)) {
133 panic("%lli: Instruction sn:%lli at PC %s is serializing before but is"
134 " entering instList with other instructions\n", curTick(),
135 completed_inst->seqNum, completed_inst->pcState());
138 // Either check this instruction, or add it to a list of
139 // instructions waiting to be checked. Instructions must be
140 // checked in program order, so if a store has committed yet not
141 // completed, there may be some instructions that are waiting
142 // behind it that have completed and must be checked.
143 if (!instList.empty()) {
144 if (youngestSN < completed_inst->seqNum) {
145 DPRINTF(Checker, "Adding instruction [sn:%lli] PC:%s to list\n",
146 completed_inst->seqNum, completed_inst->pcState());
147 instList.push_back(completed_inst);
148 youngestSN = completed_inst->seqNum;
151 if (!instList.front()->isCompleted()) {
154 inst = instList.front();
155 instList.pop_front();
158 if (!completed_inst->isCompleted()) {
159 if (youngestSN < completed_inst->seqNum) {
160 DPRINTF(Checker, "Adding instruction [sn:%lli] PC:%s to list\n",
161 completed_inst->seqNum, completed_inst->pcState());
162 instList.push_back(completed_inst);
163 youngestSN = completed_inst->seqNum;
167 if (youngestSN < completed_inst->seqNum) {
168 inst = completed_inst;
169 youngestSN = completed_inst->seqNum;
176 // Make sure a serializing instruction is actually seen as
177 // serializing. instList should be empty here
178 if (inst->isSerializeAfter() && !instList.empty()) {
179 panic("%lli: Instruction sn:%lli at PC %s is serializing after but is"
180 " exiting instList with other instructions\n", curTick(),
181 completed_inst->seqNum, completed_inst->pcState());
183 unverifiedInst = inst;
186 // Try to check all instructions that are completed, ending if we
187 // run out of instructions to check or if an instruction is not
190 DPRINTF(Checker, "Processing instruction [sn:%lli] PC:%s.\n",
191 unverifiedInst->seqNum, unverifiedInst->pcState());
192 unverifiedReq = NULL;
193 unverifiedReq = unverifiedInst->reqToVerify;
194 unverifiedMemData = unverifiedInst->memData;
195 // Make sure results queue is empty
196 while (!result.empty()) {
201 Fault fault = NoFault;
203 // maintain $r0 semantics
204 thread->setIntReg(ZeroReg, 0);
205 #if THE_ISA == ALPHA_ISA
206 thread->setFloatReg(ZeroReg, 0.0);
209 // Check if any recent PC changes match up with anything we
210 // expect to happen. This is mostly to check if traps or
211 // PC-based events have occurred in both the checker and CPU.
213 DPRINTF(Checker, "Changed PC recently to %s\n",
216 if (newPCState == thread->pcState()) {
217 DPRINTF(Checker, "Changed PC matches expected PC\n");
219 warn("%lli: Changed PC does not match expected PC, "
220 "changed: %s, expected: %s",
221 curTick(), thread->pcState(), newPCState);
222 CheckerCPU::handleError();
224 willChangePC = false;
229 DPRINTF(Checker, "Changed NextPC recently to %#x\n",
230 thread->nextInstAddr());
231 changedNextPC = false;
234 // Try to fetch the instruction
235 uint64_t fetchOffset = 0;
236 bool fetchDone = false;
239 Addr fetch_PC = thread->instAddr();
240 fetch_PC = (fetch_PC & PCMask) + fetchOffset;
244 // If not in the middle of a macro instruction
245 if (!curMacroStaticInst) {
246 // set up memory request for instruction fetch
247 memReq = new Request(unverifiedInst->threadNumber, fetch_PC,
251 fetch_PC, thread->contextId(),
252 unverifiedInst->threadNumber);
253 memReq->setVirt(0, fetch_PC, sizeof(MachInst),
254 Request::INST_FETCH, masterId, thread->instAddr());
257 fault = itb->translateFunctional(memReq, tc, BaseTLB::Execute);
259 if (fault != NoFault) {
260 if (unverifiedInst->getFault() == NoFault) {
261 // In this case the instruction was not a dummy
262 // instruction carrying an ITB fault. In the single
263 // threaded case the ITB should still be able to
264 // translate this instruction; in the SMT case it's
265 // possible that its ITB entry was kicked out.
266 warn("%lli: Instruction PC %s was not found in the "
267 "ITB!", curTick(), thread->pcState());
268 handleError(unverifiedInst);
270 // go to the next instruction
273 // Give up on an ITB fault..
275 unverifiedInst = NULL;
278 // The instruction is carrying an ITB fault. Handle
279 // the fault and see if our results match the CPU on
281 fault = unverifiedInst->getFault();
286 PacketPtr pkt = new Packet(memReq, MemCmd::ReadReq);
288 pkt->dataStatic(&machInst);
289 icachePort->sendFunctional(pkt);
290 machInst = gtoh(machInst);
297 if (fault == NoFault) {
298 TheISA::PCState pcState = thread->pcState();
300 if (isRomMicroPC(pcState.microPC())) {
303 microcodeRom.fetchMicroop(pcState.microPC(), NULL);
304 } else if (!curMacroStaticInst) {
305 //We're not in the middle of a macro instruction
306 StaticInstPtr instPtr = NULL;
308 //Predecode, ie bundle up an ExtMachInst
309 //If more fetch data is needed, pass it in.
310 Addr fetchPC = (pcState.instAddr() & PCMask) + fetchOffset;
311 thread->decoder.moreBytes(pcState, fetchPC, machInst);
313 //If an instruction is ready, decode it.
314 //Otherwise, we'll have to fetch beyond the
315 //MachInst at the current pc.
316 if (thread->decoder.instReady()) {
318 instPtr = thread->decoder.decode(pcState);
319 thread->pcState(pcState);
322 fetchOffset += sizeof(TheISA::MachInst);
325 //If we decoded an instruction and it's microcoded,
326 //start pulling out micro ops
327 if (instPtr && instPtr->isMacroop()) {
328 curMacroStaticInst = instPtr;
330 instPtr->fetchMicroop(pcState.microPC());
332 curStaticInst = instPtr;
335 // Read the next micro op from the macro-op
337 curMacroStaticInst->fetchMicroop(pcState.microPC());
342 // reset decoder on Checker
343 thread->decoder.reset();
345 // Check Checker and CPU get same instruction, and record
346 // any faults the CPU may have had.
347 Fault unverifiedFault;
348 if (fault == NoFault) {
349 unverifiedFault = unverifiedInst->getFault();
351 // Checks that the instruction matches what we expected it to be.
352 // Checks both the machine instruction and the PC.
353 validateInst(unverifiedInst);
356 // keep an instruction count
360 // Either the instruction was a fault and we should process the fault,
361 // or we should just go ahead execute the instruction. This assumes
362 // that the instruction is properly marked as a fault.
363 if (fault == NoFault) {
364 // Execute Checker instruction and trace
365 if (!unverifiedInst->isUnverifiable()) {
366 Trace::InstRecord *traceData = tracer->getInstRecord(curTick(),
371 fault = curStaticInst->execute(this, traceData);
378 if (fault == NoFault && unverifiedFault == NoFault) {
379 thread->funcExeInst++;
380 // Checks to make sure instrution results are correct.
381 validateExecution(unverifiedInst);
383 if (curStaticInst->isLoad()) {
386 } else if (fault != NoFault && unverifiedFault == NoFault) {
387 panic("%lli: sn: %lli at PC: %s took a fault in checker "
388 "but not in driver CPU\n", curTick(),
389 unverifiedInst->seqNum, unverifiedInst->pcState());
390 } else if (fault == NoFault && unverifiedFault != NoFault) {
391 panic("%lli: sn: %lli at PC: %s took a fault in driver "
392 "CPU but not in checker\n", curTick(),
393 unverifiedInst->seqNum, unverifiedInst->pcState());
397 // Take any faults here
398 if (fault != NoFault) {
400 fault->invoke(tc, curStaticInst);
402 newPCState = thread->pcState();
403 DPRINTF(Checker, "Fault, PC is now %s\n", newPCState);
404 curMacroStaticInst = StaticInst::nullStaticInstPtr;
411 // @todo: Determine if these should happen only if the
412 // instruction hasn't faulted. In the SimpleCPU case this may
413 // not be true, but in the O3 or Ozone case this may be true.
417 oldpc = thread->instAddr();
418 system->pcEventQueue.service(tc);
420 } while (oldpc != thread->instAddr());
423 newPCState = thread->pcState();
424 DPRINTF(Checker, "PC Event, PC is now %s\n", newPCState);
428 // @todo: Optionally can check all registers. (Or just those
429 // that have been modified).
432 // Continue verifying instructions if there's another completed
433 // instruction waiting to be verified.
434 if (instList.empty()) {
436 } else if (instList.front()->isCompleted()) {
437 unverifiedInst = NULL;
438 unverifiedInst = instList.front();
439 instList.pop_front();
444 unverifiedInst = NULL;
447 template <class Impl>
449 Checker<Impl>::switchOut()
454 template <class Impl>
456 Checker<Impl>::takeOverFrom(BaseCPU *oldCPU)
460 template <class Impl>
462 Checker<Impl>::validateInst(DynInstPtr &inst)
464 if (inst->instAddr() != thread->instAddr()) {
465 warn("%lli: PCs do not match! Inst: %s, checker: %s",
466 curTick(), inst->pcState(), thread->pcState());
468 warn("%lli: Changed PCs recently, may not be an error",
475 if (curStaticInst != inst->staticInst) {
476 warn("%lli: StaticInstPtrs don't match. (%s, %s).\n", curTick(),
477 curStaticInst->getName(), inst->staticInst->getName());
481 template <class Impl>
483 Checker<Impl>::validateExecution(DynInstPtr &inst)
485 uint64_t checker_val;
488 bool result_mismatch = false;
490 if (inst->isUnverifiable()) {
491 // Unverifiable instructions assume they were executed
492 // properly by the CPU. Grab the result from the
493 // instruction and write it to the register.
494 copyResult(inst, 0, idx);
495 } else if (inst->numDestRegs() > 0 && !result.empty()) {
496 DPRINTF(Checker, "Dest regs %d, number of checker dest regs %d\n",
497 inst->numDestRegs(), result.size());
498 for (int i = 0; i < inst->numDestRegs() && !result.empty(); i++) {
499 result.front().get(checker_val);
502 inst->template popResult<uint64_t>(inst_val);
503 if (checker_val != inst_val) {
504 result_mismatch = true;
509 } // Checker CPU checks all the saved results in the dyninst passed by
510 // the cpu model being checked against the saved results present in
511 // the static inst executed in the Checker. Sometimes the number
512 // of saved results differs between the dyninst and static inst, but
513 // this is ok and not a bug. May be worthwhile to try and correct this.
515 if (result_mismatch) {
516 warn("%lli: Instruction results do not match! (Values may not "
517 "actually be integers) Inst: %#x, checker: %#x",
518 curTick(), inst_val, checker_val);
520 // It's useful to verify load values from memory, but in MP
521 // systems the value obtained at execute may be different than
522 // the value obtained at completion. Similarly DMA can
523 // present the same problem on even UP systems. Thus there is
524 // the option to only warn on loads having a result error.
525 // The load/store queue in Detailed CPU can also cause problems
526 // if load/store forwarding is allowed.
527 if (inst->isLoad() && warnOnlyOnLoadError) {
528 copyResult(inst, inst_val, idx);
534 if (inst->nextInstAddr() != thread->nextInstAddr()) {
535 warn("%lli: Instruction next PCs do not match! Inst: %#x, "
537 curTick(), inst->nextInstAddr(), thread->nextInstAddr());
541 // Checking side effect registers can be difficult if they are not
542 // checked simultaneously with the execution of the instruction.
543 // This is because other valid instructions may have modified
544 // these registers in the meantime, and their values are not
545 // stored within the DynInst.
546 while (!miscRegIdxs.empty()) {
547 int misc_reg_idx = miscRegIdxs.front();
550 if (inst->tcBase()->readMiscRegNoEffect(misc_reg_idx) !=
551 thread->readMiscRegNoEffect(misc_reg_idx)) {
552 warn("%lli: Misc reg idx %i (side effect) does not match! "
553 "Inst: %#x, checker: %#x",
554 curTick(), misc_reg_idx,
555 inst->tcBase()->readMiscRegNoEffect(misc_reg_idx),
556 thread->readMiscRegNoEffect(misc_reg_idx));
563 // This function is weird, if it is called it means the Checker and
564 // O3 have diverged, so panic is called for now. It may be useful
565 // to resynch states and continue if the divergence is a false positive
566 template <class Impl>
568 Checker<Impl>::validateState()
570 if (updateThisCycle) {
571 // Change this back to warn if divergences end up being false positives
572 panic("%lli: Instruction PC %#x results didn't match up, copying all "
573 "registers from main CPU", curTick(), unverifiedInst->instAddr());
575 // Terribly convoluted way to make sure O3 model does not implode
576 bool no_squash_from_TC = unverifiedInst->thread->noSquashFromTC;
577 unverifiedInst->thread->noSquashFromTC = true;
579 // Heavy-weight copying of all registers
580 thread->copyArchRegs(unverifiedInst->tcBase());
581 unverifiedInst->thread->noSquashFromTC = no_squash_from_TC;
583 // Set curStaticInst to unverifiedInst->staticInst
584 curStaticInst = unverifiedInst->staticInst;
585 // Also advance the PC. Hopefully no PC-based events happened.
587 updateThisCycle = false;
591 template <class Impl>
593 Checker<Impl>::copyResult(DynInstPtr &inst, uint64_t mismatch_val,
596 // We've already popped one dest off the queue,
597 // so do the fix-up then start with the next dest reg;
598 if (start_idx >= 0) {
599 RegIndex idx = inst->destRegIdx(start_idx);
600 if (idx < TheISA::FP_Base_DepTag) {
601 thread->setIntReg(idx, mismatch_val);
602 } else if (idx < TheISA::Ctrl_Base_DepTag) {
603 thread->setFloatRegBits(idx, mismatch_val);
604 } else if (idx < TheISA::Max_DepTag) {
605 thread->setMiscReg(idx - TheISA::Ctrl_Base_DepTag,
611 for (int i = start_idx; i < inst->numDestRegs(); i++) {
612 RegIndex idx = inst->destRegIdx(i);
613 inst->template popResult<uint64_t>(res);
614 if (idx < TheISA::FP_Base_DepTag) {
615 thread->setIntReg(idx, res);
616 } else if (idx < TheISA::Ctrl_Base_DepTag) {
617 thread->setFloatRegBits(idx, res);
618 } else if (idx < TheISA::Max_DepTag) {
619 // Try to get the proper misc register index for ARM here...
620 thread->setMiscReg(idx - TheISA::Ctrl_Base_DepTag, res);
621 } // else Register is out of range...
625 template <class Impl>
627 Checker<Impl>::dumpAndExit(DynInstPtr &inst)
629 cprintf("Error detected, instruction information:\n");
630 cprintf("PC:%s, nextPC:%#x\n[sn:%lli]\n[tid:%i]\n"
633 inst->nextInstAddr(),
636 inst->isCompleted());
638 CheckerCPU::dumpAndExit();
641 template <class Impl>
643 Checker<Impl>::dumpInsts()
647 InstListIt inst_list_it = --(instList.end());
649 cprintf("Inst list size: %i\n", instList.size());
651 while (inst_list_it != instList.end())
653 cprintf("Instruction:%i\n",
656 cprintf("PC:%s\n[sn:%lli]\n[tid:%i]\n"
658 (*inst_list_it)->pcState(),
659 (*inst_list_it)->seqNum,
660 (*inst_list_it)->threadNumber,
661 (*inst_list_it)->isCompleted());