diff --git a/lib/mammoth/CMakeLists.txt b/lib/mammoth/CMakeLists.txt index fcb4966..b25e601 100644 --- a/lib/mammoth/CMakeLists.txt +++ b/lib/mammoth/CMakeLists.txt @@ -8,6 +8,7 @@ add_library(mammoth_lib STATIC src/new.cpp src/process.cpp src/port.cpp + src/port_server.cpp src/thread.cpp ) diff --git a/lib/mammoth/include/mammoth/port.h b/lib/mammoth/include/mammoth/port.h index 97c72b7..4e64d60 100644 --- a/lib/mammoth/include/mammoth/port.h +++ b/lib/mammoth/include/mammoth/port.h @@ -11,17 +11,11 @@ class Port { static glcr::ErrorOr Create(); Port(uint64_t port_cap); - glcr::ErrorCode RecvCap(uint64_t* num_bytes, char* msg, uint64_t* cap); - z_err_t PollForIntCap(uint64_t* msg, uint64_t* cap); - template z_err_t WriteMessage(const T& obj, uint64_t cap); glcr::ErrorCode WriteString(glcr::String str, uint64_t cap); - // FIXME: We can't create error_ors of ints - glcr::ErrorCode Duplicate(uint64_t* new_cap); - private: uint64_t port_cap_; }; diff --git a/lib/mammoth/include/mammoth/port_server.h b/lib/mammoth/include/mammoth/port_server.h new file mode 100644 index 0000000..1cc783a --- /dev/null +++ b/lib/mammoth/include/mammoth/port_server.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +class PortServer { + public: + static glcr::ErrorOr Create(); + static PortServer AdoptCap(z_cap_t cap); + + glcr::ErrorCode CreateClient(z_cap_t* new_port); + + glcr::ErrorCode RecvCap(uint64_t* num_bytes, char* msg, uint64_t* cap); + glcr::ErrorCode PollForIntCap(uint64_t* msg, uint64_t* cap); + + private: + z_cap_t port_cap_; + + PortServer(z_cap_t cap); +}; diff --git a/lib/mammoth/src/init.cpp b/lib/mammoth/src/init.cpp index c378cfe..474e5ab 100644 --- a/lib/mammoth/src/init.cpp +++ b/lib/mammoth/src/init.cpp @@ -4,7 +4,7 @@ #include #include "mammoth/debug.h" -#include "mammoth/port.h" +#include "mammoth/port_server.h" uint64_t gSelfProcCap = 0; uint64_t gSelfVmasCap = 0; @@ -15,7 +15,7 @@ uint64_t gBootDenaliVmmoCap = 0; uint64_t gBootVictoriaFallsVmmoCap = 0; z_err_t ParseInitPort(uint64_t init_port_cap) { - Port port(init_port_cap); + PortServer port = PortServer::AdoptCap(init_port_cap); z_err_t ret; uint64_t init_sig, init_cap; while ((ret = port.PollForIntCap(&init_sig, &init_cap)) != glcr::EMPTY) { diff --git a/lib/mammoth/src/port.cpp b/lib/mammoth/src/port.cpp index 110def5..b810cde 100644 --- a/lib/mammoth/src/port.cpp +++ b/lib/mammoth/src/port.cpp @@ -12,34 +12,6 @@ glcr::ErrorOr Port::Create() { } Port::Port(uint64_t port_cap) : port_cap_(port_cap) {} -glcr::ErrorCode Port::RecvCap(uint64_t *num_bytes, char *msg, uint64_t *cap) { - uint64_t caps = 1; - RET_ERR(ZPortRecv(port_cap_, num_bytes, reinterpret_cast(msg), - &caps, cap)); - - if (caps != 1) { - return glcr::FAILED_PRECONDITION; - } - return glcr::OK; -} -z_err_t Port::PollForIntCap(uint64_t *msg, uint64_t *cap) { - uint64_t bytes = sizeof(uint64_t); - uint64_t caps = 1; - RET_ERR(ZPortPoll(port_cap_, &bytes, reinterpret_cast(msg), &caps, - cap)); - - if (bytes != sizeof(uint64_t)) { - return glcr::FAILED_PRECONDITION; - } - if (caps != 1) { - return glcr::FAILED_PRECONDITION; - } - return glcr::OK; -} glcr::ErrorCode Port::WriteString(glcr::String str, uint64_t cap) { return ZPortSend(port_cap_, str.length() + 1, str.cstr(), 1, &cap); } - -glcr::ErrorCode Port::Duplicate(uint64_t *new_cap) { - return ZCapDuplicate(port_cap_, new_cap); -} diff --git a/lib/mammoth/src/port_server.cpp b/lib/mammoth/src/port_server.cpp new file mode 100644 index 0000000..9de44c8 --- /dev/null +++ b/lib/mammoth/src/port_server.cpp @@ -0,0 +1,45 @@ +#include "mammoth/port_server.h" + +#include + +glcr::ErrorOr PortServer::Create() { + z_cap_t port; + RET_ERR(ZPortCreate(&port)); + return PortServer(port); +} + +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) { + // FIXME: Restrict permissions. + return ZCapDuplicate(port_cap_, new_port); +} + +glcr::ErrorCode PortServer::RecvCap(uint64_t *num_bytes, char *msg, + uint64_t *cap) { + uint64_t caps = 1; + RET_ERR(ZPortRecv(port_cap_, num_bytes, reinterpret_cast(msg), + &caps, cap)); + + if (caps != 1) { + return glcr::FAILED_PRECONDITION; + } + return glcr::OK; +} + +z_err_t PortServer::PollForIntCap(uint64_t *msg, uint64_t *cap) { + uint64_t bytes = sizeof(uint64_t); + uint64_t caps = 1; + RET_ERR(ZPortPoll(port_cap_, &bytes, reinterpret_cast(msg), &caps, + cap)); + + if (bytes != sizeof(uint64_t)) { + return glcr::FAILED_PRECONDITION; + } + if (caps != 1) { + return glcr::FAILED_PRECONDITION; + } + return glcr::OK; +} diff --git a/sys/yellowstone/yellowstone_server.cpp b/sys/yellowstone/yellowstone_server.cpp index 652b975..ab4580e 100644 --- a/sys/yellowstone/yellowstone_server.cpp +++ b/sys/yellowstone/yellowstone_server.cpp @@ -32,11 +32,11 @@ glcr::ErrorCode HandleDenaliRegistration(z_cap_t endpoint_cap) { glcr::ErrorOr YellowstoneServer::Create() { ASSIGN_OR_RETURN(EndpointServer server, EndpointServer::Create()); - ASSIGN_OR_RETURN(Port port, Port::Create()); + ASSIGN_OR_RETURN(PortServer port, PortServer::Create()); return YellowstoneServer(server, port); } -YellowstoneServer::YellowstoneServer(EndpointServer server, Port port) +YellowstoneServer::YellowstoneServer(EndpointServer server, PortServer port) : server_(server), register_port_(port) {} Thread YellowstoneServer::RunServer() { @@ -62,7 +62,7 @@ void YellowstoneServer::ServerThread() { case kYellowstoneGetRegistration: { dbgln("Yellowstone::GetRegistration"); uint64_t reg_cap; - check(register_port_.Duplicate(®_cap)); + check(register_port_.CreateClient(®_cap)); YellowstoneGetRegistrationResp resp; check(ZReplyPortSend(reply_port_cap, sizeof(resp), &resp, 1, ®_cap)); break; diff --git a/sys/yellowstone/yellowstone_server.h b/sys/yellowstone/yellowstone_server.h index 30b355e..9b24d0a 100644 --- a/sys/yellowstone/yellowstone_server.h +++ b/sys/yellowstone/yellowstone_server.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include class YellowstoneServer { @@ -19,7 +19,7 @@ class YellowstoneServer { private: EndpointServer server_; - Port register_port_; + PortServer register_port_; static const uint64_t kBufferSize = 128; uint8_t server_buffer_[kBufferSize]; @@ -29,5 +29,5 @@ class YellowstoneServer { z_cap_t denali_cap_ = 0; z_cap_t victoria_falls_cap_ = 0; - YellowstoneServer(EndpointServer server, Port port); + YellowstoneServer(EndpointServer server, PortServer port); };