mem: Fix guest corruption when caches handle uncacheable accesses
[gem5.git] / src / mem / packet.hh
index 38eb2e92bc0db8fcba60ea624cc66d13e389bcc7..fbcf185ccb7e35230fac333b155ae58e72151047 100644 (file)
@@ -1,5 +1,18 @@
 /*
+ * Copyright (c) 2012 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
  * Copyright (c) 2006 The Regents of The University of Michigan
+ * Copyright (c) 2010 Advanced Micro Devices, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,6 +41,7 @@
  * Authors: Ron Dreslinski
  *          Steve Reinhardt
  *          Ali Saidi
+ *          Andreas Hansson
  */
 
 /**
 #ifndef __MEM_PACKET_HH__
 #define __MEM_PACKET_HH__
 
+#include <bitset>
 #include <cassert>
 #include <list>
-#include <bitset>
 
 #include "base/cast.hh"
 #include "base/compiler.hh"
-#include "base/fast_alloc.hh"
 #include "base/flags.hh"
 #include "base/misc.hh"
 #include "base/printable.hh"
+#include "base/types.hh"
 #include "mem/request.hh"
-#include "sim/host.hh"
 #include "sim/core.hh"
 
-
-struct Packet;
+class Packet;
 typedef Packet *PacketPtr;
 typedef uint8_t* PacketDataPtr;
 typedef std::list<PacketPtr> PacketList;
@@ -79,14 +91,26 @@ class MemCmd
         HardPFReq,
         SoftPFResp,
         HardPFResp,
+        // WriteInvalidateReq transactions used to be generated by the
+        // DMA ports when writing full blocks to memory, however, it
+        // is not used anymore since we put the I/O cache in place to
+        // deal with partial block writes. Hence, WriteInvalidateReq
+        // and WriteInvalidateResp are currently unused. The
+        // implication is that the I/O cache does read-exclusive
+        // operations on every full-cache-block DMA, and ultimately
+        // this needs to be fixed.
         WriteInvalidateReq,
         WriteInvalidateResp,
         UpgradeReq,
+        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
         UpgradeResp,
+        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
+        UpgradeFailResp,        // Valid for SCUpgradeReq only
         ReadExReq,
         ReadExResp,
         LoadLockedReq,
         StoreCondReq,
+        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
         StoreCondResp,
         SwapReq,
         SwapResp,
@@ -96,11 +120,14 @@ class MemCmd
         // @TODO these should be classified as responses rather than
         // requests; coding them as requests initially for backwards
         // compatibility
-        NetworkNackError,  // nacked at network layer (not by protocol)
         InvalidDestError,  // packet dest field invalid
         BadAddressError,   // memory address invalid
+        FunctionalReadError, // unable to fulfill functional read
+        FunctionalWriteError, // unable to fulfill functional write
         // Fake simulator-only commands
         PrintReq,       // Print state matching address
+        FlushReq,      //request for a cache flush
+        InvalidationReq,   // request for address to be invalidated from lsq
         NUM_MEM_CMDS
     };
 
@@ -112,7 +139,7 @@ class MemCmd
     {
         IsRead,         //!< Data flows from responder to requester
         IsWrite,        //!< Data flows from requester to responder
-        IsPrefetch,     //!< Not a demand access
+        IsUpgrade,
         IsInvalidate,
         NeedsExclusive, //!< Requires exclusive copy to complete in-cache
         IsRequest,      //!< Issued by requester
@@ -120,10 +147,11 @@ class MemCmd
         NeedsResponse,  //!< Requester needs response from target
         IsSWPrefetch,
         IsHWPrefetch,
-        IsLocked,       //!< Alpha/MIPS LL or SC access
+        IsLlsc,         //!< Alpha/MIPS LL or SC access
         HasData,        //!< There is an associated payload
         IsError,        //!< Error response
         IsPrint,        //!< Print state matching address (for debugging)
+        IsFlush,        //!< Flush the address from caches
         NUM_COMMAND_ATTRIBUTES
     };
 
@@ -158,7 +186,8 @@ class MemCmd
   public:
 
     bool isRead() const         { return testCmdAttrib(IsRead); }
-    bool isWrite()  const       { return testCmdAttrib(IsWrite); }
+    bool isWrite() const        { return testCmdAttrib(IsWrite); }
+    bool isUpgrade() const      { return testCmdAttrib(IsUpgrade); }
     bool isRequest() const      { return testCmdAttrib(IsRequest); }
     bool isResponse() const     { return testCmdAttrib(IsResponse); }
     bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
@@ -166,9 +195,10 @@ class MemCmd
     bool isInvalidate() const   { return testCmdAttrib(IsInvalidate); }
     bool hasData() const        { return testCmdAttrib(HasData); }
     bool isReadWrite() const    { return isRead() && isWrite(); }
-    bool isLocked() const       { return testCmdAttrib(IsLocked); }
+    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
     bool isError() const        { return testCmdAttrib(IsError); }
     bool isPrint() const        { return testCmdAttrib(IsPrint); }
+    bool isFlush() const        { return testCmdAttrib(IsFlush); }
 
     const Command
     responseCommand() const
@@ -195,12 +225,11 @@ class MemCmd
  * ultimate destination and back, possibly being conveyed by several
  * different Packets along the way.)
  */
