Paging Implementation
This commit is contained in:
parent
c3e1897c23
commit
042405c597
@ -13,7 +13,7 @@ edition = "2021"
|
||||
# panic = "abort" # 禁用 panic 时的栈展开
|
||||
|
||||
[dependencies]
|
||||
bootloader = "0.9.20"
|
||||
bootloader = { version = "0.9.20", features = ["map_physical_memory"] }
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.2"
|
||||
x86_64 = "0.14.7"
|
||||
|
10
src/lib.rs
10
src/lib.rs
@ -10,6 +10,7 @@ pub mod serial;
|
||||
pub mod vga_buffer;
|
||||
pub mod interrupts;
|
||||
pub mod gdt;
|
||||
pub mod memory;
|
||||
|
||||
pub trait Testable {
|
||||
fn run(&self) -> ();
|
||||
@ -42,8 +43,13 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
|
||||
#[cfg(test)]
|
||||
entry_point!(test_kernel_main);
|
||||
|
||||
#[cfg(test)]
|
||||
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
||||
init();
|
||||
test_main();
|
||||
hlt_loop();
|
||||
|
29
src/main.rs
29
src/main.rs
@ -6,25 +6,26 @@
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use anos::println;
|
||||
use bootloader::{BootInfo, entry_point};
|
||||
|
||||
entry_point!(kernel_main);
|
||||
|
||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
use anos::memory::{ self, BootInfoFrameAllocator };
|
||||
use x86_64::{ structures::paging::Page, VirtAddr };
|
||||
|
||||
#[no_mangle] // 不重整函数名,否则编译器可能会生成名为 _ZN3blog_os4_start7hb173fedf945531caE 的函数
|
||||
// extern "C" 告诉编译器这个函数应当使用 C 语言的调用约定
|
||||
// 函数名为 _start 是因为大多数系统默认用这个名字作为入口点名称
|
||||
// -> ! 代表这是一个发散函数,不允许有任何返回值,因为它不会被任何函数调用,而是将直接被 bootloader 调用
|
||||
pub extern "C" fn _start() -> ! {
|
||||
println!("Hello World{}", "!");
|
||||
|
||||
anos::init();
|
||||
|
||||
let ptr = 0x2031b2 as *mut u32;
|
||||
unsafe { let x = *ptr; }
|
||||
println!("read worked");
|
||||
unsafe { *ptr = 42 };
|
||||
println!("write worked");
|
||||
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||
|
||||
use x86_64::registers::control::Cr3;
|
||||
let (level_4_page_table, _) = Cr3::read();
|
||||
println!("Level 4 page table at: {:?}", level_4_page_table.start_address());
|
||||
let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000));
|
||||
memory::create_example_mapping(page, &mut mapper, &mut frame_allocator);
|
||||
|
||||
let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
|
||||
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e)};
|
||||
|
||||
// 触发一个中断
|
||||
x86_64::instructions::interrupts::int3();
|
||||
|
78
src/memory.rs
Normal file
78
src/memory.rs
Normal file
@ -0,0 +1,78 @@
|
||||
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
||||
use x86_64::{
|
||||
structures::paging::{
|
||||
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
||||
},
|
||||
PhysAddr, VirtAddr,
|
||||
};
|
||||
|
||||
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
|
||||
let level_4_table = active_level_4_table(physical_memory_offset);
|
||||
OffsetPageTable::new(level_4_table, physical_memory_offset)
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
let phys = level_4_table_frame.start_address();
|
||||
let virt = physical_memory_offset + phys.as_u64();
|
||||
let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
|
||||
|
||||
&mut *page_table_ptr
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
pub struct EmptyFrameAllocator;
|
||||
|
||||
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BootInfoFrameAllocator {
|
||||
memory_map: &'static MemoryMap,
|
||||
next: usize,
|
||||
}
|
||||
|
||||
impl BootInfoFrameAllocator {
|
||||
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
|
||||
BootInfoFrameAllocator {
|
||||
memory_map,
|
||||
next: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
||||
let regions = self.memory_map.iter();
|
||||
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
||||
let addr_ranges = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr());
|
||||
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
||||
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