Endpoint syscalls implemented

This commit is contained in:
Drew Galbraith 2023-06-21 23:14:42 -07:00
parent 69501bfe01
commit c064af5fa7
27 changed files with 391 additions and 42 deletions

View file

@ -20,9 +20,11 @@ add_executable(zion
memory/user_stack_manager.cpp
object/address_space.cpp
object/channel.cpp
object/endpoint.cpp
object/memory_object.cpp
object/port.cpp
object/process.cpp
object/reply_port.cpp
object/thread.cpp
scheduler/context_switch.s
scheduler/jump_user_space.s
@ -32,9 +34,11 @@ add_executable(zion
syscall/capability.cpp
syscall/channel.cpp
syscall/debug.cpp
syscall/endpoint.cpp
syscall/memory_object.cpp
syscall/port.cpp
syscall/process.cpp
syscall/reply_port.cpp
syscall/syscall.cpp
syscall/syscall_enter.s
syscall/thread.cpp

View file

@ -130,8 +130,8 @@ SYS4(EndpointRecv, z_cap_t, endpoint_cap, uint64_t*, num_bytes, void*, data,
z_cap_t*, reply_port_cap);
SYS5(ReplyPortSend, z_cap_t, reply_port_cap, uint64_t, num_bytes, const void*,
data, uint64_t, num_caps, z_cap_t*, caps);
SYS5(ReplyPortRecv, z_cap_t, reply_port_cap, uint64_t*, num_bytes, const void*,
data, uint64_t*, num_caps, z_cap_t*, caps);
SYS5(ReplyPortRecv, z_cap_t, reply_port_cap, uint64_t*, num_bytes, void*, data,
uint64_t*, num_caps, z_cap_t*, caps);
SYS2(CapDuplicate, z_cap_t, cap_in, z_cap_t*, cap_out);

View file

@ -5,6 +5,6 @@
extern uint64_t gSelfProcCap;
extern uint64_t gSelfVmasCap;
extern uint64_t gInitChannelCap;
extern uint64_t gInitEndpointCap;
extern uint64_t gBootDenaliVmmoCap;

View file

@ -85,6 +85,6 @@ typedef uint64_t z_cap_t;
#define Z_INIT_SELF_PROC 0x4000'0000
#define Z_INIT_SELF_VMAS 0x4000'0001
#define Z_INIT_CHANNEL 0x4100'0000
#define Z_INIT_ENDPOINT 0x4100'0000
#define Z_BOOT_DENALI_VMMO 0x4200'0000

View file

@ -70,3 +70,59 @@ void UnboundedMessageQueue::WriteKernel(uint64_t init,
pending_messages_.PushBack(msg);
}
glcr::ErrorCode SingleMessageQueue::PushBack(uint64_t num_bytes,
const void* bytes,
uint64_t num_caps,
const z_cap_t* caps) {
if (has_written_) {
return glcr::FAILED_PRECONDITION;
}
num_bytes_ = num_bytes;
bytes_ = new uint8_t[num_bytes];
for (uint64_t i = 0; i < num_bytes; i++) {
bytes_[i] = reinterpret_cast<const uint8_t*>(bytes)[i];
}
for (uint64_t i = 0; i < num_caps; i++) {
// FIXME: This would feel safer closer to the relevant syscall.
auto cap = gScheduler->CurrentProcess().ReleaseCapability(caps[i]);
if (!cap) {
return glcr::CAP_NOT_FOUND;
}
caps_.PushBack(cap);
}
has_written_ = true;
return glcr::OK;
}
glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps,
z_cap_t* caps) {
if (!has_written_ || has_read_) {
return glcr::FAILED_PRECONDITION;
}
if (num_bytes_ > *num_bytes) {
return glcr::BUFFER_SIZE;
}
if (caps_.size() > *num_caps) {
return glcr::BUFFER_SIZE;
}
*num_bytes = num_bytes_;
for (uint64_t i = 0; i < num_bytes_; i++) {
reinterpret_cast<uint8_t*>(bytes)[i] = bytes_[i];
}
*num_caps = caps_.size();
auto& proc = gScheduler->CurrentProcess();
for (uint64_t i = 0; i < *num_caps; i++) {
caps[i] = proc.AddExistingCapability(caps_.PopFront());
}
has_read_ = true;
return glcr::OK;
}

View file

@ -2,6 +2,7 @@
#include <glacier/memory/ref_ptr.h>
#include <glacier/memory/shared_ptr.h>
#include <glacier/status/error.h>
#include "capability/capability.h"
#include "include/ztypes.h"
@ -11,10 +12,10 @@ class MessageQueue {
public:
virtual ~MessageQueue() {}
virtual z_err_t PushBack(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps) = 0;
virtual z_err_t PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
z_cap_t* caps) = 0;
virtual glcr::ErrorCode PushBack(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps) = 0;
virtual glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps, z_cap_t* caps) = 0;
};
class UnboundedMessageQueue : public MessageQueue {
@ -24,10 +25,10 @@ class UnboundedMessageQueue : public MessageQueue {
UnboundedMessageQueue& operator=(const UnboundedMessageQueue&) = delete;
virtual ~UnboundedMessageQueue() override {}
z_err_t PushBack(uint64_t num_bytes, const void* bytes, uint64_t num_caps,
const z_cap_t* caps) override;
z_err_t PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
z_cap_t* caps) override;
glcr::ErrorCode PushBack(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps) override;
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
z_cap_t* caps) override;
void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap);
@ -44,3 +45,25 @@ class UnboundedMessageQueue : public MessageQueue {
LinkedList<glcr::SharedPtr<Message>> pending_messages_;
};
class SingleMessageQueue : public MessageQueue {
public:
SingleMessageQueue() {}
SingleMessageQueue(const SingleMessageQueue&) = delete;
SingleMessageQueue(SingleMessageQueue&&) = delete;
virtual ~SingleMessageQueue() override {}
glcr::ErrorCode PushBack(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps) override;
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
z_cap_t* caps) override;
bool empty() { return has_written_ == false; };
private:
bool has_written_ = false;
bool has_read_ = false;
uint64_t num_bytes_;
uint8_t* bytes_;
LinkedList<glcr::RefPtr<Capability>> caps_;
};

