Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d66082b414 | ||
|
|
294e926e52 | ||
|
|
9cc6b83453 | ||
|
|
4b46c0c6eb | ||
|
|
463ae0abc3 | ||
|
|
f5b93ef12f |
9
.gitignore
vendored
9
.gitignore
vendored
@ -15,3 +15,12 @@ mkfs
|
|||||||
kernel/kernel
|
kernel/kernel
|
||||||
user/usys.S
|
user/usys.S
|
||||||
.gdbinit
|
.gdbinit
|
||||||
|
myapi.key
|
||||||
|
*-handin.tar.gz
|
||||||
|
xv6.out*
|
||||||
|
.vagrant/
|
||||||
|
submissions/
|
||||||
|
ph
|
||||||
|
barrier
|
||||||
|
/lab-*.json
|
||||||
|
.DS_Store
|
||||||
16
.vscode/c_cpp_properties.json
vendored
Normal file
16
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"defines": ["LAB_MMAP"],
|
||||||
|
"compilerPath": "/usr/bin/gcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++17",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
260
Makefile
260
Makefile
@ -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
|
K=kernel
|
||||||
U=user
|
U=user
|
||||||
|
|
||||||
OBJS = \
|
OBJS = \
|
||||||
$K/entry.o \
|
$K/entry.o \
|
||||||
$K/start.o \
|
|
||||||
$K/console.o \
|
|
||||||
$K/printf.o \
|
|
||||||
$K/uart.o \
|
|
||||||
$K/kalloc.o \
|
$K/kalloc.o \
|
||||||
$K/spinlock.o \
|
|
||||||
$K/string.o \
|
$K/string.o \
|
||||||
$K/main.o \
|
$K/main.o \
|
||||||
$K/vm.o \
|
$K/vm.o \
|
||||||
@ -30,6 +32,34 @@ OBJS = \
|
|||||||
$K/plic.o \
|
$K/plic.o \
|
||||||
$K/virtio_disk.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-
|
# riscv64-unknown-elf- or riscv64-linux-gnu-
|
||||||
# perhaps in /opt/riscv/bin
|
# perhaps in /opt/riscv/bin
|
||||||
#TOOLPREFIX =
|
#TOOLPREFIX =
|
||||||
@ -57,12 +87,28 @@ OBJCOPY = $(TOOLPREFIX)objcopy
|
|||||||
OBJDUMP = $(TOOLPREFIX)objdump
|
OBJDUMP = $(TOOLPREFIX)objdump
|
||||||
|
|
||||||
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
|
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 += -MD
|
||||||
CFLAGS += -mcmodel=medany
|
CFLAGS += -mcmodel=medany
|
||||||
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
|
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
|
||||||
CFLAGS += -I.
|
CFLAGS += -I.
|
||||||
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
|
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)
|
# Disable PIE when possible (for Ubuntu 16.10 toolchain)
|
||||||
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
|
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
|
||||||
CFLAGS += -fno-pie -no-pie
|
CFLAGS += -fno-pie -no-pie
|
||||||
@ -73,11 +119,17 @@ endif
|
|||||||
|
|
||||||
LDFLAGS = -z max-page-size=4096
|
LDFLAGS = -z max-page-size=4096
|
||||||
|
|
||||||
$K/kernel: $(OBJS) $K/kernel.ld $U/initcode
|
$K/kernel: $(OBJS) $(OBJS_KCSAN) $K/kernel.ld $U/initcode
|
||||||
$(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS)
|
$(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) $(OBJS_KCSAN)
|
||||||
$(OBJDUMP) -S $K/kernel > $K/kernel.asm
|
$(OBJDUMP) -S $K/kernel > $K/kernel.asm
|
||||||
$(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym
|
$(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
|
$U/initcode: $U/initcode.S
|
||||||
$(CC) $(CFLAGS) -march=rv64g -nostdinc -I. -Ikernel -c $U/initcode.S -o $U/initcode.o
|
$(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
|
$(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
|
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)
|
_%: %.o $(ULIB)
|
||||||
$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^
|
$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^
|
||||||
$(OBJDUMP) -S $@ > $*.asm
|
$(OBJDUMP) -S $@ > $*.asm
|
||||||
@ -107,7 +163,7 @@ $U/_forktest: $U/forktest.o $(ULIB)
|
|||||||
$(OBJDUMP) -S $U/_forktest > $U/forktest.asm
|
$(OBJDUMP) -S $U/_forktest > $U/forktest.asm
|
||||||
|
|
||||||
mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h
|
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
|
# 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
|
# that disk image changes after first build are persistent until clean. More
|
||||||
@ -132,9 +188,81 @@ UPROGS=\
|
|||||||
$U/_grind\
|
$U/_grind\
|
||||||
$U/_wc\
|
$U/_wc\
|
||||||
$U/_zombie\
|
$U/_zombie\
|
||||||
|
$U/_mmaptest\
|
||||||
|
|
||||||
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
|
-include kernel/*.d user/*.d
|
||||||
|
|
||||||
@ -144,7 +272,8 @@ clean:
|
|||||||
$U/initcode $U/initcode.out $K/kernel fs.img \
|
$U/initcode $U/initcode.out $K/kernel fs.img \
|
||||||
mkfs/mkfs .gdbinit \
|
mkfs/mkfs .gdbinit \
|
||||||
$U/usys.S \
|
$U/usys.S \
|
||||||
$(UPROGS)
|
$(UPROGS) \
|
||||||
|
ph barrier
|
||||||
|
|
||||||
# try to generate a unique GDB port
|
# try to generate a unique GDB port
|
||||||
GDBPORT = $(shell expr `id -u` % 5000 + 25000)
|
GDBPORT = $(shell expr `id -u` % 5000 + 25000)
|
||||||
@ -155,12 +284,22 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
|
|||||||
ifndef CPUS
|
ifndef CPUS
|
||||||
CPUS := 3
|
CPUS := 3
|
||||||
endif
|
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 = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic
|
||||||
QEMUOPTS += -global virtio-mmio.force-legacy=false
|
QEMUOPTS += -global virtio-mmio.force-legacy=false
|
||||||
QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0
|
QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0
|
||||||
QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
|
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: $K/kernel fs.img
|
||||||
$(QEMU) $(QEMUOPTS)
|
$(QEMU) $(QEMUOPTS)
|
||||||
|
|
||||||
@ -171,3 +310,102 @@ qemu-gdb: $K/kernel .gdbinit fs.img
|
|||||||
@echo "*** Now run 'gdb' in another window." 1>&2
|
@echo "*** Now run 'gdb' in another window." 1>&2
|
||||||
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)
|
$(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 web handin
|
||||||
|
##
|
||||||
|
|
||||||
|
|
||||||
|
WEBSUB := https://6828.scripts.mit.edu/2022/handin.py
|
||||||
|
|
||||||
|
handin: tarball-pref myapi.key
|
||||||
|
@SUF=$(LAB); \
|
||||||
|
curl -f -F file=@lab-$$SUF-handin.tar.gz -F key=\<myapi.key $(WEBSUB)/upload \
|
||||||
|
> /dev/null || { \
|
||||||
|
echo ; \
|
||||||
|
echo Submit seems to have failed.; \
|
||||||
|
echo Please go to $(WEBSUB)/ and upload the tarball manually.; }
|
||||||
|
|
||||||
|
handin-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
|
||||||
|
|
||||||
|
UPSTREAM := $(shell git remote -v | grep -m 1 "xv6-labs-2022" | awk '{split($$0,a," "); print a[1]}')
|
||||||
|
|
||||||
|
tarball: handin-check
|
||||||
|
git archive --format=tar HEAD | gzip > lab-$(LAB)-handin.tar.gz
|
||||||
|
|
||||||
|
tarball-pref: handin-check
|
||||||
|
@SUF=$(LAB); \
|
||||||
|
git archive --format=tar HEAD > lab-$$SUF-handin.tar; \
|
||||||
|
git diff $(UPSTREAM)/$(LAB) > /tmp/lab-$$SUF-diff.patch; \
|
||||||
|
tar -rf lab-$$SUF-handin.tar /tmp/lab-$$SUF-diff.patch; \
|
||||||
|
gzip -c lab-$$SUF-handin.tar > lab-$$SUF-handin.tar.gz; \
|
||||||
|
rm lab-$$SUF-handin.tar; \
|
||||||
|
rm /tmp/lab-$$SUF-diff.patch; \
|
||||||
|
|
||||||
|
myapi.key:
|
||||||
|
@echo Get an API key for yourself by visiting $(WEBSUB)/
|
||||||
|
@read -p "Please enter your API key: " k; \
|
||||||
|
if test `echo "$$k" |tr -d '\n' |wc -c` = 32 ; then \
|
||||||
|
TF=`mktemp -t tmp.XXXXXX`; \
|
||||||
|
if test "x$$TF" != "x" ; then \
|
||||||
|
echo "$$k" |tr -d '\n' > $$TF; \
|
||||||
|
mv -f $$TF $@; \
|
||||||
|
else \
|
||||||
|
echo mktemp failed; \
|
||||||
|
false; \
|
||||||
|
fi; \
|
||||||
|
else \
|
||||||
|
echo Bad API key: $$k; \
|
||||||
|
echo An API key should be 32 characters long.; \
|
||||||
|
false; \
|
||||||
|
fi;
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: handin tarball tarball-pref clean grade handin-check
|
||||||
|
|||||||
36
README
36
README
@ -6,7 +6,7 @@ ACKNOWLEDGMENTS
|
|||||||
|
|
||||||
xv6 is inspired by John Lions's Commentary on UNIX 6th Edition (Peer
|
xv6 is inspired by John Lions's Commentary on UNIX 6th Edition (Peer
|
||||||
to Peer Communications; ISBN: 1-57398-013-7; 1st edition (June 14,
|
to Peer Communications; ISBN: 1-57398-013-7; 1st edition (June 14,
|
||||||
2000)). See also https://pdos.csail.mit.edu/6.828/, which provides
|
2000)). See also https://pdos.csail.mit.edu/6.1810/, which provides
|
||||||
pointers to on-line resources for v6.
|
pointers to on-line resources for v6.
|
||||||
|
|
||||||
The following people have made contributions: Russ Cox (context switching,
|
The following people have made contributions: Russ Cox (context switching,
|
||||||
@ -14,29 +14,31 @@ locking), Cliff Frey (MP), Xiao Yu (MP), Nickolai Zeldovich, and Austin
|
|||||||
Clements.
|
Clements.
|
||||||
|
|
||||||
We are also grateful for the bug reports and patches contributed by
|
We are also grateful for the bug reports and patches contributed by
|
||||||
Takahiro Aoyagi, Silas Boyd-Wickizer, Anton Burtsev, Ian Chen, Dan
|
Takahiro Aoyagi, Silas Boyd-Wickizer, Anton Burtsev, carlclone, Ian
|
||||||
Cross, Cody Cutler, Mike CAT, Tej Chajed, Asami Doi, eyalz800, Nelson
|
Chen, Dan Cross, Cody Cutler, Mike CAT, Tej Chajed, Asami Doi,
|
||||||
Elhage, Saar Ettinger, Alice Ferrazzi, Nathaniel Filardo, flespark,
|
eyalz800, Nelson Elhage, Saar Ettinger, Alice Ferrazzi, Nathaniel
|
||||||
Peter Froehlich, Yakir Goaron, Shivam Handa, Matt Harvey, Bryan Henry,
|
Filardo, flespark, Peter Froehlich, Yakir Goaron, Shivam Handa, Matt
|
||||||
jaichenhengjie, Jim Huang, Matúš Jókay, Alexander Kapshuk, Anders
|
Harvey, Bryan Henry, jaichenhengjie, Jim Huang, Matúš Jókay, John
|
||||||
Kaseorg, kehao95, Wolfgang Keller, Jungwoo Kim, Jonathan Kimmitt,
|
Jolly, Alexander Kapshuk, Anders Kaseorg, kehao95, Wolfgang Keller,
|
||||||
Eddie Kohler, Vadim Kolontsov, Austin Liew, l0stman, Pavan
|
Jungwoo Kim, Jonathan Kimmitt, Eddie Kohler, Vadim Kolontsov, Austin
|
||||||
Maddamsetti, Imbar Marinescu, Yandong Mao, Matan Shabtay, Hitoshi
|
Liew, l0stman, Pavan Maddamsetti, Imbar Marinescu, Yandong Mao, Matan
|
||||||
Mitake, Carmi Merimovich, Mark Morrissey, mtasm, Joel Nider,
|
Shabtay, Hitoshi Mitake, Carmi Merimovich, Mark Morrissey, mtasm, Joel
|
||||||
OptimisticSide, Greg Price, Jude Rich, Ayan Shafqat, Eldar Sehayek,
|
Nider, Hayato Ohhashi, OptimisticSide, Harry Porter, Greg Price, Jude
|
||||||
Yongming Shen, Fumiya Shigemitsu, Cam Tenny, tyfkda, Warren Toomey,
|
Rich, segfault, Ayan Shafqat, Eldar Sehayek, Yongming Shen, Fumiya
|
||||||
Stephen Tu, Rafael Ubal, Amane Uehara, Pablo Ventura, Xi Wang, Keiichi
|
Shigemitsu, Cam Tenny, tyfkda, Warren Toomey, Stephen Tu, Rafael Ubal,
|
||||||
Watanabe, Nicolas Wolovick, wxdao, Grant Wu, Jindong Zhang, Icenowy
|
Amane Uehara, Pablo Ventura, Xi Wang, WaheedHafez, Keiichi Watanabe,
|
||||||
Zheng, ZhUyU1997, and Zou Chang Wei.
|
Nicolas Wolovick, wxdao, Grant Wu, Jindong Zhang, Icenowy Zheng,
|
||||||
|
ZhUyU1997, and Zou Chang Wei.
|
||||||
|
|
||||||
|
|
||||||
The code in the files that constitute xv6 is
|
The code in the files that constitute xv6 is
|
||||||
Copyright 2006-2020 Frans Kaashoek, Robert Morris, and Russ Cox.
|
Copyright 2006-2022 Frans Kaashoek, Robert Morris, and Russ Cox.
|
||||||
|
|
||||||
ERROR REPORTS
|
ERROR REPORTS
|
||||||
|
|
||||||
Please send errors and suggestions to Frans Kaashoek and Robert Morris
|
Please send errors and suggestions to Frans Kaashoek and Robert Morris
|
||||||
(kaashoek,rtm@mit.edu). The main purpose of xv6 is as a teaching
|
(kaashoek,rtm@mit.edu). The main purpose of xv6 is as a teaching
|
||||||
operating system for MIT's 6.S081, so we are more interested in
|
operating system for MIT's 6.1810, so we are more interested in
|
||||||
simplifications and clarifications than new features.
|
simplifications and clarifications than new features.
|
||||||
|
|
||||||
BUILDING AND RUNNING XV6
|
BUILDING AND RUNNING XV6
|
||||||
|
|||||||
1
conf/lab.mk
Normal file
1
conf/lab.mk
Normal file
@ -0,0 +1 @@
|
|||||||
|
LAB=mmap
|
||||||
57
grade-lab-mmap
Executable file
57
grade-lab-mmap
Executable file
@ -0,0 +1,57 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import re
|
||||||
|
from gradelib import *
|
||||||
|
|
||||||
|
r = Runner(save("xv6.out"))
|
||||||
|
|
||||||
|
@test(0, "running mmaptest")
|
||||||
|
def test_mmaptest():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'mmaptest'
|
||||||
|
]), timeout=180)
|
||||||
|
|
||||||
|
@test(20, "mmaptest: mmap f", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_mmap_f():
|
||||||
|
r.match('^test mmap f: OK$')
|
||||||
|
|
||||||
|
@test(10, "mmaptest: mmap private", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_mmap_private():
|
||||||
|
r.match('^test mmap private: OK$')
|
||||||
|
|
||||||
|
@test(10, "mmaptest: mmap read-only", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_mmap_readonly():
|
||||||
|
r.match('^test mmap read-only: OK$')
|
||||||
|
|
||||||
|
@test(10, "mmaptest: mmap read/write", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_mmap_readwrite():
|
||||||
|
r.match('^test mmap read/write: OK$')
|
||||||
|
|
||||||
|
@test(10, "mmaptest: mmap dirty", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_mmap_dirty():
|
||||||
|
r.match('^test mmap dirty: OK$')
|
||||||
|
|
||||||
|
@test(10, "mmaptest: not-mapped unmap", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_mmap_unmap():
|
||||||
|
r.match('^test not-mapped unmap: OK$')
|
||||||
|
|
||||||
|
@test(10, "mmaptest: two files", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_mmap_two():
|
||||||
|
r.match('^test mmap two files: OK$')
|
||||||
|
|
||||||
|
@test(40, "mmaptest: fork_test", parent=test_mmaptest)
|
||||||
|
def test_mmaptest_fork_test():
|
||||||
|
r.match('^fork_test OK$')
|
||||||
|
|
||||||
|
@test(19, "usertests")
|
||||||
|
def test_usertests():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'usertests -q'
|
||||||
|
]), timeout=300)
|
||||||
|
r.match('^ALL TESTS PASSED$')
|
||||||
|
|
||||||
|
@test(1, "time")
|
||||||
|
def test_time():
|
||||||
|
check_time()
|
||||||
|
|
||||||
|
run_tests()
|
||||||
611
gradelib.py
Normal file
611
gradelib.py
Normal file
@ -0,0 +1,611 @@
|
|||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import sys, os, re, time, socket, select, subprocess, errno, shutil, random, string
|
||||||
|
from subprocess import check_call, Popen
|
||||||
|
from optparse import OptionParser
|
||||||
|
|
||||||
|
__all__ = []
|
||||||
|
|
||||||
|
##################################################################
|
||||||
|
# Test structure
|
||||||
|
#
|
||||||
|
|
||||||
|
__all__ += ["test", "end_part", "run_tests", "get_current_test"]
|
||||||
|
|
||||||
|
TESTS = []
|
||||||
|
TOTAL = POSSIBLE = 0
|
||||||
|
PART_TOTAL = PART_POSSIBLE = 0
|
||||||
|
CURRENT_TEST = None
|
||||||
|
|
||||||
|
def test(points, title=None, parent=None):
|
||||||
|
"""Decorator for declaring test functions. If title is None, the
|
||||||
|
title of the test will be derived from the function name by
|
||||||
|
stripping the leading "test_" and replacing underscores with
|
||||||
|
spaces."""
|
||||||
|
|
||||||
|
def register_test(fn, title=title):
|
||||||
|
if not title:
|
||||||
|
assert fn.__name__.startswith("test_")
|
||||||
|
title = fn.__name__[5:].replace("_", " ")
|
||||||
|
if parent:
|
||||||
|
title = " " + title
|
||||||
|
|
||||||
|
def run_test():
|
||||||
|
global TOTAL, POSSIBLE, CURRENT_TEST
|
||||||
|
|
||||||
|
# Handle test dependencies
|
||||||
|
if run_test.complete:
|
||||||
|
return run_test.ok
|
||||||
|
run_test.complete = True
|
||||||
|
parent_failed = False
|
||||||
|
if parent:
|
||||||
|
parent_failed = not parent()
|
||||||
|
|
||||||
|
# Run the test
|
||||||
|
fail = None
|
||||||
|
start = time.time()
|
||||||
|
CURRENT_TEST = run_test
|
||||||
|
sys.stdout.write("== Test %s == " % title)
|
||||||
|
if parent:
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
sys.stdout.flush()
|
||||||
|
try:
|
||||||
|
if parent_failed:
|
||||||
|
raise AssertionError('Parent failed: %s' % parent.__name__)
|
||||||
|
fn()
|
||||||
|
except AssertionError as e:
|
||||||
|
fail = str(e)
|
||||||
|
|
||||||
|
# Display and handle test result
|
||||||
|
POSSIBLE += points
|
||||||
|
if points:
|
||||||
|
print("%s: %s" % (title, \
|
||||||
|
(color("red", "FAIL") if fail else color("green", "OK"))), end=' ')
|
||||||
|
if time.time() - start > 0.1:
|
||||||
|
print("(%.1fs)" % (time.time() - start), end=' ')
|
||||||
|
print()
|
||||||
|
if fail:
|
||||||
|
print(" %s" % fail.replace("\n", "\n "))
|
||||||
|
else:
|
||||||
|
TOTAL += points
|
||||||
|
for callback in run_test.on_finish:
|
||||||
|
callback(fail)
|
||||||
|
CURRENT_TEST = None
|
||||||
|
|
||||||
|
run_test.ok = not fail
|
||||||
|
return run_test.ok
|
||||||
|
|
||||||
|
# Record test metadata on the test wrapper function
|
||||||
|
run_test.__name__ = fn.__name__
|
||||||
|
run_test.title = title
|
||||||
|
run_test.complete = False
|
||||||
|
run_test.ok = False
|
||||||
|
run_test.on_finish = []
|
||||||
|
TESTS.append(run_test)
|
||||||
|
return run_test
|
||||||
|
return register_test
|
||||||
|
|
||||||
|
def end_part(name):
|
||||||
|
def show_part():
|
||||||
|
global PART_TOTAL, PART_POSSIBLE
|
||||||
|
print("Part %s score: %d/%d" % \
|
||||||
|
(name, TOTAL - PART_TOTAL, POSSIBLE - PART_POSSIBLE))
|
||||||
|
print()
|
||||||
|
PART_TOTAL, PART_POSSIBLE = TOTAL, POSSIBLE
|
||||||
|
show_part.title = ""
|
||||||
|
TESTS.append(show_part)
|
||||||
|
|
||||||
|
def run_tests():
|
||||||
|
"""Set up for testing and run the registered test functions."""
|
||||||
|
|
||||||
|
# Handle command line
|
||||||
|
global options
|
||||||
|
parser = OptionParser(usage="usage: %prog [-v] [filters...]")
|
||||||
|
parser.add_option("-v", "--verbose", action="store_true",
|
||||||
|
help="print commands")
|
||||||
|
parser.add_option("--color", choices=["never", "always", "auto"],
|
||||||
|
default="auto", help="never, always, or auto")
|
||||||
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
# Start with a full build to catch build errors
|
||||||
|
make()
|
||||||
|
|
||||||
|
# Clean the file system if there is one
|
||||||
|
reset_fs()
|
||||||
|
|
||||||
|
# Run tests
|
||||||
|
limit = list(map(str.lower, args))
|
||||||
|
try:
|
||||||
|
for test in TESTS:
|
||||||
|
if not limit or any(l in test.title.lower() for l in limit):
|
||||||
|
test()
|
||||||
|
if not limit:
|
||||||
|
print("Score: %d/%d" % (TOTAL, POSSIBLE))
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
if TOTAL < POSSIBLE:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
def get_current_test():
|
||||||
|
if not CURRENT_TEST:
|
||||||
|
raise RuntimeError("No test is running")
|
||||||
|
return CURRENT_TEST
|
||||||
|
|
||||||
|
##################################################################
|
||||||
|
# Assertions
|
||||||
|
#
|
||||||
|
|
||||||
|
__all__ += ["assert_equal", "assert_lines_match"]
|
||||||
|
|
||||||
|
def assert_equal(got, expect, msg=""):
|
||||||
|
if got == expect:
|
||||||
|
return
|
||||||
|
if msg:
|
||||||
|
msg += "\n"
|
||||||
|
raise AssertionError("%sgot:\n %s\nexpected:\n %s" %
|
||||||
|
(msg, str(got).replace("\n", "\n "),
|
||||||
|
str(expect).replace("\n", "\n ")))
|
||||||
|
|
||||||
|
def assert_lines_match(text, *regexps, **kw):
|
||||||
|
"""Assert that all of regexps match some line in text. If a 'no'
|
||||||
|
keyword argument is given, it must be a list of regexps that must
|
||||||
|
*not* match any line in text."""
|
||||||
|
|
||||||
|
def assert_lines_match_kw(no=[]):
|
||||||
|
return no
|
||||||
|
no = assert_lines_match_kw(**kw)
|
||||||
|
|
||||||
|
# Check text against regexps
|
||||||
|
lines = text.splitlines()
|
||||||
|
good = set()
|
||||||
|
bad = set()
|
||||||
|
for i, line in enumerate(lines):
|
||||||
|
if any(re.match(r, line) for r in regexps):
|
||||||
|
good.add(i)
|
||||||
|
regexps = [r for r in regexps if not re.match(r, line)]
|
||||||
|
if any(re.match(r, line) for r in no):
|
||||||
|
bad.add(i)
|
||||||
|
|
||||||
|
if not regexps and not bad:
|
||||||
|
return
|
||||||
|
|
||||||
|
# We failed; construct an informative failure message
|
||||||
|
show = set()
|
||||||
|
for lineno in good.union(bad):
|
||||||
|
for offset in range(-2, 3):
|
||||||
|
show.add(lineno + offset)
|
||||||
|
if regexps:
|
||||||
|
show.update(n for n in range(len(lines) - 5, len(lines)))
|
||||||
|
|
||||||
|
msg = []
|
||||||
|
last = -1
|
||||||
|
for lineno in sorted(show):
|
||||||
|
if 0 <= lineno < len(lines):
|
||||||
|
if lineno != last + 1:
|
||||||
|
msg.append("...")
|
||||||
|
last = lineno
|
||||||
|
msg.append("%s %s" % (color("red", "BAD ") if lineno in bad else
|
||||||
|
color("green", "GOOD") if lineno in good
|
||||||
|
else " ",
|
||||||
|
lines[lineno]))
|
||||||
|
if last != len(lines) - 1:
|
||||||
|
msg.append("...")
|
||||||
|
if bad:
|
||||||
|
msg.append("unexpected lines in output")
|
||||||
|
for r in regexps:
|
||||||
|
msg.append(color("red", "MISSING") + " '%s'" % r)
|
||||||
|
raise AssertionError("\n".join(msg))
|
||||||
|
|
||||||
|
##################################################################
|
||||||
|
# Utilities
|
||||||
|
#
|
||||||
|
|
||||||
|
__all__ += ["make", "maybe_unlink", "reset_fs", "color", "random_str", "check_time", "check_answers"]
|
||||||
|
|
||||||
|
MAKE_TIMESTAMP = 0
|
||||||
|
|
||||||
|
def pre_make():
|
||||||
|
"""Delay prior to running make to ensure file mtimes change."""
|
||||||
|
while int(time.time()) == MAKE_TIMESTAMP:
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
def post_make():
|
||||||
|
"""Record the time after make completes so that the next run of
|
||||||
|
make can be delayed if needed."""
|
||||||
|
global MAKE_TIMESTAMP
|
||||||
|
MAKE_TIMESTAMP = int(time.time())
|
||||||
|
|
||||||
|
def make(*target):
|
||||||
|
pre_make()
|
||||||
|
if Popen(("make",) + target).wait():
|
||||||
|
sys.exit(1)
|
||||||
|
post_make()
|
||||||
|
|
||||||
|
def show_command(cmd):
|
||||||
|
from pipes import quote
|
||||||
|
print("\n$", " ".join(map(quote, cmd)))
|
||||||
|
|
||||||
|
def maybe_unlink(*paths):
|
||||||
|
for path in paths:
|
||||||
|
try:
|
||||||
|
os.unlink(path)
|
||||||
|
except EnvironmentError as e:
|
||||||
|
if e.errno != errno.ENOENT:
|
||||||
|
raise
|
||||||
|
|
||||||
|
COLORS = {"default": "\033[0m", "red": "\033[31m", "green": "\033[32m"}
|
||||||
|
|
||||||
|
def color(name, text):
|
||||||
|
if options.color == "always" or (options.color == "auto" and os.isatty(1)):
|
||||||
|
return COLORS[name] + text + COLORS["default"]
|
||||||
|
return text
|
||||||
|
|
||||||
|
def reset_fs():
|
||||||
|
if os.path.exists("obj/fs/clean-fs.img"):
|
||||||
|
shutil.copyfile("obj/fs/clean-fs.img", "obj/fs/fs.img")
|
||||||
|
|
||||||
|
def random_str(n=8):
|
||||||
|
letters = string.ascii_letters + string.digits
|
||||||
|
return ''.join(random.choice(letters) for _ in range(n))
|
||||||
|
|
||||||
|
def check_time():
|
||||||
|
try:
|
||||||
|
print("")
|
||||||
|
with open('time.txt') as f:
|
||||||
|
d = f.read().strip()
|
||||||
|
if not re.match(r'^\d+$', d):
|
||||||
|
raise AssertionError('time.txt does not contain a single integer (number of hours spent on the lab)')
|
||||||
|
except IOError:
|
||||||
|
raise AssertionError('Cannot read time.txt')
|
||||||
|
|
||||||
|
def check_answers(file, n=10):
|
||||||
|
try:
|
||||||
|
with open(file) as f:
|
||||||
|
d = f.read().strip()
|
||||||
|
if len(d) < n:
|
||||||
|
raise AssertionError('%s does not seem to contain enough text' % file)
|
||||||
|
except IOError:
|
||||||
|
raise AssertionError('Cannot read %s' % file)
|
||||||
|
|
||||||
|
|
||||||
|
##################################################################
|
||||||
|
# Controllers
|
||||||
|
#
|
||||||
|
|
||||||
|
__all__ += ["QEMU", "GDBClient"]
|
||||||
|
|
||||||
|
class QEMU(object):
|
||||||
|
_GDBPORT = None
|
||||||
|
|
||||||
|
def __init__(self, *make_args):
|
||||||
|
# Check that QEMU is not currently running
|
||||||
|
try:
|
||||||
|
GDBClient(self.get_gdb_port(), timeout=0).close()
|
||||||
|
except socket.error:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
print("""\
|
||||||
|
GDB stub found on port %d.
|
||||||
|
QEMU appears to already be running. Please exit it if possible or use
|
||||||
|
'killall qemu' or 'killall qemu.real'.""" % self.get_gdb_port(), file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if options.verbose:
|
||||||
|
show_command(("make",) + make_args)
|
||||||
|
cmd = ("make", "-s", "--no-print-directory") + make_args
|
||||||
|
self.proc = Popen(cmd, stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
stdin=subprocess.PIPE)
|
||||||
|
# Accumulated output as a string
|
||||||
|
self.output = ""
|
||||||
|
# Accumulated output as a bytearray
|
||||||
|
self.outbytes = bytearray()
|
||||||
|
self.on_output = []
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_gdb_port():
|
||||||
|
if QEMU._GDBPORT is None:
|
||||||
|
p = Popen(["make", "-s", "--no-print-directory", "print-gdbport"],
|
||||||
|
stdout=subprocess.PIPE)
|
||||||
|
(out, _) = p.communicate()
|
||||||
|
if p.returncode:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Failed to get gdbport: make exited with %d" %
|
||||||
|
p.returncode)
|
||||||
|
QEMU._GDBPORT = int(out)
|
||||||
|
return QEMU._GDBPORT
|
||||||
|
|
||||||
|
def fileno(self):
|
||||||
|
if self.proc:
|
||||||
|
return self.proc.stdout.fileno()
|
||||||
|
|
||||||
|
def handle_read(self):
|
||||||
|
buf = os.read(self.proc.stdout.fileno(), 4096)
|
||||||
|
self.outbytes.extend(buf)
|
||||||
|
self.output = self.outbytes.decode("utf-8", "replace")
|
||||||
|
for callback in self.on_output:
|
||||||
|
callback(buf)
|
||||||
|
if buf == b"":
|
||||||
|
self.wait()
|
||||||
|
return
|
||||||
|
|
||||||
|
def write(self, buf):
|
||||||
|
if isinstance(buf, str):
|
||||||
|
buf = buf.encode('utf-8')
|
||||||
|
self.proc.stdin.write(buf)
|
||||||
|
self.proc.stdin.flush()
|
||||||
|
|
||||||
|
def wait(self):
|
||||||
|
if self.proc:
|
||||||
|
self.proc.wait()
|
||||||
|
self.proc = None
|
||||||
|
|
||||||
|
def kill(self):
|
||||||
|
if self.proc:
|
||||||
|
self.proc.terminate()
|
||||||
|
|
||||||
|
class GDBClient(object):
|
||||||
|
def __init__(self, port, timeout=15):
|
||||||
|
start = time.time()
|
||||||
|
while True:
|
||||||
|
self.sock = socket.socket()
|
||||||
|
try:
|
||||||
|
self.sock.settimeout(1)
|
||||||
|
self.sock.connect(("localhost", port))
|
||||||
|
break
|
||||||
|
except socket.error:
|
||||||
|
if time.time() >= start + timeout:
|
||||||
|
raise
|
||||||
|
self.__buf = ""
|
||||||
|
|
||||||
|
def fileno(self):
|
||||||
|
if self.sock:
|
||||||
|
return self.sock.fileno()
|
||||||
|
|
||||||
|
def handle_read(self):
|
||||||
|
try:
|
||||||
|
data = self.sock.recv(4096).decode("ascii", "replace")
|
||||||
|
except socket.error:
|
||||||
|
data = ""
|
||||||
|
if data == "":
|
||||||
|
self.sock.close()
|
||||||
|
self.sock = None
|
||||||
|
return
|
||||||
|
self.__buf += data
|
||||||
|
|
||||||
|
while True:
|
||||||
|
m = re.search(r"\$([^#]*)#[0-9a-zA-Z]{2}", self.__buf)
|
||||||
|
if not m:
|
||||||
|
break
|
||||||
|
pkt = m.group(1)
|
||||||
|
self.__buf = self.__buf[m.end():]
|
||||||
|
|
||||||
|
if pkt.startswith("T05"):
|
||||||
|
# Breakpoint
|
||||||
|
raise TerminateTest
|
||||||
|
|
||||||
|
def __send(self, cmd):
|
||||||
|
packet = "$%s#%02x" % (cmd, sum(map(ord, cmd)) % 256)
|
||||||
|
self.sock.sendall(packet.encode("ascii"))
|
||||||
|
|
||||||
|
def __send_break(self):
|
||||||
|
self.sock.sendall(b"\x03")
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.sock:
|
||||||
|
self.sock.close()
|
||||||
|
self.sock = None
|
||||||
|
|
||||||
|
def cont(self):
|
||||||
|
self.__send("c")
|
||||||
|
|
||||||
|
def breakpoint(self, addr):
|
||||||
|
self.__send("Z1,%x,1" % addr)
|
||||||
|
|
||||||
|
|
||||||
|
##################################################################
|
||||||
|
# QEMU test runner
|
||||||
|
#
|
||||||
|
|
||||||
|
__all__ += ["TerminateTest", "Runner"]
|
||||||
|
|
||||||
|
class TerminateTest(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Runner():
|
||||||
|
def __init__(self, *default_monitors):
|
||||||
|
self.__default_monitors = default_monitors
|
||||||
|
|
||||||
|
def run_qemu(self, *monitors, **kw):
|
||||||
|
"""Run a QEMU-based test. monitors should functions that will
|
||||||
|
be called with this Runner instance once QEMU and GDB are
|
||||||
|
started. Typically, they should register callbacks that throw
|
||||||
|
TerminateTest when stop events occur. The target_base
|
||||||
|
argument gives the make target to run. The make_args argument
|
||||||
|
should be a list of additional arguments to pass to make. The
|
||||||
|
timeout argument bounds how long to run before returning."""
|
||||||
|
|
||||||
|
def run_qemu_kw(target_base="qemu", make_args=[], timeout=30):
|
||||||
|
return target_base, make_args, timeout
|
||||||
|
target_base, make_args, timeout = run_qemu_kw(**kw)
|
||||||
|
|
||||||
|
# Start QEMU
|
||||||
|
pre_make()
|
||||||
|
self.qemu = QEMU(target_base + "-gdb", *make_args)
|
||||||
|
self.gdb = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Wait for QEMU to start or make to fail. This will set
|
||||||
|
# self.gdb if QEMU starts.
|
||||||
|
self.qemu.on_output = [self.__monitor_start]
|
||||||
|
self.__react([self.qemu], timeout=90)
|
||||||
|
self.qemu.on_output = []
|
||||||
|
if self.gdb is None:
|
||||||
|
print("Failed to connect to QEMU; output:")
|
||||||
|
print(self.qemu.output)
|
||||||
|
sys.exit(1)
|
||||||
|
post_make()
|
||||||
|
|
||||||
|
# QEMU and GDB are up
|
||||||
|
self.reactors = [self.qemu, self.gdb]
|
||||||
|
|
||||||
|
# Start monitoring
|
||||||
|
for m in self.__default_monitors + monitors:
|
||||||
|
m(self)
|
||||||
|
|
||||||
|
# Run and react
|
||||||
|
self.gdb.cont()
|
||||||
|
self.__react(self.reactors, timeout)
|
||||||
|
finally:
|
||||||
|
# Shutdown QEMU
|
||||||
|
try:
|
||||||
|
if self.gdb is None:
|
||||||
|
sys.exit(1)
|
||||||
|
self.qemu.kill()
|
||||||
|
self.__react(self.reactors, 5)
|
||||||
|
self.gdb.close()
|
||||||
|
self.qemu.wait()
|
||||||
|
except:
|
||||||
|
print("""\
|
||||||
|
Failed to shutdown QEMU. You might need to 'killall qemu' or
|
||||||
|
'killall qemu.real'.
|
||||||
|
""")
|
||||||
|
raise
|
||||||
|
|
||||||
|
def __monitor_start(self, output):
|
||||||
|
if b"\n" in output:
|
||||||
|
try:
|
||||||
|
self.gdb = GDBClient(self.qemu.get_gdb_port(), timeout=2)
|
||||||
|
raise TerminateTest
|
||||||
|
except socket.error:
|
||||||
|
pass
|
||||||
|
if not len(output):
|
||||||
|
raise TerminateTest
|
||||||
|
|
||||||
|
def __react(self, reactors, timeout):
|
||||||
|
deadline = time.time() + timeout
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
timeleft = deadline - time.time()
|
||||||
|
if timeleft < 0:
|
||||||
|
sys.stdout.write("Timeout! ")
|
||||||
|
sys.stdout.flush()
|
||||||
|
return
|
||||||
|
|
||||||
|
rset = [r for r in reactors if r.fileno() is not None]
|
||||||
|
if not rset:
|
||||||
|
return
|
||||||
|
|
||||||
|
rset, _, _ = select.select(rset, [], [], timeleft)
|
||||||
|
for reactor in rset:
|
||||||
|
reactor.handle_read()
|
||||||
|
except TerminateTest:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def user_test(self, binary, *monitors, **kw):
|
||||||
|
"""Run a user test using the specified binary. Monitors and
|
||||||
|
keyword arguments are as for run_qemu. This runs on a disk
|
||||||
|
snapshot unless the keyword argument 'snapshot' is False."""
|
||||||
|
|
||||||
|
maybe_unlink("obj/kern/init.o", "obj/kern/kernel")
|
||||||
|
if kw.pop("snapshot", True):
|
||||||
|
kw.setdefault("make_args", []).append("QEMUEXTRA+=-snapshot")
|
||||||
|
self.run_qemu(target_base="run-%s" % binary, *monitors, **kw)
|
||||||
|
|
||||||
|
def match(self, *args, **kwargs):
|
||||||
|
"""Shortcut to call assert_lines_match on the most recent QEMU
|
||||||
|
output."""
|
||||||
|
|
||||||
|
assert_lines_match(self.qemu.output, *args, **kwargs)
|
||||||
|
|
||||||
|
##################################################################
|
||||||
|
# Monitors
|
||||||
|
#
|
||||||
|
|
||||||
|
__all__ += ["save", "stop_breakpoint", "call_on_line", "stop_on_line", "shell_script"]
|
||||||
|
|
||||||
|
def save(path):
|
||||||
|
"""Return a monitor that writes QEMU's output to path. If the
|
||||||
|
test fails, copy the output to path.test-name."""
|
||||||
|
|
||||||
|
def setup_save(runner):
|
||||||
|
f.seek(0)
|
||||||
|
f.truncate()
|
||||||
|
runner.qemu.on_output.append(f.write)
|
||||||
|
get_current_test().on_finish.append(save_on_finish)
|
||||||
|
|
||||||
|
def save_on_finish(fail):
|
||||||
|
f.flush()
|
||||||
|
save_path = path + "." + get_current_test().__name__[5:]
|
||||||
|
if fail:
|
||||||
|
shutil.copyfile(path, save_path)
|
||||||
|
print(" QEMU output saved to %s" % save_path)
|
||||||
|
elif os.path.exists(save_path):
|
||||||
|
os.unlink(save_path)
|
||||||
|
print(" (Old %s failure log removed)" % save_path)
|
||||||
|
|
||||||
|
f = open(path, "wb")
|
||||||
|
return setup_save
|
||||||
|
|
||||||
|
def stop_breakpoint(addr):
|
||||||
|
"""Returns a monitor that stops when addr is reached. addr may be
|
||||||
|
a number or the name of a symbol."""
|
||||||
|
|
||||||
|
def setup_breakpoint(runner):
|
||||||
|
if isinstance(addr, str):
|
||||||
|
addrs = [int(sym[:16], 16) for sym in open("kernel/kernel.sym")
|
||||||
|
if sym[17:].strip() == addr]
|
||||||
|
assert len(addrs), "Symbol %s not found" % addr
|
||||||
|
runner.gdb.breakpoint(addrs[0])
|
||||||
|
else:
|
||||||
|
runner.gdb.breakpoint(addr)
|
||||||
|
return setup_breakpoint
|
||||||
|
|
||||||
|
def call_on_line(regexp, callback):
|
||||||
|
"""Returns a monitor that calls 'callback' when QEMU prints a line
|
||||||
|
matching 'regexp'."""
|
||||||
|
|
||||||
|
def setup_call_on_line(runner):
|
||||||
|
buf = bytearray()
|
||||||
|
def handle_output(output):
|
||||||
|
buf.extend(output)
|
||||||
|
while b"\n" in buf:
|
||||||
|
line, buf[:] = buf.split(b"\n", 1)
|
||||||
|
line = line.decode("utf-8", "replace")
|
||||||
|
if re.match(regexp, line):
|
||||||
|
callback(line)
|
||||||
|
runner.qemu.on_output.append(handle_output)
|
||||||
|
return setup_call_on_line
|
||||||
|
|
||||||
|
def stop_on_line(regexp):
|
||||||
|
"""Returns a monitor that stops when QEMU prints a line matching
|
||||||
|
'regexp'."""
|
||||||
|
|
||||||
|
def stop(line):
|
||||||
|
raise TerminateTest
|
||||||
|
return call_on_line(regexp, stop)
|
||||||
|
|
||||||
|
def shell_script(script, terminate_match=None):
|
||||||
|
"""Returns a monitor that plays the script, and stops when the script is
|
||||||
|
done executing."""
|
||||||
|
|
||||||
|
def setup_call_on_line(runner):
|
||||||
|
class context:
|
||||||
|
n = 0
|
||||||
|
buf = bytearray()
|
||||||
|
def handle_output(output):
|
||||||
|
context.buf.extend(output)
|
||||||
|
if terminate_match is not None:
|
||||||
|
if re.match(terminate_match, context.buf.decode('utf-8', 'replace')):
|
||||||
|
raise TerminateTest
|
||||||
|
if b'$ ' in context.buf:
|
||||||
|
context.buf = bytearray()
|
||||||
|
if context.n < len(script):
|
||||||
|
runner.qemu.write(script[context.n])
|
||||||
|
runner.qemu.write('\n')
|
||||||
|
context.n += 1
|
||||||
|
else:
|
||||||
|
if terminate_match is None:
|
||||||
|
raise TerminateTest
|
||||||
|
runner.qemu.on_output.append(handle_output)
|
||||||
|
return setup_call_on_line
|
||||||
@ -187,3 +187,6 @@ void virtio_disk_intr(void);
|
|||||||
|
|
||||||
// number of elements in fixed-size array
|
// number of elements in fixed-size array
|
||||||
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
|
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
|
||||||
|
|
||||||
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||||
@ -3,3 +3,13 @@
|
|||||||
#define O_RDWR 0x002
|
#define O_RDWR 0x002
|
||||||
#define O_CREATE 0x200
|
#define O_CREATE 0x200
|
||||||
#define O_TRUNC 0x400
|
#define O_TRUNC 0x400
|
||||||
|
|
||||||
|
#ifdef LAB_MMAP
|
||||||
|
#define PROT_NONE 0x0
|
||||||
|
#define PROT_READ 0x1
|
||||||
|
#define PROT_WRITE 0x2
|
||||||
|
#define PROT_EXEC 0x4
|
||||||
|
|
||||||
|
#define MAP_SHARED 0x01
|
||||||
|
#define MAP_PRIVATE 0x02
|
||||||
|
#endif
|
||||||
|
|||||||
171
kernel/proc.c
171
kernel/proc.c
@ -5,6 +5,15 @@
|
|||||||
#include "spinlock.h"
|
#include "spinlock.h"
|
||||||
#include "proc.h"
|
#include "proc.h"
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
#include "fcntl.h"
|
||||||
|
#include "sleeplock.h"
|
||||||
|
#include "fs.h"
|
||||||
|
#include "file.h"
|
||||||
|
|
||||||
|
static struct vma vma_pool[NMAXVMA];
|
||||||
|
static struct spinlock lock_vma_pool;
|
||||||
|
uint64 do_munmap(uint64 addr, int length);
|
||||||
|
uint64 do_mmap(struct proc* p, int length, int prot, int flags, int fd);
|
||||||
|
|
||||||
struct cpu cpus[NCPU];
|
struct cpu cpus[NCPU];
|
||||||
|
|
||||||
@ -33,7 +42,7 @@ void
|
|||||||
proc_mapstacks(pagetable_t kpgtbl)
|
proc_mapstacks(pagetable_t kpgtbl)
|
||||||
{
|
{
|
||||||
struct proc *p;
|
struct proc *p;
|
||||||
|
initlock(&lock_vma_pool, "vma_pool");
|
||||||
for(p = proc; p < &proc[NPROC]; p++) {
|
for(p = proc; p < &proc[NPROC]; p++) {
|
||||||
char *pa = kalloc();
|
char *pa = kalloc();
|
||||||
if(pa == 0)
|
if(pa == 0)
|
||||||
@ -318,6 +327,31 @@ fork(void)
|
|||||||
np->parent = p;
|
np->parent = p;
|
||||||
release(&wait_lock);
|
release(&wait_lock);
|
||||||
|
|
||||||
|
acquire(&np->lock);
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
if (p->map_region[i]) {
|
||||||
|
struct vma* newvma = 0;
|
||||||
|
acquire(&lock_vma_pool);
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
if (vma_pool[i].length == 0) {
|
||||||
|
newvma = &vma_pool[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!newvma || newvma->length)
|
||||||
|
panic("mmap: vma pool run out");
|
||||||
|
newvma->f = filedup(p->map_region[i]->f);
|
||||||
|
newvma->addr = p->map_region[i]->addr;
|
||||||
|
newvma->prot = p->map_region[i]->prot;
|
||||||
|
newvma->length = p->map_region[i]->length;
|
||||||
|
newvma->flag = p->map_region[i]->flag;
|
||||||
|
newvma->offset = p->map_region[i]->offset;
|
||||||
|
release(&lock_vma_pool);
|
||||||
|
np->map_region[i] = newvma;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
release(&np->lock);
|
||||||
|
|
||||||
acquire(&np->lock);
|
acquire(&np->lock);
|
||||||
np->state = RUNNABLE;
|
np->state = RUNNABLE;
|
||||||
release(&np->lock);
|
release(&np->lock);
|
||||||
@ -351,6 +385,13 @@ exit(int status)
|
|||||||
if(p == initproc)
|
if(p == initproc)
|
||||||
panic("init exiting");
|
panic("init exiting");
|
||||||
|
|
||||||
|
// clear mmaps
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
if (p->map_region[i]) {
|
||||||
|
do_munmap(p->map_region[i]->addr, p->map_region[i]->length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Close all open files.
|
// Close all open files.
|
||||||
for(int fd = 0; fd < NOFILE; fd++){
|
for(int fd = 0; fd < NOFILE; fd++){
|
||||||
if(p->ofile[fd]){
|
if(p->ofile[fd]){
|
||||||
@ -681,3 +722,131 @@ procdump(void)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64 do_mmap(struct proc* p, int length, int prot, int flags, int fd)
|
||||||
|
{
|
||||||
|
uint64 addr;
|
||||||
|
struct vma* newvma = 0;
|
||||||
|
if (fd < 0 || fd > NOFILE || p->ofile[fd] == 0){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!p->ofile[fd]->writable && (prot & PROT_WRITE) && (flags != MAP_PRIVATE)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// find vacant addr space
|
||||||
|
addr = MMAP_START;
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
// in case some va has been occupied by some other vma but not accessed
|
||||||
|
if (p->map_region[i]) {
|
||||||
|
int _tmp = PGROUNDUP(p->map_region[i]->addr + length);
|
||||||
|
addr = _tmp > addr ? _tmp : addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
acquire(&lock_vma_pool);
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
if (vma_pool[i].length == 0) {
|
||||||
|
newvma = &vma_pool[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!newvma || newvma->length)
|
||||||
|
panic("mmap: vma pool run out");
|
||||||
|
newvma->f = p->ofile[fd];
|
||||||
|
newvma->addr = addr;
|
||||||
|
newvma->prot = prot;
|
||||||
|
newvma->length = length;
|
||||||
|
newvma->flag = flags;
|
||||||
|
newvma->offset = 0;
|
||||||
|
release(&lock_vma_pool);
|
||||||
|
|
||||||
|
filedup(newvma->f);
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
if (p->map_region[i] == 0) {
|
||||||
|
p->map_region[i] = newvma;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// void *mmap(void *addr, int length, int prot, int flags, int fd, int offset);
|
||||||
|
// int munmap(void *addr, int length);
|
||||||
|
uint64 sys_mmap(void)
|
||||||
|
{
|
||||||
|
uint64 addr;
|
||||||
|
int length, prot, flags, fd, offset;
|
||||||
|
argaddr(0, &addr);
|
||||||
|
argint(1, &length);
|
||||||
|
argint(2, &prot);
|
||||||
|
argint(3, &flags);
|
||||||
|
argint(4, &fd);
|
||||||
|
argint(5, &offset);
|
||||||
|
// some preliminary check
|
||||||
|
if (addr != 0 || offset != 0) panic("Unsupported non-zero value for arg addr and off");
|
||||||
|
return do_mmap(myproc(), length, prot, flags, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64 do_munmap(uint64 addr, int length)
|
||||||
|
{
|
||||||
|
struct vma* vma0 = 0;
|
||||||
|
struct proc* p = myproc();
|
||||||
|
int imap = 0;
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
if(p->map_region[i]
|
||||||
|
&& p->map_region[i]->addr <= addr
|
||||||
|
&& addr < p->map_region[i]->addr + p->map_region[i]->length) {
|
||||||
|
vma0 = p->map_region[i];
|
||||||
|
imap = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (vma0 == 0) {
|
||||||
|
printf("failed to ummap %p\n", addr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// remove all relavant pages
|
||||||
|
// by reading the test code, no need to worry about non-aligned unmap
|
||||||
|
// or those unmaps which would break a vma into parts
|
||||||
|
uint64 vma_end = PGROUNDUP(vma0->addr + vma0->length);
|
||||||
|
uint64 free_start = MAX(PGROUNDDOWN(addr), vma0->addr);
|
||||||
|
uint64 free_end = MIN(PGROUNDUP(addr + length), vma_end);
|
||||||
|
for (uint64 va0 = free_start; va0 < free_end; va0 += PGSIZE) {
|
||||||
|
if (walkaddr(p->pagetable, va0)){
|
||||||
|
if (vma0->flag == MAP_SHARED) {
|
||||||
|
begin_op();
|
||||||
|
ilock(vma0->f->ip);
|
||||||
|
// int wrsz =
|
||||||
|
writei(vma0->f->ip, 1, va0, va0 + vma0->offset - vma0->addr, PGSIZE); // ? no sure if to write whole page
|
||||||
|
// printf("Write %d bytes from %p @offset=%d(off=%x,addr=%x)\n", wrsz, va0, va0 + vma0->offset - vma0->addr, vma0->offset, vma0->addr);
|
||||||
|
iunlock(vma0->f->ip);
|
||||||
|
end_op();
|
||||||
|
}
|
||||||
|
uvmunmap(p->pagetable, va0, 1, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (free_start <= vma0->addr && free_end >= vma_end) {
|
||||||
|
// all released
|
||||||
|
fileclose(vma0->f);
|
||||||
|
memset(vma0, 0, sizeof(struct vma));
|
||||||
|
p->map_region[imap] = 0;
|
||||||
|
} else if (free_end >= vma_end) {
|
||||||
|
vma0->length = free_start - vma0->addr;
|
||||||
|
} else if (free_start <= vma0->addr) {
|
||||||
|
vma0->length -= free_end - vma0->addr;
|
||||||
|
vma0->offset += free_end - vma0->addr;
|
||||||
|
vma0->addr = free_end;
|
||||||
|
} else {
|
||||||
|
panic("unsupported unmap type");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64 sys_munmap(void)
|
||||||
|
{
|
||||||
|
uint64 addr;
|
||||||
|
int length;
|
||||||
|
argaddr(0, &addr);
|
||||||
|
argint(1, &length);
|
||||||
|
return do_munmap(addr, length);
|
||||||
|
}
|
||||||
@ -81,6 +81,18 @@ struct trapframe {
|
|||||||
|
|
||||||
enum procstate { UNUSED, USED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
|
enum procstate { UNUSED, USED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
|
||||||
|
|
||||||
|
struct vma {
|
||||||
|
uint64 addr;
|
||||||
|
uint length;
|
||||||
|
uint prot;
|
||||||
|
uint flag;
|
||||||
|
uint offset;
|
||||||
|
struct file* f;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define NMAXVMA 20
|
||||||
|
#define MMAP_START (0x40000000)
|
||||||
|
|
||||||
// Per-process state
|
// Per-process state
|
||||||
struct proc {
|
struct proc {
|
||||||
struct spinlock lock;
|
struct spinlock lock;
|
||||||
@ -103,5 +115,6 @@ struct proc {
|
|||||||
struct context context; // swtch() here to run process
|
struct context context; // swtch() here to run process
|
||||||
struct file *ofile[NOFILE]; // Open files
|
struct file *ofile[NOFILE]; // Open files
|
||||||
struct inode *cwd; // Current directory
|
struct inode *cwd; // Current directory
|
||||||
|
struct vma* map_region[NMAXVMA];
|
||||||
char name[16]; // Process name (debugging)
|
char name[16]; // Process name (debugging)
|
||||||
};
|
};
|
||||||
|
|||||||
@ -101,6 +101,8 @@ extern uint64 sys_unlink(void);
|
|||||||
extern uint64 sys_link(void);
|
extern uint64 sys_link(void);
|
||||||
extern uint64 sys_mkdir(void);
|
extern uint64 sys_mkdir(void);
|
||||||
extern uint64 sys_close(void);
|
extern uint64 sys_close(void);
|
||||||
|
extern uint64 sys_mmap(void);
|
||||||
|
extern uint64 sys_munmap(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.
|
||||||
@ -126,6 +128,8 @@ static uint64 (*syscalls[])(void) = {
|
|||||||
[SYS_link] sys_link,
|
[SYS_link] sys_link,
|
||||||
[SYS_mkdir] sys_mkdir,
|
[SYS_mkdir] sys_mkdir,
|
||||||
[SYS_close] sys_close,
|
[SYS_close] sys_close,
|
||||||
|
[SYS_mmap] sys_mmap,
|
||||||
|
[SYS_munmap] sys_munmap,
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
@ -20,3 +20,5 @@
|
|||||||
#define SYS_link 19
|
#define SYS_link 19
|
||||||
#define SYS_mkdir 20
|
#define SYS_mkdir 20
|
||||||
#define SYS_close 21
|
#define SYS_close 21
|
||||||
|
#define SYS_mmap 22
|
||||||
|
#define SYS_munmap 23
|
||||||
|
|||||||
@ -55,6 +55,8 @@ sys_sleep(void)
|
|||||||
uint ticks0;
|
uint ticks0;
|
||||||
|
|
||||||
argint(0, &n);
|
argint(0, &n);
|
||||||
|
if(n < 0)
|
||||||
|
n = 0;
|
||||||
acquire(&tickslock);
|
acquire(&tickslock);
|
||||||
ticks0 = ticks;
|
ticks0 = ticks;
|
||||||
while(ticks - ticks0 < n){
|
while(ticks - ticks0 < n){
|
||||||
|
|||||||
@ -5,6 +5,10 @@
|
|||||||
#include "spinlock.h"
|
#include "spinlock.h"
|
||||||
#include "proc.h"
|
#include "proc.h"
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
#include "fcntl.h"
|
||||||
|
#include "sleeplock.h"
|
||||||
|
#include "fs.h"
|
||||||
|
#include "file.h"
|
||||||
|
|
||||||
struct spinlock tickslock;
|
struct spinlock tickslock;
|
||||||
uint ticks;
|
uint ticks;
|
||||||
@ -65,9 +69,36 @@ usertrap(void)
|
|||||||
intr_on();
|
intr_on();
|
||||||
|
|
||||||
syscall();
|
syscall();
|
||||||
|
} else if(r_scause() == 13){
|
||||||
|
int va0 = r_stval();
|
||||||
|
struct vma* vma0 = 0;
|
||||||
|
for (int i = 0; i < NMAXVMA; ++ i) {
|
||||||
|
if(p->map_region[i]
|
||||||
|
&& p->map_region[i]->addr <= va0
|
||||||
|
&& va0 < p->map_region[i]->addr + p->map_region[i]->length) {
|
||||||
|
vma0 = p->map_region[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (vma0 == 0) {
|
||||||
|
goto bad;
|
||||||
|
}
|
||||||
|
uint64 phy_addr = (uint64)kalloc();
|
||||||
|
uint pgflag = PTE_U | (vma0->prot << 1);
|
||||||
|
// save some code... there is PTE_R/W/X = PROT_READ/WRITE/EXEC << 1
|
||||||
|
mappages(p->pagetable, va0, PGSIZE, phy_addr, pgflag);
|
||||||
|
memset((void*)phy_addr, 0, PGSIZE);
|
||||||
|
begin_op();
|
||||||
|
ilock(vma0->f->ip);
|
||||||
|
// int rdsz =
|
||||||
|
readi(vma0->f->ip, 0, phy_addr, va0 + vma0->offset - vma0->addr, PGSIZE);
|
||||||
|
// printf("Load %d bytes to %p @offset=%d\n", rdsz, va0,va0 + vma0->offset - vma0->addr);
|
||||||
|
iunlock(vma0->f->ip);
|
||||||
|
end_op();
|
||||||
} else if((which_dev = devintr()) != 0){
|
} else if((which_dev = devintr()) != 0){
|
||||||
// ok
|
// ok
|
||||||
} else {
|
} else {
|
||||||
|
bad:
|
||||||
printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
|
printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
|
||||||
printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
|
printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
|
||||||
setkilled(p);
|
setkilled(p);
|
||||||
|
|||||||
@ -179,8 +179,10 @@ uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free)
|
|||||||
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)
|
if((pte = walk(pagetable, a, 0)) == 0)
|
||||||
panic("uvmunmap: walk");
|
panic("uvmunmap: walk");
|
||||||
if((*pte & PTE_V) == 0)
|
if((*pte & PTE_V) == 0){
|
||||||
|
printf("%p\n", va);
|
||||||
panic("uvmunmap: not mapped");
|
panic("uvmunmap: not mapped");
|
||||||
|
}
|
||||||
if(PTE_FLAGS(*pte) == PTE_V)
|
if(PTE_FLAGS(*pte) == PTE_V)
|
||||||
panic("uvmunmap: not a leaf");
|
panic("uvmunmap: not a leaf");
|
||||||
if(do_free){
|
if(do_free){
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
|
#include "kernel/fcntl.h"
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
char buf[512];
|
char buf[512];
|
||||||
@ -32,7 +33,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(i = 1; i < argc; i++){
|
for(i = 1; i < argc; i++){
|
||||||
if((fd = open(argv[i], 0)) < 0){
|
if((fd = open(argv[i], O_RDONLY)) < 0){
|
||||||
fprintf(2, "cat: cannot open %s\n", argv[i]);
|
fprintf(2, "cat: cannot open %s\n", argv[i]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
|
#include "kernel/fcntl.h"
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
@ -51,7 +52,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(i = 2; i < argc; i++){
|
for(i = 2; i < argc; i++){
|
||||||
if((fd = open(argv[i], 0)) < 0){
|
if((fd = open(argv[i], O_RDONLY)) < 0){
|
||||||
printf("grep: cannot open %s\n", argv[i]);
|
printf("grep: cannot open %s\n", argv[i]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
#include "kernel/fs.h"
|
#include "kernel/fs.h"
|
||||||
|
#include "kernel/fcntl.h"
|
||||||
|
|
||||||
char*
|
char*
|
||||||
fmtname(char *path)
|
fmtname(char *path)
|
||||||
@ -30,7 +31,7 @@ ls(char *path)
|
|||||||
struct dirent de;
|
struct dirent de;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if((fd = open(path, 0)) < 0){
|
if((fd = open(path, O_RDONLY)) < 0){
|
||||||
fprintf(2, "ls: cannot open %s\n", path);
|
fprintf(2, "ls: cannot open %s\n", path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
297
user/mmaptest.c
Normal file
297
user/mmaptest.c
Normal file
@ -0,0 +1,297 @@
|
|||||||
|
#include "kernel/param.h"
|
||||||
|
#include "kernel/fcntl.h"
|
||||||
|
#include "kernel/types.h"
|
||||||
|
#include "kernel/stat.h"
|
||||||
|
#include "kernel/riscv.h"
|
||||||
|
#include "kernel/fs.h"
|
||||||
|
#include "user/user.h"
|
||||||
|
|
||||||
|
void mmap_test();
|
||||||
|
void fork_test();
|
||||||
|
char buf[BSIZE];
|
||||||
|
|
||||||
|
#define MAP_FAILED ((char *) -1)
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
mmap_test();
|
||||||
|
fork_test();
|
||||||
|
printf("mmaptest: all tests succeeded\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *testname = "???";
|
||||||
|
|
||||||
|
void
|
||||||
|
err(char *why)
|
||||||
|
{
|
||||||
|
printf("mmaptest: %s failed: %s, pid=%d\n", testname, why, getpid());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// check the content of the two mapped pages.
|
||||||
|
//
|
||||||
|
void
|
||||||
|
_v1(char *p)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < PGSIZE*2; i++) {
|
||||||
|
if (i < PGSIZE + (PGSIZE/2)) {
|
||||||
|
if (p[i] != 'A') {
|
||||||
|
printf("mismatch at %d, wanted 'A', got 0x%x\n", i, p[i]);
|
||||||
|
err("v1 mismatch (1)");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (p[i] != 0) {
|
||||||
|
printf("mismatch at %d, wanted zero, got 0x%x\n", i, p[i]);
|
||||||
|
err("v1 mismatch (2)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// create a file to be mapped, containing
|
||||||
|
// 1.5 pages of 'A' and half a page of zeros.
|
||||||
|
//
|
||||||
|
void
|
||||||
|
makefile(const char *f)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int n = PGSIZE/BSIZE;
|
||||||
|
|
||||||
|
unlink(f);
|
||||||
|
int fd = open(f, O_WRONLY | O_CREATE);
|
||||||
|
if (fd == -1)
|
||||||
|
err("open");
|
||||||
|
memset(buf, 'A', BSIZE);
|
||||||
|
// write 1.5 page
|
||||||
|
for (i = 0; i < n + n/2; i++) {
|
||||||
|
if (write(fd, buf, BSIZE) != BSIZE)
|
||||||
|
err("write 0 makefile");
|
||||||
|
}
|
||||||
|
if (close(fd) == -1)
|
||||||
|
err("close");
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
mmap_test(void)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int i;
|
||||||
|
const char * const f = "mmap.dur";
|
||||||
|
printf("mmap_test starting\n");
|
||||||
|
testname = "mmap_test";
|
||||||
|
|
||||||
|
//
|
||||||
|
// create a file with known content, map it into memory, check that
|
||||||
|
// the mapped memory has the same bytes as originally written to the
|
||||||
|
// file.
|
||||||
|
//
|
||||||
|
makefile(f);
|
||||||
|
if ((fd = open(f, O_RDONLY)) == -1)
|
||||||
|
err("open");
|
||||||
|
|
||||||
|
printf("test mmap f\n");
|
||||||
|
//
|
||||||
|
// this call to mmap() asks the kernel to map the content
|
||||||
|
// of open file fd into the address space. the first
|
||||||
|
// 0 argument indicates that the kernel should choose the
|
||||||
|
// virtual address. the second argument indicates how many
|
||||||
|
// bytes to map. the third argument indicates that the
|
||||||
|
// mapped memory should be read-only. the fourth argument
|
||||||
|
// indicates that, if the process modifies the mapped memory,
|
||||||
|
// that the modifications should not be written back to
|
||||||
|
// the file nor shared with other processes mapping the
|
||||||
|
// same file (of course in this case updates are prohibited
|
||||||
|
// due to PROT_READ). the fifth argument is the file descriptor
|
||||||
|
// of the file to be mapped. the last argument is the starting
|
||||||
|
// offset in the file.
|
||||||
|
//
|
||||||
|
char *p = mmap(0, PGSIZE*2, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
|
if (p == MAP_FAILED)
|
||||||
|
err("mmap (1)");
|
||||||
|
_v1(p);
|
||||||
|
if (munmap(p, PGSIZE*2) == -1)
|
||||||
|
err("munmap (1)");
|
||||||
|
|
||||||
|
printf("test mmap f: OK\n");
|
||||||
|
|
||||||
|
printf("test mmap private\n");
|
||||||
|
// should be able to map file opened read-only with private writable
|
||||||
|
// mapping
|
||||||
|
p = mmap(0, PGSIZE*2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
|
||||||
|
if (p == MAP_FAILED)
|
||||||
|
err("mmap (2)");
|
||||||
|
if (close(fd) == -1)
|
||||||
|
err("close");
|
||||||
|
_v1(p);
|
||||||
|
for (i = 0; i < PGSIZE*2; i++)
|
||||||
|
p[i] = 'Z';
|
||||||
|
if (munmap(p, PGSIZE*2) == -1)
|
||||||
|
err("munmap (2)");
|
||||||
|
|
||||||
|
printf("test mmap private: OK\n");
|
||||||
|
|
||||||
|
printf("test mmap read-only\n");
|
||||||
|
|
||||||
|
// check that mmap doesn't allow read/write mapping of a
|
||||||
|
// file opened read-only.
|
||||||
|
if ((fd = open(f, O_RDONLY)) == -1)
|
||||||
|
err("open");
|
||||||
|
p = mmap(0, PGSIZE*3, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (p != MAP_FAILED)
|
||||||
|
err("mmap call should have failed");
|
||||||
|
if (close(fd) == -1)
|
||||||
|
err("close");
|
||||||
|
|
||||||
|
printf("test mmap read-only: OK\n");
|
||||||
|
|
||||||
|
printf("test mmap read/write\n");
|
||||||
|
|
||||||
|
// check that mmap does allow read/write mapping of a
|
||||||
|
// file opened read/write.
|
||||||
|
if ((fd = open(f, O_RDWR)) == -1)
|
||||||
|
err("open");
|
||||||
|
p = mmap(0, PGSIZE*3, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (p == MAP_FAILED)
|
||||||
|
err("mmap (3)");
|
||||||
|
if (close(fd) == -1)
|
||||||
|
err("close");
|
||||||
|
|
||||||
|
// check that the mapping still works after close(fd).
|
||||||
|
_v1(p);
|
||||||
|
|
||||||
|
// write the mapped memory.
|
||||||
|
for (i = 0; i < PGSIZE*2; i++)
|
||||||
|
p[i] = 'Z';
|
||||||
|
|
||||||
|
// unmap just the first two of three pages of mapped memory.
|
||||||
|
if (munmap(p, PGSIZE*2) == -1)
|
||||||
|
err("munmap (3)");
|
||||||
|
|
||||||
|
printf("test mmap read/write: OK\n");
|
||||||
|
|
||||||
|
printf("test mmap dirty\n");
|
||||||
|
|
||||||
|
// check that the writes to the mapped memory were
|
||||||
|
// written to the file.
|
||||||
|
if ((fd = open(f, O_RDWR)) == -1)
|
||||||
|
err("open");
|
||||||
|
for (i = 0; i < PGSIZE + (PGSIZE/2); i++){
|
||||||
|
char b;
|
||||||
|
if (read(fd, &b, 1) != 1)
|
||||||
|
err("read (1)");
|
||||||
|
if (b != 'Z')
|
||||||
|
err("file does not contain modifications");
|
||||||
|
}
|
||||||
|
if (close(fd) == -1)
|
||||||
|
err("close");
|
||||||
|
|
||||||
|
printf("test mmap dirty: OK\n");
|
||||||
|
|
||||||
|
printf("test not-mapped unmap\n");
|
||||||
|
|
||||||
|
// unmap the rest of the mapped memory.
|
||||||
|
if (munmap(p+PGSIZE*2, PGSIZE) == -1)
|
||||||
|
err("munmap (4)");
|
||||||
|
|
||||||
|
printf("test not-mapped unmap: OK\n");
|
||||||
|
|
||||||
|
printf("test mmap two files\n");
|
||||||
|
|
||||||
|
//
|
||||||
|
// mmap two files at the same time.
|
||||||
|
//
|
||||||
|
int fd1;
|
||||||
|
if((fd1 = open("mmap1", O_RDWR|O_CREATE)) < 0)
|
||||||
|
err("open mmap1");
|
||||||
|
if(write(fd1, "12345", 5) != 5)
|
||||||
|
err("write mmap1");
|
||||||
|
char *p1 = mmap(0, PGSIZE, PROT_READ, MAP_PRIVATE, fd1, 0);
|
||||||
|
if(p1 == MAP_FAILED)
|
||||||
|
err("mmap mmap1");
|
||||||
|
close(fd1);
|
||||||
|
unlink("mmap1");
|
||||||
|
|
||||||
|
int fd2;
|
||||||
|
if((fd2 = open("mmap2", O_RDWR|O_CREATE)) < 0)
|
||||||
|
err("open mmap2");
|
||||||
|
if(write(fd2, "67890", 5) != 5)
|
||||||
|
err("write mmap2");
|
||||||
|
char *p2 = mmap(0, PGSIZE, PROT_READ, MAP_PRIVATE, fd2, 0);
|
||||||
|
if(p2 == MAP_FAILED)
|
||||||
|
err("mmap mmap2");
|
||||||
|
close(fd2);
|
||||||
|
unlink("mmap2");
|
||||||
|
|
||||||
|
if(memcmp(p1, "12345", 5) != 0)
|
||||||
|
err("mmap1 mismatch");
|
||||||
|
if(memcmp(p2, "67890", 5) != 0)
|
||||||
|
err("mmap2 mismatch");
|
||||||
|
|
||||||
|
munmap(p1, PGSIZE);
|
||||||
|
if(memcmp(p2, "67890", 5) != 0)
|
||||||
|
err("mmap2 mismatch (2)");
|
||||||
|
munmap(p2, PGSIZE);
|
||||||
|
|
||||||
|
printf("test mmap two files: OK\n");
|
||||||
|
|
||||||
|
printf("mmap_test: ALL OK\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// mmap a file, then fork.
|
||||||
|
// check that the child sees the mapped file.
|
||||||
|
//
|
||||||
|
void
|
||||||
|
fork_test(void)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int pid;
|
||||||
|
const char * const f = "mmap.dur";
|
||||||
|
|
||||||
|
printf("fork_test starting\n");
|
||||||
|
testname = "fork_test";
|
||||||
|
|
||||||
|
// mmap the file twice.
|
||||||
|
makefile(f);
|
||||||
|
if ((fd = open(f, O_RDONLY)) == -1)
|
||||||
|
err("open");
|
||||||
|
unlink(f);
|
||||||
|
char *p1 = mmap(0, PGSIZE*2, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
if (p1 == MAP_FAILED)
|
||||||
|
err("mmap (4)");
|
||||||
|
char *p2 = mmap(0, PGSIZE*2, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
if (p2 == MAP_FAILED)
|
||||||
|
err("mmap (5)");
|
||||||
|
|
||||||
|
// read just 2nd page.
|
||||||
|
if(*(p1+PGSIZE) != 'A')
|
||||||
|
err("fork mismatch (1)");
|
||||||
|
|
||||||
|
if((pid = fork()) < 0)
|
||||||
|
err("fork");
|
||||||
|
if (pid == 0) {
|
||||||
|
_v1(p1);
|
||||||
|
munmap(p1, PGSIZE); // just the first page
|
||||||
|
exit(0); // tell the parent that the mapping looks OK.
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = -1;
|
||||||
|
wait(&status);
|
||||||
|
|
||||||
|
if(status != 0){
|
||||||
|
printf("fork_test failed\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check that the parent's mappings are still there.
|
||||||
|
_v1(p1);
|
||||||
|
_v1(p2);
|
||||||
|
|
||||||
|
printf("fork_test OK\n");
|
||||||
|
}
|
||||||
|
|
||||||
@ -22,6 +22,8 @@ int getpid(void);
|
|||||||
char* sbrk(int);
|
char* sbrk(int);
|
||||||
int sleep(int);
|
int sleep(int);
|
||||||
int uptime(void);
|
int uptime(void);
|
||||||
|
void *mmap(void *addr, int length, int prot, int flags, int fd, int offset);
|
||||||
|
int munmap(void *addr, int length);
|
||||||
|
|
||||||
// ulib.c
|
// ulib.c
|
||||||
int stat(const char*, struct stat*);
|
int stat(const char*, struct stat*);
|
||||||
|
|||||||
@ -36,3 +36,5 @@ entry("getpid");
|
|||||||
entry("sbrk");
|
entry("sbrk");
|
||||||
entry("sleep");
|
entry("sleep");
|
||||||
entry("uptime");
|
entry("uptime");
|
||||||
|
entry("mmap");
|
||||||
|
entry("munmap");
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
|
#include "kernel/fcntl.h"
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
char buf[512];
|
char buf[512];
|
||||||
@ -43,7 +44,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(i = 1; i < argc; i++){
|
for(i = 1; i < argc; i++){
|
||||||
if((fd = open(argv[i], 0)) < 0){
|
if((fd = open(argv[i], O_RDONLY)) < 0){
|
||||||
printf("wc: cannot open %s\n", argv[i]);
|
printf("wc: cannot open %s\n", argv[i]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user