mem: Change warmupCycle stat to warmupTick
[gem5.git] / src / mem / mem_checker.cc
1 /*
2 * Copyright (c) 2014 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 * 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.
24 *
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.
36 */
37
38 #include "mem/mem_checker.hh"
39
40 #include "base/logging.hh"
41
42 void
43 MemChecker::WriteCluster::startWrite(MemChecker::Serial serial, Tick _start,
44 uint8_t data)
45 {
46 assert(!isComplete());
47
48 if (start == TICK_FUTURE) {
49 // Initialize a fresh write cluster
50 start = _start;
51 }
52 chatty_assert(start <= _start, "WriteClusters must filled in order!");
53
54 ++numIncomplete;
55
56 if (complete != TICK_FUTURE) {
57 // Reopen a closed write cluster
58 assert(_start < complete); // Should open a new write cluster instead
59 // Also somewhat fishy wrt causality / ordering of calls vs time
60 // progression TODO: Check me!
61 complete = TICK_FUTURE;
62 }
63
64 // Create new transaction, and denote completion time to be in the future.
65 writes.insert(std::make_pair(serial,
66 MemChecker::Transaction(serial, _start, TICK_FUTURE, data)));
67 }
68
69 void
70 MemChecker::WriteCluster::completeWrite(MemChecker::Serial serial,
71 Tick _complete)
72 {
73 auto it = writes.find(serial);
74
75 if (it == writes.end()) {
76 warn("Could not locate write transaction: serial = %d, "
77 "complete = %d\n", serial, _complete);
78 return;
79 }
80
81 // Record completion time of the write
82 assert(it->second.complete == TICK_FUTURE);
83 it->second.complete = _complete;
84
85 // Update max completion time for the cluster
86 if (completeMax < _complete) {
87 completeMax = _complete;
88 }
89
90 if (--numIncomplete == 0) {
91 // All writes have completed, this cluster is now complete and will be
92 // assigned the max of completion tick values among all writes.
93 //
94 // Note that we cannot simply keep updating complete, because that
95 // would count the cluster as closed already. Instead, we keep
96 // TICK_FUTURE until all writes have completed.
97 complete = completeMax;
98 }
99 }
100
101 void
102 MemChecker::WriteCluster::abortWrite(MemChecker::Serial serial)
103 {
104 if (!writes.erase(serial)) {
105 warn("Could not locate write transaction: serial = %d\n", serial);
106 return;
107 }
108
109 if (--numIncomplete == 0 && !writes.empty()) {
110 // This write cluster is now complete, and we can assign the current
111 // completeMax value.
112 complete = completeMax;
113 }
114
115 // Note: this WriteCluster is in pristine state if this was the only
116 // write present; the cluster will get reused through
117 // getIncompleteWriteCluster().
118 }
119
120 void
121 MemChecker::ByteTracker::startRead(MemChecker::Serial serial, Tick start)
122 {
123 outstandingReads.insert(std::make_pair(serial,
124 MemChecker::Transaction(serial, start, TICK_FUTURE)));
125 }
126
127 bool
128 MemChecker::ByteTracker::inExpectedData(Tick start, Tick complete,
129 uint8_t data)
130 {
131 _lastExpectedData.clear();
132
133 bool wc_overlap = true;
134
135 // Find the last value read from the location
136 const Transaction& last_obs =
137 *lastCompletedTransaction(&readObservations, start);
138 bool last_obs_valid = (last_obs.complete != TICK_INITIAL);
139
140 // Scan backwards through the write clusters to find the closest younger
141 // preceding & overlapping writes.
142 for (auto cluster = writeClusters.rbegin();
143 cluster != writeClusters.rend() && wc_overlap; ++cluster) {
144 for (const auto& addr_write : cluster->writes) {
145 const Transaction& write = addr_write.second;
146
147 if (write.complete < last_obs.start) {
148 // If this write transaction completed before the last
149 // observation, we ignore it as the last_observation has the
150 // correct value
151 continue;
152 }
153
154 if (write.data == data) {
155 // Found a match, end search.
156 return true;
157 }
158
159 // Record possible, but non-matching data for debugging
160 _lastExpectedData.push_back(write.data);
161
162 if (write.complete > start) {
163 // This write overlapped with the transaction we want to check
164 // -> continue checking the overlapping write cluster
165 continue;
166 }
167
168 // This write cluster has writes that have completed before the
169 // checked transaction. There is no need to check an earlier
170 // write-cluster -> set the exit condition for the outer loop
171 wc_overlap = false;
172
173 if (last_obs.complete < write.start) {
174 // We found a write which started after the last observed read,
175 // therefore we can not longer consider the value seen by the
176 // last observation as a valid expected value.
177 //
178 // Once all writes have been iterated through, we can check if
179 // the last observation is still valid to compare against.
180 last_obs_valid = false;
181 }
182 }
183 }
184
185 // We have not found any matching write, so far; check other sources of
186 // confirmation
187 if (last_obs_valid) {
188 // The last observation is not outdated according to the writes we have
189 // seen so far.
190 assert(last_obs.complete <= start);
191 if (last_obs.data == data) {
192 // Matched data from last observation -> all good
193 return true;
194 }
195 // Record non-matching, but possible value
196 _lastExpectedData.push_back(last_obs.data);
197 } else {
198 // We have not seen any valid observation, and the only writes
199 // observed are overlapping, so anything (in particular the
200 // initialisation value) goes
201 // NOTE: We can overlap with multiple write clusters, here
202 if (!writeClusters.empty() && wc_overlap) {
203 // ensure that all write clusters really overlap this read
204 assert(writeClusters.begin()->start < complete &&
205 writeClusters.rbegin()->complete > start);
206 return true;
207 }
208 }
209
210 if (_lastExpectedData.empty()) {
211 assert(last_obs.complete == TICK_INITIAL);
212 // We have not found any possible (non-matching data). Can happen in
213 // initial system state
214 DPRINTF(MemChecker, "no last observation nor write! start = %d, "\
215 "complete = %d, data = %#x\n", start, complete, data);
216 return true;
217 }
218 return false;
219 }
220
221 bool
222 MemChecker::ByteTracker::completeRead(MemChecker::Serial serial,
223 Tick complete, uint8_t data)
224 {
225 auto it = outstandingReads.find(serial);
226
227 if (it == outstandingReads.end()) {
228 // Can happen if concurrent with reset_address_range
229 warn("Could not locate read transaction: serial = %d, complete = %d\n",
230 serial, complete);
231 return true;
232 }
233
234 Tick start = it->second.start;
235 outstandingReads.erase(it);
236
237 // Verify data
238 const bool result = inExpectedData(start, complete, data);
239
240 readObservations.emplace_back(serial, start, complete, data);
241 pruneTransactions();
242
243 return result;
244 }
245
246 MemChecker::WriteCluster*
247 MemChecker::ByteTracker::getIncompleteWriteCluster()
248 {
249 if (writeClusters.empty() || writeClusters.back().isComplete()) {
250 writeClusters.emplace_back();
251 }
252
253 return &writeClusters.back();
254 }
255
256 void
257 MemChecker::ByteTracker::startWrite(MemChecker::Serial serial, Tick start,
258 uint8_t data)
259 {
260 getIncompleteWriteCluster()->startWrite(serial, start, data);
261 }
262
263 void
264 MemChecker::ByteTracker::completeWrite(MemChecker::Serial serial,
265 Tick complete)
266 {
267 getIncompleteWriteCluster()->completeWrite(serial, complete);
268 pruneTransactions();
269 }
270
271 void
272 MemChecker::ByteTracker::abortWrite(MemChecker::Serial serial)
273 {
274 getIncompleteWriteCluster()->abortWrite(serial);
275 }
276
277 void
278 MemChecker::ByteTracker::pruneTransactions()
279 {
280 // Obtain tick of first outstanding read. If there are no outstanding
281 // reads, we use curTick(), i.e. we will remove all readObservation except
282 // the most recent one.
283 const Tick before = outstandingReads.empty() ? curTick() :
284 outstandingReads.begin()->second.start;
285
286 // Pruning of readObservations
287 readObservations.erase(readObservations.begin(),
288 lastCompletedTransaction(&readObservations, before));
289
290 // Pruning of writeClusters
291 if (!writeClusters.empty()) {
292 writeClusters.erase(writeClusters.begin(),
293 lastCompletedTransaction(&writeClusters, before));
294 }
295 }
296
297 bool
298 MemChecker::completeRead(MemChecker::Serial serial, Tick complete,
299 Addr addr, size_t size, uint8_t *data)
300 {
301 bool result = true;
302
303 DPRINTF(MemChecker,
304 "completing read: serial = %d, complete = %d, "
305 "addr = %#llx, size = %d\n", serial, complete, addr, size);
306
307 for (size_t i = 0; i < size; ++i) {
308 ByteTracker *tracker = getByteTracker(addr + i);
309
310 if (!tracker->completeRead(serial, complete, data[i])) {
311 // Generate error message, and aggregate all failures for the bytes
312 // considered in this transaction in one message.
313 if (result) {
314 result = false;
315 errorMessage = "";
316 } else {
317 errorMessage += "\n";
318 }
319
320 errorMessage += csprintf(" Read transaction for address %#llx "
321 "failed: received %#x, expected ",
322 (unsigned long long)(addr + i), data[i]);
323
324 for (size_t j = 0; j < tracker->lastExpectedData().size(); ++j) {
325 errorMessage +=
326 csprintf("%#x%s",
327 tracker->lastExpectedData()[j],
328 (j == tracker->lastExpectedData().size() - 1)
329 ? "" : "|");
330 }
331 }
332 }
333
334 if (!result) {
335 DPRINTF(MemChecker, "read of %#llx @ cycle %d failed:\n%s\n", addr,
336 complete, errorMessage);
337 }
338
339 return result;
340 }
341
342 void
343 MemChecker::reset(Addr addr, size_t size)
344 {
345 for (size_t i = 0; i < size; ++i) {
346 byte_trackers.erase(addr + i);
347 }
348 }