[PATCH v3 13/17] target/loongarch: Use correct address when flush tlb

Bibo Mao posted 17 patches 3 months, 3 weeks ago
Maintainers: Song Gao <gaosong@loongson.cn>, Bibo Mao <maobibo@loongson.cn>, Jiaxun Yang <jiaxun.yang@flygoat.com>
There is a newer version of this series
[PATCH v3 13/17] target/loongarch: Use correct address when flush tlb
Posted by Bibo Mao 3 months, 3 weeks ago
With tlb_flush_range_by_mmuidx(), the virtual address is 64 bit.
However on LoongArch TLB emulation system, virtual address is
48 bit. It is necessary to convert 48 bit address to 64 bit when
flush tlb, also fix address calculation issue with odd page.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
 target/loongarch/tcg/tlb_helper.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_helper.c
index 715c5a20da..61cc19610e 100644
--- a/target/loongarch/tcg/tlb_helper.c
+++ b/target/loongarch/tcg/tlb_helper.c
@@ -96,6 +96,15 @@ static void raise_mmu_exception(CPULoongArchState *env, target_ulong address,
    }
 }
 
+/* Convert 48 bit virtual address from LoongArch TLB to 64 bit VA */
+static inline target_ulong __vaddr(target_ulong addr)
+{
+    target_ulong high;
+
+    high = -(addr >> (TARGET_VIRT_ADDR_SPACE_BITS - 1));
+    return addr + (high << TARGET_VIRT_ADDR_SPACE_BITS);
+}
+
 static void invalidate_tlb_entry(CPULoongArchState *env, int index)
 {
     target_ulong addr, mask, pagesize;
@@ -115,16 +124,15 @@ static void invalidate_tlb_entry(CPULoongArchState *env, int index)
     tlb_ps = FIELD_EX64(tlb->tlb_misc, TLB_MISC, PS);
     pagesize = MAKE_64BIT_MASK(tlb_ps, 1);
     mask = MAKE_64BIT_MASK(0, tlb_ps + 1);
+    addr = __vaddr((tlb_vppn << R_TLB_MISC_VPPN_SHIFT) & ~mask);
 
     if (tlb_v0) {
-        addr = (tlb_vppn << R_TLB_MISC_VPPN_SHIFT) & ~mask;    /* even */
         tlb_flush_range_by_mmuidx(env_cpu(env), addr, pagesize,
                                   mmu_idx, TARGET_LONG_BITS);
     }
 
     if (tlb_v1) {
-        addr = (tlb_vppn << R_TLB_MISC_VPPN_SHIFT) & pagesize;    /* odd */
-        tlb_flush_range_by_mmuidx(env_cpu(env), addr, pagesize,
+        tlb_flush_range_by_mmuidx(env_cpu(env), addr + pagesize, pagesize,
                                   mmu_idx, TARGET_LONG_BITS);
     }
 }
-- 
2.39.3
Re: [PATCH v3 13/17] target/loongarch: Use correct address when flush tlb
Posted by Richard Henderson 3 months, 3 weeks ago
On 7/24/25 15:37, Bibo Mao wrote:
> With tlb_flush_range_by_mmuidx(), the virtual address is 64 bit.
> However on LoongArch TLB emulation system, virtual address is
> 48 bit. It is necessary to convert 48 bit address to 64 bit when
> flush tlb, also fix address calculation issue with odd page.
> 
> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
> ---
>   target/loongarch/tcg/tlb_helper.c | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_helper.c
> index 715c5a20da..61cc19610e 100644
> --- a/target/loongarch/tcg/tlb_helper.c
> +++ b/target/loongarch/tcg/tlb_helper.c
> @@ -96,6 +96,15 @@ static void raise_mmu_exception(CPULoongArchState *env, target_ulong address,
>      }
>   }
>   
> +/* Convert 48 bit virtual address from LoongArch TLB to 64 bit VA */
> +static inline target_ulong __vaddr(target_ulong addr)
> +{
> +    target_ulong high;
> +
> +    high = -(addr >> (TARGET_VIRT_ADDR_SPACE_BITS - 1));
> +    return addr + (high << TARGET_VIRT_ADDR_SPACE_BITS);
> +}

Don't use __ symbols.
Also, this function is

     sextract64(addr, 0, TARGET_VIRT_ADDR_SPACE_BITS - 1)


