2023-06-12 19:20:51 -07:00
|
|
|
#include "object/port.h"
|
|
|
|
|
2023-06-12 20:56:25 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
|
|
|
|
2023-06-12 19:20:51 -07:00
|
|
|
Port::Port() {}
|
|
|
|
|
|
|
|
z_err_t Port::Write(const ZMessage& msg) {
|
|
|
|
if (msg.num_bytes > 0x1000) {
|
|
|
|
dbgln("Large message size unimplemented: %x", msg.num_bytes);
|
|
|
|
return Z_ERR_INVALID;
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:58:50 -07:00
|
|
|
auto message = MakeShared<Message>();
|
2023-06-17 02:01:21 -07:00
|
|
|
message->num_bytes = msg.num_bytes;
|
2023-06-16 15:58:50 -07:00
|
|
|
message->bytes = new uint8_t[msg.num_bytes];
|
2023-06-12 19:20:51 -07:00
|
|
|
for (uint64_t i = 0; i < msg.num_bytes; i++) {
|
2023-06-19 18:39:46 -07:00
|
|
|
message->bytes[i] = static_cast<uint8_t*>(msg.data)[i];
|
2023-06-16 15:58:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < msg.num_caps; i++) {
|
|
|
|
auto cap = gScheduler->CurrentProcess().ReleaseCapability(msg.caps[i]);
|
|
|
|
if (!cap) {
|
|
|
|
return Z_ERR_CAP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
message->caps.PushBack(cap);
|
2023-06-12 19:20:51 -07:00
|
|
|
}
|
2023-06-12 20:56:25 -07:00
|
|
|
|
|
|
|
MutexHolder lock(mutex_);
|
2023-06-12 19:20:51 -07:00
|
|
|
pending_messages_.PushBack(message);
|
2023-06-12 20:56:25 -07:00
|
|
|
if (blocked_threads_.size() > 0) {
|
2023-06-16 01:29:00 -07:00
|
|
|
auto thread = blocked_threads_.PopFront();
|
|
|
|
thread->SetState(Thread::RUNNABLE);
|
|
|
|
gScheduler->Enqueue(thread);
|
2023-06-12 20:56:25 -07:00
|
|
|
}
|
2023-06-12 19:20:51 -07:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
z_err_t Port::Read(ZMessage& msg) {
|
2023-06-12 20:56:25 -07:00
|
|
|
mutex_.Lock();
|
|
|
|
while (pending_messages_.size() < 1) {
|
|
|
|
blocked_threads_.PushBack(gScheduler->CurrentThread());
|
|
|
|
mutex_.Unlock();
|
|
|
|
gScheduler->Yield();
|
|
|
|
mutex_.Lock();
|
2023-06-12 19:20:51 -07:00
|
|
|
}
|
2023-06-12 20:56:25 -07:00
|
|
|
mutex_.Unlock();
|
2023-06-12 19:20:51 -07:00
|
|
|
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-16 15:58:50 -07:00
|
|
|
auto next_msg = pending_messages_.PeekFront();
|
|
|
|
if (next_msg->num_bytes > msg.num_bytes) {
|
|
|
|
return Z_ERR_BUFF_SIZE;
|
|
|
|
}
|
|
|
|
if (next_msg->caps.size() > msg.num_caps) {
|
2023-06-12 19:20:51 -07:00
|
|
|
return Z_ERR_BUFF_SIZE;
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:58:50 -07:00
|
|
|
msg.num_bytes = next_msg->num_bytes;
|
2023-06-12 19:20:51 -07:00
|
|
|
|
|
|
|
for (uint64_t i = 0; i < msg.num_bytes; i++) {
|
2023-06-19 18:39:46 -07:00
|
|
|
static_cast<uint8_t*>(msg.data)[i] = next_msg->bytes[i];
|
2023-06-16 15:58:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.num_caps = next_msg->caps.size();
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
for (uint64_t i = 0; i < msg.num_caps; i++) {
|
|
|
|
msg.caps[i] = proc.AddExistingCapability(next_msg->caps.PopFront());
|
2023-06-12 19:20:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pending_messages_.PopFront();
|
|
|
|
|
|
|
|
return Z_OK;
|
|
|
|
}
|
2023-06-16 23:15:28 -07:00
|
|
|
|
2023-06-17 00:07:58 -07:00
|
|
|
void Port::WriteKernel(uint64_t init, RefPtr<Capability> cap) {
|
|
|
|
MutexHolder h(mutex_);
|
|
|
|
|
|
|
|
auto msg = 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);
|
|
|
|
}
|
|
|
|
|
2023-06-16 23:15:28 -07:00
|
|
|
bool Port::HasMessages() {
|
|
|
|
MutexHolder h(mutex_);
|
|
|
|
return pending_messages_.size() != 0;
|
|
|
|
}
|