[mammoth] Update EndpointServer to have move-only semantics.

This commit is contained in:
Drew Galbraith 2023-06-26 11:38:17 -07:00
parent 16c30d12fb
commit 2e89aee5a3
8 changed files with 40 additions and 29 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <glacier/memory/unique_ptr.h>
#include <glacier/status/error_or.h> #include <glacier/status/error_or.h>
#include <ztypes.h> #include <ztypes.h>
@ -7,8 +8,12 @@
class EndpointServer { class EndpointServer {
public: public:
static glcr::ErrorOr<EndpointServer> Create(); EndpointServer() = delete;
static EndpointServer Adopt(z_cap_t endpoint_cap); EndpointServer(const EndpointServer&) = delete;
EndpointServer& operator=(const EndpointServer&) = delete;
static glcr::ErrorOr<glcr::UniquePtr<EndpointServer>> Create();
static glcr::UniquePtr<EndpointServer> Adopt(z_cap_t endpoint_cap);
glcr::ErrorOr<EndpointClient> CreateClient(); glcr::ErrorOr<EndpointClient> CreateClient();

View File

@ -1,13 +1,13 @@
#include "mammoth/endpoint_server.h" #include "mammoth/endpoint_server.h"
glcr::ErrorOr<EndpointServer> EndpointServer::Create() { glcr::ErrorOr<glcr::UniquePtr<EndpointServer>> EndpointServer::Create() {
uint64_t cap; uint64_t cap;
RET_ERR(ZEndpointCreate(&cap)); RET_ERR(ZEndpointCreate(&cap));
return EndpointServer(cap); return glcr::UniquePtr<EndpointServer>(new EndpointServer(cap));
} }
EndpointServer EndpointServer::Adopt(z_cap_t endpoint_cap) { glcr::UniquePtr<EndpointServer> EndpointServer::Adopt(z_cap_t endpoint_cap) {
return EndpointServer(endpoint_cap); return glcr::UniquePtr<EndpointServer>(new EndpointServer(endpoint_cap));
} }
glcr::ErrorOr<EndpointClient> EndpointServer::CreateClient() { glcr::ErrorOr<EndpointClient> EndpointServer::CreateClient() {

View File

@ -28,11 +28,12 @@ uint64_t main(uint64_t init_port_cap) {
auto resp_cap = resp_cap_or.value(); auto resp_cap = resp_cap_or.value();
PortClient notify = PortClient::AdoptPort(resp_cap.second()); PortClient notify = PortClient::AdoptPort(resp_cap.second());
ASSIGN_OR_RETURN(EndpointServer endpoint, EndpointServer::Create()); ASSIGN_OR_RETURN(glcr::UniquePtr<EndpointServer> endpoint,
ASSIGN_OR_RETURN(EndpointClient client, endpoint.CreateClient()); EndpointServer::Create());
ASSIGN_OR_RETURN(EndpointClient client, endpoint->CreateClient());
notify.WriteMessage("denali", client.GetCap()); notify.WriteMessage("denali", client.GetCap());
DenaliServer server(endpoint, driver); DenaliServer server(glcr::Move(endpoint), driver);
RET_ERR(server.RunServer()); RET_ERR(server.RunServer());
// FIXME: Add thread join. // FIXME: Add thread join.
return 0; return 0;

View File

@ -1,5 +1,6 @@
#include "denali_server.h" #include "denali_server.h"
#include <glacier/memory/move.h>
#include <glacier/status/error.h> #include <glacier/status/error.h>
#include <mammoth/debug.h> #include <mammoth/debug.h>
#include <zcall.h> #include <zcall.h>
@ -12,8 +13,9 @@ void HandleResponse(z_cap_t reply_port, uint64_t lba, uint64_t size,
} }
} // namespace } // namespace
DenaliServer::DenaliServer(EndpointServer server, AhciDriver& driver) DenaliServer::DenaliServer(glcr::UniquePtr<EndpointServer> server,
: server_(server), driver_(driver) { AhciDriver& driver)
: server_(glcr::Move(server)), driver_(driver) {
gServer = this; gServer = this;
} }
@ -21,7 +23,7 @@ glcr::ErrorCode DenaliServer::RunServer() {
while (true) { while (true) {
uint64_t buff_size = kBuffSize; uint64_t buff_size = kBuffSize;
z_cap_t reply_port; z_cap_t reply_port;
RET_ERR(server_.Recieve(&buff_size, read_buffer_, &reply_port)); RET_ERR(server_->Recieve(&buff_size, read_buffer_, &reply_port));
if (buff_size < sizeof(uint64_t)) { if (buff_size < sizeof(uint64_t)) {
dbgln("Skipping invalid message"); dbgln("Skipping invalid message");
continue; continue;

View File

@ -8,7 +8,7 @@
class DenaliServer { class DenaliServer {
public: public:
DenaliServer(EndpointServer server, AhciDriver& driver); DenaliServer(glcr::UniquePtr<EndpointServer> server, AhciDriver& driver);
glcr::ErrorCode RunServer(); glcr::ErrorCode RunServer();
@ -17,7 +17,7 @@ class DenaliServer {
private: private:
static const uint64_t kBuffSize = 1024; static const uint64_t kBuffSize = 1024;
EndpointServer server_; glcr::UniquePtr<EndpointServer> server_;
uint8_t read_buffer_[kBuffSize]; uint8_t read_buffer_[kBuffSize];
AhciDriver& driver_; AhciDriver& driver_;

View File

@ -12,18 +12,18 @@ uint64_t main(uint64_t port_cap) {
dbgln("Yellowstone Initializing."); dbgln("Yellowstone Initializing.");
check(ParseInitPort(port_cap)); check(ParseInitPort(port_cap));
ASSIGN_OR_RETURN(YellowstoneServer server, YellowstoneServer::Create()); ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create());
Thread server_thread = server.RunServer(); Thread server_thread = server->RunServer();
Thread registration_thread = server.RunRegistration(); Thread registration_thread = server->RunRegistration();
DumpPciEDevices(); DumpPciEDevices();
uint64_t vaddr; uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr)); check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr));
ASSIGN_OR_RETURN(EndpointClient client, server.GetServerClient()); ASSIGN_OR_RETURN(EndpointClient client, server->GetServerClient());
check(SpawnProcessFromElfRegion(vaddr, client)); check(SpawnProcessFromElfRegion(vaddr, client));
check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootVictoriaFallsVmmoCap, &vaddr)); check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootVictoriaFallsVmmoCap, &vaddr));
ASSIGN_OR_RETURN(client, server.GetServerClient()); ASSIGN_OR_RETURN(client, server->GetServerClient());
check(SpawnProcessFromElfRegion(vaddr, client)); check(SpawnProcessFromElfRegion(vaddr, client));
check(server_thread.Join()); check(server_thread.Join());

View File

@ -30,14 +30,16 @@ glcr::ErrorCode HandleDenaliRegistration(z_cap_t endpoint_cap) {
} // namespace } // namespace
glcr::ErrorOr<YellowstoneServer> YellowstoneServer::Create() { glcr::ErrorOr<glcr::UniquePtr<YellowstoneServer>> YellowstoneServer::Create() {
ASSIGN_OR_RETURN(EndpointServer server, EndpointServer::Create()); ASSIGN_OR_RETURN(auto server, EndpointServer::Create());
ASSIGN_OR_RETURN(PortServer port, PortServer::Create()); ASSIGN_OR_RETURN(PortServer port, PortServer::Create());
return YellowstoneServer(server, port); return glcr::UniquePtr<YellowstoneServer>(
new YellowstoneServer(glcr::Move(server), port));
} }
YellowstoneServer::YellowstoneServer(EndpointServer server, PortServer port) YellowstoneServer::YellowstoneServer(glcr::UniquePtr<EndpointServer> server,
: server_(server), register_port_(port) {} PortServer port)
: server_(glcr::Move(server)), register_port_(port) {}
Thread YellowstoneServer::RunServer() { Thread YellowstoneServer::RunServer() {
return Thread(ServerThreadBootstrap, this); return Thread(ServerThreadBootstrap, this);
@ -52,7 +54,7 @@ void YellowstoneServer::ServerThread() {
uint64_t num_bytes = kBufferSize; uint64_t num_bytes = kBufferSize;
uint64_t reply_port_cap; uint64_t reply_port_cap;
// FIXME: Error handling. // FIXME: Error handling.
check(server_.Recieve(&num_bytes, server_buffer_, &reply_port_cap)); check(server_->Recieve(&num_bytes, server_buffer_, &reply_port_cap));
YellowstoneGetReq* req = YellowstoneGetReq* req =
reinterpret_cast<YellowstoneGetReq*>(server_buffer_); reinterpret_cast<YellowstoneGetReq*>(server_buffer_);
switch (req->type) { switch (req->type) {
@ -102,5 +104,5 @@ void YellowstoneServer::RegistrationThread() {
} }
glcr::ErrorOr<EndpointClient> YellowstoneServer::GetServerClient() { glcr::ErrorOr<EndpointClient> YellowstoneServer::GetServerClient() {
return server_.CreateClient(); return server_->CreateClient();
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <glacier/memory/unique_ptr.h>
#include <glacier/status/error_or.h> #include <glacier/status/error_or.h>
#include <mammoth/endpoint_server.h> #include <mammoth/endpoint_server.h>
#include <mammoth/port_server.h> #include <mammoth/port_server.h>
@ -7,7 +8,7 @@
class YellowstoneServer { class YellowstoneServer {
public: public:
static glcr::ErrorOr<YellowstoneServer> Create(); static glcr::ErrorOr<glcr::UniquePtr<YellowstoneServer>> Create();
Thread RunServer(); Thread RunServer();
Thread RunRegistration(); Thread RunRegistration();
@ -18,7 +19,7 @@ class YellowstoneServer {
glcr::ErrorOr<EndpointClient> GetServerClient(); glcr::ErrorOr<EndpointClient> GetServerClient();
private: private:
EndpointServer server_; glcr::UniquePtr<EndpointServer> server_;
PortServer register_port_; PortServer register_port_;
static const uint64_t kBufferSize = 128; static const uint64_t kBufferSize = 128;
@ -29,5 +30,5 @@ class YellowstoneServer {
z_cap_t denali_cap_ = 0; z_cap_t denali_cap_ = 0;
z_cap_t victoria_falls_cap_ = 0; z_cap_t victoria_falls_cap_ = 0;
YellowstoneServer(EndpointServer server, PortServer port); YellowstoneServer(glcr::UniquePtr<EndpointServer> server, PortServer port);
}; };