diff --git a/zion/lib/message_queue.cpp b/zion/lib/message_queue.cpp index 0d09552..b830947 100644 --- a/zion/lib/message_queue.cpp +++ b/zion/lib/message_queue.cpp @@ -48,10 +48,8 @@ glcr::ErrorCode UnboundedMessageQueue::PushBack( return glcr::OK; } -glcr::ErrorCode UnboundedMessageQueue::PopFront(uint64_t* num_bytes, - void* bytes, uint64_t* num_caps, - z_cap_t* caps, - z_cap_t* reply_cap) { +glcr::ErrorOr UnboundedMessageQueue::PopFront( + uint64_t data_buf_size, uint64_t cap_buf_size) { mutex_->Lock(); while (pending_messages_.empty()) { auto thread = gScheduler->CurrentThread(); @@ -65,35 +63,14 @@ glcr::ErrorCode UnboundedMessageQueue::PopFront(uint64_t* num_bytes, MutexHolder lock(mutex_); auto& next_msg = pending_messages_.PeekFront(); - if (next_msg.data.size() > *num_bytes) { + if (next_msg.data.size() > data_buf_size) { return glcr::BUFFER_SIZE; } - if (next_msg.caps.size() > *num_caps) { + if (next_msg.caps.size() > cap_buf_size) { return glcr::BUFFER_SIZE; } - next_msg = pending_messages_.PopFront(); - - *num_bytes = next_msg.data.size(); - - for (uint64_t i = 0; i < *num_bytes; i++) { - static_cast(bytes)[i] = next_msg.data[i]; - } - - auto& proc = gScheduler->CurrentProcess(); - if (reply_cap != nullptr) { - if (!next_msg.reply_cap) { - dbgln("Tried to read reply capability off of a message without one"); - return glcr::INTERNAL; - } - *reply_cap = proc.AddExistingCapability(next_msg.reply_cap); - } - - *num_caps = next_msg.caps.size(); - for (uint64_t i = 0; i < *num_caps; i++) { - caps[i] = proc.AddExistingCapability(next_msg.caps[i]); - } - return glcr::OK; + return pending_messages_.PopFront(); } void UnboundedMessageQueue::WriteKernel(uint64_t init, @@ -150,9 +127,8 @@ glcr::ErrorCode SingleMessageQueue::PushBack( return glcr::OK; } -glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes, - uint64_t* num_caps, z_cap_t* caps, - z_cap_t* reply_port) { +glcr::ErrorOr SingleMessageQueue::PopFront(uint64_t data_buf_size, + uint64_t cap_buf_size) { mutex_->Lock(); while (!has_written_) { auto thread = gScheduler->CurrentThread(); @@ -169,29 +145,14 @@ glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes, return glcr::FAILED_PRECONDITION; } - if (message_.data.size() > *num_bytes) { + if (message_.data.size() > data_buf_size) { return glcr::BUFFER_SIZE; } - if (message_.caps.size() > *num_caps) { + if (message_.caps.size() > cap_buf_size) { return glcr::BUFFER_SIZE; } - *num_bytes = message_.data.size(); - for (uint64_t i = 0; i < message_.data.size(); i++) { - reinterpret_cast(bytes)[i] = message_.data[i]; - } - - if (reply_port != nullptr) { - dbgln("Tried to read a reply port a single message queue"); - return glcr::INTERNAL; - } - - *num_caps = message_.caps.size(); - auto& proc = gScheduler->CurrentProcess(); - for (uint64_t i = 0; i < *num_caps; i++) { - caps[i] = proc.AddExistingCapability(message_.caps[i]); - } has_read_ = true; - return glcr::OK; + return glcr::Move(message_); } diff --git a/zion/lib/message_queue.h b/zion/lib/message_queue.h index 39cf906..af6c025 100644 --- a/zion/lib/message_queue.h +++ b/zion/lib/message_queue.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "capability/capability.h" #include "include/ztypes.h" @@ -28,9 +29,8 @@ class MessageQueue { virtual glcr::ErrorCode PushBack(const glcr::ArrayView& message, const glcr::ArrayView& 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) = 0; + virtual glcr::ErrorOr PopFront(uint64_t data_buf_size, + uint64_t cap_buf_size) = 0; virtual bool empty() = 0; protected: @@ -50,8 +50,8 @@ class UnboundedMessageQueue : public MessageQueue { glcr::ErrorCode PushBack(const glcr::ArrayView& message, const glcr::ArrayView& 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; + glcr::ErrorOr PopFront(uint64_t data_buf_size, + uint64_t cap_buf_size) override; void WriteKernel(uint64_t init, glcr::RefPtr cap); @@ -74,8 +74,8 @@ class SingleMessageQueue : public MessageQueue { glcr::ErrorCode PushBack(const glcr::ArrayView& message, const glcr::ArrayView& 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; + glcr::ErrorOr PopFront(uint64_t data_buf_size, + uint64_t cap_buf_size) override; bool empty() override { MutexHolder h(mutex_); diff --git a/zion/object/ipc_object.cpp b/zion/object/ipc_object.cpp index d82d178..fed7c72 100644 --- a/zion/object/ipc_object.cpp +++ b/zion/object/ipc_object.cpp @@ -14,14 +14,7 @@ glcr::ErrorCode IpcObject::Send(const glcr::ArrayView& message, 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) { - 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); +glcr::ErrorOr IpcObject::Recv(uint64_t data_buf_size, + uint64_t cap_buf_size) { + return GetRecvMessageQueue().PopFront(data_buf_size, cap_buf_size); } diff --git a/zion/object/ipc_object.h b/zion/object/ipc_object.h index cdc927f..9f78618 100644 --- a/zion/object/ipc_object.h +++ b/zion/object/ipc_object.h @@ -17,11 +17,8 @@ class IpcObject : public KernelObject { const glcr::ArrayView& 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; + virtual glcr::ErrorOr Recv(uint64_t data_buf_size, + uint64_t cap_buf_size) final; bool HasMessages() { return !GetRecvMessageQueue().empty(); } diff --git a/zion/syscall/ipc.cpp b/zion/syscall/ipc.cpp index c234211..28a25b1 100644 --- a/zion/syscall/ipc.cpp +++ b/zion/syscall/ipc.cpp @@ -1,6 +1,7 @@ #include "syscall/ipc.h" #include "capability/capability.h" +#include "debug/debug.h" #include "interrupt/interrupt.h" #include "object/endpoint.h" #include "object/reply_port.h" @@ -12,6 +13,44 @@ glcr::ArrayView Buffer(const void* bytes, uint64_t num_bytes) { return glcr::ArrayView(reinterpret_cast(const_cast(bytes)), num_bytes); } + +template +glcr::ErrorCode TranslateIpcMessageToResponse(const IpcMessage& message, + T* resp) { + if (message.data.size() > *resp->num_bytes) { + return glcr::BUFFER_SIZE; + } + if (message.caps.size() > *resp->num_caps) { + return glcr::BUFFER_SIZE; + } + + *resp->num_bytes = message.data.size(); + for (uint64_t i = 0; i < message.data.size(); i++) { + reinterpret_cast(resp->data)[i] = message.data[i]; + } + + *resp->num_caps = message.caps.size(); + auto& proc = gScheduler->CurrentProcess(); + for (uint64_t i = 0; i < *resp->num_caps; i++) { + resp->caps[i] = proc.AddExistingCapability(message.caps[i]); + } + return glcr::OK; +} + +template +glcr::ErrorCode TranslateIpcMessageToResponseWithReplyPort( + const IpcMessage& message, T* resp) { + TranslateIpcMessageToResponse(message, resp); + + if (!message.reply_cap) { + dbgln("Tried to read reply capability off of a message without one"); + return glcr::INTERNAL; + } + auto& proc = gScheduler->CurrentProcess(); + *resp->reply_port_cap = proc.AddExistingCapability(message.reply_cap); + return glcr::OK; +} + } // namespace glcr::ErrorCode ChannelCreate(ZChannelCreateReq* req) { @@ -38,7 +77,8 @@ glcr::ErrorCode ChannelRecv(ZChannelRecvReq* req) { RET_ERR(ValidateCapability(chan_cap, kZionPerm_Read)); auto chan = chan_cap->obj(); - return chan->Recv(req->num_bytes, req->data, req->num_caps, req->caps); + ASSIGN_OR_RETURN(IpcMessage msg, chan->Recv(*req->num_bytes, *req->num_caps)); + return TranslateIpcMessageToResponse(msg, req); } glcr::ErrorCode PortCreate(ZPortCreateReq* req) { @@ -64,7 +104,8 @@ glcr::ErrorCode PortRecv(ZPortRecvReq* req) { RET_ERR(ValidateCapability(port_cap, kZionPerm_Read)); auto port = port_cap->obj(); - return port->Recv(req->num_bytes, req->data, req->num_caps, req->caps); + ASSIGN_OR_RETURN(IpcMessage msg, port->Recv(*req->num_bytes, *req->num_caps)); + return TranslateIpcMessageToResponse(msg, req); } glcr::ErrorCode PortPoll(ZPortPollReq* req) { @@ -78,7 +119,8 @@ glcr::ErrorCode PortPoll(ZPortPollReq* req) { if (!port->HasMessages()) { return glcr::EMPTY; } - return port->Recv(req->num_bytes, req->data, req->num_caps, req->caps); + ASSIGN_OR_RETURN(IpcMessage msg, port->Recv(*req->num_bytes, *req->num_caps)); + return TranslateIpcMessageToResponse(msg, req); } glcr::ErrorCode IrqRegister(ZIrqRegisterReq* req) { @@ -123,13 +165,9 @@ glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req) { ValidateCapability(endpoint_cap, kZionPerm_Read); auto endpoint = endpoint_cap->obj(); - uint64_t num_caps = 1; - RET_ERR(endpoint->Recv(req->num_bytes, req->data, req->num_caps, req->caps, - req->reply_port_cap)); - if (num_caps != 1) { - return glcr::INTERNAL; - } - return glcr::OK; + ASSIGN_OR_RETURN(IpcMessage msg, + endpoint->Recv(*req->num_bytes, *req->num_caps)); + return TranslateIpcMessageToResponseWithReplyPort(msg, req); } glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* req) { @@ -148,5 +186,7 @@ glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req) { ValidateCapability(reply_port_cap, kZionPerm_Read); auto reply_port = reply_port_cap->obj(); - return reply_port->Recv(req->num_bytes, req->data, req->num_caps, req->caps); + ASSIGN_OR_RETURN(IpcMessage msg, + reply_port->Recv(*req->num_bytes, *req->num_caps)); + return TranslateIpcMessageToResponse(msg, req); }