xref: /freebsd/sys/dev/hwpmc/hwpmc_arm.c (revision 1d386b48)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005, Joseph Koshy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/pmc.h>
34 
35 #include <vm/vm.h>
36 #include <vm/pmap.h>
37 
38 #include <machine/cpu.h>
39 #include <machine/md_var.h>
40 #include <machine/pmc_mdep.h>
41 #include <machine/stack.h>
42 #include <machine/vmparam.h>
43 
44 /* XXX: Userland code compiled with gcc will need an heuristic
45  * to be correctly detected.
46  */
47 #ifdef __clang__
48 #define PC_OFF 1
49 #define FP_OFF 0
50 #else
51 #define PC_OFF -1
52 #define FP_OFF -3
53 #endif
54 
55 struct pmc_mdep *
56 pmc_md_initialize(void)
57 {
58 #ifdef CPU_CORTEXA
59 	if (cpu_class == CPU_CLASS_CORTEXA)
60 		return pmc_armv7_initialize();
61 #endif
62 	return NULL;
63 }
64 
65 void
66 pmc_md_finalize(struct pmc_mdep *md)
67 {
68 #ifdef CPU_CORTEXA
69 	if (cpu_class == CPU_CLASS_CORTEXA)
70 		pmc_armv7_finalize(md);
71 #endif
72 }
73 
74 int
75 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
76     struct trapframe *tf)
77 {
78 	uintptr_t pc, ra, fp;
79 	int count;
80 
81 	KASSERT(TRAPF_USERMODE(tf) == 0,("[arm,%d] not a kernel backtrace",
82 	    __LINE__));
83 
84 	pc = PMC_TRAPFRAME_TO_PC(tf);
85 	*cc++ = pc;
86 
87 	if (maxsamples <= 1)
88 		return (1);
89 
90 	fp = PMC_TRAPFRAME_TO_FP(tf);
91 	if (!PMC_IN_KERNEL(pc) || !PMC_IN_KERNEL_STACK(fp))
92 		return (1);
93 
94 	for (count = 1; count < maxsamples; count++) {
95 		/* Use saved lr as pc. */
96 		ra = fp + PC_OFF * sizeof(uintptr_t);
97 		if (!PMC_IN_KERNEL_STACK(ra))
98 			break;
99 		pc = *(uintptr_t *)ra;
100 		if (!PMC_IN_KERNEL(pc))
101 			break;
102 
103 		*cc++ = pc;
104 
105 		/* Switch to next frame up */
106 		ra = fp + FP_OFF * sizeof(uintptr_t);
107 		if (!PMC_IN_KERNEL_STACK(ra))
108 			break;
109 		fp = *(uintptr_t *)ra;
110 		if (!PMC_IN_KERNEL_STACK(fp))
111 			break;
112 	}
113 
114 	return (count);
115 }
116 
117 int
118 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
119     struct trapframe *tf)
120 {
121 	uintptr_t pc, r, oldfp, fp;
122 	int count;
123 
124 	KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
125 	    __LINE__, (void *) tf));
126 
127 	pc = PMC_TRAPFRAME_TO_PC(tf);
128 	*cc++ = pc;
129 
130 	if (maxsamples <= 1)
131 		return (1);
132 
133 	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
134 
135 	if (!PMC_IN_USERSPACE(pc) ||
136 	    !PMC_IN_USERSPACE(fp))
137 		return (1);
138 
139 	for (count = 1; count < maxsamples; count++) {
140 		/* Use saved lr as pc. */
141 		r = fp + PC_OFF * sizeof(uintptr_t);
142 		if (copyin((void *)r, &pc, sizeof(pc)) != 0)
143 			break;
144 		if (!PMC_IN_USERSPACE(pc))
145 			break;
146 
147 		*cc++ = pc;
148 
149 		/* Switch to next frame up */
150 		oldfp = fp;
151 		r = fp + FP_OFF * sizeof(uintptr_t);
152 		if (copyin((void *)r, &fp, sizeof(fp)) != 0)
153 			break;
154 		if (fp < oldfp || !PMC_IN_USERSPACE(fp))
155 			break;
156 	}
157 
158 	return (count);
159 }
160