ahrightos-kernel 0.1.0

RISC-V 64 Rust teaching kernel for AhRightOS.
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Process Management Module
//!
//! Chapter 6 - Process Management Experiment

pub mod pid;
pub use pid::ProcessId;

pub mod pcb;
pub use pcb::{ProcessControlBlock, ProcessState};

pub mod context;
pub use context::{
    ProcessContext, PROCESS_CONTEXT_SATP_OFFSET, PROCESS_CONTEXT_SEPC_OFFSET,
    PROCESS_CONTEXT_SSTATUS_OFFSET,
};

pub mod scheduler;
pub mod state; // PTA ex5: Rust enum+match exercise
pub use scheduler::RoundRobinScheduler;

pub mod elf;
pub mod switch;
pub mod user;
pub mod user_elf;

use crate::println;
use alloc::boxed::Box;
use core::sync::atomic::{AtomicBool, Ordering};
use lazy_static::lazy_static;
use spin::Mutex;

// ── Global scheduler ──────────────────────────────────────────────

lazy_static! {
    pub static ref SCHEDULER: Mutex<RoundRobinScheduler> = Mutex::new(RoundRobinScheduler::new(5));
}

static mut SCHEDULER_STARTED: bool = false;

// ── Global frame allocator (single-cpu, interrupt-safe) ─

use crate::memory::{PhysAddr, SimpleFrameAllocator};

static mut FRAME_ALLOCATOR: Option<SimpleFrameAllocator> = None;
static FRAME_ALLOCATOR_BUSY: AtomicBool = AtomicBool::new(false);

lazy_static! {
    static ref KERNEL_PAGE_TABLE_PADDR: Mutex<Option<usize>> = Mutex::new(None);
}

pub fn init_frame_allocator_from(kernel_end: usize) {
    let memory_end = 0x8000_0000usize + 128 * 1024 * 1024;
    let allocator = SimpleFrameAllocator::new(kernel_end, memory_end);
    unsafe {
        FRAME_ALLOCATOR = Some(allocator);
    }
}

pub fn set_kernel_page_table_paddr(paddr: PhysAddr) {
    *KERNEL_PAGE_TABLE_PADDR.lock() = Some(paddr.as_usize());
}

pub fn kernel_page_table_paddr() -> Option<PhysAddr> {
    let paddr = *KERNEL_PAGE_TABLE_PADDR.lock();
    paddr.map(PhysAddr::new)
}

#[allow(static_mut_refs)]
pub fn with_frame_allocator<F, R>(f: F) -> R
where
    F: FnOnce(&mut SimpleFrameAllocator) -> R,
{
    if FRAME_ALLOCATOR_BUSY.swap(true, Ordering::Acquire) {
        panic!("frame allocator reentered — interrupt-in-allocator detected");
    }
    let result = unsafe { f(FRAME_ALLOCATOR.as_mut().expect("frame allocator not set")) };
    FRAME_ALLOCATOR_BUSY.store(false, Ordering::Release);
    result
}

// ── Task trap ─────────────────────────────────────────────────────

extern "C" fn task_trap() -> ! {
    crate::serial_println!("[TASK] Kernel task returned unexpectedly — halting");
    crate::hlt_loop();
}

// ── sstatus helpers ───────────────────────────────────────────────

fn kernel_sstatus_init() -> usize {
    let raw: usize;
    unsafe {
        core::arch::asm!("csrr {}, sstatus", out(reg) raw);
    }
    (raw | (1 << 8) | (1 << 5)) & !(1 << 1) // SPP=1 SPIE=1 SIE=0
}

// ── Create kernel-level task ──────────────────────────────────────

/// Create a kernel task PCB backed by the supplied stack.
///
/// # Safety
///
/// `stack` must be a valid, uniquely owned 4 KiB task stack that remains
/// alive and unmoved for as long as the returned PCB can be scheduled.
pub unsafe fn create_kernel_task(
    pid: ProcessId,
    name: &'static str,
    entry: extern "C" fn() -> !,
    stack: *mut [u8; 4096],
) -> Box<ProcessControlBlock> {
    let mut pcb = ProcessControlBlock::new(pid, name, None);
    let ctx = pcb.context_mut();

    let stack_ptr = unsafe { (*stack).as_mut_ptr() };
    ctx.set_ra(task_trap as *const () as usize);
    ctx.set_sp(stack_ptr as usize + 4096);
    ctx.set_sepc(entry as usize);
    ctx.set_sstatus(kernel_sstatus_init());
    ctx.set_satp(riscv::register::satp::read().bits());

    Box::new(pcb)
}

