27 Commits
main ... traps

Author SHA1 Message Date
7c42fbed17 trapslab finished 2025-06-23 19:08:25 +08:00
70ee668b9b comment added 2025-06-23 11:18:49 +08:00
c7c962ffef alarm task finished 2025-06-18 16:02:24 +08:00
4c08583743 backtrace task finished 2025-06-14 17:09:29 +08:00
b63d0c9147 answers-traps.txt added 2025-06-12 11:43:43 +08:00
028cf61d61 trapslab initialized 2025-06-12 10:45:04 +08:00
2a8fad821b usertest patched 2025-06-03 10:22:56 +08:00
ccc7f1ac12 pgtbl lab finished 2025-06-03 08:51:07 +08:00
7b2491c658 usertests and optional finished 2025-05-29 21:10:27 +08:00
e12b8c1d32 print_kpgtbl bug fixed 2025-05-29 20:26:55 +08:00
19e38dc598 task5 finished 2025-05-29 20:01:24 +08:00
b4bdc3e1db prepare for next 2025-05-29 20:00:24 +08:00
8fb0212778 task4 finished 2025-05-29 11:15:51 +08:00
f0aaffb0b6 task3 finished 2025-05-27 17:11:31 +08:00
bd21012f73 task2 finished 2025-05-27 15:59:57 +08:00
5292fdc6ac gcc-15 bug fixed 2025-05-23 21:47:16 +08:00
38997cbef6 pagetable lab initialized 2025-05-06 11:20:36 +08:00
0e751d690f optinal finished 2025-03-26 20:17:38 +08:00
2001e8e478 optinal load_avg to fix 2025-03-26 09:32:27 +08:00
e66ab82e63 syscall optional to finish 2025-03-25 19:18:29 +08:00
992f76ca30 syscall lab finished 2025-03-25 11:36:21 +08:00
a087b429df syscall trace finished 2025-03-25 10:35:06 +08:00
d92eea9e49 syscall lab need to finish 2025-03-25 01:13:50 +08:00
e33ff43dd6 syscall lab start 2025-03-08 11:25:52 +08:00
07fc8a52bd syscall branch 2025-03-04 11:23:18 +08:00
962a7083da optional added 2025-03-03 18:33:08 +08:00
a226786836 util branch case closed 2025-03-03 17:47:43 +08:00
53 changed files with 1219 additions and 1638 deletions

2
.gitignore vendored
View File

@ -24,3 +24,5 @@ ph
barrier
/lab-*.json
.DS_Store
compile_commands.json

5
.lldbinit Normal file
View File

@ -0,0 +1,5 @@
settings set target.default-arch riscv64
platform select remote-gdb-server
process connect connect://127.0.0.1:26000
target create kernel/kernel
settings set stop-disassembly-display always

View File

@ -1,6 +1,6 @@
The xv6 software is:
Copyright (c) 2006-2019 Frans Kaashoek, Robert Morris, Russ Cox,
Copyright (c) 2006-2024 Frans Kaashoek, Robert Morris, Russ Cox,
Massachusetts Institute of Technology
Permission is hereby granted, free of charge, to any person obtaining

View File