-class Packet : public FastAlloc, public Printable
+class Packet : public Printable
 {
   public:
     typedef uint32_t FlagsType;
     typedef ::Flags<FlagsType> Flags;
-    typedef short NodeID;
 
   private:
     static const FlagsType PUBLIC_FLAGS           = 0x00000000;
@@ -219,9 +248,6 @@ class Packet : public FastAlloc, public Printable
     /// Are the 'addr' and 'size' fields valid?
     static const FlagsType VALID_ADDR             = 0x00000100;
     static const FlagsType VALID_SIZE             = 0x00000200;
-    /// Is the 'src' field valid? 
-    static const FlagsType VALID_SRC              = 0x00000400;
-    static const FlagsType VALID_DST              = 0x00000800;
     /// Is the data pointer set to a value that shouldn't be freed
     /// when the packet is destroyed?
     static const FlagsType STATIC_DATA            = 0x00001000;
@@ -231,6 +257,9 @@ class Packet : public FastAlloc, public Printable
     /// the data pointer points to an array (thus delete []) needs to
     /// be called on it rather than simply delete.
     static const FlagsType ARRAY_DATA             = 0x00004000;
+    /// suppress the error if this packet encounters a functional
+    /// access failure.
+    static const FlagsType SUPPRESS_FUNC_ERROR    = 0x00008000;
 
     Flags flags;
 
@@ -258,24 +287,31 @@ class Packet : public FastAlloc, public Printable
     Addr addr;
 
     /// The size of the request or transfer.
-    int size;
+    unsigned size;
 
     /**
-     * Device address (e.g., bus ID) of the source of the
-     * transaction. The source is not responsible for setting this
-     * field; it is set implicitly by the interconnect when the packet
-     * is first sent.
+     * Source port identifier set on a request packet to enable
+     * appropriate routing of the responses. The source port
+     * identifier is set by any multiplexing component, e.g. a bus, as
+     * the timing responses need this information to be routed back to
+     * the appropriate port at a later point in time. The field can be
+     * updated (over-written) as the request packet passes through
+     * additional multiplexing components, and it is their
+     * responsibility to remember the original source port identifier,
+     * for example by using an appropriate sender state. The latter is
+     * done in the cache and bridge.
      */
-    NodeID src;
+    PortID src;
 
     /**
-     * Device address (e.g., bus ID) of the destination of the
-     * transaction. The special value Broadcast indicates that the
-     * packet should be routed based on its address. This field is
-     * initialized in the constructor and is thus always valid (unlike
-     * addr, size, and src).
+     * Destination port identifier that is present on all response
+     * packets that passed through a multiplexing component as a
+     * request packet. The source port identifier is turned into a
+     * destination port identifier when the packet is turned into a
+     * response, and the destination is used, e.g. by the bus, to
+     * select the appropriate path through the interconnect.
      */
-    NodeID dest;
+    PortID dest;
 
     /**
      * The original value of the command field.  Only valid when the
@@ -285,6 +321,13 @@ class Packet : public FastAlloc, public Printable
      */
     MemCmd origCmd;
 
+    /**
+     * These values specify the range of bytes found that satisfy a
+     * functional read.
+     */
+    uint16_t bytesValidStart;
+    uint16_t bytesValidEnd;
+
   public:
     /// Used to calculate latencies for each packet.
     Tick time;
@@ -295,10 +338,6 @@ class Packet : public FastAlloc, public Printable
     /// The time at which the first chunk of the packet will be transmitted
     Tick firstWordTime;
 
-    /// The special destination address indicating that the packet
-    /// should be routed based on its address.
-    static const NodeID Broadcast = -1;
-
     /**
      * A virtual base opaque structure used to hold state associated
      * with the packet but specific to the sending device (e.g., an
@@ -317,7 +356,7 @@ class Packet : public FastAlloc, public Printable
      * Object used to maintain state of a PrintReq.  The senderState
      * field of a PrintReq should always be of this type.
      */
-    class PrintReqState : public SenderState, public FastAlloc
+    class PrintReqState : public SenderState
     {
       private:
         /**
@@ -393,7 +432,8 @@ class Packet : public FastAlloc, public Printable
     inline int cmdToIndex() const { return cmd.toInt(); }
 
     bool isRead() const         { return cmd.isRead(); }
-    bool isWrite()  const       { return cmd.isWrite(); }
+    bool isWrite() const        { return cmd.isWrite(); }
+    bool isUpgrade()  const     { return cmd.isUpgrade(); }
     bool isRequest() const      { return cmd.isRequest(); }
     bool isResponse() const     { return cmd.isResponse(); }
     bool needsExclusive() const { return cmd.needsExclusive(); }
@@ -401,32 +441,29 @@ class Packet : public FastAlloc, public Printable
     bool isInvalidate() const   { return cmd.isInvalidate(); }
     bool hasData() const        { return cmd.hasData(); }
     bool isReadWrite() const    { return cmd.isReadWrite(); }
-    bool isLocked() const       { return cmd.isLocked(); }
+    bool isLLSC() const         { return cmd.isLLSC(); }
     bool isError() const        { return cmd.isError(); }
     bool isPrint() const        { return cmd.isPrint(); }
+    bool isFlush() const        { return cmd.isFlush(); }
 
     // Snoop flags
     void assertMemInhibit()     { flags.set(MEM_INHIBIT); }
-    bool memInhibitAsserted()   { return flags.any(MEM_INHIBIT); }
+    bool memInhibitAsserted()   { return flags.isSet(MEM_INHIBIT); }
     void assertShared()         { flags.set(SHARED); }
-    bool sharedAsserted()       { return flags.any(SHARED); }
+    bool sharedAsserted()       { return flags.isSet(SHARED); }
 
     // Special control flags
     void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
-    bool isExpressSnoop()       { return flags.any(EXPRESS_SNOOP); }
+    bool isExpressSnoop()       { return flags.isSet(EXPRESS_SNOOP); }
     void setSupplyExclusive()   { flags.set(SUPPLY_EXCLUSIVE); }
-    bool isSupplyExclusive()    { return flags.any(SUPPLY_EXCLUSIVE); }
+    void clearSupplyExclusive() { flags.clear(SUPPLY_EXCLUSIVE); }
+    bool isSupplyExclusive()    { return flags.isSet(SUPPLY_EXCLUSIVE); }
+    void setSuppressFuncError() { flags.set(SUPPRESS_FUNC_ERROR); }
+    bool suppressFuncError()    { return flags.isSet(SUPPRESS_FUNC_ERROR); }
 
     // Network error conditions... encapsulate them as methods since
     // their encoding keeps changing (from result field to command
     // field, etc.)
-    void
-    setNacked()
-    {
-        assert(isResponse());
-        cmd = MemCmd::NetworkNackError;
-    }
-
     void
     setBadAddress()
     {
@@ -434,36 +471,81 @@ class Packet : public FastAlloc, public Printable
         cmd = MemCmd::BadAddressError;
     }
 
-    bool wasNacked() const     { return cmd == MemCmd::NetworkNackError; }
     bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
     void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
 
+    bool isSrcValid() const { return src != InvalidPortID; }
     /// Accessor function to get the source index of the packet.
-    NodeID getSrc() const    { assert(flags.any(VALID_SRC)); return src; }
+    PortID getSrc() const { assert(isSrcValid()); return src; }
     /// Accessor function to set the source index of the packet.
-    void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
+    void setSrc(PortID _src) { src = _src; }
     /// Reset source field, e.g. to retransmit packet on different bus.
-    void clearSrc() { flags.clear(VALID_SRC); }
+    void clearSrc() { src = InvalidPortID; }
 
+    bool isDestValid() const { return dest != InvalidPortID; }
     /// Accessor function for the destination index of the packet.
-    NodeID getDest() const     { assert(flags.any(VALID_DST)); return dest; }
+    PortID getDest() const { assert(isDestValid()); return dest; }
     /// Accessor function to set the destination index of the packet.
-    void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
+    void setDest(PortID _dest) { dest = _dest; }
+    /// Reset destination field, e.g. to turn a response into a request again.
+    void clearDest() { dest = InvalidPortID; }
+
+    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
+    /**
+     * Update the address of this packet mid-transaction. This is used
+     * by the address mapper to change an already set address to a new
+     * one based on the system configuration. It is intended to remap
+     * an existing address, so it asserts that the current address is
+     * valid.
+     */
+    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
 
-    Addr getAddr() const { assert(flags.all(VALID_ADDR)); return addr; }
-    int getSize() const  { assert(flags.all(VALID_SIZE)); return size; }
+    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
     Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
 
+    /**
+     * It has been determined that the SC packet should successfully update
+     * memory.  Therefore, convert this SC packet to a normal write.
+     */
+    void
+    convertScToWrite()
+    {
+        assert(isLLSC());
+        assert(isWrite());
+        cmd = MemCmd::WriteReq;
+    }
+
+    /**
+     * When ruby is in use, Ruby will monitor the cache line and thus M5 
+     * phys memory should treat LL ops as normal reads. 
+     */
+    void
+    convertLlToRead()
+    {
+        assert(isLLSC());
+        assert(isRead());
+        cmd = MemCmd::ReadReq;
+    }
+
     /**
      * Constructor.  Note that a Request object must be constructed
      * first, but the Requests's physical address and size fields need
-     * not be valid. The command and destination addresses must be
-     * supplied.
+     * not be valid. The command must be supplied.
      */
-    Packet(Request *_req, MemCmd _cmd, NodeID _dest)
-        :  cmd(_cmd), req(_req), data(NULL), addr(_req->paddr),
-           size(_req->size), dest(_dest), time(curTick), senderState(NULL)
+    Packet(Request *_req, MemCmd _cmd)
+        :  cmd(_cmd), req(_req), data(NULL),
+           src(InvalidPortID), dest(InvalidPortID),
+           bytesValidStart(0), bytesValidEnd(0),
+           time(curTick()), senderState(NULL)
     {
+        if (req->hasPaddr()) {
+            addr = req->getPaddr();
+            flags.set(VALID_ADDR);
+        }
+        if (req->hasSize()) {
+            size = req->getSize();
+            flags.set(VALID_SIZE);
+        }
     }
 
     /**
@@ -471,11 +553,18 @@ class Packet : public FastAlloc, public Printable
      * a request that is for a whole block, not the address from the
      * req.  this allows for overriding the size/addr of the req.
      */
-    Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
+    Packet(Request *_req, MemCmd _cmd, int _blkSize)
         :  cmd(_cmd), req(_req), data(NULL),
-           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
-           time(curTick), senderState(NULL)
+           src(InvalidPortID), dest(InvalidPortID),
+           bytesValidStart(0), bytesValidEnd(0),
+           time(curTick()), senderState(NULL)
     {
+        if (req->hasPaddr()) {
+            addr = req->getPaddr() & ~(_blkSize - 1);
+            flags.set(VALID_ADDR);
+        }
+        size = _blkSize;
+        flags.set(VALID_SIZE);
     }
 
     /**
@@ -487,15 +576,17 @@ class Packet : public FastAlloc, public Printable
      */
     Packet(Packet *pkt, bool clearFlags = false)
         :  cmd(pkt->cmd), req(pkt->req),
-           data(pkt->flags.any(STATIC_DATA) ? pkt->data : NULL),
+           data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
            addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
-           time(curTick), senderState(pkt->senderState)
+           bytesValidStart(pkt->bytesValidStart), bytesValidEnd(pkt->bytesValidEnd),
+           time(curTick()), senderState(pkt->senderState)
     {
         if (!clearFlags)
             flags.set(pkt->flags & COPY_FLAGS);
 
-        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
+        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
         flags.set(pkt->flags & STATIC_DATA);
+
     }
 
     /**
@@ -521,11 +612,11 @@ class Packet : public FastAlloc, public Printable
     void
     reinitFromRequest()
     {
-        assert(req->flags.any(Request::VALID_PADDR));
+        assert(req->hasPaddr());
         flags = 0;
-        addr = req->paddr;
-        size = req->size;
-        time = req->time;
+        addr = req->getPaddr();
+        size = req->getSize();
+        time = req->time();
 
         flags.set(VALID_ADDR|VALID_SIZE);
         deleteData();
@@ -533,9 +624,10 @@ class Packet : public FastAlloc, public Printable
 
     /**
      * Take a request packet and modify it in place to be suitable for
-     * returning as a response to that request.  The source and
-     * destination fields are *not* modified, as is appropriate for
-     * atomic accesses.
+     * returning as a response to that request. The source field is
+     * turned into the destination, and subsequently cleared. Note
+     * that the latter is not necessary for atomic requests, but
+     * causes no harm as neither field is valid.
      */
     void
     makeResponse()
@@ -544,13 +636,13 @@ class Packet : public FastAlloc, public Printable
         assert(isRequest());
         origCmd = cmd;
         cmd = cmd.responseCommand();
-        if (flags.any(VALID_SRC)) {
-            dest = src;
-            flags.set(VALID_DST);
-            flags.clear(VALID_SRC);
-        } else {
-            flags.clear(VALID_DST);
-        }
+
+        // responses are never express, even if the snoop that
+        // triggered them was
+        flags.clear(EXPRESS_SNOOP);
+
+        dest = src;
+        clearSrc();
     }
 
     void
@@ -565,18 +657,25 @@ class Packet : public FastAlloc, public Printable
         makeResponse();
     }
 
