Compare commits

...

3 Commits

23 changed files with 296 additions and 39 deletions

View File

@ -8,6 +8,9 @@ class String {
public:
String(const char* cstr);
const char* cstr() const { return cstr_; }
uint64_t length() const { return length_; }
bool operator==(const String& str);
private:

View File

@ -12,6 +12,8 @@ class EndpointClient {
template <typename Req, typename Resp>
glcr::ErrorOr<glcr::Pair<Resp, z_cap_t>> CallEndpoint(const Req& req);
z_cap_t GetCap() { return cap_; }
private:
EndpointClient(uint64_t cap) : cap_(cap) {}
z_cap_t cap_;

View File

@ -1,17 +1,27 @@
#pragma once
#include <glacier/status/error_or.h>
#include <glacier/string/string.h>
#include <stdint.h>
#include <zcall.h>
// FIXME: Split send and receive.
class Port {
public:
static glcr::ErrorOr<Port> Create();
Port(uint64_t port_cap);
glcr::ErrorCode RecvCap(uint64_t* num_bytes, char* msg, uint64_t* cap);
z_err_t PollForIntCap(uint64_t* msg, uint64_t* cap);
template <typename T>
z_err_t WriteMessage(const T& obj, uint64_t cap);
glcr::ErrorCode WriteString(glcr::String str, uint64_t cap);
// FIXME: We can't create error_ors of ints
glcr::ErrorCode Duplicate(uint64_t* new_cap);
private:
uint64_t port_cap_;
};

View File

@ -1,8 +1,9 @@
#pragma once
#include <glacier/status/error_or.h>
#include <glacier/status/error.h>
#include <stdint.h>
#include "mammoth/endpoint_client.h"
glcr::ErrorOr<EndpointClient> SpawnProcessFromElfRegion(uint64_t program);
glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
EndpointClient client);

View File

@ -1,5 +1,6 @@
#pragma once
#include <glacier/status/error.h>
#include <stdint.h>
class Thread {
@ -9,6 +10,8 @@ class Thread {
Thread() : thread_cap_(0) {}
Thread(Entry e, const void* arg1);
[[nodiscard]] glcr::ErrorCode Join();
private:
uint64_t thread_cap_;
};

View File

@ -5,8 +5,23 @@
#include "mammoth/debug.h"
glcr::ErrorOr<Port> Port::Create() {
z_cap_t port;
RET_ERR(ZPortCreate(&port));
return Port(port);
}
Port::Port(uint64_t port_cap) : port_cap_(port_cap) {}
glcr::ErrorCode Port::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;
}
z_err_t Port::PollForIntCap(uint64_t *msg, uint64_t *cap) {
uint64_t bytes = sizeof(uint64_t);
uint64_t caps = 1;
@ -21,3 +36,10 @@ z_err_t Port::PollForIntCap(uint64_t *msg, uint64_t *cap) {
}
return glcr::OK;
}
glcr::ErrorCode Port::WriteString(glcr::String str, uint64_t cap) {
return ZPortSend(port_cap_, str.length() + 1, str.cstr(), 1, &cap);
}
glcr::ErrorCode Port::Duplicate(uint64_t *new_cap) {
return ZCapDuplicate(port_cap_, new_cap);
}

View File

@ -95,10 +95,8 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
} // namespace
glcr::ErrorOr<EndpointClient> SpawnProcessFromElfRegion(uint64_t program) {
ASSIGN_OR_RETURN(EndpointServer server, EndpointServer::Create());
ASSIGN_OR_RETURN(EndpointClient client, server.CreateClient());
glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
EndpointClient client) {
uint64_t proc_cap;
uint64_t as_cap;
uint64_t foreign_port_id;
@ -114,7 +112,7 @@ glcr::ErrorOr<EndpointClient> SpawnProcessFromElfRegion(uint64_t program) {
#if MAM_PROC_DEBUG
dbgln("Spawn");
#endif
check(ZProcessSpawn(gSelfProcCap, port_cap_donate, &proc_cap, &as_cap,
RET_ERR(ZProcessSpawn(gSelfProcCap, port_cap_donate, &proc_cap, &as_cap,
&foreign_port_id));
uint64_t entry_point = LoadElfProgram(program, as_cap);
@ -123,17 +121,17 @@ glcr::ErrorOr<EndpointClient> SpawnProcessFromElfRegion(uint64_t program) {
dbgln("Thread Create");
#endif
uint64_t thread_cap;
check(ZThreadCreate(proc_cap, &thread_cap));
RET_ERR(ZThreadCreate(proc_cap, &thread_cap));
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_ENDPOINT, server.GetCap()));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
RET_ERR(p.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client.GetCap()));
#if MAM_PROC_DEBUG
dbgln("Thread start");
#endif
check(ZThreadStart(thread_cap, entry_point, foreign_port_id, 0));
RET_ERR(ZThreadStart(thread_cap, entry_point, foreign_port_id, 0));
return client;
return glcr::OK;
}

View File

@ -10,7 +10,7 @@ namespace {
extern "C" void thread_entry(Thread::Entry entry, void* arg1) {
entry(arg1);
ZThreadExit();
(void)ZThreadExit();
}
} // namespace
@ -21,3 +21,5 @@ Thread::Thread(Entry e, const void* arg1) {
reinterpret_cast<uint64_t>(e),
reinterpret_cast<uint64_t>(arg1)));
}
glcr::ErrorCode Thread::Join() { return ZThreadWait(thread_cap_); }

