77 lines
1.4 KiB
C
77 lines
1.4 KiB
C
// Physical memory allocator, for user processes,
|
|
// kernel stacks, page-table pages,
|
|
// and pipe buffers. Allocates whole 4096-byte pages.
|
|
|
|
#include "types.h"
|
|
#include "memlayout.h"
|
|
#include "spinlock.h"
|
|
#include "riscv.h"
|
|
#include "defs.h"
|
|
|
|
/** Free list of physical pages. */
|
|
void freerange(void *pa_start, void *pa_end);
|
|
|
|
/** first address after kernel. defined by kernel.ld. */
|
|
extern char end[];
|
|
|
|
/** A run is a node in the free list. */
|
|
struct run {
|
|
struct run *next;
|
|
};
|
|
|
|
/** Kernel memory allocator. */
|
|
struct {
|
|
struct spinlock lock;
|
|
struct run *freelist;
|
|
} kmem;
|
|
|
|
void
|
|
kinit()
|
|
{
|
|
initlock(&kmem.lock, "kmem");
|
|
freerange(end, (void *)PHYSTOP);
|
|
}
|
|
|
|
void
|
|
freerange(void *pa_start, void *pa_end)
|
|
{
|
|
char *p;
|
|
p = (char *)PGROUNDUP((u64)pa_start);
|
|
for(; p + PGSIZE <= (char *)pa_end; p += PGSIZE)
|
|
kfree(p);
|
|
}
|
|
|
|
void
|
|
kfree(void *pa)
|
|
{
|
|
struct run *r;
|
|
|
|
if(((u64)pa % PGSIZE) != 0 || (char *)pa < end || (u64)pa >= PHYSTOP)
|
|
panic("kfree");
|
|
|
|
// Fill with junk to catch dangling refs.
|
|
memset(pa, 1, PGSIZE);
|
|
|
|
r = (struct run *)pa;
|
|
|
|
acquire(&kmem.lock);
|
|
r->next = kmem.freelist;
|
|
kmem.freelist = r;
|
|
release(&kmem.lock);
|
|
}
|
|
|
|
void *
|
|
kalloc(void)
|
|
{
|
|
struct run *r;
|
|
|
|
acquire(&kmem.lock);
|
|
r = kmem.freelist;
|
|
if(r)
|
|
kmem.freelist = r->next;
|
|
release(&kmem.lock);
|
|
|
|
if(r)
|
|
memset((char *)r, 5, PGSIZE); // fill with junk
|
|
return (void *)r;
|
|
}
|