-    /**
-     * Take a request packet that has been returned as NACKED and
-     * modify it so that it can be sent out again. Only packets that
-     * need a response can be NACKED, so verify that that is true.
-     */
     void
-    reinitNacked()
+    setFunctionalResponseStatus(bool success)
     {
-        assert(wasNacked());
-        cmd = origCmd;
-        assert(needsResponse());
-        setDest(Broadcast);
+        if (!success) {
+            if (isWrite()) {
+                cmd = MemCmd::FunctionalWriteError;
+            } else {
+                cmd = MemCmd::FunctionalReadError;
+            }
+        }
+    }
+
+    void
+    setSize(unsigned size)
+    {
+        assert(!flags.isSet(VALID_SIZE));
+
+        this->size = size;
+        flags.set(VALID_SIZE);
     }
 
 
@@ -588,7 +687,7 @@ class Packet : public FastAlloc, public Printable
     void
     dataStatic(T *p)
     {
-        assert(flags.none(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
+        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
         data = (PacketDataPtr)p;
         flags.set(STATIC_DATA);
     }
@@ -601,7 +700,7 @@ class Packet : public FastAlloc, public Printable
     void
     dataDynamicArray(T *p)
     {
-        assert(flags.none(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
+        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
         data = (PacketDataPtr)p;
         flags.set(DYNAMIC_DATA|ARRAY_DATA);
     }
@@ -614,7 +713,7 @@ class Packet : public FastAlloc, public Printable
     void
     dataDynamic(T *p)
     {
-        assert(flags.none(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
+        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
         data = (PacketDataPtr)p;
         flags.set(DYNAMIC_DATA);
     }
@@ -624,9 +723,9 @@ class Packet : public FastAlloc, public Printable
      */
     template <typename T>
     T*
-    getPtr()
+    getPtr(bool null_ok = false)
     {
-        assert(flags.any(STATIC_DATA|DYNAMIC_DATA));
+        assert(null_ok || flags.isSet(STATIC_DATA|DYNAMIC_DATA));
         return (T*)data;
     }
 
@@ -648,7 +747,8 @@ class Packet : public FastAlloc, public Printable
     void
     setData(uint8_t *p)
     {
-        std::memcpy(getPtr<uint8_t>(), p, getSize());
+        if (p != getPtr<uint8_t>())
+            std::memcpy(getPtr<uint8_t>(), p, getSize());
     }
 
     /**
@@ -687,10 +787,9 @@ class Packet : public FastAlloc, public Printable
     void
     deleteData()
     {
-        if (flags.any(ARRAY_DATA))
+        if (flags.isSet(ARRAY_DATA))
             delete [] data;
-        else if (flags.any(DYNAMIC_DATA))
-
+        else if (flags.isSet(DYNAMIC_DATA))
             delete data;
 
         flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
@@ -702,13 +801,14 @@ class Packet : public FastAlloc, public Printable
     allocate()
     {
         if (data) {
-            assert(flags.none(STATIC_DATA|DYNAMIC_DATA));
-        } else {
-            flags.set(DYNAMIC_DATA|ARRAY_DATA);
-            data = new uint8_t[getSize()];
+            assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
+            return;
         }
-    }
 
+        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
+        flags.set(DYNAMIC_DATA|ARRAY_DATA);
+        data = new uint8_t[getSize()];
+    }
 
     /**
      * Check a functional request against a memory value represented