[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

@ -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) {}
glcr::ErrorCode PortServer::CreateClient(z_cap_t *new_port) {
glcr::ErrorOr<PortClient> 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,

View file

@ -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<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client.GetCap()));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client.GetCap()));
#if MAM_PROC_DEBUG
dbgln("Thread start");