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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
//! SPDX-License-Identifier: AGPL-3.0-or-later
//! Copyright 2025 Ahmad Ali Parr / SnapKitty οΏ½ https://github.com/SNAPKITTYWEST/phaser-ags
//! ═══════════════════════════════════════════════════════════════════
//!  PHASER AGS β€” Process management (complete, no stubs)
//!
//!  - Full PCB with kernel stack, user stack, page table
//!  - Round-robin preemptive scheduler via timer tick
//!  - Context switch through trap frame save/restore
//!  - Process creation with proper initial register state
//!  - Process cleanup (free all resources on exit/kill)
//! ═══════════════════════════════════════════════════════════════════

const std = @import("std");
const driver = @import("driver.zig");
const trap = @import("trap.zig");
const memory = @import("memory.zig");

const MAX_PROCS: usize = 16;
const KSTACK_PAGES: usize = 2;    // 8 KB kernel stack
const USTACK_PAGES: usize = 4;    // 16 KB user stack

// ─── Process states ──────────────────────────────────────────────

const ProcState = enum(u8) {
    unused    = 0,
    runnable  = 1,
    running   = 2,
    sleeping  = 3,
    zombie    = 4,
};

// ─── Process Control Block ───────────────────────────────────────

pub const Process = struct {
    pid:        u16,
    state:      ProcState,
    ksp:        u64,              // Saved kernel stack pointer (trap frame on it)
    kstack:     u32,             // Kernel stack base address
    page_root:  *memory.PageTable,
    entry:      u32,
    quantum:    u32,             // Ticks remaining in time slice
    total_ticks: u64,           // Total ticks this process has run
    exit_code:  i32,            // Exit code (valid when zombie)
    name:       [16]u8,
    // File descriptor table
    fds:        [16]?FdEntry,
};

pub const FdEntry = struct {
    kind: FdKind,
    dev: ?*const DevOps,
    offset: u64,
    flags: u32,

    pub const FdKind = enum(u8) {
        none = 0,
        device = 1,
        pipe = 2,
        file = 3,
    };
};

pub const DevOps = struct {
    read: *const fn ([]u8) usize,
    write: *const fn ([]const u8) usize,
    close: *const fn () void,
};

// ─── Global process table ────────────────────────────────────────

var procs: [MAX_PROCS]Process = undefined;
var current_pid: u16 = 0;
var next_pid: u16 = 1;

// ─── Initialize process table ────────────────────────────────────

pub fn init() void {
    for (&procs) |*p| {
        p.* = .{
            .pid = 0,
            .state = .unused,
            .ksp = 0,
            .kstack = 0,
            .page_root = undefined,
            .entry = 0,
            .quantum = 0,
            .total_ticks = 0,
            .exit_code = 0,
            .name = std.mem.zeroes([16]u8),
            .fds = .{null} ** 16,
        };
    }
    driver.Uart.puts("Process table: ");
    driver.Uart.putHex(MAX_PROCS);
    driver.Uart.puts(" slots initialized\n");
}

// ─── Allocate a free PCB slot ────────────────────────────────────

fn allocSlot() ?*Process {
    for (&procs) |*p| {
        if (p.state == .unused) {
            p.pid = next_pid;
            next_pid +|= 1;
            if (next_pid == 0) next_pid = 1;  // skip 0
            return p;
        }
    }
    return null;
}

// ─── Create a new process ────────────────────────────────────────

