]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hwpmc/hwpmc_arm.c
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / sys / dev / hwpmc / hwpmc_arm.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/pmc.h>
35 #include <sys/proc.h>
36 #include <sys/systm.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
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/pmap.h>
46
47 /* XXX: Userland code compiled with gcc will need an heuristic
48  * to be correctly detected. 
49  */
50 #ifdef __clang__
51 #define PC_OFF 1
52 #define FP_OFF 0
53 #else
54 #define PC_OFF -1
55 #define FP_OFF -3
56 #endif
57
58 struct pmc_mdep *
59 pmc_md_initialize()
60 {
61 #ifdef CPU_XSCALE_IXP425
62         if (cpu_class == CPU_CLASS_XSCALE)
63                 return pmc_xscale_initialize();
64 #endif
65 #ifdef CPU_CORTEXA
66         if (cpu_class == CPU_CLASS_CORTEXA)
67                 return pmc_armv7_initialize();
68 #endif
69         return NULL;
70 }
71
72 void
73 pmc_md_finalize(struct pmc_mdep *md)
74 {
75 #ifdef CPU_XSCALE_IXP425
76         if (cpu_class == CPU_CLASS_XSCALE)
77                 pmc_xscale_finalize(md);
78         else
79                 KASSERT(0, ("[arm,%d] Unknown CPU Class 0x%x", __LINE__,
80                     cpu_class));
81 #endif
82 #ifdef CPU_CORTEXA
83         if (cpu_class == CPU_CLASS_CORTEXA)
84                 pmc_armv7_finalize(md);
85 #endif
86 }
87
88 int
89 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
90     struct trapframe *tf)
91 {
92         uintptr_t pc, r, stackstart, stackend, fp;
93         struct thread *td;
94         int count;
95
96         KASSERT(TRAPF_USERMODE(tf) == 0,("[arm,%d] not a kernel backtrace",
97             __LINE__));
98
99         td = curthread;
100         pc = PMC_TRAPFRAME_TO_PC(tf);
101         *cc++ = pc;
102
103         if (maxsamples <= 1)
104                 return (1);
105
106         stackstart = (uintptr_t) td->td_kstack;
107         stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
108         fp = PMC_TRAPFRAME_TO_FP(tf);
109
110         if (!PMC_IN_KERNEL(pc) ||
111             !PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
112                 return (1);
113
114         for (count = 1; count < maxsamples; count++) {
115                 /* Use saved lr as pc. */
116                 r = fp + PC_OFF * sizeof(uintptr_t);
117                 if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
118                         break;
119                 pc = *(uintptr_t *)r;
120                 if (!PMC_IN_KERNEL(pc))
121                         break;
122
123                 *cc++ = pc;
124
125                 /* Switch to next frame up */
126                 r = fp + FP_OFF * sizeof(uintptr_t);
127                 if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
128                         break;
129                 fp = *(uintptr_t *)r;
130                 if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
131                         break;
132         }
133
134         return (count);
135 }
136
137 int
138 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
139     struct trapframe *tf)
140 {
141         uintptr_t pc, r, oldfp, fp;
142         struct thread *td;
143         int count;
144
145         KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
146             __LINE__, (void *) tf));
147
148         td = curthread;
149         pc = PMC_TRAPFRAME_TO_PC(tf);
150         *cc++ = pc;
151
152         if (maxsamples <= 1)
153                 return (1);
154
155         oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
156
157         if (!PMC_IN_USERSPACE(pc) ||
158             !PMC_IN_USERSPACE(fp))
159                 return (1);
160
161         for (count = 1; count < maxsamples; count++) {
162                 /* Use saved lr as pc. */
163                 r = fp + PC_OFF * sizeof(uintptr_t);
164                 if (copyin((void *)r, &pc, sizeof(pc)) != 0)
165                         break;
166                 if (!PMC_IN_USERSPACE(pc))
167                         break;
168
169                 *cc++ = pc;
170
171                 /* Switch to next frame up */
172                 oldfp = fp;
173                 r = fp + FP_OFF * sizeof(uintptr_t);
174                 if (copyin((void *)r, &fp, sizeof(fp)) != 0)
175                         break;
176                 if (fp < oldfp || !PMC_IN_USERSPACE(fp))
177                         break;
178         }
179
180         return (count);
181 }