kernel: remove support for sending kernel handles via port/channel

This commit is contained in:
2026-02-26 20:50:34 +00:00
parent b59d0d8948
commit e4de3af00d
17 changed files with 231 additions and 370 deletions

View File

@@ -12,7 +12,7 @@ static struct object_type channel_type = {
.ob_header_offset = offsetof(struct channel, c_base),
};
BTREE_DEFINE_SIMPLE_GET(struct kmsg, msgid_t, msg_node, msg_id, get_msg_with_id)
BTREE_DEFINE_SIMPLE_GET(struct msg, msgid_t, msg_node, msg_id, get_msg_with_id)
kern_status_t channel_type_init(void)
{
@@ -36,7 +36,7 @@ extern struct channel *channel_create(void)
return channel;
}
static bool try_enqueue(struct btree *tree, struct kmsg *msg)
static bool try_enqueue(struct btree *tree, struct msg *msg)
{
if (!tree->b_root) {
tree->b_root = &msg->msg_node;
@@ -46,8 +46,8 @@ static bool try_enqueue(struct btree *tree, struct kmsg *msg)
struct btree_node *cur = tree->b_root;
while (1) {
struct kmsg *cur_node
= BTREE_CONTAINER(struct kmsg, msg_node, cur);
struct msg *cur_node
= BTREE_CONTAINER(struct msg, msg_node, cur);
struct btree_node *next = NULL;
if (msg->msg_id > cur_node->msg_id) {
@@ -76,7 +76,7 @@ static bool try_enqueue(struct btree *tree, struct kmsg *msg)
}
static void kmsg_reply_error(
struct kmsg *msg,
struct msg *msg,
kern_status_t status,
unsigned long *lock_flags)
{
@@ -87,13 +87,13 @@ static void kmsg_reply_error(
spin_unlock_irqrestore(&msg->msg_lock, *lock_flags);
}
static struct kmsg *get_next_msg(
static struct msg *get_next_msg(
struct channel *channel,
unsigned long *lock_flags)
{
struct btree_node *cur = btree_first(&channel->c_msg);
while (cur) {
struct kmsg *msg = BTREE_CONTAINER(struct kmsg, msg_node, cur);
struct msg *msg = BTREE_CONTAINER(struct msg, msg_node, cur);
spin_lock_irqsave(&msg->msg_lock, lock_flags);
if (msg->msg_status == KMSG_WAIT_RECEIVE) {
msg->msg_status = KMSG_WAIT_REPLY;
@@ -110,7 +110,7 @@ static struct kmsg *get_next_msg(
extern kern_status_t channel_enqueue_msg(
struct channel *channel,
struct kmsg *msg)
struct msg *msg)
{
fill_random(&msg->msg_id, sizeof msg->msg_id);
while (!try_enqueue(&channel->c_msg, msg)) {
@@ -124,13 +124,14 @@ extern kern_status_t channel_enqueue_msg(
extern kern_status_t channel_recv_msg(
struct channel *channel,
struct msg *out_msg,
msgid_t *out_id,
struct iovec *out_data,
size_t out_data_count,
unsigned long *irq_flags)
{
struct wait_item waiter;
struct thread *self = current_thread();
struct kmsg *msg = NULL;
struct msg *msg = NULL;
unsigned long msg_lock_flags;
wait_item_init(&waiter, self);
@@ -160,12 +161,12 @@ extern kern_status_t channel_recv_msg(
kern_status_t status = vm_region_memmove_v(
dst,
0,
out_msg->msg_data,
out_msg->msg_data_count,
out_data,
out_data_count,
src,
0,
msg->msg_req.msg_data,
msg->msg_req.msg_data_count,
msg->msg_req_data,
msg->msg_req_data_count,
VM_REGION_COPY_ALL,
NULL);
vm_region_unlock_pair_irqrestore(src, dst, f);
@@ -175,18 +176,6 @@ extern kern_status_t channel_recv_msg(
return status;
}
status = handle_list_transfer(
receiver->t_handles,
out_msg->msg_handles,
out_msg->msg_handles_count,
sender->t_handles,
msg->msg_req.msg_handles,
msg->msg_req.msg_handles_count);
if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags);
return status;
}
*out_id = msg->msg_id;
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
@@ -197,11 +186,12 @@ extern kern_status_t channel_recv_msg(
extern kern_status_t channel_reply_msg(
struct channel *channel,
msgid_t id,
const struct msg *resp,
const struct iovec *resp_data,
size_t resp_data_count,
unsigned long *irq_flags)
{
unsigned long msg_lock_flags;
struct kmsg *msg = get_msg_with_id(&channel->c_msg, id);
struct msg *msg = get_msg_with_id(&channel->c_msg, id);
if (!msg) {
return KERN_INVALID_ARGUMENT;
}
@@ -226,12 +216,12 @@ extern kern_status_t channel_reply_msg(
kern_status_t status = vm_region_memmove_v(
dst,
0,
msg->msg_resp.msg_data,
msg->msg_resp.msg_data_count,
msg->msg_resp_data,
msg->msg_resp_data_count,
src,
0,
resp->msg_data,
resp->msg_data_count,
resp_data,
resp_data_count,
VM_REGION_COPY_ALL,
NULL);
vm_region_unlock_pair_irqrestore(src, dst, f);
@@ -241,18 +231,6 @@ extern kern_status_t channel_reply_msg(
return status;
}
status = handle_list_transfer(
receiver->t_handles,
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, &msg_lock_flags);
return status;
}
kmsg_reply_error(msg, KERN_OK, &msg_lock_flags);
return KERN_OK;
@@ -268,7 +246,7 @@ extern kern_status_t channel_read_msg(
size_t *nr_read)
{
unsigned long msg_lock_flags;
struct kmsg *msg = get_msg_with_id(&channel->c_msg, id);
struct msg *msg = get_msg_with_id(&channel->c_msg, id);
if (!msg) {
return KERN_INVALID_ARGUMENT;
}
@@ -292,8 +270,8 @@ extern kern_status_t channel_read_msg(
dest_iov_count,
src_region,
offset,
msg->msg_req.msg_data,
msg->msg_req.msg_data_count,
msg->msg_req_data,
msg->msg_req_data_count,
VM_REGION_COPY_ALL,
nr_read);
vm_region_unlock_pair_irqrestore(src_region, dest_region, f);
@@ -305,12 +283,45 @@ extern kern_status_t channel_read_msg(
extern kern_status_t channel_write_msg(
struct channel *channel,
msgid_t msg,
msgid_t id,
size_t offset,
struct vm_region *src_region,
const struct iovec *src_iov,
size_t src_iov_count,
size_t *nr_written)
{
return KERN_UNIMPLEMENTED;
unsigned long msg_lock_flags;
struct msg *msg = get_msg_with_id(&channel->c_msg, id);
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 vm_region *dest_region
= msg->msg_sender_thread->tr_parent->t_address_space;
unsigned long f;
vm_region_lock_pair_irqsave(src_region, dest_region, &f);
kern_status_t status = vm_region_memmove_v(
dest_region,
0,
msg->msg_resp_data,
msg->msg_resp_data_count,
src_region,
offset,
src_iov,
src_iov_count,
VM_REGION_COPY_ALL,
nr_written);
vm_region_unlock_pair_irqrestore(src_region, dest_region, f);
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
return status;
}

View File

@@ -191,123 +191,3 @@ struct handle *handle_table_get_handle(
return &tab->t_handles.t_handle_list[handle_index];
}
struct handle_list_iterator {
struct handle_list *it_list;
size_t it_list_count;
size_t it_list_ptr;
kern_handle_t *it_handles;
size_t it_nr_handles;
};
static void handle_list_iterator_begin(
struct handle_list_iterator *it,
struct handle_list *list,
size_t list_count)
{
memset(it, 0x0, sizeof *it);
it->it_list = list;
it->it_list_count = list_count;
while (it->it_list_ptr < list_count) {
if (list[it->it_list_ptr].l_nr_handles > 0) {
break;
}
it->it_list_ptr++;
}
if (it->it_list_ptr >= list_count) {
return;
}
it->it_handles = list[it->it_list_ptr].l_handles;
it->it_nr_handles = list[it->it_list_ptr].l_nr_handles;
}
static void handle_list_iterator_seek(
struct handle_list_iterator *it,
size_t nr_handles)
{
if (nr_handles > it->it_nr_handles) {
nr_handles = it->it_nr_handles;
}
if (nr_handles < it->it_nr_handles) {
it->it_handles += nr_handles;
it->it_nr_handles -= nr_handles;
return;
}
it->it_list_ptr++;
while (it->it_list_ptr < it->it_list_count) {
if (it->it_list[it->it_list_ptr].l_nr_handles > 0) {
break;
}
it->it_list_ptr++;
}
if (it->it_list_ptr >= it->it_list_count) {
return;
}
it->it_handles = it->it_list[it->it_list_ptr].l_handles;
it->it_nr_handles = it->it_list[it->it_list_ptr].l_nr_handles;
}
kern_status_t handle_list_transfer(
struct handle_table *dest_table,
struct handle_list *dest_list,
size_t dest_list_count,
struct handle_table *src_table,
const struct handle_list *src_list,
size_t src_list_count)
{
struct handle_list_iterator src, dest;
handle_list_iterator_begin(
&src,
(struct handle_list *)src_list,
src_list_count);
handle_list_iterator_begin(&dest, dest_list, dest_list_count);
while (src.it_nr_handles && dest.it_nr_handles) {
size_t to_copy = MIN(src.it_nr_handles, dest.it_nr_handles);
for (size_t i = 0; i < to_copy; i++) {
kern_handle_t handle_v = src.it_handles[i];
struct handle *handle
= handle_table_get_handle(src_table, handle_v);
if (!handle) {
return KERN_HANDLE_INVALID;
}
struct object *obj = object_ref(handle->h_object);
handle_flags_t flags = handle->h_flags;
handle_table_free_handle(src_table, handle_v);
struct handle *dest_slot = NULL;
kern_status_t status = handle_table_alloc_handle(
dest_table,
&dest_slot,
&handle_v);
if (status != KERN_OK) {
return status;
}
dest_slot->h_object = obj;
dest_slot->h_flags = flags;
object_add_handle(obj);
object_unref(obj);
dest.it_handles[i] = handle_v;
}
handle_list_iterator_seek(&src, to_copy);
handle_list_iterator_seek(&dest, to_copy);
}
return KERN_OK;
}

View File

@@ -20,7 +20,7 @@ struct port *port_cast(struct object *obj)
return PORT_CAST(obj);
}
static void wait_for_reply(struct kmsg *msg, unsigned long *lock_flags)
static void wait_for_reply(struct msg *msg, unsigned long *lock_flags)
{
struct wait_item waiter;
struct thread *self = current_thread();
@@ -78,8 +78,10 @@ kern_status_t port_disconnect(struct port *port)
kern_status_t port_send_msg(
struct port *port,
const struct msg *req,
struct msg *resp,
const struct iovec *req_data,
size_t req_data_count,
struct iovec *resp_data,
size_t resp_data_count,
unsigned long *lock_flags)
{
if (port->p_status != PORT_READY) {
@@ -87,13 +89,15 @@ kern_status_t port_send_msg(
}
struct thread *self = current_thread();
struct kmsg *msg = &self->tr_msg;
struct msg *msg = &self->tr_msg;
memset(msg, 0x0, sizeof *msg);
msg->msg_status = KMSG_WAIT_RECEIVE;
msg->msg_sender_thread = self;
msg->msg_sender_port = port;
msg->msg_req = *req;
msg->msg_resp = *resp;
msg->msg_req_data = req_data;
msg->msg_req_data_count = req_data_count;
msg->msg_resp_data = resp_data;
msg->msg_resp_data_count = resp_data_count;
unsigned long flags;
channel_lock_irqsave(port->p_remote, &flags);