mem: Fix guest corruption when caches handle uncacheable accesses
[gem5.git] / src / mem / port.cc
index 36ca6304a851bb2d7f68b961fa5baffbe0c12906..45045f40eaadbe9af022e10ece24c2ddcad90804 100644 (file)
@@ -59,42 +59,100 @@ Port::~Port()
 {
 }
 
-/**
- * Master port
- */
-MasterPort::MasterPort(const std::string& name, MemObject* owner, PortID _id)
-    : Port(name, *owner, _id), _slavePort(NULL)
+BaseMasterPort::BaseMasterPort(const std::string& name, MemObject* owner,
+                               PortID _id)
+    : Port(name, *owner, _id), _baseSlavePort(NULL)
 {
 }
 
-MasterPort::~MasterPort()
+BaseMasterPort::~BaseMasterPort()
 {
 }
 
-SlavePort&
-MasterPort::getSlavePort() const
+BaseSlavePort&
+BaseMasterPort::getSlavePort() const
 {
-    if(_slavePort == NULL)
+    if(_baseSlavePort == NULL)
         panic("Cannot getSlavePort on master port %s that is not connected\n",
               name());
 
-    return *_slavePort;
+    return *_baseSlavePort;
 }
 
-void
-MasterPort::bind(SlavePort& slave_port)
+bool
+BaseMasterPort::isConnected() const
+{
+    return _baseSlavePort != NULL;
+}
+
+BaseSlavePort::BaseSlavePort(const std::string& name, MemObject* owner,
+                             PortID _id)
+    : Port(name, *owner, _id), _baseMasterPort(NULL)
+{
+}
+
+BaseSlavePort::~BaseSlavePort()
 {
-    // master port keeps track of the slave port
-    _slavePort = &slave_port;
+}
 
-    // slave port also keeps track of master port
-    _slavePort->bind(*this);
+BaseMasterPort&
+BaseSlavePort::getMasterPort() const
+{
+    if(_baseMasterPort == NULL)
+        panic("Cannot getMasterPort on slave port %s that is not connected\n",
+              name());
+
+    return *_baseMasterPort;
 }
 
 bool
-MasterPort::isConnected() const
+BaseSlavePort::isConnected() const
+{
+    return _baseMasterPort != NULL;
+}
+
+/**
+ * Master port
+ */
+MasterPort::MasterPort(const std::string& name, MemObject* owner, PortID _id)
+    : BaseMasterPort(name, owner, _id), _slavePort(NULL)
+{
+}
+
+MasterPort::~MasterPort()
+{
+}
+
+void
+MasterPort::bind(BaseSlavePort& slave_port)
+{
+    // bind on the level of the base ports
+    _baseSlavePort = &slave_port;
+
+    // also attempt to base the slave to the appropriate type
+    SlavePort* cast_slave_port = dynamic_cast<SlavePort*>(&slave_port);
+
+    // if this port is compatible, then proceed with the binding
+    if (cast_slave_port != NULL) {
+        // master port keeps track of the slave port
+        _slavePort = cast_slave_port;
+        // slave port also keeps track of master port
+        _slavePort->bind(*this);
+    } else {
+        fatal("Master port %s cannot bind to %s\n", name(),
+              slave_port.name());
+    }
+}
+
+void
+MasterPort::unbind()
 {
-    return _slavePort != NULL;
+    if (_slavePort == NULL)
+        panic("Attempting to unbind master port %s that is not connected\n",
+              name());
+    _slavePort->unbind();
+    _slavePort = NULL;
+    _baseSlavePort = NULL;
 }
 
 unsigned
@@ -158,7 +216,7 @@ MasterPort::printAddr(Addr a)
  * Slave port
  */
 SlavePort::SlavePort(const std::string& name, MemObject* owner, PortID id)
-    : Port(name, *owner, id), _masterPort(NULL)
+    : BaseSlavePort(name, owner, id), _masterPort(NULL)
 {
 }
 
@@ -167,19 +225,17 @@ SlavePort::~SlavePort()
 }
 
 void
-SlavePort::bind(MasterPort& master_port)
+SlavePort::unbind()
 {
-    _masterPort = &master_port;
+    _baseMasterPort = NULL;
+    _masterPort = NULL;
 }
 
-MasterPort&
-SlavePort::getMasterPort() const
+void
+SlavePort::bind(MasterPort& master_port)
 {
-    if (_masterPort == NULL)
-        panic("Cannot getMasterPort on slave port %s that is not connected\n",
-              name());
-
-    return *_masterPort;
+    _baseMasterPort = &master_port;
+    _masterPort = &master_port;
 }
 
 unsigned
@@ -188,12 +244,6 @@ SlavePort::peerBlockSize() const
     return _masterPort->deviceBlockSize();
 }
 
-bool
-SlavePort::isConnected() const
-{
-    return _masterPort != NULL;
-}
-
 Tick
 SlavePort::sendAtomicSnoop(PacketPtr pkt)
 {