Skip to content

Commit

Permalink
Merge pull request #164 from comparch-security/fix-prefetch
Browse files Browse the repository at this point in the history
Fix prefetch
  • Loading branch information
wsong83 authored Dec 3, 2024
2 parents aadcb62 + 581d731 commit bd72089
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 201 deletions.
23 changes: 18 additions & 5 deletions cache/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ class CacheBase : public CacheMonitorSupport
// access both meta and data in one function call
virtual std::pair<CMMetadataBase *, CMDataBase *> access_line(uint32_t ai, uint32_t s, uint32_t w) = 0;

virtual void replace_read(uint32_t ai, uint32_t s, uint32_t w, bool prefetch, bool genre = false) = 0;
virtual void replace_write(uint32_t ai, uint32_t s, uint32_t w, bool demand_acc, bool genre = false) = 0;
virtual void replace_manage(uint32_t ai, uint32_t s, uint32_t w, bool hit, uint32_t evict, bool genre = false) = 0;

virtual bool query_coloc(uint64_t addrA, uint64_t addrB) = 0;
virtual LocInfo query_loc(uint64_t addr) { return LocInfo(id, this, addr); }
virtual void query_fill_loc(LocInfo *loc, uint64_t addr) = 0;
Expand Down Expand Up @@ -330,21 +334,30 @@ class CacheSkewed : public CacheBase
data_return_buffer(buffer_data);
}