View File

@ -15,6 +15,7 @@ target_link_libraries(denali
cxx
glacier
mammoth_lib
yellowstonestub
)
set_target_properties(denali PROPERTIES

View File

@ -2,7 +2,9 @@
#include <mammoth/debug.h>
#include <mammoth/endpoint_server.h>
#include <mammoth/init.h>
#include <mammoth/port.h>
#include <stdint.h>
#include <yellowstone.h>
#include "ahci/ahci_driver.h"
#include "denali_server.h"
@ -12,7 +14,24 @@ uint64_t main(uint64_t init_port_cap) {
AhciDriver driver;
RET_ERR(driver.Init());
EndpointServer endpoint = EndpointServer::Adopt(gInitEndpointCap);
EndpointClient yellowstone = EndpointClient::AdoptEndpoint(gInitEndpointCap);
YellowstoneGetReq req{
.type = kYellowstoneGetRegistration,
};
auto resp_cap_or =
yellowstone
.CallEndpoint<YellowstoneGetReq, YellowstoneGetRegistrationResp>(req);
if (!resp_cap_or.ok()) {
dbgln("Bad call");
check(resp_cap_or.error());
}
auto resp_cap = resp_cap_or.value();
Port notify(resp_cap.second());
ASSIGN_OR_RETURN(EndpointServer endpoint, EndpointServer::Create());
ASSIGN_OR_RETURN(EndpointClient client, endpoint.CreateClient());
notify.WriteMessage("denali", client.GetCap());
DenaliServer server(endpoint, driver);
RET_ERR(server.RunServer());
// FIXME: Add thread join.

View File

@ -2,6 +2,7 @@ add_executable(yellowstone
hw/gpt.cpp
hw/pcie.cpp
yellowstone.cpp
yellowstone_server.cpp
)
target_include_directories(yellowstone
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
@ -9,6 +10,7 @@ add_executable(yellowstone
target_link_libraries(yellowstone
cxx
mammoth_lib
glacier
libdenali
)
@ -16,3 +18,17 @@ set_target_properties(yellowstone PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}"
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${BASE_LINK_FLAGS}"
)
add_library(yellowstonestub
stub/yellowstone_stub.cpp
)
target_include_directories(yellowstonestub
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
set_target_properties(yellowstonestub PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}"
LINK_FLAGS "${CMAKE_EXE_LINK_FLAGS} ${BASE_LINK_FLAGS}"
)

View File

@ -0,0 +1,12 @@
#pragma once
#include <stdint.h>
const uint64_t kYellowstoneGetAhci = 0x01;
const uint64_t kYellowstoneGetRegistration = 0x02;
struct YellowstoneGetReq {
uint64_t type;
};
struct YellowstoneGetRegistrationResp {};

View File

@ -0,0 +1,3 @@
// PLACEHOLDER
#include <stdint.h>
uint64_t a = 0;

View File

@ -1,4 +1,3 @@
#include <denali/denali.h>
#include <mammoth/debug.h>
#include <mammoth/endpoint_client.h>
#include <mammoth/init.h>
@ -7,32 +6,28 @@
#include "hw/gpt.h"
#include "hw/pcie.h"
#include "yellowstone_server.h"
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();
DumpPciEDevices();
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr));
auto endpoint_or = SpawnProcessFromElfRegion(vaddr);
if (!endpoint_or) {
check(endpoint_or.error());
}
EndpointClient endpoint = endpoint_or.value();
DenaliClient client(endpoint);
GptReader reader(client);
check(reader.ParsePartitionTables());
ASSIGN_OR_RETURN(EndpointClient client, server.GetServerClient());
check(SpawnProcessFromElfRegion(vaddr, client));
check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootVictoriaFallsVmmoCap, &vaddr));
auto error_or = SpawnProcessFromElfRegion(vaddr);
if (!error_or) {
check(error_or.error());
}
ASSIGN_OR_RETURN(client, server.GetServerClient());
check(SpawnProcessFromElfRegion(vaddr, client));
check(server_thread.Join());
check(registration_thread.Join());
dbgln("Yellowstone Finished Successfully.");
return 0;
}

