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 * $FreeBSD$
32 */
33
34#ifndef _MACHINE_PROFILE_H_
35#define	_MACHINE_PROFILE_H_
36
37#define	_MCOUNT_DECL	void __mcount
38
39#define	FUNCTION_ALIGNMENT	4
40
41typedef __ptrdiff_t	fptrdiff_t;
42
43/*
44 * The mcount trampoline macro, expanded in libc/gmon/mcount.c
45 *
46 * For PowerPC SVR4 ABI profiling, the compiler will insert
47 * a data declaration and code sequence at the start of a routine of the form
48 *
49 * .function_mc:       	.data
50 *			.align	2
51 *			.long	0
52 *			.text
53 *
54 * function:		mflr	%r0
55 *			addis	%r11,%r0, .function_mc@ha
56 *			stw	%r0,4(%r1)
57 *			addi	%r0,%r11, .function_mc@l
58 *			bl	_mcount
59 *
60 * The link register is saved in the LR save word in the caller's
61 * stack frame, r0 is set up to point to the allocated longword,
62 * and control is transferred to _mcount.
63 *
64 * On return from _mcount, the routine should function as it would
65 * with no profiling so _mcount must restore register state to that upon
66 * entry. Any routine called by the _mcount trampoline will save
67 * callee-save registers, so _mcount must make sure it saves volatile
68 * registers that may have state after it returns i.e. parameter registers.
69 *
70 * The FreeBSD libc mcount routine ignores the r0 longword pointer, but
71 * instead requires as parameters the current PC and called PC. The current
72 * PC is obtained from the link register, as a result of "bl _mcount" in
73 * the stub, while the caller's PC is obtained from the LR save word.
74 *
75 * On return from libc mcount, the return is done indirectly with the
76 * ctr register rather than the link register, to allow the link register
77 * to be restored to what it was on entry to the profiled routine.
78 */
79
80#if defined(__powerpc64__)
81
82#if !defined(_CALL_ELF) || _CALL_ELF == 1
83#define MCOUNT_PREAMBLE \
84	"	.align	2			\n" \
85	"	.globl	_mcount			\n" \
86	"	.section \".opd\",\"aw\"	\n" \
87	"	.align	3			\n" \
88	"_mcount:				\n" \
89	"	.quad .L._mcount,.TOC.@tocbase,0\n" \
90	"	.previous			\n" \
91	"	.size   _mcount,24		\n" \
92	"	.type	_mcount,@function	\n" \
93	"	.align	4			\n" \
94	".L._mcount:				\n"
95#else
96#define MCOUNT_PREAMBLE \
97	"	.globl	_mcount			\n" \
98	"	.type	_mcount,@function	\n" \
99	"	.align	4			\n" \
100	"_mcount:				\n"
101#endif
102
103#define	MCOUNT					\
104__asm(	MCOUNT_PREAMBLE \
105	"	stdu	%r1,-(288+128)(%r1)	\n" \
106	"	std	%r3,48(%r1)		\n" \
107	"	std	%r4,56(%r1)		\n" \
108	"	std	%r5,64(%r1)		\n" \
109	"	std	%r6,72(%r1)		\n" \
110	"	std	%r7,80(%r1)		\n" \
111	"	std	%r8,88(%r1)		\n" \
112	"	std	%r9,96(%r1)		\n" \
113	"	std	%r10,104(%r1)		\n" \
114	"	mflr	%r4			\n" \
115	"	std	%r4,112(%r1)		\n" \
116	"	ld	%r3,0(%r1)		\n" \
117	"	ld	%r3,0(%r3)		\n" \
118	"	ld	%r3,16(%r3)		\n" \
119	"	bl	__mcount		\n" \
120	"	nop				\n" \
121	"	ld	%r4,112(%r1)		\n" \
122	"	mtlr	%r4			\n" \
123	"	ld	%r3,48(%r1)		\n" \
124	"	ld	%r4,56(%r1)		\n" \
125	"	ld	%r5,64(%r1)		\n" \
126	"	ld	%r6,72(%r1)		\n" \
127	"	ld	%r7,80(%r1)		\n" \
128	"	ld	%r8,88(%r1)		\n" \
129	"	ld	%r9,96(%r1)		\n" \
130	"	ld	%r10,104(%r1)		\n" \
131	"	addi	%r1,%r1,(288+128)	\n" \
132	"	blr				\n");
133#else
134
135#ifdef PIC
136#define _PLT "@plt"
137#else
138#define _PLT
139#endif
140
141#define	MCOUNT					\
142__asm(	"	.globl	_mcount			\n" \
143	"	.type	_mcount,@function	\n" \
144	"	.align	4			\n" \
145	"_mcount:				\n" \
146	"	stwu	%r1,-64(%r1)		\n" \
147	"	stw	%r3,16(%r1)		\n" \
148	"	stw	%r4,20(%r1)		\n" \
149	"	stw	%r5,24(%r1)		\n" \
150	"	stw	%r6,28(%r1)		\n" \
151	"	stw	%r7,32(%r1)		\n" \
152	"	stw	%r8,36(%r1)		\n" \
153	"	stw	%r9,40(%r1)		\n" \
154	"	stw	%r10,44(%r1)		\n" \
155	"	mflr	%r4			\n" \
156	"	stw	%r4,48(%r1)		\n" \
157	"	lwz	%r3,68(%r1)		\n" \
158	"	bl	__mcount" _PLT "	\n" \
159	"	lwz	%r3,68(%r1)		\n" \
160	"	mtlr	%r3			\n" \
161	"	lwz	%r4,48(%r1)		\n" \
162	"	mtctr	%r4			\n" \
163	"	lwz	%r3,16(%r1)		\n" \
164	"	lwz	%r4,20(%r1)		\n" \
165	"	lwz	%r5,24(%r1)		\n" \
166	"	lwz	%r6,28(%r1)		\n" \
167	"	lwz	%r7,32(%r1)		\n" \
168	"	lwz	%r8,36(%r1)		\n" \
169	"	lwz	%r9,40(%r1)		\n" \
170	"	lwz	%r10,44(%r1)		\n" \
171	"	addi	%r1,%r1,64		\n" \
172	"	bctr				\n" \
173	"_mcount_end:				\n" \
174	"	.size	_mcount,_mcount_end-_mcount");
175#endif
176
177#ifdef _KERNEL
178#define	MCOUNT_ENTER(s)		s = intr_disable()
179#define	MCOUNT_EXIT(s)		intr_restore(s)
180#define	MCOUNT_DECL(s)		register_t s;
181
182#ifndef COMPILING_LINT
183#ifdef AIM
184#include <machine/trap.h>
185#define	__PROFILE_VECTOR_BASE	EXC_RST
186#define	__PROFILE_VECTOR_TOP	(EXC_LAST + 0x100)
187#endif	/* AIM */
188#if defined(BOOKE)
189extern char interrupt_vector_base[];
190extern char interrupt_vector_top[];
191#define	__PROFILE_VECTOR_BASE	(uintfptr_t)interrupt_vector_base
192#define	__PROFILE_VECTOR_TOP	(uintfptr_t)interrupt_vector_top
193#endif	/* BOOKE_E500 */
194
195#endif	/* !COMPILING_LINT */
196
197#ifndef __PROFILE_VECTOR_BASE
198#define	__PROFILE_VECTOR_BASE	0
199#endif
200#ifndef __PROFILE_VECTOR_TOP
201#define	__PROFILE_VECTOR_TOP	1
202#endif
203
204static __inline void
205powerpc_profile_interrupt(void)
206{
207}
208
209static __inline void
210powerpc_profile_userspace(void)
211{
212}
213
214#define	MCOUNT_FROMPC_USER(pc)				\
215	((pc < (uintfptr_t)VM_MAXUSER_ADDRESS) ?	\
216	    (uintfptr_t)powerpc_profile_userspace : pc)
217
218#define	MCOUNT_FROMPC_INTR(pc)				\
219	((pc >= __PROFILE_VECTOR_BASE &&		\
220	  pc < __PROFILE_VECTOR_TOP) ?			\
221	    (uintfptr_t)powerpc_profile_interrupt : ~0U)
222
223void __mcount(uintfptr_t frompc, uintfptr_t selfpc);
224
225#else	/* !_KERNEL */
226
227#ifdef __powerpc64__
228typedef u_long	uintfptr_t;
229#else
230typedef u_int	uintfptr_t;
231#endif
232
233#endif	/* _KERNEL */
234
235#endif /* !_MACHINE_PROFILE_H_ */
236