mem-cache: Protect tag from being mishandled

Make the tag variable private, so that every access to it must pass
through a getter and a setter. This protects it from being incorrectly
updated when there is no direct match between a tag and a data entry,
as is the case of sector, compressed, decoupled, and many other table
layouts.

As a side effect, a block matching function has been created, which
also depends directly on the mapping between tag and data entries.

Change-Id: I848a154404feb5cbcea8d0fd2509bf49e1d73bd0
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/34955
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/mem/cache/cache_blk.cc b/src/mem/cache/cache_blk.cc
index 4d7e408..4b3325c 100644
--- a/src/mem/cache/cache_blk.cc
+++ b/src/mem/cache/cache_blk.cc
@@ -11,6 +11,7 @@
  * unmodified and in its entirety in all distributions of the software,
  * modified or unmodified, in source code or in binary form.
  *
+ * Copyright (c) 2020 Inria
  * Copyright (c) 2007 The Regents of The University of Michigan
  * All rights reserved.
  *
@@ -42,6 +43,12 @@
 
 #include "base/cprintf.hh"
 
+bool
+CacheBlk::matchTag(Addr tag, bool is_secure) const
+{
+    return isValid() && (getTag() == tag) && (isSecure() == is_secure);
+}
+
 void
 CacheBlk::insert(const Addr tag, const bool is_secure,
                  const int src_requestor_ID, const uint32_t task_ID)
@@ -50,7 +57,7 @@
     assert(status == 0);
 
     // Set block tag
-    this->tag = tag;
+    setTag(tag);
 
     // Set source requestor ID
     srcRequestorId = src_requestor_ID;
diff --git a/src/mem/cache/cache_blk.hh b/src/mem/cache/cache_blk.hh
index 99f8545..1de5a81 100644
--- a/src/mem/cache/cache_blk.hh
+++ b/src/mem/cache/cache_blk.hh
@@ -11,6 +11,7 @@
  * unmodified and in its entirety in all distributions of the software,
  * modified or unmodified, in source code or in binary form.
  *
+ * Copyright (c) 2020 Inria
  * Copyright (c) 2003-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
@@ -87,8 +88,6 @@
     /** Task Id associated with this block */
     uint32_t task_id;
 
-    /** Data block tag value. */
-    Addr tag;
     /**
      * Contains a copy of the data in this block for easy access. This is used
      * for efficient execution when the data could be actually stored in
@@ -210,7 +209,7 @@
      */
     virtual void invalidate()
     {
-        tag = MaxAddr;
+        setTag(MaxAddr);
         task_id = ContextSwitchTaskId::Unknown;
         status = 0;
         whenReady = MaxTick;
@@ -239,6 +238,20 @@
     }
 
     /**
+     * Get tag associated to this block.
+     *
+     * @return The tag value.
+     */
+    virtual Addr getTag() const { return _tag; }
+
+    /**
+     * Set tag associated to this block.
+     *
+     * @param The tag value.
+     */
+    virtual void setTag(Addr tag) { _tag = tag; }
+
+    /**
      * Check if this block holds data from the secure memory space.
      * @return True if the block holds data from the secure memory space.
      */