View file

@ -5,7 +5,7 @@
void Mutex::Lock() {
while (__atomic_fetch_or(&lock_, 0x1, __ATOMIC_SEQ_CST) == 0x1) {
dbgln("Lock sleep: %s", name_);
// dbgln("Lock sleep: %s", name_);
gScheduler->Preempt();
}
}

43
zion/object/endpoint.cpp Normal file
View file

@ -0,0 +1,43 @@
#include "object/endpoint.h"
#include "scheduler/scheduler.h"
glcr::RefPtr<Endpoint> Endpoint::Create() {
return glcr::AdoptPtr(new Endpoint);
}
glcr::ErrorCode Endpoint::Write(uint64_t num_bytes, const void* data,
z_cap_t reply_port_cap) {
MutexHolder h(mutex_);
RET_ERR(message_queue_.PushBack(num_bytes, data, 1, &reply_port_cap));
if (blocked_threads_.size() > 0) {
auto thread = blocked_threads_.PopFront();
thread->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(thread);
}
return glcr::OK;
}
glcr::ErrorCode Endpoint::Read(uint64_t* num_bytes, void* data,
z_cap_t* reply_port_cap) {
mutex_.Lock();
while (message_queue_.empty()) {
auto thread = gScheduler->CurrentThread();
thread->SetState(Thread::BLOCKED);
mutex_.Unlock();
gScheduler->Yield();
mutex_.Lock();
}
mutex_.Unlock();
MutexHolder h(mutex_);
uint64_t num_caps = 1;
RET_ERR(message_queue_.PopFront(num_bytes, data, &num_caps, reply_port_cap));
if (num_caps != 1) {
return glcr::INTERNAL;
}
return glcr::OK;
}

37
zion/object/endpoint.h Normal file
View file

@ -0,0 +1,37 @@
#pragma once
#include <glacier/container/intrusive_list.h>
#include <glacier/memory/ref_ptr.h>
#include <glacier/status/error.h>
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/kernel_object.h"
class Endpoint;
class ReplyPort;
template <>
struct KernelObjectTag<Endpoint> {
static const uint64_t type = KernelObject::ENDPOINT;
};
class Endpoint : public KernelObject {
public:
uint64_t TypeTag() override { return KernelObject::ENDPOINT; }
static glcr::RefPtr<Endpoint> Create();
glcr::ErrorCode Write(uint64_t num_bytes, const void* data,
z_cap_t reply_port_cap);
glcr::ErrorCode Read(uint64_t* num_bytes, void* data,
z_cap_t* reply_port_cap);
private:
Mutex mutex_{"endpoint"};
UnboundedMessageQueue message_queue_;
glcr::IntrusiveList<Thread> blocked_threads_;
Endpoint() {}
};

View file

@ -12,6 +12,8 @@ class KernelObject : public glcr::RefCounted<KernelObject> {
MEMORY_OBJECT = 0x4,
CHANNEL = 0x5,
PORT = 0x6,
ENDPOINT = 0x7,
REPLY_PORT = 0x8,
};
virtual uint64_t TypeTag() = 0;

View file

@ -0,0 +1,41 @@
#include "object/reply_port.h"
#include "scheduler/scheduler.h"
glcr::RefPtr<ReplyPort> ReplyPort::Create() {
return glcr::AdoptPtr(new ReplyPort);
}
uint64_t ReplyPort::Write(uint64_t num_bytes, const void* data,
uint64_t num_caps, uint64_t* caps) {
MutexHolder h(mutex_);
RET_ERR(message_holder_.PushBack(num_bytes, data, num_caps, caps));
if (blocked_thread_) {
// FIXME: We need to handle the case where the blocked thread has died I
// think.
blocked_thread_->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(blocked_thread_);
blocked_thread_ = nullptr;
}
return glcr::OK;
}
uint64_t ReplyPort::Read(uint64_t* num_bytes, void* data, uint64_t* num_caps,
uint64_t* caps) {
mutex_.Lock();
if (message_holder_.empty()) {
// Multiple threads can't block on a reply port.
if (blocked_thread_) {
mutex_.Unlock();
return glcr::FAILED_PRECONDITION;
}
blocked_thread_ = gScheduler->CurrentThread();
blocked_thread_->SetState(Thread::BLOCKED);
mutex_.Unlock();
gScheduler->Yield();
mutex_.Lock();
}
return message_holder_.PopFront(num_bytes, data, num_caps, caps);
}