pub fn create(entry: u32, name: []const u8) ?*Process {
    const proc = allocSlot() orelse {
        driver.Uart.puts("proc: table full\n");
        return null;
    };

    // Allocate kernel stack
    const kstack = memory.allocRange(KSTACK_PAGES) orelse {
        proc.state = .unused;
        driver.Uart.puts("proc: kstack alloc failed\n");
        return null;
    };
    proc.kstack = kstack;

    // Set up initial trap frame at top of kernel stack
    const tf_addr = kstack + KSTACK_PAGES * memory.PAGE_SIZE - @sizeOf(trap.TrapFrame);
    const tf: *trap.TrapFrame = @ptrFromInt(tf_addr);

    // Zero the entire trap frame
    const tf_bytes: [*]u8 = @ptrCast(tf);
    var i: usize = 0;
    while (i < @sizeOf(trap.TrapFrame)) : (i += 1) {
        tf_bytes[i] = 0;
    }

    tf.mepc = entry;
    tf.mstatus = 0x00001880;  // MPP=M (bits 12:11=11), MIE=0 (bit 3)
    // We run everything in M-mode for now, so MPP=M

    // Allocate user stack
    const ustack = memory.allocRange(USTACK_PAGES) orelse {
        memory.freeRange(kstack, KSTACK_PAGES);
        proc.state = .unused;
        driver.Uart.puts("proc: ustack alloc failed\n");
        return null;
    };
    tf.s0 = 0;                         // frame pointer
    tf.sp = ustack + USTACK_PAGES * memory.PAGE_SIZE;  // stack grows down

    // Create address space
    const root = memory.createAddressSpace() orelse {
        memory.freeRange(kstack, KSTACK_PAGES);
        memory.freeRange(ustack, USTACK_PAGES);
        proc.state = .unused;
        driver.Uart.puts("proc: page table alloc failed\n");
        return null;
    };

    // Map user stack in the process's page table
    var pi: u32 = 0;
    while (pi < USTACK_PAGES) : (pi += 1) {
        const upage = ustack + pi * memory.PAGE_SIZE;
        const uva = upage;  // Identity for M-mode
        if (!memory.mapPage(root, uva, upage, memory.PTE_RW)) {
            driver.Uart.puts("proc: ustack map failed\n");
            // Clean up on failure
            memory.destroyAddressSpace(root);
            memory.freeRange(kstack, KSTACK_PAGES);
            memory.freeRange(ustack, USTACK_PAGES);
            proc.state = .unused;
            return null;
        }
    }

    proc.page_root = root;
    proc.ksp = tf_addr;  // Trap frame is on kernel stack
    proc.entry = entry;
    proc.quantum = DEFAULT_QUANTUM;
    proc.total_ticks = 0;
    proc.exit_code = 0;
    proc.state = .runnable;

    // Copy name
    const len = @min(name.len, 15);
    @memcpy(proc.name[0..len], name[0..len]);
    proc.name[len] = 0;

    // Set up default file descriptors: stdin=0, stdout=1, stderr=2
    const uart_dev = DevOps{
        .read = uartDevRead,
        .write = uartDevWrite,
        .close = uartDevClose,
    };
    // We store the dev ops pointer β€” it's a global, safe to point to
    proc.fds[0] = FdEntry{ .kind = .device, .dev = &uart_read_ops, .offset = 0, .flags = 0 };
    proc.fds[1] = FdEntry{ .kind = .device, .dev = &uart_write_ops, .offset = 0, .flags = 0 };
    proc.fds[2] = FdEntry{ .kind = .device, .dev = &uart_write_ops, .offset = 0, .flags = 0 };

    driver.Uart.puts("Created proc ");
    driver.Uart.putHex(proc.pid);
    driver.Uart.putc(' ');
    driver.Uart.puts(&proc.name);
    driver.Uart.puts(" entry=");
    driver.Uart.putHex(entry);
    driver.Uart.putc('\n');

    return proc;
}

// ─── UART device operations ──────────────────────────────────────

var uart_read_ops = DevOps{
    .read = uartDevRead,
    .write = uartDevWrite,
    .close = uartDevClose,
};
var uart_write_ops = DevOps{
    .read = uartDevRead,
    .write = uartDevWrite,
    .close = uartDevClose,
};

fn uartDevRead(buf: []u8) usize {
    var i: usize = 0;
    while (i < buf.len) {
        if ((driver.regRead(u32, driver.MemoryMap.UART0_BASE + 0x14) & 0x01) != 0) {
            buf[i] = driver.Uart.getc();
            i += 1;
        } else break;
    }
    return i;
}

fn uartDevWrite(buf: []const u8) usize {
    driver.Uart.puts(buf);
    return buf.len;
}

fn uartDevClose() void {}

// ─── Get current process ─────────────────────────────────────────

pub fn getCurrent() ?*Process {
    if (current_pid == 0) return null;
    for (&procs) |*p| {
        if (p.pid == current_pid and p.state == .running) return p;
    }
    return null;
}

pub fn setCurrentPid(pid: u16) void {
    current_pid = pid;
}

pub fn getByPid(pid: u16) ?*Process {
    for (&procs) |*p| {
        if (p.pid == pid and p.state != .unused) return p;
    }
    return null;
}

// ─── Default time slice ──────────────────────────────────────────

const DEFAULT_QUANTUM: u32 = 10;  // 10 timer ticks

// ─── Round-robin scheduler ───────────────────────────────────────

pub fn schedule() ?*Process {
    // Find the currently running process index to start search after it
    var start_idx: usize = 0;
    if (current_pid != 0) {
        for (&procs, 0..) |p, i| {
            if (p.pid == current_pid) {
                start_idx = @intCast((i + 1) % MAX_PROCS);
                break;
            }
        }
    }

    // Scan for runnable process (round-robin from start_idx)
    var i: usize = 0;
    while (i < MAX_PROCS) : (i += 1) {
        const idx = (start_idx + i) % MAX_PROCS;
        if (procs[idx].state == .runnable) {
            return &procs[idx];
        }
    }

    // All runnable procs exhausted their quanta β€” refresh
    var found: ?*Process = null;
    for (&procs) |*p| {
        if (p.state == .runnable) {
            p.quantum = DEFAULT_QUANTUM;
            if (found == null) found = p;
        }
    }
    return found;
}

