summaryrefslogtreecommitdiff
path: root/lume-legacy/include/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'lume-legacy/include/kernel')
-rw-r--r--lume-legacy/include/kernel/buf.h35
-rw-r--r--lume-legacy/include/kernel/config.h12
-rw-r--r--lume-legacy/include/kernel/cpu.h24
-rw-r--r--lume-legacy/include/kernel/elf.h51
-rw-r--r--lume-legacy/include/kernel/exec.h11
-rw-r--r--lume-legacy/include/kernel/fdt.h45
-rw-r--r--lume-legacy/include/kernel/mm.h32
-rw-r--r--lume-legacy/include/kernel/pmm.h60
-rw-r--r--lume-legacy/include/kernel/proc.h34
-rw-r--r--lume-legacy/include/kernel/proc_def.h75
-rw-r--r--lume-legacy/include/kernel/riscv.h404
-rw-r--r--lume-legacy/include/kernel/runqueue.h21
-rw-r--r--lume-legacy/include/kernel/sbi.h29
-rw-r--r--lume-legacy/include/kernel/slab.h15
-rw-r--r--lume-legacy/include/kernel/spinlock.h53
-rw-r--r--lume-legacy/include/kernel/sync.h43
-rw-r--r--lume-legacy/include/kernel/syscall.h85
-rw-r--r--lume-legacy/include/kernel/timer.h17
-rw-r--r--lume-legacy/include/kernel/trap.h55
19 files changed, 1101 insertions, 0 deletions
diff --git a/lume-legacy/include/kernel/buf.h b/lume-legacy/include/kernel/buf.h
new file mode 100644
index 0000000..d0d849c
--- /dev/null
+++ b/lume-legacy/include/kernel/buf.h
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+#include "kernel/spinlock.h"
+
+#define BSIZE 1024
+#define NBUF 30
+
+struct Buf
+{
+ int valid; // Whether the data has been read from the disk (1=valid, 0=invalid)
+ int disk; // Whether it has been held by the driver (dirty bit/write-back flag is usually extended here)
+ uint32 dev; // Device Number
+ uint32 blockno; // Block number
+ uint32 refcnt; // Reference Counting
+ struct Buf *prev; // LRU linked list
+ struct Buf *next;
+ uchar data[BSIZE];
+
+ Mutex lock; // Sleep Lock
+};
+
+namespace BufferCache
+{
+ void init();
+ struct Buf *bread(uint32 dev, uint32 blockno);
+ void bwrite(struct Buf *b);
+ void brelse(struct Buf *b);
+ void bpin(struct Buf *b);
+ void bunpin(struct Buf *b);
+} // namespace BufferCache
diff --git a/lume-legacy/include/kernel/config.h b/lume-legacy/include/kernel/config.h
new file mode 100644
index 0000000..49df2ff
--- /dev/null
+++ b/lume-legacy/include/kernel/config.h
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard Qin
+ */
+
+#pragma once
+#include "common/types.h"
+#include "kernel/fdt.h"
+
+// Legacy compat — these are still referenced in some places
+extern uint64 g_uart_base;
+extern uint64 g_dtb_addr; \ No newline at end of file
diff --git a/lume-legacy/include/kernel/cpu.h b/lume-legacy/include/kernel/cpu.h
new file mode 100644
index 0000000..62bdc17
--- /dev/null
+++ b/lume-legacy/include/kernel/cpu.h
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+#include "kernel/proc_def.h"
+
+struct RunQueue;
+
+struct cpu
+{
+ struct Proc *proc;
+ struct Context context;
+ int noff;
+ int intena;
+
+ struct RunQueue *runqueue;
+};
+
+
+extern struct cpu cpus[];
+struct cpu *mycpu(); \ No newline at end of file
diff --git a/lume-legacy/include/kernel/elf.h b/lume-legacy/include/kernel/elf.h
new file mode 100644
index 0000000..30193d2
--- /dev/null
+++ b/lume-legacy/include/kernel/elf.h
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+
+#define ELF_MAGIC 0x464C457F // "\x7FELF" in little endian
+
+// ELF File Header
+struct elfhdr
+{
+ uint32 magic; // Must equal ELF_MAGIC
+ uint8 elf[12];
+ uint16 type;
+ uint16 machine;
+ uint32 version;
+ uint64 entry; // Program Entry Point (Virtual Address)
+ uint64 phoff; // File offset of the Program Header Table
+ uint64 shoff;
+ uint32 flags;
+ uint16 ehsize;
+ uint16 phentsize;
+ uint16 phnum; // Number of Program Headers
+ uint16 shentsize;
+ uint16 shnum;
+ uint16 shstrndx;
+};
+
+// Program Header
+// Describes how to load segments from a file into memory
+struct proghdr
+{
+ uint32 type;
+ uint32 flags;
+ uint64 off; // Offset of the section in the file
+ uint64 vaddr; // Virtual address of the segment in memory
+ uint64 paddr;
+ uint64 filesz; // Size of the section in the file
+ uint64 memsz; // The size of the segment in memory (memsz >= filesz)
+ uint64 align;
+};
+
+// Values for Proghdr type
+#define ELF_PROG_LOAD 1
+
+// Flag bits for Proghdr flags
+#define ELF_PROG_FLAG_EXEC 1
+#define ELF_PROG_FLAG_WRITE 2
+#define ELF_PROG_FLAG_READ 4 \ No newline at end of file
diff --git a/lume-legacy/include/kernel/exec.h b/lume-legacy/include/kernel/exec.h
new file mode 100644
index 0000000..304e03f
--- /dev/null
+++ b/lume-legacy/include/kernel/exec.h
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+
+namespace Exec
+{
+ int exec(char *path, char **argv);
+} \ No newline at end of file
diff --git a/lume-legacy/include/kernel/fdt.h b/lume-legacy/include/kernel/fdt.h
new file mode 100644
index 0000000..89cbae6
--- /dev/null
+++ b/lume-legacy/include/kernel/fdt.h
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard Qin
+ * LumeOS - FDT Device Discovery
+ */
+
+#pragma once
+#include "common/types.h"
+
+// Single device descriptor (populated from FDT)
+struct FdtDevice {
+ uint64 base_addr; // MMIO base address
+ uint64 size; // MMIO region size
+ uint32 irq; // Interrupt number (0 = none)
+};
+
+// Device registry — populated by fdt_parse(), consumed by drivers
+struct DeviceRegistry {
+ // Memory
+ uint64 mem_base;
+ uint64 mem_size;
+
+ // Core platform devices
+ FdtDevice uart;
+ FdtDevice plic;
+ FdtDevice clint;
+
+ // VirtIO devices (multiple possible)
+ static constexpr int MAX_VIRTIO = 8;
+ FdtDevice virtio[MAX_VIRTIO];
+ int virtio_count;
+
+ // MMIO ranges for kvminit() to map
+ static constexpr int MAX_MMIO = 16;
+ struct MmioRegion {
+ uint64 base;
+ uint64 size;
+ } mmio_regions[MAX_MMIO];
+ int mmio_count;
+};
+
+extern DeviceRegistry g_devices;
+
+// Parse FDT and populate g_devices
+void fdt_parse(uint64 dtb);
diff --git a/lume-legacy/include/kernel/mm.h b/lume-legacy/include/kernel/mm.h
new file mode 100644
index 0000000..e3de09a
--- /dev/null
+++ b/lume-legacy/include/kernel/mm.h
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+
+namespace VM
+{
+ extern uint64 *kernel_pagetable;
+
+ void kvminit();
+ void kvminithart();
+
+ uint64 *uvmcreate();
+ void uvminit(uint64 *pagetable, uchar *src, uint64 sz);
+ uint64 uvmalloc(uint64 *pagetable, uint64 oldsz, uint64 newsz, int xperm);
+ uint64 uvmdealloc(uint64 *pagetable, uint64 oldsz, uint64 newsz);
+ void uvmfree(uint64 *pagetable, uint64 sz);
+ int uvmcopy(uint64 *oldpt, uint64 *newpt, uint64 sz);
+ void uvmunmap(uint64 *pagetable, uint64 va, uint64 npages, int do_free);
+
+ int copyout(uint64 *pagetable, uint64 dstva, char *src, uint64 len);
+ int copyin(uint64 *pagetable, char *dst, uint64 srcva, uint64 len);
+ int copyinstr(uint64 *pagetable, char *dst, uint64 srcva, uint64 max);
+
+ int handle_cow_fault(uint64 *pagetable, uint64 va);
+ int mappages(uint64 *pagetable, uint64 va, uint64 size, uint64 pa, int perm);
+
+ uint64 walkaddr(uint64 *pagetable, uint64 va);
+} \ No newline at end of file
diff --git a/lume-legacy/include/kernel/pmm.h b/lume-legacy/include/kernel/pmm.h
new file mode 100644
index 0000000..261786d
--- /dev/null
+++ b/lume-legacy/include/kernel/pmm.h
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard Qin
+ */
+
+#pragma once
+#include "common/types.h"
+#include "kernel/spinlock.h"
+#include "kernel/fdt.h" // For g_devices
+
+// Physical Memory Layout Definition
+#define KERNBASE 0x80000000
+
+// Dynamic memory size from FDT
+// Default to 128MB if FDT not present/parsed
+extern uint64 g_phys_size;
+
+#define PHYSIZE (g_phys_size)
+#define PHYSTOP (KERNBASE + PHYSIZE)
+
+// Buddy System Max Order
+// Order 10 => 2^10 pages = 1024 * 4KB = 4MB contiguous block
+// Order 11 => 2^11 pages = 8MB
+#define MAX_ORDER 11
+
+// Page flag
+#define PG_used 0x01 // This page is occupied
+#define PG_reserved 0x02 // This page is reserved (kernel code/data, DTB, MemMap)
+
+// Physical Page Metadata Structure
+struct Page
+{
+ uint8 flags; // Page Status (PG_used, PG_reserved)
+ uint8 order; // If it is the first page of a free block, record the order of the current block
+ uint16 refcnt; // Reference Counting
+ struct Page *next; // Free list pointer (points to the next free block of the same size)
+ struct Page *prev; // Doubly linked list (easy to remove)
+};
+
+namespace PMM
+{
+ // Initialize physical memory management
+ void init();
+
+ // Core Allocation Interface (Buddy System)
+ // order: 0 ~ MAX_ORDER-1
+ void *alloc_pages(int order);
+ void free_pages(void *pa, int order);
+
+ // Compatible with old interface (allocate single page with order=0 and automatically reset to zero)
+ void *alloc_page();
+ void free_page(void *pa);
+
+ // Helper function
+ struct Page *pa_to_page(uint64 pa);
+ uint64 page_to_pa(struct Page *page);
+
+ // Debug information
+ void print_free_lists();
+} \ No newline at end of file
diff --git a/lume-legacy/include/kernel/proc.h b/lume-legacy/include/kernel/proc.h
new file mode 100644
index 0000000..e11dc47
--- /dev/null
+++ b/lume-legacy/include/kernel/proc.h
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+#include "kernel/proc_def.h"
+#include "kernel/spinlock.h"
+
+namespace ProcManager
+{
+ void init();
+ void scheduler();
+ void yield();
+
+ void create_kernel_thread(void (*func)(), const char *name);
+
+ void user_init();
+ void exit(int status);
+ int wait(uint64 addr);
+
+ void sleep(void *chan, Spinlock *lk);
+ void wakeup(void *chan);
+
+ int growproc(int n);
+
+ int kill(int pid);
+}
+
+void forkret();
+struct Proc *myproc();
+void usertrapret();
+int fork(); \ No newline at end of file
diff --git a/lume-legacy/include/kernel/proc_def.h b/lume-legacy/include/kernel/proc_def.h
new file mode 100644
index 0000000..50f27d7
--- /dev/null
+++ b/lume-legacy/include/kernel/proc_def.h
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+#include "common/fcntl.h"
+
+struct cpu;
+struct Trapframe;
+class Inode;
+struct file;
+
+enum ProcState
+{
+ UNUSED,
+ USED,
+ SLEEPING,
+ RUNNABLE,
+ RUNNING,
+ ZOMBIE,
+};
+
+#define PRIO_MAX 0
+#define PRIO_MIN 3
+#define N_PRIO 4
+
+// Context structure
+struct Context
+{
+ uint64 ra;
+ uint64 sp;
+ uint64 s0;
+ uint64 s1;
+ uint64 s2;
+ uint64 s3;
+ uint64 s4;
+ uint64 s5;
+ uint64 s6;
+ uint64 s7;
+ uint64 s8;
+ uint64 s9;
+ uint64 s10;
+ uint64 s11;
+};
+
+struct Proc
+{
+ struct Context context;
+ struct Trapframe *tf;
+ uint64 *pagetable;
+ enum ProcState state;
+ int pid;
+ void *kstack;
+ char name[16];
+ uint64 sz;
+ struct Proc *parent;
+ void *chan;
+ int xstate;
+ int killed;
+
+ Inode *cwd;
+ struct file *ofile[NOFILE];
+
+ struct Proc *next;
+ struct Proc *prev;
+
+ struct Proc *all_next;
+ struct Proc *all_prev;
+
+ int priority;
+ int ticks;
+ struct cpu *cpu;
+}; \ No newline at end of file
diff --git a/lume-legacy/include/kernel/riscv.h b/lume-legacy/include/kernel/riscv.h
new file mode 100644
index 0000000..0c586a9
--- /dev/null
+++ b/lume-legacy/include/kernel/riscv.h
@@ -0,0 +1,404 @@
+#ifndef __ASSEMBLER__
+
+// which hart (core) is this?
+static inline uint64
+r_mhartid()
+{
+ uint64 x;
+ asm volatile("csrr %0, mhartid" : "=r" (x) );
+ return x;
+}
+
+// Machine Status Register, mstatus
+
+#define MSTATUS_MPP_MASK (3L << 11) // previous mode.
+#define MSTATUS_MPP_M (3L << 11)
+#define MSTATUS_MPP_S (1L << 11)
+#define MSTATUS_MPP_U (0L << 11)
+
+static inline uint64
+r_mstatus()
+{
+ uint64 x;
+ asm volatile("csrr %0, mstatus" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_mstatus(uint64 x)
+{
+ asm volatile("csrw mstatus, %0" : : "r" (x));
+}
+
+// machine exception program counter, holds the
+// instruction address to which a return from
+// exception will go.
+static inline void
+w_mepc(uint64 x)
+{
+ asm volatile("csrw mepc, %0" : : "r" (x));
+}
+
+// Supervisor Status Register, sstatus
+
+#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User
+#define SSTATUS_SPIE (1L << 5) // Supervisor Previous Interrupt Enable
+#define SSTATUS_UPIE (1L << 4) // User Previous Interrupt Enable
+#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable
+#define SSTATUS_UIE (1L << 0) // User Interrupt Enable
+
+static inline uint64
+r_sstatus()
+{
+ uint64 x;
+ asm volatile("csrr %0, sstatus" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_sstatus(uint64 x)
+{
+ asm volatile("csrw sstatus, %0" : : "r" (x));
+}
+
+// Supervisor Interrupt Pending
+static inline uint64
+r_sip()
+{
+ uint64 x;
+ asm volatile("csrr %0, sip" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_sip(uint64 x)
+{
+ asm volatile("csrw sip, %0" : : "r" (x));
+}
+
+// Supervisor Interrupt Enable
+#define SIE_SEIE (1L << 9) // external
+#define SIE_STIE (1L << 5) // timer
+#define SIE_SSIE (1L << 1) // software
+static inline uint64
+r_sie()
+{
+ uint64 x;
+ asm volatile("csrr %0, sie" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_sie(uint64 x)
+{
+ asm volatile("csrw sie, %0" : : "r" (x));
+}
+
+// Machine-mode Interrupt Enable
+#define MIE_STIE (1L << 5) // supervisor timer
+static inline uint64
+r_mie()
+{
+ uint64 x;
+ asm volatile("csrr %0, mie" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_mie(uint64 x)
+{
+ asm volatile("csrw mie, %0" : : "r" (x));
+}
+
+// supervisor exception program counter, holds the
+// instruction address to which a return from
+// exception will go.
+static inline void
+w_sepc(uint64 x)
+{
+ asm volatile("csrw sepc, %0" : : "r" (x));
+}
+
+static inline uint64
+r_sepc()
+{
+ uint64 x;
+ asm volatile("csrr %0, sepc" : "=r" (x) );
+ return x;
+}
+
+// Machine Exception Delegation
+static inline uint64
+r_medeleg()
+{
+ uint64 x;
+ asm volatile("csrr %0, medeleg" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_medeleg(uint64 x)
+{
+ asm volatile("csrw medeleg, %0" : : "r" (x));
+}
+
+// Machine Interrupt Delegation
+static inline uint64
+r_mideleg()
+{
+ uint64 x;
+ asm volatile("csrr %0, mideleg" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_mideleg(uint64 x)
+{
+ asm volatile("csrw mideleg, %0" : : "r" (x));
+}
+
+// Supervisor Trap-Vector Base Address
+// low two bits are mode.
+static inline void
+w_stvec(uint64 x)
+{
+ asm volatile("csrw stvec, %0" : : "r" (x));
+}
+
+static inline uint64
+r_stvec()
+{
+ uint64 x;
+ asm volatile("csrr %0, stvec" : "=r" (x) );
+ return x;
+}
+
+// Supervisor Timer Comparison Register
+static inline uint64
+r_stimecmp()
+{
+ uint64 x;
+ // asm volatile("csrr %0, stimecmp" : "=r" (x) );
+ asm volatile("csrr %0, 0x14d" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_stimecmp(uint64 x)
+{
+ // asm volatile("csrw stimecmp, %0" : : "r" (x));
+ asm volatile("csrw 0x14d, %0" : : "r" (x));
+}
+
+// Machine Environment Configuration Register
+static inline uint64
+r_menvcfg()
+{
+ uint64 x;
+ // asm volatile("csrr %0, menvcfg" : "=r" (x) );
+ asm volatile("csrr %0, 0x30a" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_menvcfg(uint64 x)
+{
+ //asm volatile("csrw menvcfg, %0" : : "r" (x));
+ asm volatile("csrw 0x30a, %0" : : "r" (x));
+}
+
+// Physical Memory Protection
+static inline void
+w_pmpcfg0(uint64 x)
+{
+ asm volatile("csrw pmpcfg0, %0" : : "r" (x));
+}
+
+static inline void
+w_pmpaddr0(uint64 x)
+{
+ asm volatile("csrw pmpaddr0, %0" : : "r" (x));
+}
+
+// use riscv's sv39 page table scheme.
+#define SATP_SV39 (8L << 60)
+
+#define MAKE_SATP(pagetable) (SATP_SV39 | (((uint64)pagetable) >> 12))
+
+// supervisor address translation and protection;
+// holds the address of the page table.
+static inline void
+w_satp(uint64 x)
+{
+ asm volatile("csrw satp, %0" : : "r" (x));
+}
+
+static inline uint64
+r_satp()
+{
+ uint64 x;
+ asm volatile("csrr %0, satp" : "=r" (x) );
+ return x;
+}
+
+// Supervisor Trap Cause
+static inline uint64
+r_scause()
+{
+ uint64 x;
+ asm volatile("csrr %0, scause" : "=r" (x) );
+ return x;
+}
+
+// Supervisor Trap Value
+static inline uint64
+r_stval()
+{
+ uint64 x;
+ asm volatile("csrr %0, stval" : "=r" (x) );
+ return x;
+}
+
+// Machine-mode Counter-Enable
+static inline void
+w_mcounteren(uint64 x)
+{
+ asm volatile("csrw mcounteren, %0" : : "r" (x));
+}
+
+static inline uint64
+r_mcounteren()
+{
+ uint64 x;
+ asm volatile("csrr %0, mcounteren" : "=r" (x) );
+ return x;
+}
+
+// machine-mode cycle counter
+static inline uint64
+r_time()
+{
+ uint64 x;
+ asm volatile("csrr %0, time" : "=r" (x) );
+ return x;
+}
+
+// enable device interrupts
+static inline void
+intr_on()
+{
+ w_sstatus(r_sstatus() | SSTATUS_SIE);
+}
+
+// disable device interrupts
+static inline void
+intr_off()
+{
+ w_sstatus(r_sstatus() & ~SSTATUS_SIE);
+}
+
+// are device interrupts enabled?
+static inline int
+intr_get()
+{
+ uint64 x = r_sstatus();
+ return (x & SSTATUS_SIE) != 0;
+}
+
+static inline uint64
+r_sp()
+{
+ uint64 x;
+ asm volatile("mv %0, sp" : "=r" (x) );
+ return x;
+}
+
+static inline uint64
+r_fp()
+{
+ uint64 x;
+ asm volatile("mv %0, s0" : "=r" (x) );
+ return x;
+}
+
+// read and write tp, the thread pointer, which xv6 uses to hold
+// this core's hartid (core number), the index into cpus[].
+static inline uint64
+r_tp()
+{
+ uint64 x;
+ asm volatile("mv %0, tp" : "=r" (x) );
+ return x;
+}
+
+static inline void
+w_tp(uint64 x)
+{
+ asm volatile("mv tp, %0" : : "r" (x));
+}
+
+static inline uint64
+r_ra()
+{
+ uint64 x;
+ asm volatile("mv %0, ra" : "=r" (x) );
+ return x;
+}
+
+// flush the TLB.
+static inline void
+sfence_vma()
+{
+ // the zero, zero means flush all TLB entries.
+ asm volatile("sfence.vma zero, zero");
+}
+
+typedef uint64 pte_t;
+typedef uint64 *pagetable_t; // 512 PTEs
+
+#endif // __ASSEMBLER__
+
+#define PGSIZE 4096 // bytes per page
+#define PGSHIFT 12 // bits of offset within a page
+
+#ifdef LAB_PGTBL
+#define SUPERPGSIZE (2 * (1 << 20)) // bytes per page
+#define SUPERPGROUNDUP(sz) (((sz)+SUPERPGSIZE-1) & ~(SUPERPGSIZE-1))
+#define SUPERPGROUNDDOWN(sz) (SUPERPGROUNDUP(sz)-SUPERPGSIZE)
+#endif
+
+#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
+#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
+
+#define PTE_V (1L << 0) // valid
+#define PTE_R (1L << 1)
+#define PTE_W (1L << 2)
+#define PTE_X (1L << 3)
+#define PTE_U (1L << 4) // user can access
+
+// COW Flag (PTE bit 8)
+#define PTE_COW (1L << 8)
+
+#if defined(LAB_MMAP) || defined(LAB_PGTBL) || defined(LAB_COW)
+#define PTE_LEAF(pte) (((pte) & PTE_R) | ((pte) & PTE_W) | ((pte) & PTE_X))
+#endif
+
+// shift a physical address to the right place for a PTE.
+#define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
+
+#define PTE2PA(pte) (((pte) >> 10) << 12)
+
+#define PTE_FLAGS(pte) ((pte) & 0x3FF)
+
+// extract the three 9-bit page table indices from a virtual address.
+#define PXMASK 0x1FF // 9 bits
+#define PXSHIFT(level) (PGSHIFT+(9*(level)))
+#define PX(level, va) ((((uint64) (va)) >> PXSHIFT(level)) & PXMASK)
+
+// one beyond the highest possible virtual address.
+// MAXVA is actually one bit less than the max allowed by
+// Sv39, to avoid having to sign-extend virtual addresses
+// that have the high bit set.
+#define MAXVA (1L << (9 + 9 + 9 + 12 - 1))
+#define TRAMPOLINE (MAXVA - PGSIZE)
+#define TRAPFRAME (TRAMPOLINE - PGSIZE) \ No newline at end of file
diff --git a/lume-legacy/include/kernel/runqueue.h b/lume-legacy/include/kernel/runqueue.h
new file mode 100644
index 0000000..14704ce
--- /dev/null
+++ b/lume-legacy/include/kernel/runqueue.h
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "kernel/spinlock.h"
+#include "kernel/proc_def.h"
+
+struct RunQueue
+{
+ Spinlock lock;
+ struct Proc *heads[N_PRIO];
+ struct Proc *tails[N_PRIO];
+ uint64 count;
+
+ void init(const char *name);
+ void enqueue(struct Proc *p);
+ struct Proc *dequeue();
+ struct Proc *steal();
+}; \ No newline at end of file
diff --git a/lume-legacy/include/kernel/sbi.h b/lume-legacy/include/kernel/sbi.h
new file mode 100644
index 0000000..a6be5b3
--- /dev/null
+++ b/lume-legacy/include/kernel/sbi.h
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+
+// SBI Extension IDs
+#define SBI_EID_SRST 0x53525354
+
+// SBI Function IDs for SRST
+#define SBI_FID_SRST_RESET 0
+
+// Reset Types
+#define SBI_SRST_TYPE_SHUTDOWN 0
+#define SBI_SRST_TYPE_COLD_REBOOT 1
+#define SBI_SRST_TYPE_WARM_REBOOT 2
+
+// Reset Reasons
+#define SBI_SRST_REASON_NONE 0
+#define SBI_SRST_REASON_SYSTEM_FAILURE 1
+
+namespace SBI
+{
+ void set_timer(uint64 stime_value);
+ void sbi_shutdown();
+ void sbi_reboot();
+} // namespace SBI
diff --git a/lume-legacy/include/kernel/slab.h b/lume-legacy/include/kernel/slab.h
new file mode 100644
index 0000000..bb7b699
--- /dev/null
+++ b/lume-legacy/include/kernel/slab.h
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+
+namespace Slab
+{
+ void init();
+
+ void *kmalloc(uint64 size);
+ void kfree(void *ptr);
+} \ No newline at end of file
diff --git a/lume-legacy/include/kernel/spinlock.h b/lume-legacy/include/kernel/spinlock.h
new file mode 100644
index 0000000..1511199
--- /dev/null
+++ b/lume-legacy/include/kernel/spinlock.h
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+
+struct cpu;
+
+// Spinlock
+// Features: Non-interruptible, busy-wait loop, extremely short hold time.
+// Scenario: Protecting core data such as scheduler, interrupt handling, PMM, etc.
+class Spinlock
+{
+public:
+ volatile uint32 locked; // Whether it is locked (0: unlocked, 1: locked)
+ const char *name; // Lock Name (for debugging)
+ struct cpu *cpu; // The CPU holding the lock
+
+ // Constructor
+ Spinlock() : locked(0), name("uninit"), cpu(nullptr) {}
+
+ // Initialize lock name
+ void init(const char *name);
+
+ // Acquire lock
+ void acquire();
+
+ // Release lock
+ void release();
+
+ // Check if the current CPU holds the lock
+ bool holding();
+};
+
+// Mutex
+// Features: Allows interruption, allows sleeping (yield CPU), can hold for a long time.
+// Scenarios: File system IO, device driver waiting, long-term buffering operations.
+class Mutex
+{
+public:
+ uint32 locked; // Whether it is locked
+ const char *name; // Lock Name
+ int pid; // PID of the process holding the lock (for debugging)
+
+ Spinlock lk; // Internal spin lock, protects the state of the Mutex itself
+
+ void init(const char *name);
+ void acquire();
+ void release();
+ bool holding();
+}; \ No newline at end of file
diff --git a/lume-legacy/include/kernel/sync.h b/lume-legacy/include/kernel/sync.h
new file mode 100644
index 0000000..7f7c208
--- /dev/null
+++ b/lume-legacy/include/kernel/sync.h
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+#include "kernel/spinlock.h"
+#include "kernel/proc_def.h"
+
+
+struct WaitQueue
+{
+ Spinlock lock;
+ struct Proc *head;
+ struct Proc *tail;
+
+ void init(const char *name);
+ void sleep();
+ void wakeup();
+ void wakeup_all();
+};
+
+struct Semaphore
+{
+ int value;
+ WaitQueue wq;
+ Spinlock lock;
+
+ void init(int val, const char *name);
+ void wait();
+ void post();
+};
+
+struct CondVar
+{
+ WaitQueue wq;
+
+ void init(const char *name);
+ void wait(Spinlock *mutex_lock);
+ void signal();
+ void broadcast();
+}; \ No newline at end of file
diff --git a/lume-legacy/include/kernel/syscall.h b/lume-legacy/include/kernel/syscall.h
new file mode 100644
index 0000000..3594c08
--- /dev/null
+++ b/lume-legacy/include/kernel/syscall.h
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard Qin
+ * Lume OS - Linux Compatibility Layer
+ *
+ * Strictly aligned with Linux RISC-V 64 ABI (asm-generic)
+ * Reference: linux/usr/include/asm-generic/unistd.h
+ */
+
+#pragma once
+
+// File System
+#define SYS_getcwd 17
+#define SYS_dup 23
+#define SYS_dup3 24
+#define SYS_fcntl 25
+#define SYS_ioctl 29
+#define SYS_mknodat 33
+#define SYS_mkdirat 34
+#define SYS_unlinkat 35
+#define SYS_linkat 37
+#define SYS_umount2 39
+#define SYS_mount 40
+#define SYS_statfs 43
+#define SYS_ftruncate 46
+#define SYS_faccessat 48
+#define SYS_chdir 49
+#define SYS_fchmod 52
+#define SYS_fchmodat 53
+#define SYS_fchownat 54
+#define SYS_openat 56
+#define SYS_close 57
+#define SYS_vhangup 58
+#define SYS_pipe2 59
+#define SYS_getdents64 61
+#define SYS_lseek 62
+#define SYS_read 63
+#define SYS_write 64
+#define SYS_readv 65
+#define SYS_writev 66
+#define SYS_pread64 67
+#define SYS_pwrite64 68
+#define SYS_pselect6 72
+#define SYS_ppoll 73
+#define SYS_readlinkat 78
+#define SYS_fstatat 79 // newfstatat
+#define SYS_fstat 80
+
+// Process Management
+#define SYS_exit 93
+#define SYS_exit_group 94
+#define SYS_set_tid_address 96
+#define SYS_nanosleep 101
+#define SYS_kill 129
+#define SYS_rt_sigaction 134
+#define SYS_times 153
+#define SYS_uname 160
+#define SYS_gettimeofday 169
+#define SYS_getpid 172
+#define SYS_getppid 173
+#define SYS_getuid 174
+#define SYS_geteuid 175
+#define SYS_getgid 176
+#define SYS_getegid 177
+#define SYS_sysinfo 179
+#define SYS_brk 214
+#define SYS_munmap 215
+#define SYS_mremap 216
+#define SYS_clone 220
+#define SYS_execve 221
+#define SYS_mmap 222
+#define SYS_wait4 260
+#define SYS_prlimit64 261
+
+// System
+#define SYS_reboot 142 // Used for shutdown
+
+// === Custom / Debug (Non-Standard) ===
+// Placed above 1000 to avoid conflicts with Linux ABI
+#define SYS_putc 1000
+#define SYS_disk_test 1001
+
+#ifndef __ASSEMBLER__
+void syscall();
+#endif \ No newline at end of file
diff --git a/lume-legacy/include/kernel/timer.h b/lume-legacy/include/kernel/timer.h
new file mode 100644
index 0000000..8b97bbe
--- /dev/null
+++ b/lume-legacy/include/kernel/timer.h
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#include "common/types.h"
+
+namespace Timer{
+ uint64 get_time();
+ void set_next_trigger();
+ void init();
+
+ void tick();
+ uint64 get_ticks();
+ Spinlock *get_lock();
+ void *get_tick_chan();
+} \ No newline at end of file
diff --git a/lume-legacy/include/kernel/trap.h b/lume-legacy/include/kernel/trap.h
new file mode 100644
index 0000000..ddc1d12
--- /dev/null
+++ b/lume-legacy/include/kernel/trap.h
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Richard QIn
+ */
+
+#pragma once
+#include "common/types.h"
+
+struct Trapframe
+{
+ /* 0 */ uint64 kernel_satp;
+ /* 8 */ uint64 kernel_sp;
+ /* 16 */ uint64 kernel_trap;
+ /* 24 */ uint64 epc;
+ /* 32 */ uint64 kernel_hartid;
+
+ /* 40 */ uint64 ra; // x1
+ /* 48 */ uint64 sp; // x2
+ /* 56 */ uint64 gp; // x3
+ /* 64 */ uint64 tp; // x4
+ /* 72 */ uint64 t0; // x5
+ /* 80 */ uint64 t1; // x6
+ /* 88 */ uint64 t2; // x7
+ /* 96 */ uint64 s0; // x8 (Frame Pointer)
+ /* 104 */ uint64 s1; // x9
+ /* 112 */ uint64 a0; // x10
+ /* 120 */ uint64 a1; // x11
+ /* 128 */ uint64 a2; // x12
+ /* 136 */ uint64 a3; // x13
+ /* 144 */ uint64 a4; // x14
+ /* 152 */ uint64 a5; // x15
+ /* 160 */ uint64 a6; // x16
+ /* 168 */ uint64 a7; // x17
+ /* 176 */ uint64 s2; // x18
+ /* 184 */ uint64 s3; // x19
+ /* 192 */ uint64 s4; // x20
+ /* 200 */ uint64 s5; // x21
+ /* 208 */ uint64 s6; // x22
+ /* 216 */ uint64 s7; // x23
+ /* 224 */ uint64 s8; // x24
+ /* 232 */ uint64 s9; // x25
+ /* 240 */ uint64 s10; // x26
+ /* 248 */ uint64 s11; // x27
+ /* 256 */ uint64 t3; // x28
+ /* 264 */ uint64 t4; // x29
+ /* 272 */ uint64 t5; // x30
+ /* 280 */ uint64 t6; // x31
+};
+
+namespace Trap
+{
+ void init();
+ void inithart();
+ int devintr();
+} \ No newline at end of file