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('\b');
uartputc(' '); uartputc(' ');
uartputc('\b'); uartputc('\b');

2
main.c
View file

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

10
proc.c
View file

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

View file

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

View file

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

56
vm.c
View file

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