// SPDX-License-Identifier: GPL-2.0-only1/*2* stacktrace.c : stacktracing APIs needed by rest of kernel3* (wrappers over ARC dwarf based unwinder)4*5* Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)6*7* vineetg: aug 20098* -Implemented CONFIG_STACKTRACE APIs, primarily save_stack_trace_tsk( )9* for displaying task's kernel mode call stack in /proc/<pid>/stack10* -Iterator based approach to have single copy of unwinding core and APIs11* needing unwinding, implement the logic in iterator regarding:12* = which frame onwards to start capture13* = which frame to stop capturing (wchan)14* = specifics of data structs where trace is saved(CONFIG_STACKTRACE etc)15*16* vineetg: March 200917* -Implemented correct versions of thread_saved_pc() and __get_wchan()18*19* rajeshwarr: 200820* -Initial implementation21*/2223#include <linux/ptrace.h>24#include <linux/export.h>25#include <linux/stacktrace.h>26#include <linux/kallsyms.h>27#include <linux/sched/debug.h>2829#include <asm/arcregs.h>30#include <asm/unwind.h>31#include <asm/stacktrace.h>32#include <asm/switch_to.h>3334/*-------------------------------------------------------------------------35* Unwinder Iterator36*-------------------------------------------------------------------------37*/3839#ifdef CONFIG_ARC_DW2_UNWIND4041static int42seed_unwind_frame_info(struct task_struct *tsk, struct pt_regs *regs,43struct unwind_frame_info *frame_info)44{45if (regs) {46/*47* Asynchronous unwinding of intr/exception48* - Just uses the pt_regs passed49*/50frame_info->task = tsk;5152frame_info->regs.r27 = regs->fp;53frame_info->regs.r28 = regs->sp;54frame_info->regs.r31 = regs->blink;55frame_info->regs.r63 = regs->ret;56frame_info->call_frame = 0;57} else if (tsk == NULL || tsk == current) {58/*59* synchronous unwinding (e.g. dump_stack)60* - uses current values of SP and friends61*/62unsigned long fp, sp, blink, ret;63frame_info->task = current;6465__asm__ __volatile__(66"mov %0,r27\n\t"67"mov %1,r28\n\t"68"mov %2,r31\n\t"69"mov %3,r63\n\t"70: "=r"(fp), "=r"(sp), "=r"(blink), "=r"(ret)71);7273frame_info->regs.r27 = fp;74frame_info->regs.r28 = sp;75frame_info->regs.r31 = blink;76frame_info->regs.r63 = ret;77frame_info->call_frame = 0;78} else {79/*80* Asynchronous unwinding of a likely sleeping task81* - first ensure it is actually sleeping82* - if so, it will be in __switch_to, kernel mode SP of task83* is safe-kept and BLINK at a well known location in there84*/8586if (task_is_running(tsk))87return -1;8889frame_info->task = tsk;9091frame_info->regs.r27 = TSK_K_FP(tsk);92frame_info->regs.r28 = TSK_K_ESP(tsk);93frame_info->regs.r31 = TSK_K_BLINK(tsk);94frame_info->regs.r63 = (unsigned int)__switch_to;9596/* In the prologue of __switch_to, first FP is saved on stack97* and then SP is copied to FP. Dwarf assumes cfa as FP based98* but we didn't save FP. The value retrieved above is FP's99* state in previous frame.100* As a work around for this, we unwind from __switch_to start101* and adjust SP accordingly. The other limitation is that102* __switch_to macro is dwarf rules are not generated for inline103* assembly code104*/105frame_info->regs.r27 = 0;106frame_info->regs.r28 += 60;107frame_info->call_frame = 0;108109}110return 0;111}112113#endif114115notrace noinline unsigned int116arc_unwind_core(struct task_struct *tsk, struct pt_regs *regs,117int (*consumer_fn) (unsigned int, void *), void *arg)118{119#ifdef CONFIG_ARC_DW2_UNWIND120int ret = 0, cnt = 0;121unsigned int address;122struct unwind_frame_info frame_info;123124if (seed_unwind_frame_info(tsk, regs, &frame_info))125return 0;126127while (1) {128address = UNW_PC(&frame_info);129130if (!address || !__kernel_text_address(address))131break;132133if (consumer_fn(address, arg) == -1)134break;135136ret = arc_unwind(&frame_info);137if (ret)138break;139140frame_info.regs.r63 = frame_info.regs.r31;141142if (cnt++ > 128) {143printk("unwinder looping too long, aborting !\n");144return 0;145}146}147148return address; /* return the last address it saw */149#else150/* On ARC, only Dward based unwinder works. fp based backtracing is151* not possible (-fno-omit-frame-pointer) because of the way function152* prologue is setup (callee regs saved and then fp set and not other153* way around154*/155pr_warn_once("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");156return 0;157158#endif159}160161/*-------------------------------------------------------------------------162* callbacks called by unwinder iterator to implement kernel APIs163*164* The callback can return -1 to force the iterator to stop, which by default165* keeps going till the bottom-most frame.166*-------------------------------------------------------------------------167*/168169/* Call-back which plugs into unwinding core to dump the stack in170* case of panic/OOPs/BUG etc171*/172static int __print_sym(unsigned int address, void *arg)173{174const char *loglvl = arg;175176printk("%s %pS\n", loglvl, (void *)address);177return 0;178}179180#ifdef CONFIG_STACKTRACE181182/* Call-back which plugs into unwinding core to capture the183* traces needed by kernel on /proc/<pid>/stack184*/185static int __collect_all(unsigned int address, void *arg)186{187struct stack_trace *trace = arg;188189if (trace->skip > 0)190trace->skip--;191else192trace->entries[trace->nr_entries++] = address;193194if (trace->nr_entries >= trace->max_entries)195return -1;196197return 0;198}199200static int __collect_all_but_sched(unsigned int address, void *arg)201{202struct stack_trace *trace = arg;203204if (in_sched_functions(address))205return 0;206207if (trace->skip > 0)208trace->skip--;209else210trace->entries[trace->nr_entries++] = address;211212if (trace->nr_entries >= trace->max_entries)213return -1;214215return 0;216}217218#endif219220static int __get_first_nonsched(unsigned int address, void *unused)221{222if (in_sched_functions(address))223return 0;224225return -1;226}227228/*-------------------------------------------------------------------------229* APIs expected by various kernel sub-systems230*-------------------------------------------------------------------------231*/232233noinline void show_stacktrace(struct task_struct *tsk, struct pt_regs *regs,234const char *loglvl)235{236printk("%s\nStack Trace:\n", loglvl);237arc_unwind_core(tsk, regs, __print_sym, (void *)loglvl);238}239EXPORT_SYMBOL(show_stacktrace);240241/* Expected by sched Code */242void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)243{244show_stacktrace(tsk, NULL, loglvl);245}246247/* Another API expected by schedular, shows up in "ps" as Wait Channel248* Of course just returning schedule( ) would be pointless so unwind until249* the function is not in schedular code250*/251unsigned int __get_wchan(struct task_struct *tsk)252{253return arc_unwind_core(tsk, NULL, __get_first_nonsched, NULL);254}255256#ifdef CONFIG_STACKTRACE257258/*259* API required by CONFIG_STACKTRACE, CONFIG_LATENCYTOP.260* A typical use is when /proc/<pid>/stack is queried by userland261*/262void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)263{264/* Assumes @tsk is sleeping so unwinds from __switch_to */265arc_unwind_core(tsk, NULL, __collect_all_but_sched, trace);266}267268void save_stack_trace(struct stack_trace *trace)269{270/* Pass NULL for task so it unwinds the current call frame */271arc_unwind_core(NULL, NULL, __collect_all, trace);272}273EXPORT_SYMBOL_GPL(save_stack_trace);274#endif275276277