summaryrefslogtreecommitdiff
path: root/na/kernel/task.cc
blob: 9f09d62f1e5adea1e48e8a2138b7f2991e98c63d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * Task Management — create, exit, destroy, kthread
 *
 * Implements task lifecycle management: kernel thread creation,
 * task exit (zombie transition), and resource reclamation.
 *
 * Reference: docs/specs/task.md §4-§6
 */

#include <lume/task.h>
#include <lume/sched.h>
#include <lume/pmm.h>
#include <lume/addr.h>
#include <lume/new.h>
#include <lume/kprintf.h>
#include <lume/klog.h>
#include <lume/vmm.h>
#include <lume/errno.h>
#include <lume/pid.h>
#include <arch/cpu.h>
#include <arch/context.h>
#include <kernel/sync/spinlock.h>

/* ======================================================================
 * Forward declarations
 * ====================================================================== */

extern "C" void kthread_entry_trampoline();
extern "C" void ret_from_fork();

/* ======================================================================
 * Global task list
 * ====================================================================== */

list_node g_all_tasks;
Spinlock g_task_list_lock;
int32 g_nr_tasks = 0;

/* Special tasks */
TaskControlBlock* g_kernel_task = nullptr;  /* PID 0 container */
TaskControlBlock* g_init_task = nullptr;    /* PID 1 (future) */

/* ======================================================================
 * task_init() — called once from kernel_main before sched_init
 * ====================================================================== */

void task_init()
{
    g_all_tasks.init();
    g_task_list_lock.init("task_list");
    g_pid_alloc.init();

    printk(KERN_INFO "init: task_init done (pid_alloc ready)\n");
}

/* ======================================================================
 * Internal: allocate TCB + kernel stack
 * ====================================================================== */

static TaskControlBlock* alloc_task(const char* name, uint8 priority,
                                    uint8 flags)
{
    auto* task = new TaskControlBlock;
    if (!task) return nullptr;

    task->context = {};

    /* Allocate 8KB kernel stack (order-1 = 2 pages) */
    Page* stack_page = pmm_alloc_pages(1);
    if (!stack_page) {
        delete task;
        return nullptr;
    }
    task->kstack_pa = page_to_pa(stack_page).raw;
    task->kstack_va = pa_to_va(phys_addr(task->kstack_pa)).raw;

    /* Write stack canary at bottom */
    *reinterpret_cast<uint64*>(task->kstack_va) = kStackCanary;

    /* Basic fields */
    task->name = name;
    task->priority = priority;
    task->time_slice = priority_to_timeslice(priority);
    task->state = TaskState::Runnable;
    task->cpu_id = static_cast<uint8>(arch::cpu::id());
    task->flags = flags;
    task->exit_code = 0;
    task->parent = nullptr;
    task->mm = nullptr;
    task->trapframe = nullptr;

    /* Init all list nodes */
    task->run_link.init();
    task->all_link.init();
    task->children.init();
    task->sibling.init();
    task->wait_chld.init("wait_chld");

    /* Default sched class */
    extern const SchedClass g_sched_class_o1;
    task->sched_class = &g_sched_class_o1;
    task->sched_entity = &task->run_link;

    return task;
}

/* Add task to global task list */
static void task_list_add(TaskControlBlock* task)
{
    LockGuard guard(g_task_list_lock);

    list_node* head = &g_all_tasks;
    task->all_link.prev = head->prev;
    task->all_link.next = head;
    head->prev->next = &task->all_link;
    head->prev = &task->all_link;

    g_nr_tasks++;
}

/* Remove task from global task list */
static void task_list_remove(TaskControlBlock* task)
{
    LockGuard guard(g_task_list_lock);

    task->all_link.prev->next = task->all_link.next;
    task->all_link.next->prev = task->all_link.prev;
    task->all_link.init();

    if (g_nr_tasks > 0)
        g_nr_tasks--;
}

/* ======================================================================
 * kthread_create — create a kernel thread
 * ====================================================================== */

TaskControlBlock* kthread_create(const char* name,
                                 void (*entry)(void*),
                                 void* arg,
                                 uint8 priority)
{
    TaskControlBlock* task = alloc_task(name, priority, TF_KTHREAD);
    if (!task) return nullptr;

    /* Allocate PID */
    task->pid = g_pid_alloc.alloc();
    if (task->pid < 0) {
        /* PID exhausted — clean up */
            pmm_free_pages(pa_to_page(phys_addr(task->kstack_pa)), 1);
        delete task;
        return nullptr;
    }
    task->tgid = task->pid;

    /* Set up context for kthread_entry_trampoline */
    uint64 stack_top = task->kstack_va + kKernelStackSize;
    task->context = {};
    task->context.ra = reinterpret_cast<uint64>(kthread_entry_trampoline);
    task->context.sp = stack_top;
    task->context.s[0] = reinterpret_cast<uint64>(entry);
    task->context.s[1] = reinterpret_cast<uint64>(arg);

    /* Parent is kernel_task (PID 0) */
    task->parent = g_kernel_task;
    if (g_kernel_task) {
        LockGuard guard(g_task_list_lock);
        list_node* head = &g_kernel_task->children;
        task->sibling.prev = head->prev;
        task->sibling.next = head;
        head->prev->next = &task->sibling;
        head->prev = &task->sibling;
    }

    /* Add to global list and enqueue */
    task_list_add(task);
    sched_enqueue(task);

    return task;
}

/* ======================================================================
 * task_exit — transition current task to Zombie
 * ====================================================================== */

