[zion] Add a thread wait syscall

This commit is contained in:
Drew Galbraith 2023-06-22 02:17:50 -07:00
parent f0e8ce14a4
commit 36d82370c1
9 changed files with 45 additions and 8 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <glacier/status/error.h>
#include <stdint.h>
class Thread {
@ -9,6 +10,8 @@ class Thread {
Thread() : thread_cap_(0) {}
Thread(Entry e, const void* arg1);
[[nodiscard]] glcr::ErrorCode Join();
private:
uint64_t thread_cap_;
};

View File

@ -10,7 +10,7 @@ namespace {
extern "C" void thread_entry(Thread::Entry entry, void* arg1) {
entry(arg1);
ZThreadExit();
(void)ZThreadExit();
}
} // namespace
@ -21,3 +21,5 @@ Thread::Thread(Entry e, const void* arg1) {
reinterpret_cast<uint64_t>(e),
reinterpret_cast<uint64_t>(arg1)));
}
glcr::ErrorCode Thread::Join() { return ZThreadWait(thread_cap_); }

View File

@ -96,6 +96,7 @@ SYS2(ThreadCreate, z_cap_t, proc_cap, z_cap_t*, thread_cap);
SYS4(ThreadStart, z_cap_t, thread_cap, uint64_t, entry, uint64_t, arg1,
uint64_t, arg2);
SYS0(ThreadExit);
SYS1(ThreadWait, z_cap_t, thread_cap);
SYS4(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t,
vmmo_cap, uint64_t*, vaddr);

View File

@ -17,6 +17,7 @@ const uint64_t kZionProcessSpawn = 0x2;
const uint64_t kZionThreadCreate = 0x10;
const uint64_t kZionThreadStart = 0x11;
const uint64_t kZionThreadExit = 0x12;
const uint64_t kZionThreadWait = 0x13;
// Memory Calls
const uint64_t kZionAddressSpaceMap = 0x21;

View File

@ -69,5 +69,18 @@ void Thread::Exit() {
#endif
state_ = FINISHED;
process_.CheckState();
while (!blocked_threads_.size() == 0) {
auto thread = blocked_threads_.PopFront();
thread->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(thread);
}
gScheduler->Yield();
}
void Thread::Wait() {
// FIXME: We need synchronization code here.
auto thread = gScheduler->CurrentThread();
thread->SetState(Thread::BLOCKED);
blocked_threads_.PushBack(thread);
gScheduler->Yield();
}

View File

@ -48,6 +48,8 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
void SetState(State state) { state_ = state; }
void Exit();
void Wait();
private:
friend class glcr::MakeRefCountedFriend<Thread>;
Thread(Process& proc, uint64_t tid);
@ -68,4 +70,6 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
// Stack pointer to take when returning from userspace.
// I don't think me mind clobbering the stack here.
uint64_t rsp0_start_;
glcr::IntrusiveList<Thread> blocked_threads_;
};

View File

@ -55,6 +55,7 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
CASE(ThreadCreate);
CASE(ThreadStart);
CASE(ThreadExit);
CASE(ThreadWait);
// syscall/address_space.h
CASE(AddressSpaceMap);
// syscall/memory_object.h

View File

@ -3,18 +3,18 @@
#include "capability/capability.h"
#include "scheduler/scheduler.h"
z_err_t ThreadCreate(ZThreadCreateReq* req) {
glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req) {
auto& curr_proc = gScheduler->CurrentProcess();
auto cap = curr_proc.GetCapability(req->proc_cap);
RET_ERR(ValidateCapability<Process>(cap, ZC_PROC_SPAWN_THREAD));
auto parent_proc = cap->obj<Process>();
auto thread = parent_proc->CreateThread();
*req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE);
*req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE | ZC_READ);
return glcr::OK;
}
z_err_t ThreadStart(ZThreadStartReq* req) {
glcr::ErrorCode ThreadStart(ZThreadStartReq* req) {
auto& curr_proc = gScheduler->CurrentProcess();
auto cap = curr_proc.GetCapability(req->thread_cap);
RET_ERR(ValidateCapability<Thread>(cap, ZC_WRITE));
@ -25,8 +25,17 @@ z_err_t ThreadStart(ZThreadStartReq* req) {
return glcr::OK;
}
z_err_t ThreadExit(ZThreadExitReq*) {
glcr::ErrorCode ThreadExit(ZThreadExitReq*) {
auto curr_thread = gScheduler->CurrentThread();
curr_thread->Exit();
panic("Returned from thread exit");
}
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req) {
auto& curr_proc = gScheduler->CurrentProcess();
auto cap = curr_proc.GetCapability(req->thread_cap);
RET_ERR(ValidateCapability<Thread>(cap, ZC_READ));
auto thread = cap->obj<Thread>();
thread->Wait();
return glcr::OK;
}

View File

@ -1,7 +1,10 @@
#pragma once
#include <glacier/status/error.h>
#include "include/zcall.h"
z_err_t ThreadCreate(ZThreadCreateReq* req);
z_err_t ThreadStart(ZThreadStartReq* req);
z_err_t ThreadExit(ZThreadExitReq*);
glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req);
glcr::ErrorCode ThreadStart(ZThreadStartReq* req);
glcr::ErrorCode ThreadExit(ZThreadExitReq*);
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req);