Space police

This commit is contained in:
Austin Clements 2010-09-01 00:41:25 -04:00
parent 5efca9054f
commit b0751a3e9b
6 changed files with 45 additions and 45 deletions

View file

@ -163,7 +163,7 @@ consputc(int c)
;
}
if (c == BACKSPACE) {
if(c == BACKSPACE){
uartputc('\b');
uartputc(' ');
uartputc('\b');

2
main.c
View file

@ -29,7 +29,7 @@ void
jkstack(void)
{
char *kstack = kalloc();
if (!kstack)
if(!kstack)
panic("jkstack\n");
char *top = kstack + PGSIZE;
asm volatile("movl %0,%%esp" : : "r" (top));

10
proc.c
View file

@ -116,9 +116,9 @@ userinit(void)
p = allocproc();
initproc = p;
if (!(p->pgdir = setupkvm()))
if(!(p->pgdir = setupkvm()))
panic("userinit: out of memory?");
if (!allocuvm(p->pgdir, 0x0, (int)_binary_initcode_size))
if(!allocuvm(p->pgdir, 0x0, (int)_binary_initcode_size))
panic("userinit: out of memory?");
inituvm(p->pgdir, 0x0, _binary_initcode_start,
(int)_binary_initcode_size);
@ -144,10 +144,10 @@ int
growproc(int n)
{
if(n > 0){
if (!allocuvm(proc->pgdir, (char *)proc->sz, n))
if(!allocuvm(proc->pgdir, (char *)proc->sz, n))
return -1;
} else if(n < 0){
if (!deallocuvm(proc->pgdir, (char *)(proc->sz + n), 0 - n))
if(!deallocuvm(proc->pgdir, (char *)(proc->sz + n), 0 - n))
return -1;
}
proc->sz += n;
@ -169,7 +169,7 @@ fork(void)
return -1;
// Copy process state from p.
if (!(np->pgdir = copyuvm(proc->pgdir, proc->sz))) {
if(!(np->pgdir = copyuvm(proc->pgdir, proc->sz))){
kfree(np->kstack);
np->kstack = 0;
np->state = UNUSED;

View file

@ -17,8 +17,8 @@ main(int argc, char *argv[])
int i;
printf(1, "stressfs starting\n");
for (i = 0; i < 4; i++) {
if (fork() > 0) {
for(i = 0; i < 4; i++){
if(fork() > 0){
break;
}
}
@ -28,7 +28,7 @@ main(int argc, char *argv[])
char path[] = "stressfs0";
path[8] += i;
int fd = open(path, O_CREATE | O_RDWR);
for (i = 0; i < 100; i++)
for(i = 0; i < 100; i++)
printf(fd, "%d\n", i);
close(fd);

View file

@ -1342,28 +1342,28 @@ sbrktest(void)
printf(1, "pipe() failed\n");
exit();
}
for (i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
if ((pids[i] = fork()) == 0) {
for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
if((pids[i] = fork()) == 0) {
// allocate the full 640K
sbrk((640 * 1024) - (uint)sbrk(0));
write(fds[1], "x", 1);
// sit around until killed
while (1) sleep(1000);
for(;;) sleep(1000);
}
char scratch;
if (pids[i] != -1)
if(pids[i] != -1)
read(fds[0], &scratch, 1);
}
// if those failed allocations freed up the pages they did allocate,
// we'll be able to allocate here
c = sbrk(4096);
for (i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
if (pids[i] == -1)
for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
if(pids[i] == -1)
continue;
kill(pids[i]);
wait();
}
if (c == (char*)0xffffffff) {
if(c == (char*)0xffffffff) {
printf(stdout, "failed sbrk leaked memory\n");
exit();
}

56
vm.c
View file

@ -44,9 +44,9 @@ walkpgdir(pde_t *pgdir, const void *va, int create)
pte_t *pgtab;
pde = &pgdir[PDX(va)];
if (*pde & PTE_P) {
if(*pde & PTE_P){
pgtab = (pte_t*) PTE_ADDR(*pde);
} else if (!create || !(r = (uint) kalloc()))
} else if(!create || !(r = (uint) kalloc()))
return 0;
else {
pgtab = (pte_t*) r;
@ -127,7 +127,7 @@ switchuvm(struct proc *p)
cpu->ts.esp0 = (uint)proc->kstack + KSTACKSIZE;
ltr(SEG_TSS << 3);
if (p->pgdir == 0)
if(p->pgdir == 0)
panic("switchuvm: no pgdir\n");
lcr3(PADDR(p->pgdir)); // switch to new address space
@ -149,17 +149,17 @@ setupkvm(void)
pde_t *pgdir;
// Allocate page directory
if (!(pgdir = (pde_t *) kalloc()))
if(!(pgdir = (pde_t *) kalloc()))
return 0;
memset(pgdir, 0, PGSIZE);
// Map IO space from 640K to 1Mbyte
if (!mappages(pgdir, (void *)USERTOP, 0x60000, USERTOP, PTE_W))
if(!mappages(pgdir, (void *)USERTOP, 0x60000, USERTOP, PTE_W))
return 0;
// Map kernel and free memory pool
if (!mappages(pgdir, (void *)0x100000, PHYSTOP-0x100000, 0x100000, PTE_W))
if(!mappages(pgdir, (void *)0x100000, PHYSTOP-0x100000, 0x100000, PTE_W))
return 0;
// Map devices such as ioapic, lapic, ...
if (!mappages(pgdir, (void *)0xFE000000, 0x2000000, 0xFE000000, PTE_W))
if(!mappages(pgdir, (void *)0xFE000000, 0x2000000, 0xFE000000, PTE_W))
return 0;
return pgdir;
}
@ -172,7 +172,7 @@ char*
uva2ka(pde_t *pgdir, char *uva)
{
pte_t *pte = walkpgdir(pgdir, uva, 0);
if (pte == 0) return 0;
if(pte == 0) return 0;
uint pa = PTE_ADDR(*pte);
return (char *)pa;
}
@ -185,7 +185,7 @@ uva2ka(pde_t *pgdir, char *uva)
int
allocuvm(pde_t *pgdir, char *addr, uint sz)
{
if (addr + sz > (char*)USERTOP)
if(addr + sz > (char*)USERTOP)
return 0;
char *first = PGROUNDDOWN(addr);
char *last = PGROUNDDOWN(addr + sz - 1);
@ -212,7 +212,7 @@ allocuvm(pde_t *pgdir, char *addr, uint sz)
int
deallocuvm(pde_t *pgdir, char *addr, uint sz)
{
if (addr + sz > (char*)USERTOP)
if(addr + sz > (char*)USERTOP)
return 0;
char *first = (char*) PGROUNDUP((uint)addr);
char *last = PGROUNDDOWN(addr + sz - 1);
@ -237,17 +237,17 @@ freevm(pde_t *pgdir)
{
uint i, j, da;
if (!pgdir)
if(!pgdir)
panic("freevm: no pgdir\n");
for (i = 0; i < NPDENTRIES; i++) {
for(i = 0; i < NPDENTRIES; i++){
da = PTE_ADDR(pgdir[i]);
if (da != 0) {
if(da != 0){
pte_t *pgtab = (pte_t*) da;
for (j = 0; j < NPTENTRIES; j++) {
if (pgtab[j] != 0) {
for(j = 0; j < NPTENTRIES; j++){
if(pgtab[j] != 0){
uint pa = PTE_ADDR(pgtab[j]);
uint va = PGADDR(i, j, 0);
if (va < USERTOP) // user memory
if(va < USERTOP) // user memory
kfree((void *) pa);
pgtab[j] = 0;
}
@ -265,13 +265,13 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
uint i, pa, n;
pte_t *pte;
if ((uint)addr % PGSIZE != 0)
if((uint)addr % PGSIZE != 0)
panic("loaduvm: addr must be page aligned\n");
for (i = 0; i < sz; i += PGSIZE) {
if (!(pte = walkpgdir(pgdir, addr+i, 0)))
for(i = 0; i < sz; i += PGSIZE){
if(!(pte = walkpgdir(pgdir, addr+i, 0)))
panic("loaduvm: address should exist\n");
pa = PTE_ADDR(*pte);
if (sz - i < PGSIZE) n = sz - i;
if(sz - i < PGSIZE) n = sz - i;
else n = PGSIZE;
if(readi(ip, (char *)pa, offset+i, n) != n)
return 0;
@ -285,12 +285,12 @@ inituvm(pde_t *pgdir, char *addr, char *init, uint sz)
uint i, pa, n, off;
pte_t *pte;
for (i = 0; i < sz; i += PGSIZE) {
if (!(pte = walkpgdir(pgdir, (void *)(i+addr), 0)))
for(i = 0; i < sz; i += PGSIZE){
if(!(pte = walkpgdir(pgdir, (void *)(i+addr), 0)))
panic("inituvm: pte should exist\n");
off = (i+(uint)addr) % PGSIZE;
pa = PTE_ADDR(*pte);
if (sz - i < PGSIZE) n = sz - i;
if(sz - i < PGSIZE) n = sz - i;
else n = PGSIZE;
memmove((char *)pa+off, init+i, n);
}
@ -306,16 +306,16 @@ copyuvm(pde_t *pgdir, uint sz)
uint pa, i;
char *mem;
if (!d) return 0;
for (i = 0; i < sz; i += PGSIZE) {
if (!(pte = walkpgdir(pgdir, (void *)i, 0)))
if(!d) return 0;
for(i = 0; i < sz; i += PGSIZE){
if(!(pte = walkpgdir(pgdir, (void *)i, 0)))
panic("copyuvm: pte should exist\n");
if(*pte & PTE_P){
pa = PTE_ADDR(*pte);
if (!(mem = kalloc()))
if(!(mem = kalloc()))
goto bad;
memmove(mem, (char *)pa, PGSIZE);
if (!mappages(d, (void *)i, PGSIZE, PADDR(mem), PTE_W|PTE_U))
if(!mappages(d, (void *)i, PGSIZE, PADDR(mem), PTE_W|PTE_U))
goto bad;
}
}