mem, arm: Move some helper methods into the base PortProxy class.

These were originally in the SETranslatingPortProxy class, but they're
not specific to SE mode in any way and are an unnecessary divergence
between the SE and FS mode translating port proxies.

Change-Id: I8cb77531cc287bd15b2386410ffa7b43cdfa67d0
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/18570
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
diff --git a/src/mem/fs_translating_port_proxy.cc b/src/mem/fs_translating_port_proxy.cc
index 15ad823..a21d328 100644
--- a/src/mem/fs_translating_port_proxy.cc
+++ b/src/mem/fs_translating_port_proxy.cc
@@ -66,12 +66,8 @@
 {
 }
 
-FSTranslatingPortProxy::~FSTranslatingPortProxy()
-{
-}
-
-void
-FSTranslatingPortProxy::readBlob(Addr addr, uint8_t *p, int size) const
+bool
+FSTranslatingPortProxy::tryReadBlob(Addr addr, uint8_t *p, int size) const
 {
     Addr paddr;
     for (ChunkGenerator gen(addr, size, TheISA::PageBytes); !gen.done();
@@ -85,10 +81,12 @@
         PortProxy::readBlobPhys(paddr, 0, p, gen.size());
         p += gen.size();
     }
+    return true;
 }
 
-void
-FSTranslatingPortProxy::writeBlob(Addr addr, const uint8_t *p, int size) const
+bool
+FSTranslatingPortProxy::tryWriteBlob(
+        Addr addr, const uint8_t *p, int size) const
 {
     Addr paddr;
     for (ChunkGenerator gen(addr, size, TheISA::PageBytes); !gen.done();
@@ -102,10 +100,11 @@
         PortProxy::writeBlobPhys(paddr, 0, p, gen.size());
         p += gen.size();
     }
+    return true;
 }
 
-void
-FSTranslatingPortProxy::memsetBlob(Addr address, uint8_t v, int size) const
+bool
+FSTranslatingPortProxy::tryMemsetBlob(Addr address, uint8_t v, int size) const
 {
     Addr paddr;
     for (ChunkGenerator gen(address, size, TheISA::PageBytes); !gen.done();
@@ -118,6 +117,7 @@
 
         PortProxy::memsetBlobPhys(paddr, 0, v, gen.size());
     }
+    return true;
 }
 
 void
diff --git a/src/mem/fs_translating_port_proxy.hh b/src/mem/fs_translating_port_proxy.hh
index d4b4eb5..5ae8700 100644
--- a/src/mem/fs_translating_port_proxy.hh
+++ b/src/mem/fs_translating_port_proxy.hh
@@ -81,20 +81,20 @@
 
     FSTranslatingPortProxy(MasterPort &port, unsigned int cacheLineSize);
 
-    ~FSTranslatingPortProxy();
+    ~FSTranslatingPortProxy() {}
 
-    /** Version of readblob that translates virt->phys and deals
+    /** Version of tryReadblob that translates virt->phys and deals
       * with page boundries. */
-    void readBlob(Addr addr, uint8_t *p, int size) const override;
+    bool tryReadBlob(Addr addr, uint8_t *p, int size) const override;
 
-    /** Version of writeBlob that translates virt->phys and deals
+    /** Version of tryWriteBlob that translates virt->phys and deals
       * with page boundries. */
-    void writeBlob(Addr addr, const uint8_t *p, int size) const override;
+    bool tryWriteBlob(Addr addr, const uint8_t *p, int size) const override;
 
     /**
      * Fill size bytes starting at addr with byte value val.
      */
-    void memsetBlob(Addr address, uint8_t  v, int size) const override;
+    bool tryMemsetBlob(Addr address, uint8_t  v, int size) const override;
 };
 
 void CopyOut(ThreadContext *tc, void *dest, Addr src, size_t cplen);
diff --git a/src/mem/port_proxy.cc b/src/mem/port_proxy.cc
index f13bcbe..97eb67e 100644
--- a/src/mem/port_proxy.cc
+++ b/src/mem/port_proxy.cc
@@ -87,3 +87,26 @@
 
     delete [] buf;
 }
