Skip to content

Commit

Permalink
fix: add doc
Browse files Browse the repository at this point in the history
Standardize some variable names.
  • Loading branch information
Godones committed Dec 3, 2023
1 parent 472c5d8 commit ead2770
Show file tree
Hide file tree
Showing 27 changed files with 270 additions and 83 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ members = [
"kernel",
"userlib",
"boot",
"modules/gmanager",
"modules/basemachine",
"dep/gmanager",
"dep/basemachine",
"apps/slint-helper",
"apps/cat",
"apps/sleep",
Expand Down
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ make vf2 LOG=WARN VF2=y SMP=2
// 这里smp=2 表示的是单核启动,对于u74-mc处理器,0号核不会被启动,从1号开始。
```

### Run cv1811h

```
make sdcard
// 制作fat32
make vf2 LOG=WARN CV1811h=y SMP=1
// 等待修复
```

## Run Unmatched

```
Expand All @@ -119,10 +110,6 @@ make unmatched LOG= UNMATCHED=y SMP=2
// 这里smp=2 表示的是单核启动,对于u74-mc处理器,0号核不会被启动,从1号开始。
```

目前cv1811h开发板可以上板启动,但是我们暂时没有处理其需要的特殊页表项。对于visionfive2和unmatched,可以启动并运行bash。



## GDB

1. `gdb-server`
Expand Down
2 changes: 1 addition & 1 deletion apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ ifeq ($(GUI),y)
slint \
memory-game \
printdemo \
egui \
egui
endif
2 changes: 1 addition & 1 deletion boot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
cfg-if = "1.0.0"
riscv = "0.10.0"
preprint = "0.1.0"
basemachine = { path = "../modules/basemachine" }
basemachine = { path = "../dep/basemachine" }
kernel = { version = "0.1.0", path = "../kernel" }


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/doc.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 开发文档集合

- [决赛规划](doc/target.md)
- [规划](doc/target.md)
- [开发日志](doc/开发日志.md)
- [系统架构](doc/系统架构.md)
- [设备管理](doc/设备管理.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/doc/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cd ./redis-3.2.9 && make CC=$(CC) MALLOC=libc # 编译redis
## Run

```
server: redis-server redis.conf &
server: redis-server ./bin/redis.conf &
bench : redis-benchmark -n 1000 -c 1
client: redis-cli
Expand Down
Binary file removed docs/doc/report/Alien+北京理工大学-new.pptx
Binary file not shown.
Binary file removed docs/doc/report/Alien+北京理工大学.pdf
Binary file not shown.
Binary file removed docs/doc/report/Alien+北京理工大学.pptx
Binary file not shown.
14 changes: 0 additions & 14 deletions docs/doc/开发日志.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,6 @@ http://nicephil.blinkenshell.org/my_book/ch07.html 比较细致

为了运行测试,我们修改了内核中部分代码,以获得更高的分数,在提交完评测后,我们将os恢复到之前的状态。为此,我们删除了一些将可执行文件包含到内核的代码。同时,对于init程序,其不再直接运行评测程序,而是直接启动bash。

qemu

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

starfive

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



unmatched

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



## 2023/8/2-8/4
Expand Down
6 changes: 3 additions & 3 deletions docs/doc/系统架构.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
- [Godones/preprint](https://github.com/Godones/preprint) **no_std外部模块打印**
- [Godones/doubly-linked-list](https://github.com/Godones/doubly-linked-list) **侵入式双向链表**
- [Godones/dbfs2](https://github.com/Godones/dbfs2) **高性能数据库文件系统**
- [module/basemachine](../../modules/basemachine) **设备树机器基本信息**
- [module/gmanager](../../modules/gmanager) **最小索引分配管理**
- [module/Input2event](../../modules/input2event) **virtio输入事件转换**
- [module/basemachine](../../dep/basemachine) **设备树机器基本信息**
- [module/gmanager](../../dep/gmanager) **最小索引分配管理**
- [module/Input2event](../../dep/input2event) **virtio输入事件转换**
- [module/pager](../modules/pager) **物理页管理**
- [module/plic](../modules/plic) **riscv平台控制器驱动**
- [module/simplegui](../modules/simplegui) **embedded graphics部件支持**
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Welcome to the Alien OS blog, where Alien OS maintainers announce development an

## Posts from 2023

**2023/12** [更多模块的改进](./stage-refactor.md)

**2023/11** [新的VFS实现和文件系统系统调用改进](./new-vfs.md)

**~2023/11** [之前的一些开发记录文档](./doc.md)
Expand Down
192 changes: 192 additions & 0 deletions docs/stage-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# 改进的阶段成果

自从上次发布第一篇[blog](./new-vfs.md)以来,我们对内核的部分继续做了很多改进。这主要包含了几部分的内容,首先是对文件系统的改进已经完成,其次是对网络相关的部分的进一步模块化,在用户态,我们新增了一个模块以方便使用`slint` gui框架开发。

## 文件系统的改进

目前内核将会使用位于[rvfs](https://github.com/os-module/rvfs)中的几个文件系统来构建系统中的目录结构

```rust
vfscore = { git = "https://github.com/os-module/rvfs.git", features = [
"linux_error",
] }
devfs = { git = "https://github.com/os-module/rvfs.git" }
dynfs = { git = "https://github.com/os-module/rvfs.git" }
ramfs = { git = "https://github.com/os-module/rvfs.git" }
fat-vfs = { git = "https://github.com/os-module/rvfs.git" }
```

目前系统的目录结构大致如下:

```
|
|--root
|--var
|--log
|--tmp
|--run
|--etc
|--dev
|--proc
|--sys
|--bin
|--tmp
```

在每个目录下,还有一些文件,这里的许多文件在系统启动时被创建,位于bin目录下的则是一些测试以及几个linux应用程序。在内部,我们对大多数文件系统相关的系统调用进行了重新实现,并统一了错误处理,大多数的文件系统调用如下面的形式:

```rust
#[syscall_func(79)]
pub fn sys_fstateat(
dir_fd: isize,
path: *const u8,
stat: *mut u8,
flag: usize,
) -> AlienResult<isize>
```

在`vfscore`内部,有自定义的`VfsError`,因为我们引入`linux_error` feature,因此`VfsError`实现了到`LinuxErrno` 的转换,使用`Into<T>` trait。在系统中,`AlienResult<T>` 被直接定义为`Result<T, AlienError>`, 这大大方便了代码的编写,我们不再需要进行`unwrap`或者额外的判断,一个`?`可以向上传递大多数的错误。

在注册所有设备之后,我们才会建立文件系统的目录结构,在这个过程中,所有的设备会形成一个设备文件,生成到`/dev/`目录下。这些设备实现了`VfsInode` 接口,因此对设备的访问现在与普通文件变得一致。

对内核文件系统相关系统调用的实现同时也促进我们对`rvfs`模块的改进,我们增加了一些有用的函数,并检查出一些bug,对于错误的处理也变得更加合理,这对后续新的文件系统支持大有益处。

## 网络模块

与文件系统类似,网络子系统的协议栈部分也被我们移出了内核代码中,内核中只保留网络系统调用实现。协议栈主要分为两部分,下层是网卡的抽象,上层是基于[smoltcp](https://github.com/smoltcp-rs/smoltcp)的实现。大部分的代码来自[Arceos](https://github.com/rcore-os/arceos) 和[smoltcp](https://github.com/smoltcp-rs/smoltcp),并做了一些修改,这个部分作为一个独立的仓库存在[simple-net](https://github.com/os-module/simple-net)。

对于网卡设备的抽象,我们定义了一个接口:

```rust
pub trait NetBufOps: Any {
fn packet(&self) -> &[u8];
fn packet_mut(&mut self) -> &mut [u8];
fn packet_len(&self) -> usize;
}

/// Operations that require a network device (NIC) driver to implement.
pub trait NetDriverOps: Send + Sync {
fn medium(&self) -> Medium;
/// The ethernet address of the NIC.
fn mac_address(&self) -> EthernetAddress;

/// Whether can transmit packets.
fn can_transmit(&self) -> bool;

/// Whether can receive packets.
fn can_receive(&self) -> bool;

/// Size of the receive queue.
fn rx_queue_size(&self) -> usize;

/// Size of the transmit queue.
fn tx_queue_size(&self) -> usize;

/// Gives back the `rx_buf` to the receive queue for later receiving.
///
/// `rx_buf` should be the same as the one returned by
/// [`NetDriverOps::receive`].
fn recycle_rx_buffer(&mut self, rx_buf: Box<dyn NetBufOps>) -> Result<(), NetError>;

/// Poll the transmit queue and gives back the buffers for previous transmiting.
/// returns [`DevResult`].
fn recycle_tx_buffers(&mut self) -> Result<(), NetError>;

/// Transmits a packet in the buffer to the network, without blocking,
/// returns [`DevResult`].
fn transmit(&mut self, tx_buf: Box<dyn NetBufOps>) -> Result<(), NetError>;

/// Receives a packet from the network and store it in the [`NetBuf`],
/// returns the buffer.
///
/// Before receiving, the driver should have already populated some buffers
/// in the receive queue by [`NetDriverOps::recycle_rx_buffer`].
///
/// If currently no incomming packets, returns an error with type
/// [`DevError::Again`].
fn receive(&mut self) -> Result<Box<dyn NetBufOps>, NetError>;

/// Allocate a memory buffer of a specified size for network transmission,
/// returns [`DevResult`]
fn alloc_tx_buffer(&mut self, size: usize) -> Result<Box<dyn NetBufOps>, NetError>;
}
```

这个接口定义了网卡需要支持的功能,我们在此基础之上实现了`loopback``virtio-net`两个设备,`loopback`设备所作的工作非常简单,只是把发送的包原封不动地放到队列中。`virtio-net`设备是对使用`virtio`协议的网卡设备的封装,其使用了[virtio-drivers ](https://github.com/rcore-os/virtio-drivers) 来完成更多的工作。

[netcore](https://github.com/os-module/simple-net)中,我们为`smoltcp` 实现一些定义的接口,从而可以驱动`smoltcp`协议栈工作。在此基础上,实现了`tcp``udp`两个数据结构。

虽然协议栈目前被移出了内核之外,但是模块化程度依然不够,而且不够灵活。目前`netcore`的入口函数形式如下:

```rust
pub fn init_net(
device: Box<dyn NetDriverOps>,
kernel_func: Arc<dyn KernelNetFunc>,
ip: IpAddress,
gate_way: IpAddress,
test: bool,
)
```

入口函数会初始化一些必要的数据结构,这些数据结构目前以全局变量的形式存在:

```rust
pub static NET_INTERFACE: Once<NetInterfaceWrapper> = Once::new();
pub static SOCKET_SET: Lazy<SocketSetWrapper> = Lazy::new(SocketSetWrapper::new);
pub static LISTENING_TABLE: Lazy<ListenTable> = Lazy::new(ListenTable::new);
```

这导致了我们只能使用一个网卡设备,但对于准备的大多数测试,我们只能在回环设备下完成。这导致了qemu的`virtio`设备无法被有效利用。

初次之外,对于互斥锁的使用,我们直接引用了内核中使用的`kernel-sync`:

```
[dependencies]
kernel-sync = { git = "https://github.com/os-module/kernel-sync.git" }
```

这显然限制了其灵活性,因此在后面的改进当中,我们需要进一步的去除全局变量的使用,支持多个网卡设备,同时将互斥锁的使用更换为`lock-api`

## virt2slint

我们在内核中添加了一些GUI相关的支持。要在内核中添加GUI设备,我们需要添加一些qemu参数:

```
QEMU_ARGS += -device virtio-gpu-device \
-device virtio-tablet-device \
-device virtio-keyboard-device
```

在内核中,我们使用已有的`virtio`驱动程序添加了对这些设备的使用。并提供了两个简单的系统调用来向用户态提供显示缓冲区和输入事件:

```rust
#[syscall_func(2000)]
pub fn sys_framebuffer() -> isize
#[syscall_func(2001)]
pub fn sys_framebuffer_flush() -> isize
#[syscall_func(2002)]
pub fn sys_event_get(event_buf: *mut u64, len: usize) -> isize
```

在用户态,为了简化开发难度,我们使用了[slint](https://slint.dev/)框架来提供gui的开发,对于嵌入式系统,这需要提供一些底层接口的实现,比如事件的传递,缓冲区的绘制。

`virt2slint` 这个crate提供了`virtio`的输入事件到`slint`的输入事件的转换支持,有了这个支持,就可以方便地驱动起一些嵌入式系统的GUI了。

apps目录下,我们移植了`slint`官方提供的几个嵌入式应用程序:

```
printdemo
memory-game
slint
sysinfo
todo
```

## 杂项

除了上面这些较大的改动之外,内核中还有一些其它的改进,比如一些无用代码的删除以及一些重构。

## 下一步

目前我们已经完成了内核中已有模块的大多数重构,但内核中还有一些很多小的模块,这些模块因为其耦合性导致修改起来更加复杂,因此可能需要从其周围的依赖进行逐步的修改和删减。其中很重要的部分是页表的管理,这部分涉及到了写时复制、懒分配等比较复杂的策略。

6 changes: 3 additions & 3 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ rslab = { version = "0.2.1", optional = true }
syscall-table = { git = "https://github.com/os-module/syscall-table.git" }
plic = { git = "https://github.com/os-module/plic" }
pager = { git = "https://github.com/os-module/pager", default-features = false, optional = true }
kernel-sync = { git = "https://github.com/os-module/kernel-sync.git" }
kernel-sync = { git = "https://github.com/os-module/kernel-sync.git",features = ["lock_api","riscv"] }
pconst = { git = "https://github.com/os-module/pconst.git", features = [
"trick",
] }
gmanager = { path = "../modules/gmanager" }
basemachine = { path = "../modules/basemachine" }
gmanager = { path = "../dep/gmanager" }
basemachine = { path = "../dep/basemachine" }
uart16550 = { version = "0.0.1", optional = true }
uart8250 = { git = "https://github.com/os-module/uart-rs.git", optional = true }
smpscheduler = { git = "https://github.com/os-module/smpscheduler.git" }
Expand Down
8 changes: 4 additions & 4 deletions kernel/src/driver/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ impl UartDevice for Uart {

impl DeviceBase for Uart {
fn hand_irq(&self) {
let mut inner = self.inner.lock();
loop {
let mut inner = self.inner.lock();
if let Some(c) = inner.0._read() {
inner.1.rx_buf.push_back(c);
if !inner.1.wait_queue.is_empty() {
let process = inner.1.wait_queue.pop_front().unwrap();
process.update_state(TaskState::Ready);
GLOBAL_TASK_MANAGER.add_task(Arc::new(FifoTask::new(process)));
let task = inner.1.wait_queue.pop_front().unwrap();
task.update_state(TaskState::Ready);
GLOBAL_TASK_MANAGER.add_task(Arc::new(FifoTask::new(task)));
}
} else {
break;
Expand Down
Loading

0 comments on commit ead2770

Please sign in to comment.