gem5  v19.0.0.0
abstract_mem.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012,2017-2019 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) 2001-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: Ron Dreslinski
41  * Ali Saidi
42  * Andreas Hansson
43  */
44 
45 #include "mem/abstract_mem.hh"
46 
47 #include <vector>
48 
49 #include "arch/locked_mem.hh"
50 #include "cpu/base.hh"
51 #include "cpu/thread_context.hh"
52 #include "debug/LLSC.hh"
53 #include "debug/MemoryAccess.hh"
54 #include "mem/packet_access.hh"
55 #include "sim/system.hh"
56 
57 using namespace std;
58 
60  ClockedObject(p), range(params()->range), pmemAddr(NULL),
61  backdoor(params()->range, nullptr,
62  (MemBackdoor::Flags)(MemBackdoor::Readable |
63  MemBackdoor::Writeable)),
64  confTableReported(p->conf_table_reported), inAddrMap(p->in_addr_map),
65  kvmMap(p->kvm_map), _system(NULL),
66  stats(*this)
67 {
68 }
69 
70 void
72 {
73  assert(system());
74 
75  if (size() % _system->getPageBytes() != 0)
76  panic("Memory Size not divisible by page size\n");
77 }
78 
79 void
81 {
82  // If there was an existing backdoor, let everybody know it's going away.
83  if (backdoor.ptr())
85 
86  // The back door can't handle interleaved memory.
87  backdoor.ptr(range.interleaved() ? nullptr : pmem_addr);
88 
89  pmemAddr = pmem_addr;
90 }
91 
93  : Stats::Group(&_mem), mem(_mem),
94  bytesRead(this, "bytes_read",
95  "Number of bytes read from this memory"),
96  bytesInstRead(this, "bytes_inst_read",
97  "Number of instructions bytes read from this memory"),
98  bytesWritten(this, "bytes_written",
99  "Number of bytes written to this memory"),
100  numReads(this, "num_reads",
101  "Number of read requests responded to by this memory"),
102  numWrites(this, "num_writes",
103  "Number of write requests responded to by this memory"),
104  numOther(this, "num_other",
105  "Number of other requests responded to by this memory"),
106  bwRead(this, "bw_read",
107  "Total read bandwidth from this memory (bytes/s)"),
108  bwInstRead(this, "bw_inst_read",
109  "Instruction read bandwidth from this memory (bytes/s)"),
110  bwWrite(this, "bw_write",
111  "Write bandwidth from this memory (bytes/s)"),
112  bwTotal(this, "bw_total",
113  "Total bandwidth to/from this memory (bytes/s)")
114 {
115 }
116 
117 void
119 {
120  using namespace Stats;
121 
123 
124  System *sys = mem.system();
125  assert(sys);
126  const auto max_masters = sys->maxMasters();
127 
128  bytesRead
129  .init(max_masters)
130  .flags(total | nozero | nonan)
131  ;
132  for (int i = 0; i < max_masters; i++) {
134  }
135 
137  .init(max_masters)
138  .flags(total | nozero | nonan)
139  ;
140  for (int i = 0; i < max_masters; i++) {
142  }
143 
145  .init(max_masters)
146  .flags(total | nozero | nonan)
147  ;
148  for (int i = 0; i < max_masters; i++) {
150  }
151 
152  numReads
153  .init(max_masters)
154  .flags(total | nozero | nonan)
155  ;
156  for (int i = 0; i < max_masters; i++) {
157  numReads.subname(i, sys->getMasterName(i));
158  }
159 
160  numWrites
161  .init(max_masters)
162  .flags(total | nozero | nonan)
163  ;
164  for (int i = 0; i < max_masters; i++) {
166  }
167 
168  numOther
169  .init(max_masters)
170  .flags(total | nozero | nonan)
171  ;
172  for (int i = 0; i < max_masters; i++) {
173  numOther.subname(i, sys->getMasterName(i));
174  }
175 
176  bwRead
177  .precision(0)
178  .prereq(bytesRead)
179  .flags(total | nozero | nonan)
180  ;
181  for (int i = 0; i < max_masters; i++) {
182  bwRead.subname(i, sys->getMasterName(i));
183  }
184 
185  bwInstRead
186  .precision(0)
188  .flags(total | nozero | nonan)
189  ;
190  for (int i = 0; i < max_masters; i++) {
192  }
193 
194  bwWrite
195  .precision(0)
197  .flags(total | nozero | nonan)
198  ;
199  for (int i = 0; i < max_masters; i++) {
200  bwWrite.subname(i, sys->getMasterName(i));
201  }
202 
203  bwTotal
204  .precision(0)
205  .prereq(bwTotal)
206  .flags(total | nozero | nonan)
207  ;
208  for (int i = 0; i < max_masters; i++) {
209  bwTotal.subname(i, sys->getMasterName(i));
210  }
211 
216 }
217 
218 AddrRange
220 {
221  return range;
222 }
223 
224 // Add load-locked to tracking list. Should only be called if the
225 // operation is a load and the LLSC flag is set.
226 void
228 {
229  const RequestPtr &req = pkt->req;
230  Addr paddr = LockedAddr::mask(req->getPaddr());
231 
232  // first we check if we already have a locked addr for this
233  // xc. Since each xc only gets one, we just update the
234  // existing record with the new address.
236 
237  for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
238  if (i->matchesContext(req)) {
239  DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
240  req->contextId(), paddr);
241  i->addr = paddr;
242  return;
243  }
244  }
245 
246  // no record for this xc: need to allocate a new one
247  DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
248  req->contextId(), paddr);
249  lockedAddrList.push_front(LockedAddr(req));
250 }
251 
252 
253 // Called on *writes* only... both regular stores and
254 // store-conditional operations. Check for conventional stores which
255 // conflict with locked addresses, and for success/failure of store
256 // conditionals.
257 bool
259 {
260  const RequestPtr &req = pkt->req;
261  Addr paddr = LockedAddr::mask(req->getPaddr());
262  bool isLLSC = pkt->isLLSC();
263 
264  // Initialize return value. Non-conditional stores always
265  // succeed. Assume conditional stores will fail until proven
266  // otherwise.
267  bool allowStore = !isLLSC;
268 
269  // Iterate over list. Note that there could be multiple matching records,
270  // as more than one context could have done a load locked to this location.
271  // Only remove records when we succeed in finding a record for (xc, addr);
272  // then, remove all records with this address. Failed store-conditionals do
273  // not blow unrelated reservations.
275 
276  if (isLLSC) {
277  while (i != lockedAddrList.end()) {
278  if (i->addr == paddr && i->matchesContext(req)) {
279  // it's a store conditional, and as far as the memory system can
280  // tell, the requesting context's lock is still valid.
281  DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
282  req->contextId(), paddr);
283  allowStore = true;
284  break;
285  }
286  // If we didn't find a match, keep searching! Someone else may well
287  // have a reservation on this line here but we may find ours in just
288  // a little while.
289  i++;
290  }
291  req->setExtraData(allowStore ? 1 : 0);
292  }
293  // LLSCs that succeeded AND non-LLSC stores both fall into here:
294  if (allowStore) {
295  // We write address paddr. However, there may be several entries with a
296  // reservation on this address (for other contextIds) and they must all
297  // be removed.
298  i = lockedAddrList.begin();
299  while (i != lockedAddrList.end()) {
300  if (i->addr == paddr) {
301  DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
302  i->contextId, paddr);
303  ContextID owner_cid = i->contextId;
304  assert(owner_cid != InvalidContextID);
305  ContextID requester_cid = req->hasContextId() ?
306  req->contextId() :
308  if (owner_cid != requester_cid) {
309  ThreadContext* ctx = system()->getThreadContext(owner_cid);
311  }
312  i = lockedAddrList.erase(i);
313  } else {
314  i++;
315  }
316  }
317  }
318 
319  return allowStore;
320 }
321 
322 #if TRACING_ON
323 static inline void
324 tracePacket(System *sys, const char *label, PacketPtr pkt)
325 {
326  int size = pkt->getSize();
327 #if THE_ISA != NULL_ISA
328  if (size == 1 || size == 2 || size == 4 || size == 8) {
329  DPRINTF(MemoryAccess,"%s from %s of size %i on address %#x data "
330  "%#x %c\n", label, sys->getMasterName(pkt->req->masterId()),
331  size, pkt->getAddr(), pkt->getUintX(TheISA::GuestByteOrder),
332  pkt->req->isUncacheable() ? 'U' : 'C');
333  return;
334  }
335 #endif
336  DPRINTF(MemoryAccess, "%s from %s of size %i on address %#x %c\n",
337  label, sys->getMasterName(pkt->req->masterId()),
338  size, pkt->getAddr(), pkt->req->isUncacheable() ? 'U' : 'C');
339  DDUMP(MemoryAccess, pkt->getConstPtr<uint8_t>(), pkt->getSize());
340 }
341 
342 # define TRACE_PACKET(A) tracePacket(system(), A, pkt)
343 #else
344 # define TRACE_PACKET(A)
345 #endif
346 
347 void
349 {
350  if (pkt->cacheResponding()) {
351  DPRINTF(MemoryAccess, "Cache responding to %#llx: not responding\n",
352  pkt->getAddr());
353  return;
354  }
355 
356  if (pkt->cmd == MemCmd::CleanEvict || pkt->cmd == MemCmd::WritebackClean) {
357  DPRINTF(MemoryAccess, "CleanEvict on 0x%x: not responding\n",
358  pkt->getAddr());
359  return;
360  }
361 
362  assert(pkt->getAddrRange().isSubset(range));
363 
364  uint8_t *host_addr = toHostAddr(pkt->getAddr());
365 
366  if (pkt->cmd == MemCmd::SwapReq) {
367  if (pkt->isAtomicOp()) {
368  if (pmemAddr) {
369  pkt->setData(host_addr);
370  (*(pkt->getAtomicOp()))(host_addr);
371  }
372  } else {
373  std::vector<uint8_t> overwrite_val(pkt->getSize());
374  uint64_t condition_val64;
375  uint32_t condition_val32;
376 
377  panic_if(!pmemAddr, "Swap only works if there is real memory " \
378  "(i.e. null=False)");
379 
380  bool overwrite_mem = true;
381  // keep a copy of our possible write value, and copy what is at the
382  // memory address into the packet
383  pkt->writeData(&overwrite_val[0]);
384  pkt->setData(host_addr);
385 
386  if (pkt->req->isCondSwap()) {
387  if (pkt->getSize() == sizeof(uint64_t)) {
388  condition_val64 = pkt->req->getExtraData();
389  overwrite_mem = !std::memcmp(&condition_val64, host_addr,
390  sizeof(uint64_t));
391  } else if (pkt->getSize() == sizeof(uint32_t)) {
392  condition_val32 = (uint32_t)pkt->req->getExtraData();
393  overwrite_mem = !std::memcmp(&condition_val32, host_addr,
394  sizeof(uint32_t));
395  } else
396  panic("Invalid size for conditional read/write\n");
397  }
398 
399  if (overwrite_mem)
400  std::memcpy(host_addr, &overwrite_val[0], pkt->getSize());
401 
402  assert(!pkt->req->isInstFetch());
403  TRACE_PACKET("Read/Write");
404  stats.numOther[pkt->req->masterId()]++;
405  }
406  } else if (pkt->isRead()) {
407  assert(!pkt->isWrite());
408  if (pkt->isLLSC()) {
409  assert(!pkt->fromCache());
410  // if the packet is not coming from a cache then we have
411  // to do the LL/SC tracking here
412  trackLoadLocked(pkt);
413  }
414  if (pmemAddr) {
415  pkt->setData(host_addr);
416  }
417  TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
418  stats.numReads[pkt->req->masterId()]++;
419  stats.bytesRead[pkt->req->masterId()] += pkt->getSize();
420  if (pkt->req->isInstFetch())
421  stats.bytesInstRead[pkt->req->masterId()] += pkt->getSize();
422  } else if (pkt->isInvalidate() || pkt->isClean()) {
423  assert(!pkt->isWrite());
424  // in a fastmem system invalidating and/or cleaning packets
425  // can be seen due to cache maintenance requests
426 
427  // no need to do anything
428  } else if (pkt->isWrite()) {
429  if (writeOK(pkt)) {
430  if (pmemAddr) {
431  pkt->writeData(host_addr);
432  DPRINTF(MemoryAccess, "%s write due to %s\n",
433  __func__, pkt->print());
434  }
435  assert(!pkt->req->isInstFetch());
436  TRACE_PACKET("Write");
437  stats.numWrites[pkt->req->masterId()]++;
438  stats.bytesWritten[pkt->req->masterId()] += pkt->getSize();
439  }
440  } else {
441  panic("Unexpected packet %s", pkt->print());
442  }
443 
444  if (pkt->needsResponse()) {
445  pkt->makeResponse();
446  }
447 }
448 
449 void
451 {
452  assert(pkt->getAddrRange().isSubset(range));
453 
454  uint8_t *host_addr = toHostAddr(pkt->getAddr());
455 
456  if (pkt->isRead()) {
457  if (pmemAddr) {
458  pkt->setData(host_addr);
459  }
460  TRACE_PACKET("Read");
461  pkt->makeResponse();
462  } else if (pkt->isWrite()) {
463  if (pmemAddr) {
464  pkt->writeData(host_addr);
465  }
466  TRACE_PACKET("Write");
467  pkt->makeResponse();
468  } else if (pkt->isPrint()) {
469  Packet::PrintReqState *prs =
470  dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
471  assert(prs);
472  // Need to call printLabels() explicitly since we're not going
473  // through printObj().
474  prs->printLabels();
475  // Right now we just print the single byte at the specified address.
476  ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *host_addr);
477  } else {
478  panic("AbstractMemory: unimplemented functional command %s",
479  pkt->cmdString());
480  }
481 }
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
void ccprintf(cp::Print &print)
Definition: cprintf.hh:131
#define DPRINTF(x,...)
Definition: trace.hh:229
void functionalAccess(PacketPtr pkt)
Perform an untimed memory read or write without changing anything but the memory itself.
Derived & subname(off_type index, const std::string &name)
Set the subfield name for the given index, and marks this stat to print at the end of simulation...
Definition: statistics.hh:379
AtomicOpFunctor * getAtomicOp() const
Accessor function to atomic op.
Definition: packet.hh:764
Bitfield< 7 > i
std::string getMasterName(MasterID master_id)
Get the name of an object for a given request id.
Definition: system.cc:652
Object used to maintain state of a PrintReq.
Definition: packet.hh:414
const FlagsType nonan
Don&#39;t print if this is NAN.
Definition: info.hh:61
std::shared_ptr< Request > RequestPtr
Definition: request.hh:83
Locked address class that represents a physical address and a context id.
Definition: abstract_mem.hh:65
bool writeOK(PacketPtr pkt)
bool isClean() const
Definition: packet.hh:545
AbstractMemory(const AbstractMemory &)
bool isSubset(const AddrRange &r) const
Determine if this range is a subset of another range, i.e.
Definition: addr_range.hh:383
bool cacheResponding() const
Definition: packet.hh:591
#define DDUMP(x, data, count)
Definition: trace.hh:228
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:66
Definition: system.hh:77
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:586
void regStats() override
Callback to set stat parameters.
const std::string & curPrefix()
Returns the current line prefix.
Definition: packet.hh:443
bool isWrite() const
Definition: packet.hh:529
bool checkLockedAddrList(PacketPtr pkt)
Derived & flags(Flags _flags)
Set the flags and marks this stat to print at the end of simulation.
Definition: statistics.hh:336
bool isInvalidate() const
Definition: packet.hh:543
ThreadContext is the external interface to all thread state for anything outside of the CPU...
Stats::Formula simSeconds
Definition: stat_control.cc:64
void globalClearExclusive(XC *xc)
Definition: locked_mem.hh:86
bool isRead() const
Definition: packet.hh:528
const ByteOrder GuestByteOrder
Definition: isa_traits.hh:42
Derived & init(size_type size)
Set this vector to have the given size.
Definition: statistics.hh:1152
uint64_t getUintX(ByteOrder endian) const
Get the data in the packet byte swapped from the specified endianness and zero-extended to 64 bits...
Definition: packet.cc:337
AddrRange getAddrRange() const
Get the address range.
ThreadContext * getThreadContext(ContextID tid) const
Definition: system.hh:194
bool isAtomicOp() const
Definition: packet.hh:765
RequestPtr req
A pointer to the original request.
Definition: packet.hh:327
std::ostream & os
Definition: packet.hh:434
Stats::Vector bytesWritten
Number of bytes written to this memory.
Stats::Vector numOther
Number of other requests.
AbstractMemory declaration.
unsigned getSize() const
Definition: packet.hh:736
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:72
static Addr mask(Addr paddr)
Definition: abstract_mem.hh:81
bool needsResponse() const
Definition: packet.hh:542
AbstractMemoryParams Params
void setData(const uint8_t *p)
Copy data into the packet from the provided pointer.
Definition: packet.hh:1158
Definition: flags.hh:35
uint8_t * pmemAddr
The ClockedObject class extends the SimObject with a clock and accessor functions to relate ticks to ...
void printLabels()
Print all of the pending unprinted labels on the stack.
Definition: packet.cc:461
Group()=delete
Derived & prereq(const Stat &prereq)
Set the prerequisite stat and marks this stat to print at the end of simulation.
Definition: statistics.hh:350
void access(PacketPtr pkt)
Perform an untimed memory access and update all the state (e.g.
Addr getAddr() const
Definition: packet.hh:726
AddrRange getAddrRange() const
Get address range to which this packet belongs.
Definition: packet.cc:228
void writeData(uint8_t *p) const
Copy data from the packet to the memory at the provided pointer.
Definition: packet.hh:1187
STL list class.
Definition: stl.hh:54
std::list< LockedAddr > lockedAddrList
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
Stats::Formula bwTotal
Total bandwidth from this memory.
Derived & precision(int _precision)
Set the precision and marks this stat to print at the end of simulation.
Definition: statistics.hh:324
Stats::Vector bytesRead
Number of total bytes read from this memory.
A Packet is used to encapsulate a transfer between two objects in the memory system (e...
Definition: packet.hh:255
Stats::Vector bytesInstRead
Number of instruction bytes read from this memory.
void trackLoadLocked(PacketPtr pkt)
const FlagsType total
Print the total.
Definition: info.hh:51
uint8_t * ptr() const
Definition: backdoor.hh:82
MasterID maxMasters()
Get the number of masters registered in the system.
Definition: system.hh:412
bool isLLSC() const
Definition: packet.hh:554
void invalidate()
Definition: backdoor.hh:136
AddrRange range
void makeResponse()
Take a request packet and modify it in place to be suitable for returning as a response to that reque...
Definition: packet.hh:937
void init() override
Initialise this memory.
Definition: abstract_mem.cc:71
bool fromCache() const
Definition: packet.hh:546
Stats::Vector numWrites
Number of write requests.
System * system() const
read the system pointer Implemented for completeness with the setter
uint8_t * toHostAddr(Addr addr) const
Transform a gem5 address space address into its physical counterpart in the host address space...
const AbstractMemory & mem
bool interleaved() const
Determine if the range is interleaved or not.
Definition: addr_range.hh:250
void setBackingStore(uint8_t *pmem_addr)
Set the host memory backing store to be used by this memory controller.
Definition: abstract_mem.cc:80
Stats::Formula bwRead
Read bandwidth from this memory.
SenderState * senderState
This packet&#39;s sender state.
Definition: packet.hh:480
void print(std::ostream &o, int verbosity=0, const std::string &prefix="") const
Definition: packet.cc:376
MemCmd cmd
The command field of the packet.
Definition: packet.hh:322
uint64_t size() const
Get the memory size.
System * _system
Pointer to the System object.
const ContextID InvalidContextID
Definition: types.hh:232
MemBackdoor backdoor
bool isPrint() const
Definition: packet.hh:556
const T * getConstPtr() const
Definition: packet.hh:1099
An abstract memory represents a contiguous block of physical memory, with an associated address range...
bool_vector8 mem[]
Definition: reset_stim.h:43
Stats::Formula bwWrite
Write bandwidth from this memory.
const std::string & cmdString() const
Return the string name of the cmd field (for debugging and tracing).
Definition: packet.hh:523
const FlagsType nozero
Don&#39;t print if this is zero.
Definition: info.hh:59
Bitfield< 0 > p
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:185
std::vector< Info * > stats
Definition: group.hh:177
Stats::Formula bwInstRead
Read bandwidth from this memory.
#define TRACE_PACKET(A)
MemStats(AbstractMemory &mem)
Definition: abstract_mem.cc:92
int ContextID
Globally unique thread context ID.
Definition: types.hh:231
Addr getPageBytes() const
Get the page bytes for the ISA.
Definition: system.hh:305
Stats::Vector numReads
Number of read requests.

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