Skip to content

Commit 32c5b25

Browse files
[libcpu] add risc-v e310 porting
1 parent 45bd118 commit 32c5b25

File tree

4 files changed

+489
-0
lines changed

4 files changed

+489
-0
lines changed

libcpu/risc-v/e310/SConscript

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Import('rtconfig')
2+
from building import *
3+
4+
cwd = GetCurrentDir()
5+
src = Glob('*.c')
6+
CPPPATH = [cwd]
7+
8+
if rtconfig.PLATFORM == 'gcc':
9+
src += Glob('*_gcc.S')
10+
11+
group = DefineGroup('libcpu', src, depend = [''], CPPPATH = CPPPATH)
12+
13+
Return('group')

libcpu/risc-v/e310/context_gcc.S

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
;/*
2+
; * File : context_gcc.S
3+
; * This file is part of RT-Thread RTOS
4+
; * COPYRIGHT (C) 2018, RT-Thread Development Team
5+
; *
6+
; * This program is free software; you can redistribute it and/or modify
7+
; * it under the terms of the GNU General Public License as published by
8+
; * the Free Software Foundation; either version 2 of the License, or
9+
; * (at your option) any later version.
10+
; *
11+
; * This program is distributed in the hope that it will be useful,
12+
; * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
; * GNU General Public License for more details.
15+
; *
16+
; * You should have received a copy of the GNU General Public License along
17+
; * with this program; if not, write to the Free Software Foundation, Inc.,
18+
; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
; *
20+
; * Change Logs:
21+
; * Date Author Notes
22+
; * 2017-07-16 zhangjun for hifive1
23+
; * 2018-05-29 tanek optimize rt_hw_interrupt_*
24+
; * 2018-05-29 tanek add mie register to context
25+
; */
26+
27+
/*
28+
* rt_base_t rt_hw_interrupt_disable(void);
29+
*/
30+
.globl rt_hw_interrupt_disable
31+
rt_hw_interrupt_disable:
32+
csrrci a0, mstatus, 8
33+
ret
34+
35+
/*
36+
* void rt_hw_interrupt_enable(rt_base_t level);
37+
*/
38+
.globl rt_hw_interrupt_enable
39+
rt_hw_interrupt_enable:
40+
csrw mstatus, a0
41+
ret
42+
43+
/*
44+
* void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
45+
* a0 --> from
46+
* a1 --> to
47+
*/
48+
.globl rt_hw_context_switch
49+
rt_hw_context_switch:
50+
51+
/* saved from thread context
52+
* x1/ra -> sp(0)
53+
* x1/ra -> sp(1)
54+
* mstatus.mie -> sp(2)
55+
* x(i) -> sp(i-4)
56+
*/
57+
addi sp, sp, -32 * 4
58+
sw sp, (a0)
59+
60+
sw x1, 0 * 4(sp)
61+
sw x1, 1 * 4(sp)
62+
63+
csrr a0, mstatus
64+
andi a0, a0, 8
65+
beqz a0, save_mpie
66+
li a0, 0x80
67+
save_mpie:
68+
sw a0, 2 * 4(sp)
69+
70+
sw x4, 4 * 4(sp)
71+
sw x5, 5 * 4(sp)
72+
sw x6, 6 * 4(sp)
73+
sw x7, 7 * 4(sp)
74+
sw x8, 8 * 4(sp)
75+
sw x9, 9 * 4(sp)
76+
sw x10, 10 * 4(sp)
77+
sw x11, 11 * 4(sp)
78+
sw x12, 12 * 4(sp)
79+
sw x13, 13 * 4(sp)
80+
sw x14, 14 * 4(sp)
81+
sw x15, 15 * 4(sp)
82+
sw x16, 16 * 4(sp)
83+
sw x17, 17 * 4(sp)
84+
sw x18, 18 * 4(sp)
85+
sw x19, 19 * 4(sp)
86+
sw x20, 20 * 4(sp)
87+
sw x21, 21 * 4(sp)
88+
sw x22, 22 * 4(sp)
89+
sw x23, 23 * 4(sp)
90+
sw x24, 24 * 4(sp)
91+
sw x25, 25 * 4(sp)
92+
sw x26, 26 * 4(sp)
93+
sw x27, 27 * 4(sp)
94+
sw x28, 28 * 4(sp)
95+
sw x29, 29 * 4(sp)
96+
sw x30, 30 * 4(sp)
97+
sw x31, 31 * 4(sp)
98+
99+
/* restore to thread context
100+
* sp(0) -> epc;
101+
* sp(1) -> ra;
102+
* sp(i) -> x(i+2)
103+
*/
104+
lw sp, (a1)
105+
106+
/* resw ra to mepc */
107+
lw a1, 0 * 4(sp)
108+
csrw mepc, a1
109+
lw x1, 1 * 4(sp)
110+
111+
/* force to machin mode(MPP=11) */
112+
li a1, 0x00001800;
113+
csrs mstatus, a1
114+
lw a1, 2 * 4(sp)
115+
csrs mstatus, a1
116+
117+
lw x4, 4 * 4(sp)
118+
lw x5, 5 * 4(sp)
119+
lw x6, 6 * 4(sp)
120+
lw x7, 7 * 4(sp)
121+
lw x8, 8 * 4(sp)
122+
lw x9, 9 * 4(sp)
123+
lw x10, 10 * 4(sp)
124+
lw x11, 11 * 4(sp)
125+
lw x12, 12 * 4(sp)
126+
lw x13, 13 * 4(sp)
127+
lw x14, 14 * 4(sp)
128+
lw x15, 15 * 4(sp)
129+
lw x16, 16 * 4(sp)
130+
lw x17, 17 * 4(sp)
131+
lw x18, 18 * 4(sp)
132+
lw x19, 19 * 4(sp)
133+
lw x20, 20 * 4(sp)
134+
lw x21, 21 * 4(sp)
135+
lw x22, 22 * 4(sp)
136+
lw x23, 23 * 4(sp)
137+
lw x24, 24 * 4(sp)
138+
lw x25, 25 * 4(sp)
139+
lw x26, 26 * 4(sp)
140+
lw x27, 27 * 4(sp)
141+
lw x28, 28 * 4(sp)
142+
lw x29, 29 * 4(sp)
143+
lw x30, 30 * 4(sp)
144+
lw x31, 31 * 4(sp)
145+
146+
addi sp, sp, 32 * 4
147+
mret
148+
149+
/*
150+
* void rt_hw_context_switch_to(rt_uint32 to);
151+
* a0 --> to
152+
*/
153+
.globl rt_hw_context_switch_to
154+
rt_hw_context_switch_to:
155+
lw sp, (a0)
156+
157+
/* load epc from stack */
158+
lw a0, 0 * 4(sp)
159+
csrw mepc, a0
160+
lw x1, 1 * 4(sp)
161+
/* load mstatus from stack */
162+
lw a0, 2 * 4(sp)
163+
csrw mstatus, a0
164+
lw x4, 4 * 4(sp)
165+
lw x5, 5 * 4(sp)
166+
lw x6, 6 * 4(sp)
167+
lw x7, 7 * 4(sp)
168+
lw x8, 8 * 4(sp)
169+
lw x9, 9 * 4(sp)
170+
lw x10, 10 * 4(sp)
171+
lw x11, 11 * 4(sp)
172+
lw x12, 12 * 4(sp)
173+
lw x13, 13 * 4(sp)
174+
lw x14, 14 * 4(sp)
175+
lw x15, 15 * 4(sp)
176+
lw x16, 16 * 4(sp)
177+
lw x17, 17 * 4(sp)
178+
lw x18, 18 * 4(sp)
179+
lw x19, 19 * 4(sp)
180+
lw x20, 20 * 4(sp)
181+
lw x21, 21 * 4(sp)
182+
lw x22, 22 * 4(sp)
183+
lw x23, 23 * 4(sp)
184+
lw x24, 24 * 4(sp)
185+
lw x25, 25 * 4(sp)
186+
lw x26, 26 * 4(sp)
187+
lw x27, 27 * 4(sp)
188+
lw x28, 28 * 4(sp)
189+
lw x29, 29 * 4(sp)
190+
lw x30, 30 * 4(sp)
191+
lw x31, 31 * 4(sp)
192+
193+
addi sp, sp, 32 * 4
194+
mret
195+
196+
/*
197+
* void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
198+
*/
199+
.globl rt_thread_switch_interrupt_flag
200+
.globl rt_interrupt_from_thread
201+
.globl rt_interrupt_to_thread
202+
.globl rt_hw_context_switch_interrupt
203+
rt_hw_context_switch_interrupt:
204+
addi sp, sp, -16
205+
sw s0, 12(sp)
206+
sw a0, 8(sp)
207+
sw a5, 4(sp)
208+
209+
la a0, rt_thread_switch_interrupt_flag
210+
lw a5, (a0)
211+
bnez a5, _reswitch
212+
li a5, 1
213+
sw a5, (a0)
214+
215+
la a5, rt_interrupt_from_thread
216+
lw a0, 8(sp)
217+
sw a0, (a5)
218+
219+
_reswitch:
220+
la a5, rt_interrupt_to_thread
221+
sw a1, (a5)
222+
223+
lw a5, 4(sp)
224+
lw a0, 8(sp)
225+
lw s0, 12(sp)
226+
addi sp, sp, 16
227+
ret

