Skip to content

Commit

Permalink
SV32 translation test with basic PageTableWalk (#20)
Browse files Browse the repository at this point in the history
This PR introduces a basic implementation of the SV32 page table walk,
following a test-driven approach. A test was written to validate the
functionality, which led to defining the `sv32PageTableWalk` method for
address translation. The current implementation of `sv32PageTableWalk`
checks the validity of PTEs but does not include comprehensive checks
for other conditions. This sets up a foundation for further enhancement
in the page table walk process, with future iterations expected to
include more advanced validations.

---------

Co-authored-by: Kathlene Magnus <[email protected]>
  • Loading branch information
skhanx86 and kathlenemagnus authored Dec 13, 2024
1 parent 7fbc085 commit 98ca826
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 301 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_library(atlascore
Execute.cpp
AtlasExtractor.cpp
AtlasInst.cpp
PageTableWalker.cpp
observers/Observer.cpp
observers/CoSimObserver.cpp
observers/InstructionLogger.cpp
Expand Down
119 changes: 103 additions & 16 deletions core/PageTable.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <map>
#include <iostream>
#include "../include/AtlasTypes.hpp"
Expand All @@ -10,25 +12,110 @@ namespace atlas
private:
const uint16_t max_entries;
const uint32_t baseAddressOfpageTable;
bool isValidIndex(uint32_t idx);
size_t size();
uint16_t getMaxNumEntries(MMUMode mode) const;

bool isValidIndex(uint32_t idx)
{
uint32_t pte_size = (Mode == MMUMode::SV32) ? sizeof(RV32) : sizeof(RV64);
return ((idx >= baseAddressOfpageTable)
&& (idx < ((max_entries * pte_size) + baseAddressOfpageTable)));
}

size_t size() const { return pageTable.size(); }

uint16_t getMaxNumEntries() const
{
if (Mode == MMUMode::SV32)
return PT_ENTRIES_SV32;
else if (Mode == MMUMode::SV39)
return PT_ENTRIES_SV39;
return 0;
}

public:
std::map<uint32_t, PageTableEntry<Mode>>
pageTable; // uint32_t will be able to accomodate all the types of indexes(VPNs) for
// addressing the PTE

PageTable(uint32_t baseAddr);
void addEntry(uint32_t index, PageTableEntry<Mode> entry);
PageTableEntry<Mode> getEntry(uint32_t index);
void removeEntry(uint32_t index);
void print(std::ostream & os);
bool contains(uint32_t index);
pageTable; // uint32_t accommodates all types of VPNs for addressing the PTE

PageTable(uint32_t baseAddr) :
max_entries(getMaxNumEntries()),
baseAddressOfpageTable(baseAddr)
{
}

void addEntry(uint32_t index, PageTableEntry<Mode> entry)
{
if (size() <= max_entries && isValidIndex(index))
{
auto it = pageTable.find(index);
if (it != pageTable.end())
{
it->second = entry; // Update the existing entry
}
else
{
pageTable.insert({index, entry}); // Insert the new entry
}
}
else
{
throw std::runtime_error(
"Page table has reached its maximum capacity or index is out of bounds!");
}
}

PageTableEntry<Mode> getEntry(uint32_t index)
{
if (isValidIndex(index))
{
auto it = pageTable.find(index);
if (it != pageTable.end())
{
return it->second;
}
else
{
throw std::out_of_range("Index not found in the page table");
}
}
else
{
throw std::out_of_range("Invalid index");
}
}

void removeEntry(uint32_t index)
{
if (isValidIndex(index))
{
auto it = pageTable.find(index);
if (it != pageTable.end())
{
pageTable.erase(it);
}
else
{
throw std::out_of_range("Index not found in the page table");
}
}
else
{
throw std::out_of_range("Invalid index");
}
}

bool contains(uint32_t index) const { return pageTable.find(index) != pageTable.end(); }

void print(std::ostream & os) const
{
for (const auto & [index, entry] : pageTable)
{
os << "Index: " << index << " | PTE: " << entry.getPTE() << std::endl;
}
}
};

template <MMUMode Mode>
std::ostream & operator<<(std::ostream & os, const PageTable<Mode> & pt);
template <MMUMode Mode> std::ostream & operator<<(std::ostream & os, const PageTable<Mode> & pt)
{
pt.print(os);
return os;
}
} // namespace atlas

#include "PageTable.tpp"
109 changes: 0 additions & 109 deletions core/PageTable.tpp

This file was deleted.

Loading

0 comments on commit 98ca826

Please sign in to comment.