// ── Bootstrap the first task ──────────────────────────────────────

/// # Safety
///
/// Caller must ensure `ctx` points to a valid, properly initialised
/// `ProcessContext` that matches the task the scheduler has selected
/// as current.  This function disables interrupts and jumps into
/// assembly (`__start_first_task`) which never returns.
pub unsafe fn start_first_task(ctx: *const ProcessContext) {
    SCHEDULER_STARTED = true;
    riscv::register::sstatus::clear_sie();

    // Set sscratch to the dedicated trap stack so traps find a clean stack.
    unsafe {
        core::arch::asm!("csrw sscratch, {}", in(reg) crate::interrupts::trap_stack_top());
    }

    switch::__start_first_task(ctx);
}

// ── Scheduling — called from timer interrupt ──────────────────────

/// Called on every timer tick with the current trap frame.
/// If the scheduler picks a different task, the trap frame is
/// updated in-place so that `trap_return` resumes the new task.
pub fn scheduler_tick(tf: &mut crate::interrupts::TrapFrame) {
    let mut sched = SCHEDULER.lock();

    // Save interrupted task's state from trap frame to its PCB
    if let Some(pcb) = sched.current_mut() {
        tf.save_to(pcb.context_mut());
    }

    // Run scheduler tick (may switch current task)
    sched.tick();

    // Load the (possibly new) current task's state into the trap frame
    if let Some(pcb) = sched.current() {
        tf.load_from(pcb.context());
    }
}

pub fn debug_scheduler_state() {
    let sched = SCHEDULER.lock();
    let stats = sched.stats();
    println!(
        "  [SCHED] current={:?}, ready={}, blocked={}, zombies={}, total={}, is_idle={}",
        sched.current().map(|p| (p.name(), p.pid().as_usize())),
        stats.ready,
        stats.blocked,
        stats.zombies,
        stats.total,
        sched.is_idle(),
    );
}

// ── Fork: deep-copy address space → new PCB → add to scheduler ──

/// Fork the current user process by deep-copying the parent's user pages.
/// Returns child PID (to parent) or 0 (to child).
pub fn fork_current(tf: &crate::interrupts::TrapFrame) -> isize {
    let (parent_pid, parent_satp) = {
        let sched = SCHEDULER.lock();
        let current = match sched.current() {
            Some(p) => p,
            None => return -1isize,
        };
        (current.pid(), current.context().satp)
    };

    let child_satp = match with_frame_allocator(|allocator| {
        crate::memory::address_space::deep_copy_user_address_space(parent_satp, allocator)
    }) {
        Ok(satp) => satp,
        Err(reason) => {
            crate::serial_println!("[FORK] address-space copy failed: {}", reason);
            return -1isize;
        }
    };

    // Build child PCB
    let child_pid = ProcessId::new();
    let mut child_pcb = ProcessControlBlock::new(child_pid, "child", Some(parent_pid));

    {
        let child_ctx = child_pcb.context_mut();
        // Copy parent registers from trap frame (x1-x31)
        child_ctx.x.copy_from_slice(&tf.x[1..]);
        child_ctx.x[9] = 0; // a0 = 0 for child
        child_ctx.sepc = tf.sepc.wrapping_add(4);
        // Return with SIE disabled in the saved status, but keep SPIE set so
        // sret re-enables timer preemption when the child reaches user mode.
        child_ctx.sstatus = (tf.sstatus | (1 << 5)) & !(1 << 1);
        child_ctx.satp = child_satp;
    }

    // Add child to parent's children list before queueing it.
    let mut sched = SCHEDULER.lock();
    if let Some(parent) = sched.current_mut() {
        if parent.pid() != parent_pid {
            return -1isize;
        }
        parent.add_child(child_pid);
    } else {
        return -1isize;
    }

    if sched.add_process(Box::new(child_pcb)).is_err() {
        if let Some(parent) = sched.current_mut() {
            parent.remove_child(child_pid);
        }
        return -1isize;
    }

    crate::serial_println!(
        "[FORK] parent={} child={} satp={:#x}",
        parent_pid.as_usize(),
        child_pid.as_usize(),
        child_satp
    );

    child_pid.as_usize() as isize
}

