1139825Simp/*-
2109481Sgrehan * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
3109481Sgrehan * All rights reserved.
4109481Sgrehan *
5109481Sgrehan * Author: Chris G. Demetriou
6109481Sgrehan *
7109481Sgrehan * Permission to use, copy, modify and distribute this software and
8109481Sgrehan * its documentation is hereby granted, provided that both the copyright
9109481Sgrehan * notice and this permission notice appear in all copies of the
10109481Sgrehan * software, derivative works or modified versions, and any portions
11109481Sgrehan * thereof, and that both notices appear in supporting documentation.
12109481Sgrehan *
13109481Sgrehan * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14109481Sgrehan * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
15109481Sgrehan * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16109481Sgrehan *
17109481Sgrehan * Carnegie Mellon requests users of this software to return to
18109481Sgrehan *
19109481Sgrehan *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
20109481Sgrehan *  School of Computer Science
21109481Sgrehan *  Carnegie Mellon University
22109481Sgrehan *  Pittsburgh PA 15213-3890
23109481Sgrehan *
24109481Sgrehan * any improvements or extensions that they make and grant Carnegie the
25109481Sgrehan * rights to redistribute these changes.
26109481Sgrehan *
27109481Sgrehan *	from: NetBSD: profile.h,v 1.9 1997/04/06 08:47:37 cgd Exp
28109481Sgrehan *	from: FreeBSD: src/sys/alpha/include/profile.h,v 1.4 1999/12/29
29109481Sgrehan * $FreeBSD$
30109481Sgrehan */
31109481Sgrehan
32109481Sgrehan#ifndef _MACHINE_PROFILE_H_
33109481Sgrehan#define	_MACHINE_PROFILE_H_
34109481Sgrehan
35153813Sgrehan#define	_MCOUNT_DECL	void __mcount
36109481Sgrehan
37209591Smarcel#define	FUNCTION_ALIGNMENT	4
38109481Sgrehan
39209975Snwhitehorntypedef __ptrdiff_t	fptrdiff_t;
40109481Sgrehan
41153813Sgrehan/*
42153813Sgrehan * The mcount trampoline macro, expanded in libc/gmon/mcount.c
43153813Sgrehan *
44153813Sgrehan * For PowerPC SVR4 ABI profiling, the compiler will insert
45153813Sgrehan * a data declaration and code sequence at the start of a routine of the form
46153813Sgrehan *
47153813Sgrehan * .function_mc:       	.data
48153813Sgrehan *			.align	2
49153813Sgrehan *			.long	0
50153813Sgrehan *			.text
51153813Sgrehan *
52153813Sgrehan * function:		mflr	%r0
53153813Sgrehan *			addis	%r11,%r0, .function_mc@ha
54153813Sgrehan *			stw	%r0,4(%r1)
55153813Sgrehan *			addi	%r0,%r11, .function_mc@l
56153813Sgrehan *			bl	_mcount
57153813Sgrehan *
58153813Sgrehan * The link register is saved in the LR save word in the caller's
59153813Sgrehan * stack frame, r0 is set up to point to the allocated longword,
60153813Sgrehan * and control is transferred to _mcount.
61153813Sgrehan *
62153813Sgrehan * On return from _mcount, the routine should function as it would
63153813Sgrehan * with no profiling so _mcount must restore register state to that upon
64153813Sgrehan * entry. Any routine called by the _mcount trampoline will save
65153813Sgrehan * callee-save registers, so _mcount must make sure it saves volatile
66153813Sgrehan * registers that may have state after it returns i.e. parameter registers.
67153813Sgrehan *
68153813Sgrehan * The FreeBSD libc mcount routine ignores the r0 longword pointer, but
69153813Sgrehan * instead requires as parameters the current PC and called PC. The current
70153813Sgrehan * PC is obtained from the link register, as a result of "bl _mcount" in
71153813Sgrehan * the stub, while the caller's PC is obtained from the LR save word.
72153813Sgrehan *
73153813Sgrehan * On return from libc mcount, the return is done indirectly with the
74153813Sgrehan * ctr register rather than the link register, to allow the link register
75153813Sgrehan * to be restored to what it was on entry to the profiled routine.
76153813Sgrehan */
77153813Sgrehan
78209975Snwhitehorn#ifdef __powerpc64__
79209975Snwhitehorn#define	MCOUNT					\
80209975Snwhitehorn__asm(	"	.text				\n" \
81209975Snwhitehorn	"	.align	2			\n" \
82209975Snwhitehorn	"	.globl	_mcount			\n" \
83209975Snwhitehorn	"	.section \".opd\",\"aw\"	\n" \
84209975Snwhitehorn	"	.align	3			\n" \
85209975Snwhitehorn	"_mcount:				\n" \
86218824Snwhitehorn	"	.quad .L._mcount,.TOC.@tocbase,0\n" \
87209975Snwhitehorn	"	.previous			\n" \
88234615Snwhitehorn	"	.size   _mcount,24		\n" \
89218824Snwhitehorn	"	.type	_mcount,@function	\n" \
90209975Snwhitehorn	"	.align	4			\n" \
91218824Snwhitehorn	".L._mcount:				\n" \
92218824Snwhitehorn	"	stdu	%r1,-(288+128)(%r1)	\n" \
93209975Snwhitehorn	"	std	%r3,48(%r1)		\n" \
94209975Snwhitehorn	"	std	%r4,56(%r1)		\n" \
95209975Snwhitehorn	"	std	%r5,64(%r1)		\n" \
96209975Snwhitehorn	"	std	%r6,72(%r1)		\n" \
97209975Snwhitehorn	"	std	%r7,80(%r1)		\n" \
98209975Snwhitehorn	"	std	%r8,88(%r1)		\n" \
99209975Snwhitehorn	"	std	%r9,96(%r1)		\n" \
100209975Snwhitehorn	"	std	%r10,104(%r1)		\n" \
101209975Snwhitehorn	"	mflr	%r4			\n" \
102209975Snwhitehorn	"	std	%r4,112(%r1)		\n" \
103209975Snwhitehorn	"	ld	%r3,0(%r1)		\n" \
104209975Snwhitehorn	"	ld	%r3,0(%r3)		\n" \
105209975Snwhitehorn	"	ld	%r3,16(%r3)		\n" \
106218824Snwhitehorn	"	bl	__mcount		\n" \
107209975Snwhitehorn	"	nop				\n" \
108209975Snwhitehorn	"	ld	%r4,112(%r1)		\n" \
109209975Snwhitehorn	"	mtlr	%r4			\n" \
110209975Snwhitehorn	"	ld	%r3,48(%r1)		\n" \
111209975Snwhitehorn	"	ld	%r4,56(%r1)		\n" \
112209975Snwhitehorn	"	ld	%r5,64(%r1)		\n" \
113209975Snwhitehorn	"	ld	%r6,72(%r1)		\n" \
114209975Snwhitehorn	"	ld	%r7,80(%r1)		\n" \
115209975Snwhitehorn	"	ld	%r8,88(%r1)		\n" \
116209975Snwhitehorn	"	ld	%r9,96(%r1)		\n" \
117209975Snwhitehorn	"	ld	%r10,104(%r1)		\n" \
118218824Snwhitehorn	"	addi	%r1,%r1,(288+128)	\n" \
119209975Snwhitehorn	"	blr				\n");
120209975Snwhitehorn#else
121209975Snwhitehorn
122153813Sgrehan#ifdef PIC
123153813Sgrehan#define _PLT "@plt"
124153813Sgrehan#else
125153813Sgrehan#define _PLT
126153813Sgrehan#endif
127153813Sgrehan
128184319Smarcel#define	MCOUNT					\
129184319Smarcel__asm(	"	.globl	_mcount			\n" \
130184319Smarcel	"	.type	_mcount,@function	\n" \
131184319Smarcel	"	.align	4			\n" \
132184319Smarcel	"_mcount:				\n" \
133184319Smarcel	"	stwu	%r1,-64(%r1)		\n" \
134184319Smarcel	"	stw	%r3,16(%r1)		\n" \
135184319Smarcel	"	stw	%r4,20(%r1)		\n" \
136184319Smarcel	"	stw	%r5,24(%r1)		\n" \
137184319Smarcel	"	stw	%r6,28(%r1)		\n" \
138184319Smarcel	"	stw	%r7,32(%r1)		\n" \
139184319Smarcel	"	stw	%r8,36(%r1)		\n" \
140184319Smarcel	"	stw	%r9,40(%r1)		\n" \
141184319Smarcel	"	stw	%r10,44(%r1)		\n" \
142184319Smarcel	"	mflr	%r4			\n" \
143184319Smarcel	"	stw	%r4,48(%r1)		\n" \
144184319Smarcel	"	lwz	%r3,68(%r1)		\n" \
145184319Smarcel	"	bl	__mcount" _PLT "	\n" \
146184319Smarcel	"	lwz	%r3,68(%r1)		\n" \
147184319Smarcel	"	mtlr	%r3			\n" \
148184319Smarcel	"	lwz	%r4,48(%r1)		\n" \
149184319Smarcel	"	mtctr	%r4			\n" \
150184319Smarcel	"	lwz	%r3,16(%r1)		\n" \
151184319Smarcel	"	lwz	%r4,20(%r1)		\n" \
152184319Smarcel	"	lwz	%r5,24(%r1)		\n" \
153184319Smarcel	"	lwz	%r6,28(%r1)		\n" \
154184319Smarcel	"	lwz	%r7,32(%r1)		\n" \
155184319Smarcel	"	lwz	%r8,36(%r1)		\n" \
156184319Smarcel	"	lwz	%r9,40(%r1)		\n" \
157184319Smarcel	"	lwz	%r10,44(%r1)		\n" \
158184319Smarcel	"	addi	%r1,%r1,64		\n" \
159184319Smarcel	"	bctr				\n" \
160184319Smarcel	"_mcount_end:				\n" \
161184319Smarcel	"	.size	_mcount,_mcount_end-_mcount");
162209975Snwhitehorn#endif
163109481Sgrehan
164109481Sgrehan#ifdef _KERNEL
165184319Smarcel#define	MCOUNT_ENTER(s)		s = intr_disable()
166184319Smarcel#define	MCOUNT_EXIT(s)		intr_restore(s)
167184319Smarcel#define	MCOUNT_DECL(s)		register_t s;
168134398Smarcel
169184319Smarcel#ifndef COMPILING_LINT
170184319Smarcel#ifdef AIM
171184319Smarcel#include <machine/trap.h>
172184319Smarcel#define	__PROFILE_VECTOR_BASE	EXC_RST
173184319Smarcel#define	__PROFILE_VECTOR_TOP	(EXC_LAST + 0x100)
174184319Smarcel#endif	/* AIM */
175236141Sraj#if defined(BOOKE)
176184319Smarcelextern char interrupt_vector_base[];
177184319Smarcelextern char interrupt_vector_top[];
178184319Smarcel#define	__PROFILE_VECTOR_BASE	(uintfptr_t)interrupt_vector_base
179184319Smarcel#define	__PROFILE_VECTOR_TOP	(uintfptr_t)interrupt_vector_top
180236141Sraj#endif	/* BOOKE_E500 || BOOKE_PPC4XX */
181236141Sraj
182184319Smarcel#endif	/* !COMPILING_LINT */
183134398Smarcel
184184319Smarcel#ifndef __PROFILE_VECTOR_BASE
185184319Smarcel#define	__PROFILE_VECTOR_BASE	0
186184319Smarcel#endif
187184319Smarcel#ifndef __PROFILE_VECTOR_TOP
188184319Smarcel#define	__PROFILE_VECTOR_TOP	1
189184319Smarcel#endif
190134398Smarcel
191184319Smarcelstatic __inline void
192184319Smarcelpowerpc_profile_interrupt(void)
193184319Smarcel{
194184319Smarcel}
195134398Smarcel
196184319Smarcelstatic __inline void
197184319Smarcelpowerpc_profile_userspace(void)
198184319Smarcel{
199184319Smarcel}
200109481Sgrehan
201184319Smarcel#define	MCOUNT_FROMPC_USER(pc)				\
202184319Smarcel	((pc < (uintfptr_t)VM_MAXUSER_ADDRESS) ?	\
203184319Smarcel	    (uintfptr_t)powerpc_profile_userspace : pc)
204184319Smarcel
205184319Smarcel#define	MCOUNT_FROMPC_INTR(pc)				\
206184319Smarcel	((pc >= __PROFILE_VECTOR_BASE &&		\
207184319Smarcel	  pc < __PROFILE_VECTOR_TOP) ?			\
208184319Smarcel	    (uintfptr_t)powerpc_profile_interrupt : ~0U)
209184319Smarcel
210184319Smarcelvoid __mcount(uintfptr_t frompc, uintfptr_t selfpc);
211184319Smarcel
212147298Sjkoshy#else	/* !_KERNEL */
213147298Sjkoshy
214209975Snwhitehorn#ifdef __powerpc64__
215209975Snwhitehorntypedef u_long	uintfptr_t;
216209975Snwhitehorn#else
217147298Sjkoshytypedef u_int	uintfptr_t;
218209975Snwhitehorn#endif
219147298Sjkoshy
220147298Sjkoshy#endif	/* _KERNEL */
221147298Sjkoshy
222109481Sgrehan#endif /* !_MACHINE_PROFILE_H_ */
223