xref: /netbsd-src/sys/arch/powerpc/include/asm.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: asm.h,v 1.46 2014/03/06 19:05:24 matt Exp $	*/
2 
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 
34 #ifndef _PPC_ASM_H_
35 #define _PPC_ASM_H_
36 
37 #ifdef _LP64
38 
39 /* ppc64 is always PIC, r2 is always the TOC */
40 
41 # define PIC_PLT(x)	.x
42 
43 #else
44 
45 # ifdef __PIC__
46 #  define PIC_PROLOGUE	XXX
47 #  define PIC_EPILOGUE	XXX
48 #  define PIC_PLT(x)	x+32768@plt
49 #  ifdef __STDC__
50 #   define PIC_TOCNAME(name) 	.LCTOC_##name
51 #  else
52 #   define PIC_TOCNAME(name) 	.LCTOC_/**/name
53 #  endif /* __STDC __*/
54 #  define PIC_TOCSETUP(name, reg)						\
55 		.pushsection ".got2","aw"				;\
56 	PIC_TOCNAME(name) = . + 32768					;\
57 		.popsection						;\
58 		bcl	20,31,1001f					;\
59 	1001:	mflr	reg						;\
60 		addis	reg,reg,PIC_TOCNAME(name)-1001b@ha		;\
61 		addi	reg,reg,PIC_TOCNAME(name)-1001b@l
62 #  define PIC_GOTSETUP(reg)						\
63 		bcl	20,31,2002f					;\
64 	2002:	mflr	reg						;\
65 		addis	reg,reg,_GLOBAL_OFFSET_TABLE_-2002b@ha		;\
66 		addi	reg,reg,_GLOBAL_OFFSET_TABLE_-2002b@l
67 #  ifdef __STDC__
68 #   define PIC_GOT(x)	XXX
69 #   define PIC_GOTOFF(x)	XXX
70 #  else	/* not __STDC__ */
71 #   define PIC_GOT(x)	XXX
72 #   define PIC_GOTOFF(x)	XXX
73 #  endif /* __STDC__ */
74 # else /* !__PIC__ */
75 #  define PIC_PROLOGUE
76 #  define PIC_EPILOGUE
77 #  define PIC_PLT(x)	x
78 #  define PIC_GOT(x)	x
79 #  define PIC_GOTOFF(x)	x
80 #  define PIC_GOTSETUP(r)
81 #  define PIC_TOCSETUP(n, r)
82 # endif /* __PIC__ */
83 
84 #endif /* __LP64__ */
85 
86 #define	_C_LABEL(x)	x
87 #define	_ASM_LABEL(x)	x
88 
89 #define	_GLOBAL(x) \
90 	.data; .align 2; .globl x; x:
91 
92 #ifdef GPROF
93 # define _PROF_PROLOGUE	mflr 0; stw 0,4(1); bl _mcount
94 #else
95 # define _PROF_PROLOGUE
96 #endif
97 
98 #ifdef _LP64
99 
100 # define SF_HEADER_SZ	48
101 # define SF_PARAM_SZ	64
102 # define SF_SZ		(SF_HEADER_SZ + SF_PARAM_SZ)
103 
104 # define SF_SP		 0
105 # define SF_CR		 8
106 # define SF_LR		16
107 # define SF_PARAM	SF_HEADER_SZ
108 
109 # define _XENTRY(y)			\
110 	.globl	y;			\
111 	.pushsection ".opd","aw";	\
112 	.align	3;			\
113 y:	.quad	.##y,.TOC.@tocbase,0;	\
114 	.popsection;			\
115 	.size	y,24;			\
116 	.type	.##y,@function;		\
117 	.globl	.##y;			\
118 	.align	3;			\
119 .##y:
120 
121 #define _ENTRY(x)	.text; _XENTRY(x)
122 
123 # define ENTRY(y) _ENTRY(y)
124 
125 # define END(y)	.size .##y,. - .##y
126 
127 # define CALL(y)			\
128 	bl	.y;			\
129 	nop
130 
131 # define ENTRY_NOPROFILE(y)	ENTRY(y)
132 # define ASENTRY(y)		ENTRY(y)
133 #else /* !_LP64 */
134 
135 # define _XENTRY(x)	.align 2; .globl x; .type x,@function; x:
136 # define _ENTRY(x)	.text; _XENTRY(x)
137 
138 # define ENTRY(y)	_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
139 
140 # define END(y)		.size _C_LABEL(y),.-_C_LABEL(y)
141 
142 # define CALL(y)			\
143 	bl	y
144 
145 # define ENTRY_NOPROFILE(y) _ENTRY(_C_LABEL(y))
146 # define ASENTRY(y)	_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
147 #endif /* __LP64__ */
148 
149 #define	GLOBAL(y)	_GLOBAL(_C_LABEL(y))
150 
151 #define	ASMSTR		.asciz
152 
153 #undef __RCSID
154 #define RCSID(x)	__RCSID(x)
155 #define	__RCSID(x)	.pushsection .ident; .asciz x; .popsection
156 
157 #ifdef __ELF__
158 # define WEAK_ALIAS(alias,sym)						\
159 	.weak alias;							\
160 	alias = sym
161 #endif /* __ELF__ */
162 /*
163  * STRONG_ALIAS: create a strong alias.
164  */
165 #define STRONG_ALIAS(alias,sym)						\
166 	.globl alias;							\
167 	alias = sym
168 
169 #ifdef __STDC__
170 # define WARN_REFERENCES(sym,msg)					\
171 	.pushsection .gnu.warning. ## sym;				\
172 	.ascii msg;							\
173 	.popsection
174 #else
175 # define WARN_REFERENCES(sym,msg)					\
176 	.pushsection .gnu.warning./**/sym;				\
177 	.ascii msg;							\
178 	.popsection
179 #endif /* __STDC__ */
180 
181 #ifdef _KERNEL
182 /*
183  * Get cpu_info pointer for current processor.  Always in SPRG0. *ALWAYS*
184  */
185 # define GET_CPUINFO(r)		mfsprg r,0
186 /*
187  * IN:
188  *	R4[er] = first free byte beyond end/esym.
189  *
190  * OUT:
191  *	R1[sp] = new kernel stack
192  *	R4[er] = kernelend
193  */
194 
195 # ifdef CI_INTSTK
196 #  define INIT_CPUINFO_INTSTK(er,tmp1)					\
197 	addis	er,er,INTSTK@ha;					\
198 	addi	er,er,INTSTK@l;						\
199 	stptr	er,CI_INTSTK(tmp1)
200 # else
201 #  define INIT_CPUINFO_INTSTK(er,tmp1)	/* nothing */
202 # endif /* CI_INTSTK */
203 
204 /*
205  * We use lis/ori instead of lis/addi in case tmp2 is r0.
206  */
207 # define INIT_CPUINFO(er,sp,tmp1,tmp2) 					\
208 	li	tmp1,PAGE_MASK;						\
209 	add	er,er,tmp1;						\
210 	andc	er,er,tmp1;		/* page align */		\
211 	lis	tmp1,_C_LABEL(cpu_info)@ha;				\
212 	addi	tmp1,tmp1,_C_LABEL(cpu_info)@l;				\
213 	mtsprg0	tmp1;			/* save for later use */	\
214 	INIT_CPUINFO_INTSTK(er,tmp1);					\
215 	lis	tmp2,_C_LABEL(emptyidlespin)@h;				\
216 	ori	tmp2,tmp2,_C_LABEL(emptyidlespin)@l;			\
217 	stptr	tmp2,CI_IDLESPIN(tmp1);					\
218 	li	tmp2,-1;						\
219 	stint	tmp2,CI_IDEPTH(tmp1);					\
220 	li	tmp2,0;							\
221 	lis	%r13,_C_LABEL(lwp0)@h;					\
222 	ori	%r13,%r13,_C_LABEL(lwp0)@l;				\
223 	stptr	er,L_PCB(%r13);		/* XXXuvm_lwp_getuarea */	\
224 	stptr	tmp1,L_CPU(%r13);	 				\
225 	addis	er,er,USPACE@ha;	/* stackpointer for lwp0 */	\
226 	addi	er,er,USPACE@l;		/* stackpointer for lwp0 */	\
227 	addi	sp,er,-FRAMELEN-CALLFRAMELEN;	/* stackpointer for lwp0 */ \
228 	stptr	sp,L_MD_UTF(%r13);	/* save in lwp0.l_md.md_utf */	\
229 		/* er = end of mem reserved for kernel */		\
230 	li	tmp2,0;							\
231 	stptr	tmp2,-CALLFRAMELEN(er);	/* end of stack chain */	\
232 	stptru	tmp2,-CALLFRAMELEN(sp)	/* end of stack chain */
233 
234 #endif /* _KERNEL */
235 
236 
237 #if defined(_REGNAMES) && (defined(_KERNEL) || defined(_STANDALONE))
238   /* Condition Register Bit Fields */
239 # define cr0	 0
240 # define cr1	 1
241 # define cr2	 2
242 # define cr3	 3
243 # define cr4	 4
244 # define cr5	 5
245 # define cr6	 6
246 # define cr7	 7
247   /* General Purpose Registers (GPRs) */
248 # define r0	 0
249 # define r1	 1
250 # define r2	 2
251 # define r3	 3
252 # define r4	 4
253 # define r5	 5
254 # define r6	 6
255 # define r7	 7
256 # define r8	 8
257 # define r9	 9
258 # define r10	10
259 # define r11	11
260 # define r12	12
261 # define r13	13
262 # define r14	14
263 # define r15	15
264 # define r16	16
265 # define r17	17
266 # define r18	18
267 # define r19	19
268 # define r20	20
269 # define r21	21
270 # define r22	22
271 # define r23	23
272 # define r24	24
273 # define r25	25
274 # define r26	26
275 # define r27	27
276 # define r28	28
277 # define r29	29
278 # define r30	30
279 # define r31	31
280   /* Floating Point Registers (FPRs) */
281 # define fr0	 0
282 # define fr1	 1
283 # define fr2	 2
284 # define fr3	 3
285 # define fr4	 4
286 # define fr5	 5
287 # define fr6	 6
288 # define fr7	 7
289 # define fr8	 8
290 # define fr9	 9
291 # define fr10	10
292 # define fr11	11
293 # define fr12	12
294 # define fr13	13
295 # define fr14	14
296 # define fr15	15
297 # define fr16	16
298 # define fr17	17
299 # define fr18	18
300 # define fr19	19
301 # define fr20	20
302 # define fr21	21
303 # define fr22	22
304 # define fr23	23
305 # define fr24	24
306 # define fr25	25
307 # define fr26	26
308 # define fr27	27
309 # define fr28	28
310 # define fr29	29
311 # define fr30	30
312 # define fr31	31
313 #endif /* _REGNAMES && (_KERNEL || _STANDALONE) */
314 
315 /*
316  * Add some psuedo instructions to made sharing of assembly versions of
317  * ILP32 and LP64 code possible.
318  */
319 #define ldint		lwz	/* not needed but for completeness */
320 #define ldintu		lwzu	/* not needed but for completeness */
321 #define stint		stw	/* not needed but for completeness */
322 #define stintu		stwu	/* not needed but for completeness */
323 
324 #ifndef _LP64
325 
326 # define ldlong		lwz	/* load "C" long */
327 # define ldlongu	lwzu	/* load "C" long with udpate */
328 # define stlong		stw	/* load "C" long */
329 # define stlongu	stwu	/* load "C" long with udpate */
330 # define ldptr		lwz	/* load "C" pointer */
331 # define ldptru		lwzu	/* load "C" pointer with udpate */
332 # define stptr		stw	/* load "C" pointer */
333 # define stptru		stwu	/* load "C" pointer with udpate */
334 # define ldreg		lwz	/* load PPC general register */
335 # define ldregu		lwzu	/* load PPC general register with udpate */
336 # define streg		stw	/* load PPC general register */
337 # define stregu		stwu	/* load PPC general register with udpate */
338 # define SZREG		4	/* 4 byte registers */
339 # define P2SZREG	2
340 
341 # define lptrarx	lwarx	/* load "C" pointer with reservation */
342 # define llongarx	lwarx	/* load "C" long with reservation */
343 # define lregarx	lwarx	/* load PPC general register with reservation */
344 
345 # define stptrcx	stwcx	/* store "C" pointer conditional */
346 # define stlongcx	stwcx	/* store "C" long conditional */
347 # define stregcx	stwcx	/* store PPC general register conditional */
348 
349 # define clrrptri	clrrwi	/* clear right "C" pointer immediate */
350 # define clrrlongi	clrrwi	/* clear right "C" long immediate */
351 # define clrrregi	clrrwi	/* clear right PPC general register immediate */
352 
353 # define cmpptr		cmpw
354 # define cmplong	cmpw
355 # define cmpreg		cmpw
356 # define cmpptri	cmpwi
357 # define cmplongi	cmpwi
358 # define cmpregi	cmpwi
359 # define cmpptrl	cmpwl
360 # define cmplongl	cmpwl
361 # define cmpregl	cmpwl
362 # define cmpptrli	cmpwli
363 # define cmplongli	cmpwli
364 # define cmpregli	cmpwli
365 
366 #else /* __LP64__ */
367 
368 # define ldlong		ld	/* load "C" long */
369 # define ldlongu	ldu	/* load "C" long with update */
370 # define stlong		std	/* store "C" long */
371 # define stlongu	stdu	/* store "C" long with update */
372 # define ldptr		ld	/* load "C" pointer */
373 # define ldptru		ldu	/* load "C" pointer with update */
374 # define stptr		std	/* store "C" pointer */
375 # define stptru		stdu	/* store "C" pointer with update */
376 # define ldreg		ld	/* load PPC general register */
377 # define ldregu		ldu	/* load PPC general register with update */
378 # define streg		std	/* store PPC general register */
379 # define stregu		stdu	/* store PPC general register with update */
380 /* redefined this to force an error on PPC64 to catch their use.  */
381 # define lmw		lmd	/* load multiple PPC general registers */
382 # define stmw		stmd	/* store multiple PPC general registers */
383 # define SZREG		8	/* 8 byte registers */
384 # define P2SZREG	3
385 
386 # define lptrarx	ldarx	/* load "C" pointer with reservation */
387 # define llongarx	ldarx	/* load "C" long with reservation */
388 # define lregarx	ldarx	/* load PPC general register with reservation */
389 
390 # define stptrcx	stdcx	/* store "C" pointer conditional */
391 # define stlongcx	stdcx	/* store "C" long conditional */
392 # define stregax	stdcx	/* store PPC general register conditional */
393 
394 # define clrrptri	clrrdi	/* clear right "C" pointer immediate */
395 # define clrrlongi	clrrdi	/* clear right "C" long immediate */
396 # define clrrregi	clrrdi	/* clear right PPC general register immediate */
397 
398 # define cmpptr		cmpd
399 # define cmplong	cmpd
400 # define cmpreg		cmpd
401 # define cmpptri	cmpdi
402 # define cmplongi	cmpdi
403 # define cmpregi	cmpdi
404 # define cmpptrl	cmpdl
405 # define cmplongl	cmpdl
406 # define cmpregl	cmpdl
407 # define cmpptrli	cmpdli
408 # define cmplongli	cmpdli
409 # define cmpregli	cmpdli
410 
411 #endif /* __LP64__ */
412 
413 #ifdef _LOCORE
414 .macro	stmd	r,dst
415 	i = 0
416     .rept	32-\r
417 	std	i+\r, i*8+\dst
418 	i = i + 1
419     .endr
420 .endm
421 
422 .macro	lmd	r,dst
423 	i = 0
424     .rept	32-\r
425 	ld	i+\r, i*8+\dst
426 	i = i + 1
427     .endr
428 .endm
429 #endif /* _LOCORE */
430 
431 #endif /* !_PPC_ASM_H_ */
432