execsp.S revision 1.1
1/*	$NetBSD: execsp.S,v 1.1 2025/04/20 22:34:07 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#define	_LOCORE
30
31#include <machine/asm.h>
32
33RCSID("$NetBSD: execsp.S,v 1.1 2025/04/20 22:34:07 riastradh Exp $")
34
35/*
36 * void execsp_start(void (*cleanup@a0)(void),
37 *     struct ps_strings *ps_strings@a1)
38 *
39 *	ELF entry point.  Saves the stack pointer in startsp and defers
40 *	to the usual csu __start routine.
41 */
42ENTRY(execsp_start)
43	.option push
44	.option norelax		/* Don't optimize this into `mv gp, gp'! */
45	lla	gp, __global_pointer$
46	.option pop
47
48	PTR_LA	t0, _C_LABEL(startsp)	/* t0 := &startsp */
49	PTR_S	sp, 0(t0)		/* startsp := sp */
50	tail	_C_LABEL(__start)	/* tail call __start */
51END(execsp_start)
52
53/*
54 * void execsp_ctor(void)
55 *
56 *	ELF constructor.  Saves the stack pointer in ctorsp and
57 *	returns.
58 */
59ENTRY(execsp_ctor)
60	PTR_LA	t0, _C_LABEL(ctorsp)	/* t0 := &ctorsp */
61	PTR_S	sp, 0(t0)		/* ctorsp := sp */
62	ret
63END(execsp_ctor)
64
65	/* Make execsp_ctor a constructor. */
66	.section .init_array,"aw"
67	.p2align PTR_SCALESHIFT
68	PTR_WORD	_C_LABEL(execsp_ctor)
69
70/*
71 * int main(int argc@a0, char **argv@a1, ...)
72 *
73 *	Main function.  Saves the stack pointer in mainsp and returns
74 *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
75 *	been initialized.
76 */
77ENTRY(main)
78	PTR_LA	t0, _C_LABEL(mainsp)	/* t0 := &mainsp */
79	PTR_S	sp, 0(t0)		/* mainsp := sp */
80	li	a0, 0			/* return 0 */
81	ret
82END(main)
83
84/*
85 * void execsp_dtor(void)
86 *
87 *	ELF destructor.  Saves the stack pointer in dtorsp and defers
88 *	to the C execsp_main in h_execsp.c to report the stack pointers
89 *	back to the t_signal_and_sp parent.
90 */
91ENTRY(execsp_dtor)
92	PTR_LA	t0, _C_LABEL(dtorsp)	/* t0 := &dtorsp */
93	PTR_S	sp, 0(t0)		/* dtorsp := sp */
94	tail	_C_LABEL(execsp_main)	/* tail call execsp_main */
95END(execsp_dtor)
96
97	/* Make execsp_ctor a destructor. */
98	.section .fini_array,"aw"
99	.p2align PTR_SCALESHIFT
100	PTR_WORD	_C_LABEL(execsp_dtor)
101