From patchwork Tue Dec 5 08:59:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zong Li X-Patchwork-Id: 173808 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3296501vqy; Tue, 5 Dec 2023 01:00:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFi5QW1fT6IO7Cw/VqIhve0Vlh4Venwe+QjZtiFC0e+XVTrm+JvgcuuerL3WnnkxE1PysuL X-Received: by 2002:a17:902:d305:b0:1d0:700b:3f79 with SMTP id b5-20020a170902d30500b001d0700b3f79mr1052700plc.51.1701766823776; Tue, 05 Dec 2023 01:00:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701766823; cv=none; d=google.com; s=arc-20160816; b=OSz1MY27Ezk46uNV41MFyQzvtwty/UJiS+OiinKDXHlgWKKm5zRSSPxnDqDIxJmiX4 fx2BunALUxtXZuDbzNYhLlKDz8EduWSdlagayRqZVmF9cVzLGG7iUA1XSOOO3vWaJHqA zJmaxf05O0HnuPNFBimz/pjZofh1iHvUFF/vWhCeljL76biRHGAf7JfxsrpyzyLLOXWm ytYTvzWz4MLuTcdiPt516bnyKgGGKGMAr3tszt1JZbtozFG+k8AOpdWas5Dh9abEB1ve pyjeAIivmSELqXUXgmnVe2AR/7x/vyRccdI10Fe3pVhnqfIaH+lW4le7GcWjKCnu7zzy BP/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=Wi6K2Brpiu9Fka3m9z0bXgXfpslNvz1u3mb0b6dJW6o=; fh=oYEYdJA79gR45iwIb16CMKPTkgEmpKJXMMpr8SpVOVg=; b=YRzrfLGWh/R9EIs2iyxAutzs2LJ+ZnhyfGQKcFMnBv2WvENgkgC2Fpf59UxvM97/4X CTj6FvjefKNgloisDAVV3+wlDGjqD6cW4WkyCoUuIHLrp5qyz/X/I6zwACJdHlj1iPJx 4D7um0JiLmUG74r19f/AFWDbMYalSESStS/vnwh3DHNvYcnLvrgntTyzydgu8mpzBnR0 lPUnRXnzdhlbnyndXpI4QLCthuWe0VdhmlvHNTcsod+X/gUGWvE8m7w1iGVFe6ZKjmrQ Uq3Bv8pGduPMkkKWW2u8Zchl+/S5qDMYOkO01u8dTcNo44gXPHWKkiC+yqRFmast7zTX zSiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=m544HOAu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id l10-20020a170903244a00b001cf570ea116si9827063pls.353.2023.12.05.01.00.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 01:00:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=m544HOAu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5FEB0801F8B9; Tue, 5 Dec 2023 01:00:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234920AbjLEJAE (ORCPT + 99 others); Tue, 5 Dec 2023 04:00:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234860AbjLEJAB (ORCPT ); Tue, 5 Dec 2023 04:00:01 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57401184 for ; Tue, 5 Dec 2023 01:00:05 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1d04c097e34so29954765ad.0 for ; Tue, 05 Dec 2023 01:00:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1701766804; x=1702371604; darn=vger.kernel.org; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Wi6K2Brpiu9Fka3m9z0bXgXfpslNvz1u3mb0b6dJW6o=; b=m544HOAuNFcIu+ul2XhWjOVdgMp4Ozi4RmvgDecZb0y12mlA8Vek9manj1OR2nm44J GYDQyGHXvEUANx3ha22Bii8l0rBDL9hr9n3vlBmiH6pSW/Uu+JmRP4M0e6aoI+n3jtio q8p4J1/pAQlbtXgLTEXkwaLo+Z5/zCxo5aOtyhAXKZnPOt97RsXQ9dTSn831Sg4WzRlg WrPBlKpLwM2rqQaSV1NbtwhiACufxIgAU/N1hMQK02liHQxVBii41Dw4wDQl99Fdtj6z iANx+dB52azxEIbzjnVE+XH5kdT7lqLRhlHr1U/k2XQmnVFBMEH+cX9XbJWOTuG6rQiy QsKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701766804; x=1702371604; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Wi6K2Brpiu9Fka3m9z0bXgXfpslNvz1u3mb0b6dJW6o=; b=G26V+ek9L5wUXUWwiBtrhAfFOUnPT8a24qJpB2iDwh8RDhhApPzadS9Rqdr3xBW2ve bpejDk9NyKhGzi6oUGTe//Wy47Dip9jy0WOoJmQTI4q6/Uh5cYQY1DMYQ5PQmdBKjpHo kzY4E1bpb9hcLZRq+2/qwS8YMKDNfGMZWtvRq5XkOE9gYtY8loeeXJACl8oqv6a8X/48 B4yzurNPoO8t0BKZN3A7L3f+CFI12bQoSNav5YmJYfjr1NTG0K/UoWAE9ZRv0P2XGlSU qK6oEjAynLrkv9Mt24vjjS5IgANmmBrnBuTpyM8kVK8DhzgHu8HB2JZEfYjpk0wIQhF7 L6Uw== X-Gm-Message-State: AOJu0YzszvdBryRmPlDdDx8pGLLnYvOtrsdlqV8MSOOLI4wFOFOI0ohS cuFEqIP0ooyyoTxCT+cFPX01YA== X-Received: by 2002:a17:902:ecca:b0:1cf:f868:5b8c with SMTP id a10-20020a170902ecca00b001cff8685b8cmr1228712plh.8.1701766804259; Tue, 05 Dec 2023 01:00:04 -0800 (PST) Received: from hsinchu26.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id q1-20020a17090311c100b001cfc9c926b7sm6134629plh.75.2023.12.05.01.00.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 01:00:04 -0800 (PST) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH] riscv: add CALLER_ADDRx support Date: Tue, 5 Dec 2023 08:59:59 +0000 Message-Id: <20231205085959.32177-1-zong.li@sifive.com> X-Mailer: git-send-email 2.17.1 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 05 Dec 2023 01:00:20 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784431848684599306 X-GMAIL-MSGID: 1784431848684599306 CALLER_ADDRx returns caller's address at specified level, they are used for several tracers. These macros eventually use __builtin_return_address(n) to get the caller's address if arch doesn't define their own implementation. In RISC-V, __builtin_return_address(n) only works when n == 0, we need to walk the stack frame to get the caller's address at specified level. data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows: callsite -> return_address -> arch_stack_walk -> walk_stackframe | | | | level 3 level 2 level 1 level 0 Signed-off-by: Zong Li Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 arch/riscv/kernel/return_address.c diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h index 2b2f5df7ef2c..42777f91a9c5 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -25,6 +25,11 @@ #define ARCH_SUPPORTS_FTRACE_OPS 1 #ifndef __ASSEMBLY__ + +extern void *return_address(unsigned int level); + +#define ftrace_return_address(n) return_address(n) + void MCOUNT_NAME(void); static inline unsigned long ftrace_call_adjust(unsigned long addr) { diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index fee22a3d1b53..40d054939ae2 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,) @@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.o diff --git a/arch/riscv/kernel/return_address.c b/arch/riscv/kernel/return_address.c new file mode 100644 index 000000000000..c2008d4aa6e5 --- /dev/null +++ b/arch/riscv/kernel/return_address.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * This code come from arch/arm64/kernel/return_address.c + * + * Copyright (C) 2023 SiFive. + */ + +#include +#include +#include + +struct return_address_data { + unsigned int level; + void *addr; +}; + +static bool save_return_addr(void *d, unsigned long pc) +{ + struct return_address_data *data = d; + + if (!data->level) { + data->addr = (void *)pc; + return false; + } + + --data->level; + + return true; +} +NOKPROBE_SYMBOL(save_return_addr); + +void *return_address(unsigned int level) +{ + struct return_address_data data; + + data.level = level + 3; + data.addr = NULL; + + arch_stack_walk(save_return_addr, &data, current, NULL); + + if (!data.level) + return data.addr; + else + return NULL; + +} +EXPORT_SYMBOL_GPL(return_address); +NOKPROBE_SYMBOL(return_address);