Compare commits

...

4 Commits

18 changed files with 280 additions and 19 deletions

View File

@ -1,5 +1,8 @@
add_library(glacier STATIC
string/string.cpp)
string/string.cpp
string/string_view.cpp
string/str_split.cpp
)
target_include_directories(glacier
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}

View File

@ -11,10 +11,33 @@ class Vector {
Vector() : data_(nullptr), size_(0), capacity_(0) {}
Vector(const Vector&) = delete;
// TODO: Implement Move
Vector(Vector&&) = delete;
Vector(Vector&& other)
: data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
other.data_ = nullptr;
other.size_ = 0;
other.capacity_ = 0;
}
Vector& operator=(Vector&& other) {
if (data_) {
delete[] data_;
}
~Vector() { delete[] data_; }
data_ = other.data_;
size_ = other.size_;
capacity_ = other.capacity_;
other.data_ = nullptr;
other.size_ = 0;
other.capacity_ = 0;
return *this;
}
~Vector() {
if (data_) {
delete[] data_;
}
}
// FIXME: Handle downsizing.
void Resize(uint64_t capacity);

View File

@ -0,0 +1,17 @@
#include "glacier/string/str_split.h"
namespace glcr {
Vector<StringView> StrSplit(const StringView& str, char delimiter) {
Vector<StringView> strings;
uint64_t cur_pos = 0;
uint64_t next_pos = 0;
while ((next_pos = str.find(delimiter, cur_pos)) != str.npos) {
strings.PushBack(str.substr(cur_pos, next_pos - cur_pos));
cur_pos = next_pos + 1;
}
strings.PushBack(str.substr(cur_pos, str.size() - cur_pos));
return strings;
}
} // namespace glcr

View File

@ -0,0 +1,11 @@
#pragma once
#include "glacier/container/vector.h"
#include "glacier/string/string_view.h"
namespace glcr {
// TODO: Add a split that uses a StringView as a delimeter.
Vector<StringView> StrSplit(const StringView& str, char delimeter);
} // namespace glcr

View File

