From 6f6925aa36611f966b1a58ae8eb03d2dd8197c64 Mon Sep 17 00:00:00 2001 From: Bibo Mao Date: Wed, 27 Aug 2025 12:11:02 +0800 Subject: [PATCH] target/loongarch: Move last PTE lookup into page table walker loop The last PTE lookup sentence is much similiar with the whole page table walker loop, move it into the whole loop. Signed-off-by: Bibo Mao Reviewed-by: Song Gao --- target/loongarch/cpu_helper.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/target/loongarch/cpu_helper.c b/target/loongarch/cpu_helper.c index 8388bfb782..520fd74b2b 100644 --- a/target/loongarch/cpu_helper.c +++ b/target/loongarch/cpu_helper.c @@ -124,7 +124,7 @@ static TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context, } base &= TARGET_PHYS_MASK; - for (level = 4; level > 0; level--) { + for (level = 4; level >= 0; level--) { get_dir_base_width(env, &dir_base, &dir_width, level); if (dir_width == 0) { @@ -135,17 +135,19 @@ static TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context, index = (address >> dir_base) & ((1 << dir_width) - 1); phys = base | index << 3; base = ldq_phys(cs->as, phys); - if (FIELD_EX64(base, TLBENTRY, HUGE)) { - /* base is a huge pte */ - break; - } else { - /* Discard high bits with page directory table */ - base &= TARGET_PHYS_MASK; + if (level) { + if (FIELD_EX64(base, TLBENTRY, HUGE)) { + /* base is a huge pte */ + break; + } else { + /* Discard high bits with page directory table */ + base &= TARGET_PHYS_MASK; + } } } /* pte */ - if (FIELD_EX64(base, TLBENTRY, HUGE)) { + if (level > 0) { /* Huge Page. base is pte */ base = FIELD_DP64(base, TLBENTRY, LEVEL, 0); base = FIELD_DP64(base, TLBENTRY, HUGE, 0); @@ -153,12 +155,6 @@ static TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context, base = FIELD_DP64(base, TLBENTRY, HGLOBAL, 0); base = FIELD_DP64(base, TLBENTRY, G, 1); } - } else { - /* Normal Page. base points to pte */ - get_dir_base_width(env, &dir_base, &dir_width, 0); - index = (address >> dir_base) & ((1 << dir_width) - 1); - phys = base | index << 3; - base = ldq_phys(cs->as, phys); } context->ps = dir_base; -- 2.47.3