Debian Codegraph @

debian/rust-vendor/portable-atomic/src/imp/interrupt/riscv.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 /*
4 Refs:
5 - RISC-V Instruction Set Manual
6 Machine Status (mstatus and mstatush) Registers
7 https://docs.riscv.org/reference/isa/v20260120/priv/machine.html#3-1-1-6-machine-status-mstatus-and-mstatush-registers
8 Supervisor Status (sstatus) Register
9 https://docs.riscv.org/reference/isa/v20260120/priv/supervisor.html#sstatus
10
11 See also src/imp/riscv.rs.
12
13 See tests/asm-test/asm/portable-atomic for generated assembly.
14 */
15
16 #[cfg(not(portable_atomic_no_asm))]
17 use core::arch::asm;
18
19 #[cfg_attr(
20 portable_atomic_no_cfg_target_has_atomic,
21 cfg(any(test, portable_atomic_no_atomic_cas))
22 )]
23 #[cfg_attr(
24 not(portable_atomic_no_cfg_target_has_atomic),
25 cfg(any(test, not(target_has_atomic = "ptr")))
26 )]
27 pub(super) use super::super::riscv as atomic;
28
29 cfg_sel!({
30 // Supervisor-mode (S-mode)
31 #[cfg(portable_atomic_s_mode)]
32 {
33 // Status register
34 macro_rules! status {
35 () => {
36 "sstatus"
37 };
38 }
39 // SIE (Supervisor Interrupt Enable) bit (1 << 1)
40 #[cfg(portable_atomic_s_mode)]
41 macro_rules! mask {
42 () => {
43 "0x2"
44 };
45 }
46 const MASK: State = 0x2;
47 }
48 // Machine-mode (M-mode)
49 #[cfg(else)]
50 {
51 // Status register
52 macro_rules! status {
53 () => {
54 "mstatus"
55 };
56 }
57 // MIE (Machine Interrupt Enable) bit (1 << 3)
58 macro_rules! mask {
59 () => {
60 "0x8"
61 };
62 }
63 const MASK: State = 0x8;
64 }
65 });
66
67 pub(crate) type State = crate::utils::RegSize;
68
69 /// Disables interrupts and returns the previous interrupt state.
70 #[inline(always)]
71 pub(crate) fn disable() -> State {
72 let status: State;
73 // SAFETY: reading mstatus/sstatus and disabling interrupts is safe.
74 // (see module-level comments of interrupt/mod.rs on the safety of using privileged instructions)
75 unsafe {
76 asm!(
77 concat!("csrrci {status}, ", status!(), ", ", mask!()), // atomic { status = status!(); status!() &= !mask!() }
78 status = out(reg) status,
79 // Do not use `nomem` and `readonly` because prevent subsequent memory accesses from being reordered before interrupts are disabled.
80 options(nostack, preserves_flags),
81 );
82 }
83 status
84 }
85
86 /// Restores the previous interrupt state.
87 ///
88 /// # Safety
89 ///
90 /// The state must be the one retrieved by the previous `disable`.
91 #[inline(always)]
92 pub(crate) unsafe fn restore(prev_status: State) {
93 // SAFETY: the caller must guarantee that the state was retrieved by the previous `disable`,
94 //
95 // This avoids clobbering other fields in the mstatus/sstatus register.
96 unsafe {
97 asm!(
98 concat!("csrs ", status!(), ", {mask}"), // atomic { status!() |= mask }
99 mask = in(reg) prev_status & MASK,
100 // Do not use `nomem` and `readonly` because prevent preceding memory accesses from being reordered after interrupts are enabled.
101 options(nostack, preserves_flags),
102 );
103 }
104 }