From mboxrd@z Thu Jan 1 00:00:00 1970 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 mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06F72C433F5 for ; Sun, 21 Nov 2021 18:46:51 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 666814B15E; Sun, 21 Nov 2021 13:46:51 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MA7CnkzRc03G; Sun, 21 Nov 2021 13:46:49 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 81C454B14D; Sun, 21 Nov 2021 13:46:49 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 83A434B13D for ; Sun, 21 Nov 2021 13:46:48 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id peqbcfXiWuSG for ; Sun, 21 Nov 2021 13:46:46 -0500 (EST) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 9FEC34B139 for ; Sun, 21 Nov 2021 13:46:46 -0500 (EST) Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 59516601FF; Sun, 21 Nov 2021 18:46:45 +0000 (UTC) Received: from ip-185-104-136-29.ptr.icomera.net ([185.104.136.29] helo=wait-a-minute.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1morrO-006vG1-V2; Sun, 21 Nov 2021 18:46:43 +0000 Date: Sun, 21 Nov 2021 18:46:41 +0000 Message-ID: <87fsrps5wu.wl-maz@kernel.org> From: Marc Zyngier To: Reiji Watanabe Subject: Re: [RFC PATCH v3 21/29] KVM: arm64: Introduce framework to trap disabled features In-Reply-To: <20211117064359.2362060-22-reijiw@google.com> References: <20211117064359.2362060-1-reijiw@google.com> <20211117064359.2362060-22-reijiw@google.com> 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/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.104.136.29 X-SA-Exim-Rcpt-To: reijiw@google.com, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, pbonzini@redhat.com, will@kernel.org, drjones@redhat.com, liangpeng10@huawei.com, pshier@google.com, ricarkol@google.com, oupton@google.com, jingzhangos@google.com, rananta@google.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Cc: kvm@vger.kernel.org, Will Deacon , Peter Shier , Paolo Bonzini , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On Wed, 17 Nov 2021 06:43:51 +0000, Reiji Watanabe wrote: > > When a CPU feature that is supported on the host is not exposed to > its guest, emulating a real CPU's behavior (by trapping or disabling > guest's using the feature) is generally a desirable behavior (when > it's possible without any or little side effect). > > Introduce feature_config_ctrl structure, which manages feature > information to program configuration register to trap or disable > the feature when the feature is not exposed to the guest, and > functions that uses the structure to activate trapping the feature. > > At present, no feature has feature_config_ctrl yet and the following > patches will add the feature_config_ctrl for several features. > > Signed-off-by: Reiji Watanabe > --- > arch/arm64/kvm/sys_regs.c | 121 +++++++++++++++++++++++++++++++++++++- > 1 file changed, 120 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c > index 2f96103fc0d2..501de08dacb7 100644 > --- a/arch/arm64/kvm/sys_regs.c > +++ b/arch/arm64/kvm/sys_regs.c > @@ -376,8 +376,38 @@ static int arm64_check_features(u64 check_types, u64 val, u64 lim) > (cpuid_feature_extract_unsigned_field(val, ID_AA64ISAR1_GPI_SHIFT) >= \ > ID_AA64ISAR1_GPI_IMP_DEF) > > +enum vcpu_config_reg { > + VCPU_HCR_EL2 = 1, > + VCPU_MDCR_EL2, > + VCPU_CPTR_EL2, > +}; > + > +/* > + * Feature information to program configuration register to trap or disable > + * guest's using a feature when the feature is not exposed to the guest. > + */ > +struct feature_config_ctrl { > + /* ID register/field for the feature */ > + u32 ftr_reg; /* ID register */ > + bool ftr_signed; /* Is the feature field signed ? */ > + u8 ftr_shift; /* Field of ID register for the feature */ > + s8 ftr_min; /* Min value that indicate the feature */ > + > + /* > + * Function to check trapping is needed. This is used when the above > + * fields are not enough to determine if trapping is needed. > + */ > + bool (*ftr_need_trap)(struct kvm_vcpu *vcpu); > + > + /* Configuration register information to trap the feature. */ > + enum vcpu_config_reg cfg_reg; /* Configuration register */ > + u64 cfg_mask; /* Field of the configuration register */ > + u64 cfg_val; /* Value that are set for the field */ Although this probably works for the use cases you have in mind, some trap bits are actually working the other way around (clear to trap). So you probably want to turn this into cfg_set and add a cfg_clear for a good measure, dropping cfg_mask in the process. That being said, the current trend is to move to FGT, meaning that a single register is unlikely to cut it in the long run. I'd rather you simply have a configuration function here (and the helper you already have is probably enough). > +}; > + > struct id_reg_info { > u32 sys_reg; /* Register ID */ > + u64 sys_val; /* Sanitized system value */ > > /* > * Limit value of the register for a vcpu. The value is the sanitized > @@ -410,11 +440,15 @@ struct id_reg_info { > /* Return the reset value of the register for the vCPU */ > u64 (*get_reset_val)(struct kvm_vcpu *vcpu, > const struct id_reg_info *id_reg); > + > + /* Information to trap features that are disabled for the guest */ > + const struct feature_config_ctrl *(*trap_features)[]; > }; > > static void id_reg_info_init(struct id_reg_info *id_reg) > { > - id_reg->vcpu_limit_val = read_sanitised_ftr_reg(id_reg->sys_reg); > + id_reg->sys_val = read_sanitised_ftr_reg(id_reg->sys_reg); > + id_reg->vcpu_limit_val = id_reg->sys_val; > if (id_reg->init) > id_reg->init(id_reg); > } > @@ -952,6 +986,47 @@ static int validate_id_reg(struct kvm_vcpu *vcpu, > return err; > } > > +static void feature_trap_activate(struct kvm_vcpu *vcpu, > + const struct feature_config_ctrl *config) > +{ > + u64 *reg_ptr, reg_val; > + > + switch (config->cfg_reg) { > + case VCPU_HCR_EL2: > + reg_ptr = &vcpu->arch.hcr_el2; > + break; > + case VCPU_MDCR_EL2: > + reg_ptr = &vcpu->arch.mdcr_el2; > + break; > + case VCPU_CPTR_EL2: > + reg_ptr = &vcpu->arch.cptr_el2; > + break; > + } > + > + /* Update cfg_mask fields with cfg_val */ > + reg_val = (*reg_ptr & ~config->cfg_mask); > + reg_val |= config->cfg_val; > + *reg_ptr = reg_val; > +} > + > +static inline bool feature_avail(const struct feature_config_ctrl *ctrl, > + u64 id_val) > +{ > + int field_val = cpuid_feature_extract_field(id_val, > + ctrl->ftr_shift, ctrl->ftr_signed); > + > + return (field_val >= ctrl->ftr_min); > +} > + > +static inline bool vcpu_feature_is_available(struct kvm_vcpu *vcpu, > + const struct feature_config_ctrl *ctrl) > +{ > + u64 val; > + > + val = __read_id_reg(vcpu, ctrl->ftr_reg); > + return feature_avail(ctrl, val); > +} > + > /* > * ARMv8.1 mandates at least a trivial LORegion implementation, where all the > * RW registers are RES0 (which we can implement as RAZ/WI). On an ARMv8.0 > @@ -1831,6 +1906,42 @@ static int reg_from_user(u64 *val, const void __user *uaddr, u64 id); > static int reg_to_user(void __user *uaddr, const u64 *val, u64 id); > static u64 sys_reg_to_index(const struct sys_reg_desc *reg); > > +static void id_reg_features_trap_activate(struct kvm_vcpu *vcpu, > + const struct id_reg_info *id_reg) > +{ > + u64 val; > + int i = 0; > + const struct feature_config_ctrl **ctrlp_array, *ctrl; > + > + if (!id_reg || !id_reg->trap_features) > + /* No information to trap a feature */ > + return; > + > + val = __read_id_reg(vcpu, id_reg->sys_reg); > + if (val == id_reg->sys_val) > + /* No feature needs to be trapped (no feature is disabled). */ > + return; > + > + ctrlp_array = *id_reg->trap_features; > + while ((ctrl = ctrlp_array[i++]) != NULL) { > + if (ctrl->ftr_need_trap && ctrl->ftr_need_trap(vcpu)) { > + feature_trap_activate(vcpu, ctrl); > + continue; > + } > + > + if (!feature_avail(ctrl, id_reg->sys_val)) > + /* The feature is not supported on the host. */ > + continue; > + > + if (feature_avail(ctrl, val)) > + /* The feature is enabled for the guest. */ > + continue; > + > + /* The feature is supported but disabled. */ > + feature_trap_activate(vcpu, ctrl); > + } > +} > + > /* Visibility overrides for SVE-specific control registers */ > static unsigned int sve_visibility(const struct kvm_vcpu *vcpu, > const struct sys_reg_desc *rd) > @@ -3457,6 +3568,14 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) > return write_demux_regids(uindices); > } > > +void kvm_vcpu_init_traps(struct kvm_vcpu *vcpu) Who is going to call this? At which point? Please document the use constraints on this. > +{ > + int i; > + > + for (i = 0; i < ARRAY_SIZE(id_reg_info_table); i++) > + id_reg_features_trap_activate(vcpu, id_reg_info_table[i]); > +} > + > /* ID register's fractional field information with its feature field. */ > struct feature_frac { > u32 id; > -- > 2.34.0.rc1.387.gb447b232ab-goog > > Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm