From b1a912a8a6ff472a49b2e0a09cfd433adfc2cb24 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 18 May 2024 21:53:38 -0400 Subject: reorganization, cross compiler --- kernel/include/hilbert/kernel/application.hpp | 193 ++++++++++++++++++++++---- kernel/include/hilbert/kernel/framebuffer.hpp | 10 +- kernel/include/hilbert/kernel/input.hpp | 2 + kernel/include/hilbert/kernel/panic.hpp | 3 +- kernel/include/hilbert/kernel/terminal.hpp | 32 ----- 5 files changed, 172 insertions(+), 68 deletions(-) delete mode 100644 kernel/include/hilbert/kernel/terminal.hpp (limited to 'kernel/include/hilbert') diff --git a/kernel/include/hilbert/kernel/application.hpp b/kernel/include/hilbert/kernel/application.hpp index 51f304d..7d48d8d 100644 --- a/kernel/include/hilbert/kernel/application.hpp +++ b/kernel/include/hilbert/kernel/application.hpp @@ -7,21 +7,165 @@ namespace hilbert::kernel::application { - void init_syscalls(); + class process; + class thread; - enum class app_state { + enum class thread_state { running, paused, - zombie + waiting }; - struct app_instance { + enum class stream_result { + success, + bad_handle, + io_error, + out_of_bounds, + does_not_exist, + not_a_regular_file, + not_an_executable, + not_writable, + not_seekable, + socket_id_already_used, + socket_id_not_in_use, + socket_listener_closed, + other_end_closed, + already_exists, + not_sized + }; + + enum class seek_origin { + beginning, + end, + current_position + }; + + class stream { + public: + virtual ~stream() {} + virtual stream_result seek(seek_origin origin, int64_t offset) = 0; + virtual stream_result read(uint64_t count, void *into) = 0; + virtual stream_result write(uint64_t count, const void *from) = 0; + virtual stream_result get_length(uint64_t &out) = 0; + virtual stream_result set_length(uint64_t to) = 0; + }; + + class vfile_stream : public stream { - utility::id_allocator open_files; + private: + vfile::vfile file; + uint64_t offset; - vfile::vfile working_dir; + public: + vfile_stream(vfile::vfile &&file); + virtual stream_result seek(seek_origin origin, int64_t offset) override; + virtual stream_result read(uint64_t count, void *into) override; + virtual stream_result write(uint64_t count, const void *from) override; + virtual stream_result get_length(uint64_t &out) override; + virtual stream_result set_length(uint64_t to) override; - app_state state; + }; + + struct socket { + utility::queue process_a_threads_waiting_to_read; + utility::queue process_b_threads_waiting_to_read; + utility::queue a_to_b; + utility::queue b_to_a; + bool a_closed; + bool b_closed; + }; + + class socket_stream : public stream { + + private: + socket *sock; + bool are_we_b; + + utility::queue &our_threads_waiting_to_read; + utility::queue &their_threads_waiting_to_read; + utility::queue &them_to_us; + utility::queue &us_to_them; + bool &them_closed; + bool &us_closed; + + public: + socket_stream(socket *sock, bool are_we_b); + ~socket_stream(); + virtual stream_result seek(seek_origin origin, int64_t offset) override; + virtual stream_result read(uint64_t count, void *into) override; + virtual stream_result write(uint64_t count, const void *from) override; + virtual stream_result get_length(uint64_t &out) override; + virtual stream_result set_length(uint64_t to) override; + + }; + + struct string_pair { + utility::string a; + utility::string b; + }; + + struct socket_listener { + utility::string id; + utility::queue waiting_to_accept_connection; + utility::queue waiting_to_connect; + bool is_listening; + }; + + struct [[gnu::packed]] cpu_state { + + uint64_t rax; + uint64_t rbx; + uint64_t rcx; + uint64_t rdx; + uint64_t rdi; + uint64_t rsi; + uint64_t rbp; + uint64_t rsp; + uint64_t r8; + uint64_t r9; + uint64_t r10; + uint64_t r11; + uint64_t r12; + uint64_t r13; + uint64_t r14; + uint64_t r15; + + uint64_t rflags; + uint64_t rip; + uint64_t cr3; + + //only used if in_syscall is true. starts at rsp. no red zone + //needs to be saved since save_thread_state doesn't use it. + void *kernel_stack_copy; + bool in_syscall; + + }; + + struct thread { + + //propogated to process on destruction if this is the last thread. + int exit_code; + ~thread(); + + process *the_process; + thread_state state; + //only valid if paused or waiting + cpu_state cpu; + + stream *just_connected_to; + stream *just_accepted; + + }; + + struct process { + + void end_process(unsigned exit_code); + void cleanup(); + + utility::list threads; + utility::vector environment; + utility::id_allocator open_streams; + utility::id_allocator socket_listeners; uint64_t *p4; uint64_t *p3; @@ -35,17 +179,10 @@ namespace hilbert::kernel::application { //set to 0 if none uint64_t framebuffer_vaddr; - //only valid if state is zombie + //only valid if there are no threads int32_t exit_code; - //only valid if state is paused - struct { - uint64_t rip; - uint64_t rsp; - //TODO: etc. - } saved_regs; - - app_instance(); + process(); //vaddr and paddr must be aligned, and vaddr must be < 0x0080.0000.0000 void map_page(uint64_t vaddr, uint64_t paddr, @@ -62,16 +199,22 @@ namespace hilbert::kernel::application { }; - extern app_instance *running_app; + extern utility::id_allocator *processes; + extern utility::queue *paused_threads; + extern utility::queue *threads_waiting_for_input; + extern thread *running_thread; + extern utility::list *all_socket_listeners; - enum class create_app_result { - success, - device_error, - app_corrupt, - fs_corrupt - }; + stream_result create_application( + const vfile::vfile &file, process *&process_out, thread *&thread_out); + + void init_applications(); + + //returns true when resumed, false right now. + //must be called from non-interruptable syscall context. + extern "C" bool save_thread_state(cpu_state &into); - create_app_result create_app(const vfile::vfile &file, - app_instance *&out, const vfile::vfile &working_dir); + //must be called from non-interruptable context + [[noreturn]] void resume_next(); } diff --git a/kernel/include/hilbert/kernel/framebuffer.hpp b/kernel/include/hilbert/kernel/framebuffer.hpp index fb28462..d4d6b1c 100644 --- a/kernel/include/hilbert/kernel/framebuffer.hpp +++ b/kernel/include/hilbert/kernel/framebuffer.hpp @@ -18,14 +18,6 @@ namespace hilbert::kernel::framebuffer { void set_pixel(int x, int y, color c); - //[from_start_x, from_end_x) x [from_start_y, from_end_y) - // -> [to_start_x, ...) x [to_start_y, ...). - //we assume from_start_x < from_end_x and from_start_y < from_end_y. - void move_region( - int from_start_x, int from_start_y, int from_end_x, - int from_end_y, int to_start_x, int to_start_y); - - //[start_x, end_x) x [start_y, end_y) - void fill_region(int start_x, int start_y, int end_x, int end_y, color c); + void fill_color(color c); } diff --git a/kernel/include/hilbert/kernel/input.hpp b/kernel/include/hilbert/kernel/input.hpp index cccb71f..2209ddc 100644 --- a/kernel/include/hilbert/kernel/input.hpp +++ b/kernel/include/hilbert/kernel/input.hpp @@ -19,6 +19,8 @@ namespace hilbert::kernel::input { }; extern utility::queue *key_queue; + //notify a process waiting for input + void got_input(); //must be post switch to kernel page tables and mounting of file systems void init_input(); diff --git a/kernel/include/hilbert/kernel/panic.hpp b/kernel/include/hilbert/kernel/panic.hpp index 545d703..0478142 100644 --- a/kernel/include/hilbert/kernel/panic.hpp +++ b/kernel/include/hilbert/kernel/panic.hpp @@ -1,6 +1,5 @@ #pragma once namespace hilbert::kernel { - //prints to terminal and then halts. - [[noreturn]] void panic(const char *string_sz); + [[noreturn]] void panic(uint32_t code); } diff --git a/kernel/include/hilbert/kernel/terminal.hpp b/kernel/include/hilbert/kernel/terminal.hpp deleted file mode 100644 index 350d79b..0000000 --- a/kernel/include/hilbert/kernel/terminal.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace hilbert::kernel::terminal { - - extern uint8_t *termfont; - extern uint64_t termfont_len; - - void init_terminal(); - - extern int width; - extern int height; - - extern int cursor_x; - extern int cursor_y; - - extern framebuffer::color bg_color; - extern framebuffer::color fg_color; - - void put_char(char ch); - void put_string(const utility::string &str); - void put_string_sz(const char *str); - - void put_int_decimal(uint64_t n, bool with_commas = true); - - void put_int_hex(uint64_t n, int digits, bool with_dots = true); - -} -- cgit v1.2.3