Skip to content

Commit 5daa669

Browse files
committed
use 3 level ept paging
1 parent 856aab6 commit 5daa669

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ repository = "https://github.com/arceos-hypervisor/arm_vcpu"
1515
categories = ["embedded", "no-std"]
1616
keywords = ["hypervisor", "aarch64", "vcpu"]
1717

18+
[features]
19+
4-level-ept = []
1820

1921
[dependencies]
2022
log = "0.4"

src/vcpu.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,50 @@ impl<H: AxVCpuHal> Aarch64VCpu<H> {
171171
// - 4KiB granule (TG0)
172172
// - 39-bit address space (T0_SZ)
173173
// - start at level 1 (SL0)
174-
// self.guest_system_regs.vtcr_el2 = (VTCR_EL2::PS::PA_40B_1TB
175-
// + VTCR_EL2::TG0::Granule4KB
176-
// + VTCR_EL2::SH0::Inner
177-
// + VTCR_EL2::ORGN0::NormalWBRAWA
178-
// + VTCR_EL2::IRGN0::NormalWBRAWA
179-
// + VTCR_EL2::SL0.val(0b01)
180-
// + VTCR_EL2::T0SZ.val(64 - 39))
181-
// .into();
174+
#[cfg(not(feature = "4-level-ept"))]
175+
{
176+
self.guest_system_regs.vtcr_el2 = (VTCR_EL2::PS::PA_40B_1TB
177+
+ VTCR_EL2::TG0::Granule4KB
178+
+ VTCR_EL2::SH0::Inner
179+
+ VTCR_EL2::ORGN0::NormalWBRAWA
180+
+ VTCR_EL2::IRGN0::NormalWBRAWA
181+
+ VTCR_EL2::SL0.val(0b01)
182+
+ VTCR_EL2::T0SZ.val(64 - 39))
183+
.into();
184+
}
182185

183186
// use 4 level ept paging
184187
// - 4KiB granule (TG0)
185188
// - 48-bit address space (T0_SZ)
186189
// - start at level 0 (SL0)
187-
self.guest_system_regs.vtcr_el2 = (VTCR_EL2::PS::PA_48B_256TB
188-
+ VTCR_EL2::TG0::Granule4KB
189-
+ VTCR_EL2::SH0::Inner
190-
+ VTCR_EL2::ORGN0::NormalWBRAWA
191-
+ VTCR_EL2::IRGN0::NormalWBRAWA
192-
+ VTCR_EL2::SL0.val(0b10) // 0b10 means start at level 0
193-
+ VTCR_EL2::T0SZ.val(64 - 48))
194-
.into();
190+
#[cfg(feature = "4-level-ept")]
191+
{
192+
// read PARange (bits 3:0)
193+
let parange = (ID_AA64MMFR0_EL1.get() & 0xF) as u8;
194+
// ARM Definition: 0x5 indicates 48 bits PA, 0x4 indicates 44 bits PA, and so on.
195+
if parange <= 0x5 {
196+
panic!(
197+
"CPU only supports {}-bit PA (< 48), \
198+
cannot enable 4-level EPT paging!",
199+
match parange {
200+
0x0 => 32,
201+
0x1 => 36,
202+
0x2 => 40,
203+
0x3 => 42,
204+
0x4 => 44,
205+
_ => 48,
206+
}
207+
);
208+
}
209+
self.guest_system_regs.vtcr_el2 = (VTCR_EL2::PS::PA_48B_256TB
210+
+ VTCR_EL2::TG0::Granule4KB
211+
+ VTCR_EL2::SH0::Inner
212+
+ VTCR_EL2::ORGN0::NormalWBRAWA
213+
+ VTCR_EL2::IRGN0::NormalWBRAWA
214+
+ VTCR_EL2::SL0.val(0b10) // 0b10 means start at level 0
215+
+ VTCR_EL2::T0SZ.val(64 - 48))
216+
.into();
217+
}
195218

196219
let mut hcr_el2 = HCR_EL2::VM::Enable
197220
+ HCR_EL2::RW::EL1IsAarch64

0 commit comments

Comments
 (0)