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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
mod entry;
mod table;
use multiboot2::{BootInformation, ElfSectionFlags};
use crate::address::{PhysicalAddress, VirtualAddress};
use crate::frame_alloc::{round_to_page, round_to_page_upper};
pub use self::table::{ActivePageTables, InactivePageTables, PagingOffPageSet, MappingType, EntryFlags};
pub use self::table::PageTablesSet;
use self::table::entry::Entry;
use spin::Mutex;
use core::fmt::Write;
use crate::bootstrap_logging::Serial;
pub const PAGE_SIZE: usize = 4096;
const ENTRY_COUNT: usize = PAGE_SIZE / ::core::mem::size_of::<Entry>();
pub static ACTIVE_PAGE_TABLES: Mutex<ActivePageTables> = Mutex::new(ActivePageTables());
fn is_paging_on() -> bool {
let cr0: usize;
unsafe {
llvm_asm!("mov $0, cr0" : "=r"(cr0) ::: "intel" );
}
cr0 & 0x80000001 == 0x80000001
}
unsafe fn enable_paging(page_directory_address: PhysicalAddress) {
#[cfg(not(test))]
llvm_asm!("mov eax, $0
mov cr3, eax
mov eax, cr0
or eax, 0x80010001
mov cr0, eax "
:
: "r" (page_directory_address.addr())
: "eax", "memory"
: "intel", "volatile");
}
fn flush_tlb() {
#[cfg(not(test))]
unsafe {
llvm_asm!("mov eax, cr3
mov cr3, eax "
:
:
: "eax"
: "intel", "volatile");
}
}
fn swap_cr3(page_directory_address: PhysicalAddress) -> PhysicalAddress {
let old_value: PhysicalAddress;
unsafe {
llvm_asm!("mov $0, cr3
mov cr3, $1"
: "=&r"(old_value)
: "r"(page_directory_address)
: "memory"
: "intel", "volatile");
}
old_value
}
pub unsafe fn map_bootstrap(boot_info : &BootInformation) -> PagingOffPageSet {
let mut new_pages = PagingOffPageSet::paging_off_create_page_set();
new_pages.map_page_guard(VirtualAddress(0x00000000));
new_pages.map_page_guard(VirtualAddress(0xc0000000));
let _ = writeln!(Serial, "= Mapping the Bootstrap");
let elf_sections_tag = boot_info.elf_sections_tag()
.expect("GRUB, you're drunk. Give us our elf_sections_tag.");
for section in elf_sections_tag.sections() {
if !section.is_allocated() || section.name() == ".boot" || section.size() == 0 {
continue;
}
assert_eq!(section.start_address() as usize % PAGE_SIZE, 0, "sections must be page aligned");
let mut map_flags = EntryFlags::empty();
if section.flags().contains(ElfSectionFlags::WRITABLE) {
map_flags |= EntryFlags::WRITABLE
}
let from = section.start_address() as usize;
let to = from + sunrise_libutils::align_up(section.size() as usize, PAGE_SIZE);
let _ = writeln!(Serial, "= Identity mapping {:#010x}-{:#010x}", from, to);
new_pages.identity_map_region(PhysicalAddress(section.start_address() as usize),
section.size() as usize,
map_flags);
}
new_pages
}
pub trait VirtualSpaceLand {
fn start_addr() -> VirtualAddress;
fn end_addr() -> VirtualAddress;
fn start_table() -> usize {
Self::start_addr().addr() / (PAGE_SIZE * ENTRY_COUNT) as usize
}
fn end_table() -> usize {
Self::end_addr().addr() / (PAGE_SIZE * ENTRY_COUNT) as usize
}
}
pub struct KernelLand;
pub struct UserLand;
impl KernelLand {
const fn start_addr() -> VirtualAddress { VirtualAddress(0xc0000000) }
const fn end_addr() -> VirtualAddress { VirtualAddress(0xffffffff) }
}
impl UserLand {
const fn start_addr() -> VirtualAddress { VirtualAddress(0x00000000) }
const fn end_addr() -> VirtualAddress { VirtualAddress(0xbfffffff) }
}
impl VirtualSpaceLand for KernelLand {
fn start_addr() -> VirtualAddress { Self::start_addr() }
fn end_addr() -> VirtualAddress { Self::end_addr() }
}
impl VirtualSpaceLand for UserLand {
fn start_addr() -> VirtualAddress { Self::start_addr() }
fn end_addr() -> VirtualAddress { Self::end_addr() }
}
const_assert!(KernelLand::start_addr().0 < KernelLand::end_addr().0);
const_assert!(UserLand::start_addr().0 < UserLand::end_addr().0);
const_assert!(KernelLand::start_addr().0 % (ENTRY_COUNT * PAGE_SIZE) == 0);
const_assert!(UserLand::start_addr().0 % (ENTRY_COUNT * PAGE_SIZE) == 0);
pub fn get_page<Land: VirtualSpaceLand>() -> VirtualAddress {
ACTIVE_PAGE_TABLES.lock().get_page::<Land>()
}