xref: /freebsd/sys/powerpc/include/asm.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *	$NetBSD: asm.h,v 1.6.18.1 2000/07/25 08:37:14 kleink Exp $
34  * $FreeBSD$
35  */
36 
37 #ifndef _MACHINE_ASM_H_
38 #define	_MACHINE_ASM_H_
39 
40 #include <sys/cdefs.h>
41 
42 #if defined(PIC) && !defined(__powerpc64__)
43 #define	PIC_PROLOGUE	XXX
44 #define	PIC_EPILOGUE	XXX
45 #define	PIC_PLT(x)	x@plt
46 #ifdef	__STDC__
47 #define	PIC_GOT(x)	XXX
48 #else	/* not __STDC__ */
49 #define	PIC_GOT(x)	XXX
50 #endif	/* __STDC__ */
51 #else
52 #define	PIC_PROLOGUE
53 #define	PIC_EPILOGUE
54 #define	PIC_PLT(x)	x
55 #define PIC_GOT(x)	x
56 #endif
57 
58 #define	CNAME(csym)		csym
59 #define	ASMNAME(asmsym)		asmsym
60 #ifdef __powerpc64__
61 #define	HIDENAME(asmsym)	__CONCAT(_,asmsym)
62 #else
63 #define	HIDENAME(asmsym)	__CONCAT(.,asmsym)
64 #endif
65 
66 #if !defined(_CALL_ELF) || _CALL_ELF == 1
67 #ifdef _KERNEL
68 /* ELFv1 kernel uses global dot symbols */
69 #define	DOT_LABEL(name)		__CONCAT(.,name)
70 #define	TYPE_ENTRY(name)	.size	name,24; \
71 				.type	DOT_LABEL(name),@function; \
72 				.globl	DOT_LABEL(name);
73 #define	END_SIZE(name)		.size	DOT_LABEL(name),.-DOT_LABEL(name);
74 #else /* !_KERNEL */
75 /* ELFv1 user code uses local function entry points */
76 #define	DOT_LABEL(name)		__CONCAT(.L.,name)
77 #define	TYPE_ENTRY(name)	.type	name,@function;
78 #define	END_SIZE(name)		.size	name,.-DOT_LABEL(name);
79 #endif /* _KERNEL */
80 #else
81 /* ELFv2 doesn't have any of this complication */
82 #define	DOT_LABEL(name)		name
83 #define	TYPE_ENTRY(name)	.type	name,@function;
84 #define	END_SIZE(name)		.size	name,.-DOT_LABEL(name);
85 #endif
86 
87 #define	_GLOBAL(name) \
88 	.data; \
89 	.p2align 2; \
90 	.globl	name; \
91 	name:
92 
93 #ifdef __powerpc64__
94 #define TOC_NAME_FOR_REF(name)	__CONCAT(.L,name)
95 #define	TOC_REF(name)	TOC_NAME_FOR_REF(name)@toc
96 #define TOC_ENTRY(name) \
97 	.section ".toc","aw"; \
98 	TOC_NAME_FOR_REF(name): \
99         .tc name[TC],name
100 #endif
101 
102 #ifdef __powerpc64__
103 
104 #if !defined(_CALL_ELF) || _CALL_ELF == 1
105 #define	_ENTRY(name) \
106 	.section ".text"; \
107 	.p2align 2; \
108 	.globl	name; \
109 	.section ".opd","aw"; \
110 	.p2align 3; \
111 name: \
112 	.quad	DOT_LABEL(name),.TOC.@tocbase,0; \
113 	.previous; \
114 	.p2align 4; \
115 	TYPE_ENTRY(name) \
116 DOT_LABEL(name): \
117 	.cfi_startproc
118 #define	_NAKED_ENTRY(name)	_ENTRY(name)
119 #else
120 #define	_ENTRY(name) \
121 	.text; \
122 	.p2align 4; \
123 	.globl	name; \
124 	.type	name,@function; \
125 name: \
126 	.cfi_startproc; \
127 	addis	%r2, %r12, (.TOC.-name)@ha; \
128 	addi	%r2, %r2, (.TOC.-name)@l; \
129 	.localentry name, .-name;
130 
131 /* "Naked" function entry.  No TOC prologue for ELFv2. */
132 #define	_NAKED_ENTRY(name) \
133 	.text; \
134 	.p2align 4; \
135 	.globl	name; \
136 	.type	name,@function; \
137 name: \
138 	.cfi_startproc; \
139 	.localentry name, .-name;
140 #endif
141 
142 #define	_END(name) \
143 	.cfi_endproc; \
144 	.long	0; \
145 	.byte	0,0,0,0,0,0,0,0; \
146 	END_SIZE(name)
147 
148 #define	LOAD_ADDR(reg, var) \
149 	lis	reg, var@highest; \
150 	ori	reg, reg, var@higher; \
151 	rldicr	reg, reg, 32, 31; \
152 	oris	reg, reg, var@h; \
153 	ori	reg, reg, var@l;
154 #else /* !__powerpc64__ */
155 #define	_ENTRY(name) \
156 	.text; \
157 	.p2align 4; \
158 	.globl	name; \
159 	.type	name,@function; \
160 name: \
161 	.cfi_startproc
162 #define	_END(name) \
163 	.cfi_endproc; \
164 	.size	name, . - name
165 
166 #define _NAKED_ENTRY(name)	_ENTRY(name)
167 
168 #define	LOAD_ADDR(reg, var) \
169 	lis	reg, var@ha; \
170 	ori	reg, reg, var@l;
171 #endif /* __powerpc64__ */
172 
173 #if defined(PROF) || (defined(_KERNEL) && defined(GPROF))
174 # ifdef __powerpc64__
175 #   define	_PROF_PROLOGUE	mflr 0;					\
176 				std 3,48(1);				\
177 				std 4,56(1);				\
178 				std 5,64(1);				\
179 				std 0,16(1);				\
180 				stdu 1,-112(1);				\
181 				bl _mcount;				\
182 				nop;					\
183 				ld 0,112+16(1);				\
184 				ld 3,112+48(1);				\
185 				ld 4,112+56(1);				\
186 				ld 5,112+64(1);				\
187 				mtlr 0;					\
188 				addi 1,1,112
189 # else
190 #   define	_PROF_PROLOGUE	mflr 0; stw 0,4(1); bl _mcount
191 # endif
192 #else
193 # define	_PROF_PROLOGUE
194 #endif
195 
196 #define	ASEND(y)	_END(ASMNAME(y))
197 #define	ASENTRY(y)	_ENTRY(ASMNAME(y)); _PROF_PROLOGUE
198 #define	END(y)		_END(CNAME(y))
199 #define	ENTRY(y)	_ENTRY(CNAME(y)); _PROF_PROLOGUE
200 #define	GLOBAL(y)	_GLOBAL(CNAME(y))
201 
202 #define	ASENTRY_NOPROF(y)	_ENTRY(ASMNAME(y))
203 #define	ENTRY_NOPROF(y)		_ENTRY(CNAME(y))
204 
205 /* Load NIA without affecting branch prediction */
206 #define	LOAD_LR_NIA	bcl	20, 31, .+4
207 
208 /*
209  * Magic sequence to return to native endian.
210  * Overwrites r0 and r11.
211  *
212  * The encoding of the instruction "tdi 0, %r0, 0x48" in opposite endian
213  * happens to be "b . + 8". This is useful because we can write a sequence
214  * of instructions that can execute in either endian.
215  *
216  * Use a sequence of handcoded instructions that switches contexts to the
217  * instruction following the sequence, but with the correct PSL_LE bit.
218  *
219  * The same sequence works for both BE and LE because the xori will flip
220  * the bit to the other state, and the code only runs when running in the
221  * wrong endian.
222  *
223  * This sequence is NMI-reentrant.
224  *
225  * Do not change the length of this sequence without looking at the users,
226  * this is used in size-constrained places like the reset vector!
227  */
228 #define	RETURN_TO_NATIVE_ENDIAN						  \
229 	tdi	0, %r0, 0x48;	/* Endian swapped: b . + 8		*/\
230 	b	1f;		/* Will fall through to here if correct */\
231 	.long	0xa600607d;	/* mfmsr %r11				*/\
232 	.long	0x00000038;	/* li %r0, 0				*/\
233 	.long	0x6401617d;	/* mtmsrd %r0, 1 (L=1 EE,RI bits only)	*/\
234 	.long	0x01006b69;	/* xori %r11, %r11, 0x1 (PSL_LE)	*/\
235 	.long	0xa602087c;	/* mflr %r0				*/\
236 	.long	0x05009f42;	/* LOAD_LR_NIA				*/\
237 	.long	0xa6037b7d;	/* 0: mtsrr1 %r11			*/\
238 	.long	0xa602687d;	/* mflr	%r11				*/\
239 	.long	0x18006b39;	/* addi	%r11, %r11, (1f - 0b)		*/\
240 	.long	0xa6037a7d;	/* mtsrr0 %r11				*/\
241 	.long	0xa603087c;	/* mtlr %r0				*/\
242 	.long	0x2400004c;	/* rfid					*/\
243 1:	/* RETURN_TO_NATIVE_ENDIAN */
244 
245 #define	ASMSTR		.asciz
246 
247 #define	RCSID(x)	.text; .asciz x
248 
249 #undef __FBSDID
250 #if !defined(lint) && !defined(STRIP_FBSDID)
251 #define __FBSDID(s)	.ident s
252 #else
253 #define __FBSDID(s)	/* nothing */
254 #endif /* not lint and not STRIP_FBSDID */
255 
256 #define	WEAK_REFERENCE(sym, alias)				\
257 	.weak alias;						\
258 	.equ alias,sym
259 
260 #ifdef __STDC__
261 #define	WARN_REFERENCES(_sym,_msg)				\
262 	.section .gnu.warning. ## _sym ; .ascii _msg ; .text
263 #else
264 #define	WARN_REFERENCES(_sym,_msg)				\
265 	.section .gnu.warning./**/_sym ; .ascii _msg ; .text
266 #endif /* __STDC__ */
267 
268 #endif /* !_MACHINE_ASM_H_ */
269