+
+bool
+PortProxy::tryWriteString(Addr addr, const char *str) const
+{
+    do {
+        if (!tryWriteBlob(addr++, (uint8_t *)str, 1))
+            return false;
+    } while (*str++);
+    return true;
+}
+
+bool
+PortProxy::tryReadString(std::string &str, Addr addr) const
+{
+    while (true) {
+        uint8_t c;
+        if (!tryReadBlob(addr++, &c, 1))
+            return false;
+        if (!c)
+            return true;
+        str += c;
+    }
+}
diff --git a/src/mem/port_proxy.hh b/src/mem/port_proxy.hh
index bed448d..dcc1905 100644
--- a/src/mem/port_proxy.hh
+++ b/src/mem/port_proxy.hh
@@ -92,32 +92,9 @@
     {}
     virtual ~PortProxy() { }
 
-    /**
-     * Read size bytes memory at address and store in p.
-     */
-    virtual void
-    readBlob(Addr addr, uint8_t* p, int size) const
-    {
-        readBlobPhys(addr, 0, p, size);
-    }
 
-    /**
-     * Write size bytes from p to address.
-     */
-    virtual void
-    writeBlob(Addr addr, const uint8_t* p, int size) const
-    {
-        writeBlobPhys(addr, 0, p, size);
-    }
 
-    /**
-     * Fill size bytes starting at addr with byte value val.
-     */
-    virtual void
-    memsetBlob(Addr addr, uint8_t v, int size) const
-    {
-        memsetBlobPhys(addr, 0, v, size);
-    }
+    /** Fixed functionality for use in base classes. */
 
     /**
      * Read size bytes memory at physical address and store in p.
@@ -137,6 +114,77 @@
     void memsetBlobPhys(Addr addr, Request::Flags flags,
                         uint8_t v, int size) const;
 
+
+
+    /** Methods to override in base classes */
+
+    /**
+     * Read size bytes memory at address and store in p.
+     * Returns true on success and false on failure.
+     */
+    virtual bool
+    tryReadBlob(Addr addr, uint8_t *p, int size) const
+    {
+        readBlobPhys(addr, 0, p, size);
+        return true;
+    }
+
+    /**
+     * Write size bytes from p to address.
+     * Returns true on success and false on failure.
+     */
+    virtual bool
+    tryWriteBlob(Addr addr, const uint8_t *p, int size) const
+    {
+        writeBlobPhys(addr, 0, p, size);
+        return true;
+    }
+
+    /**
+     * Fill size bytes starting at addr with byte value val.
+     * Returns true on success and false on failure.
+     */
+    virtual bool
+    tryMemsetBlob(Addr addr, uint8_t val, int size) const
+    {
+        memsetBlobPhys(addr, 0, val, size);
+        return true;
+    }
+
+
+
+    /** Higher level interfaces based on the above. */
+
+    /**
+     * Same as tryReadBlob, but insists on success.
+     */
+    void
+    readBlob(Addr addr, uint8_t* p, int size) const
+    {
+        if (!tryReadBlob(addr, p, size))
+            fatal("readBlob(%#x, ...) failed", addr);
+    }
+
+    /**
+     * Same as tryWriteBlob, but insists on success.
+     */
+    void
+    writeBlob(Addr addr, const uint8_t* p, int size) const
+    {
+        if (!tryWriteBlob(addr, p, size))
+            fatal("writeBlob(%#x, ...) failed", addr);
+    }
+
+    /**
+     * Same as tryMemsetBlob, but insists on success.
+     */
+    void
+    memsetBlob(Addr addr, uint8_t v, int size) const
+    {
+        if (!tryMemsetBlob(addr, v, size))
+            fatal("memsetBlob(%#x, ...) failed", addr);
+    }
+
     /**
      * Read sizeof(T) bytes from address and return as object T.
      */
