xref: /DragonOS/kernel/src/arch/riscv64/mm/mod.rs (revision c75ef4e2126c180bf04c08635ffa5a278619c035)
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的内存管理架构结构体
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     unsafe fn init() {
57         todo!()
58     }
59 
60     unsafe fn invalidate_page(address: crate::mm::VirtAddr) {
61         todo!()
62     }
63 
64     unsafe fn invalidate_all() {
65         todo!()
66     }
67 
68     unsafe fn table(table_kind: crate::mm::PageTableKind) -> crate::mm::PhysAddr {
69         todo!()
70     }
71 
72     unsafe fn set_table(table_kind: crate::mm::PageTableKind, table: crate::mm::PhysAddr) {
73         todo!()
74     }
75 
76     fn virt_is_valid(virt: crate::mm::VirtAddr) -> bool {
77         todo!()
78     }
79 
80     fn initial_page_table() -> crate::mm::PhysAddr {
81         todo!()
82     }
83 
84     fn setup_new_usermapper() -> Result<crate::mm::ucontext::UserMapper, SystemError> {
85         todo!()
86     }
87 }
88 
89 /// 获取内核地址默认的页面标志
90 pub unsafe fn kernel_page_flags<A: MemoryManagementArch>(virt: VirtAddr) -> PageFlags<A> {
91     unimplemented!("riscv64::kernel_page_flags")
92 }
93 
94 /// 全局的页帧分配器
95 #[derive(Debug, Clone, Copy, Hash)]
96 pub struct LockedFrameAllocator;
97 
98 impl FrameAllocator for LockedFrameAllocator {
99     unsafe fn allocate(&mut self, count: PageFrameCount) -> Option<(PhysAddr, PageFrameCount)> {
100         unimplemented!("RiscV64 LockedFrameAllocator::allocate")
101     }
102 
103     unsafe fn free(&mut self, address: crate::mm::PhysAddr, count: PageFrameCount) {
104         assert!(count.data().is_power_of_two());
105         unimplemented!("RiscV64 LockedFrameAllocator::free")
106     }
107 
108     unsafe fn usage(&self) -> PageFrameUsage {
109         unimplemented!("RiscV64 LockedFrameAllocator::usage")
110     }
111 }
112