2023-06-21 23:20:56 -07:00
|
|
|
#include "syscall/ipc.h"
|
|
|
|
|
|
|
|
#include "capability/capability.h"
|
|
|
|
#include "interrupt/interrupt.h"
|
|
|
|
#include "object/endpoint.h"
|
|
|
|
#include "object/reply_port.h"
|
|
|
|
#include "scheduler/scheduler.h"
|
|
|
|
|
2023-11-02 23:31:08 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
glcr::ArrayView<uint8_t> Buffer(const void* bytes, uint64_t num_bytes) {
|
|
|
|
return glcr::ArrayView(reinterpret_cast<uint8_t*>(const_cast<void*>(bytes)),
|
|
|
|
num_bytes);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode ChannelCreate(ZChannelCreateReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto chan_pair = Channel::CreateChannelPair();
|
2023-08-01 18:22:41 -07:00
|
|
|
*req->channel1 = proc.AddNewCapability(chan_pair.first());
|
|
|
|
*req->channel2 = proc.AddNewCapability(chan_pair.second());
|
2023-06-21 23:20:56 -07:00
|
|
|
return glcr::OK;
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode ChannelSend(ZChannelSendReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto chan_cap = proc.GetCapability(req->chan_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
RET_ERR(ValidateCapability<Channel>(chan_cap, kZionPerm_Write));
|
2023-06-21 23:20:56 -07:00
|
|
|
|
|
|
|
auto chan = chan_cap->obj<Channel>();
|
2023-11-02 23:31:08 -07:00
|
|
|
return chan->Send(Buffer(req->data, req->num_bytes),
|
|
|
|
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode ChannelRecv(ZChannelRecvReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto chan_cap = proc.GetCapability(req->chan_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
RET_ERR(ValidateCapability<Channel>(chan_cap, kZionPerm_Read));
|
2023-06-21 23:20:56 -07:00
|
|
|
|
|
|
|
auto chan = chan_cap->obj<Channel>();
|
2023-06-21 23:42:21 -07:00
|
|
|
return chan->Recv(req->num_bytes, req->data, req->num_caps, req->caps);
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode PortCreate(ZPortCreateReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port = glcr::MakeRefCounted<Port>();
|
2023-08-01 18:22:41 -07:00
|
|
|
*req->port_cap = proc.AddNewCapability(port);
|
2023-06-21 23:20:56 -07:00
|
|
|
return glcr::OK;
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode PortSend(ZPortSendReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port_cap = proc.GetCapability(req->port_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Write));
|
2023-06-21 23:20:56 -07:00
|
|
|
|
|
|
|
auto port = port_cap->obj<Port>();
|
2023-11-02 23:31:08 -07:00
|
|
|
return port->Send(Buffer(req->data, req->num_bytes),
|
|
|
|
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode PortRecv(ZPortRecvReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port_cap = proc.GetCapability(req->port_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Read));
|
2023-06-21 23:20:56 -07:00
|
|
|
|
|
|
|
auto port = port_cap->obj<Port>();
|
2023-06-21 23:42:21 -07:00
|
|
|
return port->Recv(req->num_bytes, req->data, req->num_caps, req->caps);
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode PortPoll(ZPortPollReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto port_cap = proc.GetCapability(req->port_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
RET_ERR(ValidateCapability<Port>(port_cap, kZionPerm_Read));
|
2023-06-21 23:20:56 -07:00
|
|
|
|
|
|
|
auto port = port_cap->obj<Port>();
|
|
|
|
// FIXME: Race condition here where this call could block if the last message
|
|
|
|
// is removed between this check and the port read.
|
|
|
|
if (!port->HasMessages()) {
|
|
|
|
return glcr::EMPTY;
|
|
|
|
}
|
2023-06-21 23:42:21 -07:00
|
|
|
return port->Recv(req->num_bytes, req->data, req->num_caps, req->caps);
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode IrqRegister(ZIrqRegisterReq* req) {
|
2023-06-21 23:20:56 -07:00
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
if (req->irq_num != Z_IRQ_PCI_BASE) {
|
|
|
|
// FIXME: Don't hardcode this nonsense.
|
|
|
|
return glcr::UNIMPLEMENTED;
|
|
|
|
}
|
|
|
|
glcr::RefPtr<Port> port = glcr::MakeRefCounted<Port>();
|
2023-08-01 18:22:41 -07:00
|
|
|
*req->port_cap = proc.AddNewCapability(port);
|
2023-06-21 23:20:56 -07:00
|
|
|
RegisterPciPort(port);
|
|
|
|
return glcr::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
glcr::ErrorCode EndpointCreate(ZEndpointCreateReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
2023-08-01 18:22:41 -07:00
|
|
|
*req->endpoint_cap = proc.AddNewCapability(Endpoint::Create());
|
2023-06-21 23:20:56 -07:00
|
|
|
return glcr::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
glcr::ErrorCode EndpointSend(ZEndpointSendReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
|
|
|
|
auto endpoint_cap = proc.GetCapability(req->endpoint_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
ValidateCapability<Endpoint>(endpoint_cap, kZionPerm_Write);
|
2023-06-21 23:20:56 -07:00
|
|
|
auto endpoint = endpoint_cap->obj<Endpoint>();
|
|
|
|
|
|
|
|
auto reply_port = ReplyPort::Create();
|
2023-08-01 18:22:41 -07:00
|
|
|
*req->reply_port_cap = proc.AddNewCapability(reply_port, kZionPerm_Read);
|
|
|
|
uint64_t reply_port_cap_to_send =
|
2023-08-01 18:43:48 -07:00
|
|
|
proc.AddNewCapability(reply_port, kZionPerm_Write | kZionPerm_Transmit);
|
2023-11-02 23:31:08 -07:00
|
|
|
return endpoint->Send(
|
|
|
|
Buffer(req->data, req->num_bytes),
|
|
|
|
glcr::ArrayView<z_cap_t>(const_cast<z_cap_t*>(req->caps), req->num_caps),
|
|
|
|
reply_port_cap_to_send);
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
glcr::ErrorCode EndpointRecv(ZEndpointRecvReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
|
|
|
|
auto endpoint_cap = proc.GetCapability(req->endpoint_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
ValidateCapability<Endpoint>(endpoint_cap, kZionPerm_Read);
|
2023-06-21 23:20:56 -07:00
|
|
|
auto endpoint = endpoint_cap->obj<Endpoint>();
|
|
|
|
|
2023-06-21 23:42:21 -07:00
|
|
|
uint64_t num_caps = 1;
|
2023-10-24 23:32:05 -07:00
|
|
|
RET_ERR(endpoint->Recv(req->num_bytes, req->data, req->num_caps, req->caps,
|
2023-06-21 23:42:21 -07:00
|
|
|
req->reply_port_cap));
|
|
|
|
if (num_caps != 1) {
|
|
|
|
return glcr::INTERNAL;
|
|
|
|
}
|
|
|
|
return glcr::OK;
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
glcr::ErrorCode ReplyPortSend(ZReplyPortSendReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
auto reply_port_cap = proc.GetCapability(req->reply_port_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
ValidateCapability<ReplyPort>(reply_port_cap, kZionPerm_Read);
|
2023-06-21 23:20:56 -07:00
|
|
|
auto reply_port = reply_port_cap->obj<ReplyPort>();
|
|
|
|
|
2023-11-02 23:31:08 -07:00
|
|
|
return reply_port->Send(Buffer(req->data, req->num_bytes),
|
|
|
|
glcr::ArrayView<z_cap_t>(req->caps, req->num_caps));
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|
|
|
|
glcr::ErrorCode ReplyPortRecv(ZReplyPortRecvReq* req) {
|
|
|
|
auto& proc = gScheduler->CurrentProcess();
|
|
|
|
|
|
|
|
auto reply_port_cap = proc.GetCapability(req->reply_port_cap);
|
2023-08-01 18:22:41 -07:00
|
|
|
ValidateCapability<ReplyPort>(reply_port_cap, kZionPerm_Read);
|
2023-06-21 23:20:56 -07:00
|
|
|
auto reply_port = reply_port_cap->obj<ReplyPort>();
|
|
|
|
|
2023-06-21 23:42:21 -07:00
|
|
|
return reply_port->Recv(req->num_bytes, req->data, req->num_caps, req->caps);
|
2023-06-21 23:20:56 -07:00
|
|
|
}
|