xref: /freebsd/sys/i386/include/asmacros.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /* -*- mode: asm -*- */
2 /*-
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Copyright (c) 1993 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 #ifndef _MACHINE_ASMACROS_H_
36 #define _MACHINE_ASMACROS_H_
37 
38 #include <sys/cdefs.h>
39 
40 /* XXX too much duplication in various asm*.h's. */
41 
42 /*
43  * CNAME is used to manage the relationship between symbol names in C
44  * and the equivalent assembly language names.  CNAME is given a name as
45  * it would be used in a C program.  It expands to the equivalent assembly
46  * language name.
47  */
48 #define CNAME(csym)		csym
49 
50 #define ALIGN_DATA	.p2align 2	/* 4 byte alignment, zero filled */
51 #define ALIGN_TEXT	.p2align 2,0x90	/* 4-byte alignment, nop filled */
52 #define SUPERALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
53 
54 #define GEN_ENTRY(name)		ALIGN_TEXT; .globl CNAME(name); \
55 				.type CNAME(name),@function; CNAME(name):
56 #define ENTRY(name)		GEN_ENTRY(name)
57 #define ALTENTRY(name)		GEN_ENTRY(name)
58 #define	END(name)		.size name, . - name
59 
60 #ifdef LOCORE
61 
62 #define	GSEL_KPL	0x0020	/* GSEL(GCODE_SEL, SEL_KPL) */
63 #define	SEL_RPL_MASK	0x0003
64 
65 /*
66  * Convenience macro for declaring interrupt entry points.
67  */
68 #define	IDTVEC(name)	ALIGN_TEXT; .globl __CONCAT(X,name); \
69 			.type __CONCAT(X,name),@function; __CONCAT(X,name):
70 
71 /*
72  * Macros to create and destroy a trap frame.
73  */
74 	.macro	PUSH_FRAME2
75 	pushal
76 	pushl	$0
77 	movw	%ds,(%esp)
78 	pushl	$0
79 	movw	%es,(%esp)
80 	pushl	$0
81 	movw	%fs,(%esp)
82 	movl	%esp,%ebp
83 	.endm
84 
85 	.macro	PUSH_FRAME
86 	pushl	$0		/* dummy error code */
87 	pushl	$0		/* dummy trap type */
88 	PUSH_FRAME2
89 	.endm
90 
91 /*
92  * Access per-CPU data.
93  */
94 #define	PCPU(member)	%fs:PC_ ## member
95 
96 #define	PCPU_ADDR(member, reg)						\
97 	movl %fs:PC_PRVSPACE, reg ;					\
98 	addl $PC_ ## member, reg
99 
100 /*
101  * Setup the kernel segment registers.
102  */
103 	.macro	SET_KERNEL_SREGS
104 	movl	$KDSEL, %eax	/* reload with kernel's data segment */
105 	movl	%eax, %ds
106 	movl	%eax, %es
107 	movl	$KPSEL, %eax	/* reload with per-CPU data segment */
108 	movl	%eax, %fs
109 	.endm
110 
111 	.macro	NMOVE_STACKS
112 	movl	PCPU(KESP0), %edx
113 	movl	$TF_SZ, %ecx
114 	testl	$PSL_VM, TF_EFLAGS(%esp)
115 	jz	.L\@.1
116 	addl	$VM86_STACK_SPACE, %ecx
117 .L\@.1:	subl	%ecx, %edx
118 	movl	%edx, %edi
119 	movl	%esp, %esi
120 	rep; movsb
121 	movl	%edx, %esp
122 	.endm
123 
124 	.macro	LOAD_KCR3
125 	call	.L\@.1
126 .L\@.1:	popl	%eax
127 	movl	(tramp_idleptd - .L\@.1)(%eax), %eax
128 	movl	%eax, %cr3
129 	.endm
130 
131 	.macro	MOVE_STACKS
132 	LOAD_KCR3
133 	NMOVE_STACKS
134 	.endm
135 
136 	.macro	KENTER
137 	testl	$PSL_VM, TF_EFLAGS(%esp)
138 	jz	.L\@.1
139 	LOAD_KCR3
140 	movl	PCPU(CURPCB), %eax
141 	testl	$PCB_VM86CALL, PCB_FLAGS(%eax)
142 	jnz	.L\@.3
143 	NMOVE_STACKS
144 	movl	$handle_ibrs_entry,%edx
145 	call	*%edx
146 	jmp	.L\@.3
147 .L\@.1:	testb	$SEL_RPL_MASK, TF_CS(%esp)
148 	jz	.L\@.3
149 .L\@.2:	MOVE_STACKS
150 	movl	$handle_ibrs_entry,%edx
151 	call	*%edx
152 .L\@.3:
153 	.endm
154 
155 #endif /* LOCORE */
156 
157 #ifdef __STDC__
158 #define ELFNOTE(name, type, desctype, descdata...) \
159 .pushsection .note.name, "a", @note     ;       \
160   .align 4                              ;       \
161   .long 2f - 1f         /* namesz */    ;       \
162   .long 4f - 3f         /* descsz */    ;       \
163   .long type                            ;       \
164 1:.asciz #name                          ;       \
165 2:.align 4                              ;       \
166 3:desctype descdata                     ;       \
167 4:.align 4                              ;       \
168 .popsection
169 #else /* !__STDC__, i.e. -traditional */
170 #define ELFNOTE(name, type, desctype, descdata) \
171 .pushsection .note.name, "a", @note     ;       \
172   .align 4                              ;       \
173   .long 2f - 1f         /* namesz */    ;       \
174   .long 4f - 3f         /* descsz */    ;       \
175   .long type                            ;       \
176 1:.asciz "name"                         ;       \
177 2:.align 4                              ;       \
178 3:desctype descdata                     ;       \
179 4:.align 4                              ;       \
180 .popsection
181 #endif /* __STDC__ */
182 
183 #endif /* !_MACHINE_ASMACROS_H_ */
184