diff options
Diffstat (limited to 'kernel/ipc.c')
-rw-r--r-- | kernel/ipc.c | 121 |
1 files changed, 0 insertions, 121 deletions
diff --git a/kernel/ipc.c b/kernel/ipc.c deleted file mode 100644 index a3e1160..0000000 --- a/kernel/ipc.c +++ /dev/null @@ -1,121 +0,0 @@ -#include <assert.h> -#include <interrupts.h> -#include <ipc.h> -#include <math.h> -#include <stdbool.h> -#include <string.h> - -struct IpcEndpoint { - u8 in_use; - u32 pid; -}; - -struct IpcEndpoint ipc_endpoints[100]; - -bool ipc_register_endpoint(u32 endpoint) { - if (endpoint >= 100) { - return false; - } - if (ipc_endpoints[endpoint].in_use) { - return false; - } - ipc_endpoints[endpoint].in_use = 1; - ipc_endpoints[endpoint].pid = current_task->pid; - return true; -} - -bool ipc_endpoint_to_pid(u32 endpoint, u32 *pid) { - if (endpoint >= 100) { - return false; - } - if (!ipc_endpoints[endpoint].in_use) { - return false; - } - *pid = ipc_endpoints[endpoint].pid; - return true; -} - -int ipc_get_mailbox(u32 id, struct IpcMailbox **out) { - process_t *p; - if (!get_task_from_pid(id, &p)) { - return 0; - } - *out = &p->ipc_mailbox; - return 1; -} - -int ipc_has_data(process_t *p) { - if (!p) { - p = current_task; - } - struct IpcMailbox *handler = &p->ipc_mailbox; - u32 read_ptr = handler->read_ptr; - struct IpcMessage *ipc_message = &handler->data[read_ptr]; - return ipc_message->is_used; -} - -int ipc_read(u8 *buffer, u32 length, u32 *sender_pid) { - struct IpcMailbox *handler = ¤t_task->ipc_mailbox; - - u32 read_ptr = handler->read_ptr; - struct IpcMessage *ipc_message = &handler->data[read_ptr]; - for (;;) { - if (!ipc_message->is_used) { - if (current_task->is_interrupted) { - current_task->is_interrupted = 0; - current_task->is_halted = 0; - return 0; - } - current_task->is_halted = 1; - switch_task(); - continue; - } - break; - } - current_task->is_halted = 0; - disable_interrupts(); - ipc_message->is_used = 0; - // TODO: Verify sender_pid is a valid address - if (sender_pid) { - assert(mmu_is_valid_userpointer(sender_pid, sizeof(ipc_message->sender_pid))); - *sender_pid = ipc_message->sender_pid; - } - - u32 len = min(length, ipc_message->size); - memcpy(buffer, ipc_message->buffer, len); - - // Update read_ptr - read_ptr++; - if (read_ptr >= IPC_NUM_DATA) { - read_ptr = 0; - } - handler->read_ptr = read_ptr; - return len; -} - -int ipc_write_to_process(int pid, u8 *buffer, u32 length) { - struct IpcMailbox *handler; - assert(ipc_get_mailbox(pid, &handler)); - - u32 write_ptr = handler->write_ptr; - struct IpcMessage *ipc_message = &handler->data[write_ptr]; - ipc_message->is_used = 1; - u32 len = min(IPC_BUFFER_SIZE, length); - ipc_message->sender_pid = current_task->pid; - ipc_message->size = len; - memcpy(ipc_message->buffer, buffer, len); - - // Update write_ptr - write_ptr++; - if (write_ptr >= IPC_NUM_DATA) { - write_ptr = 0; - } - handler->write_ptr = write_ptr; - return len; -} - -int ipc_write(int endpoint, u8 *buffer, u32 length) { - u32 pid; - assert(ipc_endpoint_to_pid(endpoint, &pid)); - return ipc_write_to_process(pid, buffer, length); -} |