1 /* $NetBSD: execsp.S,v 1.2 2025/04/21 02:31:22 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 33 RCSID("$NetBSD: execsp.S,v 1.2 2025/04/21 02:31:22 riastradh Exp $") 34 35 .set noat 36 .text 37 38 /* 39 * void execsp_start(void *stackpointer@a0, void (*cleanup@a1)(void), 40 * void *obj_main@a2, struct ps_strings *ps_strings@a3) 41 * 42 * ELF entry point. Saves the stack pointer in startsp and defers 43 * to the usual csu __start routine. 44 */ 45 LEAF(execsp_start, 4) 46 LDGP(pv) 47 ldq at_reg, startsp(gp) !literal 48 stq sp, 0(at_reg) 49 ldq pv, __start(gp) !literal 50 jmp (pv), __start 51 END(execsp_start) 52 53 /* 54 * void execsp_ctor(void) 55 * 56 * ELF constructor. Saves the stack pointer in ctorsp and 57 * returns. 58 */ 59 LEAF(execsp_ctor, 0) 60 LDGP(pv) 61 ldq at_reg, ctorsp(gp) !literal 62 stq sp, 0(at_reg) 63 RET 64 END(execsp_ctor) 65 66 /* Make execsp_ctor a constructor. */ 67 .pushsection .ctors,"aw",@progbits 68 .p2align 3 69 .quad execsp_ctor 70 .popsection 71 72 /* 73 * int main(int argc@a0, char **argv@a1, ...) 74 * 75 * Main function. Saves the stack pointer in mainsp and returns 76 * zero. We will call execsp_main in execsp_dtor once dtorsp has 77 * been initialized. 78 */ 79 LEAF(main, 2) 80 LDGP(pv) 81 ldq at_reg, mainsp(gp) !literal 82 stq sp, 0(at_reg) 83 mov zero, v0 84 RET 85 END(main) 86 87 /* 88 * void execsp_dtor(void) 89 * 90 * ELF destructor. Saves the stack pointer in dtorsp and defers 91 * to the C execsp_main in h_execsp.c to report the stack pointers 92 * back to the t_signal_and_sp parent. 93 */ 94 LEAF(execsp_dtor, 0) 95 LDGP(pv) 96 ldq at_reg, dtorsp(gp) !literal 97 stq sp, 0(at_reg) 98 ldq pv, execsp_main(gp) !literal 99 jmp (pv), execsp_main 100 END(execsp_dtor) 101 102 /* Make execsp_ctor a destructor. */ 103 .pushsection .dtors,"aw",@progbits 104 .p2align 3 105 .quad execsp_dtor 106 .popsection 107