summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrail5 <andrew@rail5.org>2026-03-06 15:33:36 +0800
committerMichael Tokarev <mjt@tls.msk.ru>2026-03-13 10:15:33 +0300
commit3a7b0cd6a6051c920f17c0798ba0be970c1e8869 (patch)
treed8b896d9af5ed05fbbc206c55b53333c1ecacb7f
parent92a24cdbcca3a6bbaf9c5b39f0eebf687169745b (diff)
downloadqemu-3a7b0cd6a6051c920f17c0798ba0be970c1e8869.tar.gz
qemu-3a7b0cd6a6051c920f17c0798ba0be970c1e8869.zip
target/loongarch: Preserve PTE permission bits in LDPTE
The LDPTE helper loads a page table entry (or huge page entry) from guest memory and currently applies the PALEN mask to the whole 64-bit value. That mask is intended to constrain the physical address bits, but masking the full entry also clears upper permission bits in the PTE, including NX (bit 62). As a result, LoongArch TCG can incorrectly allow instruction fetches from NX mappings when translation is driven through software page-walk. Fix this by masking only the PPN/address field with PALEN while preserving permission bits, and by clearing any non-architectural (software) bits using a hardware PTE mask. LDDIR is unchanged since it returns the base address of the next page table level. Reported at: https://gitlab.com/qemu-project/qemu/-/issues/3319 -Fixes: 56599a705f2 ("target/loongarch: Introduce loongarch_palen_mask()") Fixes: f757a2cd6948 ("target/loongarch: Add LoongArch interrupt and exception handler") Cc: qemu-stable@nongnu.org Signed-off-by: rail5 (Andrew S. Rightenburg) <andrew@rail5.org> Reviewed-by: Bibo Mao <maobibo@loongson.cn> Reviewed-by: Song Gao <gaosong@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> (cherry picked from commit 2d877bc02a3b94998cbdd784d194c173d308a98a) (Mjt: backport to 10.1.x which lacks v10.2.0-1568-g56599a705f "target/loongarch: Introduce loongarch_palen_mask()") (fixing the Fixes: tag) Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
-rw-r--r--target/loongarch/cpu.c11
-rw-r--r--target/loongarch/cpu.h1
-rw-r--r--target/loongarch/tcg/tlb_helper.c23
3 files changed, 32 insertions, 3 deletions
diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
index 9ca85a56a2..266b0b97d0 100644
--- a/target/loongarch/cpu.c
+++ b/target/loongarch/cpu.c
@@ -575,6 +575,17 @@ static void loongarch_cpu_reset_hold(Object *obj, ResetType type)
#ifdef CONFIG_TCG
env->fcsr0_mask = FCSR0_M1 | FCSR0_M2 | FCSR0_M3;
+
+ if (is_la64(env)) {
+ env->hw_pte_mask = MAKE_64BIT_MASK(0, 9) |
+ R_TLBENTRY_64_PPN_MASK |
+ R_TLBENTRY_64_NR_MASK |
+ R_TLBENTRY_64_NX_MASK |
+ R_TLBENTRY_64_RPLV_MASK;
+ } else {
+ env->hw_pte_mask = MAKE_64BIT_MASK(0, 9) |
+ R_TLBENTRY_32_PPN_MASK;
+ }
#endif
env->fcsr0 = 0x0;
diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h
index 9538e8d61d..65b702938f 100644
--- a/target/loongarch/cpu.h
+++ b/target/loongarch/cpu.h
@@ -380,6 +380,7 @@ typedef struct CPUArchState {
uint32_t fcsr0_mask;
uint64_t lladdr; /* LL virtual address compared against SC */
uint64_t llval;
+ uint64_t hw_pte_mask; /* Mask of architecturally-defined (hardware) PTE bits. */
#endif
#ifndef CONFIG_USER_ONLY
#ifdef CONFIG_TCG
diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_helper.c
index 8872593ff0..9a86b4bb52 100644
--- a/target/loongarch/tcg/tlb_helper.c
+++ b/target/loongarch/tcg/tlb_helper.c
@@ -559,6 +559,20 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
cpu_loop_exit_restore(cs, retaddr);
}
+static inline uint64_t loongarch_sanitize_hw_pte(CPULoongArchState *env,
+ uint64_t pte)
+{
+ uint64_t ppn_mask = is_la64(env) ? R_TLBENTRY_64_PPN_MASK : R_TLBENTRY_32_PPN_MASK;
+
+ /*
+ * Keep only architecturally-defined PTE bits. Guests may use some
+ * otherwise-unused bits for software purposes.
+ */
+ pte &= env->hw_pte_mask;
+
+ return (pte & ~ppn_mask) | ((pte & ppn_mask) & TARGET_PHYS_MASK);
+}
+
target_ulong helper_lddir(CPULoongArchState *env, target_ulong base,
target_ulong level, uint32_t mem_idx)
{
@@ -599,6 +613,7 @@ void helper_ldpte(CPULoongArchState *env, target_ulong base, target_ulong odd,
{
CPUState *cs = env_cpu(env);
target_ulong phys, tmp0, ptindex, ptoffset0, ptoffset1, badv;
+ uint64_t pte_raw;
uint64_t ptbase = FIELD_EX64(env->CSR_PWCL, CSR_PWCL, PTBASE);
uint64_t ptwidth = FIELD_EX64(env->CSR_PWCL, CSR_PWCL, PTWIDTH);
uint64_t dir_base, dir_width;
@@ -611,7 +626,6 @@ void helper_ldpte(CPULoongArchState *env, target_ulong base, target_ulong odd,
* and the other is the huge page entry,
* whose bit 6 should be 1.
*/
- base = base & TARGET_PHYS_MASK;
if (FIELD_EX64(base, TLBENTRY, HUGE)) {
/*
* Gets the huge page level and Gets huge page size.
@@ -635,7 +649,7 @@ void helper_ldpte(CPULoongArchState *env, target_ulong base, target_ulong odd,
* when loaded into the tlb,
* so the tlb page size needs to be divided by 2.
*/
- tmp0 = base;
+ tmp0 = loongarch_sanitize_hw_pte(env, base);
if (odd) {
tmp0 += MAKE_64BIT_MASK(ps, 1);
}
@@ -647,12 +661,15 @@ void helper_ldpte(CPULoongArchState *env, target_ulong base, target_ulong odd,
} else {
badv = env->CSR_TLBRBADV;
+ base = base & TARGET_PHYS_MASK;
+
ptindex = (badv >> ptbase) & ((1 << ptwidth) - 1);
ptindex = ptindex & ~0x1; /* clear bit 0 */
ptoffset0 = ptindex << 3;
ptoffset1 = (ptindex + 1) << 3;
phys = base | (odd ? ptoffset1 : ptoffset0);
- tmp0 = ldq_phys(cs->as, phys) & TARGET_PHYS_MASK;
+ pte_raw = ldq_le_phys(cs->as, phys);
+ tmp0 = loongarch_sanitize_hw_pte(env, pte_raw);
ps = ptbase;
}