Received: by 10.192.165.156 with SMTP id m28csp772152imm; Mon, 16 Apr 2018 08:28:52 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+kkszZTHyVPa9gv8b1RDnWxetbzhuU/F4KlL7D0dO09B9wH4kuyxAqd1tR07rxMSwHhrox X-Received: by 2002:a17:902:2983:: with SMTP id h3-v6mr15625196plb.346.1523892532598; Mon, 16 Apr 2018 08:28:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523892532; cv=none; d=google.com; s=arc-20160816; b=f9y0vc0gRN5Q7N0qisAtp2Umoc59pqWWBmspoNNJIsRyfRwRPAAQycWgvU+FV9jZem Qv7O32aa3Br+vo+q2AYAz0E7MMyFbmrGd40BZoKHmAytnJrt1DSZsDOMTZmIIU3TSw8Q t0RrkLqy6OxQfNmnIiE05usy2qp3dZhfUzC4HgaccU081s3MX5bKjXqX3EQQRS0A1Iq3 6W+FlBm8y56tAaNO1YqHaxAk2kVKvY/WCBMLSffWiClFIms6DP6Uf5lQClxRqnsiV1nn U+AdxWyd7VQDLXIPybrnlFFKp0FmqhIGq6+OsdK9R9CGmlKxJDXKwZVccpMTmbMeE/Me W04w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Nj5eF153u/RhtmixPl2cVjqjByJt50l0MRS18i+hFow=; b=hWy6e3jW/p5FTxsaqBSMMrJVKhDHHxXlOadmf6GAYT/os8zM8lqhGa/S34/1FoANVF gJj7wqL6WFlAV5UiQc0i2i6PIg2+0bA135R/nxjjhMLHnEL666XqJjs0TUKCVO/Fy/FJ wY+X/irG7BEQZxgcXmX1PGcpTEP/olicCpHekrdOuSbuJInkzFOEoVUttEf4GMX5qtSt MZK8OUdQV+usMjfRCVi2NM2Xo2RScUlRNLqpOFnzwwnIUi0Q5caEqI5YX2M5QL1ozKgj oJIJtm54xrZxqmqp4DV7JQQ4jmXZO/+rDQrHpfyhzr/+hKdtcRHXIPbnwvK5NHXoHDEW Zoyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=B1XxsBUy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m63-v6si12293156pld.52.2018.04.16.08.28.38; Mon, 16 Apr 2018 08:28:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=B1XxsBUy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753050AbeDPPZz (ORCPT + 99 others); Mon, 16 Apr 2018 11:25:55 -0400 Received: from 8bytes.org ([81.169.241.247]:35888 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752841AbeDPPZo (ORCPT ); Mon, 16 Apr 2018 11:25:44 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id E653D5E3; Mon, 16 Apr 2018 17:25:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1523892337; bh=LLnuBitcfIJThdnONxleqSsdjciXdrIT2o2aO6acdRU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B1XxsBUyrL/8w2qm8GxydLhJ+IIWuZdkaDmwXKqidqU/M4rC5T6imtmKw1m7/9nMP lH0Xi4eZOEdbnVOa+6wOun2iO6CdzUWqBVGWsTWIne8ZDHVwhO8wNdVGyKvi2Nxuqz 8B51PfKTAZhwmE851xnIFP+X+hAE5eqOhpxQWqw4lzsAgw7+Co8MT/DnBSKyUSFLic OESbR0CYkEs6zA2/dwAg7EYZIdp3Gs3XDdLxmH99qQ/yet0loyPeE3RUHSNUdfH1vJ HajulwXen8388VUfYhia3NnkPcy5sS7pP1jLmHrhqe1RyYnM1qftccDGRjldhbclzl SPiVdWuAE/ccw== 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/35] x86/entry/32: Leave the kernel via trampoline stack Date: Mon, 16 Apr 2018 17:24:56 +0200 Message-Id: <1523892323-14741-9-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523892323-14741-1-git-send-email-joro@8bytes.org> References: <1523892323-14741-1-git-send-email-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 1d6b527..927df80 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 */ btr $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: -- 2.7.4