port_proxy.hh (14012:1bdf42ed6add) port_proxy.hh (14196:ce364f5517f3)
1/*
2 * Copyright (c) 2011-2013, 2018 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 * Authors: Andreas Hansson
38 */
39
40/**
41 * @file
42 * PortProxy Object Declaration.
43 *
44 * Port proxies are used when non-structural entities need access to
45 * the memory system (or structural entities that want to peak into
46 * the memory system without making a real memory access).
47 *
48 * Proxy objects replace the previous FunctionalPort, TranslatingPort
49 * and VirtualPort objects, which provided the same functionality as
50 * the proxies, but were instances of ports not corresponding to real
51 * structural ports of the simulated system. Via the port proxies all
52 * the accesses go through an actual port (either the system port,
53 * e.g. for processes or initialisation, or a the data port of the
54 * CPU, e.g. for threads) and thus are transparent to a potentially
55 * distributed memory and automatically adhere to the memory map of
56 * the system.
57 */
58
59#ifndef __MEM_PORT_PROXY_HH__
60#define __MEM_PORT_PROXY_HH__
61
1/*
2 * Copyright (c) 2011-2013, 2018 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 * Authors: Andreas Hansson
38 */
39
40/**
41 * @file
42 * PortProxy Object Declaration.
43 *
44 * Port proxies are used when non-structural entities need access to
45 * the memory system (or structural entities that want to peak into
46 * the memory system without making a real memory access).
47 *
48 * Proxy objects replace the previous FunctionalPort, TranslatingPort
49 * and VirtualPort objects, which provided the same functionality as
50 * the proxies, but were instances of ports not corresponding to real
51 * structural ports of the simulated system. Via the port proxies all
52 * the accesses go through an actual port (either the system port,
53 * e.g. for processes or initialisation, or a the data port of the
54 * CPU, e.g. for threads) and thus are transparent to a potentially
55 * distributed memory and automatically adhere to the memory map of
56 * the system.
57 */
58
59#ifndef __MEM_PORT_PROXY_HH__
60#define __MEM_PORT_PROXY_HH__
61
62#include <functional>
62#include <limits>
63
64#include "mem/port.hh"
65#include "sim/byteswap.hh"
66
67/**
63#include <limits>
64
65#include "mem/port.hh"
66#include "sim/byteswap.hh"
67
68/**
68 * This object is a proxy for a structural port, to be used for debug
69 * accesses.
69 * This object is a proxy for a port or other object which implements the
70 * functional response protocol, to be used for debug accesses.
70 *
71 * This proxy object is used when non structural entities
72 * (e.g. thread contexts, object file loaders) need access to the
73 * memory system. It calls the corresponding functions on the underlying
71 *
72 * This proxy object is used when non structural entities
73 * (e.g. thread contexts, object file loaders) need access to the
74 * memory system. It calls the corresponding functions on the underlying
74 * structural port, and provides templatized convenience access functions.
75 * protocol, and provides templatized convenience access functions.
75 *
76 * The addresses are interpreted as physical addresses.
77 *
78 * @sa SETranslatingProxy
79 * @sa FSTranslatingProxy
80 */
76 *
77 * The addresses are interpreted as physical addresses.
78 *
79 * @sa SETranslatingProxy
80 * @sa FSTranslatingProxy
81 */
81class PortProxy
82class PortProxy : FunctionalRequestProtocol
82{
83{
84 public:
85 typedef std::function<void(PacketPtr pkt)> SendFunctionalFunc;
86
83 private:
87 private:
88 SendFunctionalFunc sendFunctional;
84
89
85 /** The actual physical port used by this proxy. */
86 MasterPort &_port;
87
88 /** Granularity of any transactions issued through this proxy. */
89 const unsigned int _cacheLineSize;
90
90 /** Granularity of any transactions issued through this proxy. */
91 const unsigned int _cacheLineSize;
92
93 void
94 recvFunctionalSnoop(PacketPtr pkt) override
95 {
96 // Since port proxies aren't anyone else's peer, they should never
97 // receive snoops.
98 panic("Port proxies should never receive snoops.");
99 }
100
91 public:
101 public:
92 PortProxy(MasterPort &port, unsigned int cacheLineSize) :
93 _port(port), _cacheLineSize(cacheLineSize)
102 PortProxy(SendFunctionalFunc func, unsigned int cacheLineSize) :
103 sendFunctional(func), _cacheLineSize(cacheLineSize)
94 {}
104 {}
105 PortProxy(const MasterPort &port, unsigned int cacheLineSize) :
106 sendFunctional([&port](PacketPtr pkt)->void {
107 port.sendFunctional(pkt);
108 }), _cacheLineSize(cacheLineSize)
109 {}
95 virtual ~PortProxy() { }
96
97
98
99 /** Fixed functionality for use in base classes. */
100
101 /**
102 * Read size bytes memory at physical address and store in p.
103 */
104 void readBlobPhys(Addr addr, Request::Flags flags,
105 void *p, int size) const;
106
107 /**
108 * Write size bytes from p to physical address.
109 */
110 void writeBlobPhys(Addr addr, Request::Flags flags,
111 const void *p, int size) const;
112
113 /**
114 * Fill size bytes starting at physical addr with byte value val.
115 */
116 void memsetBlobPhys(Addr addr, Request::Flags flags,
117 uint8_t v, int size) const;
118
119
120
121 /** Methods to override in base classes */
122
123 /**
124 * Read size bytes memory at address and store in p.
125 * Returns true on success and false on failure.
126 */
127 virtual bool
128 tryReadBlob(Addr addr, void *p, int size) const
129 {
130 readBlobPhys(addr, 0, p, size);
131 return true;
132 }
133
134 /**
135 * Write size bytes from p to address.
136 * Returns true on success and false on failure.
137 */
138 virtual bool
139 tryWriteBlob(Addr addr, const void *p, int size) const
140 {
141 writeBlobPhys(addr, 0, p, size);
142 return true;
143 }
144
145 /**
146 * Fill size bytes starting at addr with byte value val.
147 * Returns true on success and false on failure.
148 */
149 virtual bool
150 tryMemsetBlob(Addr addr, uint8_t val, int size) const
151 {
152 memsetBlobPhys(addr, 0, val, size);
153 return true;
154 }
155
156
157
158 /** Higher level interfaces based on the above. */
159
160 /**
161 * Same as tryReadBlob, but insists on success.
162 */
163 void
164 readBlob(Addr addr, void *p, int size) const
165 {
166 if (!tryReadBlob(addr, p, size))
167 fatal("readBlob(%#x, ...) failed", addr);
168 }
169
170 /**
171 * Same as tryWriteBlob, but insists on success.
172 */
173 void
174 writeBlob(Addr addr, const void *p, int size) const
175 {
176 if (!tryWriteBlob(addr, p, size))
177 fatal("writeBlob(%#x, ...) failed", addr);
178 }
179
180 /**
181 * Same as tryMemsetBlob, but insists on success.
182 */
183 void
184 memsetBlob(Addr addr, uint8_t v, int size) const
185 {
186 if (!tryMemsetBlob(addr, v, size))
187 fatal("memsetBlob(%#x, ...) failed", addr);
188 }
189
190 /**
191 * Read sizeof(T) bytes from address and return as object T.
192 */
193 template <typename T>
194 T read(Addr address) const;
195
196 /**
197 * Write object T to address. Writes sizeof(T) bytes.
198 */
199 template <typename T>
200 void write(Addr address, const T &data) const;
201
202 /**
203 * Read sizeof(T) bytes from address and return as object T.
204 * Performs endianness conversion from the selected guest to host order.
205 */
206 template <typename T>
207 T read(Addr address, ByteOrder guest_byte_order) const;
208
209 /**
210 * Write object T to address. Writes sizeof(T) bytes.
211 * Performs endianness conversion from host to the selected guest order.
212 */
213 template <typename T>
214 void write(Addr address, T data, ByteOrder guest_byte_order) const;
215
216 /**
217 * Write the string str into guest memory at address addr.
218 * Returns true on success and false on failure.
219 */
220 bool tryWriteString(Addr addr, const char *str) const;
221
222 /**
223 * Same as tryWriteString, but insists on success.
224 */
225 void
226 writeString(Addr addr, const char *str) const
227 {
228 if (!tryWriteString(addr, str))
229 fatal("writeString(%#x, ...) failed", addr);
230 }
231
232 /**
233 * Reads the string at guest address addr into the std::string str.
234 * Returns true on success and false on failure.
235 */
236 bool tryReadString(std::string &str, Addr addr) const;
237
238 /**
239 * Same as tryReadString, but insists on success.
240 */
241 void
242 readString(std::string &str, Addr addr) const
243 {
244 if (!tryReadString(str, addr))
245 fatal("readString(%#x, ...) failed", addr);
246 }
247
248 /**
249 * Reads the string at guest address addr into the char * str, reading up
250 * to maxlen characters. The last character read is always a nul
251 * terminator. Returns true on success and false on failure.
252 */
253 bool tryReadString(char *str, Addr addr, size_t maxlen) const;
254
255 /**
256 * Same as tryReadString, but insists on success.
257 */
258 void
259 readString(char *str, Addr addr, size_t maxlen) const
260 {
261 if (!tryReadString(str, addr, maxlen))
262 fatal("readString(%#x, ...) failed", addr);
263 }
264};
265
266
267template <typename T>
268T
269PortProxy::read(Addr address) const
270{
271 T data;
272 readBlob(address, &data, sizeof(T));
273 return data;
274}
275
276template <typename T>
277void
278PortProxy::write(Addr address, const T &data) const
279{
280 writeBlob(address, &data, sizeof(T));
281}
282
283template <typename T>
284T
285PortProxy::read(Addr address, ByteOrder byte_order) const
286{
287 T data;
288 readBlob(address, &data, sizeof(T));
289 return gtoh(data, byte_order);
290}
291
292template <typename T>
293void
294PortProxy::write(Addr address, T data, ByteOrder byte_order) const
295{
296 data = htog(data, byte_order);
297 writeBlob(address, &data, sizeof(T));
298}
299
300#endif // __MEM_PORT_PROXY_HH__
110 virtual ~PortProxy() { }
111
112
113
114 /** Fixed functionality for use in base classes. */
115
116 /**
117 * Read size bytes memory at physical address and store in p.
118 */
119 void readBlobPhys(Addr addr, Request::Flags flags,
120 void *p, int size) const;
121
122 /**
123 * Write size bytes from p to physical address.
124 */
125 void writeBlobPhys(Addr addr, Request::Flags flags,
126 const void *p, int size) const;
127
128 /**
129 * Fill size bytes starting at physical addr with byte value val.
130 */
131 void memsetBlobPhys(Addr addr, Request::Flags flags,
132 uint8_t v, int size) const;
133
134
135
136 /** Methods to override in base classes */
137
138 /**
139 * Read size bytes memory at address and store in p.
140 * Returns true on success and false on failure.
141 */
142 virtual bool
143 tryReadBlob(Addr addr, void *p, int size) const
144 {
145 readBlobPhys(addr, 0, p, size);
146 return true;
147 }
148
149 /**
150 * Write size bytes from p to address.
151 * Returns true on success and false on failure.
152 */
153 virtual bool
154 tryWriteBlob(Addr addr, const void *p, int size) const
155 {
156 writeBlobPhys(addr, 0, p, size);
157 return true;
158 }
159
160 /**
161 * Fill size bytes starting at addr with byte value val.
162 * Returns true on success and false on failure.
163 */
164 virtual bool
165 tryMemsetBlob(Addr addr, uint8_t val, int size) const
166 {
167 memsetBlobPhys(addr, 0, val, size);
168 return true;
169 }
170
171
172
173 /** Higher level interfaces based on the above. */
174
175 /**
176 * Same as tryReadBlob, but insists on success.
177 */
178 void
179 readBlob(Addr addr, void *p, int size) const
180 {
181 if (!tryReadBlob(addr, p, size))
182 fatal("readBlob(%#x, ...) failed", addr);
183 }
184
185 /**
186 * Same as tryWriteBlob, but insists on success.
187 */
188 void
189 writeBlob(Addr addr, const void *p, int size) const
190 {
191 if (!tryWriteBlob(addr, p, size))
192 fatal("writeBlob(%#x, ...) failed", addr);
193 }
194
195 /**
196 * Same as tryMemsetBlob, but insists on success.
197 */
198 void
199 memsetBlob(Addr addr, uint8_t v, int size) const
200 {
201 if (!tryMemsetBlob(addr, v, size))
202 fatal("memsetBlob(%#x, ...) failed", addr);
203 }
204
205 /**
206 * Read sizeof(T) bytes from address and return as object T.
207 */
208 template <typename T>
209 T read(Addr address) const;
210
211 /**
212 * Write object T to address. Writes sizeof(T) bytes.
213 */
214 template <typename T>
215 void write(Addr address, const T &data) const;
216
217 /**
218 * Read sizeof(T) bytes from address and return as object T.
219 * Performs endianness conversion from the selected guest to host order.
220 */
221 template <typename T>
222 T read(Addr address, ByteOrder guest_byte_order) const;
223
224 /**
225 * Write object T to address. Writes sizeof(T) bytes.
226 * Performs endianness conversion from host to the selected guest order.
227 */
228 template <typename T>
229 void write(Addr address, T data, ByteOrder guest_byte_order) const;
230
231 /**
232 * Write the string str into guest memory at address addr.
233 * Returns true on success and false on failure.
234 */
235 bool tryWriteString(Addr addr, const char *str) const;
236
237 /**
238 * Same as tryWriteString, but insists on success.
239 */
240 void
241 writeString(Addr addr, const char *str) const
242 {
243 if (!tryWriteString(addr, str))
244 fatal("writeString(%#x, ...) failed", addr);
245 }
246
247 /**
248 * Reads the string at guest address addr into the std::string str.
249 * Returns true on success and false on failure.
250 */
251 bool tryReadString(std::string &str, Addr addr) const;
252
253 /**
254 * Same as tryReadString, but insists on success.
255 */
256 void
257 readString(std::string &str, Addr addr) const
258 {
259 if (!tryReadString(str, addr))
260 fatal("readString(%#x, ...) failed", addr);
261 }
262
263 /**
264 * Reads the string at guest address addr into the char * str, reading up
265 * to maxlen characters. The last character read is always a nul
266 * terminator. Returns true on success and false on failure.
267 */
268 bool tryReadString(char *str, Addr addr, size_t maxlen) const;
269
270 /**
271 * Same as tryReadString, but insists on success.
272 */
273 void
274 readString(char *str, Addr addr, size_t maxlen) const
275 {
276 if (!tryReadString(str, addr, maxlen))
277 fatal("readString(%#x, ...) failed", addr);
278 }
279};
280
281
282template <typename T>
283T
284PortProxy::read(Addr address) const
285{
286 T data;
287 readBlob(address, &data, sizeof(T));
288 return data;
289}
290
291template <typename T>
292void
293PortProxy::write(Addr address, const T &data) const
294{
295 writeBlob(address, &data, sizeof(T));
296}
297
298template <typename T>
299T
300PortProxy::read(Addr address, ByteOrder byte_order) const
301{
302 T data;
303 readBlob(address, &data, sizeof(T));
304 return gtoh(data, byte_order);
305}
306
307template <typename T>
308void
309PortProxy::write(Addr address, T data, ByteOrder byte_order) const
310{
311 data = htog(data, byte_order);
312 writeBlob(address, &data, sizeof(T));
313}
314
315#endif // __MEM_PORT_PROXY_HH__