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