1 /* $OpenBSD: md_init.h,v 1.22 2023/11/19 00:46:54 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 Ross Harvey 5 * Copyright (c) 2001 Simon Burge 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the NetBSD 19 * Foundation, Inc. and its contributors. 20 * 4. Neither the name of The NetBSD Foundation nor the names of its 21 * contributors may be used to endorse or promote products derived 22 * from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 /* 38 * Allocate 32 bytes for the stack frame. Store GP at SP+16 (since 39 * this is where code generated by the compiler for fallthru processing 40 * expects it to be), and the RA at SP+24. 41 */ 42 43 /* this is gross... */ 44 45 #ifdef __ABICALLS__ 46 #define MD_FUNCTION_PROLOGUE(entry_pt) \ 47 " .frame $sp,32,$31 \n" \ 48 " .set reorder \n" \ 49 " dsubu $sp,$sp,32 \n" \ 50 " .cpsetup $25, 16, "#entry_pt" \n" \ 51 " sd $ra ,24($sp) \n" 52 #else 53 #define MD_FUNCTION_PROLOGUE(entry_pt) \ 54 " dsubu $sp,$sp,32 \n" \ 55 " sd $ra ,24($sp) \n" 56 #endif 57 58 59 #define MD_SECTION_PROLOGUE(sect, entry_pt) \ 60 __asm ( \ 61 ".section "#sect",\"ax\",@progbits \n" \ 62 ".align 2 \n" \ 63 ".globl "#entry_pt" \n" \ 64 ".type "#entry_pt",@function \n" \ 65 ".ent "#entry_pt" \n" \ 66 #entry_pt": \n" \ 67 MD_FUNCTION_PROLOGUE(entry_pt) \ 68 " /* fall thru */ \n" \ 69 ".end "#entry_pt" \n" \ 70 ".previous") 71 72 #define MD_SECTION_EPILOGUE(sect) \ 73 __asm ( \ 74 ".section "#sect",\"ax\",@progbits \n" \ 75 " ld $ra ,24($sp) \n" \ 76 " # next should have been a .cpreturn \n" \ 77 " ld $gp,16($sp) \n" \ 78 " .set noreorder \n" \ 79 " j $ra \n" \ 80 " daddu $sp,$sp,32 \n" \ 81 " .set reorder \n" \ 82 ".previous") 83 84 #define MD_SECT_CALL_FUNC(section, func) \ 85 __asm (".section "#section", \"ax\" \n" \ 86 " .local " #func " \n" \ 87 " jal " #func " \n" \ 88 ".previous") 89 90 #define MD_CRT0_START \ 91 __asm( \ 92 ".text \n" \ 93 " .align 3 \n" \ 94 " .globl __start \n" \ 95 " .ent __start \n" \ 96 " .type __start, @function \n" \ 97 "__start: \n" \ 98 " lui $gp, %hi(%neg(%gp_rel(__start))) \n" \ 99 " addiu $gp, $gp, %lo(%neg(%gp_rel(__start))) \n" \ 100 " daddu $gp, $gp, $t9 \n" \ 101 " move $a0, $sp \n" \ 102 " dsrl $a1, $sp, 4 \n" /* align stack on a */ \ 103 " dsll $sp, $a1, 4 \n" /* 16 byte boundary */ \ 104 " move $a1, $v0 \n" \ 105 " .local ___start \n" \ 106 " dla $t9, ___start \n" \ 107 " jr $t9 \n" \ 108 " .end __start \n" \ 109 " .previous") 110 111 #define MD_RCRT0_START \ 112 __asm( \ 113 ".text \n" \ 114 " .align 3 \n" \ 115 " .globl __start \n" \ 116 " .ent __start \n" \ 117 " .type __start, @function \n" \ 118 "__start: \n" \ 119 " dsubu $sp, $sp, 160 \n" \ 120 " lui $gp, %hi(%neg(%gp_rel(__start))) \n" \ 121 " addiu $gp, $gp, %lo(%neg(%gp_rel(__start))) \n" \ 122 " daddu $gp, $gp, $t9 \n" \ 123 " dla $s1, 1f \n" \ 124 " bgezal $zero, 1f \n" \ 125 "1: \n" \ 126 " dsubu $s0, $ra, $s1 \n" \ 127 " daddu $a0, $sp, 160 \n" \ 128 " daddu $a1, $sp, 0 \n" \ 129 " dla $a2, _DYNAMIC \n" \ 130 " daddu $a2, $s0 \n" \ 131 " dla $t9, _dl_boot_bind \n" \ 132 " daddu $t9, $s0 \n" \ 133 " jalr $t9 \n" \ 134 " daddu $sp, $sp, 160 \n" \ 135 " move $a0, $sp \n" \ 136 " dsrl $a1, $sp, 4 \n" /* align stack on a */ \ 137 " dsll $sp, $a1, 4 \n" /* 16 byte boundary */ \ 138 " move $a1, $zero \n" \ 139 " .local ___start \n" \ 140 " dla $t9, ___start \n" \ 141 " jr $t9 \n" \ 142 " .end __start \n" \ 143 " .globl _csu_abort \n" \ 144 " .ent _csu_abort \n" \ 145 " .type _csu_abort, @function \n" \ 146 "_csu_abort: \n" \ 147 " teq $zero, $zero, 0x52 \n" \ 148 " .end _csu_abort \n" \ 149 " .previous") 150 151 struct kframe { 152 long kargc; 153 char *kargv[1]; /* size depends on kargc */ 154 char kargstr[1]; /* size varies */ 155 char kenvstr[1]; /* size varies */ 156 }; 157 158 #define MD_START_ARGS struct kframe *kfp, void (*cleanup)(void) 159 #define MD_START_SETUP \ 160 char **argv, **envp; \ 161 long argc; \ 162 \ 163 argc = kfp->kargc; \ 164 argv = &kfp->kargv[0]; \ 165 envp = argv + argc + 1; 166