Skip to content

Commit

Permalink
Do not allow dmiWrite to touch x0 (#546)
Browse files Browse the repository at this point in the history
* Do not allow dmiWrite to touch x0

* Do not allow dmiWrite to touch x0 (address PR feedback)
  • Loading branch information
colby-nyce authored Dec 13, 2024
1 parent 7f7bf30 commit e736f0d
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion sparta/sparta/functional/Register.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ class RegisterBase : public TreeNode
*/
typedef uint16_t RegDomainT;
const RegDomainT regdomain;

/*!
* \brief Writable flag, taken from the register definition.
*/
const bool writable = true;
};

//! Represents an invalid Register ID
Expand Down Expand Up @@ -1005,6 +1010,7 @@ class RegisterBase : public TreeNode
template <typename T>
void write(T val, index_type idx=0)
{
if (!isWritable()) return;
write(&val, sizeof(val), idx * sizeof(val));
}

Expand All @@ -1014,6 +1020,7 @@ class RegisterBase : public TreeNode
template <typename T>
bool writeWithCheck(T val)
{
if (!isWritable()) return false;
if (hasWriteCB()) {
static_assert((sizeof(T)==4) || (sizeof(T)==8),
"write callback only support for 4- and 8-byte registers");
Expand All @@ -1031,6 +1038,7 @@ class RegisterBase : public TreeNode
template <typename T>
void writeUnmasked(T val, index_type idx=0)
{
if (!isWritable()) return;
writeUnmasked(&val, sizeof(val), idx * sizeof(val));
}

Expand All @@ -1049,6 +1057,7 @@ class RegisterBase : public TreeNode
template <typename T>
void poke(T val, index_type idx=0)
{
if (!isWritable()) return;
poke(&val, sizeof(val), idx * sizeof(val));
}

Expand All @@ -1060,6 +1069,7 @@ class RegisterBase : public TreeNode
template <typename T>
void pokeUnmasked(T val, index_type idx=0)
{
if (!isWritable()) return;
pokeUnmasked(&val, sizeof(val), idx * sizeof(val));
}

Expand All @@ -1081,6 +1091,7 @@ class RegisterBase : public TreeNode
template <typename T>
void dmiWrite(T val, index_type idx = 0)
{
if (!isWritable()) return;
dmiWrite_(&val, sizeof(val), sizeof(val) * idx);
}

Expand All @@ -1098,6 +1109,14 @@ class RegisterBase : public TreeNode
return *(reinterpret_cast<const T*>(mask_.data()) + idx);
}

/*!
* \brief
*/
bool isWritable() const
{
return def_.writable;
}

////////////////////////////////////////////////////////////////////////
//! @}

Expand Down Expand Up @@ -1195,6 +1214,7 @@ class RegisterBase : public TreeNode

void write(const void *buf, size_t size, size_t offset)
{
if (!isWritable()) return;
sparta_assert(offset + size <= getNumBytes(), "Access out of bounds");
RegisterBits val(reinterpret_cast<const uint8_t *>(buf), size);
RegisterBits mask = mask_ >> 8 * offset;
Expand All @@ -1204,12 +1224,14 @@ class RegisterBase : public TreeNode

void writeUnmasked(const void *buf, size_t size, size_t offset)
{
if (!isWritable()) return;
sparta_assert(offset + size <= getNumBytes(), "Access out of bounds");
write_(buf, size, offset);
}

void poke(const void *buf, size_t size, size_t offset)
{
if (!isWritable()) return;
sparta_assert(offset + size <= getNumBytes(), "Access out of bounds");
RegisterBits val(reinterpret_cast<const uint8_t *>(buf), size);
RegisterBits mask = mask_ >> 8 * offset;
Expand All @@ -1219,6 +1241,7 @@ class RegisterBase : public TreeNode

void pokeUnmasked(const void *buf, size_t size, size_t offset)
{
if (!isWritable()) return;
sparta_assert(offset + size <= getNumBytes(), "Access out of bounds");
poke_(buf, size, offset);
}
Expand Down Expand Up @@ -1351,7 +1374,6 @@ class RegisterBase : public TreeNode

write_mask |= ((partial_mask >> shift_down) << shift_up);
}

}

return ~write_mask;
Expand Down Expand Up @@ -1534,6 +1556,7 @@ class Register : public RegisterBase
template <typename T>
inline void dmiWrite(T val, index_type idx = 0)
{
if (!isWritable()) return;
dmiWriteImpl_(&val, sizeof(val), sizeof(val) * idx);
}

Expand All @@ -1546,6 +1569,7 @@ class Register : public RegisterBase
template <typename T>
inline void writeUnmasked(T val, index_type idx = 0)
{
if (!isWritable()) return;
dmiWriteImpl_(&val, sizeof(T), idx);
}

Expand Down Expand Up @@ -1583,6 +1607,7 @@ class Register : public RegisterBase

void write_(const void *buf, size_t size, size_t offset=0) override final
{
if (!isWritable()) return;
auto &post_write_noti = getPostWriteNotificationSource();

if (SPARTA_EXPECT_FALSE(post_write_noti.observed())) {
Expand All @@ -1596,12 +1621,14 @@ class Register : public RegisterBase

void poke_(const void *buf, size_t size, size_t offset=0) override final
{
if (!isWritable()) return;
dview_.getLine()->write(
dview_.getOffset() + offset, size, static_cast<const uint8_t *>(buf));
}

void dmiWrite_(const void *buf, size_t size, size_t offset = 0) override final
{
if (!isWritable()) return;
dmiWriteImpl_(buf, size, offset);
}

Expand All @@ -1612,6 +1639,7 @@ class Register : public RegisterBase

inline void dmiWriteImpl_(const void *buf, size_t size, size_t offset = 0)
{
if (!isWritable()) return;
memcpy(raw_data_ptr_ + offset, buf, size);
dview_.getLine()->flagDirty();
}
Expand Down

0 comments on commit e736f0d

Please sign in to comment.