Get frame alloctaion working
This commit is contained in:
parent
46ce73b1b0
commit
8b3a4b4b9a
@ -59,8 +59,8 @@ extern "x86-interrupt" fn double_fault_handler(sf: &mut InterruptStackFrame, e:
|
||||
panic!("Unrecoverable exception");
|
||||
}
|
||||
|
||||
use x86_64::structures::idt::PageFaultErrorCode;
|
||||
use x86_64::instructions::bochs_breakpoint;
|
||||
use x86_64::structures::idt::PageFaultErrorCode;
|
||||
|
||||
extern "x86-interrupt" fn page_fault_handler(sf: &mut InterruptStackFrame, e: PageFaultErrorCode) {
|
||||
// LLVM bug causing misaligned stacks when error codes are present.
|
||||
|
@ -8,8 +8,6 @@
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
use vga_buffer::print;
|
||||
|
||||
@ -18,7 +16,6 @@ use polling_serial::serial_print;
|
||||
|
||||
use polling_serial::serial_println;
|
||||
|
||||
|
||||
use vga_buffer::println;
|
||||
use x86_64::instructions::bochs_breakpoint;
|
||||
|
||||
@ -76,7 +73,6 @@ pub fn hlt_loop() -> ! {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
|
||||
|
21
src/main.rs
21
src/main.rs
@ -51,18 +51,15 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
#[cfg(test)]
|
||||
test_main();
|
||||
|
||||
|
||||
|
||||
x86_64::instructions::interrupts::int3();
|
||||
|
||||
use dendrobates_tinctoreus_azureus::memory;
|
||||
use x86_64::VirtAddr;
|
||||
use x86_64::structures::paging::{PageTable,MapperAllSizes};
|
||||
|
||||
use x86_64::structures::paging::{MapperAllSizes, PageTable};
|
||||
use x86_64::{structures::paging::Page, VirtAddr};
|
||||
|
||||
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||
// new: initialize a mapper
|
||||
let mapper = unsafe { memory::init(phys_mem_offset) };
|
||||
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
||||
|
||||
let addresses = [
|
||||
// the identity-mapped vga buffer page
|
||||
@ -75,7 +72,6 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
boot_info.physical_memory_offset,
|
||||
];
|
||||
|
||||
|
||||
for &address in &addresses {
|
||||
let virt = VirtAddr::new(address);
|
||||
// new: use the `mapper.translate_addr` method
|
||||
@ -83,6 +79,17 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
serial_println!("{:?} -> {:?}", virt, phys);
|
||||
}
|
||||
|
||||
let mut frame_allocator =
|
||||
unsafe { memory::BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||
|
||||
// map an unused page
|
||||
let page = Page::containing_address(VirtAddr::new(0));
|
||||
memory::create_example_mapping(page, &mut mapper, &mut frame_allocator);
|
||||
|
||||
// write the string `New!` to the screen through the new mapping
|
||||
let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
|
||||
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e) };
|
||||
|
||||
serial_println!("Preparing nasty fault...");
|
||||
unsafe {
|
||||
*(0xdead_beef as *mut u64) = 42;
|
||||
|
@ -1,6 +1,12 @@
|
||||
use x86_64::{
|
||||
structures::paging::{
|
||||
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
||||
},
|
||||
PhysAddr, VirtAddr,
|
||||
};
|
||||
|
||||
use x86_64::{structures::paging::{PageTable,OffsetPageTable}, VirtAddr, PhysAddr};
|
||||
|
||||
use bootloader::bootinfo::MemoryMap;
|
||||
use bootloader::bootinfo::MemoryRegionType;
|
||||
|
||||
/// Initialize a new OffsetPageTable.
|
||||
///
|
||||
@ -18,9 +24,7 @@ pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static>
|
||||
/// complete physical memory is mapped to virtual memory at the passed
|
||||
/// `physical_memory_offset`. Also, this function must be only called once
|
||||
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
||||
unsafe fn active_level_4_table(physical_memory_offset: VirtAddr)
|
||||
-> &'static mut PageTable
|
||||
{
|
||||
unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
|
||||
use x86_64::registers::control::Cr3;
|
||||
|
||||
let (level_4_table_frame, _) = Cr3::read();
|
||||
@ -31,3 +35,57 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr)
|
||||
|
||||
&mut *page_table_ptr // unsafe
|
||||
}
|
||||
|
||||
/// Creates an example mapping for the given page to frame `0xb8000`.
|
||||
pub fn create_example_mapping(
|
||||
page: Page,
|
||||
mapper: &mut OffsetPageTable,
|
||||
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
|
||||
) {
|
||||
use x86_64::structures::paging::PageTableFlags as Flags;
|
||||
|
||||
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
|
||||
let flags = Flags::PRESENT | Flags::WRITABLE;
|
||||
|
||||
let map_to_result = unsafe { mapper.map_to(page, frame, flags, frame_allocator) };
|
||||
map_to_result.expect("map_to failed").flush();
|
||||
}
|
||||
|
||||
/// A FrameAllocator that returns usable frames from the bootloader's memory map.
|
||||
pub struct BootInfoFrameAllocator {
|
||||
memory_map: &'static MemoryMap,
|
||||
next: usize,
|
||||
}
|
||||
|
||||
impl BootInfoFrameAllocator {
|
||||
/// Create a FrameAllocator from the passed memory map.
|
||||
///
|
||||
/// This function is unsafe because the caller must guarantee that the passed
|
||||
/// memory map is valid. The main requirement is that all frames that are marked
|
||||
/// as `USABLE` in it are really unused.
|
||||
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
|
||||
BootInfoFrameAllocator {
|
||||
memory_map,
|
||||
next: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
||||
// get usable regions from memory map
|
||||
let regions = self.memory_map.iter();
|
||||
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
||||
// map each region to its address range
|
||||
let addr_ranges = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr());
|
||||
// transform to an iterator of frame start addresses
|
||||
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
||||
// create `PhysFrame` types from the start addresses
|
||||
frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
|
||||
}
|
||||
}
|
||||
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||
let frame = self.usable_frames().nth(self.next);
|
||||
self.next += 1;
|
||||
frame
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user