]> begriffs open source - freertos/blob - portable/IAR/RISC-V/portASM.s
[AUTO][RELEASE]: Bump file header version to "10.4.5"
[freertos] / portable / IAR / RISC-V / portASM.s
1 /*\r
2  * FreeRTOS Kernel V10.4.5\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT\r
6  *\r
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\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
16  *\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
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 /*\r
30  * The FreeRTOS kernel's RISC-V port is split between the the code that is\r
31  * common across all currently supported RISC-V chips (implementations of the\r
32  * RISC-V ISA), and code which tailors the port to a specific RISC-V chip:\r
33  *\r
34  * + The code that is common to all RISC-V chips is implemented in\r
35  *   FreeRTOS\Source\portable\GCC\RISC-V-RV32\portASM.S.  There is only one\r
36  *   portASM.S file because the same file is used no matter which RISC-V chip is\r
37  *   in use.\r
38  *\r
39  * + The code that tailors the kernel's RISC-V port to a specific RISC-V\r
40  *   chip is implemented in freertos_risc_v_chip_specific_extensions.h.  There\r
41  *   is one freertos_risc_v_chip_specific_extensions.h that can be used with any\r
42  *   RISC-V chip that both includes a standard CLINT and does not add to the\r
43  *   base set of RISC-V registers.  There are additional\r
44  *   freertos_risc_v_chip_specific_extensions.h files for RISC-V implementations\r
45  *   that do not include a standard CLINT or do add to the base set of RISC-V\r
46  *   registers.\r
47  *\r
48  * CARE MUST BE TAKEN TO INCLDUE THE CORRECT\r
49  * freertos_risc_v_chip_specific_extensions.h HEADER FILE FOR THE CHIP\r
50  * IN USE.  To include the correct freertos_risc_v_chip_specific_extensions.h\r
51  * header file ensure the path to the correct header file is in the assembler's\r
52  * include path.\r
53  *\r
54  * This freertos_risc_v_chip_specific_extensions.h is for use on RISC-V chips\r
55  * that include a standard CLINT and do not add to the base set of RISC-V\r
56  * registers.\r
57  *\r
58  */\r
59 #if __riscv_xlen == 64\r
60         #define portWORD_SIZE 8\r
61         #define store_x sd\r
62         #define load_x ld\r
63 #elif __riscv_xlen == 32\r
64         #define store_x sw\r
65         #define load_x lw\r
66         #define portWORD_SIZE 4\r
67 #else\r
68         #error Assembler did not define __riscv_xlen\r
69 #endif\r
70 \r
71 #include "freertos_risc_v_chip_specific_extensions.h"\r
72 \r
73 /* Check the freertos_risc_v_chip_specific_extensions.h and/or command line\r
74 definitions. */\r
75 #if defined( portasmHAS_CLINT ) && defined( portasmHAS_MTIME )\r
76         #error The portasmHAS_CLINT constant has been deprecated.  Please replace it with portasmHAS_MTIME.  portasmHAS_CLINT and portasmHAS_MTIME cannot both be defined at once.  See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html\r
77 #endif\r
78 \r
79 #ifdef portasmHAS_CLINT\r
80         #warning The portasmHAS_CLINT constant has been deprecated.  Please replace it with portasmHAS_MTIME and portasmHAS_SIFIVE_CLINT.  For now portasmHAS_MTIME and portasmHAS_SIFIVE_CLINT are derived from portasmHAS_CLINT.  See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html\r
81         #define portasmHAS_MTIME portasmHAS_CLINT\r
82         #define portasmHAS_SIFIVE_CLINT portasmHAS_CLINT\r
83 #endif\r
84 \r
85 #ifndef portasmHAS_MTIME\r
86         #error freertos_risc_v_chip_specific_extensions.h must define portasmHAS_MTIME to either 1 (MTIME clock present) or 0 (MTIME clock not present).  See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html\r
87 #endif\r
88 \r
89 #ifndef portasmHANDLE_INTERRUPT\r
90         #error portasmHANDLE_INTERRUPT must be defined to the function to be called to handle external/peripheral interrupts.  portasmHANDLE_INTERRUPT can be defined on the assembler command line or in the appropriate freertos_risc_v_chip_specific_extensions.h header file.  https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html\r
91 #endif\r
92 \r
93 \r
94 #ifndef portasmHAS_SIFIVE_CLINT\r
95         #define portasmHAS_SIFIVE_CLINT 0\r
96 #endif\r
97 \r
98 /* CSR definitions. */\r
99 #define CSR_MSTATUS             0x300\r
100 #define CSR_MTVEC                       0x305\r
101 #define CSR_MEPC            0x341\r
102 #define CSR_MCAUSE          0x342\r
103 \r
104 \r
105 /* Only the standard core registers are stored by default.  Any additional\r
106 registers must be saved by the portasmSAVE_ADDITIONAL_REGISTERS and\r
107 portasmRESTORE_ADDITIONAL_REGISTERS macros - which can be defined in a chip\r
108 specific version of freertos_risc_v_chip_specific_extensions.h.  See the notes\r
109 at the top of this file. */\r
110 #define portCONTEXT_SIZE ( 30 * portWORD_SIZE )\r
111 \r
112         PUBLIC xPortStartFirstTask\r
113         PUBLIC freertos_risc_v_trap_handler\r
114         PUBLIC pxPortInitialiseStack\r
115         EXTERN pxCurrentTCB\r
116         EXTERN ulPortTrapHandler\r
117         EXTERN vTaskSwitchContext\r
118         EXTERN xTaskIncrementTick\r
119         EXTERN Timer_IRQHandler\r
120         EXTERN pullMachineTimerCompareRegister\r
121         EXTERN pullNextTime\r
122         EXTERN uxTimerIncrementsForOneTick /* size_t type so 32-bit on 32-bit core and 64-bits on 64-bit core. */\r
123         EXTERN xISRStackTop\r
124         EXTERN portasmHANDLE_INTERRUPT\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128         SECTION `.text`:CODE:NOROOT(2)\r
129         CODE\r
130 \r
131 freertos_risc_v_trap_handler:\r
132         addi sp, sp, -portCONTEXT_SIZE\r
133         store_x x1, 1 * portWORD_SIZE( sp )\r
134         store_x x5, 2 * portWORD_SIZE( sp )\r
135         store_x x6, 3 * portWORD_SIZE( sp )\r
136         store_x x7, 4 * portWORD_SIZE( sp )\r
137         store_x x8, 5 * portWORD_SIZE( sp )\r
138         store_x x9, 6 * portWORD_SIZE( sp )\r
139         store_x x10, 7 * portWORD_SIZE( sp )\r
140         store_x x11, 8 * portWORD_SIZE( sp )\r
141         store_x x12, 9 * portWORD_SIZE( sp )\r
142         store_x x13, 10 * portWORD_SIZE( sp )\r
143         store_x x14, 11 * portWORD_SIZE( sp )\r
144         store_x x15, 12 * portWORD_SIZE( sp )\r
145         store_x x16, 13 * portWORD_SIZE( sp )\r
146         store_x x17, 14 * portWORD_SIZE( sp )\r
147         store_x x18, 15 * portWORD_SIZE( sp )\r
148         store_x x19, 16 * portWORD_SIZE( sp )\r
149         store_x x20, 17 * portWORD_SIZE( sp )\r
150         store_x x21, 18 * portWORD_SIZE( sp )\r
151         store_x x22, 19 * portWORD_SIZE( sp )\r
152         store_x x23, 20 * portWORD_SIZE( sp )\r
153         store_x x24, 21 * portWORD_SIZE( sp )\r
154         store_x x25, 22 * portWORD_SIZE( sp )\r
155         store_x x26, 23 * portWORD_SIZE( sp )\r
156         store_x x27, 24 * portWORD_SIZE( sp )\r
157         store_x x28, 25 * portWORD_SIZE( sp )\r
158         store_x x29, 26 * portWORD_SIZE( sp )\r
159         store_x x30, 27 * portWORD_SIZE( sp )\r
160         store_x x31, 28 * portWORD_SIZE( sp )\r
161 \r
162         csrr t0, CSR_MSTATUS                                    /* Required for MPIE bit. */\r
163         store_x t0, 29 * portWORD_SIZE( sp )\r
164 \r
165         portasmSAVE_ADDITIONAL_REGISTERS        /* Defined in freertos_risc_v_chip_specific_extensions.h to save any registers unique to the RISC-V implementation. */\r
166 \r
167         load_x  t0, pxCurrentTCB                        /* Load pxCurrentTCB. */\r
168         store_x  sp, 0( t0 )                            /* Write sp to first TCB member. */\r
169 \r
170         csrr a0, CSR_MCAUSE\r
171         csrr a1, CSR_MEPC\r
172 \r
173 test_if_asynchronous:\r
174         srli a2, a0, __riscv_xlen - 1           /* MSB of mcause is 1 if handing an asynchronous interrupt - shift to LSB to clear other bits. */\r
175         beq a2, x0, handle_synchronous          /* Branch past interrupt handing if not asynchronous. */\r
176         store_x a1, 0( sp )                                     /* Asynch so save unmodified exception return address. */\r
177 \r
178 handle_asynchronous:\r
179 \r
180 #if( portasmHAS_MTIME != 0 )\r
181 \r
182         test_if_mtimer:                                         /* If there is a CLINT then the mtimer is used to generate the tick interrupt. */\r
183 \r
184                 addi t0, x0, 1\r
185 \r
186                 slli t0, t0, __riscv_xlen - 1   /* LSB is already set, shift into MSB.  Shift 31 on 32-bit or 63 on 64-bit cores. */\r
187                 addi t1, t0, 7                                  /* 0x8000[]0007 == machine timer interrupt. */\r
188                 bne a0, t1, test_if_external_interrupt\r
189 \r
190                 load_x t0, pullMachineTimerCompareRegister  /* Load address of compare register into t0. */\r
191                 load_x t1, pullNextTime                 /* Load the address of ullNextTime into t1. */\r
192 \r
193                 #if( __riscv_xlen == 32 )\r
194 \r
195                         /* Update the 64-bit mtimer compare match value in two 32-bit writes. */\r
196                         li t4, -1\r
197                         lw t2, 0(t1)                            /* Load the low word of ullNextTime into t2. */\r
198                         lw t3, 4(t1)                            /* Load the high word of ullNextTime into t3. */\r
199                         sw t4, 0(t0)                            /* Low word no smaller than old value to start with - will be overwritten below. */\r
200                         sw t3, 4(t0)                            /* Store high word of ullNextTime into compare register.  No smaller than new value. */\r
201                         sw t2, 0(t0)                            /* Store low word of ullNextTime into compare register. */\r
202                         lw t0, uxTimerIncrementsForOneTick      /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */\r
203                         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
204                         sltu t5, t4, t2                         /* See if the sum of low words overflowed (what about the zero case?). */\r
205                         add t6, t3, t5                          /* Add overflow to high word of ullNextTime. */\r
206                         sw t4, 0(t1)                            /* Store new low word of ullNextTime. */\r
207                         sw t6, 4(t1)                            /* Store new high word of ullNextTime. */\r
208 \r
209                 #endif /* __riscv_xlen == 32 */\r
210 \r
211                 #if( __riscv_xlen == 64 )\r
212 \r
213                         /* Update the 64-bit mtimer compare match value. */\r
214                         ld t2, 0(t1)                            /* Load ullNextTime into t2. */\r
215                         sd t2, 0(t0)                            /* Store ullNextTime into compare register. */\r
216                         ld t0, uxTimerIncrementsForOneTick  /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */\r
217                         add t4, t0, t2                          /* Add ullNextTime to the timer increments for one tick. */\r
218                         sd t4, 0(t1)                            /* Store ullNextTime. */\r
219 \r
220                 #endif /* __riscv_xlen == 64 */\r
221 \r
222                 load_x sp, xISRStackTop                 /* Switch to ISR stack before function call. */\r
223                 jal xTaskIncrementTick\r
224                 beqz a0, processed_source               /* Don't switch context if incrementing tick didn't unblock a task. */\r
225                 jal vTaskSwitchContext\r
226                 j processed_source\r
227 \r
228         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
229                 addi t1, t1, 4                                  /* 0x80000007 + 4 = 0x8000000b == Machine external interrupt. */\r
230                 bne a0, t1, as_yet_unhandled    /* Something as yet unhandled. */\r
231 \r
232 #endif /* portasmHAS_MTIME */\r
233 \r
234         load_x sp, xISRStackTop                         /* Switch to ISR stack before function call. */\r
235         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
236         j processed_source\r
237 \r
238 handle_synchronous:\r
239         addi a1, a1, 4                                          /* Synchronous so updated exception return address to the instruction after the instruction that generated the exeption. */\r
240         store_x a1, 0( sp )                                     /* Save updated exception return address. */\r
241 \r
242 test_if_environment_call:\r
243         li t0, 11                                                       /* 11 == environment call. */\r
244         bne a0, t0, is_exception                        /* Not an M environment call, so some other exception. */\r
245         load_x sp, xISRStackTop                         /* Switch to ISR stack before function call. */\r
246         jal vTaskSwitchContext\r
247         j processed_source\r
248 \r
249 is_exception:\r
250         csrr t0, CSR_MCAUSE                                     /* For viewing in the debugger only. */\r
251         csrr t1, CSR_MEPC                                       /* For viewing in the debugger only */\r
252         csrr t2, CSR_MSTATUS\r
253         j is_exception                                          /* No other exceptions handled yet. */\r
254 \r
255 as_yet_unhandled:\r
256         csrr t0, mcause                                         /* For viewing in the debugger only. */\r
257         j as_yet_unhandled\r
258 \r
259 processed_source:\r
260         load_x  t1, pxCurrentTCB                        /* Load pxCurrentTCB. */\r
261         load_x  sp, 0( t1 )                                     /* Read sp from first TCB member. */\r
262 \r
263         /* Load mret with the address of the next instruction in the task to run next. */\r
264         load_x t0, 0( sp )\r
265         csrw CSR_MEPC, t0\r
266 \r
267         portasmRESTORE_ADDITIONAL_REGISTERS     /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */\r
268 \r
269         /* Load mstatus with the interrupt enable bits used by the task. */\r
270         load_x  t0, 29 * portWORD_SIZE( sp )\r
271         csrw CSR_MSTATUS, t0                                            /* Required for MPIE bit. */\r
272 \r
273         load_x  x1, 1 * portWORD_SIZE( sp )\r
274         load_x  x5, 2 * portWORD_SIZE( sp )             /* t0 */\r
275         load_x  x6, 3 * portWORD_SIZE( sp )             /* t1 */\r
276         load_x  x7, 4 * portWORD_SIZE( sp )             /* t2 */\r
277         load_x  x8, 5 * portWORD_SIZE( sp )             /* s0/fp */\r
278         load_x  x9, 6 * portWORD_SIZE( sp )             /* s1 */\r
279         load_x  x10, 7 * portWORD_SIZE( sp )    /* a0 */\r
280         load_x  x11, 8 * portWORD_SIZE( sp )    /* a1 */\r
281         load_x  x12, 9 * portWORD_SIZE( sp )    /* a2 */\r
282         load_x  x13, 10 * portWORD_SIZE( sp )   /* a3 */\r
283         load_x  x14, 11 * portWORD_SIZE( sp )   /* a4 */\r
284         load_x  x15, 12 * portWORD_SIZE( sp )   /* a5 */\r
285         load_x  x16, 13 * portWORD_SIZE( sp )   /* a6 */\r
286         load_x  x17, 14 * portWORD_SIZE( sp )   /* a7 */\r
287         load_x  x18, 15 * portWORD_SIZE( sp )   /* s2 */\r
288         load_x  x19, 16 * portWORD_SIZE( sp )   /* s3 */\r
289         load_x  x20, 17 * portWORD_SIZE( sp )   /* s4 */\r
290         load_x  x21, 18 * portWORD_SIZE( sp )   /* s5 */\r
291         load_x  x22, 19 * portWORD_SIZE( sp )   /* s6 */\r
292         load_x  x23, 20 * portWORD_SIZE( sp )   /* s7 */\r
293         load_x  x24, 21 * portWORD_SIZE( sp )   /* s8 */\r
294         load_x  x25, 22 * portWORD_SIZE( sp )   /* s9 */\r
295         load_x  x26, 23 * portWORD_SIZE( sp )   /* s10 */\r
296         load_x  x27, 24 * portWORD_SIZE( sp )   /* s11 */\r
297         load_x  x28, 25 * portWORD_SIZE( sp )   /* t3 */\r
298         load_x  x29, 26 * portWORD_SIZE( sp )   /* t4 */\r
299         load_x  x30, 27 * portWORD_SIZE( sp )   /* t5 */\r
300         load_x  x31, 28 * portWORD_SIZE( sp )   /* t6 */\r
301         addi sp, sp, portCONTEXT_SIZE\r
302 \r
303         mret\r
304 \r
305 /*-----------------------------------------------------------*/\r
306 \r
307 xPortStartFirstTask:\r
308 \r
309 #if( portasmHAS_SIFIVE_CLINT != 0 )\r
310         /* If there is a clint then interrupts can branch directly to the FreeRTOS\r
311         trap handler.  Otherwise the interrupt controller will need to be configured\r
312         outside of this file. */\r
313         la t0, freertos_risc_v_trap_handler\r
314         csrw CSR_MTVEC, t0\r
315 #endif /* portasmHAS_CLILNT */\r
316 \r
317         load_x  sp, pxCurrentTCB                        /* Load pxCurrentTCB. */\r
318         load_x  sp, 0( sp )                                     /* Read sp from first TCB member. */\r
319 \r
320         load_x  x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */\r
321 \r
322         portasmRESTORE_ADDITIONAL_REGISTERS     /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */\r
323 \r
324         load_x  x6, 3 * portWORD_SIZE( sp )             /* t1 */\r
325         load_x  x7, 4 * portWORD_SIZE( sp )             /* t2 */\r
326         load_x  x8, 5 * portWORD_SIZE( sp )             /* s0/fp */\r
327         load_x  x9, 6 * portWORD_SIZE( sp )             /* s1 */\r
328         load_x  x10, 7 * portWORD_SIZE( sp )    /* a0 */\r
329         load_x  x11, 8 * portWORD_SIZE( sp )    /* a1 */\r
330         load_x  x12, 9 * portWORD_SIZE( sp )    /* a2 */\r
331         load_x  x13, 10 * portWORD_SIZE( sp )   /* a3 */\r
332         load_x  x14, 11 * portWORD_SIZE( sp )   /* a4 */\r
333         load_x  x15, 12 * portWORD_SIZE( sp )   /* a5 */\r
334         load_x  x16, 13 * portWORD_SIZE( sp )   /* a6 */\r
335         load_x  x17, 14 * portWORD_SIZE( sp )   /* a7 */\r
336         load_x  x18, 15 * portWORD_SIZE( sp )   /* s2 */\r
337         load_x  x19, 16 * portWORD_SIZE( sp )   /* s3 */\r
338         load_x  x20, 17 * portWORD_SIZE( sp )   /* s4 */\r
339         load_x  x21, 18 * portWORD_SIZE( sp )   /* s5 */\r
340         load_x  x22, 19 * portWORD_SIZE( sp )   /* s6 */\r
341         load_x  x23, 20 * portWORD_SIZE( sp )   /* s7 */\r
342         load_x  x24, 21 * portWORD_SIZE( sp )   /* s8 */\r
343         load_x  x25, 22 * portWORD_SIZE( sp )   /* s9 */\r
344         load_x  x26, 23 * portWORD_SIZE( sp )   /* s10 */\r
345         load_x  x27, 24 * portWORD_SIZE( sp )   /* s11 */\r
346         load_x  x28, 25 * portWORD_SIZE( sp )   /* t3 */\r
347         load_x  x29, 26 * portWORD_SIZE( sp )   /* t4 */\r
348         load_x  x30, 27 * portWORD_SIZE( sp )   /* t5 */\r
349         load_x  x31, 28 * portWORD_SIZE( sp )   /* t6 */\r
350 \r
351         load_x  x5, 29 * portWORD_SIZE( sp )    /* Initial mstatus into x5 (t0) */\r
352         addi x5, x5, 0x08                                               /* Set MIE bit so the first task starts with interrupts enabled - required as returns with ret not eret. */\r
353         csrrw  x0, CSR_MSTATUS, x5                                      /* Interrupts enabled from here! */\r
354         load_x  x5, 2 * portWORD_SIZE( sp )             /* Initial x5 (t0) value. */\r
355         addi    sp, sp, portCONTEXT_SIZE\r
356         ret\r
357 \r
358 /*-----------------------------------------------------------*/\r
359 \r
360 /*\r
361  * Unlike other ports pxPortInitialiseStack() is written in assembly code as it\r
362  * needs access to the portasmADDITIONAL_CONTEXT_SIZE constant.  The prototype\r
363  * for the function is as per the other ports:\r
364  * StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters );\r
365  *\r
366  * As per the standard RISC-V ABI pxTopcOfStack is passed in in a0, pxCode in\r
367  * a1, and pvParameters in a2.  The new top of stack is passed out in a0.\r
368  *\r
369  * RISC-V maps registers to ABI names as follows (X1 to X31 integer registers\r
370  * for the 'I' profile, X1 to X15 for the 'E' profile, currently I assumed).\r
371  *\r
372  * Register             ABI Name        Description                                             Saver\r
373  * x0                   zero            Hard-wired zero                                 -\r
374  * x1                   ra                      Return address                                  Caller\r
375  * x2                   sp                      Stack pointer                                   Callee\r
376  * x3                   gp                      Global pointer                                  -\r
377  * x4                   tp                      Thread pointer                                  -\r
378  * x5-7                 t0-2            Temporaries                                             Caller\r
379  * x8                   s0/fp           Saved register/Frame pointer    Callee\r
380  * x9                   s1                      Saved register                                  Callee\r
381  * x10-11               a0-1            Function Arguments/return values Caller\r
382  * x12-17               a2-7            Function arguments                              Caller\r
383  * x18-27               s2-11           Saved registers                                 Callee\r
384  * x28-31               t3-6            Temporaries                                             Caller\r
385  *\r
386  * The RISC-V context is saved t FreeRTOS tasks in the following stack frame,\r
387  * where the global and thread pointers are currently assumed to be constant so\r
388  * are not saved:\r
389  *\r
390  * mstatus\r
391  * x31\r
392  * x30\r
393  * x29\r
394  * x28\r
395  * x27\r
396  * x26\r
397  * x25\r
398  * x24\r
399  * x23\r
400  * x22\r
401  * x21\r
402  * x20\r
403  * x19\r
404  * x18\r
405  * x17\r
406  * x16\r
407  * x15\r
408  * x14\r
409  * x13\r
410  * x12\r
411  * x11\r
412  * pvParameters\r
413  * x9\r
414  * x8\r
415  * x7\r
416  * x6\r
417  * x5\r
418  * portTASK_RETURN_ADDRESS\r
419  * [chip specific registers go here]\r
420  * pxCode\r
421  */\r
422 pxPortInitialiseStack:\r
423 \r
424         csrr t0, CSR_MSTATUS                                    /* Obtain current mstatus value. */\r
425         andi t0, t0, ~0x8                                       /* Ensure interrupts are disabled when the stack is restored within an ISR.  Required when a task is created after the schedulre has been started, otherwise interrupts would be disabled anyway. */\r
426         addi t1, x0, 0x188                                      /* Generate the value 0x1880, which are the MPIE and MPP bits to set in mstatus. */\r
427         slli t1, t1, 4\r
428         or t0, t0, t1                                           /* Set MPIE and MPP bits in mstatus value. */\r
429 \r
430         addi a0, a0, -portWORD_SIZE\r
431         store_x t0, 0(a0)                                       /* mstatus onto the stack. */\r
432         addi a0, a0, -(22 * portWORD_SIZE)      /* Space for registers x11-x31. */\r
433         store_x a2, 0(a0)                                       /* Task parameters (pvParameters parameter) goes into register X10/a0 on the stack. */\r
434         addi a0, a0, -(6 * portWORD_SIZE)       /* Space for registers x5-x9. */\r
435         store_x x0, 0(a0)                                       /* Return address onto the stack, could be portTASK_RETURN_ADDRESS */\r
436         addi t0, x0, portasmADDITIONAL_CONTEXT_SIZE /* The number of chip specific additional registers. */\r
437 chip_specific_stack_frame:                              /* First add any chip specific registers to the stack frame being created. */\r
438         beq t0, x0, no_more_regs                        /* No more chip specific registers to save. */\r
439         addi a0, a0, -portWORD_SIZE                     /* Make space for chip specific register. */\r
440         store_x x0, 0(a0)                                       /* Give the chip specific register an initial value of zero. */\r
441         addi t0, t0, -1                                         /* Decrement the count of chip specific registers remaining. */\r
442         j chip_specific_stack_frame                     /* Until no more chip specific registers. */\r
443 no_more_regs:\r
444         addi a0, a0, -portWORD_SIZE\r
445         store_x a1, 0(a0)                                       /* mret value (pxCode parameter) onto the stack. */\r
446         ret\r
447 \r
448 /*-----------------------------------------------------------*/\r