From 7ececbe2818510a8acaa2a2e513085c768b14d15 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sat, 27 Dec 2025 21:56:42 +0800 Subject: feat: Implement the first user program of the lume OS --- include/common/types.h | 6 +++++- include/kernel/mm.h | 23 ++++++++++++----------- include/kernel/proc.h | 15 ++++++++++++--- include/kernel/riscv.h | 2 ++ include/kernel/trap.h | 3 ++- 5 files changed, 33 insertions(+), 16 deletions(-) (limited to 'include') diff --git a/include/common/types.h b/include/common/types.h index f9f01ca..7403a54 100644 --- a/include/common/types.h +++ b/include/common/types.h @@ -15,4 +15,8 @@ typedef uint64_t uintptr_t; typedef uint64_t uint64; typedef uint32_t uint32; -typedef uint8_t uint8; \ No newline at end of file +typedef uint8_t uint8; + +typedef unsigned int uint; +typedef unsigned short ushort; +typedef unsigned char uchar; \ No newline at end of file diff --git a/include/kernel/mm.h b/include/kernel/mm.h index 0f7c73d..07d675b 100644 --- a/include/kernel/mm.h +++ b/include/kernel/mm.h @@ -3,19 +3,20 @@ namespace VM { - typedef uint64_t* pagetable_t; + typedef uint64_t *pagetable_t; typedef uint64_t pte_t; - typedef uint64_t uint64; - - #define PTE_V (1L << 0) - #define PTE_R (1L << 1) - #define PTE_W (1L << 2) - #define PTE_X (1L << 3) - #define PTE_U (1L << 4) - - #define SATP_SV39 (8L << 60) - #define MAKE_SATP(pagetable) (SATP_SV39 | (((uint64)pagetable) >> 12)) + pte_t *walk(pagetable_t pagetable, uint64 va, int alloc); + int mappages(pagetable_t pagetable, uint64 va, uint64 size, uint64 pa, int perm); + void kvmmap(uint64 va, uint64 pa, uint64 sz, int perm); void kvminit(); void kvminithart(); + pagetable_t uvmcreate(); + void uvminit(pagetable_t pagetable, uchar *src, uint sz); + void uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free); + void uvmfree(pagetable_t pagetable, uint64 sz); + int uvmcopy(pagetable_t oldpt, pagetable_t newpt, uint64 sz); + uint64 uvmalloc(pagetable_t pagetable, uint64 oldsz, uint64 newsz); + uint64 uvmdealloc(pagetable_t pagetable, uint64 oldsz, uint64 newsz); + int copyin(pagetable_t pagetable, char *dst, uint64 srcva, uint64 len); } \ No newline at end of file diff --git a/include/kernel/proc.h b/include/kernel/proc.h index 89a3db7..559b137 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -1,5 +1,6 @@ #pragma once #include "common/types.h" +#include "kernel/trap.h" struct Context { @@ -32,18 +33,20 @@ enum ProcState{ struct Proc { struct Context context; + struct Trapframe *tf; + + uint64 *pagetable; enum ProcState state; int pid; void* kstack; char name[16]; + uint64 sz; // }; -inline void intr_on() { asm volatile("csrs sstatus, 0x2"); } -inline void intr_off() { asm volatile("csrc sstatus, 0x2"); } namespace ProcManager { @@ -52,4 +55,10 @@ namespace ProcManager void yield(); void create_kernel_thread(void (*func)(), const char *name); -} \ No newline at end of file + + void user_init(); +} + +void forkret(); +struct Proc *myproc(); +void usertrapret(); \ No newline at end of file diff --git a/include/kernel/riscv.h b/include/kernel/riscv.h index 257c54f..4b3d323 100644 --- a/include/kernel/riscv.h +++ b/include/kernel/riscv.h @@ -398,3 +398,5 @@ typedef uint64 *pagetable_t; // 512 PTEs // 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/include/kernel/trap.h b/include/kernel/trap.h index fd51ef6..79bb037 100644 --- a/include/kernel/trap.h +++ b/include/kernel/trap.h @@ -47,4 +47,5 @@ namespace Trap void init(); extern "C" void kernelvec(); extern "C" void kerneltrap(struct Trapframe *tf); -} \ No newline at end of file +} +extern "C" void usertrap(); \ No newline at end of file -- cgit v1.2.3