1 // Copyright 2024 The percore Authors.
2 // This project is dual-licensed under Apache 2.0 and MIT terms.
3 // See LICENSE-APACHE and LICENSE-MIT for details.
4 
5 use core::arch::asm;
6 
7 /// Masks IRQs, FIQs, SErrors and Debug exceptions.
8 ///
9 /// Returns the previous mask value, to be passed to [`unmask`].
mask() -> u6410 pub fn mask() -> u64 {
11     let prev;
12 
13     // SAFETY: Writing to this system register doesn't access memory in any way.
14     unsafe {
15         asm!(
16             "mrs {prev:x}, DAIF",
17             "msr DAIFSet, #0xf",
18             options(nostack),
19             prev = out(reg) prev,
20         );
21     }
22 
23     prev
24 }
25 
26 /// Restores the given previous exception mask value.
27 ///
28 /// # Safety
29 ///
30 /// Must not be called while a corresponding `ExceptionFree` token exists.
restore(prev: u64)31 pub unsafe fn restore(prev: u64) {
32     // SAFETY: Writing to this system register doesn't access memory in any way. The caller promised
33     // that there is no `ExceptionFree` token.
34     unsafe {
35         asm!(
36             "msr DAIF, {prev:x}",
37             options(nostack),
38             prev=in(reg)prev,
39         );
40     }
41 }
42