-
-
Notifications
You must be signed in to change notification settings - Fork 168
feat(syscall): 添加syscall table的实现 #1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fslongjin
merged 2 commits into
DragonOS-Community:master
from
fslongjin:feat-add-syscall-table
May 13, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| .. _syscall_api: | ||
| .. _syscall: | ||
|
|
||
| 系统调用API | ||
| 系统调用 | ||
| ==================================== | ||
|
|
||
| .. toctree:: | ||
| :maxdepth: 1 | ||
| :caption: 目录 | ||
|
|
||
| intro | ||
| syscall_table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| 系统调用表实现方案 | ||
| ==================== | ||
|
|
||
| .. note:: | ||
| Author: longjin <[email protected]> | ||
|
|
||
| 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. 填充系统调用表 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [package] | ||
| name = "syscall_table_macros" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #![no_std] | ||
| #![deny(clippy::all)] | ||
|
|
||
| #[macro_export] | ||
| #[allow(clippy::crate_in_macro_def)] | ||
| macro_rules! declare_syscall { | ||
| ($nr:ident, $inner_handle:ident) => { | ||
| paste::paste! { | ||
| #[allow(non_upper_case_globals)] | ||
| #[link_section = ".syscall_table"] | ||
| #[used] | ||
| pub static [<HANDLE_ $nr>]: crate::syscall::table::SyscallHandle = crate::syscall::table::SyscallHandle { | ||
| nr: $nr, | ||
| inner_handle: &$inner_handle, | ||
| name: stringify!($nr), | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.