xref: /freebsd/sys/dev/hwpmc/hwpmc_arm.c (revision 3df058ffaf72b8715c9a5a6a4cbaf1eac1910e43)
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 struct pmc_mdep *
46 pmc_md_initialize()
47 {
48 #ifdef CPU_XSCALE_IXP425
49 	if (cpu_class == CPU_CLASS_XSCALE)
50 		return pmc_xscale_initialize();
51 #endif
52 #ifdef CPU_CORTEXA
53 	if (cpu_class == CPU_CLASS_CORTEXA)
54 		return pmc_armv7_initialize();
55 #endif
56 	return NULL;
57 }
58 
59 void
60 pmc_md_finalize(struct pmc_mdep *md)
61 {
62 #ifdef CPU_XSCALE_IXP425
63 	if (cpu_class == CPU_CLASS_XSCALE)
64 		pmc_xscale_finalize(md);
65 	else
66 		KASSERT(0, ("[arm,%d] Unknown CPU Class 0x%x", __LINE__,
67 		    cpu_class));
68 #endif
69 #ifdef CPU_CORTEXA
70 	if (cpu_class == CPU_CLASS_CORTEXA)
71 		pmc_armv7_finalize(md);
72 #endif
73 }
74 
75 int
76 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
77     struct trapframe *tf)
78 {
79 	uintptr_t pc, r, stackstart, stackend, fp;
80 	struct thread *td;
81 	int count;
82 
83 	KASSERT(TRAPF_USERMODE(tf) == 0,("[arm,%d] not a kernel backtrace",
84 	    __LINE__));
85 
86 	td = curthread;
87 	pc = PMC_TRAPFRAME_TO_PC(tf);
88 	*cc++ = pc;
89 
90 	if (maxsamples <= 1)
91 		return (1);
92 
93 	stackstart = (uintptr_t) td->td_kstack;
94 	stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
95 	fp = PMC_TRAPFRAME_TO_FP(tf);
96 
97 	if (!PMC_IN_KERNEL(pc) ||
98 	    !PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
99 		return (1);
100 
101 	for (count = 1; count < maxsamples; count++) {
102 		/* Use saved lr as pc. */
103 		r = fp - sizeof(uintptr_t);
104 		if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
105 			break;
106 		pc = *(uintptr_t *)r;
107 		if (!PMC_IN_KERNEL(pc))
108 			break;
109 
110 		*cc++ = pc;
111 
112 		/* Switch to next frame up */
113 		r = fp - 3 * sizeof(uintptr_t);
114 		if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
115 			break;
116 		fp = *(uintptr_t *)r;
117 		if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
118 			break;
119 	}
120 
121 	return (count);
122 }
123 
124 int
125 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
126     struct trapframe *tf)
127 {
128 	uintptr_t pc, r, oldfp, fp;
129 	struct thread *td;
130 	int count;
131 
132 	KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
133 	    __LINE__, (void *) tf));
134 
135 	td = curthread;
136 	pc = PMC_TRAPFRAME_TO_PC(tf);
137 	*cc++ = pc;
138 
139 	if (maxsamples <= 1)
140 		return (1);
141 
142 	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
143 
144 	if (!PMC_IN_USERSPACE(pc) ||
145 	    !PMC_IN_USERSPACE(fp))
146 		return (1);
147 
148 	for (count = 1; count < maxsamples; count++) {
149 		/* Use saved lr as pc. */
150 		r = fp - sizeof(uintptr_t);
151 		if (copyin((void *)r, &pc, sizeof(pc)) != 0)
152 			break;
153 		if (!PMC_IN_USERSPACE(pc))
154 			break;
155 
156 		*cc++ = pc;
157 
158 		/* Switch to next frame up */
159 		oldfp = fp;
160 		r = fp - 3 * sizeof(uintptr_t);
161 		if (copyin((void *)r, &fp, sizeof(fp)) != 0)
162 			break;
163 		if (fp < oldfp || !PMC_IN_USERSPACE(fp))
164 			break;
165 	}
166 
167 	return (count);
168 }
169