[mammoth] Finish separating PortServer and PortClient.

This commit is contained in:
Drew Galbraith 2023-06-26 08:59:28 -07:00
parent 5fb9fa6ae6
commit b7a962cc26
10 changed files with 66 additions and 60 deletions

View File

@ -7,7 +7,7 @@ add_library(mammoth_lib STATIC
src/memory_region.cpp src/memory_region.cpp
src/new.cpp src/new.cpp
src/process.cpp src/process.cpp
src/port.cpp src/port_client.cpp
src/port_server.cpp src/port_server.cpp
src/thread.cpp src/thread.cpp
) )

View File

@ -1,26 +0,0 @@
#pragma once
#include <glacier/status/error_or.h>
#include <glacier/string/string.h>
#include <stdint.h>
#include <zcall.h>
// FIXME: Split send and receive.
class Port {
public:
static glcr::ErrorOr<Port> Create();
Port(uint64_t port_cap);
template <typename T>
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 <typename T>
z_err_t Port::WriteMessage(const T& obj, uint64_t cap) {
return ZPortSend(port_cap_, sizeof(obj), &obj, 1, &cap);
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <glacier/status/error_or.h>
#include <glacier/string/string.h>
#include <stdint.h>
#include <zcall.h>
class PortClient {
public:
static PortClient AdoptPort(z_cap_t port_cap);
template <typename T>
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 <typename T>
z_err_t PortClient::WriteMessage(const T& obj, z_cap_t cap) {
return ZPortSend(port_cap_, sizeof(obj), &obj, 1, &cap);
}

View File

@ -3,16 +3,20 @@
#include <glacier/status/error_or.h> #include <glacier/status/error_or.h>
#include <ztypes.h> #include <ztypes.h>
#include "mammoth/port_client.h"
class PortServer { class PortServer {
public: public:
static glcr::ErrorOr<PortServer> Create(); static glcr::ErrorOr<PortServer> Create();
static PortServer AdoptCap(z_cap_t cap); static PortServer AdoptCap(z_cap_t cap);
glcr::ErrorCode CreateClient(z_cap_t* new_port); glcr::ErrorOr<PortClient> CreateClient();
glcr::ErrorCode RecvCap(uint64_t* num_bytes, char* msg, uint64_t* cap); glcr::ErrorCode RecvCap(uint64_t* num_bytes, char* msg, uint64_t* cap);
glcr::ErrorCode PollForIntCap(uint64_t* msg, uint64_t* cap); glcr::ErrorCode PollForIntCap(uint64_t* msg, uint64_t* cap);
z_cap_t cap() { return port_cap_; }
private: private:
z_cap_t port_cap_; z_cap_t port_cap_;

View File

@ -1,17 +0,0 @@
#include "mammoth/port.h"
#include <glacier/status/error.h>
#include <zcall.h>
#include "mammoth/debug.h"
glcr::ErrorOr<Port> 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);
}

View File

@ -0,0 +1,13 @@
#include "mammoth/port_client.h"
#include <glacier/status/error.h>
#include <zcall.h>
#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);
}

View File

@ -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) {} PortServer::PortServer(z_cap_t port_cap) : port_cap_(port_cap) {}
glcr::ErrorCode PortServer::CreateClient(z_cap_t *new_port) { glcr::ErrorOr<PortClient> PortServer::CreateClient() {
// FIXME: Restrict permissions. // 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, glcr::ErrorCode PortServer::RecvCap(uint64_t *num_bytes, char *msg,

View File

@ -6,7 +6,8 @@
#include "mammoth/debug.h" #include "mammoth/debug.h"
#include "mammoth/endpoint_server.h" #include "mammoth/endpoint_server.h"
#include "mammoth/init.h" #include "mammoth/init.h"
#include "mammoth/port.h" #include "mammoth/port_client.h"
#include "mammoth/port_server.h"
#define MAM_PROC_DEBUG 0 #define MAM_PROC_DEBUG 0
@ -105,14 +106,13 @@ glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
#if MAM_PROC_DEBUG #if MAM_PROC_DEBUG
dbgln("Port Create"); dbgln("Port Create");
#endif #endif
RET_ERR(ZPortCreate(&port_cap)); ASSIGN_OR_RETURN(PortServer server, PortServer::Create());
uint64_t port_cap_donate; ASSIGN_OR_RETURN(PortClient pclient, server.CreateClient());
RET_ERR(ZCapDuplicate(port_cap, &port_cap_donate));
#if MAM_PROC_DEBUG #if MAM_PROC_DEBUG
dbgln("Spawn"); dbgln("Spawn");
#endif #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)); &foreign_port_id));
uint64_t entry_point = LoadElfProgram(program, as_cap); uint64_t entry_point = LoadElfProgram(program, as_cap);
@ -123,10 +123,9 @@ glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
uint64_t thread_cap; uint64_t thread_cap;
RET_ERR(ZThreadCreate(proc_cap, &thread_cap)); RET_ERR(ZThreadCreate(proc_cap, &thread_cap));
Port p(port_cap); RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_cap)); RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap)); RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client.GetCap()));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client.GetCap()));
#if MAM_PROC_DEBUG #if MAM_PROC_DEBUG
dbgln("Thread start"); dbgln("Thread start");

View File

@ -2,7 +2,7 @@
#include <mammoth/debug.h> #include <mammoth/debug.h>
#include <mammoth/endpoint_server.h> #include <mammoth/endpoint_server.h>
#include <mammoth/init.h> #include <mammoth/init.h>
#include <mammoth/port.h> #include <mammoth/port_client.h>
#include <stdint.h> #include <stdint.h>
#include <yellowstone.h> #include <yellowstone.h>
@ -26,7 +26,7 @@ uint64_t main(uint64_t init_port_cap) {
check(resp_cap_or.error()); check(resp_cap_or.error());
} }
auto resp_cap = resp_cap_or.value(); 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(EndpointServer endpoint, EndpointServer::Create());
ASSIGN_OR_RETURN(EndpointClient client, endpoint.CreateClient()); ASSIGN_OR_RETURN(EndpointClient client, endpoint.CreateClient());

View File

@ -61,9 +61,12 @@ void YellowstoneServer::ServerThread() {
break; break;
case kYellowstoneGetRegistration: { case kYellowstoneGetRegistration: {
dbgln("Yellowstone::GetRegistration"); dbgln("Yellowstone::GetRegistration");
uint64_t reg_cap; auto client_or = register_port_.CreateClient();
check(register_port_.CreateClient(&reg_cap)); if (!client_or.ok()) {
check(client_or.error());
}
YellowstoneGetRegistrationResp resp; YellowstoneGetRegistrationResp resp;
uint64_t reg_cap = client_or.value().cap();
check(ZReplyPortSend(reply_port_cap, sizeof(resp), &resp, 1, &reg_cap)); check(ZReplyPortSend(reply_port_cap, sizeof(resp), &resp, 1, &reg_cap));
break; break;
} }