gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Erik Hallnor
41  * Andreas Sandberg
42  */
43 
48 #ifndef __MEM_CACHE_CACHE_BLK_HH__
49 #define __MEM_CACHE_CACHE_BLK_HH__
50 
51 #include <cassert>
52 #include <cstdint>
53 #include <iosfwd>
54 #include <list>
55 #include <string>
56 
57 #include "base/printable.hh"
58 #include "base/types.hh"
60 #include "mem/packet.hh"
61 #include "mem/request.hh"
62 
66 enum CacheBlkStatusBits : unsigned {
68  BlkValid = 0x01,
70  BlkWritable = 0x02,
72  BlkReadable = 0x04,
74  BlkDirty = 0x08,
78  BlkSecure = 0x40,
81 };
82 
87 class CacheBlk : public ReplaceableEntry
88 {
89  public:
91  uint32_t task_id;
92 
102  uint8_t *data;
103 
105  typedef unsigned State;
106 
108  State status;
109 
115 
117  unsigned refCount;
118 
121 
127 
128  protected:
133  class Lock {
134  public:
135  ContextID contextId; // locking context
136  Addr lowAddr; // low address of lock range
137  Addr highAddr; // high address of lock range
138 
139  // check for matching execution context, and an address that
140  // is within the lock
141  bool matches(const RequestPtr &req) const
142  {
143  Addr req_low = req->getPaddr();
144  Addr req_high = req_low + req->getSize() -1;
145  return (contextId == req->contextId()) &&
146  (req_low >= lowAddr) && (req_high <= highAddr);
147  }
148 
149  // check if a request is intersecting and thus invalidating the lock
150  bool intersects(const RequestPtr &req) const
151  {
152  Addr req_low = req->getPaddr();
153  Addr req_high = req_low + req->getSize() - 1;
154 
155  return (req_low <= highAddr) && (req_high >= lowAddr);
156  }
157 
158  Lock(const RequestPtr &req)
159  : contextId(req->contextId()),
160  lowAddr(req->getPaddr()),
161  highAddr(lowAddr + req->getSize() - 1)
162  {
163  }
164  };
165 
169 
170  public:
171  CacheBlk() : data(nullptr), tickInserted(0)
172  {
173  invalidate();
174  }
175 
176  CacheBlk(const CacheBlk&) = delete;
177  CacheBlk& operator=(const CacheBlk&) = delete;
178  virtual ~CacheBlk() {};
179 
184  bool isWritable() const
185  {
186  const State needed_bits = BlkWritable | BlkValid;
187  return (status & needed_bits) == needed_bits;
188  }
189 
196  bool isReadable() const
197  {
198  const State needed_bits = BlkReadable | BlkValid;
199  return (status & needed_bits) == needed_bits;
200  }
201 
206  bool isValid() const
207  {
208  return (status & BlkValid) != 0;
209  }
210 
214  virtual void invalidate()
215  {
216  tag = MaxAddr;
218  status = 0;
219  whenReady = MaxTick;
220  refCount = 0;
221  srcMasterId = Request::invldMasterId;
222  lockList.clear();
223  }
224 
229  bool isDirty() const
230  {
231  return (status & BlkDirty) != 0;
232  }
233 
239  bool wasPrefetched() const
240  {
241  return (status & BlkHWPrefetched) != 0;
242  }
243 
248  bool isSecure() const
249  {
250  return (status & BlkSecure) != 0;
251  }
252 
256  virtual void setValid()
257  {
258  assert(!isValid());
259  status |= BlkValid;
260  }
261 
265  virtual void setSecure()
266  {
267  status |= BlkSecure;
268  }
269 
276  {
277  assert(whenReady != MaxTick);
278  return whenReady;
279  }
280 
288  void setWhenReady(const Tick tick)
289  {
290  assert(tick >= tickInserted);
291  whenReady = tick;
292  }
293 
305  virtual void insert(const Addr tag, const bool is_secure,
306  const int src_master_ID, const uint32_t task_ID);
307 
313  {
314  assert(pkt->isLLSC());
315  auto l = lockList.begin();
316  while (l != lockList.end()) {
317  if (l->intersects(pkt->req))
318  l = lockList.erase(l);
319  else
320  ++l;
321  }
322 
323  lockList.emplace_front(pkt->req);
324  }
325 
330  void clearLoadLocks(const RequestPtr &req)
331  {
332  auto l = lockList.begin();
333  while (l != lockList.end()) {
334  if (l->intersects(req) && l->contextId != req->contextId()) {
335  l = lockList.erase(l);
336  } else {
337  ++l;
338  }
339  }
340  }
341 
348  std::string
349  print() const override
350  {
375  unsigned state = isWritable() << 2 | isDirty() << 1 | isValid();
376  char s = '?';
377  switch (state) {
378  case 0b111: s = 'M'; break;
379  case 0b011: s = 'O'; break;
380  case 0b101: s = 'E'; break;
381  case 0b001: s = 'S'; break;
382  case 0b000: s = 'I'; break;
383  default: s = 'T'; break; // @TODO add other types
384  }
385  return csprintf("state: %x (%c) valid: %d writable: %d readable: %d "
386  "dirty: %d | tag: %#x %s", status, s,
387  isValid(), isWritable(), isReadable(), isDirty(), tag,
389  }
390 
397  {
398  assert(pkt->isWrite());
399 
400  // common case
401  if (!pkt->isLLSC() && lockList.empty())
402  return true;
403 
404  const RequestPtr &req = pkt->req;
405 
406  if (pkt->isLLSC()) {
407  // it's a store conditional... have to check for matching
408  // load locked.
409  bool success = false;
410 
411  auto l = lockList.begin();
412  while (!success && l != lockList.end()) {
413  if (l->matches(pkt->req)) {
414  // it's a store conditional, and as far as the
415  // memory system can tell, the requesting
416  // context's lock is still valid.
417  success = true;
418  lockList.erase(l);
419  } else {
420  ++l;
421  }
422  }
423 
424  req->setExtraData(success ? 1 : 0);
425  // clear any intersected locks from other contexts (our LL
426  // should already have cleared them)
427  clearLoadLocks(req);
428  return success;
429  } else {
430  // a normal write, if there is any lock not from this
431  // context we clear the list, thus for a private cache we
432  // never clear locks on normal writes
433  clearLoadLocks(req);
434  return true;
435  }
436  }
437 };
438 
444 class TempCacheBlk final : public CacheBlk
445 {
446  private:
451 
452  public:
457  TempCacheBlk(unsigned size) : CacheBlk()
458  {
459  data = new uint8_t[size];
460  }
461  TempCacheBlk(const TempCacheBlk&) = delete;
462  TempCacheBlk& operator=(const TempCacheBlk&) = delete;
463  ~TempCacheBlk() { delete [] data; };
464 
468  void invalidate() override {
470 
471  _addr = MaxAddr;
472  }
473 
474  void insert(const Addr addr, const bool is_secure,
475  const int src_master_ID=0, const uint32_t task_ID=0) override
476  {
477  // Make sure that the block has been properly invalidated
478  assert(status == 0);
479 
480  // Set block address
481  _addr = addr;
482 
483  // Set secure state
484  if (is_secure) {
485  setSecure();
486  }
487 
488  // Validate block
489  setValid();
490  }
491 
497  Addr getAddr() const
498  {
499  return _addr;
500  }
501 };
502 
510 {
512  public:
513  CacheBlkPrintWrapper(CacheBlk *_blk) : blk(_blk) {}
515  void print(std::ostream &o, int verbosity = 0,
516  const std::string &prefix = "") const;
517 };
518 
519 #endif //__MEM_CACHE_CACHE_BLK_HH__
void insert(const Addr addr, const bool is_secure, const int src_master_ID=0, const uint32_t task_ID=0) override
Set member variables when a block insertion occurs.
Definition: cache_blk.hh:474
Special instance of CacheBlk for use with tempBlk that deals with its block address regeneration...
Definition: cache_blk.hh:444
State status
The current status of this block.
Definition: cache_blk.hh:108
bool isValid() const
Checks that a block is valid.
Definition: cache_blk.hh:206
const Addr MaxAddr
Definition: types.hh:166
Tick whenReady
Which curTick() will this block be accessible.
Definition: cache_blk.hh:114
bool isWritable() const
Checks the write permissions of this block.
Definition: cache_blk.hh:184
virtual void setValid()
Set valid bit.
Definition: cache_blk.hh:256
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:349
Addr tag
Data block tag value.
Definition: cache_blk.hh:94
Declaration of a request, the overall memory request consisting of the parts of the request that are ...
std::shared_ptr< Request > RequestPtr
Definition: request.hh:83
ip6_addr_t addr
Definition: inet.hh:335
void invalidate() override
Invalidate the block and clear all state.
Definition: cache_blk.hh:468
Simple class to provide virtual print() method on cache blocks without allocating a vtable pointer fo...
Definition: cache_blk.hh:509
virtual ~CacheBlk()
Definition: cache_blk.hh:178
unsigned refCount
Number of references to this block since it was brought in.
Definition: cache_blk.hh:117
ContextID contextId
Definition: cache_blk.hh:135
block holds compressed data
Definition: cache_blk.hh:80
bool isWrite() const
Definition: packet.hh:529
virtual void setSecure()
Set secure bit.
Definition: cache_blk.hh:265
bool intersects(const RequestPtr &req) const
Definition: cache_blk.hh:150
void clearLoadLocks(const RequestPtr &req)
Clear the any load lock that intersect the request, and is from a different context.
Definition: cache_blk.hh:330
CacheBlkPrintWrapper(CacheBlk *_blk)
Definition: cache_blk.hh:513
block was a hardware prefetch yet unaccessed
Definition: cache_blk.hh:76
RequestPtr req
A pointer to the original request.
Definition: packet.hh:327
Tick tickInserted
Tick on which the block was inserted in the cache.
Definition: cache_blk.hh:126
const Tick MaxTick
Definition: types.hh:65
A Basic Cache block.
Definition: cache_blk.hh:87
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:162
bool checkWrite(PacketPtr pkt)
Handle interaction of load-locked operations and stores.
Definition: cache_blk.hh:396
void trackLoadLocked(PacketPtr pkt)
Track the fact that a local locked was issued to the block.
Definition: cache_blk.hh:312
Bitfield< 4 > s
virtual void insert(const Addr tag, const bool is_secure, const int src_master_ID, const uint32_t task_ID)
Set member variables when a block insertion occurs.
Definition: cache_blk.cc:46
uint64_t Tick
Tick count type.
Definition: types.hh:63
dirty (modified)
Definition: cache_blk.hh:74
unsigned State
block state: OR of CacheBlkStatusBit
Definition: cache_blk.hh:105
virtual std::string print() const
Prints relevant information about this entry.
bool isSecure() const
Check if this block holds data from the secure memory space.
Definition: cache_blk.hh:248
void setWhenReady(const Tick tick)
Set tick at which block&#39;s data will be available for access.
Definition: cache_blk.hh:288
STL list class.
Definition: stl.hh:54
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
bool matches(const RequestPtr &req) const
Definition: cache_blk.hh:141
A Packet is used to encapsulate a transfer between two objects in the memory system (e...
Definition: packet.hh:255
bool isReadable() const
Checks the read permissions of this block.
Definition: cache_blk.hh:196
Abstract base class for objects which support being printed to a stream for debugging.
Definition: printable.hh:44
Represents that the indicated thread context has a "lock" on the block, in the LL/SC sense...
Definition: cache_blk.hh:133
write permission
Definition: cache_blk.hh:70
CacheBlk & operator=(const CacheBlk &)=delete
bool isLLSC() const
Definition: packet.hh:554
CacheBlkStatusBits
Cache block status bit assignments.
Definition: cache_blk.hh:66
Declaration of the Packet class.
int srcMasterId
holds the source requestor ID for this block.
Definition: cache_blk.hh:120
TempCacheBlk(unsigned size)
Creates a temporary cache block, with its own storage.
Definition: cache_blk.hh:457
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:168
Invalid master id for assertion checking only.
Definition: request.hh:219
Addr getAddr() const
Get block&#39;s address.
Definition: cache_blk.hh:497
A replaceable entry is a basic entry in a 2d table-like structure that needs to have replacement func...
uint32_t task_id
Task Id associated with this block.
Definition: cache_blk.hh:91
Lock(const RequestPtr &req)
Definition: cache_blk.hh:158
valid, readable
Definition: cache_blk.hh:68
bool wasPrefetched() const
Check if this block was the result of a hardware prefetch, yet to be touched.
Definition: cache_blk.hh:239
block holds data from the secure memory space
Definition: cache_blk.hh:78
Tick getWhenReady() const
Get tick at which block&#39;s data will be available for access.
Definition: cache_blk.hh:275
Addr _addr
Copy of the block&#39;s address, used to regenerate tempBlock&#39;s address.
Definition: cache_blk.hh:450
read permission (yes, block can be valid but not readable)
Definition: cache_blk.hh:72
Bitfield< 5 > l
uint8_t * data
Contains a copy of the data in this block for easy access.
Definition: cache_blk.hh:102
int ContextID
Globally unique thread context ID.
Definition: types.hh:231
virtual ~CacheBlkPrintWrapper()
Definition: cache_blk.hh:514
virtual void invalidate()
Invalidate the block and clear all state.
Definition: cache_blk.hh:214
bool isDirty() const
Check to see if a block has been written.
Definition: cache_blk.hh:229

Generated on Fri Feb 28 2020 16:27:01 for gem5 by doxygen 1.8.13