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