xref: /freebsd/sys/amd64/include/asmacros.h (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
13c4dd356SDavid Greenman /*-
23c4dd356SDavid Greenman  * Copyright (c) 1993 The Regents of the University of California.
33c4dd356SDavid Greenman  * All rights reserved.
43c4dd356SDavid Greenman  *
53c4dd356SDavid Greenman  * Redistribution and use in source and binary forms, with or without
63c4dd356SDavid Greenman  * modification, are permitted provided that the following conditions
73c4dd356SDavid Greenman  * are met:
83c4dd356SDavid Greenman  * 1. Redistributions of source code must retain the above copyright
93c4dd356SDavid Greenman  *    notice, this list of conditions and the following disclaimer.
103c4dd356SDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
113c4dd356SDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
123c4dd356SDavid Greenman  *    documentation and/or other materials provided with the distribution.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
143c4dd356SDavid Greenman  *    may be used to endorse or promote products derived from this software
153c4dd356SDavid Greenman  *    without specific prior written permission.
163c4dd356SDavid Greenman  *
173c4dd356SDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
183c4dd356SDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193c4dd356SDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203c4dd356SDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
213c4dd356SDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223c4dd356SDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233c4dd356SDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243c4dd356SDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253c4dd356SDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263c4dd356SDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273c4dd356SDavid Greenman  * SUCH DAMAGE.
283c4dd356SDavid Greenman  *
29c3aac50fSPeter Wemm  * $FreeBSD$
303c4dd356SDavid Greenman  */
313c4dd356SDavid Greenman 
32ab9678acSBruce Evans #ifndef _MACHINE_ASMACROS_H_
33ab9678acSBruce Evans #define _MACHINE_ASMACROS_H_
34ab9678acSBruce Evans 
35ee323f62SPeter Wemm #include <sys/cdefs.h>
366605d9f3SJordan K. Hubbard 
37435929a8SBruce Evans /* XXX too much duplication in various asm*.h's. */
38912e6037SBruce Evans 
399081eec1SJohn Polstra /*
4094450a83SBruce Evans  * CNAME is used to manage the relationship between symbol names in C
419081eec1SJohn Polstra  * and the equivalent assembly language names.  CNAME is given a name as
429081eec1SJohn Polstra  * it would be used in a C program.  It expands to the equivalent assembly
4394450a83SBruce Evans  * language name.
449081eec1SJohn Polstra  */
459081eec1SJohn Polstra #define CNAME(csym)		csym
460967373eSDavid Greenman 
47afa88623SPeter Wemm #define ALIGN_DATA	.p2align 3	/* 8 byte alignment, zero filled */
489081eec1SJohn Polstra #ifdef GPROF
499081eec1SJohn Polstra #define ALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
509081eec1SJohn Polstra #else
51afa88623SPeter Wemm #define ALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
529081eec1SJohn Polstra #endif
539081eec1SJohn Polstra #define SUPERALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
549081eec1SJohn Polstra 
559081eec1SJohn Polstra #define GEN_ENTRY(name)		ALIGN_TEXT; .globl CNAME(name); \
56ea2b3e3dSBruce Evans 				.type CNAME(name),@function; CNAME(name):
57912e6037SBruce Evans #define NON_GPROF_ENTRY(name)	GEN_ENTRY(name)
58435929a8SBruce Evans #define NON_GPROF_RET		.byte 0xc3	/* opcode for `ret' */
59d2306226SDavid Greenman 
60298889efSJoseph Koshy #define	END(name)		.size name, . - name
61298889efSJoseph Koshy 
620967373eSDavid Greenman #ifdef GPROF
630967373eSDavid Greenman /*
64ea2b3e3dSBruce Evans  * __mcount is like [.]mcount except that doesn't require its caller to set
65912e6037SBruce Evans  * up a frame pointer.  It must be called before pushing anything onto the
66912e6037SBruce Evans  * stack.  gcc should eventually generate code to call __mcount in most
67912e6037SBruce Evans  * cases.  This would make -pg in combination with -fomit-frame-pointer
68912e6037SBruce Evans  * useful.  gcc has a configuration variable PROFILE_BEFORE_PROLOGUE to
69912e6037SBruce Evans  * allow profiling before setting up the frame pointer, but this is
70912e6037SBruce Evans  * inadequate for good handling of special cases, e.g., -fpic works best
71912e6037SBruce Evans  * with profiling after the prologue.
72912e6037SBruce Evans  *
73ea2b3e3dSBruce Evans  * [.]mexitcount is a new function to support non-statistical profiling if an
74435929a8SBruce Evans  * accurate clock is available.  For C sources, calls to it are generated
75435929a8SBruce Evans  * by the FreeBSD extension `-mprofiler-epilogue' to gcc.  It is best to
76ea2b3e3dSBruce Evans  * call [.]mexitcount at the end of a function like the MEXITCOUNT macro does,
77435929a8SBruce Evans  * but gcc currently generates calls to it at the start of the epilogue to
78435929a8SBruce Evans  * avoid problems with -fpic.
79912e6037SBruce Evans  *
80ea2b3e3dSBruce Evans  * [.]mcount and __mcount may clobber the call-used registers and %ef.
81ea2b3e3dSBruce Evans  * [.]mexitcount may clobber %ecx and %ef.
82912e6037SBruce Evans  *
83435929a8SBruce Evans  * Cross-jumping makes non-statistical profiling timing more complicated.
84ea2b3e3dSBruce Evans  * It is handled in many cases by calling [.]mexitcount before jumping.  It
85ea2b3e3dSBruce Evans  * is handled for conditional jumps using CROSSJUMP() and CROSSJUMP_LABEL().
86435929a8SBruce Evans  * It is handled for some fault-handling jumps by not sharing the exit
87435929a8SBruce Evans  * routine.
88912e6037SBruce Evans  *
89912e6037SBruce Evans  * ALTENTRY() must be before a corresponding ENTRY() so that it can jump to
90912e6037SBruce Evans  * the main entry point.  Note that alt entries are counted twice.  They
91912e6037SBruce Evans  * have to be counted as ordinary entries for gprof to get the call times
92912e6037SBruce Evans  * right for the ordinary entries.
93912e6037SBruce Evans  *
94912e6037SBruce Evans  * High local labels are used in macros to avoid clashes with local labels
95912e6037SBruce Evans  * in functions.
96912e6037SBruce Evans  *
97435929a8SBruce Evans  * Ordinary `ret' is used instead of a macro `RET' because there are a lot
98435929a8SBruce Evans  * of `ret's.  0xc3 is the opcode for `ret' (`#define ret ... ret' can't
99435929a8SBruce Evans  * be used because this file is sometimes preprocessed in traditional mode).
100435929a8SBruce Evans  * `ret' clobbers eflags but this doesn't matter.
1010967373eSDavid Greenman  */
102912e6037SBruce Evans #define ALTENTRY(name)		GEN_ENTRY(name) ; MCOUNT ; MEXITCOUNT ; jmp 9f
103435929a8SBruce Evans #define	CROSSJUMP(jtrue, label, jfalse) \
104435929a8SBruce Evans 	jfalse 8f; MEXITCOUNT; jmp __CONCAT(to,label); 8:
105435929a8SBruce Evans #define CROSSJUMPTARGET(label) \
106435929a8SBruce Evans 	ALIGN_TEXT; __CONCAT(to,label): ; MCOUNT; jmp label
107912e6037SBruce Evans #define ENTRY(name)		GEN_ENTRY(name) ; 9: ; MCOUNT
108cda07865SPeter Wemm #define FAKE_MCOUNT(caller)	pushq caller ; call __mcount ; popq %rcx
109912e6037SBruce Evans #define MCOUNT			call __mcount
110912e6037SBruce Evans #define MCOUNT_LABEL(name)	GEN_ENTRY(name) ; nop ; ALIGN_TEXT
11144764790SBruce Evans #ifdef GUPROF
11294450a83SBruce Evans #define MEXITCOUNT		call .mexitcount
113435929a8SBruce Evans #define ret			MEXITCOUNT ; NON_GPROF_RET
11444764790SBruce Evans #else
11544764790SBruce Evans #define MEXITCOUNT
11644764790SBruce Evans #endif
117435929a8SBruce Evans 
118435929a8SBruce Evans #else /* !GPROF */
1190967373eSDavid Greenman /*
1200967373eSDavid Greenman  * ALTENTRY() has to align because it is before a corresponding ENTRY().
1210967373eSDavid Greenman  * ENTRY() has to align to because there may be no ALTENTRY() before it.
122912e6037SBruce Evans  * If there is a previous ALTENTRY() then the alignment code for ENTRY()
123912e6037SBruce Evans  * is empty.
1240967373eSDavid Greenman  */
125912e6037SBruce Evans #define ALTENTRY(name)		GEN_ENTRY(name)
126435929a8SBruce Evans #define	CROSSJUMP(jtrue, label, jfalse)	jtrue label
127435929a8SBruce Evans #define	CROSSJUMPTARGET(label)
128912e6037SBruce Evans #define ENTRY(name)		GEN_ENTRY(name)
129912e6037SBruce Evans #define FAKE_MCOUNT(caller)
130d2306226SDavid Greenman #define MCOUNT
131912e6037SBruce Evans #define MCOUNT_LABEL(name)
132912e6037SBruce Evans #define MEXITCOUNT
133912e6037SBruce Evans #endif /* GPROF */
1340967373eSDavid Greenman 
13518e3d9f5SScott Long /*
13618e3d9f5SScott Long  * Convenience for adding frame pointers to hand-coded ASM.  Useful for
13718e3d9f5SScott Long  * DTrace, HWPMC, and KDB.
13818e3d9f5SScott Long  */
13918e3d9f5SScott Long #define PUSH_FRAME_POINTER	\
14018e3d9f5SScott Long 	pushq	%rbp ;		\
14118e3d9f5SScott Long 	movq	%rsp, %rbp ;
14218e3d9f5SScott Long #define POP_FRAME_POINTER	\
14318e3d9f5SScott Long 	popq	%rbp
14418e3d9f5SScott Long 
1450d2a2989SPeter Wemm #ifdef LOCORE
1460d2a2989SPeter Wemm /*
1478d0593f5SPeter Wemm  * Convenience macro for declaring interrupt entry points.
1480d2a2989SPeter Wemm  */
1490d2a2989SPeter Wemm #define	IDTVEC(name)	ALIGN_TEXT; .globl __CONCAT(X,name); \
1500d2a2989SPeter Wemm 			.type __CONCAT(X,name),@function; __CONCAT(X,name):
1510d2a2989SPeter Wemm 
152333b8de5SJohn Baldwin /*
153333b8de5SJohn Baldwin  * Macros to create and destroy a trap frame.
154333b8de5SJohn Baldwin  */
155333b8de5SJohn Baldwin #define PUSH_FRAME							\
156333b8de5SJohn Baldwin 	subq	$TF_RIP,%rsp ;	/* skip dummy tf_err and tf_trapno */	\
157333b8de5SJohn Baldwin 	testb	$SEL_RPL_MASK,TF_CS(%rsp) ; /* come from kernel? */	\
158333b8de5SJohn Baldwin 	jz	1f ;		/* Yes, dont swapgs again */		\
159333b8de5SJohn Baldwin 	swapgs ;							\
160333b8de5SJohn Baldwin 1:	movq	%rdi,TF_RDI(%rsp) ;					\
161333b8de5SJohn Baldwin 	movq	%rsi,TF_RSI(%rsp) ;					\
162333b8de5SJohn Baldwin 	movq	%rdx,TF_RDX(%rsp) ;					\
163333b8de5SJohn Baldwin 	movq	%rcx,TF_RCX(%rsp) ;					\
164333b8de5SJohn Baldwin 	movq	%r8,TF_R8(%rsp) ;					\
165333b8de5SJohn Baldwin 	movq	%r9,TF_R9(%rsp) ;					\
166333b8de5SJohn Baldwin 	movq	%rax,TF_RAX(%rsp) ;					\
167333b8de5SJohn Baldwin 	movq	%rbx,TF_RBX(%rsp) ;					\
168333b8de5SJohn Baldwin 	movq	%rbp,TF_RBP(%rsp) ;					\
169333b8de5SJohn Baldwin 	movq	%r10,TF_R10(%rsp) ;					\
170333b8de5SJohn Baldwin 	movq	%r11,TF_R11(%rsp) ;					\
171333b8de5SJohn Baldwin 	movq	%r12,TF_R12(%rsp) ;					\
172333b8de5SJohn Baldwin 	movq	%r13,TF_R13(%rsp) ;					\
173333b8de5SJohn Baldwin 	movq	%r14,TF_R14(%rsp) ;					\
1742c66cccaSKonstantin Belousov 	movq	%r15,TF_R15(%rsp) ;					\
1752c66cccaSKonstantin Belousov 	movw	%fs,TF_FS(%rsp) ;					\
1762c66cccaSKonstantin Belousov 	movw	%gs,TF_GS(%rsp) ;					\
1772c66cccaSKonstantin Belousov 	movw	%es,TF_ES(%rsp) ;					\
1782c66cccaSKonstantin Belousov 	movw	%ds,TF_DS(%rsp) ;					\
179595473a5SKonstantin Belousov 	movl	$TF_HASSEGS,TF_FLAGS(%rsp) ;				\
180595473a5SKonstantin Belousov 	cld
181333b8de5SJohn Baldwin 
182333b8de5SJohn Baldwin #define POP_FRAME							\
183333b8de5SJohn Baldwin 	movq	TF_RDI(%rsp),%rdi ;					\
184333b8de5SJohn Baldwin 	movq	TF_RSI(%rsp),%rsi ;					\
185333b8de5SJohn Baldwin 	movq	TF_RDX(%rsp),%rdx ;					\
186333b8de5SJohn Baldwin 	movq	TF_RCX(%rsp),%rcx ;					\
187333b8de5SJohn Baldwin 	movq	TF_R8(%rsp),%r8 ;					\
188333b8de5SJohn Baldwin 	movq	TF_R9(%rsp),%r9 ;					\
189333b8de5SJohn Baldwin 	movq	TF_RAX(%rsp),%rax ;					\
190333b8de5SJohn Baldwin 	movq	TF_RBX(%rsp),%rbx ;					\
191333b8de5SJohn Baldwin 	movq	TF_RBP(%rsp),%rbp ;					\
192333b8de5SJohn Baldwin 	movq	TF_R10(%rsp),%r10 ;					\
193333b8de5SJohn Baldwin 	movq	TF_R11(%rsp),%r11 ;					\
194333b8de5SJohn Baldwin 	movq	TF_R12(%rsp),%r12 ;					\
195333b8de5SJohn Baldwin 	movq	TF_R13(%rsp),%r13 ;					\
196333b8de5SJohn Baldwin 	movq	TF_R14(%rsp),%r14 ;					\
197333b8de5SJohn Baldwin 	movq	TF_R15(%rsp),%r15 ;					\
198333b8de5SJohn Baldwin 	testb	$SEL_RPL_MASK,TF_CS(%rsp) ; /* come from kernel? */	\
199333b8de5SJohn Baldwin 	jz	1f ;		/* keep kernel GS.base */		\
200333b8de5SJohn Baldwin 	cli ;								\
201333b8de5SJohn Baldwin 	swapgs ;							\
202333b8de5SJohn Baldwin 1:	addq	$TF_RIP,%rsp	/* skip over tf_err, tf_trapno */
203333b8de5SJohn Baldwin 
204333b8de5SJohn Baldwin /*
205333b8de5SJohn Baldwin  * Access per-CPU data.
206333b8de5SJohn Baldwin  */
207333b8de5SJohn Baldwin #define	PCPU(member)	%gs:PC_ ## member
208333b8de5SJohn Baldwin #define	PCPU_ADDR(member, reg)					\
209333b8de5SJohn Baldwin 	movq %gs:PC_PRVSPACE, reg ;				\
210333b8de5SJohn Baldwin 	addq $PC_ ## member, reg
211333b8de5SJohn Baldwin 
2120d2a2989SPeter Wemm #endif /* LOCORE */
2130d2a2989SPeter Wemm 
2141a9cdd37SRoger Pau Monné #ifdef __STDC__
2151a9cdd37SRoger Pau Monné #define ELFNOTE(name, type, desctype, descdata...) \
2161a9cdd37SRoger Pau Monné .pushsection .note.name                 ;       \
2171a9cdd37SRoger Pau Monné   .align 4                              ;       \
2181a9cdd37SRoger Pau Monné   .long 2f - 1f         /* namesz */    ;       \
2191a9cdd37SRoger Pau Monné   .long 4f - 3f         /* descsz */    ;       \
2201a9cdd37SRoger Pau Monné   .long type                            ;       \
2211a9cdd37SRoger Pau Monné 1:.asciz #name                          ;       \
2221a9cdd37SRoger Pau Monné 2:.align 4                              ;       \
2231a9cdd37SRoger Pau Monné 3:desctype descdata                     ;       \
2241a9cdd37SRoger Pau Monné 4:.align 4                              ;       \
2251a9cdd37SRoger Pau Monné .popsection
2261a9cdd37SRoger Pau Monné #else /* !__STDC__, i.e. -traditional */
2271a9cdd37SRoger Pau Monné #define ELFNOTE(name, type, desctype, descdata) \
2281a9cdd37SRoger Pau Monné .pushsection .note.name                 ;       \
2291a9cdd37SRoger Pau Monné   .align 4                              ;       \
2301a9cdd37SRoger Pau Monné   .long 2f - 1f         /* namesz */    ;       \
2311a9cdd37SRoger Pau Monné   .long 4f - 3f         /* descsz */    ;       \
2321a9cdd37SRoger Pau Monné   .long type                            ;       \
2331a9cdd37SRoger Pau Monné 1:.asciz "name"                         ;       \
2341a9cdd37SRoger Pau Monné 2:.align 4                              ;       \
2351a9cdd37SRoger Pau Monné 3:desctype descdata                     ;       \
2361a9cdd37SRoger Pau Monné 4:.align 4                              ;       \
2371a9cdd37SRoger Pau Monné .popsection
2381a9cdd37SRoger Pau Monné #endif /* __STDC__ */
2391a9cdd37SRoger Pau Monné 
240ab9678acSBruce Evans #endif /* !_MACHINE_ASMACROS_H_ */
241