xref: /linux/arch/powerpc/perf/callchain_64.c (revision ca853314e78b0a65c20b6a889a23c31f918d4aa2)
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/sigcontext.h>
15 #include <asm/ucontext.h>
16 #include <asm/vdso.h>
17 #include <asm/pte-walk.h>
18 
19 #include "callchain.h"
20 
21 /*
22  * On 64-bit we don't want to invoke hash_page on user addresses from
23  * interrupt context, so if the access faults, we read the page tables
24  * to find which page (if any) is mapped and access it directly. Radix
25  * has no need for this so it doesn't use read_user_stack_slow.
26  */
27 int read_user_stack_slow(const void __user *ptr, void *buf, int nb)
28 {
29 
30 	unsigned long addr = (unsigned long) ptr;
31 	unsigned long offset;
32 	struct page *page;
33 	void *kaddr;
34 
35 	if (get_user_page_fast_only(addr, FOLL_WRITE, &page)) {
36 		kaddr = page_address(page);
37 
38 		/* align address to page boundary */
39 		offset = addr & ~PAGE_MASK;
40 
41 		memcpy(buf, kaddr + offset, nb);
42 		put_page(page);
43 		return 0;
44 	}
45 	return -EFAULT;
46 }
47 
48 static int read_user_stack_64(const unsigned long __user *ptr, unsigned long *ret)
49 {
50 	return __read_user_stack(ptr, ret, sizeof(*ret));
51 }
52 
53 /*
54  * 64-bit user processes use the same stack frame for RT and non-RT signals.
55  */
56 struct signal_frame_64 {
57 	char		dummy[__SIGNAL_FRAMESIZE];
58 	struct ucontext	uc;
59 	unsigned long	unused[2];
60 	unsigned int	tramp[6];
61 	struct siginfo	*pinfo;
62 	void		*puc;
63 	struct siginfo	info;
64 	char		abigap[288];
65 };
66 
67 static int is_sigreturn_64_address(unsigned long nip, unsigned long fp)
68 {
69 	if (nip == fp + offsetof(struct signal_frame_64, tramp))
70 		return 1;
71 	if (current->mm->context.vdso &&
72 	    nip == VDSO64_SYMBOL(current->mm->context.vdso, sigtramp_rt64))
73 		return 1;
74 	return 0;
75 }
76 
77 /*
78  * Do some sanity checking on the signal frame pointed to by sp.
79  * We check the pinfo and puc pointers in the frame.
80  */
81 static int sane_signal_64_frame(unsigned long sp)
82 {
83 	struct signal_frame_64 __user *sf;
84 	unsigned long pinfo, puc;
85 
86 	sf = (struct signal_frame_64 __user *) sp;
87 	if (read_user_stack_64((unsigned long __user *) &sf->pinfo, &pinfo) ||
88 	    read_user_stack_64((unsigned long __user *) &sf->puc, &puc))
89 		return 0;
90 	return pinfo == (unsigned long) &sf->info &&
91 		puc == (unsigned long) &sf->uc;
92 }
93 
94 void perf_callchain_user_64(struct perf_callchain_entry_ctx *entry,
95 			    struct pt_regs *regs)
96 {
97 	unsigned long sp, next_sp;
98 	unsigned long next_ip;
99 	unsigned long lr;
100 	long level = 0;
101 	struct signal_frame_64 __user *sigframe;
102 	unsigned long __user *fp, *uregs;
103 
104 	next_ip = perf_instruction_pointer(regs);
105 	lr = regs->link;
106 	sp = regs->gpr[1];
107 	perf_callchain_store(entry, next_ip);
108 
109 	while (entry->nr < entry->max_stack) {
110 		fp = (unsigned long __user *) sp;
111 		if (invalid_user_sp(sp) || read_user_stack_64(fp, &next_sp))
112 			return;
113 		if (level > 0 && read_user_stack_64(&fp[2], &next_ip))
114 			return;
115 
116 		/*
117 		 * Note: the next_sp - sp >= signal frame size check
118 		 * is true when next_sp < sp, which can happen when
119 		 * transitioning from an alternate signal stack to the
120 		 * normal stack.
121 		 */
122 		if (next_sp - sp >= sizeof(struct signal_frame_64) &&
123 		    (is_sigreturn_64_address(next_ip, sp) ||
124 		     (level <= 1 && is_sigreturn_64_address(lr, sp))) &&
125 		    sane_signal_64_frame(sp)) {
126 			/*
127 			 * This looks like an signal frame
128 			 */
129 			sigframe = (struct signal_frame_64 __user *) sp;
130 			uregs = sigframe->uc.uc_mcontext.gp_regs;
131 			if (read_user_stack_64(&uregs[PT_NIP], &next_ip) ||
132 			    read_user_stack_64(&uregs[PT_LNK], &lr) ||
133 			    read_user_stack_64(&uregs[PT_R1], &sp))
134 				return;
135 			level = 0;
136 			perf_callchain_store_context(entry, PERF_CONTEXT_USER);
137 			perf_callchain_store(entry, next_ip);
138 			continue;
139 		}
140 
141 		if (level == 0)
142 			next_ip = lr;
143 		perf_callchain_store(entry, next_ip);
144 		++level;
145 		sp = next_sp;
146 	}
147 }
148