Skip to content

Commit

Permalink
refactor: split PMA and AR constants
Browse files Browse the repository at this point in the history
PMA is only used for the two-word description of address ranges in memory
All other constants use AR (for address range)
Also simplified machine::read_memory()
  • Loading branch information
diegonehab committed Mar 2, 2025
1 parent a55cbef commit 7d0f77a
Show file tree
Hide file tree
Showing 82 changed files with 884 additions and 848 deletions.
24 changes: 12 additions & 12 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,19 @@ DEFS+=-DJSON_HAS_FILESYSTEM=0
DEFS+=-DBOOST_ASIO_DISABLE_THREADS -DBOOST_ASIO_DISABLE_EPOLL -DBOOST_ASIO_DISABLE_EVENTFD

ifeq ($(dump),yes)
DUMP_DEFS+=-DDUMP_ILLEGAL_INSN_EXCEPTIONS
DUMP_DEFS+=-DDUMP_EXCEPTIONS
DUMP_DEFS+=-DDUMP_INTERRUPTS
DUMP_DEFS+=-DDUMP_MMU_EXCEPTIONS
DUMP_DEFS+=-DDUMP_INVALID_CSR
DUMP_DEFS+=-DDUMP_REGS
DUMP_DEFS+=-DDUMP_INSN_HIST
DUMP_DEFS+=-DDUMP_STATS
#DUMP_DEFS+=-DDUMP_ILLEGAL_INSN_EXCEPTIONS
#DUMP_DEFS+=-DDUMP_EXCEPTIONS
#DUMP_DEFS+=-DDUMP_INTERRUPTS
#DUMP_DEFS+=-DDUMP_MMU_EXCEPTIONS
#DUMP_DEFS+=-DDUMP_INVALID_CSR
#DUMP_DEFS+=-DDUMP_REGS
#DUMP_DEFS+=-DDUMP_INSN_HIST
#DUMP_DEFS+=-DDUMP_STATS
DUMP_DEFS+=-DDUMP_INSN
DUMP_DEFS+=-DDUMP_UARCH_INSN
DUMP_DEFS+=-DDUMP_SCOPED_NOTE
DUMP_DEFS+=-DDUMP_STATE_ACCESS
DUMP_DEFS+=-DDUMP_UARCH_STATE_ACCESS
#DUMP_DEFS+=-DDUMP_UARCH_INSN
#DUMP_DEFS+=-DDUMP_SCOPED_NOTE
#DUMP_DEFS+=-DDUMP_STATE_ACCESS
#DUMP_DEFS+=-DDUMP_UARCH_STATE_ACCESS
endif
DEFS += $(DUMP_DEFS)
# Pass down UARCH_DEFS to sub-makefiles
Expand Down
77 changes: 77 additions & 0 deletions src/address-range-constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
//

#ifndef ADDRESS_RANGE_CONSTANTS_H
#define ADDRESS_RANGE_CONSTANTS_H

#include <cstdint>

#include "address-range-defines.h"

