2 * Copyright (c) 2011-2013, 2018 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.
40 * PortProxy Object Declaration.
42 * Port proxies are used when non-structural entities need access to
43 * the memory system (or structural entities that want to peak into
44 * the memory system without making a real memory access).
46 * Proxy objects replace the previous FunctionalPort, TranslatingPort
47 * and VirtualPort objects, which provided the same functionality as
48 * the proxies, but were instances of ports not corresponding to real
49 * structural ports of the simulated system. Via the port proxies all
50 * the accesses go through an actual port (either the system port,
51 * e.g. for processes or initialisation, or a the data port of the
52 * CPU, e.g. for threads) and thus are transparent to a potentially
53 * distributed memory and automatically adhere to the memory map of
57 #ifndef __MEM_PORT_PROXY_HH__
58 #define __MEM_PORT_PROXY_HH__
63 #include "mem/port.hh"
64 #include "sim/byteswap.hh"
67 * This object is a proxy for a port or other object which implements the
68 * functional response protocol, to be used for debug accesses.
70 * This proxy object is used when non structural entities
71 * (e.g. thread contexts, object file loaders) need access to the
72 * memory system. It calls the corresponding functions on the underlying
73 * protocol, and provides templatized convenience access functions.
75 * The addresses are interpreted as physical addresses.
77 * @sa SETranslatingProxy
78 * @sa FSTranslatingProxy
80 class PortProxy : FunctionalRequestProtocol
83 typedef std::function<void(PacketPtr pkt)> SendFunctionalFunc;
86 SendFunctionalFunc sendFunctional;
88 /** Granularity of any transactions issued through this proxy. */
89 const unsigned int _cacheLineSize;
92 recvFunctionalSnoop(PacketPtr pkt) override
94 // Since port proxies aren't anyone else's peer, they should never
96 panic("Port proxies should never receive snoops.");
100 PortProxy(SendFunctionalFunc func, unsigned int cacheLineSize) :
101 sendFunctional(func), _cacheLineSize(cacheLineSize)
103 PortProxy(const RequestPort &port, unsigned int cacheLineSize) :
104 sendFunctional([&port](PacketPtr pkt)->void {
105 port.sendFunctional(pkt);
106 }), _cacheLineSize(cacheLineSize)
108 virtual ~PortProxy() { }
112 /** Fixed functionality for use in base classes. */
115 * Read size bytes memory at physical address and store in p.
117 void readBlobPhys(Addr addr, Request::Flags flags,
118 void *p, int size) const;
121 * Write size bytes from p to physical address.
123 void writeBlobPhys(Addr addr, Request::Flags flags,
124 const void *p, int size) const;
127 * Fill size bytes starting at physical addr with byte value val.
129 void memsetBlobPhys(Addr addr, Request::Flags flags,
130 uint8_t v, int size) const;
134 /** Methods to override in base classes */
137 * Read size bytes memory at address and store in p.
138 * Returns true on success and false on failure.
141 tryReadBlob(Addr addr, void *p, int size) const
143 readBlobPhys(addr, 0, p, size);
148 * Write size bytes from p to address.
149 * Returns true on success and false on failure.
152 tryWriteBlob(Addr addr, const void *p, int size) const
154 writeBlobPhys(addr, 0, p, size);
159 * Fill size bytes starting at addr with byte value val.
160 * Returns true on success and false on failure.
163 tryMemsetBlob(Addr addr, uint8_t val, int size) const
165 memsetBlobPhys(addr, 0, val, size);
171 /** Higher level interfaces based on the above. */
174 * Same as tryReadBlob, but insists on success.
177 readBlob(Addr addr, void *p, int size) const
179 if (!tryReadBlob(addr, p, size))
180 fatal("readBlob(%#x, ...) failed", addr);
184 * Same as tryWriteBlob, but insists on success.
187 writeBlob(Addr addr, const void *p, int size) const
189 if (!tryWriteBlob(addr, p, size))
190 fatal("writeBlob(%#x, ...) failed", addr);
194 * Same as tryMemsetBlob, but insists on success.
197 memsetBlob(Addr addr, uint8_t v, int size) const
199 if (!tryMemsetBlob(addr, v, size))
200 fatal("memsetBlob(%#x, ...) failed", addr);
204 * Read sizeof(T) bytes from address and return as object T.
206 template <typename T>
207 T read(Addr address) const;
210 * Write object T to address. Writes sizeof(T) bytes.
212 template <typename T>
213 void write(Addr address, const T &data) const;
216 * Read sizeof(T) bytes from address and return as object T.
217 * Performs endianness conversion from the selected guest to host order.
219 template <typename T>
220 T read(Addr address, ByteOrder guest_byte_order) const;
223 * Write object T to address. Writes sizeof(T) bytes.
224 * Performs endianness conversion from host to the selected guest order.
226 template <typename T>
227 void write(Addr address, T data, ByteOrder guest_byte_order) const;
230 * Write the string str into guest memory at address addr.
231 * Returns true on success and false on failure.
233 bool tryWriteString(Addr addr, const char *str) const;
236 * Same as tryWriteString, but insists on success.
239 writeString(Addr addr, const char *str) const
241 if (!tryWriteString(addr, str))
242 fatal("writeString(%#x, ...) failed", addr);
246 * Reads the string at guest address addr into the std::string str.
247 * Returns true on success and false on failure.
249 bool tryReadString(std::string &str, Addr addr) const;
252 * Same as tryReadString, but insists on success.
255 readString(std::string &str, Addr addr) const
257 if (!tryReadString(str, addr))
258 fatal("readString(%#x, ...) failed", addr);
262 * Reads the string at guest address addr into the char * str, reading up
263 * to maxlen characters. The last character read is always a nul
264 * terminator. Returns true on success and false on failure.
266 bool tryReadString(char *str, Addr addr, size_t maxlen) const;
269 * Same as tryReadString, but insists on success.
272 readString(char *str, Addr addr, size_t maxlen) const
274 if (!tryReadString(str, addr, maxlen))
275 fatal("readString(%#x, ...) failed", addr);
280 template <typename T>
282 PortProxy::read(Addr address) const
285 readBlob(address, &data, sizeof(T));
289 template <typename T>
291 PortProxy::write(Addr address, const T &data) const
293 writeBlob(address, &data, sizeof(T));
296 template <typename T>
298 PortProxy::read(Addr address, ByteOrder byte_order) const
301 readBlob(address, &data, sizeof(T));
302 return gtoh(data, byte_order);
305 template <typename T>
307 PortProxy::write(Addr address, T data, ByteOrder byte_order) const
309 data = htog(data, byte_order);
310 writeBlob(address, &data, sizeof(T));
313 #endif // __MEM_PORT_PROXY_HH__