acadia/zion/lib/message_queue.cpp

73 lines
2.1 KiB
C++
Raw Normal View History

#include "lib/message_queue.h"
#include "scheduler/scheduler.h"
z_err_t UnboundedMessageQueue::PushBack(uint64_t num_bytes, const void* bytes,
uint64_t num_caps,
const z_cap_t* caps) {
if (num_bytes > 0x1000) {
dbgln("Large message size unimplemented: %x", num_bytes);
2023-06-21 18:28:54 -07:00
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];
}
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) {
2023-06-21 18:28:54 -07:00
return glcr::CAP_NOT_FOUND;
}
message->caps.PushBack(cap);
}
pending_messages_.PushBack(message);
2023-06-21 18:28:54 -07:00
return glcr::OK;
}
z_err_t UnboundedMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
uint64_t* num_caps, z_cap_t* caps) {
auto next_msg = pending_messages_.PeekFront();
if (next_msg->num_bytes > *num_bytes) {
2023-06-21 18:28:54 -07:00
return glcr::BUFFER_SIZE;
}
if (next_msg->caps.size() > *num_caps) {
2023-06-21 18:28:54 -07:00
return glcr::BUFFER_SIZE;
}
next_msg = pending_messages_.PopFront();
*num_bytes = next_msg->num_bytes;
for (uint64_t i = 0; i < *num_bytes; i++) {
static_cast<uint8_t*>(bytes)[i] = next_msg->bytes[i];
}
*num_caps = next_msg->caps.size();
auto& proc = gScheduler->CurrentProcess();
for (uint64_t i = 0; i < *num_caps; i++) {
caps[i] = proc.AddExistingCapability(next_msg->caps.PopFront());
}
2023-06-21 18:28:54 -07:00
return glcr::OK;
}
2023-06-21 15:07:40 -07:00
void UnboundedMessageQueue::WriteKernel(uint64_t init,
glcr::RefPtr<Capability> cap) {
auto msg = glcr::MakeShared<Message>();
msg->bytes = new uint8_t[8];
msg->num_bytes = 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->caps.PushBack(cap);
pending_messages_.PushBack(msg);
}