18c2ecf20Sopenharmony_ci/** 28c2ecf20Sopenharmony_ci * @file backtrace.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * @remark Copyright 2004 Silicon Graphics Inc. All Rights Reserved. 58c2ecf20Sopenharmony_ci * @remark Read the file COPYING 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * @author Greg Banks <gnb@melbourne.sgi.com> 88c2ecf20Sopenharmony_ci * @author Keith Owens <kaos@melbourne.sgi.com> 98c2ecf20Sopenharmony_ci * Based on work done for the ia64 port of the SGI kernprof patch, which is 108c2ecf20Sopenharmony_ci * Copyright (c) 2003-2004 Silicon Graphics Inc. All Rights Reserved. 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/oprofile.h> 148c2ecf20Sopenharmony_ci#include <linux/sched.h> 158c2ecf20Sopenharmony_ci#include <linux/mm.h> 168c2ecf20Sopenharmony_ci#include <asm/ptrace.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* 198c2ecf20Sopenharmony_ci * For IA64 we need to perform a complex little dance to get both 208c2ecf20Sopenharmony_ci * the struct pt_regs and a synthetic struct switch_stack in place 218c2ecf20Sopenharmony_ci * to allow the unwind code to work. This dance requires our unwind 228c2ecf20Sopenharmony_ci * using code to be called from a function called from unw_init_running(). 238c2ecf20Sopenharmony_ci * There we only get a single void* data pointer, so use this struct 248c2ecf20Sopenharmony_ci * to hold all the data we need during the unwind. 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_citypedef struct 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci unsigned int depth; 298c2ecf20Sopenharmony_ci struct pt_regs *regs; 308c2ecf20Sopenharmony_ci struct unw_frame_info frame; 318c2ecf20Sopenharmony_ci unsigned long *prev_pfs_loc; /* state for WAR for old spinlock ool code */ 328c2ecf20Sopenharmony_ci} ia64_backtrace_t; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* Returns non-zero if the PC is in the Interrupt Vector Table */ 358c2ecf20Sopenharmony_cistatic __inline__ int in_ivt_code(unsigned long pc) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci extern char ia64_ivt[]; 388c2ecf20Sopenharmony_ci return (pc >= (u_long)ia64_ivt && pc < (u_long)ia64_ivt+32768); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* 428c2ecf20Sopenharmony_ci * Unwind to next stack frame. 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_cistatic __inline__ int next_frame(ia64_backtrace_t *bt) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci /* 478c2ecf20Sopenharmony_ci * Avoid unsightly console message from unw_unwind() when attempting 488c2ecf20Sopenharmony_ci * to unwind through the Interrupt Vector Table which has no unwind 498c2ecf20Sopenharmony_ci * information. 508c2ecf20Sopenharmony_ci */ 518c2ecf20Sopenharmony_ci if (in_ivt_code(bt->frame.ip)) 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* 558c2ecf20Sopenharmony_ci * WAR for spinlock contention from leaf functions. ia64_spinlock_contention_pre3_4 568c2ecf20Sopenharmony_ci * has ar.pfs == r0. Leaf functions do not modify ar.pfs so ar.pfs remains 578c2ecf20Sopenharmony_ci * as 0, stopping the backtrace. Record the previous ar.pfs when the current 588c2ecf20Sopenharmony_ci * IP is in ia64_spinlock_contention_pre3_4 then unwind, if pfs_loc has not changed 598c2ecf20Sopenharmony_ci * after unwind then use pt_regs.ar_pfs which is where the real ar.pfs is for 608c2ecf20Sopenharmony_ci * leaf functions. 618c2ecf20Sopenharmony_ci */ 628c2ecf20Sopenharmony_ci if (bt->prev_pfs_loc && bt->regs && bt->frame.pfs_loc == bt->prev_pfs_loc) 638c2ecf20Sopenharmony_ci bt->frame.pfs_loc = &bt->regs->ar_pfs; 648c2ecf20Sopenharmony_ci bt->prev_pfs_loc = NULL; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci return unw_unwind(&bt->frame) == 0; 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic void do_ia64_backtrace(struct unw_frame_info *info, void *vdata) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci ia64_backtrace_t *bt = vdata; 738c2ecf20Sopenharmony_ci struct switch_stack *sw; 748c2ecf20Sopenharmony_ci int count = 0; 758c2ecf20Sopenharmony_ci u_long pc, sp; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci sw = (struct switch_stack *)(info+1); 788c2ecf20Sopenharmony_ci /* padding from unw_init_running */ 798c2ecf20Sopenharmony_ci sw = (struct switch_stack *)(((unsigned long)sw + 15) & ~15); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci unw_init_frame_info(&bt->frame, current, sw); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci /* skip over interrupt frame and oprofile calls */ 848c2ecf20Sopenharmony_ci do { 858c2ecf20Sopenharmony_ci unw_get_sp(&bt->frame, &sp); 868c2ecf20Sopenharmony_ci if (sp >= (u_long)bt->regs) 878c2ecf20Sopenharmony_ci break; 888c2ecf20Sopenharmony_ci if (!next_frame(bt)) 898c2ecf20Sopenharmony_ci return; 908c2ecf20Sopenharmony_ci } while (count++ < 200); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* finally, grab the actual sample */ 938c2ecf20Sopenharmony_ci while (bt->depth-- && next_frame(bt)) { 948c2ecf20Sopenharmony_ci unw_get_ip(&bt->frame, &pc); 958c2ecf20Sopenharmony_ci oprofile_add_trace(pc); 968c2ecf20Sopenharmony_ci if (unw_is_intr_frame(&bt->frame)) { 978c2ecf20Sopenharmony_ci /* 988c2ecf20Sopenharmony_ci * Interrupt received on kernel stack; this can 998c2ecf20Sopenharmony_ci * happen when timer interrupt fires while processing 1008c2ecf20Sopenharmony_ci * a softirq from the tail end of a hardware interrupt 1018c2ecf20Sopenharmony_ci * which interrupted a system call. Don't laugh, it 1028c2ecf20Sopenharmony_ci * happens! Splice the backtrace into two parts to 1038c2ecf20Sopenharmony_ci * avoid spurious cycles in the gprof output. 1048c2ecf20Sopenharmony_ci */ 1058c2ecf20Sopenharmony_ci /* TODO: split rather than drop the 2nd half */ 1068c2ecf20Sopenharmony_ci break; 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_civoid 1128c2ecf20Sopenharmony_ciia64_backtrace(struct pt_regs * const regs, unsigned int depth) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci ia64_backtrace_t bt; 1158c2ecf20Sopenharmony_ci unsigned long flags; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* 1188c2ecf20Sopenharmony_ci * On IA64 there is little hope of getting backtraces from 1198c2ecf20Sopenharmony_ci * user space programs -- the problems of getting the unwind 1208c2ecf20Sopenharmony_ci * information from arbitrary user programs are extreme. 1218c2ecf20Sopenharmony_ci */ 1228c2ecf20Sopenharmony_ci if (user_mode(regs)) 1238c2ecf20Sopenharmony_ci return; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci bt.depth = depth; 1268c2ecf20Sopenharmony_ci bt.regs = regs; 1278c2ecf20Sopenharmony_ci bt.prev_pfs_loc = NULL; 1288c2ecf20Sopenharmony_ci local_irq_save(flags); 1298c2ecf20Sopenharmony_ci unw_init_running(do_ia64_backtrace, &bt); 1308c2ecf20Sopenharmony_ci local_irq_restore(flags); 1318c2ecf20Sopenharmony_ci} 132