]> begriffs open source - freertos/blob - FreeRTOS/Source/portable/GCC/RISC-V-RV32/portASM.S
First task running in RISC-V-Qemu-sifive_e-FreedomStudio demo.
[freertos] / FreeRTOS / Source / portable / GCC / RISC-V-RV32 / portASM.S
1 /*\r
2  * FreeRTOS Kernel V10.1.1\r
3  * Copyright (C) 2018 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 t\r
10 \r
11  o 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  * http://www.FreeRTOS.org\r
25  * http://aws.amazon.com/freertos\r
26  *\r
27  * 1 tab == 4 spaces!\r
28  */\r
29 \r
30 #if __riscv_xlen == 64\r
31     #error Not implemented yet - change lw to ld, and sw to sd.\r
32     #define WORD_SIZE 8\r
33 #elif __riscv_xlen == 32\r
34     #define WORD_SIZE 4\r
35 #else\r
36     #error Assembler has not defined __riscv_xlen\r
37 #endif\r
38 \r
39 #define CONTEXT_SIZE ( 30 * WORD_SIZE )\r
40 \r
41 .global xPortStartFirstTask\r
42 .global vPortTrapHandler\r
43 .extern pxCurrentTCB\r
44 .extern ulPortTrapHandler\r
45 \r
46 /*-----------------------------------------------------------*/\r
47 \r
48 .align 8\r
49 xPortStartFirstTask:\r
50 \r
51         la t0, vPortTrapHandler\r
52         csrw mtvec, t0\r
53 \r
54         lw  sp, pxCurrentTCB            /* Load pxCurrentTCB. */\r
55         lw  sp, 0( sp )                 /* Read sp from first TCB member. */\r
56 \r
57         lw  x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */\r
58         lw  x5, 2 * WORD_SIZE( sp )     /* t0 */\r
59         lw  x6, 3 * WORD_SIZE( sp )     /* t1 */\r
60         lw  x7, 4 * WORD_SIZE( sp )     /* t2 */\r
61         lw  x8, 5 * WORD_SIZE( sp )     /* s0/fp */\r
62         lw  x9, 6 * WORD_SIZE( sp )     /* s1 */\r
63         lw  x10, 7 * WORD_SIZE( sp )    /* a0 */\r
64         lw  x11, 8 * WORD_SIZE( sp )    /* a1 */\r
65         lw  x12, 9 * WORD_SIZE( sp )    /* a2 */\r
66         lw  x13, 10 * WORD_SIZE( sp )   /* a3 */\r
67         lw  x14, 11 * WORD_SIZE( sp )   /* a4 */\r
68         lw  x15, 12 * WORD_SIZE( sp )   /* a5 */\r
69         lw  x16, 13 * WORD_SIZE( sp )   /* a6 */\r
70         lw  x17, 14 * WORD_SIZE( sp )   /* a7 */\r
71         lw  x18, 15 * WORD_SIZE( sp )   /* s2 */\r
72         lw  x19, 16 * WORD_SIZE( sp )   /* s3 */\r
73         lw  x20, 17 * WORD_SIZE( sp )   /* s4 */\r
74         lw  x21, 18 * WORD_SIZE( sp )   /* s5 */\r
75         lw  x22, 19 * WORD_SIZE( sp )   /* s6 */\r
76         lw  x23, 20 * WORD_SIZE( sp )   /* s7 */\r
77         lw  x24, 21 * WORD_SIZE( sp )   /* s8 */\r
78         lw  x25, 22 * WORD_SIZE( sp )   /* s9 */\r
79         lw  x26, 23 * WORD_SIZE( sp )   /* s10 */\r
80         lw  x27, 24 * WORD_SIZE( sp )   /* s11 */\r
81         lw  x28, 25 * WORD_SIZE( sp )   /* t3 */\r
82         lw  x29, 26 * WORD_SIZE( sp )   /* t4 */\r
83         lw  x30, 27 * WORD_SIZE( sp )   /* t5 */\r
84         lw  x31, 28 * WORD_SIZE( sp )   /* t6 */\r
85         addi    sp, sp, CONTEXT_SIZE\r
86         csrs    mstatus, 8              /* Enable machine interrupts. */\r
87         ret\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 .align 8\r
92 vPortTrapHandler:\r
93         addi sp, sp, -CONTEXT_SIZE\r
94         sw x1, 1 * WORD_SIZE( sp )\r
95         sw x5, 2 * WORD_SIZE( sp )\r
96         sw x6, 3 * WORD_SIZE( sp )\r
97         sw x7, 4 * WORD_SIZE( sp )\r
98         sw x8, 5 * WORD_SIZE( sp )\r
99         sw x9, 6 * WORD_SIZE( sp )\r
100         sw x10, 7 * WORD_SIZE( sp )\r
101         sw x11, 8 * WORD_SIZE( sp )\r
102         sw x12, 9 * WORD_SIZE( sp )\r
103         sw x13, 10 * WORD_SIZE( sp )\r
104         sw x14, 11 * WORD_SIZE( sp )\r
105         sw x15, 12 * WORD_SIZE( sp )\r
106         sw x16, 13 * WORD_SIZE( sp )\r
107         sw x17, 14 * WORD_SIZE( sp )\r
108         sw x18, 15 * WORD_SIZE( sp )\r
109         sw x19, 16 * WORD_SIZE( sp )\r
110         sw x20, 17 * WORD_SIZE( sp )\r
111         sw x21, 18 * WORD_SIZE( sp )\r
112         sw x22, 19 * WORD_SIZE( sp )\r
113         sw x23, 20 * WORD_SIZE( sp )\r
114         sw x24, 21 * WORD_SIZE( sp )\r
115         sw x25, 22 * WORD_SIZE( sp )\r
116         sw x26, 23 * WORD_SIZE( sp )\r
117         sw x27, 24 * WORD_SIZE( sp )\r
118         sw x28, 25 * WORD_SIZE( sp )\r
119         sw x29, 26 * WORD_SIZE( sp )\r
120         sw x30, 27 * WORD_SIZE( sp )\r
121         sw x31, 28 * WORD_SIZE( sp )\r
122 \r
123         csrr t0, mstatus                /* Required for MPIE bit. */\r
124         sw t0, 29 * WORD_SIZE( sp )\r
125 \r
126         lw  t0, pxCurrentTCB            /* Load pxCurrentTCB. */\r
127         sw  sp, 0( t0 )                 /* Write sp to first TCB member. */\r
128 \r
129         csrr a0, mcause\r
130         csrr a1, mepc\r
131         mv a2, sp\r
132 /*_RB_ Does stack need aligning here? */\r
133         jal ulPortTrapHandler\r
134         csrw mepc, a0\r
135         /* Save exception return address. */\r
136         sw a0, 0( sp )\r
137 \r
138 \r
139         lw  sp, pxCurrentTCB            /* Load pxCurrentTCB. */\r
140         lw  sp, 0( sp )                 /* Read sp from first TCB member. */\r
141 \r
142         /* Load mret with the address of the next task. */\r
143         lw t0, 0( sp )\r
144         csrw mepc, t0\r
145 \r
146         /* Load mstatus with the interrupt enable bits used by the task. */\r
147         lw  t0, 29 * WORD_SIZE( sp )\r
148         csrw mstatus, t0                /* Required for MPIE bit. */\r
149 \r
150         lw  x1, 1 * WORD_SIZE( sp )\r
151         lw  x5, 2 * WORD_SIZE( sp )     /* t0 */\r
152         lw  x6, 3 * WORD_SIZE( sp )     /* t1 */\r
153         lw  x7, 4 * WORD_SIZE( sp )     /* t2 */\r
154         lw  x8, 5 * WORD_SIZE( sp )     /* s0/fp */\r
155         lw  x9, 6 * WORD_SIZE( sp )     /* s1 */\r
156         lw  x10, 7 * WORD_SIZE( sp )    /* a0 */\r
157         lw  x11, 8 * WORD_SIZE( sp )    /* a1 */\r
158         lw  x12, 9 * WORD_SIZE( sp )    /* a2 */\r
159         lw  x13, 10 * WORD_SIZE( sp )   /* a3 */\r
160         lw  x14, 11 * WORD_SIZE( sp )   /* a4 */\r
161         lw  x15, 12 * WORD_SIZE( sp )   /* a5 */\r
162         lw  x16, 13 * WORD_SIZE( sp )   /* a6 */\r
163         lw  x17, 14 * WORD_SIZE( sp )   /* a7 */\r
164         lw  x18, 15 * WORD_SIZE( sp )   /* s2 */\r
165         lw  x19, 16 * WORD_SIZE( sp )   /* s3 */\r
166         lw  x20, 17 * WORD_SIZE( sp )   /* s4 */\r
167         lw  x21, 18 * WORD_SIZE( sp )   /* s5 */\r
168         lw  x22, 19 * WORD_SIZE( sp )   /* s6 */\r
169         lw  x23, 20 * WORD_SIZE( sp )   /* s7 */\r
170         lw  x24, 21 * WORD_SIZE( sp )   /* s8 */\r
171         lw  x25, 22 * WORD_SIZE( sp )   /* s9 */\r
172         lw  x26, 23 * WORD_SIZE( sp )   /* s10 */\r
173         lw  x27, 24 * WORD_SIZE( sp )   /* s11 */\r
174         lw  x28, 25 * WORD_SIZE( sp )   /* t3 */\r
175         lw  x29, 26 * WORD_SIZE( sp )   /* t4 */\r
176         lw  x30, 27 * WORD_SIZE( sp )   /* t5 */\r
177         lw  x31, 28 * WORD_SIZE( sp )   /* t6 */\r
178         addi    sp, sp, CONTEXT_SIZE\r
179 \r
180         mret\r
181 \r
182 \r