Skip to content

Commit

Permalink
Remove ropages memory area
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Oct 26, 2023
1 parent cc89f63 commit c5b6b0f
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 114 deletions.
76 changes: 1 addition & 75 deletions lib/libriscv/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ namespace riscv
Memory<W>::~Memory()
{
this->clear_all_pages();
// only the original machine owns rodata range
if (!this->m_original_machine) {
m_ropages.pages.release();
}
// only the original machine owns arena
if (this->m_arena != nullptr) {
#ifdef __linux__
munmap(this->m_arena, this->m_arena_pages * Page::size());
Expand Down Expand Up @@ -184,15 +181,6 @@ namespace riscv
}
}

if constexpr (!riscv::binary_translation_enabled) {
if (attr.read && !attr.write && m_ropages.end == 0) {
// If the serialization fails, we will fallback to memcpy
// with set_page_attr, like normal.
if (serialize_pages(m_ropages, hdr->p_vaddr, src, len, attr))
return;
}
}

// Load into virtual memory
this->memcpy(hdr->p_vaddr, src, len);

Expand Down Expand Up @@ -235,62 +223,6 @@ namespace riscv
machine().cpu.set_execute_segment(&exec_segment);
}

template <int W> RISCV_INTERNAL
bool Memory<W>::serialize_pages(MemoryArea& area,
address_t addr, const char* src, size_t size, PageAttributes attr)
{
static constexpr address_t PSIZEMASK = Page::size()-1;
// It is not an optimization to store 1-2 pages
if (size < 2*Page::size()) {
area.pages = nullptr;
area.begin = 0;
area.end = 0;
return false;
}

const address_t prebase = addr & ~PSIZEMASK;
const address_t prelen = addr - prebase;
const address_t postbase = (addr + size) & ~PSIZEMASK;
const address_t lastpage_len = (addr + size) - postbase;
const address_t postlen = (lastpage_len > 0) ? Page::size() - lastpage_len : 0;
// The total length should be a page-sized length
const address_t total_len = prelen + size + postlen;
assert((total_len & ~PSIZEMASK) == total_len);

// Create the first and last page
// TODO: Make this a PageData struct for alignment guarantees
auto* pagedata = new uint8_t[Page::size() * 2] {};
// Fill in the first page (at page 0)
std::memset(&pagedata[0], 0, prelen);
std::memcpy(&pagedata[prelen], src, Page::size() - prelen);
// Fill in the last page (at page 1)
std::memset(&pagedata[Page::size() + lastpage_len], 0, postlen);
std::memcpy(&pagedata[Page::size()], &src[postbase - addr], lastpage_len);
area.data.reset(pagedata);

const size_t npages = total_len / Page::size();
area.pages.reset(new Page[npages]);
// Create share-able range
area.begin = addr / Page::size();
area.end = area.begin + npages;

for (size_t i = 0; i < npages; i++) {
area.pages[i].attr = attr;
// None of the pages own their page memory
area.pages[i].attr.non_owning = true;

// We have custom page data for the first and last page
if (i == 0 || i == npages-1) {
const size_t offset = (i == 0) ? 0 : Page::size();
area.pages[i].m_page.reset((PageData*) &pagedata[offset]);
} else {
const size_t offset = i * Page::size() - prelen;
area.pages[i].m_page.reset((PageData*) &src[offset]);
}
}
return true;
}

// ELF32 and ELF64 loader
template <int W> RISCV_INTERNAL
void Memory<W>::binary_loader(const MachineOptions<W>& options)
Expand Down Expand Up @@ -447,12 +379,6 @@ namespace riscv
this->m_heap_address = master.memory.m_heap_address;
this->m_mmap_address = master.memory.m_mmap_address;

// TODO: Set callback that can loan execute segments from master

this->m_ropages.begin = master.memory.m_ropages.begin;
this->m_ropages.end = master.memory.m_ropages.end;
this->m_ropages.pages.reset(master.memory.m_ropages.pages.get());

// invalidate all cached pages, because references are invalidated
this->invalidate_reset_cache();
}
Expand Down
13 changes: 0 additions & 13 deletions lib/libriscv/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,6 @@ namespace riscv
Memory(Machine<W>&, const Machine<W>&, MachineOptions<W>);
~Memory();
private:
struct MemoryArea {
address_t begin = 0;
address_t end = 0;
std::unique_ptr<Page[]> pages = nullptr;
std::unique_ptr<uint8_t[]> data = nullptr;
bool contains(address_t pg) const noexcept { return pg >= begin && pg < end; }
bool contains(address_t x1, address_t x2) const noexcept {
return x1 < end && x2 >= begin;
}
};
void clear_all_pages();
void initial_paging();
[[noreturn]] static void protection_fault(address_t);
Expand Down Expand Up @@ -231,7 +221,6 @@ namespace riscv
void binary_loader(const MachineOptions<W>&);
void binary_load_ph(const MachineOptions<W>&, const Phdr*);
void serialize_execute_segment(const MachineOptions<W>&, const Phdr*);
bool serialize_pages(MemoryArea&, address_t, const char*, size_t, PageAttributes);
void generate_decoder_cache(const MachineOptions<W>&, DecodedExecuteSegment<W>&);
// Machine copy-on-write fork
void machine_loader(const Machine<W>&, const MachineOptions<W>&);
Expand All @@ -251,8 +240,6 @@ namespace riscv
page_write_cb_t m_page_write_handler = default_page_write;
page_readf_cb_t m_page_readf_handler = default_page_read;

MemoryArea m_ropages;

address_t m_start_address = 0;
address_t m_stack_address = 0;
address_t m_exit_address = 0;
Expand Down
7 changes: 0 additions & 7 deletions lib/libriscv/memory_helpers_paging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
template <int W> inline
void Memory<W>::memzero(address_t dst, size_t len)
{
// Check if we are trying to memzero the custom RO area
const address_t p_begin = page_number(dst);
const address_t p_end = page_number(dst + len);
if (UNLIKELY(m_ropages.contains(p_begin, p_end))) {
this->protection_fault(dst);
}

while (len > 0)
{
const size_t offset = dst & (Page::size()-1); // offset within page
Expand Down
12 changes: 0 additions & 12 deletions lib/libriscv/memory_inline_pages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ inline const Page& Memory<W>::get_page(const address_t address) const
template <int W>
inline const Page& Memory<W>::get_exec_pageno(const address_t pageno) const
{
if constexpr (!riscv::binary_translation_enabled) {
if (m_ropages.contains(pageno)) {
return m_ropages.pages[pageno - m_ropages.begin];
}
}

auto it = m_pages.find(pageno);
if (LIKELY(it != m_pages.end())) {
return it->second;
Expand All @@ -62,12 +56,6 @@ inline const Page& Memory<W>::get_pageno(const address_t pageno) const
return it->second;
}

if constexpr (!riscv::binary_translation_enabled) {
if (m_ropages.contains(pageno)) {
return m_ropages.pages[pageno - m_ropages.begin];
}
}

return m_page_readf_handler(*this, pageno);
}

Expand Down
7 changes: 0 additions & 7 deletions lib/libriscv/memory_rw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ namespace riscv
return page;
}
} else {

if constexpr (!riscv::binary_translation_enabled) {
if (UNLIKELY(m_ropages.contains(pageno))) {
this->protection_fault(pageno * Page::size());
}
}

// Handler must produce a new page, or throw
Page& page = m_page_fault_handler(*this, pageno, init);
if (LIKELY(page.attr.write)) {
Expand Down

0 comments on commit c5b6b0f

Please sign in to comment.