From nobody Wed Sep 21 09:30:05 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4MXY6Q2fqSz4crvp; Wed, 21 Sep 2022 09:30:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4MXY6Q03qKz3ZY1; Wed, 21 Sep 2022 09:30:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663752606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YYzBC9uRzJf+WMlLvAOssWfKw6855iGp4UQYVd2oqr4=; b=mcsyYvZyiSPiYR63rbmG/VVf9GB08zD1Fap261Ls859h5j3a36pxVcl0P+UafN/OWXLjVt O6Ch2fkigsTjzcbWgA6G4XQ2GHO5SGITuEhnoLwI7crjOFXnFdYFawF/chp3aYyJ7suBDe ZUcIp5HEbjqHDKs7Fu/185jvle1eKNc55eTCpDHWYX51oVs1PesVcMQQIxmV24DHHHSOCS kGR9TYtaKVKnAwjrmIt8qvRQwY5qr4iFiKXs4S80fSMPJbRPVrpSCeWKroHr0vUTLnBwQn HxlQeg/NjytxQiH8lufydbJmNmedQ7mX6F3HK3EpOcJ/z7o6ndz0Lddpm/d7qg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4MXY6P6HXQz11Zn; Wed, 21 Sep 2022 09:30:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 28L9U5fS054270; Wed, 21 Sep 2022 09:30:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28L9U5AZ054266; Wed, 21 Sep 2022 09:30:05 GMT (envelope-from git) Date: Wed, 21 Sep 2022 09:30:05 GMT Message-Id: <202209210930.28L9U5AZ054266@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 4ef4d8a97c27 - stable/13 - i386: check that trap() and syscall() run on the thread kstack List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 4ef4d8a97c279a9286d94e6460b11d9b5701c53d Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663752606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YYzBC9uRzJf+WMlLvAOssWfKw6855iGp4UQYVd2oqr4=; b=wiUtwujDsdpk276S2Yt0npgI8ylrs9SYlhx50NhYDlxFlXZMscxye2h0q6nDxNMwJ2WM5f Hl/bF9tjW93eP1pYE6CmV73s+b/TrcMuNXiZ3eVuFaYsz324GFwsmx8ypQe0NrixhT7c8B 8QhsRdhVm4qKC96IsUcXntkVF1d3jztimPCgl3e0p0eAkjGPEbDwlN1AewkeaX3X6ygS1o Jtc+EXznD5pTAqcpR4PVLnKqtq5JNBFusTiUA2rQHZPSDdPUrckTCFob6HFQX0bgUtm+I2 CYJOTFVZ3561mNlUNCVCcztVc8FTCTkUZcvwegQw9I3zBaOOuzTeu4GdKA/Lpw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1663752606; a=rsa-sha256; cv=none; b=SoKhmJD0+NkVjwCuK+C5sec9rsu8og4zeaFXNGXh6lqke7nqRf9ejRxZJqNUxuX7F5Hg4n QrqTnb3ghEvw7TNqfsn7Hg1CW2j+vxeXxsUp14n8/LzGXUbLDnqlXebrTviSP5RVmFe65/ loXtrdKBtANGSVaHq8dO8lAMWepvNo7kB5jXks6K3yNXzSaWYhYPaJIA1MJDLnT9/TEwsF AkHV1RSPfa/+gGX1y+Z6fnXUomtFQrotNvZF/CCBmruchHlsbDX6R/bWxVmJ8nQNnwUu8/ LvNxihZ7jUJYrLDPrl8jlOkNBYXiH3GjLw1a1E7FPxFkaTcYbhj+HBlMr4cyXQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=4ef4d8a97c279a9286d94e6460b11d9b5701c53d commit 4ef4d8a97c279a9286d94e6460b11d9b5701c53d Author: Konstantin Belousov AuthorDate: 2022-09-07 20:13:35 +0000 Commit: Konstantin Belousov 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