2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005, Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <machine/cpu.h>
39 #include <machine/md_var.h>
40 #include <machine/pmc_mdep.h>
41 #include <machine/stack.h>
44 #include <vm/vm_param.h>
47 /* XXX: Userland code compiled with gcc will need an heuristic
48 * to be correctly detected.
62 if (cpu_class == CPU_CLASS_CORTEXA)
63 return pmc_armv7_initialize();
69 pmc_md_finalize(struct pmc_mdep *md)
72 if (cpu_class == CPU_CLASS_CORTEXA)
73 pmc_armv7_finalize(md);
78 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
81 uintptr_t pc, r, stackstart, stackend, fp;
85 KASSERT(TRAPF_USERMODE(tf) == 0,("[arm,%d] not a kernel backtrace",
89 pc = PMC_TRAPFRAME_TO_PC(tf);
95 stackstart = (uintptr_t) td->td_kstack;
96 stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
97 fp = PMC_TRAPFRAME_TO_FP(tf);
99 if (!PMC_IN_KERNEL(pc) ||
100 !PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
103 for (count = 1; count < maxsamples; count++) {
104 /* Use saved lr as pc. */
105 r = fp + PC_OFF * sizeof(uintptr_t);
106 if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
108 pc = *(uintptr_t *)r;
109 if (!PMC_IN_KERNEL(pc))
114 /* Switch to next frame up */
115 r = fp + FP_OFF * sizeof(uintptr_t);
116 if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
118 fp = *(uintptr_t *)r;
119 if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
127 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
128 struct trapframe *tf)
130 uintptr_t pc, r, oldfp, fp;
134 KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
135 __LINE__, (void *) tf));
138 pc = PMC_TRAPFRAME_TO_PC(tf);
144 oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
146 if (!PMC_IN_USERSPACE(pc) ||
147 !PMC_IN_USERSPACE(fp))
150 for (count = 1; count < maxsamples; count++) {
151 /* Use saved lr as pc. */
152 r = fp + PC_OFF * sizeof(uintptr_t);
153 if (copyin((void *)r, &pc, sizeof(pc)) != 0)
155 if (!PMC_IN_USERSPACE(pc))
160 /* Switch to next frame up */
162 r = fp + FP_OFF * sizeof(uintptr_t);
163 if (copyin((void *)r, &fp, sizeof(fp)) != 0)
165 if (fp < oldfp || !PMC_IN_USERSPACE(fp))