From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752177AbdKZLuu (ORCPT ); Sun, 26 Nov 2017 06:50:50 -0500 Received: from mail.skyhub.de ([5.9.137.197]:44174 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752072AbdKZLus (ORCPT ); Sun, 26 Nov 2017 06:50:48 -0500 Date: Sun, 26 Nov 2017 12:50:38 +0100 From: Borislav Petkov To: Ingo Molnar Cc: linux-kernel@vger.kernel.org, Dave Hansen , Andy Lutomirski , Thomas Gleixner , "H . Peter Anvin" , Peter Zijlstra , Linus Torvalds Subject: Re: [PATCH 22/43] x86/mm/kaiser: Prepare assembly for entry/exit CR3 switching Message-ID: <20171126115038.sfwpecgv6pdfzlbo@pd.tnic> References: <20171124172411.19476-1-mingo@kernel.org> <20171124172411.19476-23-mingo@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20171124172411.19476-23-mingo@kernel.org> User-Agent: NeoMutt/20170609 (1.8.3) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 24, 2017 at 06:23:50PM +0100, Ingo Molnar wrote: > diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h > index 3fd8bc560fae..e1650da01323 100644 > --- a/arch/x86/entry/calling.h > +++ b/arch/x86/entry/calling.h > @@ -1,6 +1,7 @@ > /* SPDX-License-Identifier: GPL-2.0 */ > #include > #include > +#include > > /* > > @@ -187,6 +188,70 @@ For 32-bit we have the following conventions - kernel is built with > #endif > .endm > > +#ifdef CONFIG_KAISER > + > +/* KAISER PGDs are 8k. Flip bit 12 to switch between the two halves: */ > +#define KAISER_SWITCH_MASK (1< +.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req > + movq %cr3, %r\scratch_reg > + movq %r\scratch_reg, \save_reg What happened to making it uniform so that that macro can be invoked like this: SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax ... instead of "splitting" the arg? IOW, hunk below builds here, and asm looks correct: 14bf: 31 db xor %ebx,%ebx 14c1: 0f 20 d8 mov %cr3,%rax 14c4: 49 89 c6 mov %rax,%r14 14c7: 48 a9 00 10 00 00 test $0x1000,%rax 14cd: 74 09 je 14d8 14cf: 48 25 ff ef ff ff and $0xffffffffffffefff,%rax 14d5: 0f 22 d8 mov %rax,%cr3 --- diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h index e1650da01323..d528f7060774 100644 --- a/arch/x86/entry/calling.h +++ b/arch/x86/entry/calling.h @@ -188,10 +188,12 @@ For 32-bit we have the following conventions - kernel is built with #endif .endm +#define CONFIG_KAISER + #ifdef CONFIG_KAISER /* KAISER PGDs are 8k. Flip bit 12 to switch between the two halves: */ -#define KAISER_SWITCH_MASK (1< diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S > index 34e3110b0876..4ac952080869 100644 > --- a/arch/x86/entry/entry_64.S > +++ b/arch/x86/entry/entry_64.S > @@ -168,6 +168,9 @@ ENTRY(entry_SYSCALL_64_trampoline) > /* Stash the user RSP. */ > movq %rsp, RSP_SCRATCH > > + /* Note: using %rsp as a scratch reg. */ Haha, yap, it just got freed :) > + SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp > + > /* Load the top of the task stack into RSP */ > movq CPU_ENTRY_AREA_tss + TSS_sp1 + CPU_ENTRY_AREA, %rsp > > @@ -198,6 +201,13 @@ ENTRY(entry_SYSCALL_64) > > swapgs > movq %rsp, PER_CPU_VAR(rsp_scratch) <---- newline here. > + /* > + * The kernel CR3 is needed to map the process stack, but we > + * need a scratch register to be able to load CR3. %rsp is > + * clobberable right now, so use it as a scratch register. > + * %rsp will be look crazy here for a couple instructions. s/be // or "will be looking crazy" :-) > + */ > + SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp Now, this is questionable: we did enter through the trampoline entry_SYSCALL_64_trampoline so theoretically, we wouldn't need to switch to CR3 here again because, well, we did already. I.e., entry_SYSCALL_64 is not going to be called anymore. Unless we will jump to it when we decide to jump over the trampolines in the kaiser disabled case. Just pointing it out here so that we don't forget to deal with this... > @@ -1239,7 +1254,11 @@ ENTRY(paranoid_entry) > js 1f /* negative -> in kernel */ > SWAPGS > xorl %ebx, %ebx > -1: ret > + > +1: > + SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=ax save_reg=%r14 > + > + ret > END(paranoid_entry) > > /* > @@ -1261,6 +1280,7 @@ ENTRY(paranoid_exit) > testl %ebx, %ebx /* swapgs needed? */ > jnz .Lparanoid_exit_no_swapgs > TRACE_IRQS_IRETQ > + RESTORE_CR3 %r14 RESTORE_CR3 save_reg=%r14 like the other invocation below. But if the runtime disable gets changed to a boottime one, you don't need that macro anymore. > SWAPGS_UNSAFE_STACK > jmp .Lparanoid_exit_restore > .Lparanoid_exit_no_swapgs: -- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.