From cb50a6d58c9960f2d5967df96dcb5ef414630f63 Mon Sep 17 00:00:00 2001 From: shibedrill Date: Thu, 13 Mar 2025 16:29:38 -0400 Subject: [PATCH] Tentative API --- src/kernel/arch/x86_64/asm.rs | 2 +- src/kernel/syscall_runner.rs | 23 +++++++++++------ src/lib/arch/aarch64/mod.rs | 1 + src/lib/arch/loongarch64/mod.rs | 1 + src/lib/arch/riscv64/mod.rs | 1 + src/lib/arch/x86_64/mod.rs | 3 ++- src/lib/arch/x86_64/registers_impl.rs | 4 +++ src/lib/arch/x86_64/syscall_impl.rs | 4 --- src/lib/lib.rs | 3 ++- src/lib/registers.rs | 36 +++++++++++++++++++++++++++ src/lib/syscall.rs | 21 ++++++++-------- 11 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 src/lib/arch/aarch64/mod.rs create mode 100644 src/lib/arch/loongarch64/mod.rs create mode 100644 src/lib/arch/riscv64/mod.rs create mode 100644 src/lib/arch/x86_64/registers_impl.rs create mode 100644 src/lib/registers.rs diff --git a/src/kernel/arch/x86_64/asm.rs b/src/kernel/arch/x86_64/asm.rs index 2390366..8203b08 100644 --- a/src/kernel/arch/x86_64/asm.rs +++ b/src/kernel/arch/x86_64/asm.rs @@ -6,4 +6,4 @@ pub unsafe fn halt() { unsafe { asm!("hlt"); } -} \ No newline at end of file +} diff --git a/src/kernel/syscall_runner.rs b/src/kernel/syscall_runner.rs index 319a8c3..49cfffc 100644 --- a/src/kernel/syscall_runner.rs +++ b/src/kernel/syscall_runner.rs @@ -3,20 +3,29 @@ use crate::process::Process; // FUCKING ADD CONST UNWRAP!!! -pub static KERNEL_VERSION_MAJOR: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_MAJOR"), 10) { +pub static KERNEL_VERSION_MAJOR: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_MAJOR"), 10) +{ Ok(ver) => ver, - Err(_) => { panic!("Invalid version number ") }, + Err(_) => { + panic!("Invalid version number ") + } }; -pub static KERNEL_VERSION_MINOR: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_MINOR"), 10) { +pub static KERNEL_VERSION_MINOR: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_MINOR"), 10) +{ Ok(ver) => ver, - Err(_) => { panic!("Invalid version number ") }, + Err(_) => { + panic!("Invalid version number ") + } }; -pub static KERNEL_VERSION_PATCH: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_PATCH"), 10) { +pub static KERNEL_VERSION_PATCH: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_PATCH"), 10) +{ Ok(ver) => ver, - Err(_) => { panic!("Invalid version number ") }, + Err(_) => { + panic!("Invalid version number ") + } }; #[allow(unused_variables)] pub fn run_syscall(process: &mut Process) { // Get the syscall ID from the process's CPU registers. -} \ No newline at end of file +} diff --git a/src/lib/arch/aarch64/mod.rs b/src/lib/arch/aarch64/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/lib/arch/aarch64/mod.rs @@ -0,0 +1 @@ + diff --git a/src/lib/arch/loongarch64/mod.rs b/src/lib/arch/loongarch64/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/lib/arch/loongarch64/mod.rs @@ -0,0 +1 @@ + diff --git a/src/lib/arch/riscv64/mod.rs b/src/lib/arch/riscv64/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/lib/arch/riscv64/mod.rs @@ -0,0 +1 @@ + diff --git a/src/lib/arch/x86_64/mod.rs b/src/lib/arch/x86_64/mod.rs index edd663b..4992603 100644 --- a/src/lib/arch/x86_64/mod.rs +++ b/src/lib/arch/x86_64/mod.rs @@ -1 +1,2 @@ -mod syscall_impl; \ No newline at end of file +mod registers_impl; +mod syscall_impl; diff --git a/src/lib/arch/x86_64/registers_impl.rs b/src/lib/arch/x86_64/registers_impl.rs new file mode 100644 index 0000000..7dea5d4 --- /dev/null +++ b/src/lib/arch/x86_64/registers_impl.rs @@ -0,0 +1,4 @@ +#[allow(dead_code)] +pub struct Registers { + // Private fields +} diff --git a/src/lib/arch/x86_64/syscall_impl.rs b/src/lib/arch/x86_64/syscall_impl.rs index 3fc69b5..6bf6b88 100644 --- a/src/lib/arch/x86_64/syscall_impl.rs +++ b/src/lib/arch/x86_64/syscall_impl.rs @@ -3,7 +3,3 @@ // The system call API for x86_64. use crate::syscall::*; - -struct Registers { - -} \ No newline at end of file diff --git a/src/lib/lib.rs b/src/lib/lib.rs index 67513b8..dcbb018 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -1,5 +1,6 @@ #![no_std] #![allow(unused_imports)] mod arch; +pub mod registers; pub mod syscall; -pub use arch::current::*; \ No newline at end of file +pub use arch::current::*; diff --git a/src/lib/registers.rs b/src/lib/registers.rs new file mode 100644 index 0000000..d43634f --- /dev/null +++ b/src/lib/registers.rs @@ -0,0 +1,36 @@ +// Every architecture MUST implement this as part of the ABI. +// Additional registers can be implemented with architecture-specific traits. +pub unsafe trait RegStoreLoad +where + Self: Sized, + Self: Default, +{ + // Create an instance of Registers by loading values from the CPU. + // This MUST NOT modify any CPU registers, and can only read them. + fn load_all() -> Self; + // Create a new instance to write to before we commit to the CPU. + // This MUST instantiate a zeroed set of registers. + // Do not represent unmodified registers as None variants of an Option. + fn new_zeroed() -> Self { + Self::default() + } + // Store the instance to the CPU and consume self. + // This MUST write ALL register values to the CPU, including zero values. + fn store_all(self); + + // All of these functions below MUST NOT write directly to the CPU. + // The registers will be written when store_all() is called. + + // Get the six argument or return register values. + // This MUST read registers in the same order as set_syscall_args sets them. + fn read_syscall_args(&self) -> [usize; 6]; + // Get the syscall number or error number. + // This MUST NOT zero or otherwise overwrite the register. + fn read_syscall_num(&self) -> usize; + // Set the six argument or return register values. + // This MUST write registers in the same order as read_syscall_args reads them. + fn set_syscall_args(&mut self, new_values: [usize; 6]); + // Set the syscall number or error number. + // This MUST NOT change any other registers. + fn set_syscall_num(&mut self, new_value: usize); +} diff --git a/src/lib/syscall.rs b/src/lib/syscall.rs index 661a206..b900d6a 100644 --- a/src/lib/syscall.rs +++ b/src/lib/syscall.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] // TODO: Implement a nice API for system calls. -// I don't want to have to define the argument/return value registers twice +// I don't want to have to define the argument/return value registers twice // per architecture. How do I make this work? pub enum SyscallArgs { @@ -11,15 +11,16 @@ pub enum SyscallArgs { Args3(usize, usize, usize), Args4(usize, usize, usize, usize), Args5(usize, usize, usize, usize, usize), - Args6(usize, usize, usize, usize, usize, usize) + Args6(usize, usize, usize, usize, usize, usize), } -#[repr(u16)] +#[repr(usize)] pub enum SyscallError { - Ok = 0, - SyscallNotExist = 1, - FileNotExist = 2, - ProcessNotExist = 3, - PermissionDenied = 4, - Aborted = 5, -} \ No newline at end of file + Ok, // No error. + Unspecified, // Unspecified error occurred. + SyscallNotExist, // System call does not exist. + FileNotExist, // The file mentioned does not exist. + ProcessNotExist, // The process mentioned does not exist. + PermissionDenied, // The process lacks capabilities. + Aborted, // The kernel gave up on a blocking request. +}