[refactor] 整理注释

This commit is contained in:
2026-06-06 10:31:20 +08:00
Unverified
parent a9ba4457c6
commit 30d48d2881
19 changed files with 177 additions and 192 deletions
+25 -28
View File
@@ -6,15 +6,15 @@
#include <common.h>
#include <serial.h>
// Assembly: context_switch(UINT64* old_rsp, UINT64 new_rsp)
// 汇编函数:context_switch(UINT64* old_rsp, UINT64 new_rsp)
extern "C" void context_switch(UINT64* old_rsp, UINT64 new_rsp);
static task_t g_tasks[TASK_MAX];
static UINT32 g_task_count = 0;
static task_t* g_current = NULL;
static task_t* g_task_list = NULL; // circular linked list head
static task_t* g_task_list = NULL; // 循环链表头
// Trampoline: first thing a new task runs after context_switch.
// 跳板函数:新任务在 context_switch 后首先执行的函数
static void (*g_task_entries[TASK_MAX])(void);
extern "C" void task_entry_trampoline() {
@@ -37,7 +37,7 @@ task_t* task_create(const char* name, void (*entry)(void)) {
g_task_entries[id] = entry;
// Allocate kernel stack
// 分配内核栈
UINTN stack_pages = TASK_STACK_SIZE / PAGE_SIZE;
void* stack = pmm_alloc_pages(stack_pages);
if (!stack) {
@@ -52,24 +52,21 @@ task_t* task_create(const char* name, void (*entry)(void)) {
task->stack_base = stack;
task->time_slice = TIME_SLICE_DEFAULT;
// Copy name
// 复制任务名称
str_copy(task->name, name, TASK_NAME_LEN);
// Set up initial stack for first context_switch into this task.
// Stack grows downward. context_switch will pop 6 regs then ret.
// 设置首次 context_switch 时的初始栈
// 栈向下增长。context_switch 会弹出 6 个寄存器然后 ret
//
// Layout (high addr -> low addr):
// [stack + TASK_STACK_SIZE] <- top
// return addr = task_entry_trampoline (ret goes here)
// 布局(高地址 → 低地址):
// [stack + TASK_STACK_SIZE] <- 栈顶
// 返回地址 = task_entry_trampolineret 跳转到这里)
// rbx = 0
// rbp = 0
// r12 = 0
// r13 = 0
// r14 = 0
// r15 = 0 <- RSP points here initially
//
// When preempted by timer IRQ, the ISR stub saves a full trap_frame
// on the task's stack — that layout is only created by hardware+ISR.
// r15 = 0 <- RSP 初始指向这里
//
UINT64* sp = (UINT64*)((UINT8*)stack + TASK_STACK_SIZE);
@@ -84,7 +81,7 @@ task_t* task_create(const char* name, void (*entry)(void)) {
task->rsp = (UINT64)sp;
// Insert into circular linked list
// 插入循环链表
if (g_task_list == NULL) {
task->next = task;
g_task_list = task;
@@ -103,7 +100,7 @@ task_t* task_create(const char* name, void (*entry)(void)) {
return task;
}
// Find next READY task in the circular list, starting from g_current->next
// 在循环链表中查找下一个就绪任务
static task_t* find_next_ready(void) {
if (g_current == NULL || g_task_list == NULL) return NULL;
@@ -117,7 +114,7 @@ static task_t* find_next_ready(void) {
next = next->next;
} while (next != start);
return NULL; // no READY tasks
return NULL; // 没有就绪任务
}
void yield(void) {
@@ -137,27 +134,27 @@ void yield(void) {
context_switch(&cur->rsp, next->rsp);
}
// Timer tick handler — called from PIT IRQ 0
// 定时器 tick 处理 — 由 PIT IRQ 0 调用
void scheduler_tick(void) {
if (g_current == NULL) return;
// Decrement time slice
// 递减时间片
if (g_current->time_slice > 0) {
g_current->time_slice--;
}
// If time slice expired, preempt
// 时间片用完则抢占
if (g_current->time_slice == 0) {
task_t* cur = g_current;
task_t* next = find_next_ready();
if (next == NULL || next == cur) {
// No other task ready, or only this task — reload time slice
// 没有其他就绪任务,或仅此一个 — 重置时间片
cur->time_slice = TIME_SLICE_DEFAULT;
return;
}
// Preempt
// 抢占
cur->state = TASK_STATE_READY;
cur->time_slice = TIME_SLICE_DEFAULT;
next->state = TASK_STATE_RUNNING;
@@ -174,7 +171,7 @@ void scheduler_run(void) {
return;
}
// Find first READY task
// 查找第一个就绪任务
task_t* start = g_task_list->next;
task_t* t = start;
do {
@@ -197,12 +194,12 @@ void scheduler_run(void) {
serial_write(t->name);
serial_write("'\n");
// First context switch — switch to the task's stack
// This will never return (until all tasks terminate)
// 首次上下文切换 — 切换到任务栈
// 此后不会返回(直到所有任务终止)
UINT64 dummy_rsp;
context_switch(&dummy_rsp, t->rsp);
// We only return here when ALL tasks are terminated
// 只有所有任务终止后才会返回到这里
serial_write("SCHED: all tasks finished\n");
while (1) ASM ("hlt");
}
@@ -216,10 +213,10 @@ void task_exit(void) {
g_current->state = TASK_STATE_TERMINATED;
// Yield to next task — we won't come back
// 让出 CPU 给下一个任务 — 不会回来
yield();
// Should never reach here
// 不应到达此处
while (1) ASM ("hlt");
}