2 * FreeRTOS Kernel V10.1.1
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * The FreeRTOS kernel's RISC-V port is split between the the code that is
\r
30 * common across all currently supported RISC-V chips (implementations of the
\r
31 * RISC-V ISA), and code which tailors the port to a specific RISC-V chip:
\r
33 * + The code that is common to all RISC-V chips is implemented in
\r
34 * FreeRTOS\Source\portable\GCC\RISC-V-RV32\portASM.S. There is only one
\r
35 * portASM.S file because the same file is used no matter which RISC-V chip is
\r
38 * + The code that tailors the kernel's RISC-V port to a specific RISC-V
\r
39 * chip is implemented in freertos_risc_v_port_specific_extensions.h. There
\r
40 * is one freertos_risc_v_port_specific_extensions.h that can be used with any
\r
41 * RISC-V chip that both includes a standard CLINT and does not add to the
\r
42 * base set of RISC-V registers. There are additional
\r
43 * freertos_risc_v_port_specific_extensions.h files for RISC-V implementations
\r
44 * that do not include a standard CLINT or do add to the base set of RISC-V
\r
47 * CARE MUST BE TAKEN TO INCLDUE THE CORRECT
\r
48 * freertos_risc_v_port_specific_extensions.h HEADER FILE FOR THE CHIP
\r
49 * IN USE. To include the correct freertos_risc_v_port_specific_extensions.h
\r
50 * header file ensure the path to the correct header file is in the assembler's
\r
53 * This freertos_risc_v_port_specific_extensions.h is for use on RISC-V chips
\r
54 * that include a standard CLINT and do not add to the base set of RISC-V
\r
58 #include "freertos_risc_v_port_specific_extensions.h"
\r
60 /* Check the freertos_risc_v_port_specific_extensions.h and/or command line
\r
62 #ifndef portasmHAS_CLINT
\r
63 #error freertos_risc_v_port_specific_extensions.h must define portasmHAS_CLINT to either 1 (CLINT present) or 0 (clint not present).
\r
66 #ifndef portasmHANDLE_INTERRUPT
\r
67 #error portasmHANDLE_INTERRUPT must be defined to the function to be called to handle external/peripheral interrupts. portasmHANDLE_INTERRUPT can be defined on the assmbler command line or in the appropriate freertos_risc_v_port_specific_extensions.h header file.
\r
70 #ifndef portasmSAVE_ADDITIONAL_REGISTERS
\r
71 /* portasmSAVE_ADDITIONAL_REGISTERS is not defined so assume no additional
\r
72 registers need to be saved. */
\r
73 #define portasmSAVE_ADDITIONAL_REGISTERS
\r
76 #ifndef portasmRESTORE_ADDITIONAL_REGISTERS
\r
77 /* portasmRESTORE_ADDITIONAL_REGISTERS is not defined so assume no
\r
78 additional registers need to be restored. */
\r
79 #define portasmRESTORE_ADDITIONAL_REGISTERS
\r
82 #if __riscv_xlen == 64
\r
83 #error Not implemented yet - change lw to ld, and sw to sd.
\r
84 #define portWORD_SIZE 8
\r
85 #elif __riscv_xlen == 32
\r
86 #define portWORD_SIZE 4
\r
88 #error Assembler did not define __riscv_xlen
\r
91 /* Only the standard core registers are stored by default. Any additional
\r
92 registers must be saved by the portasmSAVE_ADDITIONAL_REGISTERS and
\r
93 portasmRESTORE_ADDITIONAL_REGISTERS macros - which can be defined in a chip
\r
94 specific version of freertos_risc_v_port_specific_extensions.h. See the notes
\r
95 at the top of this file. */
\r
96 #define portCONTEXT_SIZE ( 30 * portWORD_SIZE )
\r
98 .global xPortStartFirstTask
\r
99 .global vFreeRTOSPortTrapHandler
\r
100 .extern pxCurrentTCB
\r
101 .extern ulPortTrapHandler
\r
102 .extern vTaskSwitchContext
\r
103 .extern Timer_IRQHandler
\r
104 .extern pullMachineTimerCompareRegister
\r
105 .extern pullNextTime
\r
106 .extern ulTimerIncrementsForOneTick
\r
107 .extern xISRStackTop
\r
108 .extern vPortHandleInterrupt
\r
110 /*-----------------------------------------------------------*/
\r
113 vFreeRTOSPortTrapHandler:
\r
114 addi sp, sp, -portCONTEXT_SIZE
\r
115 sw x1, 1 * portWORD_SIZE( sp )
\r
116 sw x5, 2 * portWORD_SIZE( sp )
\r
117 sw x6, 3 * portWORD_SIZE( sp )
\r
118 sw x7, 4 * portWORD_SIZE( sp )
\r
119 sw x8, 5 * portWORD_SIZE( sp )
\r
120 sw x9, 6 * portWORD_SIZE( sp )
\r
121 sw x10, 7 * portWORD_SIZE( sp )
\r
122 sw x11, 8 * portWORD_SIZE( sp )
\r
123 sw x12, 9 * portWORD_SIZE( sp )
\r
124 sw x13, 10 * portWORD_SIZE( sp )
\r
125 sw x14, 11 * portWORD_SIZE( sp )
\r
126 sw x15, 12 * portWORD_SIZE( sp )
\r
127 sw x16, 13 * portWORD_SIZE( sp )
\r
128 sw x17, 14 * portWORD_SIZE( sp )
\r
129 sw x18, 15 * portWORD_SIZE( sp )
\r
130 sw x19, 16 * portWORD_SIZE( sp )
\r
131 sw x20, 17 * portWORD_SIZE( sp )
\r
132 sw x21, 18 * portWORD_SIZE( sp )
\r
133 sw x22, 19 * portWORD_SIZE( sp )
\r
134 sw x23, 20 * portWORD_SIZE( sp )
\r
135 sw x24, 21 * portWORD_SIZE( sp )
\r
136 sw x25, 22 * portWORD_SIZE( sp )
\r
137 sw x26, 23 * portWORD_SIZE( sp )
\r
138 sw x27, 24 * portWORD_SIZE( sp )
\r
139 sw x28, 25 * portWORD_SIZE( sp )
\r
140 sw x29, 26 * portWORD_SIZE( sp )
\r
141 sw x30, 27 * portWORD_SIZE( sp )
\r
142 sw x31, 28 * portWORD_SIZE( sp )
\r
144 portasmSAVE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_port_specific_extensions.h to save any registers unique to the RISC-V implementation. */
\r
146 csrr t0, mstatus /* Required for MPIE bit. */
\r
147 sw t0, 29 * portWORD_SIZE( sp )
\r
149 lw t0, pxCurrentTCB /* Load pxCurrentTCB. */
\r
150 sw sp, 0( t0 ) /* Write sp to first TCB member. */
\r
155 test_if_asynchronous:
\r
156 srli a2, a0, 0x1f /* MSB of mcause is 1 if handing an asynchronous interrupt - shift to LSB to clear other bits. */
\r
157 beq a2, x0, handle_synchronous /* Branch past interrupt handing if not asynchronous. */
\r
158 sw a1, 0( sp ) /* Asynch so save unmodified exception return address. */
\r
160 handle_asynchronous:
\r
162 #if( portasmHAS_CLINT != 0 )
\r
164 test_if_mtimer: /* If there is a CLINT then the mtimer is used to generate the tick interrupt. */
\r
166 addi t1, t0, 7 /* 0x80000007 == machine timer interrupt. */
\r
167 bne a0, t1, test_if_external_interrupt
\r
169 lw t0, pullMachineTimerCompareRegister /* Load address of compare register into t0. */
\r
170 lw t1, pullNextTime /* Load the address of ullNextTime into t1. */
\r
171 lw t2, 0(t1) /* Load the low word of ullNextTime into t2. */
\r
172 lw t3, 4(t1) /* Load the high word of ullNextTime into t3. */
\r
173 sw t2, 0(t0) /* Store low word of ullNextTime into compare register. */
\r
174 sw t3, 4(t0) /* Store high word of ullNextTime into compare register. */
\r
175 lw t0, ulTimerIncrementsForOneTick /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */
\r
176 add t4, t0, t2 /* Add the low word of ullNextTime to the timer increments for one tick (assumes timer increment for one tick fits in 32-bits. */
\r
177 sltu t5, t4, t2 /* See if the sum of low words overflowed (what about the zero case?). */
\r
178 add t6, t3, t5 /* Add overflow to high word of ullNextTime. */
\r
179 sw t4, 0(t1) /* Store new low word of ullNextTime. */
\r
180 sw t6, 4(t1) /* Store new high word of ullNextTime. */
\r
181 lw sp, xISRStackTop /* Switch to ISR stack before function call. */
\r
182 jal xTaskIncrementTick
\r
183 beqz a0, processed_source /* Don't switch context if incrementing tick didn't unblock a task. */
\r
184 jal vTaskSwitchContext
\r
187 test_if_external_interrupt: /* If there is a CLINT and the mtimer interrupt is not pending then check to see if an external interrupt is pending. */
\r
188 addi t1, t1, 4 /* 0x80000007 + 4 = 0x8000000b == Machine external interrupt. */
\r
189 bne a0, t1, as_yet_unhandled /* Something as yet unhandled. */
\r
191 #endif /* portasmHAS_CLINT */
\r
193 lw sp, xISRStackTop /* Switch to ISR stack before function call. */
\r
194 jal portasmHANDLE_INTERRUPT /* Jump to the interrupt handler if there is no CLINT or if there is a CLINT and it has been determined that an external interrupt is pending. */
\r
197 handle_synchronous:
\r
198 addi a1, a1, 4 /* Synchronous so updated exception return address to the instruction after the instruction that generated the exeption. */
\r
199 sw a1, 0( sp ) /* Save updated exception return address. */
\r
201 test_if_environment_call:
\r
202 li t0, 11 /* 11 == environment call. */
\r
203 bne a0, t0, is_exception /* Not an M environment call, so some other exception. */
\r
204 lw sp, xISRStackTop /* Switch to ISR stack before function call. */
\r
205 jal vTaskSwitchContext
\r
217 lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
\r
218 lw sp, 0( sp ) /* Read sp from first TCB member. */
\r
220 /* Load mret with the address of the next task. */
\r
224 /* Load mstatus with the interrupt enable bits used by the task. */
\r
225 lw t0, 29 * portWORD_SIZE( sp )
\r
226 csrw mstatus, t0 /* Required for MPIE bit. */
\r
228 portasmRESTORE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_port_specific_extensions.h to restore any registers unique to the RISC-V implementation. */
\r
230 lw x1, 1 * portWORD_SIZE( sp )
\r
231 lw x5, 2 * portWORD_SIZE( sp ) /* t0 */
\r
232 lw x6, 3 * portWORD_SIZE( sp ) /* t1 */
\r
233 lw x7, 4 * portWORD_SIZE( sp ) /* t2 */
\r
234 lw x8, 5 * portWORD_SIZE( sp ) /* s0/fp */
\r
235 lw x9, 6 * portWORD_SIZE( sp ) /* s1 */
\r
236 lw x10, 7 * portWORD_SIZE( sp ) /* a0 */
\r
237 lw x11, 8 * portWORD_SIZE( sp ) /* a1 */
\r
238 lw x12, 9 * portWORD_SIZE( sp ) /* a2 */
\r
239 lw x13, 10 * portWORD_SIZE( sp ) /* a3 */
\r
240 lw x14, 11 * portWORD_SIZE( sp ) /* a4 */
\r
241 lw x15, 12 * portWORD_SIZE( sp ) /* a5 */
\r
242 lw x16, 13 * portWORD_SIZE( sp ) /* a6 */
\r
243 lw x17, 14 * portWORD_SIZE( sp ) /* a7 */
\r
244 lw x18, 15 * portWORD_SIZE( sp ) /* s2 */
\r
245 lw x19, 16 * portWORD_SIZE( sp ) /* s3 */
\r
246 lw x20, 17 * portWORD_SIZE( sp ) /* s4 */
\r
247 lw x21, 18 * portWORD_SIZE( sp ) /* s5 */
\r
248 lw x22, 19 * portWORD_SIZE( sp ) /* s6 */
\r
249 lw x23, 20 * portWORD_SIZE( sp ) /* s7 */
\r
250 lw x24, 21 * portWORD_SIZE( sp ) /* s8 */
\r
251 lw x25, 22 * portWORD_SIZE( sp ) /* s9 */
\r
252 lw x26, 23 * portWORD_SIZE( sp ) /* s10 */
\r
253 lw x27, 24 * portWORD_SIZE( sp ) /* s11 */
\r
254 lw x28, 25 * portWORD_SIZE( sp ) /* t3 */
\r
255 lw x29, 26 * portWORD_SIZE( sp ) /* t4 */
\r
256 lw x30, 27 * portWORD_SIZE( sp ) /* t5 */
\r
257 lw x31, 28 * portWORD_SIZE( sp ) /* t6 */
\r
258 addi sp, sp, portCONTEXT_SIZE
\r
261 /*-----------------------------------------------------------*/
\r
264 xPortStartFirstTask:
\r
266 #if( portasmHAS_CLINT != 0 )
\r
267 /* If there is a clint then interrupts can branch directly to the FreeRTOS
\r
268 trap handler. Otherwise the interrupt controller will need to be configured
\r
269 outside of this file. */
\r
270 la t0, vFreeRTOSPortTrapHandler
\r
272 #endif /* portasmHAS_CLILNT */
\r
274 lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
\r
275 lw sp, 0( sp ) /* Read sp from first TCB member. */
\r
277 lw x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */
\r
278 lw x5, 2 * portWORD_SIZE( sp ) /* t0 */
\r
279 lw x6, 3 * portWORD_SIZE( sp ) /* t1 */
\r
280 lw x7, 4 * portWORD_SIZE( sp ) /* t2 */
\r
281 lw x8, 5 * portWORD_SIZE( sp ) /* s0/fp */
\r
282 lw x9, 6 * portWORD_SIZE( sp ) /* s1 */
\r
283 lw x10, 7 * portWORD_SIZE( sp ) /* a0 */
\r
284 lw x11, 8 * portWORD_SIZE( sp ) /* a1 */
\r
285 lw x12, 9 * portWORD_SIZE( sp ) /* a2 */
\r
286 lw x13, 10 * portWORD_SIZE( sp ) /* a3 */
\r
287 lw x14, 11 * portWORD_SIZE( sp ) /* a4 */
\r
288 lw x15, 12 * portWORD_SIZE( sp ) /* a5 */
\r
289 lw x16, 13 * portWORD_SIZE( sp ) /* a6 */
\r
290 lw x17, 14 * portWORD_SIZE( sp ) /* a7 */
\r
291 lw x18, 15 * portWORD_SIZE( sp ) /* s2 */
\r
292 lw x19, 16 * portWORD_SIZE( sp ) /* s3 */
\r
293 lw x20, 17 * portWORD_SIZE( sp ) /* s4 */
\r
294 lw x21, 18 * portWORD_SIZE( sp ) /* s5 */
\r
295 lw x22, 19 * portWORD_SIZE( sp ) /* s6 */
\r
296 lw x23, 20 * portWORD_SIZE( sp ) /* s7 */
\r
297 lw x24, 21 * portWORD_SIZE( sp ) /* s8 */
\r
298 lw x25, 22 * portWORD_SIZE( sp ) /* s9 */
\r
299 lw x26, 23 * portWORD_SIZE( sp ) /* s10 */
\r
300 lw x27, 24 * portWORD_SIZE( sp ) /* s11 */
\r
301 lw x28, 25 * portWORD_SIZE( sp ) /* t3 */
\r
302 lw x29, 26 * portWORD_SIZE( sp ) /* t4 */
\r
303 lw x30, 27 * portWORD_SIZE( sp ) /* t5 */
\r
304 lw x31, 28 * portWORD_SIZE( sp ) /* t6 */
\r
305 addi sp, sp, portCONTEXT_SIZE
\r
306 csrs mstatus, 8 /* Enable machine interrupts. */
\r
309 /*-----------------------------------------------------------*/
\r