Organize system calls

* syscalls should all return j6_status_t now
* syscalls are grouped by category in name as well as in files
This commit is contained in:
Justin C. Miller
2019-07-05 22:27:16 -07:00
parent 19cd01ef8d
commit b056d95920
16 changed files with 149 additions and 144 deletions

View File

@@ -44,13 +44,8 @@ modules:
- src/kernel/serial.cpp - src/kernel/serial.cpp
- src/kernel/syscall.cpp - src/kernel/syscall.cpp
- src/kernel/syscall.s - src/kernel/syscall.s
- src/kernel/syscalls/exit.cpp - src/kernel/syscalls/object.cpp
- src/kernel/syscalls/fork.cpp - src/kernel/syscalls/process.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/task.s - src/kernel/task.s
- src/kernel/crtn.s - src/kernel/crtn.s

View File

@@ -1,12 +1,15 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include "j6/types.h"
#include "j6/errors.h"
extern "C" { extern "C" {
int32_t getpid(); j6_status_t getpid(uint64_t *);
int32_t fork(); j6_status_t fork(uint64_t *);
void sleep(uint64_t til); j6_status_t sleep(uint64_t til);
void debug(); j6_status_t debug();
void message(const char *msg); j6_status_t message(const char *msg);
int main(int, const char **); int main(int, const char **);
} }
@@ -15,10 +18,21 @@ extern "C" {
int int
main(int argc, const char **argv) main(int argc, const char **argv)
{ {
int32_t pid = getpid(); uint64_t pid = 0;
int32_t child = fork(); uint64_t child = 0;
j6_status_t result = fork(&child);
if (result != j6_status_ok)
return result;
message("hello from nulldrv!"); message("hello from nulldrv!");
result = getpid(&pid);
if (result != j6_status_ok)
return result;
for (int i = 1; i < 5; ++i) for (int i = 1; i < 5; ++i)
sleep(i*10); sleep(i*10);
return 0;
return pid;
} }

View File

@@ -11,8 +11,9 @@ getpid:
push rbp push rbp
mov rbp, rsp mov rbp, rsp
mov rax, 0x02 ; getpid syscall ; address of out var should already be in rdi
syscall ; pid is now already in rax, so just return mov rax, 0x13 ; getpid syscall
syscall ; result is now already in rax, so just return
pop rbp pop rbp
ret ret
@@ -33,7 +34,7 @@ sleep:
push rbp push rbp
mov rbp, rsp mov rbp, rsp
mov rax, 0x21 ; sleep syscall mov rax, 0x16 ; sleep syscall
syscall syscall
pop rbp pop rbp
@@ -44,8 +45,9 @@ fork:
push rbp push rbp
mov rbp, rsp mov rbp, rsp
mov rax, 0x03 ; address of out var should already be in rdi
syscall ; pid left in rax mov rax, 0x12
syscall ; result left in rax
pop rbp pop rbp
ret ret
@@ -57,7 +59,7 @@ message:
mov rbp, rsp mov rbp, rsp
; message should already be in rdi ; message should already be in rdi
mov rax, 0x10 mov rax, 0x14
syscall syscall
pop rbp pop rbp

View File

@@ -8,7 +8,7 @@
typedef uint64_t j6_koid_t; typedef uint64_t j6_koid_t;
/// Syscalls return status as this type /// 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 /// Handles are references and capabilities to other objects
typedef uint32_t j6_handle_t; typedef uint32_t j6_handle_t;

View File

@@ -8,8 +8,6 @@ class event :
public kobject public kobject
{ {
public: public:
static constexpr type type_id = type::event;
event() : event() :
kobject(type_id) {} kobject(type::event) {}
}; };

View File

@@ -14,10 +14,6 @@ extern "C" {
namespace syscalls { namespace syscalls {
void send() {}
void receive() {}
} // namespace syscalls } // namespace syscalls
uintptr_t syscall_registry[static_cast<unsigned>(syscall::MAX)]; uintptr_t syscall_registry[static_cast<unsigned>(syscall::MAX)];

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include "j6/types.h"
struct cpu_state; struct cpu_state;
@@ -19,7 +20,7 @@ void syscall_enable();
namespace syscalls namespace syscalls
{ {
#define SYSCALL(id, name, result, ...) result name (__VA_ARGS__); #define SYSCALL(id, name, ...) j6_status_t name (__VA_ARGS__);
#include "syscalls.inc" #include "syscalls.inc"
#undef SYSCALL #undef SYSCALL
} }

View File

@@ -1,12 +1,9 @@
SYSCALL(0x00, noop, void) SYSCALL(0x00, object_noop, void)
SYSCALL(0x01, exit, void, int64_t) SYSCALL(0x01, object_wait, j6_handle_t, j6_signal_t, j6_signal_t *)
SYSCALL(0x02, getpid, pid_t)
SYSCALL(0x03, fork, pid_t)
SYSCALL(0x10, message, void, const char *) SYSCALL(0x11, process_exit, int64_t)
SYSCALL(0x12, process_fork, pid_t*)
SYSCALL(0x20, pause, void) SYSCALL(0x13, process_getpid, pid_t*)
SYSCALL(0x21, sleep, void, uint64_t) SYSCALL(0x14, process_log, const char *)
SYSCALL(0x15, process_pause, void)
SYSCALL(0x30, send, void) SYSCALL(0x16, process_sleep, uint64_t)
SYSCALL(0x31, receive, void)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,14 +1,24 @@
#include "j6/errors.h"
#include "j6/types.h"
#include "log.h" #include "log.h"
#include "scheduler.h" #include "scheduler.h"
namespace syscalls { namespace syscalls {
void j6_status_t
noop() object_noop()
{ {
auto &s = scheduler::get(); auto &s = scheduler::get();
auto *p = s.current(); auto *p = s.current();
log::debug(logs::syscall, "Process %d called noop syscall.", p->pid); 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 } // namespace syscalls

View File

@@ -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

View File

@@ -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

View File

@@ -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