From f755cd38fe77dc460e3c6a75c2c6aa15ff0459a2 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Tue, 20 Jun 2023 14:10:28 -0700 Subject: [PATCH] [zion] Move thread syscalls to the new format. --- zion/CMakeLists.txt | 1 + zion/include/zcall.h | 15 +++++++++----- zion/include/ztypes.h | 4 ++-- zion/syscall/syscall.cpp | 42 ++++----------------------------------- zion/syscall/syscall.h | 7 +++++++ zion/syscall/thread.cpp | 34 +++++++++++++++++++++++++++++++ zion/syscall/thread.h | 7 +++++++ zion/usr/zcall.cpp | 13 ------------ zion/usr/zcall_internal.h | 7 ------- 9 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 zion/syscall/thread.cpp create mode 100644 zion/syscall/thread.h diff --git a/zion/CMakeLists.txt b/zion/CMakeLists.txt index c069f7a..932e60b 100644 --- a/zion/CMakeLists.txt +++ b/zion/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable(zion scheduler/scheduler.cpp syscall/process.cpp syscall/syscall.cpp + syscall/thread.cpp syscall/syscall_enter.s zion.cpp) diff --git a/zion/include/zcall.h b/zion/include/zcall.h index 6fb2dd7..7451833 100644 --- a/zion/include/zcall.h +++ b/zion/include/zcall.h @@ -4,6 +4,13 @@ #include "ztypes.h" +#define SYS0(name) \ + struct Z##name##Req {}; \ + [[nodiscard]] inline z_err_t Z##name() { \ + Z##name##Req req{}; \ + return SysCall1(kZion##name, &req); \ + } + #define SYS1(name, t1, a1) \ struct Z##name##Req { \ t1 a1; \ @@ -86,11 +93,9 @@ SYS5(ProcessSpawn, z_cap_t, proc_cap, z_cap_t, bootstrap_cap, z_cap_t*, new_proc_cap, z_cap_t*, new_vmas_cap, z_cap_t*, new_bootstrap_cap); SYS2(ThreadCreate, z_cap_t, proc_cap, z_cap_t*, thread_cap); - -[[nodiscard]] z_err_t ZThreadStart(z_cap_t thread_cap, uint64_t entry, - uint64_t arg1, uint64_t arg2); - -void ZThreadExit(); +SYS4(ThreadStart, z_cap_t, thread_cap, uint64_t, entry, uint64_t, arg1, + uint64_t, arg2); +SYS0(ThreadExit); [[nodiscard]] z_err_t ZAddressSpaceMap(z_cap_t vmas_cap, uint64_t vmas_offset, z_cap_t vmmo_cap, uint64_t* vaddr); diff --git a/zion/include/ztypes.h b/zion/include/ztypes.h index 492b2a3..29b9579 100644 --- a/zion/include/ztypes.h +++ b/zion/include/ztypes.h @@ -35,8 +35,8 @@ const uint64_t kZionProcessSpawn = 0x2; // Thread Calls. const uint64_t kZionThreadCreate = 0x10; -#define Z_THREAD_START 0x11 -#define Z_THREAD_EXIT 0x12 +const uint64_t kZionThreadStart = 0x11; +const uint64_t kZionThreadExit = 0x12; // Memory Calls #define Z_ADDRESS_SPACE_MAP 0x21 diff --git a/zion/syscall/syscall.cpp b/zion/syscall/syscall.cpp index ddc3ef0..7cfb9bb 100644 --- a/zion/syscall/syscall.cpp +++ b/zion/syscall/syscall.cpp @@ -14,15 +14,9 @@ #include "scheduler/process_manager.h" #include "scheduler/scheduler.h" #include "syscall/process.h" +#include "syscall/thread.h" #include "usr/zcall_internal.h" -#define RET_IF_NULL(expr) \ - { \ - if (!expr) { \ - return Z_ERR_CAP_TYPE; \ - } \ - } - #define EFER 0xC0000080 #define STAR 0xC0000081 #define LSTAR 0xC0000082 @@ -66,30 +60,6 @@ z_err_t ValidateCap(const RefPtr& cap, uint64_t permissions) { return Z_OK; } -z_err_t ThreadCreate(ZThreadCreateReq* req) { - auto& curr_proc = gScheduler->CurrentProcess(); - auto cap = curr_proc.GetCapability(req->proc_cap); - RET_ERR(ValidateCap(cap, ZC_PROC_SPAWN_THREAD)); - - auto parent_proc = cap->obj(); - RET_IF_NULL(parent_proc); - auto thread = parent_proc->CreateThread(); - *req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE); - return Z_OK; -} - -z_err_t ThreadStart(ZThreadStartReq* req) { - auto& curr_proc = gScheduler->CurrentProcess(); - auto cap = curr_proc.GetCapability(req->thread_cap); - RET_ERR(ValidateCap(cap, ZC_WRITE)); - - auto thread = cap->obj(); - RET_IF_NULL(thread); - // FIXME: validate entry point is in user space. - thread->Start(req->entry, req->arg1, req->arg2); - return Z_OK; -} - z_err_t AddressSpaceMap(ZAddressSpaceMapReq* req, ZAddressSpaceMapResp* resp) { auto& curr_proc = gScheduler->CurrentProcess(); auto vmas_cap = curr_proc.GetCapability(req->vmas_cap); @@ -248,14 +218,10 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) { // syscall/process.h CASE(ProcessExit); CASE(ProcessSpawn); + // syscall/thread.h CASE(ThreadCreate); - case Z_THREAD_START: - return ThreadStart(reinterpret_cast(req)); - case Z_THREAD_EXIT: - thread->Exit(); - panic("Returned from thread exit"); - break; - + CASE(ThreadStart); + CASE(ThreadExit); case Z_ADDRESS_SPACE_MAP: return AddressSpaceMap(reinterpret_cast(req), reinterpret_cast(resp)); diff --git a/zion/syscall/syscall.h b/zion/syscall/syscall.h index 1bd79ee..c6723ff 100644 --- a/zion/syscall/syscall.h +++ b/zion/syscall/syscall.h @@ -7,3 +7,10 @@ void InitSyscall(); // FIXME: This probably belongs in capability.h z_err_t ValidateCap(const RefPtr& cap, uint64_t permissions); + +#define RET_IF_NULL(expr) \ + { \ + if (!expr) { \ + return Z_ERR_CAP_TYPE; \ + } \ + } diff --git a/zion/syscall/thread.cpp b/zion/syscall/thread.cpp new file mode 100644 index 0000000..68a9be1 --- /dev/null +++ b/zion/syscall/thread.cpp @@ -0,0 +1,34 @@ +#include "syscall/thread.h" + +#include "scheduler/scheduler.h" +#include "syscall/syscall.h" + +z_err_t ThreadCreate(ZThreadCreateReq* req) { + auto& curr_proc = gScheduler->CurrentProcess(); + auto cap = curr_proc.GetCapability(req->proc_cap); + RET_ERR(ValidateCap(cap, ZC_PROC_SPAWN_THREAD)); + + auto parent_proc = cap->obj(); + RET_IF_NULL(parent_proc); + auto thread = parent_proc->CreateThread(); + *req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE); + return Z_OK; +} + +z_err_t ThreadStart(ZThreadStartReq* req) { + auto& curr_proc = gScheduler->CurrentProcess(); + auto cap = curr_proc.GetCapability(req->thread_cap); + RET_ERR(ValidateCap(cap, ZC_WRITE)); + + auto thread = cap->obj(); + RET_IF_NULL(thread); + // FIXME: validate entry point is in user space. + thread->Start(req->entry, req->arg1, req->arg2); + return Z_OK; +} + +z_err_t ThreadExit(ZThreadExitReq*) { + auto curr_thread = gScheduler->CurrentThread(); + curr_thread->Exit(); + panic("Returned from thread exit"); +} diff --git a/zion/syscall/thread.h b/zion/syscall/thread.h new file mode 100644 index 0000000..0d17180 --- /dev/null +++ b/zion/syscall/thread.h @@ -0,0 +1,7 @@ +#pragma once + +#include "include/zcall.h" + +z_err_t ThreadCreate(ZThreadCreateReq* req); +z_err_t ThreadStart(ZThreadStartReq* req); +z_err_t ThreadExit(ZThreadExitReq*); diff --git a/zion/usr/zcall.cpp b/zion/usr/zcall.cpp index f2fedc9..0667292 100644 --- a/zion/usr/zcall.cpp +++ b/zion/usr/zcall.cpp @@ -19,19 +19,6 @@ z_err_t SysCall1(uint64_t number, const void* first) { return SysCall2(number, first, 0); } -z_err_t ZThreadStart(z_cap_t thread_cap, uint64_t entry, uint64_t arg1, - uint64_t arg2) { - ZThreadStartReq req{ - .thread_cap = thread_cap, - .entry = entry, - .arg1 = arg1, - .arg2 = arg2, - }; - return SysCall1(Z_THREAD_START, &req); -} - -void ZThreadExit() { SysCall0(Z_THREAD_EXIT); } - z_err_t ZAddressSpaceMap(z_cap_t vmas_cap, uint64_t vmas_offset, z_cap_t vmmo_cap, uint64_t* vaddr) { ZAddressSpaceMapReq req{ diff --git a/zion/usr/zcall_internal.h b/zion/usr/zcall_internal.h index ee325dd..e6bb95e 100644 --- a/zion/usr/zcall_internal.h +++ b/zion/usr/zcall_internal.h @@ -4,13 +4,6 @@ #include "include/ztypes.h" -struct ZThreadStartReq { - z_cap_t thread_cap; - uint64_t entry; - uint64_t arg1; - uint64_t arg2; -}; - struct ZAddressSpaceMapReq { z_cap_t vmas_cap; z_cap_t vmas_offset;