mem: Fix guest corruption when caches handle uncacheable accesses
[gem5.git] / src / mem / bus.cc
index 75ece9bc85df132a8564c277abf8ee37a652e090..a880eca8f2318d46aea270b91d47b2b1b7240c29 100644 (file)
 BaseBus::BaseBus(const BaseBusParams *p)
     : MemObject(p),
       headerCycles(p->header_cycles), width(p->width),
-      defaultPortID(InvalidPortID),
+      gotAddrRanges(p->port_default_connection_count +
+                          p->port_master_connection_count, false),
+      gotAllAddrRanges(false), defaultPortID(InvalidPortID),
       useDefaultRange(p->use_default_range),
-      defaultBlockSize(p->block_size),
-      cachedBlockSize(0), cachedBlockSizeValid(false)
-{
-    //width, clock period, and header cycles must be positive
-    if (width <= 0)
-        fatal("Bus width must be positive\n");
-    if (clock <= 0)
-        fatal("Bus clock period must be positive\n");
-    if (headerCycles <= 0)
-        fatal("Number of header cycles must be positive\n");
-}
+      blockSize(p->block_size)
+{}
 
 BaseBus::~BaseBus()
 {
@@ -84,8 +77,36 @@ BaseBus::~BaseBus()
     }
 }
 
-MasterPort &
-BaseBus::getMasterPort(const std::string &if_name, int idx)
+void
+BaseBus::init()
+{
+    // determine the maximum peer block size, look at both the
+    // connected master and slave modules
+    uint32_t peer_block_size = 0;
+
+    for (MasterPortConstIter m = masterPorts.begin(); m != masterPorts.end();
+         ++m) {
+        peer_block_size = std::max((*m)->peerBlockSize(), peer_block_size);
+    }
+
+    for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
+         ++s) {
+        peer_block_size = std::max((*s)->peerBlockSize(), peer_block_size);
+    }
+
+    // if the peers do not have a block size, use the default value
+    // set through the bus parameters
+    if (peer_block_size != 0)
+        blockSize = peer_block_size;
+
+    // check if the block size is a value known to work
+    if (!(blockSize == 16 || blockSize == 32 || blockSize == 64 ||
+          blockSize == 128))
+        warn_once("Block size is neither 16, 32, 64 or 128 bytes.\n");
+}
+
+BaseMasterPort &
+BaseBus::getMasterPort(const std::string &if_name, PortID idx)
 {
     if (if_name == "master" && idx < masterPorts.size()) {
         // the master port index translates directly to the vector position
@@ -97,8 +118,8 @@ BaseBus::getMasterPort(const std::string &if_name, int idx)
     }
 }
 
-SlavePort &
-BaseBus::getSlavePort(const std::string &if_name, int idx)
+BaseSlavePort &
+BaseBus::getSlavePort(const std::string &if_name, PortID idx)
 {
     if (if_name == "slave" && idx < slavePorts.size()) {
         // the slave port index translates directly to the vector position
@@ -140,7 +161,8 @@ BaseBus::calcPacketTiming(PacketPtr pkt)
 template <typename PortClass>
 BaseBus::Layer<PortClass>::Layer(BaseBus& _bus, const std::string& _name,
                                  Tick _clock) :
-    bus(_bus), _name(_name), state(IDLE), clock(_clock), drainEvent(NULL),
+    Drainable(),
+    bus(_bus), _name(_name), state(IDLE), clock(_clock), drainManager(NULL),
     releaseEvent(this)
 {
 }
@@ -245,12 +267,12 @@ BaseBus::Layer<PortClass>::releaseLayer()
         // busy, and in the latter case the bus may be released before
         // we see a retry from the destination
         retryWaiting();
-    } else if (drainEvent) {
-        DPRINTF(Drain, "Bus done draining, processing drain event\n");
+    } else if (drainManager) {
+        DPRINTF(Drain, "Bus done draining, signaling drain manager\n");
         //If we weren't able to drain before, do it now.
-        drainEvent->process();
+        drainManager->signalDrainDone();
         // Clear the drain event once we're done with it.
-        drainEvent = NULL;
+        drainManager = NULL;
     }
 }
 
