Skip to content

Commit

Permalink
remove platform independent part to the syscall interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Jan 2, 2024
1 parent 190b897 commit de146fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 79 deletions.
63 changes: 0 additions & 63 deletions src/syscalls/interfaces/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::ffi::CStr;

pub use self::generic::*;
pub use self::uhyve::*;
#[cfg(not(target_arch = "x86_64"))]
use crate::errno::ENOSYS;
use crate::fs::{self, FileAttr};
use crate::{arch, env};

mod generic;
Expand Down Expand Up @@ -55,65 +53,4 @@ pub trait SyscallInterface: Send + Sync {
fn shutdown(&self, _arg: i32) -> ! {
arch::processor::shutdown()
}

fn unlink(&self, name: *const u8) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();
debug!("unlink {}", name);

fs::FILESYSTEM
.get()
.unwrap()
.unlink(name)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[cfg(target_arch = "x86_64")]
fn rmdir(&self, name: *const u8) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();
debug!("rmdir {}", name);

fs::FILESYSTEM
.get()
.unwrap()
.rmdir(name)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[cfg(target_arch = "x86_64")]
fn mkdir(&self, name: *const u8, mode: u32) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();
debug!("mkdir {}, mode {}", name, mode);

fs::FILESYSTEM
.get()
.unwrap()
.mkdir(name, mode)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

fn stat(&self, name: *const u8, stat: *mut FileAttr) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();
debug!("stat {}", name);

match fs::FILESYSTEM.get().unwrap().stat(name) {
Ok(attr) => unsafe {
*stat = attr;
0
},
Err(e) => -num::ToPrimitive::to_i32(&e).unwrap(),
}
}

fn lstat(&self, name: *const u8, stat: *mut FileAttr) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();
debug!("lstat {}", name);

match fs::FILESYSTEM.get().unwrap().lstat(name) {
Ok(attr) => unsafe {
*stat = attr;
0
},
Err(e) => -num::ToPrimitive::to_i32(&e).unwrap(),
}
}
}
54 changes: 38 additions & 16 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::tasks::*;
pub use self::timer::*;
use crate::env;
use crate::fd::{dup_object, get_object, remove_object, DirectoryEntry, FileDescriptor};
use crate::fs::FileAttr;
use crate::fs::{self, FileAttr};
use crate::syscalls::interfaces::SyscallInterface;
#[cfg(target_os = "none")]
use crate::{__sys_free, __sys_malloc, __sys_realloc};
Expand Down Expand Up @@ -101,37 +101,43 @@ pub extern "C" fn sys_shutdown(arg: i32) -> ! {
}

extern "C" fn __sys_unlink(name: *const u8) -> i32 {
SYS.unlink(name)
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

fs::FILESYSTEM
.get()
.unwrap()
.unlink(name)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[no_mangle]
pub extern "C" fn sys_unlink(name: *const u8) -> i32 {
kernel_function!(__sys_unlink(name))
}

#[cfg(target_arch = "x86_64")]
extern "C" fn __sys_mkdir(name: *const u8, mode: u32) -> i32 {
SYS.mkdir(name, mode)
}
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

#[cfg(not(target_arch = "x86_64"))]
extern "C" fn __sys_mkdir(_name: *const u8, _mode: u32) -> i32 {
-crate::errno::ENOSYS
fs::FILESYSTEM
.get()
.unwrap()
.mkdir(name, mode)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[no_mangle]
pub extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 {
kernel_function!(__sys_mkdir(name, mode))
}

#[cfg(target_arch = "x86_64")]
extern "C" fn __sys_rmdir(name: *const u8) -> i32 {
SYS.rmdir(name)
}
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

#[cfg(not(target_arch = "x86_64"))]
extern "C" fn __sys_rmdir(_name: *const u8) -> i32 {
-crate::errno::ENOSYS
fs::FILESYSTEM
.get()
.unwrap()
.rmdir(name)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[no_mangle]
Expand All @@ -140,7 +146,15 @@ pub extern "C" fn sys_rmdir(name: *const u8) -> i32 {
}

extern "C" fn __sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 {
SYS.stat(name, stat)
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

match fs::FILESYSTEM.get().unwrap().stat(name) {
Ok(attr) => unsafe {
*stat = attr;
0
},
Err(e) => -num::ToPrimitive::to_i32(&e).unwrap(),
}
}

#[no_mangle]
Expand All @@ -149,7 +163,15 @@ pub extern "C" fn sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 {
}

extern "C" fn __sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 {
SYS.lstat(name, stat)
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

match fs::FILESYSTEM.get().unwrap().lstat(name) {
Ok(attr) => unsafe {
*stat = attr;
0
},
Err(e) => -num::ToPrimitive::to_i32(&e).unwrap(),
}
}

#[no_mangle]
Expand Down

0 comments on commit de146fe

Please sign in to comment.