From nobody Mon Feb 9 11:26:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 4FB2633989 for ; Fri, 5 Apr 2024 12:01:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318481; cv=none; b=TOq30Dgyab0HMECCHQT5FeV1c3yQe7+nQ0JBlsNqHk1eSvux07kQUZ2p1bS1dOWs/scSTH+hbKP0pkNcIUa5dJ1UqcS+3m9sTCqAOyYFdZ/i1KYZI/tVr7059U8105dCaLr7onNrzUwdRl0pOUOV8v56QVsSNuSM+EvoWPGwxks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318481; c=relaxed/simple; bh=FRiRwc90NawBYQ1NhQ/i4LDW0LXM9/iVyn9EgL+Kf4U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sqwYn7QXx5iCzzrfG2Oof/vCQki8M0pA4SG6A/sd/r3Vh0v0gpJU9qFNiHKIGafPJbf8t2o0upMhHjca2ShBRlbKXlOO5fp5aWoJ7UkjIl2MvbznxgirdDTlIOVfLFWKxCTUy8nbr38By32JeqZIida18Psow1T3egNVXATry9M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=VpvmWBPw; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VpvmWBPw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712318479; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yv0f7+ZooceAuz8ErMSH+4jWIomyiUMNQo0xCMMDiy0=; b=VpvmWBPwdzSKBhmiDjdnOIhLCrID9qkMpb8qZBC2tVVthBDeeavyKGL3LlvAFUkq+HfEvm +cDeps2+xUNT9gDVJjok0es1VWJxlWLuLCmn9sYN72H0dTSuHVoQulR+lIewmjx/rZ68cv h7X0CKRpbj3SmQSBzQmVkQX8cJ00OJ8= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-440-ngfdyHmUMWaz51M7PYvA0g-1; Fri, 05 Apr 2024 08:01:18 -0400 X-MC-Unique: ngfdyHmUMWaz51M7PYvA0g-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-430b7d8b428so19862951cf.1 for ; Fri, 05 Apr 2024 05:01:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712318478; x=1712923278; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yv0f7+ZooceAuz8ErMSH+4jWIomyiUMNQo0xCMMDiy0=; b=WZDGHZM941C6gUQvpsSLbG9U6j1cx3TimurxzF8hRPT/ZdhsqaAOmQVypE7Mj6Epme CGkovYDBNy01yXLRePqJzr4w9v/OKXWF1snkaOGrFsl4mJsee752afJs01+8L6EJkjTp h7oeNjTR8Pa7aVRWtYl4wvegi+Tmap5dzCHvha7x6WhCpEELbKmRd0xJXJHbTrvovkQ0 70FWi0HV3SiiwVXSmKfa0Sird74YMVYijtdGCHdyUu22cGEtAuJaGjMJ3wvBRIKKVUMj rC5hnxhdkkPOLRUAsRrLf11TjHA1oWa3FF4Y+pkt6p1h9N6lzxkJ2fvHiZk8HJ7zsJNz biTg== X-Forwarded-Encrypted: i=1; AJvYcCUJNFynXRoQkbyABPjL27jQUwD3i1P/MbB0lhzY8qsosPul/SXqiAgXE4jOzsCs7hOqMT++N77NVYcksmGx1dSTDDijXk8XcpAbBrWA X-Gm-Message-State: AOJu0YzzUFLlB85NizprSszt45zVsX61lGOO4Rgrk89h4XUhGnbmbq2G UonjkFgkXZGZNfVrhUlH/nxJGYmWsMTq/YbSdMYVv8i+o1JTgmzn0ikOJ+EnPt3D47zPVm+UZxk iNpTrZWHpcRdWJj34MpAn6zdAv2p5REHBRJypEbhQSG/1IDM8xkywFahIH7YG3w== X-Received: by 2002:ac8:5886:0:b0:434:516a:5486 with SMTP id t6-20020ac85886000000b00434516a5486mr976112qta.21.1712318477245; Fri, 05 Apr 2024 05:01:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFhQuEwy17Cph2Q2g7cvwJ6s/aiHUU0/VZ7nyNR44guIaL32ndi6+BiWQVeBBtgC3cEN4G/SQ== X-Received: by 2002:ac8:5886:0:b0:434:516a:5486 with SMTP id t6-20020ac85886000000b00434516a5486mr976080qta.21.1712318476646; Fri, 05 Apr 2024 05:01:16 -0700 (PDT) Received: from rh.redhat.com (p200300c93f4c24001316a1e598258370.dip0.t-ipconnect.de. [2003:c9:3f4c:2400:1316:a1e5:9825:8370]) by smtp.gmail.com with ESMTPSA id fb17-20020a05622a481100b00434383f2518sm679808qtb.87.2024.04.05.05.01.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 05:01:16 -0700 (PDT) From: Sebastian Ott To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Catalin Marinas , Will Deacon Subject: [PATCH 1/4] KVM: arm64: change return value in arm64_check_features() Date: Fri, 5 Apr 2024 14:01:05 +0200 Message-ID: <20240405120108.11844-2-sebott@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405120108.11844-1-sebott@redhat.com> References: <20240405120108.11844-1-sebott@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" arm64_check_features() returns -E2BIG to indicate the register's feature set is a superset of the maximally-allowed register value. The only caller of that function changes this to -EINVAL since that's what userspace expects for invalid register writes. In preparation of adding another caller for arm64_check_features() that would need to do the same conversion just return -EINVAL directly. Signed-off-by: Sebastian Ott --- arch/arm64/kvm/sys_regs.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index c9f4f387155f..131f5b0ca2b9 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1445,7 +1445,7 @@ static s64 kvm_arm64_ftr_safe_value(u32 id, const str= uct arm64_ftr_bits *ftrp, * considered the safe value regardless For register fields that are not in * writable, only the value in limit is considered the safe value. * - * Return: 0 if all the fields are safe. Otherwise, return negative errno. + * Return: 0 if all the fields are safe. Otherwise, return -EINVAL. */ static int arm64_check_features(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, @@ -1464,7 +1464,7 @@ static int arm64_check_features(struct kvm_vcpu *vcpu, * only safe value is 0. */ if (sysreg_visible_as_raz(vcpu, rd)) - return val ? -E2BIG : 0; + return val ? -EINVAL : 0; =20 ftr_reg =3D get_arm64_ftr_reg(id); if (!ftr_reg) @@ -1490,12 +1490,12 @@ static int arm64_check_features(struct kvm_vcpu *vc= pu, safe_val =3D kvm_arm64_ftr_safe_value(id, ftrp, f_val, f_lim); =20 if (safe_val !=3D f_val) - return -E2BIG; + return -EINVAL; } =20 /* For fields that are not writable, values in limit are the safe values.= */ if ((val & ~mask) !=3D (limit & ~mask)) - return -E2BIG; + return -EINVAL; =20 return 0; } @@ -1840,16 +1840,6 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const s= truct sys_reg_desc *rd, IDREG(vcpu->kvm, id) =3D val; =20 mutex_unlock(&vcpu->kvm->arch.config_lock); - - /* - * arm64_check_features() returns -E2BIG to indicate the register's - * feature set is a superset of the maximally-allowed register value. - * While it would be nice to precisely describe this to userspace, the - * existing UAPI for KVM_SET_ONE_REG has it that invalid register - * writes return -EINVAL. - */ - if (ret =3D=3D -E2BIG) - ret =3D -EINVAL; return ret; } =20 --=20 2.42.0 From nobody Mon Feb 9 11:26:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 0E6D816C86E for ; Fri, 5 Apr 2024 12:01:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318484; cv=none; b=srFr9TjcIp2o9F0s1Dv2DOT8blYMQXsZ3oFmkm0T+mTOhOGGIcH9PjttwEoTpBtyP0JzhVgdiWY5kGUZ3UKGWQHfwgJhqgh8/kZRfQ6fDQp8tQg/qFDLXrNFphUbiqppzgAwzVKjXYjjOBUG9sfNzbmWBH4SLbb53Q0O0XvArSk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318484; c=relaxed/simple; bh=/0jN/zskn+pvye9zhGa9kX2fqd2uuUdTKatvYSPMGHg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SnwL0w9/yANkJvVcT3CHXO6XQViQiotGHZhrKsHpFYuqMTQaEmHqItny+TdLMKxMahyhussgWXqTUQnV7+JaB62eQ1yfxYAEelYddsfgmXvYCYEhB6FTmMPUsKNJggCDvax8MIAvTrRsLsKhtMMQUtnQ84te7IQoKeHWLXctz2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Qb2Zwydr; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Qb2Zwydr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712318482; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rsSFcUbsThM5IkqFm5UZmPp81ZNMKoAIhMlsT6uF7yg=; b=Qb2ZwydrKaa675QYBQ7dPMWeZNLPyq6L9mk50JwrLsQ3ZDTLcgZA9Pz84RCO4hqUl5g6Dh waSA1tGn5POy9bGkPy8N1WJx7q8hu4RIHiIJjtGPh9eynEFL2cTXRm9RUqncmCcOtqaIz9 6exlnop4x9iqn8NDM2v7L9mmMH7IxAA= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-386-OPspJuaKNhSwY_ngpVa5fw-1; Fri, 05 Apr 2024 08:01:21 -0400 X-MC-Unique: OPspJuaKNhSwY_ngpVa5fw-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-430b7f75294so20851211cf.2 for ; Fri, 05 Apr 2024 05:01:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712318480; x=1712923280; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rsSFcUbsThM5IkqFm5UZmPp81ZNMKoAIhMlsT6uF7yg=; b=AIP+5oDB5k1BNIUW9uZyLS7rCtHJqj+CB8KW5SCE/8U9geUfEhJtgOga2NlwpHzA5D DPtyF20/krwJ/JiHa1cEvCwN/kIErlHngrNJGSep6Hb0CZmJAbaPp751x+3vSrbxgP7w tNv55GCTnuyJyQW2vnvS+RL/H6bHJiia9Lyn1GDxR+6GBVC7sMczGSf7Or5FmtAgKnQd E3lHVVfrQKu5m8dOATFsxYMcr/wTJ5pC6Sb8hQtWUmnj5w0D8AfB5ck/E5lHbKLbKZJf mS7ktxRGu9lfM5jyNace9UZ56wFbQUPHFHNR8oSm0dgNylCdgy8GXeaFqotqWJO66yIt rzuw== X-Forwarded-Encrypted: i=1; AJvYcCWRevx+vZDMd96dNW7FxjPenGoz5A1i+ItAIDgMZ31Iqa4D8G8eJbD5s31VovsSDqBuRh8rCWm4d6S1CgSrf2tJm9TFLPGUG+GN1ekc X-Gm-Message-State: AOJu0Yxqj72AoEsOJLvGuPTF3yk5+l0BZZ3p6PecDJ4I12SsOhbW6ZAE Y1sebXAwnsI2InFFoAXq3veGBdwrOdduI10T4A/5nMwPY0YST8e3jhOSk6/LWETa9U61SX7vzg8 TZnrBg1zCQdUNMxQ03l26eR+NaK/c1MTh4mUaTfel//FK+revqi4PnTbgwE7ZLg== X-Received: by 2002:a05:622a:298f:b0:434:6094:beaa with SMTP id hd15-20020a05622a298f00b004346094beaamr1267267qtb.39.1712318480549; Fri, 05 Apr 2024 05:01:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnpiinNv2C2T+YikhgYDSg8DJI+6kCRzyHodV6nkO8b80TJOicClVWNFFNXPLtsSMcu3AVIQ== X-Received: by 2002:a05:622a:298f:b0:434:6094:beaa with SMTP id hd15-20020a05622a298f00b004346094beaamr1267234qtb.39.1712318480054; Fri, 05 Apr 2024 05:01:20 -0700 (PDT) Received: from rh.redhat.com (p200300c93f4c24001316a1e598258370.dip0.t-ipconnect.de. [2003:c9:3f4c:2400:1316:a1e5:9825:8370]) by smtp.gmail.com with ESMTPSA id fb17-20020a05622a481100b00434383f2518sm679808qtb.87.2024.04.05.05.01.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 05:01:19 -0700 (PDT) From: Sebastian Ott To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Catalin Marinas , Will Deacon Subject: [PATCH 2/4] KVM: arm64: maintain per VM value for CTR_EL0 Date: Fri, 5 Apr 2024 14:01:06 +0200 Message-ID: <20240405120108.11844-3-sebott@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405120108.11844-1-sebott@redhat.com> References: <20240405120108.11844-1-sebott@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for CTR_EL0 emulation maintain a per VM for this register and use it where appropriate. Signed-off-by: Sebastian Ott --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/sys_regs.c | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index 9e8a496fb284..481216febb46 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -318,6 +318,7 @@ struct kvm_arch { =20 /* PMCR_EL0.N value for the guest */ u8 pmcr_n; + u64 ctr_el0; =20 /* Iterator for idreg debugfs */ u8 idreg_debugfs_iter; diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 131f5b0ca2b9..4d29b1a0842d 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -215,13 +215,21 @@ void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 va= l, int reg) /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */ #define CSSELR_MAX 14 =20 +static u64 kvm_get_ctr_el0(struct kvm *kvm) +{ + if (kvm->arch.ctr_el0) + return kvm->arch.ctr_el0; + + return read_sanitised_ftr_reg(SYS_CTR_EL0); +} + /* * Returns the minimum line size for the selected cache, expressed as * Log2(bytes). */ -static u8 get_min_cache_line_size(bool icache) +static u8 get_min_cache_line_size(struct kvm *kvm, bool icache) { - u64 ctr =3D read_sanitised_ftr_reg(SYS_CTR_EL0); + u64 ctr =3D kvm_get_ctr_el0(kvm); u8 field; =20 if (icache) @@ -248,7 +256,7 @@ static u32 get_ccsidr(struct kvm_vcpu *vcpu, u32 csselr) if (vcpu->arch.ccsidr) return vcpu->arch.ccsidr[csselr]; =20 - line_size =3D get_min_cache_line_size(csselr & CSSELR_EL1_InD); + line_size =3D get_min_cache_line_size(vcpu->kvm, csselr & CSSELR_EL1_InD); =20 /* * Fabricate a CCSIDR value as the overriding value does not exist. @@ -283,7 +291,7 @@ static int set_ccsidr(struct kvm_vcpu *vcpu, u32 csselr= , u32 val) u32 i; =20 if ((val & CCSIDR_EL1_RES0) || - line_size < get_min_cache_line_size(csselr & CSSELR_EL1_InD)) + line_size < get_min_cache_line_size(vcpu->kvm, csselr & CSSELR_EL1_In= D)) return -EINVAL; =20 if (!ccsidr) { @@ -1862,7 +1870,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); =20 - p->regval =3D read_sanitised_ftr_reg(SYS_CTR_EL0); + p->regval =3D kvm_get_ctr_el0(vcpu->kvm); return true; } =20 @@ -1882,7 +1890,7 @@ static bool access_clidr(struct kvm_vcpu *vcpu, struc= t sys_reg_params *p, */ static u64 reset_clidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) { - u64 ctr_el0 =3D read_sanitised_ftr_reg(SYS_CTR_EL0); + u64 ctr_el0 =3D kvm_get_ctr_el0(vcpu->kvm); u64 clidr; u8 loc; =20 @@ -1935,7 +1943,7 @@ static u64 reset_clidr(struct kvm_vcpu *vcpu, const s= truct sys_reg_desc *r) static int set_clidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val) { - u64 ctr_el0 =3D read_sanitised_ftr_reg(SYS_CTR_EL0); + u64 ctr_el0 =3D kvm_get_ctr_el0(vcpu->kvm); u64 idc =3D !CLIDR_LOC(val) || (!CLIDR_LOUIS(val) && !CLIDR_LOUU(val)); =20 if ((val & CLIDR_EL1_RES0) || (!(ctr_el0 & CTR_EL0_IDC) && idc)) --=20 2.42.0 From nobody Mon Feb 9 11:26:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 8C11A16C6B5 for ; Fri, 5 Apr 2024 12:01:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318494; cv=none; b=DHCDsGxl5hu33QV/L3/ZkzbQwZd1Wa2w+pQYdhms5dZKy9ZYY8DjVXynwbzdf1QM3XnnNTSrSSjR5m+gvCDn22cRLYbu8a78CeaxAC+SyYl72t/IjnIOqGSvjIlIw3C7e4808+0d8aHxPNl+0KByfnfHfFRVkwEDrhuTBX5Jpi4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318494; c=relaxed/simple; bh=jqn2XCZxEDbBltMtM+7J9+MGFZAyGlmvapHglxRoTiA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F7xLPRVpS+cAMHb1Bh3LFrGUIfDpfii1hVsvFgGxh5r+1/Nlc7yk8h35SL9TC+BJjST+cGQdcTuSJOHvXEgE8tdUxLquHZ+XzV0yNx3BDPoJHaT9W7GRncaaFNViXf/sTQPMkshk4ADLyJnmgkKxz/utDfUVbelNY40xCEXLHvQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=jWsMO/YQ; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="jWsMO/YQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712318491; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Er4m6dF4CTJ/JTa0OQR3uPXdnB5OQd8nc24oiwvaXBk=; b=jWsMO/YQ08g0xQwIzLliRaVqqvcONohz4ip6nfLuZ9aa09Vo+HxaNNpiJuO+mR0KX3i9Au reFarCA/DhbYUuVdVYjpwD5f0Ee7XqZq+G3OZ/KSFrE5lsLJccGrKo+/vfwtRKkBM4R5g+ IbjuVk4Ae3Iw1MDLbaSha5DoDvJMfUE= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-677-6s_a9PAjNtqQQbZk-DyMsg-1; Fri, 05 Apr 2024 08:01:30 -0400 X-MC-Unique: 6s_a9PAjNtqQQbZk-DyMsg-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-4311b01a1ecso21592101cf.2 for ; Fri, 05 Apr 2024 05:01:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712318490; x=1712923290; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Er4m6dF4CTJ/JTa0OQR3uPXdnB5OQd8nc24oiwvaXBk=; b=hz96x3j/5IpK11Ez6CnSCxYbDkoP5uCK2Q/BM4ItpIKgr9SXI5cFGMyZi0wVKlXDYD GD2QyOwMUlU2t7XDUvU37pb9Ry/ZVeBiv0KjYxvuixxfmTmh9lFEE8ZsHO/X4FikpyPj X7M+OqyCMhTK6Kr/ZS+JVABZ7YoUlHFMbVD8U7XuxHxe3nGCHr7ShaNCZnDxKtcgOZJS T5rSb1OQ0yQxFZtS9NMlE4+2K5eIIq8nXXS1ZMLU8qupOHnglaCfuBj0n/66SqIoIY62 j821Cf35pGgdDZiQiOx3o2lH5oCnfHnRgAK02Owrm1cegV0LzH6CSXtPE4I12PnlpICK pyTg== X-Forwarded-Encrypted: i=1; AJvYcCVCQHJPxacGcvgALgqvb3sya+QvCeTgVF0Sfx7OeFlJeeKyeaN/Wsz+cIGW9XiQ4Uw8Yw4CLCj2ItJq6wzfeQgel3C1KDMtBHVgLhDe X-Gm-Message-State: AOJu0YwB+jllmo13yWtAID3eR6lrbBsm9P1v4PlwNOHCP/Je81839hXc 5Kry6Lg8L+ygaA7LGvV9ZZ1yc9Fkvpi3ohrYcnGedMFfZfriiRjuNPjEs4mlK3L3NV392QCd80q MXktMrd32MphbXGz1Yy4DB/VR+yOvosdQabisDEv+W7xqZuyAlRUfAvHTuDavZQ== X-Received: by 2002:a05:622a:40b:b0:434:3e7c:ff29 with SMTP id n11-20020a05622a040b00b004343e7cff29mr1054725qtx.8.1712318485071; Fri, 05 Apr 2024 05:01:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFiyCyRJp19IZ/hMqBIxQeEde5V/OMaJJ5oWTE9Op1Wu8z9M5yrM5kFTCU5Bk+wY8/8Aog8fg== X-Received: by 2002:a05:622a:40b:b0:434:3e7c:ff29 with SMTP id n11-20020a05622a040b00b004343e7cff29mr1054631qtx.8.1712318483158; Fri, 05 Apr 2024 05:01:23 -0700 (PDT) Received: from rh.redhat.com (p200300c93f4c24001316a1e598258370.dip0.t-ipconnect.de. [2003:c9:3f4c:2400:1316:a1e5:9825:8370]) by smtp.gmail.com with ESMTPSA id fb17-20020a05622a481100b00434383f2518sm679808qtb.87.2024.04.05.05.01.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 05:01:22 -0700 (PDT) From: Sebastian Ott To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Catalin Marinas , Will Deacon Subject: [PATCH 3/4] KVM: arm64: add emulation for CTR_EL0 register Date: Fri, 5 Apr 2024 14:01:07 +0200 Message-ID: <20240405120108.11844-4-sebott@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405120108.11844-1-sebott@redhat.com> References: <20240405120108.11844-1-sebott@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. 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; } =20 +static u64 reset_ctr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd) +{ + vcpu->kvm->arch.ctr_el0 =3D 0; + return kvm_get_ctr_el0(vcpu->kvm); +} + +static int get_ctr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, + u64 *val) +{ + *val =3D 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 =3D read_sanitised_ftr_reg(SYS_CTR_EL0); + u64 old_val =3D kvm_get_ctr_el0(vcpu->kvm); + const struct sys_reg_desc *clidr_el1; + int ret; + + if (val =3D=3D old_val) + return 0; + + if (kvm_vm_has_ran_once(vcpu->kvm)) + return -EBUSY; + + mutex_lock(&vcpu->kvm->arch.config_lock); + ret =3D arm64_check_features(vcpu, rd, val); + if (ret) { + mutex_unlock(&vcpu->kvm->arch.config_lock); + return ret; + } + if (val !=3D host_val) + vcpu->kvm->arch.ctr_el0 =3D val; + else + vcpu->kvm->arch.ctr_el0 =3D 0; + + mutex_unlock(&vcpu->kvm->arch.config_lock); + + clidr_el1 =3D get_sys_reg_desc(SYS_CLIDR_EL1); + if (clidr_el1) + clidr_el1->reset(vcpu, clidr_el1); + + return 0; +} + 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[] =3D= { { 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 =3D reset_ctr, + .get_user =3D get_ctr, .set_user =3D set_ctr, .val =3D (CTR_EL0_DIC_MAS= K | + CTR_EL0_IDC_MASK | + CTR_EL0_DminLine_MASK | + CTR_EL0_IminLine_MASK)}, { SYS_DESC(SYS_SVCR), undef_access }, =20 { PMU_SYS_REG(PMCR_EL0), .access =3D access_pmcr, .reset =3D reset_pmcr, @@ -3623,6 +3676,13 @@ static bool index_to_params(u64 id, struct sys_reg_p= arams *params) } } =20 +static const struct sys_reg_desc *get_sys_reg_desc(u32 encoding) +{ + struct sys_reg_params params =3D 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) =20 -static u64 get_ctr_el0(struct kvm_vcpu *v, const struct sys_reg_desc *r) -{ - ((struct sys_reg_desc *)r)->val =3D 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 =3D { { 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 }, }; =20 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 |=3D (HCRX_EL2_MSCEn | HCRX_EL2_MCE2); } =20 + if (vcpu->kvm->arch.ctr_el0) + vcpu->arch.hcr_el2 |=3D HCR_TID2; + if (test_bit(KVM_ARCH_FLAG_FGU_INITIALIZED, &kvm->arch.flags)) goto out; =20 --=20 2.42.0 From nobody Mon Feb 9 11:26:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 274BB16C859 for ; Fri, 5 Apr 2024 12:01:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318490; cv=none; b=t5aggCMBkpYZwwzyFqXD9hmrEnB/TJhXYZyRB+aDqHqNm670Gew9BS3j1+iO8MQTo/ZmkqMwPo4DslbyyfWNpUNMnUQmYA/xQ5Dnqf2eLPDYeZJPzDzZ3IbpapjbhFXKlN7oPPNDdHjn9urMcofap8EsiPdqaY82ca9j4eqaOVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712318490; c=relaxed/simple; bh=4UJ5Fg3bfU8lfARbEPZ8K38xQekfzxdrdG5SQuwKNMA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vx66MJr5TQqBahZm2KeMvff0FGqD3pEFWaUJCFii5ngeUQPio6m2VGq6Qz9gHeCJkf+OR6Zq73GUCc/ZjeMFXgpxomqDzs7gOqtAofFlMWR0hwDldrIV0AOiPSFelDpsCYA+EIeNW2EfPeXiRAVmx+0RdinjyQwAo7o9u86pRdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=iY4oCGn9; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iY4oCGn9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712318488; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rn4BMv+SOP2Xe0y3A99nPyLC4GfuU+9ShaoCAlDA424=; b=iY4oCGn9iN5w93qROm+Vs/KKt+uWq3xQF/9mIUmr5YwmLxSsnnWQvU0SyeiG6myqdMFuBQ SIulIKyge5X/NWxFSKrXZ37Q60gNOQYOdK/onczzMSq+UCA0/j1J5pfRxUV49ww7/bI2+L Tv81/m+M+3V5AtgrtQ0UuC3kbqiA9d4= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-388-8GhajZp8PhiTcLdHUxvujw-1; Fri, 05 Apr 2024 08:01:27 -0400 X-MC-Unique: 8GhajZp8PhiTcLdHUxvujw-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-4311b01a1ecso21591371cf.2 for ; Fri, 05 Apr 2024 05:01:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712318486; x=1712923286; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rn4BMv+SOP2Xe0y3A99nPyLC4GfuU+9ShaoCAlDA424=; b=vO4W3/Gv2pM6S3Djd0NSYysz30ke/JN3tR9IitFB3uzOVp659JnHCOUhONHPPYqTb5 iAZZGFH2jU0Pj7zRLnv4nsx4ommbIu9Qwglh58J8c7jSGDw3vDQ7/J137U8nb5NiWzia /JGSDteyQVbKyUrUXvvdTkXDxREt4BnhPa0a/XxiH997VbaR5lLE8rXZ34WKlGZi54Ia ItVyUEE3MQkfL58DjnuIDzltiYar0eHe2lMw+FKM6A8OzmYTkI3EnFH+gU7reiy76aar XHfriR33sHsYBrVPKXW5w8rqSQqCh0MdpFbbXwGA2CnDxDhPdvCUfR6rx4lnjqGQ+xZU znWw== X-Forwarded-Encrypted: i=1; AJvYcCUkinh95pibZw/tgZvap29FBZzj7cwRrDqxMAwlx8ndIpu8PXcQN82NV6BN3Yo5nShGlQBYN4uHIMtLOnmX2cnb2f3LZn8qrhy3FXrH X-Gm-Message-State: AOJu0YzIQqaBbesjxfmggbgb2Rf3lcCrlzM3zDGcwN2lcJkpb2ckO9P3 ok15dG5LFNvIyQloVtsaDZeVIQXOFj2xDm0wJKT1rqraIzwjFcnFVIojDkCNYU99N53pmD21dvB nqANi96VJoATr8kKvafkVWaBKI5Nz3LFVDCSrL82vyj2ek+bEc82vHIigkZFRKw== X-Received: by 2002:ac8:7d0a:0:b0:431:5fad:ddbd with SMTP id g10-20020ac87d0a000000b004315fadddbdmr1508847qtb.29.1712318486527; Fri, 05 Apr 2024 05:01:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJoGGlIt1CxA4rAZezuA7q+tZShCN/ywwM2H1k89TmnzVpNOpAzo4NVsKSoiRJE7N6sIm0dg== X-Received: by 2002:ac8:7d0a:0:b0:431:5fad:ddbd with SMTP id g10-20020ac87d0a000000b004315fadddbdmr1508823qtb.29.1712318486257; Fri, 05 Apr 2024 05:01:26 -0700 (PDT) Received: from rh.redhat.com (p200300c93f4c24001316a1e598258370.dip0.t-ipconnect.de. [2003:c9:3f4c:2400:1316:a1e5:9825:8370]) by smtp.gmail.com with ESMTPSA id fb17-20020a05622a481100b00434383f2518sm679808qtb.87.2024.04.05.05.01.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 05:01:25 -0700 (PDT) From: Sebastian Ott To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Catalin Marinas , Will Deacon Subject: [PATCH 4/4] KVM: arm64: show writable masks for feature registers Date: Fri, 5 Apr 2024 14:01:08 +0200 Message-ID: <20240405120108.11844-5-sebott@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405120108.11844-1-sebott@redhat.com> References: <20240405120108.11844-1-sebott@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Instead of using ~0UL provide the actual writable mask for non-id feature registers in the output of the KVM_ARM_GET_REG_WRITABLE_MASKS ioctl. Explicitely set the mask for CLIDR_EL1 to make sure we present the same value to userspace than before. This changes the mask for the CTR_EL0 register in the output of the KVM_ARM_GET_REG_WRITABLE_MASKS ioctl. Signed-off-by: Sebastian Ott --- arch/arm64/kvm/sys_regs.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index b0ba292259f9..947aa9c0784f 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -2505,7 +2505,7 @@ static const struct sys_reg_desc sys_reg_descs[] =3D { =20 { SYS_DESC(SYS_CCSIDR_EL1), access_ccsidr }, { SYS_DESC(SYS_CLIDR_EL1), access_clidr, reset_clidr, CLIDR_EL1, - .set_user =3D set_clidr }, + .set_user =3D set_clidr, .val =3D ~0UL }, { 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 }, @@ -4059,20 +4059,11 @@ int kvm_vm_ioctl_get_reg_writable_masks(struct kvm = *kvm, struct reg_mask_range * if (!is_feature_id_reg(encoding) || !reg->set_user) continue; =20 - /* - * For ID registers, we return the writable mask. Other feature - * registers return a full 64bit mask. That's not necessary - * compliant with a given revision of the architecture, but the - * RES0/RES1 definitions allow us to do that. - */ - if (is_id_reg(encoding)) { - if (!reg->val || - (is_aa32_id_reg(encoding) && !kvm_supports_32bit_el0())) - continue; - val =3D reg->val; - } else { - val =3D ~0UL; + if (!reg->val || + (is_aa32_id_reg(encoding) && !kvm_supports_32bit_el0())) { + continue; } + val =3D reg->val; =20 if (put_user(val, (masks + KVM_ARM_FEATURE_ID_RANGE_INDEX(encoding)))) return -EFAULT; --=20 2.42.0