From 58344a58ba69a62cfc98ba707f0e1bb6cc5b1f30 Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Thu, 16 Jan 2025 15:57:35 +0300 Subject: [PATCH 1/4] Add debug stuff --- .github/workflows/c-cpp.yml | 8 ++++++++ .github/workflows/codeql.yml | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 5a60709..eefcd68 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -24,3 +24,11 @@ jobs: run: make - name: run and test run: make clean && make && make test + - name: Versions + run: | + make -v + gcc -v + clang-format --version + xorriso --version + qemu-system-x86_64 -version + grub-mkrescue --version diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 638ab75..7546b94 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -84,6 +84,14 @@ jobs: make clean make make test + - name: Versions + run: | + make -v + gcc -v + clang-format --version + xorriso --version + qemu-system-x86_64 -version + grub-mkrescue --version - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v3 From 473a4d8479fa380a24ab0d1e005a1f61f1366cf4 Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Thu, 16 Jan 2025 15:57:55 +0300 Subject: [PATCH 2/4] Add sections debug --- kernel/arch/amd64/sections.c | 31 +++++++++++++++++++++++++++++++ kernel/arch/amd64/serial.c | 3 ++- kernel/include/kreflock.h | 10 +++++----- kernel/include/sys/panic.h | 4 ++-- kernel/kernel.c | 4 ++++ kernel/klibc/string.c | 3 ++- kernel/multiboot2.c | 2 ++ kernel/sys/panic.c | 4 ++-- scripts/linker.ld | 8 ++++++++ 9 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 kernel/arch/amd64/sections.c diff --git a/kernel/arch/amd64/sections.c b/kernel/arch/amd64/sections.c new file mode 100644 index 0000000..a97b566 --- /dev/null +++ b/kernel/arch/amd64/sections.c @@ -0,0 +1,31 @@ +#include "khal.h" +#include +#include "kstring.h" + +extern uint64_t kernel_section_text_start; +extern uint64_t kernel_section_text_end; +extern uint64_t kernel_section_rodata_start; +extern uint64_t kernel_section_rodata_end; +extern uint64_t kernel_section_data_start; +extern uint64_t kernel_section_data_end; +extern uint64_t kernel_section_bss_start; +extern uint64_t kernel_section_bss_end; + +int sectons_init() { + serial_printf("\t.text 0x%x-0x%x(%u)\n", &kernel_section_text_start, + &kernel_section_text_end, + &kernel_section_text_end - &kernel_section_text_start); + serial_printf("\t.rodata 0x%x-0x%x(%u)\n", &kernel_section_rodata_start, + &kernel_section_rodata_end, + &kernel_section_rodata_end - &kernel_section_rodata_start); + serial_printf("\t.data 0x%x-0x%x(%u)\n", &kernel_section_data_start, + &kernel_section_data_end, + &kernel_section_data_end - &kernel_section_data_start); + serial_printf("\t.bss 0x%x-0x%x(%u)\n", &kernel_section_bss_start, + &kernel_section_bss_end, + &kernel_section_bss_end - &kernel_section_bss_start); + + memset(&kernel_section_bss_start, 0, &kernel_section_bss_end - &kernel_section_bss_start); + + return 1; +} \ No newline at end of file diff --git a/kernel/arch/amd64/serial.c b/kernel/arch/amd64/serial.c index bb7ff4b..1085573 100644 --- a/kernel/arch/amd64/serial.c +++ b/kernel/arch/amd64/serial.c @@ -3,7 +3,8 @@ void serial_write_byte(uint8_t byte) { // Wait until the transmit holding register is empty - while ((inb(0x3f8 + 5) & 0x20) == 0); + while ((inb(0x3f8 + 5) & 0x20) == 0) + ; outb(0x3f8, byte); } diff --git a/kernel/include/kreflock.h b/kernel/include/kreflock.h index ed3bcc6..5da4ce0 100644 --- a/kernel/include/kreflock.h +++ b/kernel/include/kreflock.h @@ -34,11 +34,11 @@ typedef struct { } reflock_t; #define NEW_REFLOCK(_on_lock, _on_unlock, _strict, _allow_force) \ - { _on_lock, \ - _on_unlock, \ - _strict, \ - _allow_force, \ - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } + { \ + _on_lock, _on_unlock, _strict, _allow_force, { \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \ + } \ + } void reflock_make(reflock_t *lock); bool reflock_validate_magic(reflock_t *lock); diff --git a/kernel/include/sys/panic.h b/kernel/include/sys/panic.h index 68cae99..db6dd91 100644 --- a/kernel/include/sys/panic.h +++ b/kernel/include/sys/panic.h @@ -4,8 +4,8 @@ #include void __attribute__((noreturn)) panic_int(uint8_t int_no, const char *msg); -void __attribute__((noreturn)) panic(const char *msg, const char *func, - const int line); +void __attribute__((noreturn)) +panic(const char *msg, const char *func, const int line); #define PANIC(msg) panic(msg, __func__, __LINE__) diff --git a/kernel/kernel.c b/kernel/kernel.c index d8349e1..4223263 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -4,14 +4,18 @@ #include int multiboot2_init(uint64_t *addr, uint32_t magic); +int sectons_init(); void kernel_main64(uint64_t *multiboot2, uint32_t magic, void *esp, uint64_t base) { + sectons_init(); serial_init(); serial_printf(":D\n"); int status = multiboot2_init(multiboot2, magic); + sectons_init(); + if (status) { serial_printf("[OK]\n"); } else { diff --git a/kernel/klibc/string.c b/kernel/klibc/string.c index 15bf75e..51c0846 100644 --- a/kernel/klibc/string.c +++ b/kernel/klibc/string.c @@ -87,7 +87,8 @@ int memcmp(const void *ptr1, const void *ptr2, size_t count) { size_t __attribute__((pure)) strlen(const char *s) { int i; - for (i = 0; s[i] != '\0'; i++); + for (i = 0; s[i] != '\0'; i++) + ; return i; } diff --git a/kernel/multiboot2.c b/kernel/multiboot2.c index 48ebf3a..9ec403b 100644 --- a/kernel/multiboot2.c +++ b/kernel/multiboot2.c @@ -83,6 +83,8 @@ void handle_basic_load_base_addr(struct multiboot_tag *tag) { (struct multiboot_tag_load_base_addr *)tag; serial_printf("load_base_size: %u\n", base_addr->size); serial_printf("load_base_addr: 0x%x\n", base_addr->load_base_addr); + serial_printf("Kernel: 0x%x-0x%x\n", base_addr->load_base_addr, + base_addr->load_base_addr + base_addr->size); } void handle_mmap_tag(struct multiboot_tag *tag) { diff --git a/kernel/sys/panic.c b/kernel/sys/panic.c index 04ccc16..d98da8b 100644 --- a/kernel/sys/panic.c +++ b/kernel/sys/panic.c @@ -15,8 +15,8 @@ void __attribute__((noreturn)) panic_int(uint8_t int_no, const char *msg) { for (;;) cpu_halt(); } -void __attribute__((noreturn)) panic(const char *msg, const char *func, - const int line) { +void __attribute__((noreturn)) +panic(const char *msg, const char *func, const int line) { cpu_interrupt_lock_acquire(); serial_printf("\n\rKernel panic - %s in %s:%d\n", msg, func, line); diff --git a/scripts/linker.ld b/scripts/linker.ld index 92a384d..2e0b079 100644 --- a/scripts/linker.ld +++ b/scripts/linker.ld @@ -6,30 +6,38 @@ SECTIONS{ phys = .; .text BLOCK(4K) : ALIGN(4K) { + PROVIDE(kernel_section_text_start = .); *(.multiboot) *(.bootstrap) code = .; *(.text) + PROVIDE(kernel_section_text_end = .); } .rodata BLOCK(4K) : ALIGN(4K) { + PROVIDE(kernel_section_rodata_start = .); *(.rodata) + PROVIDE(kernel_section_rodata_end = .); } .data BLOCK(4K) : ALIGN(4K) { + PROVIDE(kernel_section_data_start = .); data = .; *(.data) *(.symbols) PROVIDE(kernel_symbols_start = .); PROVIDE(kernel_symbols_end = .); + PROVIDE(kernel_section_data_end = .); } .bss BLOCK(4K) : ALIGN(4K) { + PROVIDE(kernel_section_bss_start = .); PROVIDE(bss_start = .); bss = .; *(COMMON) *(.bss) *(.stack) + PROVIDE(kernel_section_bss_end = .); } end = .; From 99f3527c2ae402eb7c3cd8d096ed250d636cab74 Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Sat, 18 Jan 2025 13:46:26 +0300 Subject: [PATCH 3/4] Add sections table --- kernel/arch/amd64/sections.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/kernel/arch/amd64/sections.c b/kernel/arch/amd64/sections.c index a97b566..811c108 100644 --- a/kernel/arch/amd64/sections.c +++ b/kernel/arch/amd64/sections.c @@ -1,6 +1,6 @@ #include "khal.h" -#include #include "kstring.h" +#include extern uint64_t kernel_section_text_start; extern uint64_t kernel_section_text_end; @@ -11,21 +11,40 @@ extern uint64_t kernel_section_data_end; extern uint64_t kernel_section_bss_start; extern uint64_t kernel_section_bss_end; -int sectons_init() { +typedef struct { + uint64_t start; + uint64_t end; +} sections_t; + +sections_t kernel_sections[4]; + +sections_t *sectons_init() { serial_printf("\t.text 0x%x-0x%x(%u)\n", &kernel_section_text_start, &kernel_section_text_end, &kernel_section_text_end - &kernel_section_text_start); + kernel_sections[0] = + (sections_t){ kernel_section_text_start, kernel_section_text_end }; + serial_printf("\t.rodata 0x%x-0x%x(%u)\n", &kernel_section_rodata_start, &kernel_section_rodata_end, &kernel_section_rodata_end - &kernel_section_rodata_start); + kernel_sections[1] = + (sections_t){ kernel_section_rodata_start, kernel_section_rodata_end }; + serial_printf("\t.data 0x%x-0x%x(%u)\n", &kernel_section_data_start, &kernel_section_data_end, &kernel_section_data_end - &kernel_section_data_start); + kernel_sections[2] = + (sections_t){ kernel_section_data_start, kernel_section_data_end }; + serial_printf("\t.bss 0x%x-0x%x(%u)\n", &kernel_section_bss_start, &kernel_section_bss_end, &kernel_section_bss_end - &kernel_section_bss_start); - - memset(&kernel_section_bss_start, 0, &kernel_section_bss_end - &kernel_section_bss_start); + kernel_sections[3] = + (sections_t){ kernel_section_bss_start, kernel_section_bss_end }; + + memset(&kernel_section_bss_start, 0, + &kernel_section_bss_end - &kernel_section_bss_start); - return 1; + return (sections_t *)&kernel_sections; } \ No newline at end of file From 78de14d15746ddb4f3aab847f30e969b1df42242 Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Sat, 18 Jan 2025 13:52:43 +0300 Subject: [PATCH 4/4] Fix page fault --- kernel/arch/amd64/sections.c | 7 ++++--- kernel/kernel.c | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/kernel/arch/amd64/sections.c b/kernel/arch/amd64/sections.c index 811c108..a087b3b 100644 --- a/kernel/arch/amd64/sections.c +++ b/kernel/arch/amd64/sections.c @@ -19,6 +19,10 @@ typedef struct { sections_t kernel_sections[4]; sections_t *sectons_init() { + // Why this halts cpu? + //memset(&kernel_section_bss_start, 0, + // (uint64_t)&kernel_section_bss_end - + // (uint64_t)&kernel_section_bss_start); serial_printf("\t.text 0x%x-0x%x(%u)\n", &kernel_section_text_start, &kernel_section_text_end, &kernel_section_text_end - &kernel_section_text_start); @@ -43,8 +47,5 @@ sections_t *sectons_init() { kernel_sections[3] = (sections_t){ kernel_section_bss_start, kernel_section_bss_end }; - memset(&kernel_section_bss_start, 0, - &kernel_section_bss_end - &kernel_section_bss_start); - return (sections_t *)&kernel_sections; } \ No newline at end of file diff --git a/kernel/kernel.c b/kernel/kernel.c index 4223263..531d627 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -14,8 +14,6 @@ void kernel_main64(uint64_t *multiboot2, uint32_t magic, void *esp, int status = multiboot2_init(multiboot2, magic); - sectons_init(); - if (status) { serial_printf("[OK]\n"); } else {