xref: /freebsd/stand/kboot/libkboot/arch/amd64/start_arch.h (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1*a03411e8SWarner Losh /*
2*a03411e8SWarner Losh  * Copyright (c) 2022, Netflix, Inc.
3*a03411e8SWarner Losh  *
4*a03411e8SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5*a03411e8SWarner Losh  */
6*a03411e8SWarner Losh 
7*a03411e8SWarner Losh /*
8*a03411e8SWarner Losh  * Provides a _start routine that calls a _start_c routine that takes a pointer
9*a03411e8SWarner Losh  * to the stack as documented in crt1.c. We skip the pointer to _DYNAMIC since
10*a03411e8SWarner Losh  * we don't support dynamic libraries, at all. And while _start_c is our own
11*a03411e8SWarner Losh  * thing, we comport to the calling conventions that glibc and musl have and
12*a03411e8SWarner Losh  * make sure the second argument (%esi) is 0 for _DYNAMIC placeholder.  We
13*a03411e8SWarner Losh  * likely could call main directly with only a few more lines of code, but this
14*a03411e8SWarner Losh  * is simple enough and concentrates all the expressable in C stuff there.  We
15*a03411e8SWarner Losh  * also generate eh_frames should we need to debug things (it doesn't change the
16*a03411e8SWarner Losh  * genreated code, but leaves enough breadcrumbs to keep gdb happy).
17*a03411e8SWarner Losh  */
18*a03411e8SWarner Losh 
19*a03411e8SWarner Losh __asm__(
20*a03411e8SWarner Losh ".text\n"			/* ENTRY(_start) */
21*a03411e8SWarner Losh ".p2align 4,0x90\n"
22*a03411e8SWarner Losh ".global _start\n"
23*a03411e8SWarner Losh ".type _start, @function\n"
24*a03411e8SWarner Losh "_start:\n"
25*a03411e8SWarner Losh ".cfi_startproc\n"
26*a03411e8SWarner Losh "	xor	%rbp, %rbp\n"		/* Clear out the stack frame pointer */
27*a03411e8SWarner Losh "	mov	%rsp, %rdi\n"		/* Pass pointer to current stack with argc, argv and envp on it */
28*a03411e8SWarner Losh "	xor	%rsi, %rsi\n"		/* No dynamic pointer for us, to keep it simple */
29*a03411e8SWarner Losh "	andq	$-16, %rsp\n"		/* Align stack to 16-byte boundary */
30*a03411e8SWarner Losh "	call	_start_c\n"		/* Our MI code takes it from here and won't return */
31*a03411e8SWarner Losh /* NORETURN */
32*a03411e8SWarner Losh ".size _start, . - _start\n"	/* END(_start) */
33*a03411e8SWarner Losh ".cfi_endproc"
34*a03411e8SWarner Losh );
35