virtual void hook_read(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, bool prefetch, const CMMetadataBase * meta, const CMDataBase *data, uint64_t *delay) override {
if(ai < P) replacer[ai].access(s, w, true, prefetch);
virtual void hook_read(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, const CMMetadataBase * meta, const CMDataBase *data, uint64_t *delay) override {
if constexpr (EnMon || !C_VOID<DLY>) monitors->hook_read(addr, ai, s, w, hit, meta, data, delay);
}

virtual void hook_write(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, bool demand_acc, const CMMetadataBase * meta, const CMDataBase *data, uint64_t *delay) override {
if(ai < P) replacer[ai].access(s, w, demand_acc, false);
virtual void replace_read(uint32_t ai, uint32_t s, uint32_t w, bool prefetch, bool genre = false) override {
if(ai < P) replacer[ai].access(s, w, true, prefetch);
}

virtual void hook_write(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, const CMMetadataBase * meta, const CMDataBase *data, uint64_t *delay) override {
if constexpr (EnMon || !C_VOID<DLY>) monitors->hook_write(addr, ai, s, w, hit, meta, data, delay);
}

virtual void replace_write(uint32_t ai, uint32_t s, uint32_t w, bool demand_acc, bool genre = false) override {
if(ai < P) replacer[ai].access(s, w, demand_acc, false);
}

virtual void hook_manage(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, uint32_t evict, bool writeback, const CMMetadataBase * meta, const CMDataBase *data, uint64_t *delay) override {
if(ai < P && hit && evict) replacer[ai].invalid(s, w, evict == 2);
if constexpr (EnMon || !C_VOID<DLY>) monitors->hook_manage(addr, ai, s, w, hit, evict, writeback, meta, data, delay);
}

virtual void replace_manage(uint32_t ai, uint32_t s, uint32_t w, bool hit, uint32_t evict, bool genre = false) override {
if(ai < P && hit && evict) replacer[ai].invalid(s, w, evict == 2);
}

virtual CMDataBase *data_copy_buffer() override {
if (data_buffer_pool_set.empty()) return nullptr;
if constexpr (EnMT) {
Expand Down
120 changes: 52 additions & 68 deletions cache/coherence.hpp

Large diffs are not rendered by default.

78 changes: 47 additions & 31 deletions cache/dynamic_random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include "util/monitor.hpp"
#include <numeric>

#define MAGIC_ID_REMAP 2024091300ul
#define MAGIC_ID_REMAP_ASK 2024091300ul
#define MAGIC_ID_REMAP_END 2024102700ul

struct RemapHelper {
static const unsigned int replace_for_relocate = 2408200ul;
Expand Down Expand Up @@ -112,7 +113,7 @@ class InnerCohPortRemapT : public InnerCohPortT<InnerCohPortUncached, Policy, En
auto [p, s, w] = cache->size();
uint32_t P, nset, nway;
std::tie(P, nset, nway) = std::make_tuple(static_cast<uint32_t>(p), static_cast<uint32_t>(s), static_cast<uint32_t>(w));
cache->monitors->pause();
// cache->monitors->pause();
static_cast<CT *>(cache)->remap_start();
for(uint32_t ai = 0; ai < P; ai++){
for(uint32_t idx = 0; idx < nset; idx++){
Expand All @@ -123,13 +124,16 @@ class InnerCohPortRemapT : public InnerCohPortT<InnerCohPortUncached, Policy, En
}
}
static_cast<CT *>(cache)->remap_end();
cache->monitors->resume();
// cache->monitors->resume();
}

virtual void finish_resp(uint64_t addr, coh_cmd_t outer_cmd){
cache->monitors->magic_func(addr, MAGIC_ID_REMAP, &remap_flag);
if(remap_flag) remap();
remap_flag = false;
cache->monitors->magic_func(addr, MAGIC_ID_REMAP_ASK, &remap_flag);
if(remap_flag){
remap();
cache->monitors->magic_func(addr, MAGIC_ID_REMAP_END, nullptr);
remap_flag = false;
}
InnerT::finish_resp(addr, outer_cmd);
}

Expand All @@ -140,11 +144,13 @@ class InnerCohPortRemapT : public InnerCohPortT<InnerCohPortUncached, Policy, En
auto[m_meta, m_data] = cache->access_line(new_ai, new_idx, new_way);
uint64_t m_addr = m_meta->addr(new_idx);
if (m_meta->is_valid()) {
if (static_cast<MT *>(m_meta)->is_relocated()) this->evict(m_meta, m_data, new_ai, new_idx, new_way, nullptr);
else cache->hook_manage(m_addr, new_ai, new_idx, new_way, true, 1, false, m_meta, m_data, nullptr);
if (static_cast<MT *>(m_meta)->is_relocated())
this->evict(m_meta, m_data, new_ai, new_idx, new_way, nullptr);
else
cache->replace_manage(new_ai, new_idx, new_way, true, 1);
}
static_cast<CT *>(cache)->swap(m_addr, c_addr, m_meta, c_meta, m_data, c_data);
cache->hook_read(c_addr, new_ai, new_idx, new_way, false, false, m_meta, m_data, nullptr);
cache->replace_read(new_ai, new_idx, new_way, false);
static_cast<MT *>(m_meta)->to_relocated();
c_addr = m_addr;
}
Expand All @@ -157,11 +163,10 @@ class InnerCohPortRemapT : public InnerCohPortT<InnerCohPortUncached, Policy, En
auto c_data = data ? cache->data_copy_buffer() : nullptr;
static_cast<CT *>(cache)->relocate(c_addr, meta, c_meta, data, c_data);
static_cast<MT *>(meta)->to_relocated();
cache->hook_manage(c_addr, ai, idx, way, true, 1, false, c_meta, c_data, nullptr);
cache->replace_manage(ai, idx, way, true, 1);

while(c_meta->is_valid()) relocation(c_meta, c_data, c_addr);

while(c_meta->is_valid()){
relocation(c_meta, c_data, c_addr);
}
cache->meta_return_buffer(c_meta);
cache->data_return_buffer(c_data);
}
Expand All @@ -177,15 +182,17 @@ class RemapperBase : public SimpleAccMonitor
public:
RemapperBase(bool remap_enable = true) : SimpleAccMonitor(true), remap_enable(remap_enable) {}

virtual void reset() override {
remap = false;
SimpleAccMonitor::reset();
}

virtual bool magic_func(uint64_t cache_id, uint64_t addr, uint64_t magic_id, void *magic_data) override {
if (magic_id == MAGIC_ID_REMAP) {
if(remap_enable) *static_cast<bool*>(magic_data) |= remap;
if (magic_id == MAGIC_ID_REMAP_ASK) {
if(remap_enable){
*static_cast<bool*>(magic_data) |= remap;
if(remap) active = false;
}
return true;
}
if (magic_id == MAGIC_ID_REMAP_END) {
remap = false;
active = true;
return true;
}
return false;
Expand Down Expand Up @@ -259,15 +266,12 @@ class ZSEVRemapper : public RemapperBase
}
virtual void write(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, bool hit, const CMMetadataBase *meta, const CMDataBase *data) override {
if(!active) return;
cnt_access++; cnt_write++;
if(!hit) { cnt_miss++; cnt_write_miss++;}
if(access_period != 0 && (cnt_access % access_period) == 0) {
if(Z_Score_detect()) {
remap = true;
}
evicts.fill(0);
cnt_write++;
if(!hit) {
cnt_write_miss++;
}
}

virtual void invalid(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, const CMMetadataBase *meta, const CMDataBase *data) override {
if(!active) return;
cnt_invalid++;
Expand All @@ -278,10 +282,22 @@ class ZSEVRemapper : public RemapperBase
}
}

virtual void reset() override {
evicts.fill(0);
set_evict_history.fill(0.0);
RemapperBase::reset();
virtual bool magic_func(uint64_t cache_id, uint64_t addr, uint64_t magic_id, void *magic_data) override {
if (magic_id == MAGIC_ID_REMAP_ASK) {
if(remap_enable){
*static_cast<bool*>(magic_data) |= remap;
if(remap) active = false;
}
return true;
}
if (magic_id == MAGIC_ID_REMAP_END) {
remap = false;
active = true;
cnt_access = 0; cnt_miss = 0; cnt_write = 0; cnt_write_miss = 0; cnt_invalid = 0;
evicts.fill(0); set_evict_history.fill(0.0);
return true;
}
return false;
}
};

Expand Down
Loading

0 comments on commit bd72089

Please sign in to comment.