From patchwork Mon Jul 10 19:24:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13307534 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 CF462C0015E for ; Mon, 10 Jul 2023 19:25:18 +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=uyD1YZ+9oyCcvL+RIonWWxch2I KsjUdFmdipJDwJpXBUFclhro0lPRXW8tvHmE8rXbj3XAg1exNeOsH863xsEw6yJcmdAw3rszz00HR tkjoqXVJjYnYXP9ZO5ElJCaA9rm5na7xSFyKwD/O3YVh+OZdmO//X86xFJaHctABzAvDBTJ4QYyTC aDYI7fdLq1fxRfxbXPSE+aj3OfyC8jscyVgL5O8ptHwCl/FxTHo1slTXvp8EQUY8/1OF0NLBkshUR kqfj1WEV4VFC9N3OYNt3uXDddPWROnv0DuDSLqvZWdNHxtUo8SJymJYb9yiPFAA/awpj+4ilUNNUK hUf1CSXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qIwUz-00CYJD-1M; Mon, 10 Jul 2023 19:24:41 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qIwUw-00CYHU-1j for linux-arm-kernel@lists.infradead.org; Mon, 10 Jul 2023 19:24:39 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1b8130aceefso74216995ad.2 for ; Mon, 10 Jul 2023 12:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689017075; x=1691609075; 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=WEL8yeJBXLqzExhD6IHURdek5D4wvVU763ij1nXK8iYTaLrwHbm45oCwnW5EiOyICr ECOVHHRt04C8aCXCcHarD1e5tpbz/VD9139ooil7auwC5ZX9SeFEFSQJQEYvgMCiTJJT priMyy7eGQZOHQvWd+Rnvzdk2a6yDeqadOA2wAjDN8MohQcROJA4VCNaJhMeqzczzGNc Q4fO1VvryLtbUuk7JkXj6jkZV2sPZ65InW8KYpLm/PQIIg1Nyn5ZjHpKrB8rZ3myZCNl WhDLT/kCjBT6z1aYNMZP2KN/0p6C17+ILwBIKj2LCquGaWN+Po9fmNCZD1mreciXtBox ZcGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689017075; x=1691609075; 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=bN3IamNFEbEQjGLJfNyDOwYA2Xg3pE3Q3J6E0XK/vseUPl+mwsrbeuYj5K3QZAiNAO 2M9E+0LCKJyDrxLv3qf4NNU1GwhO3Jh107u7/9j6/5wZPHxge+XivFC6Sk0WRK84iUMt Fhj774k2Z7xtqOvZhHEGvIILOUgS95/gbduG1DgI3E1DhJTIEUd4woqOoYqByDNCtZlm gGOFEuRzz9rSSe5+Je4PDLffxYIWWgNPggt78Ay2HSzT+j3h7qJFdUXkKRc45wo9EgS2 UATpmaEJU3IicgpctvKCzaUclglK915F7jtf3076D7pSpOQOZEbT5GW7MtGBOFU8YUzD 7Kyg== X-Gm-Message-State: ABy/qLYLHimZljuo2Mfu88iNl1DLZf/FwXRNOZBKVUzC4gjiY+ApVodF p2ltkdN0ajpuik1JPHN0eclMxBFgW+vHISXdUw== X-Google-Smtp-Source: APBJJlF18xsYgsI0CxYuUZH80Cwz7kk1zPTVFelfLYuD+GDrkVgWul4z+afhe2yJxGfDVGHbruy74hyRYY9cbvtPLw== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a17:903:41c9:b0:1b7:ef3f:5ed3 with SMTP id u9-20020a17090341c900b001b7ef3f5ed3mr13176575ple.5.1689017075354; Mon, 10 Jul 2023 12:24:35 -0700 (PDT) Date: Mon, 10 Jul 2023 19:24:24 +0000 In-Reply-To: <20230710192430.1992246-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230710192430.1992246-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230710192430.1992246-2-jingzhangos@google.com> Subject: [PATCH v5 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-20230710_122438_572596_2BC29EC3 X-CRM114-Status: GOOD ( 10.59 ) 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 Mon Jul 10 19:24:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13307532 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 84AEDEB64DA for ; Mon, 10 Jul 2023 19:25:12 +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=IX23sUhXGhdJaW7yswsvTsbJrGXCoufM/ohQQG/zGaU=; b=gtNOHfedn9z2iQHiYLcW8f74uB 7SHKv0Kge25hPB/Mt0XHKvUuDTy5EdoJzWFlUrkBK82VBwszDmJvLUfpCAjS5O6DYfXeRdVgaFMqt +Sr+5PS8wuvvc9pEVZ/uUDLKNGE5LAWoOTnfUAflVg+gBbkuXHpuBQWTyHbwa/90bH0cZhjblZ1Gg PG+w7zj7DITDPTM/XI7w2FPDhMPTGbC3JT6tPOhgiwsAbL6axuwNSafOwSlFxNGrhvpay06CBeFSx Ga5VP6O0H+1iVYaKal3Da3A6GFcgwfbygFq+2T5RmUlo+3ooAf55W+nu+1v5CnEVudVIhKgjHPbBV zmRJAX3w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qIwUz-00CYJm-39; Mon, 10 Jul 2023 19:24:41 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qIwUw-00CYHj-1y for linux-arm-kernel@lists.infradead.org; Mon, 10 Jul 2023 19:24:39 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c361777c7f7so9072675276.0 for ; Mon, 10 Jul 2023 12:24:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689017077; x=1691609077; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NjXGqinFg6JbUCcCuD1314VDCv679WJcMyQaO6pNuqI=; b=riWsju77MCgLxcyPUA5zvABoTX1/n+uRhqXiNScFzay1HPX99z0tc697bB4UPgxFup v6vjFDK+OpdAh+ZfjD+AN7l24j6yRvX3XtBvROnruXBLFl2kWNA8PHx/58YzuBVEhk5A tzoGrsxzCwHBrSrfUBwlAu5r1zF87t1cTryo+Ov3gX8k5EDIk63Rk7uqhpoTmNfHtVXW iB6XkV+A/4mmcFig5KzmD1Tjd2NkDPiTGmGTlD/9MwaZtYufwCgye+a0Yei9/XK5XQg8 xopGAsDxQTs9dWPaMe8zfQxyD4pigDfKPNFKYllHwZX0MpFq1iOzO70eUexC7njvYRQ6 kegg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689017077; x=1691609077; 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=NjXGqinFg6JbUCcCuD1314VDCv679WJcMyQaO6pNuqI=; b=gSZ8TZUGDBmxObx6NOzVTjtY8EhGkKioHO1gXOXtTfiDA7CQRxdkHJG2gw73pxCGkd C2MbQ8CG6DWP4BcBAHNw+l+ydTf7bkUBd8dGofh78j/1tdUUF3YtOOlXDG+CZIq3n3vK WBuPbkbw79N/1IaBkBTqR7y+0CLT/Q7RNIV2KYqNfDNjTBvieJLAwJOUt20n37NANeK8 qRf90iXSUeueewS7PJcn/AQjpX1p145LD0ec9sq8TEFPGSQvlfb45XSX5F/WFbWjMJwR WuRezO/zhOMZ9dFts5Hm6gpwI4EJST/CgniW8pSk43BKRIRC+2Tdk27Cd19f35jtnIiv Eh5g== X-Gm-Message-State: ABy/qLYRqJbuv2YXUYtp57ex+KkPz1ZOOAJvs67797m6oV/2Ty+7UHaa YQp+X/7ff6T+ccwMQ3WJXX41L1zXJHagSsbY7g== X-Google-Smtp-Source: APBJJlFU/3p3XZytGnnz4g0xxHNMZggWlz1mMLZ78y9fLOkMhIqS/5mIom3amLly/wcRCruQ7qwxSVnY/RznFpLozw== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a25:ab90:0:b0:bc4:a660:528f with SMTP id v16-20020a25ab90000000b00bc4a660528fmr143365ybi.5.1689017077178; Mon, 10 Jul 2023 12:24:37 -0700 (PDT) Date: Mon, 10 Jul 2023 19:24:25 +0000 In-Reply-To: <20230710192430.1992246-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230710192430.1992246-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230710192430.1992246-3-jingzhangos@google.com> Subject: [PATCH v5 2/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-20230710_122438_655294_9A7972E9 X-CRM114-Status: GOOD ( 11.01 ) 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 c1a5ec1a016e..0160ef9cfe18 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1980,7 +1980,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), @@ -2029,7 +2029,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 Mon Jul 10 19:24:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13307536 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 8432DC0015E for ; Mon, 10 Jul 2023 19:25:22 +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=0h2GjHkV4C2uNEcoGmKqLe0lwFfv9ZLf0BNDcYFsjKU=; b=RkxfsJvX+FNCEU5xOGZyXfGldI Dy0vC3UDQDucV4szEQazccc6s6c2q8AONaowCjtChpsB8WZNNBOeiD754mZsBJdFq/OqZAZ8LXvIx oB6DEpZf9Wnd0veXJdw9tqYyUQpxOUVfdpxqaud0k2fASb29frCzPMHkK92CewRfWI8IxO7c0a7xT lUASwlZivYNHQPbzifyPM1VpBpvuOgJzM0egjB9kyH5I5aKvky4EjaDTupeOmoQv+yfiFs1jwcnMN OaAPrA/TExZCISOnUcvOTpyIi0d/fqnQHSWsVp5oGwwTTb2gfBqNU9MEwf3WLEaZtlOybo0QgObRu hMr7t9GQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qIwV5-00CYMl-18; Mon, 10 Jul 2023 19:24:47 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qIwUz-00CYIR-2E for linux-arm-kernel@lists.infradead.org; Mon, 10 Jul 2023 19:24:42 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-573d70da2dcso56536547b3.1 for ; Mon, 10 Jul 2023 12:24:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689017079; x=1691609079; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RJ6ya0NEnIa5aVQX1AiBY2Ns/ppVJUg4kBKaquDN4C0=; b=OdWaI8UetC5xlsDbivt/wI1RqtD2Ol3BBLHnxQ9/Cf9Go9SKNCN7pvoug5MOr8pFRE Jr0HJYE6+lJIFXqJiZFwE62NlyzEhmli/QtYehhll8htzke3R17cDFmL4EWRtVDdtxpj v/3b49yRvxDs3842p5I92zZ1Nxbg+u3MD2SrWdYVKspzUMp2ZxinKiQymdYZdCuRoei2 Fav1CzDCizrzfvxke6uiRWaXz9+d3t/p+qH5BHFH/8Hkr4CYv5kH/Gm2210yjr/D/U2A OJ34jvPSX4bqdF8X1d/o/dTD9bTQ9+/d/P8r54rZ+w2wkDB97xWCkJgaW1vJvrm1zxUo Dz9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689017079; x=1691609079; 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=RJ6ya0NEnIa5aVQX1AiBY2Ns/ppVJUg4kBKaquDN4C0=; b=PWzvVrMhAfdIOaHxiCkK9RBwx6wrJGlUyccIqL5zftHXXjgWwf93ET2Za+Jwv4iosl jNaPhqLQ1VSurSsc+OPqgvC/2tWydIp//PD92c4+mIL16CfCF6meKhzfy8RQCNTJrG7N olXXImuOi6+ZQd81wN6DpEyVCHypjgsTspDvhjfxz5yw9f9OgwIr+FA08vHp9iH8QevZ Wl17J29FaZUbLVt/1uL4PpnSvGX4hVXkU1Fnch4WIY9UWIUDnxuA1lwi8p5RQRYD64rv fiw54JP1kykr7PI0J+eihdH4ntoFvZHogZAPaVSIMkJiwDkK9HOG4V/HeyvC34RELDDP tbsQ== X-Gm-Message-State: ABy/qLZjmI/hr7FryR7UtOoIkUgJwv52sp030qyHPxmERK38spoHVD4n 6vpQc2npb4l6E6w3PIvYk6w9W2zefyGJuR2EYA== X-Google-Smtp-Source: APBJJlGrizomlrXrfIIBON5g1LdEHWOUxmR3lblulK7dG7BSaVkBpJJIgLVla3mrwr0NssOyEnvwpwXcUtXHepnUaA== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a25:508:0:b0:c66:b847:544d with SMTP id 8-20020a250508000000b00c66b847544dmr113736ybf.1.1689017079311; Mon, 10 Jul 2023 12:24:39 -0700 (PDT) Date: Mon, 10 Jul 2023 19:24:26 +0000 In-Reply-To: <20230710192430.1992246-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230710192430.1992246-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230710192430.1992246-4-jingzhangos@google.com> Subject: [PATCH v5 3/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-20230710_122441_753223_D41D2279 X-CRM114-Status: GOOD ( 12.78 ) 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 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 0160ef9cfe18..c44504038ae9 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1496,6 +1496,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 +1516,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); } @@ -1536,6 +1544,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 +1560,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 Mon Jul 10 19:24:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13307533 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 4D61AEB64DA for ; Mon, 10 Jul 2023 19:25:18 +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=iPXPMaeSASHcj1b9VeKbFowSz1I3P0cTosKGAwM+pAQ=; b=JMFRdSVpgXUYV1OG4/eCHxHuI2 b+Qc8ZFFYdbKPa7D3o30VZY5SbPkZxbbguxmRilOL1G8/kHYoPRs/XAYx4iDi98wAItr2k1wjyxfv kQRExRkGRVvj+39xypIMoKQaOmPTPEPU84wLTthw9XlIYKR4cTkBMYuTMF7xwbO2A0hShf1VWlTQn mp8/Es64fbODLpDK3YNbHm3WWcIazIavr+gsNVf/+ZfwKsXIoPyVvorP1DEXvHl+Q20BQB1uEZa4R 8RbG8qMMhQatdIxWbCBvvW7DfuMth0s3k4eZk0yckxQ43n/crl0+2SE+pYpQwUYeM7G/vtifXJRbe aaGfv4PA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qIwV6-00CYNH-05; Mon, 10 Jul 2023 19:24:48 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qIwV2-00CYJB-00 for linux-arm-kernel@lists.infradead.org; Mon, 10 Jul 2023 19:24:45 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c361777c7f7so9072721276.0 for ; Mon, 10 Jul 2023 12:24:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689017081; x=1691609081; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Gk6Eirn5+X3nUAq9ec4RcBoQ+OOnriAJ9kIWX3vd7n8=; b=6yhBmxLd54UFd+7UosOkhtC1yZtaJfyjSwqd7QsKub+w3plBkYJTIb2IwzdzmkGe9E zH1XAU69YBWsEi5v7PyiygVcwUsdmGeK59qRWNhKjpzXIKEsOrhG3b5C7sPPVdLLYVUJ lN7g4rJ3TvJ5eSiSmAD1EW4z465s7oSkNbUVgYJV8Hn1luXbmbEOXqHl7Rc1bVoBXFAU cTFv8j1wxQNdGHcs3hr/h610kcTu/4zXd9JE3swftGBnSa2XpF1I1hVY61slterq39+A Ncf4P7nnr5DHH6ishg6E7ywNjAN0/RaiFLUxivulGmCjXmNAEnqwTPBSa0YA/PFJ32bT 0+GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689017081; x=1691609081; 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=Gk6Eirn5+X3nUAq9ec4RcBoQ+OOnriAJ9kIWX3vd7n8=; b=G+SlnjDfQPhWbBQGqqb47NROENMdfcmZjywxfFB71NxO0jwPUHZyl3EnwuKF6uOWno jcc29jY45sDD8XCAX3HDagU+0JeICxNXPhV+nsUs6UFKo60bwT4PQSnjvnz5yGL7/iDH sW38CgruPX5+PxS/Fn+LMJ4dD/rwMP5p3a/Z7/mYunk0PTprDZBKERR0bzZpclbD7fh6 1Zhu5NF8ZI3Rtudy81YY2V/26vPCPtEs9Itt5MBi4aEXQEirC8j1rnv0XfRhcnkSwr2p DHyq9dr0CbM/Nf0BwwkNGylSlpI40EPhV9UJmzqLPukGpvPjSj4txEwGXgGQGXJ1AJb0 vCeg== X-Gm-Message-State: ABy/qLasF5YkteHgj2poEkWdifzzHg+LhnMyU4KQfovOtAL8KQBSCUWS x+C0PXVUp0K18FW4JZwHv9vQKvGKrQatrvMF2A== X-Google-Smtp-Source: APBJJlEyAOiilEAjTAf9LR5RX+k+0UPGMjGR8SkJvffevoDae5RhTEQQuHeKVSmDE6ilWmcPmE/Te8Fp11JLgQ9htQ== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a5b:88:0:b0:c2c:1b68:99b0 with SMTP id b8-20020a5b0088000000b00c2c1b6899b0mr173883ybp.5.1689017081007; Mon, 10 Jul 2023 12:24:41 -0700 (PDT) Date: Mon, 10 Jul 2023 19:24:27 +0000 In-Reply-To: <20230710192430.1992246-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230710192430.1992246-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230710192430.1992246-5-jingzhangos@google.com> Subject: [PATCH v5 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-20230710_122444_037145_DF0FD678 X-CRM114-Status: GOOD ( 11.20 ) 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 c44504038ae9..967ecd57a86a 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -2026,7 +2026,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 Mon Jul 10 19:24:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13307537 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 0B849C001DC for ; Mon, 10 Jul 2023 19:25:23 +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=PJJ4IKeglNWAxzeqV2SfDGVr3QTGN4Pi1HA2Qp8ET90=; b=W8qrFG9VESUXy5GbhDLGsBHc+M ZZIKQEU3n94u6A9q2U4HoDzLLBj4pq0+4GP7cT6XYCHIQnAeQdSbcSgzLu7TF1o/Z1lJ/YJQr6ybo KZd+jM6mxLXaJv6Tf9RofxyTUCfC8FS1eCaxPDioS/cuQHBUJm7n0pHGnb9Tan9R86oZnTSAgtj+c oFgQ9z0oWd1x4VfBQRbOVDDFGDRg3hMZZhgtiIjIT7d4cawQZ9ywta5l5n04PT7kS5AjXu5VAVmHr PYJ/rjvAQQdCL+qzKx2MvPLJKW97+jFwmbbFJExLoJrv4g+kQxH8KYAyHkDhWeMaxCPBvwvnH0HdR 1OPc1fZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qIwV6-00CYNl-2G; Mon, 10 Jul 2023 19:24:48 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qIwV3-00CYKb-07 for linux-arm-kernel@lists.infradead.org; Mon, 10 Jul 2023 19:24:46 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-553d076dce5so5583919a12.3 for ; Mon, 10 Jul 2023 12:24:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689017083; x=1691609083; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=q8Pfm1k8D0yA8LpBE9VtBtVTcRMyZS82/djAneCpkmQ=; b=GDvwqv1MlzXGhRLJnLJVug7YjvSyOR6eGB5341bF6QxHQZRZgkvaDlH66y4otQAjxR 44UbQIWUSOEUnsrC+JRRpIOD794NJBNrvpilQ7LO6keqcvGltAH1FCmFC3SyXkOwYQ/0 crkiuQnUI2yGw0A4pjGjbwtLO3U6I5Dh1NwZzexJa3qLV/JxBB+WCx0PBSJDgoXI72sq LXWeycLBLLeq7lyQ+Kz3VJhHfDhYEvJ/rpKj4QaoZuWnuNfv2i5xlOK+8ONgEsLm1Gze 4kc8pmZHweX9syFCVFkCQQ7hmDa3KevbPWic1vjbctzP5FGA9XSCZpASTHh3DdJ0ZRsF merg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689017083; x=1691609083; 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=q8Pfm1k8D0yA8LpBE9VtBtVTcRMyZS82/djAneCpkmQ=; b=fQq/m98QglOsZVw3zH9F8a0bPjiTCM4AXMaCQ0FkDcxIM/adRkjNhc43ijSbxH5LeX pckgmYt0zacjSPY3Sz2em860ldB7khrUzEcSfgZSgs1jbGGOJD+sxrTYoONLIonE1Wlx OFXwVmCh7hVpGLutSSC7YcWHb3gH06MDH5/aS37CVZolTBNZatCoeo9ERFuYlilRw69Y m/fZjY29yZtL4QroXO2Yxt11CEc99nBgQS53sROcekwKttEf33YxwBeB9u/nMbIxXiv4 JomnDaXWreUrKRx8ee8pXs7jYgEleCe3zZS8X/Q8vBK4X3QBonCjYBex4/VKLzCw1AlR ilXA== X-Gm-Message-State: ABy/qLbBWB/dIQxHkN6BcuHWCdQ7eOJtDTTL6cXbnZy3h1h380A290hd oEoQVnjDMyQdf7YAm+/fXWT9eo5xoP8LOPoIcA== X-Google-Smtp-Source: APBJJlGBCHc1RRKkPANqOkNMUbhIdTsh4hoWApD9cH5zSD49oQpk7M8yIJXyvY9F4ifLaUr+n7yMTQtPbIKIxkg7Jw== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a63:6:0:b0:550:d2d6:525b with SMTP id 6-20020a630006000000b00550d2d6525bmr9180529pga.12.1689017082796; Mon, 10 Jul 2023 12:24:42 -0700 (PDT) Date: Mon, 10 Jul 2023 19:24:28 +0000 In-Reply-To: <20230710192430.1992246-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230710192430.1992246-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230710192430.1992246-6-jingzhangos@google.com> Subject: [PATCH v5 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-20230710_122445_073161_41D9973C X-CRM114-Status: GOOD ( 11.70 ) 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 967ecd57a86a..78ccc95624fa 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1340,9 +1340,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; @@ -1566,6 +1563,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 * @@ -1840,6 +1849,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), \ @@ -2061,10 +2080,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 Mon Jul 10 19:24:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13307538 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 F2F73EB64DA for ; Mon, 10 Jul 2023 19:25:31 +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=TVlP5un3oww/Hfv2NI72lx3iAX whaPFGfPocHEnPLD7Iikz7/oiMlQ/Y9TjWacm45BgQnjJB3mXJSvEmrB2C5yJz+ziCDsWKsivrb7g oCeJKFSbUBoVCPoKi/7fZy4+LaQhha2ycHcbeIj00Kozn5KqZ+kWq4Z6gfTRPAVTa331EvS2FU5zz kvkCoz3nDAQkVjJBuyoZ7S+otBuWX3jfPTxNgh18D/P+rIyQ4Bi+hK7lamyhrNpzzKh1mB7TXUpY3 FrWeOTArj6nHkIrqG65dN6nQzlyIO/xWHthfZUnG5h2foF3TUILpJOtq+C1vOvA9YL9xqHZMAhWnn 1qRaQKcw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qIwVF-00CYS2-0P; Mon, 10 Jul 2023 19:24:57 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qIwV3-00CYLQ-2G for linux-arm-kernel@lists.infradead.org; Mon, 10 Jul 2023 19:24:47 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-55bb5191cd1so5570863a12.2 for ; Mon, 10 Jul 2023 12:24:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689017084; x=1691609084; 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=QaWI7OqJmI9M8bM1aj3NsLqaCx1I5k6gpphm8NxsBbY8FHuucsWWTeL8iN2yV1FXVb TqxFd8gqyRQCX/8ObyZ475vmft1DSuu+wKcaOCTFIvb/qakIqDzlJtZyW4zH8Z48htEB 7q0uleRZWSS1zQOz7FK5QukaTfWoOKy0ERUmO+w5zWUmDsYLLjghn8bjIBqgP68FSuL0 enjGoL11dA8v2Xmb6/fKAZeict/e+bv/j2Cn6S+4nNEj6L56SBJVAvfbZealQEU2DP8z tpZeptlRL8LohFO4CVrHvomh89oBZ6SpNqnO0vcS+ei90fzoPjqvfmwgtiHIDAi06wO2 C4Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689017084; x=1691609084; 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=XBIkx9KBY2U/HhI//Sk8KX5yM187ohWGvImiM2d8WZpkYx8EVmNLK/hFEdwqEsXd+n bCy4F8C0//jy+nuIFei98iIbtZS5mGhhcjzaKoXILguZRTFeZZ0QBRux3djc7Q645nAA Ix7qxIMgabOygZxuOnovgl0H+hUCEueu6ofl2ZeeayNH0+lHP64k8S9VLFYfHqGuNuSC jelLEZkFcZFmhvW+QAFxfrelE7xnyr5epUkFgSDvnJfTLrKCZS+B1HurXhuqGXFtd4Gl NQa587RCZdBmCaZu1M8XxU+rd37cVWKzsiKftnrngGQUsk1z2prrD8GPcr8DKuzO2e8K Gmbg== X-Gm-Message-State: ABy/qLZqGNg86+HE1TrKHwMQO/EftP83w9e5kc2kntmujw0BDHqdGFHD llRaZ85pDMVMiaCD5mheZyKA09xx1KGg6qg0Kw== X-Google-Smtp-Source: APBJJlHVokzEZ9a7PsT83p+FFKlLBIrwwQbGiY4jiVEL9w49sYETLgIQpfTACCighGl5sfBJLgZT02e1kg6SYA4k/Q== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a17:902:d2cd:b0:1b8:929f:1990 with SMTP id n13-20020a170902d2cd00b001b8929f1990mr12756961plc.6.1689017084698; Mon, 10 Jul 2023 12:24:44 -0700 (PDT) Date: Mon, 10 Jul 2023 19:24:29 +0000 In-Reply-To: <20230710192430.1992246-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230710192430.1992246-1-jingzhangos@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230710192430.1992246-7-jingzhangos@google.com> Subject: [PATCH v5 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-20230710_122445_736569_AFE35640 X-CRM114-Status: GOOD ( 17.88 ) 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(); +}