From: Djordje Todorovic <Djordje.Todorovic@htecgroup.com>
To: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Cc: "qemu-riscv@nongnu.org" <qemu-riscv@nongnu.org>,
"cfu@mips.com" <cfu@mips.com>, "mst@redhat.com" <mst@redhat.com>,
"marcel.apfelbaum@gmail.com" <marcel.apfelbaum@gmail.com>,
"dbarboza@ventanamicro.com" <dbarboza@ventanamicro.com>,
"philmd@linaro.org" <philmd@linaro.org>,
"alistair23@gmail.com" <alistair23@gmail.com>,
"thuth@redhat.com" <thuth@redhat.com>,
Djordje Todorovic <Djordje.Todorovic@htecgroup.com>
Subject: [PATCH v5 5/7] target/riscv: Fix page table walk endianness for big-endian harts
Date: Tue, 24 Mar 2026 16:40:16 +0000 [thread overview]
Message-ID: <20260324164007.549397-6-djordje.todorovic@htecgroup.com> (raw)
In-Reply-To: <20260324164007.549397-1-djordje.todorovic@htecgroup.com>
The page table walker reads PTEs using address_space_ldl/ldq which use
compile-time native endianness (always LE for RISC-V). However, when a
big-endian kernel writes PTEs via normal store instructions, they are
stored in big-endian byte order. The walker then misinterprets the PTE
values, causing page faults and a hang when the kernel enables the MMU.
The RISC-V privileged specification states that implicit data memory
accesses to supervisor-level memory management data structures follow
the hart's endianness setting (MSTATUS SBE/MBE bits).
Fix both PTE reads and atomic A/D bit updates to use the explicit _le
or _be memory access variants based on the hart's runtime endianness.
Signed-off-by: Djordje Todorovic <djordje.todorovic@htecgroup.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
target/riscv/cpu_helper.c | 28 ++++++++++++++++++++++------
1 file changed, 22 insertions(+), 6 deletions(-)
diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c
index c28832e0e3..b3d33da13e 100644
--- a/target/riscv/cpu_helper.c
+++ b/target/riscv/cpu_helper.c
@@ -1365,9 +1365,13 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
}
if (riscv_cpu_mxl(env) == MXL_RV32) {
- pte = address_space_ldl_le(cs->as, pte_addr, attrs, &res);
+ pte = riscv_cpu_data_is_big_endian(env)
+ ? address_space_ldl_be(cs->as, pte_addr, attrs, &res)
+ : address_space_ldl_le(cs->as, pte_addr, attrs, &res);
} else {
- pte = address_space_ldq_le(cs->as, pte_addr, attrs, &res);
+ pte = riscv_cpu_data_is_big_endian(env)
+ ? address_space_ldq_be(cs->as, pte_addr, attrs, &res)
+ : address_space_ldq_le(cs->as, pte_addr, attrs, &res);
}
if (res != MEMTX_OK) {
@@ -1566,12 +1570,24 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
if (memory_region_is_ram(mr)) {
target_ulong *pte_pa = qemu_map_ram_ptr(mr->ram_block, addr1);
target_ulong old_pte;
+ bool be = riscv_cpu_data_is_big_endian(env);
if (riscv_cpu_sxl(env) == MXL_RV32) {
- old_pte = qatomic_cmpxchg((uint32_t *)pte_pa, cpu_to_le32(pte), cpu_to_le32(updated_pte));
- old_pte = le32_to_cpu(old_pte);
+ uint32_t cmp = be ? cpu_to_be32(pte)
+ : cpu_to_le32(pte);
+ uint32_t val = be ? cpu_to_be32(updated_pte)
+ : cpu_to_le32(updated_pte);
+ old_pte = qatomic_cmpxchg((uint32_t *)pte_pa,
+ cmp, val);
+ old_pte = be ? be32_to_cpu(old_pte)
+ : le32_to_cpu(old_pte);
} else {
- old_pte = qatomic_cmpxchg(pte_pa, cpu_to_le64(pte), cpu_to_le64(updated_pte));
- old_pte = le64_to_cpu(old_pte);
+ target_ulong cmp = be ? cpu_to_be64(pte)
+ : cpu_to_le64(pte);
+ target_ulong val = be ? cpu_to_be64(updated_pte)
+ : cpu_to_le64(updated_pte);
+ old_pte = qatomic_cmpxchg(pte_pa, cmp, val);
+ old_pte = be ? be64_to_cpu(old_pte)
+ : le64_to_cpu(old_pte);
}
if (old_pte != pte) {
goto restart;
--
2.34.1
next prev parent reply other threads:[~2026-03-24 16:40 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-24 16:40 [PATCH v5 0/7] Add RISC-V big-endian target support Djordje Todorovic
2026-03-24 16:40 ` [PATCH v5 1/7] target/riscv: Add big-endian CPU property Djordje Todorovic
2026-03-25 7:26 ` Chao Liu
2026-03-25 10:47 ` Thomas Huth
2026-03-24 16:40 ` [PATCH v5 2/7] target/riscv: Set endianness MSTATUS bits at CPU reset Djordje Todorovic
2026-03-24 16:40 ` [PATCH v5 4/7] hw/riscv: Make boot code endianness-aware at runtime Djordje Todorovic
2026-03-24 16:40 ` [PATCH v5 3/7] target/riscv: Implement runtime data endianness via MSTATUS bits Djordje Todorovic
2026-03-24 16:40 ` Djordje Todorovic [this message]
2026-03-24 16:40 ` [PATCH v5 7/7] target/riscv: Add test for RISC-V BE Djordje Todorovic
2026-03-25 10:51 ` Thomas Huth
2026-03-24 16:40 ` [PATCH v5 6/7] target/riscv: Support runtime endianness in virtio via sysemu callback Djordje Todorovic
2026-03-25 3:58 ` [PATCH v5 0/7] Add RISC-V big-endian target support Chao Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260324164007.549397-6-djordje.todorovic@htecgroup.com \
--to=djordje.todorovic@htecgroup.com \
--cc=alistair23@gmail.com \
--cc=cfu@mips.com \
--cc=dbarboza@ventanamicro.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mst@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-riscv@nongnu.org \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox