git: fd25c62278ce - main - i386: check that trap() and syscall() run on the thread kstack

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 14 Sep 2022 15:57:42 UTC
The branch main has been updated by kib:

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

commit fd25c62278cea3492a14abadc190f43da5f74224
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-07 20:13:35 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-14 15:46:32 +0000

    i386: check that trap() and syscall() run on the thread kstack
    
    and not on the trampoline stack.  This is a useful way to ensure that
    we did not enabled interrupts while on user %cr3 or trampoline stack.
    
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
---
 sys/i386/i386/mp_machdep.c |  4 ++++
 sys/i386/i386/trap.c       | 30 ++++++++++++++++++++++++++++++
 sys/x86/include/x86_var.h  |  6 ++++++
 sys/x86/isa/atpic.c        |  1 +
 sys/x86/x86/local_apic.c   |  5 +++++
 5 files changed, 46 insertions(+)

diff --git a/sys/i386/i386/mp_machdep.c b/sys/i386/i386/mp_machdep.c
index 777aefa021b3..bf1c7faf6182 100644
--- a/sys/i386/i386/mp_machdep.c
+++ b/sys/i386/i386/mp_machdep.c
@@ -658,6 +658,7 @@ invltlb_handler(void)
 {
 	uint32_t generation;
 
+	trap_check_kstack();
 #ifdef COUNT_XINVLTLB_HITS
 	xhits_gbl[PCPU_GET(cpuid)]++;
 #endif /* COUNT_XINVLTLB_HITS */
@@ -680,6 +681,7 @@ invlpg_handler(void)
 {
 	uint32_t generation;
 
+	trap_check_kstack();
 #ifdef COUNT_XINVLTLB_HITS
 	xhits_pg[PCPU_GET(cpuid)]++;
 #endif /* COUNT_XINVLTLB_HITS */
@@ -699,6 +701,7 @@ invlrng_handler(void)
 	vm_offset_t addr, addr2;
 	uint32_t generation;
 
+	trap_check_kstack();
 #ifdef COUNT_XINVLTLB_HITS
 	xhits_rng[PCPU_GET(cpuid)]++;
 #endif /* COUNT_XINVLTLB_HITS */
@@ -724,6 +727,7 @@ invlcache_handler(void)
 {
 	uint32_t generation;
 
+	trap_check_kstack();
 #ifdef COUNT_IPIS
 	(*ipi_invlcache_counts[PCPU_GET(cpuid)])++;
 #endif /* COUNT_IPIS */
diff --git a/sys/i386/i386/trap.c b/sys/i386/i386/trap.c
index d09ba78cd83e..a4decc4976dc 100644
--- a/sys/i386/i386/trap.c
+++ b/sys/i386/i386/trap.c
@@ -184,6 +184,34 @@ SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
     &uprintf_signal, 0,
     "Print debugging information on trap signal to ctty");
 
+
+#ifdef INVARIANTS
+static __inline register_t
+read_esp(void)
+{
+	register_t res;
+
+	__asm __volatile("movl\t%%esp,%0" : "=r" (res));
+	return (res);
+}
+
+void
+trap_check_kstack(void)
+{
+	struct thread *td;
+	vm_offset_t stk;
+
+	td = curthread;
+	stk = read_esp();
+	if (stk >= PMAP_TRM_MIN_ADDRESS)
+		panic("td %p stack %#x in trampoline", td, stk);
+	if (stk < td->td_kstack || stk >= td->td_kstack +
+	    ptoa(td->td_kstack_pages))
+		panic("td %p stack %#x not in kstack VA %#x %d",
+		    td, stk, td->td_kstack, td->td_kstack_pages);
+}
+#endif
+
 /*
  * Exception, fault, and trap interface to the FreeBSD kernel.
  * This common code is called from assembly language IDT gate entry
@@ -227,6 +255,7 @@ trap(struct trapframe *frame)
 		return;
 	}
 #endif
+	trap_check_kstack();
 
 	if (type == T_RESERVED) {
 		trap_fatal(frame, 0);
@@ -1126,6 +1155,7 @@ syscall(struct trapframe *frame)
 		/* NOT REACHED */
 	}
 #endif
+	trap_check_kstack();
 	orig_tf_eflags = frame->tf_eflags;
 
 	td = curthread;
diff --git a/sys/x86/include/x86_var.h b/sys/x86/include/x86_var.h
index 3e8e643e1b9c..38a332e36078 100644
--- a/sys/x86/include/x86_var.h
+++ b/sys/x86/include/x86_var.h
@@ -175,4 +175,10 @@ uint64_t rdtsc_ordered(void);
 
 void x86_msr_op(u_int msr, u_int op, uint64_t arg1, uint64_t *res);
 
+#if defined(__i386__) && defined(INVARIANTS)
+void	trap_check_kstack(void);
+#else
+#define	trap_check_kstack()
+#endif
+
 #endif
diff --git a/sys/x86/isa/atpic.c b/sys/x86/isa/atpic.c
index 0c0ca6d3ea8d..03ad707abdfa 100644
--- a/sys/x86/isa/atpic.c
+++ b/sys/x86/isa/atpic.c
@@ -526,6 +526,7 @@ atpic_handle_intr(u_int vector, struct trapframe *frame)
 
 	kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
 	kmsan_mark(frame, sizeof(*frame), KMSAN_STATE_INITED);
+	trap_check_kstack();
 
 	KASSERT(vector < NUM_ISA_IRQS, ("unknown int %u\n", vector));
 	isrc = &atintrs[vector].at_intsrc;
diff --git a/sys/x86/x86/local_apic.c b/sys/x86/x86/local_apic.c
index 42a5a9fd4c8c..609635bdad3d 100644
--- a/sys/x86/x86/local_apic.c
+++ b/sys/x86/x86/local_apic.c
@@ -1296,6 +1296,7 @@ lapic_handle_intr(int vector, struct trapframe *frame)
 	kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
 	kmsan_mark(&vector, sizeof(vector), KMSAN_STATE_INITED);
 	kmsan_mark(frame, sizeof(*frame), KMSAN_STATE_INITED);
+	trap_check_kstack();
 
 	isrc = intr_lookup_source(apic_idt_to_irq(PCPU_GET(apic_id),
 	    vector));
@@ -1314,6 +1315,7 @@ lapic_handle_timer(struct trapframe *frame)
 
 	kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
 	kmsan_mark(frame, sizeof(*frame), KMSAN_STATE_INITED);
+	trap_check_kstack();
 
 #if defined(SMP) && !defined(SCHED_ULE)
 	/*
@@ -1433,6 +1435,7 @@ lapic_timer_stop(struct lapic *la)
 void
 lapic_handle_cmc(void)
 {
+	trap_check_kstack();
 
 	lapic_eoi();
 	cmc_intr();
@@ -1495,6 +1498,8 @@ lapic_handle_error(void)
 {
 	uint32_t esr;
 
+	trap_check_kstack();
+
 	/*
 	 * Read the contents of the error status register.  Write to
 	 * the register first before reading from it to force the APIC