// ─── Scheduler tick β€” called from timer interrupt ────────────────

pub fn schedTick() void {
    if (getCurrent()) |p| {
        if (p.quantum > 0) p.quantum -= 1;
        p.total_ticks += 1;
    }
}

// ─── Kill a process and free all resources ───────────────────────

pub fn kill(pid: u16) void {
    const p = getByPid(pid) orelse return;

    // Close all open file descriptors
    for (&p.fds) |*fd| {
        if (fd.*) |*entry| {
            if (entry.dev) |dev| {
                dev.close();
            }
            fd.* = null;
        }
    }

    // Free user stack
    const ustack_base = p.kstack + KSTACK_PAGES * memory.PAGE_SIZE - @sizeOf(trap.TrapFrame);
    // The user stack is tracked in the trap frame's sp field
    // We need to free it β€” but we need to figure out the ustack base from sp
    // Since sp = ustack + USTACK_PAGES * PAGE_SIZE, ustack = sp - USTACK_PAGES * PAGE_SIZE
    // However, the trap frame is on the kernel stack, and we've already set up
    // the sp in the initial trap frame. We need to recover it.
    // Read the saved sp from the trap frame
    const tf: *trap.TrapFrame = @ptrFromInt(@intCast(p.ksp));
    const saved_sp = tf.sp;
    if (saved_sp != 0) {
        const ustack = @intCast(saved_sp) - USTACK_PAGES * memory.PAGE_SIZE;
        memory.freeRange(ustack, USTACK_PAGES);
    }

    // Free kernel stack
    memory.freeRange(p.kstack, KSTACK_PAGES);

    // Destroy page table and any user pages
    memory.destroyAddressSpace(p.page_root);

    // Mark as zombie (parent could collect exit code)
    p.exit_code = 1;
    p.state = .zombie;

    if (current_pid == pid) {
        current_pid = 0;
    }

    driver.Uart.puts("Killed proc ");
    driver.Uart.putHex(pid);
    driver.Uart.putc('\n');
}

// ─── Exit current process ────────────────────────────────────────

pub fn exit(code: i32) void {
    if (getCurrent()) |p| {
        p.exit_code = code;
        kill(p.pid);
        trap.requestReschedule();
    }
}

// ─── Yield current process ───────────────────────────────────────

pub fn yieldExecution() void {
    if (getCurrent()) |p| {
        p.quantum = 0;  // Force reschedule
    }
    trap.requestReschedule();
}

// ─── Sleep current process ───────────────────────────────────────

pub fn sleep() void {
    if (getCurrent()) |p| {
        p.state = .sleeping;
    }
    trap.requestReschedule();
}

// ─── Wake a sleeping process ─────────────────────────────────────

pub fn wake(pid: u16) void {
    if (getByPid(pid)) |p| {
        if (p.state == .sleeping) {
            p.state = .runnable;
            p.quantum = DEFAULT_QUANTUM;
        }
    }
}

// ─── Reap zombie processes ───────────────────────────────────────

pub fn reapZombies() u32 {
    var count: u32 = 0;
    for (&procs) |*p| {
        if (p.state == .zombie) {
            p.state = .unused;
            p.pid = 0;
            count += 1;
        }
    }
    return count;
}

// ─── Get fd for a process ────────────────────────────────────────

pub fn getFd(proc: *Process, fd: u32) ?*FdEntry {
    if (fd >= 16) return null;
    if (proc.fds[fd] == null) return null;
    return &proc.fds[fd].?;
}

// ─── Allocate a free fd ──────────────────────────────────────────

pub fn allocFd(proc: *Process) ?u32 {
    var i: u32 = 0;
    while (i < 16) : (i += 1) {
        if (proc.fds[i] == null) return i;
    }
    return null;
}

// ─── Dump all processes ──────────────────────────────────────────

pub fn dumpAll() void {
    const state_names: [5][]const u8 = .{ "unused", "runbl", "runng", "sleep", "zombi" };
    driver.Uart.puts("PID  STATE   ENTRY    KSTK     QNT  TICKS  NAME\n");
    for (&procs) |p| {
        if (p.state != .unused) {
            driver.Uart.putHex(p.pid);
            driver.Uart.putc(' ');
            const si = @intFromEnum(p.state);
            if (si < 5) driver.Uart.puts(state_names[si]);
            driver.Uart.putc(' ');
            driver.Uart.putHex(p.entry);
            driver.Uart.putc(' ');
            driver.Uart.putHex(@intCast(p.ksp));
            driver.Uart.putc(' ');
            driver.Uart.putHex(p.quantum);
            driver.Uart.putc(' ');
            driver.Uart.putHex(@intCast(p.total_ticks));
            driver.Uart.putc(' ');
            for (p.name) |ch| {
                if (ch == 0) break;
                driver.Uart.putc(ch);
            }
            driver.Uart.putc('\n');
        }
    }
}