xref: /DragonOS/kernel/src/arch/riscv64/mm/init.rs (revision d46c6d27941a26de14f55a2bbf956219bcc70871)
1 use system_error::SystemError;
2 
3 use crate::{
4     arch::{
5         mm::{KERNEL_BEGIN_PA, KERNEL_BEGIN_VA, KERNEL_END_PA, KERNEL_END_VA},
6         MMArch,
7     },
8     kdebug,
9     mm::{
10         allocator::page_frame::PageFrameCount,
11         no_init::{pseudo_map_phys, EARLY_IOREMAP_PAGES},
12         page::{PageEntry, PageMapper, PageTable},
13         MemoryManagementArch, PageTableKind, PhysAddr, VirtAddr,
14     },
15 };
16 
17 #[inline(never)]
18 pub fn mm_early_init() {
19     unsafe { init_kernel_addr() };
20     // unsafe { map_initial_page_table_linearly() };
21 }
22 
23 unsafe fn init_kernel_addr() {
24     extern "C" {
25         /// 内核起始label
26         fn boot_text_start_pa();
27         /// 内核结束位置的label
28         fn _end();
29 
30         fn _start();
31 
32         /// 内核start标签被加载到的物理地址
33         fn __initial_start_load_paddr();
34     }
35     let initial_start_load_pa = *(__initial_start_load_paddr as usize as *const usize);
36     let offset = _start as usize - boot_text_start_pa as usize;
37     let start_pa = initial_start_load_pa - offset;
38 
39     let offset2 = _end as usize - boot_text_start_pa as usize;
40     let end_pa = start_pa + offset2;
41 
42     KERNEL_BEGIN_PA = PhysAddr::new(start_pa);
43     KERNEL_END_PA = PhysAddr::new(end_pa);
44 
45     KERNEL_BEGIN_VA = VirtAddr::new(boot_text_start_pa as usize);
46     KERNEL_END_VA = VirtAddr::new(_end as usize);
47 
48     kdebug!(
49         "init_kernel_addr: \n\tKERNEL_BEGIN_PA: {KERNEL_BEGIN_PA:?}
50         \tKERNEL_END_PA: {KERNEL_END_PA:?}
51         \tKERNEL_BEGIN_VA: {KERNEL_BEGIN_VA:?}
52         \tKERNEL_END_VA: {KERNEL_END_VA:?}
53     "
54     );
55 }
56