Complete reformat

This commit is contained in:
Imbus 2024-06-15 16:55:06 +02:00
parent b1a34398e9
commit d6793bf093
60 changed files with 1952 additions and 1995 deletions

View file

@ -13,7 +13,6 @@
// * Only one process at a time can use a buffer, // * Only one process at a time can use a buffer,
// so do not keep them longer than necessary. // so do not keep them longer than necessary.
#include "types.h" #include "types.h"
#include "param.h" #include "param.h"
#include "spinlock.h" #include "spinlock.h"
@ -137,17 +136,17 @@ brelse(struct buf *b)
} }
void void
bpin(struct buf *b) { bpin(struct buf *b)
{
acquire(&bcache.lock); acquire(&bcache.lock);
b->refcnt++; b->refcnt++;
release(&bcache.lock); release(&bcache.lock);
} }
void void
bunpin(struct buf *b) { bunpin(struct buf *b)
{
acquire(&bcache.lock); acquire(&bcache.lock);
b->refcnt--; b->refcnt--;
release(&bcache.lock); release(&bcache.lock);
} }

View file

@ -9,4 +9,3 @@ struct buf {
struct buf *next; struct buf *next;
u8 data[BSIZE]; u8 data[BSIZE];
}; };

View file

@ -35,7 +35,9 @@ consputc(int c)
{ {
if(c == BACKSPACE) { if(c == BACKSPACE) {
// if the user typed backspace, overwrite with a space. // 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 { } else {
uartputc_sync(c); uartputc_sync(c);
} }
@ -142,8 +144,7 @@ consoleintr(int c)
procdump(); procdump();
break; break;
case C('U'): // Kill line. case C('U'): // Kill line.
while(cons.e != cons.w && while(cons.e != cons.w && cons.buf[(cons.e - 1) % INPUT_BUF_SIZE] != '\n') {
cons.buf[(cons.e-1) % INPUT_BUF_SIZE] != '\n'){
cons.e--; cons.e--;
consputc(BACKSPACE); consputc(BACKSPACE);
} }

View file

@ -9,7 +9,8 @@
static int loadseg(pde_t *, u64, struct inode *, u32, u32); static int loadseg(pde_t *, u64, struct inode *, u32, u32);
int flags2perm(int flags) int
flags2perm(int flags)
{ {
int perm = 0; int perm = 0;
if(flags & 0x1) if(flags & 0x1)

View file

@ -179,4 +179,3 @@ filewrite(struct file *f, u64 addr, int n)
return ret; return ret;
} }

View file

@ -39,7 +39,8 @@ readsb(int dev, struct superblock *sb)
// Init fs // Init fs
void void
fsinit(int dev) { fsinit(int dev)
{
readsb(dev, &sb); readsb(dev, &sb);
if(sb.magic != FSMAGIC) if(sb.magic != FSMAGIC)
panic("invalid file system"); panic("invalid file system");

View file

@ -1,7 +1,6 @@
// On-disk file system format. // On-disk file system format.
// Both the kernel and user programs use this header file. // Both the kernel and user programs use this header file.
#define ROOTINO 1 // root i-number #define ROOTINO 1 // root i-number
#define BSIZE 1024 // block size #define BSIZE 1024 // block size
@ -57,4 +56,3 @@ struct dirent {
u16 inum; u16 inum;
char name[DIRSIZ]; char name[DIRSIZ];
}; };

View file

@ -233,4 +233,3 @@ log_write(struct buf *b)
} }
release(&log.lock); release(&log.lock);
} }

View file