r~
Re: [PATCH v3 13/17] target/loongarch: Use correct address when flush tlb
Posted by Bibo Mao 3 months, 2 weeks ago

On 2025/7/26 上午9:45, Richard Henderson wrote:
> On 7/24/25 15:37, Bibo Mao wrote:
>> With tlb_flush_range_by_mmuidx(), the virtual address is 64 bit.
>> However on LoongArch TLB emulation system, virtual address is
>> 48 bit. It is necessary to convert 48 bit address to 64 bit when
>> flush tlb, also fix address calculation issue with odd page.
>>
>> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
>> ---
>>   target/loongarch/tcg/tlb_helper.c | 14 +++++++++++---
>>   1 file changed, 11 insertions(+), 3 deletions(-)
>>
>> diff --git a/target/loongarch/tcg/tlb_helper.c 
>> b/target/loongarch/tcg/tlb_helper.c
>> index 715c5a20da..61cc19610e 100644
>> --- a/target/loongarch/tcg/tlb_helper.c
>> +++ b/target/loongarch/tcg/tlb_helper.c
>> @@ -96,6 +96,15 @@ static void raise_mmu_exception(CPULoongArchState 
>> *env, target_ulong address,
>>      }
>>   }
>> +/* Convert 48 bit virtual address from LoongArch TLB to 64 bit VA */
>> +static inline target_ulong __vaddr(target_ulong addr)
>> +{
>> +    target_ulong high;
>> +
>> +    high = -(addr >> (TARGET_VIRT_ADDR_SPACE_BITS - 1));
>> +    return addr + (high << TARGET_VIRT_ADDR_SPACE_BITS);
>> +}
> 
> Don't use __ symbols.
> Also, this function is
> 
>      sextract64(addr, 0, TARGET_VIRT_ADDR_SPACE_BITS - 1)
yeap, sextract64 is simpler and effective. How about 
loongarch_vppn_to_vaddr compared with __vaddr about function name?

Regards
Bibo Mao
> 
> 
> r~


Re: [PATCH v3 13/17] target/loongarch: Use correct address when flush tlb
Posted by Richard Henderson 3 months, 2 weeks ago
On 7/27/25 17:22, Bibo Mao wrote:
>>> +static inline target_ulong __vaddr(target_ulong addr)
>>> +{
>>> +    target_ulong high;
>>> +
>>> +    high = -(addr >> (TARGET_VIRT_ADDR_SPACE_BITS - 1));
>>> +    return addr + (high << TARGET_VIRT_ADDR_SPACE_BITS);
>>> +}
>>
>> Don't use __ symbols.
>> Also, this function is
>>
>>      sextract64(addr, 0, TARGET_VIRT_ADDR_SPACE_BITS - 1)
> yeap, sextract64 is simpler and effective. How about loongarch_vppn_to_vaddr compared with 
> __vaddr about function name?

There was one use of __vaddr in this patch.  Do you need a separate helper function at 
all?  Just use sextract64 directly within invalidate_tlb_entry.


r~

Re: [PATCH v3 13/17] target/loongarch: Use correct address when flush tlb
Posted by Bibo Mao 3 months, 2 weeks ago

On 2025/7/28 下午1:09, Richard Henderson wrote:
> On 7/27/25 17:22, Bibo Mao wrote:
>>>> +static inline target_ulong __vaddr(target_ulong addr)
>>>> +{
>>>> +    target_ulong high;
>>>> +
>>>> +    high = -(addr >> (TARGET_VIRT_ADDR_SPACE_BITS - 1));
>>>> +    return addr + (high << TARGET_VIRT_ADDR_SPACE_BITS);
>>>> +}
>>>
>>> Don't use __ symbols.
>>> Also, this function is
>>>
>>>      sextract64(addr, 0, TARGET_VIRT_ADDR_SPACE_BITS - 1)
>> yeap, sextract64 is simpler and effective. How about 
>> loongarch_vppn_to_vaddr compared with __vaddr about function name?
> 
> There was one use of __vaddr in this patch.  Do you need a separate 
> helper function at all?  Just use sextract64 directly within 
> invalidate_tlb_entry.
ok, will remove it directly.
And thanks for your kindly guidance.

Regards
Bibo Mao
> 
> 
> r~