forked from havluj/i386-memory-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccpu.cpp
executable file
·66 lines (54 loc) · 1.99 KB
/
ccpu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef __PROGTEST__
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include "common.h"
using namespace std;
#endif /* __PROGTEST__ */
//-------------------------------------------------------------------------------------------------
CCPU::CCPU(uint8_t* memStart, uint32_t pageTableRoot)
{
m_MemStart = memStart;
m_PageTableRoot = pageTableRoot;
}
//-------------------------------------------------------------------------------------------------
bool CCPU::ReadInt(uint32_t address, uint32_t& value)
{
if(address & 0x3) { return false; } // not aligned
uint32_t* addr = virtual2Physical(address, false);
if(!addr) { return false; }
value = *addr;
return true;
}
//-------------------------------------------------------------------------------------------------
bool CCPU::WriteInt(uint32_t address, uint32_t value)
{
if(address & 0x3) { return false; } // not aligned
uint32_t* addr = virtual2Physical(address, true);
if(!addr) { return false; }
*addr = value;
return true;
}
//-------------------------------------------------------------------------------------------------
uint32_t* CCPU::virtual2Physical(uint32_t address, bool write)
{
const uint32_t reqMask = BIT_PRESENT | BIT_USER | (write ? BIT_WRITE : 0);
const uint32_t orMask = BIT_REFERENCED | (write ? BIT_DIRTY : 0);
while(1) {
uint32_t* level1 = (uint32_t*) (m_MemStart + (m_PageTableRoot & ADDR_MASK)) + (address >> 22);
if((*level1 & reqMask) != reqMask) {
if(pageFaultHandler(address, write)) { continue; }
return NULL;
}
uint32_t* level2 = (uint32_t*) (m_MemStart + (*level1 & ADDR_MASK))
+ ((address >> OFFSET_BITS) & (PAGE_DIR_ENTRIES - 1));
if((*level2 & reqMask) != reqMask) {
if(pageFaultHandler(address, write)) { continue; }
return NULL;
}
*level1 |= orMask;
*level2 |= orMask;
return (uint32_t*) (m_MemStart + (*level2 & ADDR_MASK) + (address & ~ADDR_MASK));
}
}
//-------------------------------------------------------------------------------------------------