@ -187,16 +187,14 @@ proc_pagetable(struct proc *p)
// at the highest user virtual address. // at the highest user virtual address.
// only the supervisor uses it, on the way // only the supervisor uses it, on the way
// to/from user space, so not PTE_U. // to/from user space, so not PTE_U.
if(mappages(pagetable, TRAMPOLINE, PGSIZE, if(mappages(pagetable, TRAMPOLINE, PGSIZE, (u64)trampoline, PTE_R | PTE_X) < 0) {
(u64)trampoline, PTE_R | PTE_X) < 0){
uvmfree(pagetable, 0); uvmfree(pagetable, 0);
return 0; return 0;
} }
// map the trapframe page just below the trampoline page, for // map the trapframe page just below the trampoline page, for
// trampoline.S. // trampoline.S.
if(mappages(pagetable, TRAPFRAME, PGSIZE, if(mappages(pagetable, TRAPFRAME, PGSIZE, (u64)(p->trapframe), PTE_R | PTE_W) < 0) {
(u64)(p->trapframe), PTE_R | PTE_W) < 0){
uvmunmap(pagetable, TRAMPOLINE, 1, 0); uvmunmap(pagetable, TRAMPOLINE, 1, 0);
uvmfree(pagetable, 0); uvmfree(pagetable, 0);
return 0; return 0;
@ -218,15 +216,10 @@ proc_freepagetable(pagetable_t pagetable, u64 sz)
// a user program that calls exec("/init") // a user program that calls exec("/init")
// assembled from ../user/initcode.S // assembled from ../user/initcode.S
// od -t xC ../user/initcode // od -t xC ../user/initcode
u8 initcode[] = { u8 initcode[]
0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02, = { 0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02, 0x97, 0x05, 0x00, 0x00, 0x93, 0x85, 0x35, 0x02, 0x93, 0x08,
0x97, 0x05, 0x00, 0x00, 0x93, 0x85, 0x35, 0x02, 0x70, 0x00, 0x73, 0x00, 0x00, 0x00, 0x93, 0x08, 0x20, 0x00, 0x73, 0x00, 0x00, 0x00, 0xef, 0xf0, 0x9f, 0xff,
0x93, 0x08, 0x70, 0x00, 0x73, 0x00, 0x00, 0x00, 0x2f, 0x69, 0x6e, 0x69, 0x74, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 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. // Set up first user process.
void void
@ -408,8 +401,7 @@ wait(u64 addr)
if(pp->state == ZOMBIE) { if(pp->state == ZOMBIE) {
// Found one. // Found one.
pid = pp->pid; pid = pp->pid;
if(addr != 0 && copyout(p->pagetable, addr, (char *)&pp->xstate, if(addr != 0 && copyout(p->pagetable, addr, (char *)&pp->xstate, sizeof(pp->xstate)) < 0) {
sizeof(pp->xstate)) < 0) {
release(&pp->lock); release(&pp->lock);
release(&wait_lock); release(&wait_lock);
return -1; return -1;
@ -659,12 +651,7 @@ void
procdump(void) procdump(void)
{ {
static char *states[] = { static char *states[] = {
[UNUSED] "unused", [UNUSED] "unused", [USED] "used", [SLEEPING] "sleep ", [RUNNABLE] "runble", [RUNNING] "run ", [ZOMBIE] "zombie"
[USED] "used",
[SLEEPING] "sleep ",
[RUNNABLE] "runble",
[RUNNING] "run ",
[ZOMBIE] "zombie"
}; };
struct proc *p; struct proc *p;
char *state; char *state;

View file

@ -50,6 +50,3 @@ holdingsleep(struct sleeplock *lk)
release(&lk->lk); release(&lk->lk);
return r; return r;
} }

View file

@ -7,4 +7,3 @@ struct sleeplock {
char *name; // Name of lock. char *name; // Name of lock.
int pid; // Process holding lock int pid; // Process holding lock
}; };

View file

@ -6,4 +6,3 @@ struct spinlock {
char *name; // Name of lock. char *name; // Name of lock.
struct cpu *cpu; // The cpu holding the lock. struct cpu *cpu; // The cpu holding the lock.
}; };

View file

@ -104,4 +104,3 @@ strlen(const char *s)
; ;
return n; return n;
} }

View file

@ -105,26 +105,11 @@ extern u64 sys_close(void);
// An array mapping syscall numbers from syscall.h // An array mapping syscall numbers from syscall.h
// to the function that handles the system call. // to the function that handles the system call.
static u64 (*syscalls[])(void) = { static u64 (*syscalls[])(void) = {
[SYS_fork] sys_fork, [SYS_fork] sys_fork, [SYS_exit] sys_exit, [SYS_wait] sys_wait, [SYS_pipe] sys_pipe,
[SYS_exit] sys_exit, [SYS_read] sys_read, [SYS_kill] sys_kill, [SYS_exec] sys_exec, [SYS_fstat] sys_fstat,
[SYS_wait] sys_wait, [SYS_chdir] sys_chdir, [SYS_dup] sys_dup, [SYS_getpid] sys_getpid, [SYS_sbrk] sys_sbrk,
[SYS_pipe] sys_pipe, [SYS_sleep] sys_sleep, [SYS_uptime] sys_uptime, [SYS_open] sys_open, [SYS_write] sys_write,
[SYS_read] sys_read, [SYS_mknod] sys_mknod, [SYS_unlink] sys_unlink, [SYS_link] sys_link, [SYS_mkdir] sys_mkdir,
[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_close] sys_close,
}; };
@ -140,8 +125,7 @@ syscall(void)
// and store its return value in p->trapframe->a0 // and store its return value in p->trapframe->a0
p->trapframe->a0 = syscalls[num](); p->trapframe->a0 = syscalls[num]();
} else { } else {
printf("%d %s: unknown sys call %d\n", printf("%d %s: unknown sys call %d\n", p->pid, p->name, num);
p->pid, p->name, num);
p->trapframe->a0 = -1; p->trapframe->a0 = -1;
} }
} }

