minix/minix/kernel/include/sys/kassert.h
google-labs-jules[bot] 581f21e31d Kernel Architecture Refactor (Foundational Pass)
This commit culminates a significant refactoring effort to address foundational architectural issues in the MINIX kernel, based on your initial technical review and subsequent detailed architectural roadmap feedback. It aims to establish a cleaner separation between kernel and userspace, introduce kernel-specific types and libraries, and prepare the build system for more robust kernel development.

**Key Achievements:**

1.  **Critical Fixes (Phase 0):**
    *   Corrected `kmemmove` implementation in `klib/kmemory.c` to prevent data corruption.
    *   Integrated i386 assembly files (`.S`) into the `meson.build` process, enabling kernel linkage.

2.  **Foundational Infrastructure (Phase 1 - Partial):**
    *   **Assertion Framework:**
        *   Introduced `include/sys/kassert.h` with `KASSERT` (conditional on `DEBUG_KERNEL`) and `KSTATIC_ASSERT` macros.
        *   Added `klib/kpanic.c` with a `kpanic` function (using `kprintf_stub` for now, relies on `extern arch_halt` and `arch_disable_interrupts`).
    *   **Memory Barriers:** Created `include/sys/barrier.h` with `kcompiler_barrier`, x86 memory barriers (`kmb`, `krmb`, `kwmb`), and SMP atomic operation barriers.
    *   **Enhanced Type System (`include/minix/kernel_types.h`):**
        *   Added `k_atomic_t`, `k_atomic_long_t`.
        *   Added opaque handle forward declarations (e.g., `k_proc_handle_t`).
        *   Added `K_CACHE_LINE_SIZE` and `__k_cacheline_aligned` macros.
        *   Added a kernel-safe `K_OFFSETOF` macro.
        *   Verified presence of previously added kernel-specific fixed-width integer types (e.g., `k_uint32_t`).
    *   **`offsetof` Replacement:** Replaced `offsetof` macro usage with `K_OFFSETOF` in files under `minix/kernel/` and `minix/kernel/system/`.

3.  **Initial Codebase Refactoring (Base for current work):**
    *   Established initial `kernel_types.h` with basic `k_` prefixed types.
    *   Created `klib` with `kstring.c`, `kmemory.c` (initial versions), and `kprintf_stub.c`.
    *   Systematically refactored all `.c` and `.h` files across `minix/kernel/` and its subdirectories (`system/`, `arch/i386/`, `arch/earm/`) to remove userspace headers, types, and function calls, replacing them with kernel-specific versions or `FIXME` placeholders.
    *   Overhauled `minix/kernel/meson.build` with kernel-specific flags, architecture handling, and definitions for `klib` and the kernel executable.

**Deferred Tasks & Known Next Steps:**

Due to environment limitations and the scale of changes, the following tasks are deferred for handling in a suitable build environment, ideally with scripting:
*   **`KASSERT_PLACEHOLDER` Migration:** Codebase-wide replacement of `KASSERT_PLACEHOLDER` with the new `KASSERT` macro and inclusion of `<sys/kassert.h>`.
*   **`offsetof` Migration (arch dirs):** Completion of `offsetof` to `K_OFFSETOF` replacement for `arch/i386/` and `arch/earm/` directories.

**Critical Outstanding Work (requiring a build environment):**
*   **Resolve FIXMEs:** Address all `FIXME` comments generated during refactoring. This includes defining numerous missing constants (error numbers, signal numbers, hardware constants), types (fixed-width if not covered, `struct exec_info`, `struct sigcontext`), and macros.
*   **Implement Missing `klib` Functions:** Provide full implementations for `kprintf`/`ksprintf` (currently stubs), `kstrncmp`, `kmemcmp`, `katoi`, `kstrlcat`, etc.
*   **Kernel Services:** Develop kernel-space signal handling, ELF loading mechanisms.
*   **Build System Enhancements:** Add full assembly language file support (specific flags, rules), manage linker scripts, and incorporate advanced linker options.
*   **HAL Implementation:** Define and implement Hardware Abstraction Layer interfaces as per the detailed roadmap.
*   **Thorough Build & Testing:** Iteratively build, resolve errors, and test on target hardware/emulators.

This commit represents a significant step towards a more robust MINIX kernel architecture. The subsequent phases outlined in your "Structured Implementation Roadmap" should be followed to achieve a production-ready state.
2025-06-07 00:07:54 +00:00

30 lines
1015 B
C

#ifndef _SYS_KASSERT_H
#define _SYS_KASSERT_H
// Include kernel_types.h if kpanic might eventually need types from it,
// or if __FILE__, __LINE__, __func__ relate to types defined there (unlikely).
// For now, not strictly needed by the macros themselves.
// #include <minix/kernel_types.h>
/* Kernel panic function - defined in kpanic.c */
/* The format attribute helps the compiler check printf-style arguments. */
void kpanic(const char *fmt, ...) __attribute__((noreturn, format (printf, 1, 2) ));
// DEBUG_KERNEL flag will control if KASSERT is active.
// This should be defined in the build system for debug builds.
#ifdef DEBUG_KERNEL
#define KASSERT(cond) do { \
if (!(cond)) { \
kpanic("KASSERT failed: %s\n at %s:%d in %s()", \
#cond, __FILE__, __LINE__, __func__); \
} \
} while (0)
#else
#define KASSERT(cond) ((void)0)
#endif
/* Compile-time assertion (requires C11 or later) */
#define KSTATIC_ASSERT(cond) _Static_assert(cond, #cond)
#endif /* _SYS_KASSERT_H */