From nobody Wed Sep 14 15:57:42 2022 X-Original-To: dev-commits-src-main@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 4MSQ2t6V2xz4cRY8; Wed, 14 Sep 2022 15:57:42 +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 4MSQ2t60zkz3GTd; Wed, 14 Sep 2022 15:57:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663171062; 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=rChxUjVqPrIXCvGqND5cfB4XCVwkNxJJ+xDSIMu+5ok=; b=x6Q/xly+sHvOmVR3klKmokEbkvcVxJQe8HCPYk3gaiXlrfiIvGTmt1wXIeJlCNpu6BeiwX CQ1SzEvEO8VMCSnK3MHssNo8LsuMzrodM+H21VzHrZVnmprHYrUxw7Dt9N7uMuBiluAWQf iR93UdtIx6I5dc2sdBkiMiBwO1s4Y/qyeF+9J7GpTIuEdtnzg9+4ReH6lw57379c1xtA+O oXncFIB3YozPH0gcsPR51gECApej3NaqoGTsJEsoVenHcxIZjkT6qRYfqwLnYuBcS7q3Vq vy/PhohrNRlEfgh1nmsZVlvRdxEjeuO3MgypLQ9Pb6oI08faQT8hBG05Pz8NOA== 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 4MSQ2t50qpzk4c; Wed, 14 Sep 2022 15:57:42 +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 28EFvgH9010626; Wed, 14 Sep 2022 15:57:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28EFvgAv010625; Wed, 14 Sep 2022 15:57:42 GMT (envelope-from git) Date: Wed, 14 Sep 2022 15:57:42 GMT Message-Id: <202209141557.28EFvgAv010625@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: fd25c62278ce - main - i386: check that trap() and syscall() run on the thread kstack List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: fd25c62278cea3492a14abadc190f43da5f74224 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663171062; 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=rChxUjVqPrIXCvGqND5cfB4XCVwkNxJJ+xDSIMu+5ok=; b=iUSyJcQptbY+/KAwkTEh8v1uMbdiBQaVra+ld0M1YYsNZYMTAJ39+EXrhZwNOigqcblCRW dlDTGcnZewiDy+mRxV7B92JXraV1Rx0jCEWz3OS34Ig+ObT2TemUsUM4ygIHWQ/Lfa3Kec uOd12Xw2miILJIBfshz771snqht2VXFYMSzw222E7QnPlwLsW+OJWKs8u6DL828/C1R6Ct M27go6je0UK6c50sZuxDuAgPmzmZozD9IsFsn8BC68Bae+8z7K2AzO4q48Tt4nV93OJEfD bn6sRWKuLsLE5qlt0MNDn22vNq/JgJ0+pEJTYIswoGGa/InFl9LasSXnE8b40w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1663171062; a=rsa-sha256; cv=none; b=kES6JnYZ4ygEwHIqDDUv/zrR7EAvX9XNS1qFV5bo9d7P1bfKe6YijXCvRmnGrowp4KpJoL rB3IS18abi5cG0VqcXayne0I7FrFYFyFB+OqbcktiEYOlFE7j+S3lKI3RQRpRw91I2rezs NNeDiTOn/HL3ORsda1WNSNTrRom6JpAYG02l2FrYYo0DCkDGx2uK+UBRigC8PHKejXrn+e 0ZwHn74BoML4STAJAd9VkCHSk0GlzN/L//VEoT930j+z0MlF9/kdU4XWjtYWCz4TXNpaa0 wImxDPFk8dCKxSF43ECXt/VXCZ6VeRNaJkCcs1/jlmLxRrWSz6hfVj4lH7b8Wg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=fd25c62278cea3492a14abadc190f43da5f74224 commit fd25c62278cea3492a14abadc190f43da5f74224 Author: Konstantin Belousov AuthorDate: 2022-09-07 20:13:35 +0000 Commit: Konstantin Belousov 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