From 6b5a19a19d654c494e556ab714c9ae017104e052 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Wed, 22 Apr 2026 19:51:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=90=E6=A7=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clks/kernel/core/kmain.c | 7 +++++++ clks/kernel/interface/tty.c | 8 ++++++++ clks/kernel/runtime/exec.c | 8 ++++++++ clks/kernel/runtime/syscall.c | 7 +++++++ clks/kernel/storage/fs.c | 7 +++++++ 5 files changed, 37 insertions(+) diff --git a/clks/kernel/core/kmain.c b/clks/kernel/core/kmain.c index 6e7ad7f..3b8b0c2 100644 --- a/clks/kernel/core/kmain.c +++ b/clks/kernel/core/kmain.c @@ -26,6 +26,8 @@ #include #include +/* Boot orchestration file: one wrong init order and the whole damn thing faceplants. */ + #ifndef CLKS_CFG_AUDIO #define CLKS_CFG_AUDIO 1 #endif @@ -189,8 +191,10 @@ void clks_kernel_main(void) { u64 syscall_ticks; u64 fs_root_children; + /* Serial first, because when graphics dies we still need a heartbeat. */ clks_serial_init(); + /* If boot protocol handshake fails, continuing would be pure fantasy. */ if (clks_boot_base_revision_supported() == CLKS_FALSE) { clks_serial_write("[ERROR][BOOT] LIMINE BASE REVISION NOT SUPPORTED\n"); clks_cpu_halt_forever(); @@ -198,6 +202,7 @@ void clks_kernel_main(void) { boot_fb = clks_boot_get_framebuffer(); + /* TTY comes up only when framebuffer exists; no pixels, no pretty lies. */ if (boot_fb != CLKS_NULL) { clks_fb_init(boot_fb); clks_tty_init(); @@ -349,6 +354,7 @@ void clks_kernel_main(void) { clks_log(CLKS_LOG_WARN, "CFG", "KELF DISABLED BY MENUCONFIG"); #endif + /* Scheduler init is the "okay, now this mess is actually alive" moment. */ clks_scheduler_init(); #if CLKS_CFG_KLOGD_TASK @@ -453,6 +459,7 @@ void clks_kernel_main(void) { clks_log(CLKS_LOG_DEBUG, "KERNEL", "IDLE LOOP ENTER"); #endif + /* Infinite idle loop: glamorous name for "wait forever and hope interrupts behave". */ for (;;) { u64 tick_now = clks_interrupts_timer_ticks(); clks_scheduler_dispatch_current(tick_now); diff --git a/clks/kernel/interface/tty.c b/clks/kernel/interface/tty.c index b3f7ab1..92c3a01 100644 --- a/clks/kernel/interface/tty.c +++ b/clks/kernel/interface/tty.c @@ -4,6 +4,8 @@ #include #include +/* Terminal code: where simple text output turns into a whole damn life choice. */ + #define CLKS_TTY_COUNT 4 #define CLKS_TTY_MAX_ROWS 128 #define CLKS_TTY_MAX_COLS 256 @@ -69,6 +71,7 @@ static clks_bool clks_tty_scrollback_is_active(u32 tty_index); static void clks_tty_redraw_active(void); static u32 clks_tty_ansi_palette(u32 index) { + /* Good-enough ANSI colors, because perfect fidelity can wait until never. */ static const u32 palette[16] = {0x00000000U, 0x00CD3131U, 0x000DBC79U, 0x00E5E510U, 0x002472C8U, 0x00BC3FBCU, 0x0011A8CDU, 0x00E5E5E5U, 0x00666666U, 0x00F14C4CU, 0x0023D18BU, 0x00F5F543U, 0x003B8EEAU, 0x00D670D6U, 0x0029B8DBU, 0x00FFFFFFU}; @@ -132,6 +135,7 @@ static void clks_tty_flush_dirty(void) { return; } + /* Dirty-row redraw: cheap as hell, but it keeps typing snappy. */ for (row = 0U; row < clks_tty_content_rows(); row++) { if (clks_tty_dirty_rows[row] != CLKS_TRUE) { continue; @@ -172,6 +176,7 @@ static u32 clks_tty_scrollback_clamped_offset(u32 tty_index) { static void clks_tty_scrollback_push_row(u32 tty_index, u32 row) { u32 slot = clks_tty_scrollback_head[tty_index]; + /* Save the line before scroll nukes it, because users always want it back. */ clks_memcpy(clks_tty_scrollback_cells[tty_index][slot], clks_tty_cells[tty_index][row], clks_tty_cols); clks_memcpy(clks_tty_scrollback_fg[tty_index][slot], clks_tty_cell_fg[tty_index][row], (usize)clks_tty_cols * sizeof(u32)); @@ -427,6 +432,7 @@ static void clks_tty_redraw_active(void) { u32 scroll_offset = clks_tty_scrollback_clamped_offset(tty_index); u32 start_doc = (scroll_count >= scroll_offset) ? (scroll_count - scroll_offset) : 0U; + /* Full redraw is expensive, but sometimes the least cursed option. */ clks_fb_clear(CLKS_TTY_BG); clks_tty_cursor_visible = CLKS_FALSE; clks_tty_dirty_reset(); @@ -471,6 +477,7 @@ static void clks_tty_redraw_active(void) { static void clks_tty_scroll_up(u32 tty_index) { u32 row; + /* Classic terminal move: throw top line away, pretend everything is fine. */ clks_tty_scrollback_push_row(tty_index, 0U); for (row = 1; row < clks_tty_content_rows(); row++) { @@ -1167,6 +1174,7 @@ void clks_tty_init(void) { return; } + /* If framebuffer lies here, the whole UI goes to hell immediately. */ info = clks_fb_info(); clks_tty_cell_width = clks_fb_cell_width(); clks_tty_cell_height = clks_fb_cell_height(); diff --git a/clks/kernel/runtime/exec.c b/clks/kernel/runtime/exec.c index 24debe4..6dfc66a 100644 --- a/clks/kernel/runtime/exec.c +++ b/clks/kernel/runtime/exec.c @@ -11,10 +11,13 @@ #include #include +/* Process runtime core: this is where tiny mistakes become giant explosions. */ + typedef u64 (*clks_exec_entry_fn)(void); #define CLKS_EXEC_RUN_STACK_BYTES (1024ULL * 1024ULL) #define CLKS_EXEC_MAX_PROCS 64U +/* 64 is a compromise: enough to be useful, small enough to not eat RAM for breakfast. */ #define CLKS_EXEC_MAX_DEPTH 16U #define CLKS_EXEC_PATH_MAX 192U #define CLKS_EXEC_ARG_LINE_MAX 256U @@ -206,6 +209,7 @@ static void clks_exec_serial_write_hex64(u64 value) { } static void clks_exec_log_info_serial(const char *message) { + /* Serial logs stay because they save our ass when TTY is dead. */ if (CLKS_CFG_EXEC_SERIAL_LOG == 0) { (void)message; return; @@ -886,6 +890,7 @@ static i32 clks_exec_proc_find_slot_by_pid(u64 pid) { static i32 clks_exec_proc_alloc_slot(void) { u32 i; + /* First pass: find free slot. Second pass: recycle corpses. Brutal but effective. */ for (i = 0U; i < CLKS_EXEC_MAX_PROCS; i++) { if (clks_exec_proc_table[i].used == CLKS_FALSE) { return (i32)i; @@ -1569,6 +1574,7 @@ static clks_bool clks_exec_dispatch_pending_once(void) { return CLKS_FALSE; } + /* One pending process per tick keeps latency sane and avoids scheduler chaos. */ for (i = 0U; i < CLKS_EXEC_MAX_PROCS; i++) { if (clks_exec_proc_table[i].used == CLKS_TRUE && clks_exec_proc_table[i].state == CLKS_EXEC_PROC_PENDING) { u64 ignored_status = (u64)-1; @@ -1600,6 +1606,7 @@ static clks_bool clks_exec_run_path_internal(const char *path, const char *argv_ *out_pid = (u64)-1; } + /* Count every request, even bad ones, so metrics don't lie to our face. */ clks_exec_requests++; if (path == CLKS_NULL || path[0] != '/') { @@ -2701,6 +2708,7 @@ u64 clks_exec_yield(void) { void clks_exec_tick(u64 tick) { (void)tick; + /* Background pump: boring, repetitive, absolutely necessary. */ (void)clks_exec_dispatch_pending_once(); } diff --git a/clks/kernel/runtime/syscall.c b/clks/kernel/runtime/syscall.c index cec03e9..9c18e47 100644 --- a/clks/kernel/runtime/syscall.c +++ b/clks/kernel/runtime/syscall.c @@ -18,6 +18,8 @@ #include #include +/* Yes, this file is a syscall kitchen sink and nobody is pretending otherwise. */ + #define CLKS_SYSCALL_LOG_MAX_LEN 191U #define CLKS_SYSCALL_PATH_MAX 192U #define CLKS_SYSCALL_NAME_MAX 96U @@ -180,6 +182,7 @@ static inline void clks_syscall_outw(u16 port, u16 value) { #endif static clks_bool clks_syscall_in_user_exec_context(void) { + /* If this says "no", we're basically in trust-me-bro kernel mode. */ return (clks_exec_is_running() == CLKS_TRUE && clks_exec_current_path_is_user() == CLKS_TRUE) ? CLKS_TRUE : CLKS_FALSE; } @@ -215,6 +218,7 @@ static clks_bool clks_syscall_copy_user_string(u64 src_addr, char *dst, usize ds return CLKS_FALSE; } + /* Byte-by-byte copy is slow as hell, but crashing on bad pointers is worse. */ while (i + 1U < dst_size) { u64 char_addr = src_addr + (u64)i; char ch; @@ -263,6 +267,7 @@ static u64 clks_syscall_copy_text_to_user(u64 dst_addr, u64 dst_size, const char copy_len = src_len; + /* Leave room for NUL, because "almost a string" is just pain. */ if (copy_len + 1U > (usize)dst_size) { copy_len = (usize)dst_size - 1U; } @@ -469,6 +474,7 @@ static u64 clks_syscall_fb_blit(u64 arg0) { } static u64 clks_syscall_kernel_version(u64 arg0, u64 arg1) { + /* Version query: tiny syscall, huge bike-shed potential. */ usize len = clks_strlen(CLKS_VERSION_STRING); return clks_syscall_copy_text_to_user(arg0, arg1, CLKS_VERSION_STRING, len); } @@ -2343,6 +2349,7 @@ u64 clks_syscall_dispatch(void *frame_ptr) { return (u64)-1; } + /* Giant switch, yeah. Ugly, explicit, and easy to grep at 3 AM. */ switch (id) { case CLKS_SYSCALL_LOG_WRITE: return clks_syscall_log_write(frame->rbx, frame->rcx); diff --git a/clks/kernel/storage/fs.c b/clks/kernel/storage/fs.c index 301a326..4493d23 100644 --- a/clks/kernel/storage/fs.c +++ b/clks/kernel/storage/fs.c @@ -6,6 +6,8 @@ #include #include +/* Tiny in-memory FS: simple enough to reason about, still easy to screw up. */ + #define CLKS_FS_MAX_NODES 512U #define CLKS_FS_PATH_MAX CLKS_RAMDISK_PATH_MAX @@ -47,6 +49,7 @@ static clks_bool clks_fs_normalize_external_path(const char *path, char *out_int in_pos++; } + /* Normalize aggressively; weird paths are where bugs and exploits like to party. */ while (path[in_pos] != '\0') { usize comp_start = in_pos; usize comp_len; @@ -71,6 +74,7 @@ static clks_bool clks_fs_normalize_external_path(const char *path, char *out_int continue; } + /* No parent traversal here. Not today, not ever. */ if (comp_len == 2U && path[comp_start] == '.' && path[comp_start + 1U] == '.') { return CLKS_FALSE; } @@ -99,6 +103,7 @@ static clks_bool clks_fs_normalize_external_path(const char *path, char *out_int } static clks_bool clks_fs_internal_in_temp_tree(const char *internal_path) { + /* Write access is fenced into /temp so random code can't trash the world. */ if (internal_path == CLKS_NULL) { return CLKS_FALSE; } @@ -125,6 +130,7 @@ static clks_bool clks_fs_internal_is_temp_file_path(const char *internal_path) { static i32 clks_fs_find_node_by_internal(const char *internal_path) { u16 i; + /* Linear scan is boring, but with this node count it's fine as hell. */ for (i = 0U; i < clks_fs_nodes_used; i++) { if (clks_fs_nodes[i].used == CLKS_FALSE) { continue; @@ -186,6 +192,7 @@ static clks_bool clks_fs_split_parent(const char *internal_path, char *parent_ou return CLKS_TRUE; } + /* Manual split is ugly, but it avoids allocator drama during path ops. */ for (i = len; i != 0U; i--) { if (internal_path[i - 1U] == '/') { usize parent_len = i - 1U;