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