linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Shi <alex.shi@linaro.org>
To: Marc Zyngier <marc.zyngier@arm.com>,
	Will Deacon <will.deacon@arm.com>,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	stable@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Cc: Alex Shi <alex.shi@linaro.org>
Subject: [PATCH 14/29] arm64: entry: Add fake CPU feature for unmapping the kernel at EL0
Date: Wed, 28 Feb 2018 11:56:36 +0800	[thread overview]
Message-ID: <1519790211-16582-15-git-send-email-alex.shi@linaro.org> (raw)
In-Reply-To: <1519790211-16582-1-git-send-email-alex.shi@linaro.org>

From: Will Deacon <will.deacon@arm.com>

commit ea1e3de85e94 upstream.

Allow explicit disabling of the entry trampoline on the kernel command
line (kpti=off) by adding a fake CPU feature (ARM64_UNMAP_KERNEL_AT_EL0)
that can be used to toggle the alternative sequences in our entry code and
avoid use of the trampoline altogether if desired. This also allows us to
make use of a static key in arm64_kernel_unmapped_at_el0().

Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Tested-by: Laura Abbott <labbott@redhat.com>
Tested-by: Shanker Donthineni <shankerd@codeaurora.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Alex Shi <alex.shi@linaro.org>

Conflicts:
	skip non enabled cpu features in
	arch/arm64/include/asm/cpucaps.h and
	arch/arm64/kernel/cpufeature.c
	using cpus_have_cap instead of cpus_have_const_cap in
	arch/arm64/include/asm/mmu.h
---
 arch/arm64/include/asm/cpucaps.h |  3 ++-
 arch/arm64/include/asm/mmu.h     |  3 ++-
 arch/arm64/kernel/cpufeature.c   | 41 ++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kernel/entry.S        |  9 +++++----
 4 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/include/asm/cpucaps.h b/arch/arm64/include/asm/cpucaps.h
index 87b4465..7ddf233 100644
--- a/arch/arm64/include/asm/cpucaps.h
+++ b/arch/arm64/include/asm/cpucaps.h
@@ -34,7 +34,8 @@
 #define ARM64_HAS_32BIT_EL0			13
 #define ARM64_HYP_OFFSET_LOW			14
 #define ARM64_MISMATCHED_CACHE_LINE_SIZE	15
+#define ARM64_UNMAP_KERNEL_AT_EL0		16
 
-#define ARM64_NCAPS				16
+#define ARM64_NCAPS				17
 
 #endif /* __ASM_CPUCAPS_H */
diff --git a/arch/arm64/include/asm/mmu.h b/arch/arm64/include/asm/mmu.h
index 279e75b..a813edf 100644
--- a/arch/arm64/include/asm/mmu.h
+++ b/arch/arm64/include/asm/mmu.h
@@ -34,7 +34,8 @@ typedef struct {
 
 static inline bool arm64_kernel_unmapped_at_el0(void)
 {
-	return IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0);
+	return IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0) &&
+	       cpus_have_cap(ARM64_UNMAP_KERNEL_AT_EL0);
 }
 
 extern void paging_init(void);
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index 3a129d4..74b168c 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -746,6 +746,40 @@ static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry,
 	return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode();
 }
 
+#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
+static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
+
+static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
+				int __unused)
+{
+	/* Forced on command line? */
+	if (__kpti_forced) {
+		pr_info_once("kernel page table isolation forced %s by command line option\n",
+			     __kpti_forced > 0 ? "ON" : "OFF");
+		return __kpti_forced > 0;
+	}
+
+	/* Useful for KASLR robustness */
+	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
+		return true;
+
+	return false;
+}
+
+static int __init parse_kpti(char *str)
+{
+	bool enabled;
+	int ret = strtobool(str, &enabled);
+
+	if (ret)
+		return ret;
+
+	__kpti_forced = enabled ? 1 : -1;
+	return 0;
+}
+__setup("kpti=", parse_kpti);
+#endif	/* CONFIG_UNMAP_KERNEL_AT_EL0 */
+
 static const struct arm64_cpu_capabilities arm64_features[] = {
 	{
 		.desc = "GIC system register CPU interface",
@@ -829,6 +863,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		.def_scope = SCOPE_SYSTEM,
 		.matches = hyp_offset_low,
 	},
+#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
+	{
+		.capability = ARM64_UNMAP_KERNEL_AT_EL0,
+		.def_scope = SCOPE_SYSTEM,
+		.matches = unmap_kernel_at_el0,
+	},
+#endif
 	{},
 };
 
diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
index eccb6d8..54f35cc 100644
--- a/arch/arm64/kernel/entry.S
+++ b/arch/arm64/kernel/entry.S
@@ -73,6 +73,7 @@
 	.macro kernel_ventry, el, label, regsize = 64
 	.align 7
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
+alternative_if ARM64_UNMAP_KERNEL_AT_EL0
 	.if	\el == 0
 	.if	\regsize == 64
 	mrs	x30, tpidrro_el0
@@ -81,6 +82,7 @@
 	mov	x30, xzr
 	.endif
 	.endif
+alternative_else_nop_endif
 #endif
 
 	sub	sp, sp, #S_FRAME_SIZE
