git: e27089cda589 - stable/14 - amd64: on any fault during call to EFI RT, restore execution and print fault details

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 01 Jan 2025 10:38:13 UTC
The branch stable/14 has been updated by kib:

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

commit e27089cda5897633545d3df6707cd06970f03fae
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-12-24 02:35:16 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-01-01 10:30:20 +0000

    amd64: on any fault during call to EFI RT, restore execution and print fault details
    
    (cherry picked from commit dd2b5443644505af51c95503898ab363e7d7c29d)
---
 sys/amd64/amd64/trap.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/sys/amd64/amd64/trap.c b/sys/amd64/amd64/trap.c
index 396c2abd3f1f..7f1175da41df 100644
--- a/sys/amd64/amd64/trap.c
+++ b/sys/amd64/amd64/trap.c
@@ -445,6 +445,20 @@ trap(struct trapframe *frame)
 
 		KASSERT(cold || td->td_ucred != NULL,
 		    ("kernel trap doesn't have ucred"));
+
+		/*
+		 * Most likely, EFI RT faulted.  This check prevents
+		 * kdb from handling breakpoints set on the BIOS text,
+		 * if such option is ever needed.
+		 */
+		if ((td->td_pflags2 & TDP2_EFIRT) != 0 &&
+		    curpcb->pcb_onfault != NULL && type != T_PAGEFLT) {
+			trap_diag(frame, 0);
+			printf("EFI RT fault %s\n", traptype_to_msg(type));
+			frame->tf_rip = (long)curpcb->pcb_onfault;
+			return;
+		}
+
 		switch (type) {
 		case T_PAGEFLT:			/* page fault */
 			(void)trap_pfault(frame, false, NULL, NULL);
@@ -608,18 +622,6 @@ trap(struct trapframe *frame)
 			 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
 			 */
 		case T_BPTFLT:
-			/*
-			 * Most likely, EFI RT hitting INT3.  This
-			 * check prevents kdb from handling
-			 * breakpoints set on the BIOS text, if such
-			 * option is ever needed.
-			 */
-			if ((td->td_pflags2 & TDP2_EFIRT) != 0 &&
-			    curpcb->pcb_onfault != NULL) {
-				frame->tf_rip = (long)curpcb->pcb_onfault;
-				return;
-			}
-
 			/*
 			 * If KDB is enabled, let it handle the debugger trap.
 			 * Otherwise, debugger traps "can't happen".
@@ -883,6 +885,10 @@ trap_pfault(struct trapframe *frame, bool usermode, int *signo, int *ucode)
 after_vmfault:
 	if (td->td_intr_nesting_level == 0 &&
 	    curpcb->pcb_onfault != NULL) {
+		if ((td->td_pflags2 & TDP2_EFIRT) != 0) {
+			trap_diag(frame, eva);
+			printf("EFI RT page fault\n");
+		}
 		frame->tf_rip = (long)curpcb->pcb_onfault;
 		return (0);
 	}