xref: /titanic_52/usr/src/boot/sys/amd64/include/asmacros.h (revision 4a5d661a82b942b6538acd26209d959ce98b593a)
1*4a5d661aSToomas Soome /*-
2*4a5d661aSToomas Soome  * Copyright (c) 1993 The Regents of the University of California.
3*4a5d661aSToomas Soome  * All rights reserved.
4*4a5d661aSToomas Soome  *
5*4a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
6*4a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
7*4a5d661aSToomas Soome  * are met:
8*4a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
9*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
10*4a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12*4a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
13*4a5d661aSToomas Soome  * 4. Neither the name of the University nor the names of its contributors
14*4a5d661aSToomas Soome  *    may be used to endorse or promote products derived from this software
15*4a5d661aSToomas Soome  *    without specific prior written permission.
16*4a5d661aSToomas Soome  *
17*4a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*4a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*4a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*4a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*4a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*4a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*4a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*4a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*4a5d661aSToomas Soome  * SUCH DAMAGE.
28*4a5d661aSToomas Soome  *
29*4a5d661aSToomas Soome  * $FreeBSD$
30*4a5d661aSToomas Soome  */
31*4a5d661aSToomas Soome 
32*4a5d661aSToomas Soome #ifndef _MACHINE_ASMACROS_H_
33*4a5d661aSToomas Soome #define _MACHINE_ASMACROS_H_
34*4a5d661aSToomas Soome 
35*4a5d661aSToomas Soome #include <sys/cdefs.h>
36*4a5d661aSToomas Soome 
37*4a5d661aSToomas Soome /* XXX too much duplication in various asm*.h's. */
38*4a5d661aSToomas Soome 
39*4a5d661aSToomas Soome /*
40*4a5d661aSToomas Soome  * CNAME is used to manage the relationship between symbol names in C
41*4a5d661aSToomas Soome  * and the equivalent assembly language names.  CNAME is given a name as
42*4a5d661aSToomas Soome  * it would be used in a C program.  It expands to the equivalent assembly
43*4a5d661aSToomas Soome  * language name.
44*4a5d661aSToomas Soome  */
45*4a5d661aSToomas Soome #define CNAME(csym)		csym
46*4a5d661aSToomas Soome 
47*4a5d661aSToomas Soome #define ALIGN_DATA	.p2align 3	/* 8 byte alignment, zero filled */
48*4a5d661aSToomas Soome #ifdef GPROF
49*4a5d661aSToomas Soome #define ALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
50*4a5d661aSToomas Soome #else
51*4a5d661aSToomas Soome #define ALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
52*4a5d661aSToomas Soome #endif
53*4a5d661aSToomas Soome #define SUPERALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
54*4a5d661aSToomas Soome 
55*4a5d661aSToomas Soome #define GEN_ENTRY(name)		ALIGN_TEXT; .globl CNAME(name); \
56*4a5d661aSToomas Soome 				.type CNAME(name),@function; CNAME(name):
57*4a5d661aSToomas Soome #define NON_GPROF_ENTRY(name)	GEN_ENTRY(name)
58*4a5d661aSToomas Soome #define NON_GPROF_RET		.byte 0xc3	/* opcode for `ret' */
59*4a5d661aSToomas Soome 
60*4a5d661aSToomas Soome #define	END(name)		.size name, . - name
61*4a5d661aSToomas Soome 
62*4a5d661aSToomas Soome #ifdef GPROF
63*4a5d661aSToomas Soome /*
64*4a5d661aSToomas Soome  * __mcount is like [.]mcount except that doesn't require its caller to set
65*4a5d661aSToomas Soome  * up a frame pointer.  It must be called before pushing anything onto the
66*4a5d661aSToomas Soome  * stack.  gcc should eventually generate code to call __mcount in most
67*4a5d661aSToomas Soome  * cases.  This would make -pg in combination with -fomit-frame-pointer
68*4a5d661aSToomas Soome  * useful.  gcc has a configuration variable PROFILE_BEFORE_PROLOGUE to
69*4a5d661aSToomas Soome  * allow profiling before setting up the frame pointer, but this is
70*4a5d661aSToomas Soome  * inadequate for good handling of special cases, e.g., -fpic works best
71*4a5d661aSToomas Soome  * with profiling after the prologue.
72*4a5d661aSToomas Soome  *
73*4a5d661aSToomas Soome  * [.]mexitcount is a new function to support non-statistical profiling if an
74*4a5d661aSToomas Soome  * accurate clock is available.  For C sources, calls to it are generated
75*4a5d661aSToomas Soome  * by the FreeBSD extension `-mprofiler-epilogue' to gcc.  It is best to
76*4a5d661aSToomas Soome  * call [.]mexitcount at the end of a function like the MEXITCOUNT macro does,
77*4a5d661aSToomas Soome  * but gcc currently generates calls to it at the start of the epilogue to
78*4a5d661aSToomas Soome  * avoid problems with -fpic.
79*4a5d661aSToomas Soome  *
80*4a5d661aSToomas Soome  * [.]mcount and __mcount may clobber the call-used registers and %ef.
81*4a5d661aSToomas Soome  * [.]mexitcount may clobber %ecx and %ef.
82*4a5d661aSToomas Soome  *
83*4a5d661aSToomas Soome  * Cross-jumping makes non-statistical profiling timing more complicated.
84*4a5d661aSToomas Soome  * It is handled in many cases by calling [.]mexitcount before jumping.  It
85*4a5d661aSToomas Soome  * is handled for conditional jumps using CROSSJUMP() and CROSSJUMP_LABEL().
86*4a5d661aSToomas Soome  * It is handled for some fault-handling jumps by not sharing the exit
87*4a5d661aSToomas Soome  * routine.
88*4a5d661aSToomas Soome  *
89*4a5d661aSToomas Soome  * ALTENTRY() must be before a corresponding ENTRY() so that it can jump to
90*4a5d661aSToomas Soome  * the main entry point.  Note that alt entries are counted twice.  They
91*4a5d661aSToomas Soome  * have to be counted as ordinary entries for gprof to get the call times
92*4a5d661aSToomas Soome  * right for the ordinary entries.
93*4a5d661aSToomas Soome  *
94*4a5d661aSToomas Soome  * High local labels are used in macros to avoid clashes with local labels
95*4a5d661aSToomas Soome  * in functions.
96*4a5d661aSToomas Soome  *
97*4a5d661aSToomas Soome  * Ordinary `ret' is used instead of a macro `RET' because there are a lot
98*4a5d661aSToomas Soome  * of `ret's.  0xc3 is the opcode for `ret' (`#define ret ... ret' can't
99*4a5d661aSToomas Soome  * be used because this file is sometimes preprocessed in traditional mode).
100*4a5d661aSToomas Soome  * `ret' clobbers eflags but this doesn't matter.
101*4a5d661aSToomas Soome  */
102*4a5d661aSToomas Soome #define ALTENTRY(name)		GEN_ENTRY(name) ; MCOUNT ; MEXITCOUNT ; jmp 9f
103*4a5d661aSToomas Soome #define	CROSSJUMP(jtrue, label, jfalse) \
104*4a5d661aSToomas Soome 	jfalse 8f; MEXITCOUNT; jmp __CONCAT(to,label); 8:
105*4a5d661aSToomas Soome #define CROSSJUMPTARGET(label) \
106*4a5d661aSToomas Soome 	ALIGN_TEXT; __CONCAT(to,label): ; MCOUNT; jmp label
107*4a5d661aSToomas Soome #define ENTRY(name)		GEN_ENTRY(name) ; 9: ; MCOUNT
108*4a5d661aSToomas Soome #define FAKE_MCOUNT(caller)	pushq caller ; call __mcount ; popq %rcx
109*4a5d661aSToomas Soome #define MCOUNT			call __mcount
110*4a5d661aSToomas Soome #define MCOUNT_LABEL(name)	GEN_ENTRY(name) ; nop ; ALIGN_TEXT
111*4a5d661aSToomas Soome #ifdef GUPROF
112*4a5d661aSToomas Soome #define MEXITCOUNT		call .mexitcount
113*4a5d661aSToomas Soome #define ret			MEXITCOUNT ; NON_GPROF_RET
114*4a5d661aSToomas Soome #else
115*4a5d661aSToomas Soome #define MEXITCOUNT
116*4a5d661aSToomas Soome #endif
117*4a5d661aSToomas Soome 
118*4a5d661aSToomas Soome #else /* !GPROF */
119*4a5d661aSToomas Soome /*
120*4a5d661aSToomas Soome  * ALTENTRY() has to align because it is before a corresponding ENTRY().
121*4a5d661aSToomas Soome  * ENTRY() has to align to because there may be no ALTENTRY() before it.
122*4a5d661aSToomas Soome  * If there is a previous ALTENTRY() then the alignment code for ENTRY()
123*4a5d661aSToomas Soome  * is empty.
124*4a5d661aSToomas Soome  */
125*4a5d661aSToomas Soome #define ALTENTRY(name)		GEN_ENTRY(name)
126*4a5d661aSToomas Soome #define	CROSSJUMP(jtrue, label, jfalse)	jtrue label
127*4a5d661aSToomas Soome #define	CROSSJUMPTARGET(label)
128*4a5d661aSToomas Soome #define ENTRY(name)		GEN_ENTRY(name)
129*4a5d661aSToomas Soome #define FAKE_MCOUNT(caller)
130*4a5d661aSToomas Soome #define MCOUNT
131*4a5d661aSToomas Soome #define MCOUNT_LABEL(name)
132*4a5d661aSToomas Soome #define MEXITCOUNT
133*4a5d661aSToomas Soome #endif /* GPROF */
134*4a5d661aSToomas Soome 
135*4a5d661aSToomas Soome /*
136*4a5d661aSToomas Soome  * Convenience for adding frame pointers to hand-coded ASM.  Useful for
137*4a5d661aSToomas Soome  * DTrace, HWPMC, and KDB.
138*4a5d661aSToomas Soome  */
139*4a5d661aSToomas Soome #define PUSH_FRAME_POINTER	\
140*4a5d661aSToomas Soome 	pushq	%rbp ;		\
141*4a5d661aSToomas Soome 	movq	%rsp, %rbp ;
142*4a5d661aSToomas Soome #define POP_FRAME_POINTER	\
143*4a5d661aSToomas Soome 	popq	%rbp
144*4a5d661aSToomas Soome 
145*4a5d661aSToomas Soome #ifdef LOCORE
146*4a5d661aSToomas Soome /*
147*4a5d661aSToomas Soome  * Convenience macro for declaring interrupt entry points.
148*4a5d661aSToomas Soome  */
149*4a5d661aSToomas Soome #define	IDTVEC(name)	ALIGN_TEXT; .globl __CONCAT(X,name); \
150*4a5d661aSToomas Soome 			.type __CONCAT(X,name),@function; __CONCAT(X,name):
151*4a5d661aSToomas Soome 
152*4a5d661aSToomas Soome /*
153*4a5d661aSToomas Soome  * Macros to create and destroy a trap frame.
154*4a5d661aSToomas Soome  */
155*4a5d661aSToomas Soome #define PUSH_FRAME							\
156*4a5d661aSToomas Soome 	subq	$TF_RIP,%rsp ;	/* skip dummy tf_err and tf_trapno */	\
157*4a5d661aSToomas Soome 	testb	$SEL_RPL_MASK,TF_CS(%rsp) ; /* come from kernel? */	\
158*4a5d661aSToomas Soome 	jz	1f ;		/* Yes, dont swapgs again */		\
159*4a5d661aSToomas Soome 	swapgs ;							\
160*4a5d661aSToomas Soome 1:	movq	%rdi,TF_RDI(%rsp) ;					\
161*4a5d661aSToomas Soome 	movq	%rsi,TF_RSI(%rsp) ;					\
162*4a5d661aSToomas Soome 	movq	%rdx,TF_RDX(%rsp) ;					\
163*4a5d661aSToomas Soome 	movq	%rcx,TF_RCX(%rsp) ;					\
164*4a5d661aSToomas Soome 	movq	%r8,TF_R8(%rsp) ;					\
165*4a5d661aSToomas Soome 	movq	%r9,TF_R9(%rsp) ;					\
166*4a5d661aSToomas Soome 	movq	%rax,TF_RAX(%rsp) ;					\
167*4a5d661aSToomas Soome 	movq	%rbx,TF_RBX(%rsp) ;					\
168*4a5d661aSToomas Soome 	movq	%rbp,TF_RBP(%rsp) ;					\
169*4a5d661aSToomas Soome 	movq	%r10,TF_R10(%rsp) ;					\
170*4a5d661aSToomas Soome 	movq	%r11,TF_R11(%rsp) ;					\
171*4a5d661aSToomas Soome 	movq	%r12,TF_R12(%rsp) ;					\
172*4a5d661aSToomas Soome 	movq	%r13,TF_R13(%rsp) ;					\
173*4a5d661aSToomas Soome 	movq	%r14,TF_R14(%rsp) ;					\
174*4a5d661aSToomas Soome 	movq	%r15,TF_R15(%rsp) ;					\
175*4a5d661aSToomas Soome 	movw	%fs,TF_FS(%rsp) ;					\
176*4a5d661aSToomas Soome 	movw	%gs,TF_GS(%rsp) ;					\
177*4a5d661aSToomas Soome 	movw	%es,TF_ES(%rsp) ;					\
178*4a5d661aSToomas Soome 	movw	%ds,TF_DS(%rsp) ;					\
179*4a5d661aSToomas Soome 	movl	$TF_HASSEGS,TF_FLAGS(%rsp) ;				\
180*4a5d661aSToomas Soome 	cld
181*4a5d661aSToomas Soome 
182*4a5d661aSToomas Soome #define POP_FRAME							\
183*4a5d661aSToomas Soome 	movq	TF_RDI(%rsp),%rdi ;					\
184*4a5d661aSToomas Soome 	movq	TF_RSI(%rsp),%rsi ;					\
185*4a5d661aSToomas Soome 	movq	TF_RDX(%rsp),%rdx ;					\
186*4a5d661aSToomas Soome 	movq	TF_RCX(%rsp),%rcx ;					\
187*4a5d661aSToomas Soome 	movq	TF_R8(%rsp),%r8 ;					\
188*4a5d661aSToomas Soome 	movq	TF_R9(%rsp),%r9 ;					\
189*4a5d661aSToomas Soome 	movq	TF_RAX(%rsp),%rax ;					\
190*4a5d661aSToomas Soome 	movq	TF_RBX(%rsp),%rbx ;					\
191*4a5d661aSToomas Soome 	movq	TF_RBP(%rsp),%rbp ;					\
192*4a5d661aSToomas Soome 	movq	TF_R10(%rsp),%r10 ;					\
193*4a5d661aSToomas Soome 	movq	TF_R11(%rsp),%r11 ;					\
194*4a5d661aSToomas Soome 	movq	TF_R12(%rsp),%r12 ;					\
195*4a5d661aSToomas Soome 	movq	TF_R13(%rsp),%r13 ;					\
196*4a5d661aSToomas Soome 	movq	TF_R14(%rsp),%r14 ;					\
197*4a5d661aSToomas Soome 	movq	TF_R15(%rsp),%r15 ;					\
198*4a5d661aSToomas Soome 	testb	$SEL_RPL_MASK,TF_CS(%rsp) ; /* come from kernel? */	\
199*4a5d661aSToomas Soome 	jz	1f ;		/* keep kernel GS.base */		\
200*4a5d661aSToomas Soome 	cli ;								\
201*4a5d661aSToomas Soome 	swapgs ;							\
202*4a5d661aSToomas Soome 1:	addq	$TF_RIP,%rsp	/* skip over tf_err, tf_trapno */
203*4a5d661aSToomas Soome 
204*4a5d661aSToomas Soome /*
205*4a5d661aSToomas Soome  * Access per-CPU data.
206*4a5d661aSToomas Soome  */
207*4a5d661aSToomas Soome #define	PCPU(member)	%gs:PC_ ## member
208*4a5d661aSToomas Soome #define	PCPU_ADDR(member, reg)					\
209*4a5d661aSToomas Soome 	movq %gs:PC_PRVSPACE, reg ;				\
210*4a5d661aSToomas Soome 	addq $PC_ ## member, reg
211*4a5d661aSToomas Soome 
212*4a5d661aSToomas Soome #endif /* LOCORE */
213*4a5d661aSToomas Soome 
214*4a5d661aSToomas Soome #ifdef __STDC__
215*4a5d661aSToomas Soome #define ELFNOTE(name, type, desctype, descdata...) \
216*4a5d661aSToomas Soome .pushsection .note.name                 ;       \
217*4a5d661aSToomas Soome   .align 4                              ;       \
218*4a5d661aSToomas Soome   .long 2f - 1f         /* namesz */    ;       \
219*4a5d661aSToomas Soome   .long 4f - 3f         /* descsz */    ;       \
220*4a5d661aSToomas Soome   .long type                            ;       \
221*4a5d661aSToomas Soome 1:.asciz #name                          ;       \
222*4a5d661aSToomas Soome 2:.align 4                              ;       \
223*4a5d661aSToomas Soome 3:desctype descdata                     ;       \
224*4a5d661aSToomas Soome 4:.align 4                              ;       \
225*4a5d661aSToomas Soome .popsection
226*4a5d661aSToomas Soome #else /* !__STDC__, i.e. -traditional */
227*4a5d661aSToomas Soome #define ELFNOTE(name, type, desctype, descdata) \
228*4a5d661aSToomas Soome .pushsection .note.name                 ;       \
229*4a5d661aSToomas Soome   .align 4                              ;       \
230*4a5d661aSToomas Soome   .long 2f - 1f         /* namesz */    ;       \
231*4a5d661aSToomas Soome   .long 4f - 3f         /* descsz */    ;       \
232*4a5d661aSToomas Soome   .long type                            ;       \
233*4a5d661aSToomas Soome 1:.asciz "name"                         ;       \
234*4a5d661aSToomas Soome 2:.align 4                              ;       \
235*4a5d661aSToomas Soome 3:desctype descdata                     ;       \
236*4a5d661aSToomas Soome 4:.align 4                              ;       \
237*4a5d661aSToomas Soome .popsection
238*4a5d661aSToomas Soome #endif /* __STDC__ */
239*4a5d661aSToomas Soome 
240*4a5d661aSToomas Soome #endif /* !_MACHINE_ASMACROS_H_ */
241