From patchwork Sat Sep 17 01:05:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 12978939 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 D7908ECAAA1 for ; Sat, 17 Sep 2022 01:07:57 +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=N0KFniVu+JbVIsSATZLfQwlaA/l1XFGHS2SOEJNegig=; b=FX6qye/ob+DvlE2IA2np746TOq aDmYMqYnOw84IU31E6tHLuEhIrlx44ox8VeQsDiVN/tlhtrmpwBAOkAe0cYBhGPWwtLrNdOWEHlZa dHUumwuY8/v4TtQQt/Yqf6CD7vmqZ4KnwvTMmFaoJgVqgXF2GDiQ1XgfqaZCTUP5To6ovArCMmJ55 tEyV/qHIDb4js2gJIj5+4ZF+xFp659yGmxvqt+sFMu33fe98PSTLQa4UCYpUxa0rabxYP0gJObOwR U6cNEZOi4vh43Dgk9IC0fWkrX2by3dQfB5AbDxJWSACTHv2sYfhRsKLzgcvN06dLGx2RgoAR4RfMJ OZdlljWw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oZMIB-001CdY-M2; Sat, 17 Sep 2022 01:06:47 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oZMHj-001CL7-31 for linux-arm-kernel@lists.infradead.org; Sat, 17 Sep 2022 01:06:21 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-349b78c177eso90963147b3.15 for ; Fri, 16 Sep 2022 18:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=vvtE6TLaYPOCtfXk0O9FalggG3OGcnP88HBgikw4s3s=; b=ZS8EsXPJGtNTPDBSTFPeNymnL+eydmn5nRgAQiRgoh63nhdGTzOadgQMU53fsLn7vZ wyev2+MfrsanzyzIOq70pt1twN+lQLFEiarxgaLxdPOkrq2OZm1QoR0zbj3KqkoE427Z YatrzLJ4U1Qz1IUv2ublq6USSdxp1r0mjww105kH1EY5muqDOo7sBqhVPR0R8MU5Zsyp GkGSAk2LHIMqsfqJz8F8TSBUM9hXspKDyQNjs7qt0Gm511ZOS8tdFpf5kTQkQakbc/PE kc/0ZRUg2PqahDYAMM2N3CbJ6ZKl5kWN4dbhORr0VvI+EZHCar4hhJBeh2pNgIUdhczW Ii5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=vvtE6TLaYPOCtfXk0O9FalggG3OGcnP88HBgikw4s3s=; b=AwvNpE16k5HAwAoRue+UyjN+TLGqOsy6lf1SWODGV8SVGhLrB4iqZATLIp6Xy9rleI rfQ8EXc442RcMiMOQPwdVnQp0bsQVYk/yaf5cEWoENMBtE16kPGWPdUYYKtikIP04DVI Xgvtd4jWfmr4bw6ewFKd1xj8sZwdfWzuJvMdUdjcEpjRQE621AnTS1PV534zFxH3Was4 lVL4+5h3X1MH5F6XAy5/Z7ESRxq4twfVyrPn2Jytbzj+VanEZvrVGPbjiMZl9yWNBLIz kMiBQl1SEuuLHNu8qgEgsOcLMm+f0+rmM9v2Nj8pF8eisVKKQ1RQsS52B1tc77/MFNWI Cb2w== X-Gm-Message-State: ACrzQf0HcN4z0iqGMIBBT6S49dnfV23y/zJbGWpWvyLHxvIaQTMbYqXC vbQB8CHzr9K8zIkl7+3/FJXSWwEFjn0= X-Google-Smtp-Source: AMsMyM5s2RuFF4UFZBuj9Fx+pU9qkLjM1BVQwNejQE34KqLu+nW0MZjqstMEQeQCvo5RW6Lc97Yj1VOZmlQ= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a25:8704:0:b0:6b1:302c:ad63 with SMTP id a4-20020a258704000000b006b1302cad63mr3468976ybl.67.1663376772490; Fri, 16 Sep 2022 18:06:12 -0700 (PDT) Date: Fri, 16 Sep 2022 18:05:57 -0700 In-Reply-To: <20220917010600.532642-1-reijiw@google.com> Mime-Version: 1.0 References: <20220917010600.532642-1-reijiw@google.com> X-Mailer: git-send-email 2.37.3.968.ga6b4b080e4-goog Message-ID: <20220917010600.532642-2-reijiw@google.com> Subject: [PATCH v2 1/4] KVM: arm64: Preserve PSTATE.SS for the guest while single-step is enabled From: Reiji Watanabe To: Marc Zyngier , kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, James Morse , Alexandru Elisei , Suzuki K Poulose , Paolo Bonzini , Ricardo Koller , Oliver Upton , Jing Zhang , Raghavendra Rao Anata , Reiji Watanabe X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220916_180619_198233_5363D4E1 X-CRM114-Status: GOOD ( 16.60 ) 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 Preserve the PSTATE.SS value for the guest while userspace enables single-step (i.e. while KVM manipulates the PSTATE.SS) for the vCPU. Currently, while userspace enables single-step for the vCPU (with KVM_GUESTDBG_SINGLESTEP), KVM sets PSTATE.SS to 1 on every guest entry, not saving its original value. When userspace disables single-step, KVM doesn't restore the original value for the subsequent guest entry (use the current value instead). Exception return instructions copy PSTATE.SS from SPSR_ELx.SS only in certain cases when single-step is enabled (and set it to 0 in other cases). So, the value matters only when the guest enables single-step (and when the guest's Software step state isn't affected by single-step enabled by userspace, practically), though. Fix this by preserving the original PSTATE.SS value while userspace enables single-step, and restoring the value once it is disabled. This fix modifies the behavior of GET_ONE_REG/SET_ONE_REG for the PSTATE.SS while single-step is enabled by userspace. Presently, GET_ONE_REG/SET_ONE_REG gets/sets the current PSTATE.SS value, which KVM will override on the next guest entry (i.e. the value userspace gets/sets is not used for the next guest entry). With this patch, GET_ONE_REG/SET_ONE_REG will get/set the guest's preserved value, which KVM will preserve and try to restore after single-step is disabled. Fixes: 337b99bf7edf ("KVM: arm64: guest debug, add support for single-step") Signed-off-by: Reiji Watanabe --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/debug.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index e9c9388ccc02..ccf8a144f009 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -393,6 +393,7 @@ struct kvm_vcpu_arch { */ struct { u32 mdscr_el1; + bool pstate_ss; } guest_debug_preserved; /* vcpu power state */ diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index 0b28d7db7c76..1bd2a1aee11c 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -32,6 +32,10 @@ static DEFINE_PER_CPU(u64, mdcr_el2); * * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled * after we have restored the preserved value to the main context. + * + * When single-step is enabled by userspace, we tweak PSTATE.SS on every + * guest entry. Preserve PSTATE.SS so we can restore the original value + * for the vcpu after the single-step is disabled. */ static void save_guest_debug_regs(struct kvm_vcpu *vcpu) { @@ -41,6 +45,9 @@ static void save_guest_debug_regs(struct kvm_vcpu *vcpu) trace_kvm_arm_set_dreg32("Saved MDSCR_EL1", vcpu->arch.guest_debug_preserved.mdscr_el1); + + vcpu->arch.guest_debug_preserved.pstate_ss = + (*vcpu_cpsr(vcpu) & DBG_SPSR_SS); } static void restore_guest_debug_regs(struct kvm_vcpu *vcpu) @@ -51,6 +58,11 @@ static void restore_guest_debug_regs(struct kvm_vcpu *vcpu) trace_kvm_arm_set_dreg32("Restored MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1)); + + if (vcpu->arch.guest_debug_preserved.pstate_ss) + *vcpu_cpsr(vcpu) |= DBG_SPSR_SS; + else + *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS; } /**