From patchwork Tue Nov 2 09:46:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12598249 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D9CCC433FE for ; Tue, 2 Nov 2021 09:47:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5111A60296 for ; Tue, 2 Nov 2021 09:47:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230458AbhKBJti (ORCPT ); Tue, 2 Nov 2021 05:49:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230140AbhKBJtg (ORCPT ); Tue, 2 Nov 2021 05:49:36 -0400 Received: from mail-io1-xd4a.google.com (mail-io1-xd4a.google.com [IPv6:2607:f8b0:4864:20::d4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DF7FC061714 for ; Tue, 2 Nov 2021 02:47:01 -0700 (PDT) Received: by mail-io1-xd4a.google.com with SMTP id f19-20020a6b6213000000b005ddc4ce4deeso14733076iog.0 for ; Tue, 02 Nov 2021 02:47:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=1y+OLvxpVymtlL7fk66XN2cWX+vD2iLWE/0SkTIcu/U=; b=rzO1Y8VN0zUYuasfFoTvV/5jD+cKSRGlj/YpHnnIRuicH7JJIZ2PnY4j26AX38M33j w4phFGdJUTdXdCnx01ZxlpdHwuiZsF8zMYKIkTwU+Oz5ByP6W796mfarWTsctdFT1yL5 pJlkmLGyfKzIN/D3syWMWYGFIs5YynzA0lsENVBa/R/bjXWIMJgrWc22lyZuS7vFVIUg Ry7VLJisteieRR96iKby/wvJE7vRkjfUTAUGZdOcr7NC6SQDX3vX+NM/RDeG4U1iWDnV FRGpQZQSdtyOqQAjD/sUYy8cu81WON1vWgsJcY7wvINgJ71tv806zzkalWbjsCPfged8 +yUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=1y+OLvxpVymtlL7fk66XN2cWX+vD2iLWE/0SkTIcu/U=; b=1oVD8aCoBXd2YiFLXv+jWsY3lfV+yhxkHOHugae/MQ/VE8qgS6s3v1GWOGXLlB9cEd Uk9dDCzIufsy5AX19vGw9YL9BeCjTKWhAFO2pVaszU8ykhpx0uimkBbvwJKx7SMMsaf9 QKgBCzpjo0UcbSvib1uLmJq/ObqaAEGncWdRcuvFBkQqdDzz8f1V6ppZ0Pi4JdLbFsXH ZXbG/vLqyXmNb6NdbAkcet7Nnef+ifA+uKC0bJ4G4PNHhSmCrxGUPyAS97i4UVA6DmD2 O3P20HAh+US8ugbtelXHQ1QZqW0rPuubr5VK+6xOn1QzIxx2IuNz7YvxhquP6aEsMybM PKkA== X-Gm-Message-State: AOAM530iqr61RkkFrPj5F/Uf4fYZkXAzEyi3ALeryvqfUgDAlnDoJwxU mkKxnUwfo2fxoWuxT8FKPJsYB/A/Yb8= X-Google-Smtp-Source: ABdhPJyayLrRnzjr+V0pA0yQ0Z4XEZRHuv5UbfBHmBUD+1GNwyAnZEMt4nk99gXUFzj3J+ZkE8WGDda50ZA= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6602:148b:: with SMTP id a11mr26232858iow.85.1635846420975; Tue, 02 Nov 2021 02:47:00 -0700 (PDT) Date: Tue, 2 Nov 2021 09:46:46 +0000 In-Reply-To: <20211102094651.2071532-1-oupton@google.com> Message-Id: <20211102094651.2071532-2-oupton@google.com> Mime-Version: 1.0 References: <20211102094651.2071532-1-oupton@google.com> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog Subject: [PATCH v2 1/6] KVM: arm64: Correctly treat writes to OSLSR_EL1 as undefined From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Peter Shier , Ricardo Koller , Reiji Watanabe , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Any valid implementation of the architecture should generate an undefined exception for writes to a read-only register, such as OSLSR_EL1. Nonetheless, the KVM handler actually implements write-ignore behavior. Align the trap handler for OSLSR_EL1 with hardware behavior. If such a write ever traps to EL2, inject an undef into the guest and print a warning. Signed-off-by: Oliver Upton Reviewed-by: Reiji Watanabe --- 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 1d46e185f31e..17fa6ddf5405 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -292,7 +292,7 @@ static bool trap_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) { if (p->is_write) { - return ignore_write(vcpu, p); + return write_to_read_only(vcpu, p, r); } else { p->regval = (1 << 3); return true; From patchwork Tue Nov 2 09:46:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12598251 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39239C4332F for ; Tue, 2 Nov 2021 09:47:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 23A3761053 for ; Tue, 2 Nov 2021 09:47:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231135AbhKBJtj (ORCPT ); Tue, 2 Nov 2021 05:49:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230353AbhKBJtg (ORCPT ); Tue, 2 Nov 2021 05:49:36 -0400 Received: from mail-io1-xd4a.google.com (mail-io1-xd4a.google.com [IPv6:2607:f8b0:4864:20::d4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55110C061764 for ; Tue, 2 Nov 2021 02:47:02 -0700 (PDT) Received: by mail-io1-xd4a.google.com with SMTP id d19-20020a0566022d5300b005e178955ce3so8324858iow.18 for ; Tue, 02 Nov 2021 02:47:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=DbmokXu45N05i00rjRKFS9Aukc67vZn4OjwqX1uC/YU=; b=EdDi9FOXMCGHRhU016qAQn/ZmuBFvVQyBV401phw9A0mM29D7FCBkKrqoRN+ddaDKu WMNSZyuCDNWevO/SP5n9ML9GzsOq3KJVi026ObUqHeQqAO6uvqeRwYkUUMyOGQWHiYA2 tnvJWSLvjtmZY8JFr6NQakWEW4/5U9vJZL8Lqm47kV26cjyA2ANUqH8OMBybG6CG908N x0okPVWvlaqCs8jLleOKU+LoC7nP19scolOl5HFZ8y+ukdy5eVk5p2toXh3eLSrG2BBJ +fwB5JpZB8U6m0jYd/lt9RgHDohbN545YLjTCvSuLn+ccimIQxFvgF0GnE25PMzP3z+b oMng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=DbmokXu45N05i00rjRKFS9Aukc67vZn4OjwqX1uC/YU=; b=Txepl2HFkKatL+k7yQ3S27hDUi+y+YfWKUKT4NdEU8w+x8z9XaBkKAPc8xolD3wL1z Z2yOhSpPS0TDHmkiRsBagFer5TW+eY11s2fFMMb1dc279j36YgxdfA2+Yd1DXszbgRpg wmInTS2n0Mo4Gt3A/mYcO326gFtR6JMJYb30yJteRjeKZRcJesZkR4avFbEaX95hCehu DSSiYDdEgjbTVGdMC/GTQD1badzZ3IWyKxLuICHqig1rV0o5EDhyql8qATQJ8vOIuCXf 2bqcKOCw7J2jJJFh0x4gDKZquv0msecaxqGBcyfbUBNDvP3XADQq17zR3Wui0ZKJ3WnF 2eyg== X-Gm-Message-State: AOAM5306QsElZW9sSsiObAldtGgC6q9khWSmAARn5kntfU8d3gJpQicy wiTx2L6ILeatvAYEnKBZEO13hDl9X0M= X-Google-Smtp-Source: ABdhPJyDtijBd6vvihLRehY/zlMTHVriiJWyurunjgvekkIvbB0W9BlpBBhTD9Sttnoy8lUPFahbqXpiX6c= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a02:a884:: with SMTP id l4mr26257855jam.44.1635846421777; Tue, 02 Nov 2021 02:47:01 -0700 (PDT) Date: Tue, 2 Nov 2021 09:46:47 +0000 In-Reply-To: <20211102094651.2071532-1-oupton@google.com> Message-Id: <20211102094651.2071532-3-oupton@google.com> Mime-Version: 1.0 References: <20211102094651.2071532-1-oupton@google.com> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog Subject: [PATCH v2 2/6] KVM: arm64: Stash OSLSR_EL1 in the cpu context From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Peter Shier , Ricardo Koller , Reiji Watanabe , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org An upcoming change to KVM will context switch the OS Lock status between guest/host. Add OSLSR_EL1 to the cpu context and handle guest reads using the stored value. Wire up a custom handler for writes from userspace and prevent any of the invariant bits from changing. Signed-off-by: Oliver Upton Reviewed-by: Reiji Watanabe --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/sys_regs.c | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index f8be56d5342b..c98f65c4a1f7 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -172,6 +172,7 @@ enum vcpu_sysreg { MDSCR_EL1, /* Monitor Debug System Control Register */ MDCCINT_EL1, /* Monitor Debug Comms Channel Interrupt Enable Reg */ DISR_EL1, /* Deferred Interrupt Status Register */ + OSLSR_EL1, /* OS Lock Status Register */ /* Performance Monitors Registers */ PMCR_EL0, /* Control Register */ diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 17fa6ddf5405..0326b3df0736 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -291,12 +291,28 @@ static bool trap_oslsr_el1(struct kvm_vcpu *vcpu, struct sys_reg_params *p, const struct sys_reg_desc *r) { - if (p->is_write) { + if (p->is_write) return write_to_read_only(vcpu, p, r); - } else { - p->regval = (1 << 3); - return true; - } + + p->regval = __vcpu_sys_reg(vcpu, r->reg); + return true; +} + +static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, + const struct kvm_one_reg *reg, void __user *uaddr) +{ + u64 id = sys_reg_to_index(rd); + u64 val; + int err; + + err = reg_from_user(&val, uaddr, id); + if (err) + return err; + + if (val != rd->val) + return -EINVAL; + + return 0; } static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu, @@ -1441,7 +1457,8 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_MDRAR_EL1), trap_raz_wi }, { SYS_DESC(SYS_OSLAR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1 }, + { SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1, reset_val, OSLSR_EL1, 0x00000008, + .set_user = set_oslsr_el1, }, { SYS_DESC(SYS_OSDLR_EL1), trap_raz_wi }, { SYS_DESC(SYS_DBGPRCR_EL1), trap_raz_wi }, { SYS_DESC(SYS_DBGCLAIMSET_EL1), trap_raz_wi }, @@ -1916,7 +1933,7 @@ static const struct sys_reg_desc cp14_regs[] = { { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi }, DBGBXVR(1), /* DBGOSLSR */ - { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1 }, + { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1, NULL, OSLSR_EL1 }, DBGBXVR(2), DBGBXVR(3), /* DBGOSDLR */ From patchwork Tue Nov 2 09:46:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12598253 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 68A48C43219 for ; Tue, 2 Nov 2021 09:47:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5185660249 for ; Tue, 2 Nov 2021 09:47:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231168AbhKBJtk (ORCPT ); Tue, 2 Nov 2021 05:49:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229505AbhKBJth (ORCPT ); Tue, 2 Nov 2021 05:49:37 -0400 Received: from mail-io1-xd4a.google.com (mail-io1-xd4a.google.com [IPv6:2607:f8b0:4864:20::d4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ADF7C061714 for ; Tue, 2 Nov 2021 02:47:03 -0700 (PDT) Received: by mail-io1-xd4a.google.com with SMTP id f8-20020a05660215c800b005e166630a3dso10054570iow.15 for ; Tue, 02 Nov 2021 02:47:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=LW62NaqnzOIPyMSkIbkMT7S/DeS5QsuklZa8PoVQ7Ts=; b=R/UakULhkBCZbgzgewMj4LYGsljk3XONwmfkNrEjAIjteagmlKOo4UF2SA7WdVeTSl 0Ny1s3tjQ10f2WBLLhD1ataKqHZkMAkCOZMUssW6Opwyk1jMZvw5R19QIAoNJjuKNnKw 42oDFamwIXC/r7dOUEcdglXAseWkrKE16BTwsnnM1Vsrp38xTrwIhKXo+On/29TCtH0o lrpyIewcC3C3OAYPDk8xI5TzCTVHl6pdHu/t+g+3OzK5/F/+vSUZNK0voHyXiQEGY0P3 0EdJPxNWVIvmk8u9BQbWpnEskUwkfrRXvhZ6QBOQJJDNsJYcxZ6Vw3i65S12CDqx+hta 9QKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=LW62NaqnzOIPyMSkIbkMT7S/DeS5QsuklZa8PoVQ7Ts=; b=ta9Ey2C4NNtFJLYxoWngi+kQSxiV41y02WUQgqY5ksQByzPHoF006XoKrQrxGRFnOy CWpT3V+30A17KSqSyTUvHP+G2HczaubQbLTlXErCDuAbXwd0b3ygwVJUtjN2ymc1Y8/s pHWKy/nX+Iy18XtEed/dsV1yu75EFRFax6oZGFApHAVQUtzuy3bssPPcUlptJrovA+Gw Cy9/qvaUUSAuOeZiJJbH0eH4f39bODqvNqSoPDL1seTwAkMdInxMgGKXrqQjAmh/JpyC ak/13RZQJ/91/l8EzHFPENPp2849FZmHfIAxhL8ZGOQL6B8em6NRuLTtFY7OCj3SxH8v UWjw== X-Gm-Message-State: AOAM533hqbEQLrV+MvUflYk+CIjryqQA8cSyuabuxmPu8/kKR9CVr2Pt zOkJc7H3iC8NfZvw0BcOh368eQFhE44= X-Google-Smtp-Source: ABdhPJyAAd3EZ7PG9zgAtlS9xNQ7IEiF/KnBkc8peoyeSw7zPj/Pr+fPOvN0382WKxo0+yGxzbd0/Ii9uzE= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a6b:650f:: with SMTP id z15mr19435494iob.27.1635846422770; Tue, 02 Nov 2021 02:47:02 -0700 (PDT) Date: Tue, 2 Nov 2021 09:46:48 +0000 In-Reply-To: <20211102094651.2071532-1-oupton@google.com> Message-Id: <20211102094651.2071532-4-oupton@google.com> Mime-Version: 1.0 References: <20211102094651.2071532-1-oupton@google.com> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog Subject: [PATCH v2 3/6] KVM: arm64: Allow guest to set the OSLK bit From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Peter Shier , Ricardo Koller , Reiji Watanabe , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Allow writes to OSLAR and forward the OSLK bit to OSLSR. Change the reset value of the OSLK bit to 1. Allow the value to be migrated by making OSLSR_EL1.OSLK writable from userspace. Signed-off-by: Oliver Upton Reviewed-by: Reiji Watanabe --- arch/arm64/include/asm/sysreg.h | 6 ++++++ arch/arm64/kvm/sys_regs.c | 35 +++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h index b268082d67ed..6ba4dc97b69d 100644 --- a/arch/arm64/include/asm/sysreg.h +++ b/arch/arm64/include/asm/sysreg.h @@ -127,7 +127,13 @@ #define SYS_DBGWCRn_EL1(n) sys_reg(2, 0, 0, n, 7) #define SYS_MDRAR_EL1 sys_reg(2, 0, 1, 0, 0) #define SYS_OSLAR_EL1 sys_reg(2, 0, 1, 0, 4) + +#define SYS_OSLAR_OSLK BIT(0) + #define SYS_OSLSR_EL1 sys_reg(2, 0, 1, 1, 4) + +#define SYS_OSLSR_OSLK BIT(1) + #define SYS_OSDLR_EL1 sys_reg(2, 0, 1, 3, 4) #define SYS_DBGPRCR_EL1 sys_reg(2, 0, 1, 4, 4) #define SYS_DBGCLAIMSET_EL1 sys_reg(2, 0, 7, 8, 6) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 0326b3df0736..acd8aa2e5a44 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -44,6 +44,10 @@ * 64bit interface. */ +static int reg_from_user(u64 *val, const void __user *uaddr, u64 id); +static int reg_to_user(void __user *uaddr, const u64 *val, u64 id); +static u64 sys_reg_to_index(const struct sys_reg_desc *reg); + static bool read_from_write_only(struct kvm_vcpu *vcpu, struct sys_reg_params *params, const struct sys_reg_desc *r) @@ -287,6 +291,24 @@ static bool trap_loregion(struct kvm_vcpu *vcpu, return trap_raz_wi(vcpu, p, r); } +static bool trap_oslar_el1(struct kvm_vcpu *vcpu, + struct sys_reg_params *p, + const struct sys_reg_desc *r) +{ + u64 oslsr; + + if (!p->is_write) + return read_from_write_only(vcpu, p, r); + + /* Forward the OSLK bit to OSLSR */ + oslsr = __vcpu_sys_reg(vcpu, OSLSR_EL1) & ~SYS_OSLSR_OSLK; + if (p->regval & SYS_OSLAR_OSLK) + oslsr |= SYS_OSLSR_OSLK; + + __vcpu_sys_reg(vcpu, OSLSR_EL1) = oslsr; + return true; +} + static bool trap_oslsr_el1(struct kvm_vcpu *vcpu, struct sys_reg_params *p, const struct sys_reg_desc *r) @@ -309,9 +331,10 @@ static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, if (err) return err; - if (val != rd->val) + if ((val | SYS_OSLSR_OSLK) != rd->val) return -EINVAL; + __vcpu_sys_reg(vcpu, rd->reg) = val; return 0; } @@ -1176,10 +1199,6 @@ static bool access_raz_id_reg(struct kvm_vcpu *vcpu, return __access_id_reg(vcpu, p, r, true); } -static int reg_from_user(u64 *val, const void __user *uaddr, u64 id); -static int reg_to_user(void __user *uaddr, const u64 *val, u64 id); -static u64 sys_reg_to_index(const struct sys_reg_desc *reg); - /* Visibility overrides for SVE-specific control registers */ static unsigned int sve_visibility(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd) @@ -1456,8 +1475,8 @@ static const struct sys_reg_desc sys_reg_descs[] = { DBG_BCR_BVR_WCR_WVR_EL1(15), { SYS_DESC(SYS_MDRAR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_OSLAR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1, reset_val, OSLSR_EL1, 0x00000008, + { SYS_DESC(SYS_OSLAR_EL1), trap_oslar_el1 }, + { SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1, reset_val, OSLSR_EL1, 0x0000000A, .set_user = set_oslsr_el1, }, { SYS_DESC(SYS_OSDLR_EL1), trap_raz_wi }, { SYS_DESC(SYS_DBGPRCR_EL1), trap_raz_wi }, @@ -1930,7 +1949,7 @@ static const struct sys_reg_desc cp14_regs[] = { DBGBXVR(0), /* DBGOSLAR */ - { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi }, + { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_oslar_el1 }, DBGBXVR(1), /* DBGOSLSR */ { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1, NULL, OSLSR_EL1 }, From patchwork Tue Nov 2 09:46:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12598255 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7395C43217 for ; Tue, 2 Nov 2021 09:47:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B2C226054E for ; Tue, 2 Nov 2021 09:47:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231186AbhKBJtl (ORCPT ); Tue, 2 Nov 2021 05:49:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230497AbhKBJti (ORCPT ); Tue, 2 Nov 2021 05:49:38 -0400 Received: from mail-io1-xd49.google.com (mail-io1-xd49.google.com [IPv6:2607:f8b0:4864:20::d49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 596E2C061714 for ; Tue, 2 Nov 2021 02:47:04 -0700 (PDT) Received: by mail-io1-xd49.google.com with SMTP id x15-20020a056602160f00b005e1888842d5so6210655iow.14 for ; Tue, 02 Nov 2021 02:47:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=uurkhpIbGOm6d0Vndzx4M8suePh/mfJb26pfAeTQQ2Q=; b=hfG5wU8dCHVF7dzzRdCErkdSpbwwJpZuXaQLMBh7ivg7UkdR7Q2EXzLbnT41pop135 XCHRgVpXBLovwNMqy9DIRMmWmVsZ28tIM6cX3gNxKkrPynPaKlZhQ+8khbqNpw+EoGno 9oGJnKnGefLQm0tEsiIyE3j3FutcPJhp1HNLgqHfFbcSrz1R0C1IWb9eW4kQmZzvK/C0 PltELiax0LrjH9/UNYt+8l1Bs+UIeitFUh56258VomoZ8MR78MzMdJftxtD5GCXcXdhT cEH0Ki7u6Q2zMBKCNJchGI/GIhfrFO+/bowakv7b8AkGnKTiY1XXeU4m796IUaMR/Zcl cYdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=uurkhpIbGOm6d0Vndzx4M8suePh/mfJb26pfAeTQQ2Q=; b=KY+TPjdGhhuW1T4wCiXFZtx2X4rkwzdy6aYPw6+8oB7xSQlSZxQ+RqelmV0Iz0DaNf p4/qfeZm/49taLR+fRYcfoybPgguY4L/l95+NFhHsy5yZlwddh51pc2ucQJxWgsnXb/A rI5sOLAFoIKVPt/epBwWHUdWU+2zef3qFgupncBSxa2tfjOz4IiuVHSCihi1XnR/8uPm GO/FUbiXK8xNxXWbkAgNtqoizfurk7doqdJkfjQv3IB23z19agZ3izXDeZVNdh7C9nN8 bJAsNVl/PE3xBHXLtsuDzfhmeqxy5EQD3xy2sv8UtaOx68E9lNjRrEMzpYtmU6bCJd64 HiWA== X-Gm-Message-State: AOAM533NP2nRVApkHJkWiugtnG0rRO4gqZQtF8xYkRJAg5xkks4MY+LH DllchVtjeGj+583paIdgpCKmhDWcBT0= X-Google-Smtp-Source: ABdhPJzdE2f0jbKc/HrPBSVM0QUl30mxLU3ZJlUaX+L3AkUnjf0ehb/ooOshHnhNm9o1UfgzNO0sexgE6jQ= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6e02:174d:: with SMTP id y13mr24331087ill.251.1635846423807; Tue, 02 Nov 2021 02:47:03 -0700 (PDT) Date: Tue, 2 Nov 2021 09:46:49 +0000 In-Reply-To: <20211102094651.2071532-1-oupton@google.com> Message-Id: <20211102094651.2071532-5-oupton@google.com> Mime-Version: 1.0 References: <20211102094651.2071532-1-oupton@google.com> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog Subject: [PATCH v2 4/6] KVM: arm64: Emulate the OS Lock From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Peter Shier , Ricardo Koller , Reiji Watanabe , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The OS lock blocks all debug exceptions at every EL. To date, KVM has not implemented the OS lock for its guests, despite the fact that it is mandatory per the architecture. Simple context switching between the guest and host is not appropriate, as its effects are not constrained to the guest context. Emulate the OS Lock by clearing MDE and SS in MDSCR_EL1, thereby blocking all but software breakpoint instructions. To handle breakpoint instructions, trap debug exceptions to EL2 and skip the instruction. Signed-off-by: Oliver Upton --- arch/arm64/include/asm/kvm_host.h | 4 ++++ arch/arm64/kvm/debug.c | 20 +++++++++++++++----- arch/arm64/kvm/handle_exit.c | 8 ++++++++ arch/arm64/kvm/sys_regs.c | 6 +++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index c98f65c4a1f7..f13b8b79b06d 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -724,6 +724,10 @@ void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu); void kvm_arm_setup_debug(struct kvm_vcpu *vcpu); void kvm_arm_clear_debug(struct kvm_vcpu *vcpu); void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu); + +#define kvm_vcpu_os_lock_enabled(vcpu) \ + (__vcpu_sys_reg(vcpu, OSLSR_EL1) & SYS_OSLSR_OSLK) + int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index db9361338b2a..5690a9c99c89 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -95,8 +95,11 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu) MDCR_EL2_TDRA | MDCR_EL2_TDOSA); - /* Is the VM being debugged by userspace? */ - if (vcpu->guest_debug) + /* + * Check if the VM is being debugged by userspace or the guest has + * enabled the OS lock. + */ + if (vcpu->guest_debug || kvm_vcpu_os_lock_enabled(vcpu)) /* Route all software debug exceptions to EL2 */ vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE; @@ -160,8 +163,11 @@ void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) kvm_arm_setup_mdcr_el2(vcpu); - /* Is Guest debugging in effect? */ - if (vcpu->guest_debug) { + /* + * Check if the guest is being debugged or if the guest has enabled the + * OS lock. + */ + if (vcpu->guest_debug || kvm_vcpu_os_lock_enabled(vcpu)) { /* Save guest debug state */ save_guest_debug_regs(vcpu); @@ -223,6 +229,10 @@ void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) trace_kvm_arm_set_regset("WAPTS", get_num_wrps(), &vcpu->arch.debug_ptr->dbg_wcr[0], &vcpu->arch.debug_ptr->dbg_wvr[0]); + } else if (kvm_vcpu_os_lock_enabled(vcpu)) { + mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); + mdscr &= ~DBG_MDSCR_MDE; + vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); } } @@ -244,7 +254,7 @@ void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) { trace_kvm_arm_clear_debug(vcpu->guest_debug); - if (vcpu->guest_debug) { + if (vcpu->guest_debug || kvm_vcpu_os_lock_enabled(vcpu)) { restore_guest_debug_regs(vcpu); /* diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 275a27368a04..a7136888434d 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -119,6 +119,14 @@ static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu) { struct kvm_run *run = vcpu->run; u32 esr = kvm_vcpu_get_esr(vcpu); + u8 esr_ec = ESR_ELx_EC(esr); + + if (!vcpu->guest_debug) { + WARN_ONCE(esr_ec != ESR_ELx_EC_BRK64 || esr_ec != ESR_ELx_EC_BKPT32, + "Unexpected debug exception\n"); + kvm_incr_pc(vcpu); + return 1; + } run->exit_reason = KVM_EXIT_DEBUG; run->debug.arch.hsr = esr; diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index acd8aa2e5a44..d336e4c66870 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1446,9 +1446,9 @@ static unsigned int mte_visibility(const struct kvm_vcpu *vcpu, * Debug handling: We do trap most, if not all debug related system * registers. The implementation is good enough to ensure that a guest * can use these with minimal performance degradation. The drawback is - * that we don't implement any of the external debug, none of the - * OSlock protocol. This should be revisited if we ever encounter a - * more demanding guest... + * that we don't implement any of the external debug architecture. + * This should be revisited if we ever encounter a more demanding + * guest... */ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_DC_ISW), access_dcsw }, From patchwork Tue Nov 2 09:46:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12598257 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CFF5BC433F5 for ; Tue, 2 Nov 2021 09:47:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AD25B60C4B for ; Tue, 2 Nov 2021 09:47:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231265AbhKBJto (ORCPT ); Tue, 2 Nov 2021 05:49:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230353AbhKBJtk (ORCPT ); Tue, 2 Nov 2021 05:49:40 -0400 Received: from mail-ot1-x349.google.com (mail-ot1-x349.google.com [IPv6:2607:f8b0:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74DB0C061714 for ; Tue, 2 Nov 2021 02:47:05 -0700 (PDT) Received: by mail-ot1-x349.google.com with SMTP id c22-20020a0568301af600b00553c94299b0so10746090otd.20 for ; Tue, 02 Nov 2021 02:47:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=nEjtp48gVyB++aYe5TOFGmHYLzUAbR8AMXiOP8caS+Q=; b=PH5s6nRr9C9ryg6R7K3KVE7lekvhJ4vWen1srtKgyckfyIL506uPsp1thUwsDGBp2I s0rtpTcgH5+RkIAsgGui4xxyoFy2FcKhuqD+zlL2WiqoCNePlOfIDPzVfVMzLVNidPjE kDEqKvU6sNqdwfuqSVPXBVcUcyFM0qCKpCDbvWbSDAhF1onWqasK3EH8UcK/mYSHPksz mu2apwuLuFAzZv2kxhvJanRAtOYb3H5MRH72mlIaj7J5/KI9xqRFZFs16+BhvHOxddyt 171ryqXBNwgM1szDlWottlMAZQwysc78EQeNEGivOGb68Tzn2pW9Yt4pXEWl5Giqs3mz GENA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=nEjtp48gVyB++aYe5TOFGmHYLzUAbR8AMXiOP8caS+Q=; b=NEHanUVye2I6/PtefXC0araoDclOj+ZBhF7rZABpjwRGN8F2J8ppuZSmB0RtmHYgj4 S+S2fgr6TlITgt/ko7LJ7SB/ZBMQci5gkeXdkXPhLrHWJbR75997CMFyELGl8O58Ttnf ooJtKV9BcZirJXMiMuMxBwmIvEBOGfqhlISLjB7JPUnqT09DF7ssLIzMRTnkBuOY/9FL ZddPL9puEYcYrtU1n/SGaQNEWXeLu7XeM6Ie0rw4B/SrpjFlhy3SGR9t/mIsN7vmOSKa a5KYSbXdmTd5wGK/hcKBmUvvTrCHsG56j4BNALcGNAOrlcQFVZxm46SRoosE9ONTdnXF NMzA== X-Gm-Message-State: AOAM532t4sNGJ45aCz2ISaRI70VvWJdYfCa5bQsTAuuQUHjazXfp2vLA 1Sa0OkKpjvbWvdLVAax2BTYoiA9nAgU= X-Google-Smtp-Source: ABdhPJxr747gitpVlJ/b+QoTgbMhfsQFRzNX4vE8pGYUrpL6y/peDJCUncRK+fGW//73XuPwhgpbn5iWeqM= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6830:1ace:: with SMTP id r14mr25706515otc.232.1635846424779; Tue, 02 Nov 2021 02:47:04 -0700 (PDT) Date: Tue, 2 Nov 2021 09:46:50 +0000 In-Reply-To: <20211102094651.2071532-1-oupton@google.com> Message-Id: <20211102094651.2071532-6-oupton@google.com> Mime-Version: 1.0 References: <20211102094651.2071532-1-oupton@google.com> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog Subject: [PATCH v2 5/6] selftests: KVM: Add OSLSR_EL1 to the list of blessed regs From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Peter Shier , Ricardo Koller , Reiji Watanabe , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org OSLSR_EL1 is now part of the visible system register state. Add it to the get-reg-list selftest to ensure we keep it that way. Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/aarch64/get-reg-list.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/kvm/aarch64/get-reg-list.c b/tools/testing/selftests/kvm/aarch64/get-reg-list.c index cc898181faab..0c7c39a16b3f 100644 --- a/tools/testing/selftests/kvm/aarch64/get-reg-list.c +++ b/tools/testing/selftests/kvm/aarch64/get-reg-list.c @@ -761,6 +761,7 @@ static __u64 base_regs[] = { ARM64_SYS_REG(2, 0, 0, 15, 6), ARM64_SYS_REG(2, 0, 0, 15, 7), ARM64_SYS_REG(2, 4, 0, 7, 0), /* DBGVCR32_EL2 */ + ARM64_SYS_REG(2, 0, 1, 1, 4), /* OSLSR_EL1 */ ARM64_SYS_REG(3, 0, 0, 0, 5), /* MPIDR_EL1 */ ARM64_SYS_REG(3, 0, 0, 1, 0), /* ID_PFR0_EL1 */ ARM64_SYS_REG(3, 0, 0, 1, 1), /* ID_PFR1_EL1 */ From patchwork Tue Nov 2 09:46:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12598259 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42764C433EF for ; Tue, 2 Nov 2021 09:47:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29D5E60273 for ; Tue, 2 Nov 2021 09:47:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231284AbhKBJtr (ORCPT ); Tue, 2 Nov 2021 05:49:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231181AbhKBJtk (ORCPT ); Tue, 2 Nov 2021 05:49:40 -0400 Received: from mail-il1-x14a.google.com (mail-il1-x14a.google.com [IPv6:2607:f8b0:4864:20::14a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69E36C061764 for ; Tue, 2 Nov 2021 02:47:06 -0700 (PDT) Received: by mail-il1-x14a.google.com with SMTP id h14-20020a056e021d8e00b002691dcecdbaso4200513ila.23 for ; Tue, 02 Nov 2021 02:47:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=USRcUlx130z7l5DHjEbTqn2FW1uy9zzoAmm3OXXaQBk=; b=PQLfk3CFu1uKl92twaW6wnU35UbAHgLOFn7zYoU4COpmRQifsFd/zeIm5FCi0v4o0x /v+0h1A/ULEUdztocwTSe0oiqESiYPZ/hpeXLA64lpfwIpqqp3XUJM4CuRE952oFQWKu fLsSf++kgTReC9IHoLJuRBY8v8C14jwONDDLb5Mrm+gyaSe1TgfI6eHqcwEEOTofSHTN wS8Q0W7bHGRZ1baATKX5JSPRjxdR+oHD9mhS6xgGZ/jA4t+A0uFxy4S4x4p3lceEFZuM UJ1ALejVWJAhv1+rtYQZldacua675hfSzOOsdxq9odNYyfkD74lpUoSqCivx93Z0wd+e JySg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=USRcUlx130z7l5DHjEbTqn2FW1uy9zzoAmm3OXXaQBk=; b=whewEHRC7iezsPb3Saq9zHO2jDK/p1GkmRVYkwHlC2uFmOCSXGmtiMfXUhGMGPw9Gb thgFUDJMUYsScrDc3hyTR59kp2Xf8JFcnB8R2JgBCHypD2H+9bESRW3M/5PpXjKahQOt pobuVDx0WkGqyUaUqLRAMrImDf2GR0rOhdvpO/pKUNdPEwckyKBwrEXOdmuKAOWTnV6s ReZTPLTQl6JB8QHFx754LoyFVLqIpZqruz8L9gSTrpX4DtMfSZz2tYD77384ENXZ8sxn +aLWq/TgKc4YcCpPJrxuBO7edvwJA+Re/nnf0NGbqvT+FryIGc+fSoNUYRyjxl1KEIPs y6NA== X-Gm-Message-State: AOAM530PzHedF9LAy/HkcGeDxS3haoEoNtRaGhWDE7IqUQZNSpWLYonZ zQzkJAra/4kbRsjH/Buzg5ftmJM2ZPM= X-Google-Smtp-Source: ABdhPJy6wt5RMowO8zj/XK0tDxqF5oCDvoTx8hC/w3ofvmgfuVtqYfu0icCviFdBDTxdv6AyYpCRfHOVcWc= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6638:1502:: with SMTP id b2mr27109379jat.131.1635846425861; Tue, 02 Nov 2021 02:47:05 -0700 (PDT) Date: Tue, 2 Nov 2021 09:46:51 +0000 In-Reply-To: <20211102094651.2071532-1-oupton@google.com> Message-Id: <20211102094651.2071532-7-oupton@google.com> Mime-Version: 1.0 References: <20211102094651.2071532-1-oupton@google.com> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog Subject: [PATCH v2 6/6] selftests: KVM: Test OS lock behavior From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Peter Shier , Ricardo Koller , Reiji Watanabe , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM now correctly handles the OS Lock for its guests. When set, KVM blocks all debug exceptions originating from the guest. Add test cases to the debug-exceptions test to assert that software breakpoint, hardware breakpoint, watchpoint, and single-step exceptions are in fact blocked. Signed-off-by: Oliver Upton --- .../selftests/kvm/aarch64/debug-exceptions.c | 58 ++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/aarch64/debug-exceptions.c b/tools/testing/selftests/kvm/aarch64/debug-exceptions.c index e5e6c92b60da..6b6ff81cdd23 100644 --- a/tools/testing/selftests/kvm/aarch64/debug-exceptions.c +++ b/tools/testing/selftests/kvm/aarch64/debug-exceptions.c @@ -23,7 +23,7 @@ #define SPSR_D (1 << 9) #define SPSR_SS (1 << 21) -extern unsigned char sw_bp, hw_bp, bp_svc, bp_brk, hw_wp, ss_start; +extern unsigned char sw_bp, hw_bp, hw_bp2, bp_svc, bp_brk, hw_wp, ss_start; static volatile uint64_t sw_bp_addr, hw_bp_addr; static volatile uint64_t wp_addr, wp_data_addr; static volatile uint64_t svc_addr; @@ -47,6 +47,14 @@ static void reset_debug_state(void) isb(); } +static void enable_os_lock(void) +{ + write_sysreg(oslar_el1, 1); + isb(); + + GUEST_ASSERT(read_sysreg(oslsr_el1) & 2); +} + static void install_wp(uint64_t addr) { uint32_t wcr; @@ -99,6 +107,7 @@ static void guest_code(void) GUEST_SYNC(0); /* Software-breakpoint */ + reset_debug_state(); asm volatile("sw_bp: brk #0"); GUEST_ASSERT_EQ(sw_bp_addr, PC(sw_bp)); @@ -152,6 +161,51 @@ static void guest_code(void) GUEST_ASSERT_EQ(ss_addr[1], PC(ss_start) + 4); GUEST_ASSERT_EQ(ss_addr[2], PC(ss_start) + 8); + GUEST_SYNC(6); + + /* OS Lock blocking software-breakpoint */ + reset_debug_state(); + enable_os_lock(); + sw_bp_addr = 0; + asm volatile("brk #0"); + GUEST_ASSERT_EQ(sw_bp_addr, 0); + + GUEST_SYNC(7); + + /* OS Lock blocking hardware-breakpoint */ + reset_debug_state(); + enable_os_lock(); + install_hw_bp(PC(hw_bp2)); + hw_bp_addr = 0; + asm volatile("hw_bp2: nop"); + GUEST_ASSERT_EQ(hw_bp_addr, 0); + + GUEST_SYNC(8); + + /* OS Lock blocking watchpoint */ + reset_debug_state(); + enable_os_lock(); + write_data = '\0'; + wp_data_addr = 0; + install_wp(PC(write_data)); + write_data = 'x'; + GUEST_ASSERT_EQ(write_data, 'x'); + GUEST_ASSERT_EQ(wp_data_addr, 0); + + GUEST_SYNC(9); + + /* OS Lock blocking single-step */ + reset_debug_state(); + enable_os_lock(); + ss_addr[0] = 0; + install_ss(); + ss_idx = 0; + asm volatile("mrs x0, esr_el1\n\t" + "add x0, x0, #1\n\t" + "msr daifset, #8\n\t" + : : : "x0"); + GUEST_ASSERT_EQ(ss_addr[0], 0); + GUEST_DONE(); } @@ -223,7 +277,7 @@ int main(int argc, char *argv[]) vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT, ESR_EC_SVC64, guest_svc_handler); - for (stage = 0; stage < 7; stage++) { + for (stage = 0; stage < 11; stage++) { vcpu_run(vm, VCPU_ID); switch (get_ucall(vm, VCPU_ID, &uc)) {