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

Use thiserror derive macro for MapError. #63

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### New features

- `MapError` now implements `core::error::Error`.

## 0.8.0

### Breaking changes
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ categories = ["embedded", "no-std", "hardware-support"]

[dependencies]
bitflags = "2.6.0"
thiserror = { version = "2.0.3", default-features = false }
zerocopy = { version = "0.8.2", features = ["derive"], optional = true }

[features]
Expand Down
33 changes: 8 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,53 +59,36 @@ extern crate alloc;

#[cfg(target_arch = "aarch64")]
use core::arch::asm;
use core::fmt::{self, Display, Formatter};
use paging::{
Attributes, Constraints, Descriptor, MemoryRegion, PhysicalAddress, RootTable, Translation,
TranslationRegime, VaRange, VirtualAddress,
};
use thiserror::Error;

/// An error attempting to map some range in the page table.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum MapError {
/// The address requested to be mapped was out of the range supported by the page table
/// configuration.
#[error("Virtual address {0} out of range")]
AddressRange(VirtualAddress),
/// The address requested to be mapped was not valid for the mapping in use.
#[error("Invalid virtual address {0} for mapping")]
InvalidVirtualAddress(VirtualAddress),
/// The end of the memory region is before the start.
#[error("End of memory region {0} is before start.")]
RegionBackwards(MemoryRegion),
/// There was an error while updating a page table entry.
#[error("Error updating page table entry {0:?}")]
PteUpdateFault(Descriptor),
/// The requested flags are not supported for this mapping
#[error("Flags {0:?} unsupported for mapping.")]
InvalidFlags(Attributes),
/// Updating the range violates break-before-make rules and the mapping is live
#[error("Cannot remap region {0} while translation is live.")]
BreakBeforeMakeViolation(MemoryRegion),
}

impl Display for MapError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::AddressRange(va) => write!(f, "Virtual address {} out of range", va),
Self::InvalidVirtualAddress(va) => {
write!(f, "Invalid virtual address {} for mapping", va)
}
Self::RegionBackwards(region) => {
write!(f, "End of memory region {} is before start.", region)
}
Self::PteUpdateFault(desc) => {
write!(f, "Error updating page table entry {:?}", desc)
}
Self::InvalidFlags(flags) => {
write!(f, "Flags {flags:?} unsupported for mapping.")
}
Self::BreakBeforeMakeViolation(region) => {
write!(f, "Cannot remap region {region} while translation is live.")
}
}
}
}

/// Manages a level 1 page table and associated state.
///
/// Mappings should be added with [`map_range`](Self::map_range) before calling
Expand Down
Loading