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