[kernel] Add syscall helpers

Added the syscalls/helpers.h file to templatize common kobject syscall
operations. Also moved most syscall implementations to using
process::current() and thread::current() instead of asking the
scheduler.
This commit is contained in:
2020-09-23 00:22:15 -07:00
parent 6780ab1b67
commit d4283731e4
12 changed files with 96 additions and 126 deletions

View File

@@ -2,60 +2,39 @@
#include "j6/types.h"
#include "objects/channel.h"
#include "objects/process.h"
#include "syscalls/helpers.h"
namespace syscalls {
j6_status_t
channel_create(j6_handle_t *handle)
{
process &p = process::current();
channel *c = new channel;
*handle = p.add_handle(c);
construct_handle<channel>(handle);
return j6_status_ok;
}
j6_status_t
channel_close(j6_handle_t handle)
{
process &p = process::current();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel)
return j6_err_invalid_arg;
p.remove_handle(handle);
channel *c = static_cast<channel*>(o);
channel *c = remove_handle<channel>(handle);
if (!c) return j6_err_invalid_arg;
c->close();
return j6_status_ok;
}
j6_status_t
channel_send(j6_handle_t handle, size_t *len, void *data)
{
process &p = process::current();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel)
return j6_err_invalid_arg;
channel *c = static_cast<channel*>(o);
channel *c = get_handle<channel>(handle);
if (!c) return j6_err_invalid_arg;
return c->enqueue(len, data);
}
j6_status_t
channel_receive(j6_handle_t handle, size_t *len, void *data)
{
process &p = process::current();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel)
return j6_err_invalid_arg;
channel *c = static_cast<channel*>(o);
channel *c = get_handle<channel>(handle);
if (!c) return j6_err_invalid_arg;
return c->dequeue(len, data);
}

View File

@@ -3,97 +3,53 @@
#include "log.h"
#include "objects/endpoint.h"
#include "objects/process.h"
#include "scheduler.h"
#include "syscalls/helpers.h"
namespace syscalls {
j6_status_t
endpoint_create(j6_handle_t *handle)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
endpoint *e = new endpoint;
*handle = p.add_handle(e);
construct_handle<endpoint>(handle);
return j6_status_ok;
}
j6_status_t
endpoint_close(j6_handle_t handle)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::endpoint)
return j6_err_invalid_arg;
p.remove_handle(handle);
endpoint *e = static_cast<endpoint*>(o);
endpoint *e = remove_handle<endpoint>(handle);
if (!e) return j6_err_invalid_arg;
e->close();
return j6_status_ok;
}
j6_status_t
endpoint_send(j6_handle_t handle, size_t len, void *data)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::endpoint)
return j6_err_invalid_arg;
endpoint *e = static_cast<endpoint*>(o);
j6_status_t status = e->send(len, data);
return status;
endpoint *e = get_handle<endpoint>(handle);
if (!e) return j6_err_invalid_arg;
return e->send(len, data);
}
j6_status_t
endpoint_receive(j6_handle_t handle, size_t *len, void *data)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::endpoint)
return j6_err_invalid_arg;
endpoint *e = static_cast<endpoint*>(o);
j6_status_t status = e->receive(len, data);
return status;
endpoint *e = get_handle<endpoint>(handle);
if (!e) return j6_err_invalid_arg;
return e->receive(len, data);
}
j6_status_t
endpoint_sendrecv(j6_handle_t handle, size_t *len, void *data)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
endpoint *e = get_handle<endpoint>(handle);
if (!e) return j6_err_invalid_arg;
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::endpoint)
return j6_err_invalid_arg;
endpoint *e = static_cast<endpoint*>(o);
j6_status_t status = e->send(*len, data);
if (status != j6_status_ok)
return status;
status = e->receive(len, data);
return status;
return e->receive(len, data);
}
} // namespace syscalls

View File

@@ -0,0 +1,40 @@
#pragma once
/// \file syscalls/helpers.h
/// Utility functions for use in syscall handler implementations
#include "j6/types.h"
#include "objects/process.h"
namespace syscalls {
template <typename T, typename... Args>
T * construct_handle(j6_handle_t *handle, Args... args)
{
process &p = process::current();
T *o = new T {args...};
*handle = p.add_handle(o);
return o;
}
template <typename T>
T * get_handle(j6_handle_t handle)
{
process &p = process::current();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != T::type)
return nullptr;
return static_cast<T*>(o);
}
template <typename T>
T * remove_handle(j6_handle_t handle)
{
T *o = get_handle<T>(handle);
if (o) {
process &p = process::current();
p.remove_handle(handle);
}
return o;
}
}

