diff --git a/kernel/bio.c b/kernel/bio.c index 6cec700..f58e034 100644 --- a/kernel/bio.c +++ b/kernel/bio.c @@ -13,7 +13,6 @@ // * Only one process at a time can use a buffer, // so do not keep them longer than necessary. - #include "types.h" #include "param.h" #include "spinlock.h" @@ -25,7 +24,7 @@ struct { struct spinlock lock; - struct buf buf[NBUF]; + struct buf buf[NBUF]; // Linked list of all buffers, through prev/next. // Sorted by how recently the buffer was used. @@ -43,7 +42,7 @@ binit(void) // Create linked list of buffers bcache.head.prev = &bcache.head; bcache.head.next = &bcache.head; - for(b = bcache.buf; b < bcache.buf+NBUF; b++){ + for(b = bcache.buf; b < bcache.buf + NBUF; b++) { b->next = bcache.head.next; b->prev = &bcache.head; initsleeplock(&b->lock, "buffer"); @@ -55,7 +54,7 @@ binit(void) // Look through buffer cache for block on device dev. // If not found, allocate a buffer. // In either case, return locked buffer. -static struct buf* +static struct buf * bget(u32 dev, u32 blockno) { struct buf *b; @@ -63,8 +62,8 @@ bget(u32 dev, u32 blockno) acquire(&bcache.lock); // Is the block already cached? - for(b = bcache.head.next; b != &bcache.head; b = b->next){ - if(b->dev == dev && b->blockno == blockno){ + for(b = bcache.head.next; b != &bcache.head; b = b->next) { + if(b->dev == dev && b->blockno == blockno) { b->refcnt++; release(&bcache.lock); acquiresleep(&b->lock); @@ -74,7 +73,7 @@ bget(u32 dev, u32 blockno) // Not cached. // Recycle the least recently used (LRU) unused buffer. - for(b = bcache.head.prev; b != &bcache.head; b = b->prev){ + for(b = bcache.head.prev; b != &bcache.head; b = b->prev) { if(b->refcnt == 0) { b->dev = dev; b->blockno = blockno; @@ -89,7 +88,7 @@ bget(u32 dev, u32 blockno) } // Return a locked buf with the contents of the indicated block. -struct buf* +struct buf * bread(u32 dev, u32 blockno) { struct buf *b; @@ -123,7 +122,7 @@ brelse(struct buf *b) acquire(&bcache.lock); b->refcnt--; - if (b->refcnt == 0) { + if(b->refcnt == 0) { // no one is waiting for it. b->next->prev = b->prev; b->prev->next = b->next; @@ -132,22 +131,22 @@ brelse(struct buf *b) bcache.head.next->prev = b; bcache.head.next = b; } - + release(&bcache.lock); } void -bpin(struct buf *b) { +bpin(struct buf *b) +{ acquire(&bcache.lock); b->refcnt++; release(&bcache.lock); } void -bunpin(struct buf *b) { +bunpin(struct buf *b) +{ acquire(&bcache.lock); b->refcnt--; release(&bcache.lock); } - - diff --git a/kernel/buf.h b/kernel/buf.h index 366a1d9..319f8e1 100644 --- a/kernel/buf.h +++ b/kernel/buf.h @@ -1,12 +1,11 @@ struct buf { - int valid; // has data been read from disk? - int disk; // does disk "own" buf? - u32 dev; - u32 blockno; + int valid; // has data been read from disk? + int disk; // does disk "own" buf? + u32 dev; + u32 blockno; struct sleeplock lock; - u32 refcnt; - struct buf *prev; // LRU cache list - struct buf *next; - u8 data[BSIZE]; + u32 refcnt; + struct buf *prev; // LRU cache list + struct buf *next; + u8 data[BSIZE]; }; - diff --git a/kernel/console.c b/kernel/console.c index 0d2905a..ba75355 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -23,7 +23,7 @@ #include "proc.h" #define BACKSPACE 0x100 -#define C(x) ((x)-'@') // Control-x +#define C(x) ((x) - '@') // Control-x // // send one character to the uart. @@ -33,9 +33,11 @@ void consputc(int c) { - if(c == BACKSPACE){ + if(c == BACKSPACE) { // if the user typed backspace, overwrite with a space. - uartputc_sync('\b'); uartputc_sync(' '); uartputc_sync('\b'); + uartputc_sync('\b'); + uartputc_sync(' '); + uartputc_sync('\b'); } else { uartputc_sync(c); } @@ -43,13 +45,13 @@ consputc(int c) struct { struct spinlock lock; - + // input #define INPUT_BUF_SIZE 128 char buf[INPUT_BUF_SIZE]; - u32 r; // Read index - u32 w; // Write index - u32 e; // Edit index + u32 r; // Read index + u32 w; // Write index + u32 e; // Edit index } cons; // @@ -60,9 +62,9 @@ consolewrite(int user_src, u64 src, int n) { int i; - for(i = 0; i < n; i++){ + for(i = 0; i < n; i++) { char c; - if(either_copyin(&c, user_src, src+i, 1) == -1) + if(either_copyin(&c, user_src, src + i, 1) == -1) break; uartputc(c); } @@ -79,17 +81,17 @@ consolewrite(int user_src, u64 src, int n) int consoleread(int user_dst, u64 dst, int n) { - u32 target; - int c; + u32 target; + int c; char cbuf; target = n; acquire(&cons.lock); - while(n > 0){ + while(n > 0) { // wait until interrupt handler has put some // input into cons.buffer. - while(cons.r == cons.w){ - if(killed(myproc())){ + while(cons.r == cons.w) { + if(killed(myproc())) { release(&cons.lock); return -1; } @@ -98,8 +100,8 @@ consoleread(int user_dst, u64 dst, int n) c = cons.buf[cons.r++ % INPUT_BUF_SIZE]; - if(c == C('D')){ // end-of-file - if(n < target){ + if(c == C('D')) { // end-of-file + if(n < target) { // Save ^D for next time, to make sure // caller gets a 0-byte result. cons.r--; @@ -115,7 +117,7 @@ consoleread(int user_dst, u64 dst, int n) dst++; --n; - if(c == '\n'){ + if(c == '\n') { // a whole line has arrived, return to // the user-level read(). break; @@ -137,26 +139,25 @@ consoleintr(int c) { acquire(&cons.lock); - switch(c){ - case C('P'): // Print process list. + switch(c) { + case C('P'): // Print process list. procdump(); break; - case C('U'): // Kill line. - while(cons.e != cons.w && - cons.buf[(cons.e-1) % INPUT_BUF_SIZE] != '\n'){ + case C('U'): // Kill line. + while(cons.e != cons.w && cons.buf[(cons.e - 1) % INPUT_BUF_SIZE] != '\n') { cons.e--; consputc(BACKSPACE); } break; case C('H'): // Backspace case '\x7f': // Delete key - if(cons.e != cons.w){ + if(cons.e != cons.w) { cons.e--; consputc(BACKSPACE); } break; default: - if(c != 0 && cons.e-cons.r < INPUT_BUF_SIZE){ + if(c != 0 && cons.e - cons.r < INPUT_BUF_SIZE) { c = (c == '\r') ? '\n' : c; // echo back to the user. @@ -165,7 +166,7 @@ consoleintr(int c) // store for consumption by consoleread(). cons.buf[cons.e++ % INPUT_BUF_SIZE] = c; - if(c == '\n' || c == C('D') || cons.e-cons.r == INPUT_BUF_SIZE){ + if(c == '\n' || c == C('D') || cons.e - cons.r == INPUT_BUF_SIZE) { // wake up consoleread() if a whole line (or end-of-file) // has arrived. cons.w = cons.e; @@ -174,7 +175,7 @@ consoleintr(int c) } break; } - + release(&cons.lock); } diff --git a/kernel/defs.h b/kernel/defs.h index d6c47c0..b7e0db8 100644 --- a/kernel/defs.h +++ b/kernel/defs.h @@ -10,180 +10,180 @@ struct stat; struct superblock; // bio.c -void binit(void); -struct buf* bread(u32, u32); -void brelse(struct buf*); -void bwrite(struct buf*); -void bpin(struct buf*); -void bunpin(struct buf*); +void binit(void); +struct buf *bread(u32, u32); +void brelse(struct buf *); +void bwrite(struct buf *); +void bpin(struct buf *); +void bunpin(struct buf *); // console.c -void consoleinit(void); -void consoleintr(int); -void consputc(int); +void consoleinit(void); +void consoleintr(int); +void consputc(int); // exec.c -int exec(char*, char**); +int exec(char *, char **); // file.c -struct file* filealloc(void); -void fileclose(struct file*); -struct file* filedup(struct file*); -void fileinit(void); -int fileread(struct file*, u64, int n); -int filestat(struct file*, u64 addr); -int filewrite(struct file*, u64, int n); +struct file *filealloc(void); +void fileclose(struct file *); +struct file *filedup(struct file *); +void fileinit(void); +int fileread(struct file *, u64, int n); +int filestat(struct file *, u64 addr); +int filewrite(struct file *, u64, int n); // fs.c -void fsinit(int); -int dirlink(struct inode*, char*, u32); -struct inode* dirlookup(struct inode*, char*, u32*); -struct inode* ialloc(u32, short); -struct inode* idup(struct inode*); -void iinit(); -void ilock(struct inode*); -void iput(struct inode*); -void iunlock(struct inode*); -void iunlockput(struct inode*); -void iupdate(struct inode*); -int namecmp(const char*, const char*); -struct inode* namei(char*); -struct inode* nameiparent(char*, char*); -int readi(struct inode*, int, u64, u32, u32); -void stati(struct inode*, struct stat*); -int writei(struct inode*, int, u64, u32, u32); -void itrunc(struct inode*); +void fsinit(int); +int dirlink(struct inode *, char *, u32); +struct inode *dirlookup(struct inode *, char *, u32 *); +struct inode *ialloc(u32, short); +struct inode *idup(struct inode *); +void iinit(); +void ilock(struct inode *); +void iput(struct inode *); +void iunlock(struct inode *); +void iunlockput(struct inode *); +void iupdate(struct inode *); +int namecmp(const char *, const char *); +struct inode *namei(char *); +struct inode *nameiparent(char *, char *); +int readi(struct inode *, int, u64, u32, u32); +void stati(struct inode *, struct stat *); +int writei(struct inode *, int, u64, u32, u32); +void itrunc(struct inode *); // ramdisk.c -void ramdiskinit(void); -void ramdiskintr(void); -void ramdiskrw(struct buf*); +void ramdiskinit(void); +void ramdiskintr(void); +void ramdiskrw(struct buf *); // kalloc.c -void* kalloc(void); -void kfree(void *); -void kinit(void); +void *kalloc(void); +void kfree(void *); +void kinit(void); // log.c -void initlog(int, struct superblock*); -void log_write(struct buf*); -void begin_op(void); -void end_op(void); +void initlog(int, struct superblock *); +void log_write(struct buf *); +void begin_op(void); +void end_op(void); // pipe.c -int pipealloc(struct file**, struct file**); -void pipeclose(struct pipe*, int); -int piperead(struct pipe*, u64, int); -int pipewrite(struct pipe*, u64, int); +int pipealloc(struct file **, struct file **); +void pipeclose(struct pipe *, int); +int piperead(struct pipe *, u64, int); +int pipewrite(struct pipe *, u64, int); // printf.c -void printf(char*, ...); -void panic(char*) __attribute__((noreturn)); -void printfinit(void); +void printf(char *, ...); +void panic(char *) __attribute__((noreturn)); +void printfinit(void); // proc.c -int cpuid(void); -void exit(int); -int fork(void); -int growproc(int); -void proc_mapstacks(pagetable_t); -pagetable_t proc_pagetable(struct proc *); -void proc_freepagetable(pagetable_t, u64); -int kill(int); -int killed(struct proc*); -void setkilled(struct proc*); -struct cpu* mycpu(void); -struct cpu* getmycpu(void); -struct proc* myproc(); -void procinit(void); -void scheduler(void) __attribute__((noreturn)); -void sched(void); -void sleep(void*, struct spinlock*); -void userinit(void); -int wait(u64); -void wakeup(void*); -void yield(void); -int either_copyout(int user_dst, u64 dst, void *src, u64 len); -int either_copyin(void *dst, int user_src, u64 src, u64 len); -void procdump(void); +int cpuid(void); +void exit(int); +int fork(void); +int growproc(int); +void proc_mapstacks(pagetable_t); +pagetable_t proc_pagetable(struct proc *); +void proc_freepagetable(pagetable_t, u64); +int kill(int); +int killed(struct proc *); +void setkilled(struct proc *); +struct cpu *mycpu(void); +struct cpu *getmycpu(void); +struct proc *myproc(); +void procinit(void); +void scheduler(void) __attribute__((noreturn)); +void sched(void); +void sleep(void *, struct spinlock *); +void userinit(void); +int wait(u64); +void wakeup(void *); +void yield(void); +int either_copyout(int user_dst, u64 dst, void *src, u64 len); +int either_copyin(void *dst, int user_src, u64 src, u64 len); +void procdump(void); // swtch.S -void swtch(struct context*, struct context*); +void swtch(struct context *, struct context *); // spinlock.c -void acquire(struct spinlock*); -int holding(struct spinlock*); -void initlock(struct spinlock*, char*); -void release(struct spinlock*); -void push_off(void); -void pop_off(void); +void acquire(struct spinlock *); +int holding(struct spinlock *); +void initlock(struct spinlock *, char *); +void release(struct spinlock *); +void push_off(void); +void pop_off(void); // sleeplock.c -void acquiresleep(struct sleeplock*); -void releasesleep(struct sleeplock*); -int holdingsleep(struct sleeplock*); -void initsleeplock(struct sleeplock*, char*); +void acquiresleep(struct sleeplock *); +void releasesleep(struct sleeplock *); +int holdingsleep(struct sleeplock *); +void initsleeplock(struct sleeplock *, char *); // string.c -int memcmp(const void*, const void*, u32); -void* memmove(void*, const void*, u32); -void* memset(void*, int, u32); -char* safestrcpy(char*, const char*, int); -int strlen(const char*); -int strncmp(const char*, const char*, u32); -char* strncpy(char*, const char*, int); +int memcmp(const void *, const void *, u32); +void *memmove(void *, const void *, u32); +void *memset(void *, int, u32); +char *safestrcpy(char *, const char *, int); +int strlen(const char *); +int strncmp(const char *, const char *, u32); +char *strncpy(char *, const char *, int); // syscall.c -void argint(int, int*); -int argstr(int, char*, int); -void argaddr(int, u64 *); -int fetchstr(u64, char*, int); -int fetchaddr(u64, u64*); -void syscall(); +void argint(int, int *); +int argstr(int, char *, int); +void argaddr(int, u64 *); +int fetchstr(u64, char *, int); +int fetchaddr(u64, u64 *); +void syscall(); // trap.c -extern u32 ticks; -void trapinit(void); -void trapinithart(void); +extern u32 ticks; +void trapinit(void); +void trapinithart(void); extern struct spinlock tickslock; -void usertrapret(void); +void usertrapret(void); // uart.c -void uartinit(void); -void uartintr(void); -void uartputc(int); -void uartputc_sync(int); -int uartgetc(void); +void uartinit(void); +void uartintr(void); +void uartputc(int); +void uartputc_sync(int); +int uartgetc(void); // vm.c -void kvminit(void); -void kvminithart(void); -void kvmmap(pagetable_t, u64, u64, u64, int); -int mappages(pagetable_t, u64, u64, u64, int); -pagetable_t uvmcreate(void); -void uvmfirst(pagetable_t, u8 *, u32); -u64 uvmalloc(pagetable_t, u64, u64, int); -u64 uvmdealloc(pagetable_t, u64, u64); -int uvmcopy(pagetable_t, pagetable_t, u64); -void uvmfree(pagetable_t, u64); -void uvmunmap(pagetable_t, u64, u64, int); -void uvmclear(pagetable_t, u64); -pte_t * walk(pagetable_t, u64, int); -u64 walkaddr(pagetable_t, u64); -int copyout(pagetable_t, u64, char *, u64); -int copyin(pagetable_t, char *, u64, u64); -int copyinstr(pagetable_t, char *, u64, u64); +void kvminit(void); +void kvminithart(void); +void kvmmap(pagetable_t, u64, u64, u64, int); +int mappages(pagetable_t, u64, u64, u64, int); +pagetable_t uvmcreate(void); +void uvmfirst(pagetable_t, u8 *, u32); +u64 uvmalloc(pagetable_t, u64, u64, int); +u64 uvmdealloc(pagetable_t, u64, u64); +int uvmcopy(pagetable_t, pagetable_t, u64); +void uvmfree(pagetable_t, u64); +void uvmunmap(pagetable_t, u64, u64, int); +void uvmclear(pagetable_t, u64); +pte_t *walk(pagetable_t, u64, int); +u64 walkaddr(pagetable_t, u64); +int copyout(pagetable_t, u64, char *, u64); +int copyin(pagetable_t, char *, u64, u64); +int copyinstr(pagetable_t, char *, u64, u64); // plic.c -void plicinit(void); -void plicinithart(void); -int plic_claim(void); -void plic_complete(int); +void plicinit(void); +void plicinithart(void); +int plic_claim(void); +void plic_complete(int); // virtio_disk.c -void virtio_disk_init(void); -void virtio_disk_rw(struct buf *, int); -void virtio_disk_intr(void); +void virtio_disk_init(void); +void virtio_disk_rw(struct buf *, int); +void virtio_disk_intr(void); // number of elements in fixed-size array -#define NELEM(x) (sizeof(x)/sizeof((x)[0])) +#define NELEM(x) (sizeof(x) / sizeof((x)[0])) diff --git a/kernel/elf.h b/kernel/elf.h index a67d9f4..a779868 100644 --- a/kernel/elf.h +++ b/kernel/elf.h @@ -1,11 +1,11 @@ // Format of an ELF executable file -#define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian +#define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian // File header struct elfhdr { - u32 magic; // must equal ELF_MAGIC - u8 elf[12]; + u32 magic; // must equal ELF_MAGIC + u8 elf[12]; u16 type; u16 machine; u32 version; @@ -34,9 +34,9 @@ struct proghdr { }; // Values for Proghdr type -#define ELF_PROG_LOAD 1 +#define ELF_PROG_LOAD 1 // Flag bits for Proghdr flags -#define ELF_PROG_FLAG_EXEC 1 -#define ELF_PROG_FLAG_WRITE 2 -#define ELF_PROG_FLAG_READ 4 +#define ELF_PROG_FLAG_EXEC 1 +#define ELF_PROG_FLAG_WRITE 2 +#define ELF_PROG_FLAG_READ 4 diff --git a/kernel/exec.c b/kernel/exec.c index dff0a22..4f424f2 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -9,31 +9,32 @@ static int loadseg(pde_t *, u64, struct inode *, u32, u32); -int flags2perm(int flags) +int +flags2perm(int flags) { - int perm = 0; - if(flags & 0x1) - perm = PTE_X; - if(flags & 0x2) - perm |= PTE_W; - return perm; + int perm = 0; + if(flags & 0x1) + perm = PTE_X; + if(flags & 0x2) + perm |= PTE_W; + return perm; } int exec(char *path, char **argv) { - char *s, *last; - int i, off; - u64 argc, sz = 0, sp, ustack[MAXARG], stackbase; - struct elfhdr elf; - struct inode *ip; + char *s, *last; + int i, off; + u64 argc, sz = 0, sp, ustack[MAXARG], stackbase; + struct elfhdr elf; + struct inode *ip; struct proghdr ph; - pagetable_t pagetable = 0, oldpagetable; - struct proc *p = myproc(); + pagetable_t pagetable = 0, oldpagetable; + struct proc *p = myproc(); begin_op(); - if((ip = namei(path)) == 0){ + if((ip = namei(path)) == 0) { end_op(); return -1; } @@ -50,7 +51,7 @@ exec(char *path, char **argv) goto bad; // Load program into memory. - for(i=0, off=elf.phoff; itrapframe->a1 = sp; // Save program name for debugging. - for(last=s=path; *s; s++) + for(last = s = path; *s; s++) if(*s == '/') - last = s+1; + last = s + 1; safestrcpy(p->name, last, sizeof(p->name)); - + // Commit to the user image. oldpagetable = p->pagetable; p->pagetable = pagetable; p->sz = sz; - p->trapframe->epc = elf.entry; // initial program counter = main - p->trapframe->sp = sp; // initial stack pointer + p->trapframe->epc = elf.entry; // initial program counter = main + p->trapframe->sp = sp; // initial stack pointer proc_freepagetable(oldpagetable, oldsz); return argc; // this ends up in a0, the first argument to main(argc, argv) - bad: +bad: if(pagetable) proc_freepagetable(pagetable, sz); - if(ip){ + if(ip) { iunlockput(ip); end_op(); } @@ -150,7 +151,7 @@ loadseg(pagetable_t pagetable, u64 va, struct inode *ip, u32 offset, u32 sz) u32 i, n; u64 pa; - for(i = 0; i < sz; i += PGSIZE){ + for(i = 0; i < sz; i += PGSIZE) { pa = walkaddr(pagetable, va + i); if(pa == 0) panic("loadseg: address should exist"); @@ -158,9 +159,9 @@ loadseg(pagetable_t pagetable, u64 va, struct inode *ip, u32 offset, u32 sz) n = sz - i; else n = PGSIZE; - if(readi(ip, 0, (u64)pa, offset+i, n) != n) + if(readi(ip, 0, (u64)pa, offset + i, n) != n) return -1; } - + return 0; } diff --git a/kernel/fcntl.h b/kernel/fcntl.h index 44861b9..1b57ebb 100644 --- a/kernel/fcntl.h +++ b/kernel/fcntl.h @@ -1,5 +1,5 @@ -#define O_RDONLY 0x000 -#define O_WRONLY 0x001 -#define O_RDWR 0x002 -#define O_CREATE 0x200 -#define O_TRUNC 0x400 +#define O_RDONLY 0x000 +#define O_WRONLY 0x001 +#define O_RDWR 0x002 +#define O_CREATE 0x200 +#define O_TRUNC 0x400 diff --git a/kernel/file.c b/kernel/file.c index 4aceeaf..72cca61 100644 --- a/kernel/file.c +++ b/kernel/file.c @@ -16,7 +16,7 @@ struct devsw devsw[NDEV]; struct { struct spinlock lock; - struct file file[NFILE]; + struct file file[NFILE]; } ftable; void @@ -26,14 +26,14 @@ fileinit(void) } // Allocate a file structure. -struct file* +struct file * filealloc(void) { struct file *f; acquire(&ftable.lock); - for(f = ftable.file; f < ftable.file + NFILE; f++){ - if(f->ref == 0){ + for(f = ftable.file; f < ftable.file + NFILE; f++) { + if(f->ref == 0) { f->ref = 1; release(&ftable.lock); return f; @@ -44,7 +44,7 @@ filealloc(void) } // Increment ref count for file f. -struct file* +struct file * filedup(struct file *f) { acquire(&ftable.lock); @@ -64,7 +64,7 @@ fileclose(struct file *f) acquire(&ftable.lock); if(f->ref < 1) panic("fileclose"); - if(--f->ref > 0){ + if(--f->ref > 0) { release(&ftable.lock); return; } @@ -73,9 +73,9 @@ fileclose(struct file *f) f->type = FD_NONE; release(&ftable.lock); - if(ff.type == FD_PIPE){ + if(ff.type == FD_PIPE) { pipeclose(ff.pipe, ff.writable); - } else if(ff.type == FD_INODE || ff.type == FD_DEVICE){ + } else if(ff.type == FD_INODE || ff.type == FD_DEVICE) { begin_op(); iput(ff.ip); end_op(); @@ -88,9 +88,9 @@ int filestat(struct file *f, u64 addr) { struct proc *p = myproc(); - struct stat st; - - if(f->type == FD_INODE || f->type == FD_DEVICE){ + struct stat st; + + if(f->type == FD_INODE || f->type == FD_DEVICE) { ilock(f->ip); stati(f->ip, &st); iunlock(f->ip); @@ -111,13 +111,13 @@ fileread(struct file *f, u64 addr, int n) if(f->readable == 0) return -1; - if(f->type == FD_PIPE){ + if(f->type == FD_PIPE) { r = piperead(f->pipe, addr, n); - } else if(f->type == FD_DEVICE){ + } else if(f->type == FD_DEVICE) { if(f->major < 0 || f->major >= NDEV || !devsw[f->major].read) return -1; r = devsw[f->major].read(1, addr, n); - } else if(f->type == FD_INODE){ + } else if(f->type == FD_INODE) { ilock(f->ip); if((r = readi(f->ip, 1, addr, f->off, n)) > 0) f->off += r; @@ -139,34 +139,34 @@ filewrite(struct file *f, u64 addr, int n) if(f->writable == 0) return -1; - if(f->type == FD_PIPE){ + if(f->type == FD_PIPE) { ret = pipewrite(f->pipe, addr, n); - } else if(f->type == FD_DEVICE){ + } else if(f->type == FD_DEVICE) { if(f->major < 0 || f->major >= NDEV || !devsw[f->major].write) return -1; ret = devsw[f->major].write(1, addr, n); - } else if(f->type == FD_INODE){ + } else if(f->type == FD_INODE) { // write a few blocks at a time to avoid exceeding // the maximum log transaction size, including // i-node, indirect block, allocation blocks, // and 2 blocks of slop for non-aligned writes. // this really belongs lower down, since writei() // might be writing a device like the console. - int max = ((MAXOPBLOCKS-1-1-2) / 2) * BSIZE; + int max = ((MAXOPBLOCKS - 1 - 1 - 2) / 2) * BSIZE; int i = 0; - while(i < n){ + while(i < n) { int n1 = n - i; if(n1 > max) n1 = max; begin_op(); ilock(f->ip); - if ((r = writei(f->ip, 1, addr + i, f->off, n1)) > 0) + if((r = writei(f->ip, 1, addr + i, f->off, n1)) > 0) f->off += r; iunlock(f->ip); end_op(); - if(r != n1){ + if(r != n1) { // error from writei break; } @@ -179,4 +179,3 @@ filewrite(struct file *f, u64 addr, int n) return ret; } - diff --git a/kernel/file.h b/kernel/file.h index 5858326..ddb6857 100644 --- a/kernel/file.h +++ b/kernel/file.h @@ -1,32 +1,32 @@ struct file { enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type; - int ref; // reference count - char readable; - char writable; - struct pipe *pipe; // FD_PIPE - struct inode *ip; // FD_INODE and FD_DEVICE - u32 off; // FD_INODE - short major; // FD_DEVICE + int ref; // reference count + char readable; + char writable; + struct pipe *pipe; // FD_PIPE + struct inode *ip; // FD_INODE and FD_DEVICE + u32 off; // FD_INODE + short major; // FD_DEVICE }; #define major(dev) ((dev) >> 16 & 0xFFFF) #define minor(dev) ((dev) & 0xFFFF) -#define mkdev(m,n) ((u32)((m)<<16| (n))) +#define mkdev(m, n) ((u32)((m) << 16 | (n))) // in-memory copy of an inode struct inode { - u32 dev; // Device number - u32 inum; // Inode number - int ref; // Reference count - struct sleeplock lock; // protects everything below here - int valid; // inode has been read from disk? + u32 dev; // Device number + u32 inum; // Inode number + int ref; // Reference count + struct sleeplock lock; // protects everything below here + int valid; // inode has been read from disk? - short type; // copy of disk inode + short type; // copy of disk inode short major; short minor; short nlink; - u32 size; - u32 addrs[NDIRECT+1]; + u32 size; + u32 addrs[NDIRECT + 1]; }; // map major device number to device functions. diff --git a/kernel/fs.c b/kernel/fs.c index 0b5e52f..42b6e05 100644 --- a/kernel/fs.c +++ b/kernel/fs.c @@ -24,7 +24,7 @@ #define min(a, b) ((a) < (b) ? (a) : (b)) // there should be one superblock per disk device, but we run with // only one device -struct superblock sb; +struct superblock sb; // Read the super block. static void @@ -39,7 +39,8 @@ readsb(int dev, struct superblock *sb) // Init fs void -fsinit(int dev) { +fsinit(int dev) +{ readsb(dev, &sb); if(sb.magic != FSMAGIC) panic("invalid file system"); @@ -65,16 +66,16 @@ bzero(int dev, int bno) static u32 balloc(u32 dev) { - int b, bi, m; + int b, bi, m; struct buf *bp; bp = 0; - for(b = 0; b < sb.size; b += BPB){ + for(b = 0; b < sb.size; b += BPB) { bp = bread(dev, BBLOCK(b, sb)); - for(bi = 0; bi < BPB && b + bi < sb.size; bi++){ + for(bi = 0; bi < BPB && b + bi < sb.size; bi++) { m = 1 << (bi % 8); - if((bp->data[bi/8] & m) == 0){ // Is block free? - bp->data[bi/8] |= m; // Mark block in use. + if((bp->data[bi / 8] & m) == 0) { // Is block free? + bp->data[bi / 8] |= m; // Mark block in use. log_write(bp); brelse(bp); bzero(dev, b + bi); @@ -92,14 +93,14 @@ static void bfree(int dev, u32 b) { struct buf *bp; - int bi, m; + int bi, m; bp = bread(dev, BBLOCK(b, sb)); bi = b % BPB; m = 1 << (bi % 8); - if((bp->data[bi/8] & m) == 0) + if((bp->data[bi / 8] & m) == 0) panic("freeing free block"); - bp->data[bi/8] &= ~m; + bp->data[bi / 8] &= ~m; log_write(bp); brelse(bp); } @@ -175,40 +176,40 @@ bfree(int dev, u32 b) struct { struct spinlock lock; - struct inode inode[NINODE]; + struct inode inode[NINODE]; } itable; void iinit() { int i = 0; - + initlock(&itable.lock, "itable"); for(i = 0; i < NINODE; i++) { initsleeplock(&itable.inode[i].lock, "inode"); } } -static struct inode* iget(u32 dev, u32 inum); +static struct inode *iget(u32 dev, u32 inum); // Allocate an inode on device dev. // Mark it as allocated by giving it type type. // Returns an unlocked but allocated and referenced inode, // or NULL if there is no free inode. -struct inode* +struct inode * ialloc(u32 dev, short type) { - int inum; - struct buf *bp; + int inum; + struct buf *bp; struct dinode *dip; - for(inum = 1; inum < sb.ninodes; inum++){ + for(inum = 1; inum < sb.ninodes; inum++) { bp = bread(dev, IBLOCK(inum, sb)); - dip = (struct dinode*)bp->data + inum%IPB; - if(dip->type == 0){ // a free inode + dip = (struct dinode *)bp->data + inum % IPB; + if(dip->type == 0) { // a free inode memset(dip, 0, sizeof(*dip)); dip->type = type; - log_write(bp); // mark it allocated on the disk + log_write(bp); // mark it allocated on the disk brelse(bp); return iget(dev, inum); } @@ -225,11 +226,11 @@ ialloc(u32 dev, short type) void iupdate(struct inode *ip) { - struct buf *bp; + struct buf *bp; struct dinode *dip; bp = bread(ip->dev, IBLOCK(ip->inum, sb)); - dip = (struct dinode*)bp->data + ip->inum%IPB; + dip = (struct dinode *)bp->data + ip->inum % IPB; dip->type = ip->type; dip->major = ip->major; dip->minor = ip->minor; @@ -243,7 +244,7 @@ iupdate(struct inode *ip) // Find the inode with number inum on device dev // and return the in-memory copy. Does not lock // the inode and does not read it from disk. -static struct inode* +static struct inode * iget(u32 dev, u32 inum) { struct inode *ip, *empty; @@ -252,13 +253,13 @@ iget(u32 dev, u32 inum) // Is the inode already in the table? empty = 0; - for(ip = &itable.inode[0]; ip < &itable.inode[NINODE]; ip++){ - if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){ + for(ip = &itable.inode[0]; ip < &itable.inode[NINODE]; ip++) { + if(ip->ref > 0 && ip->dev == dev && ip->inum == inum) { ip->ref++; release(&itable.lock); return ip; } - if(empty == 0 && ip->ref == 0) // Remember empty slot. + if(empty == 0 && ip->ref == 0) // Remember empty slot. empty = ip; } @@ -278,7 +279,7 @@ iget(u32 dev, u32 inum) // Increment reference count for ip. // Returns ip to enable ip = idup(ip1) idiom. -struct inode* +struct inode * idup(struct inode *ip) { acquire(&itable.lock); @@ -292,7 +293,7 @@ idup(struct inode *ip) void ilock(struct inode *ip) { - struct buf *bp; + struct buf *bp; struct dinode *dip; if(ip == 0 || ip->ref < 1) @@ -300,9 +301,9 @@ ilock(struct inode *ip) acquiresleep(&ip->lock); - if(ip->valid == 0){ + if(ip->valid == 0) { bp = bread(ip->dev, IBLOCK(ip->inum, sb)); - dip = (struct dinode*)bp->data + ip->inum%IPB; + dip = (struct dinode *)bp->data + ip->inum % IPB; ip->type = dip->type; ip->major = dip->major; ip->minor = dip->minor; @@ -338,7 +339,7 @@ iput(struct inode *ip) { acquire(&itable.lock); - if(ip->ref == 1 && ip->valid && ip->nlink == 0){ + if(ip->ref == 1 && ip->valid && ip->nlink == 0) { // inode has no links and no other references: truncate and free. // ip->ref == 1 means no other process can have ip locked, @@ -382,11 +383,11 @@ iunlockput(struct inode *ip) static u32 bmap(struct inode *ip, u32 bn) { - u32 addr, *a; + u32 addr, *a; struct buf *bp; - if(bn < NDIRECT){ - if((addr = ip->addrs[bn]) == 0){ + if(bn < NDIRECT) { + if((addr = ip->addrs[bn]) == 0) { addr = balloc(ip->dev); if(addr == 0) return 0; @@ -396,19 +397,19 @@ bmap(struct inode *ip, u32 bn) } bn -= NDIRECT; - if(bn < NINDIRECT){ + if(bn < NINDIRECT) { // Load indirect block, allocating if necessary. - if((addr = ip->addrs[NDIRECT]) == 0){ + if((addr = ip->addrs[NDIRECT]) == 0) { addr = balloc(ip->dev); if(addr == 0) return 0; ip->addrs[NDIRECT] = addr; } bp = bread(ip->dev, addr); - a = (u32*)bp->data; - if((addr = a[bn]) == 0){ + a = (u32 *)bp->data; + if((addr = a[bn]) == 0) { addr = balloc(ip->dev); - if(addr){ + if(addr) { a[bn] = addr; log_write(bp); } @@ -425,21 +426,21 @@ bmap(struct inode *ip, u32 bn) void itrunc(struct inode *ip) { - int i, j; + int i, j; struct buf *bp; - u32 *a; + u32 *a; - for(i = 0; i < NDIRECT; i++){ - if(ip->addrs[i]){ + for(i = 0; i < NDIRECT; i++) { + if(ip->addrs[i]) { bfree(ip->dev, ip->addrs[i]); ip->addrs[i] = 0; } } - if(ip->addrs[NDIRECT]){ + if(ip->addrs[NDIRECT]) { bp = bread(ip->dev, ip->addrs[NDIRECT]); - a = (u32*)bp->data; - for(j = 0; j < NINDIRECT; j++){ + a = (u32 *)bp->data; + for(j = 0; j < NINDIRECT; j++) { if(a[j]) bfree(ip->dev, a[j]); } @@ -471,7 +472,7 @@ stati(struct inode *ip, struct stat *st) int readi(struct inode *ip, int user_dst, u64 dst, u32 off, u32 n) { - u32 tot, m; + u32 tot, m; struct buf *bp; if(off > ip->size || off + n < off) @@ -479,12 +480,12 @@ readi(struct inode *ip, int user_dst, u64 dst, u32 off, u32 n) if(off + n > ip->size) n = ip->size - off; - for(tot=0; totdev, addr); - m = min(n - tot, BSIZE - off%BSIZE); + m = min(n - tot, BSIZE - off % BSIZE); if(either_copyout(user_dst, dst, bp->data + (off % BSIZE), m) == -1) { brelse(bp); tot = -1; @@ -505,20 +506,20 @@ readi(struct inode *ip, int user_dst, u64 dst, u32 off, u32 n) int writei(struct inode *ip, int user_src, u64 src, u32 off, u32 n) { - u32 tot, m; + u32 tot, m; struct buf *bp; if(off > ip->size || off + n < off) return -1; - if(off + n > MAXFILE*BSIZE) + if(off + n > MAXFILE * BSIZE) return -1; - for(tot=0; totdev, addr); - m = min(n - tot, BSIZE - off%BSIZE); + m = min(n - tot, BSIZE - off % BSIZE); if(either_copyin(bp->data + (off % BSIZE), user_src, src, m) == -1) { brelse(bp); break; @@ -548,21 +549,21 @@ namecmp(const char *s, const char *t) // Look for a directory entry in a directory. // If found, set *poff to byte offset of entry. -struct inode* +struct inode * dirlookup(struct inode *dp, char *name, u32 *poff) { - u32 off, inum; + u32 off, inum; struct dirent de; if(dp->type != T_DIR) panic("dirlookup not DIR"); - for(off = 0; off < dp->size; off += sizeof(de)){ + for(off = 0; off < dp->size; off += sizeof(de)) { if(readi(dp, 0, (u64)&de, off, sizeof(de)) != sizeof(de)) panic("dirlookup read"); if(de.inum == 0) continue; - if(namecmp(name, de.name) == 0){ + if(namecmp(name, de.name) == 0) { // entry matches path element if(poff) *poff = off; @@ -579,18 +580,18 @@ dirlookup(struct inode *dp, char *name, u32 *poff) int dirlink(struct inode *dp, char *name, u32 inum) { - int off; + int off; struct dirent de; struct inode *ip; // Check that name is not present. - if((ip = dirlookup(dp, name, 0)) != 0){ + if((ip = dirlookup(dp, name, 0)) != 0) { iput(ip); return -1; } // Look for an empty dirent. - for(off = 0; off < dp->size; off += sizeof(de)){ + for(off = 0; off < dp->size; off += sizeof(de)) { if(readi(dp, 0, (u64)&de, off, sizeof(de)) != sizeof(de)) panic("dirlink read"); if(de.inum == 0) @@ -619,11 +620,11 @@ dirlink(struct inode *dp, char *name, u32 inum) // skipelem("a", name) = "", setting name = "a" // skipelem("", name) = skipelem("////", name) = 0 // -static char* +static char * skipelem(char *path, char *name) { char *s; - int len; + int len; while(*path == '/') path++; @@ -648,7 +649,7 @@ skipelem(char *path, char *name) // If parent != 0, return the inode for the parent and copy the final // path element into name, which must have room for DIRSIZ bytes. // Must be called inside a transaction since it calls iput(). -static struct inode* +static struct inode * namex(char *path, int nameiparent, char *name) { struct inode *ip, *next; @@ -658,39 +659,39 @@ namex(char *path, int nameiparent, char *name) else ip = idup(myproc()->cwd); - while((path = skipelem(path, name)) != 0){ + while((path = skipelem(path, name)) != 0) { ilock(ip); - if(ip->type != T_DIR){ + if(ip->type != T_DIR) { iunlockput(ip); return 0; } - if(nameiparent && *path == '\0'){ + if(nameiparent && *path == '\0') { // Stop one level early. iunlock(ip); return ip; } - if((next = dirlookup(ip, name, 0)) == 0){ + if((next = dirlookup(ip, name, 0)) == 0) { iunlockput(ip); return 0; } iunlockput(ip); ip = next; } - if(nameiparent){ + if(nameiparent) { iput(ip); return 0; } return ip; } -struct inode* +struct inode * namei(char *path) { char name[DIRSIZ]; return namex(path, 0, name); } -struct inode* +struct inode * nameiparent(char *path, char *name) { return namex(path, 1, name); diff --git a/kernel/fs.h b/kernel/fs.h index 2a9a16f..2a6d7bd 100644 --- a/kernel/fs.h +++ b/kernel/fs.h @@ -1,9 +1,8 @@ // On-disk file system format. // Both the kernel and user programs use this header file. - -#define ROOTINO 1 // root i-number -#define BSIZE 1024 // block size +#define ROOTINO 1 // root i-number +#define BSIZE 1024 // block size // Disk layout: // [ boot block | super block | log | inode blocks | @@ -12,49 +11,48 @@ // mkfs computes the super block and builds an initial file system. The // super block describes the disk layout: struct superblock { - u32 magic; // Must be FSMAGIC - u32 size; // Size of file system image (blocks) - u32 nblocks; // Number of data blocks - u32 ninodes; // Number of inodes. - u32 nlog; // Number of log blocks - u32 logstart; // Block number of first log block - u32 inodestart; // Block number of first inode block - u32 bmapstart; // Block number of first free map block + u32 magic; // Must be FSMAGIC + u32 size; // Size of file system image (blocks) + u32 nblocks; // Number of data blocks + u32 ninodes; // Number of inodes. + u32 nlog; // Number of log blocks + u32 logstart; // Block number of first log block + u32 inodestart; // Block number of first inode block + u32 bmapstart; // Block number of first free map block }; #define FSMAGIC 0x10203040 -#define NDIRECT 12 +#define NDIRECT 12 #define NINDIRECT (BSIZE / sizeof(u32)) -#define MAXFILE (NDIRECT + NINDIRECT) +#define MAXFILE (NDIRECT + NINDIRECT) // On-disk inode structure struct dinode { - short type; // File type - short major; // Major device number (T_DEVICE only) - short minor; // Minor device number (T_DEVICE only) - short nlink; // Number of links to inode in file system - u32 size; // Size of file (bytes) - u32 addrs[NDIRECT+1]; // Data block addresses + short type; // File type + short major; // Major device number (T_DEVICE only) + short minor; // Minor device number (T_DEVICE only) + short nlink; // Number of links to inode in file system + u32 size; // Size of file (bytes) + u32 addrs[NDIRECT + 1]; // Data block addresses }; // Inodes per block. -#define IPB (BSIZE / sizeof(struct dinode)) +#define IPB (BSIZE / sizeof(struct dinode)) // Block containing inode i -#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart) +#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart) // Bitmap bits per block -#define BPB (BSIZE*8) +#define BPB (BSIZE * 8) // Block of free map containing bit for block b -#define BBLOCK(b, sb) ((b)/BPB + sb.bmapstart) +#define BBLOCK(b, sb) ((b) / BPB + sb.bmapstart) // Directory is a file containing a sequence of dirent structures. #define DIRSIZ 14 struct dirent { - u16 inum; + u16 inum; char name[DIRSIZ]; }; - diff --git a/kernel/kalloc.c b/kernel/kalloc.c index 1aa3250..a3de3d5 100644 --- a/kernel/kalloc.c +++ b/kernel/kalloc.c @@ -20,22 +20,22 @@ struct run { struct { struct spinlock lock; - struct run *freelist; + struct run *freelist; } kmem; void kinit() { initlock(&kmem.lock, "kmem"); - freerange(end, (void*)PHYSTOP); + freerange(end, (void *)PHYSTOP); } void freerange(void *pa_start, void *pa_end) { char *p; - p = (char*)PGROUNDUP((u64)pa_start); - for(; p + PGSIZE <= (char*)pa_end; p += PGSIZE) + p = (char *)PGROUNDUP((u64)pa_start); + for(; p + PGSIZE <= (char *)pa_end; p += PGSIZE) kfree(p); } @@ -48,13 +48,13 @@ kfree(void *pa) { struct run *r; - if(((u64)pa % PGSIZE) != 0 || (char*)pa < end || (u64)pa >= PHYSTOP) + if(((u64)pa % PGSIZE) != 0 || (char *)pa < end || (u64)pa >= PHYSTOP) panic("kfree"); // Fill with junk to catch dangling refs. memset(pa, 1, PGSIZE); - r = (struct run*)pa; + r = (struct run *)pa; acquire(&kmem.lock); r->next = kmem.freelist; @@ -77,6 +77,6 @@ kalloc(void) release(&kmem.lock); if(r) - memset((char*)r, 5, PGSIZE); // fill with junk - return (void*)r; + memset((char *)r, 5, PGSIZE); // fill with junk + return (void *)r; } diff --git a/kernel/log.c b/kernel/log.c index 5b58306..b0d6601 100644 --- a/kernel/log.c +++ b/kernel/log.c @@ -38,12 +38,12 @@ struct logheader { }; struct log { - struct spinlock lock; - int start; - int size; - int outstanding; // how many FS sys calls are executing. - int committing; // in commit(), please wait. - int dev; + struct spinlock lock; + int start; + int size; + int outstanding; // how many FS sys calls are executing. + int committing; // in commit(), please wait. + int dev; struct logheader lh; }; struct log log; @@ -54,7 +54,7 @@ static void commit(); void initlog(int dev, struct superblock *sb) { - if (sizeof(struct logheader) >= BSIZE) + if(sizeof(struct logheader) >= BSIZE) panic("initlog: too big logheader"); initlock(&log.lock, "log"); @@ -70,11 +70,11 @@ install_trans(int recovering) { int tail; - for (tail = 0; tail < log.lh.n; tail++) { - struct buf *lbuf = bread(log.dev, log.start+tail+1); // read log block - struct buf *dbuf = bread(log.dev, log.lh.block[tail]); // read dst - memmove(dbuf->data, lbuf->data, BSIZE); // copy block to dst - bwrite(dbuf); // write dst to disk + for(tail = 0; tail < log.lh.n; tail++) { + struct buf *lbuf = bread(log.dev, log.start + tail + 1); // read log block + struct buf *dbuf = bread(log.dev, log.lh.block[tail]); // read dst + memmove(dbuf->data, lbuf->data, BSIZE); // copy block to dst + bwrite(dbuf); // write dst to disk if(recovering == 0) bunpin(dbuf); brelse(lbuf); @@ -86,11 +86,11 @@ install_trans(int recovering) static void read_head(void) { - struct buf *buf = bread(log.dev, log.start); - struct logheader *lh = (struct logheader *) (buf->data); - int i; + struct buf *buf = bread(log.dev, log.start); + struct logheader *lh = (struct logheader *)(buf->data); + int i; log.lh.n = lh->n; - for (i = 0; i < log.lh.n; i++) { + for(i = 0; i < log.lh.n; i++) { log.lh.block[i] = lh->block[i]; } brelse(buf); @@ -102,11 +102,11 @@ read_head(void) static void write_head(void) { - struct buf *buf = bread(log.dev, log.start); - struct logheader *hb = (struct logheader *) (buf->data); - int i; + struct buf *buf = bread(log.dev, log.start); + struct logheader *hb = (struct logheader *)(buf->data); + int i; hb->n = log.lh.n; - for (i = 0; i < log.lh.n; i++) { + for(i = 0; i < log.lh.n; i++) { hb->block[i] = log.lh.block[i]; } bwrite(buf); @@ -127,10 +127,10 @@ void begin_op(void) { acquire(&log.lock); - while(1){ - if(log.committing){ + while(1) { + if(log.committing) { sleep(&log, &log.lock); - } else if(log.lh.n + (log.outstanding+1)*MAXOPBLOCKS > LOGSIZE){ + } else if(log.lh.n + (log.outstanding + 1) * MAXOPBLOCKS > LOGSIZE) { // this op might exhaust log space; wait for commit. sleep(&log, &log.lock); } else { @@ -152,7 +152,7 @@ end_op(void) log.outstanding -= 1; if(log.committing) panic("log.committing"); - if(log.outstanding == 0){ + if(log.outstanding == 0) { do_commit = 1; log.committing = 1; } else { @@ -163,7 +163,7 @@ end_op(void) } release(&log.lock); - if(do_commit){ + if(do_commit) { // call commit w/o holding locks, since not allowed // to sleep with locks. commit(); @@ -180,11 +180,11 @@ write_log(void) { int tail; - for (tail = 0; tail < log.lh.n; tail++) { - struct buf *to = bread(log.dev, log.start+tail+1); // log block + for(tail = 0; tail < log.lh.n; tail++) { + struct buf *to = bread(log.dev, log.start + tail + 1); // log block struct buf *from = bread(log.dev, log.lh.block[tail]); // cache block memmove(to->data, from->data, BSIZE); - bwrite(to); // write the log + bwrite(to); // write the log brelse(from); brelse(to); } @@ -193,12 +193,12 @@ write_log(void) static void commit() { - if (log.lh.n > 0) { - write_log(); // Write modified blocks from cache to log - write_head(); // Write header to disk -- the real commit + if(log.lh.n > 0) { + write_log(); // Write modified blocks from cache to log + write_head(); // Write header to disk -- the real commit install_trans(0); // Now install writes to home locations log.lh.n = 0; - write_head(); // Erase the transaction from the log + write_head(); // Erase the transaction from the log } } @@ -217,20 +217,19 @@ log_write(struct buf *b) int i; acquire(&log.lock); - if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1) + if(log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1) panic("too big a transaction"); - if (log.outstanding < 1) + if(log.outstanding < 1) panic("log_write outside of trans"); - for (i = 0; i < log.lh.n; i++) { - if (log.lh.block[i] == b->blockno) // log absorption + for(i = 0; i < log.lh.n; i++) { + if(log.lh.block[i] == b->blockno) // log absorption break; } log.lh.block[i] = b->blockno; - if (i == log.lh.n) { // Add new block to log? + if(i == log.lh.n) { // Add new block to log? bpin(b); log.lh.n++; } release(&log.lock); } - diff --git a/kernel/main.c b/kernel/main.c index f0d3171..2aeca75 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -10,25 +10,25 @@ volatile static int started = 0; void main() { - if(cpuid() == 0){ + if(cpuid() == 0) { consoleinit(); printfinit(); printf("\n"); printf("xv6 kernel is booting\n"); printf("\n"); - kinit(); // physical page allocator - kvminit(); // create kernel page table - kvminithart(); // turn on paging - procinit(); // process table - trapinit(); // trap vectors - trapinithart(); // install kernel trap vector - plicinit(); // set up interrupt controller - plicinithart(); // ask PLIC for device interrupts - binit(); // buffer cache - iinit(); // inode table - fileinit(); // file table + kinit(); // physical page allocator + kvminit(); // create kernel page table + kvminithart(); // turn on paging + procinit(); // process table + trapinit(); // trap vectors + trapinithart(); // install kernel trap vector + plicinit(); // set up interrupt controller + plicinithart(); // ask PLIC for device interrupts + binit(); // buffer cache + iinit(); // inode table + fileinit(); // file table virtio_disk_init(); // emulated hard disk - userinit(); // first user process + userinit(); // first user process __sync_synchronize(); started = 1; } else { @@ -36,10 +36,10 @@ main() ; __sync_synchronize(); printf("hart %d starting\n", cpuid()); - kvminithart(); // turn on paging - trapinithart(); // install kernel trap vector - plicinithart(); // ask PLIC for device interrupts + kvminithart(); // turn on paging + trapinithart(); // install kernel trap vector + plicinithart(); // ask PLIC for device interrupts } - scheduler(); + scheduler(); } diff --git a/kernel/memlayout.h b/kernel/memlayout.h index 776f98c..59ff719 100644 --- a/kernel/memlayout.h +++ b/kernel/memlayout.h @@ -6,8 +6,8 @@ // 00001000 -- boot ROM, provided by qemu // 02000000 -- CLINT // 0C000000 -- PLIC -// 10000000 -- uart0 -// 10001000 -- virtio disk +// 10000000 -- uart0 +// 10001000 -- virtio disk // 80000000 -- boot ROM jumps here in machine mode // -kernel loads the kernel here // unused RAM after 80000000. @@ -18,34 +18,34 @@ // PHYSTOP -- end RAM used by the kernel // qemu puts UART registers here in physical memory. -#define UART0 0x10000000L +#define UART0 0x10000000L #define UART0_IRQ 10 // virtio mmio interface -#define VIRTIO0 0x10001000 +#define VIRTIO0 0x10001000 #define VIRTIO0_IRQ 1 // core local interruptor (CLINT), which contains the timer. -#define CLINT 0x2000000L -#define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8*(hartid)) -#define CLINT_MTIME (CLINT + 0xBFF8) // cycles since boot. +#define CLINT 0x2000000L +#define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8 * (hartid)) +#define CLINT_MTIME (CLINT + 0xBFF8) // cycles since boot. // qemu puts platform-level interrupt controller (PLIC) here. -#define PLIC 0x0c000000L -#define PLIC_PRIORITY (PLIC + 0x0) -#define PLIC_PENDING (PLIC + 0x1000) -#define PLIC_MENABLE(hart) (PLIC + 0x2000 + (hart)*0x100) -#define PLIC_SENABLE(hart) (PLIC + 0x2080 + (hart)*0x100) -#define PLIC_MPRIORITY(hart) (PLIC + 0x200000 + (hart)*0x2000) -#define PLIC_SPRIORITY(hart) (PLIC + 0x201000 + (hart)*0x2000) -#define PLIC_MCLAIM(hart) (PLIC + 0x200004 + (hart)*0x2000) -#define PLIC_SCLAIM(hart) (PLIC + 0x201004 + (hart)*0x2000) +#define PLIC 0x0c000000L +#define PLIC_PRIORITY (PLIC + 0x0) +#define PLIC_PENDING (PLIC + 0x1000) +#define PLIC_MENABLE(hart) (PLIC + 0x2000 + (hart) * 0x100) +#define PLIC_SENABLE(hart) (PLIC + 0x2080 + (hart) * 0x100) +#define PLIC_MPRIORITY(hart) (PLIC + 0x200000 + (hart) * 0x2000) +#define PLIC_SPRIORITY(hart) (PLIC + 0x201000 + (hart) * 0x2000) +#define PLIC_MCLAIM(hart) (PLIC + 0x200004 + (hart) * 0x2000) +#define PLIC_SCLAIM(hart) (PLIC + 0x201004 + (hart) * 0x2000) // the kernel expects there to be RAM // for use by the kernel and user pages // from physical address 0x80000000 to PHYSTOP. #define KERNBASE 0x80000000L -#define PHYSTOP (KERNBASE + 128*1024*1024) +#define PHYSTOP (KERNBASE + 128 * 1024 * 1024) // map the trampoline page to the highest address, // in both user and kernel space. @@ -53,7 +53,7 @@ // map kernel stacks beneath the trampoline, // each surrounded by invalid guard pages. -#define KSTACK(p) (TRAMPOLINE - ((p)+1)* 2*PGSIZE) +#define KSTACK(p) (TRAMPOLINE - ((p) + 1) * 2 * PGSIZE) // User memory layout. // Address zero first: diff --git a/kernel/param.h b/kernel/param.h index 6624bff..25643ad 100644 --- a/kernel/param.h +++ b/kernel/param.h @@ -1,13 +1,13 @@ -#define NPROC 64 // maximum number of processes -#define NCPU 8 // maximum number of CPUs -#define NOFILE 16 // open files per process -#define NFILE 100 // open files per system -#define NINODE 50 // maximum number of active i-nodes -#define NDEV 10 // maximum major device number -#define ROOTDEV 1 // device number of file system root disk -#define MAXARG 32 // max exec arguments -#define MAXOPBLOCKS 10 // max # of blocks any FS op writes -#define LOGSIZE (MAXOPBLOCKS*3) // max data blocks in on-disk log -#define NBUF (MAXOPBLOCKS*3) // size of disk block cache -#define FSSIZE 2000 // size of file system in blocks -#define MAXPATH 128 // maximum file path name +#define NPROC 64 // maximum number of processes +#define NCPU 8 // maximum number of CPUs +#define NOFILE 16 // open files per process +#define NFILE 100 // open files per system +#define NINODE 50 // maximum number of active i-nodes +#define NDEV 10 // maximum major device number +#define ROOTDEV 1 // device number of file system root disk +#define MAXARG 32 // max exec arguments +#define MAXOPBLOCKS 10 // max # of blocks any FS op writes +#define LOGSIZE (MAXOPBLOCKS * 3) // max data blocks in on-disk log +#define NBUF (MAXOPBLOCKS * 3) // size of disk block cache +#define FSSIZE 2000 // size of file system in blocks +#define MAXPATH 128 // maximum file path name diff --git a/kernel/pipe.c b/kernel/pipe.c index 9a7157e..9caf2be 100644 --- a/kernel/pipe.c +++ b/kernel/pipe.c @@ -12,11 +12,11 @@ struct pipe { struct spinlock lock; - char data[PIPESIZE]; - u32 nread; // number of bytes read - u32 nwrite; // number of bytes written - int readopen; // read fd is still open - int writeopen; // write fd is still open + char data[PIPESIZE]; + u32 nread; // number of bytes read + u32 nwrite; // number of bytes written + int readopen; // read fd is still open + int writeopen; // write fd is still open }; int @@ -28,7 +28,7 @@ pipealloc(struct file **f0, struct file **f1) *f0 = *f1 = 0; if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0) goto bad; - if((pi = (struct pipe*)kalloc()) == 0) + if((pi = (struct pipe *)kalloc()) == 0) goto bad; pi->readopen = 1; pi->writeopen = 1; @@ -45,9 +45,9 @@ pipealloc(struct file **f0, struct file **f1) (*f1)->pipe = pi; return 0; - bad: +bad: if(pi) - kfree((char*)pi); + kfree((char *)pi); if(*f0) fileclose(*f0); if(*f1) @@ -59,16 +59,16 @@ void pipeclose(struct pipe *pi, int writable) { acquire(&pi->lock); - if(writable){ + if(writable) { pi->writeopen = 0; wakeup(&pi->nread); } else { pi->readopen = 0; wakeup(&pi->nwrite); } - if(pi->readopen == 0 && pi->writeopen == 0){ + if(pi->readopen == 0 && pi->writeopen == 0) { release(&pi->lock); - kfree((char*)pi); + kfree((char *)pi); } else release(&pi->lock); } @@ -76,16 +76,16 @@ pipeclose(struct pipe *pi, int writable) int pipewrite(struct pipe *pi, u64 addr, int n) { - int i = 0; + int i = 0; struct proc *pr = myproc(); acquire(&pi->lock); - while(i < n){ - if(pi->readopen == 0 || killed(pr)){ + while(i < n) { + if(pi->readopen == 0 || killed(pr)) { release(&pi->lock); return -1; } - if(pi->nwrite == pi->nread + PIPESIZE){ //DOC: pipewrite-full + if(pi->nwrite == pi->nread + PIPESIZE) { // DOC: pipewrite-full wakeup(&pi->nread); sleep(&pi->nwrite, &pi->lock); } else { @@ -105,26 +105,26 @@ pipewrite(struct pipe *pi, u64 addr, int n) int piperead(struct pipe *pi, u64 addr, int n) { - int i; + int i; struct proc *pr = myproc(); - char ch; + char ch; acquire(&pi->lock); - while(pi->nread == pi->nwrite && pi->writeopen){ //DOC: pipe-empty - if(killed(pr)){ + while(pi->nread == pi->nwrite && pi->writeopen) { // DOC: pipe-empty + if(killed(pr)) { release(&pi->lock); return -1; } - sleep(&pi->nread, &pi->lock); //DOC: piperead-sleep + sleep(&pi->nread, &pi->lock); // DOC: piperead-sleep } - for(i = 0; i < n; i++){ //DOC: piperead-copy + for(i = 0; i < n; i++) { // DOC: piperead-copy if(pi->nread == pi->nwrite) break; ch = pi->data[pi->nread++ % PIPESIZE]; if(copyout(pr->pagetable, addr + i, &ch, 1) == -1) break; } - wakeup(&pi->nwrite); //DOC: piperead-wakeup + wakeup(&pi->nwrite); // DOC: piperead-wakeup release(&pi->lock); return i; } diff --git a/kernel/plic.c b/kernel/plic.c index 215bac3..90aec7e 100644 --- a/kernel/plic.c +++ b/kernel/plic.c @@ -12,21 +12,21 @@ void plicinit(void) { // set desired IRQ priorities non-zero (otherwise disabled). - *(u32*)(PLIC + UART0_IRQ*4) = 1; - *(u32*)(PLIC + VIRTIO0_IRQ*4) = 1; + *(u32 *)(PLIC + UART0_IRQ * 4) = 1; + *(u32 *)(PLIC + VIRTIO0_IRQ * 4) = 1; } void plicinithart(void) { int hart = cpuid(); - + // set enable bits for this hart's S-mode // for the uart and virtio disk. - *(u32*)PLIC_SENABLE(hart) = (1 << UART0_IRQ) | (1 << VIRTIO0_IRQ); + *(u32 *)PLIC_SENABLE(hart) = (1 << UART0_IRQ) | (1 << VIRTIO0_IRQ); // set this hart's S-mode priority threshold to 0. - *(u32*)PLIC_SPRIORITY(hart) = 0; + *(u32 *)PLIC_SPRIORITY(hart) = 0; } // ask the PLIC what interrupt we should serve. @@ -34,7 +34,7 @@ int plic_claim(void) { int hart = cpuid(); - int irq = *(u32*)PLIC_SCLAIM(hart); + int irq = *(u32 *)PLIC_SCLAIM(hart); return irq; } @@ -43,5 +43,5 @@ void plic_complete(int irq) { int hart = cpuid(); - *(u32*)PLIC_SCLAIM(hart) = irq; + *(u32 *)PLIC_SCLAIM(hart) = irq; } diff --git a/kernel/printf.c b/kernel/printf.c index 5a1f76f..006da3c 100644 --- a/kernel/printf.c +++ b/kernel/printf.c @@ -20,7 +20,7 @@ volatile int panicked = 0; // lock to avoid interleaving concurrent printf's. static struct { struct spinlock lock; - int locking; + int locking; } pr; static char digits[] = "0123456789abcdef"; @@ -29,8 +29,8 @@ static void printint(int xx, int base, int sign) { char buf[16]; - int i; - u32 x; + int i; + u32 x; if(sign && (sign = xx < 0)) x = -xx; @@ -55,7 +55,7 @@ printptr(u64 x) int i; consputc('0'); consputc('x'); - for (i = 0; i < (sizeof(u64) * 2); i++, x <<= 4) + for(i = 0; i < (sizeof(u64) * 2); i++, x <<= 4) consputc(digits[x >> (sizeof(u64) * 8 - 4)]); } @@ -64,26 +64,26 @@ void printf(char *fmt, ...) { va_list ap; - int i, c, locking; - char *s; + int i, c, locking; + char *s; locking = pr.locking; if(locking) acquire(&pr.lock); - if (fmt == 0) + if(fmt == 0) panic("null fmt"); va_start(ap, fmt); - for(i = 0; (c = fmt[i] & 0xff) != 0; i++){ - if(c != '%'){ + for(i = 0; (c = fmt[i] & 0xff) != 0; i++) { + if(c != '%') { consputc(c); continue; } c = fmt[++i] & 0xff; if(c == 0) break; - switch(c){ + switch(c) { case 'd': printint(va_arg(ap, int), 10, 1); break; @@ -94,7 +94,7 @@ printf(char *fmt, ...) printptr(va_arg(ap, u64)); break; case 's': - if((s = va_arg(ap, char*)) == 0) + if((s = va_arg(ap, char *)) == 0) s = "(null)"; for(; *s; s++) consputc(*s); diff --git a/kernel/proc.c b/kernel/proc.c index c126e09..58a3160 100644 --- a/kernel/proc.c +++ b/kernel/proc.c @@ -12,7 +12,7 @@ struct proc proc[NPROC]; struct proc *initproc; -int nextpid = 1; +int nextpid = 1; struct spinlock pid_lock; extern void forkret(void); @@ -33,12 +33,12 @@ void proc_mapstacks(pagetable_t kpgtbl) { struct proc *p; - + for(p = proc; p < &proc[NPROC]; p++) { char *pa = kalloc(); if(pa == 0) panic("kalloc"); - u64 va = KSTACK((int) (p - proc)); + u64 va = KSTACK((int)(p - proc)); kvmmap(kpgtbl, va, (u64)pa, PGSIZE, PTE_R | PTE_W); } } @@ -48,13 +48,13 @@ void procinit(void) { struct proc *p; - + initlock(&pid_lock, "nextpid"); initlock(&wait_lock, "wait_lock"); for(p = proc; p < &proc[NPROC]; p++) { - initlock(&p->lock, "proc"); - p->state = UNUSED; - p->kstack = KSTACK((int) (p - proc)); + initlock(&p->lock, "proc"); + p->state = UNUSED; + p->kstack = KSTACK((int)(p - proc)); } } @@ -70,20 +70,20 @@ cpuid() // Return this CPU's cpu struct. // Interrupts must be disabled. -struct cpu* +struct cpu * mycpu(void) { - int id = cpuid(); + int id = cpuid(); struct cpu *c = &cpus[id]; return c; } // Return the current struct proc *, or zero if none. -struct proc* +struct proc * myproc(void) { push_off(); - struct cpu *c = mycpu(); + struct cpu *c = mycpu(); struct proc *p = c->proc; pop_off(); return p; @@ -93,7 +93,7 @@ int allocpid() { int pid; - + acquire(&pid_lock); pid = nextpid; nextpid = nextpid + 1; @@ -106,7 +106,7 @@ allocpid() // If found, initialize state required to run in the kernel, // and return with p->lock held. // If there are no free procs, or a memory allocation fails, return 0. -static struct proc* +static struct proc * allocproc(void) { struct proc *p; @@ -126,7 +126,7 @@ found: p->state = USED; // Allocate a trapframe page. - if((p->trapframe = (struct trapframe *)kalloc()) == 0){ + if((p->trapframe = (struct trapframe *)kalloc()) == 0) { freeproc(p); release(&p->lock); return 0; @@ -134,7 +134,7 @@ found: // An empty user page table. p->pagetable = proc_pagetable(p); - if(p->pagetable == 0){ + if(p->pagetable == 0) { freeproc(p); release(&p->lock); return 0; @@ -156,7 +156,7 @@ static void freeproc(struct proc *p) { if(p->trapframe) - kfree((void*)p->trapframe); + kfree((void *)p->trapframe); p->trapframe = 0; if(p->pagetable) proc_freepagetable(p->pagetable, p->sz); @@ -187,16 +187,14 @@ proc_pagetable(struct proc *p) // at the highest user virtual address. // only the supervisor uses it, on the way // to/from user space, so not PTE_U. - if(mappages(pagetable, TRAMPOLINE, PGSIZE, - (u64)trampoline, PTE_R | PTE_X) < 0){ + if(mappages(pagetable, TRAMPOLINE, PGSIZE, (u64)trampoline, PTE_R | PTE_X) < 0) { uvmfree(pagetable, 0); return 0; } // map the trapframe page just below the trampoline page, for // trampoline.S. - if(mappages(pagetable, TRAPFRAME, PGSIZE, - (u64)(p->trapframe), PTE_R | PTE_W) < 0){ + if(mappages(pagetable, TRAPFRAME, PGSIZE, (u64)(p->trapframe), PTE_R | PTE_W) < 0) { uvmunmap(pagetable, TRAMPOLINE, 1, 0); uvmfree(pagetable, 0); return 0; @@ -218,15 +216,10 @@ proc_freepagetable(pagetable_t pagetable, u64 sz) // a user program that calls exec("/init") // assembled from ../user/initcode.S // od -t xC ../user/initcode -u8 initcode[] = { - 0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02, - 0x97, 0x05, 0x00, 0x00, 0x93, 0x85, 0x35, 0x02, - 0x93, 0x08, 0x70, 0x00, 0x73, 0x00, 0x00, 0x00, - 0x93, 0x08, 0x20, 0x00, 0x73, 0x00, 0x00, 0x00, - 0xef, 0xf0, 0x9f, 0xff, 0x2f, 0x69, 0x6e, 0x69, - 0x74, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00 -}; +u8 initcode[] + = { 0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02, 0x97, 0x05, 0x00, 0x00, 0x93, 0x85, 0x35, 0x02, 0x93, 0x08, + 0x70, 0x00, 0x73, 0x00, 0x00, 0x00, 0x93, 0x08, 0x20, 0x00, 0x73, 0x00, 0x00, 0x00, 0xef, 0xf0, 0x9f, 0xff, + 0x2f, 0x69, 0x6e, 0x69, 0x74, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Set up first user process. void @@ -236,15 +229,15 @@ userinit(void) p = allocproc(); initproc = p; - + // allocate one user page and copy initcode's instructions // and data into it. uvmfirst(p->pagetable, initcode, sizeof(initcode)); p->sz = PGSIZE; // prepare for the very first "return" from kernel to user. - p->trapframe->epc = 0; // user program counter - p->trapframe->sp = PGSIZE; // user stack pointer + p->trapframe->epc = 0; // user program counter + p->trapframe->sp = PGSIZE; // user stack pointer safestrcpy(p->name, "initcode", sizeof(p->name)); p->cwd = namei("/"); @@ -259,15 +252,15 @@ userinit(void) int growproc(int n) { - u64 sz; + u64 sz; struct proc *p = myproc(); sz = p->sz; - if(n > 0){ + if(n > 0) { if((sz = uvmalloc(p->pagetable, sz, sz + n, PTE_W)) == 0) { return -1; } - } else if(n < 0){ + } else if(n < 0) { sz = uvmdealloc(p->pagetable, sz, sz + n); } p->sz = sz; @@ -279,17 +272,17 @@ growproc(int n) int fork(void) { - int i, pid; + int i, pid; struct proc *np; struct proc *p = myproc(); // Allocate process. - if((np = allocproc()) == 0){ + if((np = allocproc()) == 0) { return -1; } // Copy user memory from parent to child. - if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){ + if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0) { freeproc(np); release(&np->lock); return -1; @@ -332,8 +325,8 @@ reparent(struct proc *p) { struct proc *pp; - for(pp = proc; pp < &proc[NPROC]; pp++){ - if(pp->parent == p){ + for(pp = proc; pp < &proc[NPROC]; pp++) { + if(pp->parent == p) { pp->parent = initproc; wakeup(initproc); } @@ -352,8 +345,8 @@ exit(int status) panic("init exiting"); // Close all open files. - for(int fd = 0; fd < NOFILE; fd++){ - if(p->ofile[fd]){ + for(int fd = 0; fd < NOFILE; fd++) { + if(p->ofile[fd]) { struct file *f = p->ofile[fd]; fileclose(f); p->ofile[fd] = 0; @@ -372,7 +365,7 @@ exit(int status) // Parent might be sleeping in wait(). wakeup(p->parent); - + acquire(&p->lock); p->xstate = status; @@ -391,25 +384,24 @@ int wait(u64 addr) { struct proc *pp; - int havekids, pid; + int havekids, pid; struct proc *p = myproc(); acquire(&wait_lock); - for(;;){ + for(;;) { // Scan through table looking for exited children. havekids = 0; - for(pp = proc; pp < &proc[NPROC]; pp++){ - if(pp->parent == p){ + for(pp = proc; pp < &proc[NPROC]; pp++) { + if(pp->parent == p) { // make sure the child isn't still in exit() or swtch(). acquire(&pp->lock); havekids = 1; - if(pp->state == ZOMBIE){ + if(pp->state == ZOMBIE) { // Found one. pid = pp->pid; - if(addr != 0 && copyout(p->pagetable, addr, (char *)&pp->xstate, - sizeof(pp->xstate)) < 0) { + if(addr != 0 && copyout(p->pagetable, addr, (char *)&pp->xstate, sizeof(pp->xstate)) < 0) { release(&pp->lock); release(&wait_lock); return -1; @@ -424,13 +416,13 @@ wait(u64 addr) } // No point waiting if we don't have any children. - if(!havekids || killed(p)){ + if(!havekids || killed(p)) { release(&wait_lock); return -1; } - + // Wait for a child to exit. - sleep(p, &wait_lock); //DOC: wait-sleep + sleep(p, &wait_lock); // DOC: wait-sleep } } @@ -445,10 +437,10 @@ void scheduler(void) { struct proc *p; - struct cpu *c = mycpu(); - + struct cpu *c = mycpu(); + c->proc = 0; - for(;;){ + for(;;) { // Avoid deadlock by ensuring that devices can interrupt. intr_on(); @@ -481,7 +473,7 @@ scheduler(void) void sched(void) { - int intena; + int intena; struct proc *p = myproc(); if(!holding(&p->lock)) @@ -519,7 +511,7 @@ forkret(void) // Still holding p->lock from scheduler. release(&myproc()->lock); - if (first) { + if(first) { // File system initialization must be run in the context of a // regular process (e.g., because it calls sleep), and thus cannot // be run from main(). @@ -536,7 +528,7 @@ void sleep(void *chan, struct spinlock *lk) { struct proc *p = myproc(); - + // Must acquire p->lock in order to // change p->state and then call sched. // Once we hold p->lock, we can be @@ -544,7 +536,7 @@ sleep(void *chan, struct spinlock *lk) // (wakeup locks p->lock), // so it's okay to release lk. - acquire(&p->lock); //DOC: sleeplock1 + acquire(&p->lock); // DOC: sleeplock1 release(lk); // Go to sleep. @@ -569,7 +561,7 @@ wakeup(void *chan) struct proc *p; for(p = proc; p < &proc[NPROC]; p++) { - if(p != myproc()){ + if(p != myproc()) { acquire(&p->lock); if(p->state == SLEEPING && p->chan == chan) { p->state = RUNNABLE; @@ -587,11 +579,11 @@ kill(int pid) { struct proc *p; - for(p = proc; p < &proc[NPROC]; p++){ + for(p = proc; p < &proc[NPROC]; p++) { acquire(&p->lock); - if(p->pid == pid){ + if(p->pid == pid) { p->killed = 1; - if(p->state == SLEEPING){ + if(p->state == SLEEPING) { // Wake process from sleep(). p->state = RUNNABLE; } @@ -615,7 +607,7 @@ int killed(struct proc *p) { int k; - + acquire(&p->lock); k = p->killed; release(&p->lock); @@ -629,7 +621,7 @@ int either_copyout(int user_dst, u64 dst, void *src, u64 len) { struct proc *p = myproc(); - if(user_dst){ + if(user_dst) { return copyout(p->pagetable, dst, src, len); } else { memmove((char *)dst, src, len); @@ -644,10 +636,10 @@ int either_copyin(void *dst, int user_src, u64 src, u64 len) { struct proc *p = myproc(); - if(user_src){ + if(user_src) { return copyin(p->pagetable, dst, src, len); } else { - memmove(dst, (char*)src, len); + memmove(dst, (char *)src, len); return 0; } } @@ -659,18 +651,13 @@ void procdump(void) { static char *states[] = { - [UNUSED] "unused", - [USED] "used", - [SLEEPING] "sleep ", - [RUNNABLE] "runble", - [RUNNING] "run ", - [ZOMBIE] "zombie" + [UNUSED] "unused", [USED] "used", [SLEEPING] "sleep ", [RUNNABLE] "runble", [RUNNING] "run ", [ZOMBIE] "zombie" }; struct proc *p; - char *state; + char *state; printf("\n"); - for(p = proc; p < &proc[NPROC]; p++){ + for(p = proc; p < &proc[NPROC]; p++) { if(p->state == UNUSED) continue; if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) diff --git a/kernel/proc.h b/kernel/proc.h index 19eb0da..6aed367 100644 --- a/kernel/proc.h +++ b/kernel/proc.h @@ -20,10 +20,10 @@ struct context { // Per-CPU state. struct cpu { - struct proc *proc; // The process running on this cpu, or null. - struct context context; // swtch() here to enter scheduler(). - int noff; // Depth of push_off() nesting. - int intena; // Were interrupts enabled before push_off()? + struct proc *proc; // The process running on this cpu, or null. + struct context context; // swtch() here to enter scheduler(). + int noff; // Depth of push_off() nesting. + int intena; // Were interrupts enabled before push_off()? }; extern struct cpu cpus[NCPU]; @@ -86,22 +86,22 @@ struct proc { struct spinlock lock; // p->lock must be held when using these: - enum procstate state; // Process state - void *chan; // If non-zero, sleeping on chan - int killed; // If non-zero, have been killed - int xstate; // Exit status to be returned to parent's wait - int pid; // Process ID + enum procstate state; // Process state + void *chan; // If non-zero, sleeping on chan + int killed; // If non-zero, have been killed + int xstate; // Exit status to be returned to parent's wait + int pid; // Process ID // wait_lock must be held when using this: - struct proc *parent; // Parent process + struct proc *parent; // Parent process // these are private to the process, so p->lock need not be held. - u64 kstack; // Virtual address of kernel stack - u64 sz; // Size of process memory (bytes) - pagetable_t pagetable; // User page table - struct trapframe *trapframe; // data page for trampoline.S - struct context context; // swtch() here to run process - struct file *ofile[NOFILE]; // Open files - struct inode *cwd; // Current directory - char name[16]; // Process name (debugging) + u64 kstack; // Virtual address of kernel stack + u64 sz; // Size of process memory (bytes) + pagetable_t pagetable; // User page table + struct trapframe *trapframe; // data page for trampoline.S + struct context context; // swtch() here to run process + struct file *ofile[NOFILE]; // Open files + struct inode *cwd; // Current directory + char name[16]; // Process name (debugging) }; diff --git a/kernel/ramdisk.c b/kernel/ramdisk.c index 73db406..2f96ba4 100644 --- a/kernel/ramdisk.c +++ b/kernel/ramdisk.c @@ -24,16 +24,16 @@ ramdiskrw(struct buf *b) { if(!holdingsleep(&b->lock)) panic("ramdiskrw: buf not locked"); - if((b->flags & (B_VALID|B_DIRTY)) == B_VALID) + if((b->flags & (B_VALID | B_DIRTY)) == B_VALID) panic("ramdiskrw: nothing to do"); if(b->blockno >= FSSIZE) panic("ramdiskrw: blockno too big"); - u64 diskaddr = b->blockno * BSIZE; + u64 diskaddr = b->blockno * BSIZE; char *addr = (char *)RAMDISK + diskaddr; - if(b->flags & B_DIRTY){ + if(b->flags & B_DIRTY) { // write memmove(addr, b->data, BSIZE); b->flags &= ~B_DIRTY; diff --git a/kernel/riscv.h b/kernel/riscv.h index adf7215..21c98cd 100644 --- a/kernel/riscv.h +++ b/kernel/riscv.h @@ -5,61 +5,61 @@ static inline u64 r_mhartid() { u64 x; - asm volatile("csrr %0, mhartid" : "=r" (x) ); + asm volatile("csrr %0, mhartid" : "=r"(x)); return x; } // Machine Status Register, mstatus #define MSTATUS_MPP_MASK (3L << 11) // previous mode. -#define MSTATUS_MPP_M (3L << 11) -#define MSTATUS_MPP_S (1L << 11) -#define MSTATUS_MPP_U (0L << 11) -#define MSTATUS_MIE (1L << 3) // machine-mode interrupt enable. +#define MSTATUS_MPP_M (3L << 11) +#define MSTATUS_MPP_S (1L << 11) +#define MSTATUS_MPP_U (0L << 11) +#define MSTATUS_MIE (1L << 3) // machine-mode interrupt enable. static inline u64 r_mstatus() { u64 x; - asm volatile("csrr %0, mstatus" : "=r" (x) ); + asm volatile("csrr %0, mstatus" : "=r"(x)); return x; } -static inline void +static inline void w_mstatus(u64 x) { - asm volatile("csrw mstatus, %0" : : "r" (x)); + asm volatile("csrw mstatus, %0" : : "r"(x)); } // machine exception program counter, holds the // instruction address to which a return from // exception will go. -static inline void +static inline void w_mepc(u64 x) { - asm volatile("csrw mepc, %0" : : "r" (x)); + asm volatile("csrw mepc, %0" : : "r"(x)); } // Supervisor Status Register, sstatus -#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User +#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User #define SSTATUS_SPIE (1L << 5) // Supervisor Previous Interrupt Enable #define SSTATUS_UPIE (1L << 4) // User Previous Interrupt Enable -#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable -#define SSTATUS_UIE (1L << 0) // User Interrupt Enable +#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable +#define SSTATUS_UIE (1L << 0) // User Interrupt Enable static inline u64 r_sstatus() { u64 x; - asm volatile("csrr %0, sstatus" : "=r" (x) ); + asm volatile("csrr %0, sstatus" : "=r"(x)); return x; } -static inline void +static inline void w_sstatus(u64 x) { - asm volatile("csrw sstatus, %0" : : "r" (x)); + asm volatile("csrw sstatus, %0" : : "r"(x)); } // Supervisor Interrupt Pending @@ -67,14 +67,14 @@ static inline u64 r_sip() { u64 x; - asm volatile("csrr %0, sip" : "=r" (x) ); + asm volatile("csrr %0, sip" : "=r"(x)); return x; } -static inline void +static inline void w_sip(u64 x) { - asm volatile("csrw sip, %0" : : "r" (x)); + asm volatile("csrw sip, %0" : : "r"(x)); } // Supervisor Interrupt Enable @@ -85,14 +85,14 @@ static inline u64 r_sie() { u64 x; - asm volatile("csrr %0, sie" : "=r" (x) ); + asm volatile("csrr %0, sie" : "=r"(x)); return x; } -static inline void +static inline void w_sie(u64 x) { - asm volatile("csrw sie, %0" : : "r" (x)); + asm volatile("csrw sie, %0" : : "r"(x)); } // Machine-mode Interrupt Enable @@ -103,30 +103,30 @@ static inline u64 r_mie() { u64 x; - asm volatile("csrr %0, mie" : "=r" (x) ); + asm volatile("csrr %0, mie" : "=r"(x)); return x; } -static inline void +static inline void w_mie(u64 x) { - asm volatile("csrw mie, %0" : : "r" (x)); + asm volatile("csrw mie, %0" : : "r"(x)); } // supervisor exception program counter, holds the // instruction address to which a return from // exception will go. -static inline void +static inline void w_sepc(u64 x) { - asm volatile("csrw sepc, %0" : : "r" (x)); + asm volatile("csrw sepc, %0" : : "r"(x)); } static inline u64 r_sepc() { u64 x; - asm volatile("csrr %0, sepc" : "=r" (x) ); + asm volatile("csrr %0, sepc" : "=r"(x)); return x; } @@ -135,14 +135,14 @@ static inline u64 r_medeleg() { u64 x; - asm volatile("csrr %0, medeleg" : "=r" (x) ); + asm volatile("csrr %0, medeleg" : "=r"(x)); return x; } -static inline void +static inline void w_medeleg(u64 x) { - asm volatile("csrw medeleg, %0" : : "r" (x)); + asm volatile("csrw medeleg, %0" : : "r"(x)); } // Machine Interrupt Delegation @@ -150,50 +150,50 @@ static inline u64 r_mideleg() { u64 x; - asm volatile("csrr %0, mideleg" : "=r" (x) ); + asm volatile("csrr %0, mideleg" : "=r"(x)); return x; } -static inline void +static inline void w_mideleg(u64 x) { - asm volatile("csrw mideleg, %0" : : "r" (x)); + asm volatile("csrw mideleg, %0" : : "r"(x)); } // Supervisor Trap-Vector Base Address // low two bits are mode. -static inline void +static inline void w_stvec(u64 x) { - asm volatile("csrw stvec, %0" : : "r" (x)); + asm volatile("csrw stvec, %0" : : "r"(x)); } static inline u64 r_stvec() { u64 x; - asm volatile("csrr %0, stvec" : "=r" (x) ); + asm volatile("csrr %0, stvec" : "=r"(x)); return x; } // Machine-mode interrupt vector -static inline void +static inline void w_mtvec(u64 x) { - asm volatile("csrw mtvec, %0" : : "r" (x)); + asm volatile("csrw mtvec, %0" : : "r"(x)); } // Physical Memory Protection static inline void w_pmpcfg0(u64 x) { - asm volatile("csrw pmpcfg0, %0" : : "r" (x)); + asm volatile("csrw pmpcfg0, %0" : : "r"(x)); } static inline void w_pmpaddr0(u64 x) { - asm volatile("csrw pmpaddr0, %0" : : "r" (x)); + asm volatile("csrw pmpaddr0, %0" : : "r"(x)); } // use riscv's sv39 page table scheme. @@ -203,24 +203,24 @@ w_pmpaddr0(u64 x) // supervisor address translation and protection; // holds the address of the page table. -static inline void +static inline void w_satp(u64 x) { - asm volatile("csrw satp, %0" : : "r" (x)); + asm volatile("csrw satp, %0" : : "r"(x)); } static inline u64 r_satp() { u64 x; - asm volatile("csrr %0, satp" : "=r" (x) ); + asm volatile("csrr %0, satp" : "=r"(x)); return x; } -static inline void +static inline void w_mscratch(u64 x) { - asm volatile("csrw mscratch, %0" : : "r" (x)); + asm volatile("csrw mscratch, %0" : : "r"(x)); } // Supervisor Trap Cause @@ -228,7 +228,7 @@ static inline u64 r_scause() { u64 x; - asm volatile("csrr %0, scause" : "=r" (x) ); + asm volatile("csrr %0, scause" : "=r"(x)); return x; } @@ -237,22 +237,22 @@ static inline u64 r_stval() { u64 x; - asm volatile("csrr %0, stval" : "=r" (x) ); + asm volatile("csrr %0, stval" : "=r"(x)); return x; } // Machine-mode Counter-Enable -static inline void +static inline void w_mcounteren(u64 x) { - asm volatile("csrw mcounteren, %0" : : "r" (x)); + asm volatile("csrw mcounteren, %0" : : "r"(x)); } static inline u64 r_mcounteren() { u64 x; - asm volatile("csrr %0, mcounteren" : "=r" (x) ); + asm volatile("csrr %0, mcounteren" : "=r"(x)); return x; } @@ -261,7 +261,7 @@ static inline u64 r_time() { u64 x; - asm volatile("csrr %0, time" : "=r" (x) ); + asm volatile("csrr %0, time" : "=r"(x)); return x; } @@ -291,7 +291,7 @@ static inline u64 r_sp() { u64 x; - asm volatile("mv %0, sp" : "=r" (x) ); + asm volatile("mv %0, sp" : "=r"(x)); return x; } @@ -301,21 +301,21 @@ static inline u64 r_tp() { u64 x; - asm volatile("mv %0, tp" : "=r" (x) ); + asm volatile("mv %0, tp" : "=r"(x)); return x; } -static inline void +static inline void w_tp(u64 x) { - asm volatile("mv tp, %0" : : "r" (x)); + asm volatile("mv tp, %0" : : "r"(x)); } static inline u64 r_ra() { u64 x; - asm volatile("mv %0, ra" : "=r" (x) ); + asm volatile("mv %0, ra" : "=r"(x)); return x; } @@ -327,16 +327,16 @@ sfence_vma() asm volatile("sfence.vma zero, zero"); } -typedef u64 pte_t; +typedef u64 pte_t; typedef u64 *pagetable_t; // 512 PTEs #endif // __ASSEMBLER__ -#define PGSIZE 4096 // bytes per page -#define PGSHIFT 12 // bits of offset within a page +#define PGSIZE 4096 // bytes per page +#define PGSHIFT 12 // bits of offset within a page -#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1)) -#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1)) +#define PGROUNDUP(sz) (((sz) + PGSIZE - 1) & ~(PGSIZE - 1)) +#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE - 1)) #define PTE_V (1L << 0) // valid #define PTE_R (1L << 1) @@ -352,9 +352,9 @@ typedef u64 *pagetable_t; // 512 PTEs #define PTE_FLAGS(pte) ((pte) & 0x3FF) // extract the three 9-bit page table indices from a virtual address. -#define PXMASK 0x1FF // 9 bits -#define PXSHIFT(level) (PGSHIFT+(9*(level))) -#define PX(level, va) ((((u64) (va)) >> PXSHIFT(level)) & PXMASK) +#define PXMASK 0x1FF // 9 bits +#define PXSHIFT(level) (PGSHIFT + (9 * (level))) +#define PX(level, va) ((((u64)(va)) >> PXSHIFT(level)) & PXMASK) // one beyond the highest possible virtual address. // MAXVA is actually one bit less than the max allowed by diff --git a/kernel/sleeplock.c b/kernel/sleeplock.c index 81de585..e0f05a9 100644 --- a/kernel/sleeplock.c +++ b/kernel/sleeplock.c @@ -22,7 +22,7 @@ void acquiresleep(struct sleeplock *lk) { acquire(&lk->lk); - while (lk->locked) { + while(lk->locked) { sleep(lk, &lk->lk); } lk->locked = 1; @@ -44,12 +44,9 @@ int holdingsleep(struct sleeplock *lk) { int r; - + acquire(&lk->lk); r = lk->locked && (lk->pid == myproc()->pid); release(&lk->lk); return r; } - - - diff --git a/kernel/sleeplock.h b/kernel/sleeplock.h index 250e420..7958192 100644 --- a/kernel/sleeplock.h +++ b/kernel/sleeplock.h @@ -1,10 +1,9 @@ // Long-term locks for processes struct sleeplock { - u32 locked; // Is the lock held? - struct spinlock lk; // spinlock protecting this sleep lock - - // For debugging: - char *name; // Name of lock. - int pid; // Process holding lock -}; + u32 locked; // Is the lock held? + struct spinlock lk; // spinlock protecting this sleep lock + // For debugging: + char *name; // Name of lock. + int pid; // Process holding lock +}; diff --git a/kernel/spinlock.h b/kernel/spinlock.h index 75f3a36..ad6f660 100644 --- a/kernel/spinlock.h +++ b/kernel/spinlock.h @@ -1,9 +1,8 @@ // Mutual exclusion lock. struct spinlock { - u32 locked; // Is the lock held? + u32 locked; // Is the lock held? // For debugging: - char *name; // Name of lock. - struct cpu *cpu; // The cpu holding the lock. + char *name; // Name of lock. + struct cpu *cpu; // The cpu holding the lock. }; - diff --git a/kernel/start.c b/kernel/start.c index 50cb7b2..f2bdc52 100644 --- a/kernel/start.c +++ b/kernel/start.c @@ -8,7 +8,7 @@ void main(); void timerinit(); // entry.S needs one stack per CPU. -__attribute__ ((aligned (16))) char stack0[4096 * NCPU]; +__attribute__((aligned(16))) char stack0[4096 * NCPU]; // a scratch area per CPU for machine-mode timer interrupts. u64 timer_scratch[NCPU][5]; @@ -67,7 +67,7 @@ timerinit() // ask the CLINT for a timer interrupt. int interval = 1000000; // cycles; about 1/10th second in qemu. - *(u64*)CLINT_MTIMECMP(id) = *(u64*)CLINT_MTIME + interval; + *(u64 *)CLINT_MTIMECMP(id) = *(u64 *)CLINT_MTIME + interval; // prepare information in scratch[] for timervec. // scratch[0..2] : space for timervec to save registers. diff --git a/kernel/stat.h b/kernel/stat.h index d5eadd6..9bf0083 100644 --- a/kernel/stat.h +++ b/kernel/stat.h @@ -1,11 +1,11 @@ -#define T_DIR 1 // Directory -#define T_FILE 2 // File -#define T_DEVICE 3 // Device +#define T_DIR 1 // Directory +#define T_FILE 2 // File +#define T_DEVICE 3 // Device struct stat { - int dev; // File system's disk device - u32 ino; // Inode number + int dev; // File system's disk device + u32 ino; // Inode number short type; // Type of file short nlink; // Number of links to file - u64 size; // Size of file in bytes + u64 size; // Size of file in bytes }; diff --git a/kernel/string.c b/kernel/string.c index c0a170c..cc637d3 100644 --- a/kernel/string.c +++ b/kernel/string.c @@ -1,11 +1,11 @@ #include "types.h" -void* +void * memset(void *dst, int c, u32 n) { - char *cdst = (char *) dst; - int i; - for(i = 0; i < n; i++){ + char *cdst = (char *)dst; + int i; + for(i = 0; i < n; i++) { cdst[i] = c; } return dst; @@ -18,7 +18,7 @@ memcmp(const void *v1, const void *v2, u32 n) s1 = v1; s2 = v2; - while(n-- > 0){ + while(n-- > 0) { if(*s1 != *s2) return *s1 - *s2; s1++, s2++; @@ -27,18 +27,18 @@ memcmp(const void *v1, const void *v2, u32 n) return 0; } -void* +void * memmove(void *dst, const void *src, u32 n) { const char *s; - char *d; + char *d; if(n == 0) return dst; - + s = src; d = dst; - if(s < d && s + n > d){ + if(s < d && s + n > d) { s += n; d += n; while(n-- > 0) @@ -51,7 +51,7 @@ memmove(void *dst, const void *src, u32 n) } // memcpy exists to placate GCC. Use memmove. -void* +void * memcpy(void *dst, const void *src, u32 n) { return memmove(dst, src, n); @@ -67,7 +67,7 @@ strncmp(const char *p, const char *q, u32 n) return (u8)*p - (u8)*q; } -char* +char * strncpy(char *s, const char *t, int n) { char *os; @@ -81,7 +81,7 @@ strncpy(char *s, const char *t, int n) } // Like strncpy but guaranteed to NUL-terminate. -char* +char * safestrcpy(char *s, const char *t, int n) { char *os; @@ -104,4 +104,3 @@ strlen(const char *s) ; return n; } - diff --git a/kernel/syscall.c b/kernel/syscall.c index b7e9a03..910b207 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -12,7 +12,7 @@ int fetchaddr(u64 addr, u64 *ip) { struct proc *p = myproc(); - if(addr >= p->sz || addr+sizeof(u64) > p->sz) // both tests needed, in case of overflow + if(addr >= p->sz || addr + sizeof(u64) > p->sz) // both tests needed, in case of overflow return -1; if(copyin(p->pagetable, (char *)ip, addr, sizeof(*ip)) != 0) return -1; @@ -34,7 +34,7 @@ static u64 argraw(int n) { struct proc *p = myproc(); - switch (n) { + switch(n) { case 0: return p->trapframe->a0; case 1: @@ -105,33 +105,18 @@ extern u64 sys_close(void); // An array mapping syscall numbers from syscall.h // to the function that handles the system call. static u64 (*syscalls[])(void) = { -[SYS_fork] sys_fork, -[SYS_exit] sys_exit, -[SYS_wait] sys_wait, -[SYS_pipe] sys_pipe, -[SYS_read] sys_read, -[SYS_kill] sys_kill, -[SYS_exec] sys_exec, -[SYS_fstat] sys_fstat, -[SYS_chdir] sys_chdir, -[SYS_dup] sys_dup, -[SYS_getpid] sys_getpid, -[SYS_sbrk] sys_sbrk, -[SYS_sleep] sys_sleep, -[SYS_uptime] sys_uptime, -[SYS_open] sys_open, -[SYS_write] sys_write, -[SYS_mknod] sys_mknod, -[SYS_unlink] sys_unlink, -[SYS_link] sys_link, -[SYS_mkdir] sys_mkdir, -[SYS_close] sys_close, + [SYS_fork] sys_fork, [SYS_exit] sys_exit, [SYS_wait] sys_wait, [SYS_pipe] sys_pipe, + [SYS_read] sys_read, [SYS_kill] sys_kill, [SYS_exec] sys_exec, [SYS_fstat] sys_fstat, + [SYS_chdir] sys_chdir, [SYS_dup] sys_dup, [SYS_getpid] sys_getpid, [SYS_sbrk] sys_sbrk, + [SYS_sleep] sys_sleep, [SYS_uptime] sys_uptime, [SYS_open] sys_open, [SYS_write] sys_write, + [SYS_mknod] sys_mknod, [SYS_unlink] sys_unlink, [SYS_link] sys_link, [SYS_mkdir] sys_mkdir, + [SYS_close] sys_close, }; void syscall(void) { - int num; + int num; struct proc *p = myproc(); num = p->trapframe->a7; @@ -140,8 +125,7 @@ syscall(void) // and store its return value in p->trapframe->a0 p->trapframe->a0 = syscalls[num](); } else { - printf("%d %s: unknown sys call %d\n", - p->pid, p->name, num); + printf("%d %s: unknown sys call %d\n", p->pid, p->name, num); p->trapframe->a0 = -1; } } diff --git a/kernel/syscall.h b/kernel/syscall.h index bc5f356..a378a8a 100644 --- a/kernel/syscall.h +++ b/kernel/syscall.h @@ -1,13 +1,13 @@ // System call numbers -#define SYS_fork 1 -#define SYS_exit 2 -#define SYS_wait 3 -#define SYS_pipe 4 -#define SYS_read 5 -#define SYS_kill 6 -#define SYS_exec 7 -#define SYS_fstat 8 -#define SYS_chdir 9 +#define SYS_fork 1 +#define SYS_exit 2 +#define SYS_wait 3 +#define SYS_pipe 4 +#define SYS_read 5 +#define SYS_kill 6 +#define SYS_exec 7 +#define SYS_fstat 8 +#define SYS_chdir 9 #define SYS_dup 10 #define SYS_getpid 11 #define SYS_sbrk 12 diff --git a/kernel/sysfile.c b/kernel/sysfile.c index 8f7c1c8..49ca250 100644 --- a/kernel/sysfile.c +++ b/kernel/sysfile.c @@ -21,11 +21,11 @@ static int argfd(int n, int *pfd, struct file **pf) { - int fd; + int fd; struct file *f; argint(n, &fd); - if(fd < 0 || fd >= NOFILE || (f=myproc()->ofile[fd]) == 0) + if(fd < 0 || fd >= NOFILE || (f = myproc()->ofile[fd]) == 0) return -1; if(pfd) *pfd = fd; @@ -39,11 +39,11 @@ argfd(int n, int *pfd, struct file **pf) static int fdalloc(struct file *f) { - int fd; + int fd; struct proc *p = myproc(); - for(fd = 0; fd < NOFILE; fd++){ - if(p->ofile[fd] == 0){ + for(fd = 0; fd < NOFILE; fd++) { + if(p->ofile[fd] == 0) { p->ofile[fd] = f; return fd; } @@ -55,11 +55,11 @@ u64 sys_dup(void) { struct file *f; - int fd; + int fd; if(argfd(0, 0, &f) < 0) return -1; - if((fd=fdalloc(f)) < 0) + if((fd = fdalloc(f)) < 0) return -1; filedup(f); return fd; @@ -69,8 +69,8 @@ u64 sys_read(void) { struct file *f; - int n; - u64 p; + int n; + u64 p; argaddr(1, &p); argint(2, &n); @@ -83,9 +83,9 @@ u64 sys_write(void) { struct file *f; - int n; - u64 p; - + int n; + u64 p; + argaddr(1, &p); argint(2, &n); if(argfd(0, 0, &f) < 0) @@ -97,7 +97,7 @@ sys_write(void) u64 sys_close(void) { - int fd; + int fd; struct file *f; if(argfd(0, &fd, &f) < 0) @@ -111,7 +111,7 @@ u64 sys_fstat(void) { struct file *f; - u64 st; // user pointer to struct stat + u64 st; // user pointer to struct stat argaddr(1, &st); if(argfd(0, 0, &f) < 0) @@ -123,20 +123,20 @@ sys_fstat(void) u64 sys_link(void) { - char name[DIRSIZ], new[MAXPATH], old[MAXPATH]; + char name[DIRSIZ], new[MAXPATH], old[MAXPATH]; struct inode *dp, *ip; if(argstr(0, old, MAXPATH) < 0 || argstr(1, new, MAXPATH) < 0) return -1; begin_op(); - if((ip = namei(old)) == 0){ + if((ip = namei(old)) == 0) { end_op(); return -1; } ilock(ip); - if(ip->type == T_DIR){ + if(ip->type == T_DIR) { iunlockput(ip); end_op(); return -1; @@ -149,7 +149,7 @@ sys_link(void) if((dp = nameiparent(new, name)) == 0) goto bad; ilock(dp); - if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){ + if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0) { iunlockput(dp); goto bad; } @@ -173,10 +173,10 @@ bad: static int isdirempty(struct inode *dp) { - int off; + int off; struct dirent de; - for(off=2*sizeof(de); offsize; off+=sizeof(de)){ + for(off = 2 * sizeof(de); off < dp->size; off += sizeof(de)) { if(readi(dp, 0, (u64)&de, off, sizeof(de)) != sizeof(de)) panic("isdirempty: readi"); if(de.inum != 0) @@ -190,14 +190,14 @@ sys_unlink(void) { struct inode *ip, *dp; struct dirent de; - char name[DIRSIZ], path[MAXPATH]; - u32 off; + char name[DIRSIZ], path[MAXPATH]; + u32 off; if(argstr(0, path, MAXPATH) < 0) return -1; begin_op(); - if((dp = nameiparent(path, name)) == 0){ + if((dp = nameiparent(path, name)) == 0) { end_op(); return -1; } @@ -214,7 +214,7 @@ sys_unlink(void) if(ip->nlink < 1) panic("unlink: nlink < 1"); - if(ip->type == T_DIR && !isdirempty(ip)){ + if(ip->type == T_DIR && !isdirempty(ip)) { iunlockput(ip); goto bad; } @@ -222,7 +222,7 @@ sys_unlink(void) memset(&de, 0, sizeof(de)); if(writei(dp, 0, (u64)&de, off, sizeof(de)) != sizeof(de)) panic("unlink: writei"); - if(ip->type == T_DIR){ + if(ip->type == T_DIR) { dp->nlink--; iupdate(dp); } @@ -242,18 +242,18 @@ bad: return -1; } -static struct inode* +static struct inode * create(char *path, short type, short major, short minor) { struct inode *ip, *dp; - char name[DIRSIZ]; + char name[DIRSIZ]; if((dp = nameiparent(path, name)) == 0) return 0; ilock(dp); - if((ip = dirlookup(dp, name, 0)) != 0){ + if((ip = dirlookup(dp, name, 0)) != 0) { iunlockput(dp); ilock(ip); if(type == T_FILE && (ip->type == T_FILE || ip->type == T_DEVICE)) @@ -262,7 +262,7 @@ create(char *path, short type, short major, short minor) return 0; } - if((ip = ialloc(dp->dev, type)) == 0){ + if((ip = ialloc(dp->dev, type)) == 0) { iunlockput(dp); return 0; } @@ -273,7 +273,7 @@ create(char *path, short type, short major, short minor) ip->nlink = 1; iupdate(ip); - if(type == T_DIR){ // Create . and .. entries. + if(type == T_DIR) { // Create . and .. entries. // No ip->nlink++ for ".": avoid cyclic ref count. if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0) goto fail; @@ -282,9 +282,9 @@ create(char *path, short type, short major, short minor) if(dirlink(dp, name, ip->inum) < 0) goto fail; - if(type == T_DIR){ + if(type == T_DIR) { // now that success is guaranteed: - dp->nlink++; // for ".." + dp->nlink++; // for ".." iupdate(dp); } @@ -292,7 +292,7 @@ create(char *path, short type, short major, short minor) return ip; - fail: +fail: // something went wrong. de-allocate ip. ip->nlink = 0; iupdate(ip); @@ -304,11 +304,11 @@ create(char *path, short type, short major, short minor) u64 sys_open(void) { - char path[MAXPATH]; - int fd, omode; - struct file *f; + char path[MAXPATH]; + int fd, omode; + struct file *f; struct inode *ip; - int n; + int n; argint(1, &omode); if((n = argstr(0, path, MAXPATH)) < 0) @@ -316,32 +316,32 @@ sys_open(void) begin_op(); - if(omode & O_CREATE){ + if(omode & O_CREATE) { ip = create(path, T_FILE, 0, 0); - if(ip == 0){ + if(ip == 0) { end_op(); return -1; } } else { - if((ip = namei(path)) == 0){ + if((ip = namei(path)) == 0) { end_op(); return -1; } ilock(ip); - if(ip->type == T_DIR && omode != O_RDONLY){ + if(ip->type == T_DIR && omode != O_RDONLY) { iunlockput(ip); end_op(); return -1; } } - if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)){ + if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)) { iunlockput(ip); end_op(); return -1; } - if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){ + if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0) { if(f) fileclose(f); iunlockput(ip); @@ -349,7 +349,7 @@ sys_open(void) return -1; } - if(ip->type == T_DEVICE){ + if(ip->type == T_DEVICE) { f->type = FD_DEVICE; f->major = ip->major; } else { @@ -360,7 +360,7 @@ sys_open(void) f->readable = !(omode & O_WRONLY); f->writable = (omode & O_WRONLY) || (omode & O_RDWR); - if((omode & O_TRUNC) && ip->type == T_FILE){ + if((omode & O_TRUNC) && ip->type == T_FILE) { itrunc(ip); } @@ -373,11 +373,11 @@ sys_open(void) u64 sys_mkdir(void) { - char path[MAXPATH]; + char path[MAXPATH]; struct inode *ip; begin_op(); - if(argstr(0, path, MAXPATH) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){ + if(argstr(0, path, MAXPATH) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0) { end_op(); return -1; } @@ -390,14 +390,13 @@ u64 sys_mknod(void) { struct inode *ip; - char path[MAXPATH]; - int major, minor; + char path[MAXPATH]; + int major, minor; begin_op(); argint(1, &major); argint(2, &minor); - if((argstr(0, path, MAXPATH)) < 0 || - (ip = create(path, T_DEVICE, major, minor)) == 0){ + if((argstr(0, path, MAXPATH)) < 0 || (ip = create(path, T_DEVICE, major, minor)) == 0) { end_op(); return -1; } @@ -409,17 +408,17 @@ sys_mknod(void) u64 sys_chdir(void) { - char path[MAXPATH]; + char path[MAXPATH]; struct inode *ip; - struct proc *p = myproc(); - + struct proc *p = myproc(); + begin_op(); - if(argstr(0, path, MAXPATH) < 0 || (ip = namei(path)) == 0){ + if(argstr(0, path, MAXPATH) < 0 || (ip = namei(path)) == 0) { end_op(); return -1; } ilock(ip); - if(ip->type != T_DIR){ + if(ip->type != T_DIR) { iunlockput(ip); end_op(); return -1; @@ -435,22 +434,22 @@ u64 sys_exec(void) { char path[MAXPATH], *argv[MAXARG]; - int i; - u64 uargv, uarg; + int i; + u64 uargv, uarg; argaddr(1, &uargv); if(argstr(0, path, MAXPATH) < 0) { return -1; } memset(argv, 0, sizeof(argv)); - for(i=0;; i++){ - if(i >= NELEM(argv)){ + for(i = 0;; i++) { + if(i >= NELEM(argv)) { goto bad; } - if(fetchaddr(uargv+sizeof(u64)*i, (u64*)&uarg) < 0){ + if(fetchaddr(uargv + sizeof(u64) * i, (u64 *)&uarg) < 0) { goto bad; } - if(uarg == 0){ + if(uarg == 0) { argv[i] = 0; break; } @@ -468,7 +467,7 @@ sys_exec(void) return ret; - bad: +bad: for(i = 0; i < NELEM(argv) && argv[i] != 0; i++) kfree(argv[i]); return -1; @@ -477,24 +476,24 @@ sys_exec(void) u64 sys_pipe(void) { - u64 fdarray; // user pointer to array of two integers + u64 fdarray; // user pointer to array of two integers struct file *rf, *wf; - int fd0, fd1; + int fd0, fd1; struct proc *p = myproc(); argaddr(0, &fdarray); if(pipealloc(&rf, &wf) < 0) return -1; fd0 = -1; - if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){ + if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0) { if(fd0 >= 0) p->ofile[fd0] = 0; fileclose(rf); fileclose(wf); return -1; } - if(copyout(p->pagetable, fdarray, (char*)&fd0, sizeof(fd0)) < 0 || - copyout(p->pagetable, fdarray+sizeof(fd0), (char *)&fd1, sizeof(fd1)) < 0){ + if(copyout(p->pagetable, fdarray, (char *)&fd0, sizeof(fd0)) < 0 + || copyout(p->pagetable, fdarray + sizeof(fd0), (char *)&fd1, sizeof(fd1)) < 0) { p->ofile[fd0] = 0; p->ofile[fd1] = 0; fileclose(rf); diff --git a/kernel/sysproc.c b/kernel/sysproc.c index d4d8dd6..4274a9d 100644 --- a/kernel/sysproc.c +++ b/kernel/sysproc.c @@ -12,7 +12,7 @@ sys_exit(void) int n; argint(0, &n); exit(n); - return 0; // not reached + return 0; // not reached } u64 @@ -57,8 +57,8 @@ sys_sleep(void) argint(0, &n); acquire(&tickslock); ticks0 = ticks; - while(ticks - ticks0 < n){ - if(killed(myproc())){ + while(ticks - ticks0 < n) { + if(killed(myproc())) { release(&tickslock); return -1; } diff --git a/kernel/trap.c b/kernel/trap.c index 8b7f831..359fe39 100644 --- a/kernel/trap.c +++ b/kernel/trap.c @@ -7,7 +7,7 @@ #include "defs.h" struct spinlock tickslock; -u32 ticks; +u32 ticks; extern char trampoline[], uservec[], userret[]; @@ -46,11 +46,11 @@ usertrap(void) w_stvec((u64)kernelvec); struct proc *p = myproc(); - + // save user program counter. p->trapframe->epc = r_sepc(); - - if(r_scause() == 8){ + + if(r_scause() == 8) { // system call if(killed(p)) @@ -65,7 +65,7 @@ usertrap(void) intr_on(); syscall(); - } else if((which_dev = devintr()) != 0){ + } else if((which_dev = devintr()) != 0) { // ok } else { printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid); @@ -105,11 +105,11 @@ usertrapret(void) p->trapframe->kernel_satp = r_satp(); // kernel page table p->trapframe->kernel_sp = p->kstack + PGSIZE; // process's kernel stack p->trapframe->kernel_trap = (u64)usertrap; - p->trapframe->kernel_hartid = r_tp(); // hartid for cpuid() + p->trapframe->kernel_hartid = r_tp(); // hartid for cpuid() // set up the registers that trampoline.S's sret will use // to get to user space. - + // set S Previous Privilege mode to User. unsigned long x = r_sstatus(); x &= ~SSTATUS_SPP; // clear SPP to 0 for user mode @@ -122,7 +122,7 @@ usertrapret(void) // tell trampoline.S the user page table to switch to. u64 satp = MAKE_SATP(p->pagetable); - // jump to userret in trampoline.S at the top of memory, which + // jump to userret in trampoline.S at the top of memory, which // switches to the user page table, restores user registers, // and switches to user mode with sret. u64 trampoline_userret = TRAMPOLINE + (userret - trampoline); @@ -131,20 +131,20 @@ usertrapret(void) // interrupts and exceptions from kernel code go here via kernelvec, // on whatever the current kernel stack is. -void +void kerneltrap() { int which_dev = 0; u64 sepc = r_sepc(); u64 sstatus = r_sstatus(); u64 scause = r_scause(); - + if((sstatus & SSTATUS_SPP) == 0) panic("kerneltrap: not from supervisor mode"); if(intr_get() != 0) panic("kerneltrap: interrupts enabled"); - if((which_dev = devintr()) == 0){ + if((which_dev = devintr()) == 0) { printf("scause %p\n", scause); printf("sepc=%p stval=%p\n", r_sepc(), r_stval()); panic("kerneltrap"); @@ -179,18 +179,17 @@ devintr() { u64 scause = r_scause(); - if((scause & 0x8000000000000000L) && - (scause & 0xff) == 9){ + if((scause & 0x8000000000000000L) && (scause & 0xff) == 9) { // this is a supervisor external interrupt, via PLIC. // irq indicates which device interrupted. int irq = plic_claim(); - if(irq == UART0_IRQ){ + if(irq == UART0_IRQ) { uartintr(); - } else if(irq == VIRTIO0_IRQ){ + } else if(irq == VIRTIO0_IRQ) { virtio_disk_intr(); - } else if(irq){ + } else if(irq) { printf("unexpected interrupt irq=%d\n", irq); } @@ -201,14 +200,14 @@ devintr() plic_complete(irq); return 1; - } else if(scause == 0x8000000000000001L){ + } else if(scause == 0x8000000000000001L) { // software interrupt from a machine-mode timer interrupt, // forwarded by timervec in kernelvec.S. - if(cpuid() == 0){ + if(cpuid() == 0) { clockintr(); } - + // acknowledge the software interrupt by clearing // the SSIP bit in sip. w_sip(r_sip() & ~2); @@ -218,4 +217,3 @@ devintr() return 0; } } - diff --git a/kernel/types.h b/kernel/types.h index bae5799..6626c5e 100644 --- a/kernel/types.h +++ b/kernel/types.h @@ -1,6 +1,6 @@ -typedef unsigned char u8; +typedef unsigned char u8; typedef unsigned short u16; -typedef unsigned int u32; -typedef unsigned long u64; +typedef unsigned int u32; +typedef unsigned long u64; typedef u64 pde_t; diff --git a/kernel/uart.c b/kernel/uart.c index 8154a3f..9409002 100644 --- a/kernel/uart.c +++ b/kernel/uart.c @@ -19,31 +19,31 @@ // some have different meanings for // read vs write. // see http://byterunner.com/16550.html -#define RHR 0 // receive holding register (for input bytes) -#define THR 0 // transmit holding register (for output bytes) -#define IER 1 // interrupt enable register -#define IER_RX_ENABLE (1<<0) -#define IER_TX_ENABLE (1<<1) -#define FCR 2 // FIFO control register -#define FCR_FIFO_ENABLE (1<<0) -#define FCR_FIFO_CLEAR (3<<1) // clear the content of the two FIFOs -#define ISR 2 // interrupt status register -#define LCR 3 // line control register -#define LCR_EIGHT_BITS (3<<0) -#define LCR_BAUD_LATCH (1<<7) // special mode to set baud rate -#define LSR 5 // line status register -#define LSR_RX_READY (1<<0) // input is waiting to be read from RHR -#define LSR_TX_IDLE (1<<5) // THR can accept another character to send +#define RHR 0 // receive holding register (for input bytes) +#define THR 0 // transmit holding register (for output bytes) +#define IER 1 // interrupt enable register +#define IER_RX_ENABLE (1 << 0) +#define IER_TX_ENABLE (1 << 1) +#define FCR 2 // FIFO control register +#define FCR_FIFO_ENABLE (1 << 0) +#define FCR_FIFO_CLEAR (3 << 1) // clear the content of the two FIFOs +#define ISR 2 // interrupt status register +#define LCR 3 // line control register +#define LCR_EIGHT_BITS (3 << 0) +#define LCR_BAUD_LATCH (1 << 7) // special mode to set baud rate +#define LSR 5 // line status register +#define LSR_RX_READY (1 << 0) // input is waiting to be read from RHR +#define LSR_TX_IDLE (1 << 5) // THR can accept another character to send -#define ReadReg(reg) (*(Reg(reg))) +#define ReadReg(reg) (*(Reg(reg))) #define WriteReg(reg, v) (*(Reg(reg)) = (v)) // the transmit output buffer. struct spinlock uart_tx_lock; #define UART_TX_BUF_SIZE 32 char uart_tx_buf[UART_TX_BUF_SIZE]; -u64 uart_tx_w; // write next to uart_tx_buf[uart_tx_w % UART_TX_BUF_SIZE] -u64 uart_tx_r; // read next from uart_tx_buf[uart_tx_r % UART_TX_BUF_SIZE] +u64 uart_tx_w; // write next to uart_tx_buf[uart_tx_w % UART_TX_BUF_SIZE] +u64 uart_tx_r; // read next from uart_tx_buf[uart_tx_r % UART_TX_BUF_SIZE] extern volatile int panicked; // from printf.c @@ -88,11 +88,11 @@ uartputc(int c) { acquire(&uart_tx_lock); - if(panicked){ + if(panicked) { for(;;) ; } - while(uart_tx_w == uart_tx_r + UART_TX_BUF_SIZE){ + while(uart_tx_w == uart_tx_r + UART_TX_BUF_SIZE) { // buffer is full. // wait for uartstart() to open up space in the buffer. sleep(&uart_tx_r, &uart_tx_lock); @@ -103,8 +103,7 @@ uartputc(int c) release(&uart_tx_lock); } - -// alternate version of uartputc() that doesn't +// alternate version of uartputc() that doesn't // use interrupts, for use by kernel printf() and // to echo characters. it spins waiting for the uart's // output register to be empty. @@ -113,7 +112,7 @@ uartputc_sync(int c) { push_off(); - if(panicked){ + if(panicked) { for(;;) ; } @@ -133,25 +132,25 @@ uartputc_sync(int c) void uartstart() { - while(1){ - if(uart_tx_w == uart_tx_r){ + while(1) { + if(uart_tx_w == uart_tx_r) { // transmit buffer is empty. return; } - - if((ReadReg(LSR) & LSR_TX_IDLE) == 0){ + + if((ReadReg(LSR) & LSR_TX_IDLE) == 0) { // the UART transmit holding register is full, // so we cannot give it another byte. // it will interrupt when it's ready for a new byte. return; } - + int c = uart_tx_buf[uart_tx_r % UART_TX_BUF_SIZE]; uart_tx_r += 1; - + // maybe uartputc() is waiting for space in the buffer. wakeup(&uart_tx_r); - + WriteReg(THR, c); } } @@ -161,7 +160,7 @@ uartstart() int uartgetc(void) { - if(ReadReg(LSR) & 0x01){ + if(ReadReg(LSR) & 0x01) { // input data is ready. return ReadReg(RHR); } else { @@ -176,7 +175,7 @@ void uartintr(void) { // read and process incoming characters. - while(1){ + while(1) { int c = uartgetc(); if(c == -1) break; diff --git a/kernel/virtio.h b/kernel/virtio.h index bee95a3..8916e8a 100644 --- a/kernel/virtio.h +++ b/kernel/virtio.h @@ -9,38 +9,38 @@ // virtio mmio control registers, mapped starting at 0x10001000. // from qemu virtio_mmio.h -#define VIRTIO_MMIO_MAGIC_VALUE 0x000 // 0x74726976 -#define VIRTIO_MMIO_VERSION 0x004 // version; should be 2 -#define VIRTIO_MMIO_DEVICE_ID 0x008 // device type; 1 is net, 2 is disk -#define VIRTIO_MMIO_VENDOR_ID 0x00c // 0x554d4551 -#define VIRTIO_MMIO_DEVICE_FEATURES 0x010 -#define VIRTIO_MMIO_DRIVER_FEATURES 0x020 -#define VIRTIO_MMIO_QUEUE_SEL 0x030 // select queue, write-only -#define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 // max size of current queue, read-only -#define VIRTIO_MMIO_QUEUE_NUM 0x038 // size of current queue, write-only -#define VIRTIO_MMIO_QUEUE_READY 0x044 // ready bit -#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 // write-only -#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 // read-only -#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 // write-only -#define VIRTIO_MMIO_STATUS 0x070 // read/write -#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 // physical address for descriptor table, write-only -#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 -#define VIRTIO_MMIO_DRIVER_DESC_LOW 0x090 // physical address for available ring, write-only -#define VIRTIO_MMIO_DRIVER_DESC_HIGH 0x094 -#define VIRTIO_MMIO_DEVICE_DESC_LOW 0x0a0 // physical address for used ring, write-only -#define VIRTIO_MMIO_DEVICE_DESC_HIGH 0x0a4 +#define VIRTIO_MMIO_MAGIC_VALUE 0x000 // 0x74726976 +#define VIRTIO_MMIO_VERSION 0x004 // version; should be 2 +#define VIRTIO_MMIO_DEVICE_ID 0x008 // device type; 1 is net, 2 is disk +#define VIRTIO_MMIO_VENDOR_ID 0x00c // 0x554d4551 +#define VIRTIO_MMIO_DEVICE_FEATURES 0x010 +#define VIRTIO_MMIO_DRIVER_FEATURES 0x020 +#define VIRTIO_MMIO_QUEUE_SEL 0x030 // select queue, write-only +#define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 // max size of current queue, read-only +#define VIRTIO_MMIO_QUEUE_NUM 0x038 // size of current queue, write-only +#define VIRTIO_MMIO_QUEUE_READY 0x044 // ready bit +#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 // write-only +#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 // read-only +#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 // write-only +#define VIRTIO_MMIO_STATUS 0x070 // read/write +#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 // physical address for descriptor table, write-only +#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 +#define VIRTIO_MMIO_DRIVER_DESC_LOW 0x090 // physical address for available ring, write-only +#define VIRTIO_MMIO_DRIVER_DESC_HIGH 0x094 +#define VIRTIO_MMIO_DEVICE_DESC_LOW 0x0a0 // physical address for used ring, write-only +#define VIRTIO_MMIO_DEVICE_DESC_HIGH 0x0a4 // status register bits, from qemu virtio_config.h -#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 -#define VIRTIO_CONFIG_S_DRIVER 2 -#define VIRTIO_CONFIG_S_DRIVER_OK 4 -#define VIRTIO_CONFIG_S_FEATURES_OK 8 +#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 +#define VIRTIO_CONFIG_S_DRIVER 2 +#define VIRTIO_CONFIG_S_DRIVER_OK 4 +#define VIRTIO_CONFIG_S_FEATURES_OK 8 // device feature bits -#define VIRTIO_BLK_F_RO 5 /* Disk is read-only */ -#define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */ -#define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */ -#define VIRTIO_BLK_F_MQ 12 /* support more than one vq */ +#define VIRTIO_BLK_F_RO 5 /* Disk is read-only */ +#define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */ +#define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */ +#define VIRTIO_BLK_F_MQ 12 /* support more than one vq */ #define VIRTIO_F_ANY_LAYOUT 27 #define VIRTIO_RING_F_INDIRECT_DESC 28 #define VIRTIO_RING_F_EVENT_IDX 29 @@ -61,8 +61,8 @@ struct virtq_desc { // the (entire) avail ring, from the spec. struct virtq_avail { - u16 flags; // always zero - u16 idx; // driver will write ring[idx] next + u16 flags; // always zero + u16 idx; // driver will write ring[idx] next u16 ring[NUM]; // descriptor numbers of chain heads u16 unused; }; @@ -70,13 +70,13 @@ struct virtq_avail { // one entry in the "used" ring, with which the // device tells the driver about completed requests. struct virtq_used_elem { - u32 id; // index of start of completed descriptor chain + u32 id; // index of start of completed descriptor chain u32 len; }; struct virtq_used { - u16 flags; // always zero - u16 idx; // device increments when it adds a ring[] entry + u16 flags; // always zero + u16 idx; // device increments when it adds a ring[] entry struct virtq_used_elem ring[NUM]; }; diff --git a/kernel/virtio_disk.c b/kernel/virtio_disk.c index aabd378..c8bce8e 100644 --- a/kernel/virtio_disk.c +++ b/kernel/virtio_disk.c @@ -39,23 +39,23 @@ static struct disk { struct virtq_used *used; // our own book-keeping. - char free[NUM]; // is a descriptor free? - u16 used_idx; // we've looked this far in used[2..NUM]. + char free[NUM]; // is a descriptor free? + u16 used_idx; // we've looked this far in used[2..NUM]. // track info about in-flight operations, // for use when completion interrupt arrives. // indexed by first descriptor index of chain. struct { struct buf *b; - char status; + char status; } info[NUM]; // disk command headers. // one-for-one with descriptors, for convenience. struct virtio_blk_req ops[NUM]; - + struct spinlock vdisk_lock; - + } disk; void @@ -65,13 +65,11 @@ virtio_disk_init(void) initlock(&disk.vdisk_lock, "virtio_disk"); - if(*R(VIRTIO_MMIO_MAGIC_VALUE) != 0x74726976 || - *R(VIRTIO_MMIO_VERSION) != 2 || - *R(VIRTIO_MMIO_DEVICE_ID) != 2 || - *R(VIRTIO_MMIO_VENDOR_ID) != 0x554d4551){ + if(*R(VIRTIO_MMIO_MAGIC_VALUE) != 0x74726976 || *R(VIRTIO_MMIO_VERSION) != 2 || *R(VIRTIO_MMIO_DEVICE_ID) != 2 + || *R(VIRTIO_MMIO_VENDOR_ID) != 0x554d4551) { panic("could not find virtio disk"); } - + // reset device *R(VIRTIO_MMIO_STATUS) = status; @@ -156,8 +154,8 @@ virtio_disk_init(void) static int alloc_desc() { - for(int i = 0; i < NUM; i++){ - if(disk.free[i]){ + for(int i = 0; i < NUM; i++) { + if(disk.free[i]) { disk.free[i] = 0; return i; } @@ -185,7 +183,7 @@ free_desc(int i) static void free_chain(int i) { - while(1){ + while(1) { int flag = disk.desc[i].flags; int nxt = disk.desc[i].next; free_desc(i); @@ -201,9 +199,9 @@ free_chain(int i) static int alloc3_desc(int *idx) { - for(int i = 0; i < 3; i++){ + for(int i = 0; i < 3; i++) { idx[i] = alloc_desc(); - if(idx[i] < 0){ + if(idx[i] < 0) { for(int j = 0; j < i; j++) free_desc(idx[j]); return -1; @@ -225,7 +223,7 @@ virtio_disk_rw(struct buf *b, int write) // allocate the three descriptors. int idx[3]; - while(1){ + while(1) { if(alloc3_desc(idx) == 0) { break; } @@ -244,12 +242,12 @@ virtio_disk_rw(struct buf *b, int write) buf0->reserved = 0; buf0->sector = sector; - disk.desc[idx[0]].addr = (u64) buf0; + disk.desc[idx[0]].addr = (u64)buf0; disk.desc[idx[0]].len = sizeof(struct virtio_blk_req); disk.desc[idx[0]].flags = VRING_DESC_F_NEXT; disk.desc[idx[0]].next = idx[1]; - disk.desc[idx[1]].addr = (u64) b->data; + disk.desc[idx[1]].addr = (u64)b->data; disk.desc[idx[1]].len = BSIZE; if(write) disk.desc[idx[1]].flags = 0; // device reads b->data @@ -259,7 +257,7 @@ virtio_disk_rw(struct buf *b, int write) disk.desc[idx[1]].next = idx[2]; disk.info[idx[0]].status = 0xff; // device writes 0 on success - disk.desc[idx[2]].addr = (u64) &disk.info[idx[0]].status; + disk.desc[idx[2]].addr = (u64)&disk.info[idx[0]].status; disk.desc[idx[2]].len = 1; disk.desc[idx[2]].flags = VRING_DESC_F_WRITE; // device writes the status disk.desc[idx[2]].next = 0; @@ -309,7 +307,7 @@ virtio_disk_intr() // the device increments disk.used->idx when it // adds an entry to the used ring. - while(disk.used_idx != disk.used->idx){ + while(disk.used_idx != disk.used->idx) { __sync_synchronize(); int id = disk.used->ring[disk.used_idx % NUM].id; @@ -317,7 +315,7 @@ virtio_disk_intr() panic("virtio_disk_intr status"); struct buf *b = disk.info[id].b; - b->disk = 0; // disk is done with buf + b->disk = 0; // disk is done with buf wakeup(b); disk.used_idx += 1; diff --git a/kernel/vm.c b/kernel/vm.c index ad7add9..e68db67 100644 --- a/kernel/vm.c +++ b/kernel/vm.c @@ -11,7 +11,7 @@ */ pagetable_t kernel_pagetable; -extern char etext[]; // kernel.ld sets this to end of kernel code. +extern char etext[]; // kernel.ld sets this to end of kernel code. extern char trampoline[]; // trampoline.S @@ -21,7 +21,7 @@ kvmmake(void) { pagetable_t kpgtbl; - kpgtbl = (pagetable_t) kalloc(); + kpgtbl = (pagetable_t)kalloc(); memset(kpgtbl, 0, PGSIZE); // uart registers @@ -34,10 +34,10 @@ kvmmake(void) kvmmap(kpgtbl, PLIC, PLIC, 0x400000, PTE_R | PTE_W); // map kernel text executable and read-only. - kvmmap(kpgtbl, KERNBASE, KERNBASE, (u64)etext-KERNBASE, PTE_R | PTE_X); + kvmmap(kpgtbl, KERNBASE, KERNBASE, (u64)etext - KERNBASE, PTE_R | PTE_X); // map kernel data and the physical RAM we'll make use of. - kvmmap(kpgtbl, (u64)etext, (u64)etext, PHYSTOP-(u64)etext, PTE_R | PTE_W); + kvmmap(kpgtbl, (u64)etext, (u64)etext, PHYSTOP - (u64)etext, PTE_R | PTE_W); // map the trampoline for trap entry/exit to // the highest virtual address in the kernel. @@ -45,7 +45,7 @@ kvmmake(void) // allocate and map a kernel stack for each process. proc_mapstacks(kpgtbl); - + return kpgtbl; } @@ -93,7 +93,7 @@ walk(pagetable_t pagetable, u64 va, int alloc) if(*pte & PTE_V) { pagetable = (pagetable_t)PTE2PA(*pte); } else { - if(!alloc || (pagetable = (pde_t*)kalloc()) == 0) + if(!alloc || (pagetable = (pde_t *)kalloc()) == 0) return 0; memset(pagetable, 0, PGSIZE); *pte = PA2PTE(pagetable) | PTE_V; @@ -109,7 +109,7 @@ u64 walkaddr(pagetable_t pagetable, u64 va) { pte_t *pte; - u64 pa; + u64 pa; if(va >= MAXVA) return 0; @@ -142,15 +142,15 @@ kvmmap(pagetable_t kpgtbl, u64 va, u64 pa, u64 sz, int perm) int mappages(pagetable_t pagetable, u64 va, u64 size, u64 pa, int perm) { - u64 a, last; + u64 a, last; pte_t *pte; if(size == 0) panic("mappages: size"); - + a = PGROUNDDOWN(va); last = PGROUNDDOWN(va + size - 1); - for(;;){ + for(;;) { if((pte = walk(pagetable, a, 1)) == 0) return -1; if(*pte & PTE_V) @@ -170,22 +170,22 @@ mappages(pagetable_t pagetable, u64 va, u64 size, u64 pa, int perm) void uvmunmap(pagetable_t pagetable, u64 va, u64 npages, int do_free) { - u64 a; + u64 a; pte_t *pte; if((va % PGSIZE) != 0) panic("uvmunmap: not aligned"); - for(a = va; a < va + npages*PGSIZE; a += PGSIZE){ + for(a = va; a < va + npages * PGSIZE; a += PGSIZE) { if((pte = walk(pagetable, a, 0)) == 0) panic("uvmunmap: walk"); if((*pte & PTE_V) == 0) panic("uvmunmap: not mapped"); if(PTE_FLAGS(*pte) == PTE_V) panic("uvmunmap: not a leaf"); - if(do_free){ + if(do_free) { u64 pa = PTE2PA(*pte); - kfree((void*)pa); + kfree((void *)pa); } *pte = 0; } @@ -197,7 +197,7 @@ pagetable_t uvmcreate() { pagetable_t pagetable; - pagetable = (pagetable_t) kalloc(); + pagetable = (pagetable_t)kalloc(); if(pagetable == 0) return 0; memset(pagetable, 0, PGSIZE); @@ -216,7 +216,7 @@ uvmfirst(pagetable_t pagetable, u8 *src, u32 sz) panic("uvmfirst: more than a page"); mem = kalloc(); memset(mem, 0, PGSIZE); - mappages(pagetable, 0, PGSIZE, (u64)mem, PTE_W|PTE_R|PTE_X|PTE_U); + mappages(pagetable, 0, PGSIZE, (u64)mem, PTE_W | PTE_R | PTE_X | PTE_U); memmove(mem, src, sz); } @@ -226,20 +226,20 @@ u64 uvmalloc(pagetable_t pagetable, u64 oldsz, u64 newsz, int xperm) { char *mem; - u64 a; + u64 a; if(newsz < oldsz) return oldsz; oldsz = PGROUNDUP(oldsz); - for(a = oldsz; a < newsz; a += PGSIZE){ + for(a = oldsz; a < newsz; a += PGSIZE) { mem = kalloc(); - if(mem == 0){ + if(mem == 0) { uvmdealloc(pagetable, a, oldsz); return 0; } memset(mem, 0, PGSIZE); - if(mappages(pagetable, a, PGSIZE, (u64)mem, PTE_R|PTE_U|xperm) != 0){ + if(mappages(pagetable, a, PGSIZE, (u64)mem, PTE_R | PTE_U | xperm) != 0) { kfree(mem); uvmdealloc(pagetable, a, oldsz); return 0; @@ -258,7 +258,7 @@ uvmdealloc(pagetable_t pagetable, u64 oldsz, u64 newsz) if(newsz >= oldsz) return oldsz; - if(PGROUNDUP(newsz) < PGROUNDUP(oldsz)){ + if(PGROUNDUP(newsz) < PGROUNDUP(oldsz)) { int npages = (PGROUNDUP(oldsz) - PGROUNDUP(newsz)) / PGSIZE; uvmunmap(pagetable, PGROUNDUP(newsz), npages, 1); } @@ -272,18 +272,18 @@ void freewalk(pagetable_t pagetable) { // there are 2^9 = 512 PTEs in a page table. - for(int i = 0; i < 512; i++){ + for(int i = 0; i < 512; i++) { pte_t pte = pagetable[i]; - if((pte & PTE_V) && (pte & (PTE_R|PTE_W|PTE_X)) == 0){ + if((pte & PTE_V) && (pte & (PTE_R | PTE_W | PTE_X)) == 0) { // this PTE points to a lower-level page table. u64 child = PTE2PA(pte); freewalk((pagetable_t)child); pagetable[i] = 0; - } else if(pte & PTE_V){ + } else if(pte & PTE_V) { panic("freewalk: leaf"); } } - kfree((void*)pagetable); + kfree((void *)pagetable); } // Free user memory pages, @@ -292,7 +292,7 @@ void uvmfree(pagetable_t pagetable, u64 sz) { if(sz > 0) - uvmunmap(pagetable, 0, PGROUNDUP(sz)/PGSIZE, 1); + uvmunmap(pagetable, 0, PGROUNDUP(sz) / PGSIZE, 1); freewalk(pagetable); } @@ -306,11 +306,11 @@ int uvmcopy(pagetable_t old, pagetable_t new, u64 sz) { pte_t *pte; - u64 pa, i; - u32 flags; - char *mem; + u64 pa, i; + u32 flags; + char *mem; - for(i = 0; i < sz; i += PGSIZE){ + for(i = 0; i < sz; i += PGSIZE) { if((pte = walk(old, i, 0)) == 0) panic("uvmcopy: pte should exist"); if((*pte & PTE_V) == 0) @@ -319,15 +319,15 @@ uvmcopy(pagetable_t old, pagetable_t new, u64 sz) flags = PTE_FLAGS(*pte); if((mem = kalloc()) == 0) goto err; - memmove(mem, (char*)pa, PGSIZE); - if(mappages(new, i, PGSIZE, (u64)mem, flags) != 0){ + memmove(mem, (char *)pa, PGSIZE); + if(mappages(new, i, PGSIZE, (u64)mem, flags) != 0) { kfree(mem); goto err; } } return 0; - err: +err: uvmunmap(new, 0, i / PGSIZE, 1); return -1; } @@ -338,7 +338,7 @@ void uvmclear(pagetable_t pagetable, u64 va) { pte_t *pte; - + pte = walk(pagetable, va, 0); if(pte == 0) panic("uvmclear"); @@ -353,7 +353,7 @@ copyout(pagetable_t pagetable, u64 dstva, char *src, u64 len) { u64 n, va0, pa0; - while(len > 0){ + while(len > 0) { va0 = PGROUNDDOWN(dstva); pa0 = walkaddr(pagetable, va0); if(pa0 == 0) @@ -378,7 +378,7 @@ copyin(pagetable_t pagetable, char *dst, u64 srcva, u64 len) { u64 n, va0, pa0; - while(len > 0){ + while(len > 0) { va0 = PGROUNDDOWN(srcva); pa0 = walkaddr(pagetable, va0); if(pa0 == 0) @@ -405,7 +405,7 @@ copyinstr(pagetable_t pagetable, char *dst, u64 srcva, u64 max) u64 n, va0, pa0; int got_null = 0; - while(got_null == 0 && max > 0){ + while(got_null == 0 && max > 0) { va0 = PGROUNDDOWN(srcva); pa0 = walkaddr(pagetable, va0); if(pa0 == 0) @@ -414,9 +414,9 @@ copyinstr(pagetable_t pagetable, char *dst, u64 srcva, u64 max) if(n > max) n = max; - char *p = (char *) (pa0 + (srcva - va0)); - while(n > 0){ - if(*p == '\0'){ + char *p = (char *)(pa0 + (srcva - va0)); + while(n > 0) { + if(*p == '\0') { *dst = '\0'; got_null = 1; break; @@ -431,7 +431,7 @@ copyinstr(pagetable_t pagetable, char *dst, u64 srcva, u64 max) srcva = va0 + PGSIZE; } - if(got_null){ + if(got_null) { return 0; } else { return -1; diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index 7260e42..1853023 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -5,14 +5,19 @@ #include #include -#define stat xv6_stat // avoid clash with host struct stat +#define stat xv6_stat // avoid clash with host struct stat #include "kernel/types.h" #include "kernel/fs.h" #include "kernel/stat.h" #include "kernel/param.h" #ifndef static_assert -#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0) +#define static_assert(a, b) \ + do { \ + switch(0) \ + case 0: \ + case(a):; \ + } while(0) #endif #define NINODES 200 @@ -20,22 +25,21 @@ // Disk layout: // [ boot block | sb block | log | inode blocks | free bit map | data blocks ] -int nbitmap = FSSIZE/(BSIZE*8) + 1; +int nbitmap = FSSIZE / (BSIZE * 8) + 1; int ninodeblocks = NINODES / IPB + 1; int nlog = LOGSIZE; -int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap) -int nblocks; // Number of data blocks +int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap) +int nblocks; // Number of data blocks -int fsfd; +int fsfd; struct superblock sb; -char zeroes[BSIZE]; -uint freeinode = 1; -uint freeblock; - +char zeroes[BSIZE]; +uint freeinode = 1; +uint freeblock; void balloc(int); -void wsect(uint, void*); -void winode(uint, struct dinode*); +void wsect(uint, void *); +void winode(uint, struct dinode *); void rinode(uint inum, struct dinode *ip); void rsect(uint sec, void *buf); uint ialloc(ushort type); @@ -47,7 +51,7 @@ ushort xshort(ushort x) { ushort y; - u8 *a = (u8*)&y; + u8 *a = (u8 *)&y; a[0] = x; a[1] = x >> 8; return y; @@ -57,7 +61,7 @@ uint xint(uint x) { uint y; - u8 *a = (u8*)&y; + u8 *a = (u8 *)&y; a[0] = x; a[1] = x >> 8; a[2] = x >> 16; @@ -68,16 +72,15 @@ xint(uint x) int main(int argc, char *argv[]) { - int i, cc, fd; - uint rootino, inum, off; + int i, cc, fd; + uint rootino, inum, off; struct dirent de; - char buf[BSIZE]; + char buf[BSIZE]; struct dinode din; - static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); - if(argc < 2){ + if(argc < 2) { fprintf(stderr, "Usage: mkfs fs.img files...\n"); exit(1); } @@ -85,7 +88,7 @@ main(int argc, char *argv[]) assert((BSIZE % sizeof(struct dinode)) == 0); assert((BSIZE % sizeof(struct dirent)) == 0); - fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); + fsfd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666); if(fsfd < 0) die(argv[1]); @@ -99,13 +102,13 @@ main(int argc, char *argv[]) sb.ninodes = xint(NINODES); sb.nlog = xint(nlog); sb.logstart = xint(2); - sb.inodestart = xint(2+nlog); - sb.bmapstart = xint(2+nlog+ninodeblocks); + sb.inodestart = xint(2 + nlog); + sb.bmapstart = xint(2 + nlog + ninodeblocks); - printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n", - nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE); + printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n", nmeta, nlog, + ninodeblocks, nbitmap, nblocks, FSSIZE); - freeblock = nmeta; // the first free block that we can allocate + freeblock = nmeta; // the first free block that we can allocate for(i = 0; i < FSSIZE; i++) wsect(i, zeroes); @@ -127,14 +130,14 @@ main(int argc, char *argv[]) strcpy(de.name, ".."); iappend(rootino, &de, sizeof(de)); - for(i = 2; i < argc; i++){ + for(i = 2; i < argc; i++) { // get rid of "user/" char *shortname; if(strncmp(argv[i], "user/", 5) == 0) shortname = argv[i] + 5; else shortname = argv[i]; - + assert(index(shortname, '/') == 0); if((fd = open(argv[i], 0)) < 0) @@ -163,7 +166,7 @@ main(int argc, char *argv[]) // fix size of root inode dir rinode(rootino, &din); off = xint(din.size); - off = ((off/BSIZE) + 1) * BSIZE; + off = ((off / BSIZE) + 1) * BSIZE; din.size = xint(off); winode(rootino, &din); @@ -184,13 +187,13 @@ wsect(uint sec, void *buf) void winode(uint inum, struct dinode *ip) { - char buf[BSIZE]; - uint bn; + char buf[BSIZE]; + uint bn; struct dinode *dip; bn = IBLOCK(inum, sb); rsect(bn, buf); - dip = ((struct dinode*)buf) + (inum % IPB); + dip = ((struct dinode *)buf) + (inum % IPB); *dip = *ip; wsect(bn, buf); } @@ -198,13 +201,13 @@ winode(uint inum, struct dinode *ip) void rinode(uint inum, struct dinode *ip) { - char buf[BSIZE]; - uint bn; + char buf[BSIZE]; + uint bn; struct dinode *dip; bn = IBLOCK(inum, sb); rsect(bn, buf); - dip = ((struct dinode*)buf) + (inum % IPB); + dip = ((struct dinode *)buf) + (inum % IPB); *ip = *dip; } @@ -220,7 +223,7 @@ rsect(uint sec, void *buf) uint ialloc(ushort type) { - uint inum = freeinode++; + uint inum = freeinode++; struct dinode din; bzero(&din, sizeof(din)); @@ -234,14 +237,14 @@ ialloc(ushort type) void balloc(int used) { - u8 buf[BSIZE]; + u8 buf[BSIZE]; int i; printf("balloc: first %d blocks have been allocated\n", used); - assert(used < BSIZE*8); + assert(used < BSIZE * 8); bzero(buf, BSIZE); - for(i = 0; i < used; i++){ - buf[i/8] = buf[i/8] | (0x1 << (i%8)); + for(i = 0; i < used; i++) { + buf[i / 8] = buf[i / 8] | (0x1 << (i % 8)); } printf("balloc: write bitmap block at sector %d\n", sb.bmapstart); wsect(sb.bmapstart, buf); @@ -252,34 +255,34 @@ balloc(int used) void iappend(uint inum, void *xp, int n) { - char *p = (char*)xp; - uint fbn, off, n1; + char *p = (char *)xp; + uint fbn, off, n1; struct dinode din; - char buf[BSIZE]; - uint indirect[NINDIRECT]; - uint x; + char buf[BSIZE]; + uint indirect[NINDIRECT]; + uint x; rinode(inum, &din); off = xint(din.size); // printf("append inum %d at off %d sz %d\n", inum, off, n); - while(n > 0){ + while(n > 0) { fbn = off / BSIZE; assert(fbn < MAXFILE); - if(fbn < NDIRECT){ - if(xint(din.addrs[fbn]) == 0){ + if(fbn < NDIRECT) { + if(xint(din.addrs[fbn]) == 0) { din.addrs[fbn] = xint(freeblock++); } x = xint(din.addrs[fbn]); } else { - if(xint(din.addrs[NDIRECT]) == 0){ + if(xint(din.addrs[NDIRECT]) == 0) { din.addrs[NDIRECT] = xint(freeblock++); } - rsect(xint(din.addrs[NDIRECT]), (char*)indirect); - if(indirect[fbn - NDIRECT] == 0){ + rsect(xint(din.addrs[NDIRECT]), (char *)indirect); + if(indirect[fbn - NDIRECT] == 0) { indirect[fbn - NDIRECT] = xint(freeblock++); - wsect(xint(din.addrs[NDIRECT]), (char*)indirect); + wsect(xint(din.addrs[NDIRECT]), (char *)indirect); } - x = xint(indirect[fbn-NDIRECT]); + x = xint(indirect[fbn - NDIRECT]); } n1 = min(n, (fbn + 1) * BSIZE - off); rsect(x, buf); diff --git a/user/cat.c b/user/cat.c index 598f005..250c152 100644 --- a/user/cat.c +++ b/user/cat.c @@ -10,12 +10,12 @@ cat(int fd) int n; while((n = read(fd, buf, sizeof(buf))) > 0) { - if (write(1, buf, n) != n) { + if(write(1, buf, n) != n) { fprintf(2, "cat: write error\n"); exit(1); } } - if(n < 0){ + if(n < 0) { fprintf(2, "cat: read error\n"); exit(1); } @@ -26,13 +26,13 @@ main(int argc, char *argv[]) { int fd, i; - if(argc <= 1){ + if(argc <= 1) { cat(0); exit(0); } - for(i = 1; i < argc; i++){ - if((fd = open(argv[i], 0)) < 0){ + for(i = 1; i < argc; i++) { + if((fd = open(argv[i], 0)) < 0) { fprintf(2, "cat: cannot open %s\n", argv[i]); exit(1); } diff --git a/user/echo.c b/user/echo.c index 3f19cd7..696eff5 100644 --- a/user/echo.c +++ b/user/echo.c @@ -7,9 +7,9 @@ main(int argc, char *argv[]) { int i; - for(i = 1; i < argc; i++){ + for(i = 1; i < argc; i++) { write(1, argv[i], strlen(argv[i])); - if(i + 1 < argc){ + if(i + 1 < argc) { write(1, " ", 1); } else { write(1, "\n", 1); diff --git a/user/forktest.c b/user/forktest.c index 384e75f..12ac3ae 100644 --- a/user/forktest.c +++ b/user/forktest.c @@ -5,7 +5,7 @@ #include "kernel/stat.h" #include "user/user.h" -#define N 1000 +#define N 1000 void print(const char *s) @@ -20,7 +20,7 @@ forktest(void) print("fork test\n"); - for(n=0; n 0; n--){ - if(wait(0) < 0){ + for(; n > 0; n--) { + if(wait(0) < 0) { print("wait stopped early\n"); exit(1); } } - if(wait(0) != -1){ + if(wait(0) != -1) { print("wait got too many\n"); exit(1); } diff --git a/user/grep.c b/user/grep.c index 2315a0c..4061494 100644 --- a/user/grep.c +++ b/user/grep.c @@ -5,28 +5,28 @@ #include "user/user.h" char buf[1024]; -int match(char*, char*); +int match(char *, char *); void grep(char *pattern, int fd) { - int n, m; + int n, m; char *p, *q; m = 0; - while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){ + while((n = read(fd, buf + m, sizeof(buf) - m - 1)) > 0) { m += n; buf[m] = '\0'; p = buf; - while((q = strchr(p, '\n')) != 0){ + while((q = strchr(p, '\n')) != 0) { *q = 0; - if(match(pattern, p)){ + if(match(pattern, p)) { *q = '\n'; - write(1, p, q+1 - p); + write(1, p, q + 1 - p); } - p = q+1; + p = q + 1; } - if(m > 0){ + if(m > 0) { m -= p - buf; memmove(buf, p, m); } @@ -36,22 +36,22 @@ grep(char *pattern, int fd) int main(int argc, char *argv[]) { - int fd, i; + int fd, i; char *pattern; - if(argc <= 1){ + if(argc <= 1) { fprintf(2, "usage: grep pattern [file ...]\n"); exit(1); } pattern = argv[1]; - if(argc <= 2){ + if(argc <= 2) { grep(pattern, 0); exit(0); } - for(i = 2; i < argc; i++){ - if((fd = open(argv[i], 0)) < 0){ + for(i = 2; i < argc; i++) { + if((fd = open(argv[i], 0)) < 0) { printf("grep: cannot open %s\n", argv[i]); exit(1); } @@ -65,42 +65,43 @@ main(int argc, char *argv[]) // The Practice of Programming, Chapter 9, or // https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html -int matchhere(char*, char*); -int matchstar(int, char*, char*); +int matchhere(char *, char *); +int matchstar(int, char *, char *); int match(char *re, char *text) { if(re[0] == '^') - return matchhere(re+1, text); - do{ // must look at empty string + return matchhere(re + 1, text); + do { // must look at empty string if(matchhere(re, text)) return 1; - }while(*text++ != '\0'); + } while(*text++ != '\0'); return 0; } // matchhere: search for re at beginning of text -int matchhere(char *re, char *text) +int +matchhere(char *re, char *text) { if(re[0] == '\0') return 1; if(re[1] == '*') - return matchstar(re[0], re+2, text); + return matchstar(re[0], re + 2, text); if(re[0] == '$' && re[1] == '\0') return *text == '\0'; - if(*text!='\0' && (re[0]=='.' || re[0]==*text)) - return matchhere(re+1, text+1); + if(*text != '\0' && (re[0] == '.' || re[0] == *text)) + return matchhere(re + 1, text + 1); return 0; } // matchstar: search for c*re at beginning of text -int matchstar(int c, char *re, char *text) +int +matchstar(int c, char *re, char *text) { - do{ // a * matches zero or more instances + do { // a * matches zero or more instances if(matchhere(re, text)) return 1; - }while(*text!='\0' && (*text++==c || c=='.')); + } while(*text != '\0' && (*text++ == c || c == '.')); return 0; } - diff --git a/user/grind.c b/user/grind.c index 56d3f8d..9e70a05 100644 --- a/user/grind.c +++ b/user/grind.c @@ -16,27 +16,27 @@ int do_rand(unsigned long *ctx) { -/* - * Compute x = (7^5 * x) mod (2^31 - 1) - * without overflowing 31 bits: - * (2^31 - 1) = 127773 * (7^5) + 2836 - * From "Random number generators: good ones are hard to find", - * Park and Miller, Communications of the ACM, vol. 31, no. 10, - * October 1988, p. 1195. - */ - long hi, lo, x; + /* + * Compute x = (7^5 * x) mod (2^31 - 1) + * without overflowing 31 bits: + * (2^31 - 1) = 127773 * (7^5) + 2836 + * From "Random number generators: good ones are hard to find", + * Park and Miller, Communications of the ACM, vol. 31, no. 10, + * October 1988, p. 1195. + */ + long hi, lo, x; - /* Transform to [1, 0x7ffffffe] range. */ - x = (*ctx % 0x7ffffffe) + 1; - hi = x / 127773; - lo = x % 127773; - x = 16807 * lo - 2836 * hi; - if (x < 0) - x += 0x7fffffff; - /* Transform to [0, 0x7ffffffd] range. */ - x--; - *ctx = x; - return (x); + /* Transform to [1, 0x7ffffffe] range. */ + x = (*ctx % 0x7ffffffe) + 1; + hi = x / 127773; + lo = x % 127773; + x = 16807 * lo - 2836 * hi; + if(x < 0) + x += 0x7fffffff; + /* Transform to [0, 0x7ffffffd] range. */ + x--; + *ctx = x; + return (x); } unsigned long rand_next = 1; @@ -44,124 +44,124 @@ unsigned long rand_next = 1; int rand(void) { - return (do_rand(&rand_next)); + return (do_rand(&rand_next)); } void go(int which_child) { - int fd = -1; + int fd = -1; static char buf[999]; - char *break0 = sbrk(0); - u64 iters = 0; + char *break0 = sbrk(0); + u64 iters = 0; mkdir("grindir"); - if(chdir("grindir") != 0){ + if(chdir("grindir") != 0) { printf("grind: chdir grindir failed\n"); exit(1); } chdir("/"); - - while(1){ + + while(1) { iters++; if((iters % 500) == 0) - write(1, which_child?"B":"A", 1); + write(1, which_child ? "B" : "A", 1); int what = rand() % 23; - if(what == 1){ - close(open("grindir/../a", O_CREATE|O_RDWR)); - } else if(what == 2){ - close(open("grindir/../grindir/../b", O_CREATE|O_RDWR)); - } else if(what == 3){ + if(what == 1) { + close(open("grindir/../a", O_CREATE | O_RDWR)); + } else if(what == 2) { + close(open("grindir/../grindir/../b", O_CREATE | O_RDWR)); + } else if(what == 3) { unlink("grindir/../a"); - } else if(what == 4){ - if(chdir("grindir") != 0){ + } else if(what == 4) { + if(chdir("grindir") != 0) { printf("grind: chdir grindir failed\n"); exit(1); } unlink("../b"); chdir("/"); - } else if(what == 5){ + } else if(what == 5) { close(fd); - fd = open("/grindir/../a", O_CREATE|O_RDWR); - } else if(what == 6){ + fd = open("/grindir/../a", O_CREATE | O_RDWR); + } else if(what == 6) { close(fd); - fd = open("/./grindir/./../b", O_CREATE|O_RDWR); - } else if(what == 7){ + fd = open("/./grindir/./../b", O_CREATE | O_RDWR); + } else if(what == 7) { write(fd, buf, sizeof(buf)); - } else if(what == 8){ + } else if(what == 8) { read(fd, buf, sizeof(buf)); - } else if(what == 9){ + } else if(what == 9) { mkdir("grindir/../a"); - close(open("a/../a/./a", O_CREATE|O_RDWR)); + close(open("a/../a/./a", O_CREATE | O_RDWR)); unlink("a/a"); - } else if(what == 10){ + } else if(what == 10) { mkdir("/../b"); - close(open("grindir/../b/b", O_CREATE|O_RDWR)); + close(open("grindir/../b/b", O_CREATE | O_RDWR)); unlink("b/b"); - } else if(what == 11){ + } else if(what == 11) { unlink("b"); link("../grindir/./../a", "../b"); - } else if(what == 12){ + } else if(what == 12) { unlink("../grindir/../a"); link(".././b", "/grindir/../a"); - } else if(what == 13){ + } else if(what == 13) { int pid = fork(); - if(pid == 0){ + if(pid == 0) { exit(0); - } else if(pid < 0){ + } else if(pid < 0) { printf("grind: fork failed\n"); exit(1); } wait(0); - } else if(what == 14){ + } else if(what == 14) { int pid = fork(); - if(pid == 0){ + if(pid == 0) { fork(); fork(); exit(0); - } else if(pid < 0){ + } else if(pid < 0) { printf("grind: fork failed\n"); exit(1); } wait(0); - } else if(what == 15){ + } else if(what == 15) { sbrk(6011); - } else if(what == 16){ + } else if(what == 16) { if(sbrk(0) > break0) sbrk(-(sbrk(0) - break0)); - } else if(what == 17){ + } else if(what == 17) { int pid = fork(); - if(pid == 0){ - close(open("a", O_CREATE|O_RDWR)); + if(pid == 0) { + close(open("a", O_CREATE | O_RDWR)); exit(0); - } else if(pid < 0){ + } else if(pid < 0) { printf("grind: fork failed\n"); exit(1); } - if(chdir("../grindir/..") != 0){ + if(chdir("../grindir/..") != 0) { printf("grind: chdir failed\n"); exit(1); } kill(pid); wait(0); - } else if(what == 18){ + } else if(what == 18) { int pid = fork(); - if(pid == 0){ + if(pid == 0) { kill(getpid()); exit(0); - } else if(pid < 0){ + } else if(pid < 0) { printf("grind: fork failed\n"); exit(1); } wait(0); - } else if(what == 19){ + } else if(what == 19) { int fds[2]; - if(pipe(fds) < 0){ + if(pipe(fds) < 0) { printf("grind: pipe failed\n"); exit(1); } int pid = fork(); - if(pid == 0){ + if(pid == 0) { fork(); fork(); if(write(fds[1], "x", 1) != 1) @@ -170,74 +170,74 @@ go(int which_child) if(read(fds[0], &c, 1) != 1) printf("grind: pipe read failed\n"); exit(0); - } else if(pid < 0){ + } else if(pid < 0) { printf("grind: fork failed\n"); exit(1); } close(fds[0]); close(fds[1]); wait(0); - } else if(what == 20){ + } else if(what == 20) { int pid = fork(); - if(pid == 0){ + if(pid == 0) { unlink("a"); mkdir("a"); chdir("a"); unlink("../a"); - fd = open("x", O_CREATE|O_RDWR); + fd = open("x", O_CREATE | O_RDWR); unlink("x"); exit(0); - } else if(pid < 0){ + } else if(pid < 0) { printf("grind: fork failed\n"); exit(1); } wait(0); - } else if(what == 21){ + } else if(what == 21) { unlink("c"); // should always succeed. check that there are free i-nodes, // file descriptors, blocks. - int fd1 = open("c", O_CREATE|O_RDWR); - if(fd1 < 0){ + int fd1 = open("c", O_CREATE | O_RDWR); + if(fd1 < 0) { printf("grind: create c failed\n"); exit(1); } - if(write(fd1, "x", 1) != 1){ + if(write(fd1, "x", 1) != 1) { printf("grind: write c failed\n"); exit(1); } struct stat st; - if(fstat(fd1, &st) != 0){ + if(fstat(fd1, &st) != 0) { printf("grind: fstat failed\n"); exit(1); } - if(st.size != 1){ + if(st.size != 1) { printf("grind: fstat reports wrong size %d\n", (int)st.size); exit(1); } - if(st.ino > 200){ + if(st.ino > 200) { printf("grind: fstat reports crazy i-number %d\n", st.ino); exit(1); } close(fd1); unlink("c"); - } else if(what == 22){ + } else if(what == 22) { // echo hi | cat int aa[2], bb[2]; - if(pipe(aa) < 0){ + if(pipe(aa) < 0) { fprintf(2, "grind: pipe failed\n"); exit(1); } - if(pipe(bb) < 0){ + if(pipe(bb) < 0) { fprintf(2, "grind: pipe failed\n"); exit(1); } int pid1 = fork(); - if(pid1 == 0){ + if(pid1 == 0) { close(bb[0]); close(bb[1]); close(aa[0]); close(1); - if(dup(aa[1]) != 1){ + if(dup(aa[1]) != 1) { fprintf(2, "grind: dup failed\n"); exit(1); } @@ -246,22 +246,22 @@ go(int which_child) exec("grindir/../echo", args); fprintf(2, "grind: echo: not found\n"); exit(2); - } else if(pid1 < 0){ + } else if(pid1 < 0) { fprintf(2, "grind: fork failed\n"); exit(3); } int pid2 = fork(); - if(pid2 == 0){ + if(pid2 == 0) { close(aa[1]); close(bb[0]); close(0); - if(dup(aa[0]) != 0){ + if(dup(aa[0]) != 0) { fprintf(2, "grind: dup failed\n"); exit(4); } close(aa[0]); close(1); - if(dup(bb[1]) != 1){ + if(dup(bb[1]) != 1) { fprintf(2, "grind: dup failed\n"); exit(5); } @@ -270,7 +270,7 @@ go(int which_child) exec("/cat", args); fprintf(2, "grind: cat: not found\n"); exit(6); - } else if(pid2 < 0){ + } else if(pid2 < 0) { fprintf(2, "grind: fork failed\n"); exit(7); } @@ -278,14 +278,14 @@ go(int which_child) close(aa[1]); close(bb[1]); char buf[4] = { 0, 0, 0, 0 }; - read(bb[0], buf+0, 1); - read(bb[0], buf+1, 1); - read(bb[0], buf+2, 1); + read(bb[0], buf + 0, 1); + read(bb[0], buf + 1, 1); + read(bb[0], buf + 2, 1); close(bb[0]); int st1, st2; wait(&st1); wait(&st2); - if(st1 != 0 || st2 != 0 || strcmp(buf, "hi\n") != 0){ + if(st1 != 0 || st2 != 0 || strcmp(buf, "hi\n") != 0) { printf("grind: exec pipeline failed %d %d \"%s\"\n", st1, st2, buf); exit(1); } @@ -298,24 +298,24 @@ iter() { unlink("a"); unlink("b"); - + int pid1 = fork(); - if(pid1 < 0){ + if(pid1 < 0) { printf("grind: fork failed\n"); exit(1); } - if(pid1 == 0){ + if(pid1 == 0) { rand_next ^= 31; go(0); exit(0); } int pid2 = fork(); - if(pid2 < 0){ + if(pid2 < 0) { printf("grind: fork failed\n"); exit(1); } - if(pid2 == 0){ + if(pid2 == 0) { rand_next ^= 7177; go(1); exit(0); @@ -323,7 +323,7 @@ iter() int st1 = -1; wait(&st1); - if(st1 != 0){ + if(st1 != 0) { kill(pid1); kill(pid2); } @@ -336,13 +336,13 @@ iter() int main() { - while(1){ + while(1) { int pid = fork(); - if(pid == 0){ + if(pid == 0) { iter(); exit(0); } - if(pid > 0){ + if(pid > 0) { wait(0); } sleep(20); diff --git a/user/init.c b/user/init.c index e0a5689..45b6c81 100644 --- a/user/init.c +++ b/user/init.c @@ -16,34 +16,34 @@ main(void) { int pid, wpid; - if(open("console", O_RDWR) < 0){ + if(open("console", O_RDWR) < 0) { mknod("console", CONSOLE, 0); open("console", O_RDWR); } - dup(0); // stdout - dup(0); // stderr + dup(0); // stdout + dup(0); // stderr - for(;;){ + for(;;) { printf("init: starting sh\n"); pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("init: fork failed\n"); exit(1); } - if(pid == 0){ + if(pid == 0) { exec("sh", argv); printf("init: exec sh failed\n"); exit(1); } - for(;;){ + for(;;) { // this call to wait() returns if the shell exits, // or if a parentless process exits. - wpid = wait((int *) 0); - if(wpid == pid){ + wpid = wait((int *)0); + if(wpid == pid) { // the shell exited; restart it. break; - } else if(wpid < 0){ + } else if(wpid < 0) { printf("init: wait returned an error\n"); exit(1); } else { diff --git a/user/kill.c b/user/kill.c index 1b0253b..4953b90 100644 --- a/user/kill.c +++ b/user/kill.c @@ -7,11 +7,11 @@ main(int argc, char **argv) { int i; - if(argc < 2){ + if(argc < 2) { fprintf(2, "usage: kill pid...\n"); exit(1); } - for(i=1; i= path && *p != '/'; p--) + for(p = path + strlen(path); p >= path && *p != '/'; p--) ; p++; @@ -18,49 +18,49 @@ fmtname(char *path) if(strlen(p) >= DIRSIZ) return p; memmove(buf, p, strlen(p)); - memset(buf+strlen(p), ' ', DIRSIZ-strlen(p)); + memset(buf + strlen(p), ' ', DIRSIZ - strlen(p)); return buf; } void ls(char *path) { - char buf[512], *p; - int fd; + char buf[512], *p; + int fd; struct dirent de; - struct stat st; + struct stat st; - if((fd = open(path, 0)) < 0){ + if((fd = open(path, 0)) < 0) { fprintf(2, "ls: cannot open %s\n", path); return; } - if(fstat(fd, &st) < 0){ + if(fstat(fd, &st) < 0) { fprintf(2, "ls: cannot stat %s\n", path); close(fd); return; } - switch(st.type){ + switch(st.type) { case T_DEVICE: case T_FILE: printf("%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size); break; case T_DIR: - if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ + if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) { printf("ls: path too long\n"); break; } strcpy(buf, path); - p = buf+strlen(buf); + p = buf + strlen(buf); *p++ = '/'; - while(read(fd, &de, sizeof(de)) == sizeof(de)){ + while(read(fd, &de, sizeof(de)) == sizeof(de)) { if(de.inum == 0) continue; memmove(p, de.name, DIRSIZ); p[DIRSIZ] = 0; - if(stat(buf, &st) < 0){ + if(stat(buf, &st) < 0) { printf("ls: cannot stat %s\n", buf); continue; } @@ -76,11 +76,11 @@ main(int argc, char *argv[]) { int i; - if(argc < 2){ + if(argc < 2) { ls("."); exit(0); } - for(i=1; i> (sizeof(u64) * 8 - 4)]); } @@ -52,19 +53,19 @@ void vprintf(int fd, const char *fmt, va_list ap) { char *s; - int c, i, state; + int c, i, state; state = 0; - for(i = 0; fmt[i]; i++){ + for(i = 0; fmt[i]; i++) { c = fmt[i] & 0xff; - if(state == 0){ - if(c == '%'){ + if(state == 0) { + if(c == '%') { state = '%'; } else { putc(fd, c); } - } else if(state == '%'){ - if(c == 'd'){ + } else if(state == '%') { + if(c == 'd') { printint(fd, va_arg(ap, int), 10, 1); } else if(c == 'l') { printint(fd, va_arg(ap, u64), 10, 0); @@ -72,17 +73,17 @@ vprintf(int fd, const char *fmt, va_list ap) printint(fd, va_arg(ap, int), 16, 0); } else if(c == 'p') { printptr(fd, va_arg(ap, u64)); - } else if(c == 's'){ - s = va_arg(ap, char*); + } else if(c == 's') { + s = va_arg(ap, char *); if(s == 0) s = "(null)"; - while(*s != 0){ + while(*s != 0) { putc(fd, *s); s++; } - } else if(c == 'c'){ + } else if(c == 'c') { putc(fd, va_arg(ap, u32)); - } else if(c == '%'){ + } else if(c == '%') { putc(fd, c); } else { // Unknown % sequence. Print it to draw attention. diff --git a/user/rm.c b/user/rm.c index 26b8f1f..f389b6c 100644 --- a/user/rm.c +++ b/user/rm.c @@ -7,13 +7,13 @@ main(int argc, char *argv[]) { int i; - if(argc < 2){ + if(argc < 2) { fprintf(2, "Usage: rm files...\n"); exit(1); } - for(i = 1; i < argc; i++){ - if(unlink(argv[i]) < 0){ + for(i = 1; i < argc; i++) { + if(unlink(argv[i]) < 0) { fprintf(2, "rm: %s failed to delete\n", argv[i]); break; } diff --git a/user/sh.c b/user/sh.c index 836ebcb..afc3133 100644 --- a/user/sh.c +++ b/user/sh.c @@ -18,62 +18,62 @@ struct cmd { }; struct execcmd { - int type; + int type; char *argv[MAXARGS]; char *eargv[MAXARGS]; }; struct redircmd { - int type; + int type; struct cmd *cmd; - char *file; - char *efile; - int mode; - int fd; + char *file; + char *efile; + int mode; + int fd; }; struct pipecmd { - int type; + int type; struct cmd *left; struct cmd *right; }; struct listcmd { - int type; + int type; struct cmd *left; struct cmd *right; }; struct backcmd { - int type; + int type; struct cmd *cmd; }; -int fork1(void); // Fork but panics on failure. -void panic(char*); -struct cmd *parsecmd(char*); -void runcmd(struct cmd*) __attribute__((noreturn)); +int fork1(void); // Fork but panics on failure. +void panic(char *); +struct cmd *parsecmd(char *); +void runcmd(struct cmd *) __attribute__((noreturn)); // Execute cmd. Never returns. void runcmd(struct cmd *cmd) { - int p[2]; - struct backcmd *bcmd; - struct execcmd *ecmd; - struct listcmd *lcmd; - struct pipecmd *pcmd; + int p[2]; + struct backcmd *bcmd; + struct execcmd *ecmd; + struct listcmd *lcmd; + struct pipecmd *pcmd; struct redircmd *rcmd; if(cmd == 0) exit(1); - switch(cmd->type){ + switch(cmd->type) { default: panic("runcmd"); case EXEC: - ecmd = (struct execcmd*)cmd; + ecmd = (struct execcmd *)cmd; if(ecmd->argv[0] == 0) exit(1); exec(ecmd->argv[0], ecmd->argv); @@ -81,9 +81,9 @@ runcmd(struct cmd *cmd) break; case REDIR: - rcmd = (struct redircmd*)cmd; + rcmd = (struct redircmd *)cmd; close(rcmd->fd); - if(open(rcmd->file, rcmd->mode) < 0){ + if(open(rcmd->file, rcmd->mode) < 0) { fprintf(2, "open %s failed\n", rcmd->file); exit(1); } @@ -91,7 +91,7 @@ runcmd(struct cmd *cmd) break; case LIST: - lcmd = (struct listcmd*)cmd; + lcmd = (struct listcmd *)cmd; if(fork1() == 0) runcmd(lcmd->left); wait(0); @@ -99,17 +99,17 @@ runcmd(struct cmd *cmd) break; case PIPE: - pcmd = (struct pipecmd*)cmd; + pcmd = (struct pipecmd *)cmd; if(pipe(p) < 0) panic("pipe"); - if(fork1() == 0){ + if(fork1() == 0) { close(1); dup(p[1]); close(p[0]); close(p[1]); runcmd(pcmd->left); } - if(fork1() == 0){ + if(fork1() == 0) { close(0); dup(p[0]); close(p[0]); @@ -123,7 +123,7 @@ runcmd(struct cmd *cmd) break; case BACK: - bcmd = (struct backcmd*)cmd; + bcmd = (struct backcmd *)cmd; if(fork1() == 0) runcmd(bcmd->cmd); break; @@ -146,23 +146,23 @@ int main(void) { static char buf[100]; - int fd; + int fd; // Ensure that three file descriptors are open. - while((fd = open("console", O_RDWR)) >= 0){ - if(fd >= 3){ + while((fd = open("console", O_RDWR)) >= 0) { + if(fd >= 3) { close(fd); break; } } // Read and run input commands. - while(getcmd(buf, sizeof(buf)) >= 0){ - if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){ + while(getcmd(buf, sizeof(buf)) >= 0) { + if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') { // Chdir must be called by the parent, not the child. - buf[strlen(buf)-1] = 0; // chop \n - if(chdir(buf+3) < 0) - fprintf(2, "cannot cd %s\n", buf+3); + buf[strlen(buf) - 1] = 0; // chop \n + if(chdir(buf + 3) < 0) + fprintf(2, "cannot cd %s\n", buf + 3); continue; } if(fork1() == 0) @@ -190,10 +190,10 @@ fork1(void) return pid; } -//PAGEBREAK! -// Constructors +// PAGEBREAK! +// Constructors -struct cmd* +struct cmd * execcmd(void) { struct execcmd *cmd; @@ -201,10 +201,10 @@ execcmd(void) cmd = malloc(sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd)); cmd->type = EXEC; - return (struct cmd*)cmd; + return (struct cmd *)cmd; } -struct cmd* +struct cmd * redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd) { struct redircmd *cmd; @@ -217,10 +217,10 @@ redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd) cmd->efile = efile; cmd->mode = mode; cmd->fd = fd; - return (struct cmd*)cmd; + return (struct cmd *)cmd; } -struct cmd* +struct cmd * pipecmd(struct cmd *left, struct cmd *right) { struct pipecmd *cmd; @@ -230,10 +230,10 @@ pipecmd(struct cmd *left, struct cmd *right) cmd->type = PIPE; cmd->left = left; cmd->right = right; - return (struct cmd*)cmd; + return (struct cmd *)cmd; } -struct cmd* +struct cmd * listcmd(struct cmd *left, struct cmd *right) { struct listcmd *cmd; @@ -243,10 +243,10 @@ listcmd(struct cmd *left, struct cmd *right) cmd->type = LIST; cmd->left = left; cmd->right = right; - return (struct cmd*)cmd; + return (struct cmd *)cmd; } -struct cmd* +struct cmd * backcmd(struct cmd *subcmd) { struct backcmd *cmd; @@ -255,10 +255,10 @@ backcmd(struct cmd *subcmd) memset(cmd, 0, sizeof(*cmd)); cmd->type = BACK; cmd->cmd = subcmd; - return (struct cmd*)cmd; + return (struct cmd *)cmd; } -//PAGEBREAK! -// Parsing +// PAGEBREAK! +// Parsing char whitespace[] = " \t\r\n\v"; char symbols[] = "<|>&;()"; @@ -267,7 +267,7 @@ int gettoken(char **ps, char *es, char **q, char **eq) { char *s; - int ret; + int ret; s = *ps; while(s < es && strchr(whitespace, *s)) @@ -275,7 +275,7 @@ gettoken(char **ps, char *es, char **q, char **eq) if(q) *q = s; ret = *s; - switch(*s){ + switch(*s) { case 0: break; case '|': @@ -288,7 +288,7 @@ gettoken(char **ps, char *es, char **q, char **eq) break; case '>': s++; - if(*s == '>'){ + if(*s == '>') { ret = '+'; s++; } @@ -320,21 +320,21 @@ peek(char **ps, char *es, char *toks) return *s && strchr(toks, *s); } -struct cmd *parseline(char**, char*); -struct cmd *parsepipe(char**, char*); -struct cmd *parseexec(char**, char*); -struct cmd *nulterminate(struct cmd*); +struct cmd *parseline(char **, char *); +struct cmd *parsepipe(char **, char *); +struct cmd *parseexec(char **, char *); +struct cmd *nulterminate(struct cmd *); -struct cmd* +struct cmd * parsecmd(char *s) { - char *es; + char *es; struct cmd *cmd; es = s + strlen(s); cmd = parseline(&s, es); peek(&s, es, ""); - if(s != es){ + if(s != es) { fprintf(2, "leftovers: %s\n", s); panic("syntax"); } @@ -342,62 +342,62 @@ parsecmd(char *s) return cmd; } -struct cmd* +struct cmd * parseline(char **ps, char *es) { struct cmd *cmd; cmd = parsepipe(ps, es); - while(peek(ps, es, "&")){ + while(peek(ps, es, "&")) { gettoken(ps, es, 0, 0); cmd = backcmd(cmd); } - if(peek(ps, es, ";")){ + if(peek(ps, es, ";")) { gettoken(ps, es, 0, 0); cmd = listcmd(cmd, parseline(ps, es)); } return cmd; } -struct cmd* +struct cmd * parsepipe(char **ps, char *es) { struct cmd *cmd; cmd = parseexec(ps, es); - if(peek(ps, es, "|")){ + if(peek(ps, es, "|")) { gettoken(ps, es, 0, 0); cmd = pipecmd(cmd, parsepipe(ps, es)); } return cmd; } -struct cmd* +struct cmd * parseredirs(struct cmd *cmd, char **ps, char *es) { - int tok; + int tok; char *q, *eq; - while(peek(ps, es, "<>")){ + while(peek(ps, es, "<>")) { tok = gettoken(ps, es, 0, 0); if(gettoken(ps, es, &q, &eq) != 'a') panic("missing file for redirection"); - switch(tok){ + switch(tok) { case '<': cmd = redircmd(cmd, q, eq, O_RDONLY, 0); break; case '>': - cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE|O_TRUNC, 1); + cmd = redircmd(cmd, q, eq, O_WRONLY | O_CREATE | O_TRUNC, 1); break; - case '+': // >> - cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE, 1); + case '+': // >> + cmd = redircmd(cmd, q, eq, O_WRONLY | O_CREATE, 1); break; } } return cmd; } -struct cmd* +struct cmd * parseblock(char **ps, char *es) { struct cmd *cmd; @@ -413,24 +413,24 @@ parseblock(char **ps, char *es) return cmd; } -struct cmd* +struct cmd * parseexec(char **ps, char *es) { - char *q, *eq; - int tok, argc; + char *q, *eq; + int tok, argc; struct execcmd *cmd; - struct cmd *ret; + struct cmd *ret; if(peek(ps, es, "(")) return parseblock(ps, es); ret = execcmd(); - cmd = (struct execcmd*)ret; + cmd = (struct execcmd *)ret; argc = 0; ret = parseredirs(ret, ps, es); - while(!peek(ps, es, "|)&;")){ - if((tok=gettoken(ps, es, &q, &eq)) == 0) + while(!peek(ps, es, "|)&;")) { + if((tok = gettoken(ps, es, &q, &eq)) == 0) break; if(tok != 'a') panic("syntax"); @@ -447,46 +447,46 @@ parseexec(char **ps, char *es) } // NUL-terminate all the counted strings. -struct cmd* +struct cmd * nulterminate(struct cmd *cmd) { - int i; - struct backcmd *bcmd; - struct execcmd *ecmd; - struct listcmd *lcmd; - struct pipecmd *pcmd; + int i; + struct backcmd *bcmd; + struct execcmd *ecmd; + struct listcmd *lcmd; + struct pipecmd *pcmd; struct redircmd *rcmd; if(cmd == 0) return 0; - switch(cmd->type){ + switch(cmd->type) { case EXEC: - ecmd = (struct execcmd*)cmd; - for(i=0; ecmd->argv[i]; i++) + ecmd = (struct execcmd *)cmd; + for(i = 0; ecmd->argv[i]; i++) *ecmd->eargv[i] = 0; break; case REDIR: - rcmd = (struct redircmd*)cmd; + rcmd = (struct redircmd *)cmd; nulterminate(rcmd->cmd); *rcmd->efile = 0; break; case PIPE: - pcmd = (struct pipecmd*)cmd; + pcmd = (struct pipecmd *)cmd; nulterminate(pcmd->left); nulterminate(pcmd->right); break; case LIST: - lcmd = (struct listcmd*)cmd; + lcmd = (struct listcmd *)cmd; nulterminate(lcmd->left); nulterminate(lcmd->right); break; case BACK: - bcmd = (struct backcmd*)cmd; + bcmd = (struct backcmd *)cmd; nulterminate(bcmd->cmd); break; } diff --git a/user/stressfs.c b/user/stressfs.c index 247a7a5..5f7154a 100644 --- a/user/stressfs.c +++ b/user/stressfs.c @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) { - int fd, i; + int fd, i; char path[] = "stressfs0"; char data[512]; @@ -32,14 +32,14 @@ main(int argc, char *argv[]) path[8] += i; fd = open(path, O_CREATE | O_RDWR); for(i = 0; i < 20; i++) -// printf(fd, "%d\n", i); + // printf(fd, "%d\n", i); write(fd, data, sizeof(data)); close(fd); printf("read\n"); fd = open(path, O_RDONLY); - for (i = 0; i < 20; i++) + for(i = 0; i < 20; i++) read(fd, data, sizeof(data)); close(fd); diff --git a/user/ulib.c b/user/ulib.c index a4b7ddf..332d1da 100644 --- a/user/ulib.c +++ b/user/ulib.c @@ -14,7 +14,7 @@ _main() exit(0); } -char* +char * strcpy(char *s, const char *t) { char *os; @@ -43,33 +43,33 @@ strlen(const char *s) return n; } -void* +void * memset(void *dst, int c, u32 n) { - char *cdst = (char *) dst; - int i; - for(i = 0; i < n; i++){ + char *cdst = (char *)dst; + int i; + for(i = 0; i < n; i++) { cdst[i] = c; } return dst; } -char* +char * strchr(const char *s, char c) { for(; *s; s++) if(*s == c) - return (char*)s; + return (char *)s; return 0; } -char* +char * gets(char *buf, int max) { - int i, cc; + int i, cc; char c; - for(i=0; i+1 < max; ){ + for(i = 0; i + 1 < max;) { cc = read(0, &c, 1); if(cc < 1) break; @@ -102,19 +102,19 @@ atoi(const char *s) n = 0; while('0' <= *s && *s <= '9') - n = n*10 + *s++ - '0'; + n = n * 10 + *s++ - '0'; return n; } -void* +void * memmove(void *vdst, const void *vsrc, int n) { - char *dst; + char *dst; const char *src; dst = vdst; src = vsrc; - if (src > dst) { + if(src > dst) { while(n-- > 0) *dst++ = *src++; } else { @@ -130,8 +130,8 @@ int memcmp(const void *s1, const void *s2, u32 n) { const char *p1 = s1, *p2 = s2; - while (n-- > 0) { - if (*p1 != *p2) { + while(n-- > 0) { + if(*p1 != *p2) { return *p1 - *p2; } p1++; diff --git a/user/umalloc.c b/user/umalloc.c index 0ea2475..4398225 100644 --- a/user/umalloc.c +++ b/user/umalloc.c @@ -11,14 +11,14 @@ typedef long Align; union header { struct { union header *ptr; - u32 size; + u32 size; } s; Align x; }; typedef union header Header; -static Header base; +static Header base; static Header *freep; void @@ -26,16 +26,16 @@ free(void *ap) { Header *bp, *p; - bp = (Header*)ap - 1; + bp = (Header *)ap - 1; for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr) if(p >= p->s.ptr && (bp > p || bp < p->s.ptr)) break; - if(bp + bp->s.size == p->s.ptr){ + if(bp + bp->s.size == p->s.ptr) { bp->s.size += p->s.ptr->s.size; bp->s.ptr = p->s.ptr->s.ptr; } else bp->s.ptr = p->s.ptr; - if(p + p->s.size == bp){ + if(p + p->s.size == bp) { p->s.size += bp->s.size; p->s.ptr = bp->s.ptr; } else @@ -43,36 +43,36 @@ free(void *ap) freep = p; } -static Header* +static Header * morecore(u32 nu) { - char *p; + char *p; Header *hp; if(nu < 4096) nu = 4096; p = sbrk(nu * sizeof(Header)); - if(p == (char*)-1) + if(p == (char *)-1) return 0; - hp = (Header*)p; + hp = (Header *)p; hp->s.size = nu; - free((void*)(hp + 1)); + free((void *)(hp + 1)); return freep; } -void* +void * malloc(u32 nbytes) { Header *p, *prevp; - u32 nunits; + u32 nunits; - nunits = (nbytes + sizeof(Header) - 1)/sizeof(Header) + 1; - if((prevp = freep) == 0){ + nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1; + if((prevp = freep) == 0) { base.s.ptr = freep = prevp = &base; base.s.size = 0; } - for(p = prevp->s.ptr; ; prevp = p, p = p->s.ptr){ - if(p->s.size >= nunits){ + for(p = prevp->s.ptr;; prevp = p, p = p->s.ptr) { + if(p->s.size >= nunits) { if(p->s.size == nunits) prevp->s.ptr = p->s.ptr; else { @@ -81,7 +81,7 @@ malloc(u32 nbytes) p->s.size = nunits; } freep = prevp; - return (void*)(p + 1); + return (void *)(p + 1); } if(p == freep) if((p = morecore(nunits)) == 0) diff --git a/user/user.h b/user/user.h index 8aaf59c..a27f3f1 100644 --- a/user/user.h +++ b/user/user.h @@ -1,41 +1,41 @@ struct stat; // system calls -int fork(void); -int exit(int) __attribute__((noreturn)); -int wait(int*); -int pipe(int*); -int write(int, const void*, int); -int read(int, void*, int); -int close(int); -int kill(int); -int exec(const char*, char**); -int open(const char*, int); -int mknod(const char*, short, short); -int unlink(const char*); -int fstat(int fd, struct stat*); -int link(const char*, const char*); -int mkdir(const char*); -int chdir(const char*); -int dup(int); -int getpid(void); -char* sbrk(int); -int sleep(int); -int uptime(void); +int fork(void); +int exit(int) __attribute__((noreturn)); +int wait(int *); +int pipe(int *); +int write(int, const void *, int); +int read(int, void *, int); +int close(int); +int kill(int); +int exec(const char *, char **); +int open(const char *, int); +int mknod(const char *, short, short); +int unlink(const char *); +int fstat(int fd, struct stat *); +int link(const char *, const char *); +int mkdir(const char *); +int chdir(const char *); +int dup(int); +int getpid(void); +char *sbrk(int); +int sleep(int); +int uptime(void); // ulib.c -int stat(const char*, struct stat*); -char* strcpy(char*, const char*); -void *memmove(void*, const void*, int); -char* strchr(const char*, char c); -int strcmp(const char*, const char*); -void fprintf(int, const char*, ...); -void printf(const char*, ...); -char* gets(char*, int max); -u32 strlen(const char*); -void* memset(void*, int, u32); -void* malloc(u32); -void free(void*); -int atoi(const char*); -int memcmp(const void *, const void *, u32); +int stat(const char *, struct stat *); +char *strcpy(char *, const char *); +void *memmove(void *, const void *, int); +char *strchr(const char *, char c); +int strcmp(const char *, const char *); +void fprintf(int, const char *, ...); +void printf(const char *, ...); +char *gets(char *, int max); +u32 strlen(const char *); +void *memset(void *, int, u32); +void *malloc(u32); +void free(void *); +int atoi(const char *); +int memcmp(const void *, const void *, u32); void *memcpy(void *, const void *, u32); diff --git a/user/usertests.c b/user/usertests.c index dc82c41..d4fb6be 100644 --- a/user/usertests.c +++ b/user/usertests.c @@ -17,7 +17,7 @@ // prints "OK". // -#define BUFSZ ((MAXOPBLOCKS+2)*BSIZE) +#define BUFSZ ((MAXOPBLOCKS + 2) * BSIZE) char buf[BUFSZ]; @@ -34,35 +34,35 @@ copyin(char *s) { u64 addrs[] = { 0x80000000LL, 0xffffffffffffffff }; - for(int ai = 0; ai < 2; ai++){ + for(int ai = 0; ai < 2; ai++) { u64 addr = addrs[ai]; - - int fd = open("copyin1", O_CREATE|O_WRONLY); - if(fd < 0){ + + int fd = open("copyin1", O_CREATE | O_WRONLY); + if(fd < 0) { printf("open(copyin1) failed\n"); exit(1); } - int n = write(fd, (void*)addr, 8192); - if(n >= 0){ + int n = write(fd, (void *)addr, 8192); + if(n >= 0) { printf("write(fd, %p, 8192) returned %d, not -1\n", addr, n); exit(1); } close(fd); unlink("copyin1"); - - n = write(1, (char*)addr, 8192); - if(n > 0){ + + n = write(1, (char *)addr, 8192); + if(n > 0) { printf("write(1, %p, 8192) returned %d, not -1 or 0\n", addr, n); exit(1); } - + int fds[2]; - if(pipe(fds) < 0){ + if(pipe(fds) < 0) { printf("pipe() failed\n"); exit(1); } - n = write(fds[1], (char*)addr, 8192); - if(n > 0){ + n = write(fds[1], (char *)addr, 8192); + if(n > 0) { printf("write(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n); exit(1); } @@ -78,33 +78,33 @@ copyout(char *s) { u64 addrs[] = { 0x80000000LL, 0xffffffffffffffff }; - for(int ai = 0; ai < 2; ai++){ + for(int ai = 0; ai < 2; ai++) { u64 addr = addrs[ai]; int fd = open("README", 0); - if(fd < 0){ + if(fd < 0) { printf("open(README) failed\n"); exit(1); } - int n = read(fd, (void*)addr, 8192); - if(n > 0){ + int n = read(fd, (void *)addr, 8192); + if(n > 0) { printf("read(fd, %p, 8192) returned %d, not -1 or 0\n", addr, n); exit(1); } close(fd); int fds[2]; - if(pipe(fds) < 0){ + if(pipe(fds) < 0) { printf("pipe() failed\n"); exit(1); } n = write(fds[1], "x", 1); - if(n != 1){ + if(n != 1) { printf("pipe write failed\n"); exit(1); } - n = read(fds[0], (void*)addr, 8192); - if(n > 0){ + n = read(fds[0], (void *)addr, 8192); + if(n > 0) { printf("read(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n); exit(1); } @@ -119,11 +119,11 @@ copyinstr1(char *s) { u64 addrs[] = { 0x80000000LL, 0xffffffffffffffff }; - for(int ai = 0; ai < 2; ai++){ + for(int ai = 0; ai < 2; ai++) { u64 addr = addrs[ai]; - int fd = open((char *)addr, O_CREATE|O_WRONLY); - if(fd >= 0){ + int fd = open((char *)addr, O_CREATE | O_WRONLY); + if(fd >= 0) { printf("open(%p) returned %d, not -1\n", addr, fd); exit(1); } @@ -136,50 +136,50 @@ copyinstr1(char *s) void copyinstr2(char *s) { - char b[MAXPATH+1]; + char b[MAXPATH + 1]; for(int i = 0; i < MAXPATH; i++) b[i] = 'x'; b[MAXPATH] = '\0'; - + int ret = unlink(b); - if(ret != -1){ + if(ret != -1) { printf("unlink(%s) returned %d, not -1\n", b, ret); exit(1); } int fd = open(b, O_CREATE | O_WRONLY); - if(fd != -1){ + if(fd != -1) { printf("open(%s) returned %d, not -1\n", b, fd); exit(1); } ret = link(b, b); - if(ret != -1){ + if(ret != -1) { printf("link(%s, %s) returned %d, not -1\n", b, b, ret); exit(1); } char *args[] = { "xx", 0 }; ret = exec(b, args); - if(ret != -1){ + if(ret != -1) { printf("exec(%s) returned %d, not -1\n", b, fd); exit(1); } int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n"); exit(1); } - if(pid == 0){ - static char big[PGSIZE+1]; + if(pid == 0) { + static char big[PGSIZE + 1]; for(int i = 0; i < PGSIZE; i++) big[i] = 'x'; big[PGSIZE] = '\0'; char *args2[] = { big, big, big, 0 }; ret = exec("echo", args2); - if(ret != -1){ + if(ret != -1) { printf("exec(echo, BIG) returned %d, not -1\n", fd); exit(1); } @@ -188,7 +188,7 @@ copyinstr2(char *s) int st = 0; wait(&st); - if(st != 747){ + if(st != 747) { printf("exec(echo, BIG) succeeded, should have failed\n"); exit(1); } @@ -199,40 +199,40 @@ void copyinstr3(char *s) { sbrk(8192); - u64 top = (u64) sbrk(0); - if((top % PGSIZE) != 0){ + u64 top = (u64)sbrk(0); + if((top % PGSIZE) != 0) { sbrk(PGSIZE - (top % PGSIZE)); } - top = (u64) sbrk(0); - if(top % PGSIZE){ + top = (u64)sbrk(0); + if(top % PGSIZE) { printf("oops\n"); exit(1); } - char *b = (char *) (top - 1); + char *b = (char *)(top - 1); *b = 'x'; int ret = unlink(b); - if(ret != -1){ + if(ret != -1) { printf("unlink(%s) returned %d, not -1\n", b, ret); exit(1); } int fd = open(b, O_CREATE | O_WRONLY); - if(fd != -1){ + if(fd != -1) { printf("open(%s) returned %d, not -1\n", b, fd); exit(1); } ret = link(b, b); - if(ret != -1){ + if(ret != -1) { printf("link(%s, %s) returned %d, not -1\n", b, b, ret); exit(1); } char *args[] = { "xx", 0 }; ret = exec(b, args); - if(ret != -1){ + if(ret != -1) { printf("exec(%s) returned %d, not -1\n", b, fd); exit(1); } @@ -244,44 +244,44 @@ void rwsbrk() { int fd, n; - - u64 a = (u64) sbrk(8192); + + u64 a = (u64)sbrk(8192); if(a == 0xffffffffffffffffLL) { printf("sbrk(rwsbrk) failed\n"); exit(1); } - - if ((u64) sbrk(-8192) == 0xffffffffffffffffLL) { + + if((u64)sbrk(-8192) == 0xffffffffffffffffLL) { printf("sbrk(rwsbrk) shrink failed\n"); exit(1); } - fd = open("rwsbrk", O_CREATE|O_WRONLY); - if(fd < 0){ + fd = open("rwsbrk", O_CREATE | O_WRONLY); + if(fd < 0) { printf("open(rwsbrk) failed\n"); exit(1); } - n = write(fd, (void*)(a+4096), 1024); - if(n >= 0){ - printf("write(fd, %p, 1024) returned %d, not -1\n", a+4096, n); + n = write(fd, (void *)(a + 4096), 1024); + if(n >= 0) { + printf("write(fd, %p, 1024) returned %d, not -1\n", a + 4096, n); exit(1); } close(fd); unlink("rwsbrk"); fd = open("README", O_RDONLY); - if(fd < 0){ + if(fd < 0) { printf("open(rwsbrk) failed\n"); exit(1); } - n = read(fd, (void*)(a+4096), 10); - if(n >= 0){ - printf("read(fd, %p, 10) returned %d, not -1\n", a+4096, n); + n = read(fd, (void *)(a + 4096), 10); + if(n >= 0) { + printf("read(fd, %p, 10) returned %d, not -1\n", a + 4096, n); exit(1); } close(fd); - + exit(0); } @@ -290,46 +290,46 @@ void truncate1(char *s) { char buf[32]; - + unlink("truncfile"); - int fd1 = open("truncfile", O_CREATE|O_WRONLY|O_TRUNC); + int fd1 = open("truncfile", O_CREATE | O_WRONLY | O_TRUNC); write(fd1, "abcd", 4); close(fd1); int fd2 = open("truncfile", O_RDONLY); int n = read(fd2, buf, sizeof(buf)); - if(n != 4){ + if(n != 4) { printf("%s: read %d bytes, wanted 4\n", s, n); exit(1); } - fd1 = open("truncfile", O_WRONLY|O_TRUNC); + fd1 = open("truncfile", O_WRONLY | O_TRUNC); int fd3 = open("truncfile", O_RDONLY); n = read(fd3, buf, sizeof(buf)); - if(n != 0){ + if(n != 0) { printf("aaa fd3=%d\n", fd3); printf("%s: read %d bytes, wanted 0\n", s, n); exit(1); } n = read(fd2, buf, sizeof(buf)); - if(n != 0){ + if(n != 0) { printf("bbb fd2=%d\n", fd2); printf("%s: read %d bytes, wanted 0\n", s, n); exit(1); } - + write(fd1, "abcdef", 6); n = read(fd3, buf, sizeof(buf)); - if(n != 6){ + if(n != 6) { printf("%s: read %d bytes, wanted 6\n", s, n); exit(1); } n = read(fd2, buf, sizeof(buf)); - if(n != 2){ + if(n != 2) { printf("%s: read %d bytes, wanted 2\n", s, n); exit(1); } @@ -350,13 +350,13 @@ truncate2(char *s) { unlink("truncfile"); - int fd1 = open("truncfile", O_CREATE|O_TRUNC|O_WRONLY); + int fd1 = open("truncfile", O_CREATE | O_TRUNC | O_WRONLY); write(fd1, "abcd", 4); - int fd2 = open("truncfile", O_TRUNC|O_WRONLY); + int fd2 = open("truncfile", O_TRUNC | O_WRONLY); int n = write(fd1, "x", 1); - if(n != -1){ + if(n != -1) { printf("%s: write returned %d, expected -1\n", s, n); exit(1); } @@ -371,24 +371,24 @@ truncate3(char *s) { int pid, xstatus; - close(open("truncfile", O_CREATE|O_TRUNC|O_WRONLY)); - + close(open("truncfile", O_CREATE | O_TRUNC | O_WRONLY)); + pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid == 0){ - for(int i = 0; i < 100; i++){ + if(pid == 0) { + for(int i = 0; i < 100; i++) { char buf[32]; - int fd = open("truncfile", O_WRONLY); - if(fd < 0){ + int fd = open("truncfile", O_WRONLY); + if(fd < 0) { printf("%s: open failed\n", s); exit(1); } int n = write(fd, "1234567890", 10); - if(n != 10){ + if(n != 10) { printf("%s: write got %d, expected 10\n", s, n); exit(1); } @@ -400,14 +400,14 @@ truncate3(char *s) exit(0); } - for(int i = 0; i < 150; i++){ - int fd = open("truncfile", O_CREATE|O_WRONLY|O_TRUNC); - if(fd < 0){ + for(int i = 0; i < 150; i++) { + int fd = open("truncfile", O_CREATE | O_WRONLY | O_TRUNC); + if(fd < 0) { printf("%s: open failed\n", s); exit(1); } int n = write(fd, "xxx", 3); - if(n != 3){ + if(n != 3) { printf("%s: write got %d, expected 3\n", s, n); exit(1); } @@ -418,25 +418,24 @@ truncate3(char *s) unlink("truncfile"); exit(xstatus); } - // does chdir() call iput(p->cwd) in a transaction? void iputtest(char *s) { - if(mkdir("iputdir") < 0){ + if(mkdir("iputdir") < 0) { printf("%s: mkdir failed\n", s); exit(1); } - if(chdir("iputdir") < 0){ + if(chdir("iputdir") < 0) { printf("%s: chdir iputdir failed\n", s); exit(1); } - if(unlink("../iputdir") < 0){ + if(unlink("../iputdir") < 0) { printf("%s: unlink ../iputdir failed\n", s); exit(1); } - if(chdir("/") < 0){ + if(chdir("/") < 0) { printf("%s: chdir / failed\n", s); exit(1); } @@ -449,20 +448,20 @@ exitiputtest(char *s) int pid, xstatus; pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid == 0){ - if(mkdir("iputdir") < 0){ + if(pid == 0) { + if(mkdir("iputdir") < 0) { printf("%s: mkdir failed\n", s); exit(1); } - if(chdir("iputdir") < 0){ + if(chdir("iputdir") < 0) { printf("%s: child chdir failed\n", s); exit(1); } - if(unlink("../iputdir") < 0){ + if(unlink("../iputdir") < 0) { printf("%s: unlink ../iputdir failed\n", s); exit(1); } @@ -488,25 +487,25 @@ openiputtest(char *s) { int pid, xstatus; - if(mkdir("oidir") < 0){ + if(mkdir("oidir") < 0) { printf("%s: mkdir oidir failed\n", s); exit(1); } pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid == 0){ + if(pid == 0) { int fd = open("oidir", O_RDWR); - if(fd >= 0){ + if(fd >= 0) { printf("%s: open directory for write succeeded\n", s); exit(1); } exit(0); } sleep(1); - if(unlink("oidir") != 0){ + if(unlink("oidir") != 0) { printf("%s: unlink failed\n", s); exit(1); } @@ -522,13 +521,13 @@ opentest(char *s) int fd; fd = open("echo", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: open echo failed!\n", s); exit(1); } close(fd); fd = open("doesnotexist", 0); - if(fd >= 0){ + if(fd >= 0) { printf("%s: open doesnotexist succeeded!\n", s); exit(1); } @@ -539,37 +538,37 @@ writetest(char *s) { int fd; int i; - enum { N=100, SZ=10 }; - - fd = open("small", O_CREATE|O_RDWR); - if(fd < 0){ + enum { N = 100, SZ = 10 }; + + fd = open("small", O_CREATE | O_RDWR); + if(fd < 0) { printf("%s: error: creat small failed!\n", s); exit(1); } - for(i = 0; i < N; i++){ - if(write(fd, "aaaaaaaaaa", SZ) != SZ){ + for(i = 0; i < N; i++) { + if(write(fd, "aaaaaaaaaa", SZ) != SZ) { printf("%s: error: write aa %d new file failed\n", s, i); exit(1); } - if(write(fd, "bbbbbbbbbb", SZ) != SZ){ + if(write(fd, "bbbbbbbbbb", SZ) != SZ) { printf("%s: error: write bb %d new file failed\n", s, i); exit(1); } } close(fd); fd = open("small", O_RDONLY); - if(fd < 0){ + if(fd < 0) { printf("%s: error: open small failed!\n", s); exit(1); } - i = read(fd, buf, N*SZ*2); - if(i != N*SZ*2){ + i = read(fd, buf, N * SZ * 2); + if(i != N * SZ * 2) { printf("%s: read failed\n", s); exit(1); } close(fd); - if(unlink("small") < 0){ + if(unlink("small") < 0) { printf("%s: unlink small failed\n", s); exit(1); } @@ -580,15 +579,15 @@ writebig(char *s) { int i, fd, n; - fd = open("big", O_CREATE|O_RDWR); - if(fd < 0){ + fd = open("big", O_CREATE | O_RDWR); + if(fd < 0) { printf("%s: error: creat big failed!\n", s); exit(1); } - for(i = 0; i < MAXFILE; i++){ - ((int*)buf)[0] = i; - if(write(fd, buf, BSIZE) != BSIZE){ + for(i = 0; i < MAXFILE; i++) { + ((int *)buf)[0] = i; + if(write(fd, buf, BSIZE) != BSIZE) { printf("%s: error: write big file failed\n", s, i); exit(1); } @@ -597,33 +596,32 @@ writebig(char *s) close(fd); fd = open("big", O_RDONLY); - if(fd < 0){ + if(fd < 0) { printf("%s: error: open big failed!\n", s); exit(1); } n = 0; - for(;;){ + for(;;) { i = read(fd, buf, BSIZE); - if(i == 0){ - if(n == MAXFILE - 1){ + if(i == 0) { + if(n == MAXFILE - 1) { printf("%s: read only %d blocks from big", s, n); exit(1); } break; - } else if(i != BSIZE){ + } else if(i != BSIZE) { printf("%s: read failed %d\n", s, i); exit(1); } - if(((int*)buf)[0] != n){ - printf("%s: read content of block %d is %d\n", s, - n, ((int*)buf)[0]); + if(((int *)buf)[0] != n) { + printf("%s: read content of block %d is %d\n", s, n, ((int *)buf)[0]); exit(1); } n++; } close(fd); - if(unlink("big") < 0){ + if(unlink("big") < 0) { printf("%s: unlink big failed\n", s); exit(1); } @@ -634,42 +632,43 @@ void createtest(char *s) { int i, fd; - enum { N=52 }; + enum { N = 52 }; char name[3]; name[0] = 'a'; name[2] = '\0'; - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { name[1] = '0' + i; - fd = open(name, O_CREATE|O_RDWR); + fd = open(name, O_CREATE | O_RDWR); close(fd); } name[0] = 'a'; name[2] = '\0'; - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { name[1] = '0' + i; unlink(name); } } -void dirtest(char *s) +void +dirtest(char *s) { - if(mkdir("dir0") < 0){ + if(mkdir("dir0") < 0) { printf("%s: mkdir failed\n", s); exit(1); } - if(chdir("dir0") < 0){ + if(chdir("dir0") < 0) { printf("%s: chdir dir0 failed\n", s); exit(1); } - if(chdir("..") < 0){ + if(chdir("..") < 0) { printf("%s: chdir .. failed\n", s); exit(1); } - if(unlink("dir0") < 0){ + if(unlink("dir0") < 0) { printf("%s: unlink dir0 failed\n", s); exit(1); } @@ -678,19 +677,19 @@ void dirtest(char *s) void exectest(char *s) { - int fd, xstatus, pid; + int fd, xstatus, pid; char *echoargv[] = { "echo", "OK", 0 }; - char buf[3]; + char buf[3]; unlink("echo-ok"); pid = fork(); if(pid < 0) { - printf("%s: fork failed\n", s); - exit(1); + printf("%s: fork failed\n", s); + exit(1); } if(pid == 0) { close(1); - fd = open("echo-ok", O_CREATE|O_WRONLY); + fd = open("echo-ok", O_CREATE | O_WRONLY); if(fd < 0) { printf("%s: create failed\n", s); exit(1); @@ -699,13 +698,13 @@ exectest(char *s) printf("%s: wrong fd\n", s); exit(1); } - if(exec("echo", echoargv) < 0){ + if(exec("echo", echoargv) < 0) { printf("%s: exec echo failed\n", s); exit(1); } // won't get to here } - if (wait(&xstatus) != pid) { + if(wait(&xstatus) != pid) { printf("%s: wait failed!\n", s); } if(xstatus != 0) @@ -716,7 +715,7 @@ exectest(char *s) printf("%s: open failed\n", s); exit(1); } - if (read(fd, buf, 2) != 2) { + if(read(fd, buf, 2) != 2) { printf("%s: read failed\n", s); exit(1); } @@ -727,7 +726,6 @@ exectest(char *s) printf("%s: wrong output\n", s); exit(1); } - } // simple fork and pipe read/write @@ -737,32 +735,32 @@ pipe1(char *s) { int fds[2], pid, xstatus; int seq, i, n, cc, total; - enum { N=5, SZ=1033 }; - - if(pipe(fds) != 0){ + enum { N = 5, SZ = 1033 }; + + if(pipe(fds) != 0) { printf("%s: pipe() failed\n", s); exit(1); } pid = fork(); seq = 0; - if(pid == 0){ + if(pid == 0) { close(fds[0]); - for(n = 0; n < N; n++){ + for(n = 0; n < N; n++) { for(i = 0; i < SZ; i++) buf[i] = seq++; - if(write(fds[1], buf, SZ) != SZ){ + if(write(fds[1], buf, SZ) != SZ) { printf("%s: pipe1 oops 1\n", s); exit(1); } } exit(0); - } else if(pid > 0){ + } else if(pid > 0) { close(fds[1]); total = 0; cc = 1; - while((n = read(fds[0], buf, cc)) > 0){ - for(i = 0; i < n; i++){ - if((buf[i] & 0xff) != (seq++ & 0xff)){ + while((n = read(fds[0], buf, cc)) > 0) { + for(i = 0; i < n; i++) { + if((buf[i] & 0xff) != (seq++ & 0xff)) { printf("%s: pipe1 oops 2\n", s); return; } @@ -772,7 +770,7 @@ pipe1(char *s) if(cc > sizeof(buf)) cc = sizeof(buf); } - if(total != N * SZ){ + if(total != N * SZ) { printf("%s: pipe1 oops 3 total %d\n", total); exit(1); } @@ -785,20 +783,19 @@ pipe1(char *s) } } - // test if child is killed (status = -1) void killstatus(char *s) { int xst; - - for(int i = 0; i < 100; i++){ + + for(int i = 0; i < 100; i++) { int pid1 = fork(); - if(pid1 < 0){ + if(pid1 < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid1 == 0){ + if(pid1 == 0) { while(1) { getpid(); } @@ -808,8 +805,8 @@ killstatus(char *s) kill(pid1); wait(&xst); if(xst != -1) { - printf("%s: status should be -1\n", s); - exit(1); + printf("%s: status should be -1\n", s); + exit(1); } } exit(0); @@ -843,10 +840,10 @@ preempt(char *s) pipe(pfds); pid3 = fork(); if(pid3 < 0) { - printf("%s: fork failed\n", s); - exit(1); + printf("%s: fork failed\n", s); + exit(1); } - if(pid3 == 0){ + if(pid3 == 0) { close(pfds[0]); if(write(pfds[1], "x", 1) != 1) printf("%s: preempt write error", s); @@ -856,7 +853,7 @@ preempt(char *s) } close(pfds[1]); - if(read(pfds[0], buf, sizeof(buf)) != 1){ + if(read(pfds[0], buf, sizeof(buf)) != 1) { printf("%s: preempt read error", s); return; } @@ -877,15 +874,15 @@ exitwait(char *s) { int i, pid; - for(i = 0; i < 100; i++){ + for(i = 0; i < 100; i++) { pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid){ + if(pid) { int xstate; - if(wait(&xstate) != pid){ + if(wait(&xstate) != pid) { printf("%s: wait wrong pid\n", s); exit(1); } @@ -906,20 +903,20 @@ void reparent(char *s) { int master_pid = getpid(); - for(int i = 0; i < 200; i++){ + for(int i = 0; i < 200; i++) { int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid){ - if(wait(0) != pid){ + if(pid) { + if(wait(0) != pid) { printf("%s: wait wrong pid\n", s); exit(1); } } else { int pid2 = fork(); - if(pid2 < 0){ + if(pid2 < 0) { kill(master_pid); exit(1); } @@ -933,21 +930,21 @@ reparent(char *s) void twochildren(char *s) { - for(int i = 0; i < 1000; i++){ + for(int i = 0; i < 1000; i++) { int pid1 = fork(); - if(pid1 < 0){ + if(pid1 < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid1 == 0){ + if(pid1 == 0) { exit(0); } else { int pid2 = fork(); - if(pid2 < 0){ + if(pid2 < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid2 == 0){ + if(pid2 == 0) { exit(0); } else { wait(0); @@ -961,21 +958,21 @@ twochildren(char *s) void forkfork(char *s) { - enum { N=2 }; - - for(int i = 0; i < N; i++){ + enum { N = 2 }; + + for(int i = 0; i < N; i++) { int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed", s); exit(1); } - if(pid == 0){ - for(int j = 0; j < 200; j++){ + if(pid == 0) { + for(int j = 0; j < 200; j++) { int pid1 = fork(); - if(pid1 < 0){ + if(pid1 < 0) { exit(1); } - if(pid1 == 0){ + if(pid1 == 0) { exit(0); } wait(0); @@ -985,7 +982,7 @@ forkfork(char *s) } int xstatus; - for(int i = 0; i < N; i++){ + for(int i = 0; i < N; i++) { wait(&xstatus); if(xstatus != 0) { printf("%s: fork in child failed", s); @@ -1000,18 +997,18 @@ forkforkfork(char *s) unlink("stopforking"); int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed", s); exit(1); } - if(pid == 0){ - while(1){ + if(pid == 0) { + while(1) { int fd = open("stopforking", 0); - if(fd >= 0){ + if(fd >= 0) { exit(0); } - if(fork() < 0){ - close(open("stopforking", O_CREATE|O_RDWR)); + if(fork() < 0) { + close(open("stopforking", O_CREATE | O_RDWR)); } } @@ -1019,7 +1016,7 @@ forkforkfork(char *s) } sleep(20); // two seconds - close(open("stopforking", O_CREATE|O_RDWR)); + close(open("stopforking", O_CREATE | O_RDWR)); wait(0); sleep(10); // one second } @@ -1032,13 +1029,13 @@ forkforkfork(char *s) void reparent2(char *s) { - for(int i = 0; i < 800; i++){ + for(int i = 0; i < 800; i++) { int pid1 = fork(); - if(pid1 < 0){ + if(pid1 < 0) { printf("fork failed\n"); exit(1); } - if(pid1 == 0){ + if(pid1 == 0) { fork(); fork(); exit(0); @@ -1054,21 +1051,21 @@ void mem(char *s) { void *m1, *m2; - int pid; + int pid; - if((pid = fork()) == 0){ + if((pid = fork()) == 0) { m1 = 0; - while((m2 = malloc(10001)) != 0){ - *(char**)m2 = m1; + while((m2 = malloc(10001)) != 0) { + *(char **)m2 = m1; m1 = m2; } - while(m1){ - m2 = *(char**)m1; + while(m1) { + m2 = *(char **)m1; free(m1); m1 = m2; } - m1 = malloc(1024*20); - if(m1 == 0){ + m1 = malloc(1024 * 20); + if(m1 == 0) { printf("couldn't allocate mem?!!\n", s); exit(1); } @@ -1077,7 +1074,7 @@ mem(char *s) } else { int xstatus; wait(&xstatus); - if(xstatus == -1){ + if(xstatus == -1) { // probably page fault, so might be lazy lab, // so OK. exit(0); @@ -1094,19 +1091,19 @@ void sharedfd(char *s) { int fd, pid, i, n, nc, np; - enum { N = 1000, SZ=10}; + enum { N = 1000, SZ = 10 }; char buf[SZ]; unlink("sharedfd"); - fd = open("sharedfd", O_CREATE|O_RDWR); - if(fd < 0){ + fd = open("sharedfd", O_CREATE | O_RDWR); + if(fd < 0) { printf("%s: cannot open sharedfd for writing", s); exit(1); } pid = fork(); - memset(buf, pid==0?'c':'p', sizeof(buf)); - for(i = 0; i < N; i++){ - if(write(fd, buf, sizeof(buf)) != sizeof(buf)){ + memset(buf, pid == 0 ? 'c' : 'p', sizeof(buf)); + for(i = 0; i < N; i++) { + if(write(fd, buf, sizeof(buf)) != sizeof(buf)) { printf("%s: write sharedfd failed\n", s); exit(1); } @@ -1119,16 +1116,16 @@ sharedfd(char *s) if(xstatus != 0) exit(xstatus); } - + close(fd); fd = open("sharedfd", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: cannot open sharedfd for reading\n", s); exit(1); } nc = np = 0; - while((n = read(fd, buf, sizeof(buf))) > 0){ - for(i = 0; i < sizeof(buf); i++){ + while((n = read(fd, buf, sizeof(buf))) > 0) { + for(i = 0; i < sizeof(buf); i++) { if(buf[i] == 'c') nc++; if(buf[i] == 'p') @@ -1137,7 +1134,7 @@ sharedfd(char *s) } close(fd); unlink("sharedfd"); - if(nc == N*SZ && np == N*SZ){ + if(nc == N * SZ && np == N * SZ) { exit(0); } else { printf("%s: nc/np test fails\n", s); @@ -1150,31 +1147,31 @@ sharedfd(char *s) void fourfiles(char *s) { - int fd, pid, i, j, n, total, pi; + int fd, pid, i, j, n, total, pi; char *names[] = { "f0", "f1", "f2", "f3" }; char *fname; - enum { N=12, NCHILD=4, SZ=500 }; - - for(pi = 0; pi < NCHILD; pi++){ + enum { N = 12, NCHILD = 4, SZ = 500 }; + + for(pi = 0; pi < NCHILD; pi++) { fname = names[pi]; unlink(fname); pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n", s); exit(1); } - if(pid == 0){ + if(pid == 0) { fd = open(fname, O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("create failed\n", s); exit(1); } - memset(buf, '0'+pi, SZ); - for(i = 0; i < N; i++){ - if((n = write(fd, buf, SZ)) != SZ){ + memset(buf, '0' + pi, SZ); + for(i = 0; i < N; i++) { + if((n = write(fd, buf, SZ)) != SZ) { printf("write failed %d\n", n); exit(1); } @@ -1184,19 +1181,19 @@ fourfiles(char *s) } int xstatus; - for(pi = 0; pi < NCHILD; pi++){ + for(pi = 0; pi < NCHILD; pi++) { wait(&xstatus); if(xstatus != 0) exit(xstatus); } - for(i = 0; i < NCHILD; i++){ + for(i = 0; i < NCHILD; i++) { fname = names[i]; fd = open(fname, 0); total = 0; - while((n = read(fd, buf, sizeof(buf))) > 0){ - for(j = 0; j < n; j++){ - if(buf[j] != '0'+i){ + while((n = read(fd, buf, sizeof(buf))) > 0) { + for(j = 0; j < n; j++) { + if(buf[j] != '0' + i) { printf("wrong char\n", s); exit(1); } @@ -1204,7 +1201,7 @@ fourfiles(char *s) total += n; } close(fd); - if(total != N*SZ){ + if(total != N * SZ) { printf("wrong length %d\n", total); exit(1); } @@ -1216,31 +1213,31 @@ fourfiles(char *s) void createdelete(char *s) { - enum { N = 20, NCHILD=4 }; - int pid, i, fd, pi; + enum { N = 20, NCHILD = 4 }; + int pid, i, fd, pi; char name[32]; - for(pi = 0; pi < NCHILD; pi++){ + for(pi = 0; pi < NCHILD; pi++) { pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n", s); exit(1); } - if(pid == 0){ + if(pid == 0) { name[0] = 'p' + pi; name[2] = '\0'; - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { name[1] = '0' + i; fd = open(name, O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: create failed\n", s); exit(1); } close(fd); - if(i > 0 && (i % 2 ) == 0){ + if(i > 0 && (i % 2) == 0) { name[1] = '0' + (i / 2); - if(unlink(name) < 0){ + if(unlink(name) < 0) { printf("%s: unlink failed\n", s); exit(1); } @@ -1251,22 +1248,22 @@ createdelete(char *s) } int xstatus; - for(pi = 0; pi < NCHILD; pi++){ + for(pi = 0; pi < NCHILD; pi++) { wait(&xstatus); if(xstatus != 0) exit(1); } name[0] = name[1] = name[2] = 0; - for(i = 0; i < N; i++){ - for(pi = 0; pi < NCHILD; pi++){ + for(i = 0; i < N; i++) { + for(pi = 0; pi < NCHILD; pi++) { name[0] = 'p' + pi; name[1] = '0' + i; fd = open(name, 0); - if((i == 0 || i >= N/2) && fd < 0){ + if((i == 0 || i >= N / 2) && fd < 0) { printf("%s: oops createdelete %s didn't exist\n", s, name); exit(1); - } else if((i >= 1 && i < N/2) && fd >= 0){ + } else if((i >= 1 && i < N / 2) && fd >= 0) { printf("%s: oops createdelete %s did exist\n", s, name); exit(1); } @@ -1275,8 +1272,8 @@ createdelete(char *s) } } - for(i = 0; i < N; i++){ - for(pi = 0; pi < NCHILD; pi++){ + for(i = 0; i < N; i++) { + for(pi = 0; pi < NCHILD; pi++) { name[0] = 'p' + i; name[1] = '0' + i; unlink(name); @@ -1292,7 +1289,7 @@ unlinkread(char *s) int fd, fd1; fd = open("unlinkread", O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: create unlinkread failed\n", s); exit(1); } @@ -1300,11 +1297,11 @@ unlinkread(char *s) close(fd); fd = open("unlinkread", O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: open unlinkread failed\n", s); exit(1); } - if(unlink("unlinkread") != 0){ + if(unlink("unlinkread") != 0) { printf("%s: unlink unlinkread failed\n", s); exit(1); } @@ -1313,15 +1310,15 @@ unlinkread(char *s) write(fd1, "yyy", 3); close(fd1); - if(read(fd, buf, sizeof(buf)) != SZ){ + if(read(fd, buf, sizeof(buf)) != SZ) { printf("%s: unlinkread read failed", s); exit(1); } - if(buf[0] != 'h'){ + if(buf[0] != 'h') { printf("%s: unlinkread wrong data\n", s); exit(1); } - if(write(fd, buf, 10) != 10){ + if(write(fd, buf, 10) != 10) { printf("%s: unlinkread write failed\n", s); exit(1); } @@ -1338,51 +1335,51 @@ linktest(char *s) unlink("lf1"); unlink("lf2"); - fd = open("lf1", O_CREATE|O_RDWR); - if(fd < 0){ + fd = open("lf1", O_CREATE | O_RDWR); + if(fd < 0) { printf("%s: create lf1 failed\n", s); exit(1); } - if(write(fd, "hello", SZ) != SZ){ + if(write(fd, "hello", SZ) != SZ) { printf("%s: write lf1 failed\n", s); exit(1); } close(fd); - if(link("lf1", "lf2") < 0){ + if(link("lf1", "lf2") < 0) { printf("%s: link lf1 lf2 failed\n", s); exit(1); } unlink("lf1"); - if(open("lf1", 0) >= 0){ + if(open("lf1", 0) >= 0) { printf("%s: unlinked lf1 but it is still there!\n", s); exit(1); } fd = open("lf2", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: open lf2 failed\n", s); exit(1); } - if(read(fd, buf, sizeof(buf)) != SZ){ + if(read(fd, buf, sizeof(buf)) != SZ) { printf("%s: read lf2 failed\n", s); exit(1); } close(fd); - if(link("lf2", "lf2") >= 0){ + if(link("lf2", "lf2") >= 0) { printf("%s: link lf2 lf2 succeeded! oops\n", s); exit(1); } unlink("lf2"); - if(link("lf2", "lf1") >= 0){ + if(link("lf2", "lf1") >= 0) { printf("%s: link non-existent succeeded! oops\n", s); exit(1); } - if(link(".", "lf1") >= 0){ + if(link(".", "lf1") >= 0) { printf("%s: link . lf1 succeeded! oops\n", s); exit(1); } @@ -1394,26 +1391,26 @@ concreate(char *s) { enum { N = 40 }; char file[3]; - int i, pid, n, fd; + int i, pid, n, fd; char fa[N]; struct { - u16 inum; + u16 inum; char name[DIRSIZ]; } de; file[0] = 'C'; file[2] = '\0'; - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { file[1] = '0' + i; unlink(file); pid = fork(); - if(pid && (i % 3) == 1){ + if(pid && (i % 3) == 1) { link("C0", file); - } else if(pid == 0 && (i % 5) == 1){ + } else if(pid == 0 && (i % 5) == 1) { link("C0", file); } else { fd = open(file, O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("concreate create %s failed\n", file); exit(1); } @@ -1432,16 +1429,16 @@ concreate(char *s) memset(fa, 0, sizeof(fa)); fd = open(".", 0); n = 0; - while(read(fd, &de, sizeof(de)) > 0){ + while(read(fd, &de, sizeof(de)) > 0) { if(de.inum == 0) continue; - if(de.name[0] == 'C' && de.name[2] == '\0'){ + if(de.name[0] == 'C' && de.name[2] == '\0') { i = de.name[1] - '0'; - if(i < 0 || i >= sizeof(fa)){ + if(i < 0 || i >= sizeof(fa)) { printf("%s: concreate weird file %s\n", s, de.name); exit(1); } - if(fa[i]){ + if(fa[i]) { printf("%s: concreate duplicate file %s\n", s, de.name); exit(1); } @@ -1451,20 +1448,19 @@ concreate(char *s) } close(fd); - if(n != N){ + if(n != N) { printf("%s: concreate not enough files in directory listing\n", s); exit(1); } - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { file[1] = '0' + i; pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(((i % 3) == 0 && pid == 0) || - ((i % 3) == 1 && pid != 0)){ + if(((i % 3) == 0 && pid == 0) || ((i % 3) == 1 && pid != 0)) { close(open(file, 0)); close(open(file, 0)); close(open(file, 0)); @@ -1495,17 +1491,17 @@ linkunlink(char *s) unlink("x"); pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } unsigned int x = (pid ? 1 : 97); - for(i = 0; i < 100; i++){ + for(i = 0; i < 100; i++) { x = x * 1103515245 + 12345; - if((x % 3) == 0){ + if((x % 3) == 0) { close(open("x", O_RDWR | O_CREATE)); - } else if((x % 3) == 1){ + } else if((x % 3) == 1) { link("cat", "x"); } else { unlink("x"); @@ -1518,38 +1514,37 @@ linkunlink(char *s) exit(0); } - void subdir(char *s) { int fd, cc; unlink("ff"); - if(mkdir("dd") != 0){ + if(mkdir("dd") != 0) { printf("%s: mkdir dd failed\n", s); exit(1); } fd = open("dd/ff", O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: create dd/ff failed\n", s); exit(1); } write(fd, "ff", 2); close(fd); - if(unlink("dd") >= 0){ + if(unlink("dd") >= 0) { printf("%s: unlink dd (non-empty dir) succeeded!\n", s); exit(1); } - if(mkdir("/dd/dd") != 0){ + if(mkdir("/dd/dd") != 0) { printf("subdir mkdir dd/dd failed\n", s); exit(1); } fd = open("dd/dd/ff", O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: create dd/dd/ff failed\n", s); exit(1); } @@ -1557,142 +1552,142 @@ subdir(char *s) close(fd); fd = open("dd/dd/../ff", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: open dd/dd/../ff failed\n", s); exit(1); } cc = read(fd, buf, sizeof(buf)); - if(cc != 2 || buf[0] != 'f'){ + if(cc != 2 || buf[0] != 'f') { printf("%s: dd/dd/../ff wrong content\n", s); exit(1); } close(fd); - if(link("dd/dd/ff", "dd/dd/ffff") != 0){ + if(link("dd/dd/ff", "dd/dd/ffff") != 0) { printf("link dd/dd/ff dd/dd/ffff failed\n", s); exit(1); } - if(unlink("dd/dd/ff") != 0){ + if(unlink("dd/dd/ff") != 0) { printf("%s: unlink dd/dd/ff failed\n", s); exit(1); } - if(open("dd/dd/ff", O_RDONLY) >= 0){ + if(open("dd/dd/ff", O_RDONLY) >= 0) { printf("%s: open (unlinked) dd/dd/ff succeeded\n", s); exit(1); } - if(chdir("dd") != 0){ + if(chdir("dd") != 0) { printf("%s: chdir dd failed\n", s); exit(1); } - if(chdir("dd/../../dd") != 0){ + if(chdir("dd/../../dd") != 0) { printf("%s: chdir dd/../../dd failed\n", s); exit(1); } - if(chdir("dd/../../../dd") != 0){ + if(chdir("dd/../../../dd") != 0) { printf("chdir dd/../../dd failed\n", s); exit(1); } - if(chdir("./..") != 0){ + if(chdir("./..") != 0) { printf("%s: chdir ./.. failed\n", s); exit(1); } fd = open("dd/dd/ffff", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: open dd/dd/ffff failed\n", s); exit(1); } - if(read(fd, buf, sizeof(buf)) != 2){ + if(read(fd, buf, sizeof(buf)) != 2) { printf("%s: read dd/dd/ffff wrong len\n", s); exit(1); } close(fd); - if(open("dd/dd/ff", O_RDONLY) >= 0){ + if(open("dd/dd/ff", O_RDONLY) >= 0) { printf("%s: open (unlinked) dd/dd/ff succeeded!\n", s); exit(1); } - if(open("dd/ff/ff", O_CREATE|O_RDWR) >= 0){ + if(open("dd/ff/ff", O_CREATE | O_RDWR) >= 0) { printf("%s: create dd/ff/ff succeeded!\n", s); exit(1); } - if(open("dd/xx/ff", O_CREATE|O_RDWR) >= 0){ + if(open("dd/xx/ff", O_CREATE | O_RDWR) >= 0) { printf("%s: create dd/xx/ff succeeded!\n", s); exit(1); } - if(open("dd", O_CREATE) >= 0){ + if(open("dd", O_CREATE) >= 0) { printf("%s: create dd succeeded!\n", s); exit(1); } - if(open("dd", O_RDWR) >= 0){ + if(open("dd", O_RDWR) >= 0) { printf("%s: open dd rdwr succeeded!\n", s); exit(1); } - if(open("dd", O_WRONLY) >= 0){ + if(open("dd", O_WRONLY) >= 0) { printf("%s: open dd wronly succeeded!\n", s); exit(1); } - if(link("dd/ff/ff", "dd/dd/xx") == 0){ + if(link("dd/ff/ff", "dd/dd/xx") == 0) { printf("%s: link dd/ff/ff dd/dd/xx succeeded!\n", s); exit(1); } - if(link("dd/xx/ff", "dd/dd/xx") == 0){ + if(link("dd/xx/ff", "dd/dd/xx") == 0) { printf("%s: link dd/xx/ff dd/dd/xx succeeded!\n", s); exit(1); } - if(link("dd/ff", "dd/dd/ffff") == 0){ + if(link("dd/ff", "dd/dd/ffff") == 0) { printf("%s: link dd/ff dd/dd/ffff succeeded!\n", s); exit(1); } - if(mkdir("dd/ff/ff") == 0){ + if(mkdir("dd/ff/ff") == 0) { printf("%s: mkdir dd/ff/ff succeeded!\n", s); exit(1); } - if(mkdir("dd/xx/ff") == 0){ + if(mkdir("dd/xx/ff") == 0) { printf("%s: mkdir dd/xx/ff succeeded!\n", s); exit(1); } - if(mkdir("dd/dd/ffff") == 0){ + if(mkdir("dd/dd/ffff") == 0) { printf("%s: mkdir dd/dd/ffff succeeded!\n", s); exit(1); } - if(unlink("dd/xx/ff") == 0){ + if(unlink("dd/xx/ff") == 0) { printf("%s: unlink dd/xx/ff succeeded!\n", s); exit(1); } - if(unlink("dd/ff/ff") == 0){ + if(unlink("dd/ff/ff") == 0) { printf("%s: unlink dd/ff/ff succeeded!\n", s); exit(1); } - if(chdir("dd/ff") == 0){ + if(chdir("dd/ff") == 0) { printf("%s: chdir dd/ff succeeded!\n", s); exit(1); } - if(chdir("dd/xx") == 0){ + if(chdir("dd/xx") == 0) { printf("%s: chdir dd/xx succeeded!\n", s); exit(1); } - if(unlink("dd/dd/ffff") != 0){ + if(unlink("dd/dd/ffff") != 0) { printf("%s: unlink dd/dd/ff failed\n", s); exit(1); } - if(unlink("dd/ff") != 0){ + if(unlink("dd/ff") != 0) { printf("%s: unlink dd/ff failed\n", s); exit(1); } - if(unlink("dd") == 0){ + if(unlink("dd") == 0) { printf("%s: unlink non-empty dd succeeded!\n", s); exit(1); } - if(unlink("dd/dd") < 0){ + if(unlink("dd/dd") < 0) { printf("%s: unlink dd/dd failed\n", s); exit(1); } - if(unlink("dd") < 0){ + if(unlink("dd") < 0) { printf("%s: unlink dd failed\n", s); exit(1); } @@ -1705,16 +1700,16 @@ bigwrite(char *s) int fd, sz; unlink("bigwrite"); - for(sz = 499; sz < (MAXOPBLOCKS+2)*BSIZE; sz += 471){ + for(sz = 499; sz < (MAXOPBLOCKS + 2) * BSIZE; sz += 471) { fd = open("bigwrite", O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: cannot create bigwrite\n", s); exit(1); } int i; - for(i = 0; i < 2; i++){ + for(i = 0; i < 2; i++) { int cc = write(fd, buf, sz); - if(cc != sz){ + if(cc != sz) { printf("%s: write(%d) ret %d\n", s, sz, cc); exit(1); } @@ -1724,22 +1719,21 @@ bigwrite(char *s) } } - void bigfile(char *s) { - enum { N = 20, SZ=600 }; + enum { N = 20, SZ = 600 }; int fd, i, total, cc; unlink("bigfile.dat"); fd = open("bigfile.dat", O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: cannot create bigfile", s); exit(1); } - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { memset(buf, i, SZ); - if(write(fd, buf, SZ) != SZ){ + if(write(fd, buf, SZ) != SZ) { printf("%s: write bigfile failed\n", s); exit(1); } @@ -1747,31 +1741,31 @@ bigfile(char *s) close(fd); fd = open("bigfile.dat", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: cannot open bigfile\n", s); exit(1); } total = 0; - for(i = 0; ; i++){ - cc = read(fd, buf, SZ/2); - if(cc < 0){ + for(i = 0;; i++) { + cc = read(fd, buf, SZ / 2); + if(cc < 0) { printf("%s: read bigfile failed\n", s); exit(1); } if(cc == 0) break; - if(cc != SZ/2){ + if(cc != SZ / 2) { printf("%s: short read bigfile\n", s); exit(1); } - if(buf[0] != i/2 || buf[SZ/2-1] != i/2){ + if(buf[0] != i / 2 || buf[SZ / 2 - 1] != i / 2) { printf("%s: read bigfile wrong data\n", s); exit(1); } total += cc; } close(fd); - if(total != N*SZ){ + if(total != N * SZ) { printf("%s: read bigfile wrong total\n", s); exit(1); } @@ -1785,32 +1779,32 @@ fourteen(char *s) // DIRSIZ is 14. - if(mkdir("12345678901234") != 0){ + if(mkdir("12345678901234") != 0) { printf("%s: mkdir 12345678901234 failed\n", s); exit(1); } - if(mkdir("12345678901234/123456789012345") != 0){ + if(mkdir("12345678901234/123456789012345") != 0) { printf("%s: mkdir 12345678901234/123456789012345 failed\n", s); exit(1); } fd = open("123456789012345/123456789012345/123456789012345", O_CREATE); - if(fd < 0){ + if(fd < 0) { printf("%s: create 123456789012345/123456789012345/123456789012345 failed\n", s); exit(1); } close(fd); fd = open("12345678901234/12345678901234/12345678901234", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: open 12345678901234/12345678901234/12345678901234 failed\n", s); exit(1); } close(fd); - if(mkdir("12345678901234/12345678901234") == 0){ + if(mkdir("12345678901234/12345678901234") == 0) { printf("%s: mkdir 12345678901234/12345678901234 succeeded!\n", s); exit(1); } - if(mkdir("123456789012345/12345678901234") == 0){ + if(mkdir("123456789012345/12345678901234") == 0) { printf("%s: mkdir 12345678901234/123456789012345 succeeded!\n", s); exit(1); } @@ -1827,35 +1821,35 @@ fourteen(char *s) void rmdot(char *s) { - if(mkdir("dots") != 0){ + if(mkdir("dots") != 0) { printf("%s: mkdir dots failed\n", s); exit(1); } - if(chdir("dots") != 0){ + if(chdir("dots") != 0) { printf("%s: chdir dots failed\n", s); exit(1); } - if(unlink(".") == 0){ + if(unlink(".") == 0) { printf("%s: rm . worked!\n", s); exit(1); } - if(unlink("..") == 0){ + if(unlink("..") == 0) { printf("%s: rm .. worked!\n", s); exit(1); } - if(chdir("/") != 0){ + if(chdir("/") != 0) { printf("%s: chdir / failed\n", s); exit(1); } - if(unlink("dots/.") == 0){ + if(unlink("dots/.") == 0) { printf("%s: unlink dots/. worked!\n", s); exit(1); } - if(unlink("dots/..") == 0){ + if(unlink("dots/..") == 0) { printf("%s: unlink dots/.. worked!\n", s); exit(1); } - if(unlink("dots") != 0){ + if(unlink("dots") != 0) { printf("%s: unlink dots failed!\n", s); exit(1); } @@ -1867,49 +1861,49 @@ dirfile(char *s) int fd; fd = open("dirfile", O_CREATE); - if(fd < 0){ + if(fd < 0) { printf("%s: create dirfile failed\n", s); exit(1); } close(fd); - if(chdir("dirfile") == 0){ + if(chdir("dirfile") == 0) { printf("%s: chdir dirfile succeeded!\n", s); exit(1); } fd = open("dirfile/xx", 0); - if(fd >= 0){ + if(fd >= 0) { printf("%s: create dirfile/xx succeeded!\n", s); exit(1); } fd = open("dirfile/xx", O_CREATE); - if(fd >= 0){ + if(fd >= 0) { printf("%s: create dirfile/xx succeeded!\n", s); exit(1); } - if(mkdir("dirfile/xx") == 0){ + if(mkdir("dirfile/xx") == 0) { printf("%s: mkdir dirfile/xx succeeded!\n", s); exit(1); } - if(unlink("dirfile/xx") == 0){ + if(unlink("dirfile/xx") == 0) { printf("%s: unlink dirfile/xx succeeded!\n", s); exit(1); } - if(link("README", "dirfile/xx") == 0){ + if(link("README", "dirfile/xx") == 0) { printf("%s: link to dirfile/xx succeeded!\n", s); exit(1); } - if(unlink("dirfile") != 0){ + if(unlink("dirfile") != 0) { printf("%s: unlink dirfile failed!\n", s); exit(1); } fd = open(".", O_RDWR); - if(fd >= 0){ + if(fd >= 0) { printf("%s: open . for writing succeeded!\n", s); exit(1); } fd = open(".", 0); - if(write(fd, "x", 1) > 0){ + if(write(fd, "x", 1) > 0) { printf("%s: write . succeeded!\n", s); exit(1); } @@ -1923,12 +1917,12 @@ iref(char *s) { int i, fd; - for(i = 0; i < NINODE + 1; i++){ - if(mkdir("irefd") != 0){ + for(i = 0; i < NINODE + 1; i++) { + if(mkdir("irefd") != 0) { printf("%s: mkdir irefd failed\n", s); exit(1); } - if(chdir("irefd") != 0){ + if(chdir("irefd") != 0) { printf("%s: chdir irefd failed\n", s); exit(1); } @@ -1945,7 +1939,7 @@ iref(char *s) } // clean up - for(i = 0; i < NINODE + 1; i++){ + for(i = 0; i < NINODE + 1; i++) { chdir(".."); unlink("irefd"); } @@ -1959,10 +1953,10 @@ iref(char *s) void forktest(char *s) { - enum{ N = 1000 }; + enum { N = 1000 }; int n, pid; - for(n=0; n 0; n--){ - if(wait(0) < 0){ + for(; n > 0; n--) { + if(wait(0) < 0) { printf("%s: wait stopped early\n", s); exit(1); } } - if(wait(0) != -1){ + if(wait(0) != -1) { printf("%s: wait got too many\n", s); exit(1); } @@ -1996,27 +1990,27 @@ forktest(char *s) void sbrkbasic(char *s) { - enum { TOOMUCH=1024*1024*1024}; - int i, pid, xstatus; + enum { TOOMUCH = 1024 * 1024 * 1024 }; + int i, pid, xstatus; char *c, *a, *b; // does sbrk() return the expected failure value? pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed in sbrkbasic\n"); exit(1); } - if(pid == 0){ + if(pid == 0) { a = sbrk(TOOMUCH); - if(a == (char*)0xffffffffffffffffL){ + if(a == (char *)0xffffffffffffffffL) { // it's OK if this fails. exit(0); } - - for(b = a; b < a+TOOMUCH; b += 4096){ + + for(b = a; b < a + TOOMUCH; b += 4096) { *b = 99; } - + // we should not get here! either sbrk(TOOMUCH) // should have failed, or (with lazy allocation) // a pagefault should have killed this process. @@ -2024,16 +2018,16 @@ sbrkbasic(char *s) } wait(&xstatus); - if(xstatus == 1){ + if(xstatus == 1) { printf("%s: too much memory allocated!\n", s); exit(1); } // can one sbrk() less than a page? a = sbrk(0); - for(i = 0; i < 5000; i++){ + for(i = 0; i < 5000; i++) { b = sbrk(1); - if(b != a){ + if(b != a) { printf("%s: sbrk test failed %d %x %x\n", s, i, a, b); exit(1); } @@ -2041,13 +2035,13 @@ sbrkbasic(char *s) a = b + 1; } pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: sbrk test fork failed\n", s); exit(1); } c = sbrk(1); c = sbrk(1); - if(c != a + 1){ + if(c != a + 1) { printf("%s: sbrk test failed post-fork\n", s); exit(1); } @@ -2060,9 +2054,9 @@ sbrkbasic(char *s) void sbrkmuch(char *s) { - enum { BIG=100*1024*1024 }; + enum { BIG = 100 * 1024 * 1024 }; char *c, *oldbrk, *a, *lastaddr, *p; - u64 amt; + u64 amt; oldbrk = sbrk(0); @@ -2070,7 +2064,7 @@ sbrkmuch(char *s) a = sbrk(0); amt = BIG - (u64)a; p = sbrk(amt); - if (p != a) { + if(p != a) { printf("%s: sbrk test failed to grow big address space; enough phys mem?\n", s); exit(1); } @@ -2080,18 +2074,18 @@ sbrkmuch(char *s) for(char *pp = a; pp < eee; pp += 4096) *pp = 1; - lastaddr = (char*) (BIG-1); + lastaddr = (char *)(BIG - 1); *lastaddr = 99; // can one de-allocate? a = sbrk(0); c = sbrk(-PGSIZE); - if(c == (char*)0xffffffffffffffffL){ + if(c == (char *)0xffffffffffffffffL) { printf("%s: sbrk could not deallocate\n", s); exit(1); } c = sbrk(0); - if(c != a - PGSIZE){ + if(c != a - PGSIZE) { printf("%s: sbrk deallocation produced wrong address, a %x c %x\n", s, a, c); exit(1); } @@ -2099,11 +2093,11 @@ sbrkmuch(char *s) // can one re-allocate that page? a = sbrk(0); c = sbrk(PGSIZE); - if(c != a || sbrk(0) != a + PGSIZE){ + if(c != a || sbrk(0) != a + PGSIZE) { printf("%s: sbrk re-allocation failed, a %x c %x\n", s, a, c); exit(1); } - if(*lastaddr == 99){ + if(*lastaddr == 99) { // should be zero printf("%s: sbrk de-allocation didn't really deallocate\n", s); exit(1); @@ -2111,7 +2105,7 @@ sbrkmuch(char *s) a = sbrk(0); c = sbrk(-(sbrk(0) - oldbrk)); - if(c != a){ + if(c != a) { printf("%s: sbrk downsize failed, a %x c %x\n", s, a, c); exit(1); } @@ -2122,21 +2116,21 @@ void kernmem(char *s) { char *a; - int pid; + int pid; - for(a = (char*)(KERNBASE); a < (char*) (KERNBASE+2000000); a += 50000){ + for(a = (char *)(KERNBASE); a < (char *)(KERNBASE + 2000000); a += 50000) { pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid == 0){ + if(pid == 0) { printf("%s: oops could read %x = %x\n", s, a, *a); exit(1); } int xstatus; wait(&xstatus); - if(xstatus != -1) // did kernel kill child? + if(xstatus != -1) // did kernel kill child? exit(1); } } @@ -2146,21 +2140,21 @@ void MAXVAplus(char *s) { volatile u64 a = MAXVA; - for( ; a != 0; a <<= 1){ + for(; a != 0; a <<= 1) { int pid; pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid == 0){ - *(char*)a = 99; + if(pid == 0) { + *(char *)a = 99; printf("%s: oops wrote %x\n", s, a); exit(1); } int xstatus; wait(&xstatus); - if(xstatus != -1) // did kernel kill child? + if(xstatus != -1) // did kernel kill child? exit(1); } } @@ -2170,25 +2164,26 @@ MAXVAplus(char *s) void sbrkfail(char *s) { - enum { BIG=100*1024*1024 }; - int i, xstatus; - int fds[2]; - char scratch; + enum { BIG = 100 * 1024 * 1024 }; + int i, xstatus; + int fds[2]; + char scratch; char *c, *a; - int pids[10]; - int pid; - - if(pipe(fds) != 0){ + int pids[10]; + int pid; + + if(pipe(fds) != 0) { printf("%s: pipe() failed\n", s); exit(1); } - 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 a lot of memory sbrk(BIG - (u64)sbrk(0)); write(fds[1], "x", 1); // sit around until killed - for(;;) sleep(1000); + for(;;) + sleep(1000); } if(pids[i] != -1) read(fds[0], &scratch, 1); @@ -2197,32 +2192,32 @@ sbrkfail(char *s) // if those failed allocations freed up the pages they did allocate, // we'll be able to allocate here c = sbrk(PGSIZE); - for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){ + for(i = 0; i < sizeof(pids) / sizeof(pids[0]); i++) { if(pids[i] == -1) continue; kill(pids[i]); wait(0); } - if(c == (char*)0xffffffffffffffffL){ + if(c == (char *)0xffffffffffffffffL) { printf("%s: failed sbrk leaked memory\n", s); exit(1); } - // test running fork with the above allocated page + // test running fork with the above allocated page pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } - if(pid == 0){ + if(pid == 0) { // allocate a lot of memory. // this should produce a page fault, // and thus not complete. a = sbrk(0); - sbrk(10*BIG); + sbrk(10 * BIG); int n = 0; - for (i = 0; i < 10*BIG; i += PGSIZE) { - n += *(a+i); + for(i = 0; i < 10 * BIG; i += PGSIZE) { + n += *(a + i); } // print n so the compiler doesn't optimize away // the for loop. @@ -2234,22 +2229,21 @@ sbrkfail(char *s) exit(1); } - // test reads/writes from/to allocated memory void sbrkarg(char *s) { char *a; - int fd, n; + int fd, n; a = sbrk(PGSIZE); - fd = open("sbrk", O_CREATE|O_WRONLY); + fd = open("sbrk", O_CREATE | O_WRONLY); unlink("sbrk"); - if(fd < 0) { + if(fd < 0) { printf("%s: open sbrk failed\n", s); exit(1); } - if ((n = write(fd, a, PGSIZE)) < 0) { + if((n = write(fd, a, PGSIZE)) < 0) { printf("%s: write sbrk failed\n", s); exit(1); } @@ -2257,10 +2251,10 @@ sbrkarg(char *s) // test writes to allocated memory a = sbrk(PGSIZE); - if(pipe((int *) a) != 0){ + if(pipe((int *)a) != 0) { printf("%s: pipe() failed\n", s); exit(1); - } + } } void @@ -2269,10 +2263,10 @@ validatetest(char *s) int hi; u64 p; - hi = 1100*1024; - for(p = 0; p <= (u32)hi; p += PGSIZE){ + hi = 1100 * 1024; + for(p = 0; p <= (u32)hi; p += PGSIZE) { // try to crash the kernel by passing in a bad string pointer - if(link("nosuchfile", (char*)p) != -1){ + if(link("nosuchfile", (char *)p) != -1) { printf("%s: link should not succeed\n", s); exit(1); } @@ -2286,8 +2280,8 @@ bsstest(char *s) { int i; - for(i = 0; i < sizeof(uninit); i++){ - if(uninit[i] != '\0'){ + for(i = 0; i < sizeof(uninit); i++) { + if(uninit[i] != '\0') { printf("%s: bss test failed\n", s); exit(1); } @@ -2304,26 +2298,28 @@ bigargtest(char *s) unlink("bigarg-ok"); pid = fork(); - if(pid == 0){ + if(pid == 0) { static char *args[MAXARG]; - int i; - for(i = 0; i < MAXARG-1; i++) - args[i] = "bigargs test: failed\n "; - args[MAXARG-1] = 0; + int i; + for(i = 0; i < MAXARG - 1; i++) + args[i] = "bigargs test: failed\n " + " " + " "; + args[MAXARG - 1] = 0; exec("echo", args); fd = open("bigarg-ok", O_CREATE); close(fd); exit(0); - } else if(pid < 0){ + } else if(pid < 0) { printf("%s: bigargtest: fork failed\n", s); exit(1); } - + wait(&xstatus); if(xstatus != 0) exit(xstatus); fd = open("bigarg-ok", 0); - if(fd < 0){ + if(fd < 0) { printf("%s: bigarg test failed!\n", s); exit(1); } @@ -2340,7 +2336,7 @@ fsfull() printf("fsfull test\n"); - for(nfiles = 0; ; nfiles++){ + for(nfiles = 0;; nfiles++) { char name[64]; name[0] = 'f'; name[1] = '0' + nfiles / 1000; @@ -2349,13 +2345,13 @@ fsfull() name[4] = '0' + (nfiles % 10); name[5] = '\0'; printf("writing %s\n", name); - int fd = open(name, O_CREATE|O_RDWR); - if(fd < 0){ + int fd = open(name, O_CREATE | O_RDWR); + if(fd < 0) { printf("open %s failed\n", name); break; } int total = 0; - while(1){ + while(1) { int cc = write(fd, buf, BSIZE); if(cc < BSIZE) break; @@ -2368,7 +2364,7 @@ fsfull() break; } - while(nfiles >= 0){ + while(nfiles >= 0) { char name[64]; name[0] = 'f'; name[1] = '0' + nfiles / 1000; @@ -2383,11 +2379,12 @@ fsfull() printf("fsfull test finished\n"); } -void argptest(char *s) +void +argptest(char *s) { int fd; fd = open("init", O_RDONLY); - if (fd < 0) { + if(fd < 0) { printf("%s: open failed\n", s); exit(1); } @@ -2402,20 +2399,20 @@ stacktest(char *s) { int pid; int xstatus; - + pid = fork(); if(pid == 0) { - char *sp = (char *) r_sp(); + char *sp = (char *)r_sp(); sp -= PGSIZE; // the *sp should cause a trap. printf("%s: stacktest: read below stack %p\n", s, *sp); exit(1); - } else if(pid < 0){ + } else if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } wait(&xstatus); - if(xstatus == -1) // kernel killed child? + if(xstatus == -1) // kernel killed child? exit(0); else exit(xstatus); @@ -2427,18 +2424,18 @@ textwrite(char *s) { int pid; int xstatus; - + pid = fork(); if(pid == 0) { - volatile int *addr = (int *) 0; + volatile int *addr = (int *)0; *addr = 10; exit(1); - } else if(pid < 0){ + } else if(pid < 0) { printf("%s: fork failed\n", s); exit(1); } wait(&xstatus); - if(xstatus == -1) // kernel killed child? + if(xstatus == -1) // kernel killed child? exit(0); else exit(xstatus); @@ -2447,7 +2444,7 @@ textwrite(char *s) // regression test. copyin(), copyout(), and copyinstr() used to cast // the virtual page address to u32, which (with certain wild system // call arguments) resulted in a kernel page faults. -void *big = (void*) 0xeaeb0b5b00002f5e; +void *big = (void *)0xeaeb0b5b00002f5e; void pgbug(char *s) { @@ -2466,12 +2463,12 @@ void sbrkbugs(char *s) { int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n"); exit(1); } - if(pid == 0){ - int sz = (u64) sbrk(0); + if(pid == 0) { + int sz = (u64)sbrk(0); // free all user memory; there used to be a bug that // would not adjust p->sz correctly in this case, // causing exit() to panic. @@ -2482,12 +2479,12 @@ sbrkbugs(char *s) wait(0); pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n"); exit(1); } - if(pid == 0){ - int sz = (u64) sbrk(0); + if(pid == 0) { + int sz = (u64)sbrk(0); // set the break to somewhere in the very first // page; there used to be a bug that would incorrectly // free the first page. @@ -2497,13 +2494,13 @@ sbrkbugs(char *s) wait(0); pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n"); exit(1); } - if(pid == 0){ + if(pid == 0) { // set the break in the middle of a page. - sbrk((10*4096 + 2048) - (u64)sbrk(0)); + sbrk((10 * 4096 + 2048) - (u64)sbrk(0)); // reduce the break a bit, but not enough to // cause a page to be freed. this used to cause @@ -2523,17 +2520,17 @@ sbrkbugs(char *s) void sbrklast(char *s) { - u64 top = (u64) sbrk(0); + u64 top = (u64)sbrk(0); if((top % 4096) != 0) sbrk(4096 - (top % 4096)); sbrk(4096); sbrk(10); sbrk(-20); - top = (u64) sbrk(0); - char *p = (char *) (top - 64); + top = (u64)sbrk(0); + char *p = (char *)(top - 64); p[0] = 'x'; p[1] = '\0'; - int fd = open(p, O_RDWR|O_CREATE); + int fd = open(p, O_RDWR | O_CREATE); write(fd, p, 1); close(fd); fd = open(p, O_RDWR); @@ -2543,7 +2540,6 @@ sbrklast(char *s) exit(1); } - // does sbrk handle signed int32 wrap-around with // negative arguments? void @@ -2551,23 +2547,21 @@ sbrk8000(char *s) { sbrk(0x80000004); volatile char *top = sbrk(0); - *(top-1) = *(top-1) + 1; + *(top - 1) = *(top - 1) + 1; } - - // regression test. test whether exec() leaks memory if one of the // arguments is invalid. the test passes if the kernel doesn't panic. void badarg(char *s) { - for(int i = 0; i < 50000; i++){ + for(int i = 0; i < 50000; i++) { char *argv[2]; - argv[0] = (char*)0xffffffff; + argv[0] = (char *)0xffffffff; argv[1] = 0; exec("echo", argv); } - + exit(0); } @@ -2575,68 +2569,68 @@ struct test { void (*f)(char *); char *s; } quicktests[] = { - {copyin, "copyin"}, - {copyout, "copyout"}, - {copyinstr1, "copyinstr1"}, - {copyinstr2, "copyinstr2"}, - {copyinstr3, "copyinstr3"}, - {rwsbrk, "rwsbrk" }, - {truncate1, "truncate1"}, - {truncate2, "truncate2"}, - {truncate3, "truncate3"}, - {openiputtest, "openiput"}, - {exitiputtest, "exitiput"}, - {iputtest, "iput"}, - {opentest, "opentest"}, - {writetest, "writetest"}, - {writebig, "writebig"}, - {createtest, "createtest"}, - {dirtest, "dirtest"}, - {exectest, "exectest"}, - {pipe1, "pipe1"}, - {killstatus, "killstatus"}, - {preempt, "preempt"}, - {exitwait, "exitwait"}, - {reparent, "reparent" }, - {twochildren, "twochildren"}, - {forkfork, "forkfork"}, - {forkforkfork, "forkforkfork"}, - {reparent2, "reparent2"}, - {mem, "mem"}, - {sharedfd, "sharedfd"}, - {fourfiles, "fourfiles"}, - {createdelete, "createdelete"}, - {unlinkread, "unlinkread"}, - {linktest, "linktest"}, - {concreate, "concreate"}, - {linkunlink, "linkunlink"}, - {subdir, "subdir"}, - {bigwrite, "bigwrite"}, - {bigfile, "bigfile"}, - {fourteen, "fourteen"}, - {rmdot, "rmdot"}, - {dirfile, "dirfile"}, - {iref, "iref"}, - {forktest, "forktest"}, - {sbrkbasic, "sbrkbasic"}, - {sbrkmuch, "sbrkmuch"}, - {kernmem, "kernmem"}, - {MAXVAplus, "MAXVAplus"}, - {sbrkfail, "sbrkfail"}, - {sbrkarg, "sbrkarg"}, - {validatetest, "validatetest"}, - {bsstest, "bsstest"}, - {bigargtest, "bigargtest"}, - {argptest, "argptest"}, - {stacktest, "stacktest"}, - {textwrite, "textwrite"}, - {pgbug, "pgbug" }, - {sbrkbugs, "sbrkbugs" }, - {sbrklast, "sbrklast"}, - {sbrk8000, "sbrk8000"}, - {badarg, "badarg" }, + { copyin, "copyin" }, + { copyout, "copyout" }, + { copyinstr1, "copyinstr1" }, + { copyinstr2, "copyinstr2" }, + { copyinstr3, "copyinstr3" }, + { rwsbrk, "rwsbrk" }, + { truncate1, "truncate1" }, + { truncate2, "truncate2" }, + { truncate3, "truncate3" }, + { openiputtest, "openiput" }, + { exitiputtest, "exitiput" }, + { iputtest, "iput" }, + { opentest, "opentest" }, + { writetest, "writetest" }, + { writebig, "writebig" }, + { createtest, "createtest" }, + { dirtest, "dirtest" }, + { exectest, "exectest" }, + { pipe1, "pipe1" }, + { killstatus, "killstatus" }, + { preempt, "preempt" }, + { exitwait, "exitwait" }, + { reparent, "reparent" }, + { twochildren, "twochildren" }, + { forkfork, "forkfork" }, + { forkforkfork, "forkforkfork" }, + { reparent2, "reparent2" }, + { mem, "mem" }, + { sharedfd, "sharedfd" }, + { fourfiles, "fourfiles" }, + { createdelete, "createdelete" }, + { unlinkread, "unlinkread" }, + { linktest, "linktest" }, + { concreate, "concreate" }, + { linkunlink, "linkunlink" }, + { subdir, "subdir" }, + { bigwrite, "bigwrite" }, + { bigfile, "bigfile" }, + { fourteen, "fourteen" }, + { rmdot, "rmdot" }, + { dirfile, "dirfile" }, + { iref, "iref" }, + { forktest, "forktest" }, + { sbrkbasic, "sbrkbasic" }, + { sbrkmuch, "sbrkmuch" }, + { kernmem, "kernmem" }, + { MAXVAplus, "MAXVAplus" }, + { sbrkfail, "sbrkfail" }, + { sbrkarg, "sbrkarg" }, + { validatetest, "validatetest" }, + { bsstest, "bsstest" }, + { bigargtest, "bigargtest" }, + { argptest, "argptest" }, + { stacktest, "stacktest" }, + { textwrite, "textwrite" }, + { pgbug, "pgbug" }, + { sbrkbugs, "sbrkbugs" }, + { sbrklast, "sbrklast" }, + { sbrk8000, "sbrk8000" }, + { badarg, "badarg" }, - { 0, 0}, + { 0, 0 }, }; // @@ -2648,36 +2642,36 @@ void bigdir(char *s) { enum { N = 500 }; - int i, fd; + int i, fd; char name[10]; unlink("bd"); fd = open("bd", O_CREATE); - if(fd < 0){ + if(fd < 0) { printf("%s: bigdir create failed\n", s); exit(1); } close(fd); - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { name[0] = 'x'; name[1] = '0' + (i / 64); name[2] = '0' + (i % 64); name[3] = '\0'; - if(link("bd", name) != 0){ + if(link("bd", name) != 0) { printf("%s: bigdir link(bd, %s) failed\n", s, name); exit(1); } } unlink("bd"); - for(i = 0; i < N; i++){ + for(i = 0; i < N; i++) { name[0] = 'x'; name[1] = '0' + (i / 64); name[2] = '0' + (i % 64); name[3] = '\0'; - if(unlink(name) != 0){ + if(unlink(name) != 0) { printf("%s: bigdir unlink failed", s); exit(1); } @@ -2691,31 +2685,31 @@ manywrites(char *s) { int nchildren = 4; int howmany = 30; // increase to look for deadlock - - for(int ci = 0; ci < nchildren; ci++){ + + for(int ci = 0; ci < nchildren; ci++) { int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n"); exit(1); } - if(pid == 0){ + if(pid == 0) { char name[3]; name[0] = 'b'; name[1] = 'a' + ci; name[2] = '\0'; unlink(name); - - for(int iters = 0; iters < howmany; iters++){ - for(int i = 0; i < ci+1; i++){ + + for(int iters = 0; iters < howmany; iters++) { + for(int i = 0; i < ci + 1; i++) { int fd = open(name, O_CREATE | O_RDWR); - if(fd < 0){ + if(fd < 0) { printf("%s: cannot create %s\n", s, name); exit(1); } int sz = sizeof(buf); int cc = write(fd, buf, sz); - if(cc != sz){ + if(cc != sz) { printf("%s: write(%d) ret %d\n", s, sz, cc); exit(1); } @@ -2729,7 +2723,7 @@ manywrites(char *s) } } - for(int ci = 0; ci < nchildren; ci++){ + for(int ci = 0; ci < nchildren; ci++) { int st = 0; wait(&st); if(st != 0) @@ -2747,25 +2741,25 @@ void badwrite(char *s) { int assumed_free = 600; - + unlink("junk"); - for(int i = 0; i < assumed_free; i++){ - int fd = open("junk", O_CREATE|O_WRONLY); - if(fd < 0){ + for(int i = 0; i < assumed_free; i++) { + int fd = open("junk", O_CREATE | O_WRONLY); + if(fd < 0) { printf("open junk failed\n"); exit(1); } - write(fd, (char*)0xffffffffffL, 1); + write(fd, (char *)0xffffffffffL, 1); close(fd); unlink("junk"); } - int fd = open("junk", O_CREATE|O_WRONLY); - if(fd < 0){ + int fd = open("junk", O_CREATE | O_WRONLY); + if(fd < 0) { printf("open junk failed\n"); exit(1); } - if(write(fd, "x", 1) != 1){ + if(write(fd, "x", 1) != 1) { printf("write failed\n"); exit(1); } @@ -2781,31 +2775,31 @@ badwrite(char *s) void execout(char *s) { - for(int avail = 0; avail < 15; avail++){ + for(int avail = 0; avail < 15; avail++) { int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed\n"); exit(1); - } else if(pid == 0){ + } else if(pid == 0) { // allocate all of memory. - while(1){ - u64 a = (u64) sbrk(4096); + while(1) { + u64 a = (u64)sbrk(4096); if(a == 0xffffffffffffffffLL) break; - *(char*)(a + 4096 - 1) = 1; + *(char *)(a + 4096 - 1) = 1; } // free a few pages, in order to let exec() make some // progress. for(int i = 0; i < avail; i++) sbrk(-4096); - + close(1); char *args[] = { "echo", "x", 0 }; exec("echo", args); exit(0); } else { - wait((int*)0); + wait((int *)0); } } @@ -2820,8 +2814,8 @@ diskfull(char *s) int done = 0; unlink("diskfulldir"); - - for(fi = 0; done == 0; fi++){ + + for(fi = 0; done == 0; fi++) { char name[32]; name[0] = 'b'; name[1] = 'i'; @@ -2829,16 +2823,16 @@ diskfull(char *s) name[3] = '0' + fi; name[4] = '\0'; unlink(name); - int fd = open(name, O_CREATE|O_RDWR|O_TRUNC); - if(fd < 0){ + int fd = open(name, O_CREATE | O_RDWR | O_TRUNC); + if(fd < 0) { // oops, ran out of inodes before running out of blocks. printf("%s: could not create file %s\n", s, name); done = 1; break; } - for(int i = 0; i < MAXFILE; i++){ + for(int i = 0; i < MAXFILE; i++) { char buf[BSIZE]; - if(write(fd, buf, BSIZE) != BSIZE){ + if(write(fd, buf, BSIZE) != BSIZE) { done = 1; close(fd); break; @@ -2852,7 +2846,7 @@ diskfull(char *s) // directory content. one of these file creations // is expected to fail. int nzz = 128; - for(int i = 0; i < nzz; i++){ + for(int i = 0; i < nzz; i++) { char name[32]; name[0] = 'z'; name[1] = 'z'; @@ -2860,7 +2854,7 @@ diskfull(char *s) name[3] = '0' + (i % 32); name[4] = '\0'; unlink(name); - int fd = open(name, O_CREATE|O_RDWR|O_TRUNC); + int fd = open(name, O_CREATE | O_RDWR | O_TRUNC); if(fd < 0) break; close(fd); @@ -2872,7 +2866,7 @@ diskfull(char *s) unlink("diskfulldir"); - for(int i = 0; i < nzz; i++){ + for(int i = 0; i < nzz; i++) { char name[32]; name[0] = 'z'; name[1] = 'z'; @@ -2882,7 +2876,7 @@ diskfull(char *s) unlink(name); } - for(int i = 0; i < fi; i++){ + for(int i = 0; i < fi; i++) { char name[32]; name[0] = 'b'; name[1] = 'i'; @@ -2896,8 +2890,8 @@ diskfull(char *s) void outofinodes(char *s) { - int nzz = 32*32; - for(int i = 0; i < nzz; i++){ + int nzz = 32 * 32; + for(int i = 0; i < nzz; i++) { char name[32]; name[0] = 'z'; name[1] = 'z'; @@ -2905,15 +2899,15 @@ outofinodes(char *s) name[3] = '0' + (i % 32); name[4] = '\0'; unlink(name); - int fd = open(name, O_CREATE|O_RDWR|O_TRUNC); - if(fd < 0){ + int fd = open(name, O_CREATE | O_RDWR | O_TRUNC); + if(fd < 0) { // failure is eventually expected. break; } close(fd); } - for(int i = 0; i < nzz; i++){ + for(int i = 0; i < nzz; i++) { char name[32]; name[0] = 'z'; name[1] = 'z'; @@ -2925,14 +2919,14 @@ outofinodes(char *s) } struct test slowtests[] = { - {bigdir, "bigdir"}, - {manywrites, "manywrites"}, - {badwrite, "badwrite" }, - {execout, "execout"}, - {diskfull, "diskfull"}, - {outofinodes, "outofinodes"}, - - { 0, 0}, + { bigdir, "bigdir" }, + { manywrites, "manywrites" }, + { badwrite, "badwrite" }, + { execout, "execout" }, + { diskfull, "diskfull" }, + { outofinodes, "outofinodes" }, + + { 0, 0 }, }; // @@ -2942,7 +2936,8 @@ struct test slowtests[] = { // run each test in its own process. run returns 1 if child's exit() // indicates success. int -run(void f(char *), char *s) { +run(void f(char *), char *s) +{ int pid; int xstatus; @@ -2956,7 +2951,7 @@ run(void f(char *), char *s) { exit(0); } else { wait(&xstatus); - if(xstatus != 0) + if(xstatus != 0) printf("FAILED\n"); else printf("OK\n"); @@ -2965,10 +2960,11 @@ run(void f(char *), char *s) { } int -runtests(struct test *tests, char *justone) { - for (struct test *t = tests; t->s != 0; t++) { +runtests(struct test *tests, char *justone) +{ + for(struct test *t = tests; t->s != 0; t++) { if((justone == 0) || strcmp(t->s, justone) == 0) { - if(!run(t->f, t->s)){ + if(!run(t->f, t->s)) { printf("SOME TESTS FAILED\n"); return 1; } @@ -2977,7 +2973,6 @@ runtests(struct test *tests, char *justone) { return 0; } - // // use sbrk() to count how many free physical memory pages there are. // touches the pages to force allocation. @@ -2989,24 +2984,24 @@ countfree() { int fds[2]; - if(pipe(fds) < 0){ + if(pipe(fds) < 0) { printf("pipe() failed in countfree()\n"); exit(1); } - + int pid = fork(); - if(pid < 0){ + if(pid < 0) { printf("fork failed in countfree()\n"); exit(1); } - if(pid == 0){ + if(pid == 0) { close(fds[0]); - - while(1){ - u64 a = (u64) sbrk(4096); - if(a == 0xffffffffffffffff){ + + while(1) { + u64 a = (u64)sbrk(4096); + if(a == 0xffffffffffffffff) { break; } @@ -3014,7 +3009,7 @@ countfree() *(char *)(a + 4096 - 1) = 1; // report back one more page. - if(write(fds[1], "x", 1) != 1){ + if(write(fds[1], "x", 1) != 1) { printf("write() failed in countfree()\n"); exit(1); } @@ -3026,10 +3021,10 @@ countfree() close(fds[1]); int n = 0; - while(1){ + while(1) { char c; - int cc = read(fds[0], &c, 1); - if(cc < 0){ + int cc = read(fds[0], &c, 1); + if(cc < 0) { printf("read() failed in countfree()\n"); exit(1); } @@ -3039,26 +3034,27 @@ countfree() } close(fds[0]); - wait((int*)0); - + wait((int *)0); + return n; } int -drivetests(int quick, int continuous, char *justone) { +drivetests(int quick, int continuous, char *justone) +{ do { printf("usertests starting\n"); int free0 = countfree(); int free1 = 0; - if (runtests(quicktests, justone)) { + if(runtests(quicktests, justone)) { if(continuous != 2) { return 1; } } if(!quick) { - if (justone == 0) + if(justone == 0) printf("usertests slow tests starting\n"); - if (runtests(slowtests, justone)) { + if(runtests(slowtests, justone)) { if(continuous != 2) { return 1; } @@ -3077,23 +3073,23 @@ drivetests(int quick, int continuous, char *justone) { int main(int argc, char *argv[]) { - int continuous = 0; - int quick = 0; + int continuous = 0; + int quick = 0; char *justone = 0; - if(argc == 2 && strcmp(argv[1], "-q") == 0){ + if(argc == 2 && strcmp(argv[1], "-q") == 0) { quick = 1; - } else if(argc == 2 && strcmp(argv[1], "-c") == 0){ + } else if(argc == 2 && strcmp(argv[1], "-c") == 0) { continuous = 1; - } else if(argc == 2 && strcmp(argv[1], "-C") == 0){ + } else if(argc == 2 && strcmp(argv[1], "-C") == 0) { continuous = 2; - } else if(argc == 2 && argv[1][0] != '-'){ + } else if(argc == 2 && argv[1][0] != '-') { justone = argv[1]; - } else if(argc > 1){ + } else if(argc > 1) { printf("Usage: usertests [-c] [-C] [-q] [testname]\n"); exit(1); } - if (drivetests(quick, continuous, justone)) { + if(drivetests(quick, continuous, justone)) { exit(1); } printf("ALL TESTS PASSED\n"); diff --git a/user/wc.c b/user/wc.c index 6a851ca..7719650 100644 --- a/user/wc.c +++ b/user/wc.c @@ -12,20 +12,20 @@ wc(int fd, char *name) l = w = c = 0; inword = 0; - while((n = read(fd, buf, sizeof(buf))) > 0){ - for(i=0; i 0) { + for(i = 0; i < n; i++) { c++; if(buf[i] == '\n') l++; if(strchr(" \r\t\n\v", buf[i])) inword = 0; - else if(!inword){ + else if(!inword) { w++; inword = 1; } } } - if(n < 0){ + if(n < 0) { printf("wc: read error\n"); exit(1); } @@ -37,13 +37,13 @@ main(int argc, char *argv[]) { int fd, i; - if(argc <= 1){ + if(argc <= 1) { wc(0, ""); exit(0); } - for(i = 1; i < argc; i++){ - if((fd = open(argv[i], 0)) < 0){ + for(i = 1; i < argc; i++) { + if((fd = open(argv[i], 0)) < 0) { printf("wc: cannot open %s\n", argv[i]); exit(1); } diff --git a/user/zombie.c b/user/zombie.c index 8b89a33..3925291 100644 --- a/user/zombie.c +++ b/user/zombie.c @@ -9,6 +9,6 @@ int main(void) { if(fork() > 0) - sleep(5); // Let child exit before parent. + sleep(5); // Let child exit before parent. exit(0); }