hwpmc_x86.c revision 230636
1147191Sjkoshy/*-
2183033Sjkoshy * Copyright (c) 2005,2008 Joseph Koshy
3174395Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation
4147191Sjkoshy * All rights reserved.
5147191Sjkoshy *
6174395Sjkoshy * Portions of this software were developed by A. Joseph Koshy under
7174395Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc.
8174395Sjkoshy *
9147191Sjkoshy * Redistribution and use in source and binary forms, with or without
10147191Sjkoshy * modification, are permitted provided that the following conditions
11147191Sjkoshy * are met:
12147191Sjkoshy * 1. Redistributions of source code must retain the above copyright
13147191Sjkoshy *    notice, this list of conditions and the following disclaimer.
14147191Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
15147191Sjkoshy *    notice, this list of conditions and the following disclaimer in the
16147191Sjkoshy *    documentation and/or other materials provided with the distribution.
17147191Sjkoshy *
18147191Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19147191Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20147191Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21147191Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22147191Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23147191Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24147191Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25147191Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26147191Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27147191Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28147191Sjkoshy * SUCH DAMAGE.
29147191Sjkoshy */
30147191Sjkoshy
31147191Sjkoshy#include <sys/cdefs.h>
32147191Sjkoshy__FBSDID("$FreeBSD: head/sys/dev/hwpmc/hwpmc_x86.c 230636 2012-01-28 01:38:48Z emaste $");
33147191Sjkoshy
34147191Sjkoshy#include <sys/param.h>
35147191Sjkoshy#include <sys/bus.h>
36147191Sjkoshy#include <sys/pmc.h>
37174395Sjkoshy#include <sys/proc.h>
38147191Sjkoshy#include <sys/systm.h>
39147191Sjkoshy
40174395Sjkoshy#include <machine/cpu.h>
41185341Sjkim#include <machine/cputypes.h>
42196224Sjhb#include <machine/intr_machdep.h>
43196224Sjhb#include <machine/apicvar.h>
44147191Sjkoshy#include <machine/pmc_mdep.h>
45147191Sjkoshy#include <machine/md_var.h>
46147191Sjkoshy
47174395Sjkoshy#include <vm/vm.h>
48174395Sjkoshy#include <vm/vm_param.h>
49174395Sjkoshy#include <vm/pmap.h>
50174395Sjkoshy
51174395Sjkoshy/*
52174395Sjkoshy * Attempt to walk a user call stack using a too-simple algorithm.
53174395Sjkoshy * In the general case we need unwind information associated with
54174395Sjkoshy * the executable to be able to walk the user stack.
55174395Sjkoshy *
56174395Sjkoshy * We are handed a trap frame laid down at the time the PMC interrupt
57174395Sjkoshy * was taken.  If the application is using frame pointers, the saved
58174395Sjkoshy * PC value could be:
59174395Sjkoshy * a. at the beginning of a function before the stack frame is laid
60174395Sjkoshy *    down,
61174395Sjkoshy * b. just before a 'ret', after the stack frame has been taken off,
62174395Sjkoshy * c. somewhere else in the function with a valid stack frame being
63174395Sjkoshy *    present,
64174395Sjkoshy *
65174395Sjkoshy * If the application is not using frame pointers, this algorithm will
66174395Sjkoshy * fail to yield an interesting call chain.
67174395Sjkoshy *
68174395Sjkoshy * TODO: figure out a way to use unwind information.
69174395Sjkoshy */
70147191Sjkoshy
71174395Sjkoshyint
72174395Sjkoshypmc_save_user_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
73174395Sjkoshy{
74174395Sjkoshy	int n;
75174395Sjkoshy	uint32_t instr;
76174395Sjkoshy	uintptr_t fp, oldfp, pc, r, sp;
77174395Sjkoshy
78174395Sjkoshy	KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p",
79174395Sjkoshy	    __LINE__, (void *) tf));
80174395Sjkoshy
81174395Sjkoshy	pc = PMC_TRAPFRAME_TO_PC(tf);
82174395Sjkoshy	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
83183033Sjkoshy	sp = PMC_TRAPFRAME_TO_USER_SP(tf);
84174395Sjkoshy
85174395Sjkoshy	*cc++ = pc; n = 1;
86174395Sjkoshy
87174395Sjkoshy	r = fp + sizeof(uintptr_t); /* points to return address */
88174395Sjkoshy
89174395Sjkoshy	if (!PMC_IN_USERSPACE(pc))
90174395Sjkoshy		return (n);
91174395Sjkoshy
92174395Sjkoshy	if (copyin((void *) pc, &instr, sizeof(instr)) != 0)
93174395Sjkoshy		return (n);
94174395Sjkoshy
95174395Sjkoshy	if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
96174395Sjkoshy	    PMC_AT_FUNCTION_EPILOGUE_RET(instr)) { /* ret */
97174395Sjkoshy		if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
98174395Sjkoshy			return (n);
99174395Sjkoshy	} else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
100174395Sjkoshy		sp += sizeof(uintptr_t);
101174395Sjkoshy		if (copyin((void *) sp, &pc, sizeof(pc)) != 0)
102174395Sjkoshy			return (n);
103174395Sjkoshy	} else if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
104200001Semaste	    copyin((void *) fp, &fp, sizeof(fp)) != 0)
105174395Sjkoshy		return (n);
106174395Sjkoshy
107174395Sjkoshy	for (; n < nframes;) {
108174395Sjkoshy		if (pc == 0 || !PMC_IN_USERSPACE(pc))
109174395Sjkoshy			break;
110174395Sjkoshy
111174395Sjkoshy		*cc++ = pc; n++;
112174395Sjkoshy
113174395Sjkoshy		if (fp < oldfp)
114174395Sjkoshy			break;
115174395Sjkoshy
116174395Sjkoshy		r = fp + sizeof(uintptr_t); /* address of return address */
117174395Sjkoshy		oldfp = fp;
118174395Sjkoshy
119174395Sjkoshy		if (copyin((void *) r, &pc, sizeof(pc)) != 0 ||
120174395Sjkoshy		    copyin((void *) fp, &fp, sizeof(fp)) != 0)
121174395Sjkoshy			break;
122174395Sjkoshy	}
123174395Sjkoshy
124174395Sjkoshy	return (n);
125174395Sjkoshy}
126174395Sjkoshy
127174395Sjkoshy/*
128174395Sjkoshy * Walking the kernel call stack.
129174395Sjkoshy *
130174395Sjkoshy * We are handed the trap frame laid down at the time the PMC
131174395Sjkoshy * interrupt was taken.  The saved PC could be:
132174395Sjkoshy * a. in the lowlevel trap handler, meaning that there isn't a C stack
133174395Sjkoshy *    to traverse,
134174395Sjkoshy * b. at the beginning of a function before the stack frame is laid
135174395Sjkoshy *    down,
136174395Sjkoshy * c. just before a 'ret', after the stack frame has been taken off,
137174395Sjkoshy * d. somewhere else in a function with a valid stack frame being
138174395Sjkoshy *    present.
139174395Sjkoshy *
140174395Sjkoshy * In case (d), the previous frame pointer is at [%ebp]/[%rbp] and
141174395Sjkoshy * the return address is at [%ebp+4]/[%rbp+8].
142174395Sjkoshy *
143174395Sjkoshy * For cases (b) and (c), the return address is at [%esp]/[%rsp] and
144174395Sjkoshy * the frame pointer doesn't need to be changed when going up one
145174395Sjkoshy * level in the stack.
146174395Sjkoshy *
147174395Sjkoshy * For case (a), we check if the PC lies in low-level trap handling
148174395Sjkoshy * code, and if so we terminate our trace.
149174395Sjkoshy */
150174395Sjkoshy
151174395Sjkoshyint
152174395Sjkoshypmc_save_kernel_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
153174395Sjkoshy{
154174395Sjkoshy	int n;
155174395Sjkoshy	uint32_t instr;
156174395Sjkoshy	uintptr_t fp, pc, r, sp, stackstart, stackend;
157174395Sjkoshy	struct thread *td;
158174395Sjkoshy
159174395Sjkoshy	KASSERT(TRAPF_USERMODE(tf) == 0,("[x86,%d] not a kernel backtrace",
160174395Sjkoshy	    __LINE__));
161174395Sjkoshy
162174395Sjkoshy	pc = PMC_TRAPFRAME_TO_PC(tf);
163174395Sjkoshy	fp = PMC_TRAPFRAME_TO_FP(tf);
164183033Sjkoshy	sp = PMC_TRAPFRAME_TO_KERNEL_SP(tf);
165174395Sjkoshy
166174395Sjkoshy	*cc++ = pc;
167174395Sjkoshy	r = fp + sizeof(uintptr_t); /* points to return address */
168174395Sjkoshy
169174395Sjkoshy	if ((td = curthread) == NULL)
170174395Sjkoshy		return (1);
171174395Sjkoshy
172174395Sjkoshy	if (nframes <= 1)
173174395Sjkoshy		return (1);
174174395Sjkoshy
175174395Sjkoshy	stackstart = (uintptr_t) td->td_kstack;
176174395Sjkoshy	stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
177174395Sjkoshy
178174395Sjkoshy	if (PMC_IN_TRAP_HANDLER(pc) ||
179200060Sjkoshy	    !PMC_IN_KERNEL(pc) ||
180200060Sjkoshy	    !PMC_IN_KERNEL_STACK(r, stackstart, stackend) ||
181174395Sjkoshy	    !PMC_IN_KERNEL_STACK(sp, stackstart, stackend) ||
182174395Sjkoshy	    !PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
183174395Sjkoshy		return (1);
184174395Sjkoshy
185174395Sjkoshy	instr = *(uint32_t *) pc;
186174395Sjkoshy
187174395Sjkoshy	/*
188174395Sjkoshy	 * Determine whether the interrupted function was in the
189174395Sjkoshy	 * processing of either laying down its stack frame or taking
190174395Sjkoshy	 * it off.
191174395Sjkoshy	 *
192174395Sjkoshy	 * If we haven't started laying down a stack frame, or are
193174395Sjkoshy	 * just about to return, then our caller's address is at
194174395Sjkoshy	 * *sp, and we don't have a frame to unwind.
195174395Sjkoshy	 */
196174395Sjkoshy	if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) ||
197174395Sjkoshy	    PMC_AT_FUNCTION_EPILOGUE_RET(instr))
198174395Sjkoshy		pc = *(uintptr_t *) sp;
199174395Sjkoshy	else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) {
200174395Sjkoshy		/*
201174395Sjkoshy		 * The code was midway through laying down a frame.
202174395Sjkoshy		 * At this point sp[0] has a frame back pointer,
203174395Sjkoshy		 * and the caller's address is therefore at sp[1].
204174395Sjkoshy		 */
205174395Sjkoshy		sp += sizeof(uintptr_t);
206174395Sjkoshy		if (!PMC_IN_KERNEL_STACK(sp, stackstart, stackend))
207174395Sjkoshy			return (1);
208174395Sjkoshy		pc = *(uintptr_t *) sp;
209174395Sjkoshy	} else {
210174395Sjkoshy		/*
211174395Sjkoshy		 * Not in the function prologue or epilogue.
212174395Sjkoshy		 */
213174395Sjkoshy		pc = *(uintptr_t *) r;
214174395Sjkoshy		fp = *(uintptr_t *) fp;
215174395Sjkoshy	}
216174395Sjkoshy
217174395Sjkoshy	for (n = 1; n < nframes; n++) {
218174395Sjkoshy		*cc++ = pc;
219174395Sjkoshy
220174395Sjkoshy		if (PMC_IN_TRAP_HANDLER(pc))
221174395Sjkoshy			break;
222174395Sjkoshy
223174395Sjkoshy		r = fp + sizeof(uintptr_t);
224174395Sjkoshy		if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend) ||
225200060Sjkoshy		    !PMC_IN_KERNEL_STACK(r, stackstart, stackend))
226174395Sjkoshy			break;
227174395Sjkoshy		pc = *(uintptr_t *) r;
228174395Sjkoshy		fp = *(uintptr_t *) fp;
229174395Sjkoshy	}
230174395Sjkoshy
231174395Sjkoshy	return (n);
232174395Sjkoshy}
233174395Sjkoshy
234147191Sjkoshy/*
235147191Sjkoshy * Machine dependent initialization for x86 class platforms.
236147191Sjkoshy */
237147191Sjkoshy
238147191Sjkoshystruct pmc_mdep *
239147191Sjkoshypmc_md_initialize()
240147191Sjkoshy{
241149375Sjkoshy	int i;
242149375Sjkoshy	struct pmc_mdep *md;
243149375Sjkoshy
244147191Sjkoshy	/* determine the CPU kind */
245185341Sjkim	if (cpu_vendor_id == CPU_VENDOR_AMD)
246149375Sjkoshy		md = pmc_amd_initialize();
247185341Sjkim	else if (cpu_vendor_id == CPU_VENDOR_INTEL)
248149375Sjkoshy		md = pmc_intel_initialize();
249184802Sjkoshy	else
250196224Sjhb		return (NULL);
251149375Sjkoshy
252149375Sjkoshy	/* disallow sampling if we do not have an LAPIC */
253230636Semaste	if (md != NULL && !lapic_enable_pmc())
254149375Sjkoshy		for (i = 1; i < md->pmd_nclass; i++)
255184802Sjkoshy			md->pmd_classdep[i].pcd_caps &= ~PMC_CAP_INTERRUPT;
256149375Sjkoshy
257184802Sjkoshy	return (md);
258147191Sjkoshy}
259184802Sjkoshy
260184802Sjkoshyvoid
261184802Sjkoshypmc_md_finalize(struct pmc_mdep *md)
262184802Sjkoshy{
263196224Sjhb
264196224Sjhb	lapic_disable_pmc();
265185341Sjkim	if (cpu_vendor_id == CPU_VENDOR_AMD)
266184802Sjkoshy		pmc_amd_finalize(md);
267185341Sjkim	else if (cpu_vendor_id == CPU_VENDOR_INTEL)
268184802Sjkoshy		pmc_intel_finalize(md);
269184802Sjkoshy	else
270184802Sjkoshy		KASSERT(0, ("[x86,%d] Unknown vendor", __LINE__));
271184802Sjkoshy}
272