void task_exit(int code)
{
    TaskControlBlock* curr = current_task();
    if (!curr) kernel_panic("task_exit: no current task");

    curr->exit_code = code;

    /* Release user address space if we're the last reference */
    if (curr->mm) {
        curr->mm->destroy();
        curr->mm = nullptr;
    }

    /* Reparent children to init (or kernel_task if no init yet) */
    TaskControlBlock* foster = g_init_task ? g_init_task : g_kernel_task;
    if (foster && !curr->children.is_empty()) {
        LockGuard guard(g_task_list_lock);

        list_node* child_node = curr->children.next;
        while (child_node != &curr->children) {
            list_node* next_sib = child_node->next;
            auto* child = list_entry<TaskControlBlock,
                                     &TaskControlBlock::sibling>(child_node);
            child->parent = foster;

            /* Move sibling link to foster's children */
            child_node->prev->next = child_node->next;
            child_node->next->prev = child_node->prev;

            list_node* fhead = &foster->children;
            child_node->prev = fhead->prev;
            child_node->next = fhead;
            fhead->prev->next = child_node;
            fhead->prev = child_node;

            child_node = next_sib;
        }
        curr->children.init();
    }

    /* Transition to Zombie — TCB + kstack preserved for parent wait() */
    curr->state = TaskState::Zombie;

    /* Wake parent->wait_chld queue */
    if (curr->parent) {
        curr->parent->wait_chld.wake_all();
    }

    /* Yield CPU — never returns */
    schedule();

    __builtin_unreachable();
}

void task_kill_current(int sig)
{
    int code = (sig & 0x7F) | kExitCodeSignalFlag;
    task_exit(code);
}

/* ======================================================================
 * task_destroy — fully reclaim a Zombie task (called by waitpid)
 * ====================================================================== */

void task_destroy(TaskControlBlock* task)
{
    /* Remove from global list */
    task_list_remove(task);

    /* Remove from parent's children */
    task->sibling.prev->next = task->sibling.next;
    task->sibling.next->prev = task->sibling.prev;
    task->sibling.init();

    /* Free PID */
    g_pid_alloc.free(task->pid);

    /* Free kernel stack */
    if (task->kstack_pa != 0) {
           pmm_free_pages(pa_to_page(phys_addr(task->kstack_pa)), 1);
    }

    /* Free TCB */
    delete task;
}

/* ======================================================================
 * task_wait — sleep until a child exits, then reclaim it
 * Returns PID of exited child, or -ECHILD if no children exist.
 * ====================================================================== */

int32 task_wait(int32* exit_code)
{
    TaskControlBlock* curr = current_task();
    if (!curr) return -EINVAL;

    while (true) {
        TaskControlBlock* zombie = nullptr;

        /* Look for a Zombie child */
        g_task_list_lock.acquire();
        if (curr->children.is_empty()) {
            g_task_list_lock.release();
            return -ECHILD;
        }

        list_node* node = curr->children.next;
        while (node != &curr->children) {
            auto* child = list_entry<TaskControlBlock, &TaskControlBlock::sibling>(node);
            if (child->state == TaskState::Zombie) {
                zombie = child;
                break;
            }
            node = node->next;
        }
        g_task_list_lock.release();

        if (zombie) {
            int32 pid = zombie->pid;
            if (exit_code) {
                /* In a real kernel, we copy_to_user */
                *exit_code = zombie->exit_code;
            }
            task_destroy(zombie);
            return pid;
        }

        /* Sleep if no child has exited yet */
        curr->wait_chld.sleep(&g_task_list_lock);
    }
}

/* ======================================================================
 * task_fork — clone current task
 * ====================================================================== */

int32 task_fork(arch::TrapFrame* tf)
{
    TaskControlBlock* curr = current_task();
    if (!curr) return -EINVAL;

    TaskControlBlock* child = alloc_task("forked", curr->priority, 0);
    if (!child) return -ENOMEM;

    /* Allocate PID */
    child->pid = g_pid_alloc.alloc();
    if (child->pid < 0) {
           pmm_free_pages(pa_to_page(phys_addr(child->kstack_pa)), 1);
        delete child;
        return -ENOMEM;
    }
    child->tgid = child->pid;

    /* Copy VmSpace */
    if (curr->mm) {
        child->mm = vmm_clone(curr->mm);
        if (!child->mm) {
            g_pid_alloc.free(child->pid);
            pmm_free_pages(pa_to_page(phys_addr(child->kstack_pa)), 1);
            delete child;
            return -ENOMEM;
        }
    }

    /* Copy TrapFrame to the child's kernel stack */
    uint64 tf_addr = child->kstack_va + kKernelStackSize - sizeof(arch::TrapFrame);
    child->trapframe = reinterpret_cast<arch::TrapFrame*>(tf_addr);
    *child->trapframe = *tf;

    /* Child returns 0 from fork */
    child->trapframe->a[0] = 0;

    /* Set up context for ret_from_fork */
    uint64 stack_top = tf_addr; // Context goes below TrapFrame conceptually, but we just set SP
    child->context = {};
    child->context.ra = reinterpret_cast<uint64>(ret_from_fork);
    child->context.sp = stack_top;
    child->context.s[0] = tf_addr; /* Passed to a0 in ret_from_fork */

    /* Add to task tree */
    child->parent = curr;
    {
        LockGuard guard(g_task_list_lock);
        list_node* head = &curr->children;
        child->sibling.prev = head->prev;
        child->sibling.next = head;
        head->prev->next = &child->sibling;
        head->prev = &child->sibling;
    }

    /* Add to global list and enqueue */
    task_list_add(child);
    sched_enqueue(child);

    return child->pid;
}


/* ======================================================================
 * task_check_stack_canary
 * ====================================================================== */

void task_check_stack_canary(TaskControlBlock* task)
{
    if (!task || task->kstack_va == 0) return;

    auto* canary = reinterpret_cast<uint64*>(task->kstack_va);
    if (*canary != kStackCanary) {
        kernel_panic("kernel stack overflow detected");
    }
}