// Copyright (c) 2025 shibedrill
// SPDX-License-Identifier: MIT

use intbits::Bits;
use x86_64::registers::control::EferFlags;

use crate::arch::x86_64::cpuid::*;

pub fn amd_v_supported() -> bool {
    match ProcessorVendor::try_get_current() {
        Ok(vendor) => {
            if vendor == ProcessorVendor::AMD {
                match CPUID.get_extended_processor_and_feature_identifiers() {
                    Some(features) => features.has_svm(),
                    _ => false,
                }
            } else {
                false
            }
        }
        Err(_) => false,
    }
}

#[allow(dead_code)]
pub fn enable_svm() {
    let mut efer = x86_64::registers::model_specific::Efer::read().bits();
    efer.set_bit(12, true);
    // I'm not sure why this is here. But clearly there was a reason for it.
    // So now I'm scared to touch it...
    assert!(efer.bit(12));
    unsafe { x86_64::registers::model_specific::Efer::write(EferFlags::from_bits_retain(efer)) };
}
