xref: /freebsd/sys/amd64/include/profile.h (revision 45dd2eaac379e5576f745380260470204c49beac)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)profile.h	8.1 (Berkeley) 6/11/93
32  * $FreeBSD$
33  */
34 
35 #ifndef _MACHINE_PROFILE_H_
36 #define	_MACHINE_PROFILE_H_
37 
38 #ifndef _KERNEL
39 
40 #include <sys/cdefs.h>
41 
42 #define	FUNCTION_ALIGNMENT	4
43 
44 #define	_MCOUNT_DECL \
45 static void _mcount(uintfptr_t frompc, uintfptr_t selfpc) __used; \
46 static void _mcount
47 
48 #define	MCOUNT __asm("			\n\
49 	.text				\n\
50 	.p2align 4,0x90			\n\
51 	.globl	.mcount			\n\
52 	.type	.mcount,@function	\n\
53 .mcount:				\n\
54 	pushq	%rdi			\n\
55 	pushq	%rsi			\n\
56 	pushq	%rdx			\n\
57 	pushq	%rcx			\n\
58 	pushq	%r8			\n\
59 	pushq	%r9			\n\
60 	pushq	%rax			\n\
61 	movq	8(%rbp),%rdi		\n\
62 	movq	7*8(%rsp),%rsi		\n\
63 	call	_mcount			\n\
64 	popq	%rax			\n\
65 	popq	%r9			\n\
66 	popq	%r8			\n\
67 	popq	%rcx			\n\
68 	popq	%rdx			\n\
69 	popq	%rsi			\n\
70 	popq	%rdi			\n\
71 	ret				\n\
72 	.size	.mcount, . - .mcount");
73 #if 0
74 /*
75  * We could use this, except it doesn't preserve the registers that were
76  * being passed with arguments to the function that we were inserted
77  * into.  I've left it here as documentation of what the code above is
78  * supposed to do.
79  */
80 #define	MCOUNT								\
81 void									\
82 mcount()								\
83 {									\
84 	uintfptr_t selfpc, frompc;					\
85 	/*								\
86 	 * Find the return address for mcount,				\
87 	 * and the return address for mcount's caller.			\
88 	 *								\
89 	 * selfpc = pc pushed by call to mcount				\
90 	 */								\
91 	__asm("movq 8(%%rbp),%0" : "=r" (selfpc));			\
92 	/*								\
93 	 * frompc = pc pushed by call to mcount's caller.		\
94 	 * The caller's stack frame has already been built, so %rbp is	\
95 	 * the caller's frame pointer.  The caller's raddr is in the	\
96 	 * caller's frame following the caller's caller's frame pointer.\
97 	 */								\
98 	__asm("movq (%%rbp),%0" : "=r" (frompc));			\
99 	frompc = ((uintfptr_t *)frompc)[1];				\
100 	_mcount(frompc, selfpc);					\
101 }
102 #endif
103 
104 typedef	u_long	uintfptr_t;
105 
106 /*
107  * An unsigned integral type that can hold non-negative difference between
108  * function pointers.
109  */
110 typedef	u_long	fptrdiff_t;
111 
112 __BEGIN_DECLS
113 void	mcount(void) __asm(".mcount");
114 __END_DECLS
115 
116 #endif /* !_KERNEL */
117 
118 #endif /* !_MACHINE_PROFILE_H_ */
119