xref: /openbsd-src/sys/arch/amd64/include/asm.h (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1 /*	$OpenBSD: asm.h,v 1.21 2022/01/01 23:47:14 guenther Exp $	*/
2 /*	$NetBSD: asm.h,v 1.2 2003/05/02 18:05:47 yamt Exp $	*/
3 
4 /*-
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)asm.h	5.5 (Berkeley) 5/7/91
36  */
37 
38 #ifndef _MACHINE_ASM_H_
39 #define _MACHINE_ASM_H_
40 
41 #ifdef __PIC__
42 #define PIC_PLT(x)	x@PLT
43 #define PIC_GOT(x)	x@GOTPCREL(%rip)
44 #else
45 #define PIC_PLT(x)	x
46 #define PIC_GOT(x)	x
47 #endif
48 
49 # define _C_LABEL(x)	x
50 #define	_ASM_LABEL(x)	x
51 
52 #define CVAROFF(x,y)		(_C_LABEL(x)+y)(%rip)
53 
54 #ifdef __STDC__
55 # define __CONCAT(x,y)	x ## y
56 # define __STRING(x)	#x
57 #else
58 # define __CONCAT(x,y)	x/**/y
59 # define __STRING(x)	"x"
60 #endif
61 
62 /* let kernels and others override entrypoint alignment */
63 #ifndef _ALIGN_TEXT
64 #define _ALIGN_TEXT	.align	16, 0x90
65 #endif
66 #define _ALIGN_TRAPS	.align	16, 0xcc
67 
68 #define	_FENTRY(x)	.type x,@function; x:
69 
70 /* NB == No Binding: use .globl or .weak as necessary */
71 #define	NENTRY_NB(x)	\
72 	.text; _ALIGN_TEXT; _FENTRY(x)
73 #define _ENTRY_NB(x) \
74 	.text; _ALIGN_TRAPS; _FENTRY(x)
75 #define _ENTRY(x)	.globl x; _ENTRY_NB(x)
76 #define _NENTRY(x)	.globl x; NENTRY_NB(x)
77 
78 #ifdef _KERNEL
79 #define	KUTEXT	.section .kutext, "ax", @progbits
80 
81 #define	KUTEXT_PAGE_START	.pushsection .kutext.page, "a", @progbits
82 #define	KTEXT_PAGE_START	.pushsection .ktext.page, "ax", @progbits
83 #define	KUTEXT_PAGE_END		.popsection
84 #define	KTEXT_PAGE_END		.popsection
85 
86 #define	IDTVEC(name) \
87 	KUTEXT; _ALIGN_TRAPS; IDTVEC_NOALIGN(name)
88 #define	GENTRY(x)		.globl x; _FENTRY(x)
89 #define	IDTVEC_NOALIGN(name)	GENTRY(X ## name)
90 #define	KIDTVEC(name) \
91 	.text; _ALIGN_TRAPS; IDTVEC_NOALIGN(name)
92 #define	KIDTVEC_FALLTHROUGH(name) \
93 	_ALIGN_TEXT; IDTVEC_NOALIGN(name)
94 #define KUENTRY(x) \
95 	KUTEXT; _ALIGN_TRAPS; GENTRY(x)
96 
97 /* Return stack refill, to prevent speculation attacks on natural returns */
98 #define	RET_STACK_REFILL_WITH_RCX	\
99 		mov	$8,%rcx		; \
100 		_ALIGN_TEXT		; \
101 	3:	call	5f		; \
102 	4:	pause			; \
103 		lfence			; \
104 		call	4b		; \
105 		_ALIGN_TRAPS		; \
106 	5:	call	7f		; \
107 	6:	pause			; \
108 		lfence			; \
109 		call	6b		; \
110 		_ALIGN_TRAPS		; \
111 	7:	loop	3b		; \
112 		add	$(16*8),%rsp
113 
114 #endif /* _KERNEL */
115 
116 #ifdef __STDC__
117 #define CPUVAR(off)	%gs:CPU_INFO_ ## off
118 #else
119 #define CPUVAR(off)     %gs:CPU_INFO_/**/off
120 #endif
121 
122 
123 #if defined(PROF) || defined(GPROF)
124 # define _PROF_PROLOGUE	\
125 	pushq %rbp; leaq (%rsp),%rbp; call PIC_PLT(__mcount); popq %rbp
126 #else
127 # define _PROF_PROLOGUE
128 #endif
129 
130 #if defined(_RET_PROTECTOR)
131 # define RETGUARD_SETUP_OFF(x, reg, off) \
132 	RETGUARD_SYMBOL(x); \
133 	movq (__retguard_ ## x)(%rip), %reg; \
134 	xorq off(%rsp), %reg
135 # define RETGUARD_SETUP(x, reg) \
136 	RETGUARD_SETUP_OFF(x, reg, 0)
137 # define RETGUARD_CHECK(x, reg) \
138 	xorq (%rsp), %reg; \
139 	cmpq (__retguard_ ## x)(%rip), %reg; \
140 	je 66f; \
141 	int3; int3; \
142 	.zero (0xf - ((. + 3 - x) & 0xf)), 0xcc; \
143 66:
144 # define RETGUARD_PUSH(reg) \
145 	pushq %reg
146 # define RETGUARD_POP(reg) \
147 	popq %reg
148 # define RETGUARD_SYMBOL(x) \
149 	.ifndef __retguard_ ## x; \
150 	.hidden __retguard_ ## x; \
151 	.type   __retguard_ ## x,@object; \
152 	.pushsection .openbsd.randomdata.retguard,"aw",@progbits; \
153 	.weak   __retguard_ ## x; \
154 	.p2align 3; \
155 	__retguard_ ## x: ; \
156 	.quad 0; \
157 	.size __retguard_ ## x, 8; \
158 	.popsection; \
159 	.endif
160 #else
161 # define RETGUARD_SETUP_OFF(x, reg, off)
162 # define RETGUARD_SETUP(x, reg)
163 # define RETGUARD_CHECK(x, reg)
164 # define RETGUARD_PUSH(reg)
165 # define RETGUARD_POP(reg)
166 # define RETGUARD_SYMBOL(x)
167 #endif
168 
169 #define	ENTRY(y)	_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
170 #define	NENTRY(y)	_NENTRY(_C_LABEL(y))
171 #define	ASENTRY(y)	_NENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
172 #define	ENTRY_NB(y)	_ENTRY_NB(y); _PROF_PROLOGUE
173 #define	END(y)		.size y, . - y
174 
175 #define	STRONG_ALIAS(alias,sym)						\
176 	.global alias;							\
177 	alias = sym
178 #define	WEAK_ALIAS(alias,sym)						\
179 	.weak alias;							\
180 	alias = sym
181 
182 /* XXXfvdl do not use stabs here */
183 #ifdef __STDC__
184 #define	WARN_REFERENCES(sym,msg)					\
185 	.stabs msg ## ,30,0,0,0 ;					\
186 	.stabs __STRING(_C_LABEL(sym)) ## ,1,0,0,0
187 #else
188 #define	WARN_REFERENCES(sym,msg)					\
189 	.stabs msg,30,0,0,0 ;						\
190 	.stabs __STRING(sym),1,0,0,0
191 #endif /* __STDC__ */
192 
193 /* generic retpoline ("return trampoline") generator */
194 #define	JMP_RETPOLINE(reg)		\
195 		call	69f		; \
196 	68:	pause			; \
197 		lfence			; \
198 		jmp	68b		; \
199 		_ALIGN_TRAPS		; \
200 	69:	mov	%reg,(%rsp)	; \
201 		ret			; \
202 		lfence
203 
204 #endif /* !_MACHINE_ASM_H_ */
205