static unsigned long __store_trace(struct perf_callchain_entry *entry, unsigned long sp, unsigned long low, unsigned long high) { struct stack_frame *sf; struct pt_regs *regs; while (1) { sp = sp & PSW_ADDR_INSN; if (sp < low || sp > high - sizeof(*sf)) return sp; sf = (struct stack_frame *) sp; perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN); /* */ while (1) { low = sp; sp = sf->back_chain & PSW_ADDR_INSN; if (!sp) break; if (sp <= low || sp > high - sizeof(*sf)) return sp; sf = (struct stack_frame *) sp; perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN); } /* */ sp = (unsigned long) (sf + 1); if (sp <= low || sp > high - sizeof(*regs)) return sp; regs = (struct pt_regs *) sp; perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN); low = sp; sp = regs->gprs[15]; } }
struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) { int rctx; struct perf_callchain_entry *entry; entry = get_callchain_entry(&rctx); if (rctx == -1) return NULL; if (!entry) goto exit_put; entry->nr = 0; if (!user_mode(regs)) { perf_callchain_store(entry, PERF_CONTEXT_KERNEL); perf_callchain_kernel(entry, regs); if (current->mm) regs = task_pt_regs(current); else regs = NULL; } if (regs) { perf_callchain_store(entry, PERF_CONTEXT_USER); perf_callchain_user(entry, regs); } exit_put: put_callchain_entry(rctx); return entry; }
void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) { unsigned long sp, next_sp; unsigned long next_ip; unsigned long lr; long level = 0; unsigned long *fp; lr = regs->link; sp = regs->gpr[1]; perf_callchain_store(entry, regs->nip); if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD)) return; for (;;) { fp = (unsigned long *) sp; next_sp = fp[0]; if (next_sp == sp + STACK_INT_FRAME_SIZE && fp[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) { /* * This looks like an interrupt frame for an * interrupt that occurred in the kernel */ regs = (struct pt_regs *)(sp + STACK_FRAME_OVERHEAD); next_ip = regs->nip; lr = regs->link; level = 0; perf_callchain_store(entry, PERF_CONTEXT_KERNEL); } else { if (level == 0) next_ip = lr; else next_ip = fp[STACK_FRAME_LR_SAVE]; /* * We can't tell which of the first two addresses * we get are valid, but we can filter out the * obviously bogus ones here. We replace them * with 0 rather than removing them entirely so * that userspace can tell which is which. */ if ((level == 1 && next_ip == lr) || (level <= 1 && !kernel_text_address(next_ip))) next_ip = 0; ++level; } perf_callchain_store(entry, next_ip); if (!valid_next_sp(next_sp, sp)) return; sp = next_sp; } }
struct perf_callchain_entry * perf_callchain(struct perf_event *event, struct pt_regs *regs) { int rctx; struct perf_callchain_entry *entry; int kernel = !event->attr.exclude_callchain_kernel; int user = !event->attr.exclude_callchain_user; if (!kernel && !user) return NULL; entry = get_callchain_entry(&rctx); if (rctx == -1) return NULL; if (!entry) goto exit_put; entry->nr = 0; if (kernel && !user_mode(regs)) { perf_callchain_store(entry, PERF_CONTEXT_KERNEL); perf_callchain_kernel(entry, regs); } if (user) { if (!user_mode(regs)) { if (current->mm) regs = task_pt_regs(current); else regs = NULL; } if (regs) { /* * Disallow cross-task user callchains. */ if (event->ctx->task && event->ctx->task != current) goto exit_put; perf_callchain_store(entry, PERF_CONTEXT_USER); perf_callchain_user(entry, regs); } } exit_put: put_callchain_entry(rctx); return entry; }
static struct compat_frame_tail __user * compat_user_backtrace(struct compat_frame_tail __user *tail, struct perf_callchain_entry_ctx *entry) { struct compat_frame_tail buftail; unsigned long err; /* Also check accessibility of one struct frame_tail beyond */ if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) return NULL; pagefault_disable(); err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail)); pagefault_enable(); if (err) return NULL; perf_callchain_store(entry, buftail.lr); /* * Frame pointers should strictly progress back up the stack * (towards higher addresses). */ if (tail + 1 >= (struct compat_frame_tail __user *) compat_ptr(buftail.fp)) return NULL; return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1; }
void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { /* We don't support guest os callchain now */ return; } perf_callchain_store(entry, regs->pc); if (!compat_user_mode(regs)) { /* AARCH64 mode */ struct frame_tail __user *tail; tail = (struct frame_tail __user *)regs->regs[29]; while (entry->nr < entry->max_stack && tail && !((unsigned long)tail & 0xf)) tail = user_backtrace(tail, entry); } else { #ifdef CONFIG_COMPAT /* AARCH32 compat mode */ struct compat_frame_tail __user *tail; tail = (struct compat_frame_tail __user *)regs->compat_fp - 1; while ((entry->nr < entry->max_stack) && tail && !((unsigned long)tail & 0x3)) tail = compat_user_backtrace(tail, entry); #endif } }
static int __perf_callchain_kernel(void *data, unsigned long address) { struct perf_callchain_entry_ctx *entry = data; perf_callchain_store(entry, address); return 0; }
struct perf_callchain_entry * get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user, bool crosstask, bool add_mark) { struct perf_callchain_entry *entry; int rctx; entry = get_callchain_entry(&rctx); if (rctx == -1) return NULL; if (!entry) goto exit_put; entry->nr = init_nr; if (kernel && !user_mode(regs)) { if (add_mark) perf_callchain_store(entry, PERF_CONTEXT_KERNEL); perf_callchain_kernel(entry, regs); } if (user) { if (!user_mode(regs)) { if (current->mm) regs = task_pt_regs(current); else regs = NULL; } if (regs) { if (crosstask) goto exit_put; if (add_mark) perf_callchain_store(entry, PERF_CONTEXT_USER); perf_callchain_user(entry, regs); } } exit_put: put_callchain_entry(rctx); return entry; }
/* * Gets called by walk_stackframe() for every stackframe. This will be called * whist unwinding the stackframe and is like a subroutine return so we use * the PC. */ static int callchain_trace(struct stackframe *frame, void *data) { struct perf_callchain_entry_ctx *entry = data; perf_callchain_store(entry, frame->pc); return 0; }