diff --git a/Makefile b/Makefile index dd3849c..f930a30 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: vvaucoul +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/06/14 18:51:28 by vvaucoul #+# #+# # -# Updated: 2024/07/29 14:33:08 by vvaucoul ### ########.fr # +# Updated: 2024/07/29 14:35:42 by vvaucoul ### ########.fr # # # # **************************************************************************** # diff --git a/kernel/arch/x86/boot/lowerHalfKernel.s b/kernel/arch/x86/boot/lowerHalfKernel.s index 9fc46f2..ff89889 100644 --- a/kernel/arch/x86/boot/lowerHalfKernel.s +++ b/kernel/arch/x86/boot/lowerHalfKernel.s @@ -3,6 +3,7 @@ ;******************************************************************************* STACK_SIZE equ 0x4000 +KERNEL_STACK_MARKER equ 0x4B52304E ;******************************************************************************* ;* MULTIBOOT * @@ -11,45 +12,51 @@ STACK_SIZE equ 0x4000 global __call_kmain extern BOOTLOADER_MAGIC extern display_error_msg +extern initialize_stack section .data - align 0x1000 + align 0x1000 ; Multiboot Section align 4 extern __lhk_multiboot section .bss - align 32 -stack: - resb STACK_SIZE + align 32 +extern stack section .text - global _start:function (_start.end - _start) - extern kmain + global _start:function (_start.end - _start) + extern kmain _start: - xor ebp, ebp - mov esp, stack + STACK_SIZE - push esp ; stack - push ebx ; Multiboot Info - push eax ; Magic Number - - cli - call kmain - test eax, eax - jz .hang - cmp eax, 1 - jz .error + xor ebp, ebp + + ; Initialize_stack use EAX, so we need to save it + push eax + ; Call stack initializer + call initialize_stack + pop eax + + mov esp, stack + STACK_SIZE + push esp ; stack + push ebx ; Multiboot Info + push eax ; Magic Number + + cli + call kmain + test eax, eax + jz .hang + cmp eax, 1 + jz .error .error: - call display_error_msg - hlt - jmp .error + call display_error_msg + hlt + jmp .error .hang: - hlt - jmp .hang + hlt + jmp .hang .end: - diff --git a/kernel/arch/x86/boot/stack.s b/kernel/arch/x86/boot/stack.s new file mode 100644 index 0000000..e1d1dd2 --- /dev/null +++ b/kernel/arch/x86/boot/stack.s @@ -0,0 +1,27 @@ +;******************************************************************************* +;* DEFINES * +;******************************************************************************* + +STACK_SIZE equ 0x4000 +KERNEL_STACK_MARKER equ 0x4B52304E ; "KRONOST" (KRONOS STACK) - Marker for stack initialization + +;******************************************************************************* +;* STACK INITIALIZER * +;******************************************************************************* + +section .bss + align 32 +stack: + resb STACK_SIZE + +section .text + global initialize_stack + extern stack + +initialize_stack: + ; Initialize stack with marker + mov ecx, STACK_SIZE / 4 ; Number of 32-bit words in stack + mov edi, stack ; Destination address + mov eax, KERNEL_STACK_MARKER ; Marker value + rep stosd ; Initialize stack with marker + ret \ No newline at end of file diff --git a/kernel/includes/memory/memory.h b/kernel/includes/memory/memory.h index c707f20..cbe8c6a 100644 --- a/kernel/includes/memory/memory.h +++ b/kernel/includes/memory/memory.h @@ -6,16 +6,18 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/17 14:07:18 by vvaucoul #+# #+# */ -/* Updated: 2024/07/29 12:27:23 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/29 14:57:10 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MEMORY_H #define MEMORY_H -#include -#include +#include + #include +#include +#include #define KERNEL_BASE 0x00100000 #define KERNEL_VIRTUAL_BASE 0xC0000000 diff --git a/kernel/srcs/kernel.c b/kernel/srcs/kernel.c index 3742def..8bd954c 100644 --- a/kernel/srcs/kernel.c +++ b/kernel/srcs/kernel.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/22 13:55:07 by vvaucoul #+# #+# */ -/* Updated: 2024/07/29 14:16:14 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/29 15:36:14 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,7 +88,7 @@ #endif uint32_t initial_esp; -uint32_t *kernel_stack = NULL; +uint32_t *kernel_stack; // ! ||--------------------------------------------------------------------------------|| // ! || KERNEL UTILS FUNCTIONS || @@ -196,23 +196,6 @@ static int check_multiboot(uint32_t magic_number, uint32_t addr, uint32_t *kstac kernel_log_info("LOG", "MULTIBOOT"); } - uint32_t s_base, s_top, s_size, s_usage, s_usage_percentage; - - s_base = sm_get_stack_base(); - s_top = sm_get_stack_top(); - s_size = sm_get_stack_size(); - s_usage = sm_get_stack_usage(); - s_usage_percentage = sm_get_stack_usage_percentage(); - - printk(_YELLOW "Stack Info:\n" _END); - printk("\t- Base: 0x%x\n", s_base); - printk("\t- Top: 0x%x\n", s_top); - printk("\t- Size: 0x%x\n", s_size); - printk("\t- Usage: 0x%x\n", s_usage); - printk("\t- Usage Percentage: %u % \n", s_usage_percentage); - - - kpause(); return (0); } diff --git a/kernel/srcs/multiboot/multiboot.c b/kernel/srcs/multiboot/multiboot.c index c128aeb..f5ada95 100644 --- a/kernel/srcs/multiboot/multiboot.c +++ b/kernel/srcs/multiboot/multiboot.c @@ -6,13 +6,16 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/09/10 19:02:46 by vvaucoul #+# #+# */ -/* Updated: 2024/07/29 14:03:16 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/29 15:26:02 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include +#include + #include #include #include @@ -51,24 +54,30 @@ static int multiboot_check_flag(uint32_t flag) { return (0); } -static int multiboot_init_kernel_stack(uint32_t *kernel_stack) { +static int multiboot_init_kernel_stack(uint32_t *kstack) { /* Check if kernel stack is valid */ - if (kernel_stack == NULL) { + if (kstack == NULL) { return (1); + } else { + kernel_stack = kstack; } - /* Set kernel stack marker */ - uint32_t stack_top = *(uint32_t *)kernel_stack & 0xFFFFFFF0; - for (uint32_t i = 0; i <= KERNEL_STACK_SIZE; i += sizeof(uint32_t)) { - uint32_t stack_value = *(uint32_t *)((uintptr_t)(stack_top - i)); + uint32_t s_base, s_top; + + s_base = (uintptr_t)kstack; + s_top = (uintptr_t)kstack + KERNEL_STACK_SIZE; + + printk("Kernel stack: 0x%x -> 0x%x\n", s_base, s_top); - if (stack_value != 0) { - continue; + /* Set kernel stack marker */ + for (uint32_t i = s_base; i < s_top; i += sizeof(uint32_t)) { + uint32_t *ptr = (uint32_t *)(uintptr_t)i; + + if (*ptr == 0x0) { + *ptr = KERNEL_STACK_MARKER; } - *(uint32_t *)((uintptr_t)stack_top - i) = KERNEL_STACK_MARKER; } - /** * Set kernel stack * diff --git a/kernel/srcs/stack_monitor.c b/kernel/srcs/stack_monitor.c index 780eff3..c9f3487 100644 --- a/kernel/srcs/stack_monitor.c +++ b/kernel/srcs/stack_monitor.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/29 13:02:42 by vvaucoul #+# #+# */ -/* Updated: 2024/07/29 14:31:39 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/29 15:36:02 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ #include #include +#include + /** * @brief Get the stack usage. * @@ -22,24 +24,21 @@ * @return The stack usage in bytes. */ uint32_t sm_get_stack_usage(void) { - uint32_t s_base, s_top, s_size; + uint32_t s_base = sm_get_stack_base(); + uint32_t s_top = sm_get_stack_top(); uint32_t count = 0; - s_base = sm_get_stack_base(); - s_top = sm_get_stack_top(); - s_size = sm_get_stack_size(); - - for (uint32_t i = s_base; i < s_top && i < s_base + s_size; i += sizeof(uint32_t)) { + for (uint32_t i = s_base; i < s_top; i += sizeof(uint32_t)) { uint32_t *ptr = (uint32_t *)(uintptr_t)i; - if (*ptr == sm_get_stack_marker()) { - break; - } else { + if (*ptr != KERNEL_STACK_MARKER) { count++; + } else { + break; } } - return (count); + return count * sizeof(uint32_t); } /** @@ -51,12 +50,15 @@ uint32_t sm_get_stack_usage(void) { * @return The percentage of stack usage. */ uint32_t sm_get_stack_usage_percentage(void) { - uint32_t s_size, s_usage; + uint32_t s_size = sm_get_stack_size(); + uint32_t s_usage = sm_get_stack_usage(); - s_size = sm_get_stack_size(); - s_usage = sm_get_stack_usage(); + /* Avoid division by zero */ + if (s_size == 0) { + return 0; + } - return ((s_usage * 100) / s_size); + return (s_usage * 100) / s_size; } /** @@ -67,7 +69,7 @@ uint32_t sm_get_stack_usage_percentage(void) { * @return The size of the stack in bytes. */ uint32_t sm_get_stack_size(void) { - return (KERNEL_STACK_SIZE); + return (sm_get_stack_top() - sm_get_stack_base()); } /** @@ -78,10 +80,7 @@ uint32_t sm_get_stack_size(void) { * @return The base address of the stack. */ uint32_t sm_get_stack_base(void) { - uint32_t esp; - - __asm__ volatile("mov %%esp, %0" : "=r"(esp)); - return (esp); + return (uintptr_t)kernel_stack; } /** @@ -92,10 +91,7 @@ uint32_t sm_get_stack_base(void) { * @return The top address of the stack. */ uint32_t sm_get_stack_top(void) { - uint32_t esp; - - __asm__ volatile("mov %%esp, %0" : "=r"(esp)); - return (esp + KERNEL_STACK_SIZE); + return (uintptr_t)kernel_stack + KERNEL_STACK_SIZE; } /** @@ -116,13 +112,11 @@ uint32_t sm_get_stack_marker(void) { * It can be used to monitor the stack usage in a program. */ void sm_print_stack_info(void) { - uint32_t s_base, s_top, s_size, s_usage, s_usage_percentage; - - s_base = sm_get_stack_base(); - s_top = sm_get_stack_top(); - s_size = sm_get_stack_size(); - s_usage = sm_get_stack_usage(); - s_usage_percentage = sm_get_stack_usage_percentage(); + uint32_t s_base = sm_get_stack_base(); + uint32_t s_top = sm_get_stack_top(); + uint32_t s_size = sm_get_stack_size(); + uint32_t s_usage = sm_get_stack_usage(); + uint32_t s_usage_percentage = sm_get_stack_usage_percentage(); printk(_YELLOW "Stack Info:\n" _END); printk("\t- Base: 0x%x\n", s_base); diff --git a/mk-files/sources/Sources-Boot.mk b/mk-files/sources/Sources-Boot.mk index e0d87fb..55e1a0f 100644 --- a/mk-files/sources/Sources-Boot.mk +++ b/mk-files/sources/Sources-Boot.mk @@ -6,7 +6,7 @@ # By: vvaucoul +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/06/27 17:36:21 by vvaucoul #+# #+# # -# Updated: 2023/05/31 18:46:53 by vvaucoul ### ########.fr # +# Updated: 2024/07/29 15:21:05 by vvaucoul ### ########.fr # # # # **************************************************************************** # @@ -18,6 +18,7 @@ BOOT_DIR_ERROR = $(KERNEL_BOOT_DIR)/errors MULTIBOOT_DIR = $(KERNEL_BOOT_DIR)/multiboot KBOOT_SRCS = $(BOOT).s \ + $(KERNEL_BOOT_DIR)/stack.s \ $(BOOT_DIR_ERROR)/boot_error.s ifeq ($(CHECK_HIGHER_HALF_KERNEL), false)