1f263522aSJoseph Koshy /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4f263522aSJoseph Koshy * Copyright (c) 2005, Joseph Koshy
5f263522aSJoseph Koshy * All rights reserved.
6f263522aSJoseph Koshy *
7f263522aSJoseph Koshy * Redistribution and use in source and binary forms, with or without
8f263522aSJoseph Koshy * modification, are permitted provided that the following conditions
9f263522aSJoseph Koshy * are met:
10f263522aSJoseph Koshy * 1. Redistributions of source code must retain the above copyright
11f263522aSJoseph Koshy * notice, this list of conditions and the following disclaimer.
12f263522aSJoseph Koshy * 2. Redistributions in binary form must reproduce the above copyright
13f263522aSJoseph Koshy * notice, this list of conditions and the following disclaimer in the
14f263522aSJoseph Koshy * documentation and/or other materials provided with the distribution.
15f263522aSJoseph Koshy *
16f263522aSJoseph Koshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f263522aSJoseph Koshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f263522aSJoseph Koshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f263522aSJoseph Koshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f263522aSJoseph Koshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f263522aSJoseph Koshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f263522aSJoseph Koshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f263522aSJoseph Koshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f263522aSJoseph Koshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f263522aSJoseph Koshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f263522aSJoseph Koshy * SUCH DAMAGE.
27f263522aSJoseph Koshy *
28f263522aSJoseph Koshy */
29f263522aSJoseph Koshy
30f263522aSJoseph Koshy #include <sys/param.h>
310ce207d2SRui Paulo #include <sys/systm.h>
32aba91805SMitchell Horne #include <sys/pmc.h>
33aba91805SMitchell Horne
34aba91805SMitchell Horne #include <vm/vm.h>
35aba91805SMitchell Horne #include <vm/pmap.h>
36f263522aSJoseph Koshy
37790fb4edSFabien Thomas #include <machine/cpu.h>
380ce207d2SRui Paulo #include <machine/md_var.h>
39790fb4edSFabien Thomas #include <machine/pmc_mdep.h>
400ede88a4SBjoern A. Zeeb #include <machine/stack.h>
41aba91805SMitchell Horne #include <machine/vmparam.h>
42f263522aSJoseph Koshy
43d42aefeeSFabien Thomas /* XXX: Userland code compiled with gcc will need an heuristic
44d42aefeeSFabien Thomas * to be correctly detected.
45d42aefeeSFabien Thomas */
46d42aefeeSFabien Thomas #ifdef __clang__
47d42aefeeSFabien Thomas #define PC_OFF 1
48d42aefeeSFabien Thomas #define FP_OFF 0
49d42aefeeSFabien Thomas #else
50d42aefeeSFabien Thomas #define PC_OFF -1
51d42aefeeSFabien Thomas #define FP_OFF -3
52d42aefeeSFabien Thomas #endif
53d42aefeeSFabien Thomas
54f263522aSJoseph Koshy struct pmc_mdep *
pmc_md_initialize(void)5505cef747SAndrew Turner pmc_md_initialize(void)
56f263522aSJoseph Koshy {
576411d14dSRuslan Bukin #ifdef CPU_CORTEXA
586411d14dSRuslan Bukin if (cpu_class == CPU_CLASS_CORTEXA)
596411d14dSRuslan Bukin return pmc_armv7_initialize();
604cc50ab4SFabien Thomas #endif
61f263522aSJoseph Koshy return NULL;
62f263522aSJoseph Koshy }
639596916cSJoseph Koshy
64c85e8dcfSJoseph Koshy void
pmc_md_finalize(struct pmc_mdep * md)65c85e8dcfSJoseph Koshy pmc_md_finalize(struct pmc_mdep *md)
66c85e8dcfSJoseph Koshy {
676411d14dSRuslan Bukin #ifdef CPU_CORTEXA
686411d14dSRuslan Bukin if (cpu_class == CPU_CLASS_CORTEXA)
696411d14dSRuslan Bukin pmc_armv7_finalize(md);
706411d14dSRuslan Bukin #endif
714cc50ab4SFabien Thomas }
724cc50ab4SFabien Thomas
739596916cSJoseph Koshy int
pmc_save_kernel_callchain(uintptr_t * cc,int maxsamples,struct trapframe * tf)749596916cSJoseph Koshy pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
759596916cSJoseph Koshy struct trapframe *tf)
769596916cSJoseph Koshy {
77aba91805SMitchell Horne uintptr_t pc, ra, fp;
78790fb4edSFabien Thomas int count;
794cc50ab4SFabien Thomas
80790fb4edSFabien Thomas KASSERT(TRAPF_USERMODE(tf) == 0,("[arm,%d] not a kernel backtrace",
81790fb4edSFabien Thomas __LINE__));
82790fb4edSFabien Thomas
83790fb4edSFabien Thomas pc = PMC_TRAPFRAME_TO_PC(tf);
84790fb4edSFabien Thomas *cc++ = pc;
85790fb4edSFabien Thomas
86790fb4edSFabien Thomas if (maxsamples <= 1)
87790fb4edSFabien Thomas return (1);
88790fb4edSFabien Thomas
89790fb4edSFabien Thomas fp = PMC_TRAPFRAME_TO_FP(tf);
90aba91805SMitchell Horne if (!PMC_IN_KERNEL(pc) || !PMC_IN_KERNEL_STACK(fp))
91790fb4edSFabien Thomas return (1);
92790fb4edSFabien Thomas
93790fb4edSFabien Thomas for (count = 1; count < maxsamples; count++) {
94790fb4edSFabien Thomas /* Use saved lr as pc. */
95aba91805SMitchell Horne ra = fp + PC_OFF * sizeof(uintptr_t);
96aba91805SMitchell Horne if (!PMC_IN_KERNEL_STACK(ra))
97790fb4edSFabien Thomas break;
98aba91805SMitchell Horne pc = *(uintptr_t *)ra;
99790fb4edSFabien Thomas if (!PMC_IN_KERNEL(pc))
100790fb4edSFabien Thomas break;
101790fb4edSFabien Thomas
102790fb4edSFabien Thomas *cc++ = pc;
103790fb4edSFabien Thomas
104790fb4edSFabien Thomas /* Switch to next frame up */
105aba91805SMitchell Horne ra = fp + FP_OFF * sizeof(uintptr_t);
106aba91805SMitchell Horne if (!PMC_IN_KERNEL_STACK(ra))
107790fb4edSFabien Thomas break;
108aba91805SMitchell Horne fp = *(uintptr_t *)ra;
109aba91805SMitchell Horne if (!PMC_IN_KERNEL_STACK(fp))
110790fb4edSFabien Thomas break;
111790fb4edSFabien Thomas }
112790fb4edSFabien Thomas
113790fb4edSFabien Thomas return (count);
1149596916cSJoseph Koshy }
1159596916cSJoseph Koshy
1169596916cSJoseph Koshy int
pmc_save_user_callchain(uintptr_t * cc,int maxsamples,struct trapframe * tf)1179596916cSJoseph Koshy pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
1189596916cSJoseph Koshy struct trapframe *tf)
1199596916cSJoseph Koshy {
120790fb4edSFabien Thomas uintptr_t pc, r, oldfp, fp;
121790fb4edSFabien Thomas int count;
1224cc50ab4SFabien Thomas
123790fb4edSFabien Thomas KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
124790fb4edSFabien Thomas __LINE__, (void *) tf));
125790fb4edSFabien Thomas
126790fb4edSFabien Thomas pc = PMC_TRAPFRAME_TO_PC(tf);
127790fb4edSFabien Thomas *cc++ = pc;
128790fb4edSFabien Thomas
129790fb4edSFabien Thomas if (maxsamples <= 1)
130790fb4edSFabien Thomas return (1);
131790fb4edSFabien Thomas
132790fb4edSFabien Thomas oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
133790fb4edSFabien Thomas
134790fb4edSFabien Thomas if (!PMC_IN_USERSPACE(pc) ||
135790fb4edSFabien Thomas !PMC_IN_USERSPACE(fp))
136790fb4edSFabien Thomas return (1);
137790fb4edSFabien Thomas
138790fb4edSFabien Thomas for (count = 1; count < maxsamples; count++) {
139790fb4edSFabien Thomas /* Use saved lr as pc. */
140d42aefeeSFabien Thomas r = fp + PC_OFF * sizeof(uintptr_t);
141790fb4edSFabien Thomas if (copyin((void *)r, &pc, sizeof(pc)) != 0)
142790fb4edSFabien Thomas break;
143790fb4edSFabien Thomas if (!PMC_IN_USERSPACE(pc))
144790fb4edSFabien Thomas break;
145790fb4edSFabien Thomas
146790fb4edSFabien Thomas *cc++ = pc;
147790fb4edSFabien Thomas
148790fb4edSFabien Thomas /* Switch to next frame up */
149790fb4edSFabien Thomas oldfp = fp;
150d42aefeeSFabien Thomas r = fp + FP_OFF * sizeof(uintptr_t);
151790fb4edSFabien Thomas if (copyin((void *)r, &fp, sizeof(fp)) != 0)
152790fb4edSFabien Thomas break;
153790fb4edSFabien Thomas if (fp < oldfp || !PMC_IN_USERSPACE(fp))
154790fb4edSFabien Thomas break;
155790fb4edSFabien Thomas }
156790fb4edSFabien Thomas
157790fb4edSFabien Thomas return (count);
1589596916cSJoseph Koshy }
159