View file

@ -396,8 +396,7 @@ sys_mknod(void)
begin_op(); begin_op();
argint(1, &major); argint(1, &major);
argint(2, &minor); argint(2, &minor);
if((argstr(0, path, MAXPATH)) < 0 || if((argstr(0, path, MAXPATH)) < 0 || (ip = create(path, T_DEVICE, major, minor)) == 0) {
(ip = create(path, T_DEVICE, major, minor)) == 0){
end_op(); end_op();
return -1; return -1;
} }
@ -493,8 +492,8 @@ sys_pipe(void)
fileclose(wf); fileclose(wf);
return -1; return -1;
} }
if(copyout(p->pagetable, fdarray, (char*)&fd0, sizeof(fd0)) < 0 || if(copyout(p->pagetable, fdarray, (char *)&fd0, sizeof(fd0)) < 0
copyout(p->pagetable, fdarray+sizeof(fd0), (char *)&fd1, sizeof(fd1)) < 0){ || copyout(p->pagetable, fdarray + sizeof(fd0), (char *)&fd1, sizeof(fd1)) < 0) {
p->ofile[fd0] = 0; p->ofile[fd0] = 0;
p->ofile[fd1] = 0; p->ofile[fd1] = 0;
fileclose(rf); fileclose(rf);

View file

@ -179,8 +179,7 @@ devintr()
{ {
u64 scause = r_scause(); u64 scause = r_scause();
if((scause & 0x8000000000000000L) && if((scause & 0x8000000000000000L) && (scause & 0xff) == 9) {
(scause & 0xff) == 9){
// this is a supervisor external interrupt, via PLIC. // this is a supervisor external interrupt, via PLIC.
// irq indicates which device interrupted. // irq indicates which device interrupted.
@ -218,4 +217,3 @@ devintr()
return 0; return 0;
} }
} }

View file

@ -103,7 +103,6 @@ uartputc(int c)
release(&uart_tx_lock); 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 // use interrupts, for use by kernel printf() and
// to echo characters. it spins waiting for the uart's // to echo characters. it spins waiting for the uart's

View file

@ -65,10 +65,8 @@ virtio_disk_init(void)
initlock(&disk.vdisk_lock, "virtio_disk"); initlock(&disk.vdisk_lock, "virtio_disk");
if(*R(VIRTIO_MMIO_MAGIC_VALUE) != 0x74726976 || if(*R(VIRTIO_MMIO_MAGIC_VALUE) != 0x74726976 || *R(VIRTIO_MMIO_VERSION) != 2 || *R(VIRTIO_MMIO_DEVICE_ID) != 2
*R(VIRTIO_MMIO_VERSION) != 2 || || *R(VIRTIO_MMIO_VENDOR_ID) != 0x554d4551) {
*R(VIRTIO_MMIO_DEVICE_ID) != 2 ||
*R(VIRTIO_MMIO_VENDOR_ID) != 0x554d4551){
panic("could not find virtio disk"); panic("could not find virtio disk");
} }

View file

@ -12,7 +12,12 @@
#include "kernel/param.h" #include "kernel/param.h"
#ifndef static_assert #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 #endif
#define NINODES 200 #define NINODES 200
@ -32,7 +37,6 @@ char zeroes[BSIZE];
uint freeinode = 1; uint freeinode = 1;
uint freeblock; uint freeblock;
void balloc(int); void balloc(int);
void wsect(uint, void *); void wsect(uint, void *);
void winode(uint, struct dinode *); void winode(uint, struct dinode *);
@ -74,7 +78,6 @@ main(int argc, char *argv[])
char buf[BSIZE]; char buf[BSIZE];
struct dinode din; struct dinode din;
static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); static_assert(sizeof(int) == 4, "Integers must be 4 bytes!");
if(argc < 2) { if(argc < 2) {
@ -102,8 +105,8 @@ main(int argc, char *argv[])
sb.inodestart = xint(2 + nlog); sb.inodestart = xint(2 + nlog);
sb.bmapstart = xint(2 + nlog + ninodeblocks); 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", printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n", nmeta, nlog,
nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE); ninodeblocks, nbitmap, nblocks, FSSIZE);
freeblock = nmeta; // the first free block that we can allocate freeblock = nmeta; // the first free block that we can allocate

View file

@ -81,7 +81,8 @@ match(char *re, char *text)
} }
// matchhere: search for re at beginning of text // matchhere: search for re at beginning of text
int matchhere(char *re, char *text) int
matchhere(char *re, char *text)
{ {
if(re[0] == '\0') if(re[0] == '\0')
return 1; return 1;
@ -95,7 +96,8 @@ int matchhere(char *re, char *text)
} }
// matchstar: search for c*re at beginning of text // 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)) if(matchhere(re, text))
@ -103,4 +105,3 @@ int matchstar(int c, char *re, char *text)
} while(*text != '\0' && (*text++ == c || c == '.')); } while(*text != '\0' && (*text++ == c || c == '.'));
return 0; return 0;
} }

