[Zion] Return the IpcMessage up to the syscall level.

This commit is contained in:
Drew Galbraith 2023-11-03 00:12:28 -07:00
parent d2c77e1d18
commit d7af2e3f4f
5 changed files with 73 additions and 82 deletions

View File

@ -48,10 +48,8 @@ glcr::ErrorCode UnboundedMessageQueue::PushBack(
return glcr::OK; return glcr::OK;
} }
glcr::ErrorCode UnboundedMessageQueue::PopFront(uint64_t* num_bytes, glcr::ErrorOr<IpcMessage> UnboundedMessageQueue::PopFront(
void* bytes, uint64_t* num_caps, uint64_t data_buf_size, uint64_t cap_buf_size) {
z_cap_t* caps,
z_cap_t* reply_cap) {
mutex_->Lock(); mutex_->Lock();
while (pending_messages_.empty()) { while (pending_messages_.empty()) {
auto thread = gScheduler->CurrentThread(); auto thread = gScheduler->CurrentThread();
@ -65,35 +63,14 @@ glcr::ErrorCode UnboundedMessageQueue::PopFront(uint64_t* num_bytes,
MutexHolder lock(mutex_); MutexHolder lock(mutex_);
auto& next_msg = pending_messages_.PeekFront(); 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; return glcr::BUFFER_SIZE;
} }
if (next_msg.caps.size() > *num_caps) { if (next_msg.caps.size() > cap_buf_size) {
return glcr::BUFFER_SIZE; return glcr::BUFFER_SIZE;
} }
next_msg = pending_messages_.PopFront(); return pending_messages_.PopFront();
*num_bytes = next_msg.data.size();
for (uint64_t i = 0; i < *num_bytes; i++) {
static_cast<uint8_t*>(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;
} }
void UnboundedMessageQueue::WriteKernel(uint64_t init, void UnboundedMessageQueue::WriteKernel(uint64_t init,
@ -150,9 +127,8 @@ glcr::ErrorCode SingleMessageQueue::PushBack(
return glcr::OK; return glcr::OK;
} }
glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes, glcr::ErrorOr<IpcMessage> SingleMessageQueue::PopFront(uint64_t data_buf_size,
uint64_t* num_caps, z_cap_t* caps, uint64_t cap_buf_size) {
z_cap_t* reply_port) {
mutex_->Lock(); mutex_->Lock();
while (!has_written_) { while (!has_written_) {
auto thread = gScheduler->CurrentThread(); auto thread = gScheduler->CurrentThread();
@ -169,29 +145,14 @@ glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
return glcr::FAILED_PRECONDITION; return glcr::FAILED_PRECONDITION;
} }
if (message_.data.size() > *num_bytes) { if (message_.data.size() > data_buf_size) {
return glcr::BUFFER_SIZE; return glcr::BUFFER_SIZE;
} }
if (message_.caps.size() > *num_caps) { if (message_.caps.size() > cap_buf_size) {
return glcr::BUFFER_SIZE; return glcr::BUFFER_SIZE;
} }
*num_bytes = message_.data.size();
for (uint64_t i = 0; i < message_.data.size(); i++) {
reinterpret_cast<uint8_t*>(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; has_read_ = true;
return glcr::OK; return glcr::Move(message_);
} }

View File

@ -8,6 +8,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 <glacier/status/error.h>
#include <glacier/status/error_or.h>
#include "capability/capability.h" #include "capability/capability.h"
#include "include/ztypes.h" #include "include/ztypes.h"
@ -28,9 +29,8 @@ class MessageQueue {
virtual glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message, virtual glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps, const glcr::ArrayView<z_cap_t>& caps,
z_cap_t reply_cap) = 0; z_cap_t reply_cap) = 0;
virtual glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, virtual glcr::ErrorOr<IpcMessage> PopFront(uint64_t data_buf_size,
uint64_t* num_caps, z_cap_t* caps, uint64_t cap_buf_size) = 0;
z_cap_t* reply_cap) = 0;
virtual bool empty() = 0; virtual bool empty() = 0;
protected: protected:
@ -50,8 +50,8 @@ class UnboundedMessageQueue : public MessageQueue {
glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message, glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps, const glcr::ArrayView<z_cap_t>& caps,
z_cap_t reply_cap) override; z_cap_t reply_cap) override;
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps, glcr::ErrorOr<IpcMessage> PopFront(uint64_t data_buf_size,
z_cap_t* caps, z_cap_t* reply_cap) override; uint64_t cap_buf_size) override;
void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap); void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap);
@ -74,8 +74,8 @@ class SingleMessageQueue : public MessageQueue {
glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message, glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
const glcr::ArrayView<z_cap_t>& caps, const glcr::ArrayView<z_cap_t>& caps,
z_cap_t reply_cap) override; z_cap_t reply_cap) override;
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps, glcr::ErrorOr<IpcMessage> PopFront(uint64_t data_buf_size,
z_cap_t* caps, z_cap_t* reply_cap) override; uint64_t cap_buf_size) override;
bool empty() override { bool empty() override {
MutexHolder h(mutex_); MutexHolder h(mutex_);

View File

@ -14,14 +14,7 @@ glcr::ErrorCode IpcObject::Send(const glcr::ArrayView<uint8_t>& message,
return message_queue.PushBack(message, caps, reply_port); return message_queue.PushBack(message, caps, reply_port);
} }
glcr::ErrorCode IpcObject::Recv(uint64_t* num_bytes, void* bytes, glcr::ErrorOr<IpcMessage> IpcObject::Recv(uint64_t data_buf_size,
uint64_t* num_caps, z_cap_t* caps) { uint64_t cap_buf_size) {
return Recv(num_bytes, bytes, num_caps, caps, nullptr); return GetRecvMessageQueue().PopFront(data_buf_size, cap_buf_size);
}
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

@ -17,11 +17,8 @@ class IpcObject : public KernelObject {
const glcr::ArrayView<z_cap_t>& caps, const glcr::ArrayView<z_cap_t>& caps,
const z_cap_t reply_port) final; const z_cap_t reply_port) final;
virtual glcr::ErrorCode Recv(uint64_t* num_bytes, void* bytes, virtual glcr::ErrorOr<IpcMessage> Recv(uint64_t data_buf_size,
uint64_t* num_caps, z_cap_t* caps) final; uint64_t cap_buf_size) 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(); } bool HasMessages() { return !GetRecvMessageQueue().empty(); }

