Skip to content

Commit dd0b377

Browse files
committed
Do wrmsr bit-twiddling in Rust
Signed-off-by: Joe Richey <[email protected]>
1 parent 24b9a45 commit dd0b377

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

src/asm/asm.s

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,19 @@ _x86_64_asm_write_cr4:
224224
.global _x86_64_asm_rdmsr
225225
.p2align 4
226226
_x86_64_asm_rdmsr:
227-
mov %edi,%ecx
227+
mov %edi, %ecx # First param is the MSR number
228228
rdmsr
229-
shl $0x20,%rdx # shift edx to upper 32bit
230-
mov %eax,%eax # clear upper 32bit of rax
231-
or %rdx,%rax # or with rdx
229+
shl $32, %rdx # shift edx to upper 32bit
230+
mov %eax, %eax # clear upper 32bit of rax
231+
or %rdx, %rax # or with rdx
232232
retq
233233

234234
.global _x86_64_asm_wrmsr
235235
.p2align 4
236236
_x86_64_asm_wrmsr:
237-
mov %edi,%ecx
238-
movq %rsi,%rax
239-
movq %rsi,%rdx
240-
shr $0x20,%rdx
241-
wrmsr
237+
movl %edi, %ecx # First param is the MSR number
238+
movl %esi, %eax # Second param is the low 32-bits
239+
wrmsr # Third param (high 32-bits) is already in %edx
242240
retq
243241

244242
.global _x86_64_asm_hlt

src/asm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ extern "C" {
214214
any(target_env = "gnu", target_env = "musl"),
215215
link_name = "_x86_64_asm_wrmsr"
216216
)]
217-
pub(crate) fn x86_64_asm_wrmsr(msr: u32, value: u64);
217+
pub(crate) fn x86_64_asm_wrmsr(msr: u32, low: u32, high: u32);
218218

219219
#[cfg_attr(
220220
any(target_env = "gnu", target_env = "musl"),

src/registers/model_specific.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,19 @@ mod x86_64 {
142142
/// effects.
143143
#[inline]
144144
pub unsafe fn write(&mut self, value: u64) {
145+
let low = value as u32;
146+
let high = (value >> 32) as u32;
147+
145148
#[cfg(feature = "inline_asm")]
146-
{
147-
let low = value as u32;
148-
let high = (value >> 32) as u32;
149-
asm!(
150-
"wrmsr",
151-
in("ecx") self.0,
152-
in("eax") low, in("edx") high,
153-
options(nostack, preserves_flags),
154-
);
155-
}
149+
asm!(
150+
"wrmsr",
151+
in("ecx") self.0,
152+
in("eax") low, in("edx") high,
153+
options(nostack, preserves_flags),
154+
);
156155

157156
#[cfg(not(feature = "inline_asm"))]
158-
crate::asm::x86_64_asm_wrmsr(self.0, value);
157+
crate::asm::x86_64_asm_wrmsr(self.0, low, high);
159158
}
160159
}
161160

0 commit comments

Comments
 (0)