Skip to content

Commit

Permalink
Naming serivice update
Browse files Browse the repository at this point in the history
  • Loading branch information
schoettner committed Sep 15, 2024
1 parent 0b86bc7 commit d9248e8
Show file tree
Hide file tree
Showing 23 changed files with 841 additions and 853 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
"os/application/shell",
"os/application/uptime",
"os/application/date",
"os/application/mkentry"
"os/application/mkdir"
]

# [profile.release]
Expand Down
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ condition = { files_not_exist = [ "${INITRD_DIRECTORY}" ] }
[tasks.initrd]
cwd = "${INITRD_DIRECTORY}"
command = "${TAR}"
args = [ "-cf", "${BOOTLOADER_DIRECTORY}/initrd.tar", "hello", "helloc", "shell", "uptime", "date", "mkentry" ]
args = [ "-cf", "${BOOTLOADER_DIRECTORY}/initrd.tar", "hello", "helloc", "shell", "uptime", "date", "mkdir" ]
dependencies = [ "link-members" ]
condition = { files_modified = { input = [ "${INITRD_DIRECTORY}/*" ], output = [ "${BOOTLOADER_DIRECTORY}/initrd.tar" ] } }

Expand Down
30 changes: 30 additions & 0 deletions docs/howto-add-a-syscall.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## How to add a new system call

### Work to be done in User Mode

1.
You need to add an entry for your new syscall in: `os/library/syscall`
In `enum SysCall`. Insert your new system call name before `LastEntryMarker`.

2.
For using your new System call, see examples in `os/library`. A system call is typically used within a runtime library function, not directly within an application.

*Important: Check params before firing a syscall and abort if they are not valid.*

3.
Return values for system calls are defined in `return_vals.rs`. This file is used by user and kernel mode parts.


### Work to be done in Kernel Mode

1.
You need to add the counterpart implementation for your user mode system call in a file in `os/kernel/syscall`, e.g. `sys_naming.rs`. All syscall function names should start with the prefix `sys_`.

*Important: It is of upmost important to have the correct signature and check params.*

2.
Add your system call to the `SyscallTable` in `kernel/syscall/syscall_dispatcher.rs`.

3.
The return value is `isize` and created by calling `convert_syscall_result_to_ret_code` which expects a `SyscallResult`, defined in `return_vals.rs` (see above). So kernel counterparts should use `SyscallResult` which is converted to `isize` when returning to user mode.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ cargo-features = ["edition2024"]

[package]
edition = "2024"
name = "mkentry"
name = "mkdir"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]

[lib]
crate-type = ["staticlib"]
path = "src/mkentry.rs"
path = "src/mkdir.rs"

[dependencies]
# Local dependencies
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate alloc;
#[allow(unused_imports)]
use runtime::*;
use terminal::{print, println};
use naming::mkentry;
use naming::mkdir;

#[unsafe(no_mangle)]
pub fn main() {
Expand All @@ -14,7 +14,7 @@ pub fn main() {
println!("Arg[{}]: {}", i, arg);
}

let res = mkentry("/home/schoettner", "test.txt", 1);
let res = mkdir("/home/schoettner");

println!("app: mkentry {:?}", res);
println!("app: mkdir {:?}", res);
}
4 changes: 2 additions & 2 deletions os/kernel/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

use crate::interrupt::interrupt_dispatcher;
use crate::naming::name_service;
use crate::naming;
use crate::syscall::syscall_dispatcher;
use crate::process::thread::Thread;
use alloc::format;
Expand Down Expand Up @@ -247,7 +247,7 @@ pub extern "C" fn start(multiboot2_magic: u32, multiboot2_addr: *const BootInfor
}

// Init naming service
name_service::init();
naming::api::init();

