diff --git a/zion/include/zcall.h b/zion/include/zcall.h index 058295d..d4d4852 100644 --- a/zion/include/zcall.h +++ b/zion/include/zcall.h @@ -4,7 +4,33 @@ #include "ztypes.h" -void ZProcessExit(uint64_t code); +#define SYS1(name, num, t1, a1) \ + struct Z##name##Req { \ + t1 a1; \ + }; \ + [[nodiscard]] inline z_err_t Z##name(t1 a1) { \ + Z##name##Req req{ \ + .a1 = a1, \ + }; \ + return SysCall1(num, &req); \ + } + +#define SYS2(name, num, t1, a1, t2, a2) \ + struct Z##name##Req { \ + t1 a1; \ + t2 a2; \ + }; \ + [[nodiscard]] inline z_err_t Z##name(t1 a1, t2 a2) { \ + Z##name##Req req{ \ + .a1 = a1, \ + .a2 = a2, \ + }; \ + return SysCall1(num, &req); \ + } + +z_err_t SysCall1(uint64_t code, const void* req); + +SYS1(ProcessExit, Z_PROCESS_EXIT, uint64_t, code); [[nodiscard]] z_err_t ZProcessSpawn(z_cap_t proc_cap, z_cap_t bootstrap_cap, z_cap_t* new_proc_cap, @@ -16,7 +42,7 @@ void ZProcessExit(uint64_t code); uint64_t entry, uint64_t arg1, uint64_t arg2); -[[nodiscard]] z_err_t ZThreadCreate(z_cap_t proc_cap, z_cap_t* thread_cap); +SYS2(ThreadCreate, Z_THREAD_CREATE, 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); diff --git a/zion/syscall/syscall.cpp b/zion/syscall/syscall.cpp index a24798a..cb22352 100644 --- a/zion/syscall/syscall.cpp +++ b/zion/syscall/syscall.cpp @@ -89,7 +89,7 @@ z_err_t ProcessSpawn(ZProcessSpawnReq* req, ZProcessSpawnResp* resp) { return Z_OK; } -z_err_t ThreadCreate(ZThreadCreateReq* req, ZThreadCreateResp* resp) { +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)); @@ -97,8 +97,7 @@ z_err_t ThreadCreate(ZThreadCreateReq* req, ZThreadCreateResp* resp) { auto parent_proc = cap->obj(); RET_IF_NULL(parent_proc); auto thread = parent_proc->CreateThread(); - resp->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE); - + *req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE); return Z_OK; } @@ -267,7 +266,8 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) { switch (call_id) { case Z_PROCESS_EXIT: // FIXME: kill process here. - dbgln("Exit code: %x", req); + dbgln("Exiting: %m", req); + dbgln("Exit code: %x", reinterpret_cast(req)->code); thread->Exit(); panic("Returned from thread exit"); break; @@ -275,8 +275,7 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) { return ProcessSpawn(reinterpret_cast(req), reinterpret_cast(resp)); case Z_THREAD_CREATE: - return ThreadCreate(reinterpret_cast(req), - reinterpret_cast(resp)); + return ThreadCreate(reinterpret_cast(req)); case Z_THREAD_START: return ThreadStart(reinterpret_cast(req)); case Z_THREAD_EXIT: diff --git a/zion/usr/crt0.s b/zion/usr/crt0.s index 8e5a49d..d179eb0 100644 --- a/zion/usr/crt0.s +++ b/zion/usr/crt0.s @@ -8,6 +8,7 @@ _start: _exit: // EXIT syscall. mov $1, %rdi - // Return code as a param. - mov %rax, %rsi + // Return code as a "struct" on the stack. + push %rax + mov %rsp, %rsi syscall diff --git a/zion/usr/zcall.cpp b/zion/usr/zcall.cpp index 342b286..dfd71af 100644 --- a/zion/usr/zcall.cpp +++ b/zion/usr/zcall.cpp @@ -19,10 +19,6 @@ z_err_t SysCall1(uint64_t number, const void* first) { return SysCall2(number, first, 0); } -void ZProcessExit(uint64_t code) { - SysCall1(Z_PROCESS_EXIT, reinterpret_cast(code)); -} - z_err_t ZProcessSpawn(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) { @@ -38,16 +34,6 @@ z_err_t ZProcessSpawn(z_cap_t proc_cap, z_cap_t bootstrap_cap, return ret; } -z_err_t ZThreadCreate(z_cap_t proc_cap, z_cap_t* thread_cap) { - ZThreadCreateReq req{ - .proc_cap = proc_cap, - }; - ZThreadCreateResp resp; - z_err_t ret = SysCall2(Z_THREAD_CREATE, &req, &resp); - *thread_cap = resp.thread_cap; - return ret; -} - z_err_t ZThreadStart(z_cap_t thread_cap, uint64_t entry, uint64_t arg1, uint64_t arg2) { ZThreadStartReq req{ diff --git a/zion/usr/zcall_internal.h b/zion/usr/zcall_internal.h index 556a0db..936f47e 100644 --- a/zion/usr/zcall_internal.h +++ b/zion/usr/zcall_internal.h @@ -15,14 +15,6 @@ struct ZProcessSpawnResp { z_cap_t bootstrap_cap; }; -struct ZThreadCreateReq { - z_cap_t proc_cap; -}; - -struct ZThreadCreateResp { - z_cap_t thread_cap; -}; - struct ZThreadStartReq { z_cap_t thread_cap; uint64_t entry;