Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp120031pxb; Wed, 20 Oct 2021 18:00:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZvYd7Q7G7Oxw+Y/4DJlVlC197m8Yap6ZijOjqrumbhnoenPri3s2F6AmJjADOq+IC+r/l X-Received: by 2002:a50:e188:: with SMTP id k8mr3308219edl.119.1634778021001; Wed, 20 Oct 2021 18:00:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634778020; cv=none; d=google.com; s=arc-20160816; b=qQKDZ/DBmMF+cEKn2lqJNLInEUP0TB0B4C44RUGoqil41sL9nhJ4G3BPFzqIQsDPbh QKFjqguOBRNxaGAWUl+4IiN87/nofDmBhtqvziKyfoL46fRif3+GqzfkLo4EJ32OnUJj lf5X6U/0ujK3DNzayywLuHcN9t9YORD/Ghsx1eIZXYoHZlnybcHOTsz59BI86o5pbvH3 +buUMCBEezktdis6T/D0AEjuYnGUzJa0lnuzz/tuHd36bettQYvo3uPex7qd4lT+U4RX rS7b/Y9B3KvyEwPy3x++RctnLxntDp+8UDWijqdAku5tZjH9v9Oeom+/X3GBRzqXqdVd C/nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=f5gU4jYXg/60K2J/NZTpUeO4sYd85T1KJPRJJv5qFi8=; b=q2rjmzXuHr1o+IrnAAeZjK5Uf9ticCis4jGUdLI8R9tSHA5SdzksXX1+0L7E3bL3HB fO1OV+hVTlzrcC9gSE7s2xjhgP2LJU2Hp6N/xJPN1/Sa74DfM9zVRz4+SQoYHCMEv0CA 2G7PovFMq8WOeZtWfLKNp1fouO839PswZxJg79ZnQWHG2avXRwszbOGUee+Gn4RzZ0AT PTr1DNJLvZlQuIvDFASJvpT7iAqKUIIrsIxDU2NQTf+kXTOz6Y0RPCOswok1w/e1HID4 GZYynRgJhTDNHJj4lXQ3yh137udrj2oPQxN7Dp2/c3m7N2qyEp01hbXbb8F4GFq22TFI 6O3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gMqVTtS+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g1si4943700edk.532.2021.10.20.17.59.57; Wed, 20 Oct 2021 18:00:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gMqVTtS+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231520AbhJUA5s (ORCPT + 99 others); Wed, 20 Oct 2021 20:57:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:51818 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231535AbhJUA5o (ORCPT ); Wed, 20 Oct 2021 20:57:44 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0F2FE6109F; Thu, 21 Oct 2021 00:55:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634777729; bh=f5jHlGWDUu2jM115NlKoD+47Oicte69WNpox78oxano=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gMqVTtS+DHshaegDYSPtuTW0B/DYyDMqHqpiTtq1BX/fgixCPBpFvHu1X/hkJEXxH vjXZfYNrL8FSbhpQ69pinyQLpTF2aG+ZVci8sYmVevJu8ZqjFWYGWS4cCqrZe+e70i vFhUciZhRLXK3Ne/owOTyA4/DJZv2YfQl6l+4eQgLbl1ePAwLWSfo+N9kANWi1X+DA K0fnGemHHWfIw4Hb7wdqeMMtN3dg41odF3HRkgJ9MfzMMC2JmUOm+hI9UScGflrFKf SbLNrmslf7ENXVP2NnTJU/y5FPbz/prd9u+nHQp2VnPFqBvmlSPBDy4fGbqNOKSFnE JnobIDBNLt9Fw== 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 v3 8/9] ARM: kprobes: Make a frame pointer on __kretprobe_trampoline Date: Thu, 21 Oct 2021 09:55:26 +0900 Message-Id: <163477772593.264901.7405996794526239017.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <163477765570.264901.3851692300287671122.stgit@devnote2> References: <163477765570.264901.3851692300287671122.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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 --- Changes in v3: - Avoid using !sp when storing sp itself. - Unify stmdb register list. - Keep the current assembly code when CONFIG_FRAME_POINTER=n. --- arch/arm/probes/kprobes/core.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/arch/arm/probes/kprobes/core.c b/arch/arm/probes/kprobes/core.c index 95f23b47ba27..4848404ba51b 100644 --- a/arch/arm/probes/kprobes/core.c +++ b/arch/arm/probes/kprobes/core.c @@ -368,16 +368,36 @@ 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__ ( +#ifdef CONFIG_FRAME_POINTER + "ldr lr, =__kretprobe_trampoline \n\t" + /* __kretprobe_trampoline makes a framepointer on pt_regs. */ +#ifdef CONFIG_CC_IS_CLANG + "stmdb sp, {sp, lr, pc} \n\t" + "sub sp, sp, #12 \n\t" + /* In clang case, pt_regs->ip = lr. */ + "stmdb sp!, {r0 - r11, lr} \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, sp, lr, pc} \n\t" "sub sp, sp, #16 \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, #16 \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"