// Load initial ramdisk
let initrd_tag = multiboot.module_tags()
Expand Down
145 changes: 145 additions & 0 deletions os/kernel/src/naming/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* ╔═════════════════════════════════════════════════════════════════════════╗
║ Module: api ║
╟─────────────────────────────────────────────────────────────────────────╢
║ Descr.: Public interface of the name service (ns): ║
║ - mkdir: create a directory with all sub directories ║
║ - open: open a named object ║
║ - read: read bytes from an open object ║
║ - write: write bytes into an open object ║
║ - seek: set file pointer (for files) ║
║ - init: init ns, called once ║
║ - dump: print all entries on the screen (for debugging) ║
╟─────────────────────────────────────────────────────────────────────────╢
║ Author: Michael Schoettner, Univ. Duesseldorf, 9.9.2024 ║
╚═════════════════════════════════════════════════════════════════════════╝
*/


use alloc::string::String;
use syscall::return_vals::{OpenOptions, SeekOrigin, convert_syscall_result_to_ret_code};

use crate::naming::main;
use crate::naming::main::ns_get;
use crate::naming::open_objects::ns_get_oot;


///
/// Description:
/// Init function of NS. Must be called once before using it.
///
pub fn init() {
main::init();
}

///
/// Description: Create a directory (including sub directories) for the given path
///
/// Parameters: `path` The path
///
/// Return: `SyscallResult`
///
pub fn mkdir(path: &String) -> isize {
let result = ns_get().mkdir(path);
let sysret;
match result {
Ok(()) => sysret = Ok(0), // Convert the success case to a meaningful u64, e.g., 0
Err(e) => sysret = Err(e), // Forward the error directly
}
return convert_syscall_result_to_ret_code(sysret);
}

///
/// Description: Open/create a named object
///
/// Parameters: \
/// `path` must be an absolute path \
/// `flags` see below
///
/// Return: `SyscallResult`
///
pub fn open(path: &String, flags: OpenOptions) -> isize {
let res = ns_get().open(path, flags);
let sysret;
match res {
Ok(fptr) => {
sysret = ns_get_oot().lock().create_new_handle_for_filepointer(fptr);
},
Err(e) => sysret = Err(e),
}
return convert_syscall_result_to_ret_code(sysret);

/* match res {
Ok(fptr) => ns_get_oot().lock().create_new_handle_for_filepointer(fptr),
Err(e) => Err(e),
}
*/
}

///
/// Description: \
/// Write bytes from the given buffer into the file (at the current position). \
/// The number of bytes to be written is determined by the buffer size
///
/// Parameters: \
/// `fh` file handle \
/// `buf` buffer from which bytes are copied into the file \
///
/// Return: `Ok(#bytes written)` or `Err(Errno)`
///
pub fn write(fh: usize, buf: &[u8]) -> isize {
let sysret;
match ns_get_oot().lock().get(fh) {
Ok(fptr) => sysret = fptr.write(buf),
Err(e) => sysret = Err(e),
}
return convert_syscall_result_to_ret_code(sysret);
}

///
/// Description: Set file pointer.
///
/// Parameters: \
/// `fh` file handle \
/// `offset` offset in bytes \
/// `origin` point of origin
///
/// Return: `Ok(size in bytes)` or `Err(errno)`
///
pub fn seek(fh: usize, offset: usize, origin: SeekOrigin) -> isize {
let sysret;
match ns_get_oot().lock().get(fh) {
Ok(fptr) => sysret = fptr.seek(offset, origin),
Err(e) => sysret = Err(e),
}
return convert_syscall_result_to_ret_code(sysret);
}

///
/// Description: \
/// Read bytes from the file (from current position) into the given buffer. \
/// The number of bytes to be read is determined by the buffer size
///
/// Parameters: \
/// `fh` file handle \
/// `buf` buffer to copy file bytes into \
///
/// Return: `Ok(#bytes read)` or `Err(errno)`
///
pub fn read(fh: usize, buf: &mut [u8]) -> isize {
let sysret;
match ns_get_oot().lock().get(fh) {
Ok(fptr) => sysret = fptr.read(buf),
Err(e) => sysret = Err(e),
}
return convert_syscall_result_to_ret_code(sysret);
}

///
/// Description: Dump all named objects on the screen (for debugging)
///
/// Return: `Ok(0)` or `Err(errno)`
///
pub fn dump() -> i64 {
ns_get().dump();
return 0;
}
Loading

0 comments on commit d9248e8

Please sign in to comment.