From patchwork Wed Jul 11 11:29:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10519439 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 61BFC603D7 for ; Wed, 11 Jul 2018 11:30:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 64FA928D21 for ; Wed, 11 Jul 2018 11:30:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58E4E28D44; Wed, 11 Jul 2018 11:30:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 48B9828D21 for ; Wed, 11 Jul 2018 11:30:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF4246B0277; Wed, 11 Jul 2018 07:30:10 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BCD8E6B0279; Wed, 11 Jul 2018 07:30:10 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6D126B0278; Wed, 11 Jul 2018 07:30:10 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 3FA0E6B0275 for ; Wed, 11 Jul 2018 07:30:10 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id r9-v6so6133369edh.14 for ; Wed, 11 Jul 2018 04:30:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=QzrSgrTE2+ngwQ81MH7MyBVHKq95BfSpDiwn8jD6BRs=; b=Vf8ze9S8xIaCUMThEbsDD9j8ZcfSljplGadUdi/1JTZQiCfrYLBRlEAclY8DSrzz7L aNw0aqRmJFl0tWXpl0W8rC5Rz00Rb6wsDgA0sxV5o72wzTUgaW7WkgmN0gf0RtpEqZmU SoQdBNektXuv04kbNPZsaiMKCjE+Y7zfVMthpAboM8wUzhxpBkI3fHjutqGOb2Xfn0ST h5iuy919Omsh1DlhOETwz9CfUYWV90igkin1SdgDs6ZDJx5PxGAtw3BrJRxoce+5g1DC MoWINYENAv/iE+gkseGB4Epch3GPYfMFwGHum9ur30voQZl2l2nwPUG27kCXXVd2E+ZH TwMw== X-Gm-Message-State: APt69E3IKTNHtJn9+PMsY8flpOos70q6AgM83TdtFj+y/0HD2uTF6yZe C9+wHbJ9UDGVVeNoiIvShAjpfbSWdbBRSkwQptiMQeNlbyqo7BDnN5Kq6GuLLKtipdkwmV8GBls cS77zuOnKe2zztJY3VV9jz3DlrGu2wLCsQ+S6FTm9pY9vDzSijePBf0YfM16mlgwF5w== X-Received: by 2002:a50:cc01:: with SMTP id m1-v6mr31126043edi.171.1531308609793; Wed, 11 Jul 2018 04:30:09 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeHy50VOobegw433UskNsoSQAkgEyjdbHbNgJKorNptXCu8+s9XlgYmxOazW5SqhdNzpggx X-Received: by 2002:a50:cc01:: with SMTP id m1-v6mr31125996edi.171.1531308609013; Wed, 11 Jul 2018 04:30:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531308608; cv=none; d=google.com; s=arc-20160816; b=HhtAi7msnd8qSjfRN9zISU20qhD9k4p2hixNMOTGRDzNM6jV+yg6yHXPIyt8AMrvPL sl/pToxBhIVrOgFiy7hLSwGDII1dk5LdN2EI9LLmsjKZQPkxLNnYYJgix5eTM5bOk1bi SkNllKn9q1qW/BFMX6LW0RAGmCJymkG55NolBeGOJDMFmysuKMFbTd2lw/EvJu14kJcB Vzb9hm3Vd0F0VSWG7jYvVjJJIcqK7bqcBfpLdulR2wITvTGnGhRTNa9JIX8ZFhZ0UFz8 UnstSkIuyy38Dqr2D+PeHvSq7a80xAA2O8MuZg4vR8OYrW9kyriTf6b5QgyB1EpT7t21 ketA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=QzrSgrTE2+ngwQ81MH7MyBVHKq95BfSpDiwn8jD6BRs=; b=rWGCtzDYqSTBcKEkf5DiUxzm/K0T8hOPhi9nnpd0O7WTtr10ggNyePbUOxn7HZnisL 0ZOd76HdX7qbnpxKTvkzCS6TAXwMUwh+RAak5WZ20B/VKtBYu5ro87cK+hfq8nRCQ5DX Kqa49/bJzVnaP8vNQyZK80n7UCpilIyhQDq0AUX0wnlCjqQg96a1HeHgDIWhZnJ3uTGS 85SLokVg0dfa73i1zX+jKmNEWPuHDy8p2ejXNLG9vINDdx7dHLepeNlfrLhGJzLHu+ak jhJf/GvuSKl1sZNigjTLSYf2YrnkE2j3lzZq48r904pHSACg/FSNvJ0y0jhUidkAs5Zt j5cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=Kqc+hg5D; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: from theia.8bytes.org (8bytes.org. [2a01:238:4383:600:38bc:a715:4b6d:a889]) by mx.google.com with ESMTPS id g3-v6si3523401edg.360.2018.07.11.04.30.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Jul 2018 04:30:08 -0700 (PDT) Received-SPF: pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) client-ip=2a01:238:4383:600:38bc:a715:4b6d:a889; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=Kqc+hg5D; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: by theia.8bytes.org (Postfix, from userid 1000) id 66F09767; Wed, 11 Jul 2018 13:30:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1531308601; bh=oYPgi3NksNzxfLZ6vmyXiBxDzFkNoDJf1JAzwjtUYjs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kqc+hg5DiIjFzt8Y6OUZn1RtJE0T8OpBQaSwJm1BHW/eLT6KejO20Zre2tk0nQoKQ PKxIDiJZSzY7RrcQkCDWGk3yxZrnIgdArQddH7HXlZnzZhU3edmL2hGRK6bJyK/8CF Wt9PiAT6CQKXQcyo7vUbjLH0Ku8VDx3fz2MDFj+G80BoIZWc5UnXhyLe3l3lAFYjdU nYDb7hhcyn5gKZEb0szceUy/Qd+sJOpzB+2bXCeegT6fLSi/Tj+GHe6HNQa8jt87ng h6ZUflD3BHmp92F0fog8QMzoJMI7QSYsDhqHsW3/16NymRYbruQyC5Ms/XeRwt2Z3Z lmZFEvFhEMB1Q== From: Joerg Roedel To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Andy Lutomirski , Dave Hansen , Josh Poimboeuf , Juergen Gross , Peter Zijlstra , Borislav Petkov , Jiri Kosina , Boris Ostrovsky , Brian Gerst , David Laight , Denys Vlasenko , Eduardo Valentin , Greg KH , Will Deacon , aliguori@amazon.com, daniel.gruss@iaik.tugraz.at, hughd@google.com, keescook@google.com, Andrea Arcangeli , Waiman Long , Pavel Machek , "David H . Gutteridge" , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 08/39] x86/entry/32: Leave the kernel via trampoline stack Date: Wed, 11 Jul 2018 13:29:15 +0200 Message-Id: <1531308586-29340-9-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1531308586-29340-1-git-send-email-joro@8bytes.org> References: <1531308586-29340-1-git-send-email-joro@8bytes.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Joerg Roedel Switch back to the trampoline stack before returning to userspace. Signed-off-by: Joerg Roedel --- arch/x86/entry/entry_32.S | 79 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S index 528db7d..357b82b 100644 --- a/arch/x86/entry/entry_32.S +++ b/arch/x86/entry/entry_32.S @@ -347,6 +347,60 @@ .endm /* + * Switch back from the kernel stack to the entry stack. + * + * The %esp register must point to pt_regs on the task stack. It will + * first calculate the size of the stack-frame to copy, depending on + * whether we return to VM86 mode or not. With that it uses 'rep movsl' + * to copy the contents of the stack over to the entry stack. + * + * We must be very careful here, as we can't trust the contents of the + * task-stack once we switched to the entry-stack. When an NMI happens + * while on the entry-stack, the NMI handler will switch back to the top + * of the task stack, overwriting our stack-frame we are about to copy. + * Therefore we switch the stack only after everything is copied over. + */ +.macro SWITCH_TO_ENTRY_STACK + + ALTERNATIVE "", "jmp .Lend_\@", X86_FEATURE_XENPV + + /* Bytes to copy */ + movl $PTREGS_SIZE, %ecx + +#ifdef CONFIG_VM86 + testl $(X86_EFLAGS_VM), PT_EFLAGS(%esp) + jz .Lcopy_pt_regs_\@ + + /* Additional 4 registers to copy when returning to VM86 mode */ + addl $(4 * 4), %ecx + +.Lcopy_pt_regs_\@: +#endif + + /* Initialize source and destination for movsl */ + movl PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %edi + subl %ecx, %edi + movl %esp, %esi + + /* Save future stack pointer in %ebx */ + movl %edi, %ebx + + /* Copy over the stack-frame */ + shrl $2, %ecx + cld + rep movsl + + /* + * Switch to entry-stack - needs to happen after everything is + * copied because the NMI handler will overwrite the task-stack + * when on entry-stack + */ + movl %ebx, %esp + +.Lend_\@: +.endm + +/* * %eax: prev task * %edx: next task */ @@ -586,25 +640,45 @@ ENTRY(entry_SYSENTER_32) /* Opportunistic SYSEXIT */ TRACE_IRQS_ON /* User mode traces as IRQs on. */ + + /* + * Setup entry stack - we keep the pointer in %eax and do the + * switch after almost all user-state is restored. + */ + + /* Load entry stack pointer and allocate frame for eflags/eax */ + movl PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %eax + subl $(2*4), %eax + + /* Copy eflags and eax to entry stack */ + movl PT_EFLAGS(%esp), %edi + movl PT_EAX(%esp), %esi + movl %edi, (%eax) + movl %esi, 4(%eax) + + /* Restore user registers and segments */ movl PT_EIP(%esp), %edx /* pt_regs->ip */ movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */ 1: mov PT_FS(%esp), %fs PTGS_TO_GS + popl %ebx /* pt_regs->bx */ addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */ popl %esi /* pt_regs->si */ popl %edi /* pt_regs->di */ popl %ebp /* pt_regs->bp */ - popl %eax /* pt_regs->ax */ + + /* Switch to entry stack */ + movl %eax, %esp /* * Restore all flags except IF. (We restore IF separately because * STI gives a one-instruction window in which we won't be interrupted, * whereas POPF does not.) */ - addl $PT_EFLAGS-PT_DS, %esp /* point esp at pt_regs->flags */ btrl $X86_EFLAGS_IF_BIT, (%esp) popfl + popl %eax /* * Return back to the vDSO, which will pop ecx and edx. @@ -673,6 +747,7 @@ ENTRY(entry_INT80_32) restore_all: TRACE_IRQS_IRET + SWITCH_TO_ENTRY_STACK .Lrestore_all_notrace: CHECK_AND_APPLY_ESPFIX .Lrestore_nocheck: