Compare commits
5 Commits
277b0d3ccc
...
d9df1212b7
Author | SHA1 | Date |
---|---|---|
|
d9df1212b7 | |
|
d7af2e3f4f | |
|
d2c77e1d18 | |
|
59f147193a | |
|
4c09a9d019 |
|
@ -21,7 +21,7 @@ class Array {
|
||||||
|
|
||||||
Array(const Array&) = delete;
|
Array(const Array&) = delete;
|
||||||
|
|
||||||
Array(Array&& other) : data_(other.data), size_(other.size_) {
|
Array(Array&& other) : data_(other.data_), size_(other.size_) {
|
||||||
other.data_ = nullptr;
|
other.data_ = nullptr;
|
||||||
other.size_ = 0;
|
other.size_ = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "glacier/memory/move.h"
|
||||||
|
|
||||||
namespace glcr {
|
namespace glcr {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -15,20 +17,19 @@ class LinkedList {
|
||||||
uint64_t size() const { return size_; }
|
uint64_t size() const { return size_; }
|
||||||
|
|
||||||
void PushBack(const T& item) {
|
void PushBack(const T& item) {
|
||||||
size_++;
|
|
||||||
ListItem* new_item = new ListItem{
|
ListItem* new_item = new ListItem{
|
||||||
.item = item,
|
.item = item,
|
||||||
.next = nullptr,
|
.next = nullptr,
|
||||||
};
|
};
|
||||||
if (front_ == nullptr) {
|
PushBackInternal(new_item);
|
||||||
front_ = new_item;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
ListItem* litem = front_;
|
|
||||||
while (litem->next != nullptr) {
|
void PushBack(T&& item) {
|
||||||
litem = litem->next;
|
ListItem* new_item = new ListItem{
|
||||||
}
|
.item = glcr::Move(item),
|
||||||
litem->next = new_item;
|
.next = nullptr,
|
||||||
|
};
|
||||||
|
PushBackInternal(new_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
T PopFront() {
|
T PopFront() {
|
||||||
|
@ -36,12 +37,12 @@ class LinkedList {
|
||||||
|
|
||||||
ListItem* old_front = front_;
|
ListItem* old_front = front_;
|
||||||
front_ = front_->next;
|
front_ = front_->next;
|
||||||
T ret = old_front->item;
|
T ret = glcr::Move(old_front->item);
|
||||||
delete old_front;
|
delete old_front;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
T PeekFront() const { return front_->item; }
|
T& PeekFront() const { return front_->item; }
|
||||||
|
|
||||||
struct ListItem {
|
struct ListItem {
|
||||||
T item;
|
T item;
|
||||||
|
@ -73,6 +74,19 @@ class LinkedList {
|
||||||
uint64_t size_ = 0;
|
uint64_t size_ = 0;
|
||||||
|
|
||||||
ListItem* front_ = nullptr;
|
ListItem* front_ = nullptr;
|
||||||
|
|
||||||
|
void PushBackInternal(ListItem* new_item) {
|
||||||
|
size_++;
|
||||||
|
if (front_ == nullptr) {
|
||||||
|
front_ = new_item;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ListItem* litem = front_;
|
||||||
|
while (litem->next != nullptr) {
|
||||||
|
litem = litem->next;
|
||||||
|
}
|
||||||
|
litem->next = new_item;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace glcr
|
} // namespace glcr
|
||||||
|
|
|
@ -153,7 +153,7 @@ glcr::RefPtr<Port> pci1_port;
|
||||||
extern "C" void isr_pci1();
|
extern "C" void isr_pci1();
|
||||||
extern "C" void interrupt_pci1(InterruptFrame*) {
|
extern "C" void interrupt_pci1(InterruptFrame*) {
|
||||||
dbgln("Interrupt PCI line 1");
|
dbgln("Interrupt PCI line 1");
|
||||||
pci1_port->Send({}, {});
|
pci1_port->Send({});
|
||||||
gApic->SignalEOI();
|
gApic->SignalEOI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,41 +3,9 @@
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
|
|
||||||
glcr::ErrorCode UnboundedMessageQueue::PushBack(
|
glcr::ErrorCode UnboundedMessageQueue::PushBack(IpcMessage&& message) {
|
||||||
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 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.
|
|
||||||
msg_struct->reply_cap =
|
|
||||||
gScheduler->CurrentProcess().ReleaseCapability(reply_cap);
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
// places too).
|
|
||||||
auto cap = gScheduler->CurrentProcess().GetCapability(caps[i]);
|
|
||||||
if (!cap) {
|
|
||||||
return glcr::CAP_NOT_FOUND;
|
|
||||||
}
|
|
||||||
if (!cap->HasPermissions(kZionPerm_Transmit)) {
|
|
||||||
return glcr::CAP_PERMISSION_DENIED;
|
|
||||||
}
|
|
||||||
cap = gScheduler->CurrentProcess().ReleaseCapability(caps[i]);
|
|
||||||
msg_struct->caps.PushBack(cap);
|
|
||||||
}
|
|
||||||
|
|
||||||
MutexHolder h(mutex_);
|
MutexHolder h(mutex_);
|
||||||
pending_messages_.PushBack(msg_struct);
|
pending_messages_.PushBack(glcr::Move(message));
|
||||||
|
|
||||||
if (blocked_threads_.size() > 0) {
|
if (blocked_threads_.size() > 0) {
|
||||||
auto thread = blocked_threads_.PopFront();
|
auto thread = blocked_threads_.PopFront();
|
||||||
|
@ -47,10 +15,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();
|
||||||
|
@ -63,80 +29,45 @@ glcr::ErrorCode UnboundedMessageQueue::PopFront(uint64_t* num_bytes,
|
||||||
mutex_->Release();
|
mutex_->Release();
|
||||||
|
|
||||||
MutexHolder lock(mutex_);
|
MutexHolder lock(mutex_);
|
||||||
auto next_msg = pending_messages_.PeekFront();
|
auto& next_msg = pending_messages_.PeekFront();
|
||||||
if (next_msg->message.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->message.size();
|
|
||||||
|
|
||||||
for (uint64_t i = 0; i < *num_bytes; i++) {
|
|
||||||
static_cast<uint8_t*>(bytes)[i] = next_msg->message[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.PopFront());
|
|
||||||
}
|
|
||||||
return glcr::OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnboundedMessageQueue::WriteKernel(uint64_t init,
|
void UnboundedMessageQueue::WriteKernel(uint64_t init,
|
||||||
glcr::RefPtr<Capability> cap) {
|
glcr::RefPtr<Capability> cap) {
|
||||||
// FIXME: Add synchronization here in case it is ever used outside of init.
|
// FIXME: Add synchronization here in case it is ever used outside of init.
|
||||||
auto msg = glcr::MakeShared<Message>();
|
IpcMessage msg;
|
||||||
msg->message = glcr::Array<uint8_t>(sizeof(init));
|
msg.data = glcr::Array<uint8_t>(sizeof(init));
|
||||||
|
|
||||||
uint8_t* data = reinterpret_cast<uint8_t*>(&init);
|
uint8_t* data = reinterpret_cast<uint8_t*>(&init);
|
||||||
for (uint8_t i = 0; i < sizeof(init); i++) {
|
for (uint8_t i = 0; i < sizeof(init); i++) {
|
||||||
msg->message[i] = data[i];
|
msg.data[i] = data[i];
|
||||||
}
|
}
|
||||||
msg->caps.PushBack(cap);
|
msg.caps.PushBack(cap);
|
||||||
|
|
||||||
pending_messages_.PushBack(msg);
|
pending_messages_.PushBack(glcr::Move(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode SingleMessageQueue::PushBack(
|
glcr::ErrorCode SingleMessageQueue::PushBack(IpcMessage&& message) {
|
||||||
const glcr::ArrayView<uint8_t>& message,
|
if (message.reply_cap) {
|
||||||
const glcr::ArrayView<z_cap_t>& caps, z_cap_t reply_port) {
|
|
||||||
MutexHolder h(mutex_);
|
|
||||||
if (has_written_) {
|
|
||||||
return glcr::FAILED_PRECONDITION;
|
|
||||||
}
|
|
||||||
message_ = message;
|
|
||||||
|
|
||||||
if (reply_port != kZionInvalidCapability) {
|
|
||||||
dbgln("Sent a reply port to a single message queue");
|
dbgln("Sent a reply port to a single message queue");
|
||||||
return glcr::INTERNAL;
|
return glcr::INTERNAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint64_t i = 0; i < caps.size(); i++) {
|
MutexHolder h(mutex_);
|
||||||
// FIXME: This would feel safer closer to the relevant syscall.
|
if (has_written_) {
|
||||||
auto cap = gScheduler->CurrentProcess().GetCapability(caps[i]);
|
dbgln("Double write to reply port.");
|
||||||
if (!cap) {
|
return glcr::FAILED_PRECONDITION;
|
||||||
return glcr::CAP_NOT_FOUND;
|
|
||||||
}
|
|
||||||
if (!cap->HasPermissions(kZionPerm_Transmit)) {
|
|
||||||
return glcr::CAP_PERMISSION_DENIED;
|
|
||||||
}
|
|
||||||
cap = gScheduler->CurrentProcess().ReleaseCapability(caps[i]);
|
|
||||||
caps_.PushBack(cap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message_ = glcr::Move(message);
|
||||||
has_written_ = true;
|
has_written_ = true;
|
||||||
|
|
||||||
if (blocked_threads_.size() > 0) {
|
if (blocked_threads_.size() > 0) {
|
||||||
|
@ -148,9 +79,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();
|
||||||
|
@ -164,32 +94,18 @@ glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
|
||||||
|
|
||||||
MutexHolder lock(mutex_);
|
MutexHolder lock(mutex_);
|
||||||
if (has_read_) {
|
if (has_read_) {
|
||||||
|
dbgln("Double read from reply port.");
|
||||||
return glcr::FAILED_PRECONDITION;
|
return glcr::FAILED_PRECONDITION;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message_.size() > *num_bytes) {
|
if (message_.data.size() > data_buf_size) {
|
||||||
return glcr::BUFFER_SIZE;
|
return glcr::BUFFER_SIZE;
|
||||||
}
|
}
|
||||||
if (caps_.size() > *num_caps) {
|
if (message_.caps.size() > cap_buf_size) {
|
||||||
return glcr::BUFFER_SIZE;
|
return glcr::BUFFER_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
*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) {
|
|
||||||
dbgln("Tried to read a reply port a single message queue");
|
|
||||||
return glcr::INTERNAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*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;
|
has_read_ = true;
|
||||||
|
|
||||||
return glcr::OK;
|
return glcr::Move(message_);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,24 +4,31 @@
|
||||||
#include <glacier/container/array_view.h>
|
#include <glacier/container/array_view.h>
|
||||||
#include <glacier/container/intrusive_list.h>
|
#include <glacier/container/intrusive_list.h>
|
||||||
#include <glacier/container/linked_list.h>
|
#include <glacier/container/linked_list.h>
|
||||||
|
#include <glacier/container/vector.h>
|
||||||
#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"
|
||||||
#include "object/mutex.h"
|
#include "object/mutex.h"
|
||||||
|
|
||||||
|
struct IpcMessage {
|
||||||
|
glcr::Array<uint8_t> data;
|
||||||
|
|
||||||
|
glcr::Vector<glcr::RefPtr<Capability>> caps;
|
||||||
|
|
||||||
|
glcr::RefPtr<Capability> reply_cap;
|
||||||
|
};
|
||||||
|
|
||||||
class MessageQueue {
|
class MessageQueue {
|
||||||
public:
|
public:
|
||||||
virtual ~MessageQueue() {}
|
virtual ~MessageQueue() {}
|
||||||
|
|
||||||
virtual glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
|
virtual glcr::ErrorCode PushBack(IpcMessage&&) = 0;
|
||||||
const glcr::ArrayView<z_cap_t>& caps,
|
virtual glcr::ErrorOr<IpcMessage> PopFront(uint64_t data_buf_size,
|
||||||
z_cap_t reply_cap) = 0;
|
uint64_t cap_buf_size) = 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 bool empty() = 0;
|
virtual bool empty() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -38,11 +45,9 @@ class UnboundedMessageQueue : public MessageQueue {
|
||||||
UnboundedMessageQueue& operator=(const UnboundedMessageQueue&) = delete;
|
UnboundedMessageQueue& operator=(const UnboundedMessageQueue&) = delete;
|
||||||
virtual ~UnboundedMessageQueue() override {}
|
virtual ~UnboundedMessageQueue() override {}
|
||||||
|
|
||||||
glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
|
glcr::ErrorCode PushBack(IpcMessage&& message) override;
|
||||||
const glcr::ArrayView<z_cap_t>& caps,
|
glcr::ErrorOr<IpcMessage> PopFront(uint64_t data_buf_size,
|
||||||
z_cap_t reply_cap) override;
|
uint64_t cap_buf_size) override;
|
||||||
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
|
|
||||||
z_cap_t* caps, z_cap_t* reply_cap) override;
|
|
||||||
|
|
||||||
void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap);
|
void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap);
|
||||||
|
|
||||||
|
@ -52,14 +57,7 @@ class UnboundedMessageQueue : public MessageQueue {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Message {
|
glcr::LinkedList<IpcMessage> pending_messages_;
|
||||||
glcr::Array<uint8_t> message;
|
|
||||||
|
|
||||||
glcr::LinkedList<glcr::RefPtr<Capability>> caps;
|
|
||||||
glcr::RefPtr<Capability> reply_cap;
|
|
||||||
};
|
|
||||||
|
|
||||||
glcr::LinkedList<glcr::SharedPtr<Message>> pending_messages_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SingleMessageQueue : public MessageQueue {
|
class SingleMessageQueue : public MessageQueue {
|
||||||
|
@ -69,11 +67,9 @@ class SingleMessageQueue : public MessageQueue {
|
||||||
SingleMessageQueue(SingleMessageQueue&&) = delete;
|
SingleMessageQueue(SingleMessageQueue&&) = delete;
|
||||||
virtual ~SingleMessageQueue() override {}
|
virtual ~SingleMessageQueue() override {}
|
||||||
|
|
||||||
glcr::ErrorCode PushBack(const glcr::ArrayView<uint8_t>& message,
|
glcr::ErrorCode PushBack(IpcMessage&&) override;
|
||||||
const glcr::ArrayView<z_cap_t>& caps,
|
glcr::ErrorOr<IpcMessage> PopFront(uint64_t data_buf_size,
|
||||||
z_cap_t reply_cap) override;
|
uint64_t cap_buf_size) override;
|
||||||
glcr::ErrorCode PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
|
|
||||||
z_cap_t* caps, z_cap_t* reply_cap) override;
|
|
||||||
|
|
||||||
bool empty() override {
|
bool empty() override {
|
||||||
MutexHolder h(mutex_);
|
MutexHolder h(mutex_);
|
||||||
|
@ -83,6 +79,5 @@ class SingleMessageQueue : public MessageQueue {
|
||||||
private:
|
private:
|
||||||
bool has_written_ = false;
|
bool has_written_ = false;
|
||||||
bool has_read_ = false;
|
bool has_read_ = false;
|
||||||
glcr::Array<uint8_t> message_;
|
IpcMessage message_;
|
||||||
glcr::LinkedList<glcr::RefPtr<Capability>> caps_;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,26 +2,11 @@
|
||||||
|
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
|
|
||||||
glcr::ErrorCode IpcObject::Send(const glcr::ArrayView<uint8_t>& message,
|
glcr::ErrorCode IpcObject::Send(IpcMessage&& message) {
|
||||||
const glcr::ArrayView<z_cap_t>& caps) {
|
return GetSendMessageQueue().PushBack(glcr::Move(message));
|
||||||
return Send(message, caps, kZionInvalidCapability);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode IpcObject::Send(const glcr::ArrayView<uint8_t>& message,
|
glcr::ErrorOr<IpcMessage> IpcObject::Recv(uint64_t data_buf_size,
|
||||||
const glcr::ArrayView<z_cap_t>& caps,
|
uint64_t cap_buf_size) {
|
||||||
const z_cap_t reply_port) {
|
return GetRecvMessageQueue().PopFront(data_buf_size, cap_buf_size);
|
||||||
auto& message_queue = GetSendMessageQueue();
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,17 +11,10 @@ class IpcObject : public KernelObject {
|
||||||
IpcObject(){};
|
IpcObject(){};
|
||||||
virtual ~IpcObject() {}
|
virtual ~IpcObject() {}
|
||||||
|
|
||||||
virtual glcr::ErrorCode Send(const glcr::ArrayView<uint8_t>& message,
|
virtual glcr::ErrorCode Send(IpcMessage&& message) final;
|
||||||
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,
|
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(); }
|
||||||
|
|
||||||
|
|
|
@ -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,76 @@ 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::ErrorOr<IpcMessage> TranslateRequestToIpcMessage(const T& req) {
|
||||||
|
if (req.num_bytes > 0x1000) {
|
||||||
|
dbgln("Large message size unimplemented: %x", req.num_bytes);
|
||||||
|
return glcr::UNIMPLEMENTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
IpcMessage message;
|
||||||
|
message.data = Buffer(req.data, req.num_bytes);
|
||||||
|
|
||||||
|
glcr::ArrayView<const z_cap_t> caps(req.caps, req.num_caps);
|
||||||
|
|
||||||
|
message.caps.Resize(caps.size());
|
||||||
|
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
|
||||||
|
// places too).
|
||||||
|
auto cap = gScheduler->CurrentProcess().GetCapability(caps[i]);
|
||||||
|
if (!cap) {
|
||||||
|
return glcr::CAP_NOT_FOUND;
|
||||||
|
}
|
||||||
|
if (!cap->HasPermissions(kZionPerm_Transmit)) {
|
||||||
|
return glcr::CAP_PERMISSION_DENIED;
|
||||||
|
}
|
||||||
|
message.caps.PushBack(
|
||||||
|
gScheduler->CurrentProcess().ReleaseCapability(caps[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
@ -28,8 +99,8 @@ glcr::ErrorCode ChannelSend(ZChannelSendReq* req) {
|
||||||
RET_ERR(ValidateCapability<Channel>(chan_cap, kZionPerm_Write));
|
RET_ERR(ValidateCapability<Channel>(chan_cap, kZionPerm_Write));
|
||||||
|
|
||||||
auto chan = chan_cap->obj<Channel>();
|
auto chan = chan_cap->obj<Channel>();
|
||||||
return chan->Send(Buffer(req->data, req->num_bytes),
|
ASSIGN_OR_RETURN(IpcMessage message, TranslateRequestToIpcMessage(*req));
|
||||||
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
|
return chan->Send(glcr::Move(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode ChannelRecv(ZChannelRecvReq* req) {
|
glcr::ErrorCode ChannelRecv(ZChannelRecvReq* req) {
|
||||||
|
@ -38,7 +109,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) {
|
||||||
|
@ -54,8 +126,8 @@ glcr::ErrorCode PortSend(ZPortSendReq* req) {
|
||||||
RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Write));
|
RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Write));
|
||||||
|
|
||||||
auto port = port_cap->obj<Port>();
|
auto port = port_cap->obj<Port>();
|
||||||
return port->Send(Buffer(req->data, req->num_bytes),
|
ASSIGN_OR_RETURN(IpcMessage message, TranslateRequestToIpcMessage(*req));
|
||||||
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
|
return port->Send(glcr::Move(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode PortRecv(ZPortRecvReq* req) {
|
glcr::ErrorCode PortRecv(ZPortRecvReq* req) {
|
||||||
|
@ -64,7 +136,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 +151,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) {
|
||||||
|
@ -108,12 +182,11 @@ glcr::ErrorCode EndpointSend(ZEndpointSendReq* req) {
|
||||||
|
|
||||||
auto reply_port = ReplyPort::Create();
|
auto reply_port = ReplyPort::Create();
|
||||||
*req->reply_port_cap = proc.AddNewCapability(reply_port, kZionPerm_Read);
|
*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);
|
ASSIGN_OR_RETURN(IpcMessage message, TranslateRequestToIpcMessage(*req));
|
||||||
return endpoint->Send(
|
message.reply_cap = glcr::MakeRefCounted<Capability>(
|
||||||
Buffer(req->data, req->num_bytes),
|
reply_port, kZionPerm_Write | kZionPerm_Transmit);
|
||||||
glcr::ArrayView<z_cap_t>(const_cast<z_cap_t*>(req->caps), req->num_caps),
|
return endpoint->Send(glcr::Move(message));
|
||||||
reply_port_cap_to_send);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req) {
|
glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req) {
|
||||||
|
@ -123,13 +196,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) {
|
||||||
|
@ -138,8 +207,8 @@ glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* 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->Send(Buffer(req->data, req->num_bytes),
|
ASSIGN_OR_RETURN(IpcMessage message, TranslateRequestToIpcMessage(*req));
|
||||||
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
|
return reply_port->Send(glcr::Move(message));
|
||||||
}
|
}
|
||||||
glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req) {
|
glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req) {
|
||||||
auto& proc = gScheduler->CurrentProcess();
|
auto& proc = gScheduler->CurrentProcess();
|
||||||
|
@ -148,5 +217,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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue