我們也可以修改RISC-V Linux的內(nèi)核源碼,解除2M對齊的限制。我們只需要在setup_vm()
函數(shù)中,將原來的 二級頁表改為三級頁表 ,這樣kernel入口地址只需要4K對齊,因此就能將kernel往前挪,從而利用前面的內(nèi)存。
修改代碼
路徑: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的頁表映射由二級頁表改為三級頁表:
假設(shè)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預(yù)留512KB,這樣RISC-V Linux啟動之后,可用物理內(nèi)存就會增加。
-
內(nèi)核
+關(guān)注
關(guān)注
3文章
1360瀏覽量
40185 -
Linux
+關(guān)注
關(guān)注
87文章
11207瀏覽量
208721 -
內(nèi)存
+關(guān)注
關(guān)注
8文章
2966瀏覽量
73815
發(fā)布評論請先 登錄
相關(guān)推薦
評論