-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ben Gubler
committed
Jun 7, 2018
1 parent
6175bfe
commit 46a67a4
Showing
14 changed files
with
552 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use memory::{Frame, FrameAllocator}; | ||
use multiboot2::{MemoryArea, MemoryAreaIter}; | ||
|
||
pub struct AreaFrameAllocator { | ||
next_free_frame: Frame, | ||
current_area: Option<&'static MemoryArea>, | ||
areas: MemoryAreaIter, | ||
kernel_start: Frame, | ||
kernel_end: Frame, | ||
multiboot_start: Frame, | ||
multiboot_end: Frame, | ||
} | ||
|
||
impl FrameAllocator for AreaFrameAllocator { | ||
fn allocate_frame(&mut self) -> Option<Frame> { | ||
if let Some(area) = self.current_area { | ||
// "Clone" the frame to return it if it's free. Frame doesn't | ||
// implement Clone, but we can construct an identical frame. | ||
let frame = Frame { | ||
number: self.next_free_frame.number, | ||
}; | ||
|
||
// the last frame of the current area | ||
let current_area_last_frame = { | ||
let address = area.base_addr + area.length - 1; | ||
Frame::containing_address(address as usize) | ||
}; | ||
|
||
if frame > current_area_last_frame { | ||
// all frames of current area are used, switch to next area | ||
self.choose_next_area(); | ||
} else if frame >= self.kernel_start && frame <= self.kernel_end { | ||
// `frame` is used by the kernel | ||
self.next_free_frame = Frame { | ||
number: self.kernel_end.number + 1, | ||
}; | ||
} else if frame >= self.multiboot_start && frame <= self.multiboot_end { | ||
// `frame` is used by the multiboot information structure | ||
self.next_free_frame = Frame { | ||
number: self.multiboot_end.number + 1, | ||
}; | ||
} else { | ||
// frame is unused, increment `next_free_frame` and return it | ||
self.next_free_frame.number += 1; | ||
return Some(frame); | ||
} | ||
// `frame` was not valid, try it again with the updated `next_free_frame` | ||
self.allocate_frame() | ||
} else { | ||
None // no free frames left | ||
} | ||
} | ||
|
||
fn deallocate_frame(&mut self, _frame: Frame) { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl AreaFrameAllocator { | ||
pub fn new( | ||
kernel_start: usize, | ||
kernel_end: usize, | ||
multiboot_start: usize, | ||
multiboot_end: usize, | ||
memory_areas: MemoryAreaIter, | ||
) -> AreaFrameAllocator { | ||
let mut allocator = AreaFrameAllocator { | ||
next_free_frame: Frame::containing_address(0), | ||
current_area: None, | ||
areas: memory_areas, | ||
kernel_start: Frame::containing_address(kernel_start), | ||
kernel_end: Frame::containing_address(kernel_end), | ||
multiboot_start: Frame::containing_address(multiboot_start), | ||
multiboot_end: Frame::containing_address(multiboot_end), | ||
}; | ||
allocator.choose_next_area(); | ||
allocator | ||
} | ||
|
||
fn choose_next_area(&mut self) { | ||
self.current_area = self.areas | ||
.clone() | ||
.filter(|area| { | ||
let address = area.base_addr + area.length - 1; | ||
Frame::containing_address(address as usize) >= self.next_free_frame | ||
}) | ||
.min_by_key(|area| area.base_addr); | ||
|
||
if let Some(area) = self.current_area { | ||
let start_frame = Frame::containing_address(area.base_addr as usize); | ||
if self.next_free_frame < start_frame { | ||
self.next_free_frame = start_frame; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
pub use self::area_frame_allocator::AreaFrameAllocator; | ||
pub use self::paging::test_paging; | ||
use self::paging::PhysicalAddress; | ||
|
||
mod area_frame_allocator; | ||
mod paging; | ||
|
||
pub const PAGE_SIZE: usize = 4096; | ||
|
||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Frame { | ||
number: usize, | ||
} | ||
|
||
impl Frame { | ||
fn containing_address(address: usize) -> Frame { | ||
Frame { | ||
number: address / PAGE_SIZE, | ||
} | ||
} | ||
|
||
fn start_address(&self) -> PhysicalAddress { | ||
self.number * PAGE_SIZE | ||
} | ||
} | ||
|
||
pub trait FrameAllocator { | ||
fn allocate_frame(&mut self) -> Option<Frame>; | ||
fn deallocate_frame(&mut self, frame: Frame); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use memory::Frame; | ||
|
||
pub struct Entry(u64); | ||
|
||
impl Entry { | ||
pub fn is_unused(&self) -> bool { | ||
self.0 == 0 | ||
} | ||
|
||
pub fn set_unused(&mut self) { | ||
self.0 = 0; | ||
} | ||
|
||
pub fn flags(&self) -> EntryFlags { | ||
EntryFlags::from_bits_truncate(self.0) | ||
} | ||
|
||
pub fn pointed_frame(&self) -> Option<Frame> { | ||
if self.flags().contains(PRESENT) { | ||
Some(Frame::containing_address( | ||
self.0 as usize & 0x000fffff_fffff000, | ||
)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn set(&mut self, frame: Frame, flags: EntryFlags) { | ||
assert!(frame.start_address() & !0x000fffff_fffff000 == 0); | ||
self.0 = (frame.start_address() as u64) | flags.bits(); | ||
} | ||
} | ||
|
||
bitflags! { | ||
pub struct EntryFlags: u64 { | ||
const PRESENT = 1 << 0; | ||
const WRITABLE = 1 << 1; | ||
const USER_ACCESSIBLE = 1 << 2; | ||
const WRITE_THROUGH = 1 << 3; | ||
const NO_CACHE = 1 << 4; | ||
const ACCESSED = 1 << 5; | ||
const DIRTY = 1 << 6; | ||
const HUGE_PAGE = 1 << 7; | ||
const GLOBAL = 1 << 8; | ||
const NO_EXECUTE = 1 << 63; | ||
} | ||
} |
Oops, something went wrong.