minimal_rt/arch/x86_64/
msr.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! MSR support for boot shim.
5
6use core::arch::asm;
7
8/// Write a value to an MSR.
9///
10/// # Safety
11/// The caller must guarantee that this is a safe operation, based on the
12/// behavior of the specified MSR.
13#[inline]
14pub unsafe fn write_msr(msr: u32, val: u64) {
15    let low = val as u32;
16    let high = (val >> 32) as u32;
17
18    // SAFETY: Using the `wrmsr` instruction as described in the processor
19    // vendors Software Development Manuals.
20    unsafe {
21        asm!(r#"
22        wrmsr
23        "#,
24        in("eax") low,
25        in("edx") high,
26        in("ecx") msr);
27    }
28}
29
30/// Reads a value from an MSR.
31///
32/// # Safety
33/// The caller must guarantee that this is a safe operation, based on the
34/// behavior of the specified MSR.
35#[inline]
36pub unsafe fn read_msr(msr: u32) -> u64 {
37    let mut low: u32;
38    let mut high: u32;
39
40    // SAFETY: Using the `rdmsr` instruction as described in the processor
41    // vendors Software Development Manuals.
42    unsafe {
43        asm!(r#"
44        rdmsr
45        "#,
46        out("eax") low,
47        out("edx") high,
48        in("ecx") msr);
49    }
50
51    ((high as u64) << 32) | low as u64
52}