-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement core::iter::Step
for VirtAddr
and Page
#342
Changes from 2 commits
3680907
6f891df
7916566
3e1ed63
e2b5d01
1602cfd
2909487
9bef8f0
fd8f2f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
//! Physical and virtual addresses manipulation | ||
|
||
#[cfg(feature = "step_trait")] | ||
use core::convert::TryFrom; | ||
use core::fmt; | ||
#[cfg(feature = "step_trait")] | ||
use core::iter::Step; | ||
use core::ops::{Add, AddAssign, Sub, SubAssign}; | ||
|
||
use crate::structures::paging::page_table::PageTableLevel; | ||
|
@@ -322,6 +326,44 @@ impl Sub<VirtAddr> for VirtAddr { | |
} | ||
} | ||
|
||
#[cfg(feature = "step_trait")] | ||
impl Step for VirtAddr { | ||
fn steps_between(start: &Self, end: &Self) -> Option<usize> { | ||
let mut steps = end.0.checked_sub(start.0)?; | ||
|
||
// Check if we jumped the gap. | ||
if end.0.get_bit(47) && !start.0.get_bit(47) { | ||
steps -= 0xffff_0000_0000_0000; | ||
} | ||
|
||
usize::try_from(steps).ok() | ||
} | ||
|
||
fn forward_checked(start: Self, count: usize) -> Option<Self> { | ||
let offset = u64::try_from(count).ok()?; | ||
let mut addr = start.0.checked_add(offset)?; | ||
|
||
// Jump the gap by sign extending the 47th bit. | ||
if addr.get_bits(47..) == 0x1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point. Note that we can also get a larger value ( |
||
addr.set_bits(47.., 0x1ffff); | ||
} | ||
|
||
Some(Self(addr)) | ||
} | ||
|
||
fn backward_checked(start: Self, count: usize) -> Option<Self> { | ||
let offset = u64::try_from(count).ok()?; | ||
let mut addr = start.0.checked_sub(offset)?; | ||
|
||
// Jump the gap by sign extending the 47th bit. | ||
if addr.get_bits(47..) == 0x1fffe { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same concern as above: If count is too large, this get_bits might return something smaller than 0x1fffe. |
||
addr.set_bits(47.., 0); | ||
} | ||
|
||
Some(Self(addr)) | ||
} | ||
} | ||
|
||
/// A passed `u64` was not a valid physical address. | ||
/// | ||
/// This means that bits 52 to 64 were not all null. | ||
|
@@ -577,6 +619,76 @@ mod tests { | |
assert_eq!(VirtAddr::new_truncate(123 << 47), VirtAddr(0xfffff << 47)); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "step_trait")] | ||
fn virtaddr_step() { | ||
assert_eq!(Step::forward(VirtAddr(0), 0), VirtAddr(0)); | ||
assert_eq!(Step::forward(VirtAddr(0), 1), VirtAddr(1)); | ||
assert_eq!( | ||
Step::forward(VirtAddr(0x7fff_ffff_ffff), 1), | ||
VirtAddr(0xffff_8000_0000_0000) | ||
); | ||
assert_eq!( | ||
Step::forward(VirtAddr(0xffff_8000_0000_0000), 1), | ||
VirtAddr(0xffff_8000_0000_0001) | ||
); | ||
assert_eq!( | ||
Step::forward_checked(VirtAddr(0xffff_ffff_ffff_ffff), 1), | ||
None | ||
); | ||
|
||
assert_eq!(Step::backward(VirtAddr(0), 0), VirtAddr(0)); | ||
assert_eq!(Step::backward_checked(VirtAddr(0), 1), None); | ||
assert_eq!(Step::backward(VirtAddr(1), 1), VirtAddr(0)); | ||
assert_eq!( | ||
Step::backward(VirtAddr(0xffff_8000_0000_0000), 1), | ||
VirtAddr(0x7fff_ffff_ffff) | ||
); | ||
assert_eq!( | ||
Step::backward(VirtAddr(0xffff_8000_0000_0001), 1), | ||
VirtAddr(0xffff_8000_0000_0000) | ||
); | ||
|
||
assert_eq!(Step::steps_between(&VirtAddr(0), &VirtAddr(0)), Some(0)); | ||
assert_eq!(Step::steps_between(&VirtAddr(0), &VirtAddr(1)), Some(1)); | ||
assert_eq!(Step::steps_between(&VirtAddr(1), &VirtAddr(0)), None); | ||
assert_eq!( | ||
Step::steps_between( | ||
&VirtAddr(0x7fff_ffff_ffff), | ||
&VirtAddr(0xffff_8000_0000_0000) | ||
), | ||
Some(1) | ||
); | ||
assert_eq!( | ||
Step::steps_between( | ||
&VirtAddr(0xffff_8000_0000_0000), | ||
&VirtAddr(0x7fff_ffff_ffff) | ||
), | ||
None | ||
); | ||
assert_eq!( | ||
Step::steps_between( | ||
&VirtAddr(0xffff_8000_0000_0000), | ||
&VirtAddr(0xffff_8000_0000_0000) | ||
), | ||
Some(0) | ||
); | ||
assert_eq!( | ||
Step::steps_between( | ||
&VirtAddr(0xffff_8000_0000_0000), | ||
&VirtAddr(0xffff_8000_0000_0001) | ||
), | ||
Some(1) | ||
); | ||
assert_eq!( | ||
Step::steps_between( | ||
&VirtAddr(0xffff_8000_0000_0001), | ||
&VirtAddr(0xffff_8000_0000_0000) | ||
), | ||
None | ||
); | ||
} | ||
|
||
#[test] | ||
pub fn test_align_up() { | ||
// align 1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add an assertion here that
steps
is larger than0xffff_0000_0000_0000
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming that the address is canonical
steps
is always equal to or larger than0xffff_0000_0000_0000
, is it not? I would be fine with adding a debug assertion just to make sure.