Skip to main content

strat9_kernel/framebuffer/
fpu.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! FPU/SSE state preservation around SIMD pixel operations.
24//!
25//! The SSE2 fill/blit paths clobber XMM registers, which would corrupt the
26//! floating-point state of an interrupted thread (or of the caller) when a
27//! draw happens from an interrupt context. [`FpuGuard`] saves the live
28//! x87/SSE state on construction and restores it on drop.
29//!
30//! Only the legacy 512-byte FXSAVE area is captured. AVX (YMM) registers are
31//! therefore *not* preserved: streaming/AVX code must not be introduced until
32//! this guard switches to XSAVE/XRSTOR.
33
34/// Saves the caller's FPU/SSE state and restores it when dropped.
35pub struct FpuGuard {
36    #[allow(dead_code)]
37    area: FxArea,
38}
39
40/// 512-byte FXSAVE area, 16-byte aligned as required by `fxsave`/`fxrstor`.
41#[cfg(target_arch = "x86_64")]
42#[repr(align(16))]
43struct FxArea([u8; 512]);
44
45/// Placeholder on architectures without an x86 FPU save area.
46#[cfg(not(target_arch = "x86_64"))]
47struct FxArea;
48
49impl FpuGuard {
50    /// Capture the current FPU/SSE state.
51    #[inline]
52    pub fn save() -> Self {
53        #[cfg(target_arch = "x86_64")]
54        {
55            let mut area = FxArea([0u8; 512]);
56            let ptr = area.0.as_mut_ptr();
57            debug_assert_eq!(ptr as usize % 16, 0, "FXSAVE area must be 16-byte aligned");
58            // SAFETY: `area` is 16-byte aligned and 512 bytes long, as required
59            // by FXSAVE. The instruction only writes to that area; omitting
60            // `nomem` keeps the compiler from moving memory accesses across it.
61            unsafe {
62                core::arch::asm!("fxsave [{ptr}]", ptr = in(reg) ptr, options(nostack));
63            }
64            Self { area }
65        }
66
67        #[cfg(not(target_arch = "x86_64"))]
68        {
69            Self { area: FxArea }
70        }
71    }
72}
73
74impl Drop for FpuGuard {
75    #[inline]
76    fn drop(&mut self) {
77        #[cfg(target_arch = "x86_64")]
78        {
79            let ptr = self.area.0.as_ptr();
80            debug_assert_eq!(ptr as usize % 16, 0, "FXRSTOR area must be 16-byte aligned");
81            // SAFETY: the area was filled by `save()` and is still 16-byte
82            // aligned. FXRSTOR only reads from it.
83            unsafe {
84                core::arch::asm!("fxrstor [{ptr}]", ptr = in(reg) ptr, options(nostack));
85            }
86        }
87    }
88}