From be691582ee12613278af24cb5a824eeb357f6324 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Mon, 29 Jul 2024 11:27:22 -0400 Subject: some work on compositor --- kernel/include/hilbert/kernel/application.hpp | 9 +++- kernel/include/hilbert/kernel/serial.hpp | 6 +++ kernel/include/hilbert/kernel/utility.hpp | 15 +++++- kernel/source/application.cpp | 15 ++++-- kernel/source/entry.cpp | 3 +- kernel/source/interrupts.cpp | 5 ++ kernel/source/syscall.cpp | 78 +++++++++++++++++++++++++-- 7 files changed, 119 insertions(+), 12 deletions(-) (limited to 'kernel') diff --git a/kernel/include/hilbert/kernel/application.hpp b/kernel/include/hilbert/kernel/application.hpp index a4b59ff..e7c7d2e 100644 --- a/kernel/include/hilbert/kernel/application.hpp +++ b/kernel/include/hilbert/kernel/application.hpp @@ -112,6 +112,8 @@ namespace hilbert::kernel::application { utility::list environment_variables; public: + utility::string name; + app_memory *memory; //set in get_framebuffer syscall @@ -121,13 +123,16 @@ namespace hilbert::kernel::application { uint64_t id; //this class takes ownership of memory - process(app_memory *memory); + process(app_memory *memory, const utility::string &name); ~process(); //arguments are utility::move'd void add_environment_variable( utility::string &&name, utility::string &&value); + //null if unset + utility::string *get_environment_variable(const utility::string &name); + void add_thread(thread *t); void notify_thread_ended(thread *t, int exit_code); void on_end_process(int exit_code); @@ -180,6 +185,8 @@ namespace hilbert::kernel::application { utility::maybe new_socket_stream_id; public: + utility::string name; + process *owner; //the cpu state is saved here when the thread is not running. diff --git a/kernel/include/hilbert/kernel/serial.hpp b/kernel/include/hilbert/kernel/serial.hpp index e7a44f2..7751fa0 100644 --- a/kernel/include/hilbert/kernel/serial.hpp +++ b/kernel/include/hilbert/kernel/serial.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace hilbert::kernel { @@ -15,6 +16,11 @@ namespace hilbert::kernel { } } + static inline void serial_putstr(const utility::string &str) { + for (unsigned i = 0; i < str.count; ++i) + serial_putchar(str.buffer[i]); + } + template static inline void serial_puthex(uint64_t n) { for (int d = digits - 1; d >= 0; --d) { diff --git a/kernel/include/hilbert/kernel/utility.hpp b/kernel/include/hilbert/kernel/utility.hpp index b7f1a1b..a21d3fe 100644 --- a/kernel/include/hilbert/kernel/utility.hpp +++ b/kernel/include/hilbert/kernel/utility.hpp @@ -107,6 +107,10 @@ namespace hilbert::kernel::utility { n->value = value; n->next = 0; n->prev = last; + if (last) + last->next = n; + else + first = n; last = n; } @@ -115,12 +119,19 @@ namespace hilbert::kernel::utility { n->value = value; n->next = 0; n->prev = last; + if (last) + last->next = n; + else + first = n; last = n; } void clear() { - for (node *n = first; n; n = n->next) - delete n; + if (first) { + for (node *n = first->next; n; n = n->next) + delete n->prev; + delete last; + } first = 0; last = 0; } diff --git a/kernel/source/application.cpp b/kernel/source/application.cpp index 8070019..6df49cd 100644 --- a/kernel/source/application.cpp +++ b/kernel/source/application.cpp @@ -83,7 +83,8 @@ namespace hilbert::kernel::application { resume_thread(t->saved_state); } - process::process(app_memory *memory) : memory(memory) {} + process::process(app_memory *memory, const utility::string &name) + : name(name), memory(memory) {} process::~process() { delete memory; //:p @@ -94,6 +95,14 @@ namespace hilbert::kernel::application { environment_variables.insert_end({.a = name, .b = value}); } + utility::string *process::get_environment_variable( + const utility::string &name) { + for (auto *i = environment_variables.first; i; i = i->next) + if (i->value.a == name) + return &i->value.b; + return 0; + } + void process::add_thread(thread *t) { threads.insert_end(t); } @@ -197,8 +206,8 @@ namespace hilbert::kernel::application { thread::thread(process *owner, uint64_t entry) : stack_top(owner->memory->map_new_stack()), waiting_for_socket_stream(0), - waiting_to_accept_from(0), waiting_to_connect_to(0), - waiting_for_input(false), owner(owner) { + waiting_to_accept_from(0), waiting_to_connect_to(0), waiting_for_input(false), + name(utility::string("main", 4)), owner(owner) { saved_state.rax = 0; saved_state.rbx = 0; diff --git a/kernel/source/entry.cpp b/kernel/source/entry.cpp index 2389bb1..efa70fd 100644 --- a/kernel/source/entry.cpp +++ b/kernel/source/entry.cpp @@ -214,7 +214,8 @@ extern "C" [[noreturn]] void entry() { if (load_init_result != load_app_result::success) panic(0xc39db3); - application::process *init_process = new application::process(init_memory); + application::process *init_process = + new application::process(init_memory, utility::string("init", 4)); init_process->add_environment_variable( utility::string("ARGC", 4), utility::string("1", 1)); init_process->add_environment_variable( diff --git a/kernel/source/interrupts.cpp b/kernel/source/interrupts.cpp index 41d632c..1a1f4d1 100644 --- a/kernel/source/interrupts.cpp +++ b/kernel/source/interrupts.cpp @@ -79,6 +79,11 @@ extern "C" [[noreturn]] void print_exception() { print_reg("r14", exception_info.r14); print_reg("r15", exception_info.r15); + if (application::running_thread != 0) { + serial_putstr("running app = "); + serial_putstr(application::running_thread->owner->name); + } + panic(0xba40bb); } diff --git a/kernel/source/syscall.cpp b/kernel/source/syscall.cpp index f2a6801..c631df1 100644 --- a/kernel/source/syscall.cpp +++ b/kernel/source/syscall.cpp @@ -183,7 +183,7 @@ namespace hilbert::kernel::syscall { .waiting_to_read = utility::queue(), .is_other_side_open = true, .other_process = p2, .other_end = 0 }; se2_out = new application::socket_stream_end { - .the_socket = s, .read_queue = s->queue_2, .write_queue = s->queue_2, + .the_socket = s, .read_queue = s->queue_2, .write_queue = s->queue_1, .waiting_to_read = utility::queue(), .is_other_side_open = true, .other_process = p1, .other_end = se1_out }; se1_out->other_end = se2_out; @@ -494,8 +494,12 @@ namespace hilbert::kernel::syscall { rax = (uint64_t)application::stream_result::other_end_closed; return; } - for (uint64_t i = 0; i < count; ++i) + auto &wtr_queue = ss->other_end->waiting_to_read; + for (uint64_t i = 0; i < count; ++i) { ss->write_queue.insert(buffer[i]); + if (wtr_queue.count > 0) + application::paused_threads->insert(wtr_queue.take()); + } rax = (uint64_t)application::stream_result::success; } @@ -617,7 +621,8 @@ namespace hilbert::kernel::syscall { break; } - application::process *p = new application::process(memory); + application::process *p = + new application::process(memory, file.dir_entry.name); for (uint64_t i = 0; i < psi->env_var_count; ++i) p->add_environment_variable( @@ -735,6 +740,66 @@ namespace hilbert::kernel::syscall { } + void get_environment_variable_length_syscall( + uint64_t &rax, uint64_t &rdi, uint64_t &rsi, uint64_t &rdx) { + + const char *name = (const char *)rdi; + uint64_t name_len = rsi; + set_zero(rax, rdi, rsi, rdx); + + auto *app = application::running_thread->owner; + + if (!app->memory->valid_to_read(name, name + name_len, false)) + return; + + utility::string name_string(name, name_len); + utility::string *value = app->get_environment_variable(name_string); + rax = value == 0 ? (uint64_t)-1 : value->count; + + } + + void get_environment_variable_value_syscall( + uint64_t &rax, uint64_t &rdi, uint64_t &rsi, uint64_t &rdx) { + + const char *name = (const char *)rdi; + uint64_t name_len = rsi; + char *buffer = (char *)rdx; + set_zero(rax, rdi, rsi, rdx); + + auto *app = application::running_thread->owner; + + if (!app->memory->valid_to_read(name, name + name_len, false)) + return; + + utility::string name_string(name, name_len); + utility::string *value = app->get_environment_variable(name_string); + if (value == 0) + return; + + if (!app->memory->valid_to_read(buffer, buffer + value->count, true)) + return; + + for (unsigned i = 0; i < value->count; ++i) + buffer[i] = value->buffer[i]; + + } + + void set_thread_name_syscall( + uint64_t &rax, uint64_t &rdi, uint64_t &rsi, uint64_t &rdx) { + + const char *name = (const char *)rdi; + uint64_t name_len = rsi; + set_zero(rax, rdi, rsi, rdx); + + auto *t = application::running_thread; + + if (!t->owner->memory->valid_to_read(name, name + name_len, false)) + return; + + t->name = utility::string(name, name_len); + + } + void (*handlers[])( uint64_t &rax, uint64_t &rdi, uint64_t &rsi, uint64_t &rdx) = { @@ -759,11 +824,14 @@ namespace hilbert::kernel::syscall { &set_stream_length_syscall, &get_other_end_process_handle_syscall, &start_thread_syscall, - &clear_socket_read_queue_syscall + &clear_socket_read_queue_syscall, + &get_environment_variable_length_syscall, + &get_environment_variable_value_syscall, + &set_thread_name_syscall }; - static constexpr int max_syscall_number = 20; + static constexpr int max_syscall_number = 24; } -- cgit v1.2.3