152da443eSMark Rutland /* 252da443eSMark Rutland * arm64 callchain support 352da443eSMark Rutland * 452da443eSMark Rutland * Copyright (C) 2015 ARM Limited 552da443eSMark Rutland * 652da443eSMark Rutland * This program is free software; you can redistribute it and/or modify 752da443eSMark Rutland * it under the terms of the GNU General Public License version 2 as 852da443eSMark Rutland * published by the Free Software Foundation. 952da443eSMark Rutland * 1052da443eSMark Rutland * This program is distributed in the hope that it will be useful, 1152da443eSMark Rutland * but WITHOUT ANY WARRANTY; without even the implied warranty of 1252da443eSMark Rutland * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1352da443eSMark Rutland * GNU General Public License for more details. 1452da443eSMark Rutland * 1552da443eSMark Rutland * You should have received a copy of the GNU General Public License 1652da443eSMark Rutland * along with this program. If not, see <http://www.gnu.org/licenses/>. 1752da443eSMark Rutland */ 1852da443eSMark Rutland #include <linux/perf_event.h> 1952da443eSMark Rutland #include <linux/uaccess.h> 2052da443eSMark Rutland 2152da443eSMark Rutland #include <asm/stacktrace.h> 2252da443eSMark Rutland 2352da443eSMark Rutland struct frame_tail { 2452da443eSMark Rutland struct frame_tail __user *fp; 2552da443eSMark Rutland unsigned long lr; 2652da443eSMark Rutland } __attribute__((packed)); 2752da443eSMark Rutland 2852da443eSMark Rutland /* 2952da443eSMark Rutland * Get the return address for a single stackframe and return a pointer to the 3052da443eSMark Rutland * next frame tail. 3152da443eSMark Rutland */ 3252da443eSMark Rutland static struct frame_tail __user * 3352da443eSMark Rutland user_backtrace(struct frame_tail __user *tail, 3452da443eSMark Rutland struct perf_callchain_entry *entry) 3552da443eSMark Rutland { 3652da443eSMark Rutland struct frame_tail buftail; 3752da443eSMark Rutland unsigned long err; 3852da443eSMark Rutland 3952da443eSMark Rutland /* Also check accessibility of one struct frame_tail beyond */ 4052da443eSMark Rutland if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) 4152da443eSMark Rutland return NULL; 4252da443eSMark Rutland 4352da443eSMark Rutland pagefault_disable(); 4452da443eSMark Rutland err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail)); 4552da443eSMark Rutland pagefault_enable(); 4652da443eSMark Rutland 4752da443eSMark Rutland if (err) 4852da443eSMark Rutland return NULL; 4952da443eSMark Rutland 5052da443eSMark Rutland perf_callchain_store(entry, buftail.lr); 5152da443eSMark Rutland 5252da443eSMark Rutland /* 5352da443eSMark Rutland * Frame pointers should strictly progress back up the stack 5452da443eSMark Rutland * (towards higher addresses). 5552da443eSMark Rutland */ 5652da443eSMark Rutland if (tail >= buftail.fp) 5752da443eSMark Rutland return NULL; 5852da443eSMark Rutland 5952da443eSMark Rutland return buftail.fp; 6052da443eSMark Rutland } 6152da443eSMark Rutland 6252da443eSMark Rutland #ifdef CONFIG_COMPAT 6352da443eSMark Rutland /* 6452da443eSMark Rutland * The registers we're interested in are at the end of the variable 6552da443eSMark Rutland * length saved register structure. The fp points at the end of this 6652da443eSMark Rutland * structure so the address of this struct is: 6752da443eSMark Rutland * (struct compat_frame_tail *)(xxx->fp)-1 6852da443eSMark Rutland * 6952da443eSMark Rutland * This code has been adapted from the ARM OProfile support. 7052da443eSMark Rutland */ 7152da443eSMark Rutland struct compat_frame_tail { 7252da443eSMark Rutland compat_uptr_t fp; /* a (struct compat_frame_tail *) in compat mode */ 7352da443eSMark Rutland u32 sp; 7452da443eSMark Rutland u32 lr; 7552da443eSMark Rutland } __attribute__((packed)); 7652da443eSMark Rutland 7752da443eSMark Rutland static struct compat_frame_tail __user * 7852da443eSMark Rutland compat_user_backtrace(struct compat_frame_tail __user *tail, 7952da443eSMark Rutland struct perf_callchain_entry *entry) 8052da443eSMark Rutland { 8152da443eSMark Rutland struct compat_frame_tail buftail; 8252da443eSMark Rutland unsigned long err; 8352da443eSMark Rutland 8452da443eSMark Rutland /* Also check accessibility of one struct frame_tail beyond */ 8552da443eSMark Rutland if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) 8652da443eSMark Rutland return NULL; 8752da443eSMark Rutland 8852da443eSMark Rutland pagefault_disable(); 8952da443eSMark Rutland err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail)); 9052da443eSMark Rutland pagefault_enable(); 9152da443eSMark Rutland 9252da443eSMark Rutland if (err) 9352da443eSMark Rutland return NULL; 9452da443eSMark Rutland 9552da443eSMark Rutland perf_callchain_store(entry, buftail.lr); 9652da443eSMark Rutland 9752da443eSMark Rutland /* 9852da443eSMark Rutland * Frame pointers should strictly progress back up the stack 9952da443eSMark Rutland * (towards higher addresses). 10052da443eSMark Rutland */ 10152da443eSMark Rutland if (tail + 1 >= (struct compat_frame_tail __user *) 10252da443eSMark Rutland compat_ptr(buftail.fp)) 10352da443eSMark Rutland return NULL; 10452da443eSMark Rutland 10552da443eSMark Rutland return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1; 10652da443eSMark Rutland } 10752da443eSMark Rutland #endif /* CONFIG_COMPAT */ 10852da443eSMark Rutland 10952da443eSMark Rutland void perf_callchain_user(struct perf_callchain_entry *entry, 11052da443eSMark Rutland struct pt_regs *regs) 11152da443eSMark Rutland { 11252da443eSMark Rutland if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { 11352da443eSMark Rutland /* We don't support guest os callchain now */ 11452da443eSMark Rutland return; 11552da443eSMark Rutland } 11652da443eSMark Rutland 11752da443eSMark Rutland perf_callchain_store(entry, regs->pc); 11852da443eSMark Rutland 11952da443eSMark Rutland if (!compat_user_mode(regs)) { 12052da443eSMark Rutland /* AARCH64 mode */ 12152da443eSMark Rutland struct frame_tail __user *tail; 12252da443eSMark Rutland 12352da443eSMark Rutland tail = (struct frame_tail __user *)regs->regs[29]; 12452da443eSMark Rutland 12552da443eSMark Rutland while (entry->nr < PERF_MAX_STACK_DEPTH && 12652da443eSMark Rutland tail && !((unsigned long)tail & 0xf)) 12752da443eSMark Rutland tail = user_backtrace(tail, entry); 12852da443eSMark Rutland } else { 12952da443eSMark Rutland #ifdef CONFIG_COMPAT 13052da443eSMark Rutland /* AARCH32 compat mode */ 13152da443eSMark Rutland struct compat_frame_tail __user *tail; 13252da443eSMark Rutland 13352da443eSMark Rutland tail = (struct compat_frame_tail __user *)regs->compat_fp - 1; 13452da443eSMark Rutland 13552da443eSMark Rutland while ((entry->nr < PERF_MAX_STACK_DEPTH) && 13652da443eSMark Rutland tail && !((unsigned long)tail & 0x3)) 13752da443eSMark Rutland tail = compat_user_backtrace(tail, entry); 13852da443eSMark Rutland #endif 13952da443eSMark Rutland } 14052da443eSMark Rutland } 14152da443eSMark Rutland 14252da443eSMark Rutland /* 14352da443eSMark Rutland * Gets called by walk_stackframe() for every stackframe. This will be called 14452da443eSMark Rutland * whist unwinding the stackframe and is like a subroutine return so we use 14552da443eSMark Rutland * the PC. 14652da443eSMark Rutland */ 14752da443eSMark Rutland static int callchain_trace(struct stackframe *frame, void *data) 14852da443eSMark Rutland { 14952da443eSMark Rutland struct perf_callchain_entry *entry = data; 15052da443eSMark Rutland perf_callchain_store(entry, frame->pc); 15152da443eSMark Rutland return 0; 15252da443eSMark Rutland } 15352da443eSMark Rutland 15452da443eSMark Rutland void perf_callchain_kernel(struct perf_callchain_entry *entry, 15552da443eSMark Rutland struct pt_regs *regs) 15652da443eSMark Rutland { 15752da443eSMark Rutland struct stackframe frame; 15852da443eSMark Rutland 15952da443eSMark Rutland if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { 16052da443eSMark Rutland /* We don't support guest os callchain now */ 16152da443eSMark Rutland return; 16252da443eSMark Rutland } 16352da443eSMark Rutland 16452da443eSMark Rutland frame.fp = regs->regs[29]; 16552da443eSMark Rutland frame.sp = regs->sp; 16652da443eSMark Rutland frame.pc = regs->pc; 16752da443eSMark Rutland 168*fe13f95bSAKASHI Takahiro walk_stackframe(current, &frame, callchain_trace, entry); 16952da443eSMark Rutland } 17052da443eSMark Rutland 17152da443eSMark Rutland unsigned long perf_instruction_pointer(struct pt_regs *regs) 17252da443eSMark Rutland { 17352da443eSMark Rutland if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) 17452da443eSMark Rutland return perf_guest_cbs->get_guest_ip(); 17552da443eSMark Rutland 17652da443eSMark Rutland return instruction_pointer(regs); 17752da443eSMark Rutland } 17852da443eSMark Rutland 17952da443eSMark Rutland unsigned long perf_misc_flags(struct pt_regs *regs) 18052da443eSMark Rutland { 18152da443eSMark Rutland int misc = 0; 18252da443eSMark Rutland 18352da443eSMark Rutland if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { 18452da443eSMark Rutland if (perf_guest_cbs->is_user_mode()) 18552da443eSMark Rutland misc |= PERF_RECORD_MISC_GUEST_USER; 18652da443eSMark Rutland else 18752da443eSMark Rutland misc |= PERF_RECORD_MISC_GUEST_KERNEL; 18852da443eSMark Rutland } else { 18952da443eSMark Rutland if (user_mode(regs)) 19052da443eSMark Rutland misc |= PERF_RECORD_MISC_USER; 19152da443eSMark Rutland else 19252da443eSMark Rutland misc |= PERF_RECORD_MISC_KERNEL; 19352da443eSMark Rutland } 19452da443eSMark Rutland 19552da443eSMark Rutland return misc; 19652da443eSMark Rutland } 197