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
pub mod x32;
pub mod x32_pae;
pub mod x64;
use super::{
mmu_spec::{translate_data::TranslateVec, ArchMMUSpec, MMUTranslationBase},
Architecture, ArchitectureObj, Endianess, ScopedVirtualTranslate,
};
use super::Bump;
use crate::error::{Error, Result};
use crate::iter::SplitAtIndex;
use crate::mem::PhysicalMemory;
use crate::types::{Address, PhysicalAddress};
pub struct X86Architecture {
bits: u8,
endianess: Endianess,
mmu: ArchMMUSpec,
}
impl Architecture for X86Architecture {
fn bits(&self) -> u8 {
self.bits
}
fn endianess(&self) -> Endianess {
self.endianess
}
fn page_size(&self) -> usize {
self.mmu.page_size_level(1)
}
fn size_addr(&self) -> usize {
self.mmu.addr_size.into()
}
fn address_space_bits(&self) -> u8 {
self.mmu.address_space_bits
}
}
#[derive(Clone, Copy)]
pub struct X86ScopedVirtualTranslate {
arch: &'static X86Architecture,
dtb: X86PageTableBase,
}
impl X86ScopedVirtualTranslate {
pub fn new(arch: &'static X86Architecture, dtb: Address) -> Self {
Self {
arch,
dtb: X86PageTableBase(dtb),
}
}
}
impl ScopedVirtualTranslate for X86ScopedVirtualTranslate {
fn virt_to_phys_iter<
T: PhysicalMemory + ?Sized,
B: SplitAtIndex,
VI: Iterator<Item = (Address, B)>,
VO: Extend<(PhysicalAddress, B)>,
FO: Extend<(Error, Address, B)>,
>(
&self,
mem: &mut T,
addrs: VI,
out: &mut VO,
out_fail: &mut FO,
arena: &Bump,
) {
self.arch
.mmu
.virt_to_phys_iter(mem, self.dtb, addrs, out, out_fail, arena)
}
fn translation_table_id(&self, _address: Address) -> usize {
self.dtb.0.as_u64().overflowing_shr(12).0 as usize
}
fn arch(&self) -> ArchitectureObj {
self.arch
}
}
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct X86PageTableBase(Address);
impl MMUTranslationBase for X86PageTableBase {
fn get_initial_pt(&self, _: Address) -> Address {
self.0
}
fn get_pt_by_index(&self, _: usize) -> Address {
self.0
}
fn pt_count(&self) -> usize {
1
}
fn virt_addr_filter<B, O>(
&self,
spec: &ArchMMUSpec,
addr: (Address, B),
data_to_translate: &mut TranslateVec<B>,
out_fail: &mut O,
) where
B: SplitAtIndex,
O: Extend<(Error, Address, B)>,
{
spec.virt_addr_filter(addr, &mut data_to_translate[0].vec, out_fail);
}
}
fn underlying_arch(arch: ArchitectureObj) -> Option<&'static X86Architecture> {
if arch == x64::ARCH {
Some(&x64::ARCH_SPEC)
} else if arch == x32::ARCH {
Some(&x32::ARCH_SPEC)
} else if arch == x32_pae::ARCH {
Some(&x32_pae::ARCH_SPEC)
} else {
None
}
}
pub fn new_translator(dtb: Address, arch: ArchitectureObj) -> Result<impl ScopedVirtualTranslate> {
let arch = underlying_arch(arch).ok_or(Error::InvalidArchitecture)?;
Ok(X86ScopedVirtualTranslate::new(arch, dtb))
}
pub fn is_x86_arch(arch: ArchitectureObj) -> bool {
underlying_arch(arch).is_some()
}