gem5  v20.1.0.0
cache_blk.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-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) 2003-2005 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 
45 #ifndef __MEM_CACHE_CACHE_BLK_HH__
46 #define __MEM_CACHE_CACHE_BLK_HH__
47 
48 #include <cassert>
49 #include <cstdint>
50 #include <iosfwd>
51 #include <list>
52 #include <string>
53 
54 #include "base/printable.hh"
55 #include "base/types.hh"
57 #include "mem/packet.hh"
58 #include "mem/request.hh"
59 
63 enum CacheBlkStatusBits : unsigned {
65  BlkValid = 0x01,
67  BlkWritable = 0x02,
69  BlkReadable = 0x04,
71  BlkDirty = 0x08,
75  BlkSecure = 0x40,
78 };
79 
84 class CacheBlk : public ReplaceableEntry
85 {
86  public:
88  uint32_t task_id;
89 
99  uint8_t *data;
100 
102  typedef unsigned State;
103 
106 
112 
114  unsigned refCount;
115 
118 
124 
125  protected:
130  class Lock {
131  public:
132  ContextID contextId; // locking context
133  Addr lowAddr; // low address of lock range
134  Addr highAddr; // high address of lock range
135 
136  // check for matching execution context, and an address that
137  // is within the lock
138  bool matches(const RequestPtr &req) const
139  {
140  Addr req_low = req->getPaddr();
141  Addr req_high = req_low + req->getSize() -1;
142  return (contextId == req->contextId()) &&
143  (req_low >= lowAddr) && (req_high <= highAddr);
144  }
145 
146  // check if a request is intersecting and thus invalidating the lock
147  bool intersects(const RequestPtr &req) const
148  {
149  Addr req_low = req->getPaddr();
150  Addr req_high = req_low + req->getSize() - 1;
151 
152  return (req_low <= highAddr) && (req_high >= lowAddr);
153  }
154 
155  Lock(const RequestPtr &req)
156  : contextId(req->contextId()),
157  lowAddr(req->getPaddr()),
158  highAddr(lowAddr + req->getSize() - 1)
159  {
160  }
161  };
162 
166 
167  public:
168  CacheBlk() : data(nullptr), tickInserted(0)
169  {
170  invalidate();
171  }
172 
173  CacheBlk(const CacheBlk&) = delete;
174  CacheBlk& operator=(const CacheBlk&) = delete;
175  virtual ~CacheBlk() {};
176 
181  bool isWritable() const
182  {
183  const State needed_bits = BlkWritable | BlkValid;
184  return (status & needed_bits) == needed_bits;
185  }
186 
193  bool isReadable() const
194  {
195  const State needed_bits = BlkReadable | BlkValid;
196  return (status & needed_bits) == needed_bits;
197  }
198 
203  bool isValid() const
204  {
205  return (status & BlkValid) != 0;
206  }
207 
211  virtual void invalidate()
212  {
213  tag = MaxAddr;
215  status = 0;
216  whenReady = MaxTick;
217  refCount = 0;
219  lockList.clear();
220  }
221 
226  bool isDirty() const
227  {
228  return (status & BlkDirty) != 0;
229  }
230 
236  bool wasPrefetched() const
237  {
238  return (status & BlkHWPrefetched) != 0;
239  }
240 
245  bool isSecure() const
246  {
247  return (status & BlkSecure) != 0;
248  }
249 
253  virtual void setValid()
254  {
255  assert(!isValid());
256  status |= BlkValid;
257  }
258 
262  virtual void setSecure()
263  {
264  status |= BlkSecure;
265  }
266 
273  {
274  assert(whenReady != MaxTick);
275  return whenReady;
276  }
277 
285  void setWhenReady(const Tick tick)
286  {
287  assert(tick >= tickInserted);
288  whenReady = tick;
289  }
290 
302  virtual void insert(const Addr tag, const bool is_secure,
303  const int src_requestor_ID, const uint32_t task_ID);
304 
310  {
311  assert(pkt->isLLSC());
312  auto l = lockList.begin();
313  while (l != lockList.end()) {
314  if (l->intersects(pkt->req))
315  l = lockList.erase(l);
316  else
317  ++l;
318  }
319 
320  lockList.emplace_front(pkt->req);
321  }
322 
327  void clearLoadLocks(const RequestPtr &req)
328  {
329  auto l = lockList.begin();
330  while (l != lockList.end()) {
331  if (l->intersects(req) && l->contextId != req->contextId()) {
332  l = lockList.erase(l);
333  } else {
334  ++l;
335  }
336  }
337  }
338 
345  std::string
346  print() const override
347  {
372  unsigned state = isWritable() << 2 | isDirty() << 1 | isValid();
373  char s = '?';
374  switch (state) {
375  case 0b111: s = 'M'; break;
376  case 0b011: s = 'O'; break;
377  case 0b101: s = 'E'; break;
378  case 0b001: s = 'S'; break;
379  case 0b000: s = 'I'; break;
380  default: s = 'T'; break; // @TODO add other types
381  }
382  return csprintf("state: %x (%c) valid: %d writable: %d readable: %d "
383  "dirty: %d | tag: %#x %s", status, s,
386  }
387 
394  {
395  assert(pkt->isWrite());
396 
397  // common case
398  if (!pkt->isLLSC() && lockList.empty())
399  return true;
400 
401  const RequestPtr &req = pkt->req;
402 
403  if (pkt->isLLSC()) {
404  // it's a store conditional... have to check for matching
405  // load locked.
406  bool success = false;
407 
408  auto l = lockList.begin();
409  while (!success && l != lockList.end()) {
410  if (l->matches(pkt->req)) {
411  // it's a store conditional, and as far as the
412  // memory system can tell, the requesting
413  // context's lock is still valid.
414  success = true;
415  lockList.erase(l);
416  } else {
417  ++l;
418  }
419  }
420 
421  req->setExtraData(success ? 1 : 0);
422  // clear any intersected locks from other contexts (our LL
423  // should already have cleared them)
424  clearLoadLocks(req);
425  return success;
426  } else {
427  // a normal write, if there is any lock not from this
428  // context we clear the list, thus for a private cache we
429  // never clear locks on normal writes
430  clearLoadLocks(req);
431  return true;
432  }
433  }
434 };
435 
441 class TempCacheBlk final : public CacheBlk
442 {
443  private:
448 
449  public:
454  TempCacheBlk(unsigned size) : CacheBlk()
455  {
456  data = new uint8_t[size];
457  }
458  TempCacheBlk(const TempCacheBlk&) = delete;
459  TempCacheBlk& operator=(const TempCacheBlk&) = delete;
460  ~TempCacheBlk() { delete [] data; };
461 
465  void invalidate() override {
467 
468  _addr = MaxAddr;
469  }
470 
471  void insert(const Addr addr, const bool is_secure,
472  const int src_requestor_ID=0, const uint32_t task_ID=0)
473  override
474  {
475  // Make sure that the block has been properly invalidated
476  assert(status == 0);
477 
478  // Set block address
479  _addr = addr;
480 
481  // Set secure state
482  if (is_secure) {
483  setSecure();
484  }
485 
486  // Validate block
487  setValid();
488  }
489 
495  Addr getAddr() const
496  {
497  return _addr;
498  }
499 };
500 
508 {
510  public:
513  void print(std::ostream &o, int verbosity = 0,
514  const std::string &prefix = "") const;
515 };
516 
517 #endif //__MEM_CACHE_CACHE_BLK_HH__
ReplaceableEntry
A replaceable entry is a basic entry in a 2d table-like structure that needs to have replacement func...
Definition: replaceable_entry.hh:53
CacheBlk::isWritable
bool isWritable() const
Checks the write permissions of this block.
Definition: cache_blk.hh:181
CacheBlk::setValid
virtual void setValid()
Set valid bit.
Definition: cache_blk.hh:253
ReplaceableEntry::print
virtual std::string print() const
Prints relevant information about this entry.
Definition: replaceable_entry.hh:109
CacheBlk::Lock::highAddr
Addr highAddr
Definition: cache_blk.hh:134
CacheBlk::Lock::intersects
bool intersects(const RequestPtr &req) const
Definition: cache_blk.hh:147
CacheBlk::clearLoadLocks
void clearLoadLocks(const RequestPtr &req)
Clear the any load lock that intersect the request, and is from a different context.
Definition: cache_blk.hh:327
CacheBlk::CacheBlk
CacheBlk()
Definition: cache_blk.hh:168
CacheBlk::getWhenReady
Tick getWhenReady() const
Get tick at which block's data will be available for access.
Definition: cache_blk.hh:272
CacheBlkStatusBits
CacheBlkStatusBits
Cache block status bit assignments.
Definition: cache_blk.hh:63
CacheBlk::Lock::matches
bool matches(const RequestPtr &req) const
Definition: cache_blk.hh:138
ContextID
int ContextID
Globally unique thread context ID.
Definition: types.hh:231
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
CacheBlkPrintWrapper::~CacheBlkPrintWrapper
virtual ~CacheBlkPrintWrapper()
Definition: cache_blk.hh:512
BlkCompressed
@ BlkCompressed
block holds compressed data
Definition: cache_blk.hh:77
BlkSecure
@ BlkSecure
block holds data from the secure memory space
Definition: cache_blk.hh:75
RequestPtr
std::shared_ptr< Request > RequestPtr
Definition: request.hh:82
BlkDirty
@ BlkDirty
dirty (modified)
Definition: cache_blk.hh:71
Packet::req
RequestPtr req
A pointer to the original request.
Definition: packet.hh:340
CacheBlk::Lock::Lock
Lock(const RequestPtr &req)
Definition: cache_blk.hh:155
Packet::isLLSC
bool isLLSC() const
Definition: packet.hh:582
CacheBlk::State
unsigned State
block state: OR of CacheBlkStatusBit
Definition: cache_blk.hh:102
CacheBlk::task_id
uint32_t task_id
Task Id associated with this block.
Definition: cache_blk.hh:88
MaxAddr
const Addr MaxAddr
Definition: types.hh:166
ContextSwitchTaskId::Unknown
@ Unknown
Definition: request.hh:75
request.hh
printable.hh
packet.hh
CacheBlk::lockList
std::list< Lock > lockList
List of thread contexts that have performed a load-locked (LL) on the block since the last store.
Definition: cache_blk.hh:165
CacheBlk::isDirty
bool isDirty() const
Check to see if a block has been written.
Definition: cache_blk.hh:226
TempCacheBlk::~TempCacheBlk
~TempCacheBlk()
Definition: cache_blk.hh:460
CacheBlk::status
State status
The current status of this block.
Definition: cache_blk.hh:105
Request::invldRequestorId
@ invldRequestorId
Invalid requestor id for assertion checking only.
Definition: request.hh:255
CacheBlk::print
std::string print() const override
Pretty-print tag, set and way, and interpret state bits to readable form including mapping to a MOESI...
Definition: cache_blk.hh:346
Printable
Abstract base class for objects which support being printed to a stream for debugging.
Definition: printable.hh:44
CacheBlk::invalidate
virtual void invalidate()
Invalidate the block and clear all state.
Definition: cache_blk.hh:211
CacheBlkPrintWrapper
Simple class to provide virtual print() method on cache blocks without allocating a vtable pointer fo...
Definition: cache_blk.hh:507
base.hh
CacheBlk::isReadable
bool isReadable() const
Checks the read permissions of this block.
Definition: cache_blk.hh:193
CacheBlk::checkWrite
bool checkWrite(PacketPtr pkt)
Handle interaction of load-locked operations and stores.
Definition: cache_blk.hh:393
BlkHWPrefetched
@ BlkHWPrefetched
block was a hardware prefetch yet unaccessed
Definition: cache_blk.hh:73
TempCacheBlk::TempCacheBlk
TempCacheBlk(unsigned size)
Creates a temporary cache block, with its own storage.
Definition: cache_blk.hh:454
CacheBlk::refCount
unsigned refCount
Number of references to this block since it was brought in.
Definition: cache_blk.hh:114
CacheBlk::Lock::lowAddr
Addr lowAddr
Definition: cache_blk.hh:133
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
BlkReadable
@ BlkReadable
read permission (yes, block can be valid but not readable)
Definition: cache_blk.hh:69
CacheBlk::wasPrefetched
bool wasPrefetched() const
Check if this block was the result of a hardware prefetch, yet to be touched.
Definition: cache_blk.hh:236
TempCacheBlk::insert
void insert(const Addr addr, const bool is_secure, const int src_requestor_ID=0, const uint32_t task_ID=0) override
Set member variables when a block insertion occurs.
Definition: cache_blk.hh:471
CacheBlk::Lock::contextId
ContextID contextId
Definition: cache_blk.hh:132
BlkValid
@ BlkValid
valid, readable
Definition: cache_blk.hh:65
CacheBlk::setSecure
virtual void setSecure()
Set secure bit.
Definition: cache_blk.hh:262
TempCacheBlk::_addr
Addr _addr
Copy of the block's address, used to regenerate tempBlock's address.
Definition: cache_blk.hh:447
CacheBlk::setWhenReady
void setWhenReady(const Tick tick)
Set tick at which block's data will be available for access.
Definition: cache_blk.hh:285
CacheBlk::tag
Addr tag
Data block tag value.
Definition: cache_blk.hh:91
CacheBlk::tickInserted
Tick tickInserted
Tick on which the block was inserted in the cache.
Definition: cache_blk.hh:123
CacheBlk::isValid
bool isValid() const
Checks that a block is valid.
Definition: cache_blk.hh:203
CacheBlkPrintWrapper::print
void print(std::ostream &o, int verbosity=0, const std::string &prefix="") const
Definition: cache_blk.cc:77
BlkWritable
@ BlkWritable
write permission
Definition: cache_blk.hh:67
CacheBlk::data
uint8_t * data
Contains a copy of the data in this block for easy access.
Definition: cache_blk.hh:99
CacheBlk::insert
virtual void insert(const Addr tag, const bool is_secure, const int src_requestor_ID, const uint32_t task_ID)
Set member variables when a block insertion occurs.
Definition: cache_blk.cc:46
CacheBlk
A Basic Cache block.
Definition: cache_blk.hh:84
types.hh
TempCacheBlk::operator=
TempCacheBlk & operator=(const TempCacheBlk &)=delete
CacheBlk::trackLoadLocked
void trackLoadLocked(PacketPtr pkt)
Track the fact that a local locked was issued to the block.
Definition: cache_blk.hh:309
CacheBlkPrintWrapper::blk
CacheBlk * blk
Definition: cache_blk.hh:509
CacheBlk::whenReady
Tick whenReady
Which curTick() will this block be accessible.
Definition: cache_blk.hh:111
Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:257
addr
ip6_addr_t addr
Definition: inet.hh:423
Packet::isWrite
bool isWrite() const
Definition: packet.hh:557
CacheBlk::operator=
CacheBlk & operator=(const CacheBlk &)=delete
CacheBlkPrintWrapper::CacheBlkPrintWrapper
CacheBlkPrintWrapper(CacheBlk *_blk)
Definition: cache_blk.hh:511
TempCacheBlk::invalidate
void invalidate() override
Invalidate the block and clear all state.
Definition: cache_blk.hh:465
std::list
STL list class.
Definition: stl.hh:51
TempCacheBlk
Special instance of CacheBlk for use with tempBlk that deals with its block address regeneration.
Definition: cache_blk.hh:441
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
MipsISA::l
Bitfield< 5 > l
Definition: pra_constants.hh:320
CacheBlk::~CacheBlk
virtual ~CacheBlk()
Definition: cache_blk.hh:175
CacheBlk::srcRequestorId
int srcRequestorId
holds the source requestor ID for this block.
Definition: cache_blk.hh:117
MaxTick
const Tick MaxTick
Definition: types.hh:65
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
CacheBlk::isSecure
bool isSecure() const
Check if this block holds data from the secure memory space.
Definition: cache_blk.hh:245
CacheBlk::Lock
Represents that the indicated thread context has a "lock" on the block, in the LL/SC sense.
Definition: cache_blk.hh:130
TempCacheBlk::getAddr
Addr getAddr() const
Get block's address.
Definition: cache_blk.hh:495

Generated on Wed Sep 30 2020 14:02:12 for gem5 by doxygen 1.8.17