xref: /freebsd/sys/dev/hwpmc/hwpmc_x86.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/pmc.h>
38 
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 
42 #include <machine/cpu.h>
43 #include <machine/cputypes.h>
44 #include <machine/intr_machdep.h>	/* For x86/apicvar.h */
45 #include <machine/md_var.h>
46 #include <machine/pmc_mdep.h>
47 #include <machine/stack.h>
48 #include <machine/vmparam.h>
49 
50 #include <x86/apicvar.h>
51 
52 #include "hwpmc_soft.h"
53 
54 /*
55  * Attempt to walk a user call stack using a too-simple algorithm.
56  * In the general case we need unwind information associated with
57  * the executable to be able to walk the user stack.
58  *
59  * We are handed a trap frame laid down at the time the PMC interrupt
60  * was taken.  If the application is using frame pointers, the saved
61  * PC value could be:
62  * a. at the beginning of a function before the stack frame is laid
63  *    down,
64  * b. just before a 'ret', after the stack frame has been taken off,
65  * c. somewhere else in the function with a valid stack frame being
66  *    present,
67  *
68  * If the application is not using frame pointers, this algorithm will
69  * fail to yield an interesting call chain.
70  *
71  * TODO: figure out a way to use unwind information.
72  */
73 
74 int
75 pmc_save_user_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
76 {
77 	int n;
78 	uint32_t instr;
79 	uintptr_t fp, oldfp, pc, r, sp;
80 
81 	KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
82 	    __LINE__, (void *) tf));
83 
84 	pc = PMC_TRAPFRAME_TO_PC(tf);
85 	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
86 	sp = PMC_TRAPFRAME_TO_USER_SP(tf);
87 
88 	*cc++ = pc; n = 1;
89 
90 	r = fp + sizeof(uintptr_t); /* points to return address */
91 
92 	if (!PMC_IN_USERSPACE(pc))
93 		return (n);
94 
95 	if (copyin((void *) pc, &instr, sizeof(instr)) != 0)
96 		return (n);
97 
98 	if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
99 	    PMC_AT_FUNCTION_EPILOGUE_RET(instr)) { /* ret */
100 		if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
101 			return (n);
102 	} else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
103 		sp += sizeof(uintptr_t);
104 		if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
105 			return (n);
106 	} else if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
107 	    copyin((void *) fp, &fp, sizeof(fp)) != 0)
108 		return (n);
109 
110 	for (; n < nframes;) {
111 		if (pc == 0 || !PMC_IN_USERSPACE(pc))
112 			break;
113 
114 		*cc++ = pc; n++;
115 
116 		if (fp < oldfp)
117 			break;
118 
119 		r = fp + sizeof(uintptr_t); /* address of return address */
120 		oldfp = fp;
121 
122 		if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
123 		    copyin((void *) fp, &fp, sizeof(fp)) != 0)
124 			break;
125 	}
126 
127 	return (n);
128 }
129 
130 /*
131  * Walking the kernel call stack.
132  *
133  * We are handed the trap frame laid down at the time the PMC
134  * interrupt was taken.  The saved PC could be:
135  * a. in the lowlevel trap handler, meaning that there isn't a C stack
136  *    to traverse,
137  * b. at the beginning of a function before the stack frame is laid
138  *    down,
139  * c. just before a 'ret', after the stack frame has been taken off,
140  * d. somewhere else in a function with a valid stack frame being
141  *    present.
142  *
143  * In case (d), the previous frame pointer is at [%ebp]/[%rbp] and
144  * the return address is at [%ebp+4]/[%rbp+8].
145  *
146  * For cases (b) and (c), the return address is at [%esp]/[%rsp] and
147  * the frame pointer doesn't need to be changed when going up one
148  * level in the stack.
149  *
150  * For case (a), we check if the PC lies in low-level trap handling
151  * code, and if so we terminate our trace.
152  */
153 
154 int __nosanitizeaddress __nosanitizememory
155 pmc_save_kernel_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
156 {
157 	uintptr_t fp, pc, ra, sp;
158 	uint32_t instr;
159 	int n;
160 
161 	KASSERT(TRAPF_USERMODE(tf) == 0,("[x86,%d] not a kernel backtrace",
162 	    __LINE__));
163 
164 	pc = PMC_TRAPFRAME_TO_PC(tf);
165 	fp = PMC_TRAPFRAME_TO_FP(tf);
166 	sp = PMC_TRAPFRAME_TO_KERNEL_SP(tf);
167 
168 	*cc++ = pc;
169 	ra = fp + sizeof(uintptr_t); /* points to return address */
170 
171 	if (nframes <= 1)
172 		return (1);
173 
174 	if (PMC_IN_TRAP_HANDLER(pc) || !PMC_IN_KERNEL(pc) ||
175 	    !PMC_IN_KERNEL_STACK(ra) || !PMC_IN_KERNEL_STACK(sp) ||
176 	    !PMC_IN_KERNEL_STACK(fp))
177 		return (1);
178 
179 	instr = *(uint32_t *)pc;
180 
181 	/*
182 	 * Determine whether the interrupted function was in the
183 	 * processing of either laying down its stack frame or taking
184 	 * it off.
185 	 *
186 	 * If we haven't started laying down a stack frame, or are
187 	 * just about to return, then our caller's address is at
188 	 * *sp, and we don't have a frame to unwind.
189 	 */
190 	if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
191 	    PMC_AT_FUNCTION_EPILOGUE_RET(instr))
192 		pc = *(uintptr_t *) sp;
193 	else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
194 		/*
195 		 * The code was midway through laying down a frame.
196 		 * At this point sp[0] has a frame back pointer,
197 		 * and the caller's address is therefore at sp[1].
198 		 */
199 		sp += sizeof(uintptr_t);
200 		if (!PMC_IN_KERNEL_STACK(sp))
201 			return (1);
202 		pc = *(uintptr_t *)sp;
203 	} else {
204 		/*
205 		 * Not in the function prologue or epilogue.
206 		 */
207 		pc = *(uintptr_t *)ra;
208 		fp = *(uintptr_t *)fp;
209 	}
210 
211 	for (n = 1; n < nframes; n++) {
212 		*cc++ = pc;
213 
214 		if (PMC_IN_TRAP_HANDLER(pc))
215 			break;
216 
217 		ra = fp + sizeof(uintptr_t);
218 		if (!PMC_IN_KERNEL_STACK(fp) || !PMC_IN_KERNEL_STACK(ra))
219 			break;
220 		pc = *(uintptr_t *)ra;
221 		fp = *(uintptr_t *)fp;
222 	}
223 
224 	return (n);
225 }
226 
227 /*
228  * Machine dependent initialization for x86 class platforms.
229  */
230 struct pmc_mdep *
231 pmc_md_initialize(void)
232 {
233 	int i;
234 	struct pmc_mdep *md;
235 
236 	/* determine the CPU kind */
237 	if (cpu_vendor_id == CPU_VENDOR_AMD ||
238 	    cpu_vendor_id == CPU_VENDOR_HYGON)
239 		md = pmc_amd_initialize();
240 	else if (cpu_vendor_id == CPU_VENDOR_INTEL)
241 		md = pmc_intel_initialize();
242 	else
243 		return (NULL);
244 
245 	/* disallow sampling if we do not have an LAPIC */
246 	if (md != NULL && !lapic_enable_pmc())
247 		for (i = 0; i < md->pmd_nclass; i++) {
248 			if (i == PMC_CLASS_INDEX_SOFT)
249 				continue;
250 			md->pmd_classdep[i].pcd_caps &= ~PMC_CAP_INTERRUPT;
251 		}
252 
253 	return (md);
254 }
255 
256 void
257 pmc_md_finalize(struct pmc_mdep *md)
258 {
259 
260 	lapic_disable_pmc();
261 	if (cpu_vendor_id == CPU_VENDOR_AMD ||
262 	    cpu_vendor_id == CPU_VENDOR_HYGON)
263 		pmc_amd_finalize(md);
264 	else if (cpu_vendor_id == CPU_VENDOR_INTEL)
265 		pmc_intel_finalize(md);
266 	else
267 		KASSERT(0, ("[x86,%d] Unknown vendor", __LINE__));
268 }
269