diff --git a/lib/glacier/memory/unique_ptr.h b/lib/glacier/memory/unique_ptr.h index 3eb631d..fbc3f62 100644 --- a/lib/glacier/memory/unique_ptr.h +++ b/lib/glacier/memory/unique_ptr.h @@ -17,6 +17,7 @@ class UniquePtr { T* temp = ptr_; ptr_ = other.ptr_; other.ptr_ = temp; + return *this; } ~UniquePtr() { @@ -45,7 +46,7 @@ class UniquePtr { template UniquePtr MakeUnique(Args... args) { - return UniquePtr(new T(args...)); + return UniquePtr(new T(args...)); } } // namespace glcr diff --git a/lib/mammoth/include/mammoth/endpoint_client.h b/lib/mammoth/include/mammoth/endpoint_client.h index 66a14fc..b4bc9ef 100644 --- a/lib/mammoth/include/mammoth/endpoint_client.h +++ b/lib/mammoth/include/mammoth/endpoint_client.h @@ -1,13 +1,18 @@ #pragma once #include +#include #include #include #include class EndpointClient { public: - static EndpointClient AdoptEndpoint(z_cap_t cap); + EndpointClient() = delete; + EndpointClient(const EndpointClient&) = delete; + EndpointClient& operator=(const EndpointClient&) = delete; + + static glcr::UniquePtr AdoptEndpoint(z_cap_t cap); template glcr::ErrorOr> CallEndpoint(const Req& req); diff --git a/lib/mammoth/include/mammoth/endpoint_server.h b/lib/mammoth/include/mammoth/endpoint_server.h index 6190cd6..c4e3e5a 100644 --- a/lib/mammoth/include/mammoth/endpoint_server.h +++ b/lib/mammoth/include/mammoth/endpoint_server.h @@ -15,7 +15,7 @@ class EndpointServer { static glcr::ErrorOr> Create(); static glcr::UniquePtr Adopt(z_cap_t endpoint_cap); - glcr::ErrorOr CreateClient(); + glcr::ErrorOr> CreateClient(); // FIXME: Release Cap here. z_cap_t GetCap() { return endpoint_cap_; } diff --git a/lib/mammoth/include/mammoth/process.h b/lib/mammoth/include/mammoth/process.h index dd6d0e4..4317c76 100644 --- a/lib/mammoth/include/mammoth/process.h +++ b/lib/mammoth/include/mammoth/process.h @@ -5,5 +5,5 @@ #include "mammoth/endpoint_client.h" -glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program, - EndpointClient client); +glcr::ErrorCode SpawnProcessFromElfRegion( + uint64_t program, glcr::UniquePtr client); diff --git a/lib/mammoth/src/endpoint_client.cpp b/lib/mammoth/src/endpoint_client.cpp index 7af836b..270bff5 100644 --- a/lib/mammoth/src/endpoint_client.cpp +++ b/lib/mammoth/src/endpoint_client.cpp @@ -1,3 +1,5 @@ #include "mammoth/endpoint_server.h" -EndpointClient EndpointClient::AdoptEndpoint(z_cap_t cap) { return {cap}; } +glcr::UniquePtr EndpointClient::AdoptEndpoint(z_cap_t cap) { + return glcr::UniquePtr(new EndpointClient(cap)); +} diff --git a/lib/mammoth/src/endpoint_server.cpp b/lib/mammoth/src/endpoint_server.cpp index 5f951c3..cacc5a6 100644 --- a/lib/mammoth/src/endpoint_server.cpp +++ b/lib/mammoth/src/endpoint_server.cpp @@ -10,7 +10,7 @@ glcr::UniquePtr EndpointServer::Adopt(z_cap_t endpoint_cap) { return glcr::UniquePtr(new EndpointServer(endpoint_cap)); } -glcr::ErrorOr EndpointServer::CreateClient() { +glcr::ErrorOr> EndpointServer::CreateClient() { uint64_t client_cap; // FIXME: Restrict permissions to send-only here. RET_ERR(ZCapDuplicate(endpoint_cap_, &client_cap)); diff --git a/lib/mammoth/src/process.cpp b/lib/mammoth/src/process.cpp index a238279..d4dbcad 100644 --- a/lib/mammoth/src/process.cpp +++ b/lib/mammoth/src/process.cpp @@ -96,8 +96,8 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) { } // namespace -glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program, - EndpointClient client) { +glcr::ErrorCode SpawnProcessFromElfRegion( + uint64_t program, glcr::UniquePtr client) { uint64_t proc_cap; uint64_t as_cap; uint64_t foreign_port_id; @@ -125,7 +125,7 @@ glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program, RET_ERR(pclient.WriteMessage(Z_INIT_SELF_PROC, proc_cap)); RET_ERR(pclient.WriteMessage(Z_INIT_SELF_VMAS, as_cap)); - RET_ERR(pclient.WriteMessage(Z_INIT_ENDPOINT, client.GetCap())); + RET_ERR(pclient.WriteMessage(Z_INIT_ENDPOINT, client->GetCap())); #if MAM_PROC_DEBUG dbgln("Thread start"); diff --git a/sys/denali/client/denali_client.cpp b/sys/denali/client/denali_client.cpp index 07af87d..54f0859 100644 --- a/sys/denali/client/denali_client.cpp +++ b/sys/denali/client/denali_client.cpp @@ -11,7 +11,7 @@ glcr::ErrorOr DenaliClient::ReadSectors( .lba = lba, .size = num_sectors, }; - auto pair_or = endpoint_.CallEndpoint(read); + auto pair_or = endpoint_->CallEndpoint(read); if (!pair_or) { return pair_or.error(); } diff --git a/sys/denali/denali.cpp b/sys/denali/denali.cpp index 3b86a16..79ff9c1 100644 --- a/sys/denali/denali.cpp +++ b/sys/denali/denali.cpp @@ -14,13 +14,15 @@ uint64_t main(uint64_t init_port_cap) { AhciDriver driver; RET_ERR(driver.Init()); - EndpointClient yellowstone = EndpointClient::AdoptEndpoint(gInitEndpointCap); + glcr::UniquePtr yellowstone = + EndpointClient::AdoptEndpoint(gInitEndpointCap); YellowstoneGetReq req{ .type = kYellowstoneGetRegistration, }; auto resp_cap_or = yellowstone - .CallEndpoint(req); + ->CallEndpoint( + req); if (!resp_cap_or.ok()) { dbgln("Bad call"); check(resp_cap_or.error()); @@ -30,8 +32,9 @@ uint64_t main(uint64_t init_port_cap) { ASSIGN_OR_RETURN(glcr::UniquePtr endpoint, EndpointServer::Create()); - ASSIGN_OR_RETURN(EndpointClient client, endpoint->CreateClient()); - notify.WriteMessage("denali", client.GetCap()); + ASSIGN_OR_RETURN(glcr::UniquePtr client, + endpoint->CreateClient()); + notify.WriteMessage("denali", client->GetCap()); DenaliServer server(glcr::Move(endpoint), driver); RET_ERR(server.RunServer()); diff --git a/sys/denali/include/denali/denali_client.h b/sys/denali/include/denali/denali_client.h index f660289..9622861 100644 --- a/sys/denali/include/denali/denali_client.h +++ b/sys/denali/include/denali/denali_client.h @@ -6,12 +6,13 @@ class DenaliClient { public: - DenaliClient(const EndpointClient& endpoint) : endpoint_(endpoint) {} + DenaliClient(glcr::UniquePtr endpoint) + : endpoint_(glcr::Move(endpoint)) {} glcr::ErrorOr ReadSectors(uint64_t device_id, uint64_t lba, uint64_t num_sectors); private: - EndpointClient endpoint_; + glcr::UniquePtr endpoint_; }; diff --git a/sys/yellowstone/hw/gpt.cpp b/sys/yellowstone/hw/gpt.cpp index bce2360..dcd6bea 100644 --- a/sys/yellowstone/hw/gpt.cpp +++ b/sys/yellowstone/hw/gpt.cpp @@ -47,11 +47,12 @@ struct PartitionEntry { char partition_name[72]; } __attribute__((packed)); -GptReader::GptReader(const DenaliClient& client) : denali_(client) {} +GptReader::GptReader(glcr::UniquePtr denali) + : denali_(glcr::Move(denali)) {} glcr::ErrorCode GptReader::ParsePartitionTables() { ASSIGN_OR_RETURN(MappedMemoryRegion lba_1_and_2, - denali_.ReadSectors(0, 0, 2)); + denali_->ReadSectors(0, 0, 2)); uint16_t* mbr_sig = reinterpret_cast(lba_1_and_2.vaddr() + 0x1FE); if (*mbr_sig != 0xAA55) { return glcr::FAILED_PRECONDITION; @@ -86,7 +87,7 @@ glcr::ErrorCode GptReader::ParsePartitionTables() { ASSIGN_OR_RETURN( MappedMemoryRegion part_table, - denali_.ReadSectors(0, header->lba_partition_entries, num_blocks)); + denali_->ReadSectors(0, header->lba_partition_entries, num_blocks)); dbgln("Entries"); for (uint64_t i = 0; i < num_partitions; i++) { PartitionEntry* entry = reinterpret_cast( diff --git a/sys/yellowstone/hw/gpt.h b/sys/yellowstone/hw/gpt.h index 8a6da3f..94d98fa 100644 --- a/sys/yellowstone/hw/gpt.h +++ b/sys/yellowstone/hw/gpt.h @@ -6,10 +6,10 @@ class GptReader { public: - GptReader(const DenaliClient&); + GptReader(glcr::UniquePtr denali); glcr::ErrorCode ParsePartitionTables(); private: - DenaliClient denali_; + glcr::UniquePtr denali_; }; diff --git a/sys/yellowstone/yellowstone.cpp b/sys/yellowstone/yellowstone.cpp index 4afae9c..5f839be 100644 --- a/sys/yellowstone/yellowstone.cpp +++ b/sys/yellowstone/yellowstone.cpp @@ -19,12 +19,13 @@ uint64_t main(uint64_t port_cap) { uint64_t vaddr; check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr)); - ASSIGN_OR_RETURN(EndpointClient client, server->GetServerClient()); - check(SpawnProcessFromElfRegion(vaddr, client)); + ASSIGN_OR_RETURN(glcr::UniquePtr client, + server->GetServerClient()); + check(SpawnProcessFromElfRegion(vaddr, glcr::Move(client))); check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootVictoriaFallsVmmoCap, &vaddr)); ASSIGN_OR_RETURN(client, server->GetServerClient()); - check(SpawnProcessFromElfRegion(vaddr, client)); + check(SpawnProcessFromElfRegion(vaddr, glcr::Move(client))); check(server_thread.Join()); check(registration_thread.Join()); diff --git a/sys/yellowstone/yellowstone_server.cpp b/sys/yellowstone/yellowstone_server.cpp index 543b73e..0eb27a3 100644 --- a/sys/yellowstone/yellowstone_server.cpp +++ b/sys/yellowstone/yellowstone_server.cpp @@ -21,9 +21,8 @@ void RegistrationThreadBootstrap(void* yellowstone) { } glcr::ErrorCode HandleDenaliRegistration(z_cap_t endpoint_cap) { - EndpointClient endpoint = EndpointClient::AdoptEndpoint(endpoint_cap); - DenaliClient client(endpoint); - GptReader reader(client); + GptReader reader(glcr::UniquePtr( + new DenaliClient(EndpointClient::AdoptEndpoint(endpoint_cap)))); return reader.ParsePartitionTables(); } @@ -103,6 +102,7 @@ void YellowstoneServer::RegistrationThread() { } } -glcr::ErrorOr YellowstoneServer::GetServerClient() { +glcr::ErrorOr> +YellowstoneServer::GetServerClient() { return server_->CreateClient(); } diff --git a/sys/yellowstone/yellowstone_server.h b/sys/yellowstone/yellowstone_server.h index faa7d88..f2c0ce5 100644 --- a/sys/yellowstone/yellowstone_server.h +++ b/sys/yellowstone/yellowstone_server.h @@ -16,7 +16,7 @@ class YellowstoneServer { void ServerThread(); void RegistrationThread(); - glcr::ErrorOr GetServerClient(); + glcr::ErrorOr> GetServerClient(); private: glcr::UniquePtr server_;