Skip to main content

os/memory/
mod.rs

1/*
2 * ============================================
3 * RISC-V 内存管理模块 façade
4 * ============================================
5 * 组件化边界:
6 * - ahrightos-memory crate: 可移植地址、页表、物理帧分配器类型
7 * - 本模块: RISC-V/QEMU 相关初始化、页表遍历和地址空间 glue
8 * ============================================
9 */
10
11pub mod address_space;
12pub mod paging;
13
14pub use address_space::{create_kernel_address_space, AddressSpace, MemoryArea, MemoryAreaType};
15pub use ahrightos_memory::*;
16pub use paging::{
17    map_page, map_page_verbose, translate_addr as translate_addr_current, unmap_page,
18    walk_page_table, walk_page_table_verbose,
19};
20
21/// 初始化内存管理。
22///
23/// 可移植的 `MemoryManager` 和 `SimpleFrameAllocator` 来自
24/// `ahrightos-memory` crate;这里保留 QEMU virt 的物理内存布局和启动日志。
25pub fn init(kernel_end: usize) -> MemoryManager {
26    const MEMORY_START: usize = 0x8000_0000;
27    const MEMORY_SIZE: usize = 128 * 1024 * 1024;
28    let memory_end = MEMORY_START + MEMORY_SIZE;
29
30    crate::serial_println!("[MEMORY] Initializing memory management");
31    crate::serial_println!("[MEMORY] Kernel end: {:#x}", kernel_end);
32    crate::serial_println!(
33        "[MEMORY] Memory range: {:#x} - {:#x}",
34        MEMORY_START,
35        memory_end
36    );
37
38    crate::serial_println!(
39        "[MEMORY] Frame allocator initialized: {:#x} - {:#x}",
40        (kernel_end + PAGE_SIZE - 1) & !(PAGE_SIZE - 1),
41        memory_end
42    );
43
44    MemoryManager::new(kernel_end, memory_end)
45}
46
47pub fn create_example_mapping(
48    page: Page,
49    frame_allocator: &mut SimpleFrameAllocator,
50) -> Result<(), &'static str> {
51    let frame = frame_allocator
52        .allocate()
53        .ok_or("Failed to allocate frame")?;
54
55    crate::serial_println!(
56        "[MEMORY] Example mapping: {:?} -> {:?}",
57        page.start_address(),
58        frame.start_address()
59    );
60
61    Ok(())
62}
63
64pub fn translate_addr(vaddr: VirtAddr) -> Option<PhysAddr> {
65    use riscv::register::satp;
66
67    let satp_value = satp::read();
68    let ppn = satp_value.ppn();
69    let root_paddr = PhysAddr::new(ppn << 12);
70
71    walk_page_table(root_paddr, vaddr)
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test_case]
79    fn test_virt_addr_vpn() {
80        let addr = VirtAddr::new(0x1234_5678);
81        assert_eq!(addr.page_offset(), 0x678);
82    }
83
84    #[test_case]
85    fn test_frame_alloc_dealloc_reuse() {
86        let mut allocator = SimpleFrameAllocator::new(0x8020_0000, 0x8030_0000);
87        let f1 = allocator.allocate().expect("first alloc");
88        let f2 = allocator.allocate().expect("second alloc");
89        assert_ne!(f1.start_address().as_usize(), f2.start_address().as_usize());
90
91        let f1_addr = f1.start_address().as_usize();
92        allocator.deallocate(f1);
93        let f3 = allocator.allocate().expect("reuse freed frame");
94        assert_eq!(f3.start_address().as_usize(), f1_addr);
95    }
96
97    #[test_case]
98    fn test_frame_alloc_exhaustion() {
99        let mut allocator = SimpleFrameAllocator::new(0x8020_0000, 0x8020_1000);
100        assert!(allocator.allocate().is_some());
101        assert!(allocator.allocate().is_none());
102    }
103}