View file

@ -39,7 +39,8 @@ printint(int fd, int xx, int base, int sgn)
} }
static void static void
printptr(int fd, u64 x) { printptr(int fd, u64 x)
{
int i; int i;
putc(fd, '0'); putc(fd, '0');
putc(fd, 'x'); putc(fd, 'x');

View file

@ -419,7 +419,6 @@ truncate3(char *s)
exit(xstatus); exit(xstatus);
} }
// does chdir() call iput(p->cwd) in a transaction? // does chdir() call iput(p->cwd) in a transaction?
void void
iputtest(char *s) iputtest(char *s)
@ -616,8 +615,7 @@ writebig(char *s)
exit(1); exit(1);
} }
if(((int *)buf)[0] != n) { if(((int *)buf)[0] != n) {
printf("%s: read content of block %d is %d\n", s, printf("%s: read content of block %d is %d\n", s, n, ((int *)buf)[0]);
n, ((int*)buf)[0]);
exit(1); exit(1);
} }
n++; n++;
@ -652,7 +650,8 @@ createtest(char *s)
} }
} }
void dirtest(char *s) void
dirtest(char *s)
{ {
if(mkdir("dir0") < 0) { if(mkdir("dir0") < 0) {
printf("%s: mkdir failed\n", s); printf("%s: mkdir failed\n", s);
@ -727,7 +726,6 @@ exectest(char *s)
printf("%s: wrong output\n", s); printf("%s: wrong output\n", s);
exit(1); exit(1);
} }
} }
// simple fork and pipe read/write // simple fork and pipe read/write
@ -785,7 +783,6 @@ pipe1(char *s)
} }
} }
// test if child is killed (status = -1) // test if child is killed (status = -1)
void void
killstatus(char *s) killstatus(char *s)
@ -1463,8 +1460,7 @@ concreate(char *s)
printf("%s: fork failed\n", s); printf("%s: fork failed\n", s);
exit(1); exit(1);
} }
if(((i % 3) == 0 && pid == 0) || if(((i % 3) == 0 && pid == 0) || ((i % 3) == 1 && pid != 0)) {
((i % 3) == 1 && pid != 0)){
close(open(file, 0)); close(open(file, 0));
close(open(file, 0)); close(open(file, 0));
close(open(file, 0)); close(open(file, 0));
@ -1518,7 +1514,6 @@ linkunlink(char *s)
exit(0); exit(0);
} }
void void
subdir(char *s) subdir(char *s)
{ {
@ -1724,7 +1719,6 @@ bigwrite(char *s)
} }
} }
void void
bigfile(char *s) bigfile(char *s)
{ {
@ -2188,7 +2182,8 @@ sbrkfail(char *s)
sbrk(BIG - (u64)sbrk(0)); sbrk(BIG - (u64)sbrk(0));
write(fds[1], "x", 1); write(fds[1], "x", 1);
// sit around until killed // sit around until killed
for(;;) sleep(1000); for(;;)
sleep(1000);
} }
if(pids[i] != -1) if(pids[i] != -1)
read(fds[0], &scratch, 1); read(fds[0], &scratch, 1);
@ -2234,7 +2229,6 @@ sbrkfail(char *s)
exit(1); exit(1);
} }
// test reads/writes from/to allocated memory // test reads/writes from/to allocated memory
void void
sbrkarg(char *s) sbrkarg(char *s)
@ -2308,7 +2302,9 @@ bigargtest(char *s)
static char *args[MAXARG]; static char *args[MAXARG];
int i; int i;
for(i = 0; i < MAXARG - 1; i++) for(i = 0; i < MAXARG - 1; i++)
args[i] = "bigargs test: failed\n "; args[i] = "bigargs test: failed\n "
" "
" ";
args[MAXARG - 1] = 0; args[MAXARG - 1] = 0;
exec("echo", args); exec("echo", args);
fd = open("bigarg-ok", O_CREATE); fd = open("bigarg-ok", O_CREATE);
@ -2383,7 +2379,8 @@ fsfull()
printf("fsfull test finished\n"); printf("fsfull test finished\n");
} }
void argptest(char *s) void
argptest(char *s)
{ {
int fd; int fd;
fd = open("init", O_RDONLY); fd = open("init", O_RDONLY);
@ -2543,7 +2540,6 @@ sbrklast(char *s)
exit(1); exit(1);
} }
// does sbrk handle signed int32 wrap-around with // does sbrk handle signed int32 wrap-around with
// negative arguments? // negative arguments?
void void
@ -2554,8 +2550,6 @@ sbrk8000(char *s)
*(top - 1) = *(top - 1) + 1; *(top - 1) = *(top - 1) + 1;
} }
// regression test. test whether exec() leaks memory if one of the // regression test. test whether exec() leaks memory if one of the
// arguments is invalid. the test passes if the kernel doesn't panic. // arguments is invalid. the test passes if the kernel doesn't panic.
void void
@ -2942,7 +2936,8 @@ struct test slowtests[] = {
// run each test in its own process. run returns 1 if child's exit() // run each test in its own process. run returns 1 if child's exit()
// indicates success. // indicates success.
int int
run(void f(char *), char *s) { run(void f(char *), char *s)
{
int pid; int pid;
int xstatus; int xstatus;
@ -2965,7 +2960,8 @@ run(void f(char *), char *s) {
} }
int int
runtests(struct test *tests, char *justone) { runtests(struct test *tests, char *justone)
{
for(struct test *t = tests; t->s != 0; t++) { for(struct test *t = tests; t->s != 0; t++) {
if((justone == 0) || strcmp(t->s, justone) == 0) { if((justone == 0) || strcmp(t->s, justone) == 0) {
if(!run(t->f, t->s)) { if(!run(t->f, t->s)) {
@ -2977,7 +2973,6 @@ runtests(struct test *tests, char *justone) {
return 0; return 0;
} }
// //
// use sbrk() to count how many free physical memory pages there are. // use sbrk() to count how many free physical memory pages there are.
// touches the pages to force allocation. // touches the pages to force allocation.
@ -3045,7 +3040,8 @@ countfree()
} }
int int
drivetests(int quick, int continuous, char *justone) { drivetests(int quick, int continuous, char *justone)
{
do { do {
printf("usertests starting\n"); printf("usertests starting\n");
int free0 = countfree(); int free0 = countfree();