Debian Codegraph @

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

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 /*
4 Adapted from https://github.com/Rahix/avr-device.
5
6 See also src/imp/avr.rs.
7
8 Refs:
9 - AVR® Instruction Set Manual, Rev. DS40002198B
10 https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-InstructionSet-Manual-DS40002198.pdf
11
12 See tests/asm-test/asm/portable-atomic for generated assembly.
13 */
14
15 #[cfg(not(portable_atomic_no_asm))]
16 use core::arch::asm;
17
18 #[cfg(not(portable_atomic_no_asm))]
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::avr as atomic;
28
29 pub(crate) type State = u8;
30
31 /// Disables interrupts and returns the previous interrupt state.
32 #[inline(always)]
33 pub(crate) fn disable() -> State {
34 let sreg: State;
35 // SAFETY: reading the status register (SREG) and disabling interrupts are safe.
36 // (see module-level comments of interrupt/mod.rs on the safety of using privileged instructions)
37 unsafe {
38 // Refs: https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-InstructionSet-Manual-DS40002198.pdf#page=58
39 #[cfg(not(portable_atomic_no_asm))]
40 asm!(
41 "in {sreg}, 0x3F", // sreg = SREG
42 "cli", // SREG.I = 0
43 sreg = out(reg) sreg,
44 // Do not use `nomem` and `readonly` because prevent subsequent memory accesses from being reordered before interrupts are disabled.
45 // Do not use `preserves_flags` because CLI modifies the I bit of the status register (SREG).
46 options(nostack),
47 );
48 #[cfg(portable_atomic_no_asm)]
49 {
50 llvm_asm!("in $0, 0x3F" : "=r"(sreg) ::: "volatile");
51 llvm_asm!("cli" ::: "memory" : "volatile");
52 }
53 }
54 sreg
55 }
56
57 /// Restores the previous interrupt state.
58 ///
59 /// # Safety
60 ///
61 /// The state must be the one retrieved by the previous `disable`.
62 #[inline(always)]
63 pub(crate) unsafe fn restore(prev_sreg: State) {
64 // SAFETY: the caller must guarantee that the state was retrieved by the previous `disable`.
65 //
66 // This clobbers the entire status register, but all other flags are the arithmetic flags that
67 // are changed as a side effect of arithmetic operations, etc., which LLVM recognizes unless
68 // preserves_flags is set, so it is safe to clobber them here.
69 // See also the discussion at https://github.com/taiki-e/portable-atomic/pull/40.
70 unsafe {
71 #[cfg(not(portable_atomic_no_asm))]
72 asm!(
73 "out 0x3F, {prev_sreg}", // SREG = prev_sreg
74 prev_sreg = in(reg) prev_sreg,
75 // Do not use `nomem` and `readonly` because prevent preceding memory accesses from being reordered after interrupts are enabled.
76 // Do not use `preserves_flags` because OUT modifies the status register (SREG).
77 options(nostack),
78 );
79 #[cfg(portable_atomic_no_asm)]
80 llvm_asm!("out 0x3F, $0" :: "r"(prev_sreg) : "memory" : "volatile");
81 }
82 }