diff --git a/kernel/inc/proc/process.h b/kernel/inc/proc/process.h index 79c6ac5..c1b0943 100644 --- a/kernel/inc/proc/process.h +++ b/kernel/inc/proc/process.h @@ -26,6 +26,8 @@ #include "utils/stdtypes.h" #include "utils/string.h" +#include "mm/page_map.h" +#include "drivers/elf/elf.h" typedef uint32_t pid_t; @@ -35,23 +37,27 @@ typedef enum { } processes_standard_E; typedef struct process_T process_T; -struct process_T{ - char name [128]; - pid_t id; - void* chunk; - uint32_t chunk_id; +struct process_T { + char name [128]; + pid_t id; + void* chunk; + uint32_t chunk_id; - uint32_t num_threads; - void* threads; + page_map_T* page_map; + elf_executable_T* executable; - process_T* parent; - process_T* childs; - process_T* prev; - process_T* next; + uint32_t num_threads; + void* threads; + + process_T* parent; + process_T* childs; + process_T* prev; + process_T* next; }; -pid_t process_spawn (pid_t parent, string_t name); -void process_kill_pid (pid_t pid); -void process_kill (process_T* process); +void process_kernel_spawn (elf_executable_T* executable); +pid_t process_spawn (pid_t parent, string_t name, elf_executable_T* executable, void* buffer); +void process_kill_pid (pid_t pid); +void process_kill (process_T* process); #endif //NOX_PROCESS_H diff --git a/kernel/inc/proc/scheduler.h b/kernel/inc/proc/scheduler.h index 372ef87..64f9213 100644 --- a/kernel/inc/proc/scheduler.h +++ b/kernel/inc/proc/scheduler.h @@ -27,6 +27,7 @@ #include "proc/thread.h" #include "proc/process.h" #include "utils/bitmap.h" +#include "boot/boot_info.h" #define SCHEDULER_PROCESS_CHUNK_SIZE 64 @@ -51,7 +52,7 @@ typedef struct { bool initialized; } scheduler_T; -void scheduler_init (); +void scheduler_init (boot_info_T* boot_info); cpu_state_T* scheduler_start (cpu_state_T* state); bool scheduler_is_initialized (); void scheduler_dump_info (process_T* process, uint8_t indent); diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index d65a4c3..3ca6eca 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -33,8 +33,6 @@ #include "drivers/fs/vfs.h" #include "proc/scheduler.h" -#include "drivers/elf/elf.h" - void limine_terminal_print(boot_info_T* boot_info, string_t string) { boot_info->terminal->write(boot_info->terminal->terminals[0], string, string_length(string)); } @@ -74,27 +72,13 @@ void kernel_init(boot_info_T* boot_info) { limine_terminal_print(boot_info, " ok\n"); limine_terminal_print(boot_info, " Initializing scheduler..."); - scheduler_init(); + scheduler_init(boot_info); limine_terminal_print(boot_info, " ok\n"); } -#include "utils/io.h" - -void test1() { - while (true) { - io_out_byte(LOG_PORT, 'A'); - } -} - -void test2() { - while (true) { - io_out_byte(LOG_PORT, 'B'); - } -} - -void test3() { - while (true) { - io_out_byte(LOG_PORT, 'C'); +void test() { + while(1) { + int x = 0 / 0; } } @@ -108,20 +92,23 @@ void kmain(boot_info_T boot_info) { limine_terminal_print(&boot_info, "Kernel initialized\n"); log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n"); - elf_init_kernel_exec(&boot_info); - pid_t proc1 = process_spawn(PROCESS_KERNEL, "test_1"); +// pid_t proc1 = process_spawn(PROCESS_KERNEL, "test_1"); +// +// pid_t proc1_1 = process_spawn(proc1, "test_1-1"); +// thread_spawn(proc1_1, NULL); +// pid_t proc1_2 = process_spawn(proc1, "test_1-2"); +// +// pid_t proc2 = process_spawn(PROCESS_KERNEL, "test_2"); - pid_t proc1_1 = process_spawn(proc1, "test_1-1"); - thread_spawn(proc1_1, NULL); - pid_t proc1_2 = process_spawn(proc1, "test_1-2"); + thread_T* thread = thread_spawn(PROCESS_KERNEL, test); - pid_t proc2 = process_spawn(PROCESS_KERNEL, "test_2"); + scheduler_start_thread(thread); scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0); - process_kill_pid(proc1); - scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0); +// process_kill_pid(proc1); +// scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0); // vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/test.elf"); // diff --git a/kernel/src/proc/process.c b/kernel/src/proc/process.c index bd480dc..0ab583b 100644 --- a/kernel/src/proc/process.c +++ b/kernel/src/proc/process.c @@ -26,14 +26,34 @@ #include "utils/memory.h" #include "utils/math.h" -pid_t process_spawn(pid_t parent, string_t name) { +void process_kernel_spawn(elf_executable_T* executable) { + process_T* process = memory_allocate(sizeof(process_T)); + process->num_threads = 0; + process->threads = NULL; + process->parent = NULL; + process->executable = executable; + + memory_copy("kernel", process->name, 7); + + process->page_map = g_kernel_page_map; + + // the processes' id, chunk_id and chunk fields will be set by the scheduler + + scheduler_register_process(process); +} + +pid_t process_spawn(pid_t parent, string_t name, elf_executable_T* executable, void* buffer) { process_T* process = memory_allocate(sizeof(process_T)); process->num_threads = 0; process->threads = NULL; process->parent = scheduler_get_process(parent); + process->executable = executable; memory_copy(name, process->name, MIN(string_length(name), 127)); + process->page_map = page_map_create(); + elf_mappings_apply(executable->mappings, executable->num_mappings, buffer, 0, process->page_map); + // the processes' id, chunk_id and chunk fields will be set by the scheduler return scheduler_register_process(process); @@ -46,5 +66,7 @@ void process_kill_pid(pid_t pid) { void process_kill(process_T* process) { scheduler_kill_process(process); + page_map_destruct(process->page_map); + memory_free(process); } \ No newline at end of file diff --git a/kernel/src/proc/scheduler.c b/kernel/src/proc/scheduler.c index 82f1889..551280d 100644 --- a/kernel/src/proc/scheduler.c +++ b/kernel/src/proc/scheduler.c @@ -66,7 +66,7 @@ scheduler_processes_chunk_T* scheduler_processes_chunk_create(scheduler_processe return chunk; } -void scheduler_init() { +void scheduler_init(boot_info_T* boot_info) { g_scheduler.num_threads = 0; g_scheduler.num_processes = 0; g_scheduler.running_thread = NULL; @@ -76,13 +76,13 @@ void scheduler_init() { g_scheduler_info_buffer = graphics_buffer_request(graphics_renderer_get_width()-100, 0, 100, 200, GRAPHICS_BUFFER_STANDARD); + process_kernel_spawn(elf_executable_create(boot_info->kernel_file->address)); + syscall_perform(SYSCALL_KERNEL_SCHEDULER_START); } cpu_state_T* scheduler_start(cpu_state_T* state) { - process_spawn(PROCESS_NONE, "kernel"); - thread_T* thread = thread_spawn_from_state(PROCESS_KERNEL, state); thread->global_prev = thread; thread->global_next = thread; @@ -292,11 +292,20 @@ process_T* scheduler_get_process(pid_t pid) { } thread_T* scheduler_get_current_thread() { + if (!g_scheduler.initialized) { + return NULL; + } + return g_scheduler.running_thread; } process_T* scheduler_get_current_process() { - return g_scheduler.running_thread->process; + thread_T* thread = scheduler_get_current_thread(); + if (thread == NULL) { + return NULL; + } + + return thread->process; } cpu_state_T* scheduler_switch_context(cpu_state_T* state) { diff --git a/kernel/src/proc/thread.c b/kernel/src/proc/thread.c index e04cf00..3599401 100644 --- a/kernel/src/proc/thread.c +++ b/kernel/src/proc/thread.c @@ -43,7 +43,7 @@ thread_T* thread_spawn(pid_t process, void* function) { thread->cpu_time = 0; thread->state = (cpu_state_T){ - .cr3 = (uint64_t)g_kernel_page_map, + .cr3 = (uint64_t)thread->process->page_map, .rax = 0, .rbx = 0, .rcx = 0,