xref: /dragonfly/sys/cpu/x86_64/include/profile.h (revision 9d626b29)
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  * 3. 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: src/sys/amd64/include/profile.h,v 1.33 2004/01/06 20:36:21 nectar Exp $
31  */
32 
33 #ifndef _CPU_PROFILE_H_
34 #define	_CPU_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 extern int	mcount_lock;
63 #define	MCOUNT_ENTER(s)	{ s = read_rflags(); disable_intr(); \
64  			  while (!atomic_cmpset_acq_int(&mcount_lock, 0, 1)) \
65 			  	/* nothing */ ; }
66 #define	MCOUNT_EXIT(s)	{ atomic_store_rel_int(&mcount_lock, 0); \
67 			  write_rflags(s); }
68 #endif /* GUPROF */
69 
70 #else /* !_KERNEL */
71 
72 #define	FUNCTION_ALIGNMENT	4
73 
74 #define	_MCOUNT_DECL \
75 static void _mcount(uintfptr_t frompc, uintfptr_t selfpc) __used; \
76 static void _mcount
77 
78 #ifdef	__GNUC__
79 #define	MCOUNT __asm("			\n\
80 	.text				\n\
81 	.p2align 4,0x90			\n\
82 	.globl	.mcount			\n\
83 	.type	.mcount,@function	\n\
84 .mcount:				\n\
85 	pushq	%rdi			\n\
86 	pushq	%rsi			\n\
87 	pushq	%rdx			\n\
88 	pushq	%rcx			\n\
89 	pushq	%r8			\n\
90 	pushq	%r9			\n\
91 	pushq	%rax			\n\
92 	movq	8(%rbp),%rdi		\n\
93 	movq	7*8(%rsp),%rsi		\n\
94 	call	_mcount			\n\
95 	popq	%rax			\n\
96 	popq	%r9			\n\
97 	popq	%r8			\n\
98 	popq	%rcx			\n\
99 	popq	%rdx			\n\
100 	popq	%rsi			\n\
101 	popq	%rdi			\n\
102 	ret				\n\
103 	.size	.mcount, . - .mcount");
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	unsigned long	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 /* !_CPU_PROFILE_H_ */
165