@ -26,6 +26,8 @@ String::String(const char* cstr, uint64_t str_len) : length_(str_len) {
cstr_[length_] = '\0';
}
String::String(StringView str) : String(str.data(), str.size()) {}
bool String::operator==(const String& other) {
if (other.length_ != length_) {
return false;
@ -43,4 +45,6 @@ char String::operator[](uint64_t offset) const {
return cstr_[offset];
}
String::operator StringView() const { return StringView(cstr_, length_); }
} // namespace glcr

View File

@ -2,6 +2,8 @@
#include <stdint.h>
#include "glacier/string/string_view.h"
namespace glcr {
class String {
@ -9,6 +11,7 @@ class String {
String();
String(const char* cstr);
String(const char* cstr, uint64_t str_len);
String(StringView str);
const char* cstr() const { return cstr_; }
uint64_t length() const { return length_; }
@ -16,6 +19,7 @@ class String {
bool operator==(const String& str);
char operator[](uint64_t offset) const;
operator StringView() const;
private:
char* cstr_;

View File

@ -0,0 +1,73 @@
#include "glacier/string/string_view.h"
namespace glcr {
namespace {
uint64_t cstrlen(const char* ptr) {
uint64_t len = 0;
while (ptr[len] != '\0') {
len++;
}
return len;
}
} // namespace
StringView::StringView() {}
StringView::StringView(const char* str) : value_(str), size_(cstrlen(str)) {}
StringView::StringView(const char* str, uint64_t count)
: value_(str), size_(count) {}
const char* StringView::data() const { return value_; }
uint64_t StringView::size() const { return size_; }
bool StringView::empty() const { return size_ == 0; }
char StringView::at(uint64_t pos) const { return value_[pos]; }
uint64_t StringView::find(char c, uint64_t pos) const {
for (uint64_t i = pos; i < size_; i++) {
if (value_[i] == c) {
return i;
}
}
return npos;
}
StringView StringView::substr(uint64_t start, uint64_t count) const {
// FIXME: Report an error here maybe.
if (start >= size_) {
return StringView{};
}
if (start + count > size_) {
count = size_ - start;
}
return StringView(value_ + start, count);
}
bool operator==(const StringView& str1, const StringView& str2) {
if (str1.empty() && str2.empty()) {
return true;
}
if (str1.size() != str2.size()) {
return false;
}
if (str1.data() == str2.data()) {
// Short circuit if they are the same exact string.
return true;
}
for (uint64_t i = 0; i < str1.size(); i++) {
if (str1.at(i) != str2.at(i)) {
return false;
}
}
return true;
}
bool operator!=(const StringView& str1, const StringView& str2) {
return !(str1 == str2);
}
} // namespace glcr

View File

@ -0,0 +1,35 @@
#pragma once
#include <stdint.h>
namespace glcr {
class StringView {
public:
static const uint64_t npos = -1;
StringView();
StringView(const StringView& other) = default;
StringView(const char* str);
StringView(const char* str, uint64_t count);
const char* data() const;
uint64_t size() const;
bool empty() const;
char at(uint64_t pos) const;
uint64_t find(char c, uint64_t pos = 0) const;
StringView substr(uint64_t start, uint64_t count) const;
private:
const char* value_ = nullptr;
uint64_t size_ = 0;
};
bool operator==(const StringView& str1, const StringView& str2);
bool operator!=(const StringView& str1, const StringView& str2);
} // namespace glcr

View File

@ -44,9 +44,11 @@ glcr::ErrorOr<Inode*> Ext2Driver::GetInode(uint32_t inode_number) {
return inode_table_->GetInode(inode_number);
}
glcr::ErrorCode Ext2Driver::ProbeDirectory(Inode* inode) {
glcr::ErrorOr<glcr::Vector<DirEntry>> Ext2Driver::ReadDirectory(
uint32_t inode_number) {
ASSIGN_OR_RETURN(Inode * inode, inode_table_->GetInode(inode_number));
if (!(inode->mode & 0x4000)) {
dbgln("Probing non-directory");
dbgln("Reading non directory.");
return glcr::INVALID_ARGUMENT;
}
@ -59,6 +61,7 @@ glcr::ErrorCode Ext2Driver::ProbeDirectory(Inode* inode) {
return glcr::FAILED_PRECONDITION;
}
glcr::Vector<DirEntry> directory;
for (uint64_t i = 0; i < real_block_cnt; i++) {
dbgln("Getting block %lx", inode->block[i]);
ASSIGN_OR_RETURN(MappedMemoryRegion block,
@ -66,6 +69,7 @@ glcr::ErrorCode Ext2Driver::ProbeDirectory(Inode* inode) {
uint64_t addr = block.vaddr();
while (addr < block.vaddr() + ext2_reader_->BlockSize()) {
DirEntry* entry = reinterpret_cast<DirEntry*>(addr);
directory.PushBack(*entry);
glcr::String name(entry->name, entry->name_len);
switch (entry->file_type) {
case kExt2FtFile:
@ -80,5 +84,25 @@ glcr::ErrorCode Ext2Driver::ProbeDirectory(Inode* inode) {
addr += entry->record_length;
}
}
return glcr::OK;
return directory;
}
glcr::ErrorOr<MappedMemoryRegion> Ext2Driver::ReadFile(uint64_t inode_number) {
ASSIGN_OR_RETURN(Inode * inode, inode_table_->GetInode(inode_number));
if (!(inode->mode & 0x8000)) {
dbgln("Reading non file.");
return glcr::INVALID_ARGUMENT;
}
// This calculation is cursed.
uint64_t real_block_cnt =
(inode->blocks - 1) / (ext2_reader_->BlockSize() / 512) + 1;
if (real_block_cnt > 1) {
dbgln("Can't handle scatter-gather yet.");
return glcr::UNIMPLEMENTED;
}
return ext2_reader_->ReadBlock(inode->block[0]);
}

View File

@ -15,7 +15,10 @@ class Ext2Driver {
glcr::ErrorCode ProbePartition();
glcr::ErrorOr<Inode*> GetInode(uint32_t inode_number);
glcr::ErrorCode ProbeDirectory(Inode* inode);
glcr::ErrorOr<glcr::Vector<DirEntry>> ReadDirectory(uint32_t inode_number);
glcr::ErrorOr<MappedMemoryRegion> ReadFile(uint64_t inode_number);
private:
glcr::SharedPtr<Ext2BlockReader> ext2_reader_;

View File

@ -20,9 +20,7 @@ uint64_t main(uint64_t init_cap) {
denali_info.device_id(), denali_info.lba_offset());
ASSIGN_OR_RETURN(Ext2Driver ext2, Ext2Driver::Init(glcr::Move(denali)));
ASSIGN_OR_RETURN(Inode * root, ext2.GetInode(2));
ASSIGN_OR_RETURN(auto server, VFSServer::Create());
ASSIGN_OR_RETURN(auto server, VFSServer::Create(ext2));
Thread server_thread = server->RunServer();
@ -31,7 +29,6 @@ uint64_t main(uint64_t init_cap) {
ASSIGN_OR_RETURN(auto client, server->CreateClient());
req.set_endpoint_capability(client.Capability());
check(yellowstone.RegisterEndpoint(req, empty));
check(ext2.ProbeDirectory(root));
RET_ERR(server_thread.Join());

View File

@ -1,14 +1,50 @@
#include "victoriafalls_server.h"
#include <glacier/string/str_split.h>
#include <mammoth/debug.h>
#include <zcall.h>
glcr::ErrorOr<glcr::UniquePtr<VFSServer>> VFSServer::Create() {
glcr::ErrorOr<glcr::UniquePtr<VFSServer>> VFSServer::Create(
Ext2Driver& driver) {
z_cap_t endpoint_cap;
RET_ERR(ZEndpointCreate(&endpoint_cap));
return glcr::UniquePtr<VFSServer>(new VFSServer(endpoint_cap));
return glcr::UniquePtr<VFSServer>(new VFSServer(endpoint_cap, driver));
}
glcr::ErrorCode VFSServer::HandleOpenFile(const OpenFileRequest&,
OpenFileResponse&) {
return glcr::UNIMPLEMENTED;
glcr::ErrorCode VFSServer::HandleOpenFile(const OpenFileRequest& request,
OpenFileResponse& response) {
auto path_tokens = glcr::StrSplit(request.path(), '/');
// Require all paths to be absolute rather than relative.
// If the path starts with '/' then the first token will be empty.
if (path_tokens.at(0) != "") {
return glcr::INVALID_ARGUMENT;
}
ASSIGN_OR_RETURN(auto files, driver_.ReadDirectory(2));
for (uint64_t i = 1; i < path_tokens.size() - 1; i++) {
for (uint64_t j = 0; j < files.size(); j++) {
if (path_tokens.at(i) ==
glcr::StringView(files.at(j).name, files.at(j).name_len)) {
ASSIGN_OR_RETURN(files, driver_.ReadDirectory(files.at(j).inode));
}
}
dbgln("Directory '%s' not found.", glcr::String(path_tokens.at(i)).cstr());
return glcr::NOT_FOUND;
}
MappedMemoryRegion region;
for (uint64_t j = 0; j < files.size(); j++) {
if (path_tokens.at(path_tokens.size() - 1) ==
glcr::StringView(files.at(j).name, files.at(j).name_len)) {
ASSIGN_OR_RETURN(region, driver_.ReadFile(files.at(j).inode));
}
}
if (!region) {
dbgln("File '%s' not found.",
glcr::String(path_tokens.at(path_tokens.size() - 1)).cstr());
}
response.set_path(request.path());
response.set_memory(region.cap());
return glcr::OK;
}

View File

@ -2,15 +2,20 @@
#include <glacier/memory/unique_ptr.h>
#include "fs/ext2/ext2_driver.h"
#include "victoriafalls/victoriafalls.yunq.server.h"
class VFSServer : public VFSServerBase {
public:
static glcr::ErrorOr<glcr::UniquePtr<VFSServer>> Create();
static glcr::ErrorOr<glcr::UniquePtr<VFSServer>> Create(Ext2Driver& driver);
glcr::ErrorCode HandleOpenFile(const OpenFileRequest&,
OpenFileResponse&) override;
private:
VFSServer(z_cap_t endpoint_cap) : VFSServerBase(endpoint_cap) {}
// FIXME: Don't store this as a reference.
Ext2Driver& driver_;
VFSServer(z_cap_t endpoint_cap, Ext2Driver& driver)
: VFSServerBase(endpoint_cap), driver_(driver) {}
};

View File

@ -12,6 +12,7 @@ target_link_libraries(yellowstone
denali_yunq
mammoth
glacier
victoriafalls_yunq
yellowstone_yunq
)

View File

@ -1,6 +1,7 @@
#include <mammoth/debug.h>
#include <mammoth/endpoint_client.h>
#include <mammoth/init.h>
#include <mammoth/memory_region.h>
#include <mammoth/process.h>
#include <zcall.h>
@ -33,6 +34,18 @@ uint64_t main(uint64_t port_cap) {
dbgln("VFS Available.");
auto vfs_client = server->GetVFSClient();
OpenFileRequest request;
request.set_path("/init.txt");
OpenFileResponse response;
check(vfs_client->OpenFile(request, response));
MappedMemoryRegion file =
MappedMemoryRegion::FromCapability(response.memory());
dbgln("addr: %lu, size: %lu", file.vaddr(), file.size());
dbgln("Test: '%s'", file.vaddr());
check(server_thread.Join());
dbgln("Yellowstone Finished Successfully.");
return 0;

View File

@ -85,6 +85,9 @@ glcr::ErrorCode YellowstoneServer::HandleRegisterEndpoint(
check(has_denali_mutex_.Release());
} else if (req.endpoint_name() == "victoriafalls") {
victoria_falls_cap_ = req.endpoint_capability();
// FIXME: Probably make a separate copy for use within yellowstone vs
// transmit to other processes.
vfs_client_ = glcr::MakeShared<VFSClient>(victoria_falls_cap_);
check(has_victoriafalls_mutex_.Release());
} else {
dbgln("[WARN] Got endpoint cap type: %s", req.endpoint_name().cstr());
@ -101,3 +104,7 @@ glcr::ErrorCode YellowstoneServer::WaitVictoriaFallsRegistered() {
RET_ERR(has_victoriafalls_mutex_.Lock());
return has_victoriafalls_mutex_.Release();
}
glcr::SharedPtr<VFSClient> YellowstoneServer::GetVFSClient() {
return vfs_client_;
}

View File

@ -1,11 +1,13 @@
#pragma once
#include <glacier/memory/shared_ptr.h>
#include <glacier/memory/unique_ptr.h>
#include <glacier/status/error_or.h>
#include <mammoth/endpoint_server.h>
#include <mammoth/mutex.h>
#include <mammoth/port_server.h>
#include <mammoth/thread.h>
#include <victoriafalls/victoriafalls.yunq.client.h>
#include "hw/pcie.h"
#include "lib/yellowstone/yellowstone.yunq.server.h"
@ -22,12 +24,15 @@ class YellowstoneServer : public YellowstoneServerBase {
glcr::ErrorCode WaitDenaliRegistered();
glcr::ErrorCode WaitVictoriaFallsRegistered();
glcr::SharedPtr<VFSClient> GetVFSClient();
private:
// TODO: Store these in a data structure.
z_cap_t denali_cap_ = 0;
uint64_t device_id_ = 0;
uint64_t lba_offset_ = 0;
z_cap_t victoria_falls_cap_ = 0;
glcr::SharedPtr<VFSClient> vfs_client_;
PciReader pci_reader_;