[mammoth] Move port server operations to their own class.
This commit is contained in:
parent
7989c9d616
commit
5fb9fa6ae6
|
@ -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
|
||||
)
|
||||
|
||||
|
|
|
@ -11,17 +11,11 @@ class Port {
|
|||
static glcr::ErrorOr<Port> 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 <typename T>
|
||||
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_;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <ztypes.h>
|
||||
|
||||
class PortServer {
|
||||
public:
|
||||
static glcr::ErrorOr<PortServer> 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);
|
||||
};
|
|
@ -4,7 +4,7 @@
|
|||
#include <ztypes.h>
|
||||
|
||||
#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) {
|
||||
|
|
|
@ -12,34 +12,6 @@ glcr::ErrorOr<Port> 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<uint8_t *>(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<uint8_t *>(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);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
#include "mammoth/port_server.h"
|
||||
|
||||
#include <zcall.h>
|
||||
|
||||
glcr::ErrorOr<PortServer> 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<uint8_t *>(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<uint8_t *>(msg), &caps,
|
||||
cap));
|
||||
|
||||
if (bytes != sizeof(uint64_t)) {
|
||||
return glcr::FAILED_PRECONDITION;
|
||||
}
|
||||
if (caps != 1) {
|
||||
return glcr::FAILED_PRECONDITION;
|
||||
}
|
||||
return glcr::OK;
|
||||
}
|
|
@ -32,11 +32,11 @@ glcr::ErrorCode HandleDenaliRegistration(z_cap_t endpoint_cap) {
|
|||
|
||||
glcr::ErrorOr<YellowstoneServer> 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;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <mammoth/endpoint_server.h>
|
||||
#include <mammoth/port.h>
|
||||
#include <mammoth/port_server.h>
|
||||
#include <mammoth/thread.h>
|
||||
|
||||
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);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue