execsp.S revision 1.2
11.2Sriastrad/*	$NetBSD: execsp.S,v 1.2 2025/04/27 00:03:46 riastradh Exp $	*/
21.1Suwe
31.1Suwe/*-
41.1Suwe * Copyright (c) 2025 The NetBSD Foundation, Inc.
51.1Suwe * All rights reserved.
61.1Suwe *
71.1Suwe * Redistribution and use in source and binary forms, with or without
81.1Suwe * modification, are permitted provided that the following conditions
91.1Suwe * are met:
101.1Suwe * 1. Redistributions of source code must retain the above copyright
111.1Suwe *    notice, this list of conditions and the following disclaimer.
121.1Suwe * 2. Redistributions in binary form must reproduce the above copyright
131.1Suwe *    notice, this list of conditions and the following disclaimer in the
141.1Suwe *    documentation and/or other materials provided with the distribution.
151.1Suwe *
161.1Suwe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Suwe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Suwe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Suwe * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Suwe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Suwe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Suwe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Suwe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Suwe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Suwe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Suwe * POSSIBILITY OF SUCH DAMAGE.
271.1Suwe */
281.1Suwe
291.1Suwe#include "asm.h"
301.1Suwe
311.2SriastradRCSID("$NetBSD: execsp.S,v 1.2 2025/04/27 00:03:46 riastradh Exp $")
321.1Suwe
331.1Suwe
341.1Suwe/*
351.1Suwe * void execsp_start()
361.1Suwe *
371.1Suwe *	ELF entry point.  Saves the stack pointer in startsp and defers
381.1Suwe *	to the usual csu __start routine.
391.1Suwe *
401.1Suwe *	See sys/arch/sh3/sh3/sh3_machdep.c setregs()
411.1Suwe */
421.1SuweENTRY(execsp_start)
431.1Suwe	mov	sp, r1	// for consistency, don't need to
441.1Suwe	PIC_PROLOGUE_NOSAVE(.L_start_GOT)
451.1Suwe
461.1Suwe	MOVL_VAR(.L_startsp, r0)
471.1Suwe	mov.l	r1, @r0
481.1Suwe
491.1Suwe	mov.l	.L___start, r0
501.1Suwe1:	JUMP	r0
511.1Suwe	 nop
521.1Suwe
531.1Suwe	.p2align 2
541.1Suwe.L_start_GOT:	PIC_GOT_DATUM
551.1Suwe.L_startsp:	VAR_DATUM(startsp)
561.1Suwe.L___start:	CALL_DATUM(__start, 1b)
571.1Suwe	SET_ENTRY_SIZE(execsp_start)
581.1Suwe
591.1Suwe
601.1Suwe/*
611.1Suwe * void execsp_ctor(void)
621.1Suwe *
631.1Suwe *	ELF constructor.  Saves the stack pointer in ctorsp and
641.1Suwe *	returns.
651.1Suwe */
661.1SuweENTRY(execsp_ctor)
671.1Suwe	mov	sp, r1
681.1Suwe	PIC_PROLOGUE(.L_ctor_GOT)
691.1Suwe
701.1Suwe	MOVL_VAR(.L_ctorsp, r0)
711.1Suwe	mov.l	r1, @r0
721.1Suwe
731.1Suwe	rts
741.1Suwe	 PIC_EPILOGUE_SLOT
751.1Suwe
761.1Suwe	.p2align 2
771.1Suwe.L_ctor_GOT:	PIC_GOT_DATUM
781.1Suwe.L_ctorsp:	VAR_DATUM(ctorsp)
791.1Suwe	SET_ENTRY_SIZE(execsp_ctor)
801.1Suwe
811.1Suwe	/* Make execsp_ctor a constructor. */
821.1Suwe	.pushsection .ctors, "aw", @progbits
831.1Suwe	.p2align 2
841.1Suwe	.long	_C_LABEL(execsp_ctor)
851.1Suwe	.popsection
861.1Suwe
871.1Suwe
881.1Suwe/*
891.1Suwe * int main(int argc, char **argv, ...)
901.1Suwe *
911.1Suwe *	Main function.  Saves the stack pointer in mainsp and returns
921.1Suwe *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
931.1Suwe *	been initialized.
941.1Suwe */
951.1SuweENTRY(main)
961.1Suwe	mov	sp, r1
971.1Suwe	PIC_PROLOGUE(.L_main_GOT)
981.1Suwe
991.1Suwe	MOVL_VAR(.L_mainsp, r0)
1001.1Suwe	mov.l	r1, @r0
1011.1Suwe
1021.1Suwe	PIC_EPILOGUE
1031.1Suwe	rts
1041.1Suwe	 mov	#0, r0
1051.1Suwe
1061.1Suwe	.p2align 2
1071.1Suwe.L_main_GOT:	PIC_GOT_DATUM
1081.1Suwe.L_mainsp:	VAR_DATUM(mainsp)
1091.1Suwe	SET_ENTRY_SIZE(main)
1101.1Suwe
1111.1Suwe
1121.1Suwe/*
1131.1Suwe * void execsp_dtor(void)
1141.1Suwe *
1151.1Suwe *	ELF destructor.  Saves the stack pointer in dtorsp and defers
1161.1Suwe *	to the C execsp_main in h_execsp.c to report the stack pointers
1171.1Suwe *	back to the t_signal_and_sp parent.
1181.1Suwe */
1191.1SuweENTRY(execsp_dtor)
1201.1Suwe	mov	sp, r1
1211.1Suwe	sts.l	pr, @-sp
1221.1Suwe	PIC_PROLOGUE(.L_dtor_GOT)
1231.1Suwe
1241.1Suwe	MOVL_VAR(.L_dtorsp, r0)
1251.1Suwe	mov.l	r1, @r0
1261.1Suwe
1271.1Suwe	mov.l	.L_execsp_main, r0
1281.1Suwe1:	CALL	r0
1291.1Suwe	 nop
1301.1Suwe
1311.1Suwe	PIC_EPILOGUE
1321.1Suwe	lds.l	@sp+, pr
1331.1Suwe	rts
1341.1Suwe	 nop
1351.1Suwe
1361.1Suwe	.p2align 2
1371.1Suwe.L_dtor_GOT:	PIC_GOT_DATUM
1381.1Suwe.L_dtorsp:	VAR_DATUM(dtorsp)
1391.1Suwe.L_execsp_main:	CALL_DATUM(execsp_main, 1b)
1401.1Suwe	SET_ENTRY_SIZE(execsp_dtor)
1411.1Suwe
1421.1Suwe	/* Make execsp_ctor a destructor. */
1431.1Suwe	.pushsection .dtors, "aw", @progbits
1441.1Suwe	.p2align 2
1451.1Suwe	.long	_C_LABEL(execsp_dtor)
1461.1Suwe	.popsection
147