// ── Exec: replace process image with new ELF ─────────────────────

/// Replace the current process image with a new ELF loaded from `path`.
/// FD table is preserved; address space and context are rebuilt.
///
/// Current ownership contract: the scheduler/PCB stores the installed `satp`
/// in `ProcessContext`, but it does not yet own an `AddressSpace` object or
/// reclaim old user frames on exec/exit. `exec_current` therefore installs a
/// fresh page-table root and documents the remaining lifecycle gap instead of
/// pretending to provide full POSIX-style address-space replacement.
pub fn exec_current(path: &str) -> isize {
    use crate::fs::vfs::open_file;

    // Read ELF bytes from VFS
    let file = match open_file(path) {
        Ok(f) => f,
        Err(_) => return -1isize,
    };

    let mut elf_data = alloc::vec![0u8; 65536];
    let n = match file.lock().read(&mut elf_data) {
        Ok(n) => n,
        Err(_) => return -1isize,
    };
    elf_data.truncate(n);

    let elf = match elf::Elf::parse(&elf_data) {
        Ok(e) => e,
        Err(_) => return -1isize,
    };

    with_frame_allocator(|allocator| {
        // Use the boot-created kernel root, not the current user SATP saved by
        // the trap path. Copying from the old user root would preserve stale
        // user mappings across exec and can collide with the new ELF mappings.
        let kernel_paddr = match kernel_page_table_paddr() {
            Some(paddr) => paddr,
            None => return -1isize,
        };

        let new_addr_space =
            match user::create_user_address_space(&elf, &elf_data, allocator, kernel_paddr) {
                Ok(a) => a,
                Err(_) => return -1isize,
            };

        let new_satp = {
            let ppn = new_addr_space.page_table_paddr().as_usize() >> 12;
            (8usize << 60) | ppn
        };
        // The live process context owns only the SATP value today.  Page-table
        // frames are allocator-backed and remain valid, while AddressSpace
        // metadata is not retained by PCB yet; add a PCB-owned memory object
        // before implementing frame reclamation or demand paging.

        let user_stack_top = 0x7FFF_F000usize;

        let mut sched = SCHEDULER.lock();
        if let Some(pcb) = sched.current_mut() {
            let ctx = pcb.context_mut();
            ctx.x = [0; 31];
            ctx.set_ra(0);
            ctx.set_sp(user_stack_top);
            ctx.set_sepc(elf.entry);
            // SPP=0 (User), SPIE=1, SUM=1
            let raw: usize;
            unsafe {
                core::arch::asm!("csrr {}, sstatus", out(reg) raw);
            }
            ctx.set_sstatus((raw | (1 << 5) | (1 << 18)) & !((1 << 8) | (1 << 1)));
            ctx.set_satp(new_satp);
        }

        crate::serial_println!(
            "[EXEC] Loaded ELF '{}' entry={:#x} satp={:#x}",
            path,
            elf.entry,
            new_satp
        );
        0
    })
}

/// Replace the current image and, on success, copy the new process context into
/// the live trap frame that `trap_return` will restore.
///
/// `exec_current()` updates the PCB-owned `ProcessContext`.  That is sufficient
/// for future scheduler switches, but the current syscall returns through the
/// already-saved [`TrapFrame`].  A successful exec therefore must *not* advance
/// the old `sepc`; it must install the new context into the live frame so sret
/// enters the new ELF image with its new SATP.
pub fn exec_current_into_trap_frame(tf: &mut crate::interrupts::TrapFrame, path: &str) -> isize {
    let ret = exec_current(path);
    if ret != 0 {
        return ret;
    }

    let sched = SCHEDULER.lock();
    if let Some(pcb) = sched.current() {
        tf.load_from(pcb.context());
        0
    } else {
        -1
    }
}

