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