2 * Copyright (c) 2012-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.
37 * Authors: Thomas Grass
42 #include "mem/mem_checker_monitor.hh"
46 #include "base/output.hh"
47 #include "base/trace.hh"
48 #include "debug/MemCheckerMonitor.hh"
52 MemCheckerMonitor::MemCheckerMonitor(Params
* params
)
54 masterPort(name() + "-master", *this),
55 slavePort(name() + "-slave", *this),
56 warnOnly(params
->warn_only
),
57 memchecker(params
->memchecker
)
60 MemCheckerMonitor::~MemCheckerMonitor()
64 MemCheckerMonitorParams::create()
66 return new MemCheckerMonitor(this);
70 MemCheckerMonitor::init()
72 // make sure both sides of the monitor are connected
73 if (!slavePort
.isConnected() || !masterPort
.isConnected())
74 fatal("Communication monitor is not connected on both sides.\n");
78 MemCheckerMonitor::getMasterPort(const std::string
& if_name
, PortID idx
)
80 if (if_name
== "master" || if_name
== "mem_side") {
83 return MemObject::getMasterPort(if_name
, idx
);
88 MemCheckerMonitor::getSlavePort(const std::string
& if_name
, PortID idx
)
90 if (if_name
== "slave" || if_name
== "cpu_side") {
93 return MemObject::getSlavePort(if_name
, idx
);
98 MemCheckerMonitor::recvFunctional(PacketPtr pkt
)
100 Addr addr
= pkt
->getAddr();
101 unsigned size
= pkt
->getSize();
103 // Conservatively reset this address-range. Alternatively we could try to
104 // update the values seen by the memchecker, however, there may be other
105 // reads/writes to these location from other devices we do not see.
106 memchecker
->reset(addr
, size
);
108 masterPort
.sendFunctional(pkt
);
110 DPRINTF(MemCheckerMonitor
,
111 "Forwarded functional access: addr = %#llx, size = %d\n",
116 MemCheckerMonitor::recvFunctionalSnoop(PacketPtr pkt
)
118 Addr addr
= pkt
->getAddr();
119 unsigned size
= pkt
->getSize();
122 memchecker
->reset(addr
, size
);
124 slavePort
.sendFunctionalSnoop(pkt
);
126 DPRINTF(MemCheckerMonitor
,
127 "Received functional snoop: addr = %#llx, size = %d\n",
132 MemCheckerMonitor::recvAtomic(PacketPtr pkt
)
134 assert(false && "Atomic not supported");
135 return masterPort
.sendAtomic(pkt
);
139 MemCheckerMonitor::recvAtomicSnoop(PacketPtr pkt
)
141 assert(false && "Atomic not supported");
142 return slavePort
.sendAtomicSnoop(pkt
);
146 MemCheckerMonitor::recvTimingReq(PacketPtr pkt
)
148 // should always see a request
149 assert(pkt
->isRequest());
151 // Store relevant fields of packet, because packet may be modified
152 // or even deleted when sendTiming() is called.
154 // For reads we are only interested in real reads, and not prefetches, as
155 // it is not guaranteed that the prefetch returns any useful data.
156 bool is_read
= pkt
->isRead() && !pkt
->req
->isPrefetch();
157 bool is_write
= pkt
->isWrite();
158 unsigned size
= pkt
->getSize();
159 Addr addr
= pkt
->getAddr();
160 bool expects_response
= pkt
->needsResponse() && !pkt
->cacheResponding();
161 std::unique_ptr
<uint8_t[]> pkt_data
;
162 MemCheckerMonitorSenderState
* state
= NULL
;
164 if (expects_response
&& is_write
) {
165 // On receipt of a request, only need to allocate pkt_data if this is a
166 // write. For reads, we have no data yet, so it doesn't make sense to
168 pkt_data
.reset(new uint8_t[size
]);
169 memcpy(pkt_data
.get(), pkt
->getConstPtr
<uint8_t*>(), size
);
172 // If a cache miss is served by a cache, a monitor near the memory
173 // would see a request which needs a response, but this response
174 // would not come back from the memory. Therefore
175 // we additionally have to check the inhibit flag.
176 if (expects_response
&& (is_read
|| is_write
)) {
177 state
= new MemCheckerMonitorSenderState(0);
178 pkt
->pushSenderState(state
);
181 // Attempt to send the packet
182 bool successful
= masterPort
.sendTimingReq(pkt
);
184 // If not successful, restore the sender state
185 if (!successful
&& expects_response
&& (is_read
|| is_write
)) {
186 delete pkt
->popSenderState();
189 if (successful
&& expects_response
) {
191 MemChecker::Serial serial
= memchecker
->startRead(curTick(),
195 // At the time where we push the sender-state, we do not yet know
196 // the serial the MemChecker class will assign to this request. We
197 // cannot call startRead at the time we push the sender-state, as
198 // the masterPort may not be successful in executing sendTimingReq,
199 // and in case of a failure, we must not modify the state of the
202 // Once we know that sendTimingReq was successful, we can set the
203 // serial of the newly constructed sender-state. This is legal, as
204 // we know that nobody else will touch nor is responsible for
205 // deletion of our sender-state.
206 state
->serial
= serial
;
208 DPRINTF(MemCheckerMonitor
,
209 "Forwarded read request: serial = %d, addr = %#llx, "
212 } else if (is_write
) {
213 MemChecker::Serial serial
= memchecker
->startWrite(curTick(),
218 state
->serial
= serial
;
220 DPRINTF(MemCheckerMonitor
,
221 "Forwarded write request: serial = %d, addr = %#llx, "
225 DPRINTF(MemCheckerMonitor
,
226 "Forwarded non read/write request: addr = %#llx\n", addr
);
228 } else if (successful
) {
229 DPRINTF(MemCheckerMonitor
,
230 "Forwarded request marked for cache response: addr = %#llx\n",
238 MemCheckerMonitor::recvTimingResp(PacketPtr pkt
)
240 // should always see responses
241 assert(pkt
->isResponse());
243 // Store relevant fields of packet, because packet may be modified
244 // or even deleted when sendTiming() is called.
245 bool is_read
= pkt
->isRead() && !pkt
->req
->isPrefetch();
246 bool is_write
= pkt
->isWrite();
247 bool is_failed_LLSC
= pkt
->isLLSC() && pkt
->req
->getExtraData() == 0;
248 unsigned size
= pkt
->getSize();
249 Addr addr
= pkt
->getAddr();
250 std::unique_ptr
<uint8_t[]> pkt_data
;
251 MemCheckerMonitorSenderState
* received_state
= NULL
;
254 // On receipt of a response, only need to allocate pkt_data if this is
255 // a read. For writes, we have already given the MemChecker the data on
256 // the request, so it doesn't make sense to allocate on write.
257 pkt_data
.reset(new uint8_t[size
]);
258 memcpy(pkt_data
.get(), pkt
->getConstPtr
<uint8_t*>(), size
);
261 if (is_read
|| is_write
) {
263 dynamic_cast<MemCheckerMonitorSenderState
*>(pkt
->senderState
);
265 // Restore initial sender state
266 panic_if(received_state
== NULL
,
267 "Monitor got a response without monitor sender state\n");
270 pkt
->senderState
= received_state
->predecessor
;
273 // Attempt to send the packet
274 bool successful
= slavePort
.sendTimingResp(pkt
);
276 // If packet successfully send, complete transaction in MemChecker
277 // instance, and delete sender state, otherwise restore state.
280 DPRINTF(MemCheckerMonitor
,
281 "Received read response: serial = %d, addr = %#llx, "
283 received_state
->serial
, addr
, size
);
285 bool result
= memchecker
->completeRead(received_state
->serial
,
292 warn("%s: read of %#llx @ cycle %d failed:\n%s\n",
295 memchecker
->getErrorMessage().c_str());
297 panic_if(!warnOnly
, "MemChecker violation!");
300 delete received_state
;
301 } else if (is_write
) {
302 DPRINTF(MemCheckerMonitor
,
303 "Received write response: serial = %d, addr = %#llx, "
305 received_state
->serial
, addr
, size
);
307 if (is_failed_LLSC
) {
308 // The write was not successful, let MemChecker know.
309 memchecker
->abortWrite(received_state
->serial
,
313 memchecker
->completeWrite(received_state
->serial
,
319 delete received_state
;
321 DPRINTF(MemCheckerMonitor
,
322 "Received non read/write response: addr = %#llx\n", addr
);
324 } else if (is_read
|| is_write
) {
325 // Don't delete anything and let the packet look like we
327 pkt
->senderState
= received_state
;
334 MemCheckerMonitor::recvTimingSnoopReq(PacketPtr pkt
)
336 slavePort
.sendTimingSnoopReq(pkt
);
340 MemCheckerMonitor::recvTimingSnoopResp(PacketPtr pkt
)
342 return masterPort
.sendTimingSnoopResp(pkt
);
346 MemCheckerMonitor::isSnooping() const
348 // check if the connected master port is snooping
349 return slavePort
.isSnooping();
353 MemCheckerMonitor::getAddrRanges() const
355 // get the address ranges of the connected slave port
356 return masterPort
.getAddrRanges();
360 MemCheckerMonitor::recvReqRetry()
362 slavePort
.sendRetryReq();
366 MemCheckerMonitor::recvRespRetry()
368 masterPort
.sendRetryResp();
372 MemCheckerMonitor::recvRangeChange()
374 slavePort
.sendRangeChange();