Received: by 10.223.185.116 with SMTP id b49csp5727310wrg; Tue, 27 Feb 2018 20:00:40 -0800 (PST) X-Google-Smtp-Source: AG47ELvrRBlc+i4SwIrcP+77L9vdn+6KzGRnv7wZ00v/wKpcReWNoT3CUmAKH2u4myi+cxWHLQlv X-Received: by 2002:a17:902:5a44:: with SMTP id f4-v6mr6894737plm.116.1519790440117; Tue, 27 Feb 2018 20:00:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519790440; cv=none; d=google.com; s=arc-20160816; b=g+nfYdiMy4HSg6dDkSXlbVGMxgaEmnwA+QMvMcQv7EY3AkZzK1k+KYeJBsjN/HR3AV QtPmVHvPQMKLModKLP61j47/Sy1AVB9ASiaP5oGarnBUXD6bSZyUTFYGScpK2q27i0t/ 1e7OqzIcTHHW+LI9jP9z3PxmpGnl/f/lJ1Kk1J82m7mO1ND3wSaNXls5qds3Czb6bDwe y9gIZDRfFU9vw73JWtOvRT+cST/zPZAYHlSe/7MbPVmsxESv9kVi1h1Rc8h2FR2hIzst 7iccQjILiKslyIXEnaxE+QSzLbjfyn1FyippxA2GBzfiRhrdchbPDoIcK3+MTcMf7c7y AeVA== 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=mw1ugMQkrRom83ynaVYoq/Tb8k4WnH3naAwpWK3t9VY=; b=WrK7Ims6qKXV+Xe5mBMJiivyDc9+uqKTPhlRLcaqShrDBD6DbyEo/D7hECsj3vNj4X Awun53l0tzPEYaOkfB5lEqmJte0XkCcmTbGElhPtrZOFK43zbCA/16zlBVK1MP1JSPSe UV5/gDYHKzMc1+HjVX17IFZnsssl+6ILY1JYo0hlYoSVf1ZrtASq+wjbUB2CGt2GzV2c gndUgowu1U8Ryq+pytVgkQrSw5KvH+5+4XchJOH5WnTL7OsFs0P9XW0Tvtgxg2CkwnsI vAUV0edxa9W9ql7uyhvkgiIIV/vHeNIDR+ctiZR6lGv/JQydd1SilZCGSyFfUmTSODon Gjpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WjFaltI0; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b89-v6si569970plb.809.2018.02.27.20.00.25; Tue, 27 Feb 2018 20:00:40 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=WjFaltI0; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932104AbeB1D7U (ORCPT + 99 others); Tue, 27 Feb 2018 22:59:20 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:43138 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932088AbeB1D7Q (ORCPT ); Tue, 27 Feb 2018 22:59:16 -0500 Received: by mail-pg0-f66.google.com with SMTP id e9so477992pgs.10 for ; Tue, 27 Feb 2018 19:59:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mw1ugMQkrRom83ynaVYoq/Tb8k4WnH3naAwpWK3t9VY=; b=WjFaltI08pTdm+kzBYOFtt4iDK7PidHLpFDoJAu4/JwHJxT17Wu1aphabty+nVIbvv VMcTErx8cc9VbtJVVk9seKpXKIWi4y+rfDC9opqCrxthXo+2bltaoZBqt38wN85P0EC9 r1+KxGwQbCZ2hVwAmrfjgUkwsDG6ejDyfBU5I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mw1ugMQkrRom83ynaVYoq/Tb8k4WnH3naAwpWK3t9VY=; b=fiG4uEdnF4Egt0qHA7FJ7zJhwuWg6dNMcrfFtGMKaxMNZnJgK8EEQNgLCBRX63vhck +tcOQO1U7px0c5iuvv3YLRbshiZhBR7xTcNcCTlyK+Qv2FPFkWFKDxNoGPNcg2BZlONY OZq1Q0AuNrgGhoV9OztVdu5o98aBNJlB8YLgGPrRdb36aGp/gGiW9SMKSGTaFl56dITu GVDVgQ/pvScc2OyujPZOQoQufgnGqSkRC/pRAx4vMIoupRfwudW+X4FBG3HZREaxPfUy sGkfPEPWNNsMY/GhgWT50iLpY/ihl9fSF+tFg1JbV7t8IxDLSAO8puGPmVPJWkqGNPWc CQvA== X-Gm-Message-State: APf1xPBdAggpyixZgVsDfOTbKyiRp66X5wBjMepHxLiaBMnz7RzOXvTh d+hB9oLGE54LecdOPJpewBzOPA== X-Received: by 10.99.9.195 with SMTP id 186mr12972734pgj.152.1519790356397; Tue, 27 Feb 2018 19:59:16 -0800 (PST) Received: from localhost.localdomain (176.122.172.82.16clouds.com. [176.122.172.82]) by smtp.gmail.com with ESMTPSA id q17sm739911pgt.7.2018.02.27.19.59.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Feb 2018 19:59:15 -0800 (PST) From: Alex Shi To: Marc Zyngier , Will Deacon , Ard Biesheuvel , Catalin Marinas , stable@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alex Shi 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 Message-Id: <1519790211-16582-15-git-send-email-alex.shi@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519790211-16582-1-git-send-email-alex.shi@linaro.org> References: <1519790211-16582-1-git-send-email-alex.shi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Will Deacon 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 Tested-by: Laura Abbott Tested-by: Shanker Donthineni Signed-off-by: Will Deacon Signed-off-by: Alex Shi 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