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
This commit is contained in:
Konstantin Belousov 2022-09-07 23:13:35 +03:00
parent 5313350374
commit fd25c62278
5 changed files with 46 additions and 0 deletions

View File

@ -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 */

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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