make new code like old code

Variable declarations at top of function,
separate from initialization.

Use == 0 instead of ! for checking pointers.

Consistent spacing around {, *, casts.

Declare 0-parameter functions as (void) not ().

Integer valued functions return -1 on failure, 0 on success.
This commit is contained in:
Russ Cox 2011-01-11 13:01:13 -05:00
parent 240679608c
commit 1a81e38b17
21 changed files with 227 additions and 199 deletions

View file

@ -27,15 +27,16 @@ printint(int xx, int base, int sgn)
{
static char digits[] = "0123456789abcdef";
char buf[16];
int i = 0, neg = 0;
int i, neg;
uint x;
if(sgn && xx < 0){
if(sgn && (neg = xx < 0)){
neg = 1;
x = -xx;
} else
x = xx;
i = 0;
do{
buf[i++] = digits[x % base];
}while((x /= base) != 0);

8
defs.h
View file

@ -62,7 +62,7 @@ void ioapicinit(void);
// kalloc.c
char* kalloc(void);
void kfree(char*);
void kinit();
void kinit(void);
// kbd.c
void kbdintr(void);
@ -117,8 +117,8 @@ void getcallerpcs(void*, uint*);
int holding(struct spinlock*);
void initlock(struct spinlock*, char*);
void release(struct spinlock*);
void pushcli();
void popcli();
void pushcli(void);
void popcli(void);
// string.c
int memcmp(const void*, const void*, uint);
@ -164,7 +164,7 @@ void inituvm(pde_t*, char*, uint);
int loaduvm(pde_t*, char*, struct inode *, uint, uint);
pde_t* copyuvm(pde_t*,uint);
void switchuvm(struct proc*);
void switchkvm();
void switchkvm(void);
int copyout(pde_t *pgdir, uint va, void *buf, uint len);
// number of elements in fixed-size array

22
exec.c
View file

@ -10,16 +10,17 @@ int
exec(char *path, char **argv)
{
char *s, *last;
int i, off;
uint sz = 0;
int i, off, argc;
uint sz, sp, strings[MAXARG];
struct elfhdr elf;
struct inode *ip = 0;
struct inode *ip;
struct proghdr ph;
pde_t *pgdir = 0, *oldpgdir;
pde_t *pgdir, *oldpgdir;
if((ip = namei(path)) == 0)
return -1;
ilock(ip);
pgdir = 0;
// Check ELF header
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
@ -27,10 +28,11 @@ exec(char *path, char **argv)
if(elf.magic != ELF_MAGIC)
goto bad;
if(!(pgdir = setupkvm()))
if((pgdir = setupkvm()) == 0)
goto bad;
// Load program into memory.
sz = 0;
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
goto bad;
@ -38,9 +40,9 @@ exec(char *path, char **argv)
continue;
if(ph.memsz < ph.filesz)
goto bad;
if(!(sz = allocuvm(pgdir, sz, ph.va + ph.memsz)))
if((sz = allocuvm(pgdir, sz, ph.va + ph.memsz)) == 0)
goto bad;
if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
if(loaduvm(pgdir, (char*)ph.va, ip, ph.offset, ph.filesz) < 0)
goto bad;
}
iunlockput(ip);
@ -48,7 +50,7 @@ exec(char *path, char **argv)
// Allocate a one-page stack at the next page boundary
sz = PGROUNDUP(sz);
if(!(sz = allocuvm(pgdir, sz, sz + PGSIZE)))
if((sz = allocuvm(pgdir, sz, sz + PGSIZE)) == 0)
goto bad;
// initialize stack content:
@ -64,17 +66,15 @@ exec(char *path, char **argv)
// argc -- argc argument to main()
// ffffffff -- return PC for main() call
uint sp = sz;
sp = sz;
// count arguments
int argc;
for(argc = 0; argv[argc]; argc++)
;
if(argc >= MAXARG)
goto bad;
// push strings and remember where they are
uint strings[MAXARG];
for(i = argc - 1; i >= 0; --i){
sp -= strlen(argv[i]) + 1;
strings[i] = sp;

2
ide.c
View file

@ -131,7 +131,7 @@ iderw(struct buf *b)
if((b->flags & (B_VALID|B_DIRTY)) == B_VALID)
panic("iderw: nothing to do");
if(b->dev != 0 && !havedisk1)
panic("idrw: ide disk 1 not present");
panic("iderw: ide disk 1 not present");
acquire(&idelock);

View file

@ -23,8 +23,10 @@ extern char end[]; // first address after kernel loaded from ELF file
void
kinit(void)
{
char *p;
initlock(&kmem.lock, "kmem");
char *p = (char*)PGROUNDUP((uint)end);
p = (char*)PGROUNDUP((uint)end);
for(; p + PGSIZE - 1 < (char*)PHYSTOP; p += PGSIZE)
kfree(p);
}
@ -39,7 +41,7 @@ kfree(char *v)
{
struct run *r;
if(((uint) v) % PGSIZE || v < end || (uint)v >= PHYSTOP)
if((uint)v % PGSIZE || v < end || (uint)v >= PHYSTOP)
panic("kfree");
// Fill with junk to catch dangling refs.
@ -56,7 +58,7 @@ kfree(char *v)
// Returns a pointer that the kernel can use.
// Returns 0 if the memory cannot be allocated.
char*
kalloc()
kalloc(void)
{
struct run *r;

26
main.c
View file

@ -7,7 +7,7 @@
static void bootothers(void);
static void mpmain(void);
void jkstack(void) __attribute__((noreturn));
void jmpkstack(void) __attribute__((noreturn));
void mainc(void);
// Bootstrap processor starts running C code here.
@ -20,19 +20,20 @@ main(void)
lapicinit(mpbcpu());
seginit(); // set up segments
kinit(); // initialize memory allocator
jkstack(); // call mainc() on a properly-allocated stack
jmpkstack(); // call mainc() on a properly-allocated stack
}
void
jkstack(void)
jmpkstack(void)
{
char *kstack = kalloc();
if(!kstack)
panic("jkstack\n");
char *top = kstack + PGSIZE;
asm volatile("movl %0,%%esp" : : "r" (top));
asm volatile("call mainc");
panic("jkstack");
char *kstack, *top;
kstack = kalloc();
if(kstack == 0)
panic("jmpkstack kalloc");
top = kstack + PGSIZE;
asm volatile("movl %0,%%esp; call mainc" : : "r" (top));
panic("jmpkstack");
}
// Set up hardware and software.
@ -87,8 +88,8 @@ bootothers(void)
struct cpu *c;
char *stack;
// Write bootstrap code to unused memory at 0x7000. The linker has
// placed the start of bootother.S there.
// Write bootstrap code to unused memory at 0x7000.
// The linker has placed the image of bootother.S in _binary_bootother_start.
code = (uchar*)0x7000;
memmove(code, _binary_bootother_start, (uint)_binary_bootother_size);
@ -110,4 +111,3 @@ bootothers(void)
;
}
}

6
mmu.h
View file

@ -98,10 +98,10 @@ struct segdesc {
// \--- PDX(la) --/ \--- PTX(la) --/
// page directory index
#define PDX(la) ((((uint) (la)) >> PDXSHIFT) & 0x3FF)
#define PDX(la) (((uint)(la) >> PDXSHIFT) & 0x3FF)
// page table index
#define PTX(la) ((((uint) (la)) >> PTXSHIFT) & 0x3FF)
#define PTX(la) (((uint)(la) >> PTXSHIFT) & 0x3FF)
// construct linear address from indexes and offset
#define PGADDR(d, t, o) ((uint)((d) << PDXSHIFT | (t) << PTXSHIFT | (o)))
@ -109,7 +109,7 @@ struct segdesc {
// turn a kernel linear address into a physical address.
// all of the kernel data structures have linear and
// physical addresses that are equal.
#define PADDR(a) ((uint) a)
#define PADDR(a) ((uint)(a))
// Page directory and page table constants.
#define NPDENTRIES 1024 // page directory entries per page directory

2
mp.c
View file

@ -114,7 +114,7 @@ mpinit(void)
case MPPROC:
proc = (struct mpproc*)p;
if(ncpu != proc->apicid){
cprintf("mpinit: ncpu=%d apicpid=%d", ncpu, proc->apicid);
cprintf("mpinit: ncpu=%d apicpid=%d\n", ncpu, proc->apicid);
panic("mpinit");
}
if(proc->flags & MPBOOT)

12
proc.c
View file

@ -120,7 +120,7 @@ userinit(void)
p = allocproc();
initproc = p;
if(!(p->pgdir = setupkvm()))
if((p->pgdir = setupkvm()) == 0)
panic("userinit: out of memory?");
inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
p->sz = PGSIZE;
@ -144,12 +144,14 @@ userinit(void)
int
growproc(int n)
{
uint sz = proc->sz;
uint sz;
sz = proc->sz;
if(n > 0){
if(!(sz = allocuvm(proc->pgdir, sz, sz + n)))
if((sz = allocuvm(proc->pgdir, sz, sz + n)) == 0)
return -1;
} else if(n < 0){
if(!(sz = deallocuvm(proc->pgdir, sz, sz + n)))
if((sz = deallocuvm(proc->pgdir, sz, sz + n)) == 0)
return -1;
}
proc->sz = sz;
@ -171,7 +173,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)) == 0){
kfree(np->kstack);
np->kstack = 0;
np->state = UNUSED;

View file

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

View file

@ -44,8 +44,7 @@ fetchstr(struct proc *p, uint addr, char **pp)
int
argint(int n, int *ip)
{
int x = fetchint(proc, proc->tf->esp + 4 + 4*n, ip);
return x;
return fetchint(proc, proc->tf->esp + 4 + 4*n, ip);
}
// Fetch the nth word-sized system call argument as a pointer

4
user.h
View file

@ -18,10 +18,10 @@ int link(char*, char*);
int mkdir(char*);
int chdir(char*);
int dup(int);
int getpid();
int getpid(void);
char* sbrk(int);
int sleep(int);
int uptime();
int uptime(void);
// ulib.c
int stat(char*, struct stat*);

View file

@ -1237,16 +1237,18 @@ forktest(void)
void
sbrktest(void)
{
int pid;
char *oldbrk = sbrk(0);
int fds[2], pid, pids[32], ppid;
char *a, *b, *c, *lastaddr, *oldbrk, *p, scratch;
uint amt;
printf(stdout, "sbrk test\n");
oldbrk = sbrk(0);
// can one sbrk() less than a page?
char *a = sbrk(0);
a = sbrk(0);
int i;
for(i = 0; i < 5000; i++){
char *b = sbrk(1);
b = sbrk(1);
if(b != a){
printf(stdout, "sbrk test failed %d %x %x\n", i, a, b);
exit();
@ -1259,7 +1261,7 @@ sbrktest(void)
printf(stdout, "sbrk test fork failed\n");
exit();
}
char *c = sbrk(1);
c = sbrk(1);
c = sbrk(1);
if(c != a + 1){
printf(stdout, "sbrk test failed post-fork\n");
@ -1271,13 +1273,13 @@ sbrktest(void)
// can one allocate the full 640K?
a = sbrk(0);
uint amt = (640 * 1024) - (uint) a;
char *p = sbrk(amt);
amt = (640 * 1024) - (uint)a;
p = sbrk(amt);
if(p != a){
printf(stdout, "sbrk test failed 640K test, p %x a %x\n", p, a);
exit();
}
char *lastaddr = (char *)(640 * 1024 - 1);
lastaddr = (char*)(640 * 1024 - 1);
*lastaddr = 99;
// is one forbidden from allocating more than 640K?
@ -1321,8 +1323,8 @@ sbrktest(void)
// can we read the kernel's memory?
for(a = (char*)(640*1024); a < (char*)2000000; a += 50000){
int ppid = getpid();
int pid = fork();
ppid = getpid();
pid = fork();
if(pid < 0){
printf(stdout, "fork failed\n");
exit();
@ -1338,8 +1340,6 @@ sbrktest(void)
// if we run the system out of memory, does it clean up the last
// failed allocation?
sbrk(-(sbrk(0) - oldbrk));
int pids[32];
int fds[2];
if(pipe(fds) != 0){
printf(1, "pipe() failed\n");
exit();
@ -1352,7 +1352,6 @@ sbrktest(void)
// sit around until killed
for(;;) sleep(1000);
}
char scratch;
if(pids[i] != -1)
read(fds[0], &scratch, 1);
}
@ -1392,13 +1391,13 @@ validateint(int *p)
void
validatetest(void)
{
int hi = 1100*1024;
int hi, pid;
uint p;
printf(stdout, "validate test\n");
hi = 1100*1024;
uint p;
for(p = 0; p <= (uint)hi; p += 4096){
int pid;
if((pid = fork()) == 0){
// try to crash the kernel by passing in a badly placed integer
validateint((int*)p);
@ -1425,6 +1424,7 @@ void
bsstest(void)
{
int i;
printf(stdout, "bss test\n");
for(i = 0; i < sizeof(uninit); i++){
if(uninit[i] != '\0'){

129
vm.c
View file

@ -42,23 +42,21 @@ seginit(void)
static pte_t *
walkpgdir(pde_t *pgdir, const void *va, int create)
{
uint r;
pde_t *pde;
pte_t *pgtab;
pde = &pgdir[PDX(va)];
if(*pde & PTE_P){
pgtab = (pte_t*)PTE_ADDR(*pde);
} else if(!create || !(r = (uint) kalloc()))
} else {
if(!create || (pgtab = (pte_t*)kalloc()) == 0)
return 0;
else {
pgtab = (pte_t*) r;
// Make sure all those PTE_P bits are zero.
memset(pgtab, 0, PGSIZE);
// The permissions here are overly generous, but they can
// be further restricted by the permissions in the page table
// entries, if necessary.
*pde = PADDR(r) | PTE_P | PTE_W | PTE_U;
*pde = PADDR(pgtab) | PTE_P | PTE_W | PTE_U;
}
return &pgtab[PTX(va)];
}
@ -69,13 +67,16 @@ walkpgdir(pde_t *pgdir, const void *va, int create)
static int
mappages(pde_t *pgdir, void *la, uint size, uint pa, int perm)
{
char *a = PGROUNDDOWN(la);
char *last = PGROUNDDOWN(la + size - 1);
char *a, *last;
pte_t *pte;
while(1){
pte_t *pte = walkpgdir(pgdir, a, 1);
a = PGROUNDDOWN(la);
last = PGROUNDDOWN(la + size - 1);
for(;;){
pte = walkpgdir(pgdir, a, 1);
if(pte == 0)
return 0;
return -1;
if(*pte & PTE_P)
panic("remap");
*pte = pa | perm | PTE_P;
@ -84,7 +85,7 @@ mappages(pde_t *pgdir, void *la, uint size, uint pa, int perm)
a += PGSIZE;
pa += PGSIZE;
}
return 1;
return 0;
}
// The mappings from logical to linear are one to one (i.e.,
@ -122,23 +123,26 @@ kvmalloc(void)
pde_t*
setupkvm(void)
{
pde_t *pgdir;
extern char etext[];
char *rwstart = PGROUNDDOWN(etext);
uint rwlen = (uint)rwstart - 0x100000;
char *rwstart;
pde_t *pgdir;
uint rwlen;
rwstart = PGROUNDDOWN(etext);
rwlen = (uint)rwstart - 0x100000;
// Allocate page directory
if(!(pgdir = (pde_t *) kalloc()))
if((pgdir = (pde_t*)kalloc()) == 0)
return 0;
memset(pgdir, 0, PGSIZE);
if(// Map IO space from 640K to 1Mbyte
!mappages(pgdir, (void *)USERTOP, 0x60000, USERTOP, PTE_W) ||
mappages(pgdir, (void*)USERTOP, 0x60000, USERTOP, PTE_W) < 0 ||
// Map kernel instructions
!mappages(pgdir, (void *)0x100000, rwlen, 0x100000, 0) ||
mappages(pgdir, (void*)0x100000, rwlen, 0x100000, 0) < 0 ||
// Map kernel data and free memory pool
!mappages(pgdir, rwstart, PHYSTOP-(uint)rwstart, (uint)rwstart, PTE_W) ||
mappages(pgdir, rwstart, PHYSTOP-(uint)rwstart, (uint)rwstart, PTE_W) < 0 ||
// Map devices such as ioapic, lapic, ...
!mappages(pgdir, (void *)0xFE000000, 0x2000000, 0xFE000000, PTE_W))
mappages(pgdir, (void*)0xFE000000, 0x2000000, 0xFE000000, PTE_W) < 0)
return 0;
return pgdir;
}
@ -190,13 +194,14 @@ switchuvm(struct proc *p)
char*
uva2ka(pde_t *pgdir, char *uva)
{
pte_t *pte = walkpgdir(pgdir, uva, 0);
pte_t *pte;
pte = walkpgdir(pgdir, uva, 0);
if((*pte & PTE_P) == 0)
return 0;
if((*pte & PTE_U) == 0)
return 0;
uint pa = PTE_ADDR(*pte);
return (char *)pa;
return (char*)PTE_ADDR(*pte);
}
// Load the initcode into address 0 of pgdir.
@ -204,9 +209,11 @@ uva2ka(pde_t *pgdir, char *uva)
void
inituvm(pde_t *pgdir, char *init, uint sz)
{
char *mem = kalloc();
char *mem;
if(sz >= PGSIZE)
panic("inituvm: more than a page");
mem = kalloc();
memset(mem, 0, PGSIZE);
mappages(pgdir, 0, PGSIZE, PADDR(mem), PTE_W|PTE_U);
memmove(mem, init, sz);
@ -223,15 +230,17 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
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)))
if((pte = walkpgdir(pgdir, addr+i, 0)) == 0)
panic("loaduvm: address should exist\n");
pa = PTE_ADDR(*pte);
if(sz - i < PGSIZE) n = sz - i;
else n = PGSIZE;
if(sz - i < PGSIZE)
n = sz - i;
else
n = PGSIZE;
if(readi(ip, (char*)pa, offset+i, n) != n)
return 0;
return -1;
}
return 1;
return 0;
}
// Allocate memory to the process to bring its size from oldsz to
@ -241,12 +250,17 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
int
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
{
char *a, *last, *mem;
if(newsz > USERTOP)
return 0;
char *a = (char *)PGROUNDUP(oldsz);
char *last = PGROUNDDOWN(newsz - 1);
if(newsz < oldsz)
return oldsz;
a = (char*)PGROUNDUP(oldsz);
last = PGROUNDDOWN(newsz - 1);
for(; a <= last; a += PGSIZE){
char *mem = kalloc();
mem = kalloc();
if(mem == 0){
cprintf("allocuvm out of memory\n");
deallocuvm(pgdir, newsz, oldsz);
@ -255,7 +269,7 @@ allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
memset(mem, 0, PGSIZE);
mappages(pgdir, a, PGSIZE, PADDR(mem), PTE_W|PTE_U);
}
return newsz > oldsz ? newsz : oldsz;
return newsz;
}
// Deallocate user pages to bring the process size from oldsz to
@ -265,19 +279,26 @@ allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
int
deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
{
char *a = (char *)PGROUNDUP(newsz);
char *last = PGROUNDDOWN(oldsz - 1);
char *a, *last;
pte_t *pte;
uint pa;
if(newsz >= oldsz)
return oldsz;
a = (char*)PGROUNDUP(newsz);
last = PGROUNDDOWN(oldsz - 1);
for(; a <= last; a += PGSIZE){
pte_t *pte = walkpgdir(pgdir, a, 0);
pte = walkpgdir(pgdir, a, 0);
if(pte && (*pte & PTE_P) != 0){
uint pa = PTE_ADDR(*pte);
pa = PTE_ADDR(*pte);
if(pa == 0)
panic("kfree");
kfree((void *) pa);
kfree((char*)pa);
*pte = 0;
}
}
return newsz < oldsz ? newsz : oldsz;
return newsz;
}
// Free a page table and all the physical memory pages
@ -287,14 +308,14 @@ freevm(pde_t *pgdir)
{
uint i;
if(!pgdir)
if(pgdir == 0)
panic("freevm: no pgdir");
deallocuvm(pgdir, USERTOP, 0);
for(i = 0; i < NPDENTRIES; i++){
if(pgdir[i] & PTE_P)
kfree((void *) PTE_ADDR(pgdir[i]));
kfree((char*)PTE_ADDR(pgdir[i]));
}
kfree((void *) pgdir);
kfree((char*)pgdir);
}
// Given a parent process's page table, create a copy
@ -302,22 +323,23 @@ freevm(pde_t *pgdir)
pde_t*
copyuvm(pde_t *pgdir, uint sz)
{
pde_t *d = setupkvm();
pde_t *d;
pte_t *pte;
uint pa, i;
char *mem;
if(!d) return 0;
if((d = setupkvm()) == 0)
return 0;
for(i = 0; i < sz; i += PGSIZE){
if(!(pte = walkpgdir(pgdir, (void *)i, 0)))
if((pte = walkpgdir(pgdir, (void*)i, 0)) == 0)
panic("copyuvm: pte should exist\n");
if(!(*pte & PTE_P))
panic("copyuvm: page not present\n");
pa = PTE_ADDR(*pte);
if(!(mem = kalloc()))
if((mem = kalloc()) == 0)
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) < 0)
goto bad;
}
return d;
@ -334,13 +356,16 @@ bad:
int
copyout(pde_t *pgdir, uint va, void *xbuf, uint len)
{
char *buf = (char *) xbuf;
char *buf, *pa0;
uint n, va0;
buf = (char*)xbuf;
while(len > 0){
uint va0 = (uint)PGROUNDDOWN(va);
char *pa0 = uva2ka(pgdir, (char*) va0);
va0 = (uint)PGROUNDDOWN(va);
pa0 = uva2ka(pgdir, (char*)va0);
if(pa0 == 0)
return 0;
uint n = PGSIZE - (va - va0);
return -1;
n = PGSIZE - (va - va0);
if(n > len)
n = len;
memmove(pa0 + (va - va0), buf, n);
@ -348,5 +373,5 @@ copyout(pde_t *pgdir, uint va, void *xbuf, uint len)
buf += n;
va = va0 + PGSIZE;
}
return 1;
return 0;
}