-
Notifications
You must be signed in to change notification settings - Fork 79
MapPerms: Consistency between map and iomap #577
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
Open
cazb2
wants to merge
1
commit into
seL4:main
Choose a base branch
from
au-ts:callumb/improve_permission_repr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−49
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,34 +21,80 @@ use crate::util::round_up; | |
| use crate::util::str_to_bool; | ||
| use crate::{Config, PageSize}; | ||
|
|
||
| #[repr(u8)] | ||
| pub enum SysMapPerms { | ||
| Read = 1, | ||
| Write = 2, | ||
| Execute = 4, | ||
| // We do not include attributes (execute/cached) here since seL4 does not treat them like vm_rights. | ||
| // Note on seL4 you can't mint a write only frame. However you can request a write only mapping, for the IOMMU on x86. | ||
| #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
| pub enum FrameRights { | ||
| Read, | ||
| Write, | ||
| ReadWrite, | ||
| None, | ||
| } | ||
|
|
||
| impl FrameRights { | ||
| fn from_bools(read: bool, write: bool) -> Self { | ||
| match (read, write) { | ||
| (true, false) => Self::Read, | ||
| (false, true) => Self::Write, | ||
| (true, true) => Self::ReadWrite, | ||
| (false, false) => Self::None, | ||
| } | ||
| } | ||
| pub fn read(self) -> bool { | ||
| matches!(self, Self::Read | Self::ReadWrite) | ||
| } | ||
| pub fn write(self) -> bool { | ||
| matches!(self, Self::Write | Self::ReadWrite) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
| pub struct SysMapPerms { | ||
| rights: FrameRights, | ||
| execute: bool, | ||
| } | ||
|
|
||
| pub enum SysMapPermsParseError { | ||
| InvalidChar, | ||
| WriteOnly, | ||
| } | ||
|
|
||
| impl SysMapPerms { | ||
| fn from_str(s: &str) -> Result<u8, ()> { | ||
| let mut perms = 0; | ||
| fn from_str(s: &str) -> Result<Self, SysMapPermsParseError> { | ||
| let mut read = false; | ||
| let mut write = false; | ||
| let mut execute = false; | ||
| for c in s.chars() { | ||
| match c { | ||
| 'r' => perms |= SysMapPerms::Read as u8, | ||
| 'w' => perms |= SysMapPerms::Write as u8, | ||
| 'x' => perms |= SysMapPerms::Execute as u8, | ||
| _ => return Err(()), | ||
| 'r' => read = true, | ||
| 'w' => write = true, | ||
| 'x' => execute = true, | ||
| _ => return Err(SysMapPermsParseError::InvalidChar), | ||
| } | ||
| } | ||
| let rights = match FrameRights::from_bools(read, write) { | ||
| FrameRights::Write => return Err(SysMapPermsParseError::WriteOnly), | ||
| frame_rights => frame_rights, | ||
| }; | ||
|
|
||
| Ok(perms) | ||
| Ok(Self { rights, execute }) | ||
| } | ||
| pub fn read(self) -> bool { | ||
| self.rights.read() | ||
| } | ||
| pub fn write(self) -> bool { | ||
| self.rights.write() | ||
| } | ||
| pub fn execute(self) -> bool { | ||
| self.execute | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct SysMap { | ||
| pub mr: String, | ||
| pub vaddr: u64, | ||
| pub perms: u8, | ||
| pub perms: SysMapPerms, | ||
| pub cached: bool, | ||
| /// Location in the parsed SDF file. Because this struct is | ||
| /// used in a non-XML context, we make the position optional. | ||
|
|
@@ -94,15 +140,15 @@ impl Map for SysMap { | |
| } | ||
|
|
||
| fn read(&self) -> bool { | ||
| self.perms & SysMapPerms::Read as u8 != 0 | ||
| self.perms.read() | ||
| } | ||
|
|
||
| fn write(&self) -> bool { | ||
| self.perms & SysMapPerms::Write as u8 != 0 | ||
| self.perms.write() | ||
| } | ||
|
|
||
| fn execute(&self) -> bool { | ||
| self.perms & SysMapPerms::Execute as u8 != 0 | ||
| self.perms.execute() | ||
| } | ||
|
|
||
| fn cached(&self) -> bool { | ||
|
|
@@ -136,15 +182,15 @@ impl Map for SysIOMap { | |
| } | ||
|
|
||
| fn read(&self) -> bool { | ||
| matches!(self.perms, SysIOMapPerms::Read | SysIOMapPerms::ReadWrite) | ||
| self.perms.read() | ||
| } | ||
|
|
||
| fn write(&self) -> bool { | ||
| matches!(self.perms, SysIOMapPerms::Write | SysIOMapPerms::ReadWrite) | ||
| self.perms.write() | ||
| } | ||
|
|
||
| fn execute(&self) -> bool { | ||
| false | ||
| self.perms.execute() | ||
| } | ||
|
|
||
| fn cached(&self) -> bool { | ||
|
|
@@ -214,31 +260,34 @@ impl SysMemoryRegion { | |
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
| pub enum SysIOMapPerms { | ||
| Read, | ||
| Write, | ||
| ReadWrite, | ||
| } | ||
| pub struct SysIOMapPerms(FrameRights); | ||
|
|
||
| impl SysIOMapPerms { | ||
| fn from_str(s: &str) -> Result<Self, ()> { | ||
| fn from_str(s: &str) -> Result<Self, String> { | ||
| let mut read = false; | ||
| let mut write = false; | ||
|
|
||
| for c in s.chars() { | ||
| match c { | ||
| 'r' => read = true, | ||
| 'w' => write = true, | ||
| _ => return Err(()), | ||
| _ => return Err(format!("Invalid character in string {s}")), | ||
| } | ||
| } | ||
|
|
||
| match (read, write) { | ||
| (true, true) => Ok(SysIOMapPerms::ReadWrite), | ||
| (true, false) => Ok(SysIOMapPerms::Read), | ||
| (false, true) => Ok(SysIOMapPerms::Write), | ||
| (false, false) => Err(()), | ||
| } | ||
| let frame_rights = match FrameRights::from_bools(read, write) { | ||
| FrameRights::None => return Err("Invalid frame right for IOMap".into()), | ||
| frame_rights => frame_rights, | ||
| }; | ||
| Ok(SysIOMapPerms(frame_rights)) | ||
| } | ||
| pub fn read(self) -> bool { | ||
|
Collaborator
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. Spacing between functions |
||
| self.0.read() | ||
| } | ||
| pub fn write(self) -> bool { | ||
| self.0.write() | ||
| } | ||
| pub fn execute(self) -> bool { | ||
| false | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -282,7 +331,15 @@ impl SysMap { | |
| let perms = if let Some(xml_perms) = node.attribute("perms") { | ||
| match SysMapPerms::from_str(xml_perms) { | ||
| Ok(parsed_perms) => parsed_perms, | ||
| Err(()) => { | ||
| // On all architectures, the kernel does not allow write-only mappings | ||
| Err(SysMapPermsParseError::WriteOnly) => { | ||
| return Err(value_error( | ||
| xml_sdf, | ||
| node, | ||
| "perms must not be 'w', write-only mappings are not allowed".to_string(), | ||
| )); | ||
| } | ||
| Err(_) => { | ||
| return Err(value_error( | ||
| xml_sdf, | ||
| node, | ||
|
|
@@ -292,18 +349,12 @@ impl SysMap { | |
| } | ||
| } else { | ||
| // Default to read-write | ||
| SysMapPerms::Read as u8 | SysMapPerms::Write as u8 | ||
| SysMapPerms { | ||
| rights: FrameRights::ReadWrite, | ||
| execute: false, | ||
| } | ||
| }; | ||
|
|
||
| // On all architectures, the kernel does not allow write-only mappings | ||
| if perms == SysMapPerms::Write as u8 { | ||
| return Err(value_error( | ||
| xml_sdf, | ||
| node, | ||
| "perms must not be 'w', write-only mappings are not allowed".to_string(), | ||
| )); | ||
| } | ||
|
|
||
| let cached = if let Some(xml_cached) = node.attribute("cached") { | ||
| match str_to_bool(xml_cached) { | ||
| Some(val) => val, | ||
|
|
@@ -360,7 +411,7 @@ impl SysIOMap { | |
| let perms = if let Some(xml_perms) = node.attribute("perms") { | ||
| match SysIOMapPerms::from_str(xml_perms) { | ||
| Ok(parsed_perms) => parsed_perms, | ||
| Err(()) => { | ||
| Err(_) => { | ||
| return Err(value_error( | ||
| xml_sdf, | ||
| node, | ||
|
|
@@ -371,7 +422,7 @@ impl SysIOMap { | |
| } | ||
| } else { | ||
| // Default to read-write | ||
| SysIOMapPerms::ReadWrite | ||
| SysIOMapPerms(FrameRights::ReadWrite) | ||
| }; | ||
|
|
||
| Ok(SysIOMap { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead:
map.perms().read? Since type ofpermsis the same in both cases? Then don't need these extra methods? Or does that get ugly?