mem: Fix guest corruption when caches handle uncacheable accesses
[gem5.git] / src / mem / comm_monitor.cc
index 8469a246996e36d1860e483505f242d36159abce..88e549cbb7ad773f5fce8b429bb84a7709fc35ae 100644 (file)
  *          Andreas Hansson
  */
 
+#include "base/callback.hh"
+#include "base/output.hh"
+#include "base/trace.hh"
 #include "debug/CommMonitor.hh"
 #include "mem/comm_monitor.hh"
+#include "proto/packet.pb.h"
 #include "sim/stats.hh"
 
 CommMonitor::CommMonitor(Params* params)
@@ -50,8 +54,31 @@ CommMonitor::CommMonitor(Params* params)
       samplePeriodTicks(params->sample_period),
       readAddrMask(params->read_addr_mask),
       writeAddrMask(params->write_addr_mask),
-      stats(params)
+      stats(params),
+      traceStream(NULL)
 {
+    // If we are using a trace file, then open the file,
+    if (params->trace_file != "") {
+        // If the trace file is not specified as an absolute path,
+        // append the current simulation output directory
+        std::string filename = simout.resolve(params->trace_file);
+        traceStream = new ProtoOutputStream(filename);
+
+        // Create a protobuf message for the header and write it to
+        // the stream
+        Message::PacketHeader header_msg;
+        header_msg.set_obj_id(name());
+        header_msg.set_tick_freq(SimClock::Frequency);
+        traceStream->write(header_msg);
+
+        // Register a callback to compensate for the destructor not
+        // being called. The callback forces the stream to flush and
+        // closes the output file.
+        Callback* cb = new MakeCallback<CommMonitor,
+            &CommMonitor::closeStreams>(this);
+        registerExitCallback(cb);
+    }
+
     // keep track of the sample period both in ticks and absolute time
     samplePeriod.setTick(params->sample_period);
 
@@ -60,6 +87,13 @@ CommMonitor::CommMonitor(Params* params)
             name(), samplePeriodTicks, samplePeriod);
 }
 
+void
+CommMonitor::closeStreams()
+{
+    if (traceStream != NULL)
+        delete traceStream;
+}
+
 CommMonitor*
 CommMonitorParams::create()
 {
@@ -74,8 +108,8 @@ CommMonitor::init()
         fatal("Communication monitor is not connected on both sides.\n");
 }
 
-MasterPort&
-CommMonitor::getMasterPort(const std::string& if_name, int idx)
+BaseMasterPort&
+CommMonitor::getMasterPort(const std::string& if_name, PortID idx)
 {
     if (if_name == "master") {
         return masterPort;
@@ -84,8 +118,8 @@ CommMonitor::getMasterPort(const std::string& if_name, int idx)
     }
 }
 
-SlavePort&
-CommMonitor::getSlavePort(const std::string& if_name, int idx)
+BaseSlavePort&
+CommMonitor::getSlavePort(const std::string& if_name, PortID idx)
 {
     if (if_name == "slave") {
         return slavePort;
@@ -153,6 +187,19 @@ CommMonitor::recvTimingReq(PacketPtr pkt)
         pkt->senderState = senderState;
     }
 
+    if (successful && traceStream != NULL) {
+        // Create a protobuf message representing the
+        // packet. Currently we do not preserve the flags in the
+        // trace.
+        Message::Packet pkt_msg;
+        pkt_msg.set_tick(curTick());
+        pkt_msg.set_cmd(pkt->cmdToIndex());
+        pkt_msg.set_addr(pkt->getAddr());
+        pkt_msg.set_size(pkt->getSize());
+
+        traceStream->write(pkt_msg);
+    }
+
     if (successful && isRead) {
         DPRINTF(CommMonitor, "Forwarded read request\n");
 
@@ -345,7 +392,7 @@ CommMonitor::deviceBlockSizeSlave()
 }
 
 AddrRangeList
-CommMonitor::getAddrRanges()
+CommMonitor::getAddrRanges() const
 {
     // get the address ranges of the connected slave port
     return masterPort.getAddrRanges();