Skip to content

Commit 0eda3c5

Browse files
bors[bot]almindor
andauthored
Merge #32
32: add user trap setup and handling registers r=almindor a=almindor Adds CSR handlers for user trap setup and handling registers. Also unifies common shared types. NOTE: untested, will test on real device ASAP Co-authored-by: Ales Katona <[email protected]>
2 parents 9d68612 + 73f45e3 commit 0eda3c5

File tree

13 files changed

+214
-23
lines changed

13 files changed

+214
-23
lines changed

src/register/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
#[macro_use]
1414
mod macros;
1515

16-
// TODO: User Trap Setup
17-
18-
19-
// TODO: User Trap Handling
20-
16+
// User Trap Setup
17+
pub mod ustatus;
18+
pub mod uie;
19+
pub mod utvec;
20+
21+
// User Trap Handling
22+
pub mod uscratch;
23+
pub mod uepc;
24+
pub mod ucause;
25+
pub mod utval;
26+
pub mod uip;
2127

2228
// User Floating-Point CSRs
2329
// TODO: frm, fflags

src/register/mstatus.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// TODO: Virtualization, Memory Privilege and Extension Context Fields
33

44
use bit_field::BitField;
5+
use core::mem::size_of;
56

67
/// mstatus register
78
#[derive(Clone, Copy, Debug)]
@@ -80,7 +81,7 @@ impl Mstatus {
8081
self.bits.get_bit(5)
8182
}
8283

83-
/// User Previous Interrupt Enable
84+
/// Machine Previous Interrupt Enable
8485
#[inline]
8586
pub fn mpie(&self) -> bool {
8687
self.bits.get_bit(7)
@@ -134,6 +135,13 @@ impl Mstatus {
134135
_ => unreachable!(),
135136
}
136137
}
138+
139+
/// Whether either the FS field or XS field
140+
/// signals the presence of some dirty state
141+
#[inline]
142+
pub fn sd(&self) -> bool {
143+
self.bits.get_bit(size_of::<usize>() * 8 - 1)
144+
}
137145
}
138146

139147

src/register/scause.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ impl Interrupt {
5959
}
6060
}
6161

62-
6362
impl Exception {
6463
pub fn from(nr: usize) -> Self {
6564
match nr {

src/register/sstatus.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use bit_field::BitField;
44
use core::mem::size_of;
5+
pub use super::mstatus::FS;
56

67
/// Supervisor Status Register
78
#[derive(Clone, Copy, Debug)]
@@ -16,15 +17,6 @@ pub enum SPP {
1617
User = 0,
1718
}
1819

19-
/// Floating-point unit Status
20-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
21-
pub enum FS {
22-
Off = 0,
23-
Initial = 1,
24-
Clean = 2,
25-
Dirty = 3,
26-
}
27-
2820
impl Sstatus {
2921
/// User Interrupt Enable
3022
#[inline]

src/register/stvec.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
//! stvec register
22
3+
pub use crate::register::mtvec::TrapMode;
4+
35
/// stvec register
46
#[derive(Clone, Copy, Debug)]
57
pub struct Stvec {
68
bits: usize,
79
}
810

9-
/// Trap mode
10-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
11-
pub enum TrapMode {
12-
Direct = 0,
13-
Vectored = 1,
14-
}
15-
1611
impl Stvec {
1712
/// Returns the contents of the register as raw bits
1813
pub fn bits(&self) -> usize {

src/register/ucause.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! ucause register
2+
3+
/// ucause register
4+
#[derive(Clone, Copy, Debug)]
5+
pub struct Ucause {
6+
bits: usize,
7+
}
8+
9+
impl Ucause {
10+
/// Returns the contents of the register as raw bits
11+
#[inline]
12+
pub fn bits(&self) -> usize {
13+
self.bits
14+
}
15+
}
16+
17+
read_csr_as!(Ucause, 0x042, __read_ucause);

src/register/uepc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! uepc register
2+
3+
read_csr_as_usize!(0x041, __read_uepc);
4+
write_csr_as_usize!(0x041, __write_uepc);

src/register/uie.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! uie register
2+
3+
use bit_field::BitField;
4+
5+
/// uie register
6+
#[derive(Clone, Copy, Debug)]
7+
pub struct Uie {
8+
bits: usize,
9+
}
10+
11+
impl Uie {
12+
/// Returns the contents of the register as raw bits
13+
#[inline]
14+
pub fn bits(&self) -> usize {
15+
self.bits
16+
}
17+
18+
/// User Software Interrupt Enable
19+
#[inline]
20+
pub fn usoft(&self) -> bool {
21+
self.bits.get_bit(0)
22+
}
23+
24+
/// User Timer Interrupt Enable
25+
#[inline]
26+
pub fn utimer(&self) -> bool {
27+
self.bits.get_bit(4)
28+
}
29+
30+
/// User External Interrupt Enable
31+
#[inline]
32+
pub fn uext(&self) -> bool {
33+
self.bits.get_bit(8)
34+
}
35+
}
36+
37+
read_csr_as!(Uie, 0x004, __read_uie);
38+
set!(0x004, __set_uie);
39+
clear!(0x004, __clear_uie);
40+
41+
set_clear_csr!(
42+
/// User Software Interrupt Enable
43+
, set_usoft, clear_usoft, 1 << 0);
44+
set_clear_csr!(
45+
/// User Timer Interrupt Enable
46+
, set_utimer, clear_utimer, 1 << 4);
47+
set_clear_csr!(
48+
/// User External Interrupt Enable
49+
, set_uext, clear_uext, 1 << 8);

src/register/uip.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! uip register
2+
3+
use bit_field::BitField;
4+
5+
/// uip register
6+
#[derive(Clone, Copy, Debug)]
7+
pub struct Uip {
8+
bits: usize,
9+
}
10+
11+
impl Uip {
12+
/// Returns the contents of the register as raw bits
13+
#[inline]
14+
pub fn bits(&self) -> usize {
15+
self.bits
16+
}
17+
18+
/// User Software Interrupt Pending
19+
#[inline]
20+
pub fn usoft(&self) -> bool {
21+
self.bits.get_bit(0)
22+
}
23+
24+
/// User Timer Interrupt Pending
25+
#[inline]
26+
pub fn utimer(&self) -> bool {
27+
self.bits.get_bit(4)
28+
}
29+
30+
/// User External Interrupt Pending
31+
#[inline]
32+
pub fn uext(&self) -> bool {
33+
self.bits.get_bit(8)
34+
}
35+
}
36+
37+
read_csr_as!(Uip, 0x044, __read_uip);

src/register/uscratch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! uscratch register
2+
3+
read_csr_as_usize!(0x040, __read_uscratch);
4+
write_csr_as_usize!(0x040, __write_uscratch);

0 commit comments

Comments
 (0)