@ -44,7 +44,7 @@ OBJS_KCSAN += \
$K/kcsan.o
endif
ifeq ($(LAB),$(filter $(LAB), lock))
ifeq ($(LAB),lock)
OBJS += \
$K/stats.o\
$K/sprintf.o
@ -55,7 +55,6 @@ ifeq ($(LAB),net)
OBJS += \
$K/e1000.o \
$K/net.o \
$K/sysnet.o \
$K/pci.o
endif
@ -86,7 +85,7 @@ LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -Wall -Werror -Ofast -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)
@ -96,7 +95,14 @@ endif
CFLAGS += $(XCFLAGS)
CFLAGS += -MD
CFLAGS += -mcmodel=medany
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
# 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 += -I.
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
@ -141,7 +147,7 @@ tags: $(OBJS) _init
ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
ifeq ($(LAB),$(filter $(LAB), lock))
ifeq ($(LAB),lock)
ULIB += $U/statistics.o
endif
@ -188,15 +194,18 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_sleep\
$U/_pingpong\
$U/_primes\
$U/_find\
$U/_xargs\
ifeq ($(LAB),$(filter $(LAB), lock))
ifeq ($(LAB),syscall)
UPROGS += \
$U/_attack\
$U/_attacktest\
$U/_secret
endif
ifeq ($(LAB),lock)
UPROGS += \
$U/_stats
endif
@ -204,7 +213,8 @@ endif
ifeq ($(LAB),traps)
UPROGS += \
$U/_call\
$U/_bttest
$U/_bttest\
$U/_alarmtest
endif
ifeq ($(LAB),lazy)
@ -229,10 +239,10 @@ $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 -Ofast $(XCFLAGS) notxv6/ph.c -pthread
gcc -o ph -g -O2 $(XCFLAGS) notxv6/ph.c -pthread
barrier: notxv6/barrier.c
gcc -o barrier -g -Ofast $(XCFLAGS) notxv6/barrier.c -pthread
gcc -o barrier -g -O2 $(XCFLAGS) notxv6/barrier.c -pthread
endif
ifeq ($(LAB),pgtbl)
@ -255,7 +265,7 @@ endif
ifeq ($(LAB),net)
UPROGS += \
$U/_nettests
$U/_nettest
endif
UEXTRA=
@ -269,14 +279,12 @@ fs.img: mkfs/mkfs README $(UEXTRA) $(UPROGS)
-include kernel/*.d user/*.d
clean:
rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
clean:
rm -rf *.tex *.dvi *.idx *.aux *.log *.ind *.ilg *.dSYM *.zip *.pcap \
*/*.o */*.d */*.asm */*.sym \
$U/initcode $U/initcode.out $K/kernel fs.img \
mkfs/mkfs .gdbinit \
$U/usys.S \
$(UPROGS) \
*.zip \
$U/initcode $U/initcode.out $U/usys.S $U/_* \
$K/kernel \
mkfs/mkfs fs.img .gdbinit __pycache__ xv6.out* \
ph barrier
# try to generate a unique GDB port
@ -292,7 +300,8 @@ ifeq ($(LAB),fs)
CPUS := 1
endif
FWDPORT = $(shell expr `id -u` % 5000 + 25999)
FWDPORT1 = $(shell expr `id -u` % 5000 + 25999)
FWDPORT2 = $(shell expr `id -u` % 5000 + 30999)
QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic
QEMUOPTS += -global virtio-mmio.force-legacy=false
@ -300,7 +309,7 @@ QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0
QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
ifeq ($(LAB),net)
QEMUOPTS += -netdev user,id=net0,hostfwd=udp::$(FWDPORT)-:2000 -object filter-dump,id=net0,netdev=net0,file=packets.pcap
QEMUOPTS += -netdev user,id=net0,hostfwd=udp::$(FWDPORT1)-:2000,hostfwd=udp::$(FWDPORT2)-:2001 -object filter-dump,id=net0,netdev=net0,file=packets.pcap
QEMUOPTS += -device e1000,netdev=net0,bus=pcie.0
endif
@ -318,11 +327,6 @@ 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
##
@ -340,9 +344,7 @@ 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)
python grade-lab-$(LAB)
python3.12 ./grade-lab-$(LAB) $(GRADEFLAGS)
##
## FOR submissions

35
README
View File

@ -14,25 +14,22 @@ 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, Silas Boyd-Wickizer, Anton Burtsev, carlclone, Ian
Chen, Dan Cross, Cody Cutler, Mike CAT, Tej Chajed, Asami Doi,
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, Harry Porter, Greg Price, Jude
Rich, segfault, Ayan Shafqat, Eldar Sehayek, Yongming Shen, Fumiya
Shigemitsu, Cam Tenny, tyfkda, Warren Toomey, Stephen Tu, Rafael Ubal,
Amane Uehara, Pablo Ventura, Xi Wang, WaheedHafez, Keiichi Watanabe,
Nicolas Wolovick, wxdao, Grant Wu, Jindong Zhang, Icenowy Zheng,
ZhUyU1997, and Zou Chang Wei.
The code in the files that constitute xv6 is
Copyright 2006-2022 Frans Kaashoek, Robert Morris, and Russ Cox.
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

0
README.md Normal file
View File

91
alarmtest_tests.md Normal file
View File

@ -0,0 +1,91 @@
# alarmtest.c 测试
## test0: 单次调用测试
**函数**: `test0()`
**目的**: 测试内核是否至少调用了一次 alarm 处理程序。
**描述**:
- 设置 alarm 每 2 个时钟周期触发一次。
- 在一个长循环中检查 `count` 是否被处理程序修改。
- 如果 `count > 0`,测试通过;否则失败。
---
## test1: 多次调用与寄存器恢复测试
**函数**: `test1()`
**目的**: 测试内核是否多次调用 alarm 处理程序,并确保处理程序返回后程序继续从中断点执行,且寄存器值未被破坏。
**描述**:
- 设置 alarm 每 2 个时钟周期触发一次。
- 在循环中调用 `foo()` 函数,同时检查 `count` 是否达到 10。
- 验证 `foo()` 的调用次数是否与循环计数一致。
- 如果 `count < 10` 或寄存器值不一致,测试失败;否则通过。
---
## test2: 防止重入调用
**函数**: `test2()`
**目的**: 测试内核是否防止 alarm 处理程序的重入调用。
**描述**:
- 设置 alarm 每 2 个时钟周期触发一次,处理程序为 `slow_handler()`
- 在子进程中运行一个长循环,检查 `count` 是否被修改。
- 如果 `count > 1`,测试失败;否则通过。
---
## test3: 寄存器 a0 的完整性测试
**函数**: `test3()`
**目的**: 测试从 `sys_sigreturn()` 返回时,寄存器 `a0` 的值是否被修改。
**描述**:
- 设置 alarm 每 1 个时钟周期触发一次,处理程序为 `dummy_handler()`
- 在触发 alarm 后检查寄存器 `a0` 的值是否保持为初始值 `0xac`
- 如果 `a0` 被修改,测试失败;否则通过。
---
## 处理程序说明
### `periodic()`
- 增加 `count` 值并打印 "alarm!"。
- 调用 `sigreturn()` 返回。
### `slow_handler()`
- 增加 `count` 值并打印 "alarm!"。
- 如果 `count > 1`,测试失败并退出。
- 运行一个长循环以模拟慢速处理程序。
- 调用 `sigreturn()` 返回。
### `dummy_handler()`
- 立即卸载自身并调用 `sigreturn()` 返回。
---

67
answers-traps.txt Normal file
View File

@ -0,0 +1,67 @@
问题 1: 哪些寄存器包含函数的参数?例如,在 main 对 printf 的调用中,哪个寄存器包含 13
在 RISC-V 调用约定中,函数参数通常通过寄存器 a0 到 a7 传递。
对于函数 g(int x) 和 f(int x) ,它们的参数 x 都通过寄存器 a0 传递。
在 main 对 printf 的调用中, printf("y=%d\n", 13) 语句中,数字 13 将包含在寄存器 a1 中。 printf 的第一个参数(格式字符串的地址)将包含在寄存器 a0 中。
问题 2: 在 main 的汇编代码中,对函数 f 的调用在哪里?对 g 的调用在哪里?(提示:编译器可以内联函数。)
由于编译器优化, f 和 g 函数在 main 函数中被内联了。这意味着它们的实际代码直接插入到 main 函数的调用点,而不是通过 jal 或 jalr 指令进行函数调用。
我们可以通过观察 main 函数的汇编代码来验证这一点。在 main 函数中,你会看到对 f 和 g 的操作直接在 main 的代码流中执行,而没有显式的 jal 或 jalr 到 <f> 或 <g> 的地址。
问题 3: 函数 printf 位于哪个地址?
printf 函数不在 call.asm 文件提供的汇编代码中。 call.asm 似乎只包含用户定义的函数和一些与内存分配相关的代码。 printf 是一个标准库函数,它的地址会在程序链接时确定,并且通常在运行时动态加载。因此,我们无法从 call.asm 的反汇编代码中直接找到 printf 的地址。
问题 4: 在 main 中 jalr 到 printf 之后的寄存器 ra 中有什么值?
在 main 函数中 jalr 到 printf 之后,寄存器 ra (返回地址寄存器) 将包含紧跟在 jalr 指令之后的指令的地址。这个地址是 printf 函数执行完毕后应该返回到的 main 函数中的位置。
问题 5: 运行以下代码。无符号 int i = 0x00646c72; printf(“H%x Wo%s” 57616 (字符 &i; 输出是什么?
i = 0x00646c72
printf("H%x Wo%s", 57616, (char ) &i)
输出: H0e110 Wo rld
解释:
"H" 和 "Wo" 是直接打印的字符串。
%x 对应 57616 。 57616 的十六进制是 0xE110 。
%s 对应 (char ) &i 。由于 RISC-V 是 little-endian内存中 i 的字节顺序是 72 6C 64 00 。
0x72 是字符 'r'
0x6C 是字符 'l'
0x64 是字符 'd'
0x00 是字符串终止符
因此, %s 会打印 "rld"。
问题 6: 如果 RISC-V 是 big-endian您会将 i 设置为什么 i 才能产生相同的输出?是否需要将 57616 更改为其他值?
如果 RISC-V 是 big-endian
为了使 %s 打印 "rld",我们需要将 i 设置为 0x726c6400 。在 big-endian 系统中,高位字节存储在低内存地址,所以 0x72 会在最低地址,然后是 0x6c 0x64 ,最后是 0x00 。
是否需要将 57616 更改为其他值?
不需要。 57616 是一个整数值,无论系统是 little-endian 还是 big-endian它的数值表示是不变的。 %x 格式说明符只是将其打印为十六进制。字节序只影响多字节数据类型(如 int )在内存中的存储方式,而不影响其数值。
问题 7: 在下面的代码中, 'y=' 后面会打印什么?(注意:答案不是具体值。为什么会这样?
c
printf("x=%d y=%d", 3);
输出: y= 后面会打印一个不确定的值(垃圾值)。
为什么会这样:
printf 是一个变参函数,它根据格式字符串中的格式说明符来确定需要从栈中取出多少个参数。
格式字符串是 "x=%d y=%d" 。
这意味着 printf 期望有两个 %d 对应的整数参数。
但是,在实际调用中,只提供了一个参数 3 。
因此:
1. printf 会从栈中读取第一个整数参数来填充 x=%d ,这个值就是 3 。
2. 然后, printf 会尝试读取第二个整数参数来填充 y=%d 。由于调用时只提供了 3 这一个参数, printf 会从栈上读取一个任意的、未初始化的内存位置的值,这个值是不可预测的(垃圾值)。

View File

@ -1,702 +0,0 @@
[
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-c",
"-o",
"kernel/entry.o",
"kernel/entry.S"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/entry.S",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/entry.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/kalloc.o",
"kernel/kalloc.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/kalloc.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/kalloc.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/string.o",
"kernel/string.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/string.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/string.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/main.o",
"kernel/main.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/main.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/main.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/vm.o",
"kernel/vm.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/vm.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/vm.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/proc.o",
"kernel/proc.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/proc.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/proc.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-c",
"-o",
"kernel/swtch.o",
"kernel/swtch.S"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/swtch.S",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/swtch.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-c",
"-o",
"kernel/trampoline.o",
"kernel/trampoline.S"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/trampoline.S",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/trampoline.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/trap.o",
"kernel/trap.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/trap.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/trap.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/syscall.o",
"kernel/syscall.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/syscall.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/syscall.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/sysproc.o",
"kernel/sysproc.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/sysproc.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/sysproc.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/bio.o",
"kernel/bio.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/bio.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/bio.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/fs.o",
"kernel/fs.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/fs.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/fs.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/log.o",
"kernel/log.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/log.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/log.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/sleeplock.o",
"kernel/sleeplock.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/sleeplock.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/sleeplock.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/file.o",
"kernel/file.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/file.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/file.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/pipe.o",
"kernel/pipe.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/pipe.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/pipe.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/exec.o",
"kernel/exec.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/exec.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/exec.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/sysfile.o",
"kernel/sysfile.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/sysfile.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/sysfile.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-c",
"-o",
"kernel/kernelvec.o",
"kernel/kernelvec.S"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/kernelvec.S",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/kernelvec.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/plic.o",
"kernel/plic.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/plic.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/plic.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/virtio_disk.o",
"kernel/virtio_disk.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/virtio_disk.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/virtio_disk.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/start.o",
"kernel/start.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/start.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/start.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/console.o",
"kernel/console.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/console.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/console.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/printf.o",
"kernel/printf.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/printf.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/printf.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/uart.o",
"kernel/uart.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/uart.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/uart.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-c",
"-o",
"kernel/spinlock.o",
"kernel/spinlock.c"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/spinlock.c",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/kernel/spinlock.o"
},
{
"arguments": [
"/usr/bin/riscv64-unknown-elf-gcc",
"-Wall",
"-Werror",
"-O2",
"-fno-omit-frame-pointer",
"-ggdb",
"-gdwarf-2",
"-DSOL_UTIL",
"-DLAB_UTIL",
"-mcmodel=medany",
"-ffreestanding",
"-fno-common",
"-mno-relax",
"-I.",
"-fno-stack-protector",
"-fno-pie",
"-march=rv64g",
"-nostdinc",
"-I.",
"-Ikernel",
"-c",
"-o",
"user/initcode.o",
"user/initcode.S"
],
"directory": "/run/media/gh0s7/Data/project/csail/xv6-labs",
"file": "/run/media/gh0s7/Data/project/csail/xv6-labs/user/initcode.S",
"output": "/run/media/gh0s7/Data/project/csail/xv6-labs/user/initcode.o"
}
]

View File

@ -1 +1 @@
LAB=util
LAB=traps

74
grade-lab-traps Normal file
View File

@ -0,0 +1,74 @@
#!/usr/bin/env python3
import os
import re
import subprocess
from gradelib import *
r = Runner(save("xv6.out"))
@test(5, "answers-traps.txt")
def test_answers():
# just a simple sanity check, will be graded manually
check_answers("answers-traps.txt")
BACKTRACE_RE = r"^(0x000000008[0-9a-f]+)"
def addr2line():
for f in ['riscv64-unknown-elf-addr2line', 'riscv64-linux-gnu-addr2line', 'addr2line', ]:
try:
devnull = open(os.devnull)
subprocess.Popen([f], stdout=devnull, stderr=devnull).communicate()
return f
except OSError:
continue
raise AssertionError('Cannot find the addr2line program')
@test(10, "backtrace test")
def test_backtracetest():
r.run_qemu(shell_script([
'bttest'
]))
a2l = addr2line()
matches = re.findall(BACKTRACE_RE, r.qemu.output, re.MULTILINE)
assert_equal(len(matches), 3)
files = ['sysproc.c', 'syscall.c', 'trap.c']
for f, m in zip(files, matches):
result = subprocess.run([a2l, '-e', 'kernel/kernel', m], stdout=subprocess.PIPE)
if not f in result.stdout.decode("utf-8"):
raise AssertionError('Trace is incorrect; no %s' % f)
@test(0, "running alarmtest")
def test_alarmtest():
r.run_qemu(shell_script([
'alarmtest'
]))
@test(20, "alarmtest: test0", parent=test_alarmtest)
def test_alarmtest_test0():
r.match('^test0 passed$')
@test(20, "alarmtest: test1", parent=test_alarmtest)
def test_alarmtest_test1():
r.match('^\\.?test1 passed$')
@test(10, "alarmtest: test2", parent=test_alarmtest)
def test_alarmtest_test2():
r.match('^\\.?test2 passed$')
@test(10, "alarmtest: test3", parent=test_alarmtest)
def test_alarmtest_test3():
r.match('^test3 passed$')
@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()

View File

@ -1,86 +0,0 @@
#!/usr/bin/env python3
import re
from gradelib import *
r = Runner(save("xv6.out"))
@test(5, "sleep, no arguments")
def test_sleep_no_args():
r.run_qemu(shell_script([
'sleep'
]))
r.match(no=["exec .* failed", "$ sleep\n$"])
@test(5, "sleep, returns")
def test_sleep_no_args():
r.run_qemu(shell_script([
'sleep',
'echo OK'
]))
r.match('^OK$', no=["exec .* failed", "$ sleep\n$"])
@test(10, "sleep, makes syscall")
def test_sleep():
r.run_qemu(shell_script([
'sleep 10',
'echo FAIL'
]), stop_breakpoint('sys_sleep'))
r.match('\\$ sleep 10', no=['FAIL'])
@test(20, "pingpong")
def test_pingpong():
r.run_qemu(shell_script([
'pingpong', 'echo OK'
]))
r.match('^\\d+: received ping$', '^\\d+: received pong$', '^OK$')
@test(20, "primes")
def test_primes():
r.run_qemu(shell_script([
'primes', 'echo OK'
]))
args = ['prime %d' % i for i in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]]
args.append('^OK$')
r.match(*args)
@test(10, "find, in current directory")
def test_find_curdir():
fn = random_str()
r.run_qemu(shell_script([
'echo > %s' % fn,
'find . %s' % fn
]))
r.match('./%s' % fn)
@test(10, "find, recursive")
def test_find_recursive():
needle = random_str()
dirs = [random_str() for _ in range(3)]
r.run_qemu(shell_script([
'mkdir %s' % dirs[0],
'echo > %s/%s' % (dirs[0], needle),
'mkdir %s/%s' % (dirs[0], dirs[1]),
'echo > %s/%s/%s' % (dirs[0], dirs[1], needle),
'mkdir %s' % dirs[2],
'echo > %s/%s' % (dirs[2], needle),
'find . %s' % needle
]))
r.match('./%s/%s' % (dirs[0], needle),
'./%s/%s/%s' % (dirs[0], dirs[1], needle),
'./%s/%s' % (dirs[2], needle))
@test(19, "xargs")
def test_xargs():
r.run_qemu(shell_script([
'sh < xargstest.sh',
'echo DONE',
], 'DONE'))
matches = re.findall("hello", r.qemu.output)
assert_equal(len(matches), 3, "Number of appearances of 'hello'")
@test(1, "time")
def test_time():
check_time()
run_tests()

View File

@ -10,180 +10,181 @@ struct stat;
struct superblock;
// bio.c
void binit(void);
struct buf *bread(uint, uint);
void brelse(struct buf *);
void bwrite(struct buf *);
void bpin(struct buf *);
void bunpin(struct buf *);
void binit(void);
struct buf* bread(uint, uint);
void brelse(struct buf*);
void bwrite(struct buf*);
void bpin(struct buf*);
void bunpin(struct buf*);
// console.c
void consoleinit(void);
void consoleintr(int);
void consputc(int);
void consoleinit(void);
void consoleintr(int);
void consputc(int);
// exec.c
int exec(char *, char **);
int exec(char*, char**);
// file.c
struct file *filealloc(void);
void fileclose(struct file *);
struct file *filedup(struct file *);
void fileinit(void);
int fileread(struct file *, uint64, int n);
int filestat(struct file *, uint64 addr);
int filewrite(struct file *, uint64, int n);
struct file* filealloc(void);
void fileclose(struct file*);
struct file* filedup(struct file*);
void fileinit(void);
int fileread(struct file*, uint64, int n);
int filestat(struct file*, uint64 addr);
int filewrite(struct file*, uint64, int n);
// fs.c
void fsinit(int);
int dirlink(struct inode *, char *, uint);
struct inode *dirlookup(struct inode *, char *, uint *);
struct inode *ialloc(uint, short);
struct inode *idup(struct inode *);
void iinit();
void ilock(struct inode *);
void iput(struct inode *);
void iunlock(struct inode *);
void iunlockput(struct inode *);
void iupdate(struct inode *);
int namecmp(const char *, const char *);
struct inode *namei(char *);
struct inode *nameiparent(char *, char *);
int readi(struct inode *, int, uint64, uint, uint);
void stati(struct inode *, struct stat *);
int writei(struct inode *, int, uint64, uint, uint);
void itrunc(struct inode *);
void fsinit(int);
int dirlink(struct inode*, char*, uint);
struct inode* dirlookup(struct inode*, char*, uint*);
struct inode* ialloc(uint, short);
struct inode* idup(struct inode*);
void iinit();
void ilock(struct inode*);
void iput(struct inode*);
void iunlock(struct inode*);
void iunlockput(struct inode*);
void iupdate(struct inode*);
int namecmp(const char*, const char*);
struct inode* namei(char*);
struct inode* nameiparent(char*, char*);
int readi(struct inode*, int, uint64, uint, uint);
void stati(struct inode*, struct stat*);
int writei(struct inode*, int, uint64, uint, uint);
void itrunc(struct inode*);
// ramdisk.c
void ramdiskinit(void);
void ramdiskintr(void);
void ramdiskrw(struct buf *);
void ramdiskinit(void);
void ramdiskintr(void);
void ramdiskrw(struct buf*);
// kalloc.c
void *kalloc(void);
void kfree(void *);
void kinit(void);
void* kalloc(void);
void kfree(void *);
void kinit(void);
// log.c
void initlog(int, struct superblock *);
void log_write(struct buf *);
void begin_op(void);
void end_op(void);
void initlog(int, struct superblock*);
void log_write(struct buf*);
void begin_op(void);
void end_op(void);
// pipe.c
int pipealloc(struct file **, struct file **);
void pipeclose(struct pipe *, int);
int piperead(struct pipe *, uint64, int);
int pipewrite(struct pipe *, uint64, int);
int pipealloc(struct file**, struct file**);
void pipeclose(struct pipe*, int);
int piperead(struct pipe*, uint64, int);
int pipewrite(struct pipe*, uint64, int);
// printf.c
void printf(char *, ...);
void panic(char *) __attribute__((noreturn));
void printfinit(void);
int printf(char*, ...) __attribute__ ((format (printf, 1, 2)));
void panic(char*) __attribute__((noreturn));
void printfinit(void);
void backtrace(void);
// proc.c
int cpuid(void);
void exit(int);
int fork(void);
int growproc(int);
void proc_mapstacks(pagetable_t);
pagetable_t proc_pagetable(struct proc *);
void proc_freepagetable(pagetable_t, uint64);
int kill(int);
int killed(struct proc *);
void setkilled(struct proc *);
struct cpu *mycpu(void);
struct cpu *getmycpu(void);
struct proc *myproc();
void procinit(void);
void scheduler(void) __attribute__((noreturn));
void sched(void);
void sleep(void *, struct spinlock *);
void userinit(void);
int wait(uint64);
void wakeup(void *);
void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);
int cpuid(void);
void exit(int);
int fork(void);
int growproc(int);
void proc_mapstacks(pagetable_t);
pagetable_t proc_pagetable(struct proc *);
void proc_freepagetable(pagetable_t, uint64);
int kill(int);
int killed(struct proc*);
void setkilled(struct proc*);
struct cpu* mycpu(void);
struct cpu* getmycpu(void);
struct proc* myproc();
void procinit(void);
void scheduler(void) __attribute__((noreturn));
void sched(void);
void sleep(void*, struct spinlock*);
void userinit(void);
int wait(uint64);
void wakeup(void*);
void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);
// swtch.S
void swtch(struct context *, struct context *);
void swtch(struct context*, struct context*);
// spinlock.c
void acquire(struct spinlock *);
int holding(struct spinlock *);
void initlock(struct spinlock *, char *);
void release(struct spinlock *);
void push_off(void);
void pop_off(void);
void acquire(struct spinlock*);
int holding(struct spinlock*);
void initlock(struct spinlock*, char*);
void release(struct spinlock*);
void push_off(void);
void pop_off(void);
// sleeplock.c
void acquiresleep(struct sleeplock *);
void releasesleep(struct sleeplock *);
int holdingsleep(struct sleeplock *);
void initsleeplock(struct sleeplock *, char *);
void acquiresleep(struct sleeplock*);
void releasesleep(struct sleeplock*);
int holdingsleep(struct sleeplock*);
void initsleeplock(struct sleeplock*, char*);
// string.c
int memcmp(const void *, const void *, uint);
void *memmove(void *, const void *, uint);
void *memset(void *, int, uint);
char *safestrcpy(char *, const char *, int);
int strlen(const char *);
int strncmp(const char *, const char *, uint);
char *strncpy(char *, const char *, int);
int memcmp(const void*, const void*, uint);
void* memmove(void*, const void*, uint);
void* memset(void*, int, uint);
char* safestrcpy(char*, const char*, int);
int strlen(const char*);
int strncmp(const char*, const char*, uint);
char* strncpy(char*, const char*, int);
// syscall.c
void argint(int, int *);
int argstr(int, char *, int);
void argaddr(int, uint64 *);
int fetchstr(uint64, char *, int);
int fetchaddr(uint64, uint64 *);
void syscall();
void argint(int, int*);
int argstr(int, char*, int);
void argaddr(int, uint64 *);
int fetchstr(uint64, char*, int);
int fetchaddr(uint64, uint64*);
void syscall();
// trap.c
extern uint ticks;
void trapinit(void);
void trapinithart(void);
extern uint ticks;
void trapinit(void);
void trapinithart(void);
extern struct spinlock tickslock;
void usertrapret(void);
void usertrapret(void);
// uart.c
void uartinit(void);
void uartintr(void);
void uartputc(int);
void uartputc_sync(int);
int uartgetc(void);
void uartinit(void);
void uartintr(void);
void uartputc(int);
void uartputc_sync(int);
int uartgetc(void);
// vm.c
void kvminit(void);
void kvminithart(void);
void kvmmap(pagetable_t, uint64, uint64, uint64, int);
int mappages(pagetable_t, uint64, uint64, uint64, int);
pagetable_t uvmcreate(void);
void uvmfirst(pagetable_t, uchar *, uint);
uint64 uvmalloc(pagetable_t, uint64, uint64, int);
uint64 uvmdealloc(pagetable_t, uint64, uint64);
int uvmcopy(pagetable_t, pagetable_t, uint64);
void uvmfree(pagetable_t, uint64);
void uvmunmap(pagetable_t, uint64, uint64, int);
void uvmclear(pagetable_t, uint64);
pte_t *walk(pagetable_t, uint64, int);
uint64 walkaddr(pagetable_t, uint64);
int copyout(pagetable_t, uint64, char *, uint64);
int copyin(pagetable_t, char *, uint64, uint64);
int copyinstr(pagetable_t, char *, uint64, uint64);
void kvminit(void);
void kvminithart(void);
void kvmmap(pagetable_t, uint64, uint64, uint64, int);
int mappages(pagetable_t, uint64, uint64, uint64, int);
pagetable_t uvmcreate(void);
void uvmfirst(pagetable_t, uchar *, uint);
uint64 uvmalloc(pagetable_t, uint64, uint64, int);
uint64 uvmdealloc(pagetable_t, uint64, uint64);
int uvmcopy(pagetable_t, pagetable_t, uint64);
void uvmfree(pagetable_t, uint64);
void uvmunmap(pagetable_t, uint64, uint64, int);
void uvmclear(pagetable_t, uint64);
pte_t * walk(pagetable_t, uint64, int);
uint64 walkaddr(pagetable_t, uint64);
int copyout(pagetable_t, uint64, char *, uint64);
int copyin(pagetable_t, char *, uint64, uint64);
int copyinstr(pagetable_t, char *, uint64, uint64);
// plic.c
void plicinit(void);
void plicinithart(void);
int plic_claim(void);
void plic_complete(int);
void plicinit(void);
void plicinithart(void);
int plic_claim(void);
void plic_complete(int);
// virtio_disk.c
void virtio_disk_init(void);
void virtio_disk_rw(struct buf *, int);
void virtio_disk_intr(void);
void virtio_disk_init(void);
void virtio_disk_rw(struct buf *, int);
void virtio_disk_intr(void);
// number of elements in fixed-size array
#define NELEM(x) (sizeof(x) / sizeof((x)[0]))
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))

View File

@ -75,17 +75,17 @@ exec(char *path, char **argv)
p = myproc();
uint64 oldsz = p->sz;
// Allocate two pages at the next page boundary.
// Allocate some pages at the next page boundary.
// Make the first inaccessible as a stack guard.
// Use the second as the user stack.
// Use the rest as the user stack.
sz = PGROUNDUP(sz);
uint64 sz1;
if((sz1 = uvmalloc(pagetable, sz, sz + 2*PGSIZE, PTE_W)) == 0)
if((sz1 = uvmalloc(pagetable, sz, sz + (USERSTACK+1)*PGSIZE, PTE_W)) == 0)
goto bad;
sz = sz1;
uvmclear(pagetable, sz-2*PGSIZE);
uvmclear(pagetable, sz-(USERSTACK+1)*PGSIZE);
sp = sz;
stackbase = sp - PGSIZE;
stackbase = sp - USERSTACK*PGSIZE;
// Push argument strings, prepare rest of stack in ustack.
for(argc = 0; argv[argc]; argc++) {

View File

@ -1,9 +1,9 @@
// On-disk file system format.
// Both the kernel and user programs use this header file.
#include "kernel/types.h"
#define ROOTINO 1 // root i-number
#define BSIZE 1024 // block size
#define ROOTINO 1 // root i-number
#define BSIZE 1024 // block size
// Disk layout:
// [ boot block | super block | log | inode blocks |
@ -12,14 +12,14 @@
// mkfs computes the super block and builds an initial file system. The
// super block describes the disk layout:
struct superblock {
uint magic; // Must be FSMAGIC
uint size; // Size of file system image (blocks)
uint nblocks; // Number of data blocks
uint ninodes; // Number of inodes.
uint nlog; // Number of log blocks
uint logstart; // Block number of first log block
uint inodestart; // Block number of first inode block
uint bmapstart; // Block number of first free map block
uint magic; // Must be FSMAGIC
uint size; // Size of file system image (blocks)
uint nblocks; // Number of data blocks
uint ninodes; // Number of inodes.
uint nlog; // Number of log blocks
uint logstart; // Block number of first log block
uint inodestart; // Block number of first inode block
uint bmapstart; // Block number of first free map block
};
#define FSMAGIC 0x10203040
@ -30,25 +30,25 @@ struct superblock {
// On-disk inode structure
struct dinode {
short type; // File type
short major; // Major device number (T_DEVICE only)
short minor; // Minor device number (T_DEVICE only)
short nlink; // Number of links to inode in file system
uint size; // Size of file (bytes)
uint addrs[NDIRECT + 1]; // Data block addresses
short type; // File type
short major; // Major device number (T_DEVICE only)
short minor; // Minor device number (T_DEVICE only)
short nlink; // Number of links to inode in file system
uint size; // Size of file (bytes)
uint addrs[NDIRECT+1]; // Data block addresses
};
// Inodes per block.
#define IPB (BSIZE / sizeof(struct dinode))
#define IPB (BSIZE / sizeof(struct dinode))
// Block containing inode i
#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart)
#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart)
// Bitmap bits per block
#define BPB (BSIZE * 8)
#define BPB (BSIZE*8)
// Block of free map containing bit for block b
#define BBLOCK(b, sb) ((b) / BPB + sb.bmapstart)
#define BBLOCK(b, sb) ((b)/BPB + sb.bmapstart)
// Directory is a file containing a sequence of dirent structures.
#define DIRSIZ 14
@ -57,3 +57,4 @@ struct dirent {
ushort inum;
char name[DIRSIZ];
};

View File

@ -3,7 +3,7 @@
# mode come here.
#
# the current stack is a kernel stack.
# push all registers, call kerneltrap().
# push registers, call kerneltrap().
# when kerneltrap() returns, restore registers, return.
#
.globl kerneltrap
@ -13,7 +13,7 @@ kernelvec:
# make room to save registers.
addi sp, sp, -256
# save the registers.
# save caller-saved registers.
sd ra, 0(sp)
sd sp, 8(sp)
sd gp, 16(sp)
@ -21,8 +21,6 @@ kernelvec:
sd t0, 32(sp)
sd t1, 40(sp)
sd t2, 48(sp)
sd s0, 56(sp)
sd s1, 64(sp)
sd a0, 72(sp)
sd a1, 80(sp)
sd a2, 88(sp)
@ -31,16 +29,6 @@ kernelvec:
sd a5, 112(sp)
sd a6, 120(sp)
sd a7, 128(sp)
sd s2, 136(sp)
sd s3, 144(sp)
sd s4, 152(sp)
sd s5, 160(sp)
sd s6, 168(sp)
sd s7, 176(sp)
sd s8, 184(sp)
sd s9, 192(sp)
sd s10, 200(sp)
sd s11, 208(sp)
sd t3, 216(sp)
sd t4, 224(sp)
sd t5, 232(sp)
@ -57,8 +45,6 @@ kernelvec:
ld t0, 32(sp)
ld t1, 40(sp)
ld t2, 48(sp)
ld s0, 56(sp)
ld s1, 64(sp)
ld a0, 72(sp)
ld a1, 80(sp)
ld a2, 88(sp)
@ -67,16 +53,6 @@ kernelvec:
ld a5, 112(sp)
ld a6, 120(sp)
ld a7, 128(sp)
ld s2, 136(sp)
ld s3, 144(sp)
ld s4, 152(sp)
ld s5, 160(sp)
ld s6, 168(sp)
ld s7, 176(sp)
ld s8, 184(sp)
ld s9, 192(sp)
ld s10, 200(sp)
ld s11, 208(sp)
ld t3, 216(sp)
ld t4, 224(sp)
ld t5, 232(sp)
@ -86,39 +62,3 @@ kernelvec:
# return to whatever we were doing in the kernel.
sret
#
# machine-mode timer interrupt.
#
.globl timervec
.align 4
timervec:
# start.c has set up the memory that mscratch points to:
# scratch[0,8,16] : register save area.
# scratch[24] : address of CLINT's MTIMECMP register.
# scratch[32] : desired interval between interrupts.
csrrw a0, mscratch, a0
sd a1, 0(a0)
sd a2, 8(a0)
sd a3, 16(a0)
# schedule the next timer interrupt
# by adding interval to mtimecmp.
ld a1, 24(a0) # CLINT_MTIMECMP(hart)
ld a2, 32(a0) # interval
ld a3, 0(a1)
add a3, a3, a2
sd a3, 0(a1)
# arrange for a supervisor software interrupt
# after this handler returns.
li a1, 2
csrw sip, a1
ld a3, 16(a0)
ld a2, 8(a0)
ld a1, 0(a0)
csrrw a0, mscratch, a0
mret

View File

@ -25,11 +25,6 @@
#define VIRTIO0 0x10001000
#define VIRTIO0_IRQ 1
// core local interruptor (CLINT), which contains the timer.
#define CLINT 0x2000000L
#define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8*(hartid))
#define CLINT_MTIME (CLINT + 0xBFF8) // cycles since boot.
// qemu puts platform-level interrupt controller (PLIC) here.
#define PLIC 0x0c000000L
#define PLIC_PRIORITY (PLIC + 0x0)

View File

@ -11,3 +11,5 @@
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache
#define FSSIZE 2000 // size of file system in blocks
#define MAXPATH 128 // maximum file path name
#define USERSTACK 1 // user stack pages

View File

@ -26,13 +26,13 @@ static struct {
static char digits[] = "0123456789abcdef";
static void
printint(int xx, int base, int sign)
printint(long long xx, int base, int sign)
{
char buf[16];
int i;
uint x;
unsigned long long x;
if(sign && (sign = xx < 0))
if(sign && (sign = (xx < 0)))
x = -xx;
else
x = xx;
@ -59,30 +59,71 @@ printptr(uint64 x)
consputc(digits[x >> (sizeof(uint64) * 8 - 4)]);
}
// Print to the console. only understands %d, %x, %p, %s.
void
// Print to the console.
int
printf(char *fmt, ...)
{
va_list ap;
int i, c, locking;
int i, cx, c0, c1, c2, locking;
char *s;
locking = pr.locking;
if(locking)
acquire(&pr.lock);
if (fmt == 0)
panic("null fmt");
va_start(ap, fmt);
for(i = 0; (c = fmt[i] & 0xff) != 0; i++){
if(c != '%'){
consputc(c);
for(i = 0; (cx = fmt[i] & 0xff) != 0; i++){
if(cx != '%'){
consputc(cx);
continue;
}
c = fmt[++i] & 0xff;
if(c == 0)
i++;
c0 = fmt[i+0] & 0xff;
c1 = c2 = 0;
if(c0) c1 = fmt[i+1] & 0xff;
if(c1) c2 = fmt[i+2] & 0xff;
if(c0 == 'd'){
printint(va_arg(ap, int), 10, 1);
} else if(c0 == 'l' && c1 == 'd'){
printint(va_arg(ap, uint64), 10, 1);
i += 1;
} else if(c0 == 'l' && c1 == 'l' && c2 == 'd'){
printint(va_arg(ap, uint64), 10, 1);
i += 2;
} else if(c0 == 'u'){
printint(va_arg(ap, int), 10, 0);
} else if(c0 == 'l' && c1 == 'u'){
printint(va_arg(ap, uint64), 10, 0);
i += 1;
} else if(c0 == 'l' && c1 == 'l' && c2 == 'u'){
printint(va_arg(ap, uint64), 10, 0);
i += 2;
} else if(c0 == 'x'){
printint(va_arg(ap, int), 16, 0);
} else if(c0 == 'l' && c1 == 'x'){
printint(va_arg(ap, uint64), 16, 0);
i += 1;
} else if(c0 == 'l' && c1 == 'l' && c2 == 'x'){
printint(va_arg(ap, uint64), 16, 0);
i += 2;
} else if(c0 == 'p'){
printptr(va_arg(ap, uint64));
} else if(c0 == 's'){
if((s = va_arg(ap, char*)) == 0)
s = "(null)";
for(; *s; s++)
consputc(*s);
} else if(c0 == '%'){
consputc('%');
} else if(c0 == 0){
break;
} else {
// Print unknown % sequence to draw attention.
consputc('%');
consputc(c0);
}
#if 0
switch(c){
case 'd':
printint(va_arg(ap, int), 10, 1);
@ -108,11 +149,14 @@ printf(char *fmt, ...)
consputc(c);
break;
}
#endif
}
va_end(ap);
if(locking)
release(&pr.lock);
return 0;
}
void
@ -120,8 +164,7 @@ panic(char *s)
{
pr.locking = 0;
printf("panic: ");
printf(s);
printf("\n");
printf("%s\n", s);
panicked = 1; // freeze uart output from other CPUs
for(;;)
;
@ -133,3 +176,24 @@ printfinit(void)
initlock(&pr.lock, "pr");
pr.locking = 1;
}
// 打印当前调用栈的返回地址,实现简单的函数回溯,用于调试。
void
backtrace(void)
{
printf("barcktrace:\n");
uint64 ra,fp = r_fp();//frame pointer -> address
uint64 pre_fp = *((uint64*)(fp - 16));
// 只要当前fp和上一个fp在同一页内就继续回溯
while(PGROUNDDOWN(fp)==PGROUNDDOWN(pre_fp)){
ra = *(uint64 *)(fp - 8); // 取出返回地址
printf("%p\n", (void*)ra); // 打印返回地址
fp = pre_fp; // 更新fp为上一个fp
pre_fp = *((uint64*)(fp - 16)); // 获取新的上一个fp
}
ra = *(uint64 *)(fp - 8);
printf("%p\n",(void*)ra);
}

View File

@ -132,6 +132,13 @@ found:
return 0;
}
// 为进程分配 pre_trapframe 空间,分配失败则释放进程资源并返回 0
if((p->pre_trapframe = (struct trapframe *)kalloc()) == 0){
freeproc(p);
release(&p->lock);
return 0;
}
// An empty user page table.
p->pagetable = proc_pagetable(p);
if(p->pagetable == 0){
@ -160,6 +167,10 @@ freeproc(struct proc *p)
p->trapframe = 0;
if(p->pagetable)
proc_freepagetable(p->pagetable, p->sz);
// 如果进程的 pre_trapframe 存在,则释放其占用的内存,并将指针置为 0防止悬空指针。
if(p->pre_trapframe)
kfree((void*)p->pre_trapframe);
p->pre_trapframe = 0;
p->pagetable = 0;
p->sz = 0;
p->pid = 0;
@ -454,6 +465,7 @@ scheduler(void)
// processes are waiting.
intr_on();
int found = 0;
for(p = proc; p < &proc[NPROC]; p++) {
acquire(&p->lock);
if(p->state == RUNNABLE) {
@ -467,9 +479,15 @@ scheduler(void)
// Process is done running for now.
// It should have changed its p->state before coming back.
c->proc = 0;
found = 1;
}
release(&p->lock);
}
if(found == 0) {
// nothing to run; stop running on this core until an interrupt.
intr_on();
asm volatile("wfi");
}
}
}

View File

@ -104,4 +104,8 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
int alarm_cnt; // Alarm count 触发alarm的时钟中断数量
int inter_cnt; // interrupts count 需要统计的时钟中断的数量
uint64 handler; // alarm func address 记录处理alarm的函数的地址
struct trapframe *pre_trapframe;
};

View File

@ -1,45 +0,0 @@
//
// ramdisk that uses the disk image loaded by qemu -initrd fs.img
//
#include "types.h"
#include "riscv.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "fs.h"
#include "buf.h"
void
ramdiskinit(void)
{
}
// If B_DIRTY is set, write buf to disk, clear B_DIRTY, set B_VALID.
// Else if B_VALID is not set, read buf from disk, set B_VALID.
void
ramdiskrw(struct buf *b)
{
if(!holdingsleep(&b->lock))
panic("ramdiskrw: buf not locked");
if((b->flags & (B_VALID|B_DIRTY)) == B_VALID)
panic("ramdiskrw: nothing to do");
if(b->blockno >= FSSIZE)
panic("ramdiskrw: blockno too big");
uint64 diskaddr = b->blockno * BSIZE;
char *addr = (char *)RAMDISK + diskaddr;
if(b->flags & B_DIRTY){
// write
memmove(addr, b->data, BSIZE);
b->flags &= ~B_DIRTY;
} else {
// read
memmove(b->data, addr, BSIZE);
b->flags |= B_VALID;
}
}

View File

@ -96,9 +96,7 @@ w_sie(uint64 x)
}
// Machine-mode Interrupt Enable
#define MIE_MEIE (1L << 11) // external
#define MIE_MTIE (1L << 7) // timer
#define MIE_MSIE (1L << 3) // software
#define MIE_STIE (1L << 5) // supervisor timer
static inline uint64
r_mie()
{
@ -176,11 +174,38 @@ r_stvec()
return x;
}
// Machine-mode interrupt vector
static inline void
w_mtvec(uint64 x)
// Supervisor Timer Comparison Register
static inline uint64
r_stimecmp()
{
asm volatile("csrw mtvec, %0" : : "r" (x));
uint64 x;
// asm volatile("csrr %0, stimecmp" : "=r" (x) );
asm volatile("csrr %0, 0x14d" : "=r" (x) );
return x;
}
static inline void
w_stimecmp(uint64 x)
{
// asm volatile("csrw stimecmp, %0" : : "r" (x));
asm volatile("csrw 0x14d, %0" : : "r" (x));
}
// Machine Environment Configuration Register
static inline uint64
r_menvcfg()
{
uint64 x;
// asm volatile("csrr %0, menvcfg" : "=r" (x) );
asm volatile("csrr %0, 0x30a" : "=r" (x) );
return x;
}
static inline void
w_menvcfg(uint64 x)
{
// asm volatile("csrw menvcfg, %0" : : "r" (x));
asm volatile("csrw 0x30a, %0" : : "r" (x));
}
// Physical Memory Protection
@ -217,12 +242,6 @@ r_satp()
return x;
}
static inline void
w_mscratch(uint64 x)
{
asm volatile("csrw mscratch, %0" : : "r" (x));
}
// Supervisor Trap Cause
static inline uint64
r_scause()
@ -319,6 +338,15 @@ r_ra()
return x;
}
// 读取当前帧指针s0寄存器的值
static inline uint64
r_fp()
{
uint64 x;
asm volatile("mv %0, s0" : "=r" (x) );
return x;
}
// flush the TLB.
static inline void
sfence_vma()

View File

@ -10,12 +10,6 @@ void timerinit();
// entry.S needs one stack per CPU.
__attribute__ ((aligned (16))) char stack0[4096 * NCPU];
// a scratch area per CPU for machine-mode timer interrupts.
uint64 timer_scratch[NCPU][5];
// assembly code in kernelvec.S for machine-mode timer interrupt.
extern void timervec();
// entry.S jumps here in machine mode on stack0.
void
start()
@ -54,36 +48,19 @@ start()
asm volatile("mret");
}
// arrange to receive timer interrupts.
// they will arrive in machine mode at
// at timervec in kernelvec.S,
// which turns them into software interrupts for
// devintr() in trap.c.
// ask each hart to generate timer interrupts.
void
timerinit()
{
// each CPU has a separate source of timer interrupts.
int id = r_mhartid();
// ask the CLINT for a timer interrupt.
int interval = 1000000; // cycles; about 1/10th second in qemu.
*(uint64*)CLINT_MTIMECMP(id) = *(uint64*)CLINT_MTIME + interval;
// prepare information in scratch[] for timervec.
// scratch[0..2] : space for timervec to save registers.
// scratch[3] : address of CLINT MTIMECMP register.
// scratch[4] : desired interval (in cycles) between timer interrupts.
uint64 *scratch = &timer_scratch[id][0];
scratch[3] = CLINT_MTIMECMP(id);
scratch[4] = interval;
w_mscratch((uint64)scratch);
// set the machine-mode trap handler.
w_mtvec((uint64)timervec);
// enable machine-mode interrupts.
w_mstatus(r_mstatus() | MSTATUS_MIE);
// enable machine-mode timer interrupts.
w_mie(r_mie() | MIE_MTIE);
// enable supervisor-mode timer interrupts.
w_mie(r_mie() | MIE_STIE);
// enable the sstc extension (i.e. stimecmp).
w_menvcfg(r_menvcfg() | (1L << 63));
// allow supervisor to use stimecmp and time.
w_mcounteren(r_mcounteren() | 2);
// ask for the very first timer interrupt.
w_stimecmp(r_time() + 1000000);
}

View File

@ -1,7 +1,6 @@
#define T_DIR 1 // Directory
#define T_FILE 2 // File
#define T_DEVICE 3 // Device
#include "types.h"
#define T_DIR 1 // Directory
#define T_FILE 2 // File
#define T_DEVICE 3 // Device
struct stat {
int dev; // File system's disk device

View File

@ -101,6 +101,8 @@ extern uint64 sys_unlink(void);
extern uint64 sys_link(void);
extern uint64 sys_mkdir(void);
extern uint64 sys_close(void);
extern uint64 sys_sigalarm(void);
extern uint64 sys_sigreturn(void);
// An array mapping syscall numbers from syscall.h
// to the function that handles the system call.
@ -126,6 +128,8 @@ static uint64 (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_sigalarm] sys_sigalarm,
[SYS_sigreturn] sys_sigreturn,
};
void

View File

@ -20,3 +20,5 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_sigalarm 22
#define SYS_sigreturn 23

View File

@ -54,6 +54,8 @@ sys_sleep(void)
int n;
uint ticks0;
backtrace();
argint(0, &n);
if(n < 0)
n = 0;
@ -91,3 +93,44 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}
// sys_sigalarm函数用于设置进程的定时信号处理机制。
// 参数:
// alarm_cnt定时器计数值表示每经过alarm_cnt个时钟中断后触发一次信号处理。
// addr信号处理函数的用户空间地址。
// 实现:
// 1. 通过argint和argaddr获取用户传入的参数。
// 2. 将进程的inter_cnt中断计数器清零。
// 3. 保存信号处理函数地址和定时器计数值到进程结构体。
// 4. 返回0表示设置成功。
// sys_sigreturn函数用于在信号处理函数执行完毕后恢复进程的上下文。
// 实现:
// 1. 获取当前进程指针。
// 2. 将trapframe恢复为信号处理前保存的pre_trapframe恢复进程上下文。
// 3. 将inter_cnt中断计数器清零重新计数。
// 4. 返回信号处理前a0寄存器的值作为系统调用的返回值。
uint64
sys_sigalarm(void)
{
int alarm_cnt;
uint64 addr;
struct proc *p = myproc();
argint(0, &alarm_cnt);
argaddr(1, &addr);
p->inter_cnt = 0;
p->handler = addr;
p->alarm_cnt = alarm_cnt;
return 0;
}
uint64
sys_sigreturn(void)
{
struct proc* p = myproc();
*p->trapframe = *p->pre_trapframe;
p->inter_cnt = 0;
return p->pre_trapframe->a0;
}

View File

@ -68,17 +68,29 @@ 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());
printf("usertrap(): unexpected scause 0x%lx pid=%d\n", r_scause(), p->pid);
printf(" sepc=0x%lx stval=0x%lx\n", r_sepc(), r_stval());
setkilled(p);
}
if(killed(p))
exit(-1);
// give up the CPU if this is a timer interrupt.
if(which_dev == 2)
yield();
// 如果这是一个定时器中断则让出CPU。
if(which_dev == 2){
// 当前进程的中断计数器加一。
p->inter_cnt++;
// 如果中断计数器达到设定的报警计数并且报警计数大于0。
if (p->inter_cnt == p->alarm_cnt && 0 < p->alarm_cnt) {
// 备份当前trapframe到pre_trapframe用于后续恢复。
*p->pre_trapframe = *p->trapframe;
// 将epc设置为用户定义的handler函数地址返回用户态时会跳转到handler执行。
p->trapframe->epc = p->handler;
} else {
// 否则让出CPU进行进程调度。
yield();
}
}
usertrapret();
}
@ -145,13 +157,13 @@ kerneltrap()
panic("kerneltrap: interrupts enabled");
if((which_dev = devintr()) == 0){
printf("scause %p\n", scause);
printf("sepc=%p stval=%p\n", r_sepc(), r_stval());
// interrupt or trap from an unknown source
printf("scause=0x%lx sepc=0x%lx stval=0x%lx\n", scause, r_sepc(), r_stval());
panic("kerneltrap");
}
// give up the CPU if this is a timer interrupt.
if(which_dev == 2 && myproc() != 0 && myproc()->state == RUNNING)
if(which_dev == 2 && myproc() != 0)
yield();
// the yield() may have caused some traps to occur,
@ -163,10 +175,17 @@ kerneltrap()
void
clockintr()
{
acquire(&tickslock);
ticks++;
wakeup(&ticks);
release(&tickslock);
if(cpuid() == 0){
acquire(&tickslock);
ticks++;
wakeup(&ticks);
release(&tickslock);
}
// ask for the next timer interrupt. this also clears
// the interrupt request. 1000000 is about a tenth
// of a second.
w_stimecmp(r_time() + 1000000);
}
// check if it's an external interrupt or software interrupt,
@ -179,8 +198,7 @@ devintr()
{
uint64 scause = r_scause();
if((scause & 0x8000000000000000L) &&
(scause & 0xff) == 9){
if(scause == 0x8000000000000009L){
// this is a supervisor external interrupt, via PLIC.
// irq indicates which device interrupted.
@ -201,18 +219,9 @@ devintr()
plic_complete(irq);
return 1;
} else if(scause == 0x8000000000000001L){
// software interrupt from a machine-mode timer interrupt,
// forwarded by timervec in kernelvec.S.
if(cpuid() == 0){
clockintr();
}
// acknowledge the software interrupt by clearing
// the SSIP bit in sip.
w_sip(r_sip() & ~2);
} else if(scause == 0x8000000000000005L){
// timer interrupt.
clockintr();
return 2;
} else {
return 0;

View File

@ -1,10 +1,10 @@
typedef unsigned int uint;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef unsigned char uchar;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned int uint32;
typedef unsigned long uint64;
typedef uint64 pde_t;

View File

@ -13,7 +13,7 @@
// the UART control registers are memory-mapped
// at address UART0. this macro returns the
// address of one of the registers.
#define Reg(reg) ((volatile unsigned char *)(UART0 + reg))
#define Reg(reg) ((volatile unsigned char *)(UART0 + (reg)))
// the UART control registers.
// some have different meanings for
@ -136,6 +136,7 @@ uartstart()
while(1){
if(uart_tx_w == uart_tx_r){
// transmit buffer is empty.
ReadReg(ISR);
return;
}

View File

@ -31,7 +31,7 @@ kvmmake(void)
kvmmap(kpgtbl, VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W);
// PLIC
kvmmap(kpgtbl, PLIC, PLIC, 0x400000, PTE_R | PTE_W);
kvmmap(kpgtbl, PLIC, PLIC, 0x4000000, PTE_R | PTE_W);
// map kernel text executable and read-only.
kvmmap(kpgtbl, KERNBASE, KERNBASE, (uint64)etext-KERNBASE, PTE_R | PTE_X);

BIN
mkfs/mkfs

Binary file not shown.

View File

@ -20,7 +20,7 @@
// Disk layout:
// [ boot block | sb block | log | inode blocks | free bit map | data blocks ]
int nbitmap = FSSIZE/(BSIZE*8) + 1;
int nbitmap = FSSIZE/BPB + 1;
int ninodeblocks = NINODES / IPB + 1;
int nlog = LOGSIZE;
int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap)
@ -147,6 +147,8 @@ main(int argc, char *argv[])
if(shortname[0] == '_')
shortname += 1;
assert(strlen(shortname) <= DIRSIZ);
inum = ialloc(T_FILE);
bzero(&de, sizeof(de));
@ -238,7 +240,7 @@ balloc(int used)
int i;
printf("balloc: first %d blocks have been allocated\n", used);
assert(used < BSIZE*8);
assert(used < BPB);
bzero(buf, BSIZE);
for(i = 0; i < used; i++){
buf[i/8] = buf[i/8] | (0x1 << (i%8));

View File

@ -1 +1 @@
3
8

193
user/alarmtest.c Normal file
View File

@ -0,0 +1,193 @@
//
// test program for the alarm lab.
// you can modify this file for testing,
// but please make sure your kernel
// modifications pass the original
// versions of these tests.
//
#include "kernel/param.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/riscv.h"
#include "user/user.h"
void test0();
void test1();
void test2();
void test3();
void periodic();
void slow_handler();
void dummy_handler();
int
main(int argc, char *argv[])
{
test0();
test1();
test2();
test3();
exit(0);
}
volatile static int count;
void
periodic()
{
count = count + 1;
printf("alarm!\n");
sigreturn();
}
// tests whether the kernel calls
// the alarm handler even a single time.
void
test0()
{
int i;
printf("test0 start\n");
count = 0;
sigalarm(2, periodic);
for(i = 0; i < 1000*500000; i++){
if((i % 1000000) == 0)
write(2, ".", 1);
if(count > 0)
break;
}
sigalarm(0, 0);
if(count > 0){
printf("test0 passed\n");
} else {
printf("\ntest0 failed: the kernel never called the alarm handler\n");
}
}
void __attribute__ ((noinline)) foo(int i, int *j) {
if((i % 2500000) == 0) {
write(2, ".", 1);
}
*j += 1;
}
//
// tests that the kernel calls the handler multiple times.
//
// tests that, when the handler returns, it returns to
// the point in the program where the timer interrupt
// occurred, with all registers holding the same values they
// held when the interrupt occurred.
//
void
test1()
{
int i;
int j;
printf("test1 start\n");
count = 0;
j = 0;
sigalarm(2, periodic);
for(i = 0; i < 500000000; i++){
if(count >= 10)
break;
foo(i, &j);
}
if(count < 10){
printf("\ntest1 failed: too few calls to the handler\n");
} else if(i != j){
// the loop should have called foo() i times, and foo() should
// have incremented j once per call, so j should equal i.
// once possible source of errors is that the handler may
// return somewhere other than where the timer interrupt
// occurred; another is that that registers may not be
// restored correctly, causing i or j or the address ofj
// to get an incorrect value.
printf("\ntest1 failed: foo() executed fewer times than it was called\n");
} else {
printf("test1 passed\n");
}
}
//
// tests that kernel does not allow reentrant alarm calls.
void
test2()
{
int i;
int pid;
int status;
printf("test2 start\n");
if ((pid = fork()) < 0) {
printf("test2: fork failed\n");
}
if (pid == 0) {
count = 0;
sigalarm(2, slow_handler);
for(i = 0; i < 1000*500000; i++){
if((i % 1000000) == 0)
write(2, ".", 1);
if(count > 0)
break;
}
if (count == 0) {
printf("\ntest2 failed: alarm not called\n");
exit(1);
}
exit(0);
}
wait(&status);
if (status == 0) {
printf("test2 passed\n");
}
}
void
slow_handler()
{
count++;
printf("alarm!\n");
if (count > 1) {
printf("test2 failed: alarm handler called more than once\n");
exit(1);
}
for (int i = 0; i < 1000*500000; i++) {
asm volatile("nop"); // avoid compiler optimizing away loop
}
sigalarm(0, 0);
sigreturn();
}
//
// dummy alarm handler; after running immediately uninstall
// itself and finish signal handling
void
dummy_handler()
{
sigalarm(0, 0);
sigreturn();
}
//
// tests that the return from sys_sigreturn() does not
// modify the a0 register
void
test3()
{
uint64 a0;
sigalarm(1, dummy_handler);
printf("test3 start\n");
asm volatile("lui a5, 0");
asm volatile("addi a0, a5, 0xac" : : : "a0");
for(int i = 0; i < 500000000; i++)
;
asm volatile("mv %0, a0" : "=r" (a0) );
if(a0 != 0xac)
printf("test3 failed: register a0 changed\n");
else
printf("test3 passed\n");
}

10
user/bttest.c Normal file
View File

@ -0,0 +1,10 @@
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int
main(int argc, char *argv[])
{
sleep(1);
exit(0);
}

17
user/call.c Normal file
View File

@ -0,0 +1,17 @@
#include "kernel/param.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int g(int x) {
return x+3;
}
int f(int x) {
return g(x);
}
void main(void) {
printf("%d %d\n", f(8)+1, 13);
exit(0);
}

View File

@ -1,35 +1,38 @@
#include "kernel/fcntl.h"
#include "kernel/stat.h"
#include "kernel/types.h"
#include "kernel/fcntl.h"
#include "user/user.h"
char buf[512];
void cat(int fd) {
void
cat(int fd)
{
int n;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
while((n = read(fd, buf, sizeof(buf))) > 0) {
if (write(1, buf, n) != n) {
fprintf(2, "cat: write error\n");
exit(1);
}
}
if (n < 0) {
if(n < 0){
fprintf(2, "cat: read error\n");
exit(1);
}
}
int main(int argc, char *argv[]) {
int
main(int argc, char *argv[])
{
int fd, i;
if (argc <= 1) {
if(argc <= 1){
cat(0);
exit(0);
}
for (i = 1; i < argc; i++) {
if ((fd = open(argv[i], O_RDONLY)) < 0) {
for(i = 1; i < argc; i++){
if((fd = open(argv[i], O_RDONLY)) < 0){
fprintf(2, "cat: cannot open %s\n", argv[i]);
exit(1);
}

View File

@ -1,64 +0,0 @@
#include "kernel/fs.h"
#include "kernel/stat.h"
#include "kernel/types.h"
#include "user/user.h"
void find(char *path, char *filename) {
int fd = open(path, 0);
char buf[512], *p;
struct dirent de;
struct stat st;
if (fd < 0 || strlen(path) + 1 + DIRSIZ + 1 >= sizeof(buf)) {
fprintf(2, "find: Invalid path: %s\n", path);
// close(fd);
return;
}
if (fstat(fd, &st) < 0) {
fprintf(2, "find: Failed to stat %s\n", path);
close(fd);
return;
}
if (st.type != T_DIR) {
fprintf(2, "find: %s is not a directory\n", path);
close(fd);
return;
}
strncpy(buf, path, strlen(path) + 1);
p = buf + strlen(path);
*p++ = '/';
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
if (de.inum == 0)
continue;
if (!strcmp(de.name, ".") || !strcmp(de.name, ".."))
continue;
memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if (stat(buf, &st) < 0) {
fprintf(2, "find: Failed to stat %s\n", buf);
continue;
}
if (st.type == T_DIR) {
find(buf, filename);
} else if (st.type == T_FILE) {
if (!strcmp(filename, "*") || !strcmp(filename, de.name)) {
printf("%s\n", buf);
}
}
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: find path filename\n");
exit(1);
}
find(argv[1], argv[2]);
exit(0);
}

View File

@ -24,5 +24,5 @@ init:
# char *argv[] = { init, 0 };
.p2align 2
argv:
.long init
.long 0
.quad init
.quad 0

View File

@ -1,81 +1,87 @@
#include "kernel/fcntl.h"
#include "kernel/fs.h"
#include "kernel/stat.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"
#include "kernel/fcntl.h"
char *fmtname(char *path) {
static char buf[DIRSIZ + 1];
char*
fmtname(char *path)
{
static char buf[DIRSIZ+1];
char *p;
// Find first character after last slash.
for (p = path + strlen(path); p >= path && *p != '/'; p--)
for(p=path+strlen(path); p >= path && *p != '/'; p--)
;
p++;
// Return blank-padded name.
if (strlen(p) >= DIRSIZ)
if(strlen(p) >= DIRSIZ)
return p;
memmove(buf, p, strlen(p));
memset(buf + strlen(p), ' ', DIRSIZ - strlen(p));
memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
return buf;
}
void ls(char *path) {
void
ls(char *path)
{
char buf[512], *p;
int fd;
struct dirent de;
struct stat st;
if ((fd = open(path, O_RDONLY)) < 0) {
if((fd = open(path, O_RDONLY)) < 0){
fprintf(2, "ls: cannot open %s\n", path);
return;
}
if (fstat(fd, &st) < 0) {
if(fstat(fd, &st) < 0){
fprintf(2, "ls: cannot stat %s\n", path);
close(fd);
return;
}
switch (st.type) {
switch(st.type){
case T_DEVICE:
case T_FILE:
printf("%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
printf("%s %d %d %d\n", fmtname(path), st.type, st.ino, (int) st.size);
break;
case T_DIR:
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
printf("ls: path too long\n");
break;
}
strcpy(buf, path);
p = buf + strlen(buf);
p = buf+strlen(buf);
*p++ = '/';
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
if (de.inum == 0)
while(read(fd, &de, sizeof(de)) == sizeof(de)){
if(de.inum == 0)
continue;
memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if (stat(buf, &st) < 0) {
if(stat(buf, &st) < 0){
printf("ls: cannot stat %s\n", buf);
continue;
}
printf("%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
printf("%s %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size);
}
break;
}
close(fd);
}
int main(int argc, char *argv[]) {
int
main(int argc, char *argv[])
{
int i;
if (argc < 2) {
if(argc < 2){
ls(".");
exit(0);
}
for (i = 1; i < argc; i++)
for(i=1; i<argc; i++)
ls(argv[i]);
exit(0);
}

View File

@ -1,21 +0,0 @@
#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]) {
int proc_f2s[2], proc_s2f[2];
char buffer[8];
pipe(proc_f2s);
pipe(proc_s2f);
if (fork() == 0) {
read(proc_s2f[0], buffer, 4);
printf("%d: received %s\n", getpid(), buffer);
write(proc_f2s[1], "pong", strlen("pong"));
} else {
write(proc_s2f[1], "ping", strlen("ping"));
read(proc_f2s[0], buffer, 4);
printf("%d: received %s\n", getpid(), buffer);
}
exit(0);
}

View File

@ -1,61 +0,0 @@
#include "kernel/types.h"
#include "user/user.h"
void redirect(int n, int pd[]) {
close(n);
dup(pd[n]);
close(pd[0]);
close(pd[1]);
}
void primes() {
int previous, next;
int fd[2];
while (read(0, &previous, sizeof(int))) {
printf("prime %d\n", previous);
if (pipe(fd) < 0) {
fprintf(2, "pipe failed\n");
exit(1);
}
if (fork() == 0) {
redirect(1, fd);
while (read(0, &next, sizeof(int))) {
if (next % previous != 0) {
write(1, &next, sizeof(int));
}
}
exit(0);
} else {
close(fd[1]);
redirect(0, fd);
}
}
}
int main(int argc, char *argv[]) {
int fd[2];
if (pipe(fd) < 0) {
fprintf(2, "pipe failed\n");
exit(1);
}
if (fork() == 0) {
redirect(1, fd);
for (int i = 2; i < 36; i++) {
write(1, &i, sizeof(int));
}
exit(0);
} else {
close(fd[1]);
redirect(0, fd);
primes();
}
exit(0);
}

View File

@ -1,20 +1,26 @@
#include "kernel/stat.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include <stdarg.h>
static char digits[] = "0123456789ABCDEF";
static void putc(int fd, char c) { write(fd, &c, 1); }
static void
putc(int fd, char c)
{
write(fd, &c, 1);
}
static void printint(int fd, int xx, int base, int sgn) {
static void
printint(int fd, int xx, int base, int sgn)
{
char buf[16];
int i, neg;
uint x;
neg = 0;
if (sgn && xx < 0) {
if(sgn && xx < 0){
neg = 1;
x = -xx;
} else {
@ -22,17 +28,18 @@ static void printint(int fd, int xx, int base, int sgn) {
}
i = 0;
do {
do{
buf[i++] = digits[x % base];
} while ((x /= base) != 0);
if (neg)
}while((x /= base) != 0);
if(neg)
buf[i++] = '-';
while (--i >= 0)
while(--i >= 0)
putc(fd, buf[i]);
}
static void printptr(int fd, uint64 x) {
static void
printptr(int fd, uint64 x) {
int i;
putc(fd, '0');
putc(fd, 'x');
@ -41,58 +48,108 @@ static void printptr(int fd, uint64 x) {
}
// Print to the given fd. Only understands %d, %x, %p, %s.
void vprintf(int fd, const char *fmt, va_list ap) {
void
vprintf(int fd, const char *fmt, va_list ap)
{
char *s;
int c, i, state;
int c0, c1, c2, i, state;
state = 0;
for (i = 0; fmt[i]; i++) {
c = fmt[i] & 0xff;
if (state == 0) {
if (c == '%') {
for(i = 0; fmt[i]; i++){
c0 = fmt[i] & 0xff;
if(state == 0){
if(c0 == '%'){
state = '%';
} else {
putc(fd, c);
putc(fd, c0);
}
} else if (state == '%') {
if (c == 'd') {
} else if(state == '%'){
c1 = c2 = 0;
if(c0) c1 = fmt[i+1] & 0xff;
if(c1) c2 = fmt[i+2] & 0xff;
if(c0 == 'd'){
printint(fd, va_arg(ap, int), 10, 1);
} else if (c == 'l') {
} else if(c0 == 'l' && c1 == 'd'){
printint(fd, va_arg(ap, uint64), 10, 1);
i += 1;
} else if(c0 == 'l' && c1 == 'l' && c2 == 'd'){
printint(fd, va_arg(ap, uint64), 10, 1);
i += 2;
} else if(c0 == 'u'){
printint(fd, va_arg(ap, int), 10, 0);
} else if(c0 == 'l' && c1 == 'u'){
printint(fd, va_arg(ap, uint64), 10, 0);
} else if (c == 'x') {
i += 1;
} else if(c0 == 'l' && c1 == 'l' && c2 == 'u'){
printint(fd, va_arg(ap, uint64), 10, 0);
i += 2;
} else if(c0 == 'x'){
printint(fd, va_arg(ap, int), 16, 0);
} else if (c == 'p') {
} else if(c0 == 'l' && c1 == 'x'){
printint(fd, va_arg(ap, uint64), 16, 0);
i += 1;
} else if(c0 == 'l' && c1 == 'l' && c2 == 'x'){
printint(fd, va_arg(ap, uint64), 16, 0);
i += 2;
} else if(c0 == 'p'){
printptr(fd, va_arg(ap, uint64));
} else if (c == 's') {
s = va_arg(ap, char *);
if (s == 0)
} else if(c0 == 's'){
if((s = va_arg(ap, char*)) == 0)
s = "(null)";
while (*s != 0) {
for(; *s; s++)
putc(fd, *s);
} else if(c0 == '%'){
putc(fd, '%');
} else {
// Unknown % sequence. Print it to draw attention.
putc(fd, '%');
putc(fd, c0);
}
#if 0
if(c == 'd'){
printint(fd, va_arg(ap, int), 10, 1);
} else if(c == 'l') {
printint(fd, va_arg(ap, uint64), 10, 0);
} else if(c == 'x') {
printint(fd, va_arg(ap, int), 16, 0);
} else if(c == 'p') {
printptr(fd, va_arg(ap, uint64));
} else if(c == 's'){
s = va_arg(ap, char*);
if(s == 0)
s = "(null)";
while(*s != 0){
putc(fd, *s);
s++;
}
} else if (c == 'c') {
} else if(c == 'c'){
putc(fd, va_arg(ap, uint));
} else if (c == '%') {
} else if(c == '%'){
putc(fd, c);
} else {
// Unknown % sequence. Print it to draw attention.
putc(fd, '%');
putc(fd, c);
}
#endif
state = 0;
}
}
}
void fprintf(int fd, const char *fmt, ...) {
void
fprintf(int fd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fd, fmt, ap);
}
void printf(const char *fmt, ...) {
void
printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);

View File

@ -1,13 +0,0 @@
#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(2, "Usage: sleep <time>\n");
exit(1);
}
int time = atoi(argv[1]);
// printf("time: %d\n", time);
sleep(time);
exit(0);
}

View File

@ -1,133 +1,134 @@
#include "kernel/fcntl.h"
#include "kernel/stat.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/fcntl.h"
#include "user/user.h"
//
// wrapper so that it's OK if main() does not call exit().
//
void _main() {
void
start()
{
extern int main();
main();
exit(0);
}
char *strcpy(char *s, const char *t) {
char*
strcpy(char *s, const char *t)
{
char *os;
os = s;
while ((*s++ = *t++) != 0)
while((*s++ = *t++) != 0)
;
return os;
}
char *strncpy(char *s, const char *t, int n) {
char *os;
os = s;
if (n <= 0)
return os;
while (--n > 0 && (*s++ = *t++) != 0)
;
while (n-- > 0)
*s++ = 0;
return os;
}
int strcmp(const char *p, const char *q) {
while (*p && *p == *q)
int
strcmp(const char *p, const char *q)
{
while(*p && *p == *q)
p++, q++;
return (uchar)*p - (uchar)*q;
}
int strncmp(const char *p, const char *q, int n) {
while (n > 0 && *p && *p == *q)
p++, q++, n--;
if (n == 0)
return 0;
return (uchar)*p - (uchar)*q;
}
uint strlen(const char *s) {
uint
strlen(const char *s)
{
int n;
for (n = 0; s[n]; n++)
for(n = 0; s[n]; n++)
;
return n;
}
void *memset(void *dst, int c, uint n) {
char *cdst = (char *)dst;
void*
memset(void *dst, int c, uint n)
{
char *cdst = (char *) dst;
int i;
for (i = 0; i < n; i++) {
for(i = 0; i < n; i++){
cdst[i] = c;
}
return dst;
}
char *strchr(const char *s, char c) {
for (; *s; s++)
if (*s == c)
return (char *)s;
char*
strchr(const char *s, char c)
{
for(; *s; s++)
if(*s == c)
return (char*)s;
return 0;
}
char *gets(char *buf, int max) {
char*
gets(char *buf, int max)
{
int i, cc;
char c;
for (i = 0; i + 1 < max;) {
for(i=0; i+1 < max; ){
cc = read(0, &c, 1);
if (cc < 1)
if(cc < 1)
break;
buf[i++] = c;
if (c == '\n' || c == '\r')
if(c == '\n' || c == '\r')
break;
}
buf[i] = '\0';
return buf;
}
int stat(const char *n, struct stat *st) {
int
stat(const char *n, struct stat *st)
{
int fd;
int r;
fd = open(n, O_RDONLY);
if (fd < 0)
if(fd < 0)
return -1;
r = fstat(fd, st);
close(fd);
return r;
}
int atoi(const char *s) {
int
atoi(const char *s)
{
int n;
n = 0;
while ('0' <= *s && *s <= '9')
n = n * 10 + *s++ - '0';
while('0' <= *s && *s <= '9')
n = n*10 + *s++ - '0';
return n;
}
void *memmove(void *vdst, const void *vsrc, int n) {
void*
memmove(void *vdst, const void *vsrc, int n)
{
char *dst;
const char *src;
dst = vdst;
src = vsrc;
if (src > dst) {
while (n-- > 0)
while(n-- > 0)
*dst++ = *src++;
} else {
dst += n;
src += n;
while (n-- > 0)
while(n-- > 0)
*--dst = *--src;
}
return vdst;
}
int memcmp(const void *s1, const void *s2, uint n) {
int
memcmp(const void *s1, const void *s2, uint n)
{
const char *p1 = s1, *p2 = s2;
while (n-- > 0) {
if (*p1 != *p2) {
@ -139,6 +140,8 @@ int memcmp(const void *s1, const void *s2, uint n) {
return 0;
}
void *memcpy(void *dst, const void *src, uint n) {
void *
memcpy(void *dst, const void *src, uint n)
{
return memmove(dst, src, n);
}

View File

@ -1,44 +1,45 @@
#include "kernel/types.h"
struct stat;
// system calls
int fork(void);
int exit(int) __attribute__((noreturn));
int wait(int *);
int pipe(int *);
int write(int, const void *, int);
int read(int, void *, int);
int wait(int*);
int pipe(int*);
int write(int, const void*, int);
int read(int, void*, int);
int close(int);
int kill(int);
int exec(const char *, char **);
int open(const char *, int);
int mknod(const char *, short, short);
int unlink(const char *);
int fstat(int fd, struct stat *);
int link(const char *, const char *);
int mkdir(const char *);
int chdir(const char *);
int exec(const char*, char**);
int open(const char*, int);
int mknod(const char*, short, short);
int unlink(const char*);
int fstat(int fd, struct stat*);
int link(const char*, const char*);
int mkdir(const char*);
int chdir(const char*);
int dup(int);
int getpid(void);
char *sbrk(int);
char* sbrk(int);
int sleep(int);
int uptime(void);
int sigalarm(int, void(*)());
int sigreturn(void);
// ulib.c
int stat(const char *, struct stat *);
char *strcpy(char *, const char *);
char *strncpy(char *, const char *, int);
void *memmove(void *, const void *, int);
char *strchr(const char *, char c);
int strcmp(const char *, const char *);
int strncmp(const char *, const char *, int);
void fprintf(int, const char *, ...);
void printf(const char *, ...);
char *gets(char *, int max);
uint strlen(const char *);
void *memset(void *, int, uint);
void *malloc(uint);
void free(void *);
int atoi(const char *);
int stat(const char*, struct stat*);
char* strcpy(char*, const char*);
void *memmove(void*, const void*, int);
char* strchr(const char*, char c);
int strcmp(const char*, const char*);
void fprintf(int, const char*, ...) __attribute__ ((format (printf, 2, 3)));
void printf(const char*, ...) __attribute__ ((format (printf, 1, 2)));
char* gets(char*, int max);
uint strlen(const char*);
void* memset(void*, int, uint);
int atoi(const char*);
int memcmp(const void *, const void *, uint);
void *memcpy(void *, const void *, uint);
// umalloc.c
void* malloc(uint);
void free(void*);

View File

@ -1,6 +1,4 @@
OUTPUT_ARCH( "riscv" )
ENTRY( _main )
SECTIONS
{
@ -15,9 +13,14 @@ SECTIONS
*(.srodata .srodata.*) /* do not need to distinguish this from .rodata */
. = ALIGN(16);
*(.rodata .rodata.*)
. = ALIGN(0x1000);
}
.eh_frame : {
*(.eh_frame)
*(.eh_frame.*)
}
. = ALIGN(0x1000);
.data : {
. = ALIGN(16);
*(.sdata .sdata.*) /* do not need to distinguish this from .data */

View File

@ -32,9 +32,10 @@ char buf[BUFSZ];
void
copyin(char *s)
{
uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
uint64 addrs[] = { 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,
0xffffffffffffffff };
for(int ai = 0; ai < 2; ai++){
for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){
uint64 addr = addrs[ai];
int fd = open("copyin1", O_CREATE|O_WRONLY);
@ -44,7 +45,7 @@ copyin(char *s)
}
int n = write(fd, (void*)addr, 8192);
if(n >= 0){
printf("write(fd, %p, 8192) returned %d, not -1\n", addr, n);
printf("write(fd, %p, 8192) returned %d, not -1\n", (void*)addr, n);
exit(1);
}
close(fd);
@ -52,7 +53,7 @@ copyin(char *s)
n = write(1, (char*)addr, 8192);
if(n > 0){
printf("write(1, %p, 8192) returned %d, not -1 or 0\n", addr, n);
printf("write(1, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);
exit(1);
}
@ -63,7 +64,7 @@ copyin(char *s)
}
n = write(fds[1], (char*)addr, 8192);
if(n > 0){
printf("write(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n);
printf("write(pipe, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);
exit(1);
}
close(fds[0]);
@ -76,9 +77,10 @@ copyin(char *s)
void
copyout(char *s)
{
uint64 addrs[] = { 0LL, 0x80000000LL, 0xffffffffffffffff };
uint64 addrs[] = { 0LL, 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,
0xffffffffffffffff };
for(int ai = 0; ai < 2; ai++){
for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){
uint64 addr = addrs[ai];
int fd = open("README", 0);
@ -88,7 +90,7 @@ copyout(char *s)
}
int n = read(fd, (void*)addr, 8192);
if(n > 0){
printf("read(fd, %p, 8192) returned %d, not -1 or 0\n", addr, n);
printf("read(fd, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);
exit(1);
}
close(fd);
@ -105,7 +107,7 @@ copyout(char *s)
}
n = read(fds[0], (void*)addr, 8192);
if(n > 0){
printf("read(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n);
printf("read(pipe, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);
exit(1);
}
close(fds[0]);
@ -117,14 +119,15 @@ copyout(char *s)
void
copyinstr1(char *s)
{
uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
uint64 addrs[] = { 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,
0xffffffffffffffff };
for(int ai = 0; ai < 2; ai++){
for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){
uint64 addr = addrs[ai];
int fd = open((char *)addr, O_CREATE|O_WRONLY);
if(fd >= 0){
printf("open(%p) returned %d, not -1\n", addr, fd);
printf("open(%p) returned %d, not -1\n", (void*)addr, fd);
exit(1);
}
}
@ -241,7 +244,7 @@ copyinstr3(char *s)
// See if the kernel refuses to read/write user memory that the
// application doesn't have anymore, because it returned it.
void
rwsbrk()
rwsbrk(char* argv)
{
int fd, n;
@ -264,7 +267,7 @@ rwsbrk()
}
n = write(fd, (void*)(a+4096), 1024);
if(n >= 0){
printf("write(fd, %p, 1024) returned %d, not -1\n", a+4096, n);
printf("write(fd, %p, 1024) returned %d, not -1\n", (void*)a+4096, n);
exit(1);
}
close(fd);
@ -277,7 +280,7 @@ rwsbrk()
}
n = read(fd, (void*)(a+4096), 10);
if(n >= 0){
printf("read(fd, %p, 10) returned %d, not -1\n", a+4096, n);
printf("read(fd, %p, 10) returned %d, not -1\n", (void*)a+4096, n);
exit(1);
}
close(fd);
@ -589,7 +592,7 @@ writebig(char *s)
for(i = 0; i < MAXFILE; i++){
((int*)buf)[0] = i;
if(write(fd, buf, BSIZE) != BSIZE){
printf("%s: error: write big file failed\n", s, i);
printf("%s: error: write big file failed i=%d\n", s, i);
exit(1);
}
}
@ -606,7 +609,7 @@ writebig(char *s)
for(;;){
i = read(fd, buf, BSIZE);
if(i == 0){
if(n == MAXFILE - 1){
if(n != MAXFILE){
printf("%s: read only %d blocks from big", s, n);
exit(1);
}
@ -773,7 +776,7 @@ pipe1(char *s)
cc = sizeof(buf);
}
if(total != N * SZ){
printf("%s: pipe1 oops 3 total %d\n", total);
printf("%s: pipe1 oops 3 total %d\n", s, total);
exit(1);
}
close(fds[0]);
@ -1069,7 +1072,7 @@ mem(char *s)
}
m1 = malloc(1024*20);
if(m1 == 0){
printf("couldn't allocate mem?!!\n", s);
printf("%s: couldn't allocate mem?!!\n", s);
exit(1);
}
free(m1);
@ -1161,14 +1164,14 @@ fourfiles(char *s)
pid = fork();
if(pid < 0){
printf("fork failed\n", s);
printf("%s: fork failed\n", s);
exit(1);
}
if(pid == 0){
fd = open(fname, O_CREATE | O_RDWR);
if(fd < 0){
printf("create failed\n", s);
printf("%s: create failed\n", s);
exit(1);
}
@ -1197,7 +1200,7 @@ fourfiles(char *s)
while((n = read(fd, buf, sizeof(buf))) > 0){
for(j = 0; j < n; j++){
if(buf[j] != '0'+i){
printf("wrong char\n", s);
printf("%s: wrong char\n", s);
exit(1);
}
}
@ -1223,7 +1226,7 @@ createdelete(char *s)
for(pi = 0; pi < NCHILD; pi++){
pid = fork();
if(pid < 0){
printf("fork failed\n", s);
printf("%s: fork failed\n", s);
exit(1);
}
@ -1277,7 +1280,7 @@ createdelete(char *s)
for(i = 0; i < N; i++){
for(pi = 0; pi < NCHILD; pi++){
name[0] = 'p' + i;
name[0] = 'p' + pi;
name[1] = '0' + i;
unlink(name);
}
@ -1544,7 +1547,7 @@ subdir(char *s)
}
if(mkdir("/dd/dd") != 0){
printf("subdir mkdir dd/dd failed\n", s);
printf("%s: subdir mkdir dd/dd failed\n", s);
exit(1);
}
@ -1569,7 +1572,7 @@ subdir(char *s)
close(fd);
if(link("dd/dd/ff", "dd/dd/ffff") != 0){
printf("link dd/dd/ff dd/dd/ffff failed\n", s);
printf("%s: link dd/dd/ff dd/dd/ffff failed\n", s);
exit(1);
}
@ -1591,7 +1594,7 @@ subdir(char *s)
exit(1);
}
if(chdir("dd/../../../dd") != 0){
printf("chdir dd/../../dd failed\n", s);
printf("%s: chdir dd/../../../dd failed\n", s);
exit(1);
}
if(chdir("./..") != 0){
@ -2034,7 +2037,7 @@ sbrkbasic(char *s)
for(i = 0; i < 5000; i++){
b = sbrk(1);
if(b != a){
printf("%s: sbrk test failed %d %x %x\n", s, i, a, b);
printf("%s: sbrk test failed %d %p %p\n", s, i, a, b);
exit(1);
}
*b = 1;
@ -2092,7 +2095,7 @@ sbrkmuch(char *s)
}
c = sbrk(0);
if(c != a - PGSIZE){
printf("%s: sbrk deallocation produced wrong address, a %x c %x\n", s, a, c);
printf("%s: sbrk deallocation produced wrong address, a %p c %p\n", s, a, c);
exit(1);
}
@ -2100,7 +2103,7 @@ sbrkmuch(char *s)
a = sbrk(0);
c = sbrk(PGSIZE);
if(c != a || sbrk(0) != a + PGSIZE){
printf("%s: sbrk re-allocation failed, a %x c %x\n", s, a, c);
printf("%s: sbrk re-allocation failed, a %p c %p\n", s, a, c);
exit(1);
}
if(*lastaddr == 99){
@ -2112,7 +2115,7 @@ sbrkmuch(char *s)
a = sbrk(0);
c = sbrk(-(sbrk(0) - oldbrk));
if(c != a){
printf("%s: sbrk downsize failed, a %x c %x\n", s, a, c);
printf("%s: sbrk downsize failed, a %p c %p\n", s, a, c);
exit(1);
}
}
@ -2131,7 +2134,7 @@ kernmem(char *s)
exit(1);
}
if(pid == 0){
printf("%s: oops could read %x = %x\n", s, a, *a);
printf("%s: oops could read %p = %x\n", s, a, *a);
exit(1);
}
int xstatus;
@ -2155,7 +2158,7 @@ MAXVAplus(char *s)
}
if(pid == 0){
*(char*)a = 99;
printf("%s: oops wrote %x\n", s, a);
printf("%s: oops wrote %p\n", s, (void*)a);
exit(1);
}
int xstatus;
@ -2307,9 +2310,14 @@ bigargtest(char *s)
if(pid == 0){
static char *args[MAXARG];
int i;
char big[400];
memset(big, ' ', sizeof(big));
big[sizeof(big)-1] = '\0';
for(i = 0; i < MAXARG-1; i++)
args[i] = "bigargs test: failed\n ";
args[i] = big;
args[MAXARG-1] = 0;
// this exec() should fail (and return) because the
// arguments are too large.
exec("echo", args);
fd = open("bigarg-ok", O_CREATE);
close(fd);
@ -2406,9 +2414,9 @@ stacktest(char *s)
pid = fork();
if(pid == 0) {
char *sp = (char *) r_sp();
sp -= PGSIZE;
sp -= USERSTACK*PGSIZE;
// the *sp should cause a trap.
printf("%s: stacktest: read below stack %p\n", s, *sp);
printf("%s: stacktest: read below stack %d\n", s, *sp);
exit(1);
} else if(pid < 0){
printf("%s: fork failed\n", s);
@ -2421,27 +2429,34 @@ stacktest(char *s)
exit(xstatus);
}
// check that writes to text segment fault
// check that writes to a few forbidden addresses
// cause a fault, e.g. process's text and TRAMPOLINE.
void
textwrite(char *s)
nowrite(char *s)
{
int pid;
int xstatus;
uint64 addrs[] = { 0, 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,
0xffffffffffffffff };
pid = fork();
if(pid == 0) {
volatile int *addr = (int *) 0;
*addr = 10;
exit(1);
} else if(pid < 0){
printf("%s: fork failed\n", s);
exit(1);
for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){
pid = fork();
if(pid == 0) {
volatile int *addr = (int *) addrs[ai];
*addr = 10;
printf("%s: write to %p did not fail!\n", s, addr);
exit(0);
} else if(pid < 0){
printf("%s: fork failed\n", s);
exit(1);
}
wait(&xstatus);
if(xstatus == 0){
// kernel did not kill child!
exit(1);
}
}
wait(&xstatus);
if(xstatus == -1) // kernel killed child?
exit(0);
else
exit(xstatus);
exit(0);
}
// regression test. copyin(), copyout(), and copyinstr() used to cast
@ -2629,7 +2644,7 @@ struct test {
{bigargtest, "bigargtest"},
{argptest, "argptest"},
{stacktest, "stacktest"},
{textwrite, "textwrite"},
{nowrite, "nowrite"},
{pgbug, "pgbug" },
{sbrkbugs, "sbrkbugs" },
{sbrklast, "sbrklast"},
@ -2666,7 +2681,7 @@ bigdir(char *s)
name[2] = '0' + (i % 64);
name[3] = '\0';
if(link("bd", name) != 0){
printf("%s: bigdir link(bd, %s) failed\n", s, name);
printf("%s: bigdir i=%d link(bd, %s) failed\n", s, i, name);
exit(1);
}
}
@ -2868,7 +2883,7 @@ diskfull(char *s)
// this mkdir() is expected to fail.
if(mkdir("diskfulldir") == 0)
printf("%s: mkdir(diskfulldir) unexpectedly succeeded!\n");
printf("%s: mkdir(diskfulldir) unexpectedly succeeded!\n", s);
unlink("diskfulldir");

View File

@ -36,3 +36,5 @@ entry("getpid");
entry("sbrk");
entry("sleep");
entry("uptime");
entry("sigalarm");
entry("sigreturn");

View File

@ -1,59 +0,0 @@
#include "kernel/param.h"
#include "kernel/stat.h"
#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(2, "Usage: xargs command\n");
exit(1);
}
char *cmd[argc + 1];
int index = 0, data = 0;
for (int i = 1; i < argc; ++i) {
cmd[index++] = argv[i];
}
char buffer[MAXARG];
char line[MAXARG] = {0};
int line_pos = 0;
while ((data = read(0, buffer, MAXARG)) > 0) {
for (int i = 0; i < data; ++i) {
if (buffer[i] == '\n' || buffer[i] == ' ') {
line[line_pos] = 0;
if (line_pos > 0) {
char *arg = malloc(line_pos + 1);
strcpy(arg, line);
cmd[index++] = arg;
}
line_pos = 0;
if (buffer[i] == '\n') {
cmd[index] = 0;
if (fork() == 0) {
exec(cmd[0], cmd);
exit(1);
}
wait(0);
index = argc - 1;
}
} else {
line[line_pos++] = buffer[i];
}
}
}
// bug fixed
if (line_pos > 0) {
line[line_pos] = 0;
cmd[index++] = line;
cmd[index] = 0;
if (fork() == 0) {
exec(cmd[0], cmd);
}
wait(0);
}
exit(0);
}

View File

@ -1,6 +0,0 @@
mkdir a
echo hello > a/b
mkdir c
echo hello > c/b
echo hello > b
find . b | xargs grep hello