xref: /freebsd/sys/powerpc/include/profile.h (revision 93e779a26c651610ac6e7986d67ecc9ed2cadcbf)
1 /*-
2  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
3  * All rights reserved.
4  *
5  * Author: Chris G. Demetriou
6  *
7  * Permission to use, copy, modify and distribute this software and
8  * its documentation is hereby granted, provided that both the copyright
9  * notice and this permission notice appear in all copies of the
10  * software, derivative works or modified versions, and any portions
11  * thereof, and that both notices appear in supporting documentation.
12  *
13  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
15  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16  *
17  * Carnegie Mellon requests users of this software to return to
18  *
19  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
20  *  School of Computer Science
21  *  Carnegie Mellon University
22  *  Pittsburgh PA 15213-3890
23  *
24  * any improvements or extensions that they make and grant Carnegie the
25  * rights to redistribute these changes.
26  *
27  *	from: NetBSD: profile.h,v 1.9 1997/04/06 08:47:37 cgd Exp
28  *	from: FreeBSD: src/sys/alpha/include/profile.h,v 1.4 1999/12/29
29  * $FreeBSD$
30  */
31 
32 #ifndef _MACHINE_PROFILE_H_
33 #define	_MACHINE_PROFILE_H_
34 
35 #define	_MCOUNT_DECL	void __mcount
36 
37 #define	FUNCTION_ALIGNMENT	4
38 
39 typedef __ptrdiff_t	fptrdiff_t;
40 
41 /*
42  * The mcount trampoline macro, expanded in libc/gmon/mcount.c
43  *
44  * For PowerPC SVR4 ABI profiling, the compiler will insert
45  * a data declaration and code sequence at the start of a routine of the form
46  *
47  * .function_mc:       	.data
48  *			.align	2
49  *			.long	0
50  *			.text
51  *
52  * function:		mflr	%r0
53  *			addis	%r11,%r0, .function_mc@ha
54  *			stw	%r0,4(%r1)
55  *			addi	%r0,%r11, .function_mc@l
56  *			bl	_mcount
57  *
58  * The link register is saved in the LR save word in the caller's
59  * stack frame, r0 is set up to point to the allocated longword,
60  * and control is transferred to _mcount.
61  *
62  * On return from _mcount, the routine should function as it would
63  * with no profiling so _mcount must restore register state to that upon
64  * entry. Any routine called by the _mcount trampoline will save
65  * callee-save registers, so _mcount must make sure it saves volatile
66  * registers that may have state after it returns i.e. parameter registers.
67  *
68  * The FreeBSD libc mcount routine ignores the r0 longword pointer, but
69  * instead requires as parameters the current PC and called PC. The current
70  * PC is obtained from the link register, as a result of "bl _mcount" in
71  * the stub, while the caller's PC is obtained from the LR save word.
72  *
73  * On return from libc mcount, the return is done indirectly with the
74  * ctr register rather than the link register, to allow the link register
75  * to be restored to what it was on entry to the profiled routine.
76  */
77 
78 #if defined(__powerpc64__)
79 
80 #if !defined(_CALL_ELF) || _CALL_ELF == 1
81 #define MCOUNT_PREAMBLE \
82 	"	.align	2			\n" \
83 	"	.globl	_mcount			\n" \
84 	"	.section \".opd\",\"aw\"	\n" \
85 	"	.align	3			\n" \
86 	"_mcount:				\n" \
87 	"	.quad .L._mcount,.TOC.@tocbase,0\n" \
88 	"	.previous			\n" \
89 	"	.size   _mcount,24		\n" \
90 	"	.type	_mcount,@function	\n" \
91 	"	.align	4			\n" \
92 	".L._mcount:				\n"
93 #else
94 #define MCOUNT_PREAMBLE \
95 	"	.globl	_mcount			\n" \
96 	"	.type	_mcount,@function	\n" \
97 	"	.align	4			\n" \
98 	"_mcount:				\n"
99 #endif
100 
101 #define	MCOUNT					\
102 __asm(	MCOUNT_PREAMBLE \
103 	"	stdu	%r1,-(288+128)(%r1)	\n" \
104 	"	std	%r3,48(%r1)		\n" \
105 	"	std	%r4,56(%r1)		\n" \
106 	"	std	%r5,64(%r1)		\n" \
107 	"	std	%r6,72(%r1)		\n" \
108 	"	std	%r7,80(%r1)		\n" \
109 	"	std	%r8,88(%r1)		\n" \
110 	"	std	%r9,96(%r1)		\n" \
111 	"	std	%r10,104(%r1)		\n" \
112 	"	mflr	%r4			\n" \
113 	"	std	%r4,112(%r1)		\n" \
114 	"	ld	%r3,0(%r1)		\n" \
115 	"	ld	%r3,0(%r3)		\n" \
116 	"	ld	%r3,16(%r3)		\n" \
117 	"	bl	__mcount		\n" \
118 	"	nop				\n" \
119 	"	ld	%r4,112(%r1)		\n" \
120 	"	mtlr	%r4			\n" \
121 	"	ld	%r3,48(%r1)		\n" \
122 	"	ld	%r4,56(%r1)		\n" \
123 	"	ld	%r5,64(%r1)		\n" \
124 	"	ld	%r6,72(%r1)		\n" \
125 	"	ld	%r7,80(%r1)		\n" \
126 	"	ld	%r8,88(%r1)		\n" \
127 	"	ld	%r9,96(%r1)		\n" \
128 	"	ld	%r10,104(%r1)		\n" \
129 	"	addi	%r1,%r1,(288+128)	\n" \
130 	"	blr				\n");
131 #else
132 
133 #ifdef PIC
134 #define _PLT "@plt"
135 #else
136 #define _PLT
137 #endif
138 
139 #define	MCOUNT					\
140 __asm(	"	.globl	_mcount			\n" \
141 	"	.type	_mcount,@function	\n" \
142 	"	.align	4			\n" \
143 	"_mcount:				\n" \
144 	"	stwu	%r1,-64(%r1)		\n" \
145 	"	stw	%r3,16(%r1)		\n" \
146 	"	stw	%r4,20(%r1)		\n" \
147 	"	stw	%r5,24(%r1)		\n" \
148 	"	stw	%r6,28(%r1)		\n" \
149 	"	stw	%r7,32(%r1)		\n" \
150 	"	stw	%r8,36(%r1)		\n" \
151 	"	stw	%r9,40(%r1)		\n" \
152 	"	stw	%r10,44(%r1)		\n" \
153 	"	mflr	%r4			\n" \
154 	"	stw	%r4,48(%r1)		\n" \
155 	"	lwz	%r3,68(%r1)		\n" \
156 	"	bl	__mcount" _PLT "	\n" \
157 	"	lwz	%r3,68(%r1)		\n" \
158 	"	mtlr	%r3			\n" \
159 	"	lwz	%r4,48(%r1)		\n" \
160 	"	mtctr	%r4			\n" \
161 	"	lwz	%r3,16(%r1)		\n" \
162 	"	lwz	%r4,20(%r1)		\n" \
163 	"	lwz	%r5,24(%r1)		\n" \
164 	"	lwz	%r6,28(%r1)		\n" \
165 	"	lwz	%r7,32(%r1)		\n" \
166 	"	lwz	%r8,36(%r1)		\n" \
167 	"	lwz	%r9,40(%r1)		\n" \
168 	"	lwz	%r10,44(%r1)		\n" \
169 	"	addi	%r1,%r1,64		\n" \
170 	"	bctr				\n" \
171 	"_mcount_end:				\n" \
172 	"	.size	_mcount,_mcount_end-_mcount");
173 #endif
174 
175 #ifdef _KERNEL
176 #define	MCOUNT_ENTER(s)		s = intr_disable()
177 #define	MCOUNT_EXIT(s)		intr_restore(s)
178 #define	MCOUNT_DECL(s)		register_t s;
179 
180 #ifndef COMPILING_LINT
181 #ifdef AIM
182 #include <machine/trap.h>
183 #define	__PROFILE_VECTOR_BASE	EXC_RST
184 #define	__PROFILE_VECTOR_TOP	(EXC_LAST + 0x100)
185 #endif	/* AIM */
186 #if defined(BOOKE)
187 extern char interrupt_vector_base[];
188 extern char interrupt_vector_top[];
189 #define	__PROFILE_VECTOR_BASE	(uintfptr_t)interrupt_vector_base
190 #define	__PROFILE_VECTOR_TOP	(uintfptr_t)interrupt_vector_top
191 #endif	/* BOOKE_E500 || BOOKE_PPC4XX */
192 
193 #endif	/* !COMPILING_LINT */
194 
195 #ifndef __PROFILE_VECTOR_BASE
196 #define	__PROFILE_VECTOR_BASE	0
197 #endif
198 #ifndef __PROFILE_VECTOR_TOP
199 #define	__PROFILE_VECTOR_TOP	1
200 #endif
201 
202 static __inline void
203 powerpc_profile_interrupt(void)
204 {
205 }
206 
207 static __inline void
208 powerpc_profile_userspace(void)
209 {
210 }
211 
212 #define	MCOUNT_FROMPC_USER(pc)				\
213 	((pc < (uintfptr_t)VM_MAXUSER_ADDRESS) ?	\
214 	    (uintfptr_t)powerpc_profile_userspace : pc)
215 
216 #define	MCOUNT_FROMPC_INTR(pc)				\
217 	((pc >= __PROFILE_VECTOR_BASE &&		\
218 	  pc < __PROFILE_VECTOR_TOP) ?			\
219 	    (uintfptr_t)powerpc_profile_interrupt : ~0U)
220 
221 void __mcount(uintfptr_t frompc, uintfptr_t selfpc);
222 
223 #else	/* !_KERNEL */
224 
225 #ifdef __powerpc64__
226 typedef u_long	uintfptr_t;
227 #else
228 typedef u_int	uintfptr_t;
229 #endif
230 
231 #endif	/* _KERNEL */
232 
233 #endif /* !_MACHINE_PROFILE_H_ */
234