Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2252235rwd; Wed, 17 May 2023 07:43:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7taO/LSiPL0dWPfQMJTFpIQnC/5HVrHen8K5q9JFotF/PB2kSTK1RR51VnPDBAIhntnfNG X-Received: by 2002:a05:6a00:ac1:b0:64a:f730:1552 with SMTP id c1-20020a056a000ac100b0064af7301552mr1414400pfl.19.1684334594618; Wed, 17 May 2023 07:43:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684334594; cv=none; d=google.com; s=arc-20160816; b=uwLOI5KVw6YdF6CbfwI6+dckthwO4t+TEtULxH4iEc0tFHe5JXxbflKjTfKs5cEf+h krYgEvjnL6rjxyRo0Nue/ScC/RrwDuskKh4K4FaVraQZnC+Ib07vXcZcs5qo0K2Ti1a7 JdrBGFPBhgDgdIlHxCq/KHqpQ+nwm3rIEC8Kh3fPwWouQqUkCCybjFdCTYCzeo2K094f ILqW9r06YoY78v1DdAkp7Jpkt/HTDsOEHCXVZVZFdl1KPpYumbPltA5bCcPEoBYF0L3B W3yDAi9rK3Fr0/4Vw3pVmtq10Ka1dxJU00n09AvugkvkU02A+FN3FAILjpGwVXoW/AGG KSNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=L3a5bAROSsHuhnL+zzmP9Z2g9Mqzb/pGlEFiWTEF8Ac=; b=TtWcXvST5SSCFtHT1UnM36eEouCMiw+GSZTIvVYRErOic4dD4PHMFmmZF2to+RtQjl 9rWIDRxNggqOnSGeildE0H2e1053hc8b/BfZuJW+dCd2b/PLmaON8JJv9qMKSqlzh9rk feTYCyaOhlQTeEWSXXVvtudPVnrGlEJSk3PZZp1jPkwc0niZB2oJqOL0unfidqdAKE25 L4a7cme8jMdkP759tNblCfLusem9OsZpIObLNjleuaPQ3gwoy9jzNkwnDjaBxZb5IsJt pP981BAQLnvEsOcgYgKzoJ1ox2IZZ8WLTyWFE6jkfwctOPrMvt4C9aD8b/ZHg0LpwKep KZoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QF90qzEa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s10-20020aa78bca000000b00643430939acsi21778628pfd.164.2023.05.17.07.43.03; Wed, 17 May 2023 07:43:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QF90qzEa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229787AbjEQOUF (ORCPT + 99 others); Wed, 17 May 2023 10:20:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232107AbjEQOT6 (ORCPT ); Wed, 17 May 2023 10:19:58 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96C4549C7 for ; Wed, 17 May 2023 07:19:56 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2679F63EE8 for ; Wed, 17 May 2023 14:19:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82F91C4339B; Wed, 17 May 2023 14:19:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684333195; bh=a5GdT+Gc6cojyiH2uD2wUgdqNAz0P+GG+pbONDDq51Q=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=QF90qzEarHBB84DjXmKAn0hFZOFl1Ud1AszbceBwnMONI43D6uxkl3cPk3xvnibsh xe/RJeDS3C32hjsu+GC++DU70FEvkcuQFigjbE3TFgZh+Bdrb5AJitLuXF+mTjbdkb 3sUW0sr1TqVwDzW3XmlWOonixCWczD2ShKabNkMyH+6V60naJD7b9UAWDV6DetkLv4 YF44mM0SEu0d8mFHu8jusvinvDTKyvCYJS3bLiN7ZQBZRdXdc8AC15gYYZoWj4wRot YdWgUyxZsslUUXqo2RtzxtD6RQVotdNTLtsTOdAaMT+337aGoWtn90kfkIvck1KU04 Phvm75+Thk66w== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pzI0P-00FsMd-5D; Wed, 17 May 2023 15:19:53 +0100 Date: Wed, 17 May 2023 15:19:52 +0100 Message-ID: <86pm6zkpkn.wl-maz@kernel.org> From: Marc Zyngier To: Mostafa Saleh Cc: oliver.upton@linux.dev, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, 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 Subject: Re: [PATCH] KVM: arm64: Use BTI for pKVM In-Reply-To: References: <20230516141846.792193-1-smostafa@google.com> <864jocmg75.wl-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/28.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: smostafa@google.com, oliver.upton@linux.dev, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, 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 X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 17 May 2023 09:49:50 +0100, Mostafa Saleh wrote: > > Hi Marc, > > On Tue, May 16, 2023 at 04:47:10PM +0100, Marc Zyngier wrote: > > On Tue, 16 May 2023 15:18:46 +0100, > > Mostafa Saleh wrote: > > > > > > CONFIG_ARM64_BTI_KERNEL compiles the kernel to support ARMv8.5-BTI. > > > However, the nvhe code doesn't make use of it as it doesn't map any > > > pages with Guarded Page(GP) bit. > > > > > > This patch maps pKVM .text section with GP bit which matches the > > > kernel handling for BTI. > > > > Why pKVM only? Surely we can benefit from it all over the nvhe code, > > right? > Yes, I will add it also for nvhe in v2. > > > > > > > A new flag is added to enum kvm_pgtable_prot: KVM_PGTABLE_PROT_GP_S1, > > > which represents BTI guarded page in hypervisor stage-1 page table. > > > > > > Signed-off-by: Mostafa Saleh > > > --- > > > arch/arm64/include/asm/kvm_pgtable.h | 3 +++ > > > arch/arm64/kvm/hyp/nvhe/setup.c | 8 ++++++-- > > > arch/arm64/kvm/hyp/pgtable.c | 6 ++++-- > > > 3 files changed, 13 insertions(+), 4 deletions(-) > > > > > > diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h > > > index 4cd6762bda80..5bcd06d664d3 100644 > > > --- a/arch/arm64/include/asm/kvm_pgtable.h > > > +++ b/arch/arm64/include/asm/kvm_pgtable.h > > > @@ -151,6 +151,7 @@ enum kvm_pgtable_stage2_flags { > > > * @KVM_PGTABLE_PROT_W: Write permission. > > > * @KVM_PGTABLE_PROT_R: Read permission. > > > * @KVM_PGTABLE_PROT_DEVICE: Device attributes. > > > + * @KVM_PGTABLE_PROT_GP_S1: GP(guarded page) used for BTI in stage-1 only > > > * @KVM_PGTABLE_PROT_SW0: Software bit 0. > > > * @KVM_PGTABLE_PROT_SW1: Software bit 1. > > > * @KVM_PGTABLE_PROT_SW2: Software bit 2. > > > @@ -163,6 +164,8 @@ enum kvm_pgtable_prot { > > > > > > KVM_PGTABLE_PROT_DEVICE = BIT(3), > > > > > > + KVM_PGTABLE_PROT_GP_S1 = BIT(50), > > > + > > > KVM_PGTABLE_PROT_SW0 = BIT(55), > > > KVM_PGTABLE_PROT_SW1 = BIT(56), > > > KVM_PGTABLE_PROT_SW2 = BIT(57), > > > diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c > > > index 110f04627785..95f80e2b2946 100644 > > > --- a/arch/arm64/kvm/hyp/nvhe/setup.c > > > +++ b/arch/arm64/kvm/hyp/nvhe/setup.c > > > @@ -66,7 +66,7 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > > > { > > > void *start, *end, *virt = hyp_phys_to_virt(phys); > > > unsigned long pgt_size = hyp_s1_pgtable_pages() << PAGE_SHIFT; > > > - enum kvm_pgtable_prot prot; > > > + enum kvm_pgtable_prot prot = PAGE_HYP_EXEC; > > > int ret, i; > > > > > > /* Recreate the hyp page-table using the early page allocator */ > > > @@ -88,7 +88,11 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > > > if (ret) > > > return ret; > > > > > > - ret = pkvm_create_mappings(__hyp_text_start, __hyp_text_end, PAGE_HYP_EXEC); > > > + /* Hypervisor text is mapped as guarded pages(GP). */ > > > + if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && cpus_have_const_cap(ARM64_BTI)) > > > + prot |= KVM_PGTABLE_PROT_GP_S1; > > > > Is there any reason why this isn't a final cap? I also dislike the > > IS_ENABLED(), but I can see that we don't have separate caps for > > in-kernel BTI and userspace visible BTI... > I was trying to make this close to EL1 code (system_supports_bti()), > I see in hypervisor cpus_have_const_cap is the same as cpus_have_final_cap. Ah, yes. If that's immaterial, then no need for a change. > Yes, I don't see a way to distinguish if BTI was enabled for the kernel > in EL2 without CONFIG_ARM64_BTI_KERNEL. > > > > + > > > + ret = pkvm_create_mappings(__hyp_text_start, __hyp_text_end, prot); > > > if (ret) > > > return ret; > > > > > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > > > index 3d61bd3e591d..028e198acd48 100644 > > > --- a/arch/arm64/kvm/hyp/pgtable.c > > > +++ b/arch/arm64/kvm/hyp/pgtable.c > > > @@ -145,7 +145,8 @@ static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 level) > > > u64 type = (level == KVM_PGTABLE_MAX_LEVELS - 1) ? KVM_PTE_TYPE_PAGE : > > > KVM_PTE_TYPE_BLOCK; > > > > > > - pte |= attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI); > > > + pte |= attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI | > > > + KVM_PGTABLE_PROT_GP_S1); > > > pte |= FIELD_PREP(KVM_PTE_TYPE, type); > > > pte |= KVM_PTE_VALID; > > > > > > @@ -378,7 +379,8 @@ static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep) > > > attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_AP, ap); > > > attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_SH, sh); > > > attr |= KVM_PTE_LEAF_ATTR_LO_S1_AF; > > > - attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW; > > > + attr |= prot & (KVM_PTE_LEAF_ATTR_HI_SW | KVM_PGTABLE_PROT_GP_S1); > > > + > > > > You should probably check that the page is executable before blindly > > accepting to set the GP bit (don't accept it for non-exec pages). > Will do in v2. > > > Another thing to check would be the state of SCTLR_EL2.BT, which I > > think we clear by construction, but it be worth having a look. > Yes, I see it is initialised by zero in ___kvm_hyp_init in hyp-init.S, > I believe this should be changed to 1 when BTI is enabled (as in > bti_enable() for EL1), I will update it. Yup, I'd like to see some level of consistency with what we do at EL1, at least so that we are all aligned on what we allow from an ABI perspective. Thanks, M. -- Without deviation from the norm, progress is not possible.