View File

@ -0,0 +1,105 @@
#include "yellowstone_server.h"
#include <denali/denali.h>
#include <glacier/string/string.h>
#include <mammoth/debug.h>
#include <stdlib.h>
#include "hw/gpt.h"
#include "include/yellowstone.h"
// FIXME: This linkage was missing :(
void* operator new[](uint64_t size) { return malloc(size); }
namespace {
void ServerThreadBootstrap(void* yellowstone) {
dbgln("Yellowstone server starting");
static_cast<YellowstoneServer*>(yellowstone)->ServerThread();
}
void RegistrationThreadBootstrap(void* yellowstone) {
dbgln("Yellowstone registration starting");
static_cast<YellowstoneServer*>(yellowstone)->RegistrationThread();
}
glcr::ErrorCode HandleDenaliRegistration(z_cap_t endpoint_cap) {
EndpointClient endpoint = EndpointClient::AdoptEndpoint(endpoint_cap);
DenaliClient client(endpoint);
GptReader reader(client);
return reader.ParsePartitionTables();
}
} // namespace
glcr::ErrorOr<YellowstoneServer> YellowstoneServer::Create() {
ASSIGN_OR_RETURN(EndpointServer server, EndpointServer::Create());
ASSIGN_OR_RETURN(Port port, Port::Create());
return YellowstoneServer(server, port);
}
YellowstoneServer::YellowstoneServer(EndpointServer server, Port port)
: server_(server), register_port_(port) {}
Thread YellowstoneServer::RunServer() {
return Thread(ServerThreadBootstrap, this);
}
Thread YellowstoneServer::RunRegistration() {
return Thread(RegistrationThreadBootstrap, this);
}
void YellowstoneServer::ServerThread() {
while (true) {
uint64_t num_bytes = kBufferSize;
uint64_t reply_port_cap;
// FIXME: Error handling.
check(server_.Recieve(&num_bytes, server_buffer_, &reply_port_cap));
YellowstoneGetReq* req =
reinterpret_cast<YellowstoneGetReq*>(server_buffer_);
switch (req->type) {
case kYellowstoneGetAhci:
dbgln("Yellowstone::GetAHCI");
break;
case kYellowstoneGetRegistration: {
dbgln("Yellowstone::GetRegistration");
uint64_t reg_cap;
check(register_port_.Duplicate(&reg_cap));
YellowstoneGetRegistrationResp resp;
check(ZReplyPortSend(reply_port_cap, sizeof(resp), &resp, 1, &reg_cap));
break;
}
default:
dbgln("Unknown request type: %x", req->type);
break;
}
}
}
void YellowstoneServer::RegistrationThread() {
while (true) {
uint64_t num_bytes = kBufferSize;
z_cap_t endpoint_cap;
// FIXME: Better error handling here.
check(register_port_.RecvCap(&num_bytes, registration_buffer_,
&endpoint_cap));
glcr::String name(registration_buffer_);
if (name == "denali") {
denali_cap_ = endpoint_cap;
check(HandleDenaliRegistration(denali_cap_));
continue;
}
if (name == "victoriafalls") {
victoria_falls_cap_ = endpoint_cap;
continue;
}
dbgln("[WARN] Got endpoint cap type:");
dbgln(name.cstr());
}
}
glcr::ErrorOr<EndpointClient> YellowstoneServer::GetServerClient() {
return server_.CreateClient();
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <glacier/status/error_or.h>
#include <mammoth/endpoint_server.h>
#include <mammoth/port.h>
#include <mammoth/thread.h>
class YellowstoneServer {
public:
static glcr::ErrorOr<YellowstoneServer> Create();
Thread RunServer();
Thread RunRegistration();
void ServerThread();
void RegistrationThread();
glcr::ErrorOr<EndpointClient> GetServerClient();
private:
EndpointServer server_;
Port register_port_;
static const uint64_t kBufferSize = 128;
uint8_t server_buffer_[kBufferSize];
char registration_buffer_[kBufferSize];
// TODO: Store these in a data structure.
z_cap_t denali_cap_ = 0;
z_cap_t victoria_falls_cap_ = 0;
YellowstoneServer(EndpointServer server, Port port);
};

View File

@ -96,6 +96,7 @@ SYS2(ThreadCreate, z_cap_t, proc_cap, z_cap_t*, thread_cap);
SYS4(ThreadStart, z_cap_t, thread_cap, uint64_t, entry, uint64_t, arg1,
uint64_t, arg2);
SYS0(ThreadExit);
SYS1(ThreadWait, z_cap_t, thread_cap);
SYS4(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t,
vmmo_cap, uint64_t*, vaddr);

View File

@ -17,6 +17,7 @@ const uint64_t kZionProcessSpawn = 0x2;
const uint64_t kZionThreadCreate = 0x10;
const uint64_t kZionThreadStart = 0x11;
const uint64_t kZionThreadExit = 0x12;
const uint64_t kZionThreadWait = 0x13;
// Memory Calls
const uint64_t kZionAddressSpaceMap = 0x21;

View File

@ -69,5 +69,18 @@ void Thread::Exit() {
#endif
state_ = FINISHED;
process_.CheckState();
while (!blocked_threads_.size() == 0) {
auto thread = blocked_threads_.PopFront();
thread->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(thread);
}
gScheduler->Yield();
}
void Thread::Wait() {
// FIXME: We need synchronization code here.
auto thread = gScheduler->CurrentThread();
thread->SetState(Thread::BLOCKED);
blocked_threads_.PushBack(thread);
gScheduler->Yield();
}

View File

@ -48,6 +48,8 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
void SetState(State state) { state_ = state; }
void Exit();
void Wait();
private:
friend class glcr::MakeRefCountedFriend<Thread>;
Thread(Process& proc, uint64_t tid);
@ -68,4 +70,6 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
// Stack pointer to take when returning from userspace.
// I don't think me mind clobbering the stack here.
uint64_t rsp0_start_;
glcr::IntrusiveList<Thread> blocked_threads_;
};

