From 16b9d4fd8b149df28edbf073557d4bb6e9f7ec8d Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sat, 23 Jan 2021 20:34:03 -0800 Subject: [PATCH] [kernel] Have process_start syscall take a list of handles This also prompted a change of the process initialization protocol to allow handles to get typed, and changing to marking them as just self/other handls. This also means exposing the object type enum to userspace. --- src/drivers/fb/main.cpp | 2 +- src/drivers/nulldrv/main.cpp | 1 - src/include/j6/init.h | 17 +++++++++++----- src/include/j6/object_types.inc | 12 +++++++++++ src/include/j6/types.h | 15 +++++--------- src/include/syscalls.inc | 2 +- src/kernel/objects/kobject.h | 14 +++---------- src/kernel/scheduler.cpp | 23 +++++++++++----------- src/kernel/syscalls/process.cpp | 12 ++++++++++- src/libraries/libc/arch/x86_64/init_libc.c | 5 +++-- 10 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 src/include/j6/object_types.inc diff --git a/src/drivers/fb/main.cpp b/src/drivers/fb/main.cpp index efedc50..d8e6eec 100644 --- a/src/drivers/fb/main.cpp +++ b/src/drivers/fb/main.cpp @@ -41,7 +41,7 @@ main(int argc, const char **argv) j6_init_framebuffer *fb = nullptr; for (unsigned i = 0; i < initc; ++i) { if (initv[i].type == j6_init_desc_framebuffer) { - fb = reinterpret_cast(initv[i].value); + fb = reinterpret_cast(initv[i].data); break; } } diff --git a/src/drivers/nulldrv/main.cpp b/src/drivers/nulldrv/main.cpp index d15a16a..52e3d18 100644 --- a/src/drivers/nulldrv/main.cpp +++ b/src/drivers/nulldrv/main.cpp @@ -15,7 +15,6 @@ extern j6_handle_t __handle_sys; j6_handle_t endp = j6_handle_invalid; extern "C" { - void _init_libc(j6_process_init *); int main(int, const char **); } diff --git a/src/include/j6/init.h b/src/include/j6/init.h index c53e049..b0dcfcd 100644 --- a/src/include/j6/init.h +++ b/src/include/j6/init.h @@ -3,18 +3,25 @@ /// Types used in process and thread initialization #include +#include "j6/types.h" enum j6_init_type { // `value` is a: - j6_init_handle_system, // Handle to the system - j6_init_handle_process, // Handle to this process - j6_init_handle_thread, // Handle to this thread - j6_init_handle_space, // Handle to this process' address space + j6_init_handle_self, // Handle to the system + j6_init_handle_other, // Handle to this process j6_init_desc_framebuffer // Pointer to a j6_init_framebuffer descriptor }; +struct j6_typed_handle { + enum j6_object_type type; + j6_handle_t handle; +}; + struct j6_init_value { enum j6_init_type type; - uint64_t value; + union { + struct j6_typed_handle handle; + void *data; + }; }; /// Structure defining a framebuffer. diff --git a/src/include/j6/object_types.inc b/src/include/j6/object_types.inc new file mode 100644 index 0000000..6d38d33 --- /dev/null +++ b/src/include/j6/object_types.inc @@ -0,0 +1,12 @@ +OBJECT_TYPE( none, 0x00 ) + +OBJECT_TYPE( system, 0x01 ) + +OBJECT_TYPE( event, 0x02 ) +OBJECT_TYPE( channel, 0x03 ) +OBJECT_TYPE( endpoint, 0x04 ) + +OBJECT_TYPE( vma, 0x05 ) + +OBJECT_TYPE( process, 0x06 ) +OBJECT_TYPE( thread, 0x07 ) diff --git a/src/include/j6/types.h b/src/include/j6/types.h index 587775e..6223bdb 100644 --- a/src/include/j6/types.h +++ b/src/include/j6/types.h @@ -36,15 +36,10 @@ typedef uint64_t j6_handle_t; #define j6_handle_id_mask 0xffffffffull #define j6_handle_invalid 0xffffffffull -/// A process' initial data structure for communicating with the system -struct j6_process_init -{ - j6_handle_t process; - j6_handle_t handles[3]; -}; +enum j6_object_type { +#define OBJECT_TYPE( name, val ) j6_object_type_ ## name = val, +#include "j6/object_types.inc" +#undef OBJECT_TYPE -/// A thread's initial data structure -struct j6_thread_init -{ - j6_handle_t thread; + j6_object_type_max }; diff --git a/src/include/syscalls.inc b/src/include/syscalls.inc index 80304ed..26314b0 100644 --- a/src/include/syscalls.inc +++ b/src/include/syscalls.inc @@ -9,7 +9,7 @@ SYSCALL(0x0a, object_signal, j6_handle_t, j6_signal_t) SYSCALL(0x0b, object_close, j6_handle_t) SYSCALL(0x10, process_create, j6_handle_t *) -SYSCALL(0x11, process_start, j6_handle_t *, uintptr_t) +SYSCALL(0x11, process_start, j6_handle_t, uintptr_t, j6_handle_t *, size_t) SYSCALL(0x11, process_kill, j6_handle_t) SYSCALL(0x17, process_exit, int32_t) diff --git a/src/kernel/objects/kobject.h b/src/kernel/objects/kobject.h index 63843fc..38e58be 100644 --- a/src/kernel/objects/kobject.h +++ b/src/kernel/objects/kobject.h @@ -16,17 +16,9 @@ public: /// Types of kernel objects. enum class type : uint16_t { - none, - system, - - event, - channel, - endpoint, - - vma, - - process, - thread, +#define OBJECT_TYPE( name, val ) name = val, +#include "j6/object_types.inc" +#undef OBJECT_TYPE max }; diff --git a/src/kernel/scheduler.cpp b/src/kernel/scheduler.cpp index 8757b73..66317da 100644 --- a/src/kernel/scheduler.cpp +++ b/src/kernel/scheduler.cpp @@ -119,27 +119,26 @@ load_process_image(const kernel::args::program *program) fb_desc->flags |= 1; j6_init_value *initv = push(tcb->rsp3); - initv->type = j6_init_handle_system; - initv->value = static_cast(proc.add_handle(&system::get())); + initv->type = j6_init_handle_other; + initv->handle.type = j6_object_type_system; + initv->handle.handle = proc.add_handle(&system::get()); initv = push(tcb->rsp3); - initv->type = j6_init_handle_process; - initv->value = static_cast(proc.self_handle()); + initv->type = j6_init_handle_self; + initv->handle.type = j6_object_type_process; + initv->handle.handle = proc.self_handle(); initv = push(tcb->rsp3); - initv->type = j6_init_handle_thread; - initv->value = static_cast(th.self_handle()); - - initv = push(tcb->rsp3); - initv->type = j6_init_handle_space; - //initv->value = static_cast(proc.add_handle(&space)); + initv->type = j6_init_handle_self; + initv->handle.type = j6_object_type_thread; + initv->handle.handle = th.self_handle(); initv = push(tcb->rsp3); initv->type = j6_init_desc_framebuffer; - initv->value = reinterpret_cast(fb_desc); + initv->data = fb_desc; uint64_t *initc = push(tcb->rsp3); - *initc = 5; + *initc = 4; char **argv0 = push(tcb->rsp3); *argv0 = message_arg; diff --git a/src/kernel/syscalls/process.cpp b/src/kernel/syscalls/process.cpp index 179c6b6..d984dd6 100644 --- a/src/kernel/syscalls/process.cpp +++ b/src/kernel/syscalls/process.cpp @@ -16,8 +16,18 @@ process_create(j6_handle_t *handle) } j6_status_t -process_start(j6_handle_t *handle, uintptr_t entrypoint) +process_start(j6_handle_t handle, uintptr_t entrypoint, j6_handle_t *handles, size_t handle_count) { + process &p = process::current(); + process *c = get_handle(handle); + if (handle_count && !handles) + return j6_err_invalid_arg; + + for (size_t i = 0; i < handle_count; ++i) { + kobject *o = p.lookup_handle(handles[i]); + if (o) c->add_handle(o); + } + return j6_err_nyi; } diff --git a/src/libraries/libc/arch/x86_64/init_libc.c b/src/libraries/libc/arch/x86_64/init_libc.c index 421e97d..466b97a 100644 --- a/src/libraries/libc/arch/x86_64/init_libc.c +++ b/src/libraries/libc/arch/x86_64/init_libc.c @@ -28,8 +28,9 @@ _init_libc(uint64_t *rsp) __initv = (struct j6_init_value *)rsp; for (unsigned i = 0; i < __initc; ++i) { - if (__initv[i].type == j6_init_handle_system) { - __handle_sys = __initv[i].value; + if (__initv[i].type == j6_init_handle_other && + __initv[i].handle.type == j6_object_type_system) { + __handle_sys = __initv[i].handle.handle; break; } }