xref: /freebsd/sys/amd64/include/asmacros.h (revision 94450a83e82dea2e31ab3b5afaf1df3a9cb7e6bd)
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.
133c4dd356SDavid Greenman  * 4. 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 
600967373eSDavid Greenman #ifdef GPROF
610967373eSDavid Greenman /*
62ea2b3e3dSBruce Evans  * __mcount is like [.]mcount except that doesn't require its caller to set
63912e6037SBruce Evans  * up a frame pointer.  It must be called before pushing anything onto the
64912e6037SBruce Evans  * stack.  gcc should eventually generate code to call __mcount in most
65912e6037SBruce Evans  * cases.  This would make -pg in combination with -fomit-frame-pointer
66912e6037SBruce Evans  * useful.  gcc has a configuration variable PROFILE_BEFORE_PROLOGUE to
67912e6037SBruce Evans  * allow profiling before setting up the frame pointer, but this is
68912e6037SBruce Evans  * inadequate for good handling of special cases, e.g., -fpic works best
69912e6037SBruce Evans  * with profiling after the prologue.
70912e6037SBruce Evans  *
71ea2b3e3dSBruce Evans  * [.]mexitcount is a new function to support non-statistical profiling if an
72435929a8SBruce Evans  * accurate clock is available.  For C sources, calls to it are generated
73435929a8SBruce Evans  * by the FreeBSD extension `-mprofiler-epilogue' to gcc.  It is best to
74ea2b3e3dSBruce Evans  * call [.]mexitcount at the end of a function like the MEXITCOUNT macro does,
75435929a8SBruce Evans  * but gcc currently generates calls to it at the start of the epilogue to
76435929a8SBruce Evans  * avoid problems with -fpic.
77912e6037SBruce Evans  *
78ea2b3e3dSBruce Evans  * [.]mcount and __mcount may clobber the call-used registers and %ef.
79ea2b3e3dSBruce Evans  * [.]mexitcount may clobber %ecx and %ef.
80912e6037SBruce Evans  *
81435929a8SBruce Evans  * Cross-jumping makes non-statistical profiling timing more complicated.
82ea2b3e3dSBruce Evans  * It is handled in many cases by calling [.]mexitcount before jumping.  It
83ea2b3e3dSBruce Evans  * is handled for conditional jumps using CROSSJUMP() and CROSSJUMP_LABEL().
84435929a8SBruce Evans  * It is handled for some fault-handling jumps by not sharing the exit
85435929a8SBruce Evans  * routine.
86912e6037SBruce Evans  *
87912e6037SBruce Evans  * ALTENTRY() must be before a corresponding ENTRY() so that it can jump to
88912e6037SBruce Evans  * the main entry point.  Note that alt entries are counted twice.  They
89912e6037SBruce Evans  * have to be counted as ordinary entries for gprof to get the call times
90912e6037SBruce Evans  * right for the ordinary entries.
91912e6037SBruce Evans  *
92912e6037SBruce Evans  * High local labels are used in macros to avoid clashes with local labels
93912e6037SBruce Evans  * in functions.
94912e6037SBruce Evans  *
95435929a8SBruce Evans  * Ordinary `ret' is used instead of a macro `RET' because there are a lot
96435929a8SBruce Evans  * of `ret's.  0xc3 is the opcode for `ret' (`#define ret ... ret' can't
97435929a8SBruce Evans  * be used because this file is sometimes preprocessed in traditional mode).
98435929a8SBruce Evans  * `ret' clobbers eflags but this doesn't matter.
990967373eSDavid Greenman  */
100912e6037SBruce Evans #define ALTENTRY(name)		GEN_ENTRY(name) ; MCOUNT ; MEXITCOUNT ; jmp 9f
101435929a8SBruce Evans #define	CROSSJUMP(jtrue, label, jfalse) \
102435929a8SBruce Evans 	jfalse 8f; MEXITCOUNT; jmp __CONCAT(to,label); 8:
103435929a8SBruce Evans #define CROSSJUMPTARGET(label) \
104435929a8SBruce Evans 	ALIGN_TEXT; __CONCAT(to,label): ; MCOUNT; jmp label
105912e6037SBruce Evans #define ENTRY(name)		GEN_ENTRY(name) ; 9: ; MCOUNT
106cda07865SPeter Wemm #define FAKE_MCOUNT(caller)	pushq caller ; call __mcount ; popq %rcx
107912e6037SBruce Evans #define MCOUNT			call __mcount
108912e6037SBruce Evans #define MCOUNT_LABEL(name)	GEN_ENTRY(name) ; nop ; ALIGN_TEXT
10944764790SBruce Evans #ifdef GUPROF
11094450a83SBruce Evans #define MEXITCOUNT		call .mexitcount
111435929a8SBruce Evans #define ret			MEXITCOUNT ; NON_GPROF_RET
11244764790SBruce Evans #else
11344764790SBruce Evans #define MEXITCOUNT
11444764790SBruce Evans #endif
115435929a8SBruce Evans 
116435929a8SBruce Evans #else /* !GPROF */
1170967373eSDavid Greenman /*
1180967373eSDavid Greenman  * ALTENTRY() has to align because it is before a corresponding ENTRY().
1190967373eSDavid Greenman  * ENTRY() has to align to because there may be no ALTENTRY() before it.
120912e6037SBruce Evans  * If there is a previous ALTENTRY() then the alignment code for ENTRY()
121912e6037SBruce Evans  * is empty.
1220967373eSDavid Greenman  */
123912e6037SBruce Evans #define ALTENTRY(name)		GEN_ENTRY(name)
124435929a8SBruce Evans #define	CROSSJUMP(jtrue, label, jfalse)	jtrue label
125435929a8SBruce Evans #define	CROSSJUMPTARGET(label)
126912e6037SBruce Evans #define ENTRY(name)		GEN_ENTRY(name)
127912e6037SBruce Evans #define FAKE_MCOUNT(caller)
128d2306226SDavid Greenman #define MCOUNT
129912e6037SBruce Evans #define MCOUNT_LABEL(name)
130912e6037SBruce Evans #define MEXITCOUNT
131912e6037SBruce Evans #endif /* GPROF */
1320967373eSDavid Greenman 
1330d2a2989SPeter Wemm #ifdef LOCORE
1340d2a2989SPeter Wemm /*
1358d0593f5SPeter Wemm  * Convenience macro for declaring interrupt entry points.
1360d2a2989SPeter Wemm  */
1370d2a2989SPeter Wemm #define	IDTVEC(name)	ALIGN_TEXT; .globl __CONCAT(X,name); \
1380d2a2989SPeter Wemm 			.type __CONCAT(X,name),@function; __CONCAT(X,name):
1390d2a2989SPeter Wemm 
140333b8de5SJohn Baldwin /*
141333b8de5SJohn Baldwin  * Macros to create and destroy a trap frame.
142333b8de5SJohn Baldwin  */
143333b8de5SJohn Baldwin #define PUSH_FRAME							\
144333b8de5SJohn Baldwin 	subq	$TF_RIP,%rsp ;	/* skip dummy tf_err and tf_trapno */	\
145333b8de5SJohn Baldwin 	testb	$SEL_RPL_MASK,TF_CS(%rsp) ; /* come from kernel? */	\
146333b8de5SJohn Baldwin 	jz	1f ;		/* Yes, dont swapgs again */		\
147333b8de5SJohn Baldwin 	swapgs ;							\
148333b8de5SJohn Baldwin 1:	movq	%rdi,TF_RDI(%rsp) ;					\
149333b8de5SJohn Baldwin 	movq	%rsi,TF_RSI(%rsp) ;					\
150333b8de5SJohn Baldwin 	movq	%rdx,TF_RDX(%rsp) ;					\
151333b8de5SJohn Baldwin 	movq	%rcx,TF_RCX(%rsp) ;					\
152333b8de5SJohn Baldwin 	movq	%r8,TF_R8(%rsp) ;					\
153333b8de5SJohn Baldwin 	movq	%r9,TF_R9(%rsp) ;					\
154333b8de5SJohn Baldwin 	movq	%rax,TF_RAX(%rsp) ;					\
155333b8de5SJohn Baldwin 	movq	%rbx,TF_RBX(%rsp) ;					\
156333b8de5SJohn Baldwin 	movq	%rbp,TF_RBP(%rsp) ;					\
157333b8de5SJohn Baldwin 	movq	%r10,TF_R10(%rsp) ;					\
158333b8de5SJohn Baldwin 	movq	%r11,TF_R11(%rsp) ;					\
159333b8de5SJohn Baldwin 	movq	%r12,TF_R12(%rsp) ;					\
160333b8de5SJohn Baldwin 	movq	%r13,TF_R13(%rsp) ;					\
161333b8de5SJohn Baldwin 	movq	%r14,TF_R14(%rsp) ;					\
162333b8de5SJohn Baldwin 	movq	%r15,TF_R15(%rsp)
163333b8de5SJohn Baldwin 
164333b8de5SJohn Baldwin #define POP_FRAME							\
165333b8de5SJohn Baldwin 	movq	TF_RDI(%rsp),%rdi ;					\
166333b8de5SJohn Baldwin 	movq	TF_RSI(%rsp),%rsi ;					\
167333b8de5SJohn Baldwin 	movq	TF_RDX(%rsp),%rdx ;					\
168333b8de5SJohn Baldwin 	movq	TF_RCX(%rsp),%rcx ;					\
169333b8de5SJohn Baldwin 	movq	TF_R8(%rsp),%r8 ;					\
170333b8de5SJohn Baldwin 	movq	TF_R9(%rsp),%r9 ;					\
171333b8de5SJohn Baldwin 	movq	TF_RAX(%rsp),%rax ;					\
172333b8de5SJohn Baldwin 	movq	TF_RBX(%rsp),%rbx ;					\
173333b8de5SJohn Baldwin 	movq	TF_RBP(%rsp),%rbp ;					\
174333b8de5SJohn Baldwin 	movq	TF_R10(%rsp),%r10 ;					\
175333b8de5SJohn Baldwin 	movq	TF_R11(%rsp),%r11 ;					\
176333b8de5SJohn Baldwin 	movq	TF_R12(%rsp),%r12 ;					\
177333b8de5SJohn Baldwin 	movq	TF_R13(%rsp),%r13 ;					\
178333b8de5SJohn Baldwin 	movq	TF_R14(%rsp),%r14 ;					\
179333b8de5SJohn Baldwin 	movq	TF_R15(%rsp),%r15 ;					\
180333b8de5SJohn Baldwin 	testb	$SEL_RPL_MASK,TF_CS(%rsp) ; /* come from kernel? */	\
181333b8de5SJohn Baldwin 	jz	1f ;		/* keep kernel GS.base */		\
182333b8de5SJohn Baldwin 	cli ;								\
183333b8de5SJohn Baldwin 	swapgs ;							\
184333b8de5SJohn Baldwin 1:	addq	$TF_RIP,%rsp	/* skip over tf_err, tf_trapno */
185333b8de5SJohn Baldwin 
186333b8de5SJohn Baldwin /*
187333b8de5SJohn Baldwin  * Access per-CPU data.
188333b8de5SJohn Baldwin  */
189333b8de5SJohn Baldwin #define	PCPU(member)	%gs:PC_ ## member
190333b8de5SJohn Baldwin #define	PCPU_ADDR(member, reg)					\
191333b8de5SJohn Baldwin 	movq %gs:PC_PRVSPACE, reg ;				\
192333b8de5SJohn Baldwin 	addq $PC_ ## member, reg
193333b8de5SJohn Baldwin 
1940d2a2989SPeter Wemm #endif /* LOCORE */
1950d2a2989SPeter Wemm 
196ab9678acSBruce Evans #endif /* !_MACHINE_ASMACROS_H_ */
197