Skip to content

Commit 628b913

Browse files
authored
Merge pull request #335 from Freax13/cr2-read-returns-result
make `Cr2::read` return a result
2 parents 64a7ca7 + 5475d89 commit 628b913

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/registers/control.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ bitflags! {
160160
#[cfg(feature = "instructions")]
161161
mod x86_64 {
162162
use super::*;
163-
use crate::{instructions::tlb::Pcid, structures::paging::PhysFrame, PhysAddr, VirtAddr};
163+
use crate::{
164+
addr::VirtAddrNotValid, instructions::tlb::Pcid, structures::paging::PhysFrame, PhysAddr,
165+
VirtAddr,
166+
};
164167
#[cfg(feature = "inline_asm")]
165168
use core::arch::asm;
166169

@@ -251,9 +254,14 @@ mod x86_64 {
251254

252255
impl Cr2 {
253256
/// Read the current page fault linear address from the CR2 register.
257+
///
258+
/// # Errors
259+
///
260+
/// This method returns a [`VirtAddrNotValid`] error if the CR2 register contains a
261+
/// non-canonical address. Call [`Cr2::read_raw`] to handle such cases.
254262
#[inline]
255-
pub fn read() -> VirtAddr {
256-
VirtAddr::new(Self::read_raw())
263+
pub fn read() -> Result<VirtAddr, VirtAddrNotValid> {
264+
VirtAddr::try_new(Self::read_raw())
257265
}
258266

259267
/// Read the current page fault linear address from the CR2 register as a raw `u64`.

0 commit comments

Comments
 (0)