Lecture: Virtual memory

preparation

administrivia

overview

page table

page faults

$ echo
usertrap(): unexpected scause 0x000000000000000f (store/AMO page fault) pid=3
            sepc=0x0000000000000016 stval=0x00000000deadbeef
xv6 kernel is booting

hart 2 starting
hart 1 starting
scause 0x000000000000000f (store/AMO page fault)
sepc=0x0000000080004cba stval=0x00000000deadbeef
PANIC: kerneltrap
[<0x00000000800005ac>] panic+0x46/0x62
[<0x0000000080002af2>] kerneltrap+0xb4/0xd8
[<0x0000000080005ca4>] kernelvec+0x44/0x90
[<0x0000000080005b20>] sys_exec+0xe0/0x110
[<0x0000000080002cca>] syscall+0x3e/0x6c
[<0x0000000080002996>] usertrap+0x60/0x108

address spaces in xv6

page table 0x0000000087fff000
 ..0: pte 0x0000000021fff801 pa 0x0000000087ffe000
 .. ..128: pte 0x0000000021fff401 pa 0x0000000087ffd000
 .. .. ..0: pte 0x0000000004000007 pa 0x0000000010000000

applications

Q&A

diff --git a/Makefile b/Makefile
index 44ad5f2..679657c 100644
--- a/Makefile
+++ b/Makefile
@@ -123,13 +123,16 @@ $U/initcode: $U/initcode.S
 tags: $(OBJS) _init
 	etags *.S *.c
 
-ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
+ULIB = $U/crt0.o $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
 
 _%: %.o $(ULIB)
-	$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^
+	$(LD) $(LDFLAGS) -N -e _start -Ttext 0 -o $@ $^
 	$(OBJDUMP) -S $@ > $*.asm
 	$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym
 
+$U/crt0.o : $U/crt0.S
+	$(CC) $(CFLAGS) -I kernel -c -o $U/crt0.o $U/crt0.S
+
 $U/usys.S : $U/usys.pl
 	perl $U/usys.pl > $U/usys.S
 
diff --git a/user/crt0.S b/user/crt0.S
new file mode 100644
index 0000000..a132c9b
--- /dev/null
+++ b/user/crt0.S
@@ -0,0 +1,10 @@
+#include "syscall.h"
+
+.globl _start
+_start:
+ call main
+
+exit:
+ li a7, SYS_exit
+ ecall
+ jal exit