[Zion] Use the glacier ArrayView class for sending IPC msgs.

This commit is contained in:
Drew Galbraith 2023-11-02 23:31:08 -07:00
parent 6feb13d042
commit 277b0d3ccc
8 changed files with 82 additions and 85 deletions

View File

@ -153,7 +153,7 @@ glcr::RefPtr<Port> pci1_port;
extern "C" void isr_pci1();
extern "C" void interrupt_pci1(InterruptFrame*) {
dbgln("Interrupt PCI line 1");
pci1_port->Send(0, nullptr, 0, nullptr);
pci1_port->Send({}, {});
gApic->SignalEOI();
}

View File

@ -3,30 +3,24 @@
#include "debug/debug.h"
#include "scheduler/scheduler.h"
glcr::ErrorCode UnboundedMessageQueue::PushBack(uint64_t num_bytes,
const void* bytes,
uint64_t num_caps,
const z_cap_t* caps,
z_cap_t reply_cap) {
if (num_bytes > 0x1000) {
dbgln("Large message size unimplemented: %x", num_bytes);
glcr::ErrorCode UnboundedMessageQueue::PushBack(
const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps, z_cap_t reply_cap) {
if (message.size() > 0x1000) {
dbgln("Large message size unimplemented: %x", message.size());
return glcr::UNIMPLEMENTED;
}
auto message = glcr::MakeShared<Message>();
message->num_bytes = num_bytes;
message->bytes = new uint8_t[num_bytes];
for (uint64_t i = 0; i < num_bytes; i++) {
message->bytes[i] = static_cast<const uint8_t*>(bytes)[i];
}
auto msg_struct = glcr::MakeShared<Message>();
msg_struct->message = glcr::Array<uint8_t>(message);
if (reply_cap != kZionInvalidCapability) {
// FIXME: We're just trusting that capability has the correct permissions.
message->reply_cap =
msg_struct->reply_cap =
gScheduler->CurrentProcess().ReleaseCapability(reply_cap);
}
for (uint64_t i = 0; i < num_caps; i++) {
for (uint64_t i = 0; i < caps.size(); i++) {
// FIXME: This would feel safer closer to the relevant syscall.
// FIXME: Race conditions on get->check->release here. Would be better to
// have that as a single call on the process. (This pattern repeats other
@ -39,11 +33,11 @@ glcr::ErrorCode UnboundedMessageQueue::PushBack(uint64_t num_bytes,
return glcr::CAP_PERMISSION_DENIED;
}
cap = gScheduler->CurrentProcess().ReleaseCapability(caps[i]);
message->caps.PushBack(cap);
msg_struct->caps.PushBack(cap);
}
MutexHolder h(mutex_);
pending_messages_.PushBack(message);
pending_messages_.PushBack(msg_struct);
if (blocked_threads_.size() > 0) {
auto thread = blocked_threads_.PopFront();
@ -70,7 +64,7 @@ glcr::ErrorCode UnboundedMessageQueue::PopFront(uint64_t* num_bytes,
MutexHolder lock(mutex_);
auto next_msg = pending_messages_.PeekFront();
if (next_msg->num_bytes > *num_bytes) {
if (next_msg->message.size() > *num_bytes) {
return glcr::BUFFER_SIZE;
}
if (next_msg->caps.size() > *num_caps) {
@ -79,10 +73,10 @@ glcr::ErrorCode UnboundedMessageQueue::PopFront(uint64_t* num_bytes,
next_msg = pending_messages_.PopFront();
*num_bytes = next_msg->num_bytes;
*num_bytes = next_msg->message.size();
for (uint64_t i = 0; i < *num_bytes; i++) {
static_cast<uint8_t*>(bytes)[i] = next_msg->bytes[i];
static_cast<uint8_t*>(bytes)[i] = next_msg->message[i];
}
auto& proc = gScheduler->CurrentProcess();
@ -105,40 +99,32 @@ void UnboundedMessageQueue::WriteKernel(uint64_t init,
glcr::RefPtr<Capability> cap) {
// FIXME: Add synchronization here in case it is ever used outside of init.
auto msg = glcr::MakeShared<Message>();
msg->bytes = new uint8_t[8];
msg->num_bytes = sizeof(init);
msg->message = glcr::Array<uint8_t>(sizeof(init));
uint8_t* data = reinterpret_cast<uint8_t*>(&init);
for (uint8_t i = 0; i < sizeof(init); i++) {
msg->bytes[i] = data[i];
msg->message[i] = data[i];
}
msg->caps.PushBack(cap);
pending_messages_.PushBack(msg);
}
glcr::ErrorCode SingleMessageQueue::PushBack(uint64_t num_bytes,
const void* bytes,
uint64_t num_caps,
const z_cap_t* caps,
z_cap_t reply_port) {
glcr::ErrorCode SingleMessageQueue::PushBack(
const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps, z_cap_t reply_port) {
MutexHolder h(mutex_);
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];
}
message_ = message;
if (reply_port != kZionInvalidCapability) {
dbgln("Sent a reply port to a single message queue");
return glcr::INTERNAL;
}
for (uint64_t i = 0; i < num_caps; i++) {
for (uint64_t i = 0; i < caps.size(); i++) {
// FIXME: This would feel safer closer to the relevant syscall.
auto cap = gScheduler->CurrentProcess().GetCapability(caps[i]);
if (!cap) {
@ -181,16 +167,16 @@ glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
return glcr::FAILED_PRECONDITION;
}
if (num_bytes_ > *num_bytes) {
if (message_.size() > *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_bytes = message_.size();
for (uint64_t i = 0; i < message_.size(); i++) {
reinterpret_cast<uint8_t*>(bytes)[i] = message_[i];
}
if (reply_port != nullptr) {

View File

@ -1,5 +1,7 @@
#pragma once
#include <glacier/container/array.h>
#include <glacier/container/array_view.h>
#include <glacier/container/intrusive_list.h>
#include <glacier/container/linked_list.h>
#include <glacier/memory/ref_ptr.h>
@ -14,12 +16,12 @@ class MessageQueue {
public:
virtual ~MessageQueue() {}
virtual glcr::ErrorCode PushBack(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps,
z_cap_t reply_cap = 0) = 0;
virtual glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps,
z_cap_t reply_cap) = 0;
virtual glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps, z_cap_t* caps,
z_cap_t* reply_cap = nullptr) = 0;
z_cap_t* reply_cap) = 0;
virtual bool empty() = 0;
protected:
@ -36,8 +38,8 @@ class UnboundedMessageQueue : public MessageQueue {
UnboundedMessageQueue& operator=(const UnboundedMessageQueue&) = delete;
virtual ~UnboundedMessageQueue() override {}
glcr::ErrorCode PushBack(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps,
glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps,
z_cap_t reply_cap) override;
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
z_cap_t* caps, z_cap_t* reply_cap) override;
@ -51,8 +53,7 @@ class UnboundedMessageQueue : public MessageQueue {
private:
struct Message {
uint64_t num_bytes;
uint8_t* bytes;
glcr::Array<uint8_t> message;
glcr::LinkedList<glcr::RefPtr<Capability>> caps;
glcr::RefPtr<Capability> reply_cap;
@ -68,8 +69,8 @@ class SingleMessageQueue : public MessageQueue {
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,
glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps,
z_cap_t reply_cap) override;
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
z_cap_t* caps, z_cap_t* reply_cap) override;
@ -82,7 +83,6 @@ class SingleMessageQueue : public MessageQueue {
private:
bool has_written_ = false;
bool has_read_ = false;
uint64_t num_bytes_;
uint8_t* bytes_;
glcr::Array<uint8_t> message_;
glcr::LinkedList<glcr::RefPtr<Capability>> caps_;
};

View File

@ -5,18 +5,3 @@
glcr::RefPtr<Endpoint> Endpoint::Create() {
return glcr::AdoptPtr(new Endpoint);
}
glcr::ErrorCode Endpoint::Send(uint64_t num_bytes, const void* data,
uint64_t num_caps, const z_cap_t* caps,
z_cap_t reply_port_cap) {
auto& message_queue = GetSendMessageQueue();
return message_queue.PushBack(num_bytes, data, num_caps, caps,
reply_port_cap);
}
glcr::ErrorCode Endpoint::Recv(uint64_t* num_bytes, void* data,
uint64_t* num_caps, z_cap_t* caps,
z_cap_t* reply_port_cap) {
auto& message_queue = GetRecvMessageQueue();
return message_queue.PopFront(num_bytes, data, num_caps, caps,
reply_port_cap);
}

View File

@ -26,12 +26,6 @@ class Endpoint : public IpcObject {
static glcr::RefPtr<Endpoint> Create();
// FIXME: These are hacky "almost" overrides that could lead to bugs.
glcr::ErrorCode Send(uint64_t num_bytes, const void* data, uint64_t num_caps,
const z_cap_t* caps, z_cap_t reply_port_cap);
glcr::ErrorCode Recv(uint64_t* num_bytes, void* data, uint64_t* num_caps,
z_cap_t* caps, z_cap_t* reply_port_cap);
virtual MessageQueue& GetSendMessageQueue() override {
return message_queue_;
}

View File

@ -2,14 +2,26 @@
#include "scheduler/scheduler.h"
glcr::ErrorCode IpcObject::Send(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps) {
glcr::ErrorCode IpcObject::Send(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps) {
return Send(message, caps, kZionInvalidCapability);
}
glcr::ErrorCode IpcObject::Send(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps,
const z_cap_t reply_port) {
auto& message_queue = GetSendMessageQueue();
return message_queue.PushBack(num_bytes, bytes, num_caps, caps);
return message_queue.PushBack(message, caps, reply_port);
}
glcr::ErrorCode IpcObject::Recv(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps, z_cap_t* caps) {
auto& message_queue = GetRecvMessageQueue();
return message_queue.PopFront(num_bytes, bytes, num_caps, caps);
return Recv(num_bytes, bytes, num_caps, caps, nullptr);
}
glcr::ErrorCode IpcObject::Recv(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps, z_cap_t* caps,
z_cap_t* reply_port) {
auto& message_queue = GetRecvMessageQueue();
return message_queue.PopFront(num_bytes, bytes, num_caps, caps, reply_port);
}

View File

@ -11,10 +11,17 @@ class IpcObject : public KernelObject {
IpcObject(){};
virtual ~IpcObject() {}
virtual glcr::ErrorCode Send(uint64_t num_bytes, const void* bytes,
uint64_t num_caps, const z_cap_t* caps) final;
virtual glcr::ErrorCode Send(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps) final;
virtual glcr::ErrorCode Send(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps,
const z_cap_t reply_port) final;
virtual glcr::ErrorCode Recv(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps, z_cap_t* caps) final;
virtual glcr::ErrorCode Recv(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps, z_cap_t* caps,
z_cap_t* reply_port) final;
bool HasMessages() { return !GetRecvMessageQueue().empty(); }

View File

@ -6,6 +6,14 @@
#include "object/reply_port.h"
#include "scheduler/scheduler.h"
namespace {
glcr::ArrayView<uint8_t> Buffer(const void* bytes, uint64_t num_bytes) {
return glcr::ArrayView(reinterpret_cast<uint8_t*>(const_cast<void*>(bytes)),
num_bytes);
}
} // namespace
glcr::ErrorCode ChannelCreate(ZChannelCreateReq* req) {
auto& proc = gScheduler->CurrentProcess();
auto chan_pair = Channel::CreateChannelPair();
@ -20,7 +28,8 @@ glcr::ErrorCode ChannelSend(ZChannelSendReq* req) {
RET_ERR(ValidateCapability<Channel>(chan_cap, kZionPerm_Write));
auto chan = chan_cap->obj<Channel>();
return chan->Send(req->num_bytes, req->data, req->num_caps, req->caps);
return chan->Send(Buffer(req->data, req->num_bytes),
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
}
glcr::ErrorCode ChannelRecv(ZChannelRecvReq* req) {
@ -45,7 +54,8 @@ glcr::ErrorCode PortSend(ZPortSendReq* req) {
RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Write));
auto port = port_cap->obj<Port>();
return port->Send(req->num_bytes, req->data, req->num_caps, req->caps);
return port->Send(Buffer(req->data, req->num_bytes),
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
}
glcr::ErrorCode PortRecv(ZPortRecvReq* req) {
@ -100,8 +110,10 @@ glcr::ErrorCode EndpointSend(ZEndpointSendReq* req) {
*req->reply_port_cap = proc.AddNewCapability(reply_port, kZionPerm_Read);
uint64_t reply_port_cap_to_send =
proc.AddNewCapability(reply_port, kZionPerm_Write | kZionPerm_Transmit);
return endpoint->Send(req->num_bytes, req->data, req->num_caps, req->caps,
reply_port_cap_to_send);
return endpoint->Send(
Buffer(req->data, req->num_bytes),
glcr::ArrayView<z_cap_t>(const_cast<z_cap_t*>(req->caps), req->num_caps),
reply_port_cap_to_send);
}
glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req) {
@ -126,7 +138,8 @@ glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* req) {
ValidateCapability<ReplyPort>(reply_port_cap, kZionPerm_Read);
auto reply_port = reply_port_cap->obj<ReplyPort>();
return reply_port->Send(req->num_bytes, req->data, req->num_caps, req->caps);
return reply_port->Send(Buffer(req->data, req->num_bytes),
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
}
glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req) {
auto& proc = gScheduler->CurrentProcess();