1 /* Copyright (c) 2017-2018 SiFive Inc. All rights reserved.
3 This copyrighted material is made available to anyone wishing to use,
4 modify, copy, or redistribute it subject to the terms and conditions
5 of the FreeBSD License. This program is distributed in the hope that
6 it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
7 including the implied warranties of MERCHANTABILITY or FITNESS FOR
8 A PARTICULAR PURPOSE. A copy of this license is available at
9 http://www.opensource.org/licenses.
12 /* crt0.S: Entry point for RISC-V METAL programs. */
14 .section .text.libgloss.start
16 .type _start, @function
18 /* _start is defined by the METAL to have been called with the following
20 * a0: the hart ID of the currently executing hart. Harts can start at
21 * any arbitrary point, it's the C library's job to ensure the code is
23 * a1: a pointer to a description of the machine on which this code is
24 * currently executing. This is probably 0 on an embedded system
25 * because they tend to not be dynamically portable. As such, newlib
26 * ignores this argument.
27 * a2: a pointer to a function that must be run after the envirnoment has
28 * been initialized, but before user code can be expected to be run.
29 * If this is 0 then there is no function to be run. */
34 /* This is a bit funky: it's not usually sane for _start to return, but in
35 * this case we actually want to in order to signal an error to the METAL. */
38 /* Before doing anything we must initialize the global pointer, as we cannot
39 * safely perform any access that may be relaxed without GP being set. This
40 * is done with relaxation disabled to avoid relaxing the address calculation
41 * to just "addi gp, gp, 0". */
44 la gp, __global_pointer$
47 /* The METAL is designed for a bare-metal environment and therefor is expected
48 * to define its own stack pointer. We also align the stack pointer here
49 * because the only RISC-V ABI that's currently defined mandates 16-byte
53 /* Increment by hartid number of stack sizes */
64 /* If we're not hart 0, skip the initialization work */
65 la t0, __metal_boot_hart
66 bne a0, t0, _skip_init
68 /* Embedded systems frequently require relocating the data segment before C
69 * code can be run -- for example, the data segment may exist in flash upon
70 * boot and then need to get relocated into a non-persistant writable memory
71 * before C code can execute. If this is the case we do so here. This step
72 * is optional: if the METAL provides an environment in which this relocation
73 * is not necessary then it must simply set metal_segment_data_source_start to
74 * be equal to metal_segment_data_target_start. */
75 la t0, metal_segment_data_source_start
76 la t1, metal_segment_data_target_start
77 la t2, metal_segment_data_target_end
90 /* Copy the ITIM section */
91 la t0, metal_segment_itim_source_start
92 la t1, metal_segment_itim_target_start
93 la t2, metal_segment_itim_target_end
106 /* Zero the BSS segment. */
107 la t1, metal_segment_bss_target_start
108 la t2, metal_segment_bss_target_end
118 /* At this point we're in an environment that can execute C code. The first
119 * thing to do is to make the callback to the parent environment if it's been
120 * requested to do so. */
125 /* The RISC-V port only uses new-style constructors and destructors. */
126 la a0, __libc_fini_array
128 call __libc_init_array
132 /* Synchronize harts so that secondary harts wait until hart 0 finishes
134 call _synchronize_harts
136 /* Check RISC-V isa and enable FS bits if Floating Point architecture. */
148 /* This is a C runtime, so main() is defined to have some arguments. Since
149 * there's nothing sane the METAL can pass we don't bother with that but
150 * instead just setup as close to a NOP as we can. */
151 li a0, 1 /* argc=1 */
152 la a1, argv /* argv = {"libgloss", NULL} */
153 la a2, envp /* envp = {NULL} */
156 /* Call exit to handle libc's cleanup routines. Under normal contains this
157 * shouldn't even get called, but I'm still not using a tail call here
158 * because returning to the METAL is the right thing to do in pathological
162 /* And here's where we return. Again, it's a bit odd but the METAL defines
163 * this as a bad idea (ie, as opposed to leaving it undefined) and at this
164 * point it's really the only thing left to do. */
170 /* RISC-V systems always use __libc_{init,fini}_array, but for compatibility we
171 * define _{init,fini} to do nothing. */
173 .type _init, @function
175 .type _fini, @function
182 /* By default, secondary_main will cause secondary harts to spin forever.
183 * Users can redefine secondary_main themselves to run code on secondary harts */
185 .global secondary_main
186 .type secondary_main, @function
190 #if __riscv_xlen == 32
196 la t1, __metal_boot_hart
203 #if __riscv_xlen == 32
211 /* This shim allows main() to be passed a set of arguments that can satisfy the
212 * requirements of the C API. */
213 .section .rodata.libgloss.start