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 t
\r
11 o permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * http://www.FreeRTOS.org
\r
25 * http://aws.amazon.com/freertos
\r
27 * 1 tab == 4 spaces!
\r
30 #if __riscv_xlen == 64
\r
31 #error Not implemented yet - change lw to ld, and sw to sd.
\r
33 #elif __riscv_xlen == 32
\r
36 #error Assembler has not defined __riscv_xlen
\r
39 #define CONTEXT_SIZE ( 30 * WORD_SIZE )
\r
41 .global xPortStartFirstTask
\r
42 .global vPortTrapHandler
\r
43 .extern pxCurrentTCB
\r
44 .extern ulPortTrapHandler
\r
45 .extern vTaskSwitchContext
\r
46 .extern Timer_IRQHandler
\r
49 .extern pullMachineTimerCompareRegister
\r
50 .extern pullNextTime
\r
51 .extern ulTimerIncrementsForOneTick
\r
52 .extern xISRStackTop
\r
53 .extern vPortHandleInterrupt
\r
55 /*-----------------------------------------------------------*/
\r
59 addi sp, sp, -CONTEXT_SIZE
\r
60 sw x1, 1 * WORD_SIZE( sp )
\r
61 sw x5, 2 * WORD_SIZE( sp )
\r
62 sw x6, 3 * WORD_SIZE( sp )
\r
63 sw x7, 4 * WORD_SIZE( sp )
\r
64 sw x8, 5 * WORD_SIZE( sp )
\r
65 sw x9, 6 * WORD_SIZE( sp )
\r
66 sw x10, 7 * WORD_SIZE( sp )
\r
67 sw x11, 8 * WORD_SIZE( sp )
\r
68 sw x12, 9 * WORD_SIZE( sp )
\r
69 sw x13, 10 * WORD_SIZE( sp )
\r
70 sw x14, 11 * WORD_SIZE( sp )
\r
71 sw x15, 12 * WORD_SIZE( sp )
\r
72 sw x16, 13 * WORD_SIZE( sp )
\r
73 sw x17, 14 * WORD_SIZE( sp )
\r
74 sw x18, 15 * WORD_SIZE( sp )
\r
75 sw x19, 16 * WORD_SIZE( sp )
\r
76 sw x20, 17 * WORD_SIZE( sp )
\r
77 sw x21, 18 * WORD_SIZE( sp )
\r
78 sw x22, 19 * WORD_SIZE( sp )
\r
79 sw x23, 20 * WORD_SIZE( sp )
\r
80 sw x24, 21 * WORD_SIZE( sp )
\r
81 sw x25, 22 * WORD_SIZE( sp )
\r
82 sw x26, 23 * WORD_SIZE( sp )
\r
83 sw x27, 24 * WORD_SIZE( sp )
\r
84 sw x28, 25 * WORD_SIZE( sp )
\r
85 sw x29, 26 * WORD_SIZE( sp )
\r
86 sw x30, 27 * WORD_SIZE( sp )
\r
87 sw x31, 28 * WORD_SIZE( sp )
\r
89 csrr t0, mstatus /* Required for MPIE bit. */
\r
90 sw t0, 29 * WORD_SIZE( sp )
\r
92 lw t0, pxCurrentTCB /* Load pxCurrentTCB. */
\r
93 sw sp, 0( t0 ) /* Write sp to first TCB member. */
\r
98 test_if_environment_call:
\r
99 li t0, 11 /* 11 == environment call when using qemu. */
\r
100 bne a0, t0, test_if_timer
\r
101 addi a1, a1, 4 /* Synchronous so return to the instruction after the environment call. */
\r
102 sw a1, 0( sp ) /* Save updated exception return address. */
\r
103 lw sp, xISRStackTop /* Switch to ISR stack before function call. */
\r
104 jal vTaskSwitchContext
\r
109 sw a1, 0( sp ) /* Asynch so save unmodified exception return address. */
\r
112 addi t1,t0, 7 /* 0x80000007 == machine timer interrupt. */
\r
113 bne a0, t1, test_if_external_interrupt
\r
115 lw t0, pullMachineTimerCompareRegister /* Load address of compare register into t0. */
\r
116 lw t1, pullNextTime /* Load the address of ullNextTime into t1. */
\r
117 lw t2, 0(t1) /* Load the low word of ullNextTime into t2. */
\r
118 lw t3, 4(t1) /* Load the high word of ullNextTime into t3. */
\r
119 sw t2, 0(t0) /* Store low word of ullNextTime into compare register. */
\r
120 sw t3, 4(t0) /* Store high word of ullNextTime into compare register. */
\r
121 lw t0, ulTimerIncrementsForOneTick /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */
\r
122 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
123 sltu t5, t4, t2 /* See if the sum of low words overflowed (what about the zero case?). */
\r
124 add t6, t3, t5 /* Add overflow to high word of ullNextTime. */
\r
125 sw t4, 0(t1) /* Store new low word of ullNextTime. */
\r
126 sw t6, 4(t1) /* Store new high word of ullNextTime. */
\r
127 lw sp, xISRStackTop /* Switch to ISR stack before function call. */
\r
128 jal xTaskIncrementTick
\r
129 beqz a0, processed_source /* Don't switch context if incrementing tick didn't unblock a task. */
\r
130 jal vTaskSwitchContext
\r
133 test_if_external_interrupt:
\r
134 addi t1, t1, 4 /* 0x80000007 + 4 = 0x8000000b == Machine external interrupt. */
\r
135 bne a0, t1, is_exception /* Only thing left it can be. */
\r
136 jal vPortHandleInterrupt
\r
144 lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
\r
145 lw sp, 0( sp ) /* Read sp from first TCB member. */
\r
147 /* Load mret with the address of the next task. */
\r
151 /* Load mstatus with the interrupt enable bits used by the task. */
\r
152 lw t0, 29 * WORD_SIZE( sp )
\r
153 csrw mstatus, t0 /* Required for MPIE bit. */
\r
155 lw x1, 1 * WORD_SIZE( sp )
\r
156 lw x5, 2 * WORD_SIZE( sp ) /* t0 */
\r
157 lw x6, 3 * WORD_SIZE( sp ) /* t1 */
\r
158 lw x7, 4 * WORD_SIZE( sp ) /* t2 */
\r
159 lw x8, 5 * WORD_SIZE( sp ) /* s0/fp */
\r
160 lw x9, 6 * WORD_SIZE( sp ) /* s1 */
\r
161 lw x10, 7 * WORD_SIZE( sp ) /* a0 */
\r
162 lw x11, 8 * WORD_SIZE( sp ) /* a1 */
\r
163 lw x12, 9 * WORD_SIZE( sp ) /* a2 */
\r
164 lw x13, 10 * WORD_SIZE( sp ) /* a3 */
\r
165 lw x14, 11 * WORD_SIZE( sp ) /* a4 */
\r
166 lw x15, 12 * WORD_SIZE( sp ) /* a5 */
\r
167 lw x16, 13 * WORD_SIZE( sp ) /* a6 */
\r
168 lw x17, 14 * WORD_SIZE( sp ) /* a7 */
\r
169 lw x18, 15 * WORD_SIZE( sp ) /* s2 */
\r
170 lw x19, 16 * WORD_SIZE( sp ) /* s3 */
\r
171 lw x20, 17 * WORD_SIZE( sp ) /* s4 */
\r
172 lw x21, 18 * WORD_SIZE( sp ) /* s5 */
\r
173 lw x22, 19 * WORD_SIZE( sp ) /* s6 */
\r
174 lw x23, 20 * WORD_SIZE( sp ) /* s7 */
\r
175 lw x24, 21 * WORD_SIZE( sp ) /* s8 */
\r
176 lw x25, 22 * WORD_SIZE( sp ) /* s9 */
\r
177 lw x26, 23 * WORD_SIZE( sp ) /* s10 */
\r
178 lw x27, 24 * WORD_SIZE( sp ) /* s11 */
\r
179 lw x28, 25 * WORD_SIZE( sp ) /* t3 */
\r
180 lw x29, 26 * WORD_SIZE( sp ) /* t4 */
\r
181 lw x30, 27 * WORD_SIZE( sp ) /* t5 */
\r
182 lw x31, 28 * WORD_SIZE( sp ) /* t6 */
\r
183 addi sp, sp, CONTEXT_SIZE
\r
186 /*-----------------------------------------------------------*/
\r
189 xPortStartFirstTask:
\r
191 la t0, vPortTrapHandler
\r
194 lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
\r
195 lw sp, 0( sp ) /* Read sp from first TCB member. */
\r
197 lw x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */
\r
198 lw x5, 2 * WORD_SIZE( sp ) /* t0 */
\r
199 lw x6, 3 * WORD_SIZE( sp ) /* t1 */
\r
200 lw x7, 4 * WORD_SIZE( sp ) /* t2 */
\r
201 lw x8, 5 * WORD_SIZE( sp ) /* s0/fp */
\r
202 lw x9, 6 * WORD_SIZE( sp ) /* s1 */
\r
203 lw x10, 7 * WORD_SIZE( sp ) /* a0 */
\r
204 lw x11, 8 * WORD_SIZE( sp ) /* a1 */
\r
205 lw x12, 9 * WORD_SIZE( sp ) /* a2 */
\r
206 lw x13, 10 * WORD_SIZE( sp ) /* a3 */
\r
207 lw x14, 11 * WORD_SIZE( sp ) /* a4 */
\r
208 lw x15, 12 * WORD_SIZE( sp ) /* a5 */
\r
209 lw x16, 13 * WORD_SIZE( sp ) /* a6 */
\r
210 lw x17, 14 * WORD_SIZE( sp ) /* a7 */
\r
211 lw x18, 15 * WORD_SIZE( sp ) /* s2 */
\r
212 lw x19, 16 * WORD_SIZE( sp ) /* s3 */
\r
213 lw x20, 17 * WORD_SIZE( sp ) /* s4 */
\r
214 lw x21, 18 * WORD_SIZE( sp ) /* s5 */
\r
215 lw x22, 19 * WORD_SIZE( sp ) /* s6 */
\r
216 lw x23, 20 * WORD_SIZE( sp ) /* s7 */
\r
217 lw x24, 21 * WORD_SIZE( sp ) /* s8 */
\r
218 lw x25, 22 * WORD_SIZE( sp ) /* s9 */
\r
219 lw x26, 23 * WORD_SIZE( sp ) /* s10 */
\r
220 lw x27, 24 * WORD_SIZE( sp ) /* s11 */
\r
221 lw x28, 25 * WORD_SIZE( sp ) /* t3 */
\r
222 lw x29, 26 * WORD_SIZE( sp ) /* t4 */
\r
223 lw x30, 27 * WORD_SIZE( sp ) /* t5 */
\r
224 lw x31, 28 * WORD_SIZE( sp ) /* t6 */
\r
225 addi sp, sp, CONTEXT_SIZE
\r
226 csrs mstatus, 8 /* Enable machine interrupts. */
\r
229 /*-----------------------------------------------------------*/
\r