Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Sep 2022 09:30:05 GMT
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 4ef4d8a97c27 - stable/13 - i386: check that trap() and syscall() run on the thread kstack
Message-ID:  <202209210930.28L9U5AZ054266@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by kib:

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

commit 4ef4d8a97c279a9286d94e6460b11d9b5701c53d
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-07 20:13:35 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-21 09:29:02 +0000

    i386: check that trap() and syscall() run on the thread kstack
    
    (cherry picked from commit fd25c62278cea3492a14abadc190f43da5f74224)
---
 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 b1cd3649e7bf..ccd77b12ceb9 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);
@@ -1125,6 +1154,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 4a7637b464fe..756849cbe028 100644
--- a/sys/x86/include/x86_var.h
+++ b/sys/x86/include/x86_var.h
@@ -174,4 +174,10 @@ void	x86_set_fork_retval(struct thread *td);
 
 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 28c10ee7009f..a98fb8b9d716 100644
--- a/sys/x86/isa/atpic.c
+++ b/sys/x86/isa/atpic.c
@@ -525,6 +525,7 @@ atpic_handle_intr(u_int vector, struct trapframe *frame)
 
 	/* The frame may have been written into a poisoned region. */
 	kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
+	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 4ebe345e8926..4ab4e78f20e7 100644
--- a/sys/x86/x86/local_apic.c
+++ b/sys/x86/x86/local_apic.c
@@ -1369,6 +1369,7 @@ lapic_handle_intr(int vector, struct trapframe *frame)
 
 	/* The frame may have been written into a poisoned region. */
 	kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
+	trap_check_kstack();
 
 	isrc = intr_lookup_source(apic_idt_to_irq(PCPU_GET(apic_id),
 	    vector));
@@ -1387,6 +1388,7 @@ lapic_handle_timer(struct trapframe *frame)
 
 	/* The frame may have been written into a poisoned region. */
 	kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
+	trap_check_kstack();
 
 #if defined(SMP) && !defined(SCHED_ULE)
 	/*
@@ -1506,6 +1508,7 @@ lapic_timer_stop(struct lapic *la)
 void
 lapic_handle_cmc(void)
 {
+	trap_check_kstack();
 
 	lapic_eoi();
 	cmc_intr();
@@ -1568,6 +1571,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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202209210930.28L9U5AZ054266>