@@ -162,6 +210,38 @@
      */
     template <typename T>
     void write(Addr address, T data, ByteOrder guest_byte_order) const;
+
+    /**
+     * Write the string str into guest memory at address addr.
+     * Returns true on success and false on failure.
+     */
+    bool tryWriteString(Addr addr, const char *str) const;
+
+    /**
+     * Same as tryWriteString, but insists on success.
+     */
+    void
+    writeString(Addr addr, const char *str) const
+    {
+        if (!tryWriteString(addr, str))
+            fatal("writeString(%#x, ...) failed", addr);
+    }
+
+    /**
+     * Reads the string at guest address addr into the std::string str.
+     * Returns true on success and false on failure.
+     */
+    bool tryReadString(std::string &str, Addr addr) const;
+
+    /**
+     * Same as tryReadString, but insists on success.
+     */
+    void
+    readString(std::string &str, Addr addr) const
+    {
+        if (!tryReadString(str, addr))
+            fatal("readString(%#x, ...) failed", addr);
+    }
 };
 
 
diff --git a/src/mem/se_translating_port_proxy.cc b/src/mem/se_translating_port_proxy.cc
index bb30ffb..de5335a 100644
--- a/src/mem/se_translating_port_proxy.cc
+++ b/src/mem/se_translating_port_proxy.cc
@@ -61,9 +61,6 @@
       process(p), allocating(alloc)
 { }
 
-SETranslatingPortProxy::~SETranslatingPortProxy()
-{ }
-
 bool
 SETranslatingPortProxy::tryReadBlob(Addr addr, uint8_t *p, int size) const
 {
@@ -82,13 +79,6 @@
     return true;
 }
 
-void
-SETranslatingPortProxy::readBlob(Addr addr, uint8_t *p, int size) const
-{
-    if (!tryReadBlob(addr, p, size))
-        fatal("readBlob(0x%x, ...) failed", addr);
-}
-
 
 bool
 SETranslatingPortProxy::tryWriteBlob(Addr addr, const uint8_t *p,
@@ -122,13 +112,6 @@
 }
 
 
-void
-SETranslatingPortProxy::writeBlob(Addr addr, const uint8_t *p, int size) const
-{
-    if (!tryWriteBlob(addr, p, size))
-        fatal("writeBlob(0x%x, ...) failed", addr);
-}
-
 bool
 SETranslatingPortProxy::tryMemsetBlob(Addr addr, uint8_t val, int size) const
 {
@@ -150,69 +133,3 @@
 
     return true;
 }
-
-void
-SETranslatingPortProxy::memsetBlob(Addr addr, uint8_t val, int size) const
-{
-    if (!tryMemsetBlob(addr, val, size))
-        fatal("memsetBlob(0x%x, ...) failed", addr);
-}
-
-
-bool
-SETranslatingPortProxy::tryWriteString(Addr addr, const char *str) const
-{
-    uint8_t c;
-
-    Addr vaddr = addr;
-
-    do {
-        c = *str++;
-        Addr paddr;
-
-        if (!pTable->translate(vaddr++, paddr))
-            return false;
-
-        PortProxy::writeBlob(paddr, &c, 1);
-    } while (c);
-
-    return true;
-}
-
-void
-SETranslatingPortProxy::writeString(Addr addr, const char *str) const
-{
-    if (!tryWriteString(addr, str))
-        fatal("writeString(0x%x, ...) failed", addr);
-}
-
-bool
-SETranslatingPortProxy::tryReadString(std::string &str, Addr addr) const
-{
-    uint8_t c;
-
-    Addr vaddr = addr;
-
-    while (true) {
-        Addr paddr;
-
-        if (!pTable->translate(vaddr++, paddr))
-            return false;
-
-        PortProxy::readBlob(paddr, &c, 1);
-        if (c == '\0')
-            break;
-
-        str += c;
-    }
-
-    return true;
-}
-
-void
-SETranslatingPortProxy::readString(std::string &str, Addr addr) const
-{
-    if (!tryReadString(str, addr))
-        fatal("readString(0x%x, ...) failed", addr);
-}
-
diff --git a/src/mem/se_translating_port_proxy.hh b/src/mem/se_translating_port_proxy.hh
index 04bfd8a..1c8828b 100644
--- a/src/mem/se_translating_port_proxy.hh
+++ b/src/mem/se_translating_port_proxy.hh
@@ -81,22 +81,13 @@
 
   public:
     SETranslatingPortProxy(MasterPort& port, Process* p, AllocType alloc);
