Skip to content

Commit 156321d

Browse files
wtdcodeMichael Tokarev
authored andcommitted
target/riscv: fix endless translation loop on big endian systems
On big endian systems, pte and updated_pte hold big endian host data while pte_pa points to little endian target data. This means the branch at cpu_helper.c:1669 will be always satisfied and restart translation, causing an endless translation loop. The correctness of this patch can be deduced by: old_pte will hold value either from cpu_to_le32/64(pte) or cpu_to_le32/64(updated_pte), both of wich is litte endian. After that, an in-place conversion by le32/64_to_cpu(old_pte) ensures that old_pte now is in native endian, same with pte. Therefore, the endianness of the both side of if (old_pte != pte) is correct. Signed-off-by: Ziqiao Kong <[email protected]> Reviewed-by: Alistair Francis <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]> Cc: [email protected] (cherry picked from commit ad63158) Signed-off-by: Michael Tokarev <[email protected]>
1 parent b76d4a5 commit 156321d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

target/riscv/cpu_helper.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,9 +1662,11 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
16621662
target_ulong *pte_pa = qemu_map_ram_ptr(mr->ram_block, addr1);
16631663
target_ulong old_pte;
16641664
if (riscv_cpu_sxl(env) == MXL_RV32) {
1665-
old_pte = qatomic_cmpxchg((uint32_t *)pte_pa, pte, updated_pte);
1665+
old_pte = qatomic_cmpxchg((uint32_t *)pte_pa, cpu_to_le32(pte), cpu_to_le32(updated_pte));
1666+
old_pte = le32_to_cpu(old_pte);
16661667
} else {
1667-
old_pte = qatomic_cmpxchg(pte_pa, pte, updated_pte);
1668+
old_pte = qatomic_cmpxchg(pte_pa, cpu_to_le64(pte), cpu_to_le64(updated_pte));
1669+
old_pte = le64_to_cpu(old_pte);
16681670
}
16691671
if (old_pte != pte) {
16701672
goto restart;

0 commit comments

Comments
 (0)