Skip to content

Commit

Permalink
Add SysnoSet
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwhite committed Jun 30, 2022
1 parent 9522ad2 commit 853d257
Show file tree
Hide file tree
Showing 3 changed files with 457 additions and 5 deletions.
35 changes: 32 additions & 3 deletions src/arch/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@ macro_rules! syscall_enum {
}

impl $Name {
/// A slice of all possible syscalls.
pub(crate) const ALL: &'static [Self] = &[
Self::$first_syscall,
$(
Self::$syscall,
)*
];

/// Constructs a new syscall from the given ID. If the ID does not
/// represent a valid syscall, returns `None`.
pub const fn new(id: usize) -> Option<Self> {
// TODO: Get rid of this huge match and use the SysnoSet for
// checking validity.
match id {
$(#[$first_inner])*
$first_num => Some(Self::$first_syscall),
Expand Down Expand Up @@ -61,10 +71,10 @@ macro_rules! syscall_enum {
return None;
}

let mut next_id = self.id() as usize + 1;
let mut next_id = self.id() + 1;

while next_id < Self::len() {
if let Some(next) = Self::new(next_id) {
while next_id < Self::last().id() {
if let Some(next) = Self::new(next_id as usize) {
return Some(next);
}

Expand All @@ -90,7 +100,19 @@ macro_rules! syscall_enum {
}

/// Returns the length of the syscall table, including any gaps.
#[deprecated = "Sysno::len() is misleading. Use Sysno::table_size() instead."]
pub const fn len() -> usize {
Self::table_size()
}

/// Returns the total number of valid syscalls.
pub const fn count() -> usize {
Self::ALL.len()
}

/// Returns the length of the syscall table, including any gaps.
/// This is not the same thing as the total number of syscalls.
pub const fn table_size() -> usize {
(Self::last().id() - Self::first().id()) as usize + 1
}

Expand Down Expand Up @@ -127,6 +149,13 @@ macro_rules! syscall_enum {
}
}

impl From<u32> for $Name {
fn from(id: u32) -> Self {
Self::new(id as usize)
.unwrap_or_else(|| panic!("invalid syscall: {}", id))
}
}

impl From<i32> for $Name {
fn from(id: i32) -> Self {
Self::new(id as usize)
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ mod macros;
mod arch;
mod args;
mod errno;
mod set;

pub use arch::Sysno;
pub use args::SyscallArgs;
pub use errno::{Errno, ErrnoSentinel};
pub use set::SysnoSet;

pub mod raw {
//! Exposes raw syscalls that simply return a `usize` instead of a `Result`.
Expand Down Expand Up @@ -263,7 +265,7 @@ mod tests {

#[test]
fn test_syscall_len() {
assert!(Sysno::len() > 300);
assert!(Sysno::len() < 1000);
assert!(Sysno::table_size() > 300);
assert!(Sysno::table_size() < 1000);
}
}
Loading

0 comments on commit 853d257

Please sign in to comment.