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