View File

@ -55,6 +55,7 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
CASE(ThreadCreate);
CASE(ThreadStart);
CASE(ThreadExit);
CASE(ThreadWait);
// syscall/address_space.h
CASE(AddressSpaceMap);
// syscall/memory_object.h

View File

@ -3,18 +3,18 @@
#include "capability/capability.h"
#include "scheduler/scheduler.h"
z_err_t ThreadCreate(ZThreadCreateReq* req) {
glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req) {
auto& curr_proc = gScheduler->CurrentProcess();
auto cap = curr_proc.GetCapability(req->proc_cap);
RET_ERR(ValidateCapability<Process>(cap, ZC_PROC_SPAWN_THREAD));
auto parent_proc = cap->obj<Process>();
auto thread = parent_proc->CreateThread();
*req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE);
*req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE | ZC_READ);
return glcr::OK;
}
z_err_t ThreadStart(ZThreadStartReq* req) {
glcr::ErrorCode ThreadStart(ZThreadStartReq* req) {
auto& curr_proc = gScheduler->CurrentProcess();
auto cap = curr_proc.GetCapability(req->thread_cap);
RET_ERR(ValidateCapability<Thread>(cap, ZC_WRITE));
@ -25,8 +25,17 @@ z_err_t ThreadStart(ZThreadStartReq* req) {
return glcr::OK;
}
z_err_t ThreadExit(ZThreadExitReq*) {
glcr::ErrorCode ThreadExit(ZThreadExitReq*) {
auto curr_thread = gScheduler->CurrentThread();
curr_thread->Exit();
panic("Returned from thread exit");
}
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req) {
auto& curr_proc = gScheduler->CurrentProcess();
auto cap = curr_proc.GetCapability(req->thread_cap);
RET_ERR(ValidateCapability<Thread>(cap, ZC_READ));
auto thread = cap->obj<Thread>();
thread->Wait();
return glcr::OK;
}

View File

@ -1,7 +1,10 @@
#pragma once
#include <glacier/status/error.h>
#include "include/zcall.h"
z_err_t ThreadCreate(ZThreadCreateReq* req);
z_err_t ThreadStart(ZThreadStartReq* req);
z_err_t ThreadExit(ZThreadExitReq*);
glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req);
glcr::ErrorCode ThreadStart(ZThreadStartReq* req);
glcr::ErrorCode ThreadExit(ZThreadExitReq*);
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req);