execsp.S revision 1.1
1/*	$NetBSD: execsp.S,v 1.1 2025/04/20 22:32:25 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:32:25 riastradh Exp $")
34
35#include "SYS.h"
36
37	.text
38
39/*
40 * void execsp_start(void *stackpointer@a0, void (*cleanup@a1)(void),
41 *     void *obj_main@a2, struct ps_strings *ps_strings@a3)
42 *
43 *	ELF entry point.  Saves the stack pointer in startsp and defers
44 *	to the usual csu __start routine.
45 */
46LEAF(execsp_start)
47	move	t0, sp			/* save stack pointer just in case */
48
49	PIC_PROLOGUE(execsp_start)	/* gp setup, sets t3 */
50
51	PTR_LA	t1, _C_LABEL(startsp)	/* load t1 := &startsp */
52	 NOP_L				/* load hazard */
53	PTR_S	t0, 0(t1)		/* store startsp := stack pointer */
54
55	PIC_TAILCALL(__start)		/* gp restore, uses t3 */
56END(execsp_start)
57
58/*
59 * void execsp_ctor(void)
60 *
61 *	ELF constructor.  Saves the stack pointer in ctorsp and
62 *	returns.
63 */
64LEAF(execsp_ctor)
65	move	t0, sp			/* save stack pointer just in case */
66
67	PIC_PROLOGUE(execsp_ctor)	/* gp setup, sets t3 */
68
69	PTR_LA	t1, _C_LABEL(ctorsp)	/* load t1 := &ctorsp */
70	 NOP_L				/* load hazard */
71	PTR_S	t0, 0(t1)		/* store ctorsp := stack pointer */
72
73	PIC_RETURN()			/* gp restore, uses t3 */
74END(execsp_ctor)
75
76	/* Make execsp_ctor a constructor. */
77	.pushsection .ctors,"aw",@progbits
78	.p2align PTR_SCALESHIFT
79	PTR_WORD	_C_LABEL(execsp_ctor)
80	.popsection
81
82/*
83 * int main(int argc@a0, char **argv@a1, ...)
84 *
85 *	Main function.  Saves the stack pointer in mainsp and returns
86 *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
87 *	been initialized.
88 */
89LEAF(main)
90	move	t0, sp			/* save stack pointer just in case */
91
92	PIC_PROLOGUE(main)		/* gp setup, sets t3 */
93
94	PTR_LA	t1, _C_LABEL(mainsp)	/* load t1 := &mainsp */
95	 NOP_L				/* load hazard */
96	PTR_S	t0, 0(t1)		/* store mainsp := stack pointer */
97
98	move	v0, zero		/* return 0 */
99
100	PIC_RETURN()			/* gp restore, uses t3 */
101END(main)
102
103/*
104 * void execsp_dtor(void)
105 *
106 *	ELF destructor.  Saves the stack pointer in dtorsp and defers
107 *	to the C execsp_main in h_execsp.c to report the stack pointers
108 *	back to the t_signal_and_sp parent.
109 */
110LEAF(execsp_dtor)
111	move	t0, sp			/* save stack pointer just in case */
112
113	PIC_PROLOGUE(execsp_dtor)	/* gp setup, sets t3 */
114
115	PTR_LA	t1, _C_LABEL(dtorsp)	/* load t1 := &dtorsp */
116	 NOP_L				/* load hazard */
117	PTR_S	t0, 0(t1)		/* store dtorsp := stack pointer */
118
119	PIC_TAILCALL(execsp_main)	/* gp restore, uses t3 */
120END(execsp_dtor)
121
122	/* Make execsp_ctor a destructor. */
123	.pushsection .dtors,"aw",@progbits
124	.p2align PTR_SCALESHIFT
125	PTR_WORD	_C_LABEL(execsp_dtor)
126	.popsection
127