xref: /freebsd/sys/dev/hwpmc/hwpmc_x86.c (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005,2008 Joseph Koshy
5  * Copyright (c) 2007 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by A. Joseph Koshy under
9  * sponsorship from the FreeBSD Foundation and Google, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/pmc.h>
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 
44 #include <machine/cpu.h>
45 #include <machine/cputypes.h>
46 #include <machine/intr_machdep.h>	/* For x86/apicvar.h */
47 #include <machine/md_var.h>
48 #include <machine/pmc_mdep.h>
49 #include <machine/stack.h>
50 #include <machine/vmparam.h>
51 
52 #include <x86/apicvar.h>
53 
54 #include "hwpmc_soft.h"
55 
56 /*
57  * Attempt to walk a user call stack using a too-simple algorithm.
58  * In the general case we need unwind information associated with
59  * the executable to be able to walk the user stack.
60  *
61  * We are handed a trap frame laid down at the time the PMC interrupt
62  * was taken.  If the application is using frame pointers, the saved
63  * PC value could be:
64  * a. at the beginning of a function before the stack frame is laid
65  *    down,
66  * b. just before a 'ret', after the stack frame has been taken off,
67  * c. somewhere else in the function with a valid stack frame being
68  *    present,
69  *
70  * If the application is not using frame pointers, this algorithm will
71  * fail to yield an interesting call chain.
72  *
73  * TODO: figure out a way to use unwind information.
74  */
75 
76 int
77 pmc_save_user_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
78 {
79 	int n;
80 	uint32_t instr;
81 	uintptr_t fp, oldfp, pc, r, sp;
82 
83 	KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
84 	    __LINE__, (void *) tf));
85 
86 	pc = PMC_TRAPFRAME_TO_PC(tf);
87 	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
88 	sp = PMC_TRAPFRAME_TO_USER_SP(tf);
89 
90 	*cc++ = pc; n = 1;
91 
92 	r = fp + sizeof(uintptr_t); /* points to return address */
93 
94 	if (!PMC_IN_USERSPACE(pc))
95 		return (n);
96 
97 	if (copyin((void *) pc, &instr, sizeof(instr)) != 0)
98 		return (n);
99 
100 	if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
101 	    PMC_AT_FUNCTION_EPILOGUE_RET(instr)) { /* ret */
102 		if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
103 			return (n);
104 	} else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
105 		sp += sizeof(uintptr_t);
106 		if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
107 			return (n);
108 	} else if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
109 	    copyin((void *) fp, &fp, sizeof(fp)) != 0)
110 		return (n);
111 
112 	for (; n < nframes;) {
113 		if (pc == 0 || !PMC_IN_USERSPACE(pc))
114 			break;
115 
116 		*cc++ = pc; n++;
117 
118 		if (fp < oldfp)
119 			break;
120 
121 		r = fp + sizeof(uintptr_t); /* address of return address */
122 		oldfp = fp;
123 
124 		if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
125 		    copyin((void *) fp, &fp, sizeof(fp)) != 0)
126 			break;
127 	}
128 
129 	return (n);
130 }
131 
132 /*
133  * Walking the kernel call stack.
134  *
135  * We are handed the trap frame laid down at the time the PMC
136  * interrupt was taken.  The saved PC could be:
137  * a. in the lowlevel trap handler, meaning that there isn't a C stack
138  *    to traverse,
139  * b. at the beginning of a function before the stack frame is laid
140  *    down,
141  * c. just before a 'ret', after the stack frame has been taken off,
142  * d. somewhere else in a function with a valid stack frame being
143  *    present.
144  *
145  * In case (d), the previous frame pointer is at [%ebp]/[%rbp] and
146  * the return address is at [%ebp+4]/[%rbp+8].
147  *
148  * For cases (b) and (c), the return address is at [%esp]/[%rsp] and
149  * the frame pointer doesn't need to be changed when going up one
150  * level in the stack.
151  *
152  * For case (a), we check if the PC lies in low-level trap handling
153  * code, and if so we terminate our trace.
154  */
155 
156 int __nosanitizeaddress __nosanitizememory
157 pmc_save_kernel_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
158 {
159 	uintptr_t fp, pc, ra, sp;
160 	uint32_t instr;
161 	int n;
162 
163 	KASSERT(TRAPF_USERMODE(tf) == 0,("[x86,%d] not a kernel backtrace",
164 	    __LINE__));
165 
166 	pc = PMC_TRAPFRAME_TO_PC(tf);
167 	fp = PMC_TRAPFRAME_TO_FP(tf);
168 	sp = PMC_TRAPFRAME_TO_KERNEL_SP(tf);
169 
170 	*cc++ = pc;
171 	ra = fp + sizeof(uintptr_t); /* points to return address */
172 
173 	if (nframes <= 1)
174 		return (1);
175 
176 	if (PMC_IN_TRAP_HANDLER(pc) || !PMC_IN_KERNEL(pc) ||
177 	    !PMC_IN_KERNEL_STACK(ra) || !PMC_IN_KERNEL_STACK(sp) ||
178 	    !PMC_IN_KERNEL_STACK(fp))
179 		return (1);
180 
181 	instr = *(uint32_t *)pc;
182 
183 	/*
184 	 * Determine whether the interrupted function was in the
185 	 * processing of either laying down its stack frame or taking
186 	 * it off.
187 	 *
188 	 * If we haven't started laying down a stack frame, or are
189 	 * just about to return, then our caller's address is at
190 	 * *sp, and we don't have a frame to unwind.
191 	 */
192 	if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
193 	    PMC_AT_FUNCTION_EPILOGUE_RET(instr))
194 		pc = *(uintptr_t *) sp;
195 	else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
196 		/*
197 		 * The code was midway through laying down a frame.
198 		 * At this point sp[0] has a frame back pointer,
199 		 * and the caller's address is therefore at sp[1].
200 		 */
201 		sp += sizeof(uintptr_t);
202 		if (!PMC_IN_KERNEL_STACK(sp))
203 			return (1);
204 		pc = *(uintptr_t *)sp;
205 	} else {
206 		/*
207 		 * Not in the function prologue or epilogue.
208 		 */
209 		pc = *(uintptr_t *)ra;
210 		fp = *(uintptr_t *)fp;
211 	}
212 
213 	for (n = 1; n < nframes; n++) {
214 		*cc++ = pc;
215 
216 		if (PMC_IN_TRAP_HANDLER(pc))
217 			break;
218 
219 		ra = fp + sizeof(uintptr_t);
220 		if (!PMC_IN_KERNEL_STACK(fp) || !PMC_IN_KERNEL_STACK(ra))
221 			break;
222 		pc = *(uintptr_t *)ra;
223 		fp = *(uintptr_t *)fp;
224 	}
225 
226 	return (n);
227 }
228 
229 /*
230  * Machine dependent initialization for x86 class platforms.
231  */
232 struct pmc_mdep *
233 pmc_md_initialize(void)
234 {
235 	int i;
236 	struct pmc_mdep *md;
237 
238 	/* determine the CPU kind */
239 	if (cpu_vendor_id == CPU_VENDOR_AMD ||
240 	    cpu_vendor_id == CPU_VENDOR_HYGON)
241 		md = pmc_amd_initialize();
242 	else if (cpu_vendor_id == CPU_VENDOR_INTEL)
243 		md = pmc_intel_initialize();
244 	else
245 		return (NULL);
246 
247 	/* disallow sampling if we do not have an LAPIC */
248 	if (md != NULL && !lapic_enable_pmc())
249 		for (i = 0; i < md->pmd_nclass; i++) {
250 			if (i == PMC_CLASS_INDEX_SOFT)
251 				continue;
252 			md->pmd_classdep[i].pcd_caps &= ~PMC_CAP_INTERRUPT;
253 		}
254 
255 	return (md);
256 }
257 
258 void
259 pmc_md_finalize(struct pmc_mdep *md)
260 {
261 
262 	lapic_disable_pmc();
263 	if (cpu_vendor_id == CPU_VENDOR_AMD ||
264 	    cpu_vendor_id == CPU_VENDOR_HYGON)
265 		pmc_amd_finalize(md);
266 	else if (cpu_vendor_id == CPU_VENDOR_INTEL)
267 		pmc_intel_finalize(md);
268 	else
269 		KASSERT(0, ("[x86,%d] Unknown vendor", __LINE__));
270 }
271