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