|
| 1 | +// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use vmm_sys_util::syscall::SyscallReturnCode; |
| 5 | + |
| 6 | +use crate::arch::x86_64::gen::arch_prctl; |
| 7 | + |
| 8 | +const INTEL_AMX_MASK: u64 = 1u64 << arch_prctl::ARCH_XCOMP_TILEDATA; |
| 9 | + |
| 10 | +/// Errors assocaited with x86_64's dynamic XSAVE state features. |
| 11 | +#[derive(Debug, thiserror::Error, displaydoc::Display)] |
| 12 | +pub enum XstateError { |
| 13 | + /// Failed to get supported XSTATE features: {0} |
| 14 | + GetSupportedXstateFeatures(std::io::Error), |
| 15 | + /// Failed to request permission for XSTATE feature ({0}): {1} |
| 16 | + RequestXstateFeaturePermission(u32, std::io::Error), |
| 17 | +} |
| 18 | + |
| 19 | +/// Request permission for all dynamic XSTATE features. |
| 20 | +/// |
| 21 | +/// Some XSTATE features are not permitted by default, because they may require a larger area to |
| 22 | +/// save their states than the tranditional 4096-byte area. Instead, the permission for them can be |
| 23 | +/// requested via arch_prctl(). |
| 24 | +/// https://github.com/torvalds/linux/blob/master/Documentation/arch/x86/xstate.rst |
| 25 | +/// |
| 26 | +/// Firecracker requests permission for them by default if available in order to retrieve the |
| 27 | +/// full supported feature set via KVM_GET_SUPPORTED_CPUID. |
| 28 | +/// https://docs.kernel.org/virt/kvm/api.html#kvm-get-supported-cpuid |
| 29 | +/// |
| 30 | +/// Note that requested features can be masked by a CPU template. |
| 31 | +pub fn request_dynamic_xstate_features() -> Result<(), XstateError> { |
| 32 | + let supported_xfeatures = |
| 33 | + match get_supported_xfeatures().map_err(XstateError::GetSupportedXstateFeatures)? { |
| 34 | + Some(supported_xfeatures) => supported_xfeatures, |
| 35 | + // Exit early if dynamic XSTATE feature enabling is not supported on the kernel. |
| 36 | + None => return Ok(()), |
| 37 | + }; |
| 38 | + |
| 39 | + // Intel AMX's TILEDATA |
| 40 | + // |
| 41 | + // Unless requested, on kernels prior to v6.4, KVM_GET_SUPPORTED_CPUID returns an |
| 42 | + // inconsistent state where TILECFG is set but TILEDATA isn't. Such a half-enabled state |
| 43 | + // causes guest crash during boot because a guest calls XSETBV instruction with all |
| 44 | + // XSAVE feature bits enumerated on CPUID and XSETBV only accepts either of both Intel |
| 45 | + // AMX bits enabled or disabled; otherwise resulting in general protection fault. |
| 46 | + if supported_xfeatures & INTEL_AMX_MASK == INTEL_AMX_MASK { |
| 47 | + request_xfeature_permission(arch_prctl::ARCH_XCOMP_TILEDATA).map_err(|err| { |
| 48 | + XstateError::RequestXstateFeaturePermission(arch_prctl::ARCH_XCOMP_TILEDATA, err) |
| 49 | + })?; |
| 50 | + } |
| 51 | + |
| 52 | + Ok(()) |
| 53 | +} |
| 54 | + |
| 55 | +/// Get supported XSTATE features |
| 56 | +/// |
| 57 | +/// Returns Ok(None) if dynamic XSTATE feature enabling is not supported. |
| 58 | +fn get_supported_xfeatures() -> Result<Option<u64>, std::io::Error> { |
| 59 | + let mut supported_xfeatures: u64 = 0; |
| 60 | + |
| 61 | + // SAFETY: Safe because the third input (`addr`) is a valid `c_ulong` pointer. |
| 62 | + // https://man7.org/linux/man-pages/man2/arch_prctl.2.html |
| 63 | + match SyscallReturnCode(unsafe { |
| 64 | + libc::syscall( |
| 65 | + libc::SYS_arch_prctl, |
| 66 | + arch_prctl::ARCH_GET_XCOMP_SUPP, |
| 67 | + &mut supported_xfeatures as *mut libc::c_ulong, |
| 68 | + ) |
| 69 | + }) |
| 70 | + .into_empty_result() |
| 71 | + { |
| 72 | + Ok(()) => Ok(Some(supported_xfeatures)), |
| 73 | + // EINVAL is returned if the dynamic XSTATE feature enabling is not supported (e.g. kernel |
| 74 | + // version prior to v5.17). |
| 75 | + // https://github.com/torvalds/linux/commit/980fe2fddcff21937c93532b4597c8ea450346c1 |
| 76 | + Err(err) if err.raw_os_error() == Some(libc::EINVAL) => Ok(None), |
| 77 | + Err(err) => Err(err), |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// Request permission for a dynamic XSTATE feature. |
| 82 | +/// |
| 83 | +/// This should be called after `get_supported_xfeatures()` that also checks that dynamic XSTATE |
| 84 | +/// feature enabling is supported. |
| 85 | +fn request_xfeature_permission(xfeature: u32) -> Result<(), std::io::Error> { |
| 86 | + // SAFETY: Safe because the third input (`addr`) is a valid `c_ulong` value. |
| 87 | + // https://man7.org/linux/man-pages/man2/arch_prctl.2.html |
| 88 | + SyscallReturnCode(unsafe { |
| 89 | + libc::syscall( |
| 90 | + libc::SYS_arch_prctl, |
| 91 | + arch_prctl::ARCH_REQ_XCOMP_GUEST_PERM as libc::c_ulong, |
| 92 | + xfeature as libc::c_ulong, |
| 93 | + ) |
| 94 | + }) |
| 95 | + .into_empty_result() |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + |
| 102 | + // Get permitted XSTATE features. |
| 103 | + fn get_permitted_xstate_features() -> Result<u64, std::io::Error> { |
| 104 | + let mut permitted_xfeatures: u64 = 0; |
| 105 | + // SAFETY: Safe because the third input (`addr`) is a valid `c_ulong` pointer. |
| 106 | + match SyscallReturnCode(unsafe { |
| 107 | + libc::syscall( |
| 108 | + libc::SYS_arch_prctl, |
| 109 | + arch_prctl::ARCH_GET_XCOMP_GUEST_PERM, |
| 110 | + &mut permitted_xfeatures as *mut libc::c_ulong, |
| 111 | + ) |
| 112 | + }) |
| 113 | + .into_empty_result() |
| 114 | + { |
| 115 | + Ok(()) => Ok(permitted_xfeatures), |
| 116 | + Err(err) => Err(err), |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_request_xstate_feature_permission() { |
| 122 | + request_dynamic_xstate_features().unwrap(); |
| 123 | + |
| 124 | + let supported_xfeatures = match get_supported_xfeatures().unwrap() { |
| 125 | + Some(supported_xfeatures) => supported_xfeatures, |
| 126 | + // Nothing to test if dynamic XSTATE feature enabling is not supported on the kernel. |
| 127 | + None => return, |
| 128 | + }; |
| 129 | + |
| 130 | + // Check each dynamic feature is enabled. (currently only Intel AMX TILEDATA) |
| 131 | + if supported_xfeatures & INTEL_AMX_MASK == INTEL_AMX_MASK { |
| 132 | + let permitted_xfeatures = get_permitted_xstate_features().unwrap(); |
| 133 | + assert_eq!(permitted_xfeatures & INTEL_AMX_MASK, INTEL_AMX_MASK); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments