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
pub mod entry;
pub mod table;
pub mod lands;
use crate::mem::{VirtualAddress, PhysicalAddress};
pub const PAGE_SIZE: usize = 4096;
pub const ENTRY_COUNT: usize = PAGE_SIZE / ::core::mem::size_of::<entry::I386Entry>();
pub fn is_paging_on() -> bool {
let cr0: usize;
unsafe {
llvm_asm!("mov $0, cr0" : "=r"(cr0) ::: "intel" );
}
cr0 & 0x80000001 == 0x80000001
}
pub unsafe fn enable_paging(page_directory_address: PhysicalAddress) {
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 fn read_cr3() -> PhysicalAddress {
let cr3_value: usize;
unsafe {
llvm_asm!( "mov $0, cr3" : "=r"(cr3_value) : : : "intel", "volatile");
}
PhysicalAddress(cr3_value)
}
pub fn read_cr2() -> VirtualAddress {
let cr2_value : usize;
unsafe {
llvm_asm!( "mov $0, cr2" : "=r"(cr2_value) : : : "intel", "volatile");
}
VirtualAddress(cr2_value)
}