[Mammoth] Move ipc calls to separate folder mammoth.

This commit is contained in:
Drew Galbraith 2023-11-22 13:41:14 -08:00
parent 19e394ae7b
commit ad5b55bf37
19 changed files with 21 additions and 27 deletions

View file

@ -1,57 +0,0 @@
#include "mammoth/channel.h"
#include <zcall.h>
#include "mammoth/debug.h"
namespace {
uint64_t strlen(const char* ptr) {
uint64_t len = 0;
while (*ptr != '\0') {
len++;
ptr++;
}
return len;
}
} // namespace
void Channel::adopt_cap(uint64_t id) {
if (chan_cap_ != 0) {
crash("Adopting over channel.", glcr::ALREADY_EXISTS);
}
chan_cap_ = id;
}
z_cap_t Channel::release_cap() {
z_cap_t cap = chan_cap_;
chan_cap_ = 0;
return cap;
}
z_cap_t Channel::cap() { return chan_cap_; }
z_err_t Channel::WriteStr(const char* msg) {
if (!chan_cap_) {
return glcr::NULL_PTR;
}
return ZChannelSend(chan_cap_, strlen(msg), msg, 0, nullptr);
}
z_err_t Channel::ReadStr(char* buffer, uint64_t* size) {
if (!chan_cap_) {
return glcr::NULL_PTR;
}
uint64_t num_caps = 0;
return ZChannelRecv(chan_cap_, size, reinterpret_cast<uint8_t*>(buffer),
&num_caps, nullptr);
}
z_err_t CreateChannels(Channel& c1, Channel& c2) {
z_cap_t chan1, chan2;
RET_ERR(ZChannelCreate(&chan1, &chan2));
c1.adopt_cap(chan1);
c2.adopt_cap(chan2);
return glcr::OK;
}

View file

@ -1,5 +0,0 @@
#include "mammoth/endpoint_server.h"
glcr::UniquePtr<EndpointClient> EndpointClient::AdoptEndpoint(z_cap_t cap) {
return glcr::UniquePtr<EndpointClient>(new EndpointClient(cap));
}

View file

@ -1,42 +0,0 @@
#include "mammoth/endpoint_server.h"
#include "mammoth/debug.h"
// Declared as friend in EndpointServer.
void EndpointServerThreadBootstrap(void* endpoint_server) {
reinterpret_cast<EndpointServer*>(endpoint_server)->ServerThread();
}
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
uint64_t client_cap;
RET_ERR(ZCapDuplicate(endpoint_cap_, ~(kZionPerm_Read), &client_cap));
return EndpointClient::AdoptEndpoint(client_cap);
}
Thread EndpointServer::RunServer() {
return Thread(EndpointServerThreadBootstrap, this);
}
void EndpointServer::ServerThread() {
while (true) {
uint64_t message_size = kBufferSize;
uint64_t reply_port_cap = 0;
uint64_t num_caps = 0;
glcr::ErrorCode err = static_cast<glcr::ErrorCode>(
ZEndpointRecv(endpoint_cap_, &message_size, recieve_buffer_, &num_caps,
nullptr, &reply_port_cap));
if (err != glcr::OK) {
dbgln("Error in receive: {x}", err);
continue;
}
RequestContext request(recieve_buffer_, message_size);
ResponseContext response(reply_port_cap);
// FIXME: Consider pumping these errors into the response as well.
check(HandleRequest(request, response));
if (!response.HasWritten()) {
dbgln("Returning without having written a response. Req type {x}",
request.request_id());
}
}
}

View file

@ -4,7 +4,7 @@
#include <ztypes.h>
#include "mammoth/debug.h"
#include "mammoth/port_server.h"
#include "mammoth/ipc/port_server.h"
uint64_t gSelfProcCap = 0;
uint64_t gSelfVmasCap = 0;

View file

@ -1,14 +0,0 @@
#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 static_cast<glcr::ErrorCode>(
ZPortSend(port_cap_, str.length() + 1, str.cstr(), 1, &cap));
}

View file

@ -1,46 +0,0 @@
#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::ErrorOr<PortClient> PortServer::CreateClient() {
z_cap_t new_port;
RET_ERR(ZCapDuplicate(port_cap_, ~(kZionPerm_Read), &new_port));
return PortClient::AdoptPort(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;
}
glcr::ErrorCode 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;
}

View file

@ -4,10 +4,10 @@
#include <zcall.h>
#include "mammoth/debug.h"
#include "mammoth/endpoint_server.h"
#include "mammoth/init.h"
#include "mammoth/port_client.h"
#include "mammoth/port_server.h"
#include "mammoth/ipc/endpoint_server.h"
#include "mammoth/ipc/port_client.h"
#include "mammoth/ipc/port_server.h"
#define MAM_PROC_DEBUG 0