Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1000083lqs; Fri, 14 Jun 2024 11:32:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVQY2MYUv7lGtgaOf3yH9noxYbXrWqzeM3g1rq5Uz3Iv7wZJrNjP+x+RNj++TW8lYyIc1WL6WHm1Qr6b2HAe6cIytmAgioBumrovdAX+A== X-Google-Smtp-Source: AGHT+IHI62K6w3ILsqag4XlAmz4bRaW42MXkjd1yNY5SfdXl0gISRIhd3jinjw3LKfowu9mp34k/ X-Received: by 2002:a17:907:9412:b0:a6e:f847:2cd9 with SMTP id a640c23a62f3a-a6f60d1ce5bmr294773666b.21.1718389979536; Fri, 14 Jun 2024 11:32:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718389979; cv=pass; d=google.com; s=arc-20160816; b=1JmkSp79ZwmtysypOyW/YNGiuEAxaQwUWOnXk5IjsHclEK9KHTNfMEj69bPw5GZzTu B+/S6M7eBRsIbfqTE0vprfYcpW1kVNSx26cE9UbrBPP67/cvXE832o0KIHjqjck2k+Cv H7wXiLkI/rpiOkNGcrrmEtcvac53zkFAd5xSJCUj2m1LKqzCXjOgcksiwsXh5lL0zwC5 5kgg04ocwLM4NpTpmUAKD5EHgE36S/m+jmfZQTSRxqo4iKZFvB4KHwOXqvYJOXlbAwnW INcVrjuXJTwnvCV2vbdq6pI5p+u2AFJoSkjmQNcCIXR8umLvkNBO3cCfzI0qx+25rs4k wrDQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=bfjAgyWtBbRxONbdiKFFSvpzW16ct8ELGbCI1RgaYDo=; fh=2mwmJ8oiUSHX2cuqXbjc8IopOBggrj22SLn9Yxnddik=; b=k4sEsXtrbPFtIjiYfamskDmyDLPAAk2ZrXw+x4GKGpHjjpli1F4Ubkn4BkljYQ/I/L zzCfKd70TZRvk6z7lZIaGykLFSz0laZ6/nzoFB6uR3y6Pr9pEITPhMUdSa2SAoma7IFV pa24ebSkTHXiFVPD9WzIsSI6DmThqFKkt51B53ZuLmnsUAgQ28GsietP8MeGZft0uuPi esuyWicZUymMRhhdZEjuLxtdc7jyDgOERPNLdwfZF3U2xo1yUSczJFlREm3qkyr3qV0Z wXXIttvFKQGPESqf1vuZP+yTZ7aWPTaOg9jjxXIzKU6DJlQGEDK486Lh7S/34tgjsud2 0fDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=TbWo6WoK; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-215406-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215406-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f56e26510si197564166b.718.2024.06.14.11.32.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 11:32:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-215406-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=@linux.dev header.s=key1 header.b=TbWo6WoK; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-215406-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215406-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 742951F21A87 for ; Fri, 14 Jun 2024 18:32:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 22BAC19E7C9; Fri, 14 Jun 2024 18:32:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="TbWo6WoK" Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (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 42BA24409 for ; Fri, 14 Jun 2024 18:32:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718389969; cv=none; b=JpebzJTeF1x0E0bTQH9oM9sLoXZYoAZCIDy7OcrGzqQmVqLoIojBnevTC9DbMKNevicv9650Xe01h6NgrA+yQriZJYZNR0lN2GglXL+nZLsL1xzm9gU0FsS98dZIQFXwEZsrEfcnmp/v9FGiP0/Q/6YckozIJQsZuULKkq3jC3o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718389969; c=relaxed/simple; bh=kWHXfiW/eWv847hDdyNDPfVjaGDkYdtb8A2LHmGvu2c=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PHZM+hxAuP3xB7f26btZTGXE6lgVpnAfb0koqFlEmQKq50tPv9vQB5YyYO+RQraT2A45wS+4agsqXFsFF0PA1l1tB5Bwe1gpvPp95TvD6VKIZke+fhccoax2Al0VPDP7y1ycQ/8RWXxSuVzTA+maaJSZQlAnZsvmwl5EwpMMBUg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=TbWo6WoK; arc=none smtp.client-ip=91.218.175.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: sebott@redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1718389963; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=bfjAgyWtBbRxONbdiKFFSvpzW16ct8ELGbCI1RgaYDo=; b=TbWo6WoK1irRzuNAiIl8Od4+Hy08BlCQJoKsWrUFVp96WCEIXacnZGW5V8snVx167JFiyo LoH7xl7Os41ZevbeY/1nDDIpa04sLSc500zOFXzatRhoCrqVGfXtvXY0j9PYgxdnRgbiNT iQjWcGIV0uhvq4anf8OpW6dOY1sTdE4= X-Envelope-To: linux-arm-kernel@lists.infradead.org X-Envelope-To: kvmarm@lists.linux.dev X-Envelope-To: linux-kernel@vger.kernel.org X-Envelope-To: maz@kernel.org X-Envelope-To: james.morse@arm.com X-Envelope-To: suzuki.poulose@arm.com X-Envelope-To: catalin.marinas@arm.com X-Envelope-To: will@kernel.org X-Envelope-To: shahuang@redhat.com X-Envelope-To: eric.auger@redhat.com Date: Fri, 14 Jun 2024 18:32:37 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Sebastian Ott Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Marc Zyngier , James Morse , Suzuki K Poulose , Catalin Marinas , Will Deacon , Shaoqin Huang , Eric Auger Subject: Re: [PATCH v4 3/6] KVM: arm64: add emulation for CTR_EL0 register Message-ID: References: <20240603130507.17597-1-sebott@redhat.com> <20240603130507.17597-4-sebott@redhat.com> <0b148e21-1714-f4f7-bc77-2a12b990572d@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0b148e21-1714-f4f7-bc77-2a12b990572d@redhat.com> X-Migadu-Flow: FLOW_OUT On Fri, Jun 14, 2024 at 05:31:37PM +0200, Sebastian Ott wrote: [...] > Hm, but in that case we'd use reset_vm_ftr_id_reg() meaning we would have > to make IDREG() work for this reg. Either by adding special handling to > that macro or by increasing kvm->arch.id_regs[] a lot - both options don't > sound very appealing. Hiding some of the ugly details behind IDREG() isn't the worst thing, IMO. The feature ID registers are not laid out contiguously in the architecture, so it'd make sense that the corresponding KVM code not be brittle to this. The other benefit is we initialize kvm->arch.ctr_el0 exactly once, just like the other ID registers. I believe there's a quirk with this patch where an initialization that happens after a KVM_SET_ONE_REG on CTR_EL0 will clobber the userspace value. So, here's where I'm at locally, I'll work it a bit more and try to densely pack CTR_EL0 into the id_regs array. I also have some (untested) changes to get CTR_EL0 to show up in the debugfs interface we now have. Mind if I post what I have afterwards? commit 6bf81bd50dc16309a627863948d49cfeeb00897e Author: Sebastian Ott Date: Mon Jun 3 15:05:03 2024 +0200 KVM: arm64: Treat CTR_EL0 as a VM feature ID register Signed-off-by: Sebastian Ott Reviewed-by: Shaoqin Huang Signed-off-by: Oliver Upton diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 212ae77eefaf..e5b8cdd70914 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -327,10 +327,20 @@ struct kvm_arch { */ #define IDREG_IDX(id) (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id)) #define IDX_IDREG(idx) sys_reg(3, 0, 0, ((idx) >> 3) + 1, (idx) & Op2_mask) -#define IDREG(kvm, id) ((kvm)->arch.id_regs[IDREG_IDX(id)]) +#define IDREG(kvm, id) \ +(*({ \ + u64 *__reg; \ + if ((id) == SYS_CTR_EL0) \ + __reg = &(kvm)->arch.ctr_el0; \ + else \ + __reg = &((kvm)->arch.id_regs[IDREG_IDX(id)]); \ + __reg; \ +})) #define KVM_ARM_ID_REG_NUM (IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1) u64 id_regs[KVM_ARM_ID_REG_NUM]; + u64 ctr_el0; + /* Masks for VNCR-baked sysregs */ struct kvm_sysreg_masks *sysreg_masks; diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index dfabf7aec2c7..1ab2cbbc7a76 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1583,6 +1583,9 @@ static bool is_feature_id_reg(u32 encoding) */ static inline bool is_vm_ftr_id_reg(u32 id) { + if (id == SYS_CTR_EL0) + return true; + return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 && sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 && sys_reg_CRm(id) < 8); @@ -1886,7 +1889,7 @@ static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, if (p->is_write) return write_to_read_only(vcpu, p, r); - p->regval = read_sanitised_ftr_reg(SYS_CTR_EL0); + p->regval = IDREG(vcpu->kvm, SYS_CTR_EL0); return true; } @@ -2475,7 +2478,10 @@ 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 }, + ID_WRITABLE(CTR_EL0, 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, @@ -3714,18 +3720,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) -- Thanks, Oliver