libcpu/risc-v/e310/entry_gcc.S

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* File : context_gcc.S
3+
* This file is part of RT-Thread RTOS
4+
* COPYRIGHT (C) 2018, RT-Thread Development Team
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program; if not, write to the Free Software Foundation, Inc.,
18+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Change Logs:
21+
* Date Author Notes
22+
* 2018-05-29 tanek first implementation
23+
*/
24+
25+
.section .text.entry
26+
.align 2
27+
.global trap_entry
28+
trap_entry:
29+
30+
// save all from thread context
31+
addi sp, sp, -32 * 4
32+
33+
sw x1, 1 * 4(sp)
34+
li t0, 0x80
35+
sw t0, 2 * 4(sp)
36+
37+
sw x4, 4 * 4(sp)
38+
sw x5, 5 * 4(sp)
39+
sw x6, 6 * 4(sp)
40+
sw x7, 7 * 4(sp)
41+
sw x8, 8 * 4(sp)
42+
sw x9, 9 * 4(sp)
43+
sw x10, 10 * 4(sp)
44+
sw x11, 11 * 4(sp)
45+
sw x12, 12 * 4(sp)
46+
sw x13, 13 * 4(sp)
47+
sw x14, 14 * 4(sp)
48+
sw x15, 15 * 4(sp)
49+
sw x16, 16 * 4(sp)
50+
sw x17, 17 * 4(sp)
51+
sw x18, 18 * 4(sp)
52+
sw x19, 19 * 4(sp)
53+
sw x20, 20 * 4(sp)
54+
sw x21, 21 * 4(sp)
55+
sw x22, 22 * 4(sp)
56+
sw x23, 23 * 4(sp)
57+
sw x24, 24 * 4(sp)
58+
sw x25, 25 * 4(sp)
59+
sw x26, 26 * 4(sp)
60+
sw x27, 27 * 4(sp)
61+
sw x28, 28 * 4(sp)
62+
sw x29, 29 * 4(sp)
63+
sw x30, 30 * 4(sp)
64+
sw x31, 31 * 4(sp)
65+
66+
// switch to interrupt stack
67+
move s0, sp
68+
la sp, _sp
69+
70+
// interrupt handle
71+
call rt_interrupt_enter
72+
csrr a0, mcause
73+
csrr a1, mepc
74+
mv a2, sp
75+
call handle_trap
76+
call rt_interrupt_leave
77+
78+
// switch to from thread stack
79+
move sp, s0
80+
81+
// need to switch new thread
82+
la s0, rt_thread_switch_interrupt_flag
83+
lw s2, 0(s0)
84+
beqz s2, spurious_interrupt
85+
sw zero, 0(s0)
86+
87+
csrr a0, mepc
88+
sw a0, 0 * 4(sp)
89+
90+
la s0, rt_interrupt_from_thread
91+
lw s1, 0(s0)
92+
sw sp, 0(s1)
93+
94+
la s0, rt_interrupt_to_thread
95+
lw s1, 0(s0)
96+
lw sp, 0(s1)
97+
98+
lw a0, 0 * 4(sp)
99+
csrw mepc, a0
100+
101+
spurious_interrupt:
102+
lw x1, 1 * 4(sp)
103+
104+
// Remain in M-mode after mret
105+
li t0, 0x00001800
106+
csrs mstatus, t0
107+
lw t0, 2 * 4(sp)
108+
csrs mstatus, t0
109+
110+
lw x4, 4 * 4(sp)
111+
lw x5, 5 * 4(sp)
112+
lw x6, 6 * 4(sp)
113+
lw x7, 7 * 4(sp)
114+
lw x8, 8 * 4(sp)
115+
lw x9, 9 * 4(sp)
116+
lw x10, 10 * 4(sp)
117+
lw x11, 11 * 4(sp)
118+
lw x12, 12 * 4(sp)
119+
lw x13, 13 * 4(sp)
120+
lw x14, 14 * 4(sp)
121+
lw x15, 15 * 4(sp)
122+
lw x16, 16 * 4(sp)
123+
lw x17, 17 * 4(sp)
124+
lw x18, 18 * 4(sp)
125+
lw x19, 19 * 4(sp)
126+
lw x20, 20 * 4(sp)
127+
lw x21, 21 * 4(sp)
128+
lw x22, 22 * 4(sp)
129+
lw x23, 23 * 4(sp)
130+
lw x24, 24 * 4(sp)
131+
lw x25, 25 * 4(sp)
132+
lw x26, 26 * 4(sp)
133+
lw x27, 27 * 4(sp)
134+
lw x28, 28 * 4(sp)
135+
lw x29, 29 * 4(sp)
136+
lw x30, 30 * 4(sp)
137+
lw x31, 31 * 4(sp)
138+
139+
addi sp, sp, 32 * 4
140+
mret
141+
142+
.weak handle_trap
143+
handle_trap:
144+
1:
145+
j 1b

0 commit comments

Comments
 (0)