From patchwork Wed Jul 5 17:16:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mostafa Saleh X-Patchwork-Id: 13302428 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 469B1EB64DD for ; Wed, 5 Jul 2023 17:17:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: Mime-Version:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=D23ZZFglfUiWWx+TDHoqDkY6VQO61bG0f8vf7k2g6qE=; b=doV GpUo5ga+6e49JE6s0AC4Vb3CRy6YXLhuwfL/BTZ3kTPUyz3J/vPJR+UT3nQwHf7pVOnqpA2YHgOJG 7ed+Fz8T9Fz3FPuJUEMUvE0P4YtoG8izZSlc2G6nbgcWe5I3z6oJjpd7f9gAX6ULJGsEQZ2Gdn0zA vPykx0ZtzQ84pPvlnsA9HzqoU+T55amDx3QkbTA4oxQn2IGqE7UhKV+tf/K3SYB50Y13o6eMHeZti Dehx6k2uLSnZBpMVbgDZxcwi14NHBRtkyVtlJvbIf/W0f9EVwPoViWRB0OdYc5hdeVCq6ykCFlG1e EIwd6fb/3SVDHsqXuqcdp3J7JqipE/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qH67O-00GaKL-2k; Wed, 05 Jul 2023 17:16:42 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qH67M-00GaJa-0D for linux-arm-kernel@lists.infradead.org; Wed, 05 Jul 2023 17:16:41 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-3143b999f6eso1596670f8f.3 for ; Wed, 05 Jul 2023 10:16:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688577397; x=1691169397; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=JinL7apUcf1gP8XFQ2zINdZou7Iz++svzqv8hjqcpG4=; b=qhATiPaQGUeviA9TREjpGVzTZx+eTBG89ShqA4kl7lRGvGT2pMGvWUaqt7NNkQzxyq /8PfVlQzl2LO6vi9TG7V/WxWFIjejptKtQ3gE+KS0fttIaQaez4asKtUnqd5AUYlLpuD awhMObsKdv38WIDH0dL6OgfJy5Ni8d4zakklW1EH4W0zfmNzHTBiGO0AhOyjihEADCxH Fg9FGroFeOnM7yzvUcvZ3ud/7en6PdC+nOVuASFjR7k1Q1TKyabCB3ANMsSDPWmGrMd3 dsBf2Q/ywjG5B7GVmP2r7lTkTjtFXCkkDKTM8UkQgRORQmnMuSdIO/t0keG1MCfuxilF 52pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688577397; x=1691169397; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=JinL7apUcf1gP8XFQ2zINdZou7Iz++svzqv8hjqcpG4=; b=aa8GSmnqpCZ/8vorrsF6tg5ZWFCgq5GAYUpX5bU7JqMXL6dlHlYOZ1uKvC15tv7jIE GMK9/FPl/gZadJzG7QbzfqsoJ3BhT1M7Z3dJ9WBR+S+CyXSNlRQtmQRg6/TBS8OHsHvM WsTv9IKv1TNUIs24tCVK6Mn6UxH9G697SvmLSjOJXcFzl3IeLAe6CM0oNYgxOxGfcnnH r1kENVl/JAKwEh2cuX/tjp7TPudRAisVoYD14m6i4vBUZhBc8X9irfOAqG6GJFG9wzGa 9H6lkP3D8ssRlHWY6I4nRsPsgFhd7FwXVpFQFSOCSddLpTyRW99TPFGMbLvdQHPCwYT/ RUiA== X-Gm-Message-State: ABy/qLa4nRWr+NPhiMPnp61PZ7URnMdQ9Z5FXKgEm+Q9kCyR76goRFWD VMA8dz5bfrJYRdaVUC2UNQUlnkVC3ZrPkA== X-Google-Smtp-Source: APBJJlFkHZZLMEUKPg1ln3ifk7FwbaHB5Cy7w0D302g94jAtmOYJbK1FmiBpCETF2/qG1bslXczmFq8a2EmO8w== X-Received: from mostafa.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:333c]) (user=smostafa job=sendgmr) by 2002:adf:dd0a:0:b0:313:ff50:2855 with SMTP id a10-20020adfdd0a000000b00313ff502855mr108174wrm.9.1688577396831; Wed, 05 Jul 2023 10:16:36 -0700 (PDT) Date: Wed, 5 Jul 2023 17:16:32 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230705171632.3912123-1-smostafa@google.com> Subject: [PATCH] KVM: arm64: Add missing BTI instruction in kvm_host_psci_cpu_entry From: Mostafa Saleh To: maz@kernel.org, oliver.upton@linux.dev, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Cc: tabba@google.com, qperret@google.com, will@kernel.org, catalin.marinas@arm.com, yuzenghui@huawei.com, suzuki.poulose@arm.com, james.morse@arm.com, bgardon@google.com, gshan@redhat.com, sudeep.holla@arm.com, Mostafa Saleh X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230705_101640_103732_47BE5570 X-CRM114-Status: GOOD ( 14.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org kvm_host_psci_cpu_entry is called from __kvm_hyp_init_cpu through "br" instruction as __kvm_hyp_init_cpu resides in idmap section while kvm_host_psci_cpu_entry is in hyp .text so the offset is larger than 26 bits covered by "b". Which means that this function should start with "bti j" instruction. LLVM which is the only compiler supporting BTI for Linux, adds "bti j" for jump tables or by when taking the address of the block [1]. And same behaviour is observed with GCC. As kvm_host_psci_cpu_entry is a C function, this must be done in assembly. Another solution is to use X16/X17 with "br", as according to ARM ARM DDI0487I.a RLJHCL/IGMGRS, PACIASP has an implicit branch target identification instruction that is compatible with PSTATE.BTYPE 0b01 which includes "br X16/X17" And the kvm_host_psci_cpu_entry has PACIASP as it is an external function. Although I see that using explicit "bti" makes more clear than relying on which register is used. A third solution is to clear SCTLR_EL2.BT, which would make PACIASP compatible PSTATE.BTYPE 0b11 ( "br" to other registers). However this deviates from the kernel behaviour (in bti_enable()). [1] https://reviews.llvm.org/D52867 Fixes: b53d4a272349 ("KVM: arm64: Use BTI for nvhe") Signed-off-by: Mostafa Saleh Reported-and-tested-by: Sudeep Holla --- arch/arm64/kvm/hyp/nvhe/host.S | 10 ++++++++++ arch/arm64/kvm/hyp/nvhe/psci-relay.c | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index c87c63133e10..9d6e95b32097 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -297,3 +297,13 @@ SYM_CODE_START(__kvm_hyp_host_forward_smc) ret SYM_CODE_END(__kvm_hyp_host_forward_smc) + +/* + * kvm_host_psci_cpu_entry is called through br instruction, which requires + * BTI J instruction, compilers (gcc and llvm) doesn't insert BTI J for external + * functions, but BTI C instead. + */ +SYM_CODE_START(kvm_host_psci_cpu_entry) + bti j + b __kvm_host_psci_cpu_entry +SYM_CODE_END(kvm_host_psci_cpu_entry) diff --git a/arch/arm64/kvm/hyp/nvhe/psci-relay.c b/arch/arm64/kvm/hyp/nvhe/psci-relay.c index 08508783ec3d..24543d2a3490 100644 --- a/arch/arm64/kvm/hyp/nvhe/psci-relay.c +++ b/arch/arm64/kvm/hyp/nvhe/psci-relay.c @@ -200,7 +200,7 @@ static int psci_system_suspend(u64 func_id, struct kvm_cpu_context *host_ctxt) __hyp_pa(init_params), 0); } -asmlinkage void __noreturn kvm_host_psci_cpu_entry(bool is_cpu_on) +asmlinkage void __noreturn __kvm_host_psci_cpu_entry(bool is_cpu_on) { struct psci_boot_args *boot_args; struct kvm_cpu_context *host_ctxt;