diff --git a/README.md b/README.md index b7fd739..9499567 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ practices while remaining approachable to contributors and learners alike. - [x] Serial output - [x] Framebuffer abstraction + character output -- [ ] Panic functions + Assertion system -- [ ] Logging system (both visual + serial) +- [x] Panic functions + Assertion system +- [x] Logging system (both visual + serial) - [ ] Halt loop - [ ] Basics of the kernel + GDT, IDT and interrupt handlers - [ ] PIC, timer and keyboard support diff --git a/include/console.h b/include/kernel/console.h similarity index 93% rename from include/console.h rename to include/kernel/console.h index d88d346..3b1615f 100644 --- a/include/console.h +++ b/include/kernel/console.h @@ -9,8 +9,8 @@ #ifndef AVERY_CONSOLE_H #define AVERY_CONSOLE_H -#include "types.h" -#include "graphics/graphicsTypes.h" +#include "../types.h" +#include "../graphics/graphicsTypes.h" #pragma once diff --git a/include/kernel/debug.h b/include/kernel/debug.h new file mode 100644 index 0000000..abddce2 --- /dev/null +++ b/include/kernel/debug.h @@ -0,0 +1,47 @@ +/* +* debug.h +* As part of the Avery project +* Created by Max Van den Eynde in 2026 +* -------------------------------------- +* Description: ${FILE_DESCRIPTION} +* Copyright (c) 2026 Max Van den Eynde +*/ + +#ifndef AVERY_DEBUG_H +#define AVERY_DEBUG_H + +#pragma once + +enum class LogType { + Console, + Serial, +}; + +#define PANIC(msg) debug::kernelPanic(msg, __FILE__, __LINE__, __func__) + +#define VERIFY(expr) \ +do { \ +if (!(expr)) { \ +debug::kernelPanic( \ +"VERIFY failed: " #expr, \ +__FILE__, \ +__LINE__, \ +__func__ \ +); \ +} \ +} while (0) + +#ifndef NDEBUG +#define ASSERT(expr) VERIFY(expr) +#else +#define ASSERT(expr) do { } while (0) +#endif + +namespace debug { + [[noreturn]] void kernelPanic(const char* message, const char* file, int line, const char* function); + void log(const char* message, LogType logType = LogType::Serial); + void warn(const char* message, LogType logType = LogType::Serial); + void error(const char* message, LogType logType = LogType::Serial); +} + +#endif //AVERY_DEBUG_H diff --git a/kernel/core/debug.cpp b/kernel/core/debug.cpp new file mode 100644 index 0000000..cb33e1a --- /dev/null +++ b/kernel/core/debug.cpp @@ -0,0 +1,83 @@ +/* +* debug.cpp +* As part of the Avery project +* Created by Max Van den Eynde in 2026 +* -------------------------------------- +* Description: Debugging functions and more +* Copyright (c) 2026 Max Van den Eynde +*/ + +#include "../../include/kernel/debug.h" + +#include "io/serial.h" +#include "kernel/console.h" + +[[noreturn]] void debug::kernelPanic(const char* message, const char* file, + [[maybe_unused]] int line, const char* function) { + out::setColor(Color::white, Color::red); + out::clear(); + + out::println(""); + out::println("The Avery Kernel panicked!"); + out::println("Avery entered a bad execution state that resulted in an unrecoverable state."); + out::println("Restart your computer and contact the developers if the problem keeps happening"); + out::println("================="); + out::println("Crash Information:"); + out::println(message); + out::println(""); + out::print("At file: "); + out::println(file); + out::println(""); + out::print("At function: "); + out::println(function); + + asm volatile("cli"); + + while (true) { + asm volatile("hlt"); + } +} + +void debug::log(const char* message, LogType logType) { + if (logType == LogType::Serial) { + io::serialWrite("(LOG) -> "); + io::serialWrite(message); + io::serialWrite("\n"); + } + else { + out::setColor(Color::green, Color::black); + out::print("(LOG): "); + out::println(message); + } +} + + +void debug::warn(const char* message, LogType logType) { + if (logType == LogType::Serial) { + io::serialWrite("(WARNING) -> "); + io::serialWrite(message); + io::serialWrite("\n"); + } + else { + out::setColor(Color::yellow, Color::black); + out::print("(WARNING): "); + out::println(message); + } +} + + +void debug::error(const char* message, LogType logType) { + if (logType == LogType::Serial) { + io::serialWrite("(ERROR) -> "); + io::serialWrite(message); + io::serialWrite("\n"); + } + else { + out::setColor(Color::red, Color::black); + out::print("(ERROR): "); + out::println(message); + } +} + + + diff --git a/kernel/graphics/framebuffer.cpp b/kernel/graphics/framebuffer.cpp index 27668ec..b6aedb7 100644 --- a/kernel/graphics/framebuffer.cpp +++ b/kernel/graphics/framebuffer.cpp @@ -12,7 +12,8 @@ #include #include "graphics/graphicsTypes.h" -#include "types.h" +#include "../../include/types.h" +#include "kernel/debug.h" namespace { u64 fontScale(float scale) { @@ -66,11 +67,7 @@ Framebuffer Framebuffer::createFromLimineRequest( ) { Framebuffer framebuffer; - if (request.response == nullptr || request.response->framebuffer_count < 1) { - while (true) { - asm volatile("hlt"); - } - } + ASSERT(request.response == nullptr || request.response->framebuffer_count < 1); framebuffer.fb = request.response->framebuffers[0]; framebuffer.fb_ptr = static_cast(framebuffer.fb->address); diff --git a/kernel/io/console.cpp b/kernel/io/console.cpp index 17d8e50..946ad36 100644 --- a/kernel/io/console.cpp +++ b/kernel/io/console.cpp @@ -8,7 +8,7 @@ */ #include "graphics/framebuffer.h" -#include "console.h" +#include "../../include/kernel/console.h" #include "io/serial.h" diff --git a/kernel/io/serial.cpp b/kernel/io/serial.cpp index 950e072..08bd6b3 100644 --- a/kernel/io/serial.cpp +++ b/kernel/io/serial.cpp @@ -7,7 +7,7 @@ * Copyright (c) 2026 Max Van den Eynde */ -#include "types.h" +#include "../../include/types.h" #include "io/serial.h" #include "io/io.h" diff --git a/kernel/main.cpp b/kernel/main.cpp index e07bc1d..ff39692 100644 --- a/kernel/main.cpp +++ b/kernel/main.cpp @@ -1,9 +1,10 @@ #include -#include "console.h" +#include "../include/kernel/console.h" #include "core/regs.h" #include "graphics/framebuffer.h" #include "io/serial.h" +#include "kernel/debug.h" __attribute__((used, section(".limine_requests"))) static volatile uint64_t limine_base_revision[] = LIMINE_BASE_REVISION(6); @@ -30,9 +31,13 @@ extern "C" [[noreturn]] void _start() { } } + Framebuffer framebuffer = Framebuffer::createFromLimineRequest(framebuffer_request); out::initFramebufferConsole(framebuffer); + ASSERT(false == true); + + out::println("Hello, World!"); out::switchTo(ConsoleOutputMode::Serial); out::println("Hello, World!");