diff --git a/modules.yaml b/modules.yaml index f707d2a..afa4c87 100644 --- a/modules.yaml +++ b/modules.yaml @@ -44,13 +44,8 @@ modules: - src/kernel/serial.cpp - src/kernel/syscall.cpp - src/kernel/syscall.s - - src/kernel/syscalls/exit.cpp - - src/kernel/syscalls/fork.cpp - - src/kernel/syscalls/getpid.cpp - - src/kernel/syscalls/message.cpp - - src/kernel/syscalls/noop.cpp - - src/kernel/syscalls/pause.cpp - - src/kernel/syscalls/sleep.cpp + - src/kernel/syscalls/object.cpp + - src/kernel/syscalls/process.cpp - src/kernel/task.s - src/kernel/crtn.s diff --git a/src/drivers/nulldrv/main.cpp b/src/drivers/nulldrv/main.cpp index 1110f5d..86da144 100644 --- a/src/drivers/nulldrv/main.cpp +++ b/src/drivers/nulldrv/main.cpp @@ -1,12 +1,15 @@ #include #include +#include "j6/types.h" +#include "j6/errors.h" + extern "C" { - int32_t getpid(); - int32_t fork(); - void sleep(uint64_t til); - void debug(); - void message(const char *msg); + j6_status_t getpid(uint64_t *); + j6_status_t fork(uint64_t *); + j6_status_t sleep(uint64_t til); + j6_status_t debug(); + j6_status_t message(const char *msg); int main(int, const char **); } @@ -15,10 +18,21 @@ extern "C" { int main(int argc, const char **argv) { - int32_t pid = getpid(); - int32_t child = fork(); + uint64_t pid = 0; + uint64_t child = 0; + + j6_status_t result = fork(&child); + if (result != j6_status_ok) + return result; + message("hello from nulldrv!"); + + result = getpid(&pid); + if (result != j6_status_ok) + return result; + for (int i = 1; i < 5; ++i) sleep(i*10); - return 0; + + return pid; } diff --git a/src/drivers/nulldrv/main.s b/src/drivers/nulldrv/main.s index 1a59205..73a5300 100644 --- a/src/drivers/nulldrv/main.s +++ b/src/drivers/nulldrv/main.s @@ -11,8 +11,9 @@ getpid: push rbp mov rbp, rsp - mov rax, 0x02 ; getpid syscall - syscall ; pid is now already in rax, so just return + ; address of out var should already be in rdi + mov rax, 0x13 ; getpid syscall + syscall ; result is now already in rax, so just return pop rbp ret @@ -33,7 +34,7 @@ sleep: push rbp mov rbp, rsp - mov rax, 0x21 ; sleep syscall + mov rax, 0x16 ; sleep syscall syscall pop rbp @@ -44,8 +45,9 @@ fork: push rbp mov rbp, rsp - mov rax, 0x03 - syscall ; pid left in rax + ; address of out var should already be in rdi + mov rax, 0x12 + syscall ; result left in rax pop rbp ret @@ -57,7 +59,7 @@ message: mov rbp, rsp ; message should already be in rdi - mov rax, 0x10 + mov rax, 0x14 syscall pop rbp diff --git a/src/include/j6/types.h b/src/include/j6/types.h index ccff446..bae5e9a 100644 --- a/src/include/j6/types.h +++ b/src/include/j6/types.h @@ -8,7 +8,7 @@ typedef uint64_t j6_koid_t; /// Syscalls return status as this type -typedef uint32_t j6_status_t; +typedef uint64_t j6_status_t; /// Handles are references and capabilities to other objects typedef uint32_t j6_handle_t; diff --git a/src/kernel/objects/event.h b/src/kernel/objects/event.h index fe5ff64..bb4c4a0 100644 --- a/src/kernel/objects/event.h +++ b/src/kernel/objects/event.h @@ -8,8 +8,6 @@ class event : public kobject { public: - static constexpr type type_id = type::event; - event() : - kobject(type_id) {} + kobject(type::event) {} }; diff --git a/src/kernel/syscall.cpp b/src/kernel/syscall.cpp index 20acd1b..5f7d286 100644 --- a/src/kernel/syscall.cpp +++ b/src/kernel/syscall.cpp @@ -14,10 +14,6 @@ extern "C" { namespace syscalls { - -void send() {} -void receive() {} - } // namespace syscalls uintptr_t syscall_registry[static_cast(syscall::MAX)]; diff --git a/src/kernel/syscall.h b/src/kernel/syscall.h index 5005c4f..f17640f 100644 --- a/src/kernel/syscall.h +++ b/src/kernel/syscall.h @@ -1,6 +1,7 @@ #pragma once #include +#include "j6/types.h" struct cpu_state; @@ -19,7 +20,7 @@ void syscall_enable(); namespace syscalls { -#define SYSCALL(id, name, result, ...) result name (__VA_ARGS__); +#define SYSCALL(id, name, ...) j6_status_t name (__VA_ARGS__); #include "syscalls.inc" #undef SYSCALL } diff --git a/src/kernel/syscalls.inc b/src/kernel/syscalls.inc index 15f9515..b726252 100644 --- a/src/kernel/syscalls.inc +++ b/src/kernel/syscalls.inc @@ -1,12 +1,9 @@ -SYSCALL(0x00, noop, void) -SYSCALL(0x01, exit, void, int64_t) -SYSCALL(0x02, getpid, pid_t) -SYSCALL(0x03, fork, pid_t) +SYSCALL(0x00, object_noop, void) +SYSCALL(0x01, object_wait, j6_handle_t, j6_signal_t, j6_signal_t *) -SYSCALL(0x10, message, void, const char *) - -SYSCALL(0x20, pause, void) -SYSCALL(0x21, sleep, void, uint64_t) - -SYSCALL(0x30, send, void) -SYSCALL(0x31, receive, void) +SYSCALL(0x11, process_exit, int64_t) +SYSCALL(0x12, process_fork, pid_t*) +SYSCALL(0x13, process_getpid, pid_t*) +SYSCALL(0x14, process_log, const char *) +SYSCALL(0x15, process_pause, void) +SYSCALL(0x16, process_sleep, uint64_t) diff --git a/src/kernel/syscalls/exit.cpp b/src/kernel/syscalls/exit.cpp deleted file mode 100644 index 5f45733..0000000 --- a/src/kernel/syscalls/exit.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "log.h" -#include "scheduler.h" - -namespace syscalls { - -void -exit(int64_t status) -{ - auto &s = scheduler::get(); - auto *p = s.current(); - log::debug(logs::syscall, "Process %d exiting with code %d", p->pid, status); - - p->exit(status); - s.schedule(); -} - -} // namespace syscalls diff --git a/src/kernel/syscalls/fork.cpp b/src/kernel/syscalls/fork.cpp deleted file mode 100644 index 7aae1eb..0000000 --- a/src/kernel/syscalls/fork.cpp +++ /dev/null @@ -1,24 +0,0 @@ - -#include "log.h" -#include "scheduler.h" - -namespace syscalls { - -pid_t -fork() -{ - auto &s = scheduler::get(); - auto *p = s.current(); - pid_t ppid = p->pid; - - log::debug(logs::syscall, "Process %d calling fork()", ppid); - - pid_t pid = p->fork(); - - p = s.current(); - log::debug(logs::syscall, "Process %d's fork: returning %d from process %d", ppid, pid, p->pid); - - return pid; -} - -} // namespace syscalls diff --git a/src/kernel/syscalls/getpid.cpp b/src/kernel/syscalls/getpid.cpp deleted file mode 100644 index 0424a1f..0000000 --- a/src/kernel/syscalls/getpid.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "log.h" -#include "scheduler.h" - -namespace syscalls { - -pid_t -getpid() -{ - auto &s = scheduler::get(); - auto *p = s.current(); - return p->pid; -} - -} // namespace syscalls diff --git a/src/kernel/syscalls/message.cpp b/src/kernel/syscalls/message.cpp deleted file mode 100644 index 11f79d3..0000000 --- a/src/kernel/syscalls/message.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "log.h" -#include "scheduler.h" - -namespace syscalls { - -void -message(const char *message) -{ - auto &s = scheduler::get(); - auto *p = s.current(); - log::info(logs::syscall, "Message[%d]: %s", p->pid, message); -} - -} // namespace syscalls diff --git a/src/kernel/syscalls/noop.cpp b/src/kernel/syscalls/object.cpp similarity index 54% rename from src/kernel/syscalls/noop.cpp rename to src/kernel/syscalls/object.cpp index 7654693..2f50471 100644 --- a/src/kernel/syscalls/noop.cpp +++ b/src/kernel/syscalls/object.cpp @@ -1,14 +1,24 @@ +#include "j6/errors.h" +#include "j6/types.h" + #include "log.h" #include "scheduler.h" namespace syscalls { -void -noop() +j6_status_t +object_noop() { auto &s = scheduler::get(); auto *p = s.current(); log::debug(logs::syscall, "Process %d called noop syscall.", p->pid); + return j6_status_ok; +} + +j6_status_t +object_wait(j6_handle_t, j6_signal_t, j6_signal_t*) +{ + return j6_err_nyi; } } // namespace syscalls diff --git a/src/kernel/syscalls/pause.cpp b/src/kernel/syscalls/pause.cpp deleted file mode 100644 index 8c03990..0000000 --- a/src/kernel/syscalls/pause.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "log.h" -#include "scheduler.h" - -namespace syscalls { - -void -pause() -{ - auto &s = scheduler::get(); - auto *p = s.current(); - p->wait_on_signal(-1ull); - s.schedule(); -} - -} // namespace syscalls diff --git a/src/kernel/syscalls/process.cpp b/src/kernel/syscalls/process.cpp new file mode 100644 index 0000000..5a718b4 --- /dev/null +++ b/src/kernel/syscalls/process.cpp @@ -0,0 +1,93 @@ +#include "j6/errors.h" +#include "j6/types.h" + +#include "log.h" +#include "scheduler.h" + +namespace syscalls { + +j6_status_t +process_exit(int64_t status) +{ + auto &s = scheduler::get(); + auto *p = s.current(); + log::debug(logs::syscall, "Process %d exiting with code %d", p->pid, status); + + p->exit(status); + s.schedule(); + + log::error(logs::syscall, "returned to exit syscall"); + return j6_err_unexpected; +} + +j6_status_t +process_fork(pid_t *pid) +{ + if (pid == nullptr) { + return j6_err_invalid_arg; + } + + auto &s = scheduler::get(); + auto *p = s.current(); + pid_t ppid = p->pid; + + log::debug(logs::syscall, "Process %d calling fork(%016llx)", ppid, pid); + + *pid = p->fork(); + + p = s.current(); + log::debug(logs::syscall, "Process %d's fork: returning %d from process %d", ppid, *pid, p->pid); + + return j6_status_ok; +} + +j6_status_t +process_getpid(pid_t *pid) +{ + if (pid == nullptr) { + return j6_err_invalid_arg; + } + + auto &s = scheduler::get(); + auto *p = s.current(); + + *pid = p->pid; + return j6_status_ok; +} + +j6_status_t +process_log(const char *message) +{ + if (message == nullptr) { + return j6_err_invalid_arg; + } + + auto &s = scheduler::get(); + auto *p = s.current(); + log::info(logs::syscall, "Message[%d]: %s", p->pid, message); + return j6_status_ok; +} + +j6_status_t +process_pause() +{ + auto &s = scheduler::get(); + auto *p = s.current(); + p->wait_on_signal(-1ull); + s.schedule(); + return j6_status_ok; +} + +j6_status_t +process_sleep(uint64_t til) +{ + auto &s = scheduler::get(); + auto *p = s.current(); + log::debug(logs::syscall, "Process %d sleeping until %d", p->pid, til); + + p->wait_on_time(til); + s.schedule(); + return j6_status_ok; +} + +} // namespace syscalls diff --git a/src/kernel/syscalls/sleep.cpp b/src/kernel/syscalls/sleep.cpp deleted file mode 100644 index 58ef385..0000000 --- a/src/kernel/syscalls/sleep.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "log.h" -#include "scheduler.h" - -namespace syscalls { - -void -sleep(uint64_t til) -{ - auto &s = scheduler::get(); - auto *p = s.current(); - log::debug(logs::syscall, "Process %d sleeping until %d", p->pid, til); - - p->wait_on_time(til); - s.schedule(); -} - -} // namespace syscalls