mem: Encapsulate mapping gem5 to host address space
authorDaniel R. Carvalho <odanrc@yahoo.com.br>
Wed, 16 Oct 2019 12:38:04 +0000 (14:38 +0200)
committerDaniel Carvalho <odanrc@yahoo.com.br>
Thu, 12 Dec 2019 14:43:09 +0000 (14:43 +0000)
Create a function to encapsulate mapping an address in gem5's
address space to the host's address space. The returned value can
be used to access the contents of the given address.

As a side effect, make the local variable hostAddr use snake_case
to comply with gem5's coding style.

Change-Id: I2445d3ab4c7ce5746182b307c26cbafc68aa139c
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/22610
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Maintainer: Nikos Nikoleris <nikos.nikoleris@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
src/mem/abstract_mem.cc
src/mem/abstract_mem.hh

index d913f05d3447a37636ced66e1c77a5fda8b634dc..41b621b372f20463c81eac4de2ae0cdbe47f22c4 100644 (file)
@@ -361,13 +361,13 @@ AbstractMemory::access(PacketPtr pkt)
 
     assert(pkt->getAddrRange().isSubset(range));
 
-    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start();
+    uint8_t *host_addr = toHostAddr(pkt->getAddr());
 
     if (pkt->cmd == MemCmd::SwapReq) {
         if (pkt->isAtomicOp()) {
             if (pmemAddr) {
-                pkt->setData(hostAddr);
-                (*(pkt->getAtomicOp()))(hostAddr);
+                pkt->setData(host_addr);
+                (*(pkt->getAtomicOp()))(host_addr);
             }
         } else {
             std::vector<uint8_t> overwrite_val(pkt->getSize());
@@ -381,23 +381,23 @@ AbstractMemory::access(PacketPtr pkt)
             // keep a copy of our possible write value, and copy what is at the
             // memory address into the packet
             pkt->writeData(&overwrite_val[0]);
-            pkt->setData(hostAddr);
+            pkt->setData(host_addr);
 
             if (pkt->req->isCondSwap()) {
                 if (pkt->getSize() == sizeof(uint64_t)) {
                     condition_val64 = pkt->req->getExtraData();
-                    overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
+                    overwrite_mem = !std::memcmp(&condition_val64, host_addr,
                                                  sizeof(uint64_t));
                 } else if (pkt->getSize() == sizeof(uint32_t)) {
                     condition_val32 = (uint32_t)pkt->req->getExtraData();
-                    overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
+                    overwrite_mem = !std::memcmp(&condition_val32, host_addr,
                                                  sizeof(uint32_t));
                 } else
                     panic("Invalid size for conditional read/write\n");
             }
 
             if (overwrite_mem)
-                std::memcpy(hostAddr, &overwrite_val[0], pkt->getSize());
+                std::memcpy(host_addr, &overwrite_val[0], pkt->getSize());
 
             assert(!pkt->req->isInstFetch());
             TRACE_PACKET("Read/Write");
@@ -412,7 +412,7 @@ AbstractMemory::access(PacketPtr pkt)
             trackLoadLocked(pkt);
         }
         if (pmemAddr) {
-            pkt->setData(hostAddr);
+            pkt->setData(host_addr);
         }
         TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
         stats.numReads[pkt->req->masterId()]++;
@@ -428,9 +428,9 @@ AbstractMemory::access(PacketPtr pkt)
     } else if (pkt->isWrite()) {
         if (writeOK(pkt)) {
             if (pmemAddr) {
-                pkt->writeData(hostAddr);
-                DPRINTF(MemoryAccess, "%s wrote %i bytes to address %x\n",
-                        __func__, pkt->getSize(), pkt->getAddr());
+                pkt->writeData(host_addr);
+                DPRINTF(MemoryAccess, "%s write due to %s\n",
+                        __func__, pkt->print());
             }
             assert(!pkt->req->isInstFetch());
             TRACE_PACKET("Write");
@@ -451,17 +451,17 @@ AbstractMemory::functionalAccess(PacketPtr pkt)
 {
     assert(pkt->getAddrRange().isSubset(range));
 
-    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start();
+    uint8_t *host_addr = toHostAddr(pkt->getAddr());
 
     if (pkt->isRead()) {
         if (pmemAddr) {
-            pkt->setData(hostAddr);
+            pkt->setData(host_addr);
         }
         TRACE_PACKET("Read");
         pkt->makeResponse();
     } else if (pkt->isWrite()) {
         if (pmemAddr) {
-            pkt->writeData(hostAddr);
+            pkt->writeData(host_addr);
         }
         TRACE_PACKET("Write");
         pkt->makeResponse();
@@ -473,7 +473,7 @@ AbstractMemory::functionalAccess(PacketPtr pkt)
         // through printObj().
         prs->printLabels();
         // Right now we just print the single byte at the specified address.
-        ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
+        ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *host_addr);
     } else {
         panic("AbstractMemory: unimplemented functional command %s",
               pkt->cmdString());
index d92e7070c2ff5b419d30d4490eb34434421c6382..14694f9d5c242357137188605c1562c1d568e1d1 100644 (file)
@@ -270,6 +270,19 @@ class AbstractMemory : public ClockedObject
      */
     AddrRange getAddrRange() const;
 
+    /**
+     * Transform a gem5 address space address into its physical counterpart
+     * in the host address space.
+     *
+     * @param addr Address in gem5's address space.
+     * @return Pointer to the corresponding memory address of the host.
+     */
+    inline uint8_t *
+    toHostAddr(Addr addr) const
+    {
+        return pmemAddr + addr - range.start();
+    }
+
     /**
      * Get the memory size.
      *