xref: /freebsd/sys/dev/hwpmc/hwpmc_arm.c (revision ab00ac327a66a53edaac95b536b209db3ae2cd9f)
1 /*-
2  * Copyright (c) 2005, Joseph Koshy
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/pmc.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 
36 #include <machine/cpu.h>
37 #include <machine/md_var.h>
38 #include <machine/pmc_mdep.h>
39 #include <machine/stack.h>
40 
41 #include <vm/vm.h>
42 #include <vm/vm_param.h>
43 #include <vm/pmap.h>
44 
45 /* XXX: Userland code compiled with gcc will need an heuristic
46  * to be correctly detected.
47  */
48 #ifdef __clang__
49 #define PC_OFF 1
50 #define FP_OFF 0
51 #else
52 #define PC_OFF -1
53 #define FP_OFF -3
54 #endif
55 
56 struct pmc_mdep *
57 pmc_md_initialize()
58 {
59 #ifdef CPU_XSCALE_IXP425
60 	if (cpu_class == CPU_CLASS_XSCALE)
61 		return pmc_xscale_initialize();
62 #endif
63 #ifdef CPU_CORTEXA
64 	if (cpu_class == CPU_CLASS_CORTEXA)
65 		return pmc_armv7_initialize();
66 #endif
67 	return NULL;
68 }
69 
70 void
71 pmc_md_finalize(struct pmc_mdep *md)
72 {
73 #ifdef CPU_XSCALE_IXP425
74 	if (cpu_class == CPU_CLASS_XSCALE)
75 		pmc_xscale_finalize(md);
76 	else
77 		KASSERT(0, ("[arm,%d] Unknown CPU Class 0x%x", __LINE__,
78 		    cpu_class));
79 #endif
80 #ifdef CPU_CORTEXA
81 	if (cpu_class == CPU_CLASS_CORTEXA)
82 		pmc_armv7_finalize(md);
83 #endif
84 }
85 
86 int
87 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
88     struct trapframe *tf)
89 {
90 	uintptr_t pc, r, stackstart, stackend, fp;
91 	struct thread *td;
92 	int count;
93 
94 	KASSERT(TRAPF_USERMODE(tf) == 0,("[arm,%d] not a kernel backtrace",
95 	    __LINE__));
96 
97 	td = curthread;
98 	pc = PMC_TRAPFRAME_TO_PC(tf);
99 	*cc++ = pc;
100 
101 	if (maxsamples <= 1)
102 		return (1);
103 
104 	stackstart = (uintptr_t) td->td_kstack;
105 	stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
106 	fp = PMC_TRAPFRAME_TO_FP(tf);
107 
108 	if (!PMC_IN_KERNEL(pc) ||
109 	    !PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
110 		return (1);
111 
112 	for (count = 1; count < maxsamples; count++) {
113 		/* Use saved lr as pc. */
114 		r = fp + PC_OFF * sizeof(uintptr_t);
115 		if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
116 			break;
117 		pc = *(uintptr_t *)r;
118 		if (!PMC_IN_KERNEL(pc))
119 			break;
120 
121 		*cc++ = pc;
122 
123 		/* Switch to next frame up */
124 		r = fp + FP_OFF * sizeof(uintptr_t);
125 		if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
126 			break;
127 		fp = *(uintptr_t *)r;
128 		if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
129 			break;
130 	}
131 
132 	return (count);
133 }
134 
135 int
136 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
137     struct trapframe *tf)
138 {
139 	uintptr_t pc, r, oldfp, fp;
140 	struct thread *td;
141 	int count;
142 
143 	KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
144 	    __LINE__, (void *) tf));
145 
146 	td = curthread;
147 	pc = PMC_TRAPFRAME_TO_PC(tf);
148 	*cc++ = pc;
149 
150 	if (maxsamples <= 1)
151 		return (1);
152 
153 	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
154 
155 	if (!PMC_IN_USERSPACE(pc) ||
156 	    !PMC_IN_USERSPACE(fp))
157 		return (1);
158 
159 	for (count = 1; count < maxsamples; count++) {
160 		/* Use saved lr as pc. */
161 		r = fp + PC_OFF * sizeof(uintptr_t);
162 		if (copyin((void *)r, &pc, sizeof(pc)) != 0)
163 			break;
164 		if (!PMC_IN_USERSPACE(pc))
165 			break;
166 
167 		*cc++ = pc;
168 
169 		/* Switch to next frame up */
170 		oldfp = fp;
171 		r = fp + FP_OFF * sizeof(uintptr_t);
172 		if (copyin((void *)r, &fp, sizeof(fp)) != 0)
173 			break;
174 		if (fp < oldfp || !PMC_IN_USERSPACE(fp))
175 			break;
176 	}
177 
178 	return (count);
179 }
180