execsp.S revision 1.1
11.1Sriastrad/*	$NetBSD: execsp.S,v 1.1 2025/04/20 22:32:25 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:32:25 riastradh Exp $")
341.1Sriastrad
351.1Sriastrad#include "SYS.h"
361.1Sriastrad
371.1Sriastrad	.text
381.1Sriastrad
391.1Sriastrad/*
401.1Sriastrad * void execsp_start(void *stackpointer@a0, void (*cleanup@a1)(void),
411.1Sriastrad *     void *obj_main@a2, struct ps_strings *ps_strings@a3)
421.1Sriastrad *
431.1Sriastrad *	ELF entry point.  Saves the stack pointer in startsp and defers
441.1Sriastrad *	to the usual csu __start routine.
451.1Sriastrad */
461.1SriastradLEAF(execsp_start)
471.1Sriastrad	move	t0, sp			/* save stack pointer just in case */
481.1Sriastrad
491.1Sriastrad	PIC_PROLOGUE(execsp_start)	/* gp setup, sets t3 */
501.1Sriastrad
511.1Sriastrad	PTR_LA	t1, _C_LABEL(startsp)	/* load t1 := &startsp */
521.1Sriastrad	 NOP_L				/* load hazard */
531.1Sriastrad	PTR_S	t0, 0(t1)		/* store startsp := stack pointer */
541.1Sriastrad
551.1Sriastrad	PIC_TAILCALL(__start)		/* gp restore, uses t3 */
561.1SriastradEND(execsp_start)
571.1Sriastrad
581.1Sriastrad/*
591.1Sriastrad * void execsp_ctor(void)
601.1Sriastrad *
611.1Sriastrad *	ELF constructor.  Saves the stack pointer in ctorsp and
621.1Sriastrad *	returns.
631.1Sriastrad */
641.1SriastradLEAF(execsp_ctor)
651.1Sriastrad	move	t0, sp			/* save stack pointer just in case */
661.1Sriastrad
671.1Sriastrad	PIC_PROLOGUE(execsp_ctor)	/* gp setup, sets t3 */
681.1Sriastrad
691.1Sriastrad	PTR_LA	t1, _C_LABEL(ctorsp)	/* load t1 := &ctorsp */
701.1Sriastrad	 NOP_L				/* load hazard */
711.1Sriastrad	PTR_S	t0, 0(t1)		/* store ctorsp := stack pointer */
721.1Sriastrad
731.1Sriastrad	PIC_RETURN()			/* gp restore, uses t3 */
741.1SriastradEND(execsp_ctor)
751.1Sriastrad
761.1Sriastrad	/* Make execsp_ctor a constructor. */
771.1Sriastrad	.pushsection .ctors,"aw",@progbits
781.1Sriastrad	.p2align PTR_SCALESHIFT
791.1Sriastrad	PTR_WORD	_C_LABEL(execsp_ctor)
801.1Sriastrad	.popsection
811.1Sriastrad
821.1Sriastrad/*
831.1Sriastrad * int main(int argc@a0, char **argv@a1, ...)
841.1Sriastrad *
851.1Sriastrad *	Main function.  Saves the stack pointer in mainsp and returns
861.1Sriastrad *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
871.1Sriastrad *	been initialized.
881.1Sriastrad */
891.1SriastradLEAF(main)
901.1Sriastrad	move	t0, sp			/* save stack pointer just in case */
911.1Sriastrad
921.1Sriastrad	PIC_PROLOGUE(main)		/* gp setup, sets t3 */
931.1Sriastrad
941.1Sriastrad	PTR_LA	t1, _C_LABEL(mainsp)	/* load t1 := &mainsp */
951.1Sriastrad	 NOP_L				/* load hazard */
961.1Sriastrad	PTR_S	t0, 0(t1)		/* store mainsp := stack pointer */
971.1Sriastrad
981.1Sriastrad	move	v0, zero		/* return 0 */
991.1Sriastrad
1001.1Sriastrad	PIC_RETURN()			/* gp restore, uses t3 */
1011.1SriastradEND(main)
1021.1Sriastrad
1031.1Sriastrad/*
1041.1Sriastrad * void execsp_dtor(void)
1051.1Sriastrad *
1061.1Sriastrad *	ELF destructor.  Saves the stack pointer in dtorsp and defers
1071.1Sriastrad *	to the C execsp_main in h_execsp.c to report the stack pointers
1081.1Sriastrad *	back to the t_signal_and_sp parent.
1091.1Sriastrad */
1101.1SriastradLEAF(execsp_dtor)
1111.1Sriastrad	move	t0, sp			/* save stack pointer just in case */
1121.1Sriastrad
1131.1Sriastrad	PIC_PROLOGUE(execsp_dtor)	/* gp setup, sets t3 */
1141.1Sriastrad
1151.1Sriastrad	PTR_LA	t1, _C_LABEL(dtorsp)	/* load t1 := &dtorsp */
1161.1Sriastrad	 NOP_L				/* load hazard */
1171.1Sriastrad	PTR_S	t0, 0(t1)		/* store dtorsp := stack pointer */
1181.1Sriastrad
1191.1Sriastrad	PIC_TAILCALL(execsp_main)	/* gp restore, uses t3 */
1201.1SriastradEND(execsp_dtor)
1211.1Sriastrad
1221.1Sriastrad	/* Make execsp_ctor a destructor. */
1231.1Sriastrad	.pushsection .dtors,"aw",@progbits
1241.1Sriastrad	.p2align PTR_SCALESHIFT
1251.1Sriastrad	PTR_WORD	_C_LABEL(execsp_dtor)
1261.1Sriastrad	.popsection
127