View File

@@ -3,7 +3,6 @@
#include "log.h"
#include "objects/process.h"
#include "objects/thread.h"
#include "scheduler.h"
namespace syscalls {
@@ -11,14 +10,10 @@ namespace syscalls {
j6_status_t
process_koid(j6_koid_t *koid)
{
if (koid == nullptr) {
if (koid == nullptr)
return j6_err_invalid_arg;
}
TCB *tcb = scheduler::get().current();
process &p = thread::from_tcb(tcb)->parent();
*koid = p.koid();
*koid = process::current().koid();
return j6_status_ok;
}
@@ -26,8 +21,7 @@ j6_status_t
process_exit(int64_t status)
{
auto &s = scheduler::get();
TCB *tcb = s.current();
process &p = thread::from_tcb(tcb)->parent();
process &p = process::current();
log::debug(logs::syscall, "Process %llx exiting with code %d", p.koid(), status);

View File

@@ -2,31 +2,26 @@
#include "j6/types.h"
#include "log.h"
#include "scheduler.h"
#include "objects/thread.h"
namespace syscalls {
j6_status_t
system_log(const char *message)
{
if (message == nullptr) {
if (message == nullptr)
return j6_err_invalid_arg;
}
auto &s = scheduler::get();
TCB *tcb = s.current();
thread *th = thread::from_tcb(tcb);
log::info(logs::syscall, "Message[%llx]: %s", th->koid(), message);
thread &th = thread::current();
log::info(logs::syscall, "Message[%llx]: %s", th.koid(), message);
return j6_status_ok;
}
j6_status_t
system_noop()
{
auto &s = scheduler::get();
TCB *tcb = s.current();
thread *th = thread::from_tcb(tcb);
log::debug(logs::syscall, "Thread %llx called noop syscall.", th->koid());
thread &th = thread::current();
log::debug(logs::syscall, "Thread %llx called noop syscall.", th.koid());
return j6_status_ok;
}

View File

@@ -3,6 +3,7 @@
#include "log.h"
#include "objects/process.h"
#include "objects/thread.h"
#include "scheduler.h"
namespace syscalls {
@@ -10,12 +11,10 @@ namespace syscalls {
j6_status_t
thread_koid(j6_koid_t *koid)
{
if (koid == nullptr) {
if (koid == nullptr)
return j6_err_invalid_arg;
}
TCB *tcb = scheduler::get().current();
*koid = thread::from_tcb(tcb)->koid();
*koid = thread::current().koid();
return j6_status_ok;
}
@@ -23,9 +22,8 @@ j6_status_t
thread_create(void *rip, j6_handle_t *handle)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
thread &parent = thread::current();
process &p = parent.parent();
thread *child = p.create_thread(scheduler::default_priority);
child->add_thunk_user(reinterpret_cast<uintptr_t>(rip));
@@ -33,7 +31,7 @@ thread_create(void *rip, j6_handle_t *handle)
s.add_thread(child->tcb());
log::debug(logs::syscall, "Thread %llx spawned new thread %llx, handle %d",
parent->koid(), child->koid(), *handle);
parent.koid(), child->koid(), *handle);
return j6_status_ok;
}
@@ -42,11 +40,9 @@ j6_status_t
thread_exit(int64_t status)
{
auto &s = scheduler::get();
TCB *tcb = s.current();
thread *th = thread::from_tcb(tcb);
log::debug(logs::syscall, "Thread %llx exiting with code %d", th->koid(), status);
th->exit(status);
thread &th = thread::current();
log::debug(logs::syscall, "Thread %llx exiting with code %d", th.koid(), status);
th.exit(status);
s.schedule();
log::error(logs::syscall, "returned to exit syscall");
@@ -57,9 +53,8 @@ j6_status_t
thread_pause()
{
auto &s = scheduler::get();
TCB *tcb = s.current();
thread *th = thread::from_tcb(tcb);
th->wait_on_signals(th, -1ull);
thread &th = thread::current();
th.wait_on_signals(&th, -1ull);
s.schedule();
return j6_status_ok;
}
@@ -68,11 +63,10 @@ j6_status_t
thread_sleep(uint64_t til)
{
auto &s = scheduler::get();
TCB *tcb = s.current();
thread *th = thread::from_tcb(tcb);
log::debug(logs::syscall, "Thread %llx sleeping until %llu", th->koid(), til);
thread &th = thread::current();
log::debug(logs::syscall, "Thread %llx sleeping until %llu", th.koid(), til);
th->wait_on_time(til);
th.wait_on_time(til);
s.schedule();
return j6_status_ok;
}