Compare commits

..

8 Commits

25 changed files with 182 additions and 80 deletions

25
lib/glacier/memory/move.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
namespace glcr {
template <typename T>
struct RemoveReference {
typedef T type;
};
template <typename T>
struct RemoveReference<T&> {
typedef T type;
};
template <typename T>
struct RemoveReference<T&&> {
typedef T type;
};
template <typename T>
typename RemoveReference<T>::type&& Move(T&& arg) {
return static_cast<typename RemoveReference<T>::type&&>(arg);
}
} // namespace glcr

View File

@ -0,0 +1,52 @@
#pragma once
namespace glcr {
template <typename T>
class UniquePtr {
public:
UniquePtr() : ptr_(nullptr) {}
UniquePtr(decltype(nullptr)) : ptr_(nullptr) {}
UniquePtr(T* ptr) : ptr_(ptr) {}
UniquePtr(const UniquePtr<T>&) = delete;
UniquePtr& operator=(const UniquePtr<T>&) = delete;
UniquePtr(UniquePtr<T>&& other) : ptr_(other.ptr_) { other.ptr_ = nullptr; }
UniquePtr& operator=(UniquePtr<T>&& other) {
T* temp = ptr_;
ptr_ = other.ptr_;
other.ptr_ = temp;
return *this;
}
~UniquePtr() {
if (ptr_ != nullptr) {
delete ptr_;
}
}
explicit operator bool() const { return ptr_ != nullptr; }
bool empty() const { return ptr_ == nullptr; }
T* get() const { return ptr_; }
T* operator->() const { return ptr_; }
T& operator*() const { return *ptr_; }
T* release() {
T* tmp = ptr_;
ptr_ = nullptr;
return tmp;
}
private:
T* ptr_;
};
template <typename T, typename... Args>
UniquePtr<T> MakeUnique(Args... args) {
return UniquePtr<T>(new T(args...));
}
} // namespace glcr

View File

