diff --git a/lib/mammoth/CMakeLists.txt b/lib/mammoth/CMakeLists.txt index b25e601..f462f82 100644 --- a/lib/mammoth/CMakeLists.txt +++ b/lib/mammoth/CMakeLists.txt @@ -7,7 +7,7 @@ add_library(mammoth_lib STATIC src/memory_region.cpp src/new.cpp src/process.cpp - src/port.cpp + src/port_client.cpp src/port_server.cpp src/thread.cpp ) diff --git a/lib/mammoth/include/mammoth/port.h b/lib/mammoth/include/mammoth/port.h deleted file mode 100644 index 4e64d60..0000000 --- a/lib/mammoth/include/mammoth/port.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -// FIXME: Split send and receive. -class Port { - public: - static glcr::ErrorOr Create(); - Port(uint64_t port_cap); - - template - z_err_t WriteMessage(const T& obj, uint64_t cap); - - glcr::ErrorCode WriteString(glcr::String str, uint64_t cap); - - private: - uint64_t port_cap_; -}; - -template -z_err_t Port::WriteMessage(const T& obj, uint64_t cap) { - return ZPortSend(port_cap_, sizeof(obj), &obj, 1, &cap); -} diff --git a/lib/mammoth/include/mammoth/port_client.h b/lib/mammoth/include/mammoth/port_client.h new file mode 100644 index 0000000..5f33db6 --- /dev/null +++ b/lib/mammoth/include/mammoth/port_client.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include +#include + +class PortClient { + public: + static PortClient AdoptPort(z_cap_t port_cap); + + template + z_err_t WriteMessage(const T& obj, z_cap_t cap); + + glcr::ErrorCode WriteString(glcr::String str, z_cap_t cap); + + z_cap_t cap() { return port_cap_; } + + private: + z_cap_t port_cap_; + + PortClient(z_cap_t port_cap); +}; + +template +z_err_t PortClient::WriteMessage(const T& obj, z_cap_t cap) { + return ZPortSend(port_cap_, sizeof(obj), &obj, 1, &cap); +} diff --git a/lib/mammoth/include/mammoth/port_server.h b/lib/mammoth/include/mammoth/port_server.h index 1cc783a..e8939e4 100644 --- a/lib/mammoth/include/mammoth/port_server.h +++ b/lib/mammoth/include/mammoth/port_server.h @@ -3,16 +3,20 @@ #include #include +#include "mammoth/port_client.h" + class PortServer { public: static glcr::ErrorOr Create(); static PortServer AdoptCap(z_cap_t cap); - glcr::ErrorCode CreateClient(z_cap_t* new_port); + glcr::ErrorOr CreateClient(); glcr::ErrorCode RecvCap(uint64_t* num_bytes, char* msg, uint64_t* cap); glcr::ErrorCode PollForIntCap(uint64_t* msg, uint64_t* cap); + z_cap_t cap() { return port_cap_; } + private: z_cap_t port_cap_; diff --git a/lib/mammoth/src/port.cpp b/lib/mammoth/src/port.cpp deleted file mode 100644 index b810cde..0000000 --- a/lib/mammoth/src/port.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "mammoth/port.h" - -#include -#include - -#include "mammoth/debug.h" - -glcr::ErrorOr Port::Create() { - z_cap_t port; - RET_ERR(ZPortCreate(&port)); - return Port(port); -} -Port::Port(uint64_t port_cap) : port_cap_(port_cap) {} - -glcr::ErrorCode Port::WriteString(glcr::String str, uint64_t cap) { - return ZPortSend(port_cap_, str.length() + 1, str.cstr(), 1, &cap); -} diff --git a/lib/mammoth/src/port_client.cpp b/lib/mammoth/src/port_client.cpp new file mode 100644 index 0000000..64b2820 --- /dev/null +++ b/lib/mammoth/src/port_client.cpp @@ -0,0 +1,13 @@ +#include "mammoth/port_client.h" + +#include +#include + +#include "mammoth/debug.h" + +PortClient PortClient::AdoptPort(z_cap_t cap) { return PortClient(cap); } +PortClient::PortClient(z_cap_t port_cap) : port_cap_(port_cap) {} + +glcr::ErrorCode PortClient::WriteString(glcr::String str, z_cap_t cap) { + return ZPortSend(port_cap_, str.length() + 1, str.cstr(), 1, &cap); +} diff --git a/lib/mammoth/src/port_server.cpp b/lib/mammoth/src/port_server.cpp index 9de44c8..13c0e83 100644 --- a/lib/mammoth/src/port_server.cpp +++ b/lib/mammoth/src/port_server.cpp @@ -12,9 +12,11 @@ PortServer PortServer::AdoptCap(z_cap_t cap) { return PortServer(cap); } PortServer::PortServer(z_cap_t port_cap) : port_cap_(port_cap) {} -glcr::ErrorCode PortServer::CreateClient(z_cap_t *new_port) { +glcr::ErrorOr PortServer::CreateClient() { // FIXME: Restrict permissions. - return ZCapDuplicate(port_cap_, new_port); + z_cap_t new_port; + RET_ERR(ZCapDuplicate(port_cap_, &new_port)); + return PortClient::AdoptPort(new_port); } glcr::ErrorCode PortServer::RecvCap(uint64_t *num_bytes, char *msg, diff --git a/lib/mammoth/src/process.cpp b/lib/mammoth/src/process.cpp index 0578442..a238279 100644 --- a/lib/mammoth/src/process.cpp +++ b/lib/mammoth/src/process.cpp @@ -6,7 +6,8 @@ #include "mammoth/debug.h" #include "mammoth/endpoint_server.h" #include "mammoth/init.h" -#include "mammoth/port.h" +#include "mammoth/port_client.h" +#include "mammoth/port_server.h" #define MAM_PROC_DEBUG 0 @@ -105,14 +106,13 @@ glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program, #if MAM_PROC_DEBUG dbgln("Port Create"); #endif - RET_ERR(ZPortCreate(&port_cap)); - uint64_t port_cap_donate; - RET_ERR(ZCapDuplicate(port_cap, &port_cap_donate)); + ASSIGN_OR_RETURN(PortServer server, PortServer::Create()); + ASSIGN_OR_RETURN(PortClient pclient, server.CreateClient()); #if MAM_PROC_DEBUG dbgln("Spawn"); #endif - RET_ERR(ZProcessSpawn(gSelfProcCap, port_cap_donate, &proc_cap, &as_cap, + RET_ERR(ZProcessSpawn(gSelfProcCap, server.cap(), &proc_cap, &as_cap, &foreign_port_id)); uint64_t entry_point = LoadElfProgram(program, as_cap); @@ -123,10 +123,9 @@ glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program, uint64_t thread_cap; RET_ERR(ZThreadCreate(proc_cap, &thread_cap)); - Port p(port_cap); - RET_ERR(p.WriteMessage(Z_INIT_SELF_PROC, proc_cap)); - RET_ERR(p.WriteMessage(Z_INIT_SELF_VMAS, as_cap)); - RET_ERR(p.WriteMessage(Z_INIT_ENDPOINT, client.GetCap())); + RET_ERR(pclient.WriteMessage(Z_INIT_SELF_PROC, proc_cap)); + RET_ERR(pclient.WriteMessage(Z_INIT_SELF_VMAS, as_cap)); + RET_ERR(pclient.WriteMessage(Z_INIT_ENDPOINT, client.GetCap())); #if MAM_PROC_DEBUG dbgln("Thread start"); diff --git a/sys/denali/denali.cpp b/sys/denali/denali.cpp index 5dce931..320159b 100644 --- a/sys/denali/denali.cpp +++ b/sys/denali/denali.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include @@ -26,7 +26,7 @@ uint64_t main(uint64_t init_port_cap) { check(resp_cap_or.error()); } auto resp_cap = resp_cap_or.value(); - Port notify(resp_cap.second()); + PortClient notify = PortClient::AdoptPort(resp_cap.second()); ASSIGN_OR_RETURN(EndpointServer endpoint, EndpointServer::Create()); ASSIGN_OR_RETURN(EndpointClient client, endpoint.CreateClient()); diff --git a/sys/yellowstone/yellowstone_server.cpp b/sys/yellowstone/yellowstone_server.cpp index ab4580e..1e8e0cb 100644 --- a/sys/yellowstone/yellowstone_server.cpp +++ b/sys/yellowstone/yellowstone_server.cpp @@ -61,9 +61,12 @@ void YellowstoneServer::ServerThread() { break; case kYellowstoneGetRegistration: { dbgln("Yellowstone::GetRegistration"); - uint64_t reg_cap; - check(register_port_.CreateClient(®_cap)); + auto client_or = register_port_.CreateClient(); + if (!client_or.ok()) { + check(client_or.error()); + } YellowstoneGetRegistrationResp resp; + uint64_t reg_cap = client_or.value().cap(); check(ZReplyPortSend(reply_port_cap, sizeof(resp), &resp, 1, ®_cap)); break; }