Skip to content

Commit

Permalink
Merge pull request #70 from comparch-security/finish
Browse files Browse the repository at this point in the history
Add finish port
  • Loading branch information
wsong83 authored Jun 7, 2024
2 parents 2924430 + e8739de commit 8123c92
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cache/coh_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CohPolicyBase {
static const uint32_t release_msg = 2;
static const uint32_t probe_msg = 3;
static const uint32_t flush_msg = 4;
static const uint32_t finish_msg = 5;
static const uint32_t fetch_read_act = 0;
static const uint32_t fetch_write_act = 1;
static const uint32_t evict_act = 2;
Expand All @@ -48,6 +49,7 @@ class CohPolicyBase {
bool is_release(coh_cmd_t cmd) const { return cmd.msg == release_msg; }
bool is_probe(coh_cmd_t cmd) const { return cmd.msg == probe_msg; }
bool is_flush(coh_cmd_t cmd) const { return cmd.msg == flush_msg; }
bool is_finish(coh_cmd_t cmd) const { return cmd.msg == finish_msg; }

// action type
bool is_fetch_read(coh_cmd_t cmd) const { return cmd.act == fetch_read_act; }
Expand All @@ -73,6 +75,7 @@ class CohPolicyBase {
coh_cmd_t cmd_for_probe_writeback(int32_t id) const { return {id, probe_msg, writeback_act }; }
coh_cmd_t cmd_for_probe_release(int32_t id) const { return {id, probe_msg, evict_act }; }
coh_cmd_t cmd_for_probe_downgrade(int32_t id) const { return {id, probe_msg, downgrade_act }; }
coh_cmd_t cmd_for_finish(int32_t id) const { return {id, finish_msg, 0 }; }

virtual coh_cmd_t cmd_for_outer_acquire(coh_cmd_t cmd) const = 0;

Expand Down
61 changes: 55 additions & 6 deletions cache/coherence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class OuterCohPortBase

virtual void acquire_req(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) = 0;
virtual void writeback_req(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) = 0;
virtual std::pair<bool, bool> probe_resp(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) { return std::make_pair(false,false); } // may not implement if not supported

// may not implement probe_resp() and finish_req() if the port is uncached
virtual std::pair<bool, bool> probe_resp(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) { return std::make_pair(false,false); }
virtual void finish_req(uint64_t addr) {}

bool is_uncached() const { return coh_id == -1; }
virtual void query_loc_req(uint64_t addr, std::list<LocInfo> *locs) = 0;
Expand Down Expand Up @@ -69,8 +72,12 @@ class InnerCohPortBase
}

virtual void acquire_resp(uint64_t addr, CMDataBase *data_inner, CMMetadataBase *meta_inner, coh_cmd_t outer_cmd, uint64_t *delay) = 0;
virtual void writeback_resp(uint64_t addr, CMDataBase *data_inner, CMMetadataBase *meta_inner, coh_cmd_t cmd, uint64_t *delay) = 0;
virtual std::pair<bool,bool> probe_req(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) { return std::make_pair(false,false); } // may not implement if not supported
virtual void writeback_resp(uint64_t addr, CMDataBase *data_inner, CMMetadataBase *meta_inner, coh_cmd_t outer_cmd, uint64_t *delay) = 0;

// may not implement probe_req() and finish_resp() if the port is uncached
virtual std::pair<bool,bool> probe_req(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) { return std::make_pair(false,false); }
virtual void finish_record(uint64_t addr, coh_cmd_t outer_cmd) {};
virtual void finish_resp(uint64_t addr, coh_cmd_t outer_cmd) {};

virtual void query_loc_resp(uint64_t addr, std::list<LocInfo> *locs) = 0;

Expand Down Expand Up @@ -102,8 +109,21 @@ class OuterCohPortUncached : public OuterCohPortBase
}
};

// common behvior for cached outer ports
class OuterCohPortCachedBase : public OuterCohPortUncached
{
public:
OuterCohPortCachedBase(policy_ptr policy) : OuterCohPortUncached(policy) {}
virtual ~OuterCohPortCachedBase() {}

virtual void finish_req(uint64_t addr){
assert(!is_uncached());
coh->finish_resp(addr, policy->cmd_for_finish(coh_id));
}
};

// common behavior for cached outer ports
template<class OPUC> requires C_DERIVE(OPUC, OuterCohPortUncached)
template<class OPUC> requires C_DERIVE(OPUC, OuterCohPortCachedBase)
class OuterCohPortT : public OPUC
{
protected:
Expand Down Expand Up @@ -140,9 +160,10 @@ class OuterCohPortT : public OPUC
cache->hook_manage(addr, ai, s, w, hit, OPUC::policy->is_outer_evict(outer_cmd), writeback, meta, data, delay);
return std::make_pair(hit, writeback);
}

};

typedef OuterCohPortT<OuterCohPortUncached> OuterCohPort;
typedef OuterCohPortT<OuterCohPortCachedBase> OuterCohPort;

class InnerCohPortUncached : public InnerCohPortBase
{
Expand All @@ -156,6 +177,8 @@ class InnerCohPortUncached : public InnerCohPortBase
if (data_inner && data) data_inner->copy(data);
policy->meta_after_grant(cmd, meta, meta_inner);
cache->hook_read(addr, ai, s, w, hit, meta, data, delay);
if(!hit) finish_record(addr, policy->cmd_for_finish(cmd.id));
if(cmd.id == -1) finish_resp(addr, policy->cmd_for_finish(cmd.id));
}

