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
69 # define STATUS_MASK (SR_CU1|SR_PE)
71 /* Post-mips2 has no SR_PE bit. */
73 /* Turn on 64-bit addressing and additional float regs. */
74 # define STATUS_MASK (SR_CU1|SR_FR|SR_KX|SR_SX|SR_UX)
77 # define STATUS_MASK (SR_CU1)
79 /* Turn on additional float regs. */
80 # define STATUS_MASK (SR_CU1|SR_FR)
89 /* Avoid hazard from FPU enable and other SR changes. */
90 LA (t0, hardware_hazard_hook)
97 /* Check for FPU presence. Don't check if we know that soft_float is
98 being used. (This also avoids illegal instruction exceptions.) */
100 #ifndef __mips_soft_float
102 mtc1 t2,fp0 /* write to FPR 0 */
103 mtc1 zero,fp1 /* write to FPR 1 */
107 bne t0,t2,1f /* check for match */
109 bne t1,zero,1f /* double check */
111 j 2f /* FPU is present. */
115 /* FPU is not present. Set status register to say that. */
116 li v0, (STATUS_MASK-(STATUS_MASK & SR_CU1))
119 /* Avoid hazard from FPU disable. */
120 LA (t0, hardware_hazard_hook)
128 /* Fix high bits, if any, of the PC so that exception handling
129 doesn't get confused. */
134 LA (gp, _gp) # set the global data pointer
138 * zero out the bss section.
141 .globl get_mem_info .text
151 addiu v0,v0,4 # executed in delay slot
153 la t0, __lstack # make a small stack so we
154 addiu sp, t0, STARTUP_STACK_SIZE # can run some C code
155 la a0, __memsize # get the usable memory size
159 /* setup the stack pointer */
160 LA (t0, __stack) # is __stack set ?
164 /* NOTE: a0[0] contains the amount of memory available, and
165 not the last memory address. */
167 lw t0,0(a0) # last address of memory available
168 la t1,K0BASE # cached kernel memory
169 addu t0,t0,t1 # get the end of memory address
170 /* Allocate 32 bytes for the register parameters. Allocate 16
171 bytes for a null argv and envp. Round the result up to 64
172 bytes to preserve alignment. */
175 move sp,t0 # set stack pointer
179 * initialize target specific stuff. Only execute these
180 * functions it they exist.
182 .globl hardware_init_hook .text
183 .globl software_init_hook .text
184 .type _fini,@function
185 .type _init,@function
190 LA (t9, hardware_init_hook) # init the hardware if needed
196 LA (t9, software_init_hook) # init the hardware if needed
216 jal _init # run global constructors
219 addiu a1,sp,32 # argv = sp + 32
220 addiu a2,sp,40 # envp = sp + 40
222 sd zero,(a1) # argv[argc] = 0
223 sd zero,(a2) # envp[0] = 0
228 jal main # call the program start function
229 move a0,zero # set argc to 0
231 # fall through to the "exit" routine
232 jal exit # call libc exit to run the G++
234 move a0,v0 # pass through the exit code
238 /* Assume the PICBASE set up above is no longer valid below here. */
239 #ifdef __mips_embedded_pic
244 * _exit -- Exit from the application. Normally we cause a user trap
245 * to return to the ROM monitor for another run. NOTE: This is
246 * the only other routine we provide in the crt0.o object, since
247 * it may be tied to the "_start" routine. It also allows
248 * executables that contain a complete world to be linked with
249 * just the crt0.o object.
251 .globl hardware_exit_hook .text
256 #ifdef __mips_embedded_pic
257 /* Need to reinit PICBASE, since we might be called via exit()
258 rather than via a return path which would restore old s0. */
259 #define PICBASE exit_PICBASE
270 LA (t0, hardware_exit_hook)
277 # break instruction can cope with 0xfffff, but GAS limits the range:
279 b 7b # but loop back just in-case
283 /* Assume the PICBASE set up above is no longer valid below here. */
284 #ifdef __mips_embedded_pic