blob: 252e5bb318a37f5dd1b672626e399febb154706c (
plain)
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
|
#pragma once
#include <result.h>
#include <stdlib.h>
extern "C" void dump_address();
class vmm {
public:
enum Error { NoError, TableNotAllocated, AddressAlreadyMapped };
struct address {
size_t offset : 12 = 0;
size_t page_idx : 10 = 0;
size_t table_idx : 10 = 0;
constexpr address(uint32_t addr)
: offset{static_cast<size_t>(addr & 0x3ff)},
page_idx{static_cast<size_t>((addr >> 12) & 0x3ff)},
table_idx{static_cast<size_t>(addr >> 22)} {}
constexpr address(size_t table, size_t page) : page_idx{page}, table_idx{table} {}
constexpr operator uint32_t() const {
return static_cast<uint32_t>(table_idx << 22) | static_cast<uint32_t>(page_idx << 12) |
static_cast<uint32_t>(offset);
}
} __attribute__((packed));
vmm(uint32_t* addr = nullptr);
[[deprecated]] constexpr static size_t table_id(uint32_t offset) {
offset &= 0xfff00000;
offset /= (4 * 1024 * 1024);
return static_cast<size_t>(offset);
}
[[deprecated]] constexpr static size_t page_id(uint32_t offset) { return static_cast<size_t>(offset / 4096); }
static void reload() {
uint32_t cr0;
asm volatile("mov %%cr0, %0" : "=r"(cr0));
asm volatile("mov %0, %%cr0" : : "r"(cr0));
}
[[nodiscard]] Result<uint32_t, Error> map(uint32_t phys_addr, const address dest);
private:
class page_table {
public:
page_table(uint32_t);
bool set(size_t idx, uint32_t a) {
if (pages[idx] != 0) return false;
pages[idx] = a | 0x003;
return true;
}
private:
uint32_t* pages;
};
uint32_t* directory;
};
|