summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/defs.h49
-rw-r--r--kernel/file.c21
-rw-r--r--kernel/file.h8
-rw-r--r--kernel/main.c12
-rw-r--r--kernel/memlayout.h17
-rw-r--r--kernel/plic.c12
-rw-r--r--kernel/spinlock.c111
-rw-r--r--kernel/syscall.c15
-rw-r--r--kernel/syscall.h11
-rw-r--r--kernel/sysfile.c26
-rw-r--r--kernel/trap.c11
-rw-r--r--kernel/vm.c52
12 files changed, 320 insertions, 25 deletions
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;
}
}
+
+
+