xref: /openbsd-src/lib/csu/amd64/md_init.h (revision 1e43641e997f17b3e752e4bc940b17070ff580a3)
1*1e43641eSderaadt /* $OpenBSD: md_init.h,v 1.11 2023/11/18 16:26:16 deraadt Exp $ */
2118f6189Smickey 
3118f6189Smickey /*-
4118f6189Smickey  * Copyright (c) 2001 Ross Harvey
5118f6189Smickey  * All rights reserved.
6118f6189Smickey  *
7118f6189Smickey  * Redistribution and use in source and binary forms, with or without
8118f6189Smickey  * modification, are permitted provided that the following conditions
9118f6189Smickey  * are met:
10118f6189Smickey  * 1. Redistributions of source code must retain the above copyright
11118f6189Smickey  *    notice, this list of conditions and the following disclaimer.
12118f6189Smickey  * 2. Redistributions in binary form must reproduce the above copyright
13118f6189Smickey  *    notice, this list of conditions and the following disclaimer in the
14118f6189Smickey  *    documentation and/or other materials provided with the distribution.
15118f6189Smickey  * 3. All advertising materials mentioning features or use of this software
16118f6189Smickey  *    must display the following acknowledgement:
17118f6189Smickey  *      This product includes software developed by the NetBSD
18118f6189Smickey  *      Foundation, Inc. and its contributors.
19118f6189Smickey  * 4. Neither the name of The NetBSD Foundation nor the names of its
20118f6189Smickey  *    contributors may be used to endorse or promote products derived
21118f6189Smickey  *    from this software without specific prior written permission.
22118f6189Smickey  *
23118f6189Smickey  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24118f6189Smickey  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25118f6189Smickey  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26118f6189Smickey  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27118f6189Smickey  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28118f6189Smickey  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29118f6189Smickey  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30118f6189Smickey  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31118f6189Smickey  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32118f6189Smickey  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33118f6189Smickey  * POSSIBILITY OF SUCH DAMAGE.
34118f6189Smickey  */
35118f6189Smickey 
36118f6189Smickey #define MD_SECT_CALL_FUNC(section, func) \
37118f6189Smickey 	__asm (".section "#section", \"ax\"\n"	\
38118f6189Smickey 	"	call " #func "\n"		\
39118f6189Smickey 	"	.previous")
40118f6189Smickey 
41118f6189Smickey #define MD_SECTION_PROLOGUE(sect, entry_pt)	\
42118f6189Smickey 	__asm (					\
43118f6189Smickey 	".section "#sect",\"ax\",@progbits	\n" \
44118f6189Smickey 	"	.globl " #entry_pt "		\n" \
45118f6189Smickey 	"	.type " #entry_pt ",@function	\n" \
46118f6189Smickey 	"	.align 16			\n" \
47ba1a662dSderaadt 	#entry_pt":				\n" \
4809224044Sderaadt 	"	endbr64				\n" \
49e76b0f7dSkettenis 	"	subq	$8,%rsp			\n" \
50118f6189Smickey 	"	.previous")
51118f6189Smickey 
52118f6189Smickey 
53118f6189Smickey #define MD_SECTION_EPILOGUE(sect)		\
54118f6189Smickey 	__asm (					\
55118f6189Smickey 	".section "#sect",\"ax\",@progbits	\n" \
56e76b0f7dSkettenis 	"	addq	$8,%rsp			\n" \
57118f6189Smickey 	"	ret				\n" \
58118f6189Smickey 	"	.previous")
59453b49cdSguenther 
60453b49cdSguenther 
61453b49cdSguenther #define	MD_CRT0_START				\
62453b49cdSguenther 	__asm(					\
63453b49cdSguenther 	".text					\n" \
64453b49cdSguenther 	"	.align	8			\n" \
65453b49cdSguenther 	"	.globl	__start			\n" \
66453b49cdSguenther 	"	.globl	_start			\n" \
67453b49cdSguenther 	"_start:				\n" \
68453b49cdSguenther 	"__start:				\n" \
6909224044Sderaadt 	"	endbr64				\n" \
70453b49cdSguenther 	"	movq	%rdx,%rcx		\n" \
71453b49cdSguenther 	"	movq	(%rsp),%rdi		\n" \
72453b49cdSguenther 	"	leaq	16(%rsp,%rdi,8),%rdx	\n" \
73453b49cdSguenther 	"	leaq	8(%rsp),%rsi		\n" \
74453b49cdSguenther 	"	subq	$8,%rsp			\n" \
75453b49cdSguenther 	"	andq	$~15,%rsp		\n" \
76453b49cdSguenther 	"	addq	$8,%rsp			\n" \
77453b49cdSguenther 	"	jmp	___start		\n" \
78453b49cdSguenther 	"	.previous")
79b7d44eadSkurt 
80b7d44eadSkurt #define	MD_RCRT0_START					\
81b7d44eadSkurt 	__asm(						\
82b7d44eadSkurt 	".text						\n" \
83b7d44eadSkurt 	"	.align	8				\n" \
84b7d44eadSkurt 	"	.globl	__start				\n" \
85b7d44eadSkurt 	"	.type	__start,@function		\n" \
86b7d44eadSkurt 	"_start:					\n" \
87b7d44eadSkurt 	"__start:					\n" \
8809224044Sderaadt 	"	endbr64					\n" \
89b7d44eadSkurt 	"	movq	%rsp, %r12			\n" \
90b7d44eadSkurt 	"	subq	$8, %rsp			\n" \
91b7d44eadSkurt 	"	andq	$~15, %rsp			\n" \
92b7d44eadSkurt 	"	addq	$8, %rsp			\n" \
93b7d44eadSkurt 	"	pushq	%rbx				\n" \
94b7d44eadSkurt 	"	subq	$(16*8), %rsp			\n" \
95b7d44eadSkurt 	"	leaq	_DYNAMIC(%rip),%rdx		\n" \
96b7d44eadSkurt 	"	movq	%rsp, %rsi			\n" \
97b7d44eadSkurt 	"	movq	%r12, %rdi			\n" \
98b7d44eadSkurt 	"	call	_dl_boot_bind@PLT		\n" \
99b7d44eadSkurt 	"						\n" \
100b7d44eadSkurt 	"	movq	$0, %rcx			\n" \
101b7d44eadSkurt 	"	movq	%r12, %rsp			\n" \
102b7d44eadSkurt 	"	movq	(%rsp),%rdi			\n" \
103b7d44eadSkurt 	"	leaq	16(%rsp,%rdi,8),%rdx		\n" \
104b7d44eadSkurt 	"	leaq	8(%rsp),%rsi			\n" \
105b7d44eadSkurt 	"	subq	$8,%rsp				\n" \
106b7d44eadSkurt 	"	andq	$~15,%rsp			\n" \
107b7d44eadSkurt 	"	addq	$8,%rsp				\n" \
108b7d44eadSkurt 	"	jmp	___start			\n" \
109b7d44eadSkurt 	"						\n" \
110*1e43641eSderaadt 	"	.global	_csu_abort			\n" \
111*1e43641eSderaadt 	"	.type	_csu_abort,@function		\n" \
112b7d44eadSkurt 	"	.align	8				\n" \
113*1e43641eSderaadt 	"_csu_abort:					\n" \
11409224044Sderaadt 	"	endbr64					\n" \
1152bc3a8c0Sderaadt 	"	int3					\n" \
116b7d44eadSkurt 	"	.previous")
117