Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751972AbeAPQ4U (ORCPT + 1 other); Tue, 16 Jan 2018 11:56:20 -0500 Received: from 8bytes.org ([81.169.241.247]:54752 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751353AbeAPQsd (ORCPT ); Tue, 16 Jan 2018 11:48:33 -0500 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 , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 15/16] x86/entry/32: Switch between kernel and user cr3 on entry/exit Date: Tue, 16 Jan 2018 17:36:58 +0100 Message-Id: <1516120619-1159-16-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516120619-1159-1-git-send-email-joro@8bytes.org> References: <1516120619-1159-1-git-send-email-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: From: Joerg Roedel Add the cr3 switches between the kernel and the user page-table when PTI is enabled. Signed-off-by: Joerg Roedel --- arch/x86/entry/entry_32.S | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S index 14018eeb11c3..6a1d9f1e1f89 100644 --- a/arch/x86/entry/entry_32.S +++ b/arch/x86/entry/entry_32.S @@ -221,6 +221,25 @@ POP_GS_EX .endm +#define PTI_SWITCH_MASK (1 << PAGE_SHIFT) + +.macro SWITCH_TO_KERNEL_CR3 + ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI + movl %cr3, %edi + andl $(~PTI_SWITCH_MASK), %edi + movl %edi, %cr3 +.Lend_\@: +.endm + +.macro SWITCH_TO_USER_CR3 + ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI + mov %cr3, %edi + /* Flip the PGD to the user version */ + orl $(PTI_SWITCH_MASK), %edi + mov %edi, %cr3 +.Lend_\@: +.endm + /* * Switch from the entry-trampline stack to the kernel stack of the * running task. @@ -240,6 +259,7 @@ .endif pushl %edi + SWITCH_TO_KERNEL_CR3 movl %esp, %edi /* @@ -309,9 +329,12 @@ .endif pushl 4(%edi) /* fs */ + pushl (%edi) /* edi */ + + SWITCH_TO_USER_CR3 /* Restore user %edi and user %fs */ - movl (%edi), %edi + popl %edi popl %fs .Lend_\@: -- 2.13.6