]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/hwpmc/hwpmc_x86.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / dev / hwpmc / hwpmc_x86.c
1 /*-
2  * Copyright (c) 2005,2008 Joseph Koshy
3  * Copyright (c) 2007 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by A. Joseph Koshy under
7  * sponsorship from the FreeBSD Foundation and Google, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/pmc.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39
40 #include <machine/cpu.h>
41 #include <machine/cputypes.h>
42 #include <machine/intr_machdep.h>
43 #include <machine/apicvar.h>
44 #include <machine/pmc_mdep.h>
45 #include <machine/md_var.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/pmap.h>
50
51 /*
52  * Attempt to walk a user call stack using a too-simple algorithm.
53  * In the general case we need unwind information associated with
54  * the executable to be able to walk the user stack.
55  *
56  * We are handed a trap frame laid down at the time the PMC interrupt
57  * was taken.  If the application is using frame pointers, the saved
58  * PC value could be:
59  * a. at the beginning of a function before the stack frame is laid
60  *    down,
61  * b. just before a 'ret', after the stack frame has been taken off,
62  * c. somewhere else in the function with a valid stack frame being
63  *    present,
64  *
65  * If the application is not using frame pointers, this algorithm will
66  * fail to yield an interesting call chain.
67  *
68  * TODO: figure out a way to use unwind information.
69  */
70
71 int
72 pmc_save_user_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
73 {
74         int n;
75         uint32_t instr;
76         uintptr_t fp, oldfp, pc, r, sp;
77
78         KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
79             __LINE__, (void *) tf));
80
81         pc = PMC_TRAPFRAME_TO_PC(tf);
82         oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
83         sp = PMC_TRAPFRAME_TO_USER_SP(tf);
84
85         *cc++ = pc; n = 1;
86
87         r = fp + sizeof(uintptr_t); /* points to return address */
88
89         if (!PMC_IN_USERSPACE(pc))
90                 return (n);
91
92         if (copyin((void *) pc, &instr, sizeof(instr)) != 0)
93                 return (n);
94
95         if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
96             PMC_AT_FUNCTION_EPILOGUE_RET(instr)) { /* ret */
97                 if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
98                         return (n);
99         } else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
100                 sp += sizeof(uintptr_t);
101                 if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
102                         return (n);
103         } else if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
104             copyin((void *) fp, &fp, sizeof(fp) != 0))
105                 return (n);
106
107         for (; n < nframes;) {
108                 if (pc == 0 || !PMC_IN_USERSPACE(pc))
109                         break;
110
111                 *cc++ = pc; n++;
112
113                 if (fp < oldfp)
114                         break;
115
116                 r = fp + sizeof(uintptr_t); /* address of return address */
117                 oldfp = fp;
118
119                 if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
120                     copyin((void *) fp, &fp, sizeof(fp)) != 0)
121                         break;
122         }
123
124         return (n);
125 }
126
127 /*
128  * Walking the kernel call stack.
129  *
130  * We are handed the trap frame laid down at the time the PMC
131  * interrupt was taken.  The saved PC could be:
132  * a. in the lowlevel trap handler, meaning that there isn't a C stack
133  *    to traverse,
134  * b. at the beginning of a function before the stack frame is laid
135  *    down,
136  * c. just before a 'ret', after the stack frame has been taken off,
137  * d. somewhere else in a function with a valid stack frame being
138  *    present.
139  *
140  * In case (d), the previous frame pointer is at [%ebp]/[%rbp] and
141  * the return address is at [%ebp+4]/[%rbp+8].
142  *
143  * For cases (b) and (c), the return address is at [%esp]/[%rsp] and
144  * the frame pointer doesn't need to be changed when going up one
145  * level in the stack.
146  *
147  * For case (a), we check if the PC lies in low-level trap handling
148  * code, and if so we terminate our trace.
149  */
150
151 int
152 pmc_save_kernel_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
153 {
154         int n;
155         uint32_t instr;
156         uintptr_t fp, pc, r, sp, stackstart, stackend;
157         struct thread *td;
158
159         KASSERT(TRAPF_USERMODE(tf) == 0,("[x86,%d] not a kernel backtrace",
160             __LINE__));
161
162         pc = PMC_TRAPFRAME_TO_PC(tf);
163         fp = PMC_TRAPFRAME_TO_FP(tf);
164         sp = PMC_TRAPFRAME_TO_KERNEL_SP(tf);
165
166         *cc++ = pc;
167         r = fp + sizeof(uintptr_t); /* points to return address */
168
169         if ((td = curthread) == NULL)
170                 return (1);
171
172         if (nframes <= 1)
173                 return (1);
174
175         stackstart = (uintptr_t) td->td_kstack;
176         stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
177
178         if (PMC_IN_TRAP_HANDLER(pc) ||
179             !PMC_IN_KERNEL(pc) || !PMC_IN_KERNEL(r) ||
180             !PMC_IN_KERNEL_STACK(sp, stackstart, stackend) ||
181             !PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
182                 return (1);
183
184         instr = *(uint32_t *) pc;
185
186         /*
187          * Determine whether the interrupted function was in the
188          * processing of either laying down its stack frame or taking
189          * it off.
190          *
191          * If we haven't started laying down a stack frame, or are
192          * just about to return, then our caller's address is at
193          * *sp, and we don't have a frame to unwind.
194          */
195         if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
196             PMC_AT_FUNCTION_EPILOGUE_RET(instr))
197                 pc = *(uintptr_t *) sp;
198         else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
199                 /*
200                  * The code was midway through laying down a frame.
201                  * At this point sp[0] has a frame back pointer,
202                  * and the caller's address is therefore at sp[1].
203                  */
204                 sp += sizeof(uintptr_t);
205                 if (!PMC_IN_KERNEL_STACK(sp, stackstart, stackend))
206                         return (1);
207                 pc = *(uintptr_t *) sp;
208         } else {
209                 /*
210                  * Not in the function prologue or epilogue.
211                  */
212                 pc = *(uintptr_t *) r;
213                 fp = *(uintptr_t *) fp;
214         }
215
216         for (n = 1; n < nframes; n++) {
217                 *cc++ = pc;
218
219                 if (PMC_IN_TRAP_HANDLER(pc))
220                         break;
221
222                 r = fp + sizeof(uintptr_t);
223                 if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend) ||
224                     !PMC_IN_KERNEL(r))
225                         break;
226                 pc = *(uintptr_t *) r;
227                 fp = *(uintptr_t *) fp;
228         }
229
230         return (n);
231 }
232
233 /*
234  * Machine dependent initialization for x86 class platforms.
235  */
236
237 struct pmc_mdep *
238 pmc_md_initialize()
239 {
240         int i;
241         struct pmc_mdep *md;
242
243         /* determine the CPU kind */
244         if (cpu_vendor_id == CPU_VENDOR_AMD)
245                 md = pmc_amd_initialize();
246         else if (cpu_vendor_id == CPU_VENDOR_INTEL)
247                 md = pmc_intel_initialize();
248         else
249                 return (NULL);
250
251         /* disallow sampling if we do not have an LAPIC */
252         if (!lapic_enable_pmc())
253                 for (i = 1; i < md->pmd_nclass; i++)
254                         md->pmd_classdep[i].pcd_caps &= ~PMC_CAP_INTERRUPT;
255
256         return (md);
257 }
258
259 void
260 pmc_md_finalize(struct pmc_mdep *md)
261 {
262
263         lapic_disable_pmc();
264         if (cpu_vendor_id == CPU_VENDOR_AMD)
265                 pmc_amd_finalize(md);
266         else if (cpu_vendor_id == CPU_VENDOR_INTEL)
267                 pmc_intel_finalize(md);
268         else
269                 KASSERT(0, ("[x86,%d] Unknown vendor", __LINE__));
270 }