2 * Copyright (c) 2011,2013,2017-2018 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.
41 #include "cpu/checker/cpu.hh"
46 #include "arch/generic/tlb.hh"
47 #include "arch/vtophys.hh"
48 #include "cpu/base.hh"
49 #include "cpu/simple_thread.hh"
50 #include "cpu/static_inst.hh"
51 #include "cpu/thread_context.hh"
52 #include "cpu/utils.hh"
53 #include "params/CheckerCPU.hh"
54 #include "sim/full_system.hh"
57 using namespace TheISA
;
62 masterId
= systemPtr
->getMasterId(this);
65 CheckerCPU::CheckerCPU(Params
*p
)
66 : BaseCPU(p
, true), systemPtr(NULL
), icachePort(NULL
), dcachePort(NULL
),
67 tc(NULL
), thread(NULL
),
68 unverifiedReq(nullptr),
69 unverifiedMemData(nullptr)
72 curMacroStaticInst
= NULL
;
80 changedPC
= willChangePC
= false;
82 exitOnError
= p
->exitOnError
;
83 warnOnlyOnLoadError
= p
->warnOnlyOnLoadError
;
86 workload
= p
->workload
;
91 CheckerCPU::~CheckerCPU()
96 CheckerCPU::setSystem(System
*system
)
98 const Params
*p(dynamic_cast<const Params
*>(_params
));
103 thread
= new SimpleThread(this, 0, systemPtr
, itb
, dtb
,
106 thread
= new SimpleThread(this, 0, systemPtr
,
107 workload
.size() ? workload
[0] : NULL
,
108 itb
, dtb
, p
->isa
[0]);
111 tc
= thread
->getTC();
112 threadContexts
.push_back(tc
);
113 thread
->kernelStats
= NULL
;
114 // Thread should never be null after this
115 assert(thread
!= NULL
);
119 CheckerCPU::setIcachePort(MasterPort
*icache_port
)
121 icachePort
= icache_port
;
125 CheckerCPU::setDcachePort(MasterPort
*dcache_port
)
127 dcachePort
= dcache_port
;
131 CheckerCPU::serialize(ostream
&os
) const
136 CheckerCPU::unserialize(CheckpointIn
&cp
)
141 CheckerCPU::genMemFragmentRequest(Addr frag_addr
, int size
,
142 Request::Flags flags
,
143 const std::vector
<bool>& byte_enable
,
144 int& frag_size
, int& size_left
) const
146 frag_size
= std::min(
147 cacheLineSize() - addrBlockOffset(frag_addr
, cacheLineSize()),
149 size_left
-= frag_size
;
153 if (!byte_enable
.empty()) {
154 // Set up byte-enable mask for the current fragment
155 auto it_start
= byte_enable
.cbegin() + (size
- (frag_size
+
157 auto it_end
= byte_enable
.cbegin() + (size
- size_left
);
158 if (isAnyActiveElement(it_start
, it_end
)) {
159 mem_req
= std::make_shared
<Request
>(0, frag_addr
, frag_size
,
160 flags
, masterId
, thread
->pcState().instAddr(),
162 mem_req
->setByteEnable(std::vector
<bool>(it_start
, it_end
));
165 mem_req
= std::make_shared
<Request
>(0, frag_addr
, frag_size
,
166 flags
, masterId
, thread
->pcState().instAddr(),
174 CheckerCPU::readMem(Addr addr
, uint8_t *data
, unsigned size
,
175 Request::Flags flags
,
176 const std::vector
<bool>& byte_enable
)
178 assert(byte_enable
.empty() || byte_enable
.size() == size
);
180 Fault fault
= NoFault
;
181 bool checked_flags
= false;
182 bool flags_match
= true;
185 Addr frag_addr
= addr
;
187 int size_left
= size
;
190 // Need to account for multiple accesses like the Atomic and TimingSimple
192 RequestPtr mem_req
= genMemFragmentRequest(frag_addr
, size
, flags
,
193 byte_enable
, frag_size
,
196 predicate
= (mem_req
!= nullptr);
198 // translate to physical address
200 fault
= dtb
->translateFunctional(mem_req
, tc
, BaseTLB::Read
);
203 if (predicate
&& !checked_flags
&& fault
== NoFault
&& unverifiedReq
) {
204 flags_match
= checkFlags(unverifiedReq
, mem_req
->getVaddr(),
205 mem_req
->getPaddr(), mem_req
->getFlags());
206 pAddr
= mem_req
->getPaddr();
207 checked_flags
= true;
211 if (predicate
&& fault
== NoFault
&&
212 !mem_req
->getFlags().isSet(Request::NO_ACCESS
)) {
213 PacketPtr pkt
= Packet::createRead(mem_req
);
215 pkt
->dataStatic(data
);
217 if (!(mem_req
->isUncacheable() || mem_req
->isMmappedIpr())) {
218 // Access memory to see if we have the same data
219 dcachePort
->sendFunctional(pkt
);
221 // Assume the data is correct if it's an uncached access
222 memcpy(data
, unverifiedMemData
, frag_size
);
228 if (fault
!= NoFault
) {
229 if (mem_req
->isPrefetch()) {
235 //If we don't need to access a second cache line, stop now.
241 // Setup for accessing next cache line
242 frag_addr
+= frag_size
;
244 unverifiedMemData
+= frag_size
;
248 warn("%lli: Flags do not match CPU:%#x %#x %#x Checker:%#x %#x %#x\n",
249 curTick(), unverifiedReq
->getVaddr(), unverifiedReq
->getPaddr(),
250 unverifiedReq
->getFlags(), frag_addr
, pAddr
, flags
);
258 CheckerCPU::writeMem(uint8_t *data
, unsigned size
,
259 Addr addr
, Request::Flags flags
, uint64_t *res
,
260 const std::vector
<bool>& byte_enable
)
262 assert(byte_enable
.empty() || byte_enable
.size() == size
);
264 Fault fault
= NoFault
;
265 bool checked_flags
= false;
266 bool flags_match
= true;
268 static uint8_t zero_data
[64] = {};
270 Addr frag_addr
= addr
;
272 int size_left
= size
;
275 // Need to account for a multiple access like Atomic and Timing CPUs
277 RequestPtr mem_req
= genMemFragmentRequest(frag_addr
, size
, flags
,
278 byte_enable
, frag_size
,
281 predicate
= (mem_req
!= nullptr);
284 fault
= dtb
->translateFunctional(mem_req
, tc
, BaseTLB::Write
);
287 if (predicate
&& !checked_flags
&& fault
== NoFault
&& unverifiedReq
) {
288 flags_match
= checkFlags(unverifiedReq
, mem_req
->getVaddr(),
289 mem_req
->getPaddr(), mem_req
->getFlags());
290 pAddr
= mem_req
->getPaddr();
291 checked_flags
= true;
295 * We don't actually check memory for the store because there
296 * is no guarantee it has left the lsq yet, and therefore we
297 * can't verify the memory on stores without lsq snooping
298 * enabled. This is left as future work for the Checker: LSQ snooping
299 * and memory validation after stores have committed.
301 bool was_prefetch
= mem_req
->isPrefetch();
303 //If we don't need to access a second cache line, stop now.
304 if (fault
!= NoFault
|| size_left
== 0)
306 if (fault
!= NoFault
&& was_prefetch
) {
312 frag_addr
+= frag_size
;
316 warn("%lli: Flags do not match CPU:%#x %#x Checker:%#x %#x %#x\n",
317 curTick(), unverifiedReq
->getVaddr(), unverifiedReq
->getPaddr(),
318 unverifiedReq
->getFlags(), frag_addr
, pAddr
, flags
);
322 // Assume the result was the same as the one passed in. This checker
323 // doesn't check if the SC should succeed or fail, it just checks the
325 if (unverifiedReq
&& res
&& unverifiedReq
->extraDataValid())
326 *res
= unverifiedReq
->getExtraData();
328 // Entire purpose here is to make sure we are getting the
329 // same data to send to the mem system as the CPU did.
330 // Cannot check this is actually what went to memory because
331 // there stores can be in ld/st queue or coherent operations
332 // overwriting values.
333 bool extraData
= false;
335 extraData
= unverifiedReq
->extraDataValid() ?
336 unverifiedReq
->getExtraData() : true;
339 // If the request is to ZERO a cache block, there is no data to check
340 // against, but it's all zero. We need something to compare to, so use a
341 // const set of zeros.
342 if (flags
& Request::STORE_NO_DATA
) {
344 assert(sizeof(zero_data
) <= size
);
348 if (unverifiedReq
&& unverifiedMemData
&&
349 memcmp(data
, unverifiedMemData
, size
) && extraData
) {
350 warn("%lli: Store value does not match value sent to memory! "
351 "data: %#x inst_data: %#x", curTick(), data
,
360 CheckerCPU::dbg_vtophys(Addr addr
)
362 return vtophys(tc
, addr
);
366 * Checks if the flags set by the Checker and Checkee match.
369 CheckerCPU::checkFlags(const RequestPtr
&unverified_req
, Addr vAddr
,
370 Addr pAddr
, int flags
)
372 Addr unverifiedVAddr
= unverified_req
->getVaddr();
373 Addr unverifiedPAddr
= unverified_req
->getPaddr();
374 int unverifiedFlags
= unverified_req
->getFlags();
376 if (unverifiedVAddr
!= vAddr
||
377 unverifiedPAddr
!= pAddr
||
378 unverifiedFlags
!= flags
) {
386 CheckerCPU::dumpAndExit()
388 warn("%lli: Checker PC:%s",
389 curTick(), thread
->pcState());
390 panic("Checker found an error!");