View File

@ -1,6 +1,7 @@
#include "syscall/ipc.h" #include "syscall/ipc.h"
#include "capability/capability.h" #include "capability/capability.h"
#include "debug/debug.h"
#include "interrupt/interrupt.h" #include "interrupt/interrupt.h"
#include "object/endpoint.h" #include "object/endpoint.h"
#include "object/reply_port.h" #include "object/reply_port.h"
@ -12,6 +13,44 @@ glcr::ArrayView<uint8_t> Buffer(const void* bytes, uint64_t num_bytes) {
return glcr::ArrayView(reinterpret_cast<uint8_t*>(const_cast<void*>(bytes)), return glcr::ArrayView(reinterpret_cast<uint8_t*>(const_cast<void*>(bytes)),
num_bytes); num_bytes);
} }
template <typename T>
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<uint8_t*>(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 <typename T>
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 } // namespace
glcr::ErrorCode ChannelCreate(ZChannelCreateReq* req) { glcr::ErrorCode ChannelCreate(ZChannelCreateReq* req) {
@ -38,7 +77,8 @@ glcr::ErrorCode ChannelRecv(ZChannelRecvReq* req) {
RET_ERR(ValidateCapability<Channel>(chan_cap, kZionPerm_Read)); RET_ERR(ValidateCapability<Channel>(chan_cap, kZionPerm_Read));
auto chan = chan_cap->obj<Channel>(); auto chan = chan_cap->obj<Channel>();
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) { glcr::ErrorCode PortCreate(ZPortCreateReq* req) {
@ -64,7 +104,8 @@ glcr::ErrorCode PortRecv(ZPortRecvReq* req) {
RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Read)); RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Read));
auto port = port_cap->obj<Port>(); auto port = port_cap->obj<Port>();
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) { glcr::ErrorCode PortPoll(ZPortPollReq* req) {
@ -78,7 +119,8 @@ glcr::ErrorCode PortPoll(ZPortPollReq* req) {
if (!port->HasMessages()) { if (!port->HasMessages()) {
return glcr::EMPTY; 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) { glcr::ErrorCode IrqRegister(ZIrqRegisterReq* req) {
@ -123,13 +165,9 @@ glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req) {
ValidateCapability<Endpoint>(endpoint_cap, kZionPerm_Read); ValidateCapability<Endpoint>(endpoint_cap, kZionPerm_Read);
auto endpoint = endpoint_cap->obj<Endpoint>(); auto endpoint = endpoint_cap->obj<Endpoint>();
uint64_t num_caps = 1; ASSIGN_OR_RETURN(IpcMessage msg,
RET_ERR(endpoint->Recv(req->num_bytes, req->data, req->num_caps, req->caps, endpoint->Recv(*req->num_bytes, *req->num_caps));
req->reply_port_cap)); return TranslateIpcMessageToResponseWithReplyPort(msg, req);
if (num_caps != 1) {
return glcr::INTERNAL;
}
return glcr::OK;
} }
glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* req) { glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* req) {
@ -148,5 +186,7 @@ glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req) {
ValidateCapability<ReplyPort>(reply_port_cap, kZionPerm_Read); ValidateCapability<ReplyPort>(reply_port_cap, kZionPerm_Read);
auto reply_port = reply_port_cap->obj<ReplyPort>(); auto reply_port = reply_port_cap->obj<ReplyPort>();
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);
} }