feat(syscall): 添加syscall table的实现 (#1164)

* feat(syscall): 添加syscall table的实现

- 实现syscall table
- 为syscall table适配write/writev、read和readv系统调用

---------

Signed-off-by: longjin <longjin@DragonOS.org>
This commit is contained in:
LoGin
2025-05-13 18:59:18 +08:00
committed by GitHub
parent 545bc2c346
commit b322121dd9
29 changed files with 874 additions and 227 deletions

View File

@ -17,7 +17,7 @@ import os
# -- Project information -----------------------------------------------------
project = 'DragonOS'
copyright = '2022-2024, DragonOS Community'
copyright = '2022-2025, DragonOS Community'
author = 'longjin'
github_org = 'DragonOS-Community'
github_repo = 'DragonOS'
@ -31,7 +31,12 @@ release = 'dev'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['myst_parser', 'sphinx_multiversion']
extensions = [
'myst_parser',
'sphinx_multiversion',
'sphinxcontrib.mermaid',
'sphinx.ext.extlinks',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

View File

@ -33,6 +33,7 @@
kernel/container/index
kernel/libs/index
kernel/trace/index
kernel/syscall/index
@ -42,11 +43,6 @@
userland/appdev/index
.. toctree::
:maxdepth: 1
:caption: 系统调用api文档
syscall_api/index
.. toctree::
:maxdepth: 1

View File

@ -67,7 +67,7 @@ Syscall: │ sys_open, sys_read, sys_write, sys_close, │
- `sys_fchmod`:修改文件权限(未实现)
- 其他系统调用接口(未实现)
&emsp;&emsp;关于接口的具体含义,可以参考 [DragonOS系统调用接口](../../syscall_api/index.rst)
&emsp;&emsp;关于接口的具体含义,可以参考Linux的相关文档
## 虚拟文件系统VFS

View File

@ -1,10 +1,10 @@
.. _syscall_api:
.. _syscall:
系统调用API
系统调用
====================================
.. toctree::
:maxdepth: 1
:caption: 目录
intro
syscall_table

View File

@ -0,0 +1,120 @@
系统调用表实现方案
====================
.. note::
Author: longjin <longjin@dragonos.org>
Date: 2025/05/13
概述
----
.. mermaid::
:align: center
:caption: 系统调用表架构
classDiagram
class Syscall {
<<trait>>
+num_args() usize
+handle(args, from_user) Result<usize, SystemError>
+entry_format(args) Vec<FormattedSyscallParam>
}
class SyscallHandle {
+nr: usize
+inner_handle: &dyn Syscall
}
class SyscallTable {
-entries: [Option<&SyscallHandle>; 512]
+get(nr) Option<&dyn Syscall>
}
Syscall <|.. SysXXXXXXHandle
SyscallHandle "1" *-- "1" Syscall
SyscallTable "1" *-- "512" SyscallHandle
相比于将原本集中在一个大match中的系统调用分发本方案采用基于trait和系统调用表的实现。主要优势包括
- 降低栈内存使用:避免单个大函数占用过多栈空间
- 支持参数打印:通过统一的参数格式化接口
- 更好的扩展性:新增系统调用无需修改分发逻辑
核心设计
--------
Syscall Trait
~~~~~~~~~~~~~
所有系统调用处理函数都需要实现 `Syscall` trait
.. code-block:: rust
pub trait Syscall: Send + Sync + 'static {
fn num_args(&self) -> usize;
fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError>;
fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam>;
}
- `num_args()`: 返回该系统调用需要的参数数量
- `handle()`: 实际执行系统调用处理
- `entry_format()`: 格式化参数用于调试打印
SyscallHandle
~~~~~~~~~~~~~
`SyscallHandle` 结构体将系统调用号与处理函数关联:
.. code-block:: rust
pub struct SyscallHandle {
pub nr: usize, // 系统调用号
pub inner_handle: &'static dyn Syscall, // 处理函数
pub name: &'static str,
}
SyscallTable
~~~~~~~~~~~~
`SyscallTable` 管理所有系统调用:
- 固定大小512项
- 编译时初始化
- 通过系统调用号快速查找处理函数
使用方式
--------
实现系统调用
~~~~~~~~~~~~
1. 定义实现``Syscall`` trait的结构体
2. 实现``handle()````entry_format()``方法
3. 使用``declare_syscall!``宏注册
参考实现:`sys_write.rs <sys_write_>`_
.. _sys_write:
https://github.com/DragonOS-Community/DragonOS/blob/master/kernel/src/filesystem/vfs/syscall/sys_write.rs
注册系统调用
~~~~~~~~~~~~
使用``declare_syscall!``宏注册系统调用:
.. code-block:: rust
syscall_table_macros::declare_syscall!(SYS_WRITE, SysWriteHandle);
参数说明:
1. 系统调用名称(用于生成符号)
2. 实现``Syscall`` trait的结构体
初始化流程
----------
1. 内核启动时调用``syscall_table_init()``
2. 从链接器符号``_syscall_table``加载所有注册的系统调用
3. 填充系统调用表

View File

@ -1,4 +1,5 @@
sphinx==5.0.2
myst-parser==0.18.0
sphinx-rtd-theme
sphinxcontrib-mermaid==1.0.0
git+https://git.mirrors.dragonos.org.cn/DragonOS-Community/sphinx-multiversion.git@5858b75#egg=sphinx-multiversion

View File

@ -1 +0,0 @@
# 简介