From b0fe61094d7c5acdbd71ec28f83397a9a194c5fa Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 16 Aug 2025 15:48:08 +0200 Subject: [PATCH] proc calloc --- kern/kalloc.c | 19 ++++++------- kern/proc.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 kern/proc.h diff --git a/kern/kalloc.c b/kern/kalloc.c index 1a034bd..d3d254c 100644 --- a/kern/kalloc.c +++ b/kern/kalloc.c @@ -1,8 +1,8 @@ +#include #include #include #include #include -#include #include #include @@ -23,12 +23,12 @@ struct Run { /** Kernel memory allocator. */ struct { - struct Spinlock lock; - struct Run *freelist; + spinlock_t lock; + struct Run *freelist; } kmem; void kalloc_init() { - initlock(&kmem.lock, "kmem"); + spinlock_init(&kmem.lock); freerange(kernel_end, (void *)PHYSTOP); } @@ -43,8 +43,7 @@ void kfree(void *pa) { // Assert that page is a ligned to a page boundary and that its correctly // sized - if (((u64)pa % PGSIZE) != 0 || (char *)pa < kernel_end || - (u64)pa >= PHYSTOP) + if (((u64)pa % PGSIZE) != 0 || (char *)pa < kernel_end || (u64)pa >= PHYSTOP) panic("kfree"); // Fill with junk to catch dangling refs. @@ -52,23 +51,23 @@ void kfree(void *pa) { r = (struct Run *)pa; - acquire(&kmem.lock); + spin_lock(&kmem.lock); r->next = kmem.freelist; kmem.freelist = r; - release(&kmem.lock); + spin_unlock(&kmem.lock); } void *kalloc(void) { struct Run *r; - acquire(&kmem.lock); + spin_lock(&kmem.lock); r = kmem.freelist; if (r) kmem.freelist = r->next; - release(&kmem.lock); + spin_unlock(&kmem.lock); if (r) memset((char *)r, 5, PGSIZE); // fill with junk diff --git a/kern/proc.h b/kern/proc.h new file mode 100644 index 0000000..b594fc9 --- /dev/null +++ b/kern/proc.h @@ -0,0 +1,77 @@ +#include + +typedef enum { + UNUSED, + USED, + SLEEPING, + RUNNABLE, + RUNNING, + ZOMBIE, +} ProcessState; + +/** Saved registers for kernel context switches. */ +struct Context { + uint64_t ra; + uint64_t sp; + + // callee-saved + uint64_t s0; + uint64_t s1; + uint64_t s2; + uint64_t s3; + uint64_t s4; + uint64_t s5; + uint64_t s6; + uint64_t s7; + uint64_t s8; + uint64_t s9; + uint64_t s10; + uint64_t s11; +}; + +/** Per-CPU state. */ +struct Cpu { + struct Process *proc; // The process running on this cpu, or null. + struct Context context; // swtch() here to enter scheduler(). + int noff; // Depth of push_off() nesting. + int intena; // Were interrupts enabled before push_off()? +}; + +typedef struct { + /* 0 */ uint64_t kernel_satp; // kernel page table + /* 8 */ uint64_t kernel_sp; // top of process's kernel stack + /* 16 */ uint64_t kernel_trap; // usertrap() + /* 24 */ uint64_t epc; // saved user program counter + /* 32 */ uint64_t kernel_hartid; // saved kernel tp + /* 40 */ uint64_t ra; + /* 48 */ uint64_t sp; + /* 56 */ uint64_t gp; + /* 64 */ uint64_t tp; + /* 72 */ uint64_t t0; + /* 80 */ uint64_t t1; + /* 88 */ uint64_t t2; + /* 96 */ uint64_t s0; + /* 104 */ uint64_t s1; + /* 112 */ uint64_t a0; + /* 120 */ uint64_t a1; + /* 128 */ uint64_t a2; + /* 136 */ uint64_t a3; + /* 144 */ uint64_t a4; + /* 152 */ uint64_t a5; + /* 160 */ uint64_t a6; + /* 168 */ uint64_t a7; + /* 176 */ uint64_t s2; + /* 184 */ uint64_t s3; + /* 192 */ uint64_t s4; + /* 200 */ uint64_t s5; + /* 208 */ uint64_t s6; + /* 216 */ uint64_t s7; + /* 224 */ uint64_t s8; + /* 232 */ uint64_t s9; + /* 240 */ uint64_t s10; + /* 248 */ uint64_t s11; + /* 256 */ uint64_t t3; + /* 264 */ uint64_t t4; + /* 272 */ uint64_t t5; + /* 280 */ uint64_t t6; +} TrapFrame_t;