Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add threads and related syscalls #264

Open
wants to merge 5 commits into
base: riscv
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The xv6 software is:

Copyright (c) 2006-2024 Frans Kaashoek, Robert Morris, Russ Cox,
Copyright (c) 2006-2019 Frans Kaashoek, Robert Morris, Russ Cox,
Massachusetts Institute of Technology

Permission is hereby granted, free of charge, to any person obtaining
Expand Down
169 changes: 154 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

# To compile and run with a lab solution, set the lab name in lab.mk
# (e.g., LB=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

Expand Down Expand Up @@ -28,7 +35,11 @@ OBJS = \
$K/sysfile.o \
$K/kernelvec.o \
$K/plic.o \
$K/virtio_disk.o
$K/virtio_disk.o \

ifeq ($(LAB),pgtbl)
OBJS += $K/vmcopyin.o
endif

# riscv64-unknown-elf- or riscv64-linux-gnu-
# perhaps in /opt/riscv/bin
Expand Down Expand Up @@ -56,17 +67,26 @@ LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump

CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb

ifdef LAB_SYSCALL_TEST
CFLAGS += -DLAB_SYSCALL_TEST
endif

GCC_VER12 := $(shell expr `gcc -dumpfullversion -dumpversion | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$$/&00/'` \>= 120000)
ifeq "$(GCC_VER12)" "1"
CFLAGS += -Wno-error=infinite-recursion
endif


ifdef LAB
LABUPPER = $(shell echo $(LAB) | tr a-z A-Z)
CFLAGS += -DSOL_$(LABUPPER)
endif

CFLAGS += -MD
CFLAGS += -mcmodel=medany
# CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
CFLAGS += -fno-common -nostdlib
CFLAGS += -fno-builtin-strncpy -fno-builtin-strncmp -fno-builtin-strlen -fno-builtin-memset
CFLAGS += -fno-builtin-memmove -fno-builtin-memcmp -fno-builtin-log -fno-builtin-bzero
CFLAGS += -fno-builtin-strchr -fno-builtin-exit -fno-builtin-malloc -fno-builtin-putc
CFLAGS += -fno-builtin-free
CFLAGS += -fno-builtin-memcpy -Wno-main
CFLAGS += -fno-builtin-printf -fno-builtin-fprintf -fno-builtin-vprintf
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)

Expand Down Expand Up @@ -97,7 +117,7 @@ tags: $(OBJS) _init
ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o

_%: %.o $(ULIB)
$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^
$(OBJDUMP) -S $@ > $*.asm
$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym

Expand All @@ -110,7 +130,7 @@ $U/usys.o : $U/usys.S
$U/_forktest: $U/forktest.o $(ULIB)
# forktest has less library code linked in - needs to be small
# in order to be able to max out the proc table.
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_forktest $U/forktest.o $U/ulib.o $U/usys.o
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_forktest $U/forktest.o $U/ulib.o $U/usys.o $U/umalloc.o
$(OBJDUMP) -S $U/_forktest > $U/forktest.asm

mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h
Expand Down Expand Up @@ -139,9 +159,35 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_waittest\
$U/_exittest\
$U/_yieldtest\
$U/_threadtest\


ifeq ($(LAB),trap)
UPROGS += \
$U/_call\
$U/_alarmtest
endif

fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
ifeq ($(LAB),lazy)
UPROGS += \
$U/_lazytests
endif

ifeq ($(LAB),cow)
UPROGS += \
$U/_cowtest
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

Expand All @@ -164,7 +210,6 @@ CPUS := 3
endif

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

Expand All @@ -178,3 +223,97 @@ qemu-gdb: $K/kernel .gdbinit fs.img
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)

##
## 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) --toolprefix $(TOOLPREFIX)

format:
python3 clang-format.py

diff:
git diff syscall-base HEAD > commit.patch

##
## FOR web handin
##


WEBSUB := https://6828.scripts.mit.edu/2020/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-2020" | 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
45 changes: 22 additions & 23 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,40 @@ ACKNOWLEDGMENTS

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,
2000)). See also https://pdos.csail.mit.edu/6.1810/, which provides
pointers to on-line resources for v6.
2000)). See also https://pdos.csail.mit.edu/6.828/, which
provides pointers to on-line resources for v6.

The following people have made contributions: Russ Cox (context switching,
locking), Cliff Frey (MP), Xiao Yu (MP), Nickolai Zeldovich, and Austin
Clements.

