packet.hh (7464:8d92c2737ac8) packet.hh (7465:f97b62be544f)
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advancec Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ron Dreslinski
29 * Steve Reinhardt
30 * Ali Saidi
31 */
32
33/**
34 * @file
35 * Declaration of the Packet class.
36 */
37
38#ifndef __MEM_PACKET_HH__
39#define __MEM_PACKET_HH__
40
41#include <cassert>
42#include <list>
43#include <bitset>
44
45#include "base/cast.hh"
46#include "base/compiler.hh"
47#include "base/fast_alloc.hh"
48#include "base/flags.hh"
49#include "base/misc.hh"
50#include "base/printable.hh"
51#include "base/types.hh"
52#include "mem/request.hh"
53#include "sim/core.hh"
54
55struct Packet;
56typedef Packet *PacketPtr;
57typedef uint8_t* PacketDataPtr;
58typedef std::list<PacketPtr> PacketList;
59
60class MemCmd
61{
62 friend class Packet;
63
64 public:
65 /**
66 * List of all commands associated with a packet.
67 */
68 enum Command
69 {
70 InvalidCmd,
71 ReadReq,
72 ReadResp,
73 ReadRespWithInvalidate,
74 WriteReq,
75 WriteResp,
76 Writeback,
77 SoftPFReq,
78 HardPFReq,
79 SoftPFResp,
80 HardPFResp,
81 WriteInvalidateReq,
82 WriteInvalidateResp,
83 UpgradeReq,
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Ron Dreslinski
30 * Steve Reinhardt
31 * Ali Saidi
32 */
33
34/**
35 * @file
36 * Declaration of the Packet class.
37 */
38
39#ifndef __MEM_PACKET_HH__
40#define __MEM_PACKET_HH__
41
42#include <cassert>
43#include <list>
44#include <bitset>
45
46#include "base/cast.hh"
47#include "base/compiler.hh"
48#include "base/fast_alloc.hh"
49#include "base/flags.hh"
50#include "base/misc.hh"
51#include "base/printable.hh"
52#include "base/types.hh"
53#include "mem/request.hh"
54#include "sim/core.hh"
55
56struct Packet;
57typedef Packet *PacketPtr;
58typedef uint8_t* PacketDataPtr;
59typedef std::list<PacketPtr> PacketList;
60
61class MemCmd
62{
63 friend class Packet;
64
65 public:
66 /**
67 * List of all commands associated with a packet.
68 */
69 enum Command
70 {
71 InvalidCmd,
72 ReadReq,
73 ReadResp,
74 ReadRespWithInvalidate,
75 WriteReq,
76 WriteResp,
77 Writeback,
78 SoftPFReq,
79 HardPFReq,
80 SoftPFResp,
81 HardPFResp,
82 WriteInvalidateReq,
83 WriteInvalidateResp,
84 UpgradeReq,
85 SCUpgradeReq, // Special "weak" upgrade for StoreCond
84 UpgradeResp,
86 UpgradeResp,
87 SCUpgradeFailReq, // Failed SCUpgradeReq in MSHR (never sent)
88 UpgradeFailResp, // Valid for SCUpgradeReq only
85 ReadExReq,
86 ReadExResp,
87 LoadLockedReq,
88 StoreCondReq,
89 StoreCondResp,
90 SwapReq,
91 SwapResp,
92 MessageReq,
93 MessageResp,
94 // Error responses
95 // @TODO these should be classified as responses rather than
96 // requests; coding them as requests initially for backwards
97 // compatibility
98 NetworkNackError, // nacked at network layer (not by protocol)
99 InvalidDestError, // packet dest field invalid
100 BadAddressError, // memory address invalid
101 // Fake simulator-only commands
102 PrintReq, // Print state matching address
103 NUM_MEM_CMDS
104 };
105
106 private:
107 /**
108 * List of command attributes.
109 */
110 enum Attribute
111 {
112 IsRead, //!< Data flows from responder to requester
113 IsWrite, //!< Data flows from requester to responder
89 ReadExReq,
90 ReadExResp,
91 LoadLockedReq,
92 StoreCondReq,
93 StoreCondResp,
94 SwapReq,
95 SwapResp,
96 MessageReq,
97 MessageResp,
98 // Error responses
99 // @TODO these should be classified as responses rather than
100 // requests; coding them as requests initially for backwards
101 // compatibility
102 NetworkNackError, // nacked at network layer (not by protocol)
103 InvalidDestError, // packet dest field invalid
104 BadAddressError, // memory address invalid
105 // Fake simulator-only commands
106 PrintReq, // Print state matching address
107 NUM_MEM_CMDS
108 };
109
110 private:
111 /**
112 * List of command attributes.
113 */
114 enum Attribute
115 {
116 IsRead, //!< Data flows from responder to requester
117 IsWrite, //!< Data flows from requester to responder
118 IsUpgrade,
114 IsPrefetch, //!< Not a demand access
115 IsInvalidate,
116 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
117 IsRequest, //!< Issued by requester
118 IsResponse, //!< Issue by responder
119 NeedsResponse, //!< Requester needs response from target
120 IsSWPrefetch,
121 IsHWPrefetch,
122 IsLlsc, //!< Alpha/MIPS LL or SC access
123 HasData, //!< There is an associated payload
124 IsError, //!< Error response
125 IsPrint, //!< Print state matching address (for debugging)
126 NUM_COMMAND_ATTRIBUTES
127 };
128
129 /**
130 * Structure that defines attributes and other data associated
131 * with a Command.
132 */
133 struct CommandInfo
134 {
135 /// Set of attribute flags.
136 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
137 /// Corresponding response for requests; InvalidCmd if no
138 /// response is applicable.
139 const Command response;
140 /// String representation (for printing)
141 const std::string str;
142 };
143
144 /// Array to map Command enum to associated info.
145 static const CommandInfo commandInfo[];
146
147 private:
148
149 Command cmd;
150
151 bool
152 testCmdAttrib(MemCmd::Attribute attrib) const
153 {
154 return commandInfo[cmd].attributes[attrib] != 0;
155 }
156
157 public:
158
159 bool isRead() const { return testCmdAttrib(IsRead); }
119 IsPrefetch, //!< Not a demand access
120 IsInvalidate,
121 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
122 IsRequest, //!< Issued by requester
123 IsResponse, //!< Issue by responder
124 NeedsResponse, //!< Requester needs response from target
125 IsSWPrefetch,
126 IsHWPrefetch,
127 IsLlsc, //!< Alpha/MIPS LL or SC access
128 HasData, //!< There is an associated payload
129 IsError, //!< Error response
130 IsPrint, //!< Print state matching address (for debugging)
131 NUM_COMMAND_ATTRIBUTES
132 };
133
134 /**
135 * Structure that defines attributes and other data associated
136 * with a Command.
137 */
138 struct CommandInfo
139 {
140 /// Set of attribute flags.
141 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
142 /// Corresponding response for requests; InvalidCmd if no
143 /// response is applicable.
144 const Command response;
145 /// String representation (for printing)
146 const std::string str;
147 };
148
149 /// Array to map Command enum to associated info.
150 static const CommandInfo commandInfo[];
151
152 private:
153
154 Command cmd;
155
156 bool
157 testCmdAttrib(MemCmd::Attribute attrib) const
158 {
159 return commandInfo[cmd].attributes[attrib] != 0;
160 }
161
162 public:
163
164 bool isRead() const { return testCmdAttrib(IsRead); }
160 bool isWrite() const { return testCmdAttrib(IsWrite); }
165 bool isWrite() const { return testCmdAttrib(IsWrite); }
166 bool isUpgrade() const { return testCmdAttrib(IsUpgrade); }
161 bool isRequest() const { return testCmdAttrib(IsRequest); }
162 bool isResponse() const { return testCmdAttrib(IsResponse); }
163 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
164 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
165 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
166 bool hasData() const { return testCmdAttrib(HasData); }
167 bool isReadWrite() const { return isRead() && isWrite(); }
168 bool isLLSC() const { return testCmdAttrib(IsLlsc); }
169 bool isError() const { return testCmdAttrib(IsError); }
170 bool isPrint() const { return testCmdAttrib(IsPrint); }
171
172 const Command
173 responseCommand() const
174 {
175 return commandInfo[cmd].response;
176 }
177
178 /// Return the string to a cmd given by idx.
179 const std::string &toString() const { return commandInfo[cmd].str; }
180 int toInt() const { return (int)cmd; }
181
182 MemCmd(Command _cmd) : cmd(_cmd) { }
183 MemCmd(int _cmd) : cmd((Command)_cmd) { }
184 MemCmd() : cmd(InvalidCmd) { }
185
186 bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
187 bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
188};
189
190/**
191 * A Packet is used to encapsulate a transfer between two objects in
192 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
193 * single Request travels all the way from the requester to the
194 * ultimate destination and back, possibly being conveyed by several
195 * different Packets along the way.)
196 */
197class Packet : public FastAlloc, public Printable
198{
199 public:
200 typedef uint32_t FlagsType;
201 typedef ::Flags<FlagsType> Flags;
202 typedef short NodeID;
203
204 private:
205 static const FlagsType PUBLIC_FLAGS = 0x00000000;
206 static const FlagsType PRIVATE_FLAGS = 0x00007F0F;
207 static const FlagsType COPY_FLAGS = 0x0000000F;
208
209 static const FlagsType SHARED = 0x00000001;
210 // Special control flags
211 /// Special timing-mode atomic snoop for multi-level coherence.
212 static const FlagsType EXPRESS_SNOOP = 0x00000002;
213 /// Does supplier have exclusive copy?
214 /// Useful for multi-level coherence.
215 static const FlagsType SUPPLY_EXCLUSIVE = 0x00000004;
216 // Snoop response flags
217 static const FlagsType MEM_INHIBIT = 0x00000008;
218 /// Are the 'addr' and 'size' fields valid?
219 static const FlagsType VALID_ADDR = 0x00000100;
220 static const FlagsType VALID_SIZE = 0x00000200;
221 /// Is the 'src' field valid?
222 static const FlagsType VALID_SRC = 0x00000400;
223 static const FlagsType VALID_DST = 0x00000800;
224 /// Is the data pointer set to a value that shouldn't be freed
225 /// when the packet is destroyed?
226 static const FlagsType STATIC_DATA = 0x00001000;
227 /// The data pointer points to a value that should be freed when
228 /// the packet is destroyed.
229 static const FlagsType DYNAMIC_DATA = 0x00002000;
230 /// the data pointer points to an array (thus delete []) needs to
231 /// be called on it rather than simply delete.
232 static const FlagsType ARRAY_DATA = 0x00004000;
233
234 Flags flags;
235
236 public:
237 typedef MemCmd::Command Command;
238
239 /// The command field of the packet.
240 MemCmd cmd;
241
242 /// A pointer to the original request.
243 RequestPtr req;
244
245 private:
246 /**
247 * A pointer to the data being transfered. It can be differnt
248 * sizes at each level of the heirarchy so it belongs in the
249 * packet, not request. This may or may not be populated when a
250 * responder recieves the packet. If not populated it memory should
251 * be allocated.
252 */
253 PacketDataPtr data;
254
255 /// The address of the request. This address could be virtual or
256 /// physical, depending on the system configuration.
257 Addr addr;
258
259 /// The size of the request or transfer.
260 unsigned size;
261
262 /**
263 * Device address (e.g., bus ID) of the source of the
264 * transaction. The source is not responsible for setting this
265 * field; it is set implicitly by the interconnect when the packet
266 * is first sent.
267 */
268 NodeID src;
269
270 /**
271 * Device address (e.g., bus ID) of the destination of the
272 * transaction. The special value Broadcast indicates that the
273 * packet should be routed based on its address. This field is
274 * initialized in the constructor and is thus always valid (unlike
275 * addr, size, and src).
276 */
277 NodeID dest;
278
279 /**
280 * The original value of the command field. Only valid when the
281 * current command field is an error condition; in that case, the
282 * previous contents of the command field are copied here. This
283 * field is *not* set on non-error responses.
284 */
285 MemCmd origCmd;
286
287 public:
288 /// Used to calculate latencies for each packet.
289 Tick time;
290
291 /// The time at which the packet will be fully transmitted
292 Tick finishTime;
293
294 /// The time at which the first chunk of the packet will be transmitted
295 Tick firstWordTime;
296
297 /// The special destination address indicating that the packet
298 /// should be routed based on its address.
299 static const NodeID Broadcast = -1;
300
301 /**
302 * A virtual base opaque structure used to hold state associated
303 * with the packet but specific to the sending device (e.g., an
304 * MSHR). A pointer to this state is returned in the packet's
305 * response so that the sender can quickly look up the state
306 * needed to process it. A specific subclass would be derived
307 * from this to carry state specific to a particular sending
308 * device.
309 */
310 struct SenderState
311 {
312 virtual ~SenderState() {}
313 };
314
315 /**
316 * Object used to maintain state of a PrintReq. The senderState
317 * field of a PrintReq should always be of this type.
318 */
319 class PrintReqState : public SenderState, public FastAlloc
320 {
321 private:
322 /**
323 * An entry in the label stack.
324 */
325 struct LabelStackEntry
326 {
327 const std::string label;
328 std::string *prefix;
329 bool labelPrinted;
330 LabelStackEntry(const std::string &_label, std::string *_prefix);
331 };
332
333 typedef std::list<LabelStackEntry> LabelStack;
334 LabelStack labelStack;
335
336 std::string *curPrefixPtr;
337
338 public:
339 std::ostream &os;
340 const int verbosity;
341
342 PrintReqState(std::ostream &os, int verbosity = 0);
343 ~PrintReqState();
344
345 /**
346 * Returns the current line prefix.
347 */
348 const std::string &curPrefix() { return *curPrefixPtr; }
349
350 /**
351 * Push a label onto the label stack, and prepend the given
352 * prefix string onto the current prefix. Labels will only be
353 * printed if an object within the label's scope is printed.
354 */
355 void pushLabel(const std::string &lbl,
356 const std::string &prefix = " ");
357
358 /**
359 * Pop a label off the label stack.
360 */
361 void popLabel();
362
363 /**
364 * Print all of the pending unprinted labels on the
365 * stack. Called by printObj(), so normally not called by
366 * users unless bypassing printObj().
367 */
368 void printLabels();
369
370 /**
371 * Print a Printable object to os, because it matched the
372 * address on a PrintReq.
373 */
374 void printObj(Printable *obj);
375 };
376
377 /**
378 * This packet's sender state. Devices should use dynamic_cast<>
379 * to cast to the state appropriate to the sender. The intent of
380 * this variable is to allow a device to attach extra information
381 * to a request. A response packet must return the sender state
382 * that was attached to the original request (even if a new packet
383 * is created).
384 */
385 SenderState *senderState;
386
387 /// Return the string name of the cmd field (for debugging and
388 /// tracing).
389 const std::string &cmdString() const { return cmd.toString(); }
390
391 /// Return the index of this command.
392 inline int cmdToIndex() const { return cmd.toInt(); }
393
394 bool isRead() const { return cmd.isRead(); }
167 bool isRequest() const { return testCmdAttrib(IsRequest); }
168 bool isResponse() const { return testCmdAttrib(IsResponse); }
169 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
170 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
171 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
172 bool hasData() const { return testCmdAttrib(HasData); }
173 bool isReadWrite() const { return isRead() && isWrite(); }
174 bool isLLSC() const { return testCmdAttrib(IsLlsc); }
175 bool isError() const { return testCmdAttrib(IsError); }
176 bool isPrint() const { return testCmdAttrib(IsPrint); }
177
178 const Command
179 responseCommand() const
180 {
181 return commandInfo[cmd].response;
182 }
183
184 /// Return the string to a cmd given by idx.
185 const std::string &toString() const { return commandInfo[cmd].str; }
186 int toInt() const { return (int)cmd; }
187
188 MemCmd(Command _cmd) : cmd(_cmd) { }
189 MemCmd(int _cmd) : cmd((Command)_cmd) { }
190 MemCmd() : cmd(InvalidCmd) { }
191
192 bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
193 bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
194};
195
196/**
197 * A Packet is used to encapsulate a transfer between two objects in
198 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
199 * single Request travels all the way from the requester to the
200 * ultimate destination and back, possibly being conveyed by several
201 * different Packets along the way.)
202 */
203class Packet : public FastAlloc, public Printable
204{
205 public:
206 typedef uint32_t FlagsType;
207 typedef ::Flags<FlagsType> Flags;
208 typedef short NodeID;
209
210 private:
211 static const FlagsType PUBLIC_FLAGS = 0x00000000;
212 static const FlagsType PRIVATE_FLAGS = 0x00007F0F;
213 static const FlagsType COPY_FLAGS = 0x0000000F;
214
215 static const FlagsType SHARED = 0x00000001;
216 // Special control flags
217 /// Special timing-mode atomic snoop for multi-level coherence.
218 static const FlagsType EXPRESS_SNOOP = 0x00000002;
219 /// Does supplier have exclusive copy?
220 /// Useful for multi-level coherence.
221 static const FlagsType SUPPLY_EXCLUSIVE = 0x00000004;
222 // Snoop response flags
223 static const FlagsType MEM_INHIBIT = 0x00000008;
224 /// Are the 'addr' and 'size' fields valid?
225 static const FlagsType VALID_ADDR = 0x00000100;
226 static const FlagsType VALID_SIZE = 0x00000200;
227 /// Is the 'src' field valid?
228 static const FlagsType VALID_SRC = 0x00000400;
229 static const FlagsType VALID_DST = 0x00000800;
230 /// Is the data pointer set to a value that shouldn't be freed
231 /// when the packet is destroyed?
232 static const FlagsType STATIC_DATA = 0x00001000;
233 /// The data pointer points to a value that should be freed when
234 /// the packet is destroyed.
235 static const FlagsType DYNAMIC_DATA = 0x00002000;
236 /// the data pointer points to an array (thus delete []) needs to
237 /// be called on it rather than simply delete.
238 static const FlagsType ARRAY_DATA = 0x00004000;
239
240 Flags flags;
241
242 public:
243 typedef MemCmd::Command Command;
244
245 /// The command field of the packet.
246 MemCmd cmd;
247
248 /// A pointer to the original request.
249 RequestPtr req;
250
251 private:
252 /**
253 * A pointer to the data being transfered. It can be differnt
254 * sizes at each level of the heirarchy so it belongs in the
255 * packet, not request. This may or may not be populated when a
256 * responder recieves the packet. If not populated it memory should
257 * be allocated.
258 */
259 PacketDataPtr data;
260
261 /// The address of the request. This address could be virtual or
262 /// physical, depending on the system configuration.
263 Addr addr;
264
265 /// The size of the request or transfer.
266 unsigned size;
267
268 /**
269 * Device address (e.g., bus ID) of the source of the
270 * transaction. The source is not responsible for setting this
271 * field; it is set implicitly by the interconnect when the packet
272 * is first sent.
273 */
274 NodeID src;
275
276 /**
277 * Device address (e.g., bus ID) of the destination of the
278 * transaction. The special value Broadcast indicates that the
279 * packet should be routed based on its address. This field is
280 * initialized in the constructor and is thus always valid (unlike
281 * addr, size, and src).
282 */
283 NodeID dest;
284
285 /**
286 * The original value of the command field. Only valid when the
287 * current command field is an error condition; in that case, the
288 * previous contents of the command field are copied here. This
289 * field is *not* set on non-error responses.
290 */
291 MemCmd origCmd;
292
293 public:
294 /// Used to calculate latencies for each packet.
295 Tick time;
296
297 /// The time at which the packet will be fully transmitted
298 Tick finishTime;
299
300 /// The time at which the first chunk of the packet will be transmitted
301 Tick firstWordTime;
302
303 /// The special destination address indicating that the packet
304 /// should be routed based on its address.
305 static const NodeID Broadcast = -1;
306
307 /**
308 * A virtual base opaque structure used to hold state associated
309 * with the packet but specific to the sending device (e.g., an
310 * MSHR). A pointer to this state is returned in the packet's
311 * response so that the sender can quickly look up the state
312 * needed to process it. A specific subclass would be derived
313 * from this to carry state specific to a particular sending
314 * device.
315 */
316 struct SenderState
317 {
318 virtual ~SenderState() {}
319 };
320
321 /**
322 * Object used to maintain state of a PrintReq. The senderState
323 * field of a PrintReq should always be of this type.
324 */
325 class PrintReqState : public SenderState, public FastAlloc
326 {
327 private:
328 /**
329 * An entry in the label stack.
330 */
331 struct LabelStackEntry
332 {
333 const std::string label;
334 std::string *prefix;
335 bool labelPrinted;
336 LabelStackEntry(const std::string &_label, std::string *_prefix);
337 };
338
339 typedef std::list<LabelStackEntry> LabelStack;
340 LabelStack labelStack;
341
342 std::string *curPrefixPtr;
343
344 public:
345 std::ostream &os;
346 const int verbosity;
347
348 PrintReqState(std::ostream &os, int verbosity = 0);
349 ~PrintReqState();
350
351 /**
352 * Returns the current line prefix.
353 */
354 const std::string &curPrefix() { return *curPrefixPtr; }
355
356 /**
357 * Push a label onto the label stack, and prepend the given
358 * prefix string onto the current prefix. Labels will only be
359 * printed if an object within the label's scope is printed.
360 */
361 void pushLabel(const std::string &lbl,
362 const std::string &prefix = " ");
363
364 /**
365 * Pop a label off the label stack.
366 */
367 void popLabel();
368
369 /**
370 * Print all of the pending unprinted labels on the
371 * stack. Called by printObj(), so normally not called by
372 * users unless bypassing printObj().
373 */
374 void printLabels();
375
376 /**
377 * Print a Printable object to os, because it matched the
378 * address on a PrintReq.
379 */
380 void printObj(Printable *obj);
381 };
382
383 /**
384 * This packet's sender state. Devices should use dynamic_cast<>
385 * to cast to the state appropriate to the sender. The intent of
386 * this variable is to allow a device to attach extra information
387 * to a request. A response packet must return the sender state
388 * that was attached to the original request (even if a new packet
389 * is created).
390 */
391 SenderState *senderState;
392
393 /// Return the string name of the cmd field (for debugging and
394 /// tracing).
395 const std::string &cmdString() const { return cmd.toString(); }
396
397 /// Return the index of this command.
398 inline int cmdToIndex() const { return cmd.toInt(); }
399
400 bool isRead() const { return cmd.isRead(); }
395 bool isWrite() const { return cmd.isWrite(); }
401 bool isWrite() const { return cmd.isWrite(); }
402 bool isUpgrade() const { return cmd.isUpgrade(); }
396 bool isRequest() const { return cmd.isRequest(); }
397 bool isResponse() const { return cmd.isResponse(); }
398 bool needsExclusive() const { return cmd.needsExclusive(); }
399 bool needsResponse() const { return cmd.needsResponse(); }
400 bool isInvalidate() const { return cmd.isInvalidate(); }
401 bool hasData() const { return cmd.hasData(); }
402 bool isReadWrite() const { return cmd.isReadWrite(); }
403 bool isLLSC() const { return cmd.isLLSC(); }
404 bool isError() const { return cmd.isError(); }
405 bool isPrint() const { return cmd.isPrint(); }
406
407 // Snoop flags
408 void assertMemInhibit() { flags.set(MEM_INHIBIT); }
409 bool memInhibitAsserted() { return flags.isSet(MEM_INHIBIT); }
410 void assertShared() { flags.set(SHARED); }
411 bool sharedAsserted() { return flags.isSet(SHARED); }
412
413 // Special control flags
414 void setExpressSnoop() { flags.set(EXPRESS_SNOOP); }
415 bool isExpressSnoop() { return flags.isSet(EXPRESS_SNOOP); }
416 void setSupplyExclusive() { flags.set(SUPPLY_EXCLUSIVE); }
417 bool isSupplyExclusive() { return flags.isSet(SUPPLY_EXCLUSIVE); }
418
419 // Network error conditions... encapsulate them as methods since
420 // their encoding keeps changing (from result field to command
421 // field, etc.)
422 void
423 setNacked()
424 {
425 assert(isResponse());
426 cmd = MemCmd::NetworkNackError;
427 }
428
429 void
430 setBadAddress()
431 {
432 assert(isResponse());
433 cmd = MemCmd::BadAddressError;
434 }
435
436 bool wasNacked() const { return cmd == MemCmd::NetworkNackError; }
437 bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
438 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
439
440 bool isSrcValid() { return flags.isSet(VALID_SRC); }
441 /// Accessor function to get the source index of the packet.
442 NodeID getSrc() const { assert(flags.isSet(VALID_SRC)); return src; }
443 /// Accessor function to set the source index of the packet.
444 void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
445 /// Reset source field, e.g. to retransmit packet on different bus.
446 void clearSrc() { flags.clear(VALID_SRC); }
447
448 bool isDestValid() { return flags.isSet(VALID_DST); }
449 /// Accessor function for the destination index of the packet.
450 NodeID getDest() const { assert(flags.isSet(VALID_DST)); return dest; }
451 /// Accessor function to set the destination index of the packet.
452 void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
453
454 Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
455 unsigned getSize() const { assert(flags.isSet(VALID_SIZE)); return size; }
456 Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
457
458 /**
459 * Constructor. Note that a Request object must be constructed
460 * first, but the Requests's physical address and size fields need
461 * not be valid. The command and destination addresses must be
462 * supplied.
463 */
464 Packet(Request *_req, MemCmd _cmd, NodeID _dest)
465 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
466 dest(_dest), time(curTick), senderState(NULL)
467 {
468 if (req->hasPaddr()) {
469 addr = req->getPaddr();
470 flags.set(VALID_ADDR);
471 }
472 if (req->hasSize()) {
473 size = req->getSize();
474 flags.set(VALID_SIZE);
475 }
476 }
477
478 /**
479 * Alternate constructor if you are trying to create a packet with
480 * a request that is for a whole block, not the address from the
481 * req. this allows for overriding the size/addr of the req.
482 */
483 Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
484 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
485 dest(_dest), time(curTick), senderState(NULL)
486 {
487 if (req->hasPaddr()) {
488 addr = req->getPaddr() & ~(_blkSize - 1);
489 flags.set(VALID_ADDR);
490 }
491 size = _blkSize;
492 flags.set(VALID_SIZE);
493 }
494
495 /**
496 * Alternate constructor for copying a packet. Copy all fields
497 * *except* if the original packet's data was dynamic, don't copy
498 * that, as we can't guarantee that the new packet's lifetime is
499 * less than that of the original packet. In this case the new
500 * packet should allocate its own data.
501 */
502 Packet(Packet *pkt, bool clearFlags = false)
503 : cmd(pkt->cmd), req(pkt->req),
504 data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
505 addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
506 time(curTick), senderState(pkt->senderState)
507 {
508 if (!clearFlags)
509 flags.set(pkt->flags & COPY_FLAGS);
510
511 flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
512 flags.set(pkt->flags & STATIC_DATA);
513 }
514
515 /**
516 * clean up packet variables
517 */
518 ~Packet()
519 {
520 // If this is a request packet for which there's no response,
521 // delete the request object here, since the requester will
522 // never get the chance.
523 if (req && isRequest() && !needsResponse())
524 delete req;
525 deleteData();
526 }
527
528 /**
529 * Reinitialize packet address and size from the associated
530 * Request object, and reset other fields that may have been
531 * modified by a previous transaction. Typically called when a
532 * statically allocated Request/Packet pair is reused for multiple
533 * transactions.
534 */
535 void
536 reinitFromRequest()
537 {
538 assert(req->hasPaddr());
539 flags = 0;
540 addr = req->getPaddr();
541 size = req->getSize();
542 time = req->time();
543
544 flags.set(VALID_ADDR|VALID_SIZE);
545 deleteData();
546 }
547
548 /**
549 * Take a request packet and modify it in place to be suitable for
550 * returning as a response to that request. The source and
551 * destination fields are *not* modified, as is appropriate for
552 * atomic accesses.
553 */
554 void
555 makeResponse()
556 {
557 assert(needsResponse());
558 assert(isRequest());
559 origCmd = cmd;
560 cmd = cmd.responseCommand();
561
562 // responses are never express, even if the snoop that
563 // triggered them was
564 flags.clear(EXPRESS_SNOOP);
565
566 dest = src;
567 flags.set(VALID_DST, flags.isSet(VALID_SRC));
568 flags.clear(VALID_SRC);
569 }
570
571 void
572 makeAtomicResponse()
573 {
574 makeResponse();
575 }
576
577 void
578 makeTimingResponse()
579 {
580 makeResponse();
581 }
582
583 /**
584 * Take a request packet that has been returned as NACKED and
585 * modify it so that it can be sent out again. Only packets that
586 * need a response can be NACKED, so verify that that is true.
587 */
588 void
589 reinitNacked()
590 {
591 assert(wasNacked());
592 cmd = origCmd;
593 assert(needsResponse());
594 setDest(Broadcast);
595 }
596
597 void
598 setSize(unsigned size)
599 {
600 assert(!flags.isSet(VALID_SIZE));
601
602 this->size = size;
603 flags.set(VALID_SIZE);
604 }
605
606
607 /**
608 * Set the data pointer to the following value that should not be
609 * freed.
610 */
611 template <typename T>
612 void
613 dataStatic(T *p)
614 {
615 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
616 data = (PacketDataPtr)p;
617 flags.set(STATIC_DATA);
618 }
619
620 /**
621 * Set the data pointer to a value that should have delete []
622 * called on it.
623 */
624 template <typename T>
625 void
626 dataDynamicArray(T *p)
627 {
628 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
629 data = (PacketDataPtr)p;
630 flags.set(DYNAMIC_DATA|ARRAY_DATA);
631 }
632
633 /**
634 * set the data pointer to a value that should have delete called
635 * on it.
636 */
637 template <typename T>
638 void
639 dataDynamic(T *p)
640 {
641 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
642 data = (PacketDataPtr)p;
643 flags.set(DYNAMIC_DATA);
644 }
645
646 /**
647 * get a pointer to the data ptr.
648 */
649 template <typename T>
650 T*
651 getPtr()
652 {
653 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
654 return (T*)data;
655 }
656
657 /**
658 * return the value of what is pointed to in the packet.
659 */
660 template <typename T>
661 T get();
662
663 /**
664 * set the value in the data pointer to v.
665 */
666 template <typename T>
667 void set(T v);
668
669 /**
670 * Copy data into the packet from the provided pointer.
671 */
672 void
673 setData(uint8_t *p)
674 {
675 std::memcpy(getPtr<uint8_t>(), p, getSize());
676 }
677
678 /**
679 * Copy data into the packet from the provided block pointer,
680 * which is aligned to the given block size.
681 */
682 void
683 setDataFromBlock(uint8_t *blk_data, int blkSize)
684 {
685 setData(blk_data + getOffset(blkSize));
686 }
687
688 /**
689 * Copy data from the packet to the provided block pointer, which
690 * is aligned to the given block size.
691 */
692 void
693 writeData(uint8_t *p)
694 {
695 std::memcpy(p, getPtr<uint8_t>(), getSize());
696 }
697
698 /**
699 * Copy data from the packet to the memory at the provided pointer.
700 */
701 void
702 writeDataToBlock(uint8_t *blk_data, int blkSize)
703 {
704 writeData(blk_data + getOffset(blkSize));
705 }
706
707 /**
708 * delete the data pointed to in the data pointer. Ok to call to
709 * matter how data was allocted.
710 */
711 void
712 deleteData()
713 {
714 if (flags.isSet(ARRAY_DATA))
715 delete [] data;
716 else if (flags.isSet(DYNAMIC_DATA))
717 delete data;
718
719 flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
720 data = NULL;
721 }
722
723 /** If there isn't data in the packet, allocate some. */
724 void
725 allocate()
726 {
727 if (data) {
728 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
729 return;
730 }
731
732 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
733 flags.set(DYNAMIC_DATA|ARRAY_DATA);
734 data = new uint8_t[getSize()];
735 }
736
737
738 /**
739 * Check a functional request against a memory value represented
740 * by a base/size pair and an associated data array. If the
741 * functional request is a read, it may be satisfied by the memory
742 * value. If the functional request is a write, it may update the
743 * memory value.
744 */
745 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
746
747 /**
748 * Check a functional request against a memory value stored in
749 * another packet (i.e. an in-transit request or response).
750 */
751 bool
752 checkFunctional(PacketPtr other)
753 {
754 uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
755 return checkFunctional(other, other->getAddr(), other->getSize(),
756 data);
757 }
758
759 /**
760 * Push label for PrintReq (safe to call unconditionally).
761 */
762 void
763 pushLabel(const std::string &lbl)
764 {
765 if (isPrint())
766 safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
767 }
768
769 /**
770 * Pop label for PrintReq (safe to call unconditionally).
771 */
772 void
773 popLabel()
774 {
775 if (isPrint())
776 safe_cast<PrintReqState*>(senderState)->popLabel();
777 }
778
779 void print(std::ostream &o, int verbosity = 0,
780 const std::string &prefix = "") const;
781};
782
783#endif //__MEM_PACKET_HH
403 bool isRequest() const { return cmd.isRequest(); }
404 bool isResponse() const { return cmd.isResponse(); }
405 bool needsExclusive() const { return cmd.needsExclusive(); }
406 bool needsResponse() const { return cmd.needsResponse(); }
407 bool isInvalidate() const { return cmd.isInvalidate(); }
408 bool hasData() const { return cmd.hasData(); }
409 bool isReadWrite() const { return cmd.isReadWrite(); }
410 bool isLLSC() const { return cmd.isLLSC(); }
411 bool isError() const { return cmd.isError(); }
412 bool isPrint() const { return cmd.isPrint(); }
413
414 // Snoop flags
415 void assertMemInhibit() { flags.set(MEM_INHIBIT); }
416 bool memInhibitAsserted() { return flags.isSet(MEM_INHIBIT); }
417 void assertShared() { flags.set(SHARED); }
418 bool sharedAsserted() { return flags.isSet(SHARED); }
419
420 // Special control flags
421 void setExpressSnoop() { flags.set(EXPRESS_SNOOP); }
422 bool isExpressSnoop() { return flags.isSet(EXPRESS_SNOOP); }
423 void setSupplyExclusive() { flags.set(SUPPLY_EXCLUSIVE); }
424 bool isSupplyExclusive() { return flags.isSet(SUPPLY_EXCLUSIVE); }
425
426 // Network error conditions... encapsulate them as methods since
427 // their encoding keeps changing (from result field to command
428 // field, etc.)
429 void
430 setNacked()
431 {
432 assert(isResponse());
433 cmd = MemCmd::NetworkNackError;
434 }
435
436 void
437 setBadAddress()
438 {
439 assert(isResponse());
440 cmd = MemCmd::BadAddressError;
441 }
442
443 bool wasNacked() const { return cmd == MemCmd::NetworkNackError; }
444 bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
445 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
446
447 bool isSrcValid() { return flags.isSet(VALID_SRC); }
448 /// Accessor function to get the source index of the packet.
449 NodeID getSrc() const { assert(flags.isSet(VALID_SRC)); return src; }
450 /// Accessor function to set the source index of the packet.
451 void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
452 /// Reset source field, e.g. to retransmit packet on different bus.
453 void clearSrc() { flags.clear(VALID_SRC); }
454
455 bool isDestValid() { return flags.isSet(VALID_DST); }
456 /// Accessor function for the destination index of the packet.
457 NodeID getDest() const { assert(flags.isSet(VALID_DST)); return dest; }
458 /// Accessor function to set the destination index of the packet.
459 void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
460
461 Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
462 unsigned getSize() const { assert(flags.isSet(VALID_SIZE)); return size; }
463 Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
464
465 /**
466 * Constructor. Note that a Request object must be constructed
467 * first, but the Requests's physical address and size fields need
468 * not be valid. The command and destination addresses must be
469 * supplied.
470 */
471 Packet(Request *_req, MemCmd _cmd, NodeID _dest)
472 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
473 dest(_dest), time(curTick), senderState(NULL)
474 {
475 if (req->hasPaddr()) {
476 addr = req->getPaddr();
477 flags.set(VALID_ADDR);
478 }
479 if (req->hasSize()) {
480 size = req->getSize();
481 flags.set(VALID_SIZE);
482 }
483 }
484
485 /**
486 * Alternate constructor if you are trying to create a packet with
487 * a request that is for a whole block, not the address from the
488 * req. this allows for overriding the size/addr of the req.
489 */
490 Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
491 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
492 dest(_dest), time(curTick), senderState(NULL)
493 {
494 if (req->hasPaddr()) {
495 addr = req->getPaddr() & ~(_blkSize - 1);
496 flags.set(VALID_ADDR);
497 }
498 size = _blkSize;
499 flags.set(VALID_SIZE);
500 }
501
502 /**
503 * Alternate constructor for copying a packet. Copy all fields
504 * *except* if the original packet's data was dynamic, don't copy
505 * that, as we can't guarantee that the new packet's lifetime is
506 * less than that of the original packet. In this case the new
507 * packet should allocate its own data.
508 */
509 Packet(Packet *pkt, bool clearFlags = false)
510 : cmd(pkt->cmd), req(pkt->req),
511 data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
512 addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
513 time(curTick), senderState(pkt->senderState)
514 {
515 if (!clearFlags)
516 flags.set(pkt->flags & COPY_FLAGS);
517
518 flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
519 flags.set(pkt->flags & STATIC_DATA);
520 }
521
522 /**
523 * clean up packet variables
524 */
525 ~Packet()
526 {
527 // If this is a request packet for which there's no response,
528 // delete the request object here, since the requester will
529 // never get the chance.
530 if (req && isRequest() && !needsResponse())
531 delete req;
532 deleteData();
533 }
534
535 /**
536 * Reinitialize packet address and size from the associated
537 * Request object, and reset other fields that may have been
538 * modified by a previous transaction. Typically called when a
539 * statically allocated Request/Packet pair is reused for multiple
540 * transactions.
541 */
542 void
543 reinitFromRequest()
544 {
545 assert(req->hasPaddr());
546 flags = 0;
547 addr = req->getPaddr();
548 size = req->getSize();
549 time = req->time();
550
551 flags.set(VALID_ADDR|VALID_SIZE);
552 deleteData();
553 }
554
555 /**
556 * Take a request packet and modify it in place to be suitable for
557 * returning as a response to that request. The source and
558 * destination fields are *not* modified, as is appropriate for
559 * atomic accesses.
560 */
561 void
562 makeResponse()
563 {
564 assert(needsResponse());
565 assert(isRequest());
566 origCmd = cmd;
567 cmd = cmd.responseCommand();
568
569 // responses are never express, even if the snoop that
570 // triggered them was
571 flags.clear(EXPRESS_SNOOP);
572
573 dest = src;
574 flags.set(VALID_DST, flags.isSet(VALID_SRC));
575 flags.clear(VALID_SRC);
576 }
577
578 void
579 makeAtomicResponse()
580 {
581 makeResponse();
582 }
583
584 void
585 makeTimingResponse()
586 {
587 makeResponse();
588 }
589
590 /**
591 * Take a request packet that has been returned as NACKED and
592 * modify it so that it can be sent out again. Only packets that
593 * need a response can be NACKED, so verify that that is true.
594 */
595 void
596 reinitNacked()
597 {
598 assert(wasNacked());
599 cmd = origCmd;
600 assert(needsResponse());
601 setDest(Broadcast);
602 }
603
604 void
605 setSize(unsigned size)
606 {
607 assert(!flags.isSet(VALID_SIZE));
608
609 this->size = size;
610 flags.set(VALID_SIZE);
611 }
612
613
614 /**
615 * Set the data pointer to the following value that should not be
616 * freed.
617 */
618 template <typename T>
619 void
620 dataStatic(T *p)
621 {
622 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
623 data = (PacketDataPtr)p;
624 flags.set(STATIC_DATA);
625 }
626
627 /**
628 * Set the data pointer to a value that should have delete []
629 * called on it.
630 */
631 template <typename T>
632 void
633 dataDynamicArray(T *p)
634 {
635 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
636 data = (PacketDataPtr)p;
637 flags.set(DYNAMIC_DATA|ARRAY_DATA);
638 }
639
640 /**
641 * set the data pointer to a value that should have delete called
642 * on it.
643 */
644 template <typename T>
645 void
646 dataDynamic(T *p)
647 {
648 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
649 data = (PacketDataPtr)p;
650 flags.set(DYNAMIC_DATA);
651 }
652
653 /**
654 * get a pointer to the data ptr.
655 */
656 template <typename T>
657 T*
658 getPtr()
659 {
660 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
661 return (T*)data;
662 }
663
664 /**
665 * return the value of what is pointed to in the packet.
666 */
667 template <typename T>
668 T get();
669
670 /**
671 * set the value in the data pointer to v.
672 */
673 template <typename T>
674 void set(T v);
675
676 /**
677 * Copy data into the packet from the provided pointer.
678 */
679 void
680 setData(uint8_t *p)
681 {
682 std::memcpy(getPtr<uint8_t>(), p, getSize());
683 }
684
685 /**
686 * Copy data into the packet from the provided block pointer,
687 * which is aligned to the given block size.
688 */
689 void
690 setDataFromBlock(uint8_t *blk_data, int blkSize)
691 {
692 setData(blk_data + getOffset(blkSize));
693 }
694
695 /**
696 * Copy data from the packet to the provided block pointer, which
697 * is aligned to the given block size.
698 */
699 void
700 writeData(uint8_t *p)
701 {
702 std::memcpy(p, getPtr<uint8_t>(), getSize());
703 }
704
705 /**
706 * Copy data from the packet to the memory at the provided pointer.
707 */
708 void
709 writeDataToBlock(uint8_t *blk_data, int blkSize)
710 {
711 writeData(blk_data + getOffset(blkSize));
712 }
713
714 /**
715 * delete the data pointed to in the data pointer. Ok to call to
716 * matter how data was allocted.
717 */
718 void
719 deleteData()
720 {
721 if (flags.isSet(ARRAY_DATA))
722 delete [] data;
723 else if (flags.isSet(DYNAMIC_DATA))
724 delete data;
725
726 flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
727 data = NULL;
728 }
729
730 /** If there isn't data in the packet, allocate some. */
731 void
732 allocate()
733 {
734 if (data) {
735 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
736 return;
737 }
738
739 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
740 flags.set(DYNAMIC_DATA|ARRAY_DATA);
741 data = new uint8_t[getSize()];
742 }
743
744
745 /**
746 * Check a functional request against a memory value represented
747 * by a base/size pair and an associated data array. If the
748 * functional request is a read, it may be satisfied by the memory
749 * value. If the functional request is a write, it may update the
750 * memory value.
751 */
752 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
753
754 /**
755 * Check a functional request against a memory value stored in
756 * another packet (i.e. an in-transit request or response).
757 */
758 bool
759 checkFunctional(PacketPtr other)
760 {
761 uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
762 return checkFunctional(other, other->getAddr(), other->getSize(),
763 data);
764 }
765
766 /**
767 * Push label for PrintReq (safe to call unconditionally).
768 */
769 void
770 pushLabel(const std::string &lbl)
771 {
772 if (isPrint())
773 safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
774 }
775
776 /**
777 * Pop label for PrintReq (safe to call unconditionally).
778 */
779 void
780 popLabel()
781 {
782 if (isPrint())
783 safe_cast<PrintReqState*>(senderState)->popLabel();
784 }
785
786 void print(std::ostream &o, int verbosity = 0,
787 const std::string &prefix = "") const;
788};
789
790#endif //__MEM_PACKET_HH