1 use system_error::SystemError; 2 3 use crate::mm::{ 4 allocator::page_frame::{FrameAllocator, PageFrameCount, PageFrameUsage}, 5 page::PageFlags, 6 MemoryManagementArch, PhysAddr, VirtAddr, 7 }; 8 9 pub mod bump; 10 pub(super) mod init; 11 12 pub type PageMapper = crate::mm::page::PageMapper<RiscV64MMArch, LockedFrameAllocator>; 13 14 /// RiscV64的内存管理架构结构体(sv39) 15 #[derive(Debug, Clone, Copy, Hash)] 16 pub struct RiscV64MMArch; 17 18 impl MemoryManagementArch for RiscV64MMArch { 19 const PAGE_SHIFT: usize = 12; 20 21 const PAGE_ENTRY_SHIFT: usize = 9; 22 23 /// sv39分页只有三级 24 const PAGE_LEVELS: usize = 3; 25 26 const ENTRY_ADDRESS_SHIFT: usize = 39; 27 28 const ENTRY_FLAG_DEFAULT_PAGE: usize = Self::ENTRY_FLAG_PRESENT; 29 30 const ENTRY_FLAG_DEFAULT_TABLE: usize = Self::ENTRY_FLAG_PRESENT; 31 32 const ENTRY_FLAG_PRESENT: usize = 1 << 0; 33 34 const ENTRY_FLAG_READONLY: usize = 1 << 1; 35 36 const ENTRY_FLAG_READWRITE: usize = (1 << 2) | (1 << 1); 37 38 const ENTRY_FLAG_USER: usize = (1 << 4); 39 40 const ENTRY_FLAG_WRITE_THROUGH: usize = (2 << 61); 41 42 const ENTRY_FLAG_CACHE_DISABLE: usize = (2 << 61); 43 44 const ENTRY_FLAG_NO_EXEC: usize = 0; 45 46 const ENTRY_FLAG_EXEC: usize = (1 << 3); 47 48 const PHYS_OFFSET: usize = 0xffff_ffc0_0000_0000; 49 50 const USER_END_VADDR: crate::mm::VirtAddr = VirtAddr::new(0x0000_003f_ffff_ffff); 51 52 const USER_BRK_START: crate::mm::VirtAddr = VirtAddr::new(0x0000_001f_ffff_ffff); 53 54 const USER_STACK_START: crate::mm::VirtAddr = VirtAddr::new(0x0000_001f_ffa0_0000); 55 56 /// 在距离sv39的顶端还有1G的位置,设置为FIXMAP的起始地址 57 const FIXMAP_START_VADDR: VirtAddr = VirtAddr::new(0xffff_ffff_8000_0000); 58 /// 设置1MB的fixmap空间 59 const FIXMAP_SIZE: usize = 256 * 4096; 60 61 unsafe fn init() { 62 todo!() 63 } 64 65 unsafe fn invalidate_page(address: crate::mm::VirtAddr) { 66 todo!() 67 } 68 69 unsafe fn invalidate_all() { 70 todo!() 71 } 72 73 unsafe fn table(table_kind: crate::mm::PageTableKind) -> crate::mm::PhysAddr { 74 todo!() 75 } 76 77 unsafe fn set_table(table_kind: crate::mm::PageTableKind, table: crate::mm::PhysAddr) { 78 todo!() 79 } 80 81 fn virt_is_valid(virt: crate::mm::VirtAddr) -> bool { 82 todo!() 83 } 84 85 fn initial_page_table() -> crate::mm::PhysAddr { 86 todo!() 87 } 88 89 fn setup_new_usermapper() -> Result<crate::mm::ucontext::UserMapper, SystemError> { 90 todo!() 91 } 92 } 93 94 /// 获取内核地址默认的页面标志 95 pub unsafe fn kernel_page_flags<A: MemoryManagementArch>(virt: VirtAddr) -> PageFlags<A> { 96 unimplemented!("riscv64::kernel_page_flags") 97 } 98 99 /// 全局的页帧分配器 100 #[derive(Debug, Clone, Copy, Hash)] 101 pub struct LockedFrameAllocator; 102 103 impl FrameAllocator for LockedFrameAllocator { 104 unsafe fn allocate(&mut self, count: PageFrameCount) -> Option<(PhysAddr, PageFrameCount)> { 105 unimplemented!("RiscV64 LockedFrameAllocator::allocate") 106 } 107 108 unsafe fn free(&mut self, address: crate::mm::PhysAddr, count: PageFrameCount) { 109 assert!(count.data().is_power_of_two()); 110 unimplemented!("RiscV64 LockedFrameAllocator::free") 111 } 112 113 unsafe fn usage(&self) -> PageFrameUsage { 114 unimplemented!("RiscV64 LockedFrameAllocator::usage") 115 } 116 } 117