Skip to content

Commit

Permalink
Merge branch 'dyn' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Jul 28, 2023
2 parents e2aabab + 8a95e12 commit 8c02205
Show file tree
Hide file tree
Showing 40 changed files with 924 additions and 312 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

kernel-qemu
#kernel-qemu
.idea
build
*.dts
Expand Down
36 changes: 21 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ VF2 ?=n
CV1811h ?=n
FEATURES :=
QEMU_ARGS :=

MEMORY_SIZE := 128M
img ?=fat32


Expand All @@ -46,16 +46,15 @@ define boot_qemu
qemu-system-riscv64 \
-M virt $(1)\
-bios $(BOOTLOADER) \
-device loader,file=kernel-qemu,addr=$(KERNEL_ENTRY_PA) \
-drive file=$(IMG),if=none,format=raw,id=x0 \
-device virtio-blk-device,drive=x0 \
-kernel kernel-qemu\
-$(QEMU_ARGS) \
-smp $(SMP) -m 256M \
-smp $(SMP) -m $(MEMORY_SIZE) \
-serial mon:stdio
endef

all:run
all:

install:
@cargo install --git https://github.com/os-module/elfinfo
Expand All @@ -78,9 +77,9 @@ trace_info:
user:
@cd apps && make all

sdcard:$(img) user testelf
sdcard:$(img) testelf user

run:install compile img
run:install compile sdcard
@echo qemu booot $(SMP)
$(call boot_qemu)
@#rm ./kernel-qemu
Expand Down Expand Up @@ -109,20 +108,28 @@ cv1811h:board



test:install compile $(img) SecondFile testelf
$(call boot_qemu)
f_test:
qemu-system-riscv64 \
-machine virt \
-kernel kernel-qemu \
-m 128M \
-nographic \
-smp 2 \
-drive file=./tools/sdcard.img,if=none,format=raw,id=x0 \
-device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 \
-device virtio-net-device,netdev=net -netdev user,id=net

