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