我们也可以修改RISC-V Linux的内核源码,解除2M对齐的限制。我们只需要在setup_vm()
函数中,将原来的 二级页表改为三级页表 ,这样kernel入口地址只需要4K对齐,因此就能将kernel往前挪,从而利用前面的内存。
路径:arch/riscv/mm/init.c
注释原来的2M对齐检查:
对kernel的前2M页表映射由二级页表改为三级页表:
//新增一个PTE
pte_t trampoline_pte[PTRS_PER_PTE] __page_aligned_bss;
create_pgd_mapping(trampoline_pg_dir,PAGE_OFFSET,
(uintptr_t)trampoline_pmd,PGDIR_SIZE,PAGE_TABLE);
create_pmd_mapping(trampoline_pmd,PAGE_OFFSET,
(uintptr_t)trampoline_pte,PMD_SIZE,PAGE_TABLE);
end_va = PAGE_OFFSET + PMD_SIZE;
for (va = PAGE_OFFSET; va < end_va; va += PAGE_SIZE)
{
create_pte_mapping(trampoline_pte,PAGE_OFFSET,
load_pa + (va - PAGE_OFFSET),
PAGE_SIZE,PAGE_KERNEL_EXEC);
}
对整个kernel的页表映射由二级页表改为三级页表:
假设kernel大小为4M+
//定义三个PTE
pte_t load_sz_pte[PTRS_PER_PTE] __page_aligned_bss;
pte_t load_sz_pte1[PTRS_PER_PTE] __page_aligned_bss;
pte_t load_sz_pte2[PTRS_PER_PTE] __page_aligned_bss;
//=======0-2M======
create_pgd_mapping(early_pg_dir,PAGE_OFFSET,
(uintptr_t)early_pmd,PGDIR_SIZE,PAGE_TABLE);
create_pmd_mapping(early_pmd,PAGE_OFFSET,
(uintptr_t)load_sz_pte,PMD_SIZE,PAGE_TABLE);
end_va = PAGE_OFFSET + PMD_SIZE;
for (va = PAGE_OFFSET; va < end_va; va += PAGE_SIZE)
{
create_pte_mapping(load_sz_pte,PAGE_OFFSET,
load_pa + (va - PAGE_OFFSET),
PAGE_SIZE,PAGE_KERNEL_EXEC);
}
//=======2-4M==========
create_pgd_mapping(early_pg_dir,PAGE_OFFSET + PMD_SIZE,
(uintptr_t)early_pmd,PGDIR_SIZE,PAGE_TABLE);
create_pmd_mapping(early_pmd,PAGE_OFFSET,
(uintptr_t)load_sz_pte1,PMD_SIZE,PAGE_TABLE);
end_va = PAGE_OFFSET + (PMD_SIZE * 2);
for (va = PAGE_OFFSET + PMD_SIZE; va < end_va; va += PAGE_SIZE)
{
create_pte_mapping(load_sz_pte1,va,
load_pa + (va - PAGE_OFFSET),
PAGE_SIZE,PAGE_KERNEL_EXEC);
}
//=======4-6M==========
create_pgd_mapping(early_pg_dir,PAGE_OFFSET + (PMD_SIZE*2),
(uintptr_t)early_pmd,PGDIR_SIZE,PAGE_TABLE);
create_pmd_mapping(early_pmd,PAGE_OFFSET,
(uintptr_t)load_sz_pte2,PMD_SIZE,PAGE_TABLE);
end_va = PAGE_OFFSET + (PMD_SIZE * 3);
for (va = PAGE_OFFSET + (PMD_SIZE*2); va < end_va; va += PAGE_SIZE)
{
create_pte_mapping(load_sz_pte2,va,
load_pa + (va - PAGE_OFFSET),
PAGE_SIZE,PAGE_KERNEL_EXEC);
}
通过以上的代码修改,就能将Kernel入口地址往前挪1.5M,只给opensbi预留512KB,这样RISC-V Linux启动之后,可用物理内存就会增加。
全部0条评论
快来发表一下你的评论吧 !