Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp426472lqp; Sat, 13 Apr 2024 03:19:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVn9rhFjlURzbC9jVRJtCy+agfdmASjlbL64iN+fVJDaP6HdlspvbyVgzrDxViFgj/ZfgC/DMIU68mayFKMQYQDxA3VCBFJ0VCkA6aL6A== X-Google-Smtp-Source: AGHT+IFHe3k+MNqcRMYsPA4fOLIV1VIVSuaMSHrrxYvMdPjBdUz4vfFY2C7z6WtVuXWzVKIfJjnj X-Received: by 2002:a17:906:6809:b0:a52:54bb:71b5 with SMTP id k9-20020a170906680900b00a5254bb71b5mr193483ejr.12.1713003587597; Sat, 13 Apr 2024 03:19:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713003587; cv=pass; d=google.com; s=arc-20160816; b=o9KfN22xFyliV2iVjlOaceHapwCXG+Yfcg0NPpM212RK6Hzxvrjb/FcGqYDJs6PKwM ne+ivML/3oqc/bu5OcZN51A6Kkqcjoeq7F2j/nECy4E09Q2+0nKQ5/5mRLTJ23D3Tz9e MDJeR+K1BPbRDqOZHn7Ai9kBkLf5xWqo483God4gpbwKK+6ObvMJ7hIEv8bHp1OH3Gk/ YKmYoICz7pil4zeaSNGwOx517IFbcepvTLOj10ffkol/BsnWjqMGZaColQobkWMwj8EM Yma0PznZcB33S+oMdn+UTV5Mrh+Bpvxug+W+o18GiyFgjdI9ViEcLJ36WFgN2i9LoGrc BXGQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature; bh=Hcb3jCqFyrnCzkLiZgDLhcQSakO73E+7EUsAQefj2S4=; fh=0hc3dUGA5pT/daIdk5fmn4nrXMhnORCs1NUzkzkbS0c=; b=BlQTPtYcvkx12f3A1z1ys8bxlY6cvVdrb4s6yJHgw1LZ4ilNkIjayLS4ambQD3XOzY CHwLMjQBwc3T2YmjnCZi+T//1g7etnXj0vb83jPkuLHpf1njNxN2YwZMxfXYh40GC/KK svgJFMyyPiTVNMdMulShrse55wSAU/8F4HP09YtWCdUHJrdhqOpXzU7E3Zgvx+YUQgGo 1UHeVFUGzK9nc+72srOnpmzT6Dng0Wq9Hw9RJBYo4oBMfQqR2d8wKKHSwsKmrqSRyw3W 4zTdopqcmTzfGF/7qFBAYJC48DYo+4ut5nANG6IT2Q9Q2JZt5Ijk52S1UQ9h9PNrO0z8 BfjQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=c29dNQ+y; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-143673-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143673-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z24-20020a170906715800b00a524d96b9cesi414538ejj.11.2024.04.13.03.19.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Apr 2024 03:19:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-143673-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=c29dNQ+y; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-143673-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143673-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2DA621F22219 for ; Sat, 13 Apr 2024 10:19:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C50343D547; Sat, 13 Apr 2024 10:19:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="c29dNQ+y" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C25451CFA8; Sat, 13 Apr 2024 10:19:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713003579; cv=none; b=j9U9t2BlWA9OQctnEcwD0xBCZg9ECltG0O1/4TnQfMdSemMdYP8R3+auv8JpvO3Ytpcq/VA5vpA+TEBx3fnHPOdKrkE+xjHjQhkjr84d8qm0srOJmCLgxbT0SwUDr/sL0MvCp+Cl4iMWrXl4MqF/gLLykhu3siEvfOPJU+omxMY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713003579; c=relaxed/simple; bh=9Hu40LkvlfQcCNkyBLs/RLhZHlI4yg/7v8dPJiVYyEs=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=brgSzYd1hRa4gLodZ7ktpKw0OrQiYGIA27scsYAsjgMlOAKijwBdWtK4PQHOEzaKYPuCstQa92KhJD/toz1ah2iHRj4Go3Lj4oXP1pnI3MUsxOVPqI5HUA2628zFZt20U7UjzxrxV2GlZKCn63nAvdV5YLFI1JRB0uN0HWhL9GY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=c29dNQ+y; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C37BC4AF07; Sat, 13 Apr 2024 10:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713003579; bh=9Hu40LkvlfQcCNkyBLs/RLhZHlI4yg/7v8dPJiVYyEs=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=c29dNQ+yEubEhbE3XnXXfhoAGCvaWZ5K9uB9X21vxDxDhDmr4B+n0VE6ouSrj7cG1 pjpqVKhq7uzDJ3WOpuR2H7UCz2V0J4CPBEdUN09AHuQuV3VAXxI+oGYb/LOZMqQ/Q0 eZ5ZP3jd0EkZty/k32oHNI2pTXOM3ZM47DKIJRfetlxyNJXfgt2EXfVGpHHXf3WlM+ PWiZG9yn7GLfpOtiXXYA6zlCFf0813Lc39o8CyLCkaZde2t13pXUW9BWdzGVRUpAIQ L8FqQc5mvo+ewtlQtGXy7rGYpwNsHbgpdRc1xkgAlUDkv5ZOpmZJGWfZb4Y2RKPXRB W7XOKRXg7YY/A== 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 1rvaTx-0047xk-6h; Sat, 13 Apr 2024 11:19:37 +0100 Date: Sat, 13 Apr 2024 11:19:35 +0100 Message-ID: <86edb9sgy0.wl-maz@kernel.org> From: Marc Zyngier To: Sebastian Ott Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Catalin Marinas , Will Deacon Subject: Re: [PATCH 3/4] KVM: arm64: add emulation for CTR_EL0 register In-Reply-To: <20240405120108.11844-4-sebott@redhat.com> References: <20240405120108.11844-1-sebott@redhat.com> <20240405120108.11844-4-sebott@redhat.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/29.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: sebott@redhat.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Fri, 05 Apr 2024 13:01:07 +0100, Sebastian Ott wrote: > > CTR_EL0 is currently handled as an invariant register, thus > guests will be presented with the host value of that register. > Add emulation for CTR_EL0 based on a per VM value. > > When CTR_EL0 is changed the reset function for CLIDR_EL1 is > called to make sure we present the guest with consistent > register values. Isn't that a change in the userspace ABI? You are now creating an explicit ordering between the write to CTR_EL0 and the rest of the cache hierarchy registers. It has the obvious capacity to lead to the wrong result in a silent way... > > Signed-off-by: Sebastian Ott > --- > arch/arm64/kvm/sys_regs.c | 72 ++++++++++++++++++++++++++++++++++----- > 1 file changed, 64 insertions(+), 8 deletions(-) > > diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c > index 4d29b1a0842d..b0ba292259f9 100644 > --- a/arch/arm64/kvm/sys_regs.c > +++ b/arch/arm64/kvm/sys_regs.c > @@ -1874,6 +1874,55 @@ static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, > return true; > } > > +static u64 reset_ctr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd) > +{ > + vcpu->kvm->arch.ctr_el0 = 0; > + return kvm_get_ctr_el0(vcpu->kvm); I'd expect the cached value to be reset instead of being set to 0. What are you achieving by this? > +} > + > +static int get_ctr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, > + u64 *val) > +{ > + *val = kvm_get_ctr_el0(vcpu->kvm); > + return 0; > +} > + > +static const struct sys_reg_desc *get_sys_reg_desc(u32 encoding); > + > +static int set_ctr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, > + u64 val) > +{ > + u64 host_val = read_sanitised_ftr_reg(SYS_CTR_EL0); > + u64 old_val = kvm_get_ctr_el0(vcpu->kvm); > + const struct sys_reg_desc *clidr_el1; > + int ret; > + > + if (val == old_val) > + return 0; > + > + if (kvm_vm_has_ran_once(vcpu->kvm)) > + return -EBUSY; > + > + mutex_lock(&vcpu->kvm->arch.config_lock); > + ret = arm64_check_features(vcpu, rd, val); > + if (ret) { > + mutex_unlock(&vcpu->kvm->arch.config_lock); > + return ret; > + } > + if (val != host_val) > + vcpu->kvm->arch.ctr_el0 = val; > + else > + vcpu->kvm->arch.ctr_el0 = 0; > + > + mutex_unlock(&vcpu->kvm->arch.config_lock); > + > + clidr_el1 = get_sys_reg_desc(SYS_CLIDR_EL1); > + if (clidr_el1) > + clidr_el1->reset(vcpu, clidr_el1); > + > + return 0; No check against what can be changed, and in what direction? You seem to be allowing a guest to migrate from a host with IDC==1 to one where IDC==0 (same for DIC). How can that work? Same for the cache lines, which can be larger on the target... How will the guest survive that? > +} > + > static bool access_clidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, > const struct sys_reg_desc *r) > { > @@ -2460,7 +2509,11 @@ static const struct sys_reg_desc sys_reg_descs[] = { > { SYS_DESC(SYS_CCSIDR2_EL1), undef_access }, > { SYS_DESC(SYS_SMIDR_EL1), undef_access }, > { SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 }, > - { SYS_DESC(SYS_CTR_EL0), access_ctr }, > + { SYS_DESC(SYS_CTR_EL0), access_ctr, .reset = reset_ctr, > + .get_user = get_ctr, .set_user = set_ctr, .val = (CTR_EL0_DIC_MASK | > + CTR_EL0_IDC_MASK | > + CTR_EL0_DminLine_MASK | > + CTR_EL0_IminLine_MASK)}, > { SYS_DESC(SYS_SVCR), undef_access }, > > { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr, > @@ -3623,6 +3676,13 @@ static bool index_to_params(u64 id, struct sys_reg_params *params) > } > } > > +static const struct sys_reg_desc *get_sys_reg_desc(u32 encoding) > +{ > + struct sys_reg_params params = encoding_to_params(encoding); > + > + return find_reg(¶ms, sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); > +} > + > const struct sys_reg_desc *get_reg_by_id(u64 id, > const struct sys_reg_desc table[], > unsigned int num) > @@ -3676,18 +3736,11 @@ FUNCTION_INVARIANT(midr_el1) > FUNCTION_INVARIANT(revidr_el1) > FUNCTION_INVARIANT(aidr_el1) > > -static u64 get_ctr_el0(struct kvm_vcpu *v, const struct sys_reg_desc *r) > -{ > - ((struct sys_reg_desc *)r)->val = read_sanitised_ftr_reg(SYS_CTR_EL0); > - return ((struct sys_reg_desc *)r)->val; > -} > - > /* ->val is filled in by kvm_sys_reg_table_init() */ > static struct sys_reg_desc invariant_sys_regs[] __ro_after_init = { > { SYS_DESC(SYS_MIDR_EL1), NULL, get_midr_el1 }, > { SYS_DESC(SYS_REVIDR_EL1), NULL, get_revidr_el1 }, > { SYS_DESC(SYS_AIDR_EL1), NULL, get_aidr_el1 }, > - { SYS_DESC(SYS_CTR_EL0), NULL, get_ctr_el0 }, > }; > > static int get_invariant_sys_reg(u64 id, u64 __user *uaddr) > @@ -4049,6 +4102,9 @@ void kvm_init_sysreg(struct kvm_vcpu *vcpu) > vcpu->arch.hcrx_el2 |= (HCRX_EL2_MSCEn | HCRX_EL2_MCE2); > } > > + if (vcpu->kvm->arch.ctr_el0) > + vcpu->arch.hcr_el2 |= HCR_TID2; Why trap CTR_EL0 if the values are the same as the host? I really dislike the use of the value 0 as a such an indication. Why isn't this grouped with the traps in vcpu_reset_hcr()? Thanks, M. -- Without deviation from the norm, progress is not possible.