@@ -289,6 +302,14 @@
     }
 
     /**
+     * Checks if the given information corresponds to this block's.
+     *
+     * @param tag The tag value to compare to.
+     * @param is_secure Whether secure bit is set.
+     */
+    virtual bool matchTag(Addr tag, bool is_secure) const;
+
+    /**
      * Set member variables when a block insertion occurs. Resets reference
      * count to 1 (the insertion counts as a reference), and touch block if
      * it hadn't been touched previously. Sets the insertion tick to the
@@ -380,9 +401,8 @@
           default:    s = 'T'; break; // @TODO add other types
         }
         return csprintf("state: %x (%c) valid: %d writable: %d readable: %d "
-                        "dirty: %d | tag: %#x %s", status, s,
-                        isValid(), isWritable(), isReadable(), isDirty(), tag,
-                        ReplaceableEntry::print());
+            "dirty: %d | tag: %#x %s", status, s, isValid(), isWritable(),
+            isReadable(), isDirty(), getTag(), ReplaceableEntry::print());
     }
 
     /**
@@ -431,6 +451,10 @@
             return true;
         }
     }
+
+  private:
+    /** Data block tag value. */
+    Addr _tag;
 };
 
 /**
diff --git a/src/mem/cache/tags/base.cc b/src/mem/cache/tags/base.cc
index 32c6d29..985a1eb 100644
--- a/src/mem/cache/tags/base.cc
+++ b/src/mem/cache/tags/base.cc
@@ -86,8 +86,7 @@
     // Search for block
     for (const auto& location : entries) {
         CacheBlk* blk = static_cast<CacheBlk*>(location);
-        if ((blk->tag == tag) && blk->isValid() &&
-            (blk->isSecure() == is_secure)) {
+        if (blk->matchTag(tag, is_secure)) {
             return blk;
         }
     }
diff --git a/src/mem/cache/tags/base_set_assoc.hh b/src/mem/cache/tags/base_set_assoc.hh
index d743df7..d273f1e 100644
--- a/src/mem/cache/tags/base_set_assoc.hh
+++ b/src/mem/cache/tags/base_set_assoc.hh
@@ -226,7 +226,7 @@
      */
     Addr regenerateBlkAddr(const CacheBlk* blk) const override
     {
-        return indexingPolicy->regenerateAddr(blk->tag, blk);
+        return indexingPolicy->regenerateAddr(blk->getTag(), blk);
     }
 
     void forEachBlk(std::function<void(CacheBlk &)> visitor) override {
diff --git a/src/mem/cache/tags/compressed_tags.cc b/src/mem/cache/tags/compressed_tags.cc
index 126f664..f86d1a5 100644
--- a/src/mem/cache/tags/compressed_tags.cc
+++ b/src/mem/cache/tags/compressed_tags.cc
@@ -115,8 +115,7 @@
     const uint64_t offset = extractSectorOffset(addr);
     for (const auto& entry : superblock_entries){
         SuperBlk* superblock = static_cast<SuperBlk*>(entry);
-        if ((tag == superblock->getTag()) && superblock->isValid() &&
-            (is_secure == superblock->isSecure()) &&
+        if (superblock->matchTag(tag, is_secure) &&
             !superblock->blks[offset]->isValid() &&
             superblock->isCompressed() &&
             superblock->canCoAllocate(compressed_size))
diff --git a/src/mem/cache/tags/fa_lru.cc b/src/mem/cache/tags/fa_lru.cc
index 962c9fd..82f9519 100644
--- a/src/mem/cache/tags/fa_lru.cc
+++ b/src/mem/cache/tags/fa_lru.cc
@@ -118,7 +118,7 @@
 {
     // Erase block entry reference in the hash table
     M5_VAR_USED auto num_erased =
-        tagHash.erase(std::make_pair(blk->tag, blk->isSecure()));
+        tagHash.erase(std::make_pair(blk->getTag(), blk->isSecure()));
 
     // Sanity check; only one block reference should be erased
     assert(num_erased == 1);
@@ -177,7 +177,7 @@
     }
 
     if (blk && blk->isValid()) {
-        assert(blk->tag == tag);
+        assert(blk->getTag() == tag);
         assert(blk->isSecure() == is_secure);
     }
 
@@ -222,7 +222,7 @@
     moveToHead(falruBlk);
 
     // Insert new block in the hash table
-    tagHash[std::make_pair(blk->tag, blk->isSecure())] = falruBlk;
+    tagHash[std::make_pair(blk->getTag(), blk->isSecure())] = falruBlk;
 }
 
 void
diff --git a/src/mem/cache/tags/fa_lru.hh b/src/mem/cache/tags/fa_lru.hh
index b7d7a73..5feb518 100644
--- a/src/mem/cache/tags/fa_lru.hh
+++ b/src/mem/cache/tags/fa_lru.hh
@@ -251,7 +251,7 @@
      */
     Addr regenerateBlkAddr(const CacheBlk* blk) const override
     {
-        return blk->tag;
+        return blk->getTag();
     }
 
     void forEachBlk(std::function<void(CacheBlk &)> visitor) override {
diff --git a/src/mem/cache/tags/sector_blk.cc b/src/mem/cache/tags/sector_blk.cc
index e914cef..81c9de2 100644
--- a/src/mem/cache/tags/sector_blk.cc
+++ b/src/mem/cache/tags/sector_blk.cc
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018 Inria
+ * Copyright (c) 2018, 2020 Inria
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -66,7 +66,21 @@
 Addr
 SectorSubBlk::getTag() const
 {
-    return _sectorBlk->getTag();
+    // If the sub-block is valid its tag must match its sector's
+    const Addr tag = _sectorBlk->getTag();
+    assert(!isValid() || (CacheBlk::getTag() == tag));
+    return tag;
+}
+
+void
+SectorSubBlk::setTag(Addr tag)
+{
+    CacheBlk::setTag(tag);
+
+    // The sector block handles its own tag's invalidation
+    if (tag != MaxAddr) {
+        _sectorBlk->setTag(tag);
+    }
 }
 
 void
@@ -95,15 +109,10 @@
                      const int src_requestor_ID, const uint32_t task_ID)
 {
     // Make sure it is not overwriting another sector
-    panic_if((_sectorBlk && _sectorBlk->isValid()) &&
-             ((_sectorBlk->getTag() != tag) ||
-              (_sectorBlk->isSecure() != is_secure)),
-              "Overwriting valid sector!");
+    panic_if(_sectorBlk && _sectorBlk->isValid() &&
+        !_sectorBlk->matchTag(tag, is_secure), "Overwriting valid sector!");
 
     CacheBlk::insert(tag, is_secure, src_requestor_ID, task_ID);
-
-    // Set sector tag
-    _sectorBlk->setTag(tag);
 }
 
 std::string
@@ -163,6 +172,7 @@
     // so clear secure bit
     if (--_validCounter == 0) {
         _secureBit = false;
+        setTag(MaxAddr);
     }
 }
 
@@ -180,3 +190,9 @@
         blk->setPosition(set, way);
     }
 }
+
+bool
+SectorBlk::matchTag(Addr tag, bool is_secure) const
+{
+    return isValid() && (getTag() == tag) && (isSecure() == is_secure);
+}
diff --git a/src/mem/cache/tags/sector_blk.hh b/src/mem/cache/tags/sector_blk.hh
index 5538aa1..c5c4fc8 100644
--- a/src/mem/cache/tags/sector_blk.hh
+++ b/src/mem/cache/tags/sector_blk.hh
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018 Inria
+ * Copyright (c) 2018, 2020 Inria
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -92,12 +92,8 @@
      */
     int getSectorOffset() const;
 
-    /**
-     * Get tag associated to this block.
-     *
-     * @return The tag value.
-     */
-    Addr getTag() const;
+    Addr getTag() const override;
+    void setTag(Addr tag) override;
 
     /**
      * Set valid bit and inform sector block.
@@ -228,6 +224,14 @@
      * @param way The way of this entry and sub-entries.
      */
     void setPosition(const uint32_t set, const uint32_t way) override;
+
+    /**
+     * Checks if the given information corresponds to this block's.
+     *
+     * @param tag The tag value to compare to.
+     * @param is_secure Whether secure bit is set.
+     */
+    virtual bool matchTag(Addr tag, bool is_secure) const;
 };
 
 #endif //__MEM_CACHE_TAGS_SECTOR_BLK_HH__
diff --git a/src/mem/cache/tags/sector_tags.cc b/src/mem/cache/tags/sector_tags.cc
index bf40664..e43237e 100644
--- a/src/mem/cache/tags/sector_tags.cc
+++ b/src/mem/cache/tags/sector_tags.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Inria
+ * Copyright (c) 2018, 2020 Inria
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -209,8 +209,7 @@
     // Search for block
     for (const auto& sector : entries) {
         auto blk = static_cast<SectorBlk*>(sector)->blks[offset];
-        if (blk->getTag() == tag && blk->isValid() &&
-            blk->isSecure() == is_secure) {
+        if (blk->matchTag(tag, is_secure)) {
             return blk;
         }
     }
@@ -232,8 +231,7 @@
     SectorBlk* victim_sector = nullptr;
     for (const auto& sector : sector_entries) {
         SectorBlk* sector_blk = static_cast<SectorBlk*>(sector);
-        if ((tag == sector_blk->getTag()) && sector_blk->isValid() &&
-            (is_secure == sector_blk->isSecure())){
+        if (sector_blk->matchTag(tag, is_secure)) {
             victim_sector = sector_blk;
             break;
         }
@@ -251,8 +249,7 @@
 
     // Get evicted blocks. Blocks are only evicted if the sectors mismatch and
     // the currently existing sector is valid.
-    if ((tag == victim_sector->getTag()) &&
-        (is_secure == victim_sector->isSecure())){
+    if (victim_sector->matchTag(tag, is_secure)) {
         // It would be a hit if victim was valid, and upgrades do not call
         // findVictim, so it cannot happen
         assert(!victim->isValid());
@@ -282,7 +279,8 @@
 {
     const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk);
     const SectorBlk* sec_blk = blk_cast->getSectorBlock();
-    const Addr sec_addr = indexingPolicy->regenerateAddr(blk->tag, sec_blk);
+    const Addr sec_addr =
+        indexingPolicy->regenerateAddr(blk->getTag(), sec_blk);
     return sec_addr | ((Addr)blk_cast->getSectorOffset() << sectorShift);
 }