Simplify allocuvm/deallocuvm to operate in a contiguous memory model. This makes their interface match up better with proc->sz and also simplifies the callers (it even gets the main body of exec on one page).
This commit is contained in:
parent
d49d31381d
commit
79cd8b3eed
4 changed files with 44 additions and 51 deletions
4
defs.h
4
defs.h
|
@ -157,8 +157,8 @@ void kvmalloc(void);
|
||||||
void vmenable(void);
|
void vmenable(void);
|
||||||
pde_t* setupkvm(void);
|
pde_t* setupkvm(void);
|
||||||
char* uva2ka(pde_t*, char*);
|
char* uva2ka(pde_t*, char*);
|
||||||
int allocuvm(pde_t*, char*, uint);
|
int allocuvm(pde_t*, uint, uint);
|
||||||
int deallocuvm(pde_t *pgdir, char *addr, uint sz);
|
int deallocuvm(pde_t*, uint, uint);
|
||||||
void freevm(pde_t*);
|
void freevm(pde_t*);
|
||||||
void inituvm(pde_t*, char*, uint);
|
void inituvm(pde_t*, char*, uint);
|
||||||
int loaduvm(pde_t*, char*, struct inode *ip, uint, uint);
|
int loaduvm(pde_t*, char*, struct inode *ip, uint, uint);
|
||||||
|
|
26
exec.c
26
exec.c
|
@ -11,7 +11,7 @@ exec(char *path, char **argv)
|
||||||
{
|
{
|
||||||
char *mem, *s, *last;
|
char *mem, *s, *last;
|
||||||
int i, argc, arglen, len, off;
|
int i, argc, arglen, len, off;
|
||||||
uint sz, sp, spoffset, argp;
|
uint sz, sp, spbottom, argp;
|
||||||
struct elfhdr elf;
|
struct elfhdr elf;
|
||||||
struct inode *ip;
|
struct inode *ip;
|
||||||
struct proghdr ph;
|
struct proghdr ph;
|
||||||
|
@ -41,22 +41,18 @@ exec(char *path, char **argv)
|
||||||
continue;
|
continue;
|
||||||
if(ph.memsz < ph.filesz)
|
if(ph.memsz < ph.filesz)
|
||||||
goto bad;
|
goto bad;
|
||||||
if(!allocuvm(pgdir, (char *)ph.va, ph.memsz))
|
if(!(sz = allocuvm(pgdir, sz, ph.va + ph.memsz)))
|
||||||
goto bad;
|
goto bad;
|
||||||
if(ph.va + ph.memsz > sz)
|
|
||||||
sz = ph.va + ph.memsz;
|
|
||||||
if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
|
if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
iunlockput(ip);
|
iunlockput(ip);
|
||||||
|
|
||||||
// Allocate and initialize stack at sz
|
// Allocate and initialize stack at sz
|
||||||
sz = PGROUNDUP(sz);
|
sz = spbottom = PGROUNDUP(sz);
|
||||||
if(!allocuvm(pgdir, (char *)sz, PGSIZE))
|
if(!(sz = allocuvm(pgdir, sz, sz + PGSIZE)))
|
||||||
goto bad;
|
goto bad;
|
||||||
mem = uva2ka(pgdir, (char *)sz);
|
mem = uva2ka(pgdir, (char *)spbottom);
|
||||||
spoffset = sz;
|
|
||||||
sz += PGSIZE;
|
|
||||||
|
|
||||||
arglen = 0;
|
arglen = 0;
|
||||||
for(argc=0; argv[argc]; argc++)
|
for(argc=0; argv[argc]; argc++)
|
||||||
|
@ -67,22 +63,22 @@ exec(char *path, char **argv)
|
||||||
argp = sz - arglen - 4*(argc+1);
|
argp = sz - arglen - 4*(argc+1);
|
||||||
|
|
||||||
// Copy argv strings and pointers to stack.
|
// Copy argv strings and pointers to stack.
|
||||||
*(uint*)(mem+argp-spoffset + 4*argc) = 0; // argv[argc]
|
*(uint*)(mem+argp-spbottom + 4*argc) = 0; // argv[argc]
|
||||||
for(i=argc-1; i>=0; i--){
|
for(i=argc-1; i>=0; i--){
|
||||||
len = strlen(argv[i]) + 1;
|
len = strlen(argv[i]) + 1;
|
||||||
sp -= len;
|
sp -= len;
|
||||||
memmove(mem+sp-spoffset, argv[i], len);
|
memmove(mem+sp-spbottom, argv[i], len);
|
||||||
*(uint*)(mem+argp-spoffset + 4*i) = sp; // argv[i]
|
*(uint*)(mem+argp-spbottom + 4*i) = sp; // argv[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stack frame for main(argc, argv), below arguments.
|
// Stack frame for main(argc, argv), below arguments.
|
||||||
sp = argp;
|
sp = argp;
|
||||||
sp -= 4;
|
sp -= 4;
|
||||||
*(uint*)(mem+sp-spoffset) = argp;
|
*(uint*)(mem+sp-spbottom) = argp;
|
||||||
sp -= 4;
|
sp -= 4;
|
||||||
*(uint*)(mem+sp-spoffset) = argc;
|
*(uint*)(mem+sp-spbottom) = argc;
|
||||||
sp -= 4;
|
sp -= 4;
|
||||||
*(uint*)(mem+sp-spoffset) = 0xffffffff; // fake return pc
|
*(uint*)(mem+sp-spbottom) = 0xffffffff; // fake return pc
|
||||||
|
|
||||||
// Save program name for debugging.
|
// Save program name for debugging.
|
||||||
for(last=s=path; *s; s++)
|
for(last=s=path; *s; s++)
|
||||||
|
|
7
proc.c
7
proc.c
|
@ -142,14 +142,15 @@ userinit(void)
|
||||||
int
|
int
|
||||||
growproc(int n)
|
growproc(int n)
|
||||||
{
|
{
|
||||||
|
uint sz = proc->sz;
|
||||||
if(n > 0){
|
if(n > 0){
|
||||||
if(!allocuvm(proc->pgdir, (char *)proc->sz, n))
|
if(!(sz = allocuvm(proc->pgdir, sz, sz + n)))
|
||||||
return -1;
|
return -1;
|
||||||
} else if(n < 0){
|
} else if(n < 0){
|
||||||
if(!deallocuvm(proc->pgdir, (char *)(proc->sz + n), 0 - n))
|
if(!(sz = deallocuvm(proc->pgdir, sz, sz + n)))
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
proc->sz += n;
|
proc->sz = sz;
|
||||||
switchuvm(proc);
|
switchuvm(proc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
46
vm.c
46
vm.c
|
@ -228,54 +228,50 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate sz bytes more memory for a process starting at the
|
// Allocate memory to the process to bring its size from oldsz to
|
||||||
// given user address; allocates physical memory and page
|
// newsz. Allocates physical memory and page table entries. oldsz and
|
||||||
// table entries. addr and sz need not be page-aligned.
|
// newsz need not be page-aligned, nor does newsz have to be larger
|
||||||
// It is a no-op for any parts of the requested memory
|
// than oldsz. Returns the new process size or 0 on error.
|
||||||
// that are already allocated.
|
|
||||||
int
|
int
|
||||||
allocuvm(pde_t *pgdir, char *addr, uint sz)
|
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
|
||||||
{
|
{
|
||||||
if(addr + sz > (char*)USERTOP)
|
if(newsz > USERTOP)
|
||||||
return 0;
|
return 0;
|
||||||
char *a = PGROUNDDOWN(addr);
|
char *a = (char *)PGROUNDUP(oldsz);
|
||||||
char *last = PGROUNDDOWN(addr + sz - 1);
|
char *last = PGROUNDDOWN(newsz - 1);
|
||||||
for(; a <= last; a += PGSIZE){
|
for (; a <= last; a += PGSIZE){
|
||||||
pte_t *pte = walkpgdir(pgdir, a, 0);
|
|
||||||
if(pte == 0 || (*pte & PTE_P) == 0){
|
|
||||||
char *mem = kalloc();
|
char *mem = kalloc();
|
||||||
if(mem == 0){
|
if(mem == 0){
|
||||||
cprintf("allocuvm out of memory\n");
|
cprintf("allocuvm out of memory\n");
|
||||||
deallocuvm(pgdir, addr, sz);
|
deallocuvm(pgdir, newsz, oldsz);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
memset(mem, 0, PGSIZE);
|
memset(mem, 0, PGSIZE);
|
||||||
mappages(pgdir, a, PGSIZE, PADDR(mem), PTE_W|PTE_U);
|
mappages(pgdir, a, PGSIZE, PADDR(mem), PTE_W|PTE_U);
|
||||||
}
|
}
|
||||||
}
|
return newsz > oldsz ? newsz : oldsz;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deallocate some of the user pages. If addr is not page-aligned,
|
// Deallocate user pages to bring the process size from oldsz to
|
||||||
// then only deallocates starting at the next page boundary.
|
// newsz. oldsz and newsz need not be page-aligned, nor does newsz
|
||||||
|
// need to be less than oldsz. oldsz can be larger than the actual
|
||||||
|
// process size. Returns the new process size.
|
||||||
int
|
int
|
||||||
deallocuvm(pde_t *pgdir, char *addr, uint sz)
|
deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
|
||||||
{
|
{
|
||||||
if(addr + sz > (char*)USERTOP)
|
char *a = (char *)PGROUNDUP(newsz);
|
||||||
return 0;
|
char *last = PGROUNDDOWN(oldsz - 1);
|
||||||
char *a = (char *)PGROUNDUP((uint)addr);
|
|
||||||
char *last = PGROUNDDOWN(addr + sz - 1);
|
|
||||||
for(; a <= last; a += PGSIZE){
|
for(; a <= last; a += PGSIZE){
|
||||||
pte_t *pte = walkpgdir(pgdir, a, 0);
|
pte_t *pte = walkpgdir(pgdir, a, 0);
|
||||||
if(pte && (*pte & PTE_P) != 0){
|
if(pte && (*pte & PTE_P) != 0){
|
||||||
uint pa = PTE_ADDR(*pte);
|
uint pa = PTE_ADDR(*pte);
|
||||||
if(pa == 0)
|
if(pa == 0)
|
||||||
panic("deallocuvm");
|
panic("kfree");
|
||||||
kfree((void *) pa);
|
kfree((void *) pa);
|
||||||
*pte = 0;
|
*pte = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return newsz < oldsz ? newsz : oldsz;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free a page table and all the physical memory pages
|
// Free a page table and all the physical memory pages
|
||||||
|
@ -287,7 +283,7 @@ freevm(pde_t *pgdir)
|
||||||
|
|
||||||
if(!pgdir)
|
if(!pgdir)
|
||||||
panic("freevm: no pgdir");
|
panic("freevm: no pgdir");
|
||||||
deallocuvm(pgdir, 0, USERTOP);
|
deallocuvm(pgdir, USERTOP, 0);
|
||||||
for(i = 0; i < NPDENTRIES; i++){
|
for(i = 0; i < NPDENTRIES; i++){
|
||||||
if(pgdir[i] & PTE_P)
|
if(pgdir[i] & PTE_P)
|
||||||
kfree((void *) PTE_ADDR(pgdir[i]));
|
kfree((void *) PTE_ADDR(pgdir[i]));
|
||||||
|
|
Loading…
Reference in a new issue