target/loongarch: Fix NX enforcement for PTW helpers
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v2 Latest2 patches 2 repliesIn review
- v12 patches 2 repliesSuperseded
Changes in v2 from the cover letter
- Patch 1: Touch only helper_ldpte() (leave LDDIR returning a PALEN-masked base addr)
- Patch 1: Drop hard-coded PPN width (use R_TLBENTRY_{32,64}_PPN_MASK)
- Patch 1: Clear software/non-architectural PTE bits via env->hw_pte_mask
- Patch 2: Carry Reviewed-by: Bibo Mao <[email protected]>
Patches in v2 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | target/loongarch: Preserve PTE permission bits in LDPTEtarget/ target/ target/ | +34 | 1 reply | |
| 2/2 | target/loongarch: Avoid recursive PNX exception on CSR_BADI fetchtarget/ | +1 | 0 replies |
Reviews trailers per patch
All 2 patches have a Reviewed-by or Acked-by of their own; the series has one for all of them.
| Patch | Bibo Mao | Song Gao |
|---|---|---|
| Series | R-b | |
| 1/2 | R-b | |
| 2/2 | R-b |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
Cover letter [PATCH v2 0/2] target/loongarch: Fix NX enforcement for PTW helpers
From: rail5 <[email protected]>
Thanks to Bibo Mao <[email protected]> for the quick review and feedback on the original patch.
The LoongArch ISA defines NX at bit 62 in the page table entry. Under TCG, NX is checked during translation, but the software page-walk helper (LDPTE) was masking the whole PTE value with the PALEN mask. This clears upper permission bits (including NX), allowing execution from NX mappings.
Fix this by masking only the PPN/address field and preserving permission bits, while also clearing any non-architectural (software) bits via a hardware PTE mask.
Once NX is enforced, instruction fetches from NX pages correctly raise PNX, but taking PNX could end up looping because loongarch_cpu_do_interrupt() tried to fetch the faulting instruction to populate CSR_BADI, which faults with PNX again. Treat PNX like other instruction-fetch exceptions and skip the CSR_BADI fetch.
Reported at: https://gitlab.com/qemu-project/qemu/-/issues/3319
Tested with a Linux guest by mapping a page RW, writing a single instruction, mprotect(PROT_READ) (no exec) and then calling through a function pointer. With this series applied the guest receives SIGSEGV instead of executing or hanging.
Changes in v2:
- Patch 1: Touch only helper_ldpte() (leave LDDIR returning a PALEN-masked base addr)
- Patch 1: Drop hard-coded PPN width (use R_TLBENTRY_{32,64}_PPN_MASK)
- Patch 1: Clear software/non-architectural PTE bits via env->hw_pte_mask
- Patch 2: Carry Reviewed-by: Bibo Mao <[email protected]>
rail5 (2):
target/loongarch: Preserve PTE permission bits in LDPTE target/loongarch: Avoid recursive PNX exception on CSR_BADI fetch
target/loongarch/cpu.c | 11 +++++++++++ target/loongarch/cpu.h | 1 + target/loongarch/tcg/tcg_cpu.c | 2 +- target/loongarch/tcg/tlb_helper.c | 25 ++++++++++++++++++++++--- 4 files changed, 35 insertions(+), 4 deletions(-)
Signature · 1 line
2.47.3