#ifndef _MEMORY_WITH_HTIF_ #define _MEMORY_WITH_HTIF_ #include "iss/arch/riscv_hart_common.h" #include "iss/vm_types.h" #include "memory_if.h" #include #include namespace iss { namespace mmio { template struct memory_with_htif : public memory_elem { using this_class = memory_with_htif; constexpr static unsigned WORD_LEN = sizeof(WORD_TYPE) * 8; memory_with_htif(arch::priv_if hart_if) : hart_if(hart_if) {} ~memory_with_htif() = default; memory_if get_mem_if() override { return memory_if{.rd_mem{util::delegate::from(this)}, .wr_mem{util::delegate::from(this)}}; } void set_next(memory_if) override { // intenrionally left empty, leaf element } private: iss::status read_mem(iss::access_type access, uint64_t addr, unsigned length, uint8_t* data) { for(auto offs = 0U; offs < length; ++offs) { *(data + offs) = mem[(addr + offs) % mem.size()]; } return iss::Ok; } iss::status write_mem(iss::access_type access, uint64_t addr, unsigned length, uint8_t const* data) { mem_type::page_type& p = mem(addr / mem.page_size); std::copy(data, data + length, p.data() + (addr & mem.page_addr_mask)); // this->tohost handling in case of riscv-test // according to https://github.com/riscv-software-src/riscv-isa-sim/issues/364#issuecomment-607657754: if(access && iss::access_type::FUNC) { if(addr == hart_if.tohost) { return hart_if.exec_htif(data); } if((WORD_LEN == 32 && addr == hart_if.fromhost + 4) || (WORD_LEN == 64 && addr == hart_if.fromhost)) { uint64_t fhostvar = *reinterpret_cast(p.data() + (hart_if.fromhost & mem.page_addr_mask)); *reinterpret_cast(p.data() + (hart_if.tohost & mem.page_addr_mask)) = fhostvar; } } return iss::Ok; } protected: using mem_type = util::sparse_array; mem_type mem; arch::priv_if hart_if; }; } // namespace mmio } // namespace iss #endif // _MEMORY_WITH_HTIF_