Received: by 10.223.164.202 with SMTP id h10csp398556wrb; Thu, 30 Nov 2017 00:55:56 -0800 (PST) X-Google-Smtp-Source: AGs4zMZML8aznEaFOiAf+NRg/70+KjgLoJjoEnT8oNH9CWR8l3snfJJV7DZJSPWX9va6sCtcqhT7 X-Received: by 10.101.81.72 with SMTP id g8mr1710549pgq.135.1512032154870; Thu, 30 Nov 2017 00:55:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512032154; cv=none; d=google.com; s=arc-20160816; b=RDI5rU89X5+q0UuGUfgmNxxBtDqwhiydpyzfJ07a98TxKjvjsjiUa5XzbMJiyfF5+f 0T4P0eyc9G5FojBVMja39hNTRPdy0G4ROEprNDPuoWEUFbJFqTiV90nM3NE6V1EqOXKa t9Sid4RTPDsuIr4+wnL+Yp++cIGw0UgNZUBMfvECe7sw8luljqTy+uXpBjdiHm9iCC72 tQ9NrX0FlaJxyeyR3gvp1i9jeNU/74g7a5XsV6mp6ApyFQWcgKJTB1oigu6ktwUSC4Kh vu+Gb7+3ewNqDCVXv/MDXPMclHQ7CUCb6T5T71Bs83TS8jKRfx5R84QxrecZPhF7tl6f /ckA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=ctt/D9o3/b2axRXmgCer9362OVm8Tfi1roq8+rn8NkQ=; b=AJcKjB57utHWOC0P8V4u3Kqsz4kN5S+i3UqCLa3i4QINiJiuEDbMKLuGL2B3uj5PcV WPy8cvnpn6sqdMbXSLTOitxBAUtzJNXG93CI5wQul9UAIgragwa+R5YiCHvdICTd/N23 nRVFidZZTXVw95U9mShFK0Uc88AdlTakLeh2OCtjOlJsUlXnXif0danJX32LGGWKcD0C 1TZGJqbqh0Mc6V/oVvyQidDqXYL8r8lbeV7+wzbiKxnzPXEOw8c4xsHp6kMbSD2imPYB oSNj8JEOB5gV/0l9Vk370YOGZfFJpoLEkp2LBPxM93W7SDdTAUXgTRS2i+NvGh3T+Czq u7Xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qcny9qZz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h3si2642045pgr.714.2017.11.30.00.55.41; Thu, 30 Nov 2017 00:55:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qcny9qZz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752025AbdK3Iy7 (ORCPT + 99 others); Thu, 30 Nov 2017 03:54:59 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:34231 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751178AbdK3Iy4 (ORCPT ); Thu, 30 Nov 2017 03:54:56 -0500 Received: by mail-pg0-f68.google.com with SMTP id j4so2742318pgp.1 for ; Thu, 30 Nov 2017 00:54:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=ctt/D9o3/b2axRXmgCer9362OVm8Tfi1roq8+rn8NkQ=; b=qcny9qZzNiQWeLrq7NLwiFMQk4PGB1JG6OwQpfeEIN9YcGgLTEufFz3Ct2UK6HAlmL EufFSP/lNN67+GYtWopeiOvycIwH1a+MELdsIdonwxGkywZ0dnnc5sDzOv/IsuGfpaIs lCLnaIghynvrBzw/84TQcrR4tDmiXFDw1QOuAgXCl09Mmg5S/Twmd8DZZGT0DIOSkvNx n2iOTL+7XoRP7tDPq3hv+kjKnn9xRSt5AZ//Zmv+lgrDNANRT2NT5Lbk6WTHJtpTXzci VcMCFR2vWE1ish5ebs5UVYavNFo36W5SCZhafU1wZetlmn7zF/T37veEX6743g8yNd8B ESPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ctt/D9o3/b2axRXmgCer9362OVm8Tfi1roq8+rn8NkQ=; b=CF0sPL+w/xCne8Xkyh7JehCiJlSgbv5o+qQf+BZTCR3QMIMBA0N57ioUl33iS6SVfE M697BvCFHSK+TIKvc7ScAsA5m0UM7yBEXKll4F7PJskYuPB+6dnQvfwCgF6DpSl9bkyy 5KPlc/DJ2EPCKlec4BQDhapgcvwFU3j7G8UgBDjG4nr8iq/atwyvJVlHss7m1M/H/kpS Qykoq5PAcJH8/QdRNBNcAAo4cMCghZubYhrEgqGhnN/EfORu7gN0xNLitgjM5+Kxy8Yh c74x1mikCWHfPoX7x18V8k/rMeAykUASblvmFDVXI8h84eCiA7j7P1uwJizWK4p4eYs4 fbGA== X-Gm-Message-State: AJaThX7t+lpMRJQNiRcCU7yrZJIAGqDGf9QjH69cEb6eekeQjPkcLwKF MNVrRTHKHYsMkBpfQr0DEog= X-Received: by 10.99.124.89 with SMTP id l25mr1679378pgn.86.1512032096148; Thu, 30 Nov 2017 00:54:56 -0800 (PST) Received: from localhost.localdomain (36-226-155-197.dynamic-ip.hinet.net. [36.226.155.197]) by smtp.gmail.com with ESMTPSA id l5sm5703509pgo.24.2017.11.30.00.54.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Nov 2017 00:54:55 -0800 (PST) From: Alan Kao X-Google-Original-From: Alan Kao To: Palmer Dabbelt , Albert Ou , Steven Rostedt , patches@groups.riscv.org, Ingo Molnar , linux-kernel@vger.kernel.org Cc: Alan Kao Subject: [PATCH] riscv/ftrace: Add basic support Date: Thu, 30 Nov 2017 16:53:35 +0800 Message-Id: <20171130085335.7949-1-alankao@andestech.com> X-Mailer: git-send-email 2.15.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch contains basic ftrace support for RV64I platform. Specifically, function tracer (HAVE_FUNCTION_TRACER), function graph tracer (HAVE_FUNCTION_GRAPH_TRACER), and a frame pointer test (HAVE_FUNCTION_GRAPH_FP_TEST) are implemented following the instructions in Documentation/trace/ftrace-design.txt. Note that the functions in both ftrace.c and setup.c should not be hooked with the compiler's -pg option: to prevent infinite self- referencing for the former, and to ignore early setup stuff for the latter. Signed-off-by: Alan Kao --- arch/riscv/Kconfig | 8 +++ arch/riscv/include/asm/Kbuild | 1 - arch/riscv/include/asm/ftrace.h | 23 +++++++ arch/riscv/kernel/Makefile | 7 ++ arch/riscv/kernel/ftrace.c | 56 ++++++++++++++++ arch/riscv/kernel/mcount.S | 139 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/ftrace.h create mode 100644 arch/riscv/kernel/ftrace.c create mode 100644 arch/riscv/kernel/mcount.S diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 2e15e85c8f7e..07c3df0919b7 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -60,6 +60,12 @@ config PAGE_OFFSET config STACKTRACE_SUPPORT def_bool y +config TRACE_IRQFLAGS_SUPPORT + def_bool y + +config LOCKDEP_SUPPORT + def_bool y + config RWSEM_GENERIC_SPINLOCK def_bool y @@ -112,6 +118,8 @@ config ARCH_RV64I bool "RV64I" select CPU_SUPPORTS_64BIT_KERNEL select 64BIT + select HAVE_FUNCTION_TRACER + select HAVE_FUNCTION_GRAPH_TRACER endchoice diff --git a/arch/riscv/include/asm/Kbuild b/arch/riscv/include/asm/Kbuild index 18158be62a2b..680301bfbc4b 100644 --- a/arch/riscv/include/asm/Kbuild +++ b/arch/riscv/include/asm/Kbuild @@ -12,7 +12,6 @@ generic-y += errno.h generic-y += exec.h generic-y += fb.h generic-y += fcntl.h -generic-y += ftrace.h generic-y += futex.h generic-y += hardirq.h generic-y += hash.h diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h new file mode 100644 index 000000000000..38beadb07ad5 --- /dev/null +++ b/arch/riscv/include/asm/ftrace.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2017 Andes Technology Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* + * The graph frame test is not possible if CONFIG_FRAME_POINTER is not enabled. + * Check arch/riscv/kernel/mcount.S for detail. + */ +#if defined(CONFIG_FUNCTION_GRAPH_TRACER) && defined(CONFIG_FRAME_POINTER) +#define HAVE_FUNCTION_GRAPH_FP_TEST +#endif diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index ab8baf7bd142..15941f3b8363 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -2,6 +2,11 @@ # Makefile for the RISC-V Linux kernel # +#ifdef CONFIG_FTRACE +CFLAGS_REMOVE_ftrace.o = -pg +CFLAGS_REMOVE_setup.o = -pg +#endif + extra-y += head.o extra-y += vmlinux.lds @@ -29,5 +34,7 @@ CFLAGS_setup.o := -mcmodel=medany obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_MODULES) += module.o +obj-$(CONFIG_FUNCTION_TRACER) += mcount.o +obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o clean: diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c new file mode 100644 index 000000000000..e425c8f3f1cd --- /dev/null +++ b/arch/riscv/kernel/ftrace.c @@ -0,0 +1,56 @@ +/* + * arch/riscv/kernel/ftrace.c + * + * Copyright (C) 2013 Linaro Limited + * Author: AKASHI Takahiro + * Copyright (C) 2017 Andes Technology Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER +/* + * Most of this file is copied from arm64. + */ +void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, + unsigned long frame_pointer) +{ + unsigned long return_hooker = (unsigned long)&return_to_handler; + unsigned long old; + struct ftrace_graph_ent trace; + int err; + + if (unlikely(atomic_read(¤t->tracing_graph_pause))) + return; + + /* + * We don't suffer access faults, so no extra fault-recovery assembly + * is needed here. + */ + old = *parent; + + trace.func = self_addr; + trace.depth = current->curr_ret_stack + 1; + + if (!ftrace_graph_entry(&trace)) + return; + + err = ftrace_push_return_trace(old, self_addr, &trace.depth, + frame_pointer, NULL); + if (err == -EBUSY) + return; + *parent = return_hooker; +} +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ diff --git a/arch/riscv/kernel/mcount.S b/arch/riscv/kernel/mcount.S new file mode 100644 index 000000000000..5307358219bf --- /dev/null +++ b/arch/riscv/kernel/mcount.S @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2017 Andes Technology Corporation + * Author: Alan Kao + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + .text + + .macro SAVE_ABI_STATE + addi sp, sp, -16 + sd s0, 0(sp) + sd ra, 8(sp) + addi s0, sp, 16 + .endm + + /* + * The call to ftrace_return_to_handler would overwrite the return + * register if a0 was not saved. + */ + .macro SAVE_RET_ABI_STATE + addi sp, sp, -32 + sd s0, 16(sp) + sd ra, 24(sp) + sd a0, 8(sp) + addi s0, sp, 32 + .endm + + .macro STORE_ABI_STATE + ld ra, 8(sp) + ld s0, 0(sp) + addi sp, sp, 16 + .endm + + .macro STORE_RET_ABI_STATE + ld ra, 24(sp) + ld s0, 16(sp) + ld a0, 8(sp) + addi sp, sp, 32 + .endm + +ENTRY(ftrace_stub) + ret +ENDPROC(ftrace_stub) + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER +ENTRY(return_to_handler) +/* + * On implementing the frame point test, the ideal way is to compare the + * s0 (frame pointer, if enabled) on entry and the sp (stack pointer) on return. + * However, the psABI of variable-length-argument functions does not allow this. + * + * So alternatively we check the *old* frame pointer position, that is, the + * value stored in -16(s0) on entry, and the s0 on return. + */ +#ifdef HAVE_FUNCTION_GRAPH_FP_TEST + mv t6, s0 +#endif + SAVE_RET_ABI_STATE +#ifdef HAVE_FUNCTION_GRAPH_FP_TEST + mv a0, t6 +#endif + la t0, ftrace_return_to_handler + jalr t0 + mv a1, a0 + STORE_RET_ABI_STATE + jr a1 +ENDPROC(return_to_handler) +EXPORT_SYMBOL(return_to_handler) +#endif + +ENTRY(_mcount) + la t4, ftrace_stub +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + la t0, ftrace_graph_return + ld t1, 0(t0) + bne t1, t4, do_ftrace_graph_caller + + la t3, ftrace_graph_entry + ld t2, 0(t3) + la t6, ftrace_graph_entry_stub + bne t2, t6, do_ftrace_graph_caller +#endif + la t3, ftrace_trace_function + ld t5, 0(t3) + bne t5, t4, do_trace + ret + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER +/* + * A pseudo representation for the function graph tracer: + * prepare_to_return(&ra_to_caller_of_caller, ra_to_caller) + */ +do_ftrace_graph_caller: + addi a0, s0, -8 + mv a1, ra +#ifdef HAVE_FUNCTION_GRAPH_FP_TEST + ld a2, -16(s0) +#endif + SAVE_ABI_STATE + la t0, prepare_ftrace_return + jalr t0 + STORE_ABI_STATE + ret +#endif + +/* + * A pseudo representation for the function tracer: + * (*ftrace_trace_function)(ra_to_caller, ra_to_caller_of_caller) + */ +do_trace: + ld a1, -8(s0) + mv a0, ra + + SAVE_ABI_STATE + jalr t5 + STORE_ABI_STATE + ret +ENDPROC(_mcount) +EXPORT_SYMBOL(_mcount) -- 2.15.0 From 1585894482840960260@xxx Mon Dec 04 22:33:56 +0000 2017 X-GM-THRID: 1585671332603149689 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread