Implement mount/unmount system calls

This commit is contained in:
Mark Poliakov 2023-07-20 12:37:57 +03:00
parent 8254de45a3
commit 84f645fd0c
2 changed files with 44 additions and 21 deletions

View File

@ -8,13 +8,18 @@ use core::ffi::{c_char, c_void};
extern crate yggdrasil_abi as abi; extern crate yggdrasil_abi as abi;
pub use abi::error::Error; pub use abi::error::Error;
pub use abi::io::{OpenFlags, RawFd};
pub use abi::path; pub use abi::path;
pub mod alloc; pub mod alloc;
pub mod netc; pub mod netc;
pub mod sys; pub mod sys;
pub mod time; pub mod time;
pub mod io {
//! I/O data structures
pub use abi::io::{MountOptions, OpenFlags, RawFd, UnmountOptions};
}
/// Type alias for a raw file descriptor /// Type alias for a raw file descriptor
#[no_mangle] #[no_mangle]
@ -26,9 +31,7 @@ unsafe extern "C" fn memcmp(_p0: *const c_void, _p1: *const c_void, _len: usize)
unsafe extern "C" fn memcpy(p0: *mut c_void, p1: *const c_void, len: usize) -> *mut c_void { unsafe extern "C" fn memcpy(p0: *mut c_void, p1: *const c_void, len: usize) -> *mut c_void {
let mut offset = 0; let mut offset = 0;
while offset < len { while offset < len {
(p0 as *mut u8) (p0 as *mut u8).add(offset).write((p1 as *mut u8).add(offset).read());
.add(offset)
.write((p1 as *mut u8).add(offset).read());
offset += 1; offset += 1;
} }
p0 p0

View File

@ -2,8 +2,8 @@
use core::time::Duration; use core::time::Duration;
use abi::{ use abi::{
error::{Error, FromSyscallResult}, error::{Error, SyscallResult},
io::{OpenFlags, RawFd}, io::{MountOptions, OpenFlags, RawFd, UnmountOptions},
syscall::SyscallFunction, syscall::SyscallFunction,
}; };
@ -64,9 +64,7 @@ macro_rules! argp {
}; };
} }
/// [SyscallFunction::DebugTrace] call. /// System call: print a message to kernel's debug trace output.
///
/// * msg: message to print to the debug trace log.
/// ///
/// # Safety /// # Safety
/// ///
@ -75,7 +73,7 @@ pub unsafe fn debug_trace(msg: &str) {
syscall!(SyscallFunction::DebugTrace, argp!(msg.as_ptr()), argn!(msg.len())); syscall!(SyscallFunction::DebugTrace, argp!(msg.as_ptr()), argn!(msg.len()));
} }
/// [SyscallFunction::MapMemory] call. /// System call: map a virtual memory region into the process's address space.
/// ///
/// * virt: hint to the memory manager. Mapping is not guaranteed to be placed at the specific /// * virt: hint to the memory manager. Mapping is not guaranteed to be placed at the specific
/// address, but the kernel will at least try. /// address, but the kernel will at least try.
@ -89,7 +87,7 @@ pub unsafe fn map_memory(virt: Option<usize>, len: usize) -> Result<usize, Error
usize::from_syscall_result(syscall!(SyscallFunction::MapMemory, argn!(virt), argn!(len))) usize::from_syscall_result(syscall!(SyscallFunction::MapMemory, argn!(virt), argn!(len)))
} }
/// [SyscallFunction::UnmapMemory] call. /// System call: release a virtual memory region.
/// ///
/// * virt: address of the region to unmap. /// * virt: address of the region to unmap.
/// * len: length of the region to unmap. /// * len: length of the region to unmap.
@ -101,7 +99,7 @@ pub unsafe fn unmap_memory(virt: usize, len: usize) -> Result<(), Error> {
<()>::from_syscall_result(syscall!(SyscallFunction::UnmapMemory, argn!(virt), argn!(len))) <()>::from_syscall_result(syscall!(SyscallFunction::UnmapMemory, argn!(virt), argn!(len)))
} }
/// [SyscallFunction::Nanosleep] call. /// System call: suspend caller task for some time.
/// ///
/// * duration: amount of time to sleep. /// * duration: amount of time to sleep.
/// ///
@ -114,7 +112,7 @@ pub unsafe fn nanosleep(duration: Duration) {
syscall!(SyscallFunction::Nanosleep, argn!(seconds), argn!(nanos)); syscall!(SyscallFunction::Nanosleep, argn!(seconds), argn!(nanos));
} }
/// [SyscallFunction::Exit] call. /// System call: terminate current process.
/// ///
/// * code: process termination status code. /// * code: process termination status code.
/// ///
@ -126,7 +124,7 @@ pub unsafe fn exit(code: i32) -> ! {
panic!(); panic!();
} }
/// [SyscallFunction::Write] call. /// System call: write `data` to a file descriptor.
/// ///
/// * fd: file descriptor to write to. /// * fd: file descriptor to write to.
/// * data: data to write to the file. /// * data: data to write to the file.
@ -143,24 +141,24 @@ pub unsafe fn write(fd: RawFd, data: &[u8]) -> Result<usize, Error> {
)) ))
} }
/// [SyscallFunction::Read] call. /// System call: read data from a file descriptor into `buf`.
/// ///
/// * fd: file descriptor to read from. /// * fd: file descriptor to read from.
/// * data: buffer to store the read data. /// * buf: buffer to store the read data.
/// ///
/// # Safety /// # Safety
/// ///
/// Unsafe: direct system call. /// Unsafe: direct system call.
pub unsafe fn read(fd: RawFd, data: &mut [u8]) -> Result<usize, Error> { pub unsafe fn read(fd: RawFd, buf: &mut [u8]) -> Result<usize, Error> {
usize::from_syscall_result(syscall!( usize::from_syscall_result(syscall!(
SyscallFunction::Read, SyscallFunction::Read,
argn!(fd.0), argn!(fd.0),
argp!(data.as_ptr()), argp!(buf.as_ptr()),
argn!(data.len()) argn!(buf.len())
)) ))
} }
/// [SyscallFunction::Open] call. /// System call: open a file with given options.
/// ///
/// * path: path of the file to open. /// * path: path of the file to open.
/// * opts: flags and mode to open the file with. /// * opts: flags and mode to open the file with.
@ -177,7 +175,7 @@ pub unsafe fn open(path: &str, opts: OpenFlags) -> Result<RawFd, Error> {
)) ))
} }
/// [SyscallFunction::Close] call. /// System call: close a file descriptor.
/// ///
/// * fd: file descriptor to close. /// * fd: file descriptor to close.
/// ///
@ -187,3 +185,25 @@ pub unsafe fn open(path: &str, opts: OpenFlags) -> Result<RawFd, Error> {
pub unsafe fn close(fd: RawFd) -> Result<(), Error> { pub unsafe fn close(fd: RawFd) -> Result<(), Error> {
<()>::from_syscall_result(syscall!(SyscallFunction::Close, argn!(fd.0))) <()>::from_syscall_result(syscall!(SyscallFunction::Close, argn!(fd.0)))
} }
/// System call: mount a filesystem at desired mountpoint.
///
/// * options: see [MountOptions].
///
/// # Safety
///
/// Unsafe: direct system call.
pub unsafe fn mount(options: &MountOptions) -> Result<(), Error> {
<()>::from_syscall_result(syscall!(SyscallFunction::Mount, argp!(options as *const _)))
}
/// System call: unmount a filesystem from specified mountpoint.
///
/// * options: see [UnmountOptions].
///
/// # Safety
///
/// Unsafe: direct system call.
pub unsafe fn unmount(options: &UnmountOptions) -> Result<(), Error> {
<()>::from_syscall_result(syscall!(SyscallFunction::Unmount, argp!(options as *const _)))
}