testelf:
@sudo mkdir /fat/ostest
@sudo cp test/* /fat/ostest -r
@sudo mkdir /fat/libc
@#sudo mkdir /fat/ostest
@#sudo cp test/* /fat/ostest -r
@#sudo mkdir /fat/libc
if [ -d "sdcard" ]; then \
sudo cp sdcard/* /fat -r; \
sudo cp sdcard/* /fat/bin -r;\
fi
if [ -d "tools/siglibc" ]; then \
sudo cp tools/siglibc/build/* /fat/libc -r; \
fi
#if [ -d "tools/siglibc" ]; then \
# sudo cp tools/siglibc/build/* /fat/libc -r; \
# fi
@sync

dtb:
Expand Down Expand Up @@ -157,7 +164,6 @@ fat32:
@sudo cp tools/f1.txt /fat
@sudo mkdir /fat/folder
@sudo cp tools/f1.txt /fat/folder
@sudo mkdir /fat/bin
@sync


Expand Down
79 changes: 62 additions & 17 deletions apps/init/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
#![no_std]
#![no_main]

use Mstd::println;
use Mstd::process::{exec, fork, wait};
extern crate alloc;

use Mstd::process::{exec, exit, fork, waitpid};
use Mstd::system_shutdown;
use Mstd::thread::m_yield;

#[no_mangle]
fn main() -> isize {
if fork() == 0 {
exec("/bin/shell\0", &[0 as *const u8], &[0 as *const u8]);
} else {
loop {
// if fork() == 0 {
// exec("/bin/shell\0", &[0 as *const u8], &[0 as *const u8]);
// } else {
// loop {
// let mut exit_code: i32 = 0;
// let tid = wait(&mut exit_code);
// if tid == -1 {
// m_yield();
// continue;
// }
// println!(
// "[Init] Released a task, tid={}, exit_code={}",
// tid, exit_code,
// );
// }
// }
// 0
run_test();
system_shutdown();
}

const ENV: &[*const u8] = &[
"SHELL=/bash\0".as_ptr(),
"LOGNAME=root\0".as_ptr(),
"HOME=/root\0".as_ptr(),
"USER=root\0".as_ptr(),
"SHLVL=0\0".as_ptr(),
"OLDPWD=/root\0".as_ptr(),
"PS1=\x1b[1m\x1b[32mAlien\x1b[0m:\x1b[1m\x1b[34m\\w\x1b[0m\\$ \0".as_ptr(),
"_=/bin/bash\0".as_ptr(),
"PATH=/:/bin\0".as_ptr(),
"LD_LIBRARY_PATH=/\0".as_ptr(),
core::ptr::null(),
];

fn run_test() {
let commands = [
"./time-test\0",
"./run-static.sh\0",
"./run-dynamic.sh\0",
"./libc-bench2\0",
"./lua_testcode.sh\0",
"./busybox_testcode.sh\0",
"./cyclictest_testcode.sh\0",
"./unixbench_testcode.sh\0",
"./lmbench_testcode.sh\0",
"./iozone_testcode.sh\0",
];
commands.into_iter().for_each(|app| {
let args = [app.as_ptr()];
let pid = fork();
if pid == 0 {
exec(app, &args, ENV);
exit(0);
} else {
m_yield();
let mut exit_code: i32 = 0;
let tid = wait(&mut exit_code);
if tid == -1 {
m_yield();
continue;
}
println!(
"[Init] Released a task, tid={}, exit_code={}",
tid, exit_code,
);
let _x = waitpid(pid as usize, &mut exit_code);
}
}
0
});
}
4 changes: 2 additions & 2 deletions apps/shell/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use spin::Mutex;
use Mstd::fs::{chdir, get_cwd};
use Mstd::process::{exec, exit, fork, waitpid};
use Mstd::thread::m_yield;
use Mstd::{println, shutdown};
use Mstd::{println, system_shutdown};

#[derive(Debug)]
pub struct Parameter {
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Executor {
*CURRENT_DIR.lock() = Some(cwd.to_string());
}
}
"exit" => shutdown(),
"exit" => system_shutdown(),
_ => {
let mut cmd = self.cmd;
cmd.push('\0');
Expand Down
2 changes: 1 addition & 1 deletion boot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern "C" fn _start() {
asm!("\
mv tp, a0
add t0, a0, 1
slli t0, t0, 16
slli t0, t0, 13
la sp, {boot_stack}
add sp, sp, t0
call main
Expand Down
Binary file added doc/assert/image-20230728141346987.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/assert/image-20230728142459341.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/assert/image-20230728142904003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions doc/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,45 @@ http://nicephil.blinkenshell.org/my_book/ch07.html 比较细致
- [x] 正确处理task的`timer`, 需要在发生任务切换时以及内核态和用户态切换时检查是否超时,并发送信号
- [x] 处理时钟中断被忽略的问题 [unixbench.md](./unixbench.md)

## 2023/7/23-7/28

1. 进行联合测试

在前期我们对每个测试都单独进行了测试并独立通过了每个测试,但是最后我们需要将这些测试一起运行。由于对网络的支持不够完善,因此不能直接使用包含全部测试的测试脚本,在`init`程序中,我们手动运行已经支持的测例。

![image-20230728141346987](assert/image-20230728141346987.png)

2. 增加不同的内存分配器

- [x] buddy
- [x] talloc
- [x] slab

3. 修复一系列错误

- [x] 增加堆空间大小
- [x] 减小用户程序内核栈大小
- [x] 减小内核栈大小
- [x] 修复fat32删除文件失败的问题 [fat32删除文件](./fat32.md)

4. 改进

- [x] 增加时间片大小
- [x] 缺页异常重试

在多线程测试中,有的测试程序会连续创建多个线程,但是线程可能什么也不做就立即退出,如果在主进程未完成线程初始化的情况下线程被调度得到运行,那么线程退出很可能会触发缺页异常而失败。

在libc中的线程创建代码中:

![image-20230728142459341](assert/image-20230728142459341.png)

​ 这里会初始化线程描述符的相关字段

​ 而在线程退出的代码中:

![image-20230728142904003](assert/image-20230728142904003.png)

​ 这里会访问上述的相关字段。如果线程先退出而访问了这些字段,就会出现错误。为了避免这种错误,我们在线程发生缺页异常时,先将其切换掉,换别的线程运行,在下一次被调度执行后,如果主进程完成了初始化,那么他就可以继续运行,否则其可以再尝试几次,当到达尝试上限后,内核会选择杀掉它。

而通过增加时间片大小,也可以一定程度上解决这个问题。==观察到线程创建前后有两行获取锁和释放锁的操作,目前不知道是否是内核处理不当导致,需要后续对这里进行处理==

27 changes: 27 additions & 0 deletions doc/fat32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Fat32

内核需要支持`fat32`文件系统,我们实现了自己的`fat32`,但是并没有使用,而是改造了社区中已有的项目。并将其接入到我们实现的`rvfs`框架中。

## Bugs

无法直接删除文件,并回收空间

现象:在进行联合测试时,运行到`unixbench`以及`lmbench`时会触发写磁盘错误,通过单独测试,发现时此时已经没有磁盘空间。但是当单独测试其中的测例时又可以正常运行。

推测1:内核没有调用删除文件的函数?

结果:通过打印debug信息,发现fat32-vfs中有处理删除文件的操作

推测2:fat32-vfs是否实现正确?

结果:按照fat32-vfs的方式,我们在fat32原项目进行了删除文件测试,发现删除文件后磁盘空间没有减少,检查了github上是否有相关问题,没有查找到。

在阅读fat32的源代码时,发现有个`umount`函数,可以将磁盘元数据写回,回收空间,于是进行了尝试,发现空间并没有收回。

继续尝试,通过看源代码,在删除文件时没有看到回收占用空间的代码,于是查看其他函数,看哪个可以回收空间,在看到`truncate`时看到了相关的代码,于是我们在删除文件前将将文件的大小截断为0,并在删除后使用`umount`函数刷新元数据,再一次测试,在原项目通过后,我们又在fat32-vfs中修改了`unlink`的实现,按照这个步骤正确地删除文件,回收空间。



## 链接

[fat32-vfs](https://github.com/Godones/fat32-vfs.git)
4 changes: 2 additions & 2 deletions doc/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

### 动态链接(3days)

- [ ] elf文件的处理
- [x] elf文件的处理
- 处理elf文件类型,并根据类型初始化相关的数据结构
- [ ] 软链接处理 ---> use dbfs

### 系统调用(one week)

- [ ] more syscall support
- [x] more syscall support

`ioctl`: https://zhuanlan.zhihu.com/p/478259733

Expand Down
Binary file added kernel-qemu
Binary file not shown.
27 changes: 15 additions & 12 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bit_field = "0.10.1"
xmas-elf = "0.9.0"
bitflags = "1.3.2"
hashbrown = "0.13.2"
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers" }
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "de1c3b130e507702f13d142b9bee55670a4a2858" }
fdt = "0.1.3"
pci = { path = "../modules/pci-rs" }
embedded-graphics-core = "0.3.3"
Expand All @@ -23,16 +23,16 @@ lru = "0.10.0"
core2 = { version = "0.4.0", default-features = false, features = ["alloc"] }
cfg-if = "1.0.0"
### for memory manage
page-table = { git = "https://github.com/os-module/page-table.git", branch = "dev" }
#page-table = { path = "../../os-module/page-table" }
rvfs = { git = "https://github.com/Godones/rvfs.git" }
#rvfs = { path = "../../os-module/rvfs" }
fat32-vfs = { git = "https://github.com/Godones/fat32-vfs.git" }
#fat32-vfs = { path = "../../os-module/fat32-vfs" }
#page-table = { git = "https://github.com/os-module/page-table.git", branch = "dev" }
page-table = { path = "../../os-module/page-table" }
#rvfs = { git = "https://github.com/Godones/rvfs.git" }
rvfs = { path = "../../os-module/rvfs" }
#fat32-vfs = { git = "https://github.com/Godones/fat32-vfs.git" }
fat32-vfs = { path = "../../os-module/fat32-vfs" }

trace_lib = { git = "https://github.com/os-module/rtrace" }
preprint = "0.1.0"
rslab = "0.2.1"
rslab = { version = "0.2.1" }
syscall-table = { path = "../modules/syscall-table" }
plic = { path = "../modules/plic" }
pager = { path = "../modules/pager", default-features = false, features = ["bitmap"] }
Expand All @@ -41,11 +41,14 @@ kernel-sync = { path = "../modules/kernel-sync", package = "lock" }
syscall_define = { path = "../modules/syscall", package = "syscall" }
basemachine = { path = "../modules/basemachine" }

[build-dependencies]
#syscall-table = {path = "../modules/syscall-table",features = ["std"]}

talc = { version = "1.0", optional = true }
buddy_system_allocator = { version = "0.9.0", optional = true }

[features]
default = []
default = ["talloc"]
vf2 = []
cv1811h = []
cv1811h = []
slab = []
talloc = ["talc"]
buddy = ["buddy_system_allocator"]
17 changes: 11 additions & 6 deletions kernel/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub const CLOCK_FREQ: usize = 0x17d7840;

pub const FRAME_SIZE: usize = 0x1000;
pub const FRAME_BITS: usize = 12;
pub const STACK_SIZE: usize = 1024 * 64;
pub const STACK_SIZE: usize = 1024 * 8;
pub const STACK_SIZE_BITS: usize = 16;

pub const TIMER_FREQ: usize = CLOCK_FREQ;
pub const CPU_NUM: usize = 1;
pub const CPU_NUM: usize = 4;

pub const MMIO: &[(usize, usize)] = &[
(0x0010_0000, 0x00_2000), // VIRT_TEST/RTC in virt machine
Expand All @@ -38,10 +38,15 @@ pub const PROCESS_HEAP_MAX: usize = u32::MAX as usize + 1;
pub const TRAMPOLINE: usize = usize::MAX - 2 * FRAME_SIZE + 1;
pub const TRAP_CONTEXT_BASE: usize = TRAMPOLINE - FRAME_SIZE;

// 栈大小16k
pub const USER_KERNEL_STACK_SIZE: usize = 0x1000 * 4;
// 64KB
pub const USER_STACK_SIZE: usize = 0x50000;
// app内核栈大小
pub const USER_KERNEL_STACK_SIZE: usize = 0x1000 * 2;
// 用户栈大小
pub const USER_STACK_SIZE: usize = 0x50_000;

#[cfg(any(feature = "talloc", feature = "buddy"))]
pub const KERNEL_HEAP_SIZE: usize = 0x26_00000; // 16MB

pub const PIPE_BUF: usize = 4096;

// 进程数量/线程数量/描述符表大小限制
pub const MAX_PROCESS_NUM: usize = 1024;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/block_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl QemuBlockDevice {
pub fn new(device: VirtIOBlk<HalImpl, MmioTransport>) -> Self {
Self {
device: Mutex::new(device),
cache: Mutex::new(LruCache::new(NonZeroUsize::new(1024 * 12).unwrap())), // 48MB cache
cache: Mutex::new(LruCache::new(NonZeroUsize::new(512).unwrap())), // 4MB cache
dirty: Mutex::new(Vec::new()),
}
}
Expand Down
Loading

0 comments on commit 8c02205

Please sign in to comment.