profile.h revision 163738
1169691Skan/*-
2169691Skan * Copyright (c) 1992, 1993
3169691Skan *	The Regents of the University of California.  All rights reserved.
4169691Skan *
5169691Skan * Redistribution and use in source and binary forms, with or without
6169691Skan * modification, are permitted provided that the following conditions
7169691Skan * are met:
8169691Skan * 1. Redistributions of source code must retain the above copyright
9169691Skan *    notice, this list of conditions and the following disclaimer.
10169691Skan * 2. Redistributions in binary form must reproduce the above copyright
11169691Skan *    notice, this list of conditions and the following disclaimer in the
12169691Skan *    documentation and/or other materials provided with the distribution.
13169691Skan * 4. Neither the name of the University nor the names of its contributors
14169691Skan *    may be used to endorse or promote products derived from this software
15169691Skan *    without specific prior written permission.
16169691Skan *
17169691Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20169691Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27169691Skan * SUCH DAMAGE.
28169691Skan *
29169691Skan *	@(#)profile.h	8.1 (Berkeley) 6/11/93
30169691Skan * $FreeBSD: head/sys/amd64/include/profile.h 163738 2006-10-28 13:12:06Z bde $
31169691Skan */
32169691Skan
33169691Skan#ifndef _MACHINE_PROFILE_H_
34169691Skan#define	_MACHINE_PROFILE_H_
35169691Skan
36169691Skan#ifndef _SYS_CDEFS_H_
37169691Skan#error this file needs sys/cdefs.h as a prerequisite
38169691Skan#endif
39169691Skan
40169691Skan#ifdef _KERNEL
41169691Skan
42169691Skan/*
43169691Skan * Config generates something to tell the compiler to align functions on 16
44169691Skan * byte boundaries.  A strict alignment is good for keeping the tables small.
45169691Skan */
46169691Skan#define	FUNCTION_ALIGNMENT	16
47169691Skan
48169691Skan/*
49169691Skan * The kernel uses assembler stubs instead of unportable inlines.
50169691Skan * This is mainly to save a little time when profiling is not enabled,
51169691Skan * which is the usual case for the kernel.
52169691Skan */
53169691Skan#define	_MCOUNT_DECL void mcount
54169691Skan#define	MCOUNT
55169691Skan
56169691Skan#ifdef GUPROF
57169691Skan#define	MCOUNT_DECL(s)
58169691Skan#define	MCOUNT_ENTER(s)
59169691Skan#define	MCOUNT_EXIT(s)
60169691Skan#ifdef __GNUCLIKE_ASM
61169691Skan#define	MCOUNT_OVERHEAD(label)						\
62169691Skan	__asm __volatile("pushq %0; call __mcount; popq %%rcx"		\
63169691Skan			 :						\
64169691Skan			 : "i" (label)					\
65169691Skan			 : "ax", "dx", "cx", "di", "si", "r8", "r9", "memory")
66169691Skan#define	MEXITCOUNT_OVERHEAD()						\
67169691Skan	__asm __volatile("call .mexitcount; 1:"				\
68169691Skan			 : :						\
69169691Skan			 : "ax", "dx", "cx", "di", "si", "r8", "r9", "memory")
70169691Skan#define	MEXITCOUNT_OVERHEAD_GETLABEL(labelp)				\
71169691Skan	__asm __volatile("movq $1b,%0" : "=rm" (labelp))
72169691Skan#elif defined(lint)
73169691Skan#define	MCOUNT_OVERHEAD(label)
74169691Skan#define	MEXITCOUNT_OVERHEAD()
75169691Skan#define	MEXITCOUNT_OVERHEAD_GETLABEL()
76169691Skan#else
77169691Skan#error this file needs to be ported to your compiler
78169691Skan#endif /* !__GNUCLIKE_ASM */
79169691Skan#else /* !GUPROF */
80169691Skan#define	MCOUNT_DECL(s)	u_long s;
81169691Skan#ifdef SMP
82169691Skanextern int	mcount_lock;
83169691Skan#define	MCOUNT_ENTER(s)	{ s = read_rflags(); disable_intr(); \
84169691Skan 			  while (!atomic_cmpset_acq_int(&mcount_lock, 0, 1)) \
85169691Skan			  	/* nothing */ ; }
86169691Skan#define	MCOUNT_EXIT(s)	{ atomic_store_rel_int(&mcount_lock, 0); \
87169691Skan			  write_rflags(s); }
88169691Skan#else
89169691Skan#define	MCOUNT_ENTER(s)	{ s = read_rflags(); disable_intr(); }
90169691Skan#define	MCOUNT_EXIT(s)	(write_rflags(s))
91169691Skan#endif
92169691Skan#endif /* GUPROF */
93169691Skan
94169691Skanvoid bintr(void);
95169691Skanvoid btrap(void);
96169691Skanvoid eintr(void);
97169691Skanvoid user(void);
98169691Skan
99169691Skan#define	MCOUNT_FROMPC_USER(pc)					\
100169691Skan	((pc < (uintfptr_t)VM_MAXUSER_ADDRESS) ? (uintfptr_t)user : pc)
101169691Skan
102169691Skan#define	MCOUNT_FROMPC_INTR(pc)					\
103169691Skan	((pc >= (uintfptr_t)btrap && pc < (uintfptr_t)eintr) ?	\
104169691Skan	    ((pc >= (uintfptr_t)bintr) ? (uintfptr_t)bintr :	\
105169691Skan		(uintfptr_t)btrap) : ~0UL)
106169691Skan
107169691Skan#else /* !_KERNEL */
108169691Skan
109169691Skan#define	FUNCTION_ALIGNMENT	4
110169691Skan
111169691Skan#define	_MCOUNT_DECL \
112169691Skanstatic void _mcount(uintfptr_t frompc, uintfptr_t selfpc) __used; \
113169691Skanstatic void _mcount
114169691Skan
115169691Skan#ifdef __GNUCLIKE_ASM
116169691Skan#define	MCOUNT __asm("			\n\
117169691Skan	.text				\n\
118169691Skan	.p2align 4,0x90			\n\
119169691Skan	.globl	.mcount			\n\
120169691Skan	.type	.mcount,@function	\n\
121169691Skan.mcount:				\n\
122169691Skan	pushq	%rdi			\n\
123169691Skan	pushq	%rsi			\n\
124169691Skan	pushq	%rdx			\n\
125169691Skan	pushq	%rcx			\n\
126169691Skan	pushq	%r8			\n\
127169691Skan	pushq	%r9			\n\
128169691Skan	pushq	%rax			\n\
129169691Skan	movq	8(%rbp),%rdi		\n\
130169691Skan	movq	7*8(%rsp),%rsi		\n\
131169691Skan	call	_mcount			\n\
132169691Skan	popq	%rax			\n\
133169691Skan	popq	%r9			\n\
134169691Skan	popq	%r8			\n\
135169691Skan	popq	%rcx			\n\
136169691Skan	popq	%rdx			\n\
137169691Skan	popq	%rsi			\n\
138169691Skan	popq	%rdi			\n\
139169691Skan	ret				\n\
140169691Skan	.size	.mcount, . - .mcount");
141169691Skan#if 0
142169691Skan/*
143169691Skan * We could use this, except it doesn't preserve the registers that were
144169691Skan * being passed with arguments to the function that we were inserted
145169691Skan * into.  I've left it here as documentation of what the code above is
146169691Skan * supposed to do.
147169691Skan */
148169691Skan#define	MCOUNT								\
149169691Skanvoid									\
150169691Skanmcount()								\
151169691Skan{									\
152169691Skan	uintfptr_t selfpc, frompc;					\
153169691Skan	/*								\
154169691Skan	 * Find the return address for mcount,				\
155169691Skan	 * and the return address for mcount's caller.			\
156169691Skan	 *								\
157169691Skan	 * selfpc = pc pushed by call to mcount				\
158169691Skan	 */								\
159169691Skan	__asm("movq 8(%%rbp),%0" : "=r" (selfpc));			\
160169691Skan	/*								\
161169691Skan	 * frompc = pc pushed by call to mcount's caller.		\
162169691Skan	 * The caller's stack frame has already been built, so %rbp is	\
163169691Skan	 * the caller's frame pointer.  The caller's raddr is in the	\
164169691Skan	 * caller's frame following the caller's caller's frame pointer.\
165169691Skan	 */								\
166169691Skan	__asm("movq (%%rbp),%0" : "=r" (frompc));			\
167169691Skan	frompc = ((uintfptr_t *)frompc)[1];				\
168169691Skan	_mcount(frompc, selfpc);					\
169169691Skan}
170169691Skan#endif
171169691Skan#else /* !__GNUCLIKE_ASM */
172169691Skan#define	MCOUNT
173169691Skan#endif /* __GNUCLIKE_ASM */
174169691Skan
175169691Skantypedef	u_long	uintfptr_t;
176169691Skan
177169691Skan#endif /* _KERNEL */
178169691Skan
179169691Skan/*
180169691Skan * An unsigned integral type that can hold non-negative difference between
181169691Skan * function pointers.
182169691Skan */
183169691Skantypedef	u_long	fptrdiff_t;
184169691Skan
185169691Skan#ifdef _KERNEL
186169691Skan
187169691Skanvoid	mcount(uintfptr_t frompc, uintfptr_t selfpc);
188169691Skan
189169691Skan#else /* !_KERNEL */
190169691Skan
191169691Skan#include <sys/cdefs.h>
192169691Skan
193169691Skan__BEGIN_DECLS
194169691Skan#ifdef __GNUCLIKE_ASM
195169691Skanvoid	mcount(void) __asm(".mcount");
196169691Skan#endif
197169691Skan__END_DECLS
198169691Skan
199169691Skan#endif /* _KERNEL */
200169691Skan
201169691Skan#endif /* !_MACHINE_PROFILE_H_ */
202169691Skan