@@ -316,28 +338,29 @@ BaseBus::Layer<PortClass>::recvRetry()
 PortID
 BaseBus::findPort(Addr addr)
 {
-    /* An interval tree would be a better way to do this. --ali. */
+    // we should never see any address lookups before we've got the
+    // ranges of all connected slave modules
+    assert(gotAllAddrRanges);
+
+    // Check the cache
     PortID dest_id = checkPortCache(addr);
     if (dest_id != InvalidPortID)
         return dest_id;
 
-    // Check normal port ranges
-    PortMapConstIter i = portMap.find(RangeSize(addr,1));
+    // Check the address map interval tree
+    PortMapConstIter i = portMap.find(addr);
     if (i != portMap.end()) {
         dest_id = i->second;
-        updatePortCache(dest_id, i->first.start, i->first.end);
+        updatePortCache(dest_id, i->first);
         return dest_id;
     }
 
     // Check if this matches the default range
     if (useDefaultRange) {
-        AddrRangeConstIter a_end = defaultRange.end();
-        for (AddrRangeConstIter i = defaultRange.begin(); i != a_end; i++) {
-            if (*i == addr) {
-                DPRINTF(BusAddrRanges, "  found addr %#llx on default\n",
-                        addr);
-                return defaultPortID;
-            }
+        if (defaultRange.contains(addr)) {
+            DPRINTF(BusAddrRanges, "  found addr %#llx on default\n",
+                    addr);
+            return defaultPortID;
         }
     } else if (defaultPortID != InvalidPortID) {
         DPRINTF(BusAddrRanges, "Unable to find destination for addr %#llx, "
@@ -355,51 +378,64 @@ BaseBus::findPort(Addr addr)
 void
 BaseBus::recvRangeChange(PortID master_port_id)
 {
-    AddrRangeIter iter;
-
-    if (inRecvRangeChange.count(master_port_id))
-        return;
-    inRecvRangeChange.insert(master_port_id);
-
-    DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n",
-            master_port_id);
+    DPRINTF(BusAddrRanges, "Received range change from slave port %s\n",
+            masterPorts[master_port_id]->getSlavePort().name());
+
+    // remember that we got a range from this master port and thus the
+    // connected slave module
+    gotAddrRanges[master_port_id] = true;
+
+    // update the global flag
+    if (!gotAllAddrRanges) {
+        // take a logical AND of all the ports and see if we got
+        // ranges from everyone
+        gotAllAddrRanges = true;
+        std::vector<bool>::const_iterator r = gotAddrRanges.begin();
+        while (gotAllAddrRanges &&  r != gotAddrRanges.end()) {
+            gotAllAddrRanges &= *r++;
+        }
+        if (gotAllAddrRanges)
+            DPRINTF(BusAddrRanges, "Got address ranges from all slaves\n");
+    }
 
-    clearPortCache();
+    // note that we could get the range from the default port at any
+    // point in time, and we cannot assume that the default range is
+    // set before the other ones are, so we do additional checks once
+    // all ranges are provided
     if (master_port_id == defaultPortID) {
-        defaultRange.clear();
-        // Only try to update these ranges if the user set a default responder.
+        // only update if we are indeed checking ranges for the
+        // default port since the port might not have a valid range
+        // otherwise
         if (useDefaultRange) {
-            // get the address ranges of the connected slave port
-            AddrRangeList ranges =
-                masterPorts[master_port_id]->getAddrRanges();
-            for(iter = ranges.begin(); iter != ranges.end(); iter++) {
-                defaultRange.push_back(*iter);
-                DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
-                        iter->start, iter->end);
-            }
-        }
-    } else {
+            AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
 
-        assert(master_port_id < masterPorts.size() && master_port_id >= 0);
-        MasterPort *port = masterPorts[master_port_id];
+            if (ranges.size() != 1)
+                fatal("Bus %s may only have a single default range",
+                      name());
 
-        // Clean out any previously existent ids
-        for (PortMapIter portIter = portMap.begin();
-             portIter != portMap.end(); ) {
-            if (portIter->second == master_port_id)
-                portMap.erase(portIter++);
-            else
-                portIter++;
+            defaultRange = ranges.front();
+        }
+    } else {
+        // the ports are allowed to update their address ranges
+        // dynamically, so remove any existing entries
+        if (gotAddrRanges[master_port_id]) {
+            for (PortMapIter p = portMap.begin(); p != portMap.end(); ) {
+                if (p->second == master_port_id)
+                    // erasing invalidates the iterator, so advance it
+                    // before the deletion takes place
+                    portMap.erase(p++);
+                else
+                    p++;
+            }
         }
 
-        // get the address ranges of the connected slave port
-        AddrRangeList ranges = port->getAddrRanges();
+        AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
 
-        for (iter = ranges.begin(); iter != ranges.end(); iter++) {
-            DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
-                    iter->start, iter->end, master_port_id);
-            if (portMap.insert(*iter, master_port_id) == portMap.end()) {
-                PortID conflict_id = portMap.find(*iter)->second;
+        for (AddrRangeConstIter r = ranges.begin(); r != ranges.end(); ++r) {
+            DPRINTF(BusAddrRanges, "Adding range %s for id %d\n",
+                    r->to_string(), master_port_id);
+            if (portMap.insert(*r, master_port_id) == portMap.end()) {
+                PortID conflict_id = portMap.find(*r)->second;
                 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
                       name(),
                       masterPorts[master_port_id]->getSlavePort().name(),
@@ -407,52 +443,79 @@ BaseBus::recvRangeChange(PortID master_port_id)
             }
         }
     }
-    DPRINTF(BusAddrRanges, "port list has %d entries\n", portMap.size());
 
-    // tell all our neighbouring master ports that our address range
-    // has changed
-    for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
-         ++p)
-        (*p)->sendRangeChange();
+    // if we have received ranges from all our neighbouring slave
+    // modules, go ahead and tell our connected master modules in
+    // turn, this effectively assumes a tree structure of the system
+    if (gotAllAddrRanges) {
+        // also check that no range partially overlaps with the
+        // default range, this has to be done after all ranges are set
+        // as there are no guarantees for when the default range is
+        // update with respect to the other ones
+        if (useDefaultRange) {
+            for (PortID port_id = 0; port_id < masterPorts.size(); ++port_id) {
+                if (port_id == defaultPortID) {
+                    if (!gotAddrRanges[port_id])
+                        fatal("Bus %s uses default range, but none provided",
+                              name());
+                } else {
+                    AddrRangeList ranges =
+                        masterPorts[port_id]->getAddrRanges();
+
+                    for (AddrRangeConstIter r = ranges.begin();
+                         r != ranges.end(); ++r) {
+                        // see if the new range is partially
+                        // overlapping the default range
+                        if (r->intersects(defaultRange) &&
+                            !r->isSubset(defaultRange))
+                            fatal("Range %s intersects the " \
+                                  "default range of %s but is not a " \
+                                  "subset\n", r->to_string(), name());
+                    }
+                }
+            }
+        }
 
-    inRecvRangeChange.erase(master_port_id);
+        // tell all our neighbouring master ports that our address
+        // ranges have changed
+        for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
+             ++s)
+            (*s)->sendRangeChange();
+    }
+
+    clearPortCache();
 }
 
 AddrRangeList
 BaseBus::getAddrRanges() const
 {
-    AddrRangeList ranges;
+    // we should never be asked without first having sent a range
+    // change, and the latter is only done once we have all the ranges
+    // of the connected devices
+    assert(gotAllAddrRanges);
+
+    // at the moment, this never happens, as there are no cycles in
+    // the range queries and no devices on the master side of a bus
+    // (CPU, cache, bridge etc) actually care about the ranges of the
+    // ports they are connected to
 
-    DPRINTF(BusAddrRanges, "received address range request, returning:\n");
+    DPRINTF(BusAddrRanges, "Received address range request, returning:\n");
 
-    for (AddrRangeConstIter dflt_iter = defaultRange.begin();
-         dflt_iter != defaultRange.end(); dflt_iter++) {
-        ranges.push_back(*dflt_iter);
-        DPRINTF(BusAddrRanges, "  -- Dflt: %#llx : %#llx\n",dflt_iter->start,
-                dflt_iter->end);
+    // start out with the default range
+    AddrRangeList ranges;
+    if (useDefaultRange) {
+        ranges.push_back(defaultRange);
+        DPRINTF(BusAddrRanges, "  -- Default %s\n", defaultRange.to_string());
     }
-    for (PortMapConstIter portIter = portMap.begin();
-         portIter != portMap.end(); portIter++) {
-        bool subset = false;
-        for (AddrRangeConstIter dflt_iter = defaultRange.begin();
-             dflt_iter != defaultRange.end(); dflt_iter++) {
-            if ((portIter->first.start < dflt_iter->start &&
-                portIter->first.end >= dflt_iter->start) ||
-               (portIter->first.start < dflt_iter->end &&
-                portIter->first.end >= dflt_iter->end))
-                fatal("Devices can not set ranges that itersect the default set\
-                        but are not a subset of the default set.\n");
-            if (portIter->first.start >= dflt_iter->start &&
-                portIter->first.end <= dflt_iter->end) {
-                subset = true;
-                DPRINTF(BusAddrRanges, "  -- %#llx : %#llx is a SUBSET\n",
-                    portIter->first.start, portIter->first.end);
-            }
-        }
-        if (!subset) {
-            ranges.push_back(portIter->first);
-            DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",
-                    portIter->first.start, portIter->first.end);
+
+    // add any range that is not a subset of the default range
+    for (PortMapConstIter p = portMap.begin(); p != portMap.end(); ++p) {
+        if (useDefaultRange && p->first.isSubset(defaultRange)) {
+            DPRINTF(BusAddrRanges, "  -- %s is a subset of default\n",
+                    p->first.to_string());
+        } else {
+            ranges.push_back(p->first);
+            DPRINTF(BusAddrRanges, "  -- %s\n", p->first.to_string());
         }
     }
 
@@ -460,46 +523,21 @@ BaseBus::getAddrRanges() const
 }
 
 unsigned
-BaseBus::findBlockSize()
+BaseBus::deviceBlockSize() const
 {
-    if (cachedBlockSizeValid)
-        return cachedBlockSize;
-
-    unsigned max_bs = 0;
-
-    for (MasterPortConstIter m = masterPorts.begin(); m != masterPorts.end();
-         ++m) {
-        unsigned tmp_bs = (*m)->peerBlockSize();
-        if (tmp_bs > max_bs)
-            max_bs = tmp_bs;
-    }
-
-    for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
-         ++s) {
-        unsigned tmp_bs = (*s)->peerBlockSize();
-        if (tmp_bs > max_bs)
-            max_bs = tmp_bs;
-    }
-    if (max_bs == 0)
-        max_bs = defaultBlockSize;
-
-    if (max_bs != 64)
-        warn_once("Blocksize found to not be 64... hmm... probably not.\n");
-    cachedBlockSize = max_bs;
-    cachedBlockSizeValid = true;
-    return max_bs;
+    return blockSize;
 }
 
 template <typename PortClass>
 unsigned int
-BaseBus::Layer<PortClass>::drain(Event * de)
+BaseBus::Layer<PortClass>::drain(DrainManager *dm)
 {
     //We should check that we're not "doing" anything, and that noone is
     //waiting. We might be idle but have someone waiting if the device we
     //contacted for a retry didn't actually retry.
     if (!retryList.empty() || state != IDLE) {
         DPRINTF(Drain, "Bus not drained\n");
-        drainEvent = de;
+        drainManager = dm;
         return 1;
     }
     return 0;