Skip to content

Commit 83fc3b7

Browse files
ExplodingWaffleCodetector1374
authored andcommitted
Add u-mode feature
1 parent 12de573 commit 83fc3b7

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

qingke-rt/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ readme = "README.md"
1717
v2 = []
1818
v3 = ["qingke/v3"]
1919
v4 = []
20+
21+
u-mode = []
2022
# v5 is not released yet
2123
# v5 = []
2224

qingke-rt/src/lib.rs

+36-9
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,24 @@ unsafe extern "C" fn qingke_setup_interrupts() {
120120
// 0x3 both nested interrupts and hardware stack
121121
// 0x1 only hardware stack
122122

123+
// for user mode: mstatus = 0x80
124+
// mpp(m-mode previous privilege) = 0b00 = U
125+
// mpie(m-mode previous interrupt enable) = 0b1
126+
// mie(m-mode interrupt enable) = 0b0
127+
// interrupts will be enabled when mret at the end of handle_reset
128+
// jumps to main (mret does mie = mpie)
129+
// for machine mode: mstatus = 0x1880
130+
// mpp = 0b11
131+
// mpie = 0b1
132+
// mie = 0b0
133+
123134
// Qingke V2A, V2C
135+
// (does not have user mode)
124136
#[cfg(feature = "v2")]
125137
{
126138
core::arch::asm!(
127139
"
128-
li t0, 0x80
140+
li t0, 0x1880
129141
csrw mstatus, t0
130142
li t0, 0x3
131143
csrw 0x804, t0
@@ -136,33 +148,48 @@ unsafe extern "C" fn qingke_setup_interrupts() {
136148
// Qingke V3A
137149
#[cfg(feature = "v3")]
138150
{
151+
#[cfg(feature = "u-mode")]
152+
core::arch::asm!(
153+
"
154+
li t0, 0x80
155+
csrs mstatus, t0
156+
"
157+
);
158+
#[cfg(not(feature = "u-mode"))]
139159
core::arch::asm!(
140160
"
141-
li t0, 0x88
161+
li t0, 0x1880
142162
csrs mstatus, t0
143-
"
163+
"
144164
);
145165
}
146166

147-
// return to user mode
148-
// mstate
149-
// - use 0x88 to set mpp=0, return to user mode
150-
// - use 0x1888 to set mpp=3, return to machine mode
151-
152167
// corecfgr(0xbc0): 流水线控制位 & 动态预测控制位
153168
// corecfgr(0xbc0): Pipeline control bit & Dynamic prediction control
154169
#[cfg(any(
155170
feature = "v4",
156171
not(any(feature = "v2", feature = "v3", feature = "v4")) // Fallback condition
157172
))]
158173
{
174+
#[cfg(feature = "u-mode")]
175+
core::arch::asm!(
176+
"
177+
li t0, 0x1f
178+
csrw 0xbc0, t0
179+
li t0, 0x3
180+
csrw 0x804, t0
181+
li t0, 0x80
182+
csrs mstatus, t0
183+
"
184+
);
185+
#[cfg(not(feature = "u-mode"))]
159186
core::arch::asm!(
160187
"
161188
li t0, 0x1f
162189
csrw 0xbc0, t0
163190
li t0, 0x3
164191
csrw 0x804, t0
165-
li t0, 0x88
192+
li t0, 0x1880
166193
csrs mstatus, t0
167194
"
168195
);

0 commit comments

Comments
 (0)