namespace cartesi {

/// \brief Fixed address ranges.
enum AR_ranges : uint64_t {
AR_SHADOW_STATE_START = EXPAND_UINT64_C(AR_SHADOW_STATE_START_DEF), ///< Start of shadow state range
AR_SHADOW_STATE_LENGTH = EXPAND_UINT64_C(AR_SHADOW_STATE_LENGTH_DEF), ///< Length of shadow state range
AR_PMAS_START = EXPAND_UINT64_C(AR_PMAS_START_DEF), ///< Start of PMAS list range
AR_PMAS_LENGTH = EXPAND_UINT64_C(AR_PMAS_LENGTH_DEF), ///< Length of PMAS list range
AR_DTB_START = EXPAND_UINT64_C(AR_DTB_START_DEF), ///< Start of DTB range
AR_DTB_LENGTH = EXPAND_UINT64_C(AR_DTB_LENGTH_DEF), ///< Length of DTB range
AR_SHADOW_TLB_START = EXPAND_UINT64_C(AR_SHADOW_TLB_START_DEF), ///< Start of shadow TLB range
AR_SHADOW_TLB_LENGTH = EXPAND_UINT64_C(AR_SHADOW_TLB_LENGTH_DEF), ///< Length of shadow TLB range
AR_SHADOW_UARCH_STATE_START =
EXPAND_UINT64_C(AR_SHADOW_UARCH_STATE_START_DEF), ///< Start of uarch shadow state range
AR_SHADOW_UARCH_STATE_LENGTH =
EXPAND_UINT64_C(AR_SHADOW_UARCH_STATE_LENGTH_DEF), ///< Length of uarch shadow state range
AR_CLINT_START = EXPAND_UINT64_C(AR_CLINT_START_DEF), ///< Start of CLINT range
AR_CLINT_LENGTH = EXPAND_UINT64_C(AR_CLINT_LENGTH_DEF), ///< Length of CLINT range
AR_PLIC_START = EXPAND_UINT64_C(AR_PLIC_START_DEF), ///< Start of PLIC range
AR_PLIC_LENGTH = EXPAND_UINT64_C(AR_PLIC_LENGTH_DEF), ///< Length of PLIC range
AR_HTIF_START = EXPAND_UINT64_C(AR_HTIF_START_DEF), ///< Start of HTIF range
AR_HTIF_LENGTH = EXPAND_UINT64_C(AR_HTIF_LENGTH_DEF), ///< Length of HTIF range
AR_UARCH_RAM_START = EXPAND_UINT64_C(AR_UARCH_RAM_START_DEF), ///< Start of uarch RAM range
AR_UARCH_RAM_LENGTH = EXPAND_UINT64_C(AR_UARCH_RAM_LENGTH_DEF), ///< Length of uarch RAM range
AR_CMIO_RX_BUFFER_START = EXPAND_UINT64_C(AR_CMIO_RX_BUFFER_START_DEF), ///< Start of CMIO RX buffer range
AR_CMIO_RX_BUFFER_LOG2_SIZE = EXPAND_UINT64_C(AR_CMIO_RX_BUFFER_LOG2_SIZE_DEF), ///< Log2 of CMIO RX buffer range
AR_CMIO_RX_BUFFER_LENGTH = (UINT64_C(1) << AR_CMIO_RX_BUFFER_LOG2_SIZE_DEF), ///< Length of CMIO RX buffer range
AR_CMIO_TX_BUFFER_START = EXPAND_UINT64_C(AR_CMIO_TX_BUFFER_START_DEF), ///< Start of CMIO TX buffer range
AR_CMIO_TX_BUFFER_LOG2_SIZE = EXPAND_UINT64_C(AR_CMIO_TX_BUFFER_LOG2_SIZE_DEF), ///< Log2 of CMIO TX buffer range
AR_CMIO_TX_BUFFER_LENGTH = (UINT64_C(1) << AR_CMIO_TX_BUFFER_LOG2_SIZE_DEF), ///< Length of CMIO TX buffer range
AR_DRIVE_START = EXPAND_UINT64_C(AR_DRIVE_START_DEF), ///< Start address for flash drive ranges
AR_DRIVE_OFFSET = EXPAND_UINT64_C(AR_DRIVE_OFFSET_DEF), ///< Offset for extra flash drive ranges

AR_FIRST_VIRTIO_START = EXPAND_UINT64_C(AR_FIRST_VIRTIO_START_DEF), ///< Start of first VIRTIO range
AR_VIRTIO_LENGTH = EXPAND_UINT64_C(AR_VIRTIO_LENGTH_DEF), ///< Length of each VIRTIO range
AR_LAST_VIRTIO_END = EXPAND_UINT64_C(AR_LAST_VIRTIO_END_DEF), ///< End of last VIRTIO range

AR_RAM_START = EXPAND_UINT64_C(AR_RAM_START_DEF), ///< Start of RAM range
};

/// \brief PMA constants.
enum AR_constants : uint64_t {
AR_PAGE_SIZE_LOG2 = EXPAND_UINT64_C(AR_PAGE_SIZE_LOG2_DEF), ///< Log<sub>2</sub> of physical memory page size.
AR_PAGE_SIZE = (UINT64_C(1) << AR_PAGE_SIZE_LOG2_DEF), ///< Physical memory page size.
};

/// \brief PMA masks.
enum AR_masks : uint64_t {
AR_ADDRESSABLE_MASK = ((UINT64_C(1) << 56) - 1) ///< Mask for addressable ranges.
};

} // namespace cartesi

