-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathfeatures.rs
More file actions
55 lines (48 loc) · 1.53 KB
/
Copy pathfeatures.rs
File metadata and controls
55 lines (48 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright (c) 2022-2023, 2026 SUSE LLC
//
// Authors: Joerg Roedel <jroedel@suse.de>
// Carlos López <clopez@suse.de>
use crate::platform::cpuid;
use cpufeature::CpuidFeature;
use cpufeature::CpuidRegister;
/// Raw EAX from CPUID leaf 0x8000_0008 — processor capacity parameters.
pub const PHYS_ADDR_SIZES: CpuidFeature = CpuidFeature {
leaf: 0x8000_0008,
subleaf: 0,
register: CpuidRegister::Eax,
shift: 0,
width: 32,
};
/// CPUID.4000_0001:EAX — Hyper-V interface signature ("Hv#1").
pub const HYPERV_INTERFACE: CpuidFeature = CpuidFeature {
leaf: 0x4000_0001,
subleaf: 0,
register: CpuidRegister::Eax,
shift: 0,
width: 32,
};
const HYPERV_INTERFACE_SIGNATURE: u32 = 0x3123_7648;
fn cpuid_field(feature: &CpuidFeature) -> Option<u32> {
cpuid(feature).map(|regs| {
let raw = feature
.register
.select(regs.eax, regs.ebx, regs.ecx, regs.edx);
let mask = ((1u64 << feature.width) as u32).wrapping_sub(1);
(raw >> feature.shift) & mask
})
}
/// Check if the given feature is enabled by comparing it to its expected
/// value.
pub fn cpu_has_feat(feature: &CpuidFeature) -> bool {
if core::ptr::eq(feature, &HYPERV_INTERFACE) {
cpuid_field(feature) == Some(HYPERV_INTERFACE_SIGNATURE)
} else {
cpuid_field(feature) == Some(1)
}
}
/// Gets the raw value of the given feature.
pub fn cpu_get_feat(feature: &CpuidFeature) -> u32 {
cpuid_field(feature).unwrap_or(0)
}