We are also grateful for the bug reports and patches contributed by
Takahiro Aoyagi, Marcelo Arroyo, Silas Boyd-Wickizer, Anton Burtsev,
carlclone, Ian Chen, Dan Cross, Cody Cutler, Mike CAT, Tej Chajed,
Asami Doi,Wenyang Duan, eyalz800, Nelson Elhage, Saar Ettinger, Alice
Ferrazzi, Nathaniel Filardo, flespark, Peter Froehlich, Yakir Goaron,
Shivam Handa, Matt Harvey, Bryan Henry, jaichenhengjie, Jim Huang,
Matúš Jókay, John Jolly, Alexander Kapshuk, Anders Kaseorg, kehao95,
Wolfgang Keller, Jungwoo Kim, Jonathan Kimmitt, Eddie Kohler, Vadim
Kolontsov, Austin Liew, l0stman, Pavan Maddamsetti, Imbar Marinescu,
Yandong Mao, Matan Shabtay, Hitoshi Mitake, Carmi Merimovich, Mark
Morrissey, mtasm, Joel Nider, Hayato Ohhashi, OptimisticSide,
phosphagos, Harry Porter, Greg Price, RayAndrew, Jude Rich, segfault,
Ayan Shafqat, Eldar Sehayek, Yongming Shen, Fumiya Shigemitsu, snoire,
Taojie, Cam Tenny, tyfkda, Warren Toomey, Stephen Tu, Alissa Tung,
Rafael Ubal, Amane Uehara, Pablo Ventura, Xi Wang, WaheedHafez,
Keiichi Watanabe, Lucas Wolf, Nicolas Wolovick, wxdao, Grant Wu, x653,
Jindong Zhang, Icenowy Zheng, ZhUyU1997, and Zou Chang Wei.

ERROR REPORTS
Silas Boyd-Wickizer, Anton Burtsev, Dan Cross, Cody Cutler, Mike CAT,
Tej Chajed, Asami Doi, eyalz800, , Nelson Elhage, Saar Ettinger, Alice
Ferrazzi, Nathaniel Filardo, Peter Froehlich, Yakir Goaron,Shivam
Handa, Bryan Henry, jaichenhengjie, Jim Huang, Alexander Kapshuk,
Anders Kaseorg, kehao95, Wolfgang Keller, Jonathan Kimmitt, Eddie
Kohler, Austin Liew, Imbar Marinescu, Yandong Mao, Matan Shabtay,
Hitoshi Mitake, Carmi Merimovich, Mark Morrissey, mtasm, Joel Nider,
Greg Price, Ayan Shafqat, Eldar Sehayek, Yongming Shen, Fumiya
Shigemitsu, Takahiro, Cam Tenny, tyfkda, Rafael Ubal, Warren Toomey,
Stephen Tu, Pablo Ventura, Xi Wang, Keiichi Watanabe, Nicolas
Wolovick, wxdao, Grant Wu, Jindong Zhang, Icenowy Zheng, and Zou Chang
Wei.

syscall
The code in the files that constitute xv6 is
Copyright 2006-2020 Frans Kaashoek, Robert Morris, and Russ Cox.


Please send errors and suggestions to Frans Kaashoek and Robert Morris
(kaashoek,[email protected]). The main purpose of xv6 is as a teaching
operating system for MIT's 6.1810, so we are more interested in
(kaashoek,[email protected]). The main purpose of xv6 is as a teaching
operating system for MIT's 6.S081, so we are more interested in
simplifications and clarifications than new features.

BUILDING AND RUNNING XV6

You will need a RISC-V "newlib" tool chain from
https://github.com/riscv/riscv-gnu-toolchain, and qemu compiled for
riscv64-softmmu. Once they are installed, and in your shell
riscv64-softmmu. Once they are installed, and in your shell
search path, you can run "make qemu".
13 changes: 13 additions & 0 deletions clang-format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os

c_extensions = (".c")

for root, dirs, files in os.walk("kernel"):
for file in files:
if file.endswith(c_extensions):
os.system("clang-format -i -style=file " + root + "/" + file)

for root, dirs, files in os.walk("user"):
for file in files:
if file.endswith(c_extensions):
os.system("clang-format -i -style=file " + root + "/" + file)
1 change: 1 addition & 0 deletions conf/lab.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LAB=syscall
53 changes: 53 additions & 0 deletions grade-lab-syscall
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python

import re
import subprocess
from gradelib import *

r = Runner(save("xv6.out"))

@test(40, "exit test")
def test_exit_test():
subprocess.run(["make", "clean"])
r.run_qemu(shell_script([
'exittest'
]), make_args=["LAB_SYSCALL_TEST=on"])
r.match('proc \\d+ exit, parent pid \\d+, name \\w+, state \\w+')
r.match('proc \\d+ exit, child 0, pid \\d+, name \\w+, state \\w+')
r.match('proc \\d+ exit, child 1, pid \\d+, name \\w+, state \\w+')
r.match('proc \\d+ exit, child 2, pid \\d+, name \\w+, state \\w+')

@test(40, "wait test")
def test_wait_test():
r.run_qemu(shell_script([
'waittest'
]), make_args=["LAB_SYSCALL_TEST=on"])
r.match('wait test OK')

@test(19, "yield test")
def test_yield_test():
r.run_qemu(shell_script([
'yieldtest'
]), make_args=["CPUS=1", "LAB_SYSCALL_TEST=on"])
r.match("parent yield")
matches = re.findall("start to yield, user pc \w+", r.qemu.output, re.MULTILINE)
assert_equal(len(matches), 1)
sepc = matches[0][24:]
toolprefix = test_yield_test.options.toolprefix
result = subprocess.run([f"{toolprefix}addr2line", sepc, "-f", "-s", "-e", "user/_yieldtest"], stdout=subprocess.PIPE, check=True)
out = result.stdout.decode("utf-8")
assert(len(out) > 5)
assert_equal(out[:6], "yield\n")
r.match("switch to child 0")
r.match("switch to child 1")
r.match("switch to child 2")
r.match("parent yield finished")

@test(1, "time")
def test_time():
check_time()

run_tests()



Loading