Skip to main content

vck_common/
cpu.rs

1// SPDX-FileCopyrightText: 2026 JC-Lab <joseph@jc-lab.net>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! CPU feature detection shared by the kernel driver and the UEFI loader.
6
7/// Returns true if the CPU advertises AES-NI (CPUID.01H:ECX.AESNI[bit 25]).
8///
9/// This is the same hardware bit the `aes` crate's runtime detection uses to
10/// select its AES-NI backend; we query it independently only to log support
11/// status at startup.
12#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
13pub fn has_aes_ni() -> bool {
14    #[cfg(target_arch = "x86")]
15    use core::arch::x86::__cpuid;
16    #[cfg(target_arch = "x86_64")]
17    use core::arch::x86_64::__cpuid;
18
19    // CPUID leaf 1, ECX bit 25 = AES-NI.
20    const ECX_AESNI: u32 = 1 << 25;
21    // SAFETY: leaf 1 is supported on every x86 CPU that runs 64-bit Windows/UEFI.
22    // `#[allow(unused_unsafe)]`: `__cpuid` is `unsafe` on some toolchains and
23    // safe on others; keep the block for the former without warning on the latter.
24    #[allow(unused_unsafe)]
25    let info = unsafe { __cpuid(1) };
26    info.ecx & ECX_AESNI != 0
27}
28
29/// Non-x86 fallback: no AES-NI.
30#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
31pub fn has_aes_ni() -> bool {
32    false
33}