summaryrefslogtreecommitdiff
path: root/crates/windows-kernel-rs/src/intrin.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-01-03 03:20:12 -0800
committerFuwn <[email protected]>2022-01-03 03:20:12 -0800
commit85db2b507f3f69b32811c54a89d9ac7bbbc46121 (patch)
tree2efd66da452f8a6a2cc6c91584c925f237506ddf /crates/windows-kernel-rs/src/intrin.rs
downloaddriver-85db2b507f3f69b32811c54a89d9ac7bbbc46121.tar.xz
driver-85db2b507f3f69b32811c54a89d9ac7bbbc46121.zip
feat(driver): commit primer
Diffstat (limited to 'crates/windows-kernel-rs/src/intrin.rs')
-rw-r--r--crates/windows-kernel-rs/src/intrin.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/crates/windows-kernel-rs/src/intrin.rs b/crates/windows-kernel-rs/src/intrin.rs
new file mode 100644
index 0000000..f415f8d
--- /dev/null
+++ b/crates/windows-kernel-rs/src/intrin.rs
@@ -0,0 +1,25 @@
+use windows_kernel_sys::intrin::{read_msr_safe, write_msr_safe};
+
+use crate::error::{Error, IntoResult};
+
+/// Attempts to read the given model-specific register. Accessing an invalid
+/// model-specific register would normally result in a CPU exception. This
+/// function uses Structured Exception Handling (SEH) to safely catch CPU
+/// exceptions and to turn them into an [`Error`]. This prevents a hang.
+pub fn read_msr(register: u32) -> Result<u64, Error> {
+ let mut value = 0;
+
+ unsafe { read_msr_safe(register, &mut value) }.into_result()?;
+
+ Ok(value)
+}
+
+/// Attempts to write the given value to the given model-specific register.
+/// Accessing an invalid model-specific register would normally result in a CPU
+/// exception. This function uses Structured Handling (SEH) to safely catch CPU
+/// exceptions and to turn them into an [`Error`]. This prevents a hang.
+pub fn write_msr(register: u32, value: u64) -> Result<(), Error> {
+ unsafe { write_msr_safe(register, value) }.into_result()?;
+
+ Ok(())
+}