virtual void writeback_resp(uint64_t addr, CMDataBase *data_inner, CMMetadataBase *meta_inner, coh_cmd_t cmd, uint64_t *delay) {
Expand Down Expand Up @@ -262,10 +285,16 @@ class InnerCohPortUncached : public InnerCohPortBase
template<class IPUC> requires C_DERIVE(IPUC, InnerCohPortUncached)
class InnerCohPortT : public IPUC
{
private:
// record the pending finish message from inner caches
// replace the single storage to a set in multi-thread sim
uint64_t addr_pending_finish;
int32_t id_pending_finish;
protected:
using IPUC::coh;
using IPUC::outer;
public:
InnerCohPortT(policy_ptr policy) : IPUC(policy) {}
InnerCohPortT(policy_ptr policy) : IPUC(policy), addr_pending_finish(0) {}
virtual ~InnerCohPortT() {}

virtual std::pair<bool, bool> probe_req(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) {
Expand All @@ -280,6 +309,21 @@ class InnerCohPortT : public IPUC
}
return std::make_pair(hit, writeback);
}

// record pending finish
virtual void finish_record(uint64_t addr, coh_cmd_t outer_cmd) {
addr_pending_finish = addr;
id_pending_finish = outer_cmd.id;
}

// only forward the finish message recorded by previous acquire
virtual void finish_resp(uint64_t addr, coh_cmd_t outer_cmd) {
assert(addr_pending_finish == 0 || (addr_pending_finish == addr && id_pending_finish == outer_cmd.id));
if(addr_pending_finish == addr && id_pending_finish == outer_cmd.id) {
outer->finish_req(addr);
addr_pending_finish = 0;
}
}
};

typedef InnerCohPortT<InnerCohPortUncached> InnerCohPort;
Expand Down Expand Up @@ -317,6 +361,7 @@ class CoreInterface : public InnerCohPortUncached, public CoreInterfaceBase {
auto cmd = policy->cmd_for_read();
auto [meta, data, ai, s, w, hit] = access_line(addr, cmd, delay);
cache->hook_read(addr, ai, s, w, hit, meta, data, delay);
if(!hit) outer->finish_req(addr);
return data;
}

Expand All @@ -327,6 +372,7 @@ class CoreInterface : public InnerCohPortUncached, public CoreInterfaceBase {
meta->to_dirty();
if(data) data->copy(m_data);
cache->hook_write(addr, ai, s, w, hit, false, meta, data, delay);
if(!hit) outer->finish_req(addr);
}

virtual void flush(uint64_t addr, uint64_t *delay) { addr = normalize(addr); flush_line(addr, policy->cmd_for_flush(), delay); }
Expand Down Expand Up @@ -436,6 +482,9 @@ class SliceDispatcher : public CohMasterBase
virtual void query_loc_resp(uint64_t addr, std::list<LocInfo> *locs){
cohm[hasher(addr)]->query_loc_resp(addr, locs);
}
virtual void finish_resp(uint64_t addr, coh_cmd_t cmd){
cohm[hasher(addr)]->finish_resp(addr, cmd);
}
};

#endif
14 changes: 10 additions & 4 deletions cache/exclusive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class ExclusiveInnerCohPortUncachedBroadcast : public InnerCohPortUncached

cache->meta_return_buffer(meta);
cache->data_return_buffer(data);

if(!hit) finish_record(addr, policy->cmd_for_finish(cmd.id));
if(cmd.id == -1) finish_resp(addr, policy->cmd_for_finish(cmd.id));
}


Expand Down Expand Up @@ -365,6 +368,9 @@ class ExclusiveInnerCohPortUncachedDirectory : public InnerCohPortUncached

// difficult to know when data is borrowed from buffer, just return it.
cache->data_return_buffer(data);

if(!hit) finish_record(addr, policy->cmd_for_finish(outer_cmd.id));
if(outer_cmd.id == -1) finish_resp(addr, policy->cmd_for_finish(outer_cmd.id));
}

protected:
Expand Down Expand Up @@ -516,7 +522,7 @@ class ExclusiveInnerCohPortUncachedDirectory : public InnerCohPortUncached
typedef InnerCohPortT<ExclusiveInnerCohPortUncachedDirectory> ExclusiveInnerCohPortDirectory;


template<class OPUC> requires C_DERIVE(OPUC, OuterCohPortUncached)
template<class OPUC> requires C_DERIVE(OPUC, OuterCohPortCachedBase)
class ExclusiveOuterCohPortBroadcastT : public OPUC
{
protected:
Expand Down Expand Up @@ -564,10 +570,10 @@ class ExclusiveOuterCohPortBroadcastT : public OPUC

};

typedef ExclusiveOuterCohPortBroadcastT<OuterCohPortUncached> ExclusiveOuterCohPortBroadcast;
typedef ExclusiveOuterCohPortBroadcastT<OuterCohPortCachedBase> ExclusiveOuterCohPortBroadcast;


template<class OPUC> requires C_DERIVE(OPUC, OuterCohPortUncached)
template<class OPUC> requires C_DERIVE(OPUC, OuterCohPortCachedBase)
class ExclusiveOuterCohPortDirectoryT : public OPUC
{
protected:
Expand Down Expand Up @@ -615,7 +621,7 @@ class ExclusiveOuterCohPortDirectoryT : public OPUC

};

typedef ExclusiveOuterCohPortDirectoryT<OuterCohPortUncached> ExclusiveOuterCohPortDirectory;
typedef ExclusiveOuterCohPortDirectoryT<OuterCohPortCachedBase> ExclusiveOuterCohPortDirectory;

template<typename CT>
using ExclusiveL2CacheBroadcast = CoherentCacheNorm<CT, ExclusiveOuterCohPortBroadcast, ExclusiveInnerCohPortBroadcast>;
Expand Down

0 comments on commit 8123c92

Please sign in to comment.