From c4261908c495e2df9bae2dc212ef4526ead0b581 Mon Sep 17 00:00:00 2001 From: corigan01 Date: Thu, 6 Feb 2025 21:42:22 -0600 Subject: [PATCH] Sys: Add a system lib for handing syscalls directly --- Cargo.toml | 4 +- user/libsys/Cargo.toml | 9 ++++ user/libsys/src/lib.rs | 120 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 user/libsys/Cargo.toml create mode 100644 user/libsys/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index d4dcaa4b..b1b6eb47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,8 @@ members = [ "crates/portal-macro", "crates/portal", "portals/hello-portal", - "portals/quantum-portal" + "portals/quantum-portal", + "user/libsys" ] default-members = ["meta"] @@ -64,6 +65,7 @@ portal = { path = "crates/portal" } portal-macro = { path = "crates/portal-macro" } hello-portal = { path = "portals/hello-portal" } quantum-portal = { path = "portals/quantum-portal" } +libsys = { path = "user/libsys" } [profile.stage-bootsector] inherits = "release" diff --git a/user/libsys/Cargo.toml b/user/libsys/Cargo.toml new file mode 100644 index 00000000..ab059f1c --- /dev/null +++ b/user/libsys/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "libsys" +edition = "2024" +version.workspace = true +authors.workspace = true +description.workspace = true +documentation.workspace = true + +[dependencies] diff --git a/user/libsys/src/lib.rs b/user/libsys/src/lib.rs new file mode 100644 index 00000000..57fb9d15 --- /dev/null +++ b/user/libsys/src/lib.rs @@ -0,0 +1,120 @@ +/* + ____ __ __ _ __ + / __ \__ _____ ____ / /___ ____ _ / / (_) / +/ /_/ / // / _ `/ _ \/ __/ // / ' \ / /__/ / _ \ +\___\_\_,_/\_,_/_//_/\__/\_,_/_/_/_/ /____/_/_.__/ + Part of the Quantum OS Project + +Copyright 2025 Gavin Kellam + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT +OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +/// +/// Preform a raw system call to QuantumOS. +/// +/// This is a wrapper around an assembly block that preforms a system call. +#[macro_export] +macro_rules! raw_syscall { + (__priv_custom_asm, $($tt:tt)*) => { + ::core::arch::asm!( + // "int 0x80", + "syscall", + $($tt)* + clobber_abi("system") + ); + }; + ($syscall_number:expr) => {{ + let mut syscall_return: u64 = { $syscall_number } as u64; + + $crate::raw_syscall!(__priv_custom_asm, + inlateout("rax") syscall_return, + out("rcx") _, + out("r11") _, + ); + + syscall_return + }}; + ($syscall_number:expr, $arg1:expr) => {{ + let mut syscall_return: u64 = { $syscall_number } as u64; + + $crate::raw_syscall!(__priv_custom_asm, + inlateout("rax") syscall_return, + in("rdi") $arg1, + out("rcx") _, + out("r11") _, + ); + + syscall_return + }}; + ($syscall_number:expr, $arg1:expr, $arg2:expr) => {{ + let mut syscall_return: u64 = { $syscall_number } as u64; + + $crate::raw_syscall!(__priv_custom_asm, + inlateout("rax") syscall_return, + in("rdi") $arg1, + in("rsi") $arg2, + out("rcx") _, + out("r11") _, + ); + + syscall_return + }}; + ($syscall_number:expr, $arg1:expr, $arg2:expr, $arg3:expr) => {{ + let mut syscall_return: u64 = { $syscall_number } as u64; + + $crate::raw_syscall!(__priv_custom_asm, + inlateout("rax") syscall_return, + in("rdi") $arg1, + in("rsi") $arg2, + in("rdx") $arg3, + out("rcx") _, + out("r11") _, + ); + + syscall_return + }}; + ($syscall_number:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr) => {{ + let mut syscall_return: u64 = { $syscall_number } as u64; + + $crate::raw_syscall!(__priv_custom_asm, + inlateout("rax") syscall_return, + in("rdi") $arg1, + in("rsi") $arg2, + in("rdx") $arg3, + inout("rcx") $arg4 => _, + out("r11") _, + ); + + syscall_return + }}; + ($syscall_number:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr) => {{ + let mut syscall_return: u64 = { $syscall_number } as u64; + + $crate::raw_syscall!(__priv_custom_asm, + inlateout("rax") syscall_return, + in("rdi") $arg1, + in("rsi") $arg2, + in("rdx") $arg3, + inout("rcx") $arg4 => _, + in("r8") $arg5, + out("r11") _, + ); + + syscall_return + + }}; +}