diff options
| -rw-r--r-- | .gitignore | 10 | ||||
| -rw-r--r-- | Makefile | 227 | ||||
| -rw-r--r-- | kernel/defs.h | 49 | ||||
| -rw-r--r-- | kernel/file.c | 21 | ||||
| -rw-r--r-- | kernel/file.h | 8 | ||||
| -rw-r--r-- | kernel/main.c | 12 | ||||
| -rw-r--r-- | kernel/memlayout.h | 17 | ||||
| -rw-r--r-- | kernel/plic.c | 12 | ||||
| -rw-r--r-- | kernel/spinlock.c | 111 | ||||
| -rw-r--r-- | kernel/syscall.c | 15 | ||||
| -rw-r--r-- | kernel/syscall.h | 11 | ||||
| -rw-r--r-- | kernel/sysfile.c | 26 | ||||
| -rw-r--r-- | kernel/trap.c | 11 | ||||
| -rw-r--r-- | kernel/vm.c | 52 | ||||
| -rw-r--r-- | user/user.h | 9 | ||||
| -rwxr-xr-x | user/usys.pl | 2 | 
16 files changed, 552 insertions, 41 deletions
| @@ -15,3 +15,13 @@ mkfs  kernel/kernel  user/usys.S  .gdbinit +*.zip +xv6.out* +.vagrant/ +submissions/ +ph +barrier +/lab-*.json +.DS_Store +*.dSYM +*.pcap @@ -1,14 +1,16 @@ + +# To compile and run with a lab solution, set the lab name in conf/lab.mk +# (e.g., LAB=util).  Run make grade to test solution with the lab's +# grade script (e.g., grade-lab-util). + +-include conf/lab.mk +  K=kernel  U=user  OBJS = \    $K/entry.o \ -  $K/start.o \ -  $K/console.o \ -  $K/printf.o \ -  $K/uart.o \    $K/kalloc.o \ -  $K/spinlock.o \    $K/string.o \    $K/main.o \    $K/vm.o \ @@ -30,6 +32,34 @@ OBJS = \    $K/plic.o \    $K/virtio_disk.o +OBJS_KCSAN = \ +  $K/start.o \ +  $K/console.o \ +  $K/printf.o \ +  $K/uart.o \ +  $K/spinlock.o + +ifdef KCSAN +OBJS_KCSAN += \ +	$K/kcsan.o +endif + +ifeq ($(LAB),$(filter $(LAB), lock)) +OBJS += \ +	$K/stats.o\ +	$K/sprintf.o +endif + + +ifeq ($(LAB),net) +OBJS += \ +	$K/e1000.o \ +	$K/net.o \ +	$K/sysnet.o \ +	$K/pci.o +endif + +  # riscv64-unknown-elf- or riscv64-linux-gnu-  # perhaps in /opt/riscv/bin  #TOOLPREFIX =  @@ -57,12 +87,28 @@ OBJCOPY = $(TOOLPREFIX)objcopy  OBJDUMP = $(TOOLPREFIX)objdump  CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2 + +ifdef LAB +LABUPPER = $(shell echo $(LAB) | tr a-z A-Z) +XCFLAGS += -DSOL_$(LABUPPER) -DLAB_$(LABUPPER) +endif + +CFLAGS += $(XCFLAGS)  CFLAGS += -MD  CFLAGS += -mcmodel=medany  CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax  CFLAGS += -I.  CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) +ifeq ($(LAB),net) +CFLAGS += -DNET_TESTS_PORT=$(SERVERPORT) +endif + +ifdef KCSAN +CFLAGS += -DKCSAN +KCSANFLAG = -fsanitize=thread -fno-inline +endif +  # Disable PIE when possible (for Ubuntu 16.10 toolchain)  ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)  CFLAGS += -fno-pie -no-pie @@ -73,11 +119,17 @@ endif  LDFLAGS = -z max-page-size=4096 -$K/kernel: $(OBJS) $K/kernel.ld $U/initcode -	$(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS)  +$K/kernel: $(OBJS) $(OBJS_KCSAN) $K/kernel.ld $U/initcode +	$(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) $(OBJS_KCSAN)  	$(OBJDUMP) -S $K/kernel > $K/kernel.asm  	$(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym +$(OBJS): EXTRAFLAG := $(KCSANFLAG) + +$K/%.o: $K/%.c +	$(CC) $(CFLAGS) $(EXTRAFLAG) -c -o $@ $< + +  $U/initcode: $U/initcode.S  	$(CC) $(CFLAGS) -march=rv64g -nostdinc -I. -Ikernel -c $U/initcode.S -o $U/initcode.o  	$(LD) $(LDFLAGS) -N -e start -Ttext 0 -o $U/initcode.out $U/initcode.o @@ -89,6 +141,10 @@ tags: $(OBJS) _init  ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o +ifeq ($(LAB),$(filter $(LAB), lock)) +ULIB += $U/statistics.o +endif +  _%: %.o $(ULIB)  	$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^  	$(OBJDUMP) -S $@ > $*.asm @@ -107,7 +163,7 @@ $U/_forktest: $U/forktest.o $(ULIB)  	$(OBJDUMP) -S $U/_forktest > $U/forktest.asm  mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h -	gcc -Werror -Wall -I. -o mkfs/mkfs mkfs/mkfs.c +	gcc $(XCFLAGS) -Werror -Wall -I. -o mkfs/mkfs mkfs/mkfs.c  # Prevent deletion of intermediate files, e.g. cat.o, after first build, so  # that disk image changes after first build are persistent until clean.  More @@ -133,18 +189,89 @@ UPROGS=\  	$U/_wc\  	$U/_zombie\ -fs.img: mkfs/mkfs README $(UPROGS) -	mkfs/mkfs fs.img README $(UPROGS) + + + +ifeq ($(LAB),$(filter $(LAB), lock)) +UPROGS += \ +	$U/_stats +endif + +ifeq ($(LAB),traps) +UPROGS += \ +	$U/_call\ +	$U/_bttest +endif + +ifeq ($(LAB),lazy) +UPROGS += \ +	$U/_lazytests +endif + +ifeq ($(LAB),cow) +UPROGS += \ +	$U/_cowtest +endif + +ifeq ($(LAB),thread) +UPROGS += \ +	$U/_uthread + +$U/uthread_switch.o : $U/uthread_switch.S +	$(CC) $(CFLAGS) -c -o $U/uthread_switch.o $U/uthread_switch.S + +$U/_uthread: $U/uthread.o $U/uthread_switch.o $(ULIB) +	$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_uthread $U/uthread.o $U/uthread_switch.o $(ULIB) +	$(OBJDUMP) -S $U/_uthread > $U/uthread.asm + +ph: notxv6/ph.c +	gcc -o ph -g -O2 $(XCFLAGS) notxv6/ph.c -pthread + +barrier: notxv6/barrier.c +	gcc -o barrier -g -O2 $(XCFLAGS) notxv6/barrier.c -pthread +endif + +ifeq ($(LAB),pgtbl) +UPROGS += \ +	$U/_pgtbltest +endif + +ifeq ($(LAB),lock) +UPROGS += \ +	$U/_kalloctest\ +	$U/_bcachetest +endif + +ifeq ($(LAB),fs) +UPROGS += \ +	$U/_bigfile +endif + + + +ifeq ($(LAB),net) +UPROGS += \ +	$U/_nettests +endif + +UEXTRA= +ifeq ($(LAB),util) +	UEXTRA += user/xargstest.sh +endif + + +fs.img: mkfs/mkfs README $(UEXTRA) $(UPROGS) +	mkfs/mkfs fs.img README $(UEXTRA) $(UPROGS)  -include kernel/*.d user/*.d -clean:  -	rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ +clean: +	rm -rf *.tex *.dvi *.idx *.aux *.log *.ind *.ilg *.dSYM *.zip *.pcap \  	*/*.o */*.d */*.asm */*.sym \ -	$U/initcode $U/initcode.out $K/kernel fs.img \ -	mkfs/mkfs .gdbinit \ -        $U/usys.S \ -	$(UPROGS) +	$U/initcode $U/initcode.out $U/usys.S $U/_* \ +	$K/kernel \ +	mkfs/mkfs fs.img .gdbinit __pycache__ xv6.out* \ +	ph barrier  # try to generate a unique GDB port  GDBPORT = $(shell expr `id -u` % 5000 + 25000) @@ -155,12 +282,22 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \  ifndef CPUS  CPUS := 3  endif +ifeq ($(LAB),fs) +CPUS := 1 +endif + +FWDPORT = $(shell expr `id -u` % 5000 + 25999)  QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic  QEMUOPTS += -global virtio-mmio.force-legacy=false  QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0  QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 +ifeq ($(LAB),net) +QEMUOPTS += -netdev user,id=net0,hostfwd=udp::$(FWDPORT)-:2000 -object filter-dump,id=net0,netdev=net0,file=packets.pcap +QEMUOPTS += -device e1000,netdev=net0,bus=pcie.0 +endif +  qemu: $K/kernel fs.img  	$(QEMU) $(QEMUOPTS) @@ -171,3 +308,61 @@ qemu-gdb: $K/kernel .gdbinit fs.img  	@echo "*** Now run 'gdb' in another window." 1>&2  	$(QEMU) $(QEMUOPTS) -S $(QEMUGDB) +ifeq ($(LAB),net) +# try to generate a unique port for the echo server +SERVERPORT = $(shell expr `id -u` % 5000 + 25099) + +server: +	python3 server.py $(SERVERPORT) + +ping: +	python3 ping.py $(FWDPORT) +endif + +## +##  FOR testing lab grading script +## + +ifneq ($(V),@) +GRADEFLAGS += -v +endif + +print-gdbport: +	@echo $(GDBPORT) + +grade: +	@echo $(MAKE) clean +	@$(MAKE) clean || \ +          (echo "'make clean' failed.  HINT: Do you have another running instance of xv6?" && exit 1) +	./grade-lab-$(LAB) $(GRADEFLAGS) + +## +## FOR submissions +## + +submit-check: +	@if ! test -d .git; then \ +		echo No .git directory, is this a git repository?; \ +		false; \ +	fi +	@if test "$$(git symbolic-ref HEAD)" != refs/heads/$(LAB); then \ +		git branch; \ +		read -p "You are not on the $(LAB) branch.  Hand-in the current branch? [y/N] " r; \ +		test "$$r" = y; \ +	fi +	@if ! git diff-files --quiet || ! git diff-index --quiet --cached HEAD; then \ +		git status -s; \ +		echo; \ +		echo "You have uncomitted changes.  Please commit or stash them."; \ +		false; \ +	fi +	@if test -n "`git status -s`"; then \ +		git status -s; \ +		read -p "Untracked files will not be handed in.  Continue? [y/N] " r; \ +		test "$$r" = y; \ +	fi + +zipball: clean submit-check +	git archive --verbose --format zip --output lab.zip HEAD + +.PHONY: zipball clean grade submit-check diff --git a/kernel/defs.h b/kernel/defs.h index a3c962b..c6fef8c 100644 --- a/kernel/defs.h +++ b/kernel/defs.h @@ -8,6 +8,10 @@ struct spinlock;  struct sleeplock;  struct stat;  struct superblock; +#ifdef LAB_NET +struct mbuf; +struct sock; +#endif  // bio.c  void            binit(void); @@ -117,6 +121,10 @@ void            initlock(struct spinlock*, char*);  void            release(struct spinlock*);  void            push_off(void);  void            pop_off(void); +int             atomic_read4(int *addr); +#ifdef LAB_LOCK +void            freelock(struct spinlock*); +#endif  // sleeplock.c  void            acquiresleep(struct sleeplock*); @@ -187,3 +195,44 @@ void            virtio_disk_intr(void);  // number of elements in fixed-size array  #define NELEM(x) (sizeof(x)/sizeof((x)[0])) + + + +#ifdef LAB_PGTBL +// vmcopyin.c +int             copyin_new(pagetable_t, char *, uint64, uint64); +int             copyinstr_new(pagetable_t, char *, uint64, uint64); +#endif + +// stats.c +void            statsinit(void); +void            statsinc(void); + +// sprintf.c +int             snprintf(char*, int, char*, ...); + +#ifdef KCSAN +void            kcsaninit(); +#endif + +#ifdef LAB_NET +// pci.c +void            pci_init(); + +// e1000.c +void            e1000_init(uint32 *); +void            e1000_intr(void); +int             e1000_transmit(struct mbuf*); + +// net.c +void            net_rx(struct mbuf*); +void            net_tx_udp(struct mbuf*, uint32, uint16, uint16); + +// sysnet.c +void            sockinit(void); +int             sockalloc(struct file **, uint32, uint16, uint16); +void            sockclose(struct sock *); +int             sockread(struct sock *, uint64, int); +int             sockwrite(struct sock *, uint64, int); +void            sockrecvudp(struct mbuf*, uint32, uint16, uint16); +#endif diff --git a/kernel/file.c b/kernel/file.c index 25fa226..0fba21b 100644 --- a/kernel/file.c +++ b/kernel/file.c @@ -80,6 +80,11 @@ fileclose(struct file *f)      iput(ff.ip);      end_op();    } +#ifdef LAB_NET +  else if(ff.type == FD_SOCK){ +    sockclose(ff.sock); +  } +#endif  }  // Get metadata about file f. @@ -122,7 +127,13 @@ fileread(struct file *f, uint64 addr, int n)      if((r = readi(f->ip, 1, addr, f->off, n)) > 0)        f->off += r;      iunlock(f->ip); -  } else { +  } +#ifdef LAB_NET +  else if(f->type == FD_SOCK){ +    r = sockread(f->sock, addr, n); +  } +#endif +  else {      panic("fileread");    } @@ -173,7 +184,13 @@ filewrite(struct file *f, uint64 addr, int n)        i += r;      }      ret = (i == n ? n : -1); -  } else { +  } +#ifdef LAB_NET +  else if(f->type == FD_SOCK){ +    ret = sockwrite(f->sock, addr, n); +  } +#endif +  else {      panic("filewrite");    } diff --git a/kernel/file.h b/kernel/file.h index b076d1d..1eb5107 100644 --- a/kernel/file.h +++ b/kernel/file.h @@ -1,10 +1,17 @@  struct file { +#ifdef LAB_NET +  enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE, FD_SOCK } type; +#else    enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type; +#endif    int ref; // reference count    char readable;    char writable;    struct pipe *pipe; // FD_PIPE    struct inode *ip;  // FD_INODE and FD_DEVICE +#ifdef LAB_NET +  struct sock *sock; // FD_SOCK +#endif    uint off;          // FD_INODE    short major;       // FD_DEVICE  }; @@ -38,3 +45,4 @@ struct devsw {  extern struct devsw devsw[];  #define CONSOLE 1 +#define STATS   2 diff --git a/kernel/main.c b/kernel/main.c index f0d3171..48c9555 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -12,6 +12,9 @@ main()  {    if(cpuid() == 0){      consoleinit(); +#if defined(LAB_LOCK) +    statsinit(); +#endif      printfinit();      printf("\n");      printf("xv6 kernel is booting\n"); @@ -28,11 +31,18 @@ main()      iinit();         // inode table      fileinit();      // file table      virtio_disk_init(); // emulated hard disk +#ifdef LAB_NET +    pci_init(); +    sockinit(); +#endif          userinit();      // first user process +#ifdef KCSAN +    kcsaninit(); +#endif      __sync_synchronize();      started = 1;    } else { -    while(started == 0) +    while(atomic_read4((int *) &started) == 0)        ;      __sync_synchronize();      printf("hart %d starting\n", cpuid()); diff --git a/kernel/memlayout.h b/kernel/memlayout.h index cac3cb1..74d2fd4 100644 --- a/kernel/memlayout.h +++ b/kernel/memlayout.h @@ -25,6 +25,10 @@  #define VIRTIO0 0x10001000  #define VIRTIO0_IRQ 1 +#ifdef LAB_NET +#define E1000_IRQ 33 +#endif +  // core local interruptor (CLINT), which contains the timer.  #define CLINT 0x2000000L  #define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8*(hartid)) @@ -34,8 +38,11 @@  #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 @@ -50,7 +57,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)*2*PGSIZE - 3*PGSIZE)  // User memory layout.  // Address zero first: @@ -59,6 +66,14 @@  //   fixed-size stack  //   expandable heap  //   ... +//   USYSCALL (shared with kernel)  //   TRAPFRAME (p->trapframe, used by the trampoline)  //   TRAMPOLINE (the same page as in the kernel)  #define TRAPFRAME (TRAMPOLINE - PGSIZE) +#ifdef LAB_PGTBL +#define USYSCALL (TRAPFRAME - PGSIZE) + +struct usyscall { +  int pid;  // Process ID +}; +#endif diff --git a/kernel/plic.c b/kernel/plic.c index 4175db9..5c9d96a 100644 --- a/kernel/plic.c +++ b/kernel/plic.c @@ -14,6 +14,13 @@ plicinit(void)    // set desired IRQ priorities non-zero (otherwise disabled).    *(uint32*)(PLIC + UART0_IRQ*4) = 1;    *(uint32*)(PLIC + VIRTIO0_IRQ*4) = 1; +   +#ifdef LAB_NET +  // PCIE IRQs are 32 to 35 +  for(int irq = 1; irq < 0x35; irq++){ +    *(uint32*)(PLIC + irq*4) = 1; +  } +#endif    }  void @@ -25,6 +32,11 @@ plicinithart(void)    // for the uart and virtio disk.    *(uint32*)PLIC_SENABLE(hart) = (1 << UART0_IRQ) | (1 << VIRTIO0_IRQ); +#ifdef LAB_NET +  // hack to get at next 32 IRQs for e1000 +  *(uint32*)(PLIC_SENABLE(hart)+4) = 0xffffffff; +#endif +      // set this hart's S-mode priority threshold to 0.    *(uint32*)PLIC_SPRIORITY(hart) = 0;  } diff --git a/kernel/spinlock.c b/kernel/spinlock.c index 9840302..266a698 100644 --- a/kernel/spinlock.c +++ b/kernel/spinlock.c @@ -8,12 +8,52 @@  #include "proc.h"  #include "defs.h" +#ifdef LAB_LOCK +#define NLOCK 500 + +static struct spinlock *locks[NLOCK]; +struct spinlock lock_locks; + +void +freelock(struct spinlock *lk) +{ +  acquire(&lock_locks); +  int i; +  for (i = 0; i < NLOCK; i++) { +    if(locks[i] == lk) { +      locks[i] = 0; +      break; +    } +  } +  release(&lock_locks); +} + +static void +findslot(struct spinlock *lk) { +  acquire(&lock_locks); +  int i; +  for (i = 0; i < NLOCK; i++) { +    if(locks[i] == 0) { +      locks[i] = lk; +      release(&lock_locks); +      return; +    } +  } +  panic("findslot"); +} +#endif +  void  initlock(struct spinlock *lk, char *name)  {    lk->name = name;    lk->locked = 0;    lk->cpu = 0; +#ifdef LAB_LOCK +  lk->nts = 0; +  lk->n = 0; +  findslot(lk); +#endif    }  // Acquire the lock. @@ -25,12 +65,21 @@ acquire(struct spinlock *lk)    if(holding(lk))      panic("acquire"); +#ifdef LAB_LOCK +    __sync_fetch_and_add(&(lk->n), 1); +#endif       +    // On RISC-V, sync_lock_test_and_set turns into an atomic swap:    //   a5 = 1    //   s1 = &lk->locked    //   amoswap.w.aq a5, a5, (s1) -  while(__sync_lock_test_and_set(&lk->locked, 1) != 0) -    ; +  while(__sync_lock_test_and_set(&lk->locked, 1) != 0) { +#ifdef LAB_LOCK +    __sync_fetch_and_add(&(lk->nts), 1); +#else +   ; +#endif +  }    // Tell the C compiler and the processor to not move loads or stores    // past this point, to ensure that the critical section's memory @@ -108,3 +157,61 @@ pop_off(void)    if(c->noff == 0 && c->intena)      intr_on();  } + +// Read a shared 32-bit value without holding a lock +int +atomic_read4(int *addr) { +  uint32 val; +  __atomic_load(addr, &val, __ATOMIC_SEQ_CST); +  return val; +} + +#ifdef LAB_LOCK +int +snprint_lock(char *buf, int sz, struct spinlock *lk) +{ +  int n = 0; +  if(lk->n > 0) { +    n = snprintf(buf, sz, "lock: %s: #test-and-set %d #acquire() %d\n", +                 lk->name, lk->nts, lk->n); +  } +  return n; +} + +int +statslock(char *buf, int sz) { +  int n; +  int tot = 0; + +  acquire(&lock_locks); +  n = snprintf(buf, sz, "--- lock kmem/bcache stats\n"); +  for(int i = 0; i < NLOCK; i++) { +    if(locks[i] == 0) +      break; +    if(strncmp(locks[i]->name, "bcache", strlen("bcache")) == 0 || +       strncmp(locks[i]->name, "kmem", strlen("kmem")) == 0) { +      tot += locks[i]->nts; +      n += snprint_lock(buf +n, sz-n, locks[i]); +    } +  } +   +  n += snprintf(buf+n, sz-n, "--- top 5 contended locks:\n"); +  int last = 100000000; +  // stupid way to compute top 5 contended locks +  for(int t = 0; t < 5; t++) { +    int top = 0; +    for(int i = 0; i < NLOCK; i++) { +      if(locks[i] == 0) +        break; +      if(locks[i]->nts > locks[top]->nts && locks[i]->nts < last) { +        top = i; +      } +    } +    n += snprint_lock(buf+n, sz-n, locks[top]); +    last = locks[top]->nts; +  } +  n += snprintf(buf+n, sz-n, "tot= %d\n", tot); +  release(&lock_locks);   +  return n; +} +#endif diff --git a/kernel/syscall.c b/kernel/syscall.c index ed65409..beea0ef 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -102,6 +102,13 @@ extern uint64 sys_link(void);  extern uint64 sys_mkdir(void);  extern uint64 sys_close(void); +#ifdef LAB_NET +extern uint64 sys_connect(void); +#endif +#ifdef LAB_PGTBL +extern uint64 sys_pgaccess(void); +#endif +  // An array mapping syscall numbers from syscall.h  // to the function that handles the system call.  static uint64 (*syscalls[])(void) = { @@ -126,8 +133,16 @@ static uint64 (*syscalls[])(void) = {  [SYS_link]    sys_link,  [SYS_mkdir]   sys_mkdir,  [SYS_close]   sys_close, +#ifdef LAB_NET +[SYS_connect] sys_connect, +#endif +#ifdef LAB_PGTBL +[SYS_pgaccess] sys_pgaccess, +#endif  }; + +  void  syscall(void)  { diff --git a/kernel/syscall.h b/kernel/syscall.h index bc5f356..8da572e 100644 --- a/kernel/syscall.h +++ b/kernel/syscall.h @@ -20,3 +20,14 @@  #define SYS_link   19  #define SYS_mkdir  20  #define SYS_close  21 + +// System calls for labs +#define SYS_trace     22 +#define SYS_sysinfo   23 +#define SYS_sigalarm  24 +#define SYS_sigreturn 25 +#define SYS_symlink   26 +#define SYS_mmap      27 +#define SYS_munmap    28 +#define SYS_connect   29 +#define SYS_pgaccess  30 diff --git a/kernel/sysfile.c b/kernel/sysfile.c index 16b668c..4b2189a 100644 --- a/kernel/sysfile.c +++ b/kernel/sysfile.c @@ -503,3 +503,29 @@ sys_pipe(void)    }    return 0;  } + + +#ifdef LAB_NET +int +sys_connect(void) +{ +  struct file *f; +  int fd; +  uint32 raddr; +  uint32 rport; +  uint32 lport; + +  argint(0, (int*)&raddr); +  argint(1, (int*)&lport); +  argint(2, (int*)&rport); + +  if(sockalloc(&f, raddr, lport, rport) < 0) +    return -1; +  if((fd=fdalloc(f)) < 0){ +    fileclose(f); +    return -1; +  } + +  return fd; +} +#endif diff --git a/kernel/trap.c b/kernel/trap.c index 512c850..5332dda 100644 --- a/kernel/trap.c +++ b/kernel/trap.c @@ -68,6 +68,8 @@ usertrap(void)    } else if((which_dev = devintr()) != 0){      // ok    } else { + +          printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);      printf("            sepc=%p stval=%p\n", r_sepc(), r_stval());      setkilled(p); @@ -75,6 +77,7 @@ usertrap(void)    if(killed(p))      exit(-1); +      // give up the CPU if this is a timer interrupt.    if(which_dev == 2) @@ -190,7 +193,13 @@ devintr()        uartintr();      } else if(irq == VIRTIO0_IRQ){        virtio_disk_intr(); -    } else if(irq){ +    } +#ifdef LAB_NET +    else if(irq == E1000_IRQ){ +      e1000_intr(); +    } +#endif +    else if(irq){        printf("unexpected interrupt irq=%d\n", irq);      } diff --git a/kernel/vm.c b/kernel/vm.c index 5c31e87..7119242 100644 --- a/kernel/vm.c +++ b/kernel/vm.c @@ -4,6 +4,8 @@  #include "elf.h"  #include "riscv.h"  #include "defs.h" +#include "spinlock.h" +#include "proc.h"  #include "fs.h"  /* @@ -30,6 +32,14 @@ kvmmake(void)    // virtio mmio disk interface    kvmmap(kpgtbl, VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W); +#ifdef LAB_NET +  // PCI-E ECAM (configuration space), for pci.c +  kvmmap(kpgtbl, 0x30000000L, 0x30000000L, 0x10000000, PTE_R | PTE_W); + +  // pci.c maps the e1000's registers here. +  kvmmap(kpgtbl, 0x40000000L, 0x40000000L, 0x20000, PTE_R | PTE_W); +#endif   +    // PLIC    kvmmap(kpgtbl, PLIC, PLIC, 0x400000, PTE_R | PTE_W); @@ -136,9 +146,8 @@ kvmmap(pagetable_t kpgtbl, uint64 va, uint64 pa, uint64 sz, int perm)  }  // Create PTEs for virtual addresses starting at va that refer to -// physical addresses starting at pa. -// va and size MUST be page-aligned. -// Returns 0 on success, -1 if walk() couldn't +// physical addresses starting at pa. va and size might not +// be page-aligned. Returns 0 on success, -1 if walk() couldn't  // allocate a needed page-table page.  int  mappages(pagetable_t pagetable, uint64 va, uint64 size, uint64 pa, int perm) @@ -146,17 +155,11 @@ mappages(pagetable_t pagetable, uint64 va, uint64 size, uint64 pa, int perm)    uint64 a, last;    pte_t *pte; -  if((va % PGSIZE) != 0) -    panic("mappages: va not aligned"); - -  if((size % PGSIZE) != 0) -    panic("mappages: size not aligned"); -    if(size == 0)      panic("mappages: size"); -  a = va; -  last = va + size - PGSIZE; +  a = PGROUNDDOWN(va); +  last = PGROUNDDOWN(va + size - 1);    for(;;){      if((pte = walk(pagetable, a, 1)) == 0)        return -1; @@ -186,8 +189,10 @@ uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free)    for(a = va; a < va + npages*PGSIZE; a += PGSIZE){      if((pte = walk(pagetable, a, 0)) == 0)        panic("uvmunmap: walk"); -    if((*pte & PTE_V) == 0) +    if((*pte & PTE_V) == 0) { +      printf("va=%p pte=%p\n", a, *pte);        panic("uvmunmap: not mapped"); +    }      if(PTE_FLAGS(*pte) == PTE_V)        panic("uvmunmap: not a leaf");      if(do_free){ @@ -363,13 +368,21 @@ copyout(pagetable_t pagetable, uint64 dstva, char *src, uint64 len)    while(len > 0){      va0 = PGROUNDDOWN(dstva); -    if(va0 >= MAXVA) +    if (va0 >= MAXVA)        return -1; -    pte = walk(pagetable, va0, 0); -    if(pte == 0 || (*pte & PTE_V) == 0 || (*pte & PTE_U) == 0 || -       (*pte & PTE_W) == 0) +    if((pte = walk(pagetable, va0, 0)) == 0) { +      // printf("copyout: pte should exist 0x%x %d\n", dstva, len); +      return -1; +    } + + +    // forbid copyout over read-only user text pages. +    if((*pte & PTE_W) == 0) +      return -1; +     +    pa0 = walkaddr(pagetable, va0); +    if(pa0 == 0)        return -1; -    pa0 = PTE2PA(*pte);      n = PGSIZE - (dstva - va0);      if(n > len)        n = len; @@ -389,7 +402,7 @@ int  copyin(pagetable_t pagetable, char *dst, uint64 srcva, uint64 len)  {    uint64 n, va0, pa0; - +      while(len > 0){      va0 = PGROUNDDOWN(srcva);      pa0 = walkaddr(pagetable, va0); @@ -449,3 +462,6 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)      return -1;    }  } + + + diff --git a/user/user.h b/user/user.h index 4d398d5..16cf173 100644 --- a/user/user.h +++ b/user/user.h @@ -22,6 +22,14 @@ int getpid(void);  char* sbrk(int);  int sleep(int);  int uptime(void); +#ifdef LAB_NET +int connect(uint32, uint16, uint16); +#endif +#ifdef LAB_PGTBL +int pgaccess(void *base, int len, void *mask); +// usyscall region +int ugetpid(void); +#endif  // ulib.c  int stat(const char*, struct stat*); @@ -39,3 +47,4 @@ void free(void*);  int atoi(const char*);  int memcmp(const void *, const void *, uint);  void *memcpy(void *, const void *, uint); +int statistics(void*, int); diff --git a/user/usys.pl b/user/usys.pl index 01e426e..6453fe9 100755 --- a/user/usys.pl +++ b/user/usys.pl @@ -36,3 +36,5 @@ entry("getpid");  entry("sbrk");  entry("sleep");  entry("uptime"); +entry("connect"); +entry("pgaccess"); | 
