[kernel] Add userspace threading

Implement the syscalls necessary for threads to create other threads in
their same process. This involved rearranging a number of syscalls, as
well as implementing object_wait and a basic implementation of a
process' list of handles.
This commit is contained in:
2020-07-26 16:02:38 -07:00
parent 4cf222a5bb
commit ae3290c53d
22 changed files with 481 additions and 255 deletions

View File

@@ -2,24 +2,32 @@
#include "j6/types.h"
#include "log.h"
#include "objects/process.h"
#include "objects/thread.h"
#include "scheduler.h"
namespace syscalls {
j6_status_t
object_noop()
object_wait(j6_handle_t handle, j6_signal_t mask, j6_signal_t *sigs)
{
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());
return j6_status_ok;
}
scheduler &s = scheduler::get();
thread *th = thread::from_tcb(s.current());
process &p = th->parent();
j6_status_t
object_wait(j6_handle_t, j6_signal_t, j6_signal_t*)
{
return j6_err_nyi;
kobject *obj = p.lookup_handle(handle);
if (!obj)
return j6_err_invalid_arg;
obj->add_blocked_thread(th);
th->wait_on_signals(obj, mask);
s.schedule();
j6_status_t result = th->get_wait_result();
if (result == j6_status_ok) {
*sigs = th->get_wait_data();
}
return result;
}
} // namespace syscalls

View File

@@ -1,28 +1,13 @@
#include "j6/errors.h"
#include "j6/types.h"
#include "objects/process.h"
#include "log.h"
#include "objects/process.h"
#include "objects/thread.h"
#include "scheduler.h"
namespace syscalls {
j6_status_t
process_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);
s.schedule();
log::error(logs::syscall, "returned to exit syscall");
return j6_err_unexpected;
}
j6_status_t
process_koid(j6_koid_t *koid)
{
@@ -38,41 +23,19 @@ process_koid(j6_koid_t *koid)
}
j6_status_t
process_log(const char *message)
{
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);
return j6_status_ok;
}
j6_status_t
process_pause()
process_exit(int64_t status)
{
auto &s = scheduler::get();
TCB *tcb = s.current();
thread *th = thread::from_tcb(tcb);
th->wait_on_signals(th, -1ull);
process &p = thread::from_tcb(tcb)->parent();
log::debug(logs::syscall, "Process %llx exiting with code %d", p.koid(), status);
p.exit(status);
s.schedule();
return j6_status_ok;
}
j6_status_t
process_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);
th->wait_on_time(til);
s.schedule();
return j6_status_ok;
log::error(logs::syscall, "returned to exit syscall");
return j6_err_unexpected;
}
} // namespace syscalls

View File

@@ -0,0 +1,33 @@
#include "j6/errors.h"
#include "j6/types.h"
#include "log.h"
#include "scheduler.h"
namespace syscalls {
j6_status_t
system_log(const char *message)
{
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);
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());
return j6_status_ok;
}
} // namespace syscalls

View File

@@ -0,0 +1,80 @@
#include "j6/errors.h"
#include "j6/types.h"
#include "log.h"
#include "objects/process.h"
#include "scheduler.h"
namespace syscalls {
j6_status_t
thread_koid(j6_koid_t *koid)
{
if (koid == nullptr) {
return j6_err_invalid_arg;
}
TCB *tcb = scheduler::get().current();
*koid = thread::from_tcb(tcb)->koid();
return j6_status_ok;
}
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 *child = p.create_thread(scheduler::default_priority);
child->add_thunk_user(reinterpret_cast<uintptr_t>(rip));
*handle = p.add_handle(child);
s.add_thread(child->tcb());
log::debug(logs::syscall, "Thread %llx spawned new thread %llx, handle %d",
parent->koid(), child->koid(), *handle);
return j6_status_ok;
}
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);
s.schedule();
log::error(logs::syscall, "returned to exit syscall");
return j6_err_unexpected;
}
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);
s.schedule();
return j6_status_ok;
}
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);
th->wait_on_time(til);
s.schedule();
return j6_status_ok;
}
} // namespace syscalls