aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/os/common/startup/RISCV-ECLIC/compilers/GCC/crt0.S
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/os/common/startup/RISCV-ECLIC/compilers/GCC/crt0.S')
-rw-r--r--lib/chibios-contrib/os/common/startup/RISCV-ECLIC/compilers/GCC/crt0.S277
1 files changed, 277 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/common/startup/RISCV-ECLIC/compilers/GCC/crt0.S b/lib/chibios-contrib/os/common/startup/RISCV-ECLIC/compilers/GCC/crt0.S
new file mode 100644
index 000000000..222154873
--- /dev/null
+++ b/lib/chibios-contrib/os/common/startup/RISCV-ECLIC/compilers/GCC/crt0.S
@@ -0,0 +1,277 @@
1/*
2 ChibiOS - Copyright (C) 2020 Patrick Seidel
3 ChibiOS - Copyright (C) 2021 Stefan Kerkmann
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16*/
17
18/**
19 * @file crt0.S
20 * @brief RISC-V ECLIC startup file for ChibiOS.
21 *
22 * @addtogroup RISCV_ECLIC_STARTUP
23 * @{
24 */
25
26/*===========================================================================*/
27/* Module constants. */
28/*===========================================================================*/
29
30#if !defined(FALSE) || defined(__DOXYGEN__)
31#define FALSE 0
32#endif
33
34#if !defined(TRUE) || defined(__DOXYGEN__)
35#define TRUE 1
36#endif
37
38/*===========================================================================*/
39/* Module pre-compile time settings. */
40/*===========================================================================*/
41
42/**
43 * @brief Core initialization switch.
44 */
45#if !defined(CRT0_INIT_CORE) || defined(__DOXYGEN__)
46#define CRT0_INIT_CORE TRUE
47#endif
48
49/**
50 * @brief Stack segments initialization switch.
51 */
52#if !defined(CRT0_STACKS_FILL_PATTERN) || defined(__DOXYGEN__)
53#define CRT0_STACKS_FILL_PATTERN 0x55555555
54#endif
55
56/**
57 * @brief Stack segments initialization switch.
58 */
59#if !defined(CRT0_INIT_STACKS) || defined(__DOXYGEN__)
60#define CRT0_INIT_STACKS TRUE
61#endif
62
63/**
64 * @brief DATA segment initialization switch.
65 */
66#if !defined(CRT0_INIT_DATA) || defined(__DOXYGEN__)
67#define CRT0_INIT_DATA TRUE
68#endif
69
70/**
71 * @brief BSS segment initialization switch.
72 */
73#if !defined(CRT0_INIT_BSS) || defined(__DOXYGEN__)
74#define CRT0_INIT_BSS TRUE
75#endif
76
77/**
78 * @brief RAM areas initialization switch.
79 */
80#if !defined(CRT0_INIT_RAM_AREAS) || defined(__DOXYGEN__)
81#define CRT0_INIT_RAM_AREAS TRUE
82#endif
83
84/**
85 * @brief Constructors invocation switch.
86 */
87#if !defined(CRT0_CALL_CONSTRUCTORS) || defined(__DOXYGEN__)
88#define CRT0_CALL_CONSTRUCTORS TRUE
89#endif
90
91/**
92 * @brief Destructors invocation switch.
93 */
94#if !defined(CRT0_CALL_DESTRUCTORS) || defined(__DOXYGEN__)
95#define CRT0_CALL_DESTRUCTORS TRUE
96#endif
97
98/*===========================================================================*/
99/* Code section. */
100/*===========================================================================*/
101
102#if !defined(__DOXYGEN__)
103
104#include "riscv_encoding.h"
105
106.section .crt0, "ax"
107.global _start
108.type _start, @function
109_start:
110 /* Disable interrupts globaly */
111 csrc CSR_MSTATUS, MSTATUS_MIE
112
113 /* Jump to logical address first to ensure correct operation of RAM region */
114 la a0, _start
115 li a1, 1
116 slli a1, a1, 29
117 bleu a1, a0, _crt0_entry
118 srli a1, a1, 2
119 bleu a1, a0, _crt0_entry
120 la a0, _crt0_entry
121 add a0, a0, a1
122 jr a0
123
124/*
125 * CRT0 entry point.
126 */
127.global _crt0_entry
128.type _crt0_entry, @function
129_crt0_entry:
130 /* Disable interrupts globaly */
131 csrc CSR_MSTATUS, MSTATUS_MIE
132
133 /* Initialize global pointer gp for linker relaxation.*/
134 .option push
135 .option norelax
136 la gp, __global_pointer$
137 .option pop
138
139 /* Initialize stack pointer sp */
140 la sp, __process_stack_end__
141
142 /*
143 * Set the the NMI base mnvec to share
144 * with mtvec by setting CSR_MMISC_CTL
145 * bit 9 NMI_CAUSE_FFF to 1
146 */
147 li t0, MMISC_CTL_NMI_CAUSE_FFF
148 csrs CSR_MMISC_CTL, t0
149
150 /*
151 * Intialize ECLIC vector interrupt
152 * base address mtvt to vector_base
153 */
154 la t0, vector_base
155 csrw CSR_MTVT, t0
156
157 /*
158 * Set ECLIC non-vector entry to be controlled
159 * by mtvt2 CSR register.
160 * Intialize ECLIC non-vector interrupt
161 * base address mtvt2 to _irq_handler.
162 */
163 la t0, _irq_handler
164 csrw CSR_MTVT2, t0
165 csrs CSR_MTVT2, 0x1
166
167 /*
168 * Set Exception Entry MTVEC to _start_trap
169 * Due to settings above, Exception and NMI
170 * will share common entry.
171 */
172 la t0, _start_trap
173 csrw CSR_MTVEC, t0
174
175 /* Set the interrupt processing mode to ECLIC mode */
176 li t0, 0x3f
177 csrc CSR_MTVEC, t0
178 csrs CSR_MTVEC, 0x3
179
180 /* Enable mcycle and minstret counters */
181 csrci CSR_MCOUNTINHIBIT, 0x5
182
183#if CRT0_INIT_CORE == TRUE
184 /* Core initialization.*/
185 jal ra, __core_init
186#endif
187
188 /* Early initialization..*/
189 jal ra, __early_init
190
191#if CRT0_INIT_STACKS == TRUE
192 li a0, CRT0_STACKS_FILL_PATTERN
193 /* Main Stack initialization. Note, it assumes that the
194 stack size is a multiple of 4 so the linker file must
195 ensure this.*/
196 la a1, __process_stack_end__
197 la a2, __process_stack_end__
198psloop:
199 bge a1, a2, endpsloop
200 sw a0, 0(a1)
201 addi a1, a1, 4
202 j psloop
203endpsloop:
204#endif
205
206#if CRT0_INIT_DATA == TRUE
207 /* Data initialization. Note, it assumes that the DATA size
208 is a multiple of 4 so the linker file must ensure this.*/
209 la a0, __textdata_base__
210 la a1, __data_base__
211 la a2, __data_end__
212dloop:
213 bge a1, a2, enddloop
214 lw a3, 0(a0)
215 sw a3, 0(a1)
216 addi a0, a0, 4
217 addi a1, a1, 4
218 j dloop
219enddloop:
220#endif
221
222#if CRT0_INIT_BSS == TRUE
223 /* BSS initialization. Note, it assumes that the DATA size
224 is a multiple of 4 so the linker file must ensure this.*/
225 la a1, __bss_base__
226 la a2, __bss_end__
227bloop:
228 bge a1, a2, endbloop
229 sw zero, 0(a1)
230 addi a1, a1, 4
231 j bloop
232endbloop:
233#endif
234
235#if CRT0_INIT_RAM_AREAS == TRUE
236 /* RAM areas initialization.*/
237 jal ra, __init_ram_areas
238#endif
239
240 /* Late initialization..*/
241 jal ra, __late_init
242
243#if CRT0_CALL_CONSTRUCTORS == TRUE
244 /* Constructors invocation.*/
245 la s1, __init_array_base__
246 la s2, __init_array_end__
247initloop:
248 bge s1, s2, endinitloop
249 lw a0, 0(s1)
250 jalr ra, a0, 0
251 addi s1, s1, 4
252 j initloop
253endinitloop:
254#endif
255
256 /* Main program invocation, r0 contains the returned value.*/
257 jal ra, main
258
259#if CRT0_CALL_DESTRUCTORS == TRUE
260 /* Destructors invocation.*/
261 la s1, __fini_array_base__
262 la s2, __fini_array_end__
263finiloop:
264 bge s1, s2, endfiniloop
265 lw a0, 0(s1)
266 jalr ra, a0, 0
267 addi s1, s1, 4
268 j finiloop
269endfiniloop:
270#endif
271
272 /* Branching to the defined exit handler.*/
273 jal ra, __default_exit
274
275#endif
276
277/** @} */