target/riscv32: Fix masking of physical address
PATCH 1/1 target/riscv32: Fix masking of physical address
C doesn't extend the sign bit for unsigned types since there isn't a sign bit to extend. This means a promotion of a u32 to a u64 results in the upper 32 bits of the u64 being zero. If that result is then used as a mask on another u64 the upper 32 bits will be cleared. rv32 physical addresses may be up to 34 bits wide, so we don't want to clear the high bits while page aligning the address. The fix is to revert to using target_long, since a signed type will get extended.
--- target/riscv/cpu_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/cpu_helper.c +1 -1
@@ -1323,7 +1323,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,int ret =TRANSLATE_FAIL ;int mode =mmuidx_priv (mmu_idx);/* default TLB page size */ -target_ulong tlb_size =TARGET_PAGE_SIZE ; +target_long tlb_size =TARGET_PAGE_SIZE ; env->guest_phys_fault_addr =0 ;
Signature · 1 line
2.45.2
Quoted 19 lines · Andrew Jones
C doesn't extend the sign bit for unsigned types since there isn't a sign bit to extend. This means a promotion of a u32 to a u64 results in the upper 32 bits of the u64 being zero. If that result is then used as a mask on another u64 the upper 32 bits will be cleared. rv32 physical addresses may be up to 34 bits wide, so we don't want to clear the high bits while page aligning the address. The fix is to revert to using
target_long, since a signed type will get extended.Fixes af3fc195e3c8 ("target/riscv: Change the TLB page size depends on PMP entries.")Signed-off-by Andrew Jones <[email protected]>--- target/riscv/cpu_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/cpu_helper.c +0
-0
target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1326
Quoted 5 lines
target/riscv/cpu_helper.c +1
-1@@ -1323,7 +1323,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,int ret =TRANSLATE_FAIL ;int mode =mmuidx_priv (mmu_idx);/* default TLB page size */ -target_ulong tlb_size =TARGET_PAGE_SIZE ; +target_long tlb_size =TARGET_PAGE_SIZE ;
If rv32 physical addresses are 34 bits, then you probably didn't want target_*long at all.
r~
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Andrew Jones
C doesn't extend the sign bit for unsigned types since there isn't a sign bit to extend. This means a promotion of a u32 to a u64 results in the upper 32 bits of the u64 being zero. If that result is then used as a mask on another u64 the upper 32 bits will be cleared. rv32 physical addresses may be up to 34 bits wide, so we don't want to clear the high bits while page aligning the address. The fix is to revert to using
target_long, since a signed type will get extended.Fixes af3fc195e3c8 ("target/riscv: Change the TLB page size depends on PMP entries.")Signed-off-by Andrew Jones <[email protected]>--- target/riscv/cpu_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1326
Quoted 10 lines
target/riscv/cpu_helper.c +1
-1@@ -1323,7 +1323,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,int ret =TRANSLATE_FAIL ;int mode =mmuidx_priv (mmu_idx);/* default TLB page size */ -target_ulong tlb_size =TARGET_PAGE_SIZE ; +target_long tlb_size =TARGET_PAGE_SIZE ;If rv32 physical addresses are 34 bits, then you probably didn't want target_*long at all.
Yes, just using hwaddr for everything that only touches physical addresses would probably be best, but, ifaict, it's pretty common to use target_long for masks used on both virtual and physical addresses (TARGET_PAGE_MASK, for example). This 'tlb_size' variable is used on both as well.
Thanks,
drew
Quoted 29 lines · Andrew Jones
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Andrew Jones
C doesn't extend the sign bit for unsigned types since there isn't a sign bit to extend. This means a promotion of a u32 to a u64 results in the upper 32 bits of the u64 being zero. If that result is then used as a mask on another u64 the upper 32 bits will be cleared. rv32 physical addresses may be up to 34 bits wide, so we don't want to clear the high bits while page aligning the address. The fix is to revert to using
target_long, since a signed type will get extended.Fixes af3fc195e3c8 ("target/riscv: Change the TLB page size depends on PMP entries.")Signed-off-by Andrew Jones <[email protected]>--- target/riscv/cpu_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 12 lines
target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1326
Quoted 10 lines
target/riscv/cpu_helper.c +1
-1@@ -1323,7 +1323,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,int ret =TRANSLATE_FAIL ;int mode =mmuidx_priv (mmu_idx);/* default TLB page size */ -target_ulong tlb_size =TARGET_PAGE_SIZE ; +target_long tlb_size =TARGET_PAGE_SIZE ;If rv32 physical addresses are 34 bits, then you probably didn't want target_*long at all.
Quoted 4 lines
Yes, just using hwaddr for everything that only touches physical addresses would probably be best, but, ifaict, it's pretty common to use
target_longfor masks used on both virtual and physical addresses (TARGET_PAGE_MASK, for example). This 'tlb_size' variable is used on both as well.
Then maybe you want vaddr ("exec/vaddr.h"):
/**
* vaddr:
* Type wide enough to contain any #target_ulong virtual address. */
Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Andrew Jones
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Andrew Jones
C doesn't extend the sign bit for unsigned types since there isn't a sign bit to extend. This means a promotion of a u32 to a u64 results in the upper 32 bits of the u64 being zero. If that result is then used as a mask on another u64 the upper 32 bits will be cleared. rv32 physical addresses may be up to 34 bits wide, so we don't want to clear the high bits while page aligning the address. The fix is to revert to using
target_long, since a signed type will get extended.Fixes af3fc195e3c8 ("target/riscv: Change the TLB page size depends on PMP entries.")Signed-off-by Andrew Jones <[email protected]>--- target/riscv/cpu_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 12 lines
target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1326
Quoted 10 lines
target/riscv/cpu_helper.c +1
-1@@ -1323,7 +1323,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,int ret =TRANSLATE_FAIL ;int mode =mmuidx_priv (mmu_idx);/* default TLB page size */ -target_ulong tlb_size =TARGET_PAGE_SIZE ; +target_long tlb_size =TARGET_PAGE_SIZE ;If rv32 physical addresses are 34 bits, then you probably didn't want target_*long at all.
Quoted 4 lines
Yes, just using hwaddr for everything that only touches physical addresses would probably be best, but, ifaict, it's pretty common to use
target_longfor masks used on both virtual and physical addresses (TARGET_PAGE_MASK, for example). This 'tlb_size' variable is used on both as well.
Quoted 7 lines
Then maybe you want vaddr ("
exec/vaddr.h"):/**
* vaddr:
* Type wide enough to contain any #target_ulong virtual address. */
I think hwaddr would fit better in this case since riscv32 virtual addresses are 32-bit, but I see vaddr is a u64, so it would work too. I personally don't mind changing the type of tlb_size to hwaddr, but I went with target_long in this patch since that's what it was originally and masking with a signed long mask appears to be a common pattern in QEMU.
Thanks,
drew
Quoted 45 lines · Andrew Jones
Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Andrew Jones
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Andrew Jones
C doesn't extend the sign bit for unsigned types since there isn't a sign bit to extend. This means a promotion of a u32 to a u64 results in the upper 32 bits of the u64 being zero. If that result is then used as a mask on another u64 the upper 32 bits will be cleared. rv32 physical addresses may be up to 34 bits wide, so we don't want to clear the high bits while page aligning the address. The fix is to revert to using
target_long, since a signed type will get extended.Fixes af3fc195e3c8 ("target/riscv: Change the TLB page size depends on PMP entries.")Signed-off-by Andrew Jones <[email protected]>--- target/riscv/cpu_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 12 lines
target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1326
Quoted 10 lines
target/riscv/cpu_helper.c +1
-1@@ -1323,7 +1323,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,int ret =TRANSLATE_FAIL ;int mode =mmuidx_priv (mmu_idx);/* default TLB page size */ -target_ulong tlb_size =TARGET_PAGE_SIZE ; +target_long tlb_size =TARGET_PAGE_SIZE ;If rv32 physical addresses are 34 bits, then you probably didn't want target_*long at all.
Quoted 4 lines
Yes, just using hwaddr for everything that only touches physical addresses would probably be best, but, ifaict, it's pretty common to use
target_longfor masks used on both virtual and physical addresses (TARGET_PAGE_MASK, for example). This 'tlb_size' variable is used on both as well.Quoted 7 lines
Then maybe you want vaddr ("
exec/vaddr.h"):/**
* vaddr:
* Type wide enough to contain any #target_ulong virtual address. */
Quoted 5 lines
I think hwaddr would fit better in this case since riscv32 virtual addresses are 32-bit, but I see vaddr is a u64, so it would work too. I personally don't mind changing the type of
tlb_sizeto hwaddr, but I went withtarget_longin this patch since that's what it was originally and masking with a signed long mask appears to be a common pattern in QEMU.
hwaddr seems like the right approach here
Alistair
Quoted 4 lines
Thanks,
drew