gem5  v20.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cpu.cc
Go to the documentation of this file.
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 
41 #include "cpu/checker/cpu.hh"
42 
43 #include <list>
44 #include <string>
45 
46 #include "arch/generic/tlb.hh"
47 #include "cpu/base.hh"
48 #include "cpu/simple_thread.hh"
49 #include "cpu/static_inst.hh"
50 #include "cpu/thread_context.hh"
51 #include "cpu/utils.hh"
52 #include "params/CheckerCPU.hh"
53 #include "sim/full_system.hh"
54 
55 using namespace std;
56 using namespace TheISA;
57 
58 void
60 {
61  masterId = systemPtr->getMasterId(this);
62 }
63 
65  : BaseCPU(p, true), systemPtr(NULL), icachePort(NULL), dcachePort(NULL),
66  tc(NULL), thread(NULL),
67  unverifiedReq(nullptr),
68  unverifiedMemData(nullptr)
69 {
70  curStaticInst = NULL;
71  curMacroStaticInst = NULL;
72 
73  numInst = 0;
74  startNumInst = 0;
75  numLoad = 0;
76  startNumLoad = 0;
77  youngestSN = 0;
78 
79  changedPC = willChangePC = false;
80 
81  exitOnError = p->exitOnError;
82  warnOnlyOnLoadError = p->warnOnlyOnLoadError;
83  itb = p->itb;
84  dtb = p->dtb;
85  workload = p->workload;
86 
87  updateOnError = true;
88 }
89 
91 {
92 }
93 
94 void
96 {
97  const Params *p(dynamic_cast<const Params *>(_params));
98 
99  systemPtr = system;
100 
101  if (FullSystem) {
102  thread = new SimpleThread(this, 0, systemPtr, itb, dtb,
103  p->isa[0], false);
104  } else {
105  thread = new SimpleThread(this, 0, systemPtr,
106  workload.size() ? workload[0] : NULL,
107  itb, dtb, p->isa[0]);
108  }
109 
110  tc = thread->getTC();
111  threadContexts.push_back(tc);
112  thread->kernelStats = NULL;
113  // Thread should never be null after this
114  assert(thread != NULL);
115 }
116 
117 void
119 {
120  icachePort = icache_port;
121 }
122 
123 void
125 {
126  dcachePort = dcache_port;
127 }
128 
129 void
130 CheckerCPU::serialize(ostream &os) const
131 {
132 }
133 
134 void
136 {
137 }
138 
141  Request::Flags flags,
142  const std::vector<bool>& byte_enable,
143  int& frag_size, int& size_left) const
144 {
145  frag_size = std::min(
146  cacheLineSize() - addrBlockOffset(frag_addr, cacheLineSize()),
147  (Addr) size_left);
148  size_left -= frag_size;
149 
150  RequestPtr mem_req;
151 
152  if (!byte_enable.empty()) {
153  // Set up byte-enable mask for the current fragment
154  auto it_start = byte_enable.cbegin() + (size - (frag_size +
155  size_left));
156  auto it_end = byte_enable.cbegin() + (size - size_left);
157  if (isAnyActiveElement(it_start, it_end)) {
158  mem_req = std::make_shared<Request>(frag_addr, frag_size,
159  flags, masterId, thread->pcState().instAddr(),
160  tc->contextId());
161  mem_req->setByteEnable(std::vector<bool>(it_start, it_end));
162  }
163  } else {
164  mem_req = std::make_shared<Request>(frag_addr, frag_size,
165  flags, masterId, thread->pcState().instAddr(),
166  tc->contextId());
167  }
168 
169  return mem_req;
170 }
171 
172 Fault
173 CheckerCPU::readMem(Addr addr, uint8_t *data, unsigned size,
174  Request::Flags flags,
175  const std::vector<bool>& byte_enable)
176 {
177  assert(byte_enable.empty() || byte_enable.size() == size);
178 
179  Fault fault = NoFault;
180  bool checked_flags = false;
181  bool flags_match = true;
182  Addr pAddr = 0x0;
183 
184  Addr frag_addr = addr;
185  int frag_size = 0;
186  int size_left = size;
187  bool predicate;
188 
189  // Need to account for multiple accesses like the Atomic and TimingSimple
190  while (1) {
191  RequestPtr mem_req = genMemFragmentRequest(frag_addr, size, flags,
192  byte_enable, frag_size,
193  size_left);
194 
195  predicate = (mem_req != nullptr);
196 
197  // translate to physical address
198  if (predicate) {
199  fault = dtb->translateFunctional(mem_req, tc, BaseTLB::Read);
200  }
201 
202  if (predicate && !checked_flags && fault == NoFault && unverifiedReq) {
203  flags_match = checkFlags(unverifiedReq, mem_req->getVaddr(),
204  mem_req->getPaddr(), mem_req->getFlags());
205  pAddr = mem_req->getPaddr();
206  checked_flags = true;
207  }
208 
209  // Now do the access
210  if (predicate && fault == NoFault &&
211  !mem_req->getFlags().isSet(Request::NO_ACCESS)) {
212  PacketPtr pkt = Packet::createRead(mem_req);
213 
214  pkt->dataStatic(data);
215 
216  if (!(mem_req->isUncacheable() || mem_req->isLocalAccess())) {
217  // Access memory to see if we have the same data
219  } else {
220  // Assume the data is correct if it's an uncached access
221  memcpy(data, unverifiedMemData, frag_size);
222  }
223 
224  delete pkt;
225  }
226 
227  if (fault != NoFault) {
228  if (mem_req->isPrefetch()) {
229  fault = NoFault;
230  }
231  break;
232  }
233 
234  //If we don't need to access a second cache line, stop now.
235  if (size_left == 0)
236  {
237  break;
238  }
239 
240  // Setup for accessing next cache line
241  frag_addr += frag_size;
242  data += frag_size;
243  unverifiedMemData += frag_size;
244  }
245 
246  if (!flags_match) {
247  warn("%lli: Flags do not match CPU:%#x %#x %#x Checker:%#x %#x %#x\n",
248  curTick(), unverifiedReq->getVaddr(), unverifiedReq->getPaddr(),
249  unverifiedReq->getFlags(), frag_addr, pAddr, flags);
250  handleError();
251  }
252 
253  return fault;
254 }
255 
256 Fault
257 CheckerCPU::writeMem(uint8_t *data, unsigned size,
258  Addr addr, Request::Flags flags, uint64_t *res,
259  const std::vector<bool>& byte_enable)
260 {
261  assert(byte_enable.empty() || byte_enable.size() == size);
262 
263  Fault fault = NoFault;
264  bool checked_flags = false;
265  bool flags_match = true;
266  Addr pAddr = 0x0;
267  static uint8_t zero_data[64] = {};
268 
269  Addr frag_addr = addr;
270  int frag_size = 0;
271  int size_left = size;
272  bool predicate;
273 
274  // Need to account for a multiple access like Atomic and Timing CPUs
275  while (1) {
276  RequestPtr mem_req = genMemFragmentRequest(frag_addr, size, flags,
277  byte_enable, frag_size,
278  size_left);
279 
280  predicate = (mem_req != nullptr);
281 
282  if (predicate) {
283  fault = dtb->translateFunctional(mem_req, tc, BaseTLB::Write);
284  }
285 
286  if (predicate && !checked_flags && fault == NoFault && unverifiedReq) {
287  flags_match = checkFlags(unverifiedReq, mem_req->getVaddr(),
288  mem_req->getPaddr(), mem_req->getFlags());
289  pAddr = mem_req->getPaddr();
290  checked_flags = true;
291  }
292 
293  /*
294  * We don't actually check memory for the store because there
295  * is no guarantee it has left the lsq yet, and therefore we
296  * can't verify the memory on stores without lsq snooping
297  * enabled. This is left as future work for the Checker: LSQ snooping
298  * and memory validation after stores have committed.
299  */
300  bool was_prefetch = mem_req->isPrefetch();
301 
302  //If we don't need to access a second cache line, stop now.
303  if (fault != NoFault || size_left == 0)
304  {
305  if (fault != NoFault && was_prefetch) {
306  fault = NoFault;
307  }
308  break;
309  }
310 
311  frag_addr += frag_size;
312  }
313 
314  if (!flags_match) {
315  warn("%lli: Flags do not match CPU:%#x %#x Checker:%#x %#x %#x\n",
316  curTick(), unverifiedReq->getVaddr(), unverifiedReq->getPaddr(),
317  unverifiedReq->getFlags(), frag_addr, pAddr, flags);
318  handleError();
319  }
320 
321  // Assume the result was the same as the one passed in. This checker
322  // doesn't check if the SC should succeed or fail, it just checks the
323  // value.
324  if (unverifiedReq && res && unverifiedReq->extraDataValid())
325  *res = unverifiedReq->getExtraData();
326 
327  // Entire purpose here is to make sure we are getting the
328  // same data to send to the mem system as the CPU did.
329  // Cannot check this is actually what went to memory because
330  // there stores can be in ld/st queue or coherent operations
331  // overwriting values.
332  bool extraData = false;
333  if (unverifiedReq) {
334  extraData = unverifiedReq->extraDataValid() ?
335  unverifiedReq->getExtraData() : true;
336  }
337 
338  // If the request is to ZERO a cache block, there is no data to check
339  // against, but it's all zero. We need something to compare to, so use a
340  // const set of zeros.
341  if (flags & Request::STORE_NO_DATA) {
342  assert(!data);
343  assert(sizeof(zero_data) <= size);
344  data = zero_data;
345  }
346 
348  memcmp(data, unverifiedMemData, size) && extraData) {
349  warn("%lli: Store value does not match value sent to memory! "
350  "data: %#x inst_data: %#x", curTick(), data,
352  handleError();
353  }
354 
355  return fault;
356 }
357 
361 bool
362 CheckerCPU::checkFlags(const RequestPtr &unverified_req, Addr vAddr,
363  Addr pAddr, int flags)
364 {
365  Addr unverifiedVAddr = unverified_req->getVaddr();
366  Addr unverifiedPAddr = unverified_req->getPaddr();
367  int unverifiedFlags = unverified_req->getFlags();
368 
369  if (unverifiedVAddr != vAddr ||
370  unverifiedPAddr != pAddr ||
371  unverifiedFlags != flags) {
372  return false;
373  }
374 
375  return true;
376 }
377 
378 void
380 {
381  warn("%lli: Checker PC:%s",
382  curTick(), thread->pcState());
383  panic("Checker found an error!");
384 }
A MasterPort is a specialisation of a BaseMasterPort, which implements the default protocol for the t...
Definition: port.hh:71
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:163
RequestPtr unverifiedReq
Definition: cpu.hh:598
Addr addrBlockOffset(Addr addr, Addr block_size)
Calculates the offset of a given address wrt aligned fixed-size blocks.
Definition: utils.hh:50
BaseCPUParams Params
Definition: base.hh:306
decltype(nullptr) constexpr NoFault
Definition: types.hh:243
bool changedPC
Definition: cpu.hh:601
std::shared_ptr< Request > RequestPtr
Definition: request.hh:81
ip6_addr_t addr
Definition: inet.hh:330
bool checkFlags(const RequestPtr &unverified_req, Addr vAddr, Addr pAddr, int flags)
Checks if the flags set by the Checker and Checkee match.
Definition: cpu.cc:362
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition: root.cc:132
TheISA::PCState pcState() const override
The SimpleThread object provides a combination of the ThreadState object and the ThreadContext interf...
System * systemPtr
Definition: cpu.hh:128
Definition: system.hh:72
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
System * system
Definition: base.hh:382
Definition: cprintf.cc:40
bool willChangePC
Definition: cpu.hh:602
StaticInstPtr curMacroStaticInst
Definition: cpu.hh:143
void setDcachePort(MasterPort *dcache_port)
Definition: cpu.cc:124
Bitfield< 17 > os
Definition: misc.hh:803
bool updateOnError
Definition: cpu.hh:605
void dataStatic(T *p)
Set the data pointer to the following value that should not be freed.
Definition: packet.hh:1034
std::vector< ThreadContext * > threadContexts
Definition: base.hh:263
Tick curTick()
The current simulated tick.
Definition: core.hh:44
ThreadContext * tc
Definition: cpu.hh:133
MasterID masterId
id attached to all issued requests
Definition: cpu.hh:92
RequestPtr genMemFragmentRequest(Addr frag_addr, int size, Request::Flags flags, const std::vector< bool > &byte_enable, int &frag_size, int &size_left) const
Helper function used to generate the request for a single fragment of a memory access.
Definition: cpu.cc:140
Kernel::Statistics * kernelStats
uint8_t * unverifiedMemData
Definition: cpu.hh:599
bool exitOnError
Definition: cpu.hh:604
static const FlagsType STORE_NO_DATA
Definition: request.hh:196
Counter startNumInst
Definition: cpu.hh:147
Fault writeMem(uint8_t *data, unsigned size, Addr addr, Request::Flags flags, uint64_t *res, const std::vector< bool > &byte_enable=std::vector< bool >()) override
Definition: cpu.cc:257
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
ThreadContext * getTC()
Returns the pointer to this SimpleThread&#39;s ThreadContext.
static PacketPtr createRead(const RequestPtr &req)
Constructor-like methods that return Packets based on Request objects.
Definition: packet.hh:907
A Packet is used to encapsulate a transfer between two objects in the memory system (e...
Definition: packet.hh:249
SimpleThread * thread
Definition: cpu.hh:154
Fault readMem(Addr addr, uint8_t *data, unsigned size, Request::Flags flags, const std::vector< bool > &byte_enable=std::vector< bool >()) override
Definition: cpu.cc:173
void serialize(CheckpointOut &cp) const override
Serialize this object to the given output stream.
Definition: cpu.cc:130
unsigned int cacheLineSize() const
Get the cache line size of the system.
Definition: base.hh:387
Counter startNumLoad
Definition: cpu.hh:171
The request should not cause a memory access.
Definition: request.hh:134
void setIcachePort(MasterPort *icache_port)
Definition: cpu.cc:118
Counter numInst
Definition: cpu.hh:146
MasterPort * icachePort
Definition: cpu.hh:130
std::vector< Process * > workload
Definition: cpu.hh:126
void setSystem(System *system)
Definition: cpu.cc:95
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:111
virtual ~CheckerCPU()
Definition: cpu.cc:90
bool warnOnlyOnLoadError
Definition: cpu.hh:606
virtual ContextID contextId() const =0
bool isAnyActiveElement(const std::vector< bool >::const_iterator &it_start, const std::vector< bool >::const_iterator &it_end)
Test if there is any active element in an enablement range.
Definition: utils.hh:86
Counter numLoad
Definition: cpu.hh:170
BaseTLB * itb
Definition: cpu.hh:135
void sendFunctional(PacketPtr pkt) const
Send a functional request packet, where the data is instantly updated everywhere in the memory system...
Definition: port.hh:435
#define warn(...)
Definition: logging.hh:208
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: cpu.cc:59
CheckerCPU(Params *p)
Definition: cpu.cc:64
Bitfield< 0 > p
StaticInstPtr curStaticInst
Definition: cpu.hh:142
const char data[]
std::shared_ptr< FaultBase > Fault
Definition: types.hh:238
BaseTLB * dtb
Definition: cpu.hh:136
InstSeqNum youngestSN
Definition: cpu.hh:608
void unserialize(CheckpointIn &cp) override
Reconstruct the state of this object from a checkpoint.
Definition: cpu.cc:135
MasterPort * dcachePort
Definition: cpu.hh:131
void dumpAndExit()
Definition: cpu.cc:379
void handleError()
Definition: cpu.hh:583
virtual Fault translateFunctional(const RequestPtr &req, ThreadContext *tc, Mode mode)
Definition: tlb.hh:96

Generated on Thu May 28 2020 16:11:00 for gem5 by doxygen 1.8.13