2f020c4a94a6c479b86408449dcdea998cab7557
[gem5.git] / src / cpu / checker / cpu.cc
1 /*
2 * Copyright (c) 2011,2013,2017-2018 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Kevin Lim
41 * Geoffrey Blake
42 */
43
44 #include "cpu/checker/cpu.hh"
45
46 #include <list>
47 #include <string>
48
49 #include "arch/generic/tlb.hh"
50 #include "arch/vtophys.hh"
51 #include "cpu/base.hh"
52 #include "cpu/simple_thread.hh"
53 #include "cpu/static_inst.hh"
54 #include "cpu/thread_context.hh"
55 #include "cpu/utils.hh"
56 #include "params/CheckerCPU.hh"
57 #include "sim/full_system.hh"
58
59 using namespace std;
60 using namespace TheISA;
61
62 void
63 CheckerCPU::init()
64 {
65 masterId = systemPtr->getMasterId(this);
66 }
67
68 CheckerCPU::CheckerCPU(Params *p)
69 : BaseCPU(p, true), systemPtr(NULL), icachePort(NULL), dcachePort(NULL),
70 tc(NULL), thread(NULL),
71 unverifiedReq(nullptr),
72 unverifiedMemData(nullptr)
73 {
74 curStaticInst = NULL;
75 curMacroStaticInst = NULL;
76
77 numInst = 0;
78 startNumInst = 0;
79 numLoad = 0;
80 startNumLoad = 0;
81 youngestSN = 0;
82
83 changedPC = willChangePC = false;
84
85 exitOnError = p->exitOnError;
86 warnOnlyOnLoadError = p->warnOnlyOnLoadError;
87 itb = p->itb;
88 dtb = p->dtb;
89 workload = p->workload;
90
91 updateOnError = true;
92 }
93
94 CheckerCPU::~CheckerCPU()
95 {
96 }
97
98 void
99 CheckerCPU::setSystem(System *system)
100 {
101 const Params *p(dynamic_cast<const Params *>(_params));
102
103 systemPtr = system;
104
105 if (FullSystem) {
106 thread = new SimpleThread(this, 0, systemPtr, itb, dtb,
107 p->isa[0], false);
108 } else {
109 thread = new SimpleThread(this, 0, systemPtr,
110 workload.size() ? workload[0] : NULL,
111 itb, dtb, p->isa[0]);
112 }
113
114 tc = thread->getTC();
115 threadContexts.push_back(tc);
116 thread->kernelStats = NULL;
117 // Thread should never be null after this
118 assert(thread != NULL);
119 }
120
121 void
122 CheckerCPU::setIcachePort(MasterPort *icache_port)
123 {
124 icachePort = icache_port;
125 }
126
127 void
128 CheckerCPU::setDcachePort(MasterPort *dcache_port)
129 {
130 dcachePort = dcache_port;
131 }
132
133 void
134 CheckerCPU::serialize(ostream &os) const
135 {
136 }
137
138 void
139 CheckerCPU::unserialize(CheckpointIn &cp)
140 {
141 }
142
143 RequestPtr
144 CheckerCPU::genMemFragmentRequest(Addr frag_addr, int size,
145 Request::Flags flags,
146 const std::vector<bool>& byte_enable,
147 int& frag_size, int& size_left) const
148 {
149 frag_size = std::min(
150 cacheLineSize() - addrBlockOffset(frag_addr, cacheLineSize()),
151 (Addr) size_left);
152 size_left -= frag_size;
153
154 RequestPtr mem_req;
155
156 if (!byte_enable.empty()) {
157 // Set up byte-enable mask for the current fragment
158 auto it_start = byte_enable.cbegin() + (size - (frag_size +
159 size_left));
160 auto it_end = byte_enable.cbegin() + (size - size_left);
161 if (isAnyActiveElement(it_start, it_end)) {
162 mem_req = std::make_shared<Request>(0, frag_addr, frag_size,
163 flags, masterId, thread->pcState().instAddr(),
164 tc->contextId());
165 mem_req->setByteEnable(std::vector<bool>(it_start, it_end));
166 }
167 } else {
168 mem_req = std::make_shared<Request>(0, frag_addr, frag_size,
169 flags, masterId, thread->pcState().instAddr(),
170 tc->contextId());
171 }
172
173 return mem_req;
174 }
175
176 Fault
177 CheckerCPU::readMem(Addr addr, uint8_t *data, unsigned size,
178 Request::Flags flags,
179 const std::vector<bool>& byte_enable)
180 {
181 assert(byte_enable.empty() || byte_enable.size() == size);
182
183 Fault fault = NoFault;
184 bool checked_flags = false;
185 bool flags_match = true;
186 Addr pAddr = 0x0;
187
188 Addr frag_addr = addr;
189 int frag_size = 0;
190 int size_left = size;
191 bool predicate;
192
193 // Need to account for multiple accesses like the Atomic and TimingSimple
194 while (1) {
195 RequestPtr mem_req = genMemFragmentRequest(frag_addr, size, flags,
196 byte_enable, frag_size,
197 size_left);
198
199 predicate = (mem_req != nullptr);
200
201 // translate to physical address
202 if (predicate) {
203 fault = dtb->translateFunctional(mem_req, tc, BaseTLB::Read);
204 }
205
206 if (predicate && !checked_flags && fault == NoFault && unverifiedReq) {
207 flags_match = checkFlags(unverifiedReq, mem_req->getVaddr(),
208 mem_req->getPaddr(), mem_req->getFlags());
209 pAddr = mem_req->getPaddr();
210 checked_flags = true;
211 }
212
213 // Now do the access
214 if (predicate && fault == NoFault &&
215 !mem_req->getFlags().isSet(Request::NO_ACCESS)) {
216 PacketPtr pkt = Packet::createRead(mem_req);
217
218 pkt->dataStatic(data);
219
220 if (!(mem_req->isUncacheable() || mem_req->isMmappedIpr())) {
221 // Access memory to see if we have the same data
222 dcachePort->sendFunctional(pkt);
223 } else {
224 // Assume the data is correct if it's an uncached access
225 memcpy(data, unverifiedMemData, frag_size);
226 }
227
228 delete pkt;
229 }
230
231 if (fault != NoFault) {
232 if (mem_req->isPrefetch()) {
233 fault = NoFault;
234 }
235 break;
236 }
237
238 //If we don't need to access a second cache line, stop now.
239 if (size_left == 0)
240 {
241 break;
242 }
243
244 // Setup for accessing next cache line
245 frag_addr += frag_size;
246 data += frag_size;
247 unverifiedMemData += frag_size;
248 }
249
250 if (!flags_match) {
251 warn("%lli: Flags do not match CPU:%#x %#x %#x Checker:%#x %#x %#x\n",
252 curTick(), unverifiedReq->getVaddr(), unverifiedReq->getPaddr(),
253 unverifiedReq->getFlags(), frag_addr, pAddr, flags);
254 handleError();
255 }
256
257 return fault;
258 }
259
260 Fault
261 CheckerCPU::writeMem(uint8_t *data, unsigned size,
262 Addr addr, Request::Flags flags, uint64_t *res,
263 const std::vector<bool>& byte_enable)
264 {
265 assert(byte_enable.empty() || byte_enable.size() == size);
266
267 Fault fault = NoFault;
268 bool checked_flags = false;
269 bool flags_match = true;
270 Addr pAddr = 0x0;
271 static uint8_t zero_data[64] = {};
272
273 Addr frag_addr = addr;
274 int frag_size = 0;
275 int size_left = size;
276 bool predicate;
277
278 // Need to account for a multiple access like Atomic and Timing CPUs
279 while (1) {
280 RequestPtr mem_req = genMemFragmentRequest(frag_addr, size, flags,
281 byte_enable, frag_size,
282 size_left);
283
284 predicate = (mem_req != nullptr);
285
286 if (predicate) {
287 fault = dtb->translateFunctional(mem_req, tc, BaseTLB::Write);
288 }
289
290 if (predicate && !checked_flags && fault == NoFault && unverifiedReq) {
291 flags_match = checkFlags(unverifiedReq, mem_req->getVaddr(),
292 mem_req->getPaddr(), mem_req->getFlags());
293 pAddr = mem_req->getPaddr();
294 checked_flags = true;
295 }
296
297 /*
298 * We don't actually check memory for the store because there
299 * is no guarantee it has left the lsq yet, and therefore we
300 * can't verify the memory on stores without lsq snooping
301 * enabled. This is left as future work for the Checker: LSQ snooping
302 * and memory validation after stores have committed.
303 */
304 bool was_prefetch = mem_req->isPrefetch();
305
306 //If we don't need to access a second cache line, stop now.
307 if (fault != NoFault || size_left == 0)
308 {
309 if (fault != NoFault && was_prefetch) {
310 fault = NoFault;
311 }
312 break;
313 }
314
315 frag_addr += frag_size;
316 }
317
318 if (!flags_match) {
319 warn("%lli: Flags do not match CPU:%#x %#x Checker:%#x %#x %#x\n",
320 curTick(), unverifiedReq->getVaddr(), unverifiedReq->getPaddr(),
321 unverifiedReq->getFlags(), frag_addr, pAddr, flags);
322 handleError();
323 }
324
325 // Assume the result was the same as the one passed in. This checker
326 // doesn't check if the SC should succeed or fail, it just checks the
327 // value.
328 if (unverifiedReq && res && unverifiedReq->extraDataValid())
329 *res = unverifiedReq->getExtraData();
330
331 // Entire purpose here is to make sure we are getting the
332 // same data to send to the mem system as the CPU did.
333 // Cannot check this is actually what went to memory because
334 // there stores can be in ld/st queue or coherent operations
335 // overwriting values.
336 bool extraData = false;
337 if (unverifiedReq) {
338 extraData = unverifiedReq->extraDataValid() ?
339 unverifiedReq->getExtraData() : true;
340 }
341
342 // If the request is to ZERO a cache block, there is no data to check
343 // against, but it's all zero. We need something to compare to, so use a
344 // const set of zeros.
345 if (flags & Request::STORE_NO_DATA) {
346 assert(!data);
347 assert(sizeof(zero_data) <= size);
348 data = zero_data;
349 }
350
351 if (unverifiedReq && unverifiedMemData &&
352 memcmp(data, unverifiedMemData, size) && extraData) {
353 warn("%lli: Store value does not match value sent to memory! "
354 "data: %#x inst_data: %#x", curTick(), data,
355 unverifiedMemData);
356 handleError();
357 }
358
359 return fault;
360 }
361
362 Addr
363 CheckerCPU::dbg_vtophys(Addr addr)
364 {
365 return vtophys(tc, addr);
366 }
367
368 /**
369 * Checks if the flags set by the Checker and Checkee match.
370 */
371 bool
372 CheckerCPU::checkFlags(const RequestPtr &unverified_req, Addr vAddr,
373 Addr pAddr, int flags)
374 {
375 Addr unverifiedVAddr = unverified_req->getVaddr();
376 Addr unverifiedPAddr = unverified_req->getPaddr();
377 int unverifiedFlags = unverified_req->getFlags();
378
379 if (unverifiedVAddr != vAddr ||
380 unverifiedPAddr != pAddr ||
381 unverifiedFlags != flags) {
382 return false;
383 }
384
385 return true;
386 }
387
388 void
389 CheckerCPU::dumpAndExit()
390 {
391 warn("%lli: Checker PC:%s",
392 curTick(), thread->pcState());
393 panic("Checker found an error!");
394 }