]> begriffs open source - freertos/blob - FreeRTOS/Source/portable/GCC/RISC-V-RV32/portASM.S
Add trap handler to RISC-V port so there is no dependency on third party code.
[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 xPortStartScheduler\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 xPortStartScheduler:\r
50         lw      sp, pxCurrentTCB                        /* Load pxCurrentTCB. */\r
51         lw      sp, 0( sp )                                     /* Read sp from first TCB member. */\r
52         lw      x1, 0( sp )\r
53         lw      x5, 1 * WORD_SIZE( sp )         /* t0 */\r
54         lw      x6, 2 * WORD_SIZE( sp )         /* t1 */\r
55         lw      x7, 3 * WORD_SIZE( sp )         /* t2 */\r
56         lw      x8, 4 * WORD_SIZE( sp )         /* s0/fp */\r
57         lw      x9, 5 * WORD_SIZE( sp )         /* s1 */\r
58         lw      x10, 6 * WORD_SIZE( sp )        /* a0 */\r
59         lw      x11, 7 * WORD_SIZE( sp )        /* a1 */\r
60         lw      x12, 8 * WORD_SIZE( sp )        /* a2 */\r
61         lw      x13, 9 * WORD_SIZE( sp )        /* a3 */\r
62         lw      x14, 10 * WORD_SIZE( sp )       /* a4 */\r
63         lw      x15, 11 * WORD_SIZE( sp )       /* a5 */\r
64         lw      x16, 12 * WORD_SIZE( sp )       /* a6 */\r
65         lw      x17, 13 * WORD_SIZE( sp )       /* a7 */\r
66         lw      x18, 14 * WORD_SIZE( sp )       /* s2 */\r
67         lw      x19, 15 * WORD_SIZE( sp )       /* s3 */\r
68         lw      x20, 16 * WORD_SIZE( sp )       /* s4 */\r
69         lw      x21, 17 * WORD_SIZE( sp )       /* s5 */\r
70         lw      x22, 18 * WORD_SIZE( sp )       /* s6 */\r
71         lw      x23, 19 * WORD_SIZE( sp )       /* s7 */\r
72         lw      x24, 20 * WORD_SIZE( sp )       /* s8 */\r
73         lw      x25, 21 * WORD_SIZE( sp )       /* s9 */\r
74         lw      x26, 22 * WORD_SIZE( sp )       /* s10 */\r
75         lw      x27, 23 * WORD_SIZE( sp )       /* s11 */\r
76         lw      x28, 24 * WORD_SIZE( sp )       /* t3 */\r
77         lw      x29, 25 * WORD_SIZE( sp )       /* t4 */\r
78         lw      x30, 26 * WORD_SIZE( sp )       /* t5 */\r
79         lw      x31, 27 * WORD_SIZE( sp )       /* t6 */\r
80         addi    sp, sp, CONTEXT_SIZE\r
81         csrs    mie, 8                                  /* Enable soft interrupt. */\r
82         csrs    mstatus, 8                              /* Enable interrupts. */\r
83         ret\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 .align 8\r
88 vPortTrapHandler:\r
89         addi sp, sp, -CONTEXT_SIZE\r
90         sw x1, 0( sp )\r
91         sw x5, 1 * WORD_SIZE( sp )\r
92         sw x6, 2 * WORD_SIZE( sp )\r
93         sw x7, 3 * WORD_SIZE( sp )\r
94         sw x8, 4 * WORD_SIZE( sp )\r
95         sw x9, 5 * WORD_SIZE( sp )\r
96         sw x10, 6 * WORD_SIZE( sp )\r
97         sw x11, 7 * WORD_SIZE( sp )\r
98         sw x12, 8 * WORD_SIZE( sp )\r
99         sw x13, 9 * WORD_SIZE( sp )\r
100         sw x14, 10 * WORD_SIZE( sp )\r
101         sw x15, 11 * WORD_SIZE( sp )\r
102         sw x16, 12 * WORD_SIZE( sp )\r
103         sw x17, 13 * WORD_SIZE( sp )\r
104         sw x18, 14 * WORD_SIZE( sp )\r
105         sw x19, 15 * WORD_SIZE( sp )\r
106         sw x20, 16 * WORD_SIZE( sp )\r
107         sw x21, 17 * WORD_SIZE( sp )\r
108         sw x22, 18 * WORD_SIZE( sp )\r
109         sw x23, 19 * WORD_SIZE( sp )\r
110         sw x24, 20 * WORD_SIZE( sp )\r
111         sw x25, 21 * WORD_SIZE( sp )\r
112         sw x26, 22 * WORD_SIZE( sp )\r
113         sw x27, 23 * WORD_SIZE( sp )\r
114         sw x28, 24 * WORD_SIZE( sp )\r
115         sw x29, 25 * WORD_SIZE( sp )\r
116         sw x30, 26 * WORD_SIZE( sp )\r
117         sw x31, 27 * WORD_SIZE( sp )\r
118     lw  t0, pxCurrentTCB            /* Load pxCurrentTCB. */\r
119     sw  sp, 0( t0 )                 /* Write sp from first TCB member. */\r
120 \r
121         csrr t0, mepc\r
122         sw t0, 31 * WORD_SIZE( sp )\r
123 \r
124         csrr a0, mcause\r
125         csrr a1, mepc\r
126         mv a2, sp\r
127         jal handle_trap\r
128         csrw mepc, a0\r
129 \r
130         # Remain in M-mode after mret\r
131         li t0, 0x00001800 /* MSTATUS MPP */\r
132         csrs mstatus, t0\r
133 \r
134         /* Cut and past restore code from xPortStartScheduler - can be made a macro\r
135         but that makes debugging harder. */\r
136         lw      sp, pxCurrentTCB                        /* Load pxCurrentTCB. */\r
137         lw      sp, 0( sp )                                     /* Read sp from first TCB member. */\r
138         lw      x1, 0( sp )\r
139         lw      x5, 1 * WORD_SIZE( sp )         /* t0 */\r
140         lw      x6, 2 * WORD_SIZE( sp )         /* t1 */\r
141         lw      x7, 3 * WORD_SIZE( sp )         /* t2 */\r
142         lw      x8, 4 * WORD_SIZE( sp )         /* s0/fp */\r
143         lw      x9, 5 * WORD_SIZE( sp )         /* s1 */\r
144         lw      x10, 6 * WORD_SIZE( sp )        /* a0 */\r
145         lw      x11, 7 * WORD_SIZE( sp )        /* a1 */\r
146         lw      x12, 8 * WORD_SIZE( sp )        /* a2 */\r
147         lw      x13, 9 * WORD_SIZE( sp )        /* a3 */\r
148         lw      x14, 10 * WORD_SIZE( sp )       /* a4 */\r
149         lw      x15, 11 * WORD_SIZE( sp )       /* a5 */\r
150         lw      x16, 12 * WORD_SIZE( sp )       /* a6 */\r
151         lw      x17, 13 * WORD_SIZE( sp )       /* a7 */\r
152         lw      x18, 14 * WORD_SIZE( sp )       /* s2 */\r
153         lw      x19, 15 * WORD_SIZE( sp )       /* s3 */\r
154         lw      x20, 16 * WORD_SIZE( sp )       /* s4 */\r
155         lw      x21, 17 * WORD_SIZE( sp )       /* s5 */\r
156         lw      x22, 18 * WORD_SIZE( sp )       /* s6 */\r
157         lw      x23, 19 * WORD_SIZE( sp )       /* s7 */\r
158         lw      x24, 20 * WORD_SIZE( sp )       /* s8 */\r
159         lw      x25, 21 * WORD_SIZE( sp )       /* s9 */\r
160         lw      x26, 22 * WORD_SIZE( sp )       /* s10 */\r
161         lw      x27, 23 * WORD_SIZE( sp )       /* s11 */\r
162         lw      x28, 24 * WORD_SIZE( sp )       /* t3 */\r
163         lw      x29, 25 * WORD_SIZE( sp )       /* t4 */\r
164         lw      x30, 26 * WORD_SIZE( sp )       /* t5 */\r
165         lw      x31, 27 * WORD_SIZE( sp )       /* t6 */\r
166         addi sp, sp, CONTEXT_SIZE\r
167         mret\r