2 * crt0.S -- startup file for MIPS.
4 * Copyright (c) 1995, 1996, 1997, 2001 Cygnus Support
6 * The authors hereby grant permission to use, copy, modify, distribute,
7 * and license this software and its documentation for any purpose, provided
8 * that existing copyright notices are retained in all copies and that this
9 * notice is included verbatim in any distributions. No written agreement,
10 * license, or royalty fee is required for any of the authorized uses.
11 * Modifications to this software may be copyrighted by their authors
12 * and need not follow the licensing terms described here, provided that
13 * the new terms are clearly indicated on the first page of each file where
18 /* This file contains 32 bit assembly code. */
25 * Set up some room for a stack. We just grab a chunk of memory.
27 #define STACK_SIZE 0x4000
28 #define GLOBAL_SIZE 0x2000
30 #define STARTUP_STACK_SIZE 0x0100
32 /* This is for referencing addresses that are not in the .sdata or
33 .sbss section under embedded-pic, or before we've set up gp. */
34 #ifdef __mips_embedded_pic
36 # define LA(t,x) la t,x-PICBASE ; daddu t,s0,t
38 # define LA(t,x) la t,x-PICBASE ; addu t,s0,t
40 #else /* __mips_embedded_pic */
41 # define LA(t,x) la t,x
42 #endif /* __mips_embedded_pic */
45 .comm __lstack, STARTUP_STACK_SIZE
50 /* Without the following nop, GDB thinks _start is a data variable.
51 * This is probably a bug in GDB in handling a symbol that is at the
52 * start of the .text section.
56 .globl hardware_hazard_hook .text
61 #ifdef __mips_embedded_pic
62 #define PICBASE start_PICBASE
68 #if !defined(__mips64) || (__mips_fpr==32)
69 #define STATUS_MASK (SR_CU1|SR_PE)
71 # For mips3 or mips4, turn on 64-bit addressing and additional float regs
72 #define STATUS_MASK (SR_CU1|SR_PE|SR_FR|SR_KX|SR_SX|SR_UX)
79 /* Avoid hazard from FPU enable and other SR changes. */
80 LA (t0, hardware_hazard_hook)
87 /* Check for FPU presence. Don't check if we know that soft_float is
88 being used. (This also avoids illegal instruction exceptions.) */
90 #ifndef __mips_soft_float
92 mtc1 t2,fp0 /* write to FPR 0 */
93 mtc1 zero,fp1 /* write to FPR 1 */
97 bne t0,t2,1f /* check for match */
99 bne t1,zero,1f /* double check */
101 j 2f /* FPU is present. */
105 /* FPU is not present. Set status register to say that. */
106 li v0, (STATUS_MASK-(STATUS_MASK & SR_CU1))
109 /* Avoid hazard from FPU disable. */
110 LA (t0, hardware_hazard_hook)
118 /* Fix high bits, if any, of the PC so that exception handling
119 doesn't get confused. */
124 LA (gp, _gp) # set the global data pointer
128 * zero out the bss section.
131 .globl get_mem_info .text
141 addiu v0,v0,4 # executed in delay slot
143 la t0, __lstack # make a small stack so we
144 addiu sp, t0, STARTUP_STACK_SIZE # can run some C code
145 la a0, __memsize # get the usable memory size
149 /* setup the stack pointer */
150 LA (t0, __stack) # is __stack set ?
154 /* NOTE: a0[0] contains the amount of memory available, and
155 not the last memory address. */
157 lw t0,0(a0) # last address of memory available
158 la t1,K0BASE # cached kernel memory
159 addu t0,t0,t1 # get the end of memory address
160 /* Allocate 32 bytes for the register parameters. Allocate 16
161 bytes for a null argv and envp. Round the result up to 64
162 bytes to preserve alignment. */
165 move sp,t0 # set stack pointer
169 * initialize target specific stuff. Only execute these
170 * functions it they exist.
172 .globl hardware_init_hook .text
173 .globl software_init_hook .text
174 .type _fini,@function
175 .type _init,@function
180 LA (t9, hardware_init_hook) # init the hardware if needed
186 LA (t9, software_init_hook) # init the hardware if needed
206 jal _init # run global constructors
209 addiu a1,sp,32 # argv = sp + 32
210 addiu a2,sp,40 # envp = sp + 40
212 sd zero,(a1) # argv[argc] = 0
213 sd zero,(a2) # envp[0] = 0
218 jal main # call the program start function
219 move a0,zero # set argc to 0
221 # fall through to the "exit" routine
222 jal exit # call libc exit to run the G++
224 move a0,v0 # pass through the exit code
228 /* Assume the PICBASE set up above is no longer valid below here. */
229 #ifdef __mips_embedded_pic
234 * _exit -- Exit from the application. Normally we cause a user trap
235 * to return to the ROM monitor for another run. NOTE: This is
236 * the only other routine we provide in the crt0.o object, since
237 * it may be tied to the "_start" routine. It also allows
238 * executables that contain a complete world to be linked with
239 * just the crt0.o object.
241 .globl hardware_exit_hook .text
246 #ifdef __mips_embedded_pic
247 /* Need to reinit PICBASE, since we might be called via exit()
248 rather than via a return path which would restore old s0. */
249 #define PICBASE exit_PICBASE
260 LA (t0, hardware_exit_hook)
267 # break instruction can cope with 0xfffff, but GAS limits the range:
269 b 7b # but loop back just in-case
273 /* Assume the PICBASE set up above is no longer valid below here. */
274 #ifdef __mips_embedded_pic