profile.h revision 129408
1/*
2 * Copyright (c) 1992, 1993
3 *	The Regents of the University of California.  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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)profile.h	8.1 (Berkeley) 6/11/93
30 * $FreeBSD: head/sys/amd64/include/profile.h 129408 2004-05-18 22:52:32Z peter $
31 */
32
33#ifndef _MACHINE_PROFILE_H_
34#define	_MACHINE_PROFILE_H_
35
36#ifdef _KERNEL
37
38/*
39 * Config generates something to tell the compiler to align functions on 16
40 * byte boundaries.  A strict alignment is good for keeping the tables small.
41 */
42#define	FUNCTION_ALIGNMENT	16
43
44/*
45 * The kernel uses assembler stubs instead of unportable inlines.
46 * This is mainly to save a little time when profiling is not enabled,
47 * which is the usual case for the kernel.
48 */
49#define	_MCOUNT_DECL void mcount
50#define	MCOUNT
51
52#ifdef GUPROF
53#define	CALIB_SCALE	1000
54#define	KCOUNT(p,index)	((p)->kcount[(index) \
55			 / (HISTFRACTION * sizeof(HISTCOUNTER))])
56#define	MCOUNT_DECL(s)
57#define	MCOUNT_ENTER(s)
58#define	MCOUNT_EXIT(s)
59#define	PC_TO_I(p, pc)	((uintfptr_t)(pc) - (uintfptr_t)(p)->lowpc)
60#else
61#define	MCOUNT_DECL(s)	u_long s;
62#ifdef SMP
63extern int	mcount_lock;
64#define	MCOUNT_ENTER(s)	{ s = read_rflags(); disable_intr(); \
65 			  while (!atomic_cmpset_acq_int(&mcount_lock, 0, 1)) \
66			  	/* nothing */ ; }
67#define	MCOUNT_EXIT(s)	{ atomic_store_rel_int(&mcount_lock, 0); \
68			  write_rflags(s); }
69#else
70#define	MCOUNT_ENTER(s)	{ s = read_rflags(); disable_intr(); }
71#define	MCOUNT_EXIT(s)	(write_rflags(s))
72#endif
73#endif /* GUPROF */
74
75#else /* !_KERNEL */
76
77#define	FUNCTION_ALIGNMENT	4
78
79#define	_MCOUNT_DECL \
80static void _mcount(uintfptr_t frompc, uintfptr_t selfpc) __unused; \
81static void _mcount
82
83#ifdef	__GNUC__
84#define	MCOUNT __asm ("			\n\
85	.globl	.mcount			\n\
86	.type	.mcount @function	\n\
87.mcount:				\n\
88	pushq	%rbp			\n\
89	movq	%rsp, %rbp		\n\
90	pushq	%rdi			\n\
91	pushq	%rsi			\n\
92	pushq	%rdx			\n\
93	pushq	%rcx			\n\
94	pushq	%r8			\n\
95	pushq	%r9			\n\
96	movq	8(%rbp),%rsi		\n\
97	movq	(%rbp),%rdi		\n\
98	movq	8(%rdi),%rdi		\n\
99	call	_mcount			\n\
100	popq	%r9			\n\
101	popq	%r8			\n\
102	popq	%rcx			\n\
103	popq	%rdx			\n\
104	popq	%rsi			\n\
105	popq	%rdi			\n\
106	leave				\n\
107	ret				\n\
108	.size	.mcount, . - .mcount");
109#if 0
110/*
111 * We could use this, except it doesn't preserve the registers that were
112 * being passed with arguments to the function that we were inserted
113 * into.  I've left it here as documentation of what the code above is
114 * supposed to do.
115 */
116#define	MCOUNT								\
117void									\
118mcount()								\
119{									\
120	uintfptr_t selfpc, frompc;					\
121	/*								\
122	 * Find the return address for mcount,				\
123	 * and the return address for mcount's caller.			\
124	 *								\
125	 * selfpc = pc pushed by call to mcount				\
126	 */								\
127	__asm("movq 8(%%rbp),%0" : "=r" (selfpc));			\
128	/*								\
129	 * frompc = pc pushed by call to mcount's caller.		\
130	 * The caller's stack frame has already been built, so %ebp is	\
131	 * the caller's frame pointer.  The caller's raddr is in the	\
132	 * caller's frame following the caller's caller's frame pointer.\
133	 */								\
134	__asm("movq (%%rbp),%0" : "=r" (frompc));			\
135	frompc = ((uintfptr_t *)frompc)[1];				\
136	_mcount(frompc, selfpc);					\
137}
138#endif
139#else	/* __GNUC__ */
140#define	MCOUNT		\
141void			\
142mcount()		\
143{			\
144}
145#endif	/* __GNUC__ */
146
147typedef	unsigned long	uintfptr_t;
148
149#endif /* _KERNEL */
150
151/*
152 * An unsigned integral type that can hold non-negative difference between
153 * function pointers.
154 */
155typedef	u_int	fptrdiff_t;
156
157#ifdef _KERNEL
158
159void	mcount(uintfptr_t frompc, uintfptr_t selfpc);
160void	kmupetext(uintfptr_t nhighpc);
161
162#ifdef GUPROF
163struct gmonparam;
164
165void	nullfunc_loop_profiled(void);
166void	nullfunc_profiled(void);
167void	startguprof(struct gmonparam *p);
168void	stopguprof(struct gmonparam *p);
169#else
170#define	startguprof(p)
171#define	stopguprof(p)
172#endif /* GUPROF */
173
174#else /* !_KERNEL */
175
176#include <sys/cdefs.h>
177
178__BEGIN_DECLS
179#ifdef __GNUC__
180void	mcount(void) __asm(".mcount");
181#endif
182__END_DECLS
183
184#endif /* _KERNEL */
185
186#ifdef GUPROF
187/* XXX doesn't quite work outside kernel yet. */
188extern int	cputime_bias;
189
190__BEGIN_DECLS
191int	cputime(void);
192void	empty_loop(void);
193void	mexitcount(uintfptr_t selfpc);
194void	nullfunc(void);
195void	nullfunc_loop(void);
196__END_DECLS
197#endif
198
199#endif /* !_MACHINE_PROFILE_H_ */
200