Skip to content

Commit

Permalink
feat!: add iunrep CSR for unreproducible machines
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jan 19, 2024
1 parent 83f72e1 commit 8c4bea3
Show file tree
Hide file tree
Showing 28 changed files with 193 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/grpc-interfaces
3 changes: 3 additions & 0 deletions src/clua-i-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ IMPL_MACHINE_OBJ_READ_WRITE(scounteren)
IMPL_MACHINE_OBJ_READ_WRITE(senvcfg)
IMPL_MACHINE_OBJ_READ_WRITE(ilrsc)
IMPL_MACHINE_OBJ_READ_WRITE(iflags)
IMPL_MACHINE_OBJ_READ_WRITE(iunrep)
IMPL_MACHINE_OBJ_READ_WRITE(htif_tohost)
IMPL_MACHINE_OBJ_READ(htif_tohost_dev)
IMPL_MACHINE_OBJ_READ(htif_tohost_cmd)
Expand Down Expand Up @@ -572,6 +573,7 @@ static const auto machine_obj_index = cartesi::clua_make_luaL_Reg_array({
{"set_iflags_X", machine_obj_index_set_iflags_X},
{"reset_iflags_Y", machine_obj_index_reset_iflags_Y},
{"reset_iflags_X", machine_obj_index_reset_iflags_X},
{"read_iunrep", machine_obj_index_read_iunrep},
{"read_ilrsc", machine_obj_index_read_ilrsc},
{"read_marchid", machine_obj_index_read_marchid},
{"read_mcause", machine_obj_index_read_mcause},
Expand Down Expand Up @@ -626,6 +628,7 @@ static const auto machine_obj_index = cartesi::clua_make_luaL_Reg_array({
{"write_uarch_pc", machine_obj_index_write_uarch_pc},
{"write_uarch_x", machine_obj_index_write_uarch_x},
{"write_iflags", machine_obj_index_write_iflags},
{"write_iunrep", machine_obj_index_write_iunrep},
{"write_ilrsc", machine_obj_index_write_ilrsc},
{"write_mcause", machine_obj_index_write_mcause},
{"write_mcounteren", machine_obj_index_write_mcounteren},
Expand Down
3 changes: 3 additions & 0 deletions src/clua-machine-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ CM_PROC_CSR clua_check_cm_proc_csr(lua_State *L, int idx) try {
{"senvcfg", CM_PROC_SENVCFG},
{"ilrsc", CM_PROC_ILRSC},
{"iflags", CM_PROC_IFLAGS},
{"iunrep", CM_PROC_IUNREP},
{"clint_mtimecmp", CM_PROC_CLINT_MTIMECMP},
{"plic_girqpend", CM_PROC_PLIC_GIRQPEND},
{"plic_girqsrvd", CM_PROC_PLIC_GIRQSRVD},
Expand Down Expand Up @@ -801,6 +802,7 @@ static void push_cm_processor_config(lua_State *L, const cm_processor_config *p)
PUSH_CM_PROCESSOR_CONFIG_CSR(senvcfg);
PUSH_CM_PROCESSOR_CONFIG_CSR(ilrsc);
PUSH_CM_PROCESSOR_CONFIG_CSR(iflags);
PUSH_CM_PROCESSOR_CONFIG_CSR(iunrep);
}

/// \brief Pushes a cm_ram_config to the Lua stack
Expand Down Expand Up @@ -1291,6 +1293,7 @@ static void check_cm_processor_config(lua_State *L, int tabidx, cm_processor_con
p->senvcfg = opt_uint_field(L, -1, "senvcfg", def->senvcfg);
p->ilrsc = opt_uint_field(L, -1, "ilrsc", def->ilrsc);
p->iflags = opt_uint_field(L, -1, "iflags", def->iflags);
p->iunrep = opt_uint_field(L, -1, "iunrep", def->iunrep);
lua_pop(L, 1);
}

Expand Down
8 changes: 8 additions & 0 deletions src/grpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,14 @@ void grpc_virtual_machine::do_write_iflags(uint64_t val) {
write_csr(csr::iflags, val);
}

uint64_t grpc_virtual_machine::do_read_iunrep(void) const {
return read_csr(csr::iunrep);
}

void grpc_virtual_machine::do_write_iunrep(uint64_t val) {
write_csr(csr::iunrep, val);
}

uint64_t grpc_virtual_machine::do_read_htif_tohost(void) const {
return read_csr(csr::htif_tohost);
}
Expand Down
2 changes: 2 additions & 0 deletions src/grpc-virtual-machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ class grpc_virtual_machine : public i_virtual_machine {
void do_reset_iflags_Y(void) override;
void do_reset_iflags_X(void) override;
void do_write_iflags(uint64_t val) override;
uint64_t do_read_iunrep(void) const override;
void do_write_iunrep(uint64_t val) override;
uint64_t do_read_htif_tohost(void) const override;
uint64_t do_read_htif_tohost_dev(void) const override;
uint64_t do_read_htif_tohost_cmd(void) const override;
Expand Down
14 changes: 14 additions & 0 deletions src/i-state-access.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ class i_state_access { // CRTP
return derived().do_write_iflags_PRV(val);
}

/// \brief Reads CSR iunrep.
/// \returns Register value.
/// \details This is Cartesi-specific.
uint64_t read_iunrep(void) {
return derived().do_read_iunrep();
}

/// \brief Writes CSR iunrep.
/// \param val New register value.
/// \details This is Cartesi-specific.
void write_iunrep(uint64_t val) {
return derived().do_write_iunrep(val);
}

/// \brief Reads CLINT's mtimecmp.
/// \returns Register value.
uint64_t read_clint_mtimecmp(void) {
Expand Down
12 changes: 12 additions & 0 deletions src/i-virtual-machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,16 @@ class i_virtual_machine {
return do_write_iflags(val);
}

/// \brief Reads the iunrep register
uint64_t read_iunrep(void) const {
return do_read_iunrep();
}

/// \brief Writes the iunrep register
void write_iunrep(uint64_t val) {
do_write_iunrep(val);
}

/// \brief Reads htif's tohost
uint64_t read_htif_tohost(void) const {
return do_read_htif_tohost();
Expand Down Expand Up @@ -747,6 +757,8 @@ class i_virtual_machine {
virtual void do_reset_iflags_Y(void) = 0;
virtual void do_reset_iflags_X(void) = 0;
virtual void do_write_iflags(uint64_t val) = 0;
virtual uint64_t do_read_iunrep(void) const = 0;
virtual void do_write_iunrep(uint64_t val) = 0;
virtual uint64_t do_read_htif_tohost(void) const = 0;
virtual uint64_t do_read_htif_tohost_dev(void) const = 0;
virtual uint64_t do_read_htif_tohost_cmd(void) const = 0;
Expand Down
6 changes: 5 additions & 1 deletion src/json-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static auto csr_from_name(const std::string &name) {
{"senvcfg", csr::senvcfg},
{"ilrsc", csr::ilrsc},
{"iflags", csr::iflags},
{"iunrep", csr::iunrep},
{"clint_mtimecmp", csr::clint_mtimecmp},
{"plic_girqpend", csr::plic_girqpend},
{"plic_girqsrvd", csr::plic_girqsrvd},
Expand Down Expand Up @@ -165,6 +166,8 @@ static auto csr_to_name(machine::csr reg) {
return "ilrsc";
case csr::iflags:
return "iflags";
case csr::iunrep:
return "iunrep";
case csr::clint_mtimecmp:
return "clint_mtimecmp";
case csr::plic_girqpend:
Expand Down Expand Up @@ -837,6 +840,7 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, processor_config &v
ju_get_opt_field(jconfig, "senvcfg"s, value.senvcfg, new_path);
ju_get_opt_field(jconfig, "ilrsc"s, value.ilrsc, new_path);
ju_get_opt_field(jconfig, "iflags"s, value.iflags, new_path);
ju_get_opt_field(jconfig, "iunrep"s, value.iunrep, new_path);
}

template void ju_get_opt_field<uint64_t>(const nlohmann::json &j, const uint64_t &key, processor_config &value,
Expand Down Expand Up @@ -1245,7 +1249,7 @@ void to_json(nlohmann::json &j, const processor_config &config) {
{"medeleg", config.medeleg}, {"mideleg", config.mideleg}, {"mcounteren", config.mcounteren},
{"menvcfg", config.menvcfg}, {"stvec", config.stvec}, {"sscratch", config.sscratch}, {"sepc", config.sepc},
{"scause", config.scause}, {"stval", config.stval}, {"satp", config.satp}, {"scounteren", config.scounteren},
{"senvcfg", config.senvcfg}, {"ilrsc", config.ilrsc}, {"iflags", config.iflags}};
{"senvcfg", config.senvcfg}, {"ilrsc", config.ilrsc}, {"iflags", config.iflags}, {"iunrep", config.iunrep}};
}

void to_json(nlohmann::json &j, const flash_drive_configs &fs) {
Expand Down
4 changes: 4 additions & 0 deletions src/jsonrpc-discover.json
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@
"senvcfg",
"ilrsc",
"iflags",
"iunrep",
"clint_mtimecmp",
"plic_girqpend",
"plic_girqsrvd",
Expand Down Expand Up @@ -1351,6 +1352,9 @@
},
"iflags": {
"$ref": "#/components/schemas/UnsignedInteger"
},
"iunrep": {
"$ref": "#/components/schemas/UnsignedInteger"
}
}
},
Expand Down
8 changes: 8 additions & 0 deletions src/jsonrpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,14 @@ void jsonrpc_virtual_machine::do_reset_iflags_X(void) {
jsonrpc_request(m_mgr->get_mgr(), m_mgr->get_remote_address(), "machine.reset_iflags_X", std::tie(), result);
}

uint64_t jsonrpc_virtual_machine::do_read_iunrep(void) const {
return read_csr(csr::iunrep);
}

void jsonrpc_virtual_machine::do_write_iunrep(uint64_t val) {
write_csr(csr::iunrep, val);
}

bool jsonrpc_virtual_machine::do_read_uarch_halt_flag(void) const {
bool result = false;
jsonrpc_request(m_mgr->get_mgr(), m_mgr->get_remote_address(), "machine.read_uarch_halt_flag", std::tie(), result);
Expand Down
2 changes: 2 additions & 0 deletions src/jsonrpc-virtual-machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class jsonrpc_virtual_machine final : public i_virtual_machine {
void do_set_iflags_X(void) override;
void do_reset_iflags_Y(void) override;
void do_reset_iflags_X(void) override;
uint64_t do_read_iunrep(void) const override;
void do_write_iunrep(uint64_t val) override;
bool do_read_uarch_halt_flag(void) const override;
void do_set_uarch_halt_flag(void) override;
void do_reset_uarch(void) override;
Expand Down
1 change: 1 addition & 0 deletions src/machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ IMPL_MACHINE_READ_WRITE(scounteren)
IMPL_MACHINE_READ_WRITE(senvcfg)
IMPL_MACHINE_READ_WRITE(ilrsc)
IMPL_MACHINE_READ_WRITE(iflags)
IMPL_MACHINE_READ_WRITE(iunrep)
IMPL_MACHINE_READ_WRITE(htif_tohost)
IMPL_MACHINE_READ(htif_tohost_dev)
IMPL_MACHINE_READ(htif_tohost_cmd)
Expand Down
22 changes: 22 additions & 0 deletions src/machine-c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ typedef enum { // NOLINT(modernize-use-using)
CM_PROC_SENVCFG,
CM_PROC_ILRSC,
CM_PROC_IFLAGS,
CM_PROC_IUNREP,
CM_PROC_CLINT_MTIMECMP,
CM_PROC_PLIC_GIRQPEND,
CM_PROC_PLIC_GIRQSRVD,
Expand Down Expand Up @@ -183,6 +184,7 @@ typedef struct { // NOLINT(modernize-use-using)
uint64_t senvcfg; ///< Value of senvcfg CSR
uint64_t ilrsc; ///< Value of ilrsc CSR
uint64_t iflags; ///< Value of iflags CSR
uint64_t iunrep; ///< Value of iunrep CSR
} cm_processor_config;

/// \brief RAM state configuration
Expand Down Expand Up @@ -1366,6 +1368,26 @@ CM_API uint64_t cm_packed_iflags(int PRV, int X, int Y, int H);
/// \returns 0 for success, non zero code for error
CM_API int cm_write_iflags(cm_machine *m, uint64_t val, char **err_msg);

/// \brief Reads the value of the iunrep register.
/// \param m Pointer to valid machine instance
/// \param val Receives value of the register.
/// \param err_msg Receives the error message if function execution fails
/// or NULL in case of successful function execution. In case of failure error_msg
/// must be deleted by the function caller using cm_delete_cstring.
/// err_msg can be NULL, meaning the error message won't be received.
/// \returns 0 for success, non zero code for error
CM_API int cm_read_iunrep(const cm_machine *m, uint64_t *val, char **err_msg);

/// \brief Writes the value of the iunrep register.
/// \param m Pointer to valid machine instance
/// \param val New register value.
/// \param err_msg Receives the error message if function execution fails
/// or NULL in case of successful function execution. In case of failure error_msg
/// must be deleted by the function caller using cm_delete_cstring.
/// err_msg can be NULL, meaning the error message won't be received.
/// \returns 0 for success, non zero code for error
CM_API int cm_write_iunrep(cm_machine *m, uint64_t val, char **err_msg);

/// \brief Reads the value of HTIF's tohost register.
/// \param m Pointer to valid machine instance
/// \param val Receives value of the register.
Expand Down
1 change: 1 addition & 0 deletions src/machine-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct processor_config final {
uint64_t senvcfg{SENVCFG_INIT}; ///< Value of senvcfg CSR
uint64_t ilrsc{ILRSC_INIT}; ///< Value of ilrsc CSR
uint64_t iflags{IFLAGS_INIT}; ///< Value of iflags CSR
uint64_t iunrep{IUNREP_INIT}; ///< Value of iunrep CSR
};

/// \brief RAM state configuration
Expand Down
3 changes: 2 additions & 1 deletion src/machine-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ struct machine_state {
uint64_t senvcfg; ///< CSR senvcfg.

// Cartesi-specific state
uint64_t ilrsc; ///< Cartesi-specific CSR ilrsc (For LR/SC instructions).
uint64_t ilrsc; ///< Cartesi-specific CSR ilrsc (For LR/SC instructions).
uint64_t iunrep; ///< Cartesi-specific CSR iunrep

unpacked_iflags iflags; ///< Cartesi-specific unpacked CSR iflags.

Expand Down
Loading

0 comments on commit 8c4bea3

Please sign in to comment.