From patchwork Fri Jul 15 06:10:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12918773 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 C56FBC433EF for ; Fri, 15 Jul 2022 06:15:20 +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:References: Mime-Version:Message-Id: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=Rr0zhCrugt1azgKjupd5WggsLdoY2c2E5LRYiAfRsXU=; b=Z0EJLSRpf6hPHRNjrwKUcN+j56 k7d4ePu9YFqP3uQKrUJ0SlQyhB+4zK8nv7tq/R/PtKtpGGnZJNgkPw/6xiPJN0eKVB/8nfa8k12Cv eDgACvz4LaxUC67AyfSCq0aBLppgmjg1PFDxTOUJsk4l9LRIwyQ3IfB9+sbiOCoyWZcRlInhV13V4 iB0o3uxsrDwh63GN7Qq7X0pL+gtk0/ebWqk7ZGXpw5a860KAzrZbV9KxXjFiCXYHBnyshKEXWqvsb 2BmihklepS4gt9r5EBeoLdkD9JH5ZQnN43w5cvhJ0PvVACTQLP1BaU3twMxjBJeLHRarjjBWC2RdQ veBWkJqg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oCEab-004Xdh-J0; Fri, 15 Jul 2022 06:14:13 +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 1oCEY4-004WSu-2X for linux-arm-kernel@lists.infradead.org; Fri, 15 Jul 2022 06:11:37 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31c972f5f84so33621657b3.1 for ; Thu, 14 Jul 2022 23:11:35 -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=Re0lyq5fdLMfXjdX0x8O/EyKsShey1DPNrXl83M7ib4=; b=HJIzfLUePWb4mdMt5PQiB2FXqWysXIomBQwWmZmU4WV/QUZbp4L9o9VFKSwOJZifTZ Fa6IQbg+cMKv/7h9KJH2UcxrcTFfMTgmjaYMPU0KzatCgfcvq9kQCjl1mCU5KlP2UZvf saRXkT4Vpy7oTR0HAdiDDzqWQMS48FOJSp9QhUQNl4JGkHfW+2NCOJZCmji58ttWfn1x SowNG0vi9kC6tGLLFApmgH3fS6+ugyOaFaW5AYdpvJmFjuofxYo9juDxrUY5GGOH6GWX TK67Y6CrAXSsgi0VARxmXTim/P0MFuM9aFY5sRAknI9mcAvSqGV9gdOD4phpS9MBGRLP UbdQ== 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=Re0lyq5fdLMfXjdX0x8O/EyKsShey1DPNrXl83M7ib4=; b=nXYTXEHeL8MS75FZMSDolVBp2FtKaM7H4uQd2W865igZiXKfgCi4WswzoEYRoWImmZ ko+2PfQJmI4fSGSNZnk4E4ydTzYNR3ud/NTzIvpcwQIVYbcVqQ7Qc5sDluz2qst9I2rV 1ZnHn9y0t3CWMfQw/Zn04D0JcC5SwBZ85ev485KqRrb8ljXg/OHN3pVs0tMTTZUHvpvg i3mF98brB7IqdYp7l4ap/BT3Gkk04f/LaZXV5q9GJ1gv/ZHUQTxaBdMd+FZrkJ7dk8G2 /YKR7YLSSFjEfac9PZHzSw8YqN3hCSsUPer0WtanCbJ+pRVV2KmfBSosYolBNFfK1YwR ZEVg== X-Gm-Message-State: AJIora9YIjHlACUxt+bYeL3EbDVF+LKIhbffwRgkyFTLynvDuA2CO4j+ 27YreCWZ14z61uX/Lcul8AnFpid5EEbJbz8oOw== X-Google-Smtp-Source: AGRyM1sW1Rib3u02KM7AZyrfjLA2iwFLsV4nlOryhWe/xB4UcHZVdSrw+Q6l4PSjRDA2LJVX/SPqSYka+ic+lQq4/w== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:f010:455b:62ce:19e]) (user=kaleshsingh job=sendgmr) by 2002:a0d:d747:0:b0:31c:8947:7851 with SMTP id z68-20020a0dd747000000b0031c89477851mr14136383ywd.142.1657865494699; Thu, 14 Jul 2022 23:11:34 -0700 (PDT) Date: Thu, 14 Jul 2022 23:10:21 -0700 In-Reply-To: <20220715061027.1612149-1-kaleshsingh@google.com> Message-Id: <20220715061027.1612149-13-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220715061027.1612149-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog Subject: [PATCH v4 12/18] KVM: arm64: Save protected-nVHE (pKVM) hyp stacktrace From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com Cc: will@kernel.org, qperret@google.com, tabba@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, russell.king@oracle.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, drjones@redhat.com, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220714_231136_150599_4534188D X-CRM114-Status: GOOD ( 14.94 ) 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 In protected nVHE mode, the host cannot access private owned hypervisor memory. Also the hypervisor aims to remains simple to reduce the attack surface and does not provide any printk support. For the above reasons, the approach taken to provide hypervisor stacktraces in protected mode is: 1) Unwind and save the hyp stack addresses in EL2 to a shared buffer with the host (done in this patch). 2) Delegate the dumping and symbolization of the addresses to the host in EL1 (later patch in the series). Signed-off-by: Kalesh Singh --- arch/arm64/include/asm/stacktrace/nvhe.h | 18 ++++++ arch/arm64/kvm/hyp/nvhe/stacktrace.c | 70 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/asm/stacktrace/nvhe.h index 36cf7858ddd8..456a6ae08433 100644 --- a/arch/arm64/include/asm/stacktrace/nvhe.h +++ b/arch/arm64/include/asm/stacktrace/nvhe.h @@ -21,6 +21,22 @@ #include +/** + * kvm_nvhe_unwind_init - Start an unwind from the given nVHE HYP fp and pc + * + * @fp : frame pointer at which to start the unwinding. + * @pc : program counter at which to start the unwinding. + */ +static __always_inline void kvm_nvhe_unwind_init(struct unwind_state *state, + unsigned long fp, + unsigned long pc) +{ + unwind_init_common(state, NULL); + + state->fp = fp; + state->pc = pc; +} + static inline bool on_accessible_stack(const struct task_struct *tsk, unsigned long sp, unsigned long size, struct stack_info *info) @@ -33,6 +49,8 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, */ #ifdef __KVM_NVHE_HYPERVISOR__ +extern void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc); + #ifdef CONFIG_PROTECTED_NVHE_STACKTRACE static inline bool on_overflow_stack(unsigned long sp, unsigned long size, struct stack_info *info) diff --git a/arch/arm64/kvm/hyp/nvhe/stacktrace.c b/arch/arm64/kvm/hyp/nvhe/stacktrace.c index 96c8b93320eb..832a536e440f 100644 --- a/arch/arm64/kvm/hyp/nvhe/stacktrace.c +++ b/arch/arm64/kvm/hyp/nvhe/stacktrace.c @@ -11,4 +11,74 @@ DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack) #ifdef CONFIG_PROTECTED_NVHE_STACKTRACE DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], pkvm_stacktrace); + +/** + * pkvm_save_backtrace_entry - Saves a protected nVHE HYP stacktrace entry + * + * @arg : the position of the entry in the stacktrace buffer + * @where : the program counter corresponding to the stack frame + * + * Save the return address of a stack frame to the shared stacktrace buffer. + * The host can access this shared buffer from EL1 to dump the backtrace. + */ +static bool pkvm_save_backtrace_entry(void *arg, unsigned long where) +{ + unsigned long **stacktrace_pos = (unsigned long **)arg; + unsigned long stacktrace_start, stacktrace_end; + + stacktrace_start = (unsigned long)this_cpu_ptr(pkvm_stacktrace); + stacktrace_end = stacktrace_start + NVHE_STACKTRACE_SIZE - (2 * sizeof(long)); + + if ((unsigned long) *stacktrace_pos > stacktrace_end) + return false; + + /* Save the entry to the current pos in stacktrace buffer */ + **stacktrace_pos = where; + + /* A zero entry delimits the end of the stacktrace. */ + *(*stacktrace_pos + 1) = 0UL; + + /* Increment the current pos */ + ++*stacktrace_pos; + + return true; +} + +/** + * pkvm_save_backtrace - Saves the protected nVHE HYP stacktrace + * + * @fp : frame pointer at which to start the unwinding. + * @pc : program counter at which to start the unwinding. + * + * Save the unwinded stack addresses to the shared stacktrace buffer. + * The host can access this shared buffer from EL1 to dump the backtrace. + */ +static void pkvm_save_backtrace(unsigned long fp, unsigned long pc) +{ + void *stacktrace_start = (void *)this_cpu_ptr(pkvm_stacktrace); + struct unwind_state state; + + kvm_nvhe_unwind_init(&state, fp, pc); + + unwind(&state, pkvm_save_backtrace_entry, &stacktrace_start); +} +#else /* !CONFIG_PROTECTED_NVHE_STACKTRACE */ +static void pkvm_save_backtrace(unsigned long fp, unsigned long pc) +{ +} #endif /* CONFIG_PROTECTED_NVHE_STACKTRACE */ + +/** + * kvm_nvhe_prepare_backtrace - prepare to dump the nVHE backtrace + * + * @fp : frame pointer at which to start the unwinding. + * @pc : program counter at which to start the unwinding. + * + * Saves the information needed by the host to dump the nVHE hypervisor + * backtrace. + */ +void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc) +{ + if (is_protected_kvm_enabled()) + pkvm_save_backtrace(fp, pc); +}