xref: /freebsd/sys/amd64/include/profile.h (revision 2357939bc239bd5334a169b62313806178dd8f30)
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$
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
63 extern 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 static __inline void _mcount
80 
81 #ifdef	__GNUC__
82 #define	MCOUNT								\
83 void									\
84 mcount()								\
85 {									\
86 	uintfptr_t selfpc, frompc;					\
87 	/*								\
88 	 * Find the return address for mcount,				\
89 	 * and the return address for mcount's caller.			\
90 	 *								\
91 	 * selfpc = pc pushed by call to mcount				\
92 	 */								\
93 	__asm("movq 8(%%rbp),%0" : "=r" (selfpc));			\
94 	/*								\
95 	 * frompc = pc pushed by call to mcount's caller.		\
96 	 * The caller's stack frame has already been built, so %ebp is	\
97 	 * the caller's frame pointer.  The caller's raddr is in the	\
98 	 * caller's frame following the caller's caller's frame pointer.\
99 	 */								\
100 	__asm("movq (%%rbp),%0" : "=r" (frompc));				\
101 	frompc = ((uintfptr_t *)frompc)[1];				\
102 	_mcount(frompc, selfpc);					\
103 }
104 #else	/* __GNUC__ */
105 #define	MCOUNT		\
106 void			\
107 mcount()		\
108 {			\
109 }
110 #endif	/* __GNUC__ */
111 
112 typedef	unsigned long	uintfptr_t;
113 
114 #endif /* _KERNEL */
115 
116 /*
117  * An unsigned integral type that can hold non-negative difference between
118  * function pointers.
119  */
120 typedef	u_int	fptrdiff_t;
121 
122 #ifdef _KERNEL
123 
124 void	mcount(uintfptr_t frompc, uintfptr_t selfpc);
125 void	kmupetext(uintfptr_t nhighpc);
126 
127 #ifdef GUPROF
128 struct gmonparam;
129 
130 void	nullfunc_loop_profiled(void);
131 void	nullfunc_profiled(void);
132 void	startguprof(struct gmonparam *p);
133 void	stopguprof(struct gmonparam *p);
134 #else
135 #define	startguprof(p)
136 #define	stopguprof(p)
137 #endif /* GUPROF */
138 
139 #else /* !_KERNEL */
140 
141 #include <sys/cdefs.h>
142 
143 __BEGIN_DECLS
144 #ifdef __GNUC__
145 void	mcount(void) __asm(".mcount");
146 #endif
147 __END_DECLS
148 
149 #endif /* _KERNEL */
150 
151 #ifdef GUPROF
152 /* XXX doesn't quite work outside kernel yet. */
153 extern int	cputime_bias;
154 
155 __BEGIN_DECLS
156 int	cputime(void);
157 void	empty_loop(void);
158 void	mexitcount(uintfptr_t selfpc);
159 void	nullfunc(void);
160 void	nullfunc_loop(void);
161 __END_DECLS
162 #endif
163 
164 #endif /* !_MACHINE_PROFILE_H_ */
165