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