Skip to content

Commit 77a6e44

Browse files
authored
Merge pull request #102 from cryptoknight/77-add-page-size-constant
Replace magic page size numbers with constants
2 parents a84235b + 2e403e8 commit 77a6e44

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/boot
22
/target
3-
mythril/target
4-
mythril/src/blob/bios.bin
53
mythril/.cargo
4+
mythril/src/blob/bios.bin
5+
mythril/target
6+
mythril/vendor
67
_boot.img
78
**/*.rs.bk
89
debug.log

mythril/src/memory.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use x86::bits64::paging::*;
1414
use x86::controlregs::Cr0;
1515

1616
#[repr(align(4096))]
17-
pub struct Raw4kPage(pub [u8; 4096]);
17+
pub struct Raw4kPage(pub [u8; BASE_PAGE_SIZE]);
1818
impl Default for Raw4kPage {
1919
fn default() -> Self {
20-
Raw4kPage([0u8; 4096])
20+
Raw4kPage([0u8; BASE_PAGE_SIZE])
2121
}
2222
}
2323

@@ -221,7 +221,7 @@ impl fmt::Debug for HostPhysAddr {
221221
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
222222
pub struct HostPhysFrame(HostPhysAddr);
223223
impl HostPhysFrame {
224-
pub const SIZE: usize = 4096;
224+
pub const SIZE: usize = BASE_PAGE_SIZE;
225225

226226
pub fn from_start_address(addr: HostPhysAddr) -> Result<Self> {
227227
if !addr.is_frame_aligned() {
@@ -391,7 +391,7 @@ impl GuestAddressSpace {
391391
addr: GuestVirtAddr,
392392
access: GuestAccess,
393393
) -> Result<FrameIter> {
394-
//TODO: align the addr to 4096 boundary
394+
//TODO: align the addr to BASE_PAGE_SIZE boundary
395395
Ok(FrameIter {
396396
view: GuestAddressSpaceView::new(cr3, self),
397397
addr: addr,
@@ -561,7 +561,7 @@ impl<'a> Iterator for FrameIter<'a> {
561561

562562
// This is the smallest possible guest page size, so permissions
563563
// can't change except at this granularity
564-
self.addr = self.addr + 4096;
564+
self.addr = self.addr + BASE_PAGE_SIZE;
565565

566566
let physaddr =
567567
match self.view.translate_linear_address(old, self.access) {
@@ -574,15 +574,15 @@ impl<'a> Iterator for FrameIter<'a> {
574574

575575
#[repr(align(4096))]
576576
pub struct EptTable<T> {
577-
entries: [T; 512],
577+
entries: [T; PAGE_SIZE_ENTRIES],
578578
}
579579
impl<T> Default for EptTable<T>
580580
where
581581
T: Copy + Default,
582582
{
583583
fn default() -> Self {
584584
Self {
585-
entries: [T::default(); 512],
585+
entries: [T::default(); PAGE_SIZE_ENTRIES],
586586
}
587587
}
588588
}

mythril/src/vm.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use alloc::string::String;
1919
use alloc::sync::Arc;
2020
use alloc::vec::Vec;
2121
use arraydeque::ArrayDeque;
22+
use core::mem;
2223
use spin::RwLock;
2324

2425
static BIOS_BLOB: &'static [u8] = include_bytes!("blob/bios.bin");
@@ -345,7 +346,8 @@ impl VirtualMachine {
345346
addr: &GuestPhysAddr,
346347
space: &mut GuestAddressSpace,
347348
) -> Result<()> {
348-
for (i, chunk) in image.chunks(4096 as usize).enumerate() {
349+
for (i, chunk) in image.chunks(mem::size_of::<Raw4kPage>()).enumerate()
350+
{
349351
let frame_ptr =
350352
Box::into_raw(Box::new(Raw4kPage::default())) as *mut u8;
351353
let frame = HostPhysFrame::from_start_address(HostPhysAddr::new(
@@ -362,7 +364,8 @@ impl VirtualMachine {
362364

363365
space.map_frame(
364366
memory::GuestPhysAddr::new(
365-
addr.as_u64() + (i as u64 * 4096) as u64,
367+
addr.as_u64()
368+
+ (i as u64 * mem::size_of::<Raw4kPage>() as u64),
366369
),
367370
frame,
368371
false,
@@ -417,7 +420,9 @@ impl VirtualMachine {
417420
// Iterate over each page
418421
for i in 0..(config.memory << 8) {
419422
match guest_space.map_new_frame(
420-
memory::GuestPhysAddr::new((i as u64 * 4096) as u64),
423+
memory::GuestPhysAddr::new(
424+
i as u64 * mem::size_of::<Raw4kPage>() as u64,
425+
),
421426
false,
422427
) {
423428
Ok(_) | Err(Error::DuplicateMapping(_)) => continue,

0 commit comments

Comments
 (0)