33
zion/object/reply_port.h Normal file
View file

@ -0,0 +1,33 @@
#pragma once
#include <glacier/memory/ref_ptr.h>
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/kernel_object.h"
class ReplyPort;
template <>
struct KernelObjectTag<ReplyPort> {
static const uint64_t type = KernelObject::REPLY_PORT;
};
class ReplyPort : public KernelObject {
public:
uint64_t TypeTag() override { return KernelObject::REPLY_PORT; }
static glcr::RefPtr<ReplyPort> Create();
uint64_t Write(uint64_t num_bytes, const void* data, uint64_t num_caps,
uint64_t* caps);
uint64_t Read(uint64_t* num_bytes, void* data, uint64_t* num_caps,
uint64_t* caps);
private:
Mutex mutex_{"reply_port"};
SingleMessageQueue message_holder_;
glcr::RefPtr<Thread> blocked_thread_;
ReplyPort() {}
};

35
zion/syscall/endpoint.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "syscall/endpoint.h"
#include "object/endpoint.h"
#include "object/reply_port.h"
#include "scheduler/scheduler.h"
glcr::ErrorCode EndpointCreate(ZEndpointCreateReq* req) {
auto& proc = gScheduler->CurrentProcess();
*req->endpoint_cap =
proc.AddNewCapability(Endpoint::Create(), ZC_READ | ZC_WRITE);
return glcr::OK;
}
glcr::ErrorCode EndpointSend(ZEndpointSendReq* req) {
auto& proc = gScheduler->CurrentProcess();
auto endpoint_cap = proc.GetCapability(req->endpoint_cap);
ValidateCapability<Endpoint>(endpoint_cap, ZC_WRITE);
auto endpoint = endpoint_cap->obj<Endpoint>();
auto reply_port = ReplyPort::Create();
*req->reply_port_cap = proc.AddNewCapability(reply_port, ZC_READ);
uint64_t reply_port_cap_to_send = proc.AddNewCapability(reply_port, ZC_WRITE);
return endpoint->Write(req->num_bytes, req->data, reply_port_cap_to_send);
}
glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req) {
auto& proc = gScheduler->CurrentProcess();
auto endpoint_cap = proc.GetCapability(req->endpoint_cap);
ValidateCapability<Endpoint>(endpoint_cap, ZC_READ);
auto endpoint = endpoint_cap->obj<Endpoint>();
return endpoint->Read(req->num_bytes, req->data, req->reply_port_cap);
}

11
zion/syscall/endpoint.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include <glacier/status/error.h>
#include "include/zcall.h"
glcr::ErrorCode EndpointCreate(ZEndpointCreateReq* req);
glcr::ErrorCode EndpointSend(ZEndpointSendReq* req);
glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req);

View file

@ -0,0 +1,22 @@
#include "syscall/reply_port.h"
#include "object/reply_port.h"
#include "scheduler/scheduler.h"
glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* req) {
auto& proc = gScheduler->CurrentProcess();
auto reply_port_cap = proc.GetCapability(req->reply_port_cap);
ValidateCapability<ReplyPort>(reply_port_cap, ZC_WRITE);
auto reply_port = reply_port_cap->obj<ReplyPort>();
return reply_port->Write(req->num_bytes, req->data, req->num_caps, req->caps);
}
glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req) {
auto& proc = gScheduler->CurrentProcess();
auto reply_port_cap = proc.GetCapability(req->reply_port_cap);
ValidateCapability<ReplyPort>(reply_port_cap, ZC_READ);
auto reply_port = reply_port_cap->obj<ReplyPort>();
return reply_port->Read(req->num_bytes, req->data, req->num_caps, req->caps);
}

View file

@ -0,0 +1,9 @@
#pragma once
#include <glacier/status/error.h>
#include "include/zcall.h"
glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* req);
glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req);

View file

@ -8,9 +8,11 @@
#include "syscall/capability.h"
#include "syscall/channel.h"
#include "syscall/debug.h"
#include "syscall/endpoint.h"
#include "syscall/memory_object.h"
#include "syscall/port.h"
#include "syscall/process.h"
#include "syscall/reply_port.h"
#include "syscall/thread.h"
#define EFER 0xC0000080
@ -73,6 +75,13 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
CASE(PortRecv);
CASE(PortPoll);
CASE(IrqRegister);
// syscall/endpoint.h
CASE(EndpointCreate);
CASE(EndpointSend);
CASE(EndpointRecv);
// syscall/reply_port.h
CASE(ReplyPortSend);
CASE(ReplyPortRecv);
// syscall/capability.h
CASE(CapDuplicate);
// syscall/debug.h