execsp.S revision 1.1
11.1Sriastrad/*	$NetBSD: execsp.S,v 1.1 2025/04/20 22:34:07 riastradh Exp $	*/
21.1Sriastrad
31.1Sriastrad/*-
41.1Sriastrad * Copyright (c) 2025 The NetBSD Foundation, Inc.
51.1Sriastrad * All rights reserved.
61.1Sriastrad *
71.1Sriastrad * Redistribution and use in source and binary forms, with or without
81.1Sriastrad * modification, are permitted provided that the following conditions
91.1Sriastrad * are met:
101.1Sriastrad * 1. Redistributions of source code must retain the above copyright
111.1Sriastrad *    notice, this list of conditions and the following disclaimer.
121.1Sriastrad * 2. Redistributions in binary form must reproduce the above copyright
131.1Sriastrad *    notice, this list of conditions and the following disclaimer in the
141.1Sriastrad *    documentation and/or other materials provided with the distribution.
151.1Sriastrad *
161.1Sriastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sriastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sriastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sriastrad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sriastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sriastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sriastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sriastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sriastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sriastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sriastrad * POSSIBILITY OF SUCH DAMAGE.
271.1Sriastrad */
281.1Sriastrad
291.1Sriastrad#define	_LOCORE
301.1Sriastrad
311.1Sriastrad#include <machine/asm.h>
321.1Sriastrad
331.1SriastradRCSID("$NetBSD: execsp.S,v 1.1 2025/04/20 22:34:07 riastradh Exp $")
341.1Sriastrad
351.1Sriastrad/*
361.1Sriastrad * void execsp_start(void (*cleanup@a0)(void),
371.1Sriastrad *     struct ps_strings *ps_strings@a1)
381.1Sriastrad *
391.1Sriastrad *	ELF entry point.  Saves the stack pointer in startsp and defers
401.1Sriastrad *	to the usual csu __start routine.
411.1Sriastrad */
421.1SriastradENTRY(execsp_start)
431.1Sriastrad	.option push
441.1Sriastrad	.option norelax		/* Don't optimize this into `mv gp, gp'! */
451.1Sriastrad	lla	gp, __global_pointer$
461.1Sriastrad	.option pop
471.1Sriastrad
481.1Sriastrad	PTR_LA	t0, _C_LABEL(startsp)	/* t0 := &startsp */
491.1Sriastrad	PTR_S	sp, 0(t0)		/* startsp := sp */
501.1Sriastrad	tail	_C_LABEL(__start)	/* tail call __start */
511.1SriastradEND(execsp_start)
521.1Sriastrad
531.1Sriastrad/*
541.1Sriastrad * void execsp_ctor(void)
551.1Sriastrad *
561.1Sriastrad *	ELF constructor.  Saves the stack pointer in ctorsp and
571.1Sriastrad *	returns.
581.1Sriastrad */
591.1SriastradENTRY(execsp_ctor)
601.1Sriastrad	PTR_LA	t0, _C_LABEL(ctorsp)	/* t0 := &ctorsp */
611.1Sriastrad	PTR_S	sp, 0(t0)		/* ctorsp := sp */
621.1Sriastrad	ret
631.1SriastradEND(execsp_ctor)
641.1Sriastrad
651.1Sriastrad	/* Make execsp_ctor a constructor. */
661.1Sriastrad	.section .init_array,"aw"
671.1Sriastrad	.p2align PTR_SCALESHIFT
681.1Sriastrad	PTR_WORD	_C_LABEL(execsp_ctor)
691.1Sriastrad
701.1Sriastrad/*
711.1Sriastrad * int main(int argc@a0, char **argv@a1, ...)
721.1Sriastrad *
731.1Sriastrad *	Main function.  Saves the stack pointer in mainsp and returns
741.1Sriastrad *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
751.1Sriastrad *	been initialized.
761.1Sriastrad */
771.1SriastradENTRY(main)
781.1Sriastrad	PTR_LA	t0, _C_LABEL(mainsp)	/* t0 := &mainsp */
791.1Sriastrad	PTR_S	sp, 0(t0)		/* mainsp := sp */
801.1Sriastrad	li	a0, 0			/* return 0 */
811.1Sriastrad	ret
821.1SriastradEND(main)
831.1Sriastrad
841.1Sriastrad/*
851.1Sriastrad * void execsp_dtor(void)
861.1Sriastrad *
871.1Sriastrad *	ELF destructor.  Saves the stack pointer in dtorsp and defers
881.1Sriastrad *	to the C execsp_main in h_execsp.c to report the stack pointers
891.1Sriastrad *	back to the t_signal_and_sp parent.
901.1Sriastrad */
911.1SriastradENTRY(execsp_dtor)
921.1Sriastrad	PTR_LA	t0, _C_LABEL(dtorsp)	/* t0 := &dtorsp */
931.1Sriastrad	PTR_S	sp, 0(t0)		/* dtorsp := sp */
941.1Sriastrad	tail	_C_LABEL(execsp_main)	/* tail call execsp_main */
951.1SriastradEND(execsp_dtor)
961.1Sriastrad
971.1Sriastrad	/* Make execsp_ctor a destructor. */
981.1Sriastrad	.section .fini_array,"aw"
991.1Sriastrad	.p2align PTR_SCALESHIFT
1001.1Sriastrad	PTR_WORD	_C_LABEL(execsp_dtor)
101