1use bitrs::layout;
6use regio::RwSafe;
7use regio::traits::{ReadReg, RwSafeReg};
8use regio::x86::{Cpuid, Msr};
9
10use super::ArchCapabilitiesMsr;
11use super::cpuid::EXTENDED_FEATURES_B;
12
13pub const IA32_TSX_CTRL: Msr<0x0000_0122, TsxControlMsr, RwSafe> = Msr::new();
17
18layout!({
19 pub struct TsxControlMsr(u64);
21 {
22 let __ @ 63..2;
23 let rtm_disable @ 1;
24 let tsx_cpuid_clear @ 0;
25 }
26});
27
28impl TsxControlMsr {
29 fn is_supported(cpuid: impl Cpuid, msr: impl ReadReg<ArchCapabilitiesMsr>) -> bool {
30 ArchCapabilitiesMsr::is_supported(cpuid) && msr.read().tsx_ctrl()
31 }
32}
33
34pub fn tsx_is_supported(cpuid: impl Cpuid) -> bool {
35 let features = cpuid.read(EXTENDED_FEATURES_B);
37 features.hle() || features.rtm()
38}
39
40pub fn disable_tsx(
42 cpuid: impl Cpuid,
43 arch_capabilities_msr: impl ReadReg<ArchCapabilitiesMsr>,
44 tsx_control_msr: impl RwSafeReg<TsxControlMsr>,
45) -> bool {
46 if !TsxControlMsr::is_supported(&cpuid, &arch_capabilities_msr) {
47 return false;
48 }
49
50 tsx_control_msr.modify(|val| {
51 val.set_rtm_disable(true).set_tsx_cpuid_clear(true);
52 });
53
54 true
55}