@ -1,5 +1,6 @@
#pragma once
#include <glacier/memory/move.h>
#include <glacier/status/error.h>
namespace glcr {
@ -15,7 +16,7 @@ class ErrorOr {
ErrorOr(ErrorCode code) : error_(code), ok_(false) {}
ErrorOr(const T& obj) : obj_(obj), ok_(true) {}
ErrorOr(T&& obj) : obj_(obj), ok_(true) {}
ErrorOr(T&& obj) : obj_(glcr::Move(obj)), ok_(true) {}
bool ok() { return ok_; }
operator bool() { return ok_; }
@ -41,6 +42,6 @@ class ErrorOr {
if (!AOR_VAR(__LINE__).ok()) { \
return AOR_VAR(__LINE__).error(); \
} \
lhs = AOR_VAR(__LINE__).value();
lhs = glcr::Move(AOR_VAR(__LINE__).value());
} // namespace glcr

View File

@ -10,7 +10,7 @@ target_include_directories(c
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(c
zion_lib
zion_stub
)
set_target_properties(c PROPERTIES

View File

@ -1,4 +1,4 @@
add_library(mammoth_lib STATIC
add_library(mammoth STATIC
src/channel.cpp
src/debug.cpp
src/endpoint_client.cpp
@ -12,16 +12,16 @@ add_library(mammoth_lib STATIC
src/thread.cpp
)
target_include_directories(mammoth_lib
target_include_directories(mammoth
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(mammoth_lib
target_link_libraries(mammoth
glacier
c
zion_lib)
zion_stub)
set_target_properties(mammoth_lib PROPERTIES
set_target_properties(mammoth PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}"
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${BASE_LINK_FLAGS}"
)

View File

@ -1,13 +1,18 @@
#pragma once
#include <glacier/container/pair.h>
#include <glacier/memory/unique_ptr.h>
#include <glacier/status/error_or.h>
#include <zcall.h>
#include <ztypes.h>
class EndpointClient {
public:
static EndpointClient AdoptEndpoint(z_cap_t cap);
EndpointClient() = delete;
EndpointClient(const EndpointClient&) = delete;
EndpointClient& operator=(const EndpointClient&) = delete;
static glcr::UniquePtr<EndpointClient> AdoptEndpoint(z_cap_t cap);
template <typename Req, typename Resp>
glcr::ErrorOr<glcr::Pair<Resp, z_cap_t>> CallEndpoint(const Req& req);

View File

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

View File

@ -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<EndpointClient> client);

View File

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

View File

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

View File

@ -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<EndpointClient> 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<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client.GetCap()));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client->GetCap()));
#if MAM_PROC_DEBUG
dbgln("Thread start");

View File

@ -13,8 +13,8 @@ target_include_directories(denali
target_link_libraries(denali
glacier
mammoth_lib
yellowstonestub
mammoth
yellowstone_stub
)
set_target_properties(denali PROPERTIES
@ -22,18 +22,18 @@ set_target_properties(denali PROPERTIES
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${BASE_LINK_FLAGS}"
)
add_library(libdenali
add_library(denali_stub
client/denali_client.cpp
)
target_include_directories(libdenali
target_include_directories(denali_stub
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(libdenali
mammoth_lib)
target_link_libraries(denali_stub
mammoth)
set_target_properties(libdenali PROPERTIES
set_target_properties(denali_stub PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}"
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${BASE_LINK_FLAGS}"
)

View File

@ -11,7 +11,7 @@ glcr::ErrorOr<MappedMemoryRegion> DenaliClient::ReadSectors(
.lba = lba,
.size = num_sectors,
};
auto pair_or = endpoint_.CallEndpoint<DenaliRead, DenaliReadResponse>(read);
auto pair_or = endpoint_->CallEndpoint<DenaliRead, DenaliReadResponse>(read);
if (!pair_or) {
return pair_or.error();
}

View File

@ -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<EndpointClient> yellowstone =
EndpointClient::AdoptEndpoint(gInitEndpointCap);
YellowstoneGetReq req{
.type = kYellowstoneGetRegistration,
};
auto resp_cap_or =
yellowstone
.CallEndpoint<YellowstoneGetReq, YellowstoneGetRegistrationResp>(req);
->CallEndpoint<YellowstoneGetReq, YellowstoneGetRegistrationResp>(
req);
if (!resp_cap_or.ok()) {
dbgln("Bad call");
check(resp_cap_or.error());
@ -28,11 +30,13 @@ uint64_t main(uint64_t init_port_cap) {
auto resp_cap = resp_cap_or.value();
PortClient notify = PortClient::AdoptPort(resp_cap.second());
ASSIGN_OR_RETURN(EndpointServer endpoint, EndpointServer::Create());
ASSIGN_OR_RETURN(EndpointClient client, endpoint.CreateClient());
notify.WriteMessage("denali", client.GetCap());
ASSIGN_OR_RETURN(glcr::UniquePtr<EndpointServer> endpoint,
EndpointServer::Create());
ASSIGN_OR_RETURN(glcr::UniquePtr<EndpointClient> client,
endpoint->CreateClient());
notify.WriteMessage("denali", client->GetCap());
DenaliServer server(endpoint, driver);
DenaliServer server(glcr::Move(endpoint), driver);
RET_ERR(server.RunServer());
// FIXME: Add thread join.
return 0;

View File

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

View File

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

View File

@ -6,12 +6,13 @@
class DenaliClient {
public:
DenaliClient(const EndpointClient& endpoint) : endpoint_(endpoint) {}
DenaliClient(glcr::UniquePtr<EndpointClient> endpoint)
: endpoint_(glcr::Move(endpoint)) {}
glcr::ErrorOr<MappedMemoryRegion> ReadSectors(uint64_t device_id,
uint64_t lba,
uint64_t num_sectors);
private:
EndpointClient endpoint_;
glcr::UniquePtr<EndpointClient> endpoint_;
};

View File

@ -7,7 +7,7 @@ target_include_directories(victoriafalls
target_link_libraries(victoriafalls
glacier
mammoth_lib
mammoth
)
set_target_properties(victoriafalls PROPERTIES

View File

@ -8,9 +8,9 @@ add_executable(yellowstone
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(yellowstone
mammoth_lib
mammoth
glacier
libdenali
denali_stub
)
set_target_properties(yellowstone PROPERTIES
@ -19,15 +19,15 @@ set_target_properties(yellowstone PROPERTIES
)
add_library(yellowstonestub
add_library(yellowstone_stub
stub/yellowstone_stub.cpp
)
target_include_directories(yellowstonestub
target_include_directories(yellowstone_stub
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
set_target_properties(yellowstonestub PROPERTIES
set_target_properties(yellowstone_stub PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}"
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${BASE_LINK_FLAGS}"
)

View File

@ -47,11 +47,12 @@ struct PartitionEntry {
char partition_name[72];
} __attribute__((packed));
GptReader::GptReader(const DenaliClient& client) : denali_(client) {}
GptReader::GptReader(glcr::UniquePtr<DenaliClient> 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<uint16_t*>(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<PartitionEntry*>(

View File

@ -6,10 +6,10 @@
class GptReader {
public:
GptReader(const DenaliClient&);
GptReader(glcr::UniquePtr<DenaliClient> denali);
glcr::ErrorCode ParsePartitionTables();
private:
DenaliClient denali_;
glcr::UniquePtr<DenaliClient> denali_;
};

View File

@ -12,19 +12,20 @@ uint64_t main(uint64_t port_cap) {
dbgln("Yellowstone Initializing.");
check(ParseInitPort(port_cap));
ASSIGN_OR_RETURN(YellowstoneServer server, YellowstoneServer::Create());
Thread server_thread = server.RunServer();
Thread registration_thread = server.RunRegistration();
ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create());
Thread server_thread = server->RunServer();
Thread registration_thread = server->RunRegistration();
DumpPciEDevices();
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<EndpointClient> 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));
ASSIGN_OR_RETURN(client, server->GetServerClient());
check(SpawnProcessFromElfRegion(vaddr, glcr::Move(client)));
check(server_thread.Join());
check(registration_thread.Join());

View File

@ -21,23 +21,24 @@ 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<DenaliClient>(
new DenaliClient(EndpointClient::AdoptEndpoint(endpoint_cap))));
return reader.ParsePartitionTables();
}
} // namespace
glcr::ErrorOr<YellowstoneServer> YellowstoneServer::Create() {
ASSIGN_OR_RETURN(EndpointServer server, EndpointServer::Create());
glcr::ErrorOr<glcr::UniquePtr<YellowstoneServer>> YellowstoneServer::Create() {
ASSIGN_OR_RETURN(auto server, EndpointServer::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)
: server_(server), register_port_(port) {}
YellowstoneServer::YellowstoneServer(glcr::UniquePtr<EndpointServer> server,
PortServer port)
: server_(glcr::Move(server)), register_port_(port) {}
Thread YellowstoneServer::RunServer() {
return Thread(ServerThreadBootstrap, this);
@ -52,7 +53,7 @@ void YellowstoneServer::ServerThread() {
uint64_t num_bytes = kBufferSize;
uint64_t reply_port_cap;
// 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 =
reinterpret_cast<YellowstoneGetReq*>(server_buffer_);
switch (req->type) {
@ -101,6 +102,7 @@ void YellowstoneServer::RegistrationThread() {
}
}
glcr::ErrorOr<EndpointClient> YellowstoneServer::GetServerClient() {
return server_.CreateClient();
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>>
YellowstoneServer::GetServerClient() {
return server_->CreateClient();
}

View File

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

View File

@ -82,14 +82,14 @@ set_target_properties(zion
add_library(zion_lib STATIC
add_library(zion_stub STATIC
usr/crt0.s
usr/zcall.cpp)
target_include_directories(zion_lib
target_include_directories(zion_stub
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
set_target_properties(zion_lib
set_target_properties(zion_stub
PROPERTIES
COMPILE_FLAGS "${_Z_COMPILE_FLAGS}")