#endif
58 changes: 58 additions & 0 deletions src/address-range-defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
//

#ifndef AR_DEFINES_H
#define AR_DEFINES_H

// NOLINTBEGIN(cppcoreguidelines-macro-usage,cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
#define AR_SHADOW_STATE_START_DEF 0x0 ///< Shadow start address
#define AR_SHADOW_STATE_LENGTH_DEF 0x1000 ///< Shadow length in bytes
#define AR_PMAS_START_DEF 0x10000 ///< PMA Array start address
#define AR_PMAS_LENGTH_DEF 0x1000 ///< PMA Array length in bytes
#define AR_SHADOW_TLB_START_DEF 0x20000 ///< TLB start address
#define AR_SHADOW_TLB_LENGTH_DEF 0x6000 ///< TLB length in bytes
#define AR_SHADOW_UARCH_STATE_START_DEF 0x400000 ///< microarchitecture shadow state start address
#define AR_SHADOW_UARCH_STATE_LENGTH_DEF 0x1000 ///< microarchitecture shadow state length
#define AR_UARCH_RAM_START_DEF 0x600000 ///< microarchitecture RAM start address
#define AR_UARCH_RAM_LENGTH_DEF 0x200000 ///< microarchitecture RAM length
#define AR_CLINT_START_DEF 0x2000000 ///< CLINT start address
#define AR_CLINT_LENGTH_DEF 0xC0000 ///< CLINT length in bytes
#define AR_PLIC_START_DEF 0x40100000 ///< Start of PLIC range
#define AR_PLIC_LENGTH_DEF 0x00400000 ///< Length of PLIC range
#define AR_HTIF_START_DEF 0x40008000 ///< HTIF base address (to_host)
#define AR_HTIF_LENGTH_DEF 0x1000 ///< HTIF length in bytes
#define AR_FIRST_VIRTIO_START_DEF 0x40010000 ///< Start of first VIRTIO range
#define AR_VIRTIO_LENGTH_DEF 0x1000 ///< Length of each VIRTIO range
#define AR_LAST_VIRTIO_END_DEF 0x40020000 ///< End of last VIRTIO range
#define AR_DTB_START_DEF 0x7ff00000 ///< DTB start address
#define AR_DTB_LENGTH_DEF 0x100000 ///< DTB length in bytes
#define AR_CMIO_RX_BUFFER_START_DEF 0x60000000 ///< CMIO RX buffer start address
#define AR_CMIO_RX_BUFFER_LOG2_SIZE_DEF 21 ///< log<sub>2</sub> of CMIO RX buffer length in bytes
#define AR_CMIO_TX_BUFFER_START_DEF 0x60800000 ///< CMIO TX buffer start address
#define AR_CMIO_TX_BUFFER_LOG2_SIZE_DEF 21 ///< log<sub>2</sub> of CMIO TX buffer length in bytes
#define AR_DRIVE_START_DEF 0x80000000000000 ///< Start PMA address for flash drives
#define AR_DRIVE_OFFSET_DEF 0x10000000000000 ///< PMA offset for extra flash drives

#define AR_RAM_START_DEF 0x80000000 ///< RAM start address

#define AR_PAGE_SIZE_LOG2_DEF 12 ///< log<sub>2</sub> of physical memory page size.

