Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update x86_64 dependency to version 0.8.3 #92

Merged
merged 4 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ required-features = ["binary"]

[dependencies]
xmas-elf = { version = "0.6.2", optional = true }
x86_64 = { version = "0.7.2", optional = true }
x86_64 = { version = "0.8.3", optional = true }
usize_conversions = { version = "0.2.0", optional = true }
fixedvec = { version = "0.2.4", optional = true }
bit_field = { version = "0.10.0", optional = true }
Expand Down
1 change: 1 addition & 0 deletions src/bootinfo/memory_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct MemoryMap {
}

#[doc(hidden)]
#[allow(clippy::new_without_default)]
impl MemoryMap {
pub fn new() -> Self {
MemoryMap {
Expand Down
17 changes: 11 additions & 6 deletions src/frame_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use super::{frame_range, phys_frame_range};
use bootloader::bootinfo::{MemoryMap, MemoryRegion, MemoryRegionType};
use x86_64::structures::paging::{frame::PhysFrameRange, PhysFrame};
use x86_64::structures::paging::{frame::PhysFrameRange, PhysFrame, UnusedPhysFrame};

pub(crate) struct FrameAllocator<'a> {
pub memory_map: &'a mut MemoryMap,
}

impl<'a> FrameAllocator<'a> {
pub(crate) fn allocate_frame(&mut self, region_type: MemoryRegionType) -> Option<PhysFrame> {
pub(crate) fn allocate_frame(
&mut self,
region_type: MemoryRegionType,
) -> Option<UnusedPhysFrame> {
// try to find an existing region of same type that can be enlarged
let mut iter = self.memory_map.iter_mut().peekable();
while let Some(region) = iter.next() {
Expand All @@ -17,7 +20,8 @@ impl<'a> FrameAllocator<'a> {
&& next.region_type == MemoryRegionType::Usable
&& !next.range.is_empty()
{
let frame = phys_frame_range(region.range).end;
let frame =
unsafe { UnusedPhysFrame::new(phys_frame_range(region.range).end) };
region.range.end_frame_number += 1;
iter.next().unwrap().range.start_frame_number += 1;
return Some(frame);
Expand All @@ -26,7 +30,7 @@ impl<'a> FrameAllocator<'a> {
}
}

fn split_usable_region<'a, I>(iter: &mut I) -> Option<(PhysFrame, PhysFrameRange)>
fn split_usable_region<'a, I>(iter: &mut I) -> Option<(UnusedPhysFrame, PhysFrameRange)>
where
I: Iterator<Item = &'a mut MemoryRegion>,
{
Expand All @@ -38,9 +42,10 @@ impl<'a> FrameAllocator<'a> {
continue;
}

let frame = phys_frame_range(region.range).start;
let physframe = phys_frame_range(region.range).start;
let unused_frame = unsafe { UnusedPhysFrame::new(physframe) };
region.range.start_frame_number += 1;
return Some((frame, PhysFrame::range(frame, frame + 1)));
return Some((unused_frame, PhysFrame::range(physframe, physframe + 1)));
}
None
}
Expand Down
10 changes: 6 additions & 4 deletions src/level4_entries.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use core::convert::TryInto;
use fixedvec::FixedVec;
use x86_64::ux;
use x86_64::{structures::paging::Page, VirtAddr};
use x86_64::{
structures::paging::{Page, PageTableIndex},
VirtAddr,
};
use xmas_elf::program::ProgramHeader64;

pub struct UsedLevel4Entries {
Expand Down Expand Up @@ -29,7 +31,7 @@ impl UsedLevel4Entries {
used
}

pub fn get_free_entry(&mut self) -> ux::u9 {
pub fn get_free_entry(&mut self) -> PageTableIndex {
let (idx, entry) = self
.entry_state
.iter_mut()
Expand All @@ -38,6 +40,6 @@ impl UsedLevel4Entries {
.expect("no usable level 4 entries found");

*entry = true;
ux::u9::new(idx.try_into().unwrap())
PageTableIndex::new(idx.try_into().unwrap())
}
}
22 changes: 12 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ use usize_conversions::usize_from;
use x86_64::instructions::tlb;
use x86_64::structures::paging::{
frame::PhysFrameRange, page_table::PageTableEntry, Mapper, Page, PageTable, PageTableFlags,
PhysFrame, RecursivePageTable, Size2MiB, Size4KiB,
PageTableIndex, PhysFrame, RecursivePageTable, Size2MiB, Size4KiB, UnusedPhysFrame,
};
use x86_64::ux::u9;
use x86_64::{PhysAddr, VirtAddr};

// The bootloader_config.rs file contains some configuration constants set by the build script:
Expand Down Expand Up @@ -170,7 +169,7 @@ fn bootloader_main(
enable_nxe_bit();

// Create a recursive page table entry
let recursive_index = u9::new(level4_entries.get_free_entry().try_into().unwrap());
let recursive_index = PageTableIndex::new(level4_entries.get_free_entry().try_into().unwrap());
let mut entry = PageTableEntry::new();
entry.set_addr(
p4_physical,
Expand Down Expand Up @@ -243,9 +242,9 @@ fn bootloader_main(
let boot_info_page = {
let page: Page = Page::from_page_table_indices(
level4_entries.get_free_entry(),
u9::new(0),
u9::new(0),
u9::new(0),
PageTableIndex::new(0),
PageTableIndex::new(0),
PageTableIndex::new(0),
);
let frame = frame_allocator
.allocate_frame(MemoryRegionType::BootInfo)
Expand Down Expand Up @@ -287,9 +286,12 @@ fn bootloader_main(
// If offset not manually provided, find a free p4 entry and map memory here.
// One level 4 entry spans 2^48/512 bytes (over 500gib) so this should suffice.
assert!(max_phys_addr < (1 << 48) / 512);
Page::from_page_table_indices_1gib(level4_entries.get_free_entry(), u9::new(0))
.start_address()
.as_u64()
Page::from_page_table_indices_1gib(
level4_entries.get_free_entry(),
PageTableIndex::new(0),
)
.start_address()
.as_u64()
});

let virt_for_phys =
Expand All @@ -304,7 +306,7 @@ fn bootloader_main(
unsafe {
page_table::map_page(
page,
frame,
UnusedPhysFrame::new(frame),
flags,
&mut rec_page_table,
&mut frame_allocator,
Expand Down
19 changes: 14 additions & 5 deletions src/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use fixedvec::FixedVec;
use x86_64::structures::paging::mapper::{MapToError, MapperFlush, UnmapError};
use x86_64::structures::paging::{
self, Mapper, Page, PageSize, PageTableFlags, PhysFrame, RecursivePageTable, Size4KiB,
UnusedPhysFrame,
};
use x86_64::{align_up, PhysAddr, VirtAddr};
use xmas_elf::program::{self, ProgramHeader64};
Expand Down Expand Up @@ -68,8 +69,16 @@ pub(crate) fn map_segment(
for frame in PhysFrame::range_inclusive(start_frame, end_frame) {
let offset = frame - start_frame;
let page = start_page + offset;
unsafe { map_page(page, frame, page_table_flags, page_table, frame_allocator)? }
.flush();
unsafe {
map_page(
page,
UnusedPhysFrame::new(frame),
page_table_flags,
page_table,
frame_allocator,
)?
}
.flush();
}

if mem_size > file_size {
Expand All @@ -89,7 +98,7 @@ pub(crate) fn map_segment(
unsafe {
map_page(
temp_page.clone(),
new_frame.clone(),
UnusedPhysFrame::new(new_frame.clone()),
page_table_flags,
page_table,
frame_allocator,
Expand Down Expand Up @@ -159,7 +168,7 @@ pub(crate) fn map_segment(

pub(crate) unsafe fn map_page<'a, S>(
page: Page<S>,
phys_frame: PhysFrame<S>,
phys_frame: UnusedPhysFrame<S>,
flags: PageTableFlags,
page_table: &mut RecursivePageTable<'a>,
frame_allocator: &mut FrameAllocator,
Expand All @@ -171,7 +180,7 @@ where
struct PageTableAllocator<'a, 'b: 'a>(&'a mut FrameAllocator<'b>);

unsafe impl<'a, 'b> paging::FrameAllocator<Size4KiB> for PageTableAllocator<'a, 'b> {
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
fn allocate_frame(&mut self) -> Option<UnusedPhysFrame<Size4KiB>> {
self.0.allocate_frame(MemoryRegionType::PageTable)
}
}
Expand Down