kernel: implement sending, receiving, and replying to message via port/channel
This commit is contained in:
@@ -18,6 +18,11 @@ kern_status_t channel_type_init(void)
|
||||
return object_type_register(&channel_type);
|
||||
}
|
||||
|
||||
struct channel *channel_cast(struct object *obj)
|
||||
{
|
||||
return CHANNEL_CAST(obj);
|
||||
}
|
||||
|
||||
extern struct channel *channel_create(void)
|
||||
{
|
||||
struct object *channel_object = object_create(&channel_type);
|
||||
@@ -69,26 +74,31 @@ static bool try_enqueue(struct btree *tree, struct kmsg *msg)
|
||||
return true;
|
||||
}
|
||||
|
||||
static void kmsg_reply_error(struct kmsg *msg, kern_status_t status)
|
||||
static void kmsg_reply_error(
|
||||
struct kmsg *msg,
|
||||
kern_status_t status,
|
||||
unsigned long *lock_flags)
|
||||
{
|
||||
msg->msg_status = KMSG_REPLY_SENT;
|
||||
msg->msg_status = status;
|
||||
msg->msg_result = status;
|
||||
thread_awaken(msg->msg_sender_thread);
|
||||
spin_unlock_irqrestore(&msg->msg_lock, *lock_flags);
|
||||
}
|
||||
|
||||
static struct kmsg *get_next_msg(struct channel *channel)
|
||||
static struct kmsg *get_next_msg(
|
||||
struct channel *channel,
|
||||
unsigned long *lock_flags)
|
||||
{
|
||||
unsigned long flags;
|
||||
struct btree_node *cur = btree_first(&channel->c_msg);
|
||||
while (cur) {
|
||||
struct kmsg *msg = BTREE_CONTAINER(struct kmsg, msg_node, cur);
|
||||
spin_lock_irqsave(&msg->msg_lock, &flags);
|
||||
spin_lock_irqsave(&msg->msg_lock, lock_flags);
|
||||
if (msg->msg_status == KMSG_WAIT_RECEIVE) {
|
||||
msg->msg_status = KMSG_WAIT_REPLY;
|
||||
return msg;
|
||||
}
|
||||
|
||||
spin_unlock_irqrestore(&msg->msg_lock, flags);
|
||||
spin_unlock_irqrestore(&msg->msg_lock, *lock_flags);
|
||||
cur = btree_next(cur);
|
||||
}
|
||||
|
||||
@@ -118,11 +128,12 @@ extern kern_status_t channel_recv_msg(
|
||||
struct wait_item waiter;
|
||||
struct thread *self = current_thread();
|
||||
struct kmsg *msg = NULL;
|
||||
unsigned long msg_lock_flags;
|
||||
|
||||
wait_item_init(&waiter, self);
|
||||
for (;;) {
|
||||
thread_wait_begin(&waiter, &channel->c_wq);
|
||||
msg = get_next_msg(channel);
|
||||
msg = get_next_msg(channel, &msg_lock_flags);
|
||||
if (msg) {
|
||||
break;
|
||||
}
|
||||
@@ -145,11 +156,11 @@ extern kern_status_t channel_recv_msg(
|
||||
out_msg->msg_data_count,
|
||||
sender->t_address_space,
|
||||
0,
|
||||
msg->msg_req->msg_data,
|
||||
msg->msg_req->msg_data_count,
|
||||
msg->msg_req.msg_data,
|
||||
msg->msg_req.msg_data_count,
|
||||
VM_REGION_COPY_ALL);
|
||||
if (status != KERN_OK) {
|
||||
kmsg_reply_error(msg, status);
|
||||
kmsg_reply_error(msg, status, &msg_lock_flags);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -158,14 +169,17 @@ extern kern_status_t channel_recv_msg(
|
||||
out_msg->msg_handles,
|
||||
out_msg->msg_handles_count,
|
||||
sender->t_handles,
|
||||
msg->msg_req->msg_handles,
|
||||
msg->msg_req->msg_handles_count);
|
||||
msg->msg_req.msg_handles,
|
||||
msg->msg_req.msg_handles_count);
|
||||
if (status != KERN_OK) {
|
||||
kmsg_reply_error(msg, status);
|
||||
kmsg_reply_error(msg, status, &msg_lock_flags);
|
||||
return status;
|
||||
}
|
||||
|
||||
kmsg_reply_error(msg, KERN_OK);
|
||||
*out_id = msg->msg_id;
|
||||
|
||||
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
@@ -175,46 +189,54 @@ extern kern_status_t channel_reply_msg(
|
||||
const struct msg *resp,
|
||||
unsigned long *irq_flags)
|
||||
{
|
||||
unsigned long msg_lock_flags;
|
||||
struct kmsg *msg = get_msg_with_id(&channel->c_msg, id);
|
||||
if (!msg || msg->msg_status != KMSG_WAIT_REPLY) {
|
||||
if (!msg) {
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
spin_lock_irqsave(&msg->msg_lock, &msg_lock_flags);
|
||||
if (msg->msg_status != KMSG_WAIT_REPLY) {
|
||||
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
struct thread *self = current_thread();
|
||||
struct task *sender = msg->msg_sender_thread->tr_parent;
|
||||
struct task *receiver = self->tr_parent;
|
||||
/* the task that is about to receive the response */
|
||||
struct task *receiver = msg->msg_sender_thread->tr_parent;
|
||||
/* the task that is about to send the response */
|
||||
struct task *sender = self->tr_parent;
|
||||
|
||||
kern_status_t status = vm_region_memmove_v(
|
||||
receiver->t_address_space,
|
||||
0,
|
||||
msg->msg_resp->msg_data,
|
||||
msg->msg_resp->msg_data_count,
|
||||
msg->msg_resp.msg_data,
|
||||
msg->msg_resp.msg_data_count,
|
||||
sender->t_address_space,
|
||||
0,
|
||||
resp->msg_data,
|
||||
resp->msg_data_count,
|
||||
VM_REGION_COPY_ALL);
|
||||
if (status != KERN_OK) {
|
||||
kmsg_reply_error(msg, status);
|
||||
kmsg_reply_error(msg, status, &msg_lock_flags);
|
||||
return status;
|
||||
}
|
||||
|
||||
status = handle_list_transfer(
|
||||
receiver->t_handles,
|
||||
msg->msg_resp->msg_handles,
|
||||
msg->msg_resp->msg_handles_count,
|
||||
msg->msg_resp.msg_handles,
|
||||
msg->msg_resp.msg_handles_count,
|
||||
sender->t_handles,
|
||||
resp->msg_handles,
|
||||
resp->msg_handles_count);
|
||||
if (status != KERN_OK) {
|
||||
kmsg_reply_error(msg, status);
|
||||
kmsg_reply_error(msg, status, &msg_lock_flags);
|
||||
return status;
|
||||
}
|
||||
|
||||
msg->msg_status = KERN_OK;
|
||||
msg->msg_status = KMSG_REPLY_SENT;
|
||||
kmsg_reply_error(msg, KERN_OK, &msg_lock_flags);
|
||||
|
||||
return KERN_UNIMPLEMENTED;
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
extern kern_status_t channel_read_msg(
|
||||
|
||||
Reference in New Issue
Block a user