proc calloc
This commit is contained in:
parent
39ef34d41e
commit
b0fe61094d
2 changed files with 86 additions and 10 deletions
|
@ -1,8 +1,8 @@
|
||||||
|
#include <ispinlock.h>
|
||||||
#include <kalloc.h>
|
#include <kalloc.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include <panic.h>
|
#include <panic.h>
|
||||||
#include <riscv.h>
|
#include <riscv.h>
|
||||||
#include <spinlock.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <types.h>
|
#include <types.h>
|
||||||
|
|
||||||
|
@ -23,12 +23,12 @@ struct Run {
|
||||||
|
|
||||||
/** Kernel memory allocator. */
|
/** Kernel memory allocator. */
|
||||||
struct {
|
struct {
|
||||||
struct Spinlock lock;
|
spinlock_t lock;
|
||||||
struct Run *freelist;
|
struct Run *freelist;
|
||||||
} kmem;
|
} kmem;
|
||||||
|
|
||||||
void kalloc_init() {
|
void kalloc_init() {
|
||||||
initlock(&kmem.lock, "kmem");
|
spinlock_init(&kmem.lock);
|
||||||
freerange(kernel_end, (void *)PHYSTOP);
|
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
|
// Assert that page is a ligned to a page boundary and that its correctly
|
||||||
// sized
|
// sized
|
||||||
if (((u64)pa % PGSIZE) != 0 || (char *)pa < kernel_end ||
|
if (((u64)pa % PGSIZE) != 0 || (char *)pa < kernel_end || (u64)pa >= PHYSTOP)
|
||||||
(u64)pa >= PHYSTOP)
|
|
||||||
panic("kfree");
|
panic("kfree");
|
||||||
|
|
||||||
// Fill with junk to catch dangling refs.
|
// Fill with junk to catch dangling refs.
|
||||||
|
@ -52,23 +51,23 @@ void kfree(void *pa) {
|
||||||
|
|
||||||
r = (struct Run *)pa;
|
r = (struct Run *)pa;
|
||||||
|
|
||||||
acquire(&kmem.lock);
|
spin_lock(&kmem.lock);
|
||||||
r->next = kmem.freelist;
|
r->next = kmem.freelist;
|
||||||
kmem.freelist = r;
|
kmem.freelist = r;
|
||||||
release(&kmem.lock);
|
spin_unlock(&kmem.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *kalloc(void) {
|
void *kalloc(void) {
|
||||||
struct Run *r;
|
struct Run *r;
|
||||||
|
|
||||||
acquire(&kmem.lock);
|
spin_lock(&kmem.lock);
|
||||||
|
|
||||||
r = kmem.freelist;
|
r = kmem.freelist;
|
||||||
|
|
||||||
if (r)
|
if (r)
|
||||||
kmem.freelist = r->next;
|
kmem.freelist = r->next;
|
||||||
|
|
||||||
release(&kmem.lock);
|
spin_unlock(&kmem.lock);
|
||||||
|
|
||||||
if (r)
|
if (r)
|
||||||
memset((char *)r, 5, PGSIZE); // fill with junk
|
memset((char *)r, 5, PGSIZE); // fill with junk
|
||||||
|
|
77
kern/proc.h
Normal file
77
kern/proc.h
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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;
|
Loading…
Add table
Reference in a new issue