Skip to content

Commit

Permalink
Merge pull request #85 from Freax13/feature/rusage
Browse files Browse the repository at this point in the history
misc syscall features
  • Loading branch information
Freax13 authored Oct 27, 2024
2 parents f7038d0 + 9f9053a commit 7ddf1b1
Show file tree
Hide file tree
Showing 19 changed files with 1,052 additions and 124 deletions.
21 changes: 21 additions & 0 deletions tee/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tee/kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kernel-macros.workspace = true
linkme = "0.3.25"
log = { version = "0.4.21", default-features = false }
log-types = { workspace = true }
pin-project = "1.1.7"
profiler-types = { workspace = true, optional = true }
snp-types = { workspace = true }
static-page-tables = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions tee/kernel/src/fs/fd/pipe/anon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl OpenFileDescription for ReadHalf {

fn set_flags(&self, flags: OpenFlags) {
self.flags.lock().update(flags);
self.stream_buffer.notify();
}

fn path(&self) -> Result<Path> {
Expand Down Expand Up @@ -162,6 +163,7 @@ impl OpenFileDescription for WriteHalf {

fn set_flags(&self, flags: OpenFlags) {
self.flags.lock().update(flags);
self.stream_buffer.notify();
}

fn path(&self) -> Result<Path> {
Expand Down
4 changes: 4 additions & 0 deletions tee/kernel/src/fs/fd/pipe/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl OpenFileDescription for ReadHalf {

fn set_flags(&self, flags: OpenFlags) {
*self.flags.lock() = flags;
self.read_half.notify();
}

fn path(&self) -> Result<Path> {
Expand Down Expand Up @@ -278,6 +279,7 @@ impl OpenFileDescription for WriteHalf {

fn set_flags(&self, flags: OpenFlags) {
*self.flags.lock() = flags;
self.write_half.notify();
}

fn path(&self) -> Result<Path> {
Expand Down Expand Up @@ -360,6 +362,8 @@ impl OpenFileDescription for FullReadWrite {

fn set_flags(&self, flags: OpenFlags) {
*self.flags.lock() = flags;
self.read_half.notify();
self.write_half.notify();
}

fn path(&self) -> Result<Path> {
Expand Down
8 changes: 8 additions & 0 deletions tee/kernel/src/fs/fd/stream_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl<const CAPACITY: usize, const ATOMIC_WRITE_SIZE: usize> ReadHalf<CAPACITY, A
self.notify.wait()
}

pub fn notify(&self) {
self.notify.notify();
}

pub fn make_write_half(&self) -> WriteHalf<CAPACITY, ATOMIC_WRITE_SIZE> {
assert_eq!(Arc::strong_count(&self.buffer), 1);
WriteHalf {
Expand Down Expand Up @@ -272,6 +276,10 @@ impl<const CAPACITY: usize, const ATOMIC_WRITE_SIZE: usize> WriteHalf<CAPACITY,
self.notify.wait()
}

pub fn notify(&self) {
self.notify.notify();
}

pub fn make_read_half(&self) -> ReadHalf<CAPACITY, ATOMIC_WRITE_SIZE> {
assert_eq!(Arc::strong_count(&self.buffer), 1);
ReadHalf {
Expand Down
62 changes: 31 additions & 31 deletions tee/kernel/src/fs/node/tmpfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use crate::{
FileSystem, StatFs,
},
memory::page::{Buffer, KernelPage},
spin::mutex::Mutex,
spin::{mutex::Mutex, rwlock::RwLock},
time::now,
user::process::{
syscall::args::OpenFlags,
syscall::args::{ClockId, OpenFlags},
thread::{Gid, Uid},
},
};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl TmpFsDir {
uid: Uid,
gid: Gid,
) -> Arc<Self> {
let now = now();
let now = now(ClockId::Realtime);

Arc::new_cyclic(|this_weak| Self {
fs,
Expand Down Expand Up @@ -254,7 +254,7 @@ impl Directory for TmpFsDir {
let entry = guard.items.entry(file_name);
match entry {
Entry::Vacant(entry) => {
let now = now();
let now = now(ClockId::Realtime);
let link = Arc::new(TmpFsSymlink {
fs: self.fs.clone(),
ino: new_ino(),
Expand All @@ -272,7 +272,7 @@ impl Directory for TmpFsDir {
}
Entry::Occupied(mut entry) => {
ensure!(!create_new, Exist);
let now = now();
let now = now(ClockId::Realtime);
let link = Arc::new(TmpFsSymlink {
fs: self.fs.clone(),
ino: new_ino(),
Expand Down Expand Up @@ -659,7 +659,7 @@ impl Directory for TmpFsDir {
match guard.items.entry(newname) {
Entry::Vacant(e) => {
let node = e.insert(entry);
node.update_times(now(), None, None);
node.update_times(now(ClockId::Realtime), None, None);
}
Entry::Occupied(_) => bail!(Exist),
}
Expand All @@ -681,7 +681,7 @@ impl Directory for TmpFsDir {
match new_guard.items.entry(newname) {
Entry::Vacant(e) => {
let node = e.insert(entry);
node.update_times(now(), None, None);
node.update_times(now(ClockId::Realtime), None, None);
}
Entry::Occupied(_) => bail!(Exist),
}
Expand Down Expand Up @@ -756,7 +756,7 @@ pub struct TmpFsFile {
fs: Arc<TmpFs>,
ino: u64,
this: Weak<Self>,
internal: Mutex<TmpFsFileInternal>,
internal: RwLock<TmpFsFileInternal>,
file_lock_record: LazyFileLockRecord,
}

Expand All @@ -771,13 +771,13 @@ struct TmpFsFileInternal {

impl TmpFsFile {
pub fn new(fs: Arc<TmpFs>, mode: FileMode, uid: Uid, gid: Gid) -> Arc<Self> {
let now = now();
let now = now(ClockId::Realtime);

Arc::new_cyclic(|this| Self {
fs,
ino: new_ino(),
this: this.clone(),
internal: Mutex::new(TmpFsFileInternal {
internal: RwLock::new(TmpFsFileInternal {
buffer: Buffer::new(),
ownership: Ownership::new(mode, uid, gid),
atime: now,
Expand All @@ -790,17 +790,17 @@ impl TmpFsFile {
}

fn increase_link_count(&self) {
self.internal.lock().links += 1;
self.internal.write().links += 1;
}

fn decrease_link_count(&self) {
self.internal.lock().links -= 1;
self.internal.write().links -= 1;
}
}

impl INode for TmpFsFile {
fn stat(&self) -> Result<Stat> {
let guard = self.internal.lock();
let guard = self.internal.read();
// FIXME: Fill in more values.
Ok(Stat {
dev: self.fs.dev,
Expand Down Expand Up @@ -828,15 +828,15 @@ impl INode for TmpFsFile {
}

fn chmod(&self, mode: FileMode, ctx: &FileAccessContext) -> Result<()> {
self.internal.lock().ownership.chmod(mode, ctx)
self.internal.write().ownership.chmod(mode, ctx)
}

fn chown(&self, uid: Uid, gid: Gid, ctx: &FileAccessContext) -> Result<()> {
self.internal.lock().ownership.chown(uid, gid, ctx)
self.internal.write().ownership.chown(uid, gid, ctx)
}

fn update_times(&self, ctime: Timespec, atime: Option<Timespec>, mtime: Option<Timespec>) {
let mut guard = self.internal.lock();
let mut guard = self.internal.write();
guard.ctime = ctime;
if let Some(atime) = atime {
guard.atime = atime;
Expand All @@ -853,14 +853,14 @@ impl INode for TmpFsFile {

impl File for TmpFsFile {
fn get_page(&self, page_idx: usize, shared: bool) -> Result<KernelPage> {
let mut guard = self.internal.lock();
let mut guard = self.internal.write();
guard.buffer.get_page(page_idx, shared)
}

fn read(&self, offset: usize, buf: &mut [u8], no_atime: bool) -> Result<usize> {
let mut guard = self.internal.lock();
let mut guard = self.internal.write();
if !no_atime {
guard.atime = now();
guard.atime = now(ClockId::Realtime);
}
guard.buffer.read(offset, buf)
}
Expand All @@ -873,16 +873,16 @@ impl File for TmpFsFile {
len: usize,
no_atime: bool,
) -> Result<usize> {
let mut guard = self.internal.lock();
let mut guard = self.internal.write();
if !no_atime {
guard.atime = now();
guard.atime = now(ClockId::Realtime);
}
guard.buffer.read_to_user(offset, vm, pointer, len)
}

fn write(&self, offset: usize, buf: &[u8]) -> Result<usize> {
let mut guard = self.internal.lock();
let now = now();
let mut guard = self.internal.write();
let now = now(ClockId::Realtime);
guard.ctime = now;
guard.mtime = now;
guard.buffer.write(offset, buf)
Expand All @@ -895,16 +895,16 @@ impl File for TmpFsFile {
pointer: Pointer<[u8]>,
len: usize,
) -> Result<usize> {
let mut guard = self.internal.lock();
let now = now();
let mut guard = self.internal.write();
let now = now(ClockId::Realtime);
guard.ctime = now;
guard.mtime = now;
guard.buffer.write_from_user(offset, vm, pointer, len)
}

fn append(&self, buf: &[u8]) -> Result<usize> {
let mut guard = self.internal.lock();
let now = now();
let mut guard = self.internal.write();
let now = now(ClockId::Realtime);
guard.ctime = now;
guard.mtime = now;
let offset = guard.buffer.len();
Expand All @@ -917,17 +917,17 @@ impl File for TmpFsFile {
pointer: Pointer<[u8]>,
len: usize,
) -> Result<usize> {
let mut guard = self.internal.lock();
let now = now();
let mut guard = self.internal.write();
let now = now(ClockId::Realtime);
guard.ctime = now;
guard.mtime = now;
let offset = guard.buffer.len();
guard.buffer.write_from_user(offset, vm, pointer, len)
}

fn truncate(&self, len: usize) -> Result<()> {
let mut guard = self.internal.lock();
let now = now();
let mut guard = self.internal.write();
let now = now(ClockId::Realtime);
guard.ctime = now;
guard.mtime = now;
guard.buffer.truncate(len)
Expand Down
13 changes: 13 additions & 0 deletions tee/kernel/src/spin/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ impl<T> Mutex<T> {
},
}
}

pub fn into_inner(self) -> T {
self.cell.into_inner()
}
}

unsafe impl<T> Send for Mutex<T> where T: Send {}
Expand All @@ -109,6 +113,15 @@ where
}
}

impl<T> Default for Mutex<T>
where
T: Default,
{
fn default() -> Self {
Self::new(T::default())
}
}

pub struct MutexGuard<'a, T> {
mutex: &'a Mutex<T>,
}
Expand Down
Loading

0 comments on commit 7ddf1b1

Please sign in to comment.