Endpoint syscalls implemented
This commit is contained in:
parent
69501bfe01
commit
c064af5fa7
|
@ -41,6 +41,6 @@ class ErrorOr {
|
||||||
if (!AOR_VAR(__LINE__).ok()) { \
|
if (!AOR_VAR(__LINE__).ok()) { \
|
||||||
return AOR_VAR(__LINE__).error(); \
|
return AOR_VAR(__LINE__).error(); \
|
||||||
} \
|
} \
|
||||||
lhs = rhs.value();
|
lhs = AOR_VAR(__LINE__).value();
|
||||||
|
|
||||||
} // namespace glcr
|
} // namespace glcr
|
||||||
|
|
|
@ -15,6 +15,9 @@ class EndpointServer {
|
||||||
// FIXME: Release Cap here.
|
// FIXME: Release Cap here.
|
||||||
z_cap_t GetCap() { return endpoint_cap_; }
|
z_cap_t GetCap() { return endpoint_cap_; }
|
||||||
|
|
||||||
|
glcr::ErrorCode Recieve(uint64_t* num_bytes, void* data,
|
||||||
|
z_cap_t* reply_port_cap);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
z_cap_t endpoint_cap_;
|
z_cap_t endpoint_cap_;
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,18 @@ glcr::ErrorOr<EndpointServer> EndpointServer::Create() {
|
||||||
return EndpointServer(cap);
|
return EndpointServer(cap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EndpointServer EndpointServer::Adopt(z_cap_t endpoint_cap) {
|
||||||
|
return EndpointServer(endpoint_cap);
|
||||||
|
}
|
||||||
|
|
||||||
glcr::ErrorOr<EndpointClient> EndpointServer::CreateClient() {
|
glcr::ErrorOr<EndpointClient> EndpointServer::CreateClient() {
|
||||||
uint64_t client_cap;
|
uint64_t client_cap;
|
||||||
// FIXME: Restrict permissions to send-only here.
|
// FIXME: Restrict permissions to send-only here.
|
||||||
RET_ERR(ZCapDuplicate(endpoint_cap_, &client_cap));
|
RET_ERR(ZCapDuplicate(endpoint_cap_, &client_cap));
|
||||||
return EndpointClient::AdoptEndpoint(client_cap);
|
return EndpointClient::AdoptEndpoint(client_cap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glcr::ErrorCode EndpointServer::Recieve(uint64_t* num_bytes, void* data,
|
||||||
|
z_cap_t* reply_port_cap) {
|
||||||
|
return ZEndpointRecv(endpoint_cap_, num_bytes, data, reply_port_cap);
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
uint64_t gSelfProcCap = 0;
|
uint64_t gSelfProcCap = 0;
|
||||||
uint64_t gSelfVmasCap = 0;
|
uint64_t gSelfVmasCap = 0;
|
||||||
|
|
||||||
uint64_t gInitChannelCap = 0;
|
uint64_t gInitEndpointCap = 0;
|
||||||
|
|
||||||
uint64_t gBootDenaliVmmoCap = 0;
|
uint64_t gBootDenaliVmmoCap = 0;
|
||||||
|
|
||||||
|
@ -28,8 +28,8 @@ z_err_t ParseInitPort(uint64_t init_port_cap) {
|
||||||
dbgln("received vmas");
|
dbgln("received vmas");
|
||||||
gSelfVmasCap = init_cap;
|
gSelfVmasCap = init_cap;
|
||||||
break;
|
break;
|
||||||
case Z_INIT_CHANNEL:
|
case Z_INIT_ENDPOINT:
|
||||||
gInitChannelCap = init_cap;
|
gInitEndpointCap = init_cap;
|
||||||
break;
|
break;
|
||||||
case Z_BOOT_DENALI_VMMO:
|
case Z_BOOT_DENALI_VMMO:
|
||||||
dbgln("received denali");
|
dbgln("received denali");
|
||||||
|
|
|
@ -128,7 +128,7 @@ glcr::ErrorOr<EndpointClient> SpawnProcessFromElfRegion(uint64_t program) {
|
||||||
Port p(port_cap);
|
Port p(port_cap);
|
||||||
check(p.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_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_SELF_VMAS, as_cap));
|
||||||
check(p.WriteMessage<uint64_t>(Z_INIT_CHANNEL, server.GetCap()));
|
check(p.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, server.GetCap()));
|
||||||
|
|
||||||
#if MAM_PROC_DEBUG
|
#if MAM_PROC_DEBUG
|
||||||
dbgln("Thread start");
|
dbgln("Thread start");
|
||||||
|
|
|
@ -7,8 +7,11 @@
|
||||||
Command::~Command() {}
|
Command::~Command() {}
|
||||||
|
|
||||||
DmaReadCommand::DmaReadCommand(uint64_t lba, uint64_t sector_cnt,
|
DmaReadCommand::DmaReadCommand(uint64_t lba, uint64_t sector_cnt,
|
||||||
DmaCallback callback)
|
DmaCallback callback, z_cap_t reply_port)
|
||||||
: lba_(lba), sector_cnt_(sector_cnt), callback_(callback) {
|
: reply_port_(reply_port),
|
||||||
|
lba_(lba),
|
||||||
|
sector_cnt_(sector_cnt),
|
||||||
|
callback_(callback) {
|
||||||
region_ = MappedMemoryRegion::ContiguousPhysical(sector_cnt * 512);
|
region_ = MappedMemoryRegion::ContiguousPhysical(sector_cnt * 512);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,4 +49,6 @@ void DmaReadCommand::PopulatePrdt(PhysicalRegionDescriptor* prdt) {
|
||||||
prdt[0].region_address = region_.paddr();
|
prdt[0].region_address = region_.paddr();
|
||||||
prdt[0].byte_count = region_.size();
|
prdt[0].byte_count = region_.size();
|
||||||
}
|
}
|
||||||
void DmaReadCommand::Callback() { callback_(lba_, sector_cnt_, region_.cap()); }
|
void DmaReadCommand::Callback() {
|
||||||
|
callback_(reply_port_, lba_, sector_cnt_, region_.cap());
|
||||||
|
}
|
||||||
|
|
|
@ -15,8 +15,9 @@ class Command {
|
||||||
|
|
||||||
class DmaReadCommand : public Command {
|
class DmaReadCommand : public Command {
|
||||||
public:
|
public:
|
||||||
typedef void (*DmaCallback)(uint64_t, uint64_t, uint64_t);
|
typedef void (*DmaCallback)(z_cap_t, uint64_t, uint64_t, z_cap_t);
|
||||||
DmaReadCommand(uint64_t lba, uint64_t sector_cnt, DmaCallback callback);
|
DmaReadCommand(uint64_t lba, uint64_t sector_cnt, DmaCallback callback,
|
||||||
|
z_cap_t reply_port);
|
||||||
|
|
||||||
virtual ~DmaReadCommand() override;
|
virtual ~DmaReadCommand() override;
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ class DmaReadCommand : public Command {
|
||||||
void Callback() override;
|
void Callback() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
z_cap_t reply_port_;
|
||||||
uint64_t lba_;
|
uint64_t lba_;
|
||||||
uint64_t sector_cnt_;
|
uint64_t sector_cnt_;
|
||||||
DmaCallback callback_;
|
DmaCallback callback_;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <mammoth/channel.h>
|
|
||||||
#include <mammoth/debug.h>
|
#include <mammoth/debug.h>
|
||||||
|
#include <mammoth/endpoint_server.h>
|
||||||
#include <mammoth/init.h>
|
#include <mammoth/init.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -11,7 +12,8 @@ uint64_t main(uint64_t init_port_cap) {
|
||||||
AhciDriver driver;
|
AhciDriver driver;
|
||||||
RET_ERR(driver.Init());
|
RET_ERR(driver.Init());
|
||||||
|
|
||||||
DenaliServer server(gInitChannelCap, driver);
|
EndpointServer endpoint = EndpointServer::Adopt(gInitEndpointCap);
|
||||||
|
DenaliServer server(endpoint, driver);
|
||||||
RET_ERR(server.RunServer());
|
RET_ERR(server.RunServer());
|
||||||
// FIXME: Add thread join.
|
// FIXME: Add thread join.
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -6,22 +6,22 @@
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
DenaliServer* gServer = nullptr;
|
DenaliServer* gServer = nullptr;
|
||||||
void HandleResponse(uint64_t lba, uint64_t size, uint64_t cap) {
|
void HandleResponse(z_cap_t reply_port, uint64_t lba, uint64_t size,
|
||||||
gServer->HandleResponse(lba, size, cap);
|
z_cap_t mem) {
|
||||||
|
gServer->HandleResponse(reply_port, lba, size, mem);
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
DenaliServer::DenaliServer(uint64_t channel_cap, AhciDriver& driver)
|
DenaliServer::DenaliServer(EndpointServer server, AhciDriver& driver)
|
||||||
: channel_cap_(channel_cap), driver_(driver) {
|
: server_(server), driver_(driver) {
|
||||||
gServer = this;
|
gServer = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode DenaliServer::RunServer() {
|
glcr::ErrorCode DenaliServer::RunServer() {
|
||||||
while (true) {
|
while (true) {
|
||||||
uint64_t buff_size = kBuffSize;
|
uint64_t buff_size = kBuffSize;
|
||||||
uint64_t cap_size = 0;
|
z_cap_t reply_port;
|
||||||
RET_ERR(ZChannelRecv(channel_cap_, &buff_size, read_buffer_, &cap_size,
|
RET_ERR(server_.Recieve(&buff_size, read_buffer_, &reply_port));
|
||||||
nullptr));
|
|
||||||
if (buff_size < sizeof(uint64_t)) {
|
if (buff_size < sizeof(uint64_t)) {
|
||||||
dbgln("Skipping invalid message");
|
dbgln("Skipping invalid message");
|
||||||
continue;
|
continue;
|
||||||
|
@ -34,7 +34,7 @@ glcr::ErrorCode DenaliServer::RunServer() {
|
||||||
case DENALI_READ: {
|
case DENALI_READ: {
|
||||||
DenaliRead* read_req = reinterpret_cast<DenaliRead*>(read_buffer_);
|
DenaliRead* read_req = reinterpret_cast<DenaliRead*>(read_buffer_);
|
||||||
uint64_t memcap = 0;
|
uint64_t memcap = 0;
|
||||||
RET_ERR(HandleRead(*read_req));
|
RET_ERR(HandleRead(*read_req, reply_port));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -44,21 +44,22 @@ glcr::ErrorCode DenaliServer::RunServer() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode DenaliServer::HandleRead(const DenaliRead& read) {
|
glcr::ErrorCode DenaliServer::HandleRead(const DenaliRead& read,
|
||||||
|
z_cap_t reply_port) {
|
||||||
ASSIGN_OR_RETURN(AhciDevice * device, driver_.GetDevice(read.device_id));
|
ASSIGN_OR_RETURN(AhciDevice * device, driver_.GetDevice(read.device_id));
|
||||||
|
|
||||||
device->IssueCommand(
|
device->IssueCommand(
|
||||||
new DmaReadCommand(read.lba, read.size, ::HandleResponse));
|
new DmaReadCommand(read.lba, read.size, ::HandleResponse, reply_port));
|
||||||
|
|
||||||
return glcr::OK;
|
return glcr::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DenaliServer::HandleResponse(uint64_t lba, uint64_t size, uint64_t cap) {
|
void DenaliServer::HandleResponse(z_cap_t reply_port, uint64_t lba,
|
||||||
|
uint64_t size, z_cap_t mem) {
|
||||||
DenaliReadResponse resp{
|
DenaliReadResponse resp{
|
||||||
.device_id = 0,
|
.device_id = 0,
|
||||||
.lba = lba,
|
.lba = lba,
|
||||||
.size = size,
|
.size = size,
|
||||||
};
|
};
|
||||||
check(ZChannelSend(channel_cap_, sizeof(resp),
|
check(ZReplyPortSend(reply_port, sizeof(resp), &resp, 1, &mem));
|
||||||
reinterpret_cast<uint8_t*>(&resp), 1, &cap));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,26 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <glacier/status/error.h>
|
#include <glacier/status/error.h>
|
||||||
|
#include <mammoth/endpoint_server.h>
|
||||||
|
|
||||||
#include "ahci/ahci_driver.h"
|
#include "ahci/ahci_driver.h"
|
||||||
#include "denali/denali.h"
|
#include "denali/denali.h"
|
||||||
|
|
||||||
class DenaliServer {
|
class DenaliServer {
|
||||||
public:
|
public:
|
||||||
DenaliServer(uint64_t channel_cap, AhciDriver& driver);
|
DenaliServer(EndpointServer server, AhciDriver& driver);
|
||||||
|
|
||||||
glcr::ErrorCode RunServer();
|
glcr::ErrorCode RunServer();
|
||||||
|
|
||||||
void HandleResponse(uint64_t lba, uint64_t size, uint64_t cap);
|
void HandleResponse(z_cap_t reply_port, uint64_t lba, uint64_t size,
|
||||||
|
z_cap_t cap);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const uint64_t kBuffSize = 1024;
|
static const uint64_t kBuffSize = 1024;
|
||||||
uint64_t channel_cap_;
|
EndpointServer server_;
|
||||||
uint8_t read_buffer_[kBuffSize];
|
uint8_t read_buffer_[kBuffSize];
|
||||||
|
|
||||||
AhciDriver& driver_;
|
AhciDriver& driver_;
|
||||||
|
|
||||||
glcr::ErrorCode HandleRead(const DenaliRead& read);
|
glcr::ErrorCode HandleRead(const DenaliRead& read, z_cap_t reply_port);
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,9 +20,11 @@ add_executable(zion
|
||||||
memory/user_stack_manager.cpp
|
memory/user_stack_manager.cpp
|
||||||
object/address_space.cpp
|
object/address_space.cpp
|
||||||
object/channel.cpp
|
object/channel.cpp
|
||||||
|
object/endpoint.cpp
|
||||||
object/memory_object.cpp
|
object/memory_object.cpp
|
||||||
object/port.cpp
|
object/port.cpp
|
||||||
object/process.cpp
|
object/process.cpp
|
||||||
|
object/reply_port.cpp
|
||||||
object/thread.cpp
|
object/thread.cpp
|
||||||
scheduler/context_switch.s
|
scheduler/context_switch.s
|
||||||
scheduler/jump_user_space.s
|
scheduler/jump_user_space.s
|
||||||
|
@ -32,9 +34,11 @@ add_executable(zion
|
||||||
syscall/capability.cpp
|
syscall/capability.cpp
|
||||||
syscall/channel.cpp
|
syscall/channel.cpp
|
||||||
syscall/debug.cpp
|
syscall/debug.cpp
|
||||||
|
syscall/endpoint.cpp
|
||||||
syscall/memory_object.cpp
|
syscall/memory_object.cpp
|
||||||
syscall/port.cpp
|
syscall/port.cpp
|
||||||
syscall/process.cpp
|
syscall/process.cpp
|
||||||
|
syscall/reply_port.cpp
|
||||||
syscall/syscall.cpp
|
syscall/syscall.cpp
|
||||||
syscall/syscall_enter.s
|
syscall/syscall_enter.s
|
||||||
syscall/thread.cpp
|
syscall/thread.cpp
|
||||||
|
|
|
@ -130,8 +130,8 @@ SYS4(EndpointRecv, z_cap_t, endpoint_cap, uint64_t*, num_bytes, void*, data,
|
||||||
z_cap_t*, reply_port_cap);
|
z_cap_t*, reply_port_cap);
|
||||||
SYS5(ReplyPortSend, z_cap_t, reply_port_cap, uint64_t, num_bytes, const void*,
|
SYS5(ReplyPortSend, z_cap_t, reply_port_cap, uint64_t, num_bytes, const void*,
|
||||||
data, uint64_t, num_caps, z_cap_t*, caps);
|
data, uint64_t, num_caps, z_cap_t*, caps);
|
||||||
SYS5(ReplyPortRecv, z_cap_t, reply_port_cap, uint64_t*, num_bytes, const void*,
|
SYS5(ReplyPortRecv, z_cap_t, reply_port_cap, uint64_t*, num_bytes, void*, data,
|
||||||
data, uint64_t*, num_caps, z_cap_t*, caps);
|
uint64_t*, num_caps, z_cap_t*, caps);
|
||||||
|
|
||||||
SYS2(CapDuplicate, z_cap_t, cap_in, z_cap_t*, cap_out);
|
SYS2(CapDuplicate, z_cap_t, cap_in, z_cap_t*, cap_out);
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,6 @@
|
||||||
extern uint64_t gSelfProcCap;
|
extern uint64_t gSelfProcCap;
|
||||||
extern uint64_t gSelfVmasCap;
|
extern uint64_t gSelfVmasCap;
|
||||||
|
|
||||||
extern uint64_t gInitChannelCap;
|
extern uint64_t gInitEndpointCap;
|
||||||
|
|
||||||
extern uint64_t gBootDenaliVmmoCap;
|
extern uint64_t gBootDenaliVmmoCap;
|
||||||
|
|
|
@ -85,6 +85,6 @@ typedef uint64_t z_cap_t;
|
||||||
#define Z_INIT_SELF_PROC 0x4000'0000
|
#define Z_INIT_SELF_PROC 0x4000'0000
|
||||||
#define Z_INIT_SELF_VMAS 0x4000'0001
|
#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
|
#define Z_BOOT_DENALI_VMMO 0x4200'0000
|
||||||
|
|
|
@ -70,3 +70,59 @@ void UnboundedMessageQueue::WriteKernel(uint64_t init,
|
||||||
|
|
||||||
pending_messages_.PushBack(msg);
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <glacier/memory/ref_ptr.h>
|
#include <glacier/memory/ref_ptr.h>
|
||||||
#include <glacier/memory/shared_ptr.h>
|
#include <glacier/memory/shared_ptr.h>
|
||||||
|
#include <glacier/status/error.h>
|
||||||
|
|
||||||
#include "capability/capability.h"
|
#include "capability/capability.h"
|
||||||
#include "include/ztypes.h"
|
#include "include/ztypes.h"
|
||||||
|
@ -11,10 +12,10 @@ class MessageQueue {
|
||||||
public:
|
public:
|
||||||
virtual ~MessageQueue() {}
|
virtual ~MessageQueue() {}
|
||||||
|
|
||||||
virtual z_err_t PushBack(uint64_t num_bytes, const void* bytes,
|
virtual glcr::ErrorCode PushBack(uint64_t num_bytes, const void* bytes,
|
||||||
uint64_t num_caps, const z_cap_t* caps) = 0;
|
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,
|
virtual glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes,
|
||||||
z_cap_t* caps) = 0;
|
uint64_t* num_caps, z_cap_t* caps) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class UnboundedMessageQueue : public MessageQueue {
|
class UnboundedMessageQueue : public MessageQueue {
|
||||||
|
@ -24,10 +25,10 @@ class UnboundedMessageQueue : public MessageQueue {
|
||||||
UnboundedMessageQueue& operator=(const UnboundedMessageQueue&) = delete;
|
UnboundedMessageQueue& operator=(const UnboundedMessageQueue&) = delete;
|
||||||
virtual ~UnboundedMessageQueue() override {}
|
virtual ~UnboundedMessageQueue() override {}
|
||||||
|
|
||||||
z_err_t PushBack(uint64_t num_bytes, const void* bytes, uint64_t num_caps,
|
glcr::ErrorCode PushBack(uint64_t num_bytes, const void* bytes,
|
||||||
const z_cap_t* caps) override;
|
uint64_t num_caps, const z_cap_t* caps) override;
|
||||||
z_err_t PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
|
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
|
||||||
z_cap_t* caps) override;
|
z_cap_t* caps) override;
|
||||||
|
|
||||||
void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap);
|
void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap);
|
||||||
|
|
||||||
|
@ -44,3 +45,25 @@ class UnboundedMessageQueue : public MessageQueue {
|
||||||
|
|
||||||
LinkedList<glcr::SharedPtr<Message>> pending_messages_;
|
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_;
|
||||||
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
void Mutex::Lock() {
|
void Mutex::Lock() {
|
||||||
while (__atomic_fetch_or(&lock_, 0x1, __ATOMIC_SEQ_CST) == 0x1) {
|
while (__atomic_fetch_or(&lock_, 0x1, __ATOMIC_SEQ_CST) == 0x1) {
|
||||||
dbgln("Lock sleep: %s", name_);
|
// dbgln("Lock sleep: %s", name_);
|
||||||
gScheduler->Preempt();
|
gScheduler->Preempt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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() {}
|
||||||
|
};
|
|
@ -12,6 +12,8 @@ class KernelObject : public glcr::RefCounted<KernelObject> {
|
||||||
MEMORY_OBJECT = 0x4,
|
MEMORY_OBJECT = 0x4,
|
||||||
CHANNEL = 0x5,
|
CHANNEL = 0x5,
|
||||||
PORT = 0x6,
|
PORT = 0x6,
|
||||||
|
ENDPOINT = 0x7,
|
||||||
|
REPLY_PORT = 0x8,
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual uint64_t TypeTag() = 0;
|
virtual uint64_t TypeTag() = 0;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -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() {}
|
||||||
|
};
|
|
@ -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);
|
||||||
|
}
|
|
@ -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);
|
|
@ -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);
|
||||||
|
}
|
|
@ -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);
|
|
@ -8,9 +8,11 @@
|
||||||
#include "syscall/capability.h"
|
#include "syscall/capability.h"
|
||||||
#include "syscall/channel.h"
|
#include "syscall/channel.h"
|
||||||
#include "syscall/debug.h"
|
#include "syscall/debug.h"
|
||||||
|
#include "syscall/endpoint.h"
|
||||||
#include "syscall/memory_object.h"
|
#include "syscall/memory_object.h"
|
||||||
#include "syscall/port.h"
|
#include "syscall/port.h"
|
||||||
#include "syscall/process.h"
|
#include "syscall/process.h"
|
||||||
|
#include "syscall/reply_port.h"
|
||||||
#include "syscall/thread.h"
|
#include "syscall/thread.h"
|
||||||
|
|
||||||
#define EFER 0xC0000080
|
#define EFER 0xC0000080
|
||||||
|
@ -73,6 +75,13 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
|
||||||
CASE(PortRecv);
|
CASE(PortRecv);
|
||||||
CASE(PortPoll);
|
CASE(PortPoll);
|
||||||
CASE(IrqRegister);
|
CASE(IrqRegister);
|
||||||
|
// syscall/endpoint.h
|
||||||
|
CASE(EndpointCreate);
|
||||||
|
CASE(EndpointSend);
|
||||||
|
CASE(EndpointRecv);
|
||||||
|
// syscall/reply_port.h
|
||||||
|
CASE(ReplyPortSend);
|
||||||
|
CASE(ReplyPortRecv);
|
||||||
// syscall/capability.h
|
// syscall/capability.h
|
||||||
CASE(CapDuplicate);
|
CASE(CapDuplicate);
|
||||||
// syscall/debug.h
|
// syscall/debug.h
|
||||||
|
|
Loading…
Reference in New Issue