mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Merge branch 'post-09' into post-10
This commit is contained in:
@@ -2,6 +2,7 @@ use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
|||||||
use x86_64::{
|
use x86_64::{
|
||||||
structures::paging::{
|
structures::paging::{
|
||||||
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
||||||
|
UnusedPhysFrame,
|
||||||
},
|
},
|
||||||
PhysAddr, VirtAddr,
|
PhysAddr, VirtAddr,
|
||||||
};
|
};
|
||||||
@@ -44,9 +45,11 @@ pub fn create_example_mapping(
|
|||||||
use x86_64::structures::paging::PageTableFlags as Flags;
|
use x86_64::structures::paging::PageTableFlags as Flags;
|
||||||
|
|
||||||
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
|
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
|
||||||
|
// FIXME: ONLY FOR TEMPORARY TESTING
|
||||||
|
let unused_frame = unsafe { UnusedPhysFrame::new(frame) };
|
||||||
let flags = Flags::PRESENT | Flags::WRITABLE;
|
let flags = Flags::PRESENT | Flags::WRITABLE;
|
||||||
|
|
||||||
let map_to_result = unsafe { mapper.map_to(page, frame, flags, frame_allocator) };
|
let map_to_result = mapper.map_to(page, unused_frame, flags, frame_allocator);
|
||||||
map_to_result.expect("map_to failed").flush();
|
map_to_result.expect("map_to failed").flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +57,7 @@ pub fn create_example_mapping(
|
|||||||
pub struct EmptyFrameAllocator;
|
pub struct EmptyFrameAllocator;
|
||||||
|
|
||||||
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
||||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
fn allocate_frame(&mut self) -> Option<UnusedPhysFrame> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,7 +82,7 @@ impl BootInfoFrameAllocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over the usable frames specified in the memory map.
|
/// Returns an iterator over the usable frames specified in the memory map.
|
||||||
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
fn usable_frames(&self) -> impl Iterator<Item = UnusedPhysFrame> {
|
||||||
// get usable regions from memory map
|
// get usable regions from memory map
|
||||||
let regions = self.memory_map.iter();
|
let regions = self.memory_map.iter();
|
||||||
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
||||||
@@ -88,12 +91,14 @@ impl BootInfoFrameAllocator {
|
|||||||
// transform to an iterator of frame start addresses
|
// transform to an iterator of frame start addresses
|
||||||
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
||||||
// create `PhysFrame` types from the start addresses
|
// create `PhysFrame` types from the start addresses
|
||||||
frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
|
let frames = frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)));
|
||||||
|
// we know that the frames are really unused
|
||||||
|
frames.map(|f| unsafe { UnusedPhysFrame::new(f) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
||||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
fn allocate_frame(&mut self) -> Option<UnusedPhysFrame> {
|
||||||
let frame = self.usable_frames().nth(self.next);
|
let frame = self.usable_frames().nth(self.next);
|
||||||
self.next += 1;
|
self.next += 1;
|
||||||
frame
|
frame
|
||||||
|
|||||||
Reference in New Issue
Block a user