2 * Copyright (c) 2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "base/refcnt.hh"
33 #include "cpu/base.hh"
34 #include "cpu/base_dyn_inst.hh"
35 #include "cpu/checker/cpu.hh"
36 #include "cpu/simple_thread.hh"
37 #include "cpu/thread_context.hh"
38 #include "cpu/static_inst.hh"
39 #include "sim/byteswap.hh"
40 #include "sim/sim_object.hh"
41 #include "sim/stats.hh"
43 #include "cpu/o3/alpha_dyn_inst.hh"
44 #include "cpu/o3/alpha_impl.hh"
46 //#include "cpu/ozone/dyn_inst.hh"
47 //#include "cpu/ozone/ozone_impl.hh"
48 //#include "cpu/ozone/simple_impl.hh"
51 #include "sim/system.hh"
52 #include "arch/vtophys.hh"
56 //The CheckerCPU does alpha only
57 using namespace AlphaISA
;
64 CheckerCPU::CheckerCPU(Params
*p
)
65 : BaseCPU(p
), thread(NULL
), tc(NULL
)
75 changedPC
= willChangePC
= changedNextPC
= false;
77 exitOnError
= p
->exitOnError
;
88 CheckerCPU::~CheckerCPU()
93 CheckerCPU::setMemory(MemObject
*mem
)
97 thread
= new SimpleThread(this, /* thread_num */ 0, process
,
100 thread
->setStatus(ThreadContext::Suspended
);
101 tc
= thread
->getTC();
102 threadContexts
.push_back(tc
);
105 thread
= new SimpleThread(this, 0, systemPtr
, itb
, dtb
, memPtr
, false);
107 thread
->setStatus(ThreadContext::Suspended
);
108 tc
= thread
->getTC();
109 threadContexts
.push_back(tc
);
110 delete thread
->kernelStats
;
111 thread
->kernelStats
= NULL
;
118 CheckerCPU::setSystem(System
*system
)
123 thread
= new SimpleThread(this, 0, systemPtr
, itb
, dtb
, memPtr
, false);
125 thread
->setStatus(ThreadContext::Suspended
);
126 tc
= thread
->getTC();
127 threadContexts
.push_back(tc
);
128 delete thread
->kernelStats
;
129 thread
->kernelStats
= NULL
;
135 CheckerCPU::setIcachePort(Port
*icache_port
)
137 icachePort
= icache_port
;
141 CheckerCPU::setDcachePort(Port
*dcache_port
)
143 dcachePort
= dcache_port
;
147 CheckerCPU::serialize(ostream
&os
)
150 BaseCPU::serialize(os);
151 SERIALIZE_SCALAR(inst);
152 nameOut(os, csprintf("%s.xc", name()));
153 thread->serialize(os);
154 cacheCompletionEvent.serialize(os);
159 CheckerCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
162 BaseCPU::unserialize(cp, section);
163 UNSERIALIZE_SCALAR(inst);
164 thread->unserialize(cp, csprintf("%s.xc", section));
169 CheckerCPU::copySrcTranslate(Addr src
)
171 panic("Unimplemented!");
175 CheckerCPU::copy(Addr dest
)
177 panic("Unimplemented!");
182 CheckerCPU::read(Addr addr
, T
&data
, unsigned flags
)
184 // need to fill in CPU & thread IDs here
185 memReq
= new Request();
187 memReq
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
189 // translate to physical address
190 translateDataReadReq(memReq
);
192 Packet
*pkt
= new Packet(memReq
, Packet::ReadReq
, Packet::Broadcast
);
194 pkt
->dataStatic(&data
);
196 if (!(memReq
->getFlags() & UNCACHEABLE
)) {
197 // Access memory to see if we have the same data
198 dcachePort
->sendFunctional(pkt
);
200 // Assume the data is correct if it's an uncached access
201 memcpy(&data
, &unverifiedResult
.integer
, sizeof(T
));
209 #ifndef DOXYGEN_SHOULD_SKIP_THIS
213 CheckerCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
217 CheckerCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
221 CheckerCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
225 CheckerCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
227 #endif //DOXYGEN_SHOULD_SKIP_THIS
231 CheckerCPU::read(Addr addr
, double &data
, unsigned flags
)
233 return read(addr
, *(uint64_t*)&data
, flags
);
238 CheckerCPU::read(Addr addr
, float &data
, unsigned flags
)
240 return read(addr
, *(uint32_t*)&data
, flags
);
245 CheckerCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
247 return read(addr
, (uint32_t&)data
, flags
);
252 CheckerCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
254 // need to fill in CPU & thread IDs here
255 memReq
= new Request();
257 memReq
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
259 // translate to physical address
260 thread
->translateDataWriteReq(memReq
);
262 // Can compare the write data and result only if it's cacheable,
263 // not a store conditional, or is a store conditional that
265 // @todo: Verify that actual memory matches up with these values.
266 // Right now it only verifies that the instruction data is the
267 // same as what was in the request that got sent to memory; there
268 // is no verification that it is the same as what is in memory.
269 // This is because the LSQ would have to be snooped in the CPU to
272 !(unverifiedReq
->getFlags() & UNCACHEABLE
) &&
273 (!(unverifiedReq
->getFlags() & LOCKED
) ||
274 ((unverifiedReq
->getFlags() & LOCKED
) &&
275 unverifiedReq
->getScResult() == 1))) {
278 // This code would work if the LSQ allowed for snooping.
279 Packet *pkt = new Packet(memReq, Packet::ReadReq, Packet::Broadcast);
280 pkt.dataStatic(&inst_data);
282 dcachePort->sendFunctional(pkt);
286 memcpy(&inst_data
, unverifiedMemData
, sizeof(T
));
288 if (data
!= inst_data
) {
289 warn("%lli: Store value does not match value in memory! "
290 "Instruction: %#x, memory: %#x",
291 curTick
, inst_data
, data
);
296 // Assume the result was the same as the one passed in. This checker
297 // doesn't check if the SC should succeed or fail, it just checks the
299 if (res
&& unverifiedReq
->scResultValid())
300 *res
= unverifiedReq
->getScResult();
306 #ifndef DOXYGEN_SHOULD_SKIP_THIS
309 CheckerCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
313 CheckerCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
317 CheckerCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
321 CheckerCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
323 #endif //DOXYGEN_SHOULD_SKIP_THIS
327 CheckerCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
329 return write(*(uint64_t*)&data
, addr
, flags
, res
);
334 CheckerCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
336 return write(*(uint32_t*)&data
, addr
, flags
, res
);
341 CheckerCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
343 return write((uint32_t)data
, addr
, flags
, res
);
349 CheckerCPU::dbg_vtophys(Addr addr
)
351 return vtophys(xcProxy
, addr
);
353 #endif // FULL_SYSTEM
356 CheckerCPU::translateInstReq(Request
*req
)
359 return (thread
->translateInstReq(req
) == NoFault
);
361 thread
->translateInstReq(req
);
367 CheckerCPU::translateDataReadReq(Request
*req
)
369 thread
->translateDataReadReq(req
);
371 if (req
->getVaddr() != unverifiedReq
->getVaddr()) {
372 warn("%lli: Request virtual addresses do not match! Inst: %#x, "
374 curTick
, unverifiedReq
->getVaddr(), req
->getVaddr());
377 req
->setPaddr(unverifiedReq
->getPaddr());
379 if (checkFlags(req
)) {
380 warn("%lli: Request flags do not match! Inst: %#x, checker: %#x",
381 curTick
, unverifiedReq
->getFlags(), req
->getFlags());
387 CheckerCPU::translateDataWriteReq(Request
*req
)
389 thread
->translateDataWriteReq(req
);
391 if (req
->getVaddr() != unverifiedReq
->getVaddr()) {
392 warn("%lli: Request virtual addresses do not match! Inst: %#x, "
394 curTick
, unverifiedReq
->getVaddr(), req
->getVaddr());
397 req
->setPaddr(unverifiedReq
->getPaddr());
399 if (checkFlags(req
)) {
400 warn("%lli: Request flags do not match! Inst: %#x, checker: %#x",
401 curTick
, unverifiedReq
->getFlags(), req
->getFlags());
407 CheckerCPU::checkFlags(Request
*req
)
409 // Remove any dynamic flags that don't have to do with the request itself.
410 unsigned flags
= unverifiedReq
->getFlags();
411 unsigned mask
= LOCKED
| PHYSICAL
| VPTE
| ALTMODE
| UNCACHEABLE
| NO_FAULT
;
412 flags
= flags
& (mask
);
413 if (flags
== req
->getFlags()) {
420 template <class DynInstPtr
>
422 Checker
<DynInstPtr
>::tick(DynInstPtr
&completed_inst
)
426 // Either check this instruction, or add it to a list of
427 // instructions waiting to be checked. Instructions must be
428 // checked in program order, so if a store has committed yet not
429 // completed, there may be some instructions that are waiting
430 // behind it that have completed and must be checked.
431 if (!instList
.empty()) {
432 if (youngestSN
< completed_inst
->seqNum
) {
433 DPRINTF(Checker
, "Adding instruction [sn:%lli] PC:%#x to list.\n",
434 completed_inst
->seqNum
, completed_inst
->readPC());
435 instList
.push_back(completed_inst
);
436 youngestSN
= completed_inst
->seqNum
;
439 if (!instList
.front()->isCompleted()) {
442 inst
= instList
.front();
443 instList
.pop_front();
446 if (!completed_inst
->isCompleted()) {
447 if (youngestSN
< completed_inst
->seqNum
) {
448 DPRINTF(Checker
, "Adding instruction [sn:%lli] PC:%#x to list.\n",
449 completed_inst
->seqNum
, completed_inst
->readPC());
450 instList
.push_back(completed_inst
);
451 youngestSN
= completed_inst
->seqNum
;
455 if (youngestSN
< completed_inst
->seqNum
) {
456 inst
= completed_inst
;
457 youngestSN
= completed_inst
->seqNum
;
464 // Try to check all instructions that are completed, ending if we
465 // run out of instructions to check or if an instruction is not
468 DPRINTF(Checker
, "Processing instruction [sn:%lli] PC:%#x.\n",
469 inst
->seqNum
, inst
->readPC());
470 unverifiedResult
.integer
= inst
->readIntResult();
471 unverifiedReq
= inst
->req
;
472 unverifiedMemData
= inst
->memData
;
475 Fault fault
= NoFault
;
477 // maintain $r0 semantics
478 thread
->setIntReg(ZeroReg
, 0);
480 thread
->setFloatRegDouble(ZeroReg
, 0.0);
481 #endif // TARGET_ALPHA
483 // Check if any recent PC changes match up with anything we
484 // expect to happen. This is mostly to check if traps or
485 // PC-based events have occurred in both the checker and CPU.
487 DPRINTF(Checker
, "Changed PC recently to %#x\n",
490 if (newPC
== thread
->readPC()) {
491 DPRINTF(Checker
, "Changed PC matches expected PC\n");
493 warn("%lli: Changed PC does not match expected PC, "
494 "changed: %#x, expected: %#x",
495 curTick
, thread
->readPC(), newPC
);
498 willChangePC
= false;
503 DPRINTF(Checker
, "Changed NextPC recently to %#x\n",
504 thread
->readNextPC());
505 changedNextPC
= false;
508 // Try to fetch the instruction
511 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
513 #define IFETCH_FLAGS(pc) 0
516 uint64_t fetch_PC
= thread
->readPC() & ~3;
518 // set up memory request for instruction fetch
519 memReq
= new Request(inst
->threadNumber
, fetch_PC
,
521 IFETCH_FLAGS(thread
->readPC()),
522 fetch_PC
, thread
->readCpuId(), inst
->threadNumber
);
524 bool succeeded
= translateInstReq(memReq
);
527 if (inst
->getFault() == NoFault
) {
528 // In this case the instruction was not a dummy
529 // instruction carrying an ITB fault. In the single
530 // threaded case the ITB should still be able to
531 // translate this instruction; in the SMT case it's
532 // possible that its ITB entry was kicked out.
533 warn("%lli: Instruction PC %#x was not found in the ITB!",
534 curTick
, thread
->readPC());
537 // go to the next instruction
538 thread
->setPC(thread
->readNextPC());
539 thread
->setNextPC(thread
->readNextPC() + sizeof(MachInst
));
543 // The instruction is carrying an ITB fault. Handle
544 // the fault and see if our results match the CPU on
546 fault
= inst
->getFault();
550 if (fault
== NoFault
) {
551 Packet
*pkt
= new Packet(memReq
, Packet::ReadReq
,
554 pkt
->dataStatic(&machInst
);
556 icachePort
->sendFunctional(pkt
);
560 // keep an instruction count
563 // decode the instruction
564 machInst
= gtoh(machInst
);
565 // Checks that the instruction matches what we expected it to be.
566 // Checks both the machine instruction and the PC.
569 curStaticInst
= StaticInst::decode(makeExtMI(machInst
,
573 thread
->setInst(machInst
);
574 #endif // FULL_SYSTEM
576 fault
= inst
->getFault();
579 // Discard fetch's memReq.
583 // Either the instruction was a fault and we should process the fault,
584 // or we should just go ahead execute the instruction. This assumes
585 // that the instruction is properly marked as a fault.
586 if (fault
== NoFault
) {
588 thread
->funcExeInst
++;
590 fault
= curStaticInst
->execute(this, NULL
);
592 // Checks to make sure instrution results are correct.
593 validateExecution(inst
);
595 if (curStaticInst
->isLoad()) {
600 if (fault
!= NoFault
) {
602 fault
->invoke(xcProxy
);
604 newPC
= thread
->readPC();
605 DPRINTF(Checker
, "Fault, PC is now %#x\n", newPC
);
606 #else // !FULL_SYSTEM
607 fatal("fault (%d) detected @ PC 0x%08p", fault
, thread
->readPC());
608 #endif // FULL_SYSTEM
610 #if THE_ISA != MIPS_ISA
611 // go to the next instruction
612 thread
->setPC(thread
->readNextPC());
613 thread
->setNextPC(thread
->readNextPC() + sizeof(MachInst
));
615 // go to the next instruction
616 thread
->setPC(thread
->readNextPC());
617 thread
->setNextPC(thread
->readNextNPC());
618 thread
->setNextNPC(thread
->readNextNPC() + sizeof(MachInst
));
624 // @todo: Determine if these should happen only if the
625 // instruction hasn't faulted. In the SimpleCPU case this may
626 // not be true, but in the O3 or Ozone case this may be true.
630 oldpc
= thread
->readPC();
631 system
->pcEventQueue
.service(xcProxy
);
633 } while (oldpc
!= thread
->readPC());
636 newPC
= thread
->readPC();
637 DPRINTF(Checker
, "PC Event, PC is now %#x\n", newPC
);
641 // @todo: Optionally can check all registers. (Or just those
642 // that have been modified).
650 // Continue verifying instructions if there's another completed
651 // instruction waiting to be verified.
652 if (instList
.empty()) {
654 } else if (instList
.front()->isCompleted()) {
655 inst
= instList
.front();
656 instList
.pop_front();
663 template <class DynInstPtr
>
665 Checker
<DynInstPtr
>::switchOut(Sampler
*s
)
670 template <class DynInstPtr
>
672 Checker
<DynInstPtr
>::takeOverFrom(BaseCPU
*oldCPU
)
676 template <class DynInstPtr
>
678 Checker
<DynInstPtr
>::validateInst(DynInstPtr
&inst
)
680 if (inst
->readPC() != thread
->readPC()) {
681 warn("%lli: PCs do not match! Inst: %#x, checker: %#x",
682 curTick
, inst
->readPC(), thread
->readPC());
684 warn("%lli: Changed PCs recently, may not be an error",
691 MachInst mi
= static_cast<MachInst
>(inst
->staticInst
->machInst
);
693 if (mi
!= machInst
) {
694 warn("%lli: Binary instructions do not match! Inst: %#x, "
696 curTick
, mi
, machInst
);
701 template <class DynInstPtr
>
703 Checker
<DynInstPtr
>::validateExecution(DynInstPtr
&inst
)
705 if (inst
->numDestRegs()) {
706 // @todo: Support more destination registers.
707 if (inst
->isUnverifiable()) {
708 // Unverifiable instructions assume they were executed
709 // properly by the CPU. Grab the result from the
710 // instruction and write it to the register.
711 RegIndex idx
= inst
->destRegIdx(0);
712 if (idx
< TheISA::FP_Base_DepTag
) {
713 thread
->setIntReg(idx
, inst
->readIntResult());
714 } else if (idx
< TheISA::Fpcr_DepTag
) {
715 thread
->setFloatRegBits(idx
, inst
->readIntResult());
717 thread
->setMiscReg(idx
, inst
->readIntResult());
719 } else if (result
.integer
!= inst
->readIntResult()) {
720 warn("%lli: Instruction results do not match! (Values may not "
721 "actually be integers) Inst: %#x, checker: %#x",
722 curTick
, inst
->readIntResult(), result
.integer
);
727 if (inst
->readNextPC() != thread
->readNextPC()) {
728 warn("%lli: Instruction next PCs do not match! Inst: %#x, "
730 curTick
, inst
->readNextPC(), thread
->readNextPC());
734 // Checking side effect registers can be difficult if they are not
735 // checked simultaneously with the execution of the instruction.
736 // This is because other valid instructions may have modified
737 // these registers in the meantime, and their values are not
738 // stored within the DynInst.
739 while (!miscRegIdxs
.empty()) {
740 int misc_reg_idx
= miscRegIdxs
.front();
743 if (inst
->tcBase()->readMiscReg(misc_reg_idx
) !=
744 thread
->readMiscReg(misc_reg_idx
)) {
745 warn("%lli: Misc reg idx %i (side effect) does not match! "
746 "Inst: %#x, checker: %#x",
747 curTick
, misc_reg_idx
,
748 inst
->tcBase()->readMiscReg(misc_reg_idx
),
749 thread
->readMiscReg(misc_reg_idx
));
755 template <class DynInstPtr
>
757 Checker
<DynInstPtr
>::validateState()
761 template <class DynInstPtr
>
763 Checker
<DynInstPtr
>::dumpInsts()
767 InstListIt inst_list_it
= --(instList
.end());
769 cprintf("Inst list size: %i\n", instList
.size());
771 while (inst_list_it
!= instList
.end())
773 cprintf("Instruction:%i\n",
776 cprintf("PC:%#x\n[sn:%lli]\n[tid:%i]\n"
778 (*inst_list_it
)->readPC(),
779 (*inst_list_it
)->seqNum
,
780 (*inst_list_it
)->threadNumber
,
781 (*inst_list_it
)->isCompleted());
792 //class Checker<RefCountingPtr<OzoneDynInst<OzoneImpl> > >;
795 class Checker
<RefCountingPtr
<AlphaDynInst
<AlphaSimpleImpl
> > >;