Skip to content

Commit

Permalink
[Update] - Stack Monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vvaucoul committed Jul 29, 2024
1 parent 1721ffd commit 118204b
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand Down
55 changes: 31 additions & 24 deletions kernel/arch/x86/boot/lowerHalfKernel.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
;*******************************************************************************

STACK_SIZE equ 0x4000
KERNEL_STACK_MARKER equ 0x4B52304E

;*******************************************************************************
;* MULTIBOOT *
Expand All @@ -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:

27 changes: 27 additions & 0 deletions kernel/arch/x86/boot/stack.s
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions kernel/includes/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <memory/paging.h>
#include <memory/kheap.h>
#include <kernel.h>

#include <memory/frames.h>
#include <memory/kheap.h>
#include <memory/paging.h>

#define KERNEL_BASE 0x00100000
#define KERNEL_VIRTUAL_BASE 0xC0000000
Expand Down
21 changes: 2 additions & 19 deletions kernel/srcs/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -88,7 +88,7 @@
#endif

uint32_t initial_esp;
uint32_t *kernel_stack = NULL;
uint32_t *kernel_stack;

// ! ||--------------------------------------------------------------------------------||
// ! || KERNEL UTILS FUNCTIONS ||
Expand Down Expand Up @@ -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);
}
Expand Down
31 changes: 20 additions & 11 deletions kernel/srcs/multiboot/multiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <multiboot/multiboot.h>
#include <multiboot/multiboot_mmap.h>

#include <memory/memory.h>
#include <asm/asm.h>

#include <kernel.h>
#include <macros.h>
#include <stddef.h>
Expand Down Expand Up @@ -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
*
Expand Down
56 changes: 25 additions & 31 deletions kernel/srcs/stack_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

#include <kernel.h>
#include <kronos/stack_monitor.h>
#include <multiboot/multiboot_mmap.h>

#include <memory/memory.h>

/**
* @brief Get the stack usage.
*
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion mk-files/sources/Sources-Boot.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand All @@ -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)
Expand Down

0 comments on commit 118204b

Please sign in to comment.