From patchwork Fri Oct 15 12:51:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 12561457 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 761A5C433EF for ; Fri, 15 Oct 2021 12:59:31 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 43B196108E for ; Fri, 15 Oct 2021 12:59:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 43B196108E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=g2Rf/b9v9OBNj+17yW3v2mKqabtfBYXPtpyHnudhxkw=; b=wgQWsnW1t6edkB LwhQCBVNR/2kzkSoUzABdmswcz842BycA4DzE2cTQzk+xNoo9BlTex6D2iImtali4QLVVDmr8pJcF tDDxFGL5z9qePHVEb2Eag51pUgkc7cCDEIth7plodDaOxTVIquv9o8BdyGRmNfKwIL5q7tfyKEhHm xnNCE8+LJ2Q+VnGgYr84OeDAElnWuO6xd0i0KBq3eEVXPpw0wD8+nF6jmeiWgm1N1eYZdmsZSLUAD Mr1IEBMwE58IUXCpObCi0M34F+7+pfufKLHkv3vl7G6ZWavd4nrj0yKCuEThp8e6lYdHOr6aX07ZP rJiskEF+e7f3C5sku5LQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mbMmU-0074gl-75; Fri, 15 Oct 2021 12:57:51 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mbMgp-0071Yt-ON for linux-arm-kernel@lists.infradead.org; Fri, 15 Oct 2021 12:52:01 +0000 Received: by mail.kernel.org (Postfix) with ESMTPSA id A57886108E; Fri, 15 Oct 2021 12:51:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634302319; bh=lMfZZP/tcQBNE+YtsSJsSIaa1mi/f3aqAfbOhPMBTwU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VinROdbI2nOQEwbz1Bbr8uLxnMs6z2HkeNogB0M9aJsvxmpPXojOh/Ok3QcltLx4x rosRgtidldui2bVynfYGTcxW9L4QzOVkEilqPPC5v+upmHxl1/xQM7hJXhKa+mTAZv UBVeeaz9sAX6U4dq7C1QaqZYJ4TDOdMJPk2v/1PW8fYOtSgEpW5VoBLjLSRKfzdHXl LGdIWax8OFUtfaWi751ASKQ54ZlXB3oqBilFbaI/vSW/Ygi6ZFTxa4F8/8UQoh2O/4 +telTfkMb0UeeKqxoBMikS+VqSGVz6jX5RJhmujoharUU4lXe+DLeh3MD/lF07giR+ awEpdluZHPIZQ== From: Masami Hiramatsu To: Steven Rostedt Cc: "Naveen N . Rao" , Ananth N Mavinakayanahalli , Ingo Molnar , linux-kernel@vger.kernel.org, mhiramat@kernel.org, Sven Schnelle , Catalin Marinas , Will Deacon , Russell King , Nathan Chancellor , Nick Desaulniers , linux-arm-kernel@lists.infradead.org Subject: [PATCH 08/10] ARM: kprobes: Make a frame pointer on __kretprobe_trampoline Date: Fri, 15 Oct 2021 21:51:56 +0900 Message-Id: <163430231601.459050.8640625926942332950.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <163430224341.459050.2369208860773018092.stgit@devnote2> References: <163430224341.459050.2369208860773018092.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211015_055159_897229_4A6F5731 X-CRM114-Status: GOOD ( 18.62 ) 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 Currently kretprobe on ARM just fills r0-r11 of pt_regs, but that is not enough for the stacktrace. Moreover, from the user kretprobe handler, stacktrace needs a frame pointer on the __kretprobe_trampoline. This adds a frame pointer on __kretprobe_trampoline for both gcc and clang case. Those have different frame pointer so we need different but similar stack on pt_regs. Gcc makes the frame pointer (fp) to point the 'pc' address of the {fp, ip (=sp), lr, pc}, this means {r11, r13, r14, r15}. Thus if we save the r11 (fp) on pt_regs->r12, we can make this set on the end of pt_regs. On the other hand, Clang makes the frame pointer to point the 'fp' address of {fp, lr} on stack. Since the next to the pt_regs->lr is pt_regs->sp, I reused the pair of pt_regs->fp and pt_regs->ip. So this stores the 'lr' on pt_regs->ip and make the fp to point pt_regs->fp. For both cases, saves __kretprobe_trampoline address to pt_regs->lr, so that the stack tracer can identify this frame pointer has been made by the __kretprobe_trampoline. Note that if the CONFIG_FRAME_POINTER is not set, this keeps fp as is. Signed-off-by: Masami Hiramatsu Reviewed-by: Nick Desaulniers --- arch/arm/probes/kprobes/core.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/arch/arm/probes/kprobes/core.c b/arch/arm/probes/kprobes/core.c index 95f23b47ba27..7cbd65a22769 100644 --- a/arch/arm/probes/kprobes/core.c +++ b/arch/arm/probes/kprobes/core.c @@ -368,16 +368,35 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self, /* * When a retprobed function returns, trampoline_handler() is called, * calling the kretprobe's handler. We construct a struct pt_regs to - * give a view of registers r0-r11 to the user return-handler. This is - * not a complete pt_regs structure, but that should be plenty sufficient - * for kretprobe handlers which should normally be interested in r0 only - * anyway. + * give a view of registers r0-r11, sp, lr, and pc to the user + * return-handler. This is not a complete pt_regs structure, but that + * should be enough for stacktrace from the return handler with or + * without pt_regs. */ void __naked __kprobes __kretprobe_trampoline(void) { __asm__ __volatile__ ( - "sub sp, sp, #16 \n\t" + "ldr lr, =__kretprobe_trampoline \n\t" + "stmdb sp!, {sp, lr, pc} \n\t" +#ifdef CONFIG_FRAME_POINTER + /* __kretprobe_trampoline makes a framepointer on pt_regs. */ +#ifdef CONFIG_CC_IS_CLANG + /* In clang case, pt_regs->ip = lr. */ + "stmdb sp!, {lr} \n\t" "stmdb sp!, {r0 - r11} \n\t" + /* fp points regs->r11 (fp) */ + "add fp, sp, #44 \n\t" +#else /* !CONFIG_CC_IS_CLANG */ + /* In gcc case, pt_regs->ip = fp. */ + "stmdb sp!, {fp} \n\t" + "stmdb sp!, {r0 - r11} \n\t" + /* fp points regs->r15 (pc) */ + "add fp, sp, #60 \n\t" +#endif /* CONFIG_CC_IS_CLANG */ +#else /* !CONFIG_FRAME_POINTER */ + "sub sp, sp, #4 \n\t" + "stmdb sp!, {r0 - r11} \n\t" +#endif /* CONFIG_FRAME_POINTER */ "mov r0, sp \n\t" "bl trampoline_handler \n\t" "mov lr, r0 \n\t"