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