[Yellowstone][Mammoth] Delete unused cpp code.
This commit is contained in:
parent
d58cbed0df
commit
c1db6cb11f
24 changed files with 0 additions and 1590 deletions
|
|
@ -1,14 +1,7 @@
|
|||
add_library(mammoth STATIC
|
||||
file/file.cpp
|
||||
input/keyboard.cpp
|
||||
ipc/channel.cpp
|
||||
ipc/endpoint_client.cpp
|
||||
ipc/endpoint_server.cpp
|
||||
ipc/port_client.cpp
|
||||
ipc/port_server.cpp
|
||||
proc/process.cpp
|
||||
proc/thread.cpp
|
||||
sync/mutex.cpp
|
||||
sync/semaphore.cpp
|
||||
util/debug.cpp
|
||||
util/init.cpp
|
||||
|
|
|
|||
|
|
@ -1,81 +0,0 @@
|
|||
#include "file/file.h"
|
||||
|
||||
#include <glacier/string/str_split.h>
|
||||
#include <mammoth/util/init.h>
|
||||
#include <victoriafalls/victoriafalls.yunq.client.h>
|
||||
#include <yellowstone/yellowstone.yunq.client.h>
|
||||
#include <zcall.h>
|
||||
|
||||
#include "util/debug.h"
|
||||
|
||||
namespace mmth {
|
||||
namespace {
|
||||
|
||||
using yellowstone::Endpoint;
|
||||
using yellowstone::GetEndpointRequest;
|
||||
using yellowstone::YellowstoneClient;
|
||||
|
||||
VFSClient* gVfsClient = nullptr;
|
||||
|
||||
void GetVfsClientIfNeeded() {
|
||||
if (gVfsClient == nullptr) {
|
||||
// TODO: Add an unowned client so we don't have to duplicate this cap every
|
||||
// time.
|
||||
uint64_t dup_cap;
|
||||
check(ZCapDuplicate(gInitEndpointCap, kZionPerm_All, &dup_cap));
|
||||
YellowstoneClient client(dup_cap);
|
||||
|
||||
GetEndpointRequest yreq;
|
||||
yreq.set_endpoint_name("victoriafalls");
|
||||
Endpoint yresp;
|
||||
check(client.GetEndpoint(yreq, yresp));
|
||||
|
||||
gVfsClient = new VFSClient(yresp.endpoint());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void SetVfsCap(z_cap_t vfs_cap) { gVfsClient = new VFSClient(vfs_cap); }
|
||||
|
||||
File File::Open(glcr::StringView path) {
|
||||
GetVfsClientIfNeeded();
|
||||
|
||||
OpenFileRequest req;
|
||||
req.set_path(path);
|
||||
OpenFileResponse resp;
|
||||
check(gVfsClient->OpenFile(req, resp));
|
||||
|
||||
return File(OwnedMemoryRegion::FromCapability(resp.memory()), resp.size());
|
||||
}
|
||||
|
||||
glcr::StringView File::as_str() {
|
||||
return glcr::StringView((char*)raw_ptr(), size_);
|
||||
}
|
||||
|
||||
void* File::raw_ptr() { return reinterpret_cast<void*>(file_data_.vaddr()); }
|
||||
uint8_t* File::byte_ptr() {
|
||||
return reinterpret_cast<uint8_t*>(file_data_.vaddr());
|
||||
}
|
||||
|
||||
glcr::ErrorOr<glcr::Vector<glcr::String>> ListDirectory(glcr::StringView path) {
|
||||
GetVfsClientIfNeeded();
|
||||
|
||||
GetDirectoryRequest req;
|
||||
req.set_path(path);
|
||||
Directory dir;
|
||||
auto status = gVfsClient->GetDirectory(req, dir);
|
||||
if (!status.ok()) {
|
||||
dbgln("Error in getting directory: {}", status.message());
|
||||
return status.code();
|
||||
}
|
||||
|
||||
auto file_views = glcr::StrSplit(dir.filenames(), ',');
|
||||
glcr::Vector<glcr::String> files;
|
||||
for (const auto& view : glcr::StrSplit(dir.filenames(), ',')) {
|
||||
files.PushBack(view);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/container/vector.h>
|
||||
#include <glacier/memory/move.h>
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <glacier/string/string.h>
|
||||
#include <glacier/string/string_view.h>
|
||||
|
||||
#include "mammoth/util/memory_region.h"
|
||||
|
||||
namespace mmth {
|
||||
|
||||
// Intended for use in yellowstone since it already has the VFS cap.
|
||||
void SetVfsCap(z_cap_t vfs_cap);
|
||||
|
||||
class File {
|
||||
public:
|
||||
static File Open(glcr::StringView path);
|
||||
|
||||
uint64_t size() { return size_; }
|
||||
|
||||
glcr::StringView as_str();
|
||||
|
||||
void* raw_ptr();
|
||||
uint8_t* byte_ptr();
|
||||
|
||||
private:
|
||||
OwnedMemoryRegion file_data_;
|
||||
uint64_t size_;
|
||||
|
||||
File(OwnedMemoryRegion&& file, uint64_t size)
|
||||
: file_data_(glcr::Move(file)), size_(size) {}
|
||||
};
|
||||
|
||||
// TODO: Move this to a separate file.
|
||||
glcr::ErrorOr<glcr::Vector<glcr::String>> ListDirectory(glcr::StringView path);
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,228 +0,0 @@
|
|||
#include "input/keyboard.h"
|
||||
|
||||
#include <mammoth/util/init.h>
|
||||
#include <voyageurs/voyageurs.yunq.client.h>
|
||||
#include <yellowstone/yellowstone.yunq.client.h>
|
||||
|
||||
#include "util/debug.h"
|
||||
|
||||
namespace mmth {
|
||||
namespace {
|
||||
|
||||
using yellowstone::Endpoint;
|
||||
using yellowstone::GetEndpointRequest;
|
||||
using yellowstone::YellowstoneClient;
|
||||
|
||||
void KeyboardListenerEntry(void* keyboard_base) {
|
||||
reinterpret_cast<KeyboardListenerBase*>(keyboard_base)->ListenLoop();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
KeyboardListenerBase::KeyboardListenerBase() {
|
||||
auto server_or = PortServer::Create();
|
||||
if (!server_or) {
|
||||
crash("Failed to create server", server_or.error());
|
||||
}
|
||||
server_ = server_or.value();
|
||||
}
|
||||
|
||||
void KeyboardListenerBase::Register() {
|
||||
uint64_t dup_cap;
|
||||
check(ZCapDuplicate(gInitEndpointCap, kZionPerm_All, &dup_cap));
|
||||
YellowstoneClient client(dup_cap);
|
||||
|
||||
GetEndpointRequest req;
|
||||
req.set_endpoint_name("voyageurs");
|
||||
Endpoint endpt;
|
||||
check(client.GetEndpoint(req, endpt));
|
||||
|
||||
VoyageursClient vclient(endpt.endpoint());
|
||||
KeyboardListener listn;
|
||||
|
||||
// TODO: Create a "ASSIGN_OR_CRASH" macro to simplify this.
|
||||
auto client_or = server_.CreateClient();
|
||||
if (!client_or.ok()) {
|
||||
crash("Failed to create client", client_or.error());
|
||||
}
|
||||
listn.set_port_capability(client_or.value().cap());
|
||||
check(vclient.RegisterKeyboardListener(listn));
|
||||
}
|
||||
|
||||
Thread KeyboardListenerBase::Listen() {
|
||||
return Thread(KeyboardListenerEntry, this);
|
||||
}
|
||||
|
||||
void KeyboardListenerBase::ListenLoop() {
|
||||
while (true) {
|
||||
auto scancode_or = server_.RecvUint16();
|
||||
if (!scancode_or.ok()) {
|
||||
check(scancode_or.error());
|
||||
}
|
||||
uint16_t scancode = scancode_or.value();
|
||||
|
||||
Keycode k = ScancodeToKeycode(scancode & 0xFF);
|
||||
uint8_t modifiers = (scancode >> 8) & 0xFF;
|
||||
HandleKeycode(k, modifiers);
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardListenerBase::HandleKeycode(Keycode code, uint8_t modifiers) {
|
||||
char c = '\0';
|
||||
|
||||
if (code >= kA && code <= kZ) {
|
||||
if (IsShift(modifiers)) {
|
||||
const char* alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
c = alpha[code - kA];
|
||||
|
||||
} else {
|
||||
const char* alpha = "abcdefghijklmnopqrstuvwxyz";
|
||||
c = alpha[code - kA];
|
||||
}
|
||||
} else if (code >= k1 && code <= k0) {
|
||||
if (IsShift(modifiers)) {
|
||||
const char* num = "!@#$%^&*()";
|
||||
c = num[code - k1];
|
||||
} else {
|
||||
const char* num = "1234567890";
|
||||
c = num[code - k1];
|
||||
}
|
||||
} else if (code >= kMinus && code <= kBacktick) {
|
||||
if (IsShift(modifiers)) {
|
||||
const char* sym = "_+{}|?:\"<>~";
|
||||
c = sym[code - kMinus];
|
||||
} else {
|
||||
const char* sym = "-=[]\\/;',.`";
|
||||
c = sym[code - kMinus];
|
||||
}
|
||||
} else if (code == kEnter) {
|
||||
c = '\n';
|
||||
} else if (code == kSpace) {
|
||||
c = ' ';
|
||||
} else if (code == kTab) {
|
||||
c = '\t';
|
||||
} else if (code == kBackspace) {
|
||||
c = '\b';
|
||||
}
|
||||
|
||||
if (c != '\0') {
|
||||
HandleCharacter(c);
|
||||
}
|
||||
}
|
||||
|
||||
Keycode KeyboardListenerBase::ScancodeToKeycode(uint16_t scancode) {
|
||||
switch (scancode) {
|
||||
case 0x04:
|
||||
return kA;
|
||||
case 0x05:
|
||||
return kB;
|
||||
case 0x06:
|
||||
return kC;
|
||||
case 0x07:
|
||||
return kD;
|
||||
case 0x08:
|
||||
return kE;
|
||||
case 0x09:
|
||||
return kF;
|
||||
case 0x0A:
|
||||
return kG;
|
||||
case 0x0B:
|
||||
return kH;
|
||||
case 0x0C:
|
||||
return kI;
|
||||
case 0x0D:
|
||||
return kJ;
|
||||
case 0x0E:
|
||||
return kK;
|
||||
case 0x0F:
|
||||
return kL;
|
||||
case 0x10:
|
||||
return kM;
|
||||
case 0x11:
|
||||
return kN;
|
||||
case 0x12:
|
||||
return kO;
|
||||
case 0x13:
|
||||
return kP;
|
||||
case 0x14:
|
||||
return kQ;
|
||||
case 0x15:
|
||||
return kR;
|
||||
case 0x16:
|
||||
return kS;
|
||||
case 0x17:
|
||||
return kT;
|
||||
case 0x18:
|
||||
return kU;
|
||||
case 0x19:
|
||||
return kV;
|
||||
case 0x1A:
|
||||
return kW;
|
||||
case 0x1B:
|
||||
return kX;
|
||||
case 0x1C:
|
||||
return kY;
|
||||
case 0x1D:
|
||||
return kZ;
|
||||
case 0x1E:
|
||||
return k1;
|
||||
case 0x1F:
|
||||
return k2;
|
||||
case 0x20:
|
||||
return k3;
|
||||
case 0x21:
|
||||
return k4;
|
||||
case 0x22:
|
||||
return k5;
|
||||
case 0x23:
|
||||
return k6;
|
||||
case 0x24:
|
||||
return k7;
|
||||
case 0x25:
|
||||
return k8;
|
||||
case 0x26:
|
||||
return k9;
|
||||
case 0x27:
|
||||
return k0;
|
||||
case 0x28:
|
||||
return kEnter;
|
||||
case 0x29:
|
||||
return kEsc;
|
||||
case 0x2A:
|
||||
return kBackspace;
|
||||
case 0x2B:
|
||||
return kTab;
|
||||
case 0x2C:
|
||||
return kSpace;
|
||||
case 0x2D:
|
||||
return kMinus;
|
||||
case 0x2E:
|
||||
return kEquals;
|
||||
case 0x2F:
|
||||
return kLBrace;
|
||||
case 0x30:
|
||||
return kRBrace;
|
||||
case 0x31:
|
||||
return kBSlash;
|
||||
case 0x33:
|
||||
return kSemicolon;
|
||||
case 0x34:
|
||||
return kQuote;
|
||||
case 0x35:
|
||||
return kBacktick;
|
||||
case 0x36:
|
||||
return kComma;
|
||||
case 0x37:
|
||||
return kPeriod;
|
||||
case 0x38:
|
||||
return kFSlash;
|
||||
case 0x39:
|
||||
return kEsc; // Capslock
|
||||
}
|
||||
|
||||
dbgln("Unknown scancode {x}", scancode);
|
||||
|
||||
return kUnknownKeycode;
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,113 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "mammoth/ipc/port_server.h"
|
||||
#include "mammoth/proc/thread.h"
|
||||
|
||||
namespace mmth {
|
||||
|
||||
enum Keycode {
|
||||
kUnknownKeycode = 0x0,
|
||||
|
||||
kA = 0x1,
|
||||
kB = 0x2,
|
||||
kC = 0x3,
|
||||
kD = 0x4,
|
||||
kE = 0x5,
|
||||
kF = 0x6,
|
||||
kG = 0x7,
|
||||
kH = 0x8,
|
||||
kI = 0x9,
|
||||
kJ = 0xA,
|
||||
kK = 0xB,
|
||||
kL = 0xC,
|
||||
kM = 0xD,
|
||||
kN = 0xE,
|
||||
kO = 0xF,
|
||||
kP = 0x10,
|
||||
kQ = 0x11,
|
||||
kR = 0x12,
|
||||
kS = 0x13,
|
||||
kT = 0x14,
|
||||
kU = 0x15,
|
||||
kV = 0x16,
|
||||
kW = 0x17,
|
||||
kX = 0x18,
|
||||
kY = 0x19,
|
||||
kZ = 0x1A,
|
||||
|
||||
k1 = 0x20,
|
||||
k2 = 0x21,
|
||||
k3 = 0x22,
|
||||
k4 = 0x23,
|
||||
k5 = 0x24,
|
||||
k6 = 0x25,
|
||||
k7 = 0x26,
|
||||
k8 = 0x27,
|
||||
k9 = 0x28,
|
||||
k0 = 0x29,
|
||||
|
||||
kSpace = 0x30,
|
||||
kEnter = 0x31,
|
||||
kTab = 0x32,
|
||||
kBackspace = 0x33,
|
||||
kDelete = 0x34,
|
||||
|
||||
kMinus = 0x40,
|
||||
kEquals = 0x41,
|
||||
kLBrace = 0x42,
|
||||
kRBrace = 0x43,
|
||||
kBSlash = 0x44,
|
||||
kFSlash = 0x45,
|
||||
kSemicolon = 0x46,
|
||||
kQuote = 0x47,
|
||||
kComma = 0x48,
|
||||
kPeriod = 0x49,
|
||||
kBacktick = 0x4A,
|
||||
|
||||
kLShift = 0x50,
|
||||
kRShift = 0x51,
|
||||
kLCtrl = 0x52,
|
||||
kRCtrl = 0x53,
|
||||
kLAlt = 0x54,
|
||||
kRAlt = 0x55,
|
||||
kSuper = 0x56,
|
||||
kEsc = 0x57,
|
||||
kUp = 0x58,
|
||||
kDown = 0x59,
|
||||
kLeft = 0x5A,
|
||||
kRight = 0x5B,
|
||||
};
|
||||
|
||||
class KeyboardListenerBase {
|
||||
public:
|
||||
KeyboardListenerBase();
|
||||
KeyboardListenerBase(const KeyboardListenerBase&) = delete;
|
||||
KeyboardListenerBase(KeyboardListenerBase&&) = delete;
|
||||
|
||||
void Register();
|
||||
|
||||
Thread Listen();
|
||||
|
||||
void ListenLoop();
|
||||
|
||||
// Override this to recieve all raw keycodes. By default
|
||||
// this function will try to translate each keycode into
|
||||
// a printable character and call HandleCharacter.
|
||||
virtual void HandleKeycode(Keycode code, uint8_t modifiers);
|
||||
|
||||
// This function is called by the default HandleKeycode
|
||||
// implementation if you do not override it. If it recieves
|
||||
// input that corresponds to a printable character it will
|
||||
virtual void HandleCharacter(char c){};
|
||||
|
||||
private:
|
||||
PortServer server_;
|
||||
|
||||
Keycode ScancodeToKeycode(uint16_t scancode);
|
||||
|
||||
bool IsShift(uint8_t modifiers) {
|
||||
return (modifiers & 0x2) || (modifiers & 0x20);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
#include "ipc/channel.h"
|
||||
|
||||
#include <zcall.h>
|
||||
|
||||
#include "util/debug.h"
|
||||
|
||||
namespace mmth {
|
||||
namespace {
|
||||
|
||||
uint64_t strlen(const char* ptr) {
|
||||
uint64_t len = 0;
|
||||
while (*ptr != '\0') {
|
||||
len++;
|
||||
ptr++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Channel::adopt_cap(uint64_t id) {
|
||||
if (chan_cap_ != 0) {
|
||||
crash("Adopting over channel.", glcr::ALREADY_EXISTS);
|
||||
}
|
||||
chan_cap_ = id;
|
||||
}
|
||||
z_cap_t Channel::release_cap() {
|
||||
z_cap_t cap = chan_cap_;
|
||||
chan_cap_ = 0;
|
||||
return cap;
|
||||
}
|
||||
|
||||
z_cap_t Channel::cap() { return chan_cap_; }
|
||||
|
||||
z_err_t Channel::WriteStr(const char* msg) {
|
||||
if (!chan_cap_) {
|
||||
return glcr::NULL_PTR;
|
||||
}
|
||||
return ZChannelSend(chan_cap_, strlen(msg), msg, 0, nullptr);
|
||||
}
|
||||
|
||||
z_err_t Channel::ReadStr(char* buffer, uint64_t* size) {
|
||||
if (!chan_cap_) {
|
||||
return glcr::NULL_PTR;
|
||||
}
|
||||
uint64_t num_caps = 0;
|
||||
return ZChannelRecv(chan_cap_, size, reinterpret_cast<uint8_t*>(buffer),
|
||||
&num_caps, nullptr);
|
||||
}
|
||||
|
||||
z_err_t CreateChannels(Channel& c1, Channel& c2) {
|
||||
z_cap_t chan1, chan2;
|
||||
RET_ERR(ZChannelCreate(&chan1, &chan2));
|
||||
|
||||
c1.adopt_cap(chan1);
|
||||
c2.adopt_cap(chan2);
|
||||
return glcr::OK;
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
#include <stdint.h>
|
||||
#include <zcall.h>
|
||||
|
||||
namespace mmth {
|
||||
|
||||
class Channel {
|
||||
public:
|
||||
Channel() {}
|
||||
void adopt_cap(uint64_t id);
|
||||
z_cap_t release_cap();
|
||||
z_cap_t cap();
|
||||
|
||||
z_err_t WriteStr(const char* msg);
|
||||
z_err_t ReadStr(char* buffer, uint64_t* size);
|
||||
|
||||
template <typename T>
|
||||
z_err_t WriteStruct(T*);
|
||||
|
||||
template <typename T>
|
||||
z_err_t ReadStructAndCap(T*, uint64_t*);
|
||||
|
||||
// FIXME: Close channel here.
|
||||
~Channel() {}
|
||||
|
||||
private:
|
||||
z_cap_t chan_cap_ = 0;
|
||||
};
|
||||
|
||||
uint64_t CreateChannels(Channel& c1, Channel& c2);
|
||||
|
||||
template <typename T>
|
||||
z_err_t Channel::WriteStruct(T* obj) {
|
||||
return ZChannelSend(chan_cap_, sizeof(T), obj, 0, nullptr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
z_err_t Channel::ReadStructAndCap(T* obj, uint64_t* cap) {
|
||||
uint64_t num_bytes = sizeof(T);
|
||||
uint64_t num_caps = 1;
|
||||
RET_ERR(ZChannelRecv(chan_cap_, &num_bytes, obj, &num_caps, cap));
|
||||
|
||||
if (num_caps != 1 || num_bytes != sizeof(T)) {
|
||||
return glcr::FAILED_PRECONDITION;
|
||||
}
|
||||
return glcr::OK;
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
#include "ipc/endpoint_server.h"
|
||||
|
||||
namespace mmth {
|
||||
|
||||
glcr::UniquePtr<EndpointClient> EndpointClient::AdoptEndpoint(z_cap_t cap) {
|
||||
return glcr::UniquePtr<EndpointClient>(new EndpointClient(cap));
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,69 +0,0 @@
|
|||
#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>
|
||||
|
||||
namespace mmth {
|
||||
|
||||
class EndpointClient {
|
||||
public:
|
||||
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>> CallEndpointGetCap(const Req& req);
|
||||
|
||||
template <typename Req, typename Resp>
|
||||
glcr::ErrorOr<Resp> CallEndpoint(const Req& req);
|
||||
|
||||
z_cap_t GetCap() const { return cap_; }
|
||||
|
||||
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::CallEndpointGetCap(
|
||||
const Req& req) {
|
||||
uint64_t reply_port_cap;
|
||||
RET_ERR(ZEndpointSend(cap_, sizeof(Req), &req, 0, nullptr, &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};
|
||||
}
|
||||
|
||||
template <typename Req, typename Resp>
|
||||
glcr::ErrorOr<Resp> EndpointClient::CallEndpoint(const Req& req) {
|
||||
uint64_t reply_port_cap;
|
||||
RET_ERR(ZEndpointSend(cap_, sizeof(Req), &req, 0, nullptr, &reply_port_cap));
|
||||
|
||||
Resp resp;
|
||||
uint64_t num_bytes = sizeof(Resp);
|
||||
uint64_t num_caps = 0;
|
||||
RET_ERR(
|
||||
ZReplyPortRecv(reply_port_cap, &num_bytes, &resp, &num_caps, nullptr));
|
||||
|
||||
if (num_bytes != sizeof(resp)) {
|
||||
return glcr::FAILED_PRECONDITION;
|
||||
}
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
#include "ipc/endpoint_server.h"
|
||||
|
||||
#include "util/debug.h"
|
||||
|
||||
namespace mmth {
|
||||
// Declared as friend in EndpointServer.
|
||||
void EndpointServerThreadBootstrap(void* endpoint_server) {
|
||||
reinterpret_cast<EndpointServer*>(endpoint_server)->ServerThread();
|
||||
}
|
||||
|
||||
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
|
||||
uint64_t client_cap;
|
||||
RET_ERR(ZCapDuplicate(endpoint_cap_, ~(kZionPerm_Read), &client_cap));
|
||||
return EndpointClient::AdoptEndpoint(client_cap);
|
||||
}
|
||||
|
||||
Thread EndpointServer::RunServer() {
|
||||
return Thread(EndpointServerThreadBootstrap, this);
|
||||
}
|
||||
|
||||
void EndpointServer::ServerThread() {
|
||||
while (true) {
|
||||
uint64_t message_size = kBufferSize;
|
||||
uint64_t reply_port_cap = 0;
|
||||
uint64_t num_caps = 0;
|
||||
glcr::ErrorCode err = static_cast<glcr::ErrorCode>(
|
||||
ZEndpointRecv(endpoint_cap_, &message_size, recieve_buffer_, &num_caps,
|
||||
nullptr, &reply_port_cap));
|
||||
if (err != glcr::OK) {
|
||||
dbgln("Error in receive: {x}", err);
|
||||
continue;
|
||||
}
|
||||
|
||||
RequestContext request(recieve_buffer_, message_size);
|
||||
ResponseContext response(reply_port_cap);
|
||||
// FIXME: Consider pumping these errors into the response as well.
|
||||
check(HandleRequest(request, response));
|
||||
if (!response.HasWritten()) {
|
||||
dbgln("Returning without having written a response. Req type {x}",
|
||||
request.request_id());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/memory/unique_ptr.h>
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <ztypes.h>
|
||||
|
||||
#include "mammoth/ipc/endpoint_client.h"
|
||||
#include "mammoth/ipc/request_context.h"
|
||||
#include "mammoth/ipc/response_context.h"
|
||||
#include "mammoth/proc/thread.h"
|
||||
|
||||
namespace mmth {
|
||||
class EndpointServer {
|
||||
public:
|
||||
EndpointServer() = delete;
|
||||
EndpointServer(const EndpointServer&) = delete;
|
||||
EndpointServer& operator=(const EndpointServer&) = delete;
|
||||
|
||||
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> CreateClient();
|
||||
|
||||
Thread RunServer();
|
||||
|
||||
virtual glcr::ErrorCode HandleRequest(RequestContext& request,
|
||||
ResponseContext& response) = 0;
|
||||
|
||||
protected:
|
||||
EndpointServer(z_cap_t cap) : endpoint_cap_(cap) {}
|
||||
|
||||
private:
|
||||
z_cap_t endpoint_cap_;
|
||||
|
||||
static const uint64_t kBufferSize = 1024;
|
||||
uint8_t recieve_buffer_[kBufferSize];
|
||||
|
||||
friend void EndpointServerThreadBootstrap(void* endpoint_server);
|
||||
void ServerThread();
|
||||
};
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class RequestContext {
|
||||
public:
|
||||
RequestContext(void* buffer, uint64_t buffer_length)
|
||||
: buffer_(buffer), buffer_length_(buffer_length) {
|
||||
if (buffer_length_ < sizeof(uint64_t)) {
|
||||
request_id_ = -1;
|
||||
} else {
|
||||
request_id_ = *reinterpret_cast<uint64_t*>(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t request_id() { return request_id_; }
|
||||
|
||||
template <typename T>
|
||||
glcr::ErrorCode As(T** arg) {
|
||||
if (buffer_length_ < sizeof(T)) {
|
||||
return glcr::INVALID_ARGUMENT;
|
||||
}
|
||||
*arg = reinterpret_cast<T*>(buffer_);
|
||||
return glcr::OK;
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t request_id_;
|
||||
void* buffer_;
|
||||
uint64_t buffer_length_;
|
||||
};
|
||||
|
|
@ -1,159 +0,0 @@
|
|||
#include "proc/process.h"
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
#include <zcall.h>
|
||||
|
||||
#include "ipc/endpoint_server.h"
|
||||
#include "ipc/port_client.h"
|
||||
#include "ipc/port_server.h"
|
||||
#include "util/debug.h"
|
||||
#include "util/init.h"
|
||||
|
||||
#define MAM_PROC_DEBUG 0
|
||||
|
||||
namespace mmth {
|
||||
namespace {
|
||||
|
||||
typedef struct {
|
||||
char ident[16];
|
||||
uint16_t type;
|
||||
uint16_t machine;
|
||||
uint32_t version;
|
||||
uint64_t entry;
|
||||
uint64_t phoff;
|
||||
uint64_t shoff;
|
||||
uint32_t flags;
|
||||
uint16_t ehsize;
|
||||
uint16_t phentsize;
|
||||
uint16_t phnum;
|
||||
uint16_t shentsize;
|
||||
uint16_t shnum;
|
||||
uint16_t shstrndx;
|
||||
} Elf64Header;
|
||||
|
||||
typedef struct {
|
||||
uint32_t name;
|
||||
uint32_t type;
|
||||
uint64_t flags;
|
||||
uint64_t addr;
|
||||
uint64_t offset;
|
||||
uint64_t size;
|
||||
uint32_t link;
|
||||
uint32_t info;
|
||||
uint64_t addralign;
|
||||
uint64_t entsize;
|
||||
} Elf64SectionHeader;
|
||||
|
||||
typedef struct {
|
||||
uint32_t type;
|
||||
uint32_t flags;
|
||||
uint64_t offset;
|
||||
uint64_t vaddr;
|
||||
uint64_t paddr;
|
||||
uint64_t filesz;
|
||||
uint64_t memsz;
|
||||
uint64_t align;
|
||||
} Elf64ProgramHeader;
|
||||
|
||||
void memcpy(uint64_t base, uint64_t len, uint64_t dest) {
|
||||
uint8_t* srcptr = reinterpret_cast<uint8_t*>(base);
|
||||
uint8_t* destptr = reinterpret_cast<uint8_t*>(dest);
|
||||
for (uint64_t i = 0; i < len; i++) {
|
||||
destptr[i] = srcptr[i];
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
|
||||
Elf64Header* header = reinterpret_cast<Elf64Header*>(base);
|
||||
Elf64ProgramHeader* programs =
|
||||
reinterpret_cast<Elf64ProgramHeader*>(base + header->phoff);
|
||||
for (uint64_t i = 0; i < header->phnum; i++) {
|
||||
Elf64ProgramHeader& program = programs[i];
|
||||
if (program.type != 1) {
|
||||
// Only load loadable types.
|
||||
// TODO: This may break if the stack is far away?
|
||||
continue;
|
||||
}
|
||||
#if MAM_PROC_DEBUG
|
||||
dbgln(glcr::StrFormat("Program:\n\tType: {}\n\tFlags: {}\n\t", program.type,
|
||||
program.flags));
|
||||
dbgln("Create mem object");
|
||||
#endif
|
||||
uint64_t page_offset = program.vaddr & 0xFFF;
|
||||
uint64_t mem_cap;
|
||||
uint64_t size = page_offset + program.memsz;
|
||||
check(ZMemoryObjectCreate(size, &mem_cap));
|
||||
|
||||
#if MAM_PROC_DEBUG
|
||||
dbgln("Map Local");
|
||||
#endif
|
||||
uint64_t vaddr;
|
||||
check(ZAddressSpaceMap(gSelfVmasCap, 0, mem_cap, 0, &vaddr));
|
||||
uint8_t* offset = reinterpret_cast<uint8_t*>(vaddr);
|
||||
for (uint64_t j = 0; j < size; j++) {
|
||||
offset[j] = 0;
|
||||
}
|
||||
|
||||
#if MAM_PROC_DEBUG
|
||||
dbgln("Copy");
|
||||
#endif
|
||||
memcpy(base + program.offset, program.filesz, vaddr + page_offset);
|
||||
|
||||
#if MAM_PROC_DEBUG
|
||||
dbgln(glcr::StrFormat("Map Foreign: {x} {x} {x}",
|
||||
program.vaddr - page_offset, size, vaddr));
|
||||
#endif
|
||||
check(ZAddressSpaceMap(as_cap, program.vaddr - page_offset, mem_cap, 0,
|
||||
&vaddr));
|
||||
}
|
||||
return header->entry;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
glcr::ErrorOr<z_cap_t> SpawnProcessFromElfRegion(uint64_t program,
|
||||
z_cap_t yellowstone_client) {
|
||||
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
|
||||
ASSIGN_OR_RETURN(PortServer server, PortServer::Create());
|
||||
ASSIGN_OR_RETURN(PortClient pclient, server.CreateClient());
|
||||
|
||||
#if MAM_PROC_DEBUG
|
||||
dbgln("Spawn");
|
||||
#endif
|
||||
RET_ERR(ZProcessSpawn(gSelfProcCap, server.cap(), &proc_cap, &as_cap,
|
||||
&foreign_port_id));
|
||||
|
||||
uint64_t entry_point = LoadElfProgram(program, as_cap);
|
||||
|
||||
if (entry_point == 0) {
|
||||
crash("Entry Point == 0", glcr::INTERNAL);
|
||||
}
|
||||
|
||||
#if MAM_PROC_DEBUG
|
||||
dbgln("Thread Create");
|
||||
#endif
|
||||
uint64_t thread_cap;
|
||||
RET_ERR(ZThreadCreate(proc_cap, &thread_cap));
|
||||
|
||||
uint64_t dup_proc_cap;
|
||||
RET_ERR(ZCapDuplicate(proc_cap, kZionPerm_All, &dup_proc_cap));
|
||||
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, dup_proc_cap));
|
||||
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
|
||||
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, yellowstone_client));
|
||||
|
||||
#if MAM_PROC_DEBUG
|
||||
dbgln("Thread start");
|
||||
#endif
|
||||
RET_ERR(ZThreadStart(thread_cap, entry_point, foreign_port_id, 0));
|
||||
|
||||
return proc_cap;
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <stdint.h>
|
||||
#include <ztypes.h>
|
||||
|
||||
namespace mmth {
|
||||
|
||||
glcr::ErrorOr<z_cap_t> SpawnProcessFromElfRegion(uint64_t program,
|
||||
z_cap_t yellowstone_client);
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
#include "sync/mutex.h"
|
||||
|
||||
#include <zcall.h>
|
||||
|
||||
namespace mmth {
|
||||
|
||||
Mutex::Mutex(Mutex&& other) : mutex_cap_(other.mutex_cap_) {
|
||||
other.mutex_cap_ = 0;
|
||||
}
|
||||
|
||||
Mutex& Mutex::operator=(Mutex&& other) {
|
||||
// TODO: Release existing mutex if it exists.
|
||||
mutex_cap_ = other.mutex_cap_;
|
||||
other.mutex_cap_ = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
glcr::ErrorOr<Mutex> Mutex::Create() {
|
||||
z_cap_t mutex_cap;
|
||||
RET_ERR(ZMutexCreate(&mutex_cap));
|
||||
return Mutex(mutex_cap);
|
||||
}
|
||||
|
||||
glcr::ErrorCode Mutex::Lock() {
|
||||
return static_cast<glcr::ErrorCode>(ZMutexLock(mutex_cap_));
|
||||
}
|
||||
glcr::ErrorCode Mutex::Release() {
|
||||
return static_cast<glcr::ErrorCode>(ZMutexRelease(mutex_cap_));
|
||||
}
|
||||
|
||||
} // namespace mmth
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <ztypes.h>
|
||||
|
||||
namespace mmth {
|
||||
|
||||
class Mutex {
|
||||
public:
|
||||
Mutex(const Mutex&) = delete;
|
||||
Mutex(Mutex&&);
|
||||
Mutex& operator=(Mutex&&);
|
||||
|
||||
static glcr::ErrorOr<Mutex> Create();
|
||||
|
||||
glcr::ErrorCode Lock();
|
||||
glcr::ErrorCode Release();
|
||||
|
||||
private:
|
||||
z_cap_t mutex_cap_;
|
||||
|
||||
Mutex(z_cap_t mutex_cap) : mutex_cap_(mutex_cap) {}
|
||||
};
|
||||
|
||||
} // namespace mmth
|
||||
Loading…
Add table
Add a link
Reference in a new issue