2026-02-19 19:21:04 +00:00
|
|
|
#include <kernel/channel.h>
|
|
|
|
|
#include <kernel/port.h>
|
|
|
|
|
#include <kernel/util.h>
|
|
|
|
|
|
|
|
|
|
#define PORT_CAST(p) OBJECT_C_CAST(struct port, p_base, &port_type, p)
|
|
|
|
|
|
|
|
|
|
static struct object_type port_type = {
|
|
|
|
|
.ob_name = "port",
|
|
|
|
|
.ob_size = sizeof(struct port),
|
|
|
|
|
.ob_header_offset = offsetof(struct port, p_base),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
kern_status_t port_type_init(void)
|
|
|
|
|
{
|
|
|
|
|
return object_type_register(&port_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct port *port_cast(struct object *obj)
|
|
|
|
|
{
|
|
|
|
|
return PORT_CAST(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:50:34 +00:00
|
|
|
static void wait_for_reply(struct msg *msg, unsigned long *lock_flags)
|
2026-02-19 19:21:04 +00:00
|
|
|
{
|
2026-02-21 11:32:57 +00:00
|
|
|
struct wait_item waiter;
|
|
|
|
|
struct thread *self = current_thread();
|
|
|
|
|
|
|
|
|
|
wait_item_init(&waiter, self);
|
|
|
|
|
for (;;) {
|
|
|
|
|
self->tr_state = THREAD_SLEEPING;
|
|
|
|
|
if (msg->msg_status == KMSG_REPLY_SENT) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port_unlock_irqrestore(msg->msg_sender_port, *lock_flags);
|
|
|
|
|
schedule(SCHED_NORMAL);
|
|
|
|
|
port_lock_irqsave(msg->msg_sender_port, lock_flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self->tr_state = THREAD_READY;
|
2026-02-19 19:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct port *port_create(void)
|
|
|
|
|
{
|
|
|
|
|
struct object *port_object = object_create(&port_type);
|
|
|
|
|
if (!port_object) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct port *port = PORT_CAST(port_object);
|
|
|
|
|
|
|
|
|
|
port->p_status = PORT_OFFLINE;
|
|
|
|
|
|
|
|
|
|
return port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kern_status_t port_connect(struct port *port, struct channel *remote)
|
|
|
|
|
{
|
|
|
|
|
if (port->p_status != PORT_OFFLINE) {
|
|
|
|
|
return KERN_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->p_remote = remote;
|
|
|
|
|
port->p_status = PORT_READY;
|
|
|
|
|
return KERN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 11:32:57 +00:00
|
|
|
kern_status_t port_disconnect(struct port *port)
|
|
|
|
|
{
|
|
|
|
|
if (port->p_status != PORT_READY) {
|
|
|
|
|
return KERN_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->p_remote = NULL;
|
|
|
|
|
port->p_status = PORT_OFFLINE;
|
|
|
|
|
return KERN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 19:21:04 +00:00
|
|
|
kern_status_t port_send_msg(
|
|
|
|
|
struct port *port,
|
2026-03-01 19:10:01 +00:00
|
|
|
const kern_msg_t *in_msg,
|
|
|
|
|
kern_msg_t *out_reply,
|
2026-02-21 11:32:57 +00:00
|
|
|
unsigned long *lock_flags)
|
2026-02-19 19:21:04 +00:00
|
|
|
{
|
|
|
|
|
if (port->p_status != PORT_READY) {
|
|
|
|
|
return KERN_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct thread *self = current_thread();
|
2026-02-26 20:50:34 +00:00
|
|
|
struct msg *msg = &self->tr_msg;
|
2026-02-21 11:32:57 +00:00
|
|
|
memset(msg, 0x0, sizeof *msg);
|
|
|
|
|
msg->msg_status = KMSG_WAIT_RECEIVE;
|
2026-02-19 19:21:04 +00:00
|
|
|
msg->msg_sender_thread = self;
|
|
|
|
|
msg->msg_sender_port = port;
|
2026-03-01 19:10:01 +00:00
|
|
|
memcpy(&msg->msg_req, in_msg, sizeof msg->msg_req);
|
|
|
|
|
memcpy(&msg->msg_resp, out_reply, sizeof msg->msg_req);
|
2026-02-19 19:21:04 +00:00
|
|
|
|
|
|
|
|
unsigned long flags;
|
|
|
|
|
channel_lock_irqsave(port->p_remote, &flags);
|
2026-02-21 11:32:57 +00:00
|
|
|
port->p_status = PORT_SEND_BLOCKED;
|
2026-02-19 19:21:04 +00:00
|
|
|
channel_enqueue_msg(port->p_remote, msg);
|
|
|
|
|
channel_unlock_irqrestore(port->p_remote, flags);
|
|
|
|
|
|
2026-02-21 11:32:57 +00:00
|
|
|
wait_for_reply(msg, lock_flags);
|
2026-02-19 19:21:04 +00:00
|
|
|
|
2026-02-26 19:42:29 +00:00
|
|
|
channel_lock_irqsave(port->p_remote, &flags);
|
|
|
|
|
btree_delete(&port->p_remote->c_msg, &msg->msg_node);
|
|
|
|
|
channel_unlock_irqrestore(port->p_remote, flags);
|
|
|
|
|
|
2026-02-19 19:21:04 +00:00
|
|
|
return msg->msg_result;
|
|
|
|
|
}
|