[Denali] Move denali server to yunq.
This commit is contained in:
parent
acfaf26391
commit
3e4fdfee84
25 changed files with 526 additions and 163 deletions
16
sys/denali/lib/denali/denali.yunq
Normal file
16
sys/denali/lib/denali/denali.yunq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
interface Denali {
|
||||
method Read(ReadRequest) -> (ReadResponse);
|
||||
}
|
||||
|
||||
message ReadRequest {
|
||||
u64 device_id;
|
||||
u64 lba;
|
||||
u64 size;
|
||||
}
|
||||
|
||||
message ReadResponse {
|
||||
u64 device_id;
|
||||
u64 lba;
|
||||
u64 size;
|
||||
capability memory;
|
||||
}
|
||||
41
sys/denali/lib/denali/denali.yunq.client.cpp
Normal file
41
sys/denali/lib/denali/denali.yunq.client.cpp
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Generated file - DO NOT MODIFY
|
||||
#include "denali.yunq.client.h"
|
||||
|
||||
#include <glacier/buffer/byte_buffer.h>
|
||||
#include <glacier/buffer/cap_buffer.h>
|
||||
#include <zcall.h>
|
||||
|
||||
|
||||
|
||||
|
||||
glcr::ErrorCode DenaliClient::Read(const ReadRequest& request, ReadResponse& response) {
|
||||
uint64_t buffer_size = kBufferSize;
|
||||
uint64_t cap_size = kCapBufferSize;
|
||||
|
||||
const uint32_t kSentinel = 0xBEEFDEAD;
|
||||
buffer_.WriteAt<uint32_t>(0, kSentinel);
|
||||
buffer_.WriteAt<uint64_t>(8, 0);
|
||||
|
||||
cap_buffer_.Reset();
|
||||
uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_);
|
||||
buffer_.WriteAt<uint32_t>(4, 16 + length);
|
||||
|
||||
z_cap_t reply_port_cap;
|
||||
RET_ERR(ZEndpointSend(endpoint_, 16 + length, buffer_.RawPtr(), cap_buffer_.UsedSlots(), cap_buffer_.RawPtr(), &reply_port_cap));
|
||||
|
||||
// FIXME: Add a way to zero out the first buffer.
|
||||
RET_ERR(ZReplyPortRecv(reply_port_cap, &buffer_size, buffer_.RawPtr(), &cap_size, cap_buffer_.RawPtr()));
|
||||
|
||||
if (buffer_.At<uint32_t>(0) != kSentinel) {
|
||||
return glcr::INVALID_RESPONSE;
|
||||
}
|
||||
|
||||
// Check Response Code.
|
||||
RET_ERR(buffer_.At<uint64_t>(8));
|
||||
|
||||
response.ParseFromBytes(buffer_, 16, cap_buffer_);
|
||||
|
||||
return glcr::OK;
|
||||
}
|
||||
|
||||
|
||||
28
sys/denali/lib/denali/denali.yunq.client.h
Normal file
28
sys/denali/lib/denali/denali.yunq.client.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Generated file - DO NOT MODIFY
|
||||
#pragma once
|
||||
|
||||
#include <glacier/buffer/byte_buffer.h>
|
||||
#include <glacier/buffer/cap_buffer.h>
|
||||
#include <glacier/status/error.h>
|
||||
#include <ztypes.h>
|
||||
|
||||
#include "denali.yunq.h"
|
||||
|
||||
class DenaliClient {
|
||||
public:
|
||||
DenaliClient(z_cap_t Denali_cap) : endpoint_(Denali_cap) {}
|
||||
DenaliClient(const DenaliClient&) = delete;
|
||||
DenaliClient(DenaliClient&& other) : endpoint_(other.endpoint_) {other.endpoint_ = 0;};
|
||||
|
||||
z_cap_t Capability() { return endpoint_; }
|
||||
|
||||
|
||||
[[nodiscard]] glcr::ErrorCode Read(const ReadRequest& request, ReadResponse& response);
|
||||
|
||||
private:
|
||||
z_cap_t endpoint_;
|
||||
uint64_t kBufferSize = 0x1000;
|
||||
glcr::ByteBuffer buffer_{kBufferSize};
|
||||
uint64_t kCapBufferSize = 0x10;
|
||||
glcr::CapBuffer cap_buffer_{kCapBufferSize};
|
||||
};
|
||||
146
sys/denali/lib/denali/denali.yunq.cpp
Normal file
146
sys/denali/lib/denali/denali.yunq.cpp
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
// Generated file -- DO NOT MODIFY.
|
||||
#include "denali.yunq.h"
|
||||
|
||||
namespace {
|
||||
|
||||
const uint64_t header_size = 24; // 4x uint32, 1x uint64
|
||||
|
||||
struct ExtPointer {
|
||||
uint32_t offset;
|
||||
uint32_t length;
|
||||
};
|
||||
|
||||
void CheckHeader(const glcr::ByteBuffer& bytes) {
|
||||
// TODO: Check ident.
|
||||
// TODO: Parse core size.
|
||||
// TODO: Parse extension size.
|
||||
// TODO: Check CRC32
|
||||
// TODO: Parse options.
|
||||
}
|
||||
|
||||
void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, uint32_t extension_size) {
|
||||
bytes.WriteAt<uint32_t>(offset + 0, 0xDEADBEEF); // TODO: Chose a more unique ident sequence.
|
||||
bytes.WriteAt<uint32_t>(offset + 4, core_size);
|
||||
bytes.WriteAt<uint32_t>(offset + 8, extension_size);
|
||||
bytes.WriteAt<uint32_t>(offset + 12, 0); // TODO: Calculate CRC32.
|
||||
bytes.WriteAt<uint64_t>(offset + 16, 0); // TODO: Add options.
|
||||
}
|
||||
|
||||
} // namespace
|
||||
void ReadRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
||||
CheckHeader(bytes);
|
||||
// Parse device_id.
|
||||
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
|
||||
// Parse lba.
|
||||
set_lba(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
|
||||
// Parse size.
|
||||
set_size(bytes.At<uint64_t>(offset + header_size + (8 * 2)));
|
||||
}
|
||||
|
||||
void ReadRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
||||
CheckHeader(bytes);
|
||||
// Parse device_id.
|
||||
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
|
||||
// Parse lba.
|
||||
set_lba(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
|
||||
// Parse size.
|
||||
set_size(bytes.At<uint64_t>(offset + header_size + (8 * 2)));
|
||||
}
|
||||
|
||||
uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
||||
uint32_t next_extension = header_size + 8 * 3;
|
||||
const uint32_t core_size = next_extension;
|
||||
// Write device_id.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 0), device_id());
|
||||
// Write lba.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 1), lba());
|
||||
// Write size.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 2), size());
|
||||
|
||||
// The next extension pointer is the length of the message.
|
||||
WriteHeader(bytes, offset, core_size, next_extension);
|
||||
|
||||
return next_extension;
|
||||
}
|
||||
|
||||
uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
|
||||
uint32_t next_extension = header_size + 8 * 3;
|
||||
const uint32_t core_size = next_extension;
|
||||
uint64_t next_cap = 0;
|
||||
// Write device_id.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 0), device_id());
|
||||
// Write lba.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 1), lba());
|
||||
// Write size.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 2), size());
|
||||
|
||||
// The next extension pointer is the length of the message.
|
||||
WriteHeader(bytes, offset, core_size, next_extension);
|
||||
|
||||
return next_extension;
|
||||
}
|
||||
void ReadResponse::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
||||
CheckHeader(bytes);
|
||||
// Parse device_id.
|
||||
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
|
||||
// Parse lba.
|
||||
set_lba(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
|
||||
// Parse size.
|
||||
set_size(bytes.At<uint64_t>(offset + header_size + (8 * 2)));
|
||||
// Parse memory.
|
||||
// FIXME: Implement in-buffer capabilities for inprocess serialization.
|
||||
set_memory(0);
|
||||
}
|
||||
|
||||
void ReadResponse::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
||||
CheckHeader(bytes);
|
||||
// Parse device_id.
|
||||
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
|
||||
// Parse lba.
|
||||
set_lba(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
|
||||
// Parse size.
|
||||
set_size(bytes.At<uint64_t>(offset + header_size + (8 * 2)));
|
||||
// Parse memory.
|
||||
uint64_t memory_ptr = bytes.At<uint64_t>(offset + header_size + (8 * 3));
|
||||
|
||||
set_memory(caps.At(memory_ptr));
|
||||
}
|
||||
|
||||
uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
||||
uint32_t next_extension = header_size + 8 * 4;
|
||||
const uint32_t core_size = next_extension;
|
||||
// Write device_id.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 0), device_id());
|
||||
// Write lba.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 1), lba());
|
||||
// Write size.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 2), size());
|
||||
// Write memory.
|
||||
// FIXME: Implement inbuffer capabilities.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 3), 0);
|
||||
|
||||
// The next extension pointer is the length of the message.
|
||||
WriteHeader(bytes, offset, core_size, next_extension);
|
||||
|
||||
return next_extension;
|
||||
}
|
||||
|
||||
uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
|
||||
uint32_t next_extension = header_size + 8 * 4;
|
||||
const uint32_t core_size = next_extension;
|
||||
uint64_t next_cap = 0;
|
||||
// Write device_id.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 0), device_id());
|
||||
// Write lba.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 1), lba());
|
||||
// Write size.
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 2), size());
|
||||
// Write memory.
|
||||
caps.WriteAt(next_cap, memory());
|
||||
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 3), next_cap++);
|
||||
|
||||
// The next extension pointer is the length of the message.
|
||||
WriteHeader(bytes, offset, core_size, next_extension);
|
||||
|
||||
return next_extension;
|
||||
}
|
||||
58
sys/denali/lib/denali/denali.yunq.h
Normal file
58
sys/denali/lib/denali/denali.yunq.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
// Generated file - DO NOT MODIFY
|
||||
#pragma once
|
||||
|
||||
#include <glacier/buffer/byte_buffer.h>
|
||||
#include <glacier/buffer/cap_buffer.h>
|
||||
#include <glacier/string/string.h>
|
||||
#include <ztypes.h>
|
||||
class ReadRequest {
|
||||
public:
|
||||
ReadRequest() {}
|
||||
// Delete copy and move until implemented.
|
||||
ReadRequest(const ReadRequest&) = delete;
|
||||
ReadRequest(ReadRequest&&) = delete;
|
||||
|
||||
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
|
||||
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
|
||||
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
|
||||
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
|
||||
uint64_t device_id() const { return device_id_; }
|
||||
void set_device_id(const uint64_t& value) { device_id_ = value; }
|
||||
uint64_t lba() const { return lba_; }
|
||||
void set_lba(const uint64_t& value) { lba_ = value; }
|
||||
uint64_t size() const { return size_; }
|
||||
void set_size(const uint64_t& value) { size_ = value; }
|
||||
|
||||
private:
|
||||
uint64_t device_id_;
|
||||
uint64_t lba_;
|
||||
uint64_t size_;
|
||||
|
||||
};
|
||||
class ReadResponse {
|
||||
public:
|
||||
ReadResponse() {}
|
||||
// Delete copy and move until implemented.
|
||||
ReadResponse(const ReadResponse&) = delete;
|
||||
ReadResponse(ReadResponse&&) = delete;
|
||||
|
||||
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
|
||||
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
|
||||
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
|
||||
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
|
||||
uint64_t device_id() const { return device_id_; }
|
||||
void set_device_id(const uint64_t& value) { device_id_ = value; }
|
||||
uint64_t lba() const { return lba_; }
|
||||
void set_lba(const uint64_t& value) { lba_ = value; }
|
||||
uint64_t size() const { return size_; }
|
||||
void set_size(const uint64_t& value) { size_ = value; }
|
||||
z_cap_t memory() const { return memory_; }
|
||||
void set_memory(const z_cap_t& value) { memory_ = value; }
|
||||
|
||||
private:
|
||||
uint64_t device_id_;
|
||||
uint64_t lba_;
|
||||
uint64_t size_;
|
||||
z_cap_t memory_;
|
||||
|
||||
};
|
||||
104
sys/denali/lib/denali/denali.yunq.server.cpp
Normal file
104
sys/denali/lib/denali/denali.yunq.server.cpp
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
// Generated file -- DO NOT MODIFY.
|
||||
#include "denali.yunq.server.h"
|
||||
|
||||
#include <mammoth/debug.h>
|
||||
#include <zcall.h>
|
||||
|
||||
namespace {
|
||||
|
||||
const uint32_t kSentinel = 0xBEEFDEAD;
|
||||
const uint32_t kHeaderSize = 0x10;
|
||||
|
||||
void WriteError(glcr::ByteBuffer& buffer, glcr::ErrorCode err) {
|
||||
buffer.WriteAt<uint32_t>(0, kSentinel);
|
||||
buffer.WriteAt<uint32_t>(4, kHeaderSize);
|
||||
buffer.WriteAt<uint64_t>(8, err);
|
||||
}
|
||||
|
||||
void WriteHeader(glcr::ByteBuffer& buffer, uint64_t message_length) {
|
||||
buffer.WriteAt<uint32_t>(0, kSentinel);
|
||||
buffer.WriteAt<uint32_t>(4, kHeaderSize + message_length);
|
||||
buffer.WriteAt<uint64_t>(8, glcr::OK);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
void DenaliServerBaseThreadBootstrap(void* server_base) {
|
||||
((DenaliServerBase*)server_base)->ServerThread();
|
||||
}
|
||||
|
||||
glcr::ErrorOr<DenaliClient> DenaliServerBase::CreateClient() {
|
||||
uint64_t client_cap;
|
||||
// FIXME: Restrict permissions to send-only here.
|
||||
RET_ERR(ZCapDuplicate(endpoint_, &client_cap));
|
||||
return DenaliClient(client_cap);
|
||||
}
|
||||
|
||||
Thread DenaliServerBase::RunServer() {
|
||||
return Thread(DenaliServerBaseThreadBootstrap, this);
|
||||
}
|
||||
|
||||
void DenaliServerBase::ServerThread() {
|
||||
glcr::ByteBuffer recv_buffer(0x1000);
|
||||
glcr::CapBuffer recv_cap(0x10);
|
||||
glcr::ByteBuffer resp_buffer(0x1000);
|
||||
glcr::CapBuffer resp_cap(0x10);
|
||||
z_cap_t reply_port_cap;
|
||||
|
||||
while (true) {
|
||||
uint64_t recv_cap_size = 0x10;
|
||||
uint64_t recv_buf_size = 0x1000;
|
||||
glcr::ErrorCode recv_err = ZEndpointRecv(endpoint_, &recv_buf_size, recv_buffer.RawPtr(), &recv_cap_size, recv_cap.RawPtr(), &reply_port_cap);
|
||||
if (recv_err != glcr::OK) {
|
||||
dbgln("Error in receive: %x", recv_err);
|
||||
continue;
|
||||
}
|
||||
|
||||
uint64_t resp_length = 0;
|
||||
|
||||
glcr::ErrorCode reply_err = glcr::OK;
|
||||
glcr::ErrorCode err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap);
|
||||
if (err != glcr::OK) {
|
||||
WriteError(resp_buffer, err);
|
||||
reply_err = ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr);
|
||||
} else {
|
||||
WriteHeader(resp_buffer, resp_length);
|
||||
reply_err = ZReplyPortSend(reply_port_cap, kHeaderSize + resp_length, resp_buffer.RawPtr(), resp_cap.UsedSlots(), resp_cap.RawPtr());
|
||||
}
|
||||
if (reply_err != glcr::OK) {
|
||||
dbgln("Error in reply: %x", recv_err);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request,
|
||||
const glcr::CapBuffer& req_caps,
|
||||
glcr::ByteBuffer& response, uint64_t& resp_length,
|
||||
glcr::CapBuffer& resp_caps) {
|
||||
if (request.At<uint32_t>(0) != kSentinel) {
|
||||
return glcr::INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
uint64_t method_select = request.At<uint64_t>(8);
|
||||
|
||||
switch(method_select) {
|
||||
case 0: {
|
||||
ReadRequest yunq_request;
|
||||
ReadResponse yunq_response;
|
||||
|
||||
yunq_request.ParseFromBytes(request, kHeaderSize, req_caps);
|
||||
|
||||
RET_ERR(HandleRead(yunq_request, yunq_response));
|
||||
|
||||
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return glcr::UNIMPLEMENTED;
|
||||
}
|
||||
}
|
||||
return glcr::OK;
|
||||
}
|
||||
37
sys/denali/lib/denali/denali.yunq.server.h
Normal file
37
sys/denali/lib/denali/denali.yunq.server.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// Generated File -- DO NOT MODIFY.
|
||||
#pragma once
|
||||
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <mammoth/thread.h>
|
||||
#include <ztypes.h>
|
||||
|
||||
#include "denali.yunq.h"
|
||||
#include "denali.yunq.client.h"
|
||||
|
||||
|
||||
|
||||
class DenaliServerBase {
|
||||
public:
|
||||
DenaliServerBase(z_cap_t Denali_cap) : endpoint_(Denali_cap) {}
|
||||
DenaliServerBase(const DenaliServerBase&) = delete;
|
||||
DenaliServerBase(DenaliServerBase&&) = delete;
|
||||
|
||||
glcr::ErrorOr<DenaliClient> CreateClient();
|
||||
|
||||
[[nodiscard]] Thread RunServer();
|
||||
|
||||
|
||||
[[nodiscard]] virtual glcr::ErrorCode HandleRead(const ReadRequest&, ReadResponse&) = 0;
|
||||
|
||||
|
||||
private:
|
||||
z_cap_t endpoint_;
|
||||
|
||||
friend void DenaliServerBaseThreadBootstrap(void*);
|
||||
void ServerThread();
|
||||
|
||||
[[nodiscard]] glcr::ErrorCode HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps,
|
||||
glcr::ByteBuffer& response, uint64_t& resp_length,
|
||||
glcr::CapBuffer& resp_caps);
|
||||
};
|
||||
|
||||
32
sys/denali/lib/denali/scoped_denali_client.h
Normal file
32
sys/denali/lib/denali/scoped_denali_client.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <mammoth/memory_region.h>
|
||||
|
||||
#include "denali.yunq.client.h"
|
||||
|
||||
class ScopedDenaliClient : protected DenaliClient {
|
||||
public:
|
||||
ScopedDenaliClient(z_cap_t endpoint_cap, uint64_t device_id,
|
||||
uint64_t lba_offset)
|
||||
: DenaliClient(endpoint_cap),
|
||||
device_id_(device_id),
|
||||
lba_offset_(lba_offset) {}
|
||||
|
||||
glcr::ErrorOr<MappedMemoryRegion> ReadSectors(uint64_t lba,
|
||||
uint64_t num_sectors) {
|
||||
ReadRequest req;
|
||||
req.set_device_id(device_id_);
|
||||
req.set_lba(lba_offset_ + lba);
|
||||
req.set_size(num_sectors);
|
||||
|
||||
ReadResponse resp;
|
||||
RET_ERR(DenaliClient::Read(req, resp));
|
||||
|
||||
return MappedMemoryRegion::FromCapability(resp.memory());
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t device_id_;
|
||||
uint64_t lba_offset_;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue