callchain.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Performance counter callchain support - powerpc architecture code
  3. *
  4. * Copyright © 2009 Paul Mackerras, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/percpu.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/mm.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/sigcontext.h>
  20. #include <asm/ucontext.h>
  21. #include <asm/vdso.h>
  22. #ifdef CONFIG_PPC64
  23. #include "../kernel/ppc32.h"
  24. #endif
  25. /*
  26. * Is sp valid as the address of the next kernel stack frame after prev_sp?
  27. * The next frame may be in a different stack area but should not go
  28. * back down in the same stack area.
  29. */
  30. static int valid_next_sp(unsigned long sp, unsigned long prev_sp)
  31. {
  32. if (sp & 0xf)
  33. return 0; /* must be 16-byte aligned */
  34. if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
  35. return 0;
  36. if (sp >= prev_sp + STACK_FRAME_MIN_SIZE)
  37. return 1;
  38. /*
  39. * sp could decrease when we jump off an interrupt stack
  40. * back to the regular process stack.
  41. */
  42. if ((sp & ~(THREAD_SIZE - 1)) != (prev_sp & ~(THREAD_SIZE - 1)))
  43. return 1;
  44. return 0;
  45. }
  46. void
  47. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  48. {
  49. unsigned long sp, next_sp;
  50. unsigned long next_ip;
  51. unsigned long lr;
  52. long level = 0;
  53. unsigned long *fp;
  54. lr = regs->link;
  55. sp = regs->gpr[1];
  56. perf_callchain_store(entry, perf_instruction_pointer(regs));
  57. if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
  58. return;
  59. for (;;) {
  60. fp = (unsigned long *) sp;
  61. next_sp = fp[0];
  62. if (next_sp == sp + STACK_INT_FRAME_SIZE &&
  63. fp[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  64. /*
  65. * This looks like an interrupt frame for an
  66. * interrupt that occurred in the kernel
  67. */
  68. regs = (struct pt_regs *)(sp + STACK_FRAME_OVERHEAD);
  69. next_ip = regs->nip;
  70. lr = regs->link;
  71. level = 0;
  72. perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  73. } else {
  74. if (level == 0)
  75. next_ip = lr;
  76. else
  77. next_ip = fp[STACK_FRAME_LR_SAVE];
  78. /*
  79. * We can't tell which of the first two addresses
  80. * we get are valid, but we can filter out the
  81. * obviously bogus ones here. We replace them
  82. * with 0 rather than removing them entirely so
  83. * that userspace can tell which is which.
  84. */
  85. if ((level == 1 && next_ip == lr) ||
  86. (level <= 1 && !kernel_text_address(next_ip)))
  87. next_ip = 0;
  88. ++level;
  89. }
  90. perf_callchain_store(entry, next_ip);
  91. if (!valid_next_sp(next_sp, sp))
  92. return;
  93. sp = next_sp;
  94. }
  95. }
  96. #ifdef CONFIG_PPC64
  97. /*
  98. * On 64-bit we don't want to invoke hash_page on user addresses from
  99. * interrupt context, so if the access faults, we read the page tables
  100. * to find which page (if any) is mapped and access it directly.
  101. */
  102. static int read_user_stack_slow(void __user *ptr, void *buf, int nb)
  103. {
  104. int ret = -EFAULT;
  105. pgd_t *pgdir;
  106. pte_t *ptep, pte;
  107. unsigned shift;
  108. unsigned long addr = (unsigned long) ptr;
  109. unsigned long offset;
  110. unsigned long pfn, flags;
  111. void *kaddr;
  112. pgdir = current->mm->pgd;
  113. if (!pgdir)
  114. return -EFAULT;
  115. local_irq_save(flags);
  116. ptep = find_linux_pte_or_hugepte(pgdir, addr, NULL, &shift);
  117. if (!ptep)
  118. goto err_out;
  119. if (!shift)
  120. shift = PAGE_SHIFT;
  121. /* align address to page boundary */
  122. offset = addr & ((1UL << shift) - 1);
  123. pte = READ_ONCE(*ptep);
  124. if (!pte_present(pte) || !(pte_val(pte) & _PAGE_USER))
  125. goto err_out;
  126. pfn = pte_pfn(pte);
  127. if (!page_is_ram(pfn))
  128. goto err_out;
  129. /* no highmem to worry about here */
  130. kaddr = pfn_to_kaddr(pfn);
  131. memcpy(buf, kaddr + offset, nb);
  132. ret = 0;
  133. err_out:
  134. local_irq_restore(flags);
  135. return ret;
  136. }
  137. static int read_user_stack_64(unsigned long __user *ptr, unsigned long *ret)
  138. {
  139. if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned long) ||
  140. ((unsigned long)ptr & 7))
  141. return -EFAULT;
  142. pagefault_disable();
  143. if (!__get_user_inatomic(*ret, ptr)) {
  144. pagefault_enable();
  145. return 0;
  146. }
  147. pagefault_enable();
  148. return read_user_stack_slow(ptr, ret, 8);
  149. }
  150. static int read_user_stack_32(unsigned int __user *ptr, unsigned int *ret)
  151. {
  152. if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned int) ||
  153. ((unsigned long)ptr & 3))
  154. return -EFAULT;
  155. pagefault_disable();
  156. if (!__get_user_inatomic(*ret, ptr)) {
  157. pagefault_enable();
  158. return 0;
  159. }
  160. pagefault_enable();
  161. return read_user_stack_slow(ptr, ret, 4);
  162. }
  163. static inline int valid_user_sp(unsigned long sp, int is_64)
  164. {
  165. if (!sp || (sp & 7) || sp > (is_64 ? TASK_SIZE : 0x100000000UL) - 32)
  166. return 0;
  167. return 1;
  168. }
  169. /*
  170. * 64-bit user processes use the same stack frame for RT and non-RT signals.
  171. */
  172. struct signal_frame_64 {
  173. char dummy[__SIGNAL_FRAMESIZE];
  174. struct ucontext uc;
  175. unsigned long unused[2];
  176. unsigned int tramp[6];
  177. struct siginfo *pinfo;
  178. void *puc;
  179. struct siginfo info;
  180. char abigap[288];
  181. };
  182. static int is_sigreturn_64_address(unsigned long nip, unsigned long fp)
  183. {
  184. if (nip == fp + offsetof(struct signal_frame_64, tramp))
  185. return 1;
  186. if (vdso64_rt_sigtramp && current->mm->context.vdso_base &&
  187. nip == current->mm->context.vdso_base + vdso64_rt_sigtramp)
  188. return 1;
  189. return 0;
  190. }
  191. /*
  192. * Do some sanity checking on the signal frame pointed to by sp.
  193. * We check the pinfo and puc pointers in the frame.
  194. */
  195. static int sane_signal_64_frame(unsigned long sp)
  196. {
  197. struct signal_frame_64 __user *sf;
  198. unsigned long pinfo, puc;
  199. sf = (struct signal_frame_64 __user *) sp;
  200. if (read_user_stack_64((unsigned long __user *) &sf->pinfo, &pinfo) ||
  201. read_user_stack_64((unsigned long __user *) &sf->puc, &puc))
  202. return 0;
  203. return pinfo == (unsigned long) &sf->info &&
  204. puc == (unsigned long) &sf->uc;
  205. }
  206. static void perf_callchain_user_64(struct perf_callchain_entry *entry,
  207. struct pt_regs *regs)
  208. {
  209. unsigned long sp, next_sp;
  210. unsigned long next_ip;
  211. unsigned long lr;
  212. long level = 0;
  213. struct signal_frame_64 __user *sigframe;
  214. unsigned long __user *fp, *uregs;
  215. next_ip = perf_instruction_pointer(regs);
  216. lr = regs->link;
  217. sp = regs->gpr[1];
  218. perf_callchain_store(entry, next_ip);
  219. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  220. fp = (unsigned long __user *) sp;
  221. if (!valid_user_sp(sp, 1) || read_user_stack_64(fp, &next_sp))
  222. return;
  223. if (level > 0 && read_user_stack_64(&fp[2], &next_ip))
  224. return;
  225. /*
  226. * Note: the next_sp - sp >= signal frame size check
  227. * is true when next_sp < sp, which can happen when
  228. * transitioning from an alternate signal stack to the
  229. * normal stack.
  230. */
  231. if (next_sp - sp >= sizeof(struct signal_frame_64) &&
  232. (is_sigreturn_64_address(next_ip, sp) ||
  233. (level <= 1 && is_sigreturn_64_address(lr, sp))) &&
  234. sane_signal_64_frame(sp)) {
  235. /*
  236. * This looks like an signal frame
  237. */
  238. sigframe = (struct signal_frame_64 __user *) sp;
  239. uregs = sigframe->uc.uc_mcontext.gp_regs;
  240. if (read_user_stack_64(&uregs[PT_NIP], &next_ip) ||
  241. read_user_stack_64(&uregs[PT_LNK], &lr) ||
  242. read_user_stack_64(&uregs[PT_R1], &sp))
  243. return;
  244. level = 0;
  245. perf_callchain_store(entry, PERF_CONTEXT_USER);
  246. perf_callchain_store(entry, next_ip);
  247. continue;
  248. }
  249. if (level == 0)
  250. next_ip = lr;
  251. perf_callchain_store(entry, next_ip);
  252. ++level;
  253. sp = next_sp;
  254. }
  255. }
  256. static inline int current_is_64bit(void)
  257. {
  258. /*
  259. * We can't use test_thread_flag() here because we may be on an
  260. * interrupt stack, and the thread flags don't get copied over
  261. * from the thread_info on the main stack to the interrupt stack.
  262. */
  263. return !test_ti_thread_flag(task_thread_info(current), TIF_32BIT);
  264. }
  265. #else /* CONFIG_PPC64 */
  266. /*
  267. * On 32-bit we just access the address and let hash_page create a
  268. * HPTE if necessary, so there is no need to fall back to reading
  269. * the page tables. Since this is called at interrupt level,
  270. * do_page_fault() won't treat a DSI as a page fault.
  271. */
  272. static int read_user_stack_32(unsigned int __user *ptr, unsigned int *ret)
  273. {
  274. int rc;
  275. if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned int) ||
  276. ((unsigned long)ptr & 3))
  277. return -EFAULT;
  278. pagefault_disable();
  279. rc = __get_user_inatomic(*ret, ptr);
  280. pagefault_enable();
  281. return rc;
  282. }
  283. static inline void perf_callchain_user_64(struct perf_callchain_entry *entry,
  284. struct pt_regs *regs)
  285. {
  286. }
  287. static inline int current_is_64bit(void)
  288. {
  289. return 0;
  290. }
  291. static inline int valid_user_sp(unsigned long sp, int is_64)
  292. {
  293. if (!sp || (sp & 7) || sp > TASK_SIZE - 32)
  294. return 0;
  295. return 1;
  296. }
  297. #define __SIGNAL_FRAMESIZE32 __SIGNAL_FRAMESIZE
  298. #define sigcontext32 sigcontext
  299. #define mcontext32 mcontext
  300. #define ucontext32 ucontext
  301. #define compat_siginfo_t struct siginfo
  302. #endif /* CONFIG_PPC64 */
  303. /*
  304. * Layout for non-RT signal frames
  305. */
  306. struct signal_frame_32 {
  307. char dummy[__SIGNAL_FRAMESIZE32];
  308. struct sigcontext32 sctx;
  309. struct mcontext32 mctx;
  310. int abigap[56];
  311. };
  312. /*
  313. * Layout for RT signal frames
  314. */
  315. struct rt_signal_frame_32 {
  316. char dummy[__SIGNAL_FRAMESIZE32 + 16];
  317. compat_siginfo_t info;
  318. struct ucontext32 uc;
  319. int abigap[56];
  320. };
  321. static int is_sigreturn_32_address(unsigned int nip, unsigned int fp)
  322. {
  323. if (nip == fp + offsetof(struct signal_frame_32, mctx.mc_pad))
  324. return 1;
  325. if (vdso32_sigtramp && current->mm->context.vdso_base &&
  326. nip == current->mm->context.vdso_base + vdso32_sigtramp)
  327. return 1;
  328. return 0;
  329. }
  330. static int is_rt_sigreturn_32_address(unsigned int nip, unsigned int fp)
  331. {
  332. if (nip == fp + offsetof(struct rt_signal_frame_32,
  333. uc.uc_mcontext.mc_pad))
  334. return 1;
  335. if (vdso32_rt_sigtramp && current->mm->context.vdso_base &&
  336. nip == current->mm->context.vdso_base + vdso32_rt_sigtramp)
  337. return 1;
  338. return 0;
  339. }
  340. static int sane_signal_32_frame(unsigned int sp)
  341. {
  342. struct signal_frame_32 __user *sf;
  343. unsigned int regs;
  344. sf = (struct signal_frame_32 __user *) (unsigned long) sp;
  345. if (read_user_stack_32((unsigned int __user *) &sf->sctx.regs, &regs))
  346. return 0;
  347. return regs == (unsigned long) &sf->mctx;
  348. }
  349. static int sane_rt_signal_32_frame(unsigned int sp)
  350. {
  351. struct rt_signal_frame_32 __user *sf;
  352. unsigned int regs;
  353. sf = (struct rt_signal_frame_32 __user *) (unsigned long) sp;
  354. if (read_user_stack_32((unsigned int __user *) &sf->uc.uc_regs, &regs))
  355. return 0;
  356. return regs == (unsigned long) &sf->uc.uc_mcontext;
  357. }
  358. static unsigned int __user *signal_frame_32_regs(unsigned int sp,
  359. unsigned int next_sp, unsigned int next_ip)
  360. {
  361. struct mcontext32 __user *mctx = NULL;
  362. struct signal_frame_32 __user *sf;
  363. struct rt_signal_frame_32 __user *rt_sf;
  364. /*
  365. * Note: the next_sp - sp >= signal frame size check
  366. * is true when next_sp < sp, for example, when
  367. * transitioning from an alternate signal stack to the
  368. * normal stack.
  369. */
  370. if (next_sp - sp >= sizeof(struct signal_frame_32) &&
  371. is_sigreturn_32_address(next_ip, sp) &&
  372. sane_signal_32_frame(sp)) {
  373. sf = (struct signal_frame_32 __user *) (unsigned long) sp;
  374. mctx = &sf->mctx;
  375. }
  376. if (!mctx && next_sp - sp >= sizeof(struct rt_signal_frame_32) &&
  377. is_rt_sigreturn_32_address(next_ip, sp) &&
  378. sane_rt_signal_32_frame(sp)) {
  379. rt_sf = (struct rt_signal_frame_32 __user *) (unsigned long) sp;
  380. mctx = &rt_sf->uc.uc_mcontext;
  381. }
  382. if (!mctx)
  383. return NULL;
  384. return mctx->mc_gregs;
  385. }
  386. static void perf_callchain_user_32(struct perf_callchain_entry *entry,
  387. struct pt_regs *regs)
  388. {
  389. unsigned int sp, next_sp;
  390. unsigned int next_ip;
  391. unsigned int lr;
  392. long level = 0;
  393. unsigned int __user *fp, *uregs;
  394. next_ip = perf_instruction_pointer(regs);
  395. lr = regs->link;
  396. sp = regs->gpr[1];
  397. perf_callchain_store(entry, next_ip);
  398. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  399. fp = (unsigned int __user *) (unsigned long) sp;
  400. if (!valid_user_sp(sp, 0) || read_user_stack_32(fp, &next_sp))
  401. return;
  402. if (level > 0 && read_user_stack_32(&fp[1], &next_ip))
  403. return;
  404. uregs = signal_frame_32_regs(sp, next_sp, next_ip);
  405. if (!uregs && level <= 1)
  406. uregs = signal_frame_32_regs(sp, next_sp, lr);
  407. if (uregs) {
  408. /*
  409. * This looks like an signal frame, so restart
  410. * the stack trace with the values in it.
  411. */
  412. if (read_user_stack_32(&uregs[PT_NIP], &next_ip) ||
  413. read_user_stack_32(&uregs[PT_LNK], &lr) ||
  414. read_user_stack_32(&uregs[PT_R1], &sp))
  415. return;
  416. level = 0;
  417. perf_callchain_store(entry, PERF_CONTEXT_USER);
  418. perf_callchain_store(entry, next_ip);
  419. continue;
  420. }
  421. if (level == 0)
  422. next_ip = lr;
  423. perf_callchain_store(entry, next_ip);
  424. ++level;
  425. sp = next_sp;
  426. }
  427. }
  428. void
  429. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  430. {
  431. if (current_is_64bit())
  432. perf_callchain_user_64(entry, regs);
  433. else
  434. perf_callchain_user_32(entry, regs);
  435. }