[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

@@ -14,6 +14,8 @@ public:
channel(); channel();
virtual ~channel(); virtual ~channel();
static constexpr kobject::type type = kobject::type::channel;
/// Check if the channel has space for a message to be sent /// Check if the channel has space for a message to be sent
inline bool can_send() const { return check_signal(j6_signal_channel_can_send); } inline bool can_send() const { return check_signal(j6_signal_channel_can_send); }

View File

@@ -13,6 +13,8 @@ public:
endpoint(); endpoint();
virtual ~endpoint(); virtual ~endpoint();
static constexpr kobject::type type = kobject::type::endpoint;
/// Close the endpoint, waking all waiting processes with an error /// Close the endpoint, waking all waiting processes with an error
void close(); void close();

View File

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

View File

@@ -24,6 +24,8 @@ public:
/// Destructor. /// Destructor.
virtual ~process(); virtual ~process();
static constexpr kobject::type type = kobject::type::process;
/// Get the currently executing process. /// Get the currently executing process.
static process & current(); static process & current();

View File

@@ -50,6 +50,8 @@ public:
/// Destructor /// Destructor
virtual ~thread(); virtual ~thread();
static constexpr kobject::type type = kobject::type::thread;
/// Get the currently executing thread. /// Get the currently executing thread.
static thread & current(); static thread & current();

View File

@@ -38,6 +38,8 @@ public:
vm_area(size_t size, vm_flags flags = vm_flags::none); vm_area(size_t size, vm_flags flags = vm_flags::none);
virtual ~vm_area(); virtual ~vm_area();
static constexpr kobject::type type = kobject::type::vma;
/// Get the current virtual size of the memory area /// Get the current virtual size of the memory area
size_t size() const { return m_size; } size_t size() const { return m_size; }

View File

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

View File

@@ -3,97 +3,53 @@
#include "log.h" #include "log.h"
#include "objects/endpoint.h" #include "objects/endpoint.h"
#include "objects/process.h" #include "syscalls/helpers.h"
#include "scheduler.h"
namespace syscalls { namespace syscalls {
j6_status_t j6_status_t
endpoint_create(j6_handle_t *handle) endpoint_create(j6_handle_t *handle)
{ {
scheduler &s = scheduler::get(); construct_handle<endpoint>(handle);
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
endpoint *e = new endpoint;
*handle = p.add_handle(e);
return j6_status_ok; return j6_status_ok;
} }
j6_status_t j6_status_t
endpoint_close(j6_handle_t handle) endpoint_close(j6_handle_t handle)
{ {
scheduler &s = scheduler::get(); endpoint *e = remove_handle<endpoint>(handle);
TCB *tcb = s.current(); if (!e) return j6_err_invalid_arg;
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);
e->close(); e->close();
return j6_status_ok; return j6_status_ok;
} }
j6_status_t j6_status_t
endpoint_send(j6_handle_t handle, size_t len, void *data) endpoint_send(j6_handle_t handle, size_t len, void *data)
{ {
scheduler &s = scheduler::get(); endpoint *e = get_handle<endpoint>(handle);
TCB *tcb = s.current(); if (!e) return j6_err_invalid_arg;
thread *parent = thread::from_tcb(tcb); return e->send(len, data);
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;
} }
j6_status_t j6_status_t
endpoint_receive(j6_handle_t handle, size_t *len, void *data) endpoint_receive(j6_handle_t handle, size_t *len, void *data)
{ {
scheduler &s = scheduler::get(); endpoint *e = get_handle<endpoint>(handle);
TCB *tcb = s.current(); if (!e) return j6_err_invalid_arg;
thread *parent = thread::from_tcb(tcb); return e->receive(len, data);
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;
} }
j6_status_t j6_status_t
endpoint_sendrecv(j6_handle_t handle, size_t *len, void *data) endpoint_sendrecv(j6_handle_t handle, size_t *len, void *data)
{ {
scheduler &s = scheduler::get(); endpoint *e = get_handle<endpoint>(handle);
TCB *tcb = s.current(); if (!e) return j6_err_invalid_arg;
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); j6_status_t status = e->send(*len, data);
if (status != j6_status_ok) if (status != j6_status_ok)
return status; return status;
status = e->receive(len, data); return e->receive(len, data);
return status;
} }
} // namespace syscalls } // 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 "log.h"
#include "objects/process.h" #include "objects/process.h"
#include "objects/thread.h"
#include "scheduler.h" #include "scheduler.h"
namespace syscalls { namespace syscalls {
@@ -11,14 +10,10 @@ namespace syscalls {
j6_status_t j6_status_t
process_koid(j6_koid_t *koid) process_koid(j6_koid_t *koid)
{ {
if (koid == nullptr) { if (koid == nullptr)
return j6_err_invalid_arg; return j6_err_invalid_arg;
}
TCB *tcb = scheduler::get().current(); *koid = process::current().koid();
process &p = thread::from_tcb(tcb)->parent();
*koid = p.koid();
return j6_status_ok; return j6_status_ok;
} }
@@ -26,8 +21,7 @@ j6_status_t
process_exit(int64_t status) process_exit(int64_t status)
{ {
auto &s = scheduler::get(); auto &s = scheduler::get();
TCB *tcb = s.current(); process &p = process::current();
process &p = thread::from_tcb(tcb)->parent();
log::debug(logs::syscall, "Process %llx exiting with code %d", p.koid(), status); log::debug(logs::syscall, "Process %llx exiting with code %d", p.koid(), status);

View File

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

View File

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