2 * Performance counter callchain support - powerpc architecture code
4 * Copyright © 2009 Paul Mackerras, IBM Corporation.
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.
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>
17 #include <asm/ptrace.h>
18 #include <asm/pgtable.h>
19 #include <asm/sigcontext.h>
20 #include <asm/ucontext.h>
28 * Is sp valid as the address of the next kernel stack frame after prev_sp?
29 * The next frame may be in a different stack area but should not go
30 * back down in the same stack area.
32 static int valid_next_sp(unsigned long sp
, unsigned long prev_sp
)
35 return 0; /* must be 16-byte aligned */
36 if (!validate_sp(sp
, current
, STACK_FRAME_OVERHEAD
))
38 if (sp
>= prev_sp
+ STACK_FRAME_OVERHEAD
)
41 * sp could decrease when we jump off an interrupt stack
42 * back to the regular process stack.
44 if ((sp
& ~(THREAD_SIZE
- 1)) != (prev_sp
& ~(THREAD_SIZE
- 1)))
50 perf_callchain_kernel(struct perf_callchain_entry
*entry
, struct pt_regs
*regs
)
52 unsigned long sp
, next_sp
;
53 unsigned long next_ip
;
60 perf_callchain_store(entry
, PERF_CONTEXT_KERNEL
);
61 perf_callchain_store(entry
, regs
->nip
);
63 if (!validate_sp(sp
, current
, STACK_FRAME_OVERHEAD
))
67 fp
= (unsigned long *) sp
;
70 if (next_sp
== sp
+ STACK_INT_FRAME_SIZE
&&
71 fp
[STACK_FRAME_MARKER
] == STACK_FRAME_REGS_MARKER
) {
73 * This looks like an interrupt frame for an
74 * interrupt that occurred in the kernel
76 regs
= (struct pt_regs
*)(sp
+ STACK_FRAME_OVERHEAD
);
80 perf_callchain_store(entry
, PERF_CONTEXT_KERNEL
);
86 next_ip
= fp
[STACK_FRAME_LR_SAVE
];
89 * We can't tell which of the first two addresses
90 * we get are valid, but we can filter out the
91 * obviously bogus ones here. We replace them
92 * with 0 rather than removing them entirely so
93 * that userspace can tell which is which.
95 if ((level
== 1 && next_ip
== lr
) ||
96 (level
<= 1 && !kernel_text_address(next_ip
)))
102 perf_callchain_store(entry
, next_ip
);
103 if (!valid_next_sp(next_sp
, sp
))
111 * On 64-bit we don't want to invoke hash_page on user addresses from
112 * interrupt context, so if the access faults, we read the page tables
113 * to find which page (if any) is mapped and access it directly.
115 static int read_user_stack_slow(void __user
*ptr
, void *ret
, int nb
)
120 unsigned long addr
= (unsigned long) ptr
;
121 unsigned long offset
;
125 pgdir
= current
->mm
->pgd
;
129 ptep
= find_linux_pte_or_hugepte(pgdir
, addr
, &shift
);
133 /* align address to page boundary */
134 offset
= addr
& ((1UL << shift
) - 1);
140 if (!pte_present(pte
) || !(pte_val(pte
) & _PAGE_USER
))
143 if (!page_is_ram(pfn
))
146 /* no highmem to worry about here */
147 kaddr
= pfn_to_kaddr(pfn
);
148 memcpy(ret
, kaddr
+ offset
, nb
);
152 static int read_user_stack_64(unsigned long __user
*ptr
, unsigned long *ret
)
154 if ((unsigned long)ptr
> TASK_SIZE
- sizeof(unsigned long) ||
155 ((unsigned long)ptr
& 7))
158 if (!__get_user_inatomic(*ret
, ptr
))
161 return read_user_stack_slow(ptr
, ret
, 8);
164 static int read_user_stack_32(unsigned int __user
*ptr
, unsigned int *ret
)
166 if ((unsigned long)ptr
> TASK_SIZE
- sizeof(unsigned int) ||
167 ((unsigned long)ptr
& 3))
170 if (!__get_user_inatomic(*ret
, ptr
))
173 return read_user_stack_slow(ptr
, ret
, 4);
176 static inline int valid_user_sp(unsigned long sp
, int is_64
)
178 if (!sp
|| (sp
& 7) || sp
> (is_64
? TASK_SIZE
: 0x100000000UL
) - 32)
184 * 64-bit user processes use the same stack frame for RT and non-RT signals.
186 struct signal_frame_64
{
187 char dummy
[__SIGNAL_FRAMESIZE
];
189 unsigned long unused
[2];
190 unsigned int tramp
[6];
191 struct siginfo
*pinfo
;
197 static int is_sigreturn_64_address(unsigned long nip
, unsigned long fp
)
199 if (nip
== fp
+ offsetof(struct signal_frame_64
, tramp
))
201 if (vdso64_rt_sigtramp
&& current
->mm
->context
.vdso_base
&&
202 nip
== current
->mm
->context
.vdso_base
+ vdso64_rt_sigtramp
)
208 * Do some sanity checking on the signal frame pointed to by sp.
209 * We check the pinfo and puc pointers in the frame.
211 static int sane_signal_64_frame(unsigned long sp
)
213 struct signal_frame_64 __user
*sf
;
214 unsigned long pinfo
, puc
;
216 sf
= (struct signal_frame_64 __user
*) sp
;
217 if (read_user_stack_64((unsigned long __user
*) &sf
->pinfo
, &pinfo
) ||
218 read_user_stack_64((unsigned long __user
*) &sf
->puc
, &puc
))
220 return pinfo
== (unsigned long) &sf
->info
&&
221 puc
== (unsigned long) &sf
->uc
;
224 static void perf_callchain_user_64(struct perf_callchain_entry
*entry
,
225 struct pt_regs
*regs
)
227 unsigned long sp
, next_sp
;
228 unsigned long next_ip
;
231 struct signal_frame_64 __user
*sigframe
;
232 unsigned long __user
*fp
, *uregs
;
237 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
238 perf_callchain_store(entry
, next_ip
);
241 fp
= (unsigned long __user
*) sp
;
242 if (!valid_user_sp(sp
, 1) || read_user_stack_64(fp
, &next_sp
))
244 if (level
> 0 && read_user_stack_64(&fp
[2], &next_ip
))
248 * Note: the next_sp - sp >= signal frame size check
249 * is true when next_sp < sp, which can happen when
250 * transitioning from an alternate signal stack to the
253 if (next_sp
- sp
>= sizeof(struct signal_frame_64
) &&
254 (is_sigreturn_64_address(next_ip
, sp
) ||
255 (level
<= 1 && is_sigreturn_64_address(lr
, sp
))) &&
256 sane_signal_64_frame(sp
)) {
258 * This looks like an signal frame
260 sigframe
= (struct signal_frame_64 __user
*) sp
;
261 uregs
= sigframe
->uc
.uc_mcontext
.gp_regs
;
262 if (read_user_stack_64(&uregs
[PT_NIP
], &next_ip
) ||
263 read_user_stack_64(&uregs
[PT_LNK
], &lr
) ||
264 read_user_stack_64(&uregs
[PT_R1
], &sp
))
267 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
268 perf_callchain_store(entry
, next_ip
);
274 perf_callchain_store(entry
, next_ip
);
280 static inline int current_is_64bit(void)
283 * We can't use test_thread_flag() here because we may be on an
284 * interrupt stack, and the thread flags don't get copied over
285 * from the thread_info on the main stack to the interrupt stack.
287 return !test_ti_thread_flag(task_thread_info(current
), TIF_32BIT
);
290 #else /* CONFIG_PPC64 */
292 * On 32-bit we just access the address and let hash_page create a
293 * HPTE if necessary, so there is no need to fall back to reading
294 * the page tables. Since this is called at interrupt level,
295 * do_page_fault() won't treat a DSI as a page fault.
297 static int read_user_stack_32(unsigned int __user
*ptr
, unsigned int *ret
)
299 if ((unsigned long)ptr
> TASK_SIZE
- sizeof(unsigned int) ||
300 ((unsigned long)ptr
& 3))
303 return __get_user_inatomic(*ret
, ptr
);
306 static inline void perf_callchain_user_64(struct perf_callchain_entry
*entry
,
307 struct pt_regs
*regs
)
311 static inline int current_is_64bit(void)
316 static inline int valid_user_sp(unsigned long sp
, int is_64
)
318 if (!sp
|| (sp
& 7) || sp
> TASK_SIZE
- 32)
323 #define __SIGNAL_FRAMESIZE32 __SIGNAL_FRAMESIZE
324 #define sigcontext32 sigcontext
325 #define mcontext32 mcontext
326 #define ucontext32 ucontext
327 #define compat_siginfo_t struct siginfo
329 #endif /* CONFIG_PPC64 */
332 * Layout for non-RT signal frames
334 struct signal_frame_32
{
335 char dummy
[__SIGNAL_FRAMESIZE32
];
336 struct sigcontext32 sctx
;
337 struct mcontext32 mctx
;
342 * Layout for RT signal frames
344 struct rt_signal_frame_32
{
345 char dummy
[__SIGNAL_FRAMESIZE32
+ 16];
346 compat_siginfo_t info
;
347 struct ucontext32 uc
;
351 static int is_sigreturn_32_address(unsigned int nip
, unsigned int fp
)
353 if (nip
== fp
+ offsetof(struct signal_frame_32
, mctx
.mc_pad
))
355 if (vdso32_sigtramp
&& current
->mm
->context
.vdso_base
&&
356 nip
== current
->mm
->context
.vdso_base
+ vdso32_sigtramp
)
361 static int is_rt_sigreturn_32_address(unsigned int nip
, unsigned int fp
)
363 if (nip
== fp
+ offsetof(struct rt_signal_frame_32
,
364 uc
.uc_mcontext
.mc_pad
))
366 if (vdso32_rt_sigtramp
&& current
->mm
->context
.vdso_base
&&
367 nip
== current
->mm
->context
.vdso_base
+ vdso32_rt_sigtramp
)
372 static int sane_signal_32_frame(unsigned int sp
)
374 struct signal_frame_32 __user
*sf
;
377 sf
= (struct signal_frame_32 __user
*) (unsigned long) sp
;
378 if (read_user_stack_32((unsigned int __user
*) &sf
->sctx
.regs
, ®s
))
380 return regs
== (unsigned long) &sf
->mctx
;
383 static int sane_rt_signal_32_frame(unsigned int sp
)
385 struct rt_signal_frame_32 __user
*sf
;
388 sf
= (struct rt_signal_frame_32 __user
*) (unsigned long) sp
;
389 if (read_user_stack_32((unsigned int __user
*) &sf
->uc
.uc_regs
, ®s
))
391 return regs
== (unsigned long) &sf
->uc
.uc_mcontext
;
394 static unsigned int __user
*signal_frame_32_regs(unsigned int sp
,
395 unsigned int next_sp
, unsigned int next_ip
)
397 struct mcontext32 __user
*mctx
= NULL
;
398 struct signal_frame_32 __user
*sf
;
399 struct rt_signal_frame_32 __user
*rt_sf
;
402 * Note: the next_sp - sp >= signal frame size check
403 * is true when next_sp < sp, for example, when
404 * transitioning from an alternate signal stack to the
407 if (next_sp
- sp
>= sizeof(struct signal_frame_32
) &&
408 is_sigreturn_32_address(next_ip
, sp
) &&
409 sane_signal_32_frame(sp
)) {
410 sf
= (struct signal_frame_32 __user
*) (unsigned long) sp
;
414 if (!mctx
&& next_sp
- sp
>= sizeof(struct rt_signal_frame_32
) &&
415 is_rt_sigreturn_32_address(next_ip
, sp
) &&
416 sane_rt_signal_32_frame(sp
)) {
417 rt_sf
= (struct rt_signal_frame_32 __user
*) (unsigned long) sp
;
418 mctx
= &rt_sf
->uc
.uc_mcontext
;
423 return mctx
->mc_gregs
;
426 static void perf_callchain_user_32(struct perf_callchain_entry
*entry
,
427 struct pt_regs
*regs
)
429 unsigned int sp
, next_sp
;
430 unsigned int next_ip
;
433 unsigned int __user
*fp
, *uregs
;
438 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
439 perf_callchain_store(entry
, next_ip
);
441 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
442 fp
= (unsigned int __user
*) (unsigned long) sp
;
443 if (!valid_user_sp(sp
, 0) || read_user_stack_32(fp
, &next_sp
))
445 if (level
> 0 && read_user_stack_32(&fp
[1], &next_ip
))
448 uregs
= signal_frame_32_regs(sp
, next_sp
, next_ip
);
449 if (!uregs
&& level
<= 1)
450 uregs
= signal_frame_32_regs(sp
, next_sp
, lr
);
453 * This looks like an signal frame, so restart
454 * the stack trace with the values in it.
456 if (read_user_stack_32(&uregs
[PT_NIP
], &next_ip
) ||
457 read_user_stack_32(&uregs
[PT_LNK
], &lr
) ||
458 read_user_stack_32(&uregs
[PT_R1
], &sp
))
461 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
462 perf_callchain_store(entry
, next_ip
);
468 perf_callchain_store(entry
, next_ip
);
475 perf_callchain_user(struct perf_callchain_entry
*entry
, struct pt_regs
*regs
)
477 if (current_is_64bit())
478 perf_callchain_user_64(entry
, regs
);
480 perf_callchain_user_32(entry
, regs
);