// ── Exit: mark as Zombie and switch to next task ────────────────

/// Exit the current process. Does NOT return — jumps to `trap_return`.
pub fn exit_current_and_switch(tf: &mut crate::interrupts::TrapFrame, exit_code: i32) -> ! {
    let switched = {
        let mut sched = SCHEDULER.lock();
        let parent_pid = if let Some(pcb) = sched.current_mut() {
            pcb.set_exit_code(exit_code);
            crate::serial_println!(
                "[EXIT] PID={} exit_code={} parent={:?}",
                pcb.pid().as_usize(),
                exit_code,
                pcb.parent_pid()
            );
            pcb.parent_pid()
        } else {
            None
        };
        // Unblock parent if it's waiting (state == Blocked)
        if let Some(ppid) = parent_pid {
            sched.unblock_process(ppid);
        }
        sched.schedule();
        if let Some(pcb) = sched.current() {
            tf.load_from(pcb.context());
            true
        } else {
            false
        }
    };
    if !switched {
        crate::serial_println!("[EXIT] no runnable process remains; halting");
        crate::hlt_loop();
    }
    // trap_return reads the TrapFrame from sp, but this function's
    // prologue has moved sp ~0x200 bytes below the frame.  Restore sp
    // from the tf pointer so the assembly loads the correct registers.
    let tf_addr = tf as *const _ as usize;
    unsafe {
        core::arch::asm!(
            "mv sp, {tf}",
            "j trap_return",
            tf = in(reg) tf_addr,
            options(noreturn)
        );
    }
}

// ── PTA injection point ─────────────────────────────────────────
// STUDENT_CODE_HERE

// ── Wait: collect zombie child ──────────────────────────────────

/// Wait for any child to exit. If a zombie exists, returns its PID.
/// If children exist but none are zombies, blocks the current process
/// and jumps directly to trap_return (does NOT return).
/// If no ready task can run while the parent is blocked, returns EBUSY
/// instead of parking the parent without a runnable handoff.
/// Returns -1 if no children exist.
pub fn wait_current(tf: &mut crate::interrupts::TrapFrame, exit_code_ptr: usize) -> isize {
    let mut sched = SCHEDULER.lock();

    let parent_pid = match sched.current() {
        Some(p) => p.pid(),
        None => return -1,
    };

    // Check for zombie children
    if let Some((child_pid, code)) = sched.find_zombie_child(parent_pid) {
        let child_pid_value = child_pid.as_usize();
        drop(sched);

        if exit_code_ptr != 0
            && crate::syscall::user_ptr::raw_write_value(exit_code_ptr, code).is_err()
        {
            return -1;
        }

        let mut sched = SCHEDULER.lock();
        sched.remove_zombie(child_pid);
        drop(sched);

        crate::serial_println!(
            "[WAIT] Reaped zombie PID={} exit_code={}",
            child_pid_value,
            code
        );
        return child_pid_value as isize;
    }

    // Check if there are living children to wait for
    if sched.has_living_children(parent_pid) {
        if sched.ready_count() == 0 {
            crate::serial_println!(
                "[WAIT] PID={} has living children but no runnable handoff task",
                parent_pid.as_usize()
            );
            return crate::errno::EBUSY;
        }

        // Advance sepc past the ecall before saving context, so the
        // parent resumes at the next instruction when it is woken up.
        tf.sepc = tf.sepc.wrapping_add(4);
        // Block current and manually pick next Runnable process
        if let Some(pcb) = sched.current_mut() {
            tf.save_to(pcb.context_mut());
            pcb.set_state(ProcessState::Blocked)
                .expect("running process can block on wait");
        }
        // Manually pick next Ready task from ready queue
        let next = sched.pick_next_ready();
        if let Some(pcb) = next {
            tf.load_from(pcb.context());
        }
        drop(sched);
        let tf_addr = tf as *const _ as usize;
        unsafe {
            core::arch::asm!(
                "mv sp, {tf}",
                "j trap_return",
                tf = in(reg) tf_addr,
                options(noreturn)
            );
        }
    }

    -1isize
}