From: Matthias Kruk Date: Sat, 14 Mar 2020 09:03:39 +0000 (+0900) Subject: kernel/core: Use IPC mailboxes in process and cxipc implementation X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=6208d99cc16617ae149ce75a7a8fc8e19c614bc4;p=corax kernel/core: Use IPC mailboxes in process and cxipc implementation --- diff --git a/kernel/Makefile b/kernel/Makefile index e14ae2c..b96b61f 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,7 +2,8 @@ OBJECTS = arch/arch.o core/core.o vga/vga.o kbd/kbd.o sched/sched.o OBJECTS += klibc/klibc.a # klibc needs to be last DEPS = arch core vga kbd klibc sched LDFLAGS = --oformat=elf32-i386 -b elf32-i386 -m elf_i386 -CFLAGS = -m32 -Wall -nostdlib -nodefaultlibs -nostartfiles -ffreestanding +CFLAGS = -m32 -Wall -nostdlib -nodefaultlibs -nostartfiles -ffreestanding -nostdinc \ + -fno-builtin -fno-builtin-memcpy LIBGCC = /usr/lib/gcc/i686-linux-gnu/8/libgcc.a PHONY = $(DEPS) clean OUTPUT = corax diff --git a/kernel/core/cxipc.c b/kernel/core/cxipc.c index 50de0d9..32d06c1 100644 --- a/kernel/core/cxipc.c +++ b/kernel/core/cxipc.c @@ -1,3 +1,21 @@ +/* + * This file is part of the Corax operating system. + * Copyright (C) 2016-2020 Matthias Kruk + * + * Corax is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Corax is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Corax. If not, see . + */ + #include #include #include @@ -48,23 +66,14 @@ int sys_cxsend(pid_t to, struct cxmsg *msg) kmsg->cm_len = CONFIG_IPC_MSGSIZE; } - /* place the message somewhere the destination process can find it */ + /* deliver the message to the destination process */ ret_val = process_inbox_put(dproc, kmsg); if(ret_val < 0) { - kfree(kmsg); - } else { - /* wait for destination process to pick up the message */ - -#if FEATURE(DEBUG_IPC) - dbg_printf("[%u] waiting for pid %u\n", process_get_id(sproc), to); -#endif /* FEATURE(DEBUG_IPC) */ + dbg_printf("[%u] Failed to deliver message to pid %u\n", + process_get_id(sproc), to); - process_wait(to); - -#if FEATURE(DEBUG_IPC) - dbg_printf("[%u] returned from process_wait()\n", process_get_id(sproc)); -#endif /* FEATURE(DEBUG_IPC) */ + kfree(kmsg); } } } @@ -75,34 +84,25 @@ int sys_cxsend(pid_t to, struct cxmsg *msg) int sys_cxrecv(pid_t from, struct cxmsg *msg) { process_t *cproc; + struct cxmsg *kmsg; int ret_val; -#if FEATURE(DEBUG_IPC) - dbg_printf("sys_cxrecv(0x%08x, %p)\n", from, msg); -#endif /* FEATURE(DEBUG_IPC) */ - - ret_val = -EFAULT; cproc = process_get_current(); - if(cproc) { - struct cxmsg *kmsg; - #if FEATURE(DEBUG_IPC) - dbg_printf("[%u] waiting for message\n", process_get_id(cproc)); + dbg_printf("sys_cxrecv(0x%08x, %p)\n", from, msg); + dbg_printf("[%u] waiting for message\n", process_get_id(cproc)); #endif /* FEATURE(DEBUG_IPC) */ - /* process_inbox_get() always returns a valid message (or waits forever) */ - kmsg = process_inbox_get(from); + ret_val = process_inbox_get(cproc, &kmsg); + if(!ret_val) { #if FEATURE(DEBUG_IPC) dbg_printf("[%u] got a message\n", process_get_id(cproc)); #endif /* FEATURE(DEBUG_IPC) */ process_memcpy_ktop(cproc, msg, kmsg, sizeof(*msg)); - /* signal source that their message was picked up */ - process_signal(kmsg->cm_src); - /* we don't need the kernel-space buffer anymore */ kfree(kmsg); diff --git a/kernel/core/process.c b/kernel/core/process.c index 72ac3e9..828819a 100644 --- a/kernel/core/process.c +++ b/kernel/core/process.c @@ -1,3 +1,21 @@ +/* + * This file is part of the Corax operating system. + * Copyright (C) 2016-2020 Matthias Kruk + * + * Corax is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Corax is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Corax. If not, see . + */ + #include #include #include @@ -12,7 +30,11 @@ #include #include "fd.h" #include "sched.h" +#include "mailbox.h" +/* + * FIXME: Use a semaphore to count process references + */ enum procstate { PROC_STATE_INIT = 0, PROC_STATE_READY, @@ -29,11 +51,7 @@ struct process { gid_t p_gid; spinlock_t p_lock; - - struct { - struct cxmsg *msg; - pid_t sender; - } p_inbox[32]; + struct mailbox p_mbox; enum procstate p_state; int p_status; @@ -518,56 +536,26 @@ int process_memcpy_ptop(process_t *dproc, void *dst, process_t *sproc, void *src int process_inbox_put(process_t *proc, struct cxmsg *msg) { - process_t *cproc; int ret_val; - int i; - - cproc = process_get_current(); - for(ret_val = -ENOMEM, i = 0; i < 32; i++) { - if(!(proc->p_inbox[i].sender)) { - proc->p_inbox[i].sender = cproc->p_id; - proc->p_inbox[i].msg = msg; + ret_val = -EINVAL; - process_signal(proc->p_id); - ret_val = 0; - break; - } + if(proc) { + ret_val = mailbox_put(&(proc->p_mbox), msg); } return(ret_val); } -struct cxmsg* process_inbox_get(pid_t from) +int process_inbox_get(process_t *proc, struct cxmsg **msg) { - process_t *proc; - void *ret_val; - - proc = process_get_current(); - - do { - int i; - - for(ret_val = NULL, i = 0; i < 32; i++) { - /* skip empty entries */ - if(!proc->p_inbox[i].sender) { - continue; - } - - if(proc->p_inbox[i].sender == from || from == PID_ANY) { - ret_val = proc->p_inbox[i].msg; - proc->p_inbox[i].msg = NULL; - proc->p_inbox[i].sender = 0; + int ret_val; - break; - } - } + if(!proc) { + proc = process_get_current(); + } - if(!ret_val) { - dbg_printf("[%u] waiting for pid 0x%x\n", process_get_id(proc), from); - process_wait(from); - } - } while(!ret_val); + ret_val = mailbox_get(&(proc->p_mbox), (void**)msg); return(ret_val); } diff --git a/kernel/include/process.h b/kernel/include/process.h index 4d9eef9..e2fda64 100644 --- a/kernel/include/process.h +++ b/kernel/include/process.h @@ -1,6 +1,6 @@ /* * This file is part of the Corax operating system. - * Copyright (C) 2016-2019 Matthias Kruk + * Copyright (C) 2016-2020 Matthias Kruk * * Corax is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -43,7 +43,7 @@ int process_memcpy_ktop(process_t*, void*, void*, u32_t); int process_memcpy_ptop(process_t*, void*, process_t*, void*, u32_t); int process_inbox_put(process_t*, struct cxmsg*); -struct cxmsg* process_inbox_get(pid_t); +int process_inbox_get(process_t*, struct cxmsg**); int process_wait(pid_t); int process_signal(pid_t);