Simplify inituvm and userinit by assuming initcode fits on a page

This commit is contained in:
Austin Clements 2010-09-02 15:37:05 -04:00
parent b3cfd7fc37
commit f53e6110be
3 changed files with 10 additions and 19 deletions

2
defs.h
View file

@ -160,7 +160,7 @@ char* uva2ka(pde_t*, char*);
int allocuvm(pde_t*, char*, uint); int allocuvm(pde_t*, char*, uint);
int deallocuvm(pde_t *pgdir, char *addr, uint sz); int deallocuvm(pde_t *pgdir, char *addr, uint sz);
void freevm(pde_t*); void freevm(pde_t*);
void inituvm(pde_t*, char*, 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);
pde_t* copyuvm(pde_t*,uint); pde_t* copyuvm(pde_t*,uint);
void switchuvm(struct proc*); void switchuvm(struct proc*);

7
proc.c
View file

@ -120,11 +120,8 @@ userinit(void)
initproc = p; initproc = p;
if(!(p->pgdir = setupkvm())) if(!(p->pgdir = setupkvm()))
panic("userinit: out of memory?"); panic("userinit: out of memory?");
if(!allocuvm(p->pgdir, 0x0, (int)_binary_initcode_size)) inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
panic("userinit: out of memory?"); p->sz = PGSIZE;
inituvm(p->pgdir, 0x0, _binary_initcode_start,
(int)_binary_initcode_size);
p->sz = PGROUNDUP((int)_binary_initcode_size);
memset(p->tf, 0, sizeof(*p->tf)); memset(p->tf, 0, sizeof(*p->tf));
p->tf->cs = (SEG_UCODE << 3) | DPL_USER; p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
p->tf->ds = (SEG_UDATA << 3) | DPL_USER; p->tf->ds = (SEG_UDATA << 3) | DPL_USER;

20
vm.c
View file

@ -267,20 +267,14 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
} }
void void
inituvm(pde_t *pgdir, char *addr, char *init, uint sz) inituvm(pde_t *pgdir, char *init, uint sz)
{ {
uint i, pa, n, off; char *mem = kalloc();
pte_t *pte; if (sz >= PGSIZE)
panic("inituvm: more than a page");
for(i = 0; i < sz; i += PGSIZE){ memset(mem, 0, PGSIZE);
if(!(pte = walkpgdir(pgdir, (void *)(i+addr), 0))) mappages(pgdir, 0, PGSIZE, PADDR(mem), PTE_W|PTE_U);
panic("inituvm: pte should exist\n"); memmove(mem, init, sz);
off = (i+(uint)addr) % PGSIZE;
pa = PTE_ADDR(*pte);
if(sz - i < PGSIZE) n = sz - i;
else n = PGSIZE;
memmove((char *)pa+off, init+i, n);
}
} }
// given a parent process's page table, create a copy // given a parent process's page table, create a copy