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