git: c226f193515c - main - riscv: Permit spurious faults in kernel mode

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 10 Dec 2024 15:09:37 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=c226f193515c8c0665610cb519fe381987f8ee24

commit c226f193515c8c0665610cb519fe381987f8ee24
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-12-10 15:07:28 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-12-10 15:07:28 +0000

    riscv: Permit spurious faults in kernel mode
    
    Right now, pmap_enter() does not issue an sfence.vma after overwriting
    an invalid PTE, so the kernel can trigger a page fault when accessing a
    freshly created mapping.  In this case, pmap_fault() can handle the
    exception, but we may panic before that.  Move the check; this is
    consistent with arm64 and serves to ensure that we don't call vm_fault()
    etc. from a context where that's not permitted.
    
    Also fix a related bug: don't enable interrupts if they were disabled in
    the context where the exception occurred.
    
    Reviewed by:    br
    Tested by:      br
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D47688
---
 sys/riscv/riscv/trap.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/sys/riscv/riscv/trap.c b/sys/riscv/riscv/trap.c
index 3bb1fc7f1010..a1a30eb58220 100644
--- a/sys/riscv/riscv/trap.c
+++ b/sys/riscv/riscv/trap.c
@@ -227,11 +227,6 @@ page_fault_handler(struct trapframe *frame, int usermode)
 	pcb = td->td_pcb;
 	stval = frame->tf_stval;
 
-	if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
-	    WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
-	    "Kernel page fault") != 0)
-		goto fatal;
-
 	if (usermode) {
 		if (!VIRT_IS_VALID(stval)) {
 			call_trapsignal(td, SIGSEGV, SEGV_MAPERR, (void *)stval,
@@ -244,7 +239,8 @@ page_fault_handler(struct trapframe *frame, int usermode)
 		 * Enable interrupts for the duration of the page fault. For
 		 * user faults this was done already in do_trap_user().
 		 */
-		intr_enable();
+		if ((frame->tf_sstatus & SSTATUS_SIE) != 0)
+			intr_enable();
 
 		if (stval >= VM_MIN_KERNEL_ADDRESS) {
 			map = kernel_map;
@@ -268,6 +264,11 @@ page_fault_handler(struct trapframe *frame, int usermode)
 	if (VIRT_IS_VALID(va) && pmap_fault(map->pmap, va, ftype))
 		goto done;
 
+	if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
+	    WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
+	    "Kernel page fault") != 0)
+		goto fatal;
+
 	error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &sig, &ucode);
 	if (error != KERN_SUCCESS) {
 		if (usermode) {