From patchwork Tue Jul 18 16:45:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13318354 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 47686C001DC for ; Wed, 19 Jul 2023 06:36:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=yPwo85jWtMTfOUz63dbgXINqG9Thwoz5AXDZdbXYQqg=; b=EGKjFjWqlYJZB/0HuJ+0Mseg6L HIG/F5Y4TLITkj2HG2uVqbmDNmBgPXDuLcTl686gJmLZdbdF2yQjps3+9K1JcO32v8RZ3LCwlvx4w IO8B3sBqKCpSGgd6IsKXDSMeQg6DQajL8sc6BunfQVrSry4xwONmnkqiARWeyfi148P+modSTaxIi 49zwo+8dIXJSzO/YTjbkfuixNkBUX7gYyGe4IfHBPa0wVWhirTQDpDFsqTg0vP7lDUlSJiqxTLZ02 RdE/MHY871STERsjtSzpuuRpANVLrPQtHlEcxM4InfF1Qh30rjvWuXHdfSMWwaQ9w3SWOLsmspY3J AOfEDUxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM0nP-005o1H-1A; Wed, 19 Jul 2023 06:36:23 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM0nN-005o0g-2R for linux-arm-kernel@bombadil.infradead.org; Wed, 19 Jul 2023 06:36:21 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=T195FWXfqucyEBgiIGQAY+Lmu6WGEseWSNrrUvHiu4k=; b=AoVs63u3H6Q5N2BlJUR9gD9OrO mAa8VNXiSDK0rfDPHzOPLPIBr21a4aSOGnYuO3RrAqs4NTkjy67nze9UpR/tb5VtjFHlUpC/vVsuq eCTz3puPyvzGG2XR8Msxd2ohHAOSLEIQHdvRNj5UZpJG6LpIHrNXp74DU2Kx+xgAK5nDOktLzKhtA HC3eVpi8EeBQg4eF0nf4TZFNNxApHdiH/VufcwvXB0KojAsPl/1K9haibw2Lvysbyl9fGtcBkKbWE vgHOg2hj0TA+VXtzWx4IDJNaYpF44bwKMIb62I8P97Pk9tdxQNm5SCcqji9LVShBsmWNoI3VAxBWf iuAq4ykw==; Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLnqT-00BlT2-13 for linux-arm-kernel@lists.infradead.org; Tue, 18 Jul 2023 16:46:43 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1b8a4571c1aso30890315ad.0 for ; Tue, 18 Jul 2023 09:46:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689698729; x=1692290729; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=T195FWXfqucyEBgiIGQAY+Lmu6WGEseWSNrrUvHiu4k=; b=IG7T43kRHhKHqmEiMzHxNeQ56Cb0139AlcIhz2Mi/+JW/VCHRl4rYafsJFi/e2JFEi mIvhqJWXlfvC82JAWuBBk2Xi24o18hCzneiWW6VW8aULpxR+CjOWv13HmkdpoJv3DurC L2DbZRk4IwjvXC9sEiXHpWWw4Pc4Jue5D+o42rmQouFteNkDuRc4NNc9C/siUP8Ry8P8 D1oQnTyl/+aQ9787SxzIRkNdmje0ThG0RirnnIxVV7XnJB3HUNsYN5zxe8aZ/tadDsK4 akk9re/Kek0AM0PZ7I9ayA5J3l4ygxCa63qtk6CUH6Sjsz6434l1UfEiaefJAIXWtM/D F3Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689698729; x=1692290729; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=T195FWXfqucyEBgiIGQAY+Lmu6WGEseWSNrrUvHiu4k=; b=MbdVSRhSJLvo7Gr3J1x4L+XsaZyMa4/fZV5nwYXe2OAug9ow9EsDXqK+KuL9wtgKp+ VqAs9csAFa3au07hrU9Z7APdUBvsghE2caGSwFPr4oZS/l0DT7nxfhSjxPExmf2qshzj c4nxH2cSNj3vjEgN9MrFAwi7aoHMXarYiW8oD1vN9TGmGr0fB72DUAtoRJBOlRD7VIRq ax3OqVTxX0R6vuegEhYUBATnstIgLBcASXZz4J2bQQNyfmv9pRcmwsJQ84as/kiaObnB PykJgeLScnxrw0ysUlOtsYC8qZuxt41miMTbVSTYaqJMdb12u8w7Rnmsa9fgtKl/gKfA S6Aw== X-Gm-Message-State: ABy/qLbtwlHF4evbLzHlr1MOCwgaFduJKd/UIkh0lIr6vy/dCIOsugL2 a6QTJazcTimyF4hWEbhZux2JAg7jyX4Wp8MHPg== X-Google-Smtp-Source: APBJJlGDtopDEFCCENvpj9bk0ucRozmxIamafw8OCk4paTc3ega52yzZuM1x2wqjc5oWp4XpUi/NXDGnD1HR1/9cuQ== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a17:902:c952:b0:1b7:edcd:8dcf with SMTP id i18-20020a170902c95200b001b7edcd8dcfmr1194pla.4.1689698728793; Tue, 18 Jul 2023 09:45:28 -0700 (PDT) Date: Tue, 18 Jul 2023 16:45:17 +0000 In-Reply-To: <20230718164522.3498236-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230718164522.3498236-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718164522.3498236-2-jingzhangos@google.com> Subject: [PATCH v6 1/6] KVM: arm64: Use guest ID register values for the sake of emulation From: Jing Zhang To: KVM , KVMARM , ARMLinux , Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Fuad Tabba , Reiji Watanabe , Raghavendra Rao Ananta , Suraj Jitindar Singh , Cornelia Huck , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230718_174641_466501_0446D687 X-CRM114-Status: GOOD ( 10.68 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Since KVM now supports per-VM ID registers, use per-VM ID register values for the sake of emulation for DBGDIDR and LORegion. Signed-off-by: Jing Zhang --- arch/arm64/kvm/sys_regs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index bd3431823ec5..c1a5ec1a016e 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -379,7 +379,7 @@ static bool trap_loregion(struct kvm_vcpu *vcpu, struct sys_reg_params *p, const struct sys_reg_desc *r) { - u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); + u64 val = IDREG(vcpu->kvm, SYS_ID_AA64MMFR1_EL1); u32 sr = reg_to_encoding(r); if (!(val & (0xfUL << ID_AA64MMFR1_EL1_LO_SHIFT))) { @@ -2429,8 +2429,8 @@ static bool trap_dbgdidr(struct kvm_vcpu *vcpu, if (p->is_write) { return ignore_write(vcpu, p); } else { - u64 dfr = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); - u64 pfr = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); + u64 dfr = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1); + u64 pfr = IDREG(vcpu->kvm, SYS_ID_AA64PFR0_EL1); u32 el3 = !!cpuid_feature_extract_unsigned_field(pfr, ID_AA64PFR0_EL1_EL3_SHIFT); p->regval = ((((dfr >> ID_AA64DFR0_EL1_WRPs_SHIFT) & 0xf) << 28) | From patchwork Tue Jul 18 16:45:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13318331 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CECDBC001DC for ; Wed, 19 Jul 2023 06:30:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=UR92AK4+He66r/KF1BXQZg1hUnLROkT7IuSnjN3cAU0=; b=IEi5e/lo4mmQIUfX8ojNcxH7nu hlRfZv4Ed/+8u0dDQEahFOHfaoRMrBreAZiSLiboyJnTfpTCyEChr5gut9rpth+z/nQWRb1ihBTDQ Nnf5FhiF3CIWZtxmq6VbcW6opFkW/fob5WPlcCZ0Zz4X1VaGTjx9ZiER7ZeDJaEKndAROOgOEEd4V pDjPB/g9+neIZCdUX/HGCDFdXzM47AC4PLoEe6ZEo9M+i9EhDPYUeVCLM+1nGFigbQYl3ICLlqBnn 0Q8c1FVnOVWR/nW+OuYTqqgSouU8pxnmouXdW4vKg3ODR5Hh61uFyYqwsftupbR8wDcJIzBaVXbZC UDNqOSpg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM0hZ-005kmv-1i; Wed, 19 Jul 2023 06:30:21 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM0hW-005klP-0k for linux-arm-kernel@bombadil.infradead.org; Wed, 19 Jul 2023 06:30:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=cSCQIj8rChbfCOl6GeXcnV+0FBKWws4kYFyMtOPVmVQ=; b=lOghUsCbxIlsKHfLmaVDjQ5DVZ tBb4Q9RDeAAkUfD6ADaisKtsnI+UMOnxenSN2Ut/PYauR1kQZGQ3vE7v1aRf2akFM+MY4h7dqVZKV Pmq03ksaF4Akb/KzoxKiSGDo8dZsgpRA0LHeV6v2V2HShU3G8gGY1nw9S0FxGYJFhkmsAG+1W9UzG bV1aA3jULCOLLLkore0LX5HJWpyTiRPcszcSdtqq3Q38Ok0hvQWZA0r+dR+AsUeku45di8jTVN0CA YJKM2G2cu3UopT0nyCdLCN3jHavAmsG6iP3niG+og8xyhpLZ7Cl5nQfOK4A2QrrJjvWUceHb20h9+ pB/9S6WQ==; Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLnrI-00BlUU-1n for linux-arm-kernel@lists.infradead.org; Tue, 18 Jul 2023 16:47:34 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1b8a7735231so30851205ad.1 for ; Tue, 18 Jul 2023 09:47:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689698731; x=1692290731; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cSCQIj8rChbfCOl6GeXcnV+0FBKWws4kYFyMtOPVmVQ=; b=LYPlt5PCzOhzwb9jrbb1TeJjrKyo+CnqYLoLjtu7RUwIwm35AvNoNxt9ti1YGoONUN zLyopu6rgJou5cRnQQwxJwzjzVDCUPQsgLWRtG5H3DmGf+pTbTtnYEYjS9/nmd0SnQVi WuEqgoc+hy4yPwUBn7UVfPoeisRTo2Jljpwd9LAs4cBprde8U2wgWUyapgSdO/vmzDTZ kZDCy0DQAWiEu1I3NAwQC1kpRHKajYQn5llAMWcLxPYzGahRpllvuMwZTx3btJXKrXNr /bLzjohgBK/CT5D5tr7xDecygLRrJKdHua6pSeNqnQfh9f+RFDBPS6U2fAaRgiVpLIQO wgcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689698731; x=1692290731; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cSCQIj8rChbfCOl6GeXcnV+0FBKWws4kYFyMtOPVmVQ=; b=cKdVCcirf+MG4eMjL4L3/AwV6jrg2CLQ9Fje1nbZXDr3gVMy2XDm96LWVxXu4H+Mq2 VrGmrevP0hw3uMIok9kevIdRb5PDrj8hRQ1V4T+lbBXiMk8T+N4sDQafxb2dzAMwfEyG t5SbaKuINB/vXihuujnRqno2SiXzxhvA6qGFi3pm2QBFheel4YVGr7DRTVLtkiJONADp xtqyHytWsAFawlCtC/FLV/5AoPKnmu1rZO74D+9FSYA0Gu5kLBEf02Mdg8pC0yrFaGwl pC4+Sk0ZxOr1n8lIU4G6YpkVvIWKKqC7OWbTSEG+StqEtmthqZsttRtZq0+lzWo9aAyO bsZQ== X-Gm-Message-State: ABy/qLbDwmhCeFrAi7kLcBmKvui4rzy2gDCJSX6QpGpxPqAXQjzBNK/A W/tFgEyFSPII7319r8C+Fsh5uj6QQ4N8rU5OiA== X-Google-Smtp-Source: APBJJlEvf6ms5QOH822cn1EVJyhNAJK5wSXxGxatRmMDn9eETOI47IUA/zPlPUd4v3/huIcfn7gFBUTLmfU4n0bWaA== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a17:902:e812:b0:1ae:6895:cb96 with SMTP id u18-20020a170902e81200b001ae6895cb96mr1340plg.5.1689698730965; Tue, 18 Jul 2023 09:45:30 -0700 (PDT) Date: Tue, 18 Jul 2023 16:45:18 +0000 In-Reply-To: <20230718164522.3498236-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230718164522.3498236-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718164522.3498236-3-jingzhangos@google.com> Subject: [PATCH v6 2/6] KVM: arm64: Reject attempts to set invalid debug arch version From: Jing Zhang To: KVM , KVMARM , ARMLinux , Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Fuad Tabba , Reiji Watanabe , Raghavendra Rao Ananta , Suraj Jitindar Singh , Cornelia Huck , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230718_174732_734697_1231887D X-CRM114-Status: GOOD ( 15.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Oliver Upton The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't handled elegantly by the generic ID register plumbing, as the respective ID register fields have a nonzero starting value. Add an explicit check for debug versions less than v8 of the architecture. Signed-off-by: Oliver Upton Signed-off-by: Jing Zhang --- arch/arm64/kvm/sys_regs.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index c1a5ec1a016e..053d8057ff1e 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1216,8 +1216,14 @@ static s64 kvm_arm64_ftr_safe_value(u32 id, const struct arm64_ftr_bits *ftrp, /* Some features have different safe value type in KVM than host features */ switch (id) { case SYS_ID_AA64DFR0_EL1: - if (kvm_ftr.shift == ID_AA64DFR0_EL1_PMUVer_SHIFT) + switch (kvm_ftr.shift) { + case ID_AA64DFR0_EL1_PMUVer_SHIFT: kvm_ftr.type = FTR_LOWER_SAFE; + break; + case ID_AA64DFR0_EL1_DebugVer_SHIFT: + kvm_ftr.type = FTR_LOWER_SAFE; + break; + } break; case SYS_ID_DFR0_EL1: if (kvm_ftr.shift == ID_DFR0_EL1_PerfMon_SHIFT) @@ -1469,14 +1475,22 @@ static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, return val; } +#define ID_REG_LIMIT_FIELD_ENUM(val, reg, field, limit) \ +({ \ + u64 __f_val = FIELD_GET(reg##_##field##_MASK, val); \ + (val) &= ~reg##_##field##_MASK; \ + (val) |= FIELD_PREP(reg##_##field##_MASK, \ + min(__f_val, (u64)reg##_##field##_##limit)); \ + (val); \ +}) + static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd) { u64 val = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); /* Limit debug to ARMv8.0 */ - val &= ~ID_AA64DFR0_EL1_DebugVer_MASK; - val |= SYS_FIELD_PREP_ENUM(ID_AA64DFR0_EL1, DebugVer, IMP); + val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64DFR0_EL1, DebugVer, V8P8); /* * Only initialize the PMU version if the vCPU was configured with one. @@ -1496,6 +1510,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val) { + u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val); u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, val); /* @@ -1515,6 +1530,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; + /* + * ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a + * nonzero minimum safe value. + */ + if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP) + return -EINVAL; + return set_id_reg(vcpu, rd, val); } @@ -1528,6 +1550,8 @@ static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu, if (kvm_vcpu_has_pmu(vcpu)) val |= SYS_FIELD_PREP(ID_DFR0_EL1, PerfMon, perfmon); + val = ID_REG_LIMIT_FIELD_ENUM(val, ID_DFR0_EL1, CopDbg, Debugv8p8); + return val; } @@ -1536,6 +1560,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) { u8 perfmon = SYS_FIELD_GET(ID_DFR0_EL1, PerfMon, val); + u8 copdbg = SYS_FIELD_GET(ID_DFR0_EL1, CopDbg, val); if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) { val &= ~ID_DFR0_EL1_PerfMon_MASK; @@ -1551,6 +1576,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, if (perfmon != 0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3) return -EINVAL; + if (copdbg < ID_DFR0_EL1_CopDbg_Armv8) + return -EINVAL; + return set_id_reg(vcpu, rd, val); } From patchwork Tue Jul 18 16:45:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13318339 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1B761C001DC for ; Wed, 19 Jul 2023 06:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=oFfJAQvWuVfI9XmKgAWsnnPfBvyDL/SYA14vn/tJdYI=; b=JYwgWjgY6iFsuywb6IhGwBZ6D7 TYZB9+RlPTO4PlbnrddD2eJmOClt8/UWIkLahkSNResQtwnOfOnybNc4YG5Rzf9rniLXzB8j1DGpr olimLgJkdHoO7gUZkITi6teVh0AK8tTZloWdmFptbCsMZv4oovfbNuJuMw3VoRLY1xZizIgrEWO2e N8Vk2Y+mlKHCfTJrlI8aFOecdhet/4UjH8icdypve+vZ/x0CZiGIBirkicj+4MYUNII4aGWCzSeem wgYQnR4P1XQyY4IrmEaSQovUAInU4/WR0c4OsypTh21sf37MS3rkL3s6okzFD5D9ahpDHxuQMscZH njGzpsTw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM0jH-005lc9-1s; Wed, 19 Jul 2023 06:32:07 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM0jD-005lZY-1Z for linux-arm-kernel@bombadil.infradead.org; Wed, 19 Jul 2023 06:32:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=BJAEm87EHCeOWjKJA30wa69olvlB7/s8Sl9tx5zF2lQ=; b=ikL/Ve7QoVYoTOUo6fuA5NZ5V7 ++KbpKnCIA7+kmSGSE7LpucGBgkKymx/Z+g2idM5v1DGU3g/ZEka06LqbxgNNPrCCZN8QwPPLDHB4 OiJrNyhyHsg3sRqKnEAI5CTSfYPMR5eXXzZzpgzQHFSys8ODg9rOkQkO9wKyHP3WDBaDPb5HZ8F0G Zp3jDor7u6cqKksJv2pSK87WiXTCpkU17JqAaG9ulxDTsghs8B5SkA4xWSBEJtmMR0YJbdCsf+yWH XxRuxnym8qW2jbiDoQ+grBzQJZHEw6lxYX+b0EHTo9kDF3mGoeCjR9+HE5jClvbtLNXloICT77dUb ZK++cOZA==; Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLnrO-00BlUx-05 for linux-arm-kernel@lists.infradead.org; Tue, 18 Jul 2023 16:47:39 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c0d62f4487cso5154370276.0 for ; Tue, 18 Jul 2023 09:47:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689698733; x=1692290733; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BJAEm87EHCeOWjKJA30wa69olvlB7/s8Sl9tx5zF2lQ=; b=tfkUfwMnUrklnQXtS24STivSqw+iQ+fA4TFwL7HBlnVEqzpPO8wCQ0LzpDSsOXDIpq x1/xbW5Lhl6F4kgls2JBqjroHcaQnRMYYJLqW0gMXBA7aLWFZfF7BOvE2ePSI4YJGRyq tywUlFjWuB1m0rig8gPtAG6ItxfiXWgZks/sD6CDikU55wFiKZseByVkSBlcnOcVegfQ +i69uPR777tzcJrLjb0dFAMyV7mgl0lLfmDOUSf99DAyjF20886gF+hjgJTC1GBY8SF/ +MkAxzuIn0PQYGkJCzAuprDebJHw1S8ISx4qq165nToNntEQ+6B/I30mZedwjS5SCiro 8iBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689698733; x=1692290733; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BJAEm87EHCeOWjKJA30wa69olvlB7/s8Sl9tx5zF2lQ=; b=P6SlwdXsgb3WIzX3yqOAytzy92WLZJQZ9F7c7nTAf44U8vsfTLot725kO5w90jUzJ+ S3pgopwha/3hzKCnb8AuBd+XHIy+dSPCkX2SSm95O65QLDpLhDEvvllJc/iLyLNGOcwR KTlIDe5r4z6DqgKwruqCD17mHw5AmZg1KK8KYvXMVGCXnWW/+LnBOkMhndN+UZntw1bA gaQnnuWkr0kYaWnpkpNroA4yqpwVGwMGn/mifkVLqgCOCZgJlnUZ7JYk0YXHrESeFrLN MKTcvIDxgIuYO3fQGTCydJxUJaejkWsMbKEBqLgZA2aCk56DVtCjfaouBjG8XCquO2OA 3GHQ== X-Gm-Message-State: ABy/qLb4WdsPwYrNo0RW5LvVnV8fhx6RzIpSvq1dplr3iG004vN7Zwcz pUGN00X9f9/QCpepMS2wMumEHfjELb/S37jY+w== X-Google-Smtp-Source: APBJJlG7+TffRkqMu2lcoVZqj2J4kwXnhwITwWw6ib/WW3uqG7zZKtjT/gjSzmWi7SNVPapr4h2k9BU/V5PlgHmjwA== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a25:9745:0:b0:ce9:4c77:bc0b with SMTP id h5-20020a259745000000b00ce94c77bc0bmr2571ybo.11.1689698732911; Tue, 18 Jul 2023 09:45:32 -0700 (PDT) Date: Tue, 18 Jul 2023 16:45:19 +0000 In-Reply-To: <20230718164522.3498236-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230718164522.3498236-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718164522.3498236-4-jingzhangos@google.com> Subject: [PATCH v6 3/6] KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1 From: Jing Zhang To: KVM , KVMARM , ARMLinux , Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Fuad Tabba , Reiji Watanabe , Raghavendra Rao Ananta , Suraj Jitindar Singh , Cornelia Huck , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230718_174738_199260_F80F0BD3 X-CRM114-Status: GOOD ( 11.02 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from usrespace with this change. Signed-off-by: Jing Zhang --- arch/arm64/kvm/sys_regs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 053d8057ff1e..f33aec83f1b4 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -2008,7 +2008,7 @@ static const struct sys_reg_desc sys_reg_descs[] = { .set_user = set_id_dfr0_el1, .visibility = aa32_id_visibility, .reset = read_sanitised_id_dfr0_el1, - .val = ID_DFR0_EL1_PerfMon_MASK, }, + .val = GENMASK(63, 0), }, ID_HIDDEN(ID_AFR0_EL1), AA32_ID_SANITISED(ID_MMFR0_EL1), AA32_ID_SANITISED(ID_MMFR1_EL1), @@ -2057,7 +2057,7 @@ static const struct sys_reg_desc sys_reg_descs[] = { .get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, .reset = read_sanitised_id_aa64dfr0_el1, - .val = ID_AA64DFR0_EL1_PMUVer_MASK, }, + .val = GENMASK(63, 0), }, ID_SANITISED(ID_AA64DFR1_EL1), ID_UNALLOCATED(5,2), ID_UNALLOCATED(5,3), From patchwork Tue Jul 18 16:45:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13318321 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5672DEB64DA for ; Wed, 19 Jul 2023 06:22:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=wV7NaEsWhoe8h6zjQHC8CdUfLVDU5XwTU9knwKw727E=; b=WMSzBu366LYoxAfCnLKaQ7xSjT TyJJv2XCZmAi44ne6j8K2NNW3J28ucjrAS872QJ5nvRgZ4TZZPPL1CBXa0E1UVKeUb0gfouzEg8Ye xit6QWisuY/33N+N0g3YzuM/XNSNDiiKSr3z4V/WSt/hYc5aRpHUefc7Z46zv2eX5oVnOONPX8xyg 0NiHrIO8V1QzYqnIq7TCCmDlEI+x6B68j5L7jb2NPANtApqva/T1FtsObGa4O2kKQjqrDhld9oz+d INoH8PFQKpzTxtk46p5of1XCz57gTWNxxDp6aYwXWXngABzV5Mge1Vi+9EhjTuf4oAQzXWTzNkgYQ fbgzixnw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM0Zi-005ggg-0r; Wed, 19 Jul 2023 06:22:14 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM0Zg-005gen-0Q for linux-arm-kernel@bombadil.infradead.org; Wed, 19 Jul 2023 06:22:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=xS0acJp+aIDXIQPo5QGK2XMol5RfSj1wMM22zpXYALg=; b=SDgDBIiYs7UbXtNOG4fBivjel7 7shT/ls7UcDI5a54unvKKLoKLT75gFqLqujAV40qy2S9xzGo3b2WMdcDoXOGlGbkLNMap7QCwnM90 mjNy5ffMmTFb/jIgbYyRwKgHuaMKPSCzN4cjUBfP0gEpCCwa6UchJWLQ8MkCYUvCeCbvOfhumvcgu tlmSV8ccfCDKJ63fsP4TpVLxfrwwQBgPzcD72+90oxvxiTySyphUufibtKyNBuEf30oZNidN7clGT 5EXDyHLlZR4RxN1FddR1asau1uN5ERoVfAB8ZVg8RiaHrEajxGHsjRuAho32wJfhQ9v8RxKEWcSol MJuYF3UA==; Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLnqS-00BlT1-1K for linux-arm-kernel@lists.infradead.org; Tue, 18 Jul 2023 16:46:42 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-57704a25be9so84887917b3.1 for ; Tue, 18 Jul 2023 09:46:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689698734; x=1692290734; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xS0acJp+aIDXIQPo5QGK2XMol5RfSj1wMM22zpXYALg=; b=EmteLCMQlNbrceRaps12nferypQUXRntTEFrR9lZz6K827vbH7l9xInXAD8nRBwgvg Nva18ll74bKF8/JyGdMnCPK6Z5eslobhaqxhrZQXBercFtxS1nvRRu6VslmWJ9U5xlzC J82k7CDSz52ze/FeoVydqdPa0/dVg1iSLdO9q1FCCO8sqCF5WHiCRTu4+p/e9SlcTfbA Rc/JesNjjDk9jSuEVjFWNQFfHkMlTK2oJmGd9ptCIhR5AoSRUHhNWPA264e7oLmqdDgL +2Y7q2wR2u7v/LnQXbl2PTC6Z0bqSTlqmvRIh6DIgrGRYcHCTTCxgzeZ5vJgJiP3rQ9T j/Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689698734; x=1692290734; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xS0acJp+aIDXIQPo5QGK2XMol5RfSj1wMM22zpXYALg=; b=ktaryxDCnqk1xyLmLaUBnqlCWE6+D8vzO287pgzmgIiYaaVgU+Lv5VkWpydzadz/o1 RKZOKgwTjgVnVQa79NYpmOPj+2DJufa0Tk8v2erSfNAHmMXX8apwYM7JtNZ9og6tDZTa LPAhA0RJarT7ZSkYVX1grGtzIuUL1qj2esbRHddfFk2AxitPM2RrzXzYQNUQaj1CO496 HBzfXw2eRUoz16HXdm0Gax930CFj5X+aMVABb4yp4tLx//DYFJ3tgtpm8FTzEE19pib3 Ykp0CRNOrOaumTChUl7IQsMeRAunst7OFsBen/ATuhUgyO4HYxu+EROfS6ozJpwgvrtg kRyA== X-Gm-Message-State: ABy/qLa/8+6pL1KVFhTsFVfo58UuySMtjFfAjmfWglKeXxm+wq68swVk oYfGCaQMA0l8XnewKdqYilX8isbSebEfJXy6UQ== X-Google-Smtp-Source: APBJJlEQmDSoHkGi9JQIAr4wuITHz7fAluVasMRt8k18NTKlsRsIsnD0bdJ4EqDubIUM0oQB7DLI1CT99QjPdZDFHQ== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a81:fe09:0:b0:57a:6019:62aa with SMTP id j9-20020a81fe09000000b0057a601962aamr237002ywn.5.1689698734735; Tue, 18 Jul 2023 09:45:34 -0700 (PDT) Date: Tue, 18 Jul 2023 16:45:20 +0000 In-Reply-To: <20230718164522.3498236-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230718164522.3498236-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718164522.3498236-5-jingzhangos@google.com> Subject: [PATCH v6 4/6] KVM: arm64: Enable writable for ID_AA64PFR0_EL1 From: Jing Zhang To: KVM , KVMARM , ARMLinux , Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Fuad Tabba , Reiji Watanabe , Raghavendra Rao Ananta , Suraj Jitindar Singh , Cornelia Huck , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230718_174640_608458_4F50DF2D X-CRM114-Status: GOOD ( 10.91 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org All valid fields in ID_AA64PFR0_EL1 are writable from usrespace with this change. Signed-off-by: Jing Zhang --- arch/arm64/kvm/sys_regs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index f33aec83f1b4..12b81ab27dbe 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -2042,7 +2042,7 @@ static const struct sys_reg_desc sys_reg_descs[] = { .get_user = get_id_reg, .set_user = set_id_reg, .reset = read_sanitised_id_aa64pfr0_el1, - .val = ID_AA64PFR0_EL1_CSV2_MASK | ID_AA64PFR0_EL1_CSV3_MASK, }, + .val = GENMASK(63, 0), }, ID_SANITISED(ID_AA64PFR1_EL1), ID_UNALLOCATED(4,2), ID_UNALLOCATED(4,3), From patchwork Tue Jul 18 16:45:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13318358 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CAF3DC001B0 for ; Wed, 19 Jul 2023 06:41:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=0ZejQH1/nmEBsBtuQM4LUae0jonEv7Id1gHj/hU4jr0=; b=EfpDzKYrFYzVqCpIGc5Oc7unsR rkXMvGJm2244vK1XjXBiQZqiwY6v9EVY9It1TN/AbwXV4YoNuof/jYuaVI0U1MJflcWFE/9q2C/Bo l73P6U3PSHHkfP5hzKy3HPlo9eAUyRp/1y+GmeDGES4uQyAgDAkSpFi7511GEAdZKQ4lxReiLYATk EVqGJta8PUe48U7nJEKp8hQk66SK0VR0JjLsAWYkmMvw7Rh7T+lT7wO64KqQPld1EarhItk2b3q/2 hGgwLC+ie1CaYyg2KfqC9cgope9NWh94pwGGz6ksrfRr7bsMfL+gEUnxv1aNz/DudWthC8DNqQ5au XwLEmlzQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM0sG-005qdV-1f; Wed, 19 Jul 2023 06:41:24 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM0sD-005qc8-0X for linux-arm-kernel@bombadil.infradead.org; Wed, 19 Jul 2023 06:41:21 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=7vkCbr047ltjyeNSod64c+hA+AaA1LNd8Iy2F1bOfBE=; b=LQWYEMtZlAMAfaAtm0nGUs1+UL a9eLL02sv9OJiq2JdlwG1SmEeUXPV84S4X2Poh6e1z7OIouirh48K/sIrCiSsQj8EETdYFTptJ6Dr voF5SaUrXvxCMU4ONytMc5RE0zbs5J4FwybO7qjaTyuc548fzIEdiL4GiSXYcJFfHlU1WpmZHPzxv WGgcnjKJ3KmmlRm9P+eYkB5xj0jTqstNBi1mnRBajbvE0nX/4Qr2gjfc+fjyjwEafsowSOd63Wois 2r6Dj3RnDwdiuHMUUAUtC004ugex7nuKJg87u+4y1J4Fq0qZNXbar39mLRxPaYONr5+qVtfwAQsan C5oi818Q==; Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLnrP-00BlV3-0N for linux-arm-kernel@lists.infradead.org; Tue, 18 Jul 2023 16:47:40 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-666e3dad70aso5094178b3a.0 for ; Tue, 18 Jul 2023 09:47:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689698736; x=1692290736; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7vkCbr047ltjyeNSod64c+hA+AaA1LNd8Iy2F1bOfBE=; b=yDDdm3Z98ZL51ezeU3dyZisGjIZSm1scHPakb810LlPtbzZdWtSUIYn/IEOov5bS/a HhyaKTMZfG3GGLqUcOZEO3jWAbeBWbTi5j8vYHEICwvgbi8Qv93SV86ZXZ0R9uRIOEfU tfW4QxvXTLOQmrAp6N5uWwOcYGCQ1yrqQNnUXmzNz4YhAyyA4TJE3vbPvZe52JEjQ19o rSCyDjo7l5I7KgLtNETHDoB9SexVb9joZl0xIE+DtxXUBwwJrN0HrgEesHZL1dS3a3f9 1WXahQMOWqNCdfgQgckue6kJBtPxpTLLOYavVWjQIXyOfD4f5EYp3T1flDKM9NrTNHJg ca5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689698736; x=1692290736; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7vkCbr047ltjyeNSod64c+hA+AaA1LNd8Iy2F1bOfBE=; b=ioOEQ0g4saFVdy1rFhBOvgPEl+I+2B2d4cRRLrrFVhyeq+at4yf0tkq8Hw3S/aABJc AU9HLYei4Pp8G2L0ny1w3QrknGgzB8mT7zAqQS6q23e6+WS2uWpn8pZwEZBH/iWJeYpO tfJRwZJGDY7/yQARHeA4E7G46FDTGYRcwF9bclAHBzh2CXdhF7bzORftkzyvlozLZpmh FnMIRiK5apoddnouRvL/ZHdsT4p39M0LTHL3c35PffMl8JS0Jna9DiIjiA+Ub4Tux1ic 6LDZGxZZRNNJ537djvMbqP687sv1l7sjp81YFOS0R0C/uLeU1w3cf30oqaxKe7L5Lb5L r6Rg== X-Gm-Message-State: ABy/qLYBQgjXZIKtQA0pwMPetdprK3NwTDnOeZS1Kf518ngzsrPFu9td 8hWGZ5THa9SwZDccBCLbFIunVOGHagQUPjTslg== X-Google-Smtp-Source: APBJJlHDDu+HxPs3scZlNF4+YoM4eumiXMxf7JemKCB4eK0aLPYXrlzf6mMhZxkgAMPgiQBQLnY7YjFAQ4oSoy0Zbw== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a05:6a00:158e:b0:677:c9da:14b6 with SMTP id u14-20020a056a00158e00b00677c9da14b6mr159486pfk.4.1689698736287; Tue, 18 Jul 2023 09:45:36 -0700 (PDT) Date: Tue, 18 Jul 2023 16:45:21 +0000 In-Reply-To: <20230718164522.3498236-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230718164522.3498236-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718164522.3498236-6-jingzhangos@google.com> Subject: [PATCH v6 5/6] KVM: arm64: Enable writable for ID_AA64MMFR{0, 1, 2, 3}_EL1 From: Jing Zhang To: KVM , KVMARM , ARMLinux , Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Fuad Tabba , Reiji Watanabe , Raghavendra Rao Ananta , Suraj Jitindar Singh , Cornelia Huck , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230718_174739_261058_4BD4E25A X-CRM114-Status: GOOD ( 11.32 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Enable writable from userspace for ID_AA64MMFR{0, 1, 2, 3}_EL1. Added a macro for defining general writable idregs. Signed-off-by: Jing Zhang --- arch/arm64/kvm/sys_regs.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 12b81ab27dbe..d560fc178a76 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1346,9 +1346,6 @@ static u64 __kvm_read_sanitised_id_reg(const struct kvm_vcpu *vcpu, val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_WFxT); val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_MOPS); break; - case SYS_ID_AA64MMFR2_EL1: - val &= ~ID_AA64MMFR2_EL1_CCIDX_MASK; - break; case SYS_ID_MMFR4_EL1: val &= ~ARM64_FEATURE_MASK(ID_MMFR4_EL1_CCIDX); break; @@ -1582,6 +1579,18 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, return set_id_reg(vcpu, rd, val); } +static u64 read_sanitised_id_aa64mmfr2_el1(struct kvm_vcpu *vcpu, + const struct sys_reg_desc *rd) +{ + u64 val; + u32 id = reg_to_encoding(rd); + + val = read_sanitised_ftr_reg(id); + val &= ~ID_AA64MMFR2_EL1_CCIDX_MASK; + + return val; +} + /* * cpufeature ID register user accessors * @@ -1856,6 +1865,16 @@ static unsigned int elx2_visibility(const struct kvm_vcpu *vcpu, .val = 0, \ } +#define ID_SANITISED_WRITABLE(name) { \ + SYS_DESC(SYS_##name), \ + .access = access_id_reg, \ + .get_user = get_id_reg, \ + .set_user = set_id_reg, \ + .visibility = id_visibility, \ + .reset = kvm_read_sanitised_id_reg, \ + .val = GENMASK(63, 0), \ +} + /* sys_reg_desc initialiser for known cpufeature ID registers */ #define AA32_ID_SANITISED(name) { \ SYS_DESC(SYS_##name), \ @@ -2077,10 +2096,15 @@ static const struct sys_reg_desc sys_reg_descs[] = { ID_UNALLOCATED(6,7), /* CRm=7 */ - ID_SANITISED(ID_AA64MMFR0_EL1), - ID_SANITISED(ID_AA64MMFR1_EL1), - ID_SANITISED(ID_AA64MMFR2_EL1), - ID_SANITISED(ID_AA64MMFR3_EL1), + ID_SANITISED_WRITABLE(ID_AA64MMFR0_EL1), + ID_SANITISED_WRITABLE(ID_AA64MMFR1_EL1), + { SYS_DESC(SYS_ID_AA64MMFR2_EL1), + .access = access_id_reg, + .get_user = get_id_reg, + .set_user = set_id_reg, + .reset = read_sanitised_id_aa64mmfr2_el1, + .val = GENMASK(63, 0), }, + ID_SANITISED_WRITABLE(ID_AA64MMFR3_EL1), ID_UNALLOCATED(7,4), ID_UNALLOCATED(7,5), ID_UNALLOCATED(7,6), From patchwork Tue Jul 18 16:45:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13318227 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B8B85EB64DA for ; Wed, 19 Jul 2023 05:31:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=1Kb1vpWZBGFqIk1j8u4izw1uFrKOXdVl4YMY6lLFSdE=; b=n/ShB1GAG6icbM3wXpdw91cXid z2xr2q0Yk7NAqXlypySI3ZJR0mOBbXyNfyPX51OT39N8A/CIu+g4MagHSE5S1jcQg5bHeXTh/7vMc L6AejT9x3O9jBTNIlbdqPr+KZ8CVIGjOXTrvf4pTk+h0mOui+iy7e8PqDQhJle/r5dQqonC3M8oPu zkCSSRYVoJaz5XeSrvpNDyzf6kg+wcfG5tgH/XOuQlZpwdBwHA+94bUmMrWXMayaL2VZvG7Me52rL lrEataSx/hfkTClOhtzn7jCU0+IzQMw3sAxInWqNEYfSLJXyHPW9JekpeUo8/eSaOpxpn6NgjxUvT wBqgS39w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qLzmn-005KId-2W; Wed, 19 Jul 2023 05:31:41 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLzmm-005KIM-0Q for linux-arm-kernel@bombadil.infradead.org; Wed, 19 Jul 2023 05:31:40 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=f6NXlz6D/782SdBzr3IeFN6HXvUmoGCjaw12aI23xnw=; b=eirCHIaKHPle+8K16VsVAUtOfm 9nHHdqmRA5i6Y4OjpsYJE1GE8gS9wjDPb2NJ9BEp5pNKBmj6oBxEUNuu8n+5UBcjC+0xdJPvk77Y5 3bzhtKxWG6a5XpDUm3EVa1xaN8Kah35PKYWdB2a2p6TeW4nZqco0r/Ufjd4fisCjo9kSiLzmfWaIF G2kz4pL4YWZLLyIzGclRcqgrGB1z6Vvid8LSjeSIY/JCkwY3TTITaAae9mu3UfqXQ1kT9ULPOYa5F dNPxUYNyvcHOp044+Bjlzrd+RqUMf45cWnfO9V7z8PGeVk/BrmXZnNYvymKdk0uJsqGiu+Y8JtKXV i1w78hnQ==; Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLnqc-00BlTi-28 for linux-arm-kernel@lists.infradead.org; Tue, 18 Jul 2023 16:46:52 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-57704af0e64so53477917b3.0 for ; Tue, 18 Jul 2023 09:46:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689698738; x=1692290738; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=f6NXlz6D/782SdBzr3IeFN6HXvUmoGCjaw12aI23xnw=; b=UgpR3zd8Vwdo9jKsFeX+C8lfGVgsHVsC93K4Srj+qwQBEmH4IbeK9gCIxkC2gkmyzF sVYc7CatQ4EcZD82XuyUrFD/d8AsOlh52qlFYCTLHGf2FcZEBLtW7Sw3ZTAx2xSxCvkj gKoiKRD5CQzXa7Y9oLNTgyZBNgau9c9v4bZyYcZPXwjFYABruk+nl92hUHkVeXFlY3dX 6wPEyCuU75lk5fLn0FGaX4rur/c8ZRuSrfqpq6s8rDvsYwlr9xzXiBcBZd1uZhKMQ6Oi efspitsNXWraehwYFLxevmEQMMUOTGFO/rsSshN/hvf5ThmySgFOQVR67x/lXMsTaMyJ GmLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689698738; x=1692290738; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=f6NXlz6D/782SdBzr3IeFN6HXvUmoGCjaw12aI23xnw=; b=aJrL4ah0a6sIe82s46lGdDIGMo9vHquzX4qJp8M4pD3tw59+sXugMa4xMYIyKNuFWQ WlI/Hi/IgXjg87y1nYBkQ5m4G+G73MHRP0oWH9EeCmv5EjPEKIAaGGJy9hHedkvLy9bM wGEi+tNJUCEJAoN1JusKmewpNIJgbCIFFpnJYXCoertrLlCgBBWA94mS2XqgOl35zJJZ zgcJ2XFL+8Jt4+QDNgi1f/U0KhMazTyPrWcTzZCPWrpVHuqgjsKyer0B6xJya3jxfiIr 0BeoMvY2QstI2KjWAyX1ApXizd0cKKTjfV+Vs1kccSa4Vc7VuoWZnkV74HXFw3+gphIo 2VMQ== X-Gm-Message-State: ABy/qLYN5umvqsE3Fx/PoDjRVafsV6jiJBNuS0sqc/qoNjhN9eBaGufn J85DWtuD8+OOior5A/ag70wT2bqGWGAqD6rQAA== X-Google-Smtp-Source: APBJJlFQfOABzqiiF7DAq8fLGdHU4mtWhNKbQb0rOFTg9K46DQvwrbM/DePnnn41SzrwvpIZDYuIXA5dFNd5TFmZVA== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a81:b70d:0:b0:581:7b58:5e70 with SMTP id v13-20020a81b70d000000b005817b585e70mr3233ywh.5.1689698738270; Tue, 18 Jul 2023 09:45:38 -0700 (PDT) Date: Tue, 18 Jul 2023 16:45:22 +0000 In-Reply-To: <20230718164522.3498236-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230718164522.3498236-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718164522.3498236-7-jingzhangos@google.com> Subject: [PATCH v6 6/6] KVM: arm64: selftests: Test for setting ID register from usersapce From: Jing Zhang To: KVM , KVMARM , ARMLinux , Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Fuad Tabba , Reiji Watanabe , Raghavendra Rao Ananta , Suraj Jitindar Singh , Cornelia Huck , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230718_174650_840742_D4E2E6FA X-CRM114-Status: GOOD ( 17.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a test to verify setting ID registers from userapce is handled correctly by KVM. Signed-off-by: Jing Zhang --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/set_id_regs.c | 163 ++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index c692cc86e7da..87ceadc1292a 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -144,6 +144,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list TEST_GEN_PROGS_aarch64 += aarch64/hypercalls TEST_GEN_PROGS_aarch64 += aarch64/page_fault_test TEST_GEN_PROGS_aarch64 += aarch64/psci_test +TEST_GEN_PROGS_aarch64 += aarch64/set_id_regs TEST_GEN_PROGS_aarch64 += aarch64/smccc_filter TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config TEST_GEN_PROGS_aarch64 += aarch64/vgic_init diff --git a/tools/testing/selftests/kvm/aarch64/set_id_regs.c b/tools/testing/selftests/kvm/aarch64/set_id_regs.c new file mode 100644 index 000000000000..e2242ef36bab --- /dev/null +++ b/tools/testing/selftests/kvm/aarch64/set_id_regs.c @@ -0,0 +1,163 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * set_id_regs - Test for setting ID register from usersapce. + * + * Copyright (c) 2023 Google LLC. + * + * + * Test that KVM supports setting ID registers from userspace and handles the + * feature set correctly. + */ + +#include +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" +#include + +#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) +#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask)) + +struct reg_feature { + uint64_t reg; + uint64_t ftr_mask; +}; + +static void guest_code(void) +{ + for (;;) + GUEST_SYNC(0); +} + +static struct reg_feature lower_safe_reg_ftrs[] = { + { KVM_ARM64_SYS_REG(SYS_ID_AA64DFR0_EL1), ARM64_FEATURE_MASK(ID_AA64DFR0_BRPS) }, + { KVM_ARM64_SYS_REG(SYS_ID_DFR0_EL1), ARM64_FEATURE_MASK(ID_DFR0_COPDBG) }, + { KVM_ARM64_SYS_REG(SYS_ID_AA64PFR0_EL1), ARM64_FEATURE_MASK(ID_AA64PFR0_EL3) }, + { KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR0_EL1), ARM64_FEATURE_MASK(ID_AA64MMFR0_TGRAN4) }, +}; + +static void test_user_set_lower_safe(struct kvm_vcpu *vcpu) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(lower_safe_reg_ftrs); i++) { + struct reg_feature *reg_ftr = lower_safe_reg_ftrs + i; + uint64_t val, new_val, ftr; + + vcpu_get_reg(vcpu, reg_ftr->reg, &val); + ftr = field_get(reg_ftr->ftr_mask, val); + + /* Set a safe value for the feature */ + if (ftr > 0) + ftr--; + + val &= ~reg_ftr->ftr_mask; + val |= field_prep(reg_ftr->ftr_mask, ftr); + + vcpu_set_reg(vcpu, reg_ftr->reg, val); + vcpu_get_reg(vcpu, reg_ftr->reg, &new_val); + ASSERT_EQ(new_val, val); + } +} + +static struct reg_feature exact_reg_ftrs[] = { + { KVM_ARM64_SYS_REG(SYS_ID_AA64DFR0_EL1), ARM64_FEATURE_MASK(ID_AA64DFR0_DEBUGVER) }, +}; + +static void test_user_set_exact(struct kvm_vcpu *vcpu) +{ + int i, r; + + for (i = 0; i < ARRAY_SIZE(exact_reg_ftrs); i++) { + struct reg_feature *reg_ftr = exact_reg_ftrs + i; + uint64_t val, old_val, ftr; + + vcpu_get_reg(vcpu, reg_ftr->reg, &val); + ftr = field_get(reg_ftr->ftr_mask, val); + old_val = val; + + /* Exact match */ + vcpu_set_reg(vcpu, reg_ftr->reg, val); + vcpu_get_reg(vcpu, reg_ftr->reg, &val); + ASSERT_EQ(val, old_val); + + /* Smaller value */ + if (ftr > 0) + ftr--; + val &= ~reg_ftr->ftr_mask; + val |= field_prep(reg_ftr->ftr_mask, ftr); + r = __vcpu_set_reg(vcpu, reg_ftr->reg, val); + TEST_ASSERT(r < 0 && errno == EINVAL, + "Unexpected KVM_SET_ONE_REG error: r=%d, errno=%d", r, errno); + vcpu_get_reg(vcpu, reg_ftr->reg, &val); + ASSERT_EQ(val, old_val); + + /* Bigger value */ + ftr += 2; + val &= ~reg_ftr->ftr_mask; + val |= field_prep(reg_ftr->ftr_mask, ftr); + r = __vcpu_set_reg(vcpu, reg_ftr->reg, val); + TEST_ASSERT(r < 0 && errno == EINVAL, + "Unexpected KVM_SET_ONE_REG error: r=%d, errno=%d", r, errno); + vcpu_get_reg(vcpu, reg_ftr->reg, &val); + ASSERT_EQ(val, old_val); + } +} + +static struct reg_feature fail_reg_ftrs[] = { + { KVM_ARM64_SYS_REG(SYS_ID_AA64DFR0_EL1), ARM64_FEATURE_MASK(ID_AA64DFR0_WRPS) }, + { KVM_ARM64_SYS_REG(SYS_ID_DFR0_EL1), ARM64_FEATURE_MASK(ID_DFR0_MPROFDBG) }, + { KVM_ARM64_SYS_REG(SYS_ID_AA64PFR0_EL1), ARM64_FEATURE_MASK(ID_AA64PFR0_EL2) }, + { KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR0_EL1), ARM64_FEATURE_MASK(ID_AA64MMFR0_TGRAN64) }, +}; + +static void test_user_set_fail(struct kvm_vcpu *vcpu) +{ + int i, r; + + for (i = 0; i < ARRAY_SIZE(fail_reg_ftrs); i++) { + struct reg_feature *reg_ftr = fail_reg_ftrs + i; + uint64_t val, old_val, ftr; + + vcpu_get_reg(vcpu, reg_ftr->reg, &val); + ftr = field_get(reg_ftr->ftr_mask, val); + + /* Set a invalid value (too big) for the feature */ + ftr++; + + old_val = val; + val &= ~reg_ftr->ftr_mask; + val |= field_prep(reg_ftr->ftr_mask, ftr); + + r = __vcpu_set_reg(vcpu, reg_ftr->reg, val); + TEST_ASSERT(r < 0 && errno == EINVAL, + "Unexpected KVM_SET_ONE_REG error: r=%d, errno=%d", r, errno); + + vcpu_get_reg(vcpu, reg_ftr->reg, &val); + ASSERT_EQ(val, old_val); + } +} + +int main(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + + ksft_print_header(); + ksft_set_plan(3); + + test_user_set_lower_safe(vcpu); + ksft_test_result_pass("test_user_set_lower_safe\n"); + + test_user_set_exact(vcpu); + ksft_test_result_pass("test_user_set_exact\n"); + + test_user_set_fail(vcpu); + ksft_test_result_pass("test_user_set_fail\n"); + + kvm_vm_free(vm); + + ksft_finished(); +}