-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.rs
277 lines (251 loc) · 6.66 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#![no_std]
//! # Differences vs the riscv-rt version
//!
//! - The structure of exception handlers is different
//! - The structure of core interrupt handlers is different
//! - Hardware stack push is available, so no need to push manually
use qingke::{
register::mtvec::{self, TrapMode},
riscv::register::mcause,
};
#[cfg(feature = "highcode")]
pub use qingke_rt_macros::highcode;
pub use qingke_rt_macros::{entry, interrupt};
use core::arch::global_asm;
mod asm;
// Let this crate conflicts with riscv-rt
#[export_name = "error: riscv-rt appears more than once in the dependency graph"]
#[doc(hidden)]
pub static __ONCE__: () = ();
extern "C" {
fn Exception();
fn InstructionMisaligned();
fn InstructionFault();
fn IllegalInstruction();
fn LoadMisaligned();
fn LoadFault();
fn StoreMisaligned();
fn StoreFault();
fn NonMaskableInt();
fn MachineEnvCall();
fn UserEnvCall();
fn Breakpoint();
fn SysTick();
fn Software();
}
#[doc(hidden)]
#[no_mangle]
#[link_section = ".vector_table.exceptions"]
pub static __EXCEPTIONS: [Option<unsafe extern "C" fn()>; 12] = [
Some(InstructionMisaligned), // 0
Some(InstructionFault),
Some(IllegalInstruction),
Some(Breakpoint),
Some(LoadMisaligned),
Some(LoadFault), // 5, Not accurate, async
Some(StoreMisaligned),
Some(StoreFault), // 7, Not accurate, async
Some(UserEnvCall), // not available for Qingke V2
None,
None,
Some(MachineEnvCall),
];
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum CoreInterrupt {
NonMaskableInt = 2,
Exception = 3,
MachineEnvCall = 5,
UserEnvCall = 8,
Breakpoint = 9,
SysTick = 12,
Software = 14,
}
impl CoreInterrupt {
pub fn try_from(irq: u8) -> Result<Self, u8> {
match irq {
2 => Ok(CoreInterrupt::NonMaskableInt),
3 => Ok(CoreInterrupt::Exception),
5 => Ok(CoreInterrupt::MachineEnvCall),
8 => Ok(CoreInterrupt::UserEnvCall),
9 => Ok(CoreInterrupt::Breakpoint),
12 => Ok(CoreInterrupt::SysTick),
14 => Ok(CoreInterrupt::Software),
_ => Err(irq),
}
}
}
/// Core interrupts, without the first one
#[doc(hidden)]
#[no_mangle]
#[used]
#[link_section = ".vector_table.core_interrupts"]
pub static __CORE_INTERRUPTS: [Option<unsafe extern "C" fn()>; 15] = [
// None, // skip 0
None,
Some(NonMaskableInt), // 2
Some(Exception), // 3
None,
Some(MachineEnvCall), // 5
None,
None,
Some(UserEnvCall), // 8
Some(Breakpoint), // 9
None,
None,
Some(SysTick), // 12
None,
Some(Software), // 14
None,
];
// followed by .vector_table.external_interrupts
#[no_mangle]
#[link_section = ".init.rust"]
#[export_name = "_setup_interrupts"]
unsafe extern "C" fn qingke_setup_interrupts() {
// enable hardware stack push
// intsyscr(0x804): Open nested interrupts and hardware stack functions
// 0x3 both nested interrupts and hardware stack
// 0x1 only hardware stack
// for user mode: mstatus = 0x80
// mpp(m-mode previous privilege) = 0b00 = U
// mpie(m-mode previous interrupt enable) = 0b1
// mie(m-mode interrupt enable) = 0b0
// interrupts will be enabled when mret at the end of handle_reset
// jumps to main (mret does mie = mpie)
// for machine mode: mstatus = 0x1880
// mpp = 0b11
// mpie = 0b1
// mie = 0b0
// Qingke V2A, V2C
// (does not have user mode)
#[cfg(feature = "v2")]
{
core::arch::asm!(
"
li t0, 0x1880
csrw mstatus, t0
li t0, 0x3
csrw 0x804, t0
"
);
}
// Qingke V3A
#[cfg(feature = "v3")]
{
#[cfg(feature = "u-mode")]
core::arch::asm!(
"
li t0, 0x80
csrs mstatus, t0
"
);
#[cfg(not(feature = "u-mode"))]
core::arch::asm!(
"
li t0, 0x1880
csrs mstatus, t0
"
);
}
// corecfgr(0xbc0): 流水线控制位 & 动态预测控制位
// corecfgr(0xbc0): Pipeline control bit & Dynamic prediction control
#[cfg(any(
feature = "v4",
not(any(feature = "v2", feature = "v3", feature = "v4")) // Fallback condition
))]
{
#[cfg(feature = "u-mode")]
core::arch::asm!(
"
li t0, 0x1f
csrw 0xbc0, t0
li t0, 0x3
csrw 0x804, t0
li t0, 0x80
csrs mstatus, t0
"
);
#[cfg(not(feature = "u-mode"))]
core::arch::asm!(
"
li t0, 0x1f
csrw 0xbc0, t0
li t0, 0x3
csrw 0x804, t0
li t0, 0x1880
csrs mstatus, t0
"
);
qingke::register::gintenr::set_enable();
}
// Qingke V2's mtvec must be 1KB aligned.
#[cfg(feature = "highcode")]
mtvec::write(0x20000000, TrapMode::VectoredAddress);
#[cfg(not(feature = "highcode"))]
mtvec::write(0x00000000, TrapMode::VectoredAddress);
qingke::pfic::wfi_to_wfe(true);
}
#[doc(hidden)]
#[no_mangle]
#[allow(non_snake_case)]
pub fn DefaultInterruptHandler() {
loop {
// Prevent this from turning into a UDF instruction
// see rust-lang/rust#28728 for details
continue;
}
}
#[doc(hidden)]
#[no_mangle]
#[allow(non_snake_case)]
pub fn DefaultExceptionHandler() -> ! {
loop {
// Prevent this from turning into a UDF instruction
// see rust-lang/rust#28728 for details
continue;
}
}
// override _start_trap in riscv-rt
global_asm!(
r#"
.section .trap, "ax"
.global _exception_handler
_exception_handler:
addi sp, sp, -4
sw ra, 0(sp)
jal _exception_handler_rust
lw ra, 0(sp)
addi sp, sp, 4
mret
"#
);
#[doc(hidden)]
#[link_section = ".trap.rust"]
#[export_name = "_exception_handler_rust"]
pub unsafe extern "C" fn qingke_exception_handler() {
// jump according to the __EXCEPTIONS table
extern "C" {
fn ExceptionHandler();
}
let cause = mcause::read();
let code = cause.code();
if cause.is_exception() {
if code < __EXCEPTIONS.len() {
let h = &__EXCEPTIONS[code];
if let Some(handler) = h {
handler();
} else {
ExceptionHandler();
}
} else {
ExceptionHandler();
}
} else {
loop {
// Prevent this from turning into a UDF instruction
// see rust-lang/rust#28728 for details
continue;
}
}
}