|
3 | 3 | // NOTE: Adapted from cortex-m/src/interrupt.rs
|
4 | 4 |
|
5 | 5 | pub mod machine {
|
6 |
| - use crate::register::mstatus; |
| 6 | + use crate::register::{mepc, mstatus}; |
7 | 7 |
|
8 | 8 | /// Disables all interrupts in the current hart (machine mode).
|
9 | 9 | #[inline]
|
10 | 10 | pub fn disable() {
|
| 11 | + // SAFETY: It is safe to disable interrupts |
11 | 12 | unsafe { mstatus::clear_mie() }
|
12 | 13 | }
|
13 | 14 |
|
@@ -49,13 +50,55 @@ pub mod machine {
|
49 | 50 |
|
50 | 51 | r
|
51 | 52 | }
|
| 53 | + |
| 54 | + /// Execute closure `f` with interrupts enabled in the current hart (machine mode). |
| 55 | + /// |
| 56 | + /// This method is assumed to be called within an interrupt handler, and allows |
| 57 | + /// nested interrupts to occur. After the closure `f` is executed, the [`mstatus`] |
| 58 | + /// and [`mepc`] registers are properly restored to their previous values. |
| 59 | + /// |
| 60 | + /// # Safety |
| 61 | + /// |
| 62 | + /// - Do not call this function inside a critical section. |
| 63 | + /// - This method is assumed to be called within an interrupt handler. |
| 64 | + /// - Make sure to clear the interrupt flag that caused the interrupt before calling |
| 65 | + /// this method. Otherwise, the interrupt will be re-triggered before executing `f`. |
| 66 | + #[inline] |
| 67 | + pub unsafe fn nested<F, R>(f: F) -> R |
| 68 | + where |
| 69 | + F: FnOnce() -> R, |
| 70 | + { |
| 71 | + let mstatus = mstatus::read(); |
| 72 | + let mepc = mepc::read(); |
| 73 | + |
| 74 | + // enable interrupts to allow nested interrupts |
| 75 | + enable(); |
| 76 | + |
| 77 | + let r = f(); |
| 78 | + |
| 79 | + // If the interrupts were inactive before our `enable` call, then re-disable |
| 80 | + // them. Otherwise, keep them enabled |
| 81 | + if !mstatus.mie() { |
| 82 | + disable(); |
| 83 | + } |
| 84 | + |
| 85 | + // Restore MSTATUS.PIE, MSTATUS.MPP, and SEPC |
| 86 | + if !mstatus.mpie() { |
| 87 | + mstatus::set_mpie(); |
| 88 | + } |
| 89 | + mstatus::set_mpp(mstatus.mpp()); |
| 90 | + mepc::write(mepc); |
| 91 | + |
| 92 | + r |
| 93 | + } |
52 | 94 | }
|
53 | 95 | pub mod supervisor {
|
54 |
| - use crate::register::sstatus; |
| 96 | + use crate::register::{sepc, sstatus}; |
55 | 97 |
|
56 | 98 | /// Disables all interrupts in the current hart (supervisor mode).
|
57 | 99 | #[inline]
|
58 | 100 | pub fn disable() {
|
| 101 | + // SAFETY: It is safe to disable interrupts |
59 | 102 | unsafe { sstatus::clear_sie() }
|
60 | 103 | }
|
61 | 104 |
|
@@ -97,6 +140,46 @@ pub mod supervisor {
|
97 | 140 |
|
98 | 141 | r
|
99 | 142 | }
|
| 143 | + |
| 144 | + /// Execute closure `f` with interrupts enabled in the current hart (supervisor mode). |
| 145 | + /// This method is assumed to be called within an interrupt handler, and allows |
| 146 | + /// nested interrupts to occur. After the closure `f` is executed, the [`sstatus`] |
| 147 | + /// and [`sepc`] registers are properly restored to their previous values. |
| 148 | + /// |
| 149 | + /// # Safety |
| 150 | + /// |
| 151 | + /// - Do not call this function inside a critical section. |
| 152 | + /// - This method is assumed to be called within an interrupt handler. |
| 153 | + /// - Make sure to clear the interrupt flag that caused the interrupt before calling |
| 154 | + /// this method. Otherwise, the interrupt will be re-triggered before executing `f`. |
| 155 | + #[inline] |
| 156 | + pub unsafe fn nested<F, R>(f: F) -> R |
| 157 | + where |
| 158 | + F: FnOnce() -> R, |
| 159 | + { |
| 160 | + let sstatus = sstatus::read(); |
| 161 | + let sepc = sepc::read(); |
| 162 | + |
| 163 | + // enable interrupts to allow nested interrupts |
| 164 | + enable(); |
| 165 | + |
| 166 | + let r = f(); |
| 167 | + |
| 168 | + // If the interrupts were inactive before our `enable` call, then re-disable |
| 169 | + // them. Otherwise, keep them enabled |
| 170 | + if !sstatus.sie() { |
| 171 | + disable(); |
| 172 | + } |
| 173 | + |
| 174 | + // Restore SSTATUS.SPIE, SSTATUS.SPP, and SEPC |
| 175 | + if !sstatus.spie() { |
| 176 | + sstatus::set_spie(); |
| 177 | + } |
| 178 | + sstatus::set_spp(sstatus.spp()); |
| 179 | + sepc::write(sepc); |
| 180 | + |
| 181 | + r |
| 182 | + } |
100 | 183 | }
|
101 | 184 |
|
102 | 185 | #[cfg(not(feature = "s-mode"))]
|
|
0 commit comments