restructured mmu code

This commit is contained in:
folkert van heusden 2024-05-11 00:09:29 +02:00
parent ec64f77a6d
commit 04097a1b7c
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
4 changed files with 132 additions and 123 deletions

View file

@ -237,7 +237,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm
{ {
int run_mode = mode_selection == rm_cur ? c->getPSW_runmode() : c->getPSW_prev_runmode(); int run_mode = mode_selection == rm_cur ? c->getPSW_runmode() : c->getPSW_prev_runmode();
uint32_t m_offset = mmu_->calculate_physical_address(c, run_mode, addr_in, !peek_only, false, peek_only, space); uint32_t m_offset = mmu_->calculate_physical_address(c, run_mode, addr_in, !peek_only, false, space);
uint32_t io_base = mmu_->get_io_base(); uint32_t io_base = mmu_->get_io_base();
bool is_io = m_offset >= io_base; bool is_io = m_offset >= io_base;
@ -553,7 +553,7 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1
if (mmu_->is_enabled() && (addr_in & 1) == 0 /* TODO remove this? */ && addr_in != ADDR_MMR0) if (mmu_->is_enabled() && (addr_in & 1) == 0 /* TODO remove this? */ && addr_in != ADDR_MMR0)
mmu_->set_page_written_to(run_mode, d, apf); mmu_->set_page_written_to(run_mode, d, apf);
uint32_t m_offset = mmu_->calculate_physical_address(c, run_mode, addr_in, true, true, false, space); uint32_t m_offset = mmu_->calculate_physical_address(c, run_mode, addr_in, true, true, space);
uint32_t io_base = mmu_->get_io_base(); uint32_t io_base = mmu_->get_io_base();
bool is_io = m_offset >= io_base; bool is_io = m_offset >= io_base;

View file

@ -146,7 +146,7 @@ void console_ncurses::panel_update_thread()
int run_mode = current_PSW >> 14; int run_mode = current_PSW >> 14;
uint16_t current_PC = c->getPC(); uint16_t current_PC = c->getPC();
uint32_t full_addr = b->getMMU()->calculate_physical_address(c, run_mode, current_PC, false, false, true, i_space); uint32_t full_addr = b->getMMU()->calculate_physical_address(c, run_mode, current_PC, false, false, i_space);
uint16_t current_instr = b->read_word(current_PC); uint16_t current_instr = b->read_word(current_PC);

85
mmu.cpp
View file

@ -353,32 +353,13 @@ void mmu::mmudebug(const uint16_t a)
} }
} }
uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space) void mmu::verify_page_access(cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write)
{ {
uint32_t m_offset = a;
if (is_enabled() || (is_write && (getMMR0() & (1 << 8 /* maintenance check */)))) {
uint8_t apf = a >> 13; // active page field
bool d = space == d_space && get_use_data_space(run_mode);
uint16_t p_offset = a & 8191; // page offset
m_offset = get_physical_memory_offset(run_mode, d, apf);
m_offset += p_offset;
if ((getMMR3() & 16) == 0) // off is 18bit
m_offset &= 0x3ffff;
uint32_t io_base = get_io_base();
bool is_io = m_offset >= io_base;
if (trap_on_failure) {
{
const auto [ trap_action, access_control ] = get_trap_action(run_mode, d, apf, is_write); const auto [ trap_action, access_control ] = get_trap_action(run_mode, d, apf, is_write);
if (trap_action != T_PROCEED) [[unlikely]] { if (trap_action == T_PROCEED)
return;
if (is_write) if (is_write)
set_page_trapped(run_mode, d, apf); set_page_trapped(run_mode, d, apf);
@ -407,26 +388,25 @@ uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const
} }
if (trap_action == T_TRAP_250) { if (trap_action == T_TRAP_250) {
TRACE("Page access %d (for virtual address %06o): trap 0250", access_control, a); TRACE("Page access %d (for virtual address %06o): trap 0250", access_control, virt_addr);
c->trap(0250); // trap c->trap(0250); // trap
throw 5; throw 5;
} }
else { // T_ABORT_4 else { // T_ABORT_4
TRACE("Page access %d (for virtual address %06o): trap 004", access_control, a); TRACE("Page access %d (for virtual address %06o): trap 004", access_control, virt_addr);
c->trap(004); // abort c->trap(004); // abort
throw 5; throw 5;
} }
} }
}
void mmu::verify_access_valid(cpu *const c, const uint32_t m_offset, const int run_mode, const bool d, const int apf, const bool is_io, const bool is_write)
{
if (m_offset >= m->get_memory_size() && !is_io) [[unlikely]] { if (m_offset >= m->get_memory_size() && !is_io) [[unlikely]] {
if (!peek_only) TRACE("TRAP(04) (throw 6) on address %08o", m_offset);
TRACE("mmu::calculate_physical_address %o >= %o", m_offset, m->get_memory_size());
TRACE("TRAP(04) (throw 6) on address %06o", a);
if (is_locked() == false) { if (is_locked() == false) {
uint16_t temp = getMMR0(); uint16_t temp = getMMR0();
@ -450,17 +430,19 @@ uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const
throw 6; throw 6;
} }
}
void mmu::verify_page_length(cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write)
{
uint16_t pdr_len = get_pdr_len(run_mode, d, apf); uint16_t pdr_len = get_pdr_len(run_mode, d, apf);
uint16_t pdr_cmp = (a >> 6) & 127; uint16_t pdr_cmp = (virt_addr >> 6) & 127;
bool direction = get_pdr_direction(run_mode, d, apf); bool direction = get_pdr_direction(run_mode, d, apf);
// TRACE("p_offset %06o pdr_len %06o direction %d, run_mode %d, apf %d, pdr: %06o", p_offset, pdr_len, direction, run_mode, apf, pages[run_mode][d][apf].pdr);
if ((pdr_cmp > pdr_len && direction == false) || (pdr_cmp < pdr_len && direction == true)) [[unlikely]] { if ((pdr_cmp > pdr_len && direction == false) || (pdr_cmp < pdr_len && direction == true)) [[unlikely]] {
TRACE("mmu::calculate_physical_address::p_offset %o versus %o direction %d", pdr_cmp, pdr_len, direction); TRACE("mmu::calculate_physical_address::p_offset %o versus %o direction %d", pdr_cmp, pdr_len, direction);
TRACE("TRAP(0250) (throw 7) on address %06o", a); TRACE("TRAP(0250) (throw 7) on address %06o", virt_addr);
c->trap(0250); // invalid access c->trap(0250); // invalid access
if (is_locked() == false) { if (is_locked() == false) {
@ -486,14 +468,37 @@ uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const
throw 7; throw 7;
} }
} }
TRACE("virtual address %06o maps to physical address %08o (run_mode: %d, apf: %d, par: %08o, poff: %o, AC: %d, %s)", a, m_offset, run_mode, apf, uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const d_i_space_t space)
get_physical_memory_offset(run_mode, d, apf), {
p_offset, get_access_control(run_mode, d, apf), d ? "D" : "I"); uint32_t m_offset = a;
if (is_enabled() || (is_write && (getMMR0() & (1 << 8 /* maintenance check */)))) {
uint8_t apf = a >> 13; // active page field
bool d = space == d_space && get_use_data_space(run_mode);
uint16_t p_offset = a & 8191; // page offset
m_offset = get_physical_memory_offset(run_mode, d, apf);
m_offset += p_offset;
if ((getMMR3() & 16) == 0) // off is 18bit
m_offset &= 0x3ffff;
if (trap_on_failure) {
verify_page_access(c, a, run_mode, d, apf, is_write);
// e.g. ram or i/o, not unmapped
uint32_t io_base = get_io_base();
bool is_io = m_offset >= io_base;
verify_access_valid(c, m_offset, run_mode, d, apf, is_io, is_write);
verify_page_length(c, a, run_mode, d, apf, is_write);
} }
else {
// TRACE("no MMU (read physical address %08o)", m_offset);
} }
return m_offset; return m_offset;

6
mmu.h
View file

@ -62,6 +62,10 @@ private:
void set_par_pdr(const json_t *const j_in, const int run_mode, const bool is_d, const std::string & name); void set_par_pdr(const json_t *const j_in, const int run_mode, const bool is_d, const std::string & name);
#endif #endif
void verify_page_access (cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write);
void verify_access_valid(cpu *const c, const uint32_t m_offset, const int run_mode, const bool d, const int apf, const bool is_io, const bool is_write);
void verify_page_length (cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write);
public: public:
mmu(); mmu();
virtual ~mmu(); virtual ~mmu();
@ -94,7 +98,7 @@ public:
memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const; memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const;
std::pair<trap_action_t, int> get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write); std::pair<trap_action_t, int> get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write);
uint32_t calculate_physical_address(cpu *const c, const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space); uint32_t calculate_physical_address(cpu *const c, const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const d_i_space_t space);
uint16_t getMMR0() const { return MMR0; } uint16_t getMMR0() const { return MMR0; }
uint16_t getMMR1() const { return MMR1; } uint16_t getMMR1() const { return MMR1; }