[all] Add stub for new Endpoint kernel object

This commit is contained in:
Drew Galbraith 2023-06-21 21:26:24 -07:00
parent 1f7a15eed4
commit 69501bfe01
15 changed files with 144 additions and 38 deletions

View file

@ -1,6 +1,8 @@
add_library(mammoth_lib STATIC
src/channel.cpp
src/debug.cpp
src/endpoint_client.cpp
src/endpoint_server.cpp
src/init.cpp
src/memory_region.cpp
src/process.cpp

View file

@ -0,0 +1,37 @@
#pragma once
#include <glacier/container/pair.h>
#include <glacier/status/error_or.h>
#include <zcall.h>
#include <ztypes.h>
class EndpointClient {
public:
static EndpointClient AdoptEndpoint(z_cap_t cap);
template <typename Req, typename Resp>
glcr::ErrorOr<glcr::Pair<Resp, z_cap_t>> CallEndpoint(const Req& req);
private:
EndpointClient(uint64_t cap) : cap_(cap) {}
z_cap_t cap_;
};
template <typename Req, typename Resp>
glcr::ErrorOr<glcr::Pair<Resp, z_cap_t>> EndpointClient::CallEndpoint(
const Req& req) {
uint64_t reply_port_cap;
RET_ERR(ZEndpointSend(cap_, sizeof(Req), &req, &reply_port_cap));
Resp resp;
z_cap_t cap = 0;
uint64_t num_caps = 1;
uint64_t num_bytes = sizeof(Resp);
RET_ERR(ZReplyPortRecv(reply_port_cap, &num_bytes, &resp, &num_caps, &cap));
if (num_bytes != sizeof(resp) || num_caps != 1) {
return glcr::FAILED_PRECONDITION;
}
return glcr::Pair{resp, cap};
}

View file

@ -0,0 +1,22 @@
#pragma once
#include <glacier/status/error_or.h>
#include <ztypes.h>
#include "mammoth/endpoint_client.h"
class EndpointServer {
public:
static glcr::ErrorOr<EndpointServer> Create();
static EndpointServer Adopt(z_cap_t endpoint_cap);
glcr::ErrorOr<EndpointClient> CreateClient();
// FIXME: Release Cap here.
z_cap_t GetCap() { return endpoint_cap_; }
private:
z_cap_t endpoint_cap_;
EndpointServer(z_cap_t cap) : endpoint_cap_(cap) {}
};

View file

@ -3,6 +3,6 @@
#include <glacier/status/error_or.h>
#include <stdint.h>
#include "mammoth/channel.h"
#include "mammoth/endpoint_client.h"
glcr::ErrorOr<Channel> SpawnProcessFromElfRegion(uint64_t program);
glcr::ErrorOr<EndpointClient> SpawnProcessFromElfRegion(uint64_t program);

View file

@ -0,0 +1,3 @@
#include "mammoth/endpoint_server.h"
EndpointClient EndpointClient::AdoptEndpoint(z_cap_t cap) { return {cap}; }

View file

@ -0,0 +1,14 @@
#include "mammoth/endpoint_server.h"
glcr::ErrorOr<EndpointServer> EndpointServer::Create() {
uint64_t cap;
RET_ERR(ZEndpointCreate(&cap));
return EndpointServer(cap);
}
glcr::ErrorOr<EndpointClient> EndpointServer::CreateClient() {
uint64_t client_cap;
// FIXME: Restrict permissions to send-only here.
RET_ERR(ZCapDuplicate(endpoint_cap_, &client_cap));
return EndpointClient::AdoptEndpoint(client_cap);
}

View file

@ -3,8 +3,8 @@
#include <glacier/status/error.h>
#include <zcall.h>
#include "mammoth/channel.h"
#include "mammoth/debug.h"
#include "mammoth/endpoint_server.h"
#include "mammoth/init.h"
#include "mammoth/port.h"
@ -95,20 +95,21 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
} // namespace
glcr::ErrorOr<Channel> SpawnProcessFromElfRegion(uint64_t program) {
Channel local, foreign;
check(CreateChannels(local, foreign));
glcr::ErrorOr<EndpointClient> SpawnProcessFromElfRegion(uint64_t program) {
ASSIGN_OR_RETURN(EndpointServer server, EndpointServer::Create());
ASSIGN_OR_RETURN(EndpointClient client, server.CreateClient());
uint64_t proc_cap;
uint64_t as_cap;
uint64_t foreign_port_id;
uint64_t port_cap;
#if MAM_PROC_DEBUG
dbgln("Port Create");
#endif
check(ZPortCreate(&port_cap));
RET_ERR(ZPortCreate(&port_cap));
uint64_t port_cap_donate;
check(ZCapDuplicate(port_cap, &port_cap_donate));
RET_ERR(ZCapDuplicate(port_cap, &port_cap_donate));
#if MAM_PROC_DEBUG
dbgln("Spawn");
@ -127,12 +128,12 @@ glcr::ErrorOr<Channel> SpawnProcessFromElfRegion(uint64_t program) {
Port p(port_cap);
check(p.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
check(p.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
check(p.WriteMessage<uint64_t>(Z_INIT_CHANNEL, foreign.release_cap()));
check(p.WriteMessage<uint64_t>(Z_INIT_CHANNEL, server.GetCap()));
#if MAM_PROC_DEBUG
dbgln("Thread start");
#endif
check(ZThreadStart(thread_cap, entry_point, foreign_port_id, 0));
return local;
return client;
}