-    ~SETranslatingPortProxy();
+    ~SETranslatingPortProxy() {}
 
     void setPageTable(EmulationPageTable *p) { pTable = p; }
     void setProcess(Process *p) { process = p; }
-    bool tryReadBlob(Addr addr, uint8_t *p, int size) const;
-    bool tryWriteBlob(Addr addr, const uint8_t *p, int size) const;
-    bool tryMemsetBlob(Addr addr, uint8_t val, int size) const;
-    bool tryWriteString(Addr addr, const char *str) const;
-    bool tryReadString(std::string &str, Addr addr) const;
-
-    void readBlob(Addr addr, uint8_t *p, int size) const override;
-    void writeBlob(Addr addr, const uint8_t *p, int size) const override;
-    void memsetBlob(Addr addr, uint8_t val, int size) const override;
-
-    void writeString(Addr addr, const char *str) const;
-    void readString(std::string &str, Addr addr) const;
+    bool tryReadBlob(Addr addr, uint8_t *p, int size) const override;
+    bool tryWriteBlob(Addr addr, const uint8_t *p, int size) const override;
+    bool tryMemsetBlob(Addr addr, uint8_t val, int size) const override;
 };
 
 #endif // __MEM_SE_TRANSLATING_PORT_PROXY_HH__
diff --git a/src/mem/secure_port_proxy.cc b/src/mem/secure_port_proxy.cc
index 7bf23d7..645baa9 100644
--- a/src/mem/secure_port_proxy.cc
+++ b/src/mem/secure_port_proxy.cc
@@ -39,20 +39,23 @@
 
 #include "mem/secure_port_proxy.hh"
 
-void
-SecurePortProxy::readBlob(Addr addr, uint8_t *p, int size) const
+bool
+SecurePortProxy::tryReadBlob(Addr addr, uint8_t *p, int size) const
 {
     readBlobPhys(addr, Request::SECURE, p, size);
+    return true;
 }
 
-void
-SecurePortProxy::writeBlob(Addr addr, const uint8_t *p, int size) const
+bool
+SecurePortProxy::tryWriteBlob(Addr addr, const uint8_t *p, int size) const
 {
     writeBlobPhys(addr, Request::SECURE, p, size);
+    return true;
 }
 
-void
-SecurePortProxy::memsetBlob(Addr addr, uint8_t v, int size) const
+bool
+SecurePortProxy::tryMemsetBlob(Addr addr, uint8_t v, int size) const
 {
     memsetBlobPhys(addr, Request::SECURE, v, size);
+    return true;
 }
diff --git a/src/mem/secure_port_proxy.hh b/src/mem/secure_port_proxy.hh
index 857d70b..0323312 100644
--- a/src/mem/secure_port_proxy.hh
+++ b/src/mem/secure_port_proxy.hh
@@ -73,9 +73,9 @@
     SecurePortProxy(MasterPort &port, unsigned int cache_line_size)
         : PortProxy(port, cache_line_size) {}
 
-    void readBlob(Addr addr, uint8_t *p, int size) const override;
-    void writeBlob(Addr addr, const uint8_t *p, int size) const override;
-    void memsetBlob(Addr addr, uint8_t val, int size) const override;
+    bool tryReadBlob(Addr addr, uint8_t *p, int size) const override;
+    bool tryWriteBlob(Addr addr, const uint8_t *p, int size) const override;
+    bool tryMemsetBlob(Addr addr, uint8_t val, int size) const override;
 };
 
 #endif // __MEM_SECURE_PORT_PROXY_HH__