]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/RISC-V-RV32/portASM.S
Continue work on Risc V port.
[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 ( 28 * WORD_SIZE )\r
40 \r
41 .global xPortStartFirstTask\r
42 .global vPortTrapHandler\r
43 .extern pxCurrentTCB\r
44 .extern handle_trap\r
45 \r
46 /*-----------------------------------------------------------*/\r
47 \r
48 .align 8\r
49 xPortStartFirstTask:\r
50 \r
51     lw  sp, pxCurrentTCB            /* Load pxCurrentTCB. */\r
52     lw  sp, 0( sp )                 /* Read sp from first TCB member. */\r
53 \r
54     lw  x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */\r
55     lw  x5, 2 * WORD_SIZE( sp )     /* t0 */\r
56     lw  x6, 3 * WORD_SIZE( sp )     /* t1 */\r
57     lw  x7, 4 * WORD_SIZE( sp )     /* t2 */\r
58     lw  x8, 5 * WORD_SIZE( sp )     /* s0/fp */\r
59     lw  x9, 6 * WORD_SIZE( sp )     /* s1 */\r
60     lw  x10, 7 * WORD_SIZE( sp )    /* a0 */\r
61     lw  x11, 8 * WORD_SIZE( sp )    /* a1 */\r
62     lw  x12, 9 * WORD_SIZE( sp )    /* a2 */\r
63     lw  x13, 10 * WORD_SIZE( sp )   /* a3 */\r
64     lw  x14, 11 * WORD_SIZE( sp )   /* a4 */\r
65     lw  x15, 12 * WORD_SIZE( sp )   /* a5 */\r
66     lw  x16, 13 * WORD_SIZE( sp )   /* a6 */\r
67     lw  x17, 14 * WORD_SIZE( sp )   /* a7 */\r
68     lw  x18, 15 * WORD_SIZE( sp )   /* s2 */\r
69     lw  x19, 16 * WORD_SIZE( sp )   /* s3 */\r
70     lw  x20, 17 * WORD_SIZE( sp )   /* s4 */\r
71     lw  x21, 18 * WORD_SIZE( sp )   /* s5 */\r
72     lw  x22, 19 * WORD_SIZE( sp )   /* s6 */\r
73     lw  x23, 20 * WORD_SIZE( sp )   /* s7 */\r
74     lw  x24, 21 * WORD_SIZE( sp )   /* s8 */\r
75     lw  x25, 22 * WORD_SIZE( sp )   /* s9 */\r
76     lw  x26, 23 * WORD_SIZE( sp )   /* s10 */\r
77     lw  x27, 24 * WORD_SIZE( sp )   /* s11 */\r
78     lw  x28, 25 * WORD_SIZE( sp )   /* t3 */\r
79     lw  x29, 26 * WORD_SIZE( sp )   /* t4 */\r
80     lw  x30, 27 * WORD_SIZE( sp )   /* t5 */\r
81     lw  x31, 28 * WORD_SIZE( sp )   /* t6 */\r
82     addi    sp, sp, CONTEXT_SIZE\r
83     csrs    mstatus, 8              /* Enable machine interrupts. */\r
84     csrs    mie, 8                  /* Enable soft interrupt. */\r
85      ret\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 .align 8\r
90 vPortTrapHandler:\r
91         addi sp, sp, -CONTEXT_SIZE\r
92         sw x1, 1( sp )\r
93         sw x5, 2 * WORD_SIZE( sp )\r
94         sw x6, 3 * WORD_SIZE( sp )\r
95         sw x7, 4 * WORD_SIZE( sp )\r
96         sw x8, 5 * WORD_SIZE( sp )\r
97         sw x9, 6 * WORD_SIZE( sp )\r
98         sw x10, 7 * WORD_SIZE( sp )\r
99         sw x11, 8 * WORD_SIZE( sp )\r
100         sw x12, 9 * WORD_SIZE( sp )\r
101         sw x13, 10 * WORD_SIZE( sp )\r
102         sw x14, 11 * WORD_SIZE( sp )\r
103         sw x15, 12 * WORD_SIZE( sp )\r
104         sw x16, 13 * WORD_SIZE( sp )\r
105         sw x17, 14 * WORD_SIZE( sp )\r
106         sw x18, 15 * WORD_SIZE( sp )\r
107         sw x19, 16 * WORD_SIZE( sp )\r
108         sw x20, 17 * WORD_SIZE( sp )\r
109         sw x21, 18 * WORD_SIZE( sp )\r
110         sw x22, 19 * WORD_SIZE( sp )\r
111         sw x23, 20 * WORD_SIZE( sp )\r
112         sw x24, 21 * WORD_SIZE( sp )\r
113         sw x25, 22 * WORD_SIZE( sp )\r
114         sw x26, 23 * WORD_SIZE( sp )\r
115         sw x27, 24 * WORD_SIZE( sp )\r
116         sw x28, 25 * WORD_SIZE( sp )\r
117         sw x29, 26 * WORD_SIZE( sp )\r
118         sw x30, 27 * WORD_SIZE( sp )\r
119         sw x31, 28 * WORD_SIZE( sp )\r
120 \r
121     /* Save exception return address. */\r
122     csrr t0, mepc\r
123     sw t0, 0( sp )\r
124 \r
125     lw  t0, pxCurrentTCB            /* Load pxCurrentTCB. */\r
126     sw  sp, 0( t0 )                 /* Write sp from first TCB member. */\r
127 \r
128         csrr a0, mcause\r
129         csrr a1, mepc\r
130         mv a2, sp\r
131         jal handle_trap\r
132         csrw mepc, a0\r
133 \r
134         # Remain in M-mode after mret\r
135         li t0, 0x00001800 /* MSTATUS MPP */\r
136         csrs mstatus, t0\r
137 \r
138     lw  sp, pxCurrentTCB            /* Load pxCurrentTCB. */\r
139     lw  sp, 0( sp )                 /* Read sp from first TCB member. */\r
140 \r
141     /* Load mret with the address of the first task. */\r
142     lw t0, 0( sp )\r
143     csrw mepc, t0\r
144 \r
145     lw  x1, 1( sp )\r
146     lw  x5, 2 * WORD_SIZE( sp )     /* t0 */\r
147     lw  x6, 3 * WORD_SIZE( sp )     /* t1 */\r
148     lw  x7, 4 * WORD_SIZE( sp )     /* t2 */\r
149     lw  x8, 5 * WORD_SIZE( sp )     /* s0/fp */\r
150     lw  x9, 6 * WORD_SIZE( sp )     /* s1 */\r
151     lw  x10, 7 * WORD_SIZE( sp )    /* a0 */\r
152     lw  x11, 8 * WORD_SIZE( sp )    /* a1 */\r
153     lw  x12, 9 * WORD_SIZE( sp )    /* a2 */\r
154     lw  x13, 10 * WORD_SIZE( sp )   /* a3 */\r
155     lw  x14, 11 * WORD_SIZE( sp )   /* a4 */\r
156     lw  x15, 12 * WORD_SIZE( sp )   /* a5 */\r
157     lw  x16, 13 * WORD_SIZE( sp )   /* a6 */\r
158     lw  x17, 14 * WORD_SIZE( sp )   /* a7 */\r
159     lw  x18, 15 * WORD_SIZE( sp )   /* s2 */\r
160     lw  x19, 16 * WORD_SIZE( sp )   /* s3 */\r
161     lw  x20, 17 * WORD_SIZE( sp )   /* s4 */\r
162     lw  x21, 18 * WORD_SIZE( sp )   /* s5 */\r
163     lw  x22, 19 * WORD_SIZE( sp )   /* s6 */\r
164     lw  x23, 20 * WORD_SIZE( sp )   /* s7 */\r
165     lw  x24, 21 * WORD_SIZE( sp )   /* s8 */\r
166     lw  x25, 22 * WORD_SIZE( sp )   /* s9 */\r
167     lw  x26, 23 * WORD_SIZE( sp )   /* s10 */\r
168     lw  x27, 24 * WORD_SIZE( sp )   /* s11 */\r
169     lw  x28, 25 * WORD_SIZE( sp )   /* t3 */\r
170     lw  x29, 26 * WORD_SIZE( sp )   /* t4 */\r
171     lw  x30, 27 * WORD_SIZE( sp )   /* t5 */\r
172     lw  x31, 28 * WORD_SIZE( sp )   /* t6 */\r
173     addi    sp, sp, CONTEXT_SIZE\r
174 \r
175     mret\r
176 \r
177 \r