// helper for using UINT64_C with defines
#ifndef EXPAND_UINT64_C
#define EXPAND_UINT64_C(a) UINT64_C(a)
#endif
// NOLINTEND(cppcoreguidelines-macro-usage,cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
#endif /* end of include guard: AR_DEFINES_H */
22 changes: 11 additions & 11 deletions src/address-range.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "assert-printf.h"
#include "i-device-state-access.h"
#include "interpret.h"
#include "pma.h"
#include "pmas.h"

namespace cartesi {

Expand All @@ -44,7 +44,7 @@ class address_range {
uint64_t m_start; ///< Target physical address where range starts.
uint64_t m_length; ///< Length of range, in bytes.
uint64_t m_length_bit_ceil; ///< Smallest power of 2 that is not smaller than length, in bytes.
pma_flags m_flags; ///< Physical memory attribute flags for range.
pmas_flags m_flags; ///< Physical memory attribute flags for range.

public:
/// \brief Noexcept constexpr constructor for empty ranges with description
Expand Down Expand Up @@ -84,7 +84,7 @@ class address_range {
/// \param length Length of range, in bytes
/// \param f Phyical memory attribute flags for range
template <typename ABRT>
address_range(const char *description, uint64_t start, uint64_t length, const pma_flags &flags, ABRT abrt) :
address_range(const char *description, uint64_t start, uint64_t length, const pmas_flags &flags, ABRT abrt) :
m_description{},
m_start{start},
m_length{length},
Expand All @@ -98,12 +98,12 @@ class address_range {
m_description[i] = description[i];
}
// All address ranges must be page-aligned
if ((m_length & ~PMA_ISTART_START_MASK) != 0) {
if ((m_length & ~AR_ISTART_START_MASK) != 0) {
ABRTF(abrt, "length must be multiple of page size when initializing %s", m_description);
}
if ((m_start & ~PMA_ISTART_START_MASK) != 0) {
if ((m_start & ~AR_ISTART_START_MASK) != 0) {
ABRTF(abrt, "start of %s (0x%" PRIx64 ") must be aligned to page boundary of %d bytes", m_description,
start, PMA_PAGE_SIZE);
start, AR_PAGE_SIZE);
}
// It must be possible to round length up to the next power of two
if (m_length_bit_ceil == 0) {
Expand Down Expand Up @@ -148,7 +148,7 @@ class address_range {

/// \brief Returns PMA flags used during construction
/// \returns Flags
const pma_flags &get_flags() const noexcept {
const pmas_flags &get_flags() const noexcept {
return m_flags;
}

Expand Down Expand Up @@ -233,7 +233,7 @@ class address_range {
/// \brief Returns packed address range istart field as per whitepaper
/// \returns Packed address range istart
uint64_t get_istart() const noexcept {
return pma_pack_istart(m_flags, m_start);
return pmas_pack_istart(m_flags, m_start);
}

/// \brief Returns encoded addres range ilength field as per whitepaper
Expand Down Expand Up @@ -306,9 +306,9 @@ class address_range {
/// \param offset Start of range of interest, relative to start of this range
/// \param length Length of range of interest, in bytes
void mark_dirty_pages(uint64_t offset, uint64_t length) noexcept {
auto offset_aligned = offset &= ~(PMA_PAGE_SIZE - 1);
auto offset_aligned = offset &= ~(AR_PAGE_SIZE - 1);
const auto length_aligned = length + (offset - offset_aligned);
for (; offset_aligned < length_aligned; offset_aligned += PMA_PAGE_SIZE) {
for (; offset_aligned < length_aligned; offset_aligned += AR_PAGE_SIZE) {
mark_dirty_page(offset_aligned);
}
}
Expand Down Expand Up @@ -383,7 +383,7 @@ constexpr static auto make_empty_address_range(const char (&description)[N]) {
}

template <typename ABRT>
static inline auto make_address_range(const char *description, uint64_t start, uint64_t length, pma_flags f,
static inline auto make_address_range(const char *description, uint64_t start, uint64_t length, pmas_flags f,
ABRT abrt) {
return address_range{description, start, length, f, abrt};
}
Expand Down
4 changes: 2 additions & 2 deletions src/clint-address-range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "i-device-state-access.h"
#include "interpret.h"
#include "pma-constants.h"
#include "pmas-constants.h"
#include "riscv-constants.h"
#include "rtc.h"

Expand All @@ -43,7 +43,7 @@ static constexpr auto clint_mtime_rel_addr = static_cast<uint64_t>(clint_csr::mt
constexpr auto clint_mtimecmp_rel_addr = static_cast<uint64_t>(clint_csr::mtimecmp);

static constexpr uint64_t base(uint64_t v) {
return v - (v % PMA_PAGE_SIZE);
return v - (v % AR_PAGE_SIZE);
}

static bool clint_read_msip(i_device_state_access *a, uint64_t *val, int log2_size) {
Expand Down
2 changes: 1 addition & 1 deletion src/clint-address-range.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace cartesi {

class clint_address_range final : public pristine_address_range {

static constexpr pma_flags m_clint_flags{
static constexpr pmas_flags m_clint_flags{
.M = false,
.IO = true,
.E = false,
Expand Down
10 changes: 5 additions & 5 deletions src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ CM_API int luaopen_cartesi(lua_State *L) {
clua_setintegerfield(L, CM_CMIO_YIELD_MANUAL_REASON_TX_EXCEPTION, "CMIO_YIELD_MANUAL_REASON_TX_EXCEPTION", -1);
clua_setintegerfield(L, CM_CMIO_YIELD_REASON_ADVANCE_STATE, "CMIO_YIELD_REASON_ADVANCE_STATE", -1);
clua_setintegerfield(L, CM_CMIO_YIELD_REASON_INSPECT_STATE, "CMIO_YIELD_REASON_INSPECT_STATE", -1);
clua_setintegerfield(L, CM_PMA_CMIO_RX_BUFFER_START, "PMA_CMIO_RX_BUFFER_START", -1);
clua_setintegerfield(L, CM_PMA_CMIO_RX_BUFFER_LOG2_SIZE, "PMA_CMIO_RX_BUFFER_LOG2_SIZE", -1);
clua_setintegerfield(L, CM_PMA_CMIO_TX_BUFFER_START, "PMA_CMIO_TX_BUFFER_START", -1);
clua_setintegerfield(L, CM_PMA_CMIO_TX_BUFFER_LOG2_SIZE, "PMA_CMIO_TX_BUFFER_LOG2_SIZE", -1);
clua_setintegerfield(L, CM_PMA_RAM_START, "PMA_RAM_START", -1);
clua_setintegerfield(L, CM_AR_CMIO_RX_BUFFER_START, "AR_CMIO_RX_BUFFER_START", -1);
clua_setintegerfield(L, CM_AR_CMIO_RX_BUFFER_LOG2_SIZE, "AR_CMIO_RX_BUFFER_LOG2_SIZE", -1);
clua_setintegerfield(L, CM_AR_CMIO_TX_BUFFER_START, "AR_CMIO_TX_BUFFER_START", -1);
clua_setintegerfield(L, CM_AR_CMIO_TX_BUFFER_LOG2_SIZE, "AR_CMIO_TX_BUFFER_LOG2_SIZE", -1);
clua_setintegerfield(L, CM_AR_RAM_START, "AR_RAM_START", -1);
// Set other constants used by internal tests
clua_setintegerfield(L, UARCH_STATE_START_ADDRESS, "UARCH_STATE_START_ADDRESS", -1);
clua_setintegerfield(L, UARCH_STATE_LOG2_SIZE, "UARCH_STATE_LOG2_SIZE", -1);
Expand Down
Loading

0 comments on commit 7d0f77a

Please sign in to comment.