11817Sdg/*-
21817Sdg * Copyright (c) 1993 The Regents of the University of California.
31817Sdg * All rights reserved.
41817Sdg *
51817Sdg * Redistribution and use in source and binary forms, with or without
61817Sdg * modification, are permitted provided that the following conditions
71817Sdg * are met:
81817Sdg * 1. Redistributions of source code must retain the above copyright
91817Sdg *    notice, this list of conditions and the following disclaimer.
101817Sdg * 2. Redistributions in binary form must reproduce the above copyright
111817Sdg *    notice, this list of conditions and the following disclaimer in the
121817Sdg *    documentation and/or other materials provided with the distribution.
131817Sdg * 4. Neither the name of the University nor the names of its contributors
141817Sdg *    may be used to endorse or promote products derived from this software
151817Sdg *    without specific prior written permission.
161817Sdg *
171817Sdg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181817Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191817Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201817Sdg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211817Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221817Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231817Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241817Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251817Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261817Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271817Sdg * SUCH DAMAGE.
281817Sdg *
2950477Speter * $FreeBSD$
301817Sdg */
311817Sdg
322579Sbde#ifndef _MACHINE_ASMACROS_H_
332579Sbde#define _MACHINE_ASMACROS_H_
342123Sjkh
3516029Speter#include <sys/cdefs.h>
362579Sbde
3718961Sbde/* XXX too much duplication in various asm*.h's. */
3813107Sbde
3925083Sjdp/*
40163726Sbde * CNAME is used to manage the relationship between symbol names in C
4125083Sjdp * and the equivalent assembly language names.  CNAME is given a name as
4225083Sjdp * it would be used in a C program.  It expands to the equivalent assembly
43163726Sbde * language name.
4425083Sjdp */
4525083Sjdp#define CNAME(csym)		csym
4625083Sjdp
4725083Sjdp#define ALIGN_DATA	.p2align 2	/* 4 byte alignment, zero filled */
4822636Sbde#ifdef GPROF
4925083Sjdp#define ALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
5022636Sbde#else
5125083Sjdp#define ALIGN_TEXT	.p2align 2,0x90	/* 4-byte alignment, nop filled */
5222636Sbde#endif
5325083Sjdp#define SUPERALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
54757Sdg
5525083Sjdp#define GEN_ENTRY(name)		ALIGN_TEXT; .globl CNAME(name); \
5646548Sbde				.type CNAME(name),@function; CNAME(name):
5713107Sbde#define NON_GPROF_ENTRY(name)	GEN_ENTRY(name)
5818961Sbde#define NON_GPROF_RET		.byte 0xc3	/* opcode for `ret' */
59757Sdg
60171914Sjkoshy#define	END(name)		.size name, . - name
61171914Sjkoshy
62757Sdg#ifdef GPROF
63757Sdg/*
6446548Sbde * __mcount is like [.]mcount except that doesn't require its caller to set
6513107Sbde * up a frame pointer.  It must be called before pushing anything onto the
6613107Sbde * stack.  gcc should eventually generate code to call __mcount in most
6713107Sbde * cases.  This would make -pg in combination with -fomit-frame-pointer
6813107Sbde * useful.  gcc has a configuration variable PROFILE_BEFORE_PROLOGUE to
6913107Sbde * allow profiling before setting up the frame pointer, but this is
7013107Sbde * inadequate for good handling of special cases, e.g., -fpic works best
7113107Sbde * with profiling after the prologue.
7213107Sbde *
7346548Sbde * [.]mexitcount is a new function to support non-statistical profiling if an
7418961Sbde * accurate clock is available.  For C sources, calls to it are generated
7518961Sbde * by the FreeBSD extension `-mprofiler-epilogue' to gcc.  It is best to
7646548Sbde * call [.]mexitcount at the end of a function like the MEXITCOUNT macro does,
7718961Sbde * but gcc currently generates calls to it at the start of the epilogue to
7818961Sbde * avoid problems with -fpic.
7913107Sbde *
8046548Sbde * [.]mcount and __mcount may clobber the call-used registers and %ef.
8146548Sbde * [.]mexitcount may clobber %ecx and %ef.
8213107Sbde *
8318961Sbde * Cross-jumping makes non-statistical profiling timing more complicated.
8446548Sbde * It is handled in many cases by calling [.]mexitcount before jumping.  It
8546548Sbde * is handled for conditional jumps using CROSSJUMP() and CROSSJUMP_LABEL().
8618961Sbde * It is handled for some fault-handling jumps by not sharing the exit
8718961Sbde * routine.
8813107Sbde *
8913107Sbde * ALTENTRY() must be before a corresponding ENTRY() so that it can jump to
9013107Sbde * the main entry point.  Note that alt entries are counted twice.  They
9113107Sbde * have to be counted as ordinary entries for gprof to get the call times
9213107Sbde * right for the ordinary entries.
9313107Sbde *
9413107Sbde * High local labels are used in macros to avoid clashes with local labels
9513107Sbde * in functions.
9613107Sbde *
9718961Sbde * Ordinary `ret' is used instead of a macro `RET' because there are a lot
9818961Sbde * of `ret's.  0xc3 is the opcode for `ret' (`#define ret ... ret' can't
9918961Sbde * be used because this file is sometimes preprocessed in traditional mode).
10018961Sbde * `ret' clobbers eflags but this doesn't matter.
101757Sdg */
10213107Sbde#define ALTENTRY(name)		GEN_ENTRY(name) ; MCOUNT ; MEXITCOUNT ; jmp 9f
10318961Sbde#define	CROSSJUMP(jtrue, label, jfalse) \
10418961Sbde	jfalse 8f; MEXITCOUNT; jmp __CONCAT(to,label); 8:
10518961Sbde#define CROSSJUMPTARGET(label) \
10618961Sbde	ALIGN_TEXT; __CONCAT(to,label): ; MCOUNT; jmp label
10713107Sbde#define ENTRY(name)		GEN_ENTRY(name) ; 9: ; MCOUNT
10813107Sbde#define FAKE_MCOUNT(caller)	pushl caller ; call __mcount ; popl %ecx
10913107Sbde#define MCOUNT			call __mcount
11013107Sbde#define MCOUNT_LABEL(name)	GEN_ENTRY(name) ; nop ; ALIGN_TEXT
111163722Sbde#ifdef GUPROF
112163726Sbde#define MEXITCOUNT		call .mexitcount
11318961Sbde#define ret			MEXITCOUNT ; NON_GPROF_RET
114163722Sbde#else
115163722Sbde#define MEXITCOUNT
116163722Sbde#endif
11718961Sbde
11818961Sbde#else /* !GPROF */
119757Sdg/*
120757Sdg * ALTENTRY() has to align because it is before a corresponding ENTRY().
121757Sdg * ENTRY() has to align to because there may be no ALTENTRY() before it.
12213107Sbde * If there is a previous ALTENTRY() then the alignment code for ENTRY()
12313107Sbde * is empty.
124757Sdg */
12513107Sbde#define ALTENTRY(name)		GEN_ENTRY(name)
12618961Sbde#define	CROSSJUMP(jtrue, label, jfalse)	jtrue label
12718961Sbde#define	CROSSJUMPTARGET(label)
12813107Sbde#define ENTRY(name)		GEN_ENTRY(name)
12913107Sbde#define FAKE_MCOUNT(caller)
1301321Sdg#define MCOUNT
13113107Sbde#define MCOUNT_LABEL(name)
13213107Sbde#define MEXITCOUNT
13313107Sbde#endif /* GPROF */
134757Sdg
135121978Sjhb#ifdef LOCORE
136121978Sjhb/*
137153135Sjhb * Convenience macro for declaring interrupt entry points.
138121978Sjhb */
139121978Sjhb#define	IDTVEC(name)	ALIGN_TEXT; .globl __CONCAT(X,name); \
140121978Sjhb			.type __CONCAT(X,name),@function; __CONCAT(X,name):
141121978Sjhb
142153135Sjhb/*
143153135Sjhb * Macros to create and destroy a trap frame.
144153135Sjhb */
145153135Sjhb#define	PUSH_FRAME							\
146153135Sjhb	pushl	$0 ;		/* dummy error code */			\
147153135Sjhb	pushl	$0 ;		/* dummy trap type */			\
148153135Sjhb	pushal ;		/* 8 ints */				\
149153135Sjhb	pushl	%ds ;		/* save data and extra segments ... */	\
150153135Sjhb	pushl	%es ;							\
151153135Sjhb	pushl	%fs
152153135Sjhb
153153135Sjhb#define	POP_FRAME							\
154153135Sjhb	popl	%fs ;							\
155153135Sjhb	popl	%es ;							\
156153135Sjhb	popl	%ds ;							\
157153135Sjhb	popal ;								\
158153135Sjhb	addl	$4+4,%esp
159153135Sjhb
160153135Sjhb/*
161153135Sjhb * Access per-CPU data.
162153135Sjhb */
163153135Sjhb#define	PCPU(member)	%fs:PC_ ## member
164153135Sjhb
165153135Sjhb#define	PCPU_ADDR(member, reg)						\
166153135Sjhb	movl %fs:PC_PRVSPACE, reg ;					\
167153135Sjhb	addl $PC_ ## member, reg
168153135Sjhb
169153135Sjhb/*
170153135Sjhb * Setup the kernel segment registers.
171153135Sjhb */
172153135Sjhb#define	SET_KERNEL_SREGS						\
173153135Sjhb	movl	$KDSEL, %eax ;	/* reload with kernel's data segment */	\
174153135Sjhb	movl	%eax, %ds ;						\
175153135Sjhb	movl	%eax, %es ;						\
176153135Sjhb	movl	$KPSEL, %eax ;	/* reload with per-CPU data segment */	\
177153135Sjhb	movl	%eax, %fs
178153135Sjhb
179181775Skmacy#ifdef XEN
180181775Skmacy#define LOAD_CR3(reg)          \
181181775Skmacy        movl    reg,PCPU(CR3); \
182181775Skmacy        pushl   %ecx ;         \
183181775Skmacy        pushl   %edx ;         \
184181775Skmacy        pushl   %esi ;         \
185181775Skmacy        pushl   reg ;          \
186181775Skmacy        call    xen_load_cr3 ;     \
187181775Skmacy        addl    $4,%esp ;      \
188181775Skmacy        popl    %esi ;         \
189181775Skmacy        popl    %edx ;         \
190181775Skmacy        popl    %ecx ;         \
191181775Skmacy
192181775Skmacy#define READ_CR3(reg)   movl PCPU(CR3),reg;
193181775Skmacy#define LLDT(arg)                 \
194181775Skmacy        pushl   %edx ;                    \
195181775Skmacy        pushl   %eax ;                    \
196181775Skmacy        xorl    %eax,%eax ;               \
197181775Skmacy        movl    %eax,%gs ;                \
198181775Skmacy        call    i386_reset_ldt ;          \
199181775Skmacy        popl    %eax ;                    \
200181775Skmacy        popl    %edx
201181775Skmacy#define CLI             call ni_cli
202181775Skmacy#else
203181775Skmacy#define LOAD_CR3(reg)   movl reg,%cr3;
204181775Skmacy#define READ_CR3(reg)   movl %cr3,reg;
205181775Skmacy#define LLDT(arg)       lldt arg;
206181775Skmacy#define CLI             cli
207181775Skmacy#endif /* !XEN */
208181775Skmacy
209181775Skmacy
210121978Sjhb#endif /* LOCORE */
211121978Sjhb
212181775Skmacy#ifdef __STDC__
213181775Skmacy#define ELFNOTE(name, type, desctype, descdata...) \
214181775Skmacy.pushsection .note.name                 ;       \
215181775Skmacy  .align 4                              ;       \
216181775Skmacy  .long 2f - 1f         /* namesz */    ;       \
217181775Skmacy  .long 4f - 3f         /* descsz */    ;       \
218181775Skmacy  .long type                            ;       \
219181775Skmacy1:.asciz #name                          ;       \
220181775Skmacy2:.align 4                              ;       \
221181775Skmacy3:desctype descdata                     ;       \
222181775Skmacy4:.align 4                              ;       \
223181775Skmacy.popsection
224181775Skmacy#else /* !__STDC__, i.e. -traditional */
225181775Skmacy#define ELFNOTE(name, type, desctype, descdata) \
226181775Skmacy.pushsection .note.name                 ;       \
227181775Skmacy  .align 4                              ;       \
228181775Skmacy  .long 2f - 1f         /* namesz */    ;       \
229181775Skmacy  .long 4f - 3f         /* descsz */    ;       \
230181775Skmacy  .long type                            ;       \
231181775Skmacy1:.asciz "name"                         ;       \
232181775Skmacy2:.align 4                              ;       \
233181775Skmacy3:desctype descdata                     ;       \
234181775Skmacy4:.align 4                              ;       \
235181775Skmacy.popsection
236181775Skmacy#endif /* __STDC__ */
237181775Skmacy
2382579Sbde#endif /* !_MACHINE_ASMACROS_H_ */
239