@@ -208,10 +210,9 @@ alternative_else_nop_endif
 	ldr	lr, [sp, #S_LR]
 	add	sp, sp, #S_FRAME_SIZE		// restore sp
 
-#ifndef CONFIG_UNMAP_KERNEL_AT_EL0
-	eret
-#else
 	.if	\el == 0
+alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
+#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
 	bne	4f
 	msr	far_el1, x30
 	tramp_alias	x30, tramp_exit_native
@@ -219,10 +220,10 @@ alternative_else_nop_endif
 4:
 	tramp_alias	x30, tramp_exit_compat
 	br	x30
+#endif
 	.else
 	eret
 	.endif
-#endif
 	.endm
 
 	.macro	get_thread_info, rd
-- 
2.7.4

  parent reply	other threads:[~2018-02-28  3:59 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-28  3:56 [PATCH 0/29] arm meltdown fix backporting review for lts 4.9 Alex Shi
2018-02-28  3:56 ` [PATCH 01/29] arm64: mm: Use non-global mappings for kernel space Alex Shi
2018-02-28 12:08   ` Greg KH
2018-03-01 11:53     ` Alex Shi
2018-02-28  3:56 ` [PATCH 02/29] arm64: mm: Move ASID from TTBR0 to TTBR1 Alex Shi
2018-02-28  3:56 ` [PATCH 03/29] arm64: mm: Allocate ASIDs in pairs Alex Shi
2018-02-28  3:56 ` [PATCH 04/29] arm64: mm: Add arm64_kernel_unmapped_at_el0 helper Alex Shi
2018-02-28  3:56 ` [PATCH 05/29] arm64: mm: Invalidate both kernel and user ASIDs when performing TLBI Alex Shi
2018-02-28  3:56 ` [PATCH 06/29] arm64: factor out entry stack manipulation Alex Shi
2018-02-28  3:56 ` [PATCH 07/29] arm64: entry.S: move SError handling into a C function for future expansion Alex Shi
2018-02-28  3:56 ` [PATCH 08/29] module: extend 'rodata=off' boot cmdline parameter to module mappings Alex Shi
2018-02-28  3:56 ` [PATCH 09/29] arm64: entry: Add exception trampoline page for exceptions from EL0 Alex Shi
2018-02-28  3:56 ` [PATCH 10/29] arm64: mm: Map entry trampoline into trampoline and kernel page tables Alex Shi
2018-02-28  3:56 ` [PATCH 11/29] arm64: entry: Explicitly pass exception level to kernel_ventry macro Alex Shi
2018-02-28  3:56 ` [PATCH 12/29] arm64: entry: Hook up entry trampoline to exception vectors Alex Shi
2018-02-28  3:56 ` [PATCH 13/29] arm64: tls: Avoid unconditional zeroing of tpidrro_el0 for native tasks Alex Shi
2018-02-28  3:56 ` Alex Shi [this message]
2018-02-28  3:56 ` [PATCH 15/29] arm64: kaslr: Put kernel vectors address in separate data page Alex Shi
2018-02-28  3:56 ` [PATCH 16/29] arm64: use RET instruction for exiting the trampoline Alex Shi
2018-02-28  3:56 ` [PATCH 17/29] arm64: Kconfig: Add CONFIG_UNMAP_KERNEL_AT_EL0 Alex Shi
2018-02-28  3:56 ` [PATCH 18/29] arm64: Kconfig: Reword UNMAP_KERNEL_AT_EL0 kconfig entry Alex Shi
2018-02-28  3:56 ` [PATCH 19/29] arm64: Take into account ID_AA64PFR0_EL1.CSV3 Alex Shi
2018-02-28  3:56 ` [PATCH 20/29] arm64: Allow checking of a CPU-local erratum Alex Shi
2018-02-28  3:56 ` [PATCH 21/29] arm64: capabilities: Handle duplicate entries for a capability Alex Shi
2018-02-28  3:56 ` [PATCH 22/29] arm64: cputype: Add missing MIDR values for Cortex-A72 and Cortex-A75 Alex Shi
2018-02-28  3:56 ` [PATCH 23/29] arm64: cputype: Add MIDR values for Cavium ThunderX2 CPUs Alex Shi
2018-02-28  3:56 ` [PATCH 24/29] arm64: Turn on KPTI only on CPUs that need it Alex Shi
2018-02-28  3:56 ` [PATCH 25/29] arm64: kpti: Make use of nG dependent on arm64_kernel_unmapped_at_el0() Alex Shi
2018-02-28  3:56 ` [PATCH 26/29] arm64: kpti: Add ->enable callback to remap swapper using nG mappings Alex Shi
2018-02-28  3:56 ` [PATCH 27/29] arm64: Force KPTI to be disabled on Cavium ThunderX Alex Shi
2018-02-28  3:56 ` [PATCH 28/29] arm64: entry: Reword comment about post_ttbr_update_workaround Alex Shi
2018-02-28  3:56 ` [PATCH 29/29] arm64: idmap: Use "awx" flags for .idmap.text .pushsection directives Alex Shi
2018-02-28  4:02 ` [PATCH 0/29] arm meltdown fix backporting review for lts 4.9 Alex Shi
2018-03-01 15:24 ` Greg KH
2018-03-02  9:14   ` Alex Shi
2018-03-02 10:32     ` Marc Zyngier
2018-03-02 16:54     ` Greg KH
2018-03-05 12:46       ` Mark Brown
2018-03-05 13:08         ` Greg KH
2018-03-06 14:26           ` Mark Brown
2018-03-06 17:25             ` Greg KH
2018-03-06 21:31               ` Mark Brown
2018-03-13 10:03                 ` Greg KH
2018-03-07  4:43               ` Alex Shi
2018-03-07  3:27           ` Alex Shi
2018-03-07 18:24       ` Ard Biesheuvel
2018-03-13 10:04         ` Greg KH
2018-03-13 10:13           ` Ard Biesheuvel
2018-03-13 10:38             ` Greg KH
2018-03-13 13:01               ` Ard Biesheuvel
2018-03-13 13:25                 ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1519790211-16582-15-git-send-email-alex.shi@linaro.org \
    --to=alex.shi@linaro.org \
    --cc=ard.biesheuvel@linaro.org \
    --cc=catalin.marinas@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marc.zyngier@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).