2 * Copyright (c) 2014 ARM Limited
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.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef __MEM_MEM_CHECKER_HH__
42 #define __MEM_MEM_CHECKER_HH__
47 #include <unordered_map>
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "base/types.hh"
53 #include "debug/MemChecker.hh"
54 #include "params/MemChecker.hh"
55 #include "sim/core.hh"
56 #include "sim/sim_object.hh"
59 * MemChecker. Verifies that reads observe the values from permissible writes.
60 * As memory operations have a start and completion time, we consider them as
61 * transactions which have a start and end time. Because of this, the lifetimes
62 * of transactions of memory operations may be overlapping -- we assume that if
63 * there is overlap between writes, they could be reordered by the memory
64 * subsystem, and a read could any of these. For more detail, see comments of
67 * For simplicity, the permissible values a read can observe are only dependent
68 * on the particular location, and we do not consider the effect of multi-byte
69 * reads or writes. This precludes us from discovering single-copy atomicity
72 class MemChecker : public SimObject
76 * The Serial type is used to be able to uniquely identify a transaction as
77 * it passes through the system. It's value is independent of any other
80 typedef uint64_t Serial;
82 static const Serial SERIAL_INITIAL = 0; //!< Initial serial
85 * The initial tick the system starts with. Must not be larger than the
86 * minimum value that curTick() could return at any time in the system's
89 static const Tick TICK_INITIAL = 0;
92 * The maximum value that curTick() could ever return.
94 static const Tick TICK_FUTURE = MaxTick;
97 * Initial data value. No requirements.
99 static const uint8_t DATA_INITIAL = 0x00;
102 * The Transaction class captures the lifetimes of read and write
103 * operations, and the values they consumed or produced respectively.
109 Transaction(Serial _serial,
110 Tick _start, Tick _complete,
111 uint8_t _data = DATA_INITIAL)
113 start(_start), complete(_complete),
118 Serial serial; //!< Unique identifying serial
119 Tick start; //!< Start tick
120 Tick complete; //!< Completion tick
123 * Depending on the memory operation, the data value either represents:
124 * for writes, the value written upon start; for reads, the value read
130 * Orders Transactions for use with std::map.
132 bool operator<(const Transaction& rhs) const
133 { return serial < rhs.serial; }
137 * The WriteCluster class captures sets of writes where all writes are
138 * overlapping with at least one other write. Capturing writes in this way
139 * simplifies pruning of writes.
145 : start(TICK_FUTURE), complete(TICK_FUTURE),
146 completeMax(TICK_INITIAL), numIncomplete(0)
150 * Starts a write transaction.
152 * @param serial Unique identifier of the write.
153 * @param _start When the write was sent off to the memory subsystem.
154 * @param data The data that this write passed to the memory
157 void startWrite(Serial serial, Tick _start, uint8_t data);
160 * Completes a write transaction.
162 * @param serial Unique identifier of a write *previously started*.
163 * @param _complete When the write was sent off to the memory
166 void completeWrite(Serial serial, Tick _complete);
169 * Aborts a write transaction.
171 * @param serial Unique identifier of a write *previously started*.
173 void abortWrite(Serial serial);
176 * @return true if this cluster's write all completed, false otherwise.
178 bool isComplete() const { return complete != TICK_FUTURE; }
181 Tick start; //!< Start of earliest write in cluster
182 Tick complete; //!< Completion of last write in cluster
185 * Map of Serial --> Transaction of all writes in cluster; contains
186 * all, in-flight or already completed.
188 std::unordered_map<Serial, Transaction> writes;
192 size_t numIncomplete;
195 typedef std::list<Transaction> TransactionList;
196 typedef std::list<WriteCluster> WriteClusterList;
199 * The ByteTracker keeps track of transactions for the *same byte* -- all
200 * outstanding reads, the completed reads (and what they observed) and write
201 * clusters (see WriteCluster).
203 class ByteTracker : public Named
207 ByteTracker(Addr addr = 0, const MemChecker *parent = NULL)
208 : Named((parent != NULL ? parent->name() : "") +
209 csprintf(".ByteTracker@%#llx", addr))
211 // The initial transaction has start == complete == TICK_INITIAL,
212 // indicating that there has been no real write to this location;
213 // therefore, upon checking, we do not expect any particular value.
214 readObservations.emplace_back(
215 Transaction(SERIAL_INITIAL, TICK_INITIAL, TICK_INITIAL,
220 * Starts a read transaction.
222 * @param serial Unique identifier for the read.
223 * @param start When the read was sent off to the memory subsystem.
225 void startRead(Serial serial, Tick start);
228 * Given a start and end time (of any read transaction), this function
229 * iterates through all data that such a read is expected to see. The
230 * data parameter is the actual value that we observed, and the
231 * function immediately returns true when a match is found, false
234 * The set of expected data are:
236 * 1. The last value observed by a read with a completion time before
237 * this start time (if any).
239 * 2. The data produced by write transactions with a completion after
240 * the last observed read start time. Only data produced in the
241 * closest overlapping / earlier write cluster relative to this check
242 * request is considered, as writes in separate clusters are not
245 * @param start Start time of transaction to validate.
246 * @param complete End time of transaction to validate.
247 * @param data The value that we have actually seen.
249 * @return True if a match is found, false otherwise.
251 bool inExpectedData(Tick start, Tick complete, uint8_t data);
254 * Completes a read transaction that is still outstanding.
256 * @param serial Unique identifier of a read *previously started*.
257 * @param complete When the read got a response.
258 * @param data The data returned by the memory subsystem.
260 bool completeRead(Serial serial, Tick complete, uint8_t data);
263 * Starts a write transaction. Wrapper to startWrite of WriteCluster
266 * @param serial Unique identifier of the write.
267 * @param start When the write was sent off to the memory subsystem.
268 * @param data The data that this write passed to the memory
271 void startWrite(Serial serial, Tick start, uint8_t data);
274 * Completes a write transaction. Wrapper to startWrite of WriteCluster
277 * @param serial Unique identifier of a write *previously started*.
278 * @param complete When the write was sent off to the memory subsystem.
280 void completeWrite(Serial serial, Tick complete);
283 * Aborts a write transaction. Wrapper to abortWrite of WriteCluster
286 * @param serial Unique identifier of a write *previously started*.
288 void abortWrite(Serial serial);
291 * This function returns the expected data that inExpectedData iterated
292 * through in the last call. If inExpectedData last returned true, the
293 * set may be incomplete; if inExpectedData last returned false, the
294 * vector will contain the full set.
296 * @return Reference to internally maintained vector maintaining last
297 * expected data that inExpectedData iterated through.
299 const std::vector<uint8_t>& lastExpectedData() const
300 { return _lastExpectedData; }
305 * Convenience function to return the most recent incomplete write
306 * cluster. Instantiates new write cluster if the most recent one has
309 * @return The most recent incomplete write cluster.
311 WriteCluster* getIncompleteWriteCluster();
314 * Helper function to return an iterator to the entry of a container of
315 * Transaction compatible classes, before a certain tick.
317 * @param before Tick value which should be greater than the
318 * completion tick of the returned element.
320 * @return Iterator into container.
322 template <class TList>
323 typename TList::iterator lastCompletedTransaction(TList *l, Tick before)
327 // Scanning backwards increases the chances of getting a match
331 for (--it; it != l->begin() && it->complete >= before; --it);
337 * Prunes no longer needed transactions. We only keep up to the last /
338 * most recent of each, readObservations and writeClusters, before the
339 * first outstanding read.
341 * It depends on the contention / overlap between memory operations to
342 * the same location of a particular workload how large each of them
345 void pruneTransactions();
350 * Maintains a map of Serial -> Transaction for all outstanding reads.
352 * Use an ordered map here, as this makes pruneTransactions() more
353 * efficient (find first outstanding read).
355 std::map<Serial, Transaction> outstandingReads;
358 * List of completed reads, i.e. observations of reads.
360 TransactionList readObservations;
363 * List of write clusters for this address.
365 WriteClusterList writeClusters;
368 * See lastExpectedData().
370 std::vector<uint8_t> _lastExpectedData;
375 MemChecker(const MemCheckerParams *p)
377 nextSerial(SERIAL_INITIAL)
380 virtual ~MemChecker() {}
383 * Starts a read transaction.
385 * @param start Tick this read was sent to the memory subsystem.
386 * @param addr Address for read.
387 * @param size Size of data expected.
389 * @return Serial representing the unique identifier for this transaction.
391 Serial startRead(Tick start, Addr addr, size_t size);
394 * Starts a write transaction.
396 * @param start Tick when this write was sent to the memory subsystem.
397 * @param addr Address for write.
398 * @param size Size of data to be written.
399 * @param data Pointer to size bytes, containing data to be written.
401 * @return Serial representing the unique identifier for this transaction.
403 Serial startWrite(Tick start, Addr addr, size_t size, const uint8_t *data);
406 * Completes a previously started read transaction.
408 * @param serial A serial of a read that was previously started and
409 * matches the address of the previously started read.
410 * @param complete Tick we received the response from the memory subsystem.
411 * @param addr Address for read.
412 * @param size Size of data received.
413 * @param data Pointer to size bytes, containing data received.
415 * @return True if the data we received is in the expected set, false
418 bool completeRead(Serial serial, Tick complete,
419 Addr addr, size_t size, uint8_t *data);
422 * Completes a previously started write transaction.
424 * @param serial A serial of a write that was previously started and
425 * matches the address of the previously started write.
426 * @param complete Tick we received acknowledgment of completion from the
428 * @param addr Address for write.
429 * @param size The size of the data written.
431 void completeWrite(Serial serial, Tick complete, Addr addr, size_t size);
434 * Aborts a previously started write transaction.
436 * @param serial A serial of a write that was previously started and
437 * matches the address of the previously started write.
438 * @param addr Address for write.
439 * @param size The size of the data written.
441 void abortWrite(Serial serial, Addr addr, size_t size);
444 * Resets the entire checker. Note that if there are transactions
445 * in-flight, this will cause a warning to be issued if these are completed
446 * after the reset. This does not reset nextSerial to avoid such a race
447 * condition: where a transaction started before a reset with serial S,
448 * then reset() was called, followed by a start of a transaction with the
449 * same serial S and then receive a completion of the transaction before
450 * the reset with serial S.
453 { byte_trackers.clear(); }
456 * Resets an address-range. This may be useful in case other unmonitored
457 * parts of the system caused modification to this memory, but we cannot
458 * track their written values.
460 * @param addr Address base.
461 * @param size Size of range to be invalidated.
463 void reset(Addr addr, size_t size);
466 * In completeRead, if an error is encountered, this does not print nor
467 * cause an error, but instead should be handled by the caller. However, to
468 * record information about the cause of an error, completeRead creates an
469 * errorMessage. This function returns the last error that was detected in
472 * @return Reference to string of error message.
474 const std::string& getErrorMessage() const { return errorMessage; }
478 * Returns the instance of ByteTracker for the requested location.
480 ByteTracker* getByteTracker(Addr addr)
482 auto it = byte_trackers.find(addr);
483 if (it == byte_trackers.end()) {
484 it = byte_trackers.insert(
485 std::make_pair(addr, ByteTracker(addr, this))).first;
492 * Detailed error message of the last violation in completeRead.
494 std::string errorMessage;
497 * Next distinct serial to be assigned to the next transaction to be
503 * Maintain a map of address --> byte-tracker. Per-byte entries are
504 * initialized as needed.
506 * The required space for this obviously grows with the number of distinct
507 * addresses used for a particular workload. The used size is independent on
508 * the number of nodes in the system, those may affect the size of per-byte
509 * tracking information.
511 * Access via getByteTracker()!
513 std::unordered_map<Addr, ByteTracker> byte_trackers;
516 inline MemChecker::Serial
517 MemChecker::startRead(Tick start, Addr addr, size_t size)
520 "starting read: serial = %d, start = %d, addr = %#llx, "
521 "size = %d\n", nextSerial, start, addr , size);
523 for (size_t i = 0; i < size; ++i) {
524 getByteTracker(addr + i)->startRead(nextSerial, start);
530 inline MemChecker::Serial
531 MemChecker::startWrite(Tick start, Addr addr, size_t size, const uint8_t *data)
534 "starting write: serial = %d, start = %d, addr = %#llx, "
535 "size = %d\n", nextSerial, start, addr, size);
537 for (size_t i = 0; i < size; ++i) {
538 getByteTracker(addr + i)->startWrite(nextSerial, start, data[i]);
545 MemChecker::completeWrite(MemChecker::Serial serial, Tick complete,
546 Addr addr, size_t size)
549 "completing write: serial = %d, complete = %d, "
550 "addr = %#llx, size = %d\n", serial, complete, addr, size);
552 for (size_t i = 0; i < size; ++i) {
553 getByteTracker(addr + i)->completeWrite(serial, complete);
558 MemChecker::abortWrite(MemChecker::Serial serial, Addr addr, size_t size)
561 "aborting write: serial = %d, addr = %#llx, size = %d\n",
564 for (size_t i = 0; i < size; ++i) {
565 getByteTracker(addr + i)->abortWrite(serial);
569 #endif // __MEM_MEM_CHECKER_HH__