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

@@ -1,12 +1,15 @@
#include <stdint.h>
#include <stdlib.h>
#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;
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#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
}

View File

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

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

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