Skip to content
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

feat(hal-core): address type improvements #64

Merged
merged 17 commits into from
Jan 30, 2020
Merged

feat(hal-core): address type improvements #64

merged 17 commits into from
Jan 30, 2020

Conversation

hawkw
Copy link
Owner

@hawkw hawkw commented Jan 20, 2020

This branch makes the following changes:

  • feat(hal-core): implement Address::{align_up, align_down}

    Add generic default implementations of align_up and align_down to
    all types implementing Address. These are implemented using a new
    required from_usize constructor.

  • feat(hal-core): Address::as_ptr returns typed mut ptr

    As discussed in [this comment][1], as_ptr now returns a *mut T
    rather than a *const (). It will now assert that the address is
    correctly aligned for a T, using a new is_aligned_for::<T>
    convenience method.

  • feat(hal-core): validate/canonicalize x86_64 addrs

    Add #[cfg(target_arch = "x86_64")] implementations of
    Address::from_usize for PAddr/VAddr that assert the address
    is sign-extended/canonicalized when compiled in debug mode.
    Additionally, add from_usize_checked constructors that return
    Result and always validate that the constructed addresses are
    valid.

  • feat(hal-core): add inherent versions of address trait methods

@hawkw hawkw added kind/enhancement New feature or request area/hal Related to the hardware abstraction layer labels Jan 20, 2020
@hawkw hawkw requested review from mystor and iximeow January 20, 2020 19:47
@hawkw hawkw self-assigned this Jan 20, 2020
Copy link
Collaborator

@mystor mystor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to have some small unit tests checking that the behaviour of these methods is what we expect.

hal-core/src/addr.rs Outdated Show resolved Hide resolved
hal-core/src/addr.rs Outdated Show resolved Hide resolved
hal-core/src/addr.rs Outdated Show resolved Hide resolved
hal-core/src/addr.rs Outdated Show resolved Hide resolved
@hawkw hawkw requested a review from mystor January 20, 2020 22:41
Signed-off-by: Eliza Weisman <[email protected]>
if u & mask == 0 {
return self;
}
let aligned = (u | mask) + 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this more efficient than self.align_down(align) + align?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk, we'd have to test it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but probably very slightly)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fair. Let's stick with this.

hal-core/src/addr.rs Outdated Show resolved Hide resolved
hal-core/src/addr.rs Outdated Show resolved Hide resolved
@hawkw hawkw requested a review from mystor January 23, 2020 01:14
@hawkw
Copy link
Owner Author

hawkw commented Jan 23, 2020

changed the from_usize impls to debug_assert validity, and let the CPU fault in release mode. we probably want separate constructors that do make the address valid?

Copy link
Collaborator

@mystor mystor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, left a couple of comments :-)

#[inline]
#[cfg(target_arch = "x86_64")]
fn from_usize(u: usize) -> Self {
debug_assert!(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a debug_assert_eq!?

/// # Panics
///
/// - If `align` is not a power of two.
fn align_down<A: Into<usize>>(self, align: A) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can totally do this in a different patch, but it might be nice to have align_up_for and align_down_for helper methods like the is_aligned_for method.

Comment on lines 175 to 178
debug_assert!(
u & MASK == 0,
"x86_64 physical addresses may not have the 12 most significant bits set!"
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth #[cfg(target_arch = "x86_64")]-ing only this assertion, and keeping the rest of the from_usize impl common?

///
/// - If `self` is not aligned for a `T`-typed value.
fn as_ptr<T>(self) -> *mut T {
assert!(self.is_aligned_for::<T>());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we need this restriction for rust, even though there's no aligned read requirement on x64?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's correct, will add a note

Comment on lines 327 to 344
// #[cfg(target_arch = "x86_64")]
// #[test]
// fn sign_extend_vaddr() {
// let actual = VAddr::from_usize(123 | (1 << 47)).as_usize();
// let expected = (0xFFFFF << 47) | 123;
// assert_eq!(
// actual, expected,
// "\n left: `{:064b}`\n right: `{:064b}`",
// actual, expected
// );
// let actual = VAddr::from_usize(123 | (1010 << 47)).as_usize();
// let expected = 123;
// assert_eq!(
// actual, expected,
// "\n left: `{:064b}`\n right: `{:064b}`",
// actual, expected
// );
// }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to remove this.

Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
This way, they can be used even when the Address trait is not in scope,
but Address allows writing code that's generic over PAddr/VAddr.

Signed-off-by: Eliza Weisman <[email protected]>
@hawkw hawkw merged commit 46f2268 into master Jan 30, 2020
@hawkw hawkw deleted the eliza/addr/tweaks branch January 30, 2020 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hal Related to the hardware abstraction layer kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants