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