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

Generated on Tue Jun 22 2021 15:28:19 for gem5 by doxygen 1.8.17