2006-09-06 19:50:20 +02:00
|
|
|
// Physical memory allocator, intended to allocate
|
2010-08-06 17:12:18 +02:00
|
|
|
// memory for user processes. Allocates in 4096-byte pages.
|
2006-09-06 19:50:20 +02:00
|
|
|
// Free list is kept sorted and combines adjacent pages into
|
|
|
|
// long runs, to make it easier to allocate big segments.
|
2010-08-06 17:12:18 +02:00
|
|
|
// This combining is not useful now that xv6 uses paging.
|
2006-06-12 17:22:12 +02:00
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
2006-07-12 13:15:38 +02:00
|
|
|
#include "param.h"
|
2010-07-02 20:51:53 +02:00
|
|
|
#include "mmu.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
#include "spinlock.h"
|
|
|
|
|
2006-06-12 17:22:12 +02:00
|
|
|
struct run {
|
|
|
|
struct run *next;
|
|
|
|
int len; // bytes
|
|
|
|
};
|
2009-05-31 07:12:21 +02:00
|
|
|
|
|
|
|
struct {
|
|
|
|
struct spinlock lock;
|
|
|
|
struct run *freelist;
|
|
|
|
} kmem;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2010-07-02 20:51:53 +02:00
|
|
|
int nfreemem;
|
|
|
|
|
2006-09-06 19:50:20 +02:00
|
|
|
// Initialize free list of physical pages.
|
2006-06-12 17:22:12 +02:00
|
|
|
void
|
2010-07-02 20:51:53 +02:00
|
|
|
kinit(char *p, uint len)
|
2006-06-12 17:22:12 +02:00
|
|
|
{
|
2009-05-31 07:12:21 +02:00
|
|
|
initlock(&kmem.lock, "kmem");
|
2010-07-02 20:51:53 +02:00
|
|
|
nfreemem = 0;
|
2009-07-12 04:26:01 +02:00
|
|
|
kfree(p, len);
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
2007-08-10 18:37:27 +02:00
|
|
|
// Free the len bytes of memory pointed at by v,
|
2006-09-07 16:12:30 +02:00
|
|
|
// which normally should have been returned by a
|
2007-08-10 18:37:27 +02:00
|
|
|
// call to kalloc(len). (The exception is when
|
2006-09-07 16:12:30 +02:00
|
|
|
// initializing the allocator; see kinit above.)
|
2006-06-12 17:22:12 +02:00
|
|
|
void
|
2007-08-10 18:37:27 +02:00
|
|
|
kfree(char *v, int len)
|
2006-06-12 17:22:12 +02:00
|
|
|
{
|
2007-08-14 21:05:48 +02:00
|
|
|
struct run *r, *rend, **rp, *p, *pend;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2010-07-02 20:51:53 +02:00
|
|
|
if(len <= 0 || len % PGSIZE)
|
2006-06-12 17:22:12 +02:00
|
|
|
panic("kfree");
|
|
|
|
|
2006-09-06 20:06:04 +02:00
|
|
|
// Fill with junk to catch dangling refs.
|
2007-08-10 18:37:27 +02:00
|
|
|
memset(v, 1, len);
|
2006-07-01 23:26:01 +02:00
|
|
|
|
2009-05-31 07:12:21 +02:00
|
|
|
acquire(&kmem.lock);
|
2010-07-02 20:51:53 +02:00
|
|
|
nfreemem += len;
|
2007-08-10 19:02:36 +02:00
|
|
|
p = (struct run*)v;
|
|
|
|
pend = (struct run*)(v + len);
|
2009-05-31 07:12:21 +02:00
|
|
|
for(rp=&kmem.freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
|
2007-08-14 21:05:48 +02:00
|
|
|
rend = (struct run*)((char*)r + r->len);
|
2010-07-02 20:51:53 +02:00
|
|
|
if(r <= p && p < rend) {
|
|
|
|
cprintf("freeing a free page: r = 0x%x p = 0x%x rend = 0x%x\n",
|
|
|
|
r, p, rend);
|
2006-06-12 17:22:12 +02:00
|
|
|
panic("freeing free page");
|
2010-07-02 20:51:53 +02:00
|
|
|
}
|
2009-07-12 04:26:01 +02:00
|
|
|
if(rend == p){ // r before p: expand r to include p
|
2007-08-14 21:05:48 +02:00
|
|
|
r->len += len;
|
|
|
|
if(r->next && r->next == pend){ // r now next to r->next?
|
|
|
|
r->len += r->next->len;
|
|
|
|
r->next = r->next->next;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
2006-07-12 03:48:35 +02:00
|
|
|
goto out;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
2009-07-12 04:26:01 +02:00
|
|
|
if(pend == r){ // p before r: expand p to include, replace r
|
|
|
|
p->len = len + r->len;
|
|
|
|
p->next = r->next;
|
|
|
|
*rp = p;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
2007-08-14 21:05:48 +02:00
|
|
|
// Insert p before r in list.
|
2006-06-12 17:22:12 +02:00
|
|
|
p->len = len;
|
2007-08-14 21:05:48 +02:00
|
|
|
p->next = r;
|
|
|
|
*rp = p;
|
2006-07-12 03:48:35 +02:00
|
|
|
|
|
|
|
out:
|
2009-05-31 07:12:21 +02:00
|
|
|
release(&kmem.lock);
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
2006-09-06 19:50:20 +02:00
|
|
|
// Allocate n bytes of physical memory.
|
|
|
|
// Returns a kernel-segment pointer.
|
|
|
|
// Returns 0 if the memory cannot be allocated.
|
2006-07-16 18:05:37 +02:00
|
|
|
char*
|
2006-06-12 17:22:12 +02:00
|
|
|
kalloc(int n)
|
|
|
|
{
|
2007-08-10 19:02:36 +02:00
|
|
|
char *p;
|
2007-08-27 14:50:36 +02:00
|
|
|
struct run *r, **rp;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2010-07-02 20:51:53 +02:00
|
|
|
if(n % PGSIZE || n <= 0)
|
2006-06-12 17:22:12 +02:00
|
|
|
panic("kalloc");
|
|
|
|
|
2009-05-31 07:12:21 +02:00
|
|
|
acquire(&kmem.lock);
|
|
|
|
for(rp=&kmem.freelist; (r=*rp) != 0; rp=&r->next){
|
2009-07-12 04:26:01 +02:00
|
|
|
if(r->len >= n){
|
2006-06-12 17:22:12 +02:00
|
|
|
r->len -= n;
|
2007-08-10 19:02:36 +02:00
|
|
|
p = (char*)r + r->len;
|
2009-07-12 04:26:01 +02:00
|
|
|
if(r->len == 0)
|
|
|
|
*rp = r->next;
|
2010-07-02 20:51:53 +02:00
|
|
|
nfreemem -= n;
|
2009-05-31 07:12:21 +02:00
|
|
|
release(&kmem.lock);
|
2006-06-12 17:22:12 +02:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
2009-05-31 07:12:21 +02:00
|
|
|
release(&kmem.lock);
|
2006-06-12 17:22:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-07-02 20:51:53 +02:00
|
|
|
|