From fb149aa8c751f09f3ce02e0c01b78e845c4f8100 Mon Sep 17 00:00:00 2001 From: zhangjing Date: Thu, 3 Apr 2025 10:30:30 +0800 Subject: [PATCH 1/2] [libcpu][risc-v] fix:only map the 1GB space where the original code segment is located --- libcpu/risc-v/common64/mmu.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libcpu/risc-v/common64/mmu.c b/libcpu/risc-v/common64/mmu.c index 6cab811eca7..a260a06e581 100644 --- a/libcpu/risc-v/common64/mmu.c +++ b/libcpu/risc-v/common64/mmu.c @@ -691,11 +691,10 @@ void rt_hw_mem_setup_early(void) * identical mapping, * PC are still at lower region before relocating to high memory */ - for (size_t i = 0; i < __SIZE(PPN0_BIT); i++) - { - early_pgtbl[i] = COMBINEPTE(ps, MMU_MAP_EARLY); - ps += L1_PAGE_SIZE; - } + rt_ubase_t pg_idx ; + ps = (rt_ubase_t)symb_pc & (~(L1_PAGE_SIZE - 1)); + pg_idx = GET_L1(ps); + early_pgtbl[pg_idx] = COMBINEPTE(ps, MMU_MAP_EARLY); /* relocate text region */ __asm__ volatile("la %0, _start\n" : "=r"(ps)); From 84435a4da9949fa8ec87d245aa4b91b3666990cc Mon Sep 17 00:00:00 2001 From: zhangjing Date: Wed, 28 May 2025 09:38:54 +0000 Subject: [PATCH 2/2] [libcpu][risc-v]:add comment for the round down of symb_pc --- libcpu/risc-v/common64/mmu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libcpu/risc-v/common64/mmu.c b/libcpu/risc-v/common64/mmu.c index a260a06e581..19c7f7c0195 100644 --- a/libcpu/risc-v/common64/mmu.c +++ b/libcpu/risc-v/common64/mmu.c @@ -692,6 +692,9 @@ void rt_hw_mem_setup_early(void) * PC are still at lower region before relocating to high memory */ rt_ubase_t pg_idx ; + /* Round down symb_pc to L1_PAGE_SIZE boundary to ensure proper page alignment. + * This is necessary because MMU operations work with page-aligned addresses, and + * make sure all the text region is mapped.*/ ps = (rt_ubase_t)symb_pc & (~(L1_PAGE_SIZE - 1)); pg_idx = GET_L1(ps); early_pgtbl[pg_idx] = COMBINEPTE(ps, MMU_MAP_EARLY);