Extract TRights into crate jinux_rights

This commit is contained in:
Yuke Peng 2023-05-30 04:09:20 -07:00 committed by Tate, Hongliang Tian
parent 3471843793
commit 0c9495b726
29 changed files with 122 additions and 70 deletions

View File

@ -0,0 +1,14 @@
[package]
name = "jinux-rights"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
typeflags = { path = "../typeflags" }
typeflags-util = { path = "../typeflags-util" }
bitflags = "1.3"
jinux-rights-proc = { path = "../jinux-rights-proc" }
[features]

View File

@ -1,7 +1,8 @@
use bitflags::bitflags;
use typeflags::typeflags;
#![no_std]
bitflags! {
use core::ops::{Deref, DerefMut};
bitflags::bitflags! {
/// Value-based access rights.
///
/// These access rights are provided to cover a wide range of use cases.
@ -24,7 +25,7 @@ bitflags! {
}
}
typeflags! {
typeflags::typeflags! {
/// Type-based access rights.
///
/// Similar to value-based access rights (`Rights`), but represented in
@ -46,6 +47,41 @@ typeflags! {
}
/// The full set of access rights.
pub type Full = TRights![Dup, Read, Write, Exec, Signal];
pub type Full = TRightSet<TRights![Dup, Read, Write, Exec, Signal]>;
pub type ReadOp = TRights![Read];
pub type WriteOp = TRights![Write];
/// Wrapper for TRights, used to bypass an error message from the Rust compiler,
/// the relevant issue is: https://github.com/rust-lang/rfcs/issues/2758
///
/// Example:
///
/// ```rust
/// pub struct Vmo<R=Rights>(R);
///
/// impl<R:TRights> Vmo<TRightSet<R>>{
/// //...
/// }
///
/// impl Vmo<Rights>{
/// //...
/// }
///
/// ```
///
#[derive(Clone, Copy)]
pub struct TRightSet<T>(pub T);
impl<T> Deref for TRightSet<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for TRightSet<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

View File

@ -12,6 +12,7 @@ pod = { git = "https://github.com/jinzhao-dev/pod", rev = "7fa2ed2" }
jinux-input = { path = "../../comps/input" }
jinux-block = { path = "../../comps/block" }
jinux-time = { path = "../../comps/time" }
jinux-rights = { path = "../jinux-rights" }
controlled = { path = "../../libs/comp-sys/controlled" }
typeflags = { path = "../typeflags" }
typeflags-util = { path = "../typeflags-util" }

View File

@ -1,5 +1,5 @@
use crate::prelude::*;
use crate::rights::{Rights, TRights};
use jinux_rights::{Rights, TRights};
use super::*;

View File

@ -11,7 +11,7 @@ use crate::fs::utils::{
StatusFlags,
};
use crate::prelude::*;
use crate::rights::Rights;
use jinux_rights::Rights;
pub struct InodeHandle<R = Rights>(Arc<InodeHandle_>, R);

View File

@ -1,10 +1,10 @@
use crate::prelude::*;
use crate::rights::*;
use jinux_rights::{Read, TRightSet, TRights, Write};
use jinux_rights_proc::require;
use super::*;
impl<R: TRights> InodeHandle<R> {
impl<R: TRights> InodeHandle<TRightSet<R>> {
#[require(R > Read)]
pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
self.0.read(buf)

View File

@ -1,5 +1,5 @@
use crate::prelude::*;
use crate::rights::Rights;
use jinux_rights::Rights;
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug)]

View File

@ -4,7 +4,7 @@ use ringbuf::{HeapConsumer as HeapRbConsumer, HeapProducer as HeapRbProducer, He
use crate::events::Observer;
use crate::prelude::*;
use crate::rights::*;
use jinux_rights::{Read, ReadOp, TRights, Write, WriteOp};
use super::{IoEvents, Pollee, Poller, StatusFlags};

View File

@ -1,7 +1,7 @@
use super::Inode;
use crate::prelude::*;
use crate::rights::Full;
use crate::vm::vmo::{Pager, Vmo, VmoFlags, VmoOptions};
use jinux_rights::Full;
use core::ops::Range;
use jinux_frame::vm::{VmAllocOptions, VmFrame, VmFrameVec};

View File

@ -4,12 +4,12 @@ use super::{
};
use crate::fs::device::Device;
use crate::prelude::*;
use crate::rights::Full;
use crate::vm::vmo::Vmo;
use alloc::string::String;
use core::time::Duration;
use jinux_frame::vm::VmIo;
use jinux_rights::Full;
/// VFS-level representation of an inode
#[derive(Clone)]

View File

@ -38,7 +38,6 @@ pub mod events;
pub mod fs;
pub mod prelude;
mod process;
pub mod rights;
pub mod syscall;
pub mod thread;
pub mod time;

View File

@ -4,6 +4,8 @@ use jinux_frame::{
vm::{VmIo, VmSpace},
};
use jinux_rights::Full;
use crate::{
current_thread,
fs::file_table::FileTable,
@ -15,7 +17,6 @@ use crate::{
},
process_table,
},
rights::Full,
thread::{allocate_tid, thread_table, Thread, Tid},
util::write_val_to_user,
vm::vmar::Vmar,

View File

@ -16,10 +16,10 @@ use crate::fs::file_table::FileTable;
use crate::fs::fs_resolver::FsResolver;
use crate::fs::utils::FileCreationMask;
use crate::prelude::*;
use crate::rights::Full;
use crate::thread::{allocate_tid, thread_table, Thread};
use crate::vm::vmar::Vmar;
use jinux_frame::sync::WaitQueue;
use jinux_rights::Full;
pub mod clone;
pub mod fifo_scheduler;

View File

@ -1,10 +1,10 @@
use jinux_frame::{cpu::UserContext, user::UserSpace};
use jinux_rights::Full;
use crate::{
fs::fs_resolver::FsResolver,
prelude::*,
process::{program_loader::load_program_to_root_vmar, Process},
rights::Full,
thread::{Thread, Tid},
vm::vmar::Vmar,
};

View File

@ -8,9 +8,10 @@ pub mod mmap_flags;
pub mod user_heap;
use crate::prelude::*;
use jinux_rights::Full;
use user_heap::UserHeap;
use crate::{rights::Full, vm::vmar::Vmar};
use crate::vm::vmar::Vmar;
/*
* The user vm space layout is look like below.

View File

@ -1,14 +1,13 @@
use core::sync::atomic::{AtomicUsize, Ordering};
use crate::rights::Full;
use crate::vm::perms::VmPerms;
use crate::vm::vmar::Vmar;
use crate::{
prelude::*,
rights::Rights,
vm::vmo::{VmoFlags, VmoOptions},
};
use align_ext::AlignExt;
use jinux_rights::{Full, Rights};
pub const USER_HEAP_BASE: Vaddr = 0x0000_0000_1000_0000;
pub const USER_HEAP_SIZE_LIMIT: usize = PAGE_SIZE * 1000;

View File

@ -2,16 +2,15 @@
//! The process initial stack, contains arguments, environmental variables and auxiliary vectors
//! The data layout of init stack can be seen in Figure 3.9 in https://uclibc.org/docs/psABI-x86_64.pdf
use crate::rights::Rights;
use crate::vm::perms::VmPerms;
use crate::{
prelude::*,
rights::Full,
vm::{vmar::Vmar, vmo::VmoOptions},
};
use align_ext::AlignExt;
use core::mem;
use jinux_frame::vm::{VmIo, VmPerm};
use jinux_rights::{Full, Rights};
use super::aux_vec::{AuxKey, AuxVec};
use super::elf_file::Elf;

View File

@ -4,16 +4,15 @@
use crate::fs::fs_resolver::{FsPath, FsResolver, AT_FDCWD};
use crate::fs::utils::Dentry;
use crate::process::program_loader::elf::init_stack::{init_aux_vec, InitStack};
use crate::rights::Rights;
use crate::vm::perms::VmPerms;
use crate::vm::vmo::{VmoOptions, VmoRightsOp};
use crate::{
prelude::*,
rights::Full,
vm::{vmar::Vmar, vmo::Vmo},
};
use align_ext::AlignExt;
use jinux_frame::vm::{VmIo, VmPerm};
use jinux_rights::{Full, Rights};
use xmas_elf::program::{self, ProgramHeader64};
use super::elf_file::Elf;

View File

@ -3,8 +3,8 @@ mod shebang;
use crate::fs::fs_resolver::{FsPath, FsResolver, AT_FDCWD};
use crate::prelude::*;
use crate::rights::Full;
use crate::vm::vmar::Vmar;
use jinux_rights::Full;
use self::elf::{load_elf_to_root_vmar, ElfLoadInfo};
use self::shebang::parse_shebang_line;

View File

@ -2,12 +2,12 @@
use crate::fs::file_table::FileDescripter;
use crate::process::process_vm::mmap_flags::MMapFlags;
use crate::rights::Rights;
use crate::vm::perms::VmPerms;
use crate::vm::vmo::{VmoChildOptions, VmoOptions, VmoRightsOp};
use crate::{log_syscall_entry, prelude::*};
use align_ext::AlignExt;
use jinux_frame::vm::VmPerm;
use jinux_rights::Rights;
use crate::syscall::SYS_MMAP;

View File

@ -1,6 +1,6 @@
use crate::rights::Rights;
use bitflags::bitflags;
use jinux_frame::vm::VmPerm;
use jinux_rights::Rights;
bitflags! {
/// The memory access permissions of memory mappings.

View File

@ -1,12 +1,10 @@
use core::ops::Range;
use jinux_frame::vm::{Vaddr, VmIo};
use jinux_rights::Rights;
use crate::prelude::*;
use crate::{
rights::Rights,
vm::{page_fault_handler::PageFaultHandler, vmo::Vmo},
};
use crate::vm::{page_fault_handler::PageFaultHandler, vmo::Vmo};
use super::{
options::VmarChildOptions, vm_mapping::VmarMapOptions, VmPerms, Vmar, VmarRightsOp, Vmar_,

View File

@ -6,8 +6,6 @@ mod static_cap;
pub mod vm_mapping;
use crate::prelude::*;
use crate::rights::Full;
use crate::rights::Rights;
use crate::vm::perms::VmPerms;
use align_ext::AlignExt;
use alloc::collections::BTreeMap;
@ -16,6 +14,7 @@ use alloc::sync::Weak;
use alloc::vec::Vec;
use core::ops::Range;
use jinux_frame::vm::VmSpace;
use jinux_rights::{Dup, Exec, Full, Read, Rights, Signal, TRightSet, TRights, Write};
use self::vm_mapping::VmMapping;
@ -719,7 +718,10 @@ impl<R> Vmar<R> {
let rights = Rights::all();
self.check_rights(rights)?;
let vmar_ = self.0.fork_vmar_(Weak::new())?;
Ok(Vmar(vmar_, Full::new()))
Ok(Vmar(
vmar_,
TRightSet(<TRights![Dup, Read, Write, Exec, Signal]>::new()),
))
}
/// get a mapped vmo

View File

@ -138,11 +138,9 @@ mod test {
use crate::vm::page_fault_handler::PageFaultHandler;
use crate::vm::perms::VmPerms;
use crate::vm::vmo::VmoRightsOp;
use crate::{
rights::Full,
vm::{vmar::ROOT_VMAR_HIGHEST_ADDR, vmo::VmoOptions},
};
use crate::vm::{vmar::ROOT_VMAR_HIGHEST_ADDR, vmo::VmoOptions};
use jinux_frame::vm::VmIo;
use jinux_rights::Full;
#[test]
fn root_vmar() {

View File

@ -2,18 +2,16 @@ use core::ops::Range;
use crate::prelude::*;
use jinux_frame::vm::VmIo;
use jinux_rights::{Dup, Read, Rights, TRightSet, TRights};
use jinux_rights_proc::require;
use crate::{
rights::{Dup, Read, Rights, TRights},
vm::{page_fault_handler::PageFaultHandler, vmo::Vmo},
};
use crate::vm::{page_fault_handler::PageFaultHandler, vmo::Vmo};
use super::{
options::VmarChildOptions, vm_mapping::VmarMapOptions, VmPerms, Vmar, VmarRightsOp, Vmar_,
};
impl<R: TRights> Vmar<R> {
impl<R: TRights> Vmar<TRightSet<R>> {
/// Creates a root VMAR.
///
/// # Access rights
@ -22,7 +20,7 @@ impl<R: TRights> Vmar<R> {
pub fn new_root() -> Result<Self> {
let inner = Arc::new(Vmar_::new_root()?);
let rights = R::new();
let new_self = Self(inner, rights);
let new_self = Self(inner, TRightSet(rights));
Ok(new_self)
}
@ -34,7 +32,7 @@ impl<R: TRights> Vmar<R> {
/// use jinux_std::prelude::*;
/// use jinux_std::vm::{PAGE_SIZE, Vmar, VmoOptions};
///
/// let vmar = Vmar::<Full>::new().unwrap();
/// let vmar = Vmar::<RightsWrapper<Full>>::new().unwrap();
/// let vmo = VmoOptions::new(PAGE_SIZE).alloc().unwrap();
/// let target_vaddr = 0x1234000;
/// let real_vaddr = vmar
@ -62,7 +60,11 @@ impl<R: TRights> Vmar<R> {
/// which ensures that any updated memory permissions do not go beyond
/// the access rights of the underlying VMOs.
#[require(R > Dup)]
pub fn new_map(&self, vmo: Vmo<Rights>, perms: VmPerms) -> Result<VmarMapOptions<R, Rights>> {
pub fn new_map(
&self,
vmo: Vmo<Rights>,
perms: VmPerms,
) -> Result<VmarMapOptions<TRightSet<R>, Rights>> {
let dup_self = self.dup()?;
Ok(VmarMapOptions::new(dup_self, vmo, perms))
}
@ -87,7 +89,7 @@ impl<R: TRights> Vmar<R> {
/// The new VMAR child will be of the same capability class and
/// access rights as the parent.
#[require(R > Dup)]
pub fn new_child(&self, size: usize) -> Result<VmarChildOptions<R>> {
pub fn new_child(&self, size: usize) -> Result<VmarChildOptions<TRightSet<R>>> {
let dup_self = self.dup()?;
Ok(VmarChildOptions::new(dup_self, size))
}
@ -169,7 +171,7 @@ impl<R: TRights> Vmar<R> {
}
}
impl<R: TRights> VmIo for Vmar<R> {
impl<R: TRights> VmIo for Vmar<TRightSet<R>> {
fn read_bytes(&self, offset: usize, buf: &mut [u8]) -> jinux_frame::Result<()> {
self.check_rights(Rights::READ)?;
self.0.read(offset, buf)?;
@ -183,7 +185,7 @@ impl<R: TRights> VmIo for Vmar<R> {
}
}
impl<R: TRights> PageFaultHandler for Vmar<R> {
impl<R: TRights> PageFaultHandler for Vmar<TRightSet<R>> {
fn handle_page_fault(
&self,
page_fault_addr: Vaddr,
@ -200,7 +202,7 @@ impl<R: TRights> PageFaultHandler for Vmar<R> {
}
}
impl<R: TRights> VmarRightsOp for Vmar<R> {
impl<R: TRights> VmarRightsOp for Vmar<TRightSet<R>> {
fn rights(&self) -> Rights {
Rights::from_bits(R::BITS).unwrap()
}

View File

@ -4,7 +4,7 @@ use crate::prelude::*;
use jinux_frame::vm::VmIo;
use crate::rights::{Rights, TRights};
use jinux_rights::{Rights, TRights};
use super::VmoRightsOp;
use super::{

View File

@ -2,9 +2,9 @@
use core::ops::Range;
use crate::rights::Rights;
use align_ext::AlignExt;
use jinux_frame::vm::{VmAllocOptions, VmFrameVec, VmIo};
use jinux_rights::Rights;
use crate::prelude::*;

View File

@ -10,10 +10,10 @@ use typeflags_util::{SetExtend, SetExtendOp};
use crate::prelude::*;
use crate::rights::{Dup, Rights, TRights, Write};
use crate::vm::vmo::InheritedPages;
use crate::vm::vmo::VmoType;
use crate::vm::vmo::{VmoInner, Vmo_};
use jinux_rights::{Dup, Rights, TRightSet, TRights, Write};
use super::VmoRightsOp;
use super::{Pager, Vmo, VmoFlags};
@ -105,14 +105,14 @@ impl VmoOptions<Rights> {
}
}
impl<R: TRights> VmoOptions<R> {
impl<R: TRights> VmoOptions<TRightSet<R>> {
/// Allocates the VMO according to the specified options.
///
/// # Access rights
///
/// The VMO is initially assigned the access rights represented
/// by `R: TRights`.
pub fn alloc(self) -> Result<Vmo<R>> {
pub fn alloc(self) -> Result<Vmo<TRightSet<R>>> {
let VmoOptions {
size,
flags,
@ -120,7 +120,7 @@ impl<R: TRights> VmoOptions<R> {
pager,
} = self;
let vmo_ = alloc_vmo_(size, flags, pager)?;
Ok(Vmo(Arc::new(vmo_), R::new()))
Ok(Vmo(Arc::new(vmo_), TRightSet(R::new())))
}
}
@ -252,7 +252,7 @@ pub struct VmoChildOptions<R, C> {
marker: PhantomData<C>,
}
impl<R: TRights> VmoChildOptions<R, VmoSliceChild> {
impl<R: TRights> VmoChildOptions<TRightSet<R>, VmoSliceChild> {
/// Creates a default set of options for creating a slice VMO child.
///
/// A slice child of a VMO, which has direct access to a range of memory
@ -261,7 +261,7 @@ impl<R: TRights> VmoChildOptions<R, VmoSliceChild> {
///
/// The range of a child must be within that of the parent.
#[require(R > Dup)]
pub fn new_slice(parent: Vmo<R>, range: Range<usize>) -> Self {
pub fn new_slice(parent: Vmo<TRightSet<R>>, range: Range<usize>) -> Self {
Self {
flags: parent.flags() & Self::PARENT_FLAGS_MASK,
parent,
@ -373,13 +373,13 @@ impl VmoChildOptions<Rights, VmoCowChild> {
}
}
impl<R: TRights> VmoChildOptions<R, VmoSliceChild> {
impl<R: TRights> VmoChildOptions<TRightSet<R>, VmoSliceChild> {
/// Allocates the child VMO.
///
/// # Access rights
///
/// The child VMO is initially assigned all the parent's access rights.
pub fn alloc(self) -> Result<Vmo<R>> {
pub fn alloc(self) -> Result<Vmo<TRightSet<R>>> {
let VmoChildOptions {
parent,
range,
@ -392,14 +392,14 @@ impl<R: TRights> VmoChildOptions<R, VmoSliceChild> {
}
}
impl<R: TRights> VmoChildOptions<R, VmoCowChild> {
impl<R: TRights> VmoChildOptions<TRightSet<R>, VmoCowChild> {
/// Allocates the child VMO.
///
/// # Access rights
///
/// The child VMO is initially assigned all the parent's access rights
/// plus the Write right.
pub fn alloc(self) -> Result<Vmo<SetExtendOp<R, Write>>>
pub fn alloc(self) -> Result<Vmo<TRightSet<SetExtendOp<R, Write>>>>
where
R: SetExtend<Write>,
SetExtendOp<R, Write>: TRights,
@ -413,7 +413,7 @@ impl<R: TRights> VmoChildOptions<R, VmoCowChild> {
let Vmo(parent_vmo_, _) = parent;
let child_vmo_ = alloc_child_vmo_(parent_vmo_, range, flags, ChildType::Cow)?;
let right = SetExtendOp::<R, Write>::new();
Ok(Vmo(Arc::new(child_vmo_), right))
Ok(Vmo(Arc::new(child_vmo_), TRightSet(right)))
}
}
@ -501,8 +501,8 @@ impl VmoChildType for VmoCowChild {}
#[cfg(test)]
mod test {
use super::*;
use crate::rights::Full;
use jinux_frame::vm::VmIo;
use jinux_rights::Full;
#[test]
fn alloc_vmo() {

View File

@ -3,7 +3,7 @@ use core::ops::Range;
use jinux_frame::vm::VmIo;
use jinux_rights_proc::require;
use crate::rights::*;
use jinux_rights::{Dup, Rights, TRightSet, TRights, Write};
use super::VmoRightsOp;
use super::{
@ -11,7 +11,7 @@ use super::{
Vmo, VmoChildOptions,
};
impl<R: TRights> Vmo<R> {
impl<R: TRights> Vmo<TRightSet<R>> {
/// Creates a new slice VMO through a set of VMO child options.
///
/// # Example
@ -35,7 +35,7 @@ impl<R: TRights> Vmo<R> {
pub fn new_slice_child(
&self,
range: Range<usize>,
) -> Result<VmoChildOptions<R, VmoSliceChild>> {
) -> Result<VmoChildOptions<TRightSet<R>, VmoSliceChild>> {
let dup_self = self.dup()?;
Ok(VmoChildOptions::new_slice(dup_self, range))
}
@ -61,7 +61,10 @@ impl<R: TRights> Vmo<R> {
/// The child will be given the access rights of the parent
/// plus the Write right.
#[require(R > Dup)]
pub fn new_cow_child(&self, range: Range<usize>) -> Result<VmoChildOptions<R, VmoCowChild>> {
pub fn new_cow_child(
&self,
range: Range<usize>,
) -> Result<VmoChildOptions<TRightSet<R>, VmoCowChild>> {
let dup_self = self.dup()?;
Ok(VmoChildOptions::new_cow(dup_self, range))
}
@ -141,7 +144,7 @@ impl<R: TRights> Vmo<R> {
}
}
impl<R: TRights> VmIo for Vmo<R> {
impl<R: TRights> VmIo for Vmo<TRightSet<R>> {
fn read_bytes(&self, offset: usize, buf: &mut [u8]) -> jinux_frame::Result<()> {
self.check_rights(Rights::READ)?;
self.0.read_bytes(offset, buf)?;
@ -155,7 +158,7 @@ impl<R: TRights> VmIo for Vmo<R> {
}
}
impl<R: TRights> VmoRightsOp for Vmo<R> {
impl<R: TRights> VmoRightsOp for Vmo<TRightSet<R>> {
fn rights(